/* ================================================================
   DESIGN SYSTEM — CSS Custom Properties
   Project: 3D Browser Simulations
   ================================================================ */
/* Google Fonts (Inter) is injected asynchronously by components.js
   to avoid render-blocking and improve LCP (Core Web Vitals).       */

:root {
  /* ── Colors ──────────────────────────────────────────────────── */
  --color-bg: #08090e;
  --color-surface: #0f1623;
  --color-surface-2: #18202f;
  --color-surface-3: #212d42;
  --color-border: rgba(255, 255, 255, 0.07);
  --color-border-hover: rgba(96, 165, 250, 0.45);

  --color-primary: #60a5fa;
  --color-accent: #a78bfa;
  --color-accent-2: #34d399;
  --color-warning: #fbbf24;
  --color-danger: #f87171;

  --color-text: #e2e8f0;
  /* #94a3b8 on #0f1623 = 6.95:1 — passes WCAG AA (was #64748b = 3.86:1) */
  --color-muted: #94a3b8;
  --color-muted-2: #b0bccc;

  /* ── Category accent colours ─────────────────────────────────── */
  --cat-space: #3b82f6;
  --cat-space-glow: rgba(59, 130, 246, 0.18);
  --cat-physics: #f97316;
  --cat-physics-glow: rgba(249, 115, 22, 0.18);
  --cat-nature: #22c55e;
  --cat-nature-glow: rgba(34, 197, 94, 0.18);
  --cat-life: #06b6d4;
  --cat-life-glow: rgba(6, 182, 212, 0.18);
  --cat-chaos: #8b5cf6;
  --cat-chaos-glow: rgba(139, 92, 246, 0.18);
  --cat-math: #60a5fa;
  --cat-math-glow: rgba(96, 165, 250, 0.15);
  --cat-algorithms: #a3a3a3;
  --cat-algorithms-glow: rgba(163, 163, 163, 0.12);
  --cat-geometry: #93c5fd;
  --cat-geometry-glow: rgba(147, 197, 253, 0.14);
  --cat-chemistry: #c084fc;
  --cat-chemistry-glow: rgba(192, 132, 252, 0.18);
  --cat-sound: #fbbf24;
  --cat-sound-glow: rgba(251, 191, 36, 0.18);
  --cat-society: #38bdf8;
  --cat-society-glow: rgba(56, 189, 248, 0.18);
  --cat-quantum: #818cf8;
  --cat-quantum-glow: rgba(129, 140, 248, 0.18);

  /* ── Typography ──────────────────────────────────────────────── */
  --font-main: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Cascadia Code", monospace;

  --text-xs: clamp(0.7rem, 1.4vw, 0.75rem);
  --text-sm: clamp(0.8rem, 1.7vw, 0.875rem);
  --text-base: clamp(0.9rem, 2vw, 1rem);
  --text-lg: clamp(1rem, 2.3vw, 1.125rem);
  --text-xl: clamp(1.1rem, 2.8vw, 1.375rem);
  --text-2xl: clamp(1.4rem, 3.5vw, 1.875rem);
  --text-3xl: clamp(1.8rem, 4.5vw, 2.5rem);
  --text-4xl: clamp(2.5rem, 6vw, 3.5rem);
  --text-5xl: clamp(3rem, 8vw, 5rem);

  --leading-tight: 1.2;
  --leading-normal: 1.6;
  --leading-loose: 1.8;

  /* ── Border radius ───────────────────────────────────────────── */
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-2xl: 32px;
  --radius-full: 9999px;

  /* ── Shadows ─────────────────────────────────────────────────── */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.6);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.7);
  --shadow-glow: 0 0 20px rgba(96, 165, 250, 0.18);
  --shadow-glow-md: 0 0 40px rgba(96, 165, 250, 0.25);
  --shadow-glow-lg: 0 0 80px rgba(96, 165, 250, 0.3);

  /* ── Transitions ─────────────────────────────────────────────── */
  --ease-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  --ease-base: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  --ease-slow: 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  --ease-bounce: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);

  /* ── Layout ──────────────────────────────────────────────────── */
  --max-width: 1300px;
  --navbar-h: 64px;
}

