/* ---------------------------------------------------------------------
   Design system: dark charcoal ground, warm off-white text, gold and
   teal spent sparingly on structure (badges, active states, buttons) —
   never as blanket body-text color. Dark-only by design — this is the
   site's one visual identity, not a default waiting on a light variant.
--------------------------------------------------------------------- */
:root {
  /* Named palette — checked against WCAG contrast, not eyeballed:
     bg/ink 14:1, gold/teal/good/danger all 6.5:1+ on bg. */
  --paper: #1c1812;          /* page background — warm near-black */
  --paper-card: #24201a;     /* lighter surface: nav, playground, cards */
  --paper-recessed: #15120d; /* insets: editor gutter, code chips */
  --rule: #655640;           /* borders on interactive elements — buttons, inputs */
  --rule-soft: #322c22;      /* faint dividers — decorative only */
  --grid-line: #e8dcc014;    /* faint graph-paper line, ~8% opacity */

  --ink: #f0e6d2;            /* primary text — 14:1 on bg */
  --ink-soft: #c2b69c;       /* secondary text — 8.8:1 on bg */
  --ink-faint: #8f7f5f80;    /* tertiary / disabled, non-text only */

  --gold: #e8b84b;           /* accent — badges, buttons, active states — 9.6:1 on bg */
  --gold-fill: #7a5c24;      /* accent — visible border/fill */
  --gold-fill-soft: #e8b84b26;

  --teal: #6ee0d8;           /* accent — links, quiz labels — 11.2:1 on bg */
  --teal-fill: #2f6b66;      /* accent — visible border/fill */
  --teal-fill-soft: #6ee0d826;

  --good: #8fd47a;           /* semantic: correct answer — kept separate from the two accents */
  --danger: #f0806a;         /* semantic: error / incorrect */
  --danger-fill: #f0806a1a;

  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Consolas, monospace;
  --serif-body: "Source Serif 4", Georgia, "Times New Roman", serif;

  --nav-h: 60px;
  --space-1: 6px;
  --space-2: 12px;
  --space-3: 20px;
  --space-4: 32px;
  --space-5: 48px;
}

* { box-sizing: border-box; }

/* Smooth crossfade between lessons on browsers that support cross-document
   view transitions (Chrome/Edge, and increasingly others). Pure CSS,
   zero JS, and a no-op fallback elsewhere — just a normal navigation. */
@view-transition {
  navigation: auto;
}

html {
  overflow-x: hidden; /* hard guard: the page must never scroll sideways */
}
html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif-body);
}

.reading-pane {
  /* Grid items default to min-width:auto, which refuses to shrink below
     the content's natural min-content width — one long word/snippet is
     enough to blow the whole 1fr column (and the page) wider than the
     screen. min-width:0 is the standard fix. */
  min-width: 0;
  /* A quiet graph-paper grid — the same coordinate grid Module 01
     teaches — kept to a single ~12%-opacity line so it reads as texture,
     not decoration you actually notice. */
  background-color: var(--paper);
  background-image:
    repeating-linear-gradient(0deg, var(--grid-line) 0 1px, transparent 1px 36px),
    repeating-linear-gradient(90deg, var(--grid-line) 0 1px, transparent 1px 36px);
  padding: var(--space-4) 36px var(--space-5);
  overflow-y: auto;
  overflow-x: hidden;
}

::selection { background: var(--gold-fill); color: var(--ink); }

/* Base link style — catches any plain <a> that isn't one of the
   deliberately-styled UI links below (nav, footer, buttons, pills all
   have their own higher-specificity rules and are unaffected by this).
   No default-blue, no permanent underline; underline only on hover. */
a { color: var(--teal); text-decoration: none; }
a:hover { text-decoration: underline; }
a:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

/* ---------- nav ---------- */
.lesson-nav {
  display: flex; align-items: center; gap: 18px;
  height: var(--nav-h);
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--rule);
  /* fixed, not sticky: on iOS, a sticky header still gets dragged down
     during the elastic pull-to-refresh overscroll bounce at the top of
     the page. Fixed anchors to the real viewport and ignores it. */
  position: fixed; top: 0; left: 0; right: 0;
  background: var(--paper-card); z-index: 10;
}
.module-badge {
  font-family: var(--mono); font-size: 11.5px; font-weight: 700;
  letter-spacing: .06em; text-transform: uppercase;
  color: var(--gold); background: var(--gold-fill-soft);
  border: 1px solid var(--gold-fill); border-radius: 6px; padding: 5px 10px;
  white-space: nowrap; text-decoration: none; display: inline-block;
}
.module-badge:hover { background: var(--gold-fill); }
.module-badge:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }
.module-badge.elective {
  color: var(--teal); background: var(--teal-fill-soft); border-color: var(--teal-fill);
}
.module-badge.elective:hover { background: var(--teal-fill); }
.lesson-nav h1 {
  font-family: var(--mono); font-weight: 700; font-size: 14.5px;
  margin: 0; flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--ink);
}
.nav-arrows { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.nav-arrow {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 6px; flex-shrink: 0;
  font-family: var(--mono); font-size: 16px; line-height: 1;
  color: var(--ink-soft); text-decoration: none; border: 1px solid transparent;
}
.nav-arrow:hover { background: var(--paper); border-color: var(--rule); color: var(--ink); }
.nav-arrow:focus-visible { outline: 2px solid var(--teal); outline-offset: 1px; }
.nav-arrow.disabled { color: var(--ink-faint); }

.progress-dots { display: flex; gap: 6px; flex-shrink: 0; }
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: transparent; border: 1.5px solid var(--rule);
}
.dot.active { background: var(--gold); border-color: var(--gold); }

@media (max-width: 560px) {
  .lesson-nav h1 { display: none; }
  .module-badge { flex-shrink: 0; }
}

/* ---------- desktop two-pane layout ---------- */
.lesson-grid {
  /* Nav is now position:fixed, so it no longer reserves its own space
     in normal flow — push content down by exactly its height instead. */
  margin-top: var(--nav-h);
  display: grid; grid-template-columns: 1fr 1fr;
  min-height: calc(100vh - var(--nav-h));
}

.concept h2 {
  font-family: var(--mono); font-weight: 700; font-size: 16.5px;
  letter-spacing: -.01em;
  margin: 40px 0 12px; text-wrap: balance; color: var(--ink);
}
.concept h2:first-child { margin-top: 0; }
.concept p {
  line-height: 1.7; color: var(--ink); font-size: 15.5px; max-width: 60ch;
  margin: 0 0 18px;
}
.concept p:last-child { margin-bottom: 0; }
/* Emphasis stays ink — weight and style carry it, not color. Color is
   spent on structure (badges, buttons, active states) elsewhere. */
.concept strong { color: var(--ink); font-weight: 700; }
.concept em { color: var(--ink); font-style: italic; font-weight: 600; }
.concept code {
  font-family: var(--mono); font-size: .85em; background: var(--paper-recessed);
  padding: 1px 6px; border-radius: 3px; color: var(--ink); border: 1px solid var(--rule-soft);
  overflow-wrap: anywhere; /* a long unbroken snippet must wrap, never widen the page */
}

/* The "graph first" pattern: a plain math diagram before the code that
   implements it, so the shader reads as a translation of something
   already understood, not a new thing to decode from scratch. */
.math-graph {
  margin: 20px 0 28px; padding: 20px; border: 1px solid var(--rule-soft);
  border-radius: 10px; background: var(--paper-card);
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.math-graph svg { max-width: 100%; height: auto; }
.math-graph figcaption {
  font-family: var(--serif-body); font-style: italic; font-size: 13.5px;
  color: var(--ink-soft); text-align: center; max-width: 42ch; margin: 0;
}
.math-graph figcaption code { font-style: normal; }

.quiz {
  margin-top: var(--space-4); padding: 22px 24px; border: 1px solid var(--rule); border-radius: 10px;
  background: var(--paper-card);
}
.quiz h3 {
  font-family: var(--mono); font-size: 11.5px; letter-spacing: .06em; text-transform: uppercase;
  margin: 0 0 10px; color: var(--teal); font-weight: 700;
}
.quiz p { font-size: 14.5px; color: var(--ink); font-family: var(--serif-body); line-height: 1.6; margin: 0; }
.quiz-options { display: flex; flex-direction: column; gap: 10px; margin-top: var(--space-2); }
.quiz-options button {
  text-align: left; padding: 12px 16px; border-radius: 8px;
  border: 1px solid var(--rule); background: var(--paper); color: var(--ink);
  cursor: pointer; font-family: var(--serif-body); font-size: 14px;
  transition: background .12s ease, border-color .12s ease;
}
.quiz-options button:hover { border-color: var(--teal); background: var(--teal-fill-soft); }
.quiz-options button:focus-visible { outline: 2px solid var(--teal); outline-offset: 1px; }
.quiz p.quiz-feedback { margin-top: 18px; font-size: 13.5px; font-family: var(--mono); line-height: 1.5; }
.quiz-feedback.correct { color: var(--good); }
.quiz-feedback.incorrect { color: var(--danger); }

/* ---------- related video ---------- */
/* Facade pattern: a plain thumbnail + play button stand in for the real
   YouTube iframe until clicked, so browsing lessons never pays for an
   embed's JS/network weight up front — same instinct as Module E1. */
.related-video {
  margin-top: var(--space-4); padding: 22px 24px; border: 1px solid var(--rule); border-radius: 10px;
  background: var(--paper-card);
}
.related-video h3 {
  font-family: var(--mono); font-size: 11.5px; letter-spacing: .06em; text-transform: uppercase;
  margin: 0 0 14px; color: var(--gold); font-weight: 700;
}
.video-thumb {
  position: relative; display: block; width: 100%; padding: 0; margin: 0; border: none; cursor: pointer;
  aspect-ratio: 16 / 9; border-radius: 8px; overflow: hidden; background: var(--paper-recessed);
}
.video-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.play-badge {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 56px; height: 56px; border-radius: 50%;
  background: #15120dcc; border: 1.5px solid var(--gold); color: var(--gold);
  font-size: 18px; display: flex; align-items: center; justify-content: center;
  transition: background .12s ease, transform .12s ease;
}
.video-thumb:hover .play-badge, .video-thumb:focus-visible .play-badge {
  background: var(--gold); color: var(--paper); transform: translate(-50%, -50%) scale(1.08);
}
.video-thumb:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.video-frame { width: 100%; aspect-ratio: 16 / 9; border-radius: 8px; border: none; display: block; }
.video-credit { margin: 12px 0 0; font-size: 13px; color: var(--ink-soft); font-family: var(--serif-body); }
.video-credit span { color: var(--teal); }

.lesson-footer {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-2);
  margin-top: var(--space-5); padding-top: var(--space-3);
  border-top: 1px solid var(--rule-soft);
  font-family: var(--mono); font-size: 12.5px;
}
.lesson-footer a { color: var(--teal); text-decoration: none; font-weight: 500; }
.lesson-footer a:hover { text-decoration: underline; }
.lesson-footer a:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
.lesson-footer .placeholder { color: var(--ink-soft); }
.lesson-footer a.test-link { color: var(--gold); }
.lesson-footer a.test-link:focus-visible { outline-color: var(--gold); }