/* ── Light theme ───────────────────────────────────────────────── */
[data-theme="light"] {
  --color-bg: #f0f4f8;
  --color-surface: #ffffff;
  --color-surface-2: #f8fafc;
  --color-surface-3: #e8f0fe;
  --color-border: rgba(0, 0, 0, 0.07);
  --color-border-hover: rgba(37, 99, 235, 0.4);

  --color-primary: #2563eb;
  --color-accent: #7c3aed;
  --color-accent-2: #059669;

  --color-text: #0f172a;
  /* #4b5563 on #ffffff = 6.75:1 — passes WCAG AA (was #64748b = 4.34:1 fail) */
  --color-muted: #4b5563;
  --color-muted-2: #374151;

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.15);
  --shadow-glow: 0 0 20px rgba(37, 99, 235, 0.12);
  --shadow-glow-md: 0 0 40px rgba(37, 99, 235, 0.18);

  --cat-space-glow: rgba(59, 130, 246, 0.1);
  --cat-physics-glow: rgba(249, 115, 22, 0.1);
  --cat-nature-glow: rgba(34, 197, 94, 0.1);
  --cat-life-glow: rgba(6, 182, 212, 0.1);
  --cat-chaos-glow: rgba(139, 92, 246, 0.1);
  --cat-math-glow: rgba(96, 165, 250, 0.1);
  --cat-algorithms-glow: rgba(100, 100, 100, 0.07);
  --cat-geometry-glow: rgba(147, 197, 253, 0.1);
  --cat-chemistry-glow: rgba(192, 132, 252, 0.1);
  --cat-sound-glow: rgba(251, 191, 36, 0.1);
  --cat-society-glow: rgba(56, 189, 248, 0.1);
  --cat-quantum-glow: rgba(129, 140, 248, 0.1);
}

/* ── Navbar height reservation (prevents CLS — Core Web Vitals) ──
   The fixed navbar is injected by a deferred script. Reserving the
   padding-top here (before JS runs) eliminates the layout shift.
   Simulation pages use position:fixed canvas so the gap is hidden. */
body {
  padding-top: var(--navbar-h, 64px);
}
/* Embed mode (iframe, ?embed=true) — components.js sets paddingTop:0 */
body.embed-mode {
  padding-top: 0;
}

/* ── Skip navigation link (WCAG 2.1 AA — SC 2.4.1) ─────────────── */
.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  padding: 0.75rem 1.5rem;
  background: var(--color-primary, #60a5fa);
  color: #000;
  font-weight: 600;
  font-size: var(--text-sm, 0.875rem);
  text-decoration: none;
  border-radius: 0 0 var(--radius-md, 12px) 0;
  z-index: 10000;
  transition: top 0.15s ease;
}
.skip-link:focus {
  top: 0;
}

/* ── Focus indicators (WCAG 2.1 AA — SC 2.4.7) ───────────────────
   Visible outline for all keyboard-focusable elements.
   :focus-visible avoids the ring on mouse clicks.                  */
:focus-visible {
  outline: 2px solid var(--color-primary, #60a5fa);
  outline-offset: 3px;
  border-radius: 3px;
}
:focus:not(:focus-visible) {
  outline: none;
}

/* ── Scroll entrance animations ────────────────────────────────── */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(28px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* Elements that should animate in when scrolled into view */
.reveal {
  opacity: 0;
  transform: translateY(28px);
  transition:
    opacity 0.55s var(--ease-base, cubic-bezier(0.4, 0, 0.2, 1)),
    transform 0.55s var(--ease-base, cubic-bezier(0.4, 0, 0.2, 1));
}

.reveal.visible {
  opacity: 1;
  transform: translateY(0);
}

/* Stagger siblings inside a grid/flex parent */
.reveal:nth-child(2) {
  transition-delay: 0.05s;
}
.reveal:nth-child(3) {
  transition-delay: 0.1s;
}
.reveal:nth-child(4) {
  transition-delay: 0.15s;
}
.reveal:nth-child(5) {
  transition-delay: 0.2s;
}
.reveal:nth-child(6) {
  transition-delay: 0.25s;
}
.reveal:nth-child(7) {
  transition-delay: 0.3s;
}
.reveal:nth-child(8) {
  transition-delay: 0.35s;
}
.reveal:nth-child(n + 9) {
  transition-delay: 0.4s;
}

/* ── Article reading styles ─────────────────────────────────────── */
.article-body {
  max-width: 68ch;
  margin-left: auto;
  margin-right: auto;
  font-size: var(--text-lg);
  line-height: var(--leading-loose, 1.8);
  color: var(--color-text);
}

.article-body h1,
.article-body h2,
.article-body h3,
.article-body h4 {
  font-weight: 700;
  line-height: var(--leading-tight, 1.2);
  letter-spacing: -0.025em;
  color: var(--color-text);
  margin-top: 2.5em;
  margin-bottom: 0.65em;
}

.article-body h1 {
  font-size: var(--text-4xl);
  margin-top: 0;
}
.article-body h2 {
  font-size: var(--text-3xl);
}
.article-body h3 {
  font-size: var(--text-2xl);
}
.article-body h4 {
  font-size: var(--text-xl);
  letter-spacing: 0;
}

.article-body p {
  margin-bottom: 1.4em;
}
.article-body ul,
.article-body ol {
  margin-bottom: 1.4em;
  padding-left: 1.8em;
}
.article-body li {
  margin-bottom: 0.4em;
  line-height: var(--leading-loose, 1.8);
}

.article-body a {
  color: var(--color-primary);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  transition: color var(--ease-fast, 0.15s);
}
.article-body a:hover {
  color: var(--color-accent);
}

.article-body blockquote {
  border-left: 3px solid var(--color-primary);
  margin: 1.8em 0;
  padding: 0.8em 1.4em;
  background: var(--color-surface);
  border-radius: 0 var(--radius-md, 12px) var(--radius-md, 12px) 0;
  color: var(--color-muted-2);
  font-style: italic;
}

.article-body pre {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md, 12px);
  padding: 1.25em 1.5em;
  overflow-x: auto;
  margin-bottom: 1.6em;
}

.article-body code {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

.article-body pre code {
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: 1.7;
}

.article-body :not(pre) > code {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm, 6px);
  padding: 0.15em 0.45em;
  color: var(--color-accent);
}

.article-body hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 3em 0;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  font-size: var(--text-sm);
  color: var(--color-muted-2);
  margin-bottom: 2.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.article-meta-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

/* ── Print styles ───────────────────────────────────────────────── */
@media print {
  /* Hide UI chrome */
  #navbar-root,
  #footer-root,
  .skip-link,
  .sim-tags,
  .btn,
  .lang-switcher,
  .navbar-search-btn,
  .mobile-menu-toggle,
  .filter-section,
  .teacher-banner {
    display: none !important;
  }

  /* Page setup */
  body {
    background: #ffffff !important;
    color: #000000 !important;
    font-size: 12pt;
    line-height: 1.5;
  }

  a {
    color: #000000 !important;
    text-decoration: underline;
  }
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #444;
  }
  a[href^="#"]::after,
  a[href^="javascript:"]::after {
    content: "";
  }

  h1,
  h2,
  h3,
  h4 {
    color: #000000 !important;
    page-break-after: avoid;
  }

  p,
  blockquote,
  pre {
    page-break-inside: avoid;
  }

  .article-body {
    max-width: 100%;
    font-size: 11pt;
    line-height: 1.6;
    color: #000000;
  }

  .article-body pre,
  .article-body blockquote {
    background: #f5f5f5 !important;
    border-color: #cccccc !important;
    color: #000000 !important;
  }

  .article-body :not(pre) > code {
    background: #eeeeee !important;
    color: #000000 !important;
    border-color: #cccccc !important;
  }

  img {
    max-width: 100%;
    page-break-inside: avoid;
  }

  @page {
    margin: 2cm;
  }
}