/* ---------- playground pane ---------- */
.playground-pane {
  min-width: 0; /* same grid-blowout guard as .reading-pane */
  display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3);
  border-left: 1px solid var(--rule); background: var(--paper-card);
}
.canvas-wrap {
  aspect-ratio: 16 / 9; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--rule);
  box-shadow: 0 0 0 1px #00000040, 0 8px 24px -10px #00000080;
}
#canvas { width: 100%; height: 100%; display: block; }

.controls-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.controls-panel { display: flex; flex-wrap: wrap; gap: 14px; flex: 1; }
.control-row {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--mono); font-size: 12px; color: var(--ink-soft);
}
.controls-empty { font-size: 12px; color: var(--ink-soft); font-family: var(--mono); margin: 0; }

.ghost-btn {
  font-family: var(--mono); font-size: 12px; font-weight: 500; color: var(--gold);
  background: transparent; border: 1px solid var(--gold-fill); border-radius: 8px;
  padding: 7px 14px; cursor: pointer; transition: background .12s ease;
}
.ghost-btn:hover { background: var(--gold-fill-soft); }
.ghost-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

.editor-host {
  flex: 1; min-height: 220px; border-radius: 10px; overflow: hidden; border: 1px solid var(--rule);
}
.CodeMirror {
  height: 100%; font-family: var(--mono); font-size: 13px;
  background: var(--paper-recessed) !important; color: var(--ink) !important;
}
.CodeMirror-gutters { background: var(--paper-recessed) !important; border-right: 1px solid var(--rule) !important; }
.CodeMirror-linenumber { color: var(--ink-faint) !important; }
.CodeMirror-cursor { border-left-color: var(--ink) !important; }
/* CodeMirror's "default" theme ships light-mode-tuned syntax colors —
   override the token classes directly so code stays on-palette and
   readable against the dark editor background. */
.cm-keyword { color: var(--teal) !important; }
.cm-atom, .cm-builtin { color: var(--gold) !important; }
.cm-number { color: var(--gold) !important; }
.cm-def, .cm-variable-2, .cm-variable-3 { color: var(--ink) !important; }
.cm-variable { color: var(--ink-soft) !important; }
.cm-property, .cm-attribute { color: var(--teal) !important; }
.cm-operator, .cm-bracket { color: var(--ink-soft) !important; }
.cm-comment { color: var(--ink-faint) !important; font-style: italic; }
.cm-string { color: var(--good) !important; }
.cm-meta, .cm-qualifier { color: var(--ink-soft) !important; }
.cm-error { color: var(--danger) !important; }

.error-panel {
  min-height: 20px; margin: 0; padding: 10px 14px; font-family: var(--mono);
  font-size: 12.5px; color: var(--ink-soft); white-space: pre-wrap;
}
.error-panel.has-error { color: var(--danger); background: var(--danger-fill); border-radius: 6px; }

.lsp-fallback-editor {
  width: 100%; height: 100%; background: var(--paper-recessed); color: var(--ink);
  font-family: var(--mono); font-size: 13px; border: none; padding: 12px; resize: none;
}

/* ---------------------------------------------------------------------
   Mobile: read first, play second — a plain top-to-bottom stack in
   source order (reading pane, then the canvas/editor pane below it).
   No sticky pinning: that was tried and just put an empty dark box at
   the top of the page before any context. Simple beats clever here.
--------------------------------------------------------------------- */
@media (max-width: 900px) {
  .lesson-grid { grid-template-columns: 1fr; min-height: 0; }
  .reading-pane { padding: 24px 16px var(--space-4); }
  .playground-pane { border-left: none; border-top: 1px solid var(--rule); padding: var(--space-3) 16px; }
  .editor-host { min-height: 260px; }
  .lesson-footer { flex-direction: column; align-items: flex-start; gap: var(--space-2); }
}