/* ── Reduced motion ────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .reveal,
  .reveal.visible {
    opacity: 1 !important;
    transform: none !important;
    transition: none !important;
  }
}

/* ── Reduced-motion simulation overlay ─────────────────────────── */
/* Injected by components.js when window._prefersReducedMotion is set */
.sim-reduced-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: rgba(8, 9, 14, 0.88);
  color: var(--color-muted-2);
  text-align: center;
  padding: 2rem;
  z-index: 20;
  pointer-events: none;
  border-radius: inherit;
}
[data-theme="light"] .sim-reduced-overlay {
  background: rgba(240, 244, 248, 0.88);
}
.sim-reduced-overlay__icon {
  font-size: 3rem;
  line-height: 1;
}
.sim-reduced-overlay__title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--color-text);
}
.sim-reduced-overlay__sub {
  font-size: var(--text-sm);
  color: var(--color-muted);
  max-width: 34ch;
  line-height: 1.65;
}
.sim-reduced-overlay__btn {
  all: unset;
  margin-top: 0.5rem;
  cursor: pointer;
  padding: 0.55rem 1.4rem;
  border: 1px solid var(--color-border-hover);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-primary);
  transition:
    background var(--ease-fast),
    color var(--ease-fast);
  pointer-events: auto;
}
.sim-reduced-overlay__btn:hover,
.sim-reduced-overlay__btn:focus-visible {
  background: rgba(96, 165, 250, 0.12);
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ── Responsive breakpoints ────────────────────────────────────── */
/* 1400px — wide screens: slightly wider containers                */
@media (max-width: 1400px) {
  :root {
    --max-width: 1200px;
  }
}

/* 1200px — laptop / narrow desktop                                */
@media (max-width: 1200px) {
  :root {
    --max-width: 1060px;
  }
  .article-body {
    font-size: calc(var(--text-lg) * 0.97);
  }
}

/* 900px — tablet landscape / small laptop                        */
@media (max-width: 900px) {
  :root {
    --max-width: 100%;
    --text-4xl: clamp(1.8rem, 7vw, 2.5rem);
    --text-3xl: clamp(1.5rem, 6vw, 2rem);
    --text-2xl: clamp(1.2rem, 5vw, 1.6rem);
  }
  .article-body {
    max-width: 100%;
    padding-left: 0.25rem;
    padding-right: 0.25rem;
  }
}

/* 600px — mobile portrait                                         */
@media (max-width: 600px) {
  :root {
    --text-4xl: clamp(1.6rem, 8vw, 2.2rem);
    --text-3xl: clamp(1.3rem, 6vw, 1.7rem);
    --text-2xl: clamp(1.1rem, 5vw, 1.4rem);
    --text-xl: clamp(1rem, 4vw, 1.2rem);
  }
  .article-body pre {
    font-size: 0.82rem;
    padding: 1rem;
  }
}

/* 400px — very small phones                                       */
@media (max-width: 400px) {
  :root {
    --text-4xl: clamp(1.4rem, 9vw, 1.9rem);
  }
}

/* ── Touch-friendly interactive elements ──────────────────────── */
@media (pointer: coarse) {
  button,
  [role="button"],
  .cat-fb,
  .cat-sort-sel,
  .sim-hud-btn,
  a.btn,
  .btn {
    min-height: 44px;
    min-width: 44px;
  }
}

/* ── Safe-area (notch / home-bar) ──────────────────────────────── */
@supports (padding: env(safe-area-inset-bottom)) {
  body {
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  .controls-hint {
    bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

/* ── Code syntax token colors ────────────────────────────────────
   Used in article pages via <span class="code-*"> wrappers.
   Matches the One Dark Pro / GitHub Dark palette.              */
.code-comment {
  color: #6b7280;
  font-style: italic;
}
.code-keyword {
  color: #c084fc;
  font-weight: 600;
}
.code-fn {
  color: #60a5fa;
}
.code-num {
  color: #34d399;
}
.code-str {
  color: #fbbf24;
}
.code-type {
  color: #f97316;
  font-weight: 600;
}
.code-op {
  color: #94a3b8;
}
.code-const {
  color: #e879f9;
  font-weight: 600;
}
.code-var {
  color: #e2e8f0;
}
.code-param {
  color: #fb923c;
}

/* Light theme overrides for code tokens */
[data-theme="light"] .code-comment {
  color: #6b7280;
}
[data-theme="light"] .code-keyword {
  color: #7c3aed;
}
[data-theme="light"] .code-fn {
  color: #1d4ed8;
}
[data-theme="light"] .code-num {
  color: #059669;
}
[data-theme="light"] .code-str {
  color: #b45309;
}
[data-theme="light"] .code-type {
  color: #c2410c;
}
[data-theme="light"] .code-op {
  color: #475569;
}
[data-theme="light"] .code-const {
  color: #9333ea;
}
[data-theme="light"] .code-var {
  color: #0f172a;
}
[data-theme="light"] .code-param {
  color: #c2410c;
}
