@import "../../../assets/tokens.css";

/* ============================================================
   Inference Engineering — shared course stylesheet.
   Design system: "Readout" — a precision instrument reading a hot
   chip. Warm amber SIGNAL on a true graphite/white ground; numbers
   are the hero. See /DESIGN.md for the full spec.

   One file drives every page (index, 36 lessons, reference). Class
   names are stable so lesson markup is untouched; the new identity
   is carried entirely by tokens, type, and the unified components
   below. Light is default; dark is opt-in via html[data-theme=dark].
   ============================================================ */

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0 auto;
  padding: clamp(2.5rem, 6vw, 4.5rem) 1.5rem 6rem;
  max-width: var(--maxw);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-text);
  font-size: 1.075rem;
  line-height: var(--lh-body);
  font-feature-settings: "kern" 1, "liga" 1, "cv01" 1;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: color-mix(in oklch, var(--signal) 32%, transparent); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* ---- masthead ---- */
.masthead {
  border-bottom: 2px solid var(--signal);     /* the persistent amber signal bar */
  padding-bottom: 0.7rem;
  margin-bottom: clamp(1.8rem, 4vw, 2.6rem);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem 1.25rem;
  flex-wrap: wrap;
  font-family: var(--font-text);
  font-size: 0.8rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--muted);
}
.masthead > span:first-child { font-family: var(--font-display); font-weight: 700; color: var(--ink); letter-spacing: -0.01em; }
.masthead a { color: var(--muted); text-decoration: none; transition: color .14s var(--ease); }
.masthead a:hover { color: var(--accent-strong); }

/* ---- headings ---- */
h1 {
  font-family: var(--font-display);
  font-size: clamp(2.0rem, 1.45rem + 2.4vw, 2.95rem);
  line-height: 1.08;
  font-weight: 700;
  letter-spacing: -0.022em;
  margin: 0 0 0.5rem;
  text-wrap: balance;
}
.subtitle {
  font-family: var(--font-text);
  font-weight: 400;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1.4;
  margin: 0 0 clamp(2rem, 4vw, 2.8rem);
  text-wrap: pretty;
}
h2 {
  font-family: var(--font-display);
  font-size: clamp(1.35rem, 1.2rem + 0.7vw, 1.65rem);
  font-weight: 650;
  letter-spacing: -0.012em;
  line-height: 1.2;
  position: relative;
  margin: 3rem 0 0.7rem;
  padding-top: 1.35rem;
  text-wrap: balance;
}
h2::before {                              /* the amber section tick — brand structure, not an eyebrow */
  content: "";
  position: absolute;
  top: 0; left: 0;
  width: 2.2rem; height: 3px;
  background: var(--signal);
  border-radius: 2px;
}
h3 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 650;
  letter-spacing: -0.006em;
  margin: 2rem 0 0.45rem;
  color: var(--accent);
  text-wrap: balance;
}

p { margin: 0 0 1.1rem; text-wrap: pretty; }
strong, b { font-weight: 700; }
em, i { font-style: italic; }

a {
  color: var(--accent);
  text-decoration-color: color-mix(in oklch, var(--accent) 45%, transparent);
  text-underline-offset: 2px;
  transition: color .14s var(--ease), text-decoration-color .14s var(--ease);
}
a:hover { color: var(--accent-strong); text-decoration-color: var(--accent-strong); }
/* external links (marked by nav.js) open in a new tab — a small ↗ signals it */
a.ext-link::after {
  content: "↗";
  display: inline-block;
  font-size: 0.74em;
  margin-left: 0.1em;
  color: var(--muted);
  text-decoration: none;
  vertical-align: baseline;
}

/* ============================================================
   UNIFIED CALLOUT FAMILY
   One structure: 1px full border, 10px radius, calm surface, a
   semantic label. No side-stripe borders anywhere. Hierarchy comes
   from accent + fill weight — the win and the live numbers pop;
   everything else stays calm.
   ============================================================ */
.win, .cluster, .bridge, .xref, .analogy-lead, .primary, .recall, .flag, .teacher {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  padding: 0.95rem 1.15rem;
  margin: 1.8rem 0;
  font-size: 0.97rem;
  line-height: 1.55;
}
.win h3, .cluster h3, .bridge h3, .analogy-lead h3 { margin-top: 0; }

/* the "one win" — amber, the loudest box on the page */
.win {
  background: color-mix(in oklch, var(--signal) 10%, var(--bg));
  border-color: color-mix(in oklch, var(--signal) 34%, var(--line));
}
.win strong { color: var(--signal-ink); }

/* live cluster — green = real, measured on the 4×H100 lab */
.cluster {
  background: color-mix(in oklch, var(--good) 8%, var(--bg));
  border-color: color-mix(in oklch, var(--good) 30%, var(--line));
}
.cluster h3 { color: var(--good); font-size: 1.06rem; }
.cluster code { background: color-mix(in oklch, var(--good) 14%, var(--surface-2)); }

/* bridge — teal = internals ↔ the API/product you use */
.bridge {
  background: color-mix(in oklch, var(--accent) 6%, var(--bg));
  border-color: color-mix(in oklch, var(--accent) 26%, var(--line));
}
.bridge h3 { color: var(--accent-strong); font-size: 1.06rem; }
.bridge code { background: color-mix(in oklch, var(--accent) 12%, var(--surface-2)); }
.bridge table td:first-child { color: var(--accent-strong); white-space: nowrap; }

/* cross-reference locator — a "you are here" minimap that links two lessons */
.xref {
  display: flex; gap: 1.1rem; align-items: center;
  background: color-mix(in oklch, var(--accent) 5%, var(--bg));
  border-color: color-mix(in oklch, var(--accent) 24%, var(--line));
}
.xref svg { flex: 0 0 auto; width: 172px; height: auto; }
.xref p { margin: 0; }
.xref a { font-weight: 600; color: var(--accent-strong); white-space: nowrap; }
@media (max-width: 560px) {
  .xref { flex-direction: column; align-items: flex-start; gap: 0.7rem; }
  .xref svg { width: 156px; }
}

/* the shared badge pill (cluster · bridge) */
.cluster .badge, .bridge .badge {
  display: inline-block; font-family: var(--font-text);
  font-size: 0.62rem; letter-spacing: 0.07em; text-transform: uppercase; font-weight: 700;
  color: #fff; border-radius: 999px; padding: 0.12rem 0.55rem;
  margin-left: 0.45rem; vertical-align: middle;
}
.cluster .badge { background: var(--good); }
.bridge  .badge { background: var(--accent); }

/* analogy thread — amber label, calm surface (the running Pantry metaphor) */
.analogy-lead {
  background: color-mix(in oklch, var(--signal) 7%, var(--bg));
  border-color: color-mix(in oklch, var(--signal) 26%, var(--line));
}
.analogy-lead h3 { color: var(--signal-ink); }
.analogy-note {
  font-size: 0.92rem;
  background: color-mix(in oklch, var(--signal) 7%, var(--bg));
  border: 1px solid color-mix(in oklch, var(--signal) 22%, var(--line));
  border-radius: 8px;
  padding: 0.6rem 0.9rem;
  margin: 0.6rem 0 0;
}
.analogy-note b { color: var(--signal-ink); }

/* primary source — neutral, mono label */
.primary { font-size: 0.97rem; }
.primary .label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent-strong);
  font-weight: 600;
  display: block;
  margin-bottom: 0.35rem;
}

/* active recall — violet label, blur-to-reveal answer */
.recall {
  background: color-mix(in oklch, var(--recall) 6%, var(--bg));
  border-color: color-mix(in oklch, var(--recall) 28%, var(--line));
}
.recall .r-q { font-weight: 700; color: var(--recall); }
.recall .r-a {
  display: inline-block; margin-top: 0.4rem; filter: blur(5px);
  cursor: pointer; transition: filter 0.15s var(--ease); user-select: none;
}
.recall .r-a:hover, .recall .r-a.show { filter: none; user-select: text; }
.recall .r-a:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 3px; }
.recall .r-hint { font-size: 0.78rem; color: var(--muted); margin-left: 0.4rem; }

/* experimental-edition flag — neutral/violet meta */
.flag {
  font-size: 0.9rem;
  background: color-mix(in oklch, var(--recall) 6%, var(--bg));
  border-color: color-mix(in oklch, var(--recall) 24%, var(--line));
}

/* ask-your-teacher reminder — dashed neutral */
.teacher {
  margin-top: 2.5rem;
  border-style: dashed;
  border-color: var(--line-strong);
  font-size: 0.95rem;
}
.teacher strong { color: var(--signal-ink); }

/* aside / margin note — Tufte-style, ruled top & bottom */
.aside {
  font-size: 0.95rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0.9rem 0;
  margin: 1.9rem 0;
}

/* ---- tables ---- */
table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.6rem 0;
  font-size: 0.97rem;
}
th, td {
  text-align: left;
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
thead th {
  font-family: var(--font-text);
  border-bottom: 2px solid var(--line-strong);
  font-size: 0.74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
tbody th { font-weight: 600; color: var(--muted); white-space: nowrap; }
table.map td:first-child { color: var(--signal-ink); }
table.map td:last-child  { color: var(--ink); }

/* ---- code & formulas ---- */
code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--surface-2);
  color: var(--ink);          /* never inherit --muted: keeps code ≥AA on the surface-2 well (the numbers are the hero) */
  padding: 0.12em 0.36em;
  border-radius: 4px;
}
.formula {
  background: var(--surface-2);
  border: 1px solid var(--line);
  padding: 1rem 1.2rem;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.55;
  margin: 1.4rem 0;
  overflow-x: auto;
}
.formula b { color: var(--signal-ink); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---- figures & diagrams ---- */
.figure { margin: 2.2rem 0; }
.figure svg { width: 100%; height: auto; display: block; }
.figure figcaption {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
  margin-top: 0.7rem;
}
.figure text, svg text { font-family: var(--font-text); }
/* Themed default fill for diagram text. Low specificity (0,0,2) so the
   semantic fill="#hex" remaps below and class fills like .dsub still win;
   this only catches text with NO fill (which would otherwise default to
   black and vanish in dark mode). Tracks --dia-ink across both themes. */
svg text { fill: var(--dia-ink); }
.dlabel { font-size: 13px; font-weight: 700; }
.dsub   { font-size: 11px; fill: var(--muted); }
.dtag   { font-size: 10px; letter-spacing: 0.05em; text-transform: uppercase; }

/* gentle flow animation on dashed paths; off for print & reduced-motion */
.flow { stroke-dasharray: 6 6; animation: dash 1.1s linear infinite; }
@keyframes dash { to { stroke-dashoffset: -24; } }

/* embedded shared SVG (the animated hero, loaded via <object>) */
.embed {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
}
.embed object, .embed img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 720 / 320;
}

/* ============================================================
   DIAGRAM HARMONIZATION (no SVG markup is edited)
   Lesson SVGs ship a fixed semantic hex palette. We remap those
   hexes onto --dia-* tokens with case-insensitive attribute
   selectors, so figures track the design tokens in BOTH themes.
   Only the token VALUES change per theme (see the dark block).
   ============================================================ */
/* box backgrounds -> diagram surface */
svg [fill="#fff" i], svg [fill="#ffffff" i],
svg [fill="#fffdf7" i], svg [fill="#fffef9" i], svg [fill="#fbfaf6" i],
svg [fill="#faf8f3" i], svg [fill="#faf8ff" i], svg [fill="#fcfbf6" i] { fill: var(--dia-surface); }
svg [fill="#f4f1ea" i], svg [fill="#f0ead8" i], svg [fill="#ece7dd" i] { fill: var(--dia-surface-2); }
/* neutrals */
svg text[fill="#1a1a1a" i], svg text[fill="#000" i], svg text[fill="#000000" i] { fill: var(--dia-ink); }
svg text[fill="#6b6b6b" i], svg text[fill="#555" i], svg text[fill="#555555" i] { fill: var(--dia-muted); }
svg [stroke="#e3e0d8" i], svg [stroke="#eee" i] { stroke: var(--dia-line); }
svg [stroke="#b9b3a6" i], svg [stroke="#c9c2b4" i], svg [stroke="#cdbf9f" i],
svg [stroke="#c9b8a0" i], svg [stroke="#d9d2c4" i] { stroke: var(--dia-line-2); }
/* memory / pantry (blue) */
svg [fill="#1f4e7a" i] { fill: var(--dia-mem); }   svg [stroke="#1f4e7a" i] { stroke: var(--dia-mem); }
svg text[fill="#1f4e7a" i] { fill: var(--dia-mem); }
svg [fill="#eef3f8" i], svg [fill="#cfe0ef" i], svg [fill="#cdddec" i] { fill: var(--dia-mem-tint); }
svg [fill="#a9bdd1" i], svg [fill="#9aa7b4" i] { fill: var(--dia-slate); }
/* compute / kitchen (red) */
svg [fill="#7a1f1f" i] { fill: var(--dia-compute); }   svg [stroke="#7a1f1f" i] { stroke: var(--dia-compute); }
svg text[fill="#7a1f1f" i] { fill: var(--dia-compute); }
svg [fill="#fbf0ee" i], svg [fill="#faf1f1" i], svg [fill="#f4dcdc" i], svg [fill="#efe2df" i] { fill: var(--dia-compute-tint); }
/* real / measured (green) */
svg [fill="#1f6b3a" i] { fill: var(--dia-real); }   svg [stroke="#1f6b3a" i] { stroke: var(--dia-real); }
svg text[fill="#1f6b3a" i] { fill: var(--dia-real); }
svg [fill="#eef6f0" i], svg [fill="#d6ece9" i], svg [fill="#bfe6cc" i],
svg [fill="#9ccfae" i], svg [fill="#e2efe6" i] { fill: var(--dia-real-tint); }
svg [stroke="#bcdcc6" i] { stroke: color-mix(in oklch, var(--dia-real) 45%, var(--dia-line)); }
/* heat / analogy (amber) */
svg [fill="#a56a1e" i], svg text[fill="#a56a1e" i] { fill: var(--dia-heat); }
svg [fill="#c98a3a" i] { fill: var(--dia-heat); }
svg [stroke="#c98a3a" i] { stroke: var(--dia-heat); }
/* bad (red) + bridge (violet) + teal accents */
svg [fill="#9b2226" i], svg text[fill="#9b2226" i] { fill: var(--dia-bad); }   svg [stroke="#9b2226" i] { stroke: var(--dia-bad); }
svg [fill="#fbcece" i], svg [fill="#c9a9a9" i], svg [fill="#c08a8a" i], svg [fill="#f4dcdc" i] { fill: color-mix(in oklch, var(--dia-bad) 22%, var(--dia-surface)); }
svg [fill="#5a4fa3" i], svg text[fill="#5a4fa3" i] { fill: var(--dia-bridge); }   svg [stroke="#5a4fa3" i] { stroke: var(--dia-bridge); }
svg [fill="#3f7a8a" i], svg text[fill="#3f7a8a" i] { fill: var(--dia-teal); }   svg [stroke="#3f7a8a" i] { stroke: var(--dia-teal); }
svg [fill="#7a8aa0" i] { fill: var(--dia-slate); }

/* ---- citations ---- */
.cite { font-size: 0.72em; vertical-align: super; line-height: 0; }
.cite a { text-decoration: none; }
.refs {
  margin-top: 3rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
  font-size: 0.85rem;
  color: var(--muted);
}
.refs ol { padding-left: 1.3rem; }
.refs li { margin-bottom: 0.4rem; }

/* ---- lesson variant tabs (predict-first ↔ diagram-first) ---- */
.tabs {
  display: flex; gap: 1.5rem; border-bottom: 1px solid var(--line);
  margin: 1.4rem 0 2rem;
}
.tabs button {
  font-family: var(--font-text); font-size: 0.78rem; font-weight: 600;
  letter-spacing: 0.01em; cursor: pointer; background: none; border: none;
  color: var(--muted); padding: 0 0 0.65rem; margin-bottom: -1px;
  border-bottom: 2px solid transparent; transition: color .14s var(--ease), border-color .14s var(--ease), transform .12s var(--ease);
}
.tabs button:active { transform: scale(0.96); }
.tabs button.active { color: var(--ink); border-bottom-color: var(--signal); }
.tabs button:hover:not(.active) { color: var(--accent-strong); }
.variant { display: none; }
.variant.active { display: block; }

/* ---- index: lesson cards & part headings ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 0.9rem; margin: 1.4rem 0; }
.card {
  display: block; position: relative; overflow: hidden;
  border: 1px solid var(--line); border-radius: 12px;
  padding: 1rem 1.15rem; text-decoration: none; color: var(--ink); background: var(--surface);
  transition: border-color .16s var(--ease), background .16s var(--ease);
}
.card::before {                              /* amber top-rule, revealed on hover */
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--signal); transform: scaleX(0); transform-origin: left;
  transition: transform .22s var(--ease);
}
.card:hover { border-color: color-mix(in oklch, var(--signal) 40%, var(--line)); background: var(--bg); }
.card:hover::before { transform: scaleX(1); }
.card .n { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.card .t { font-family: var(--font-display); font-weight: 650; letter-spacing: -0.01em; margin: 0.25rem 0 0.2rem; text-wrap: balance; }
.card .d { font-size: 0.88rem; color: var(--muted); line-height: 1.45; }
.card.soon { opacity: 0.55; pointer-events: none; }
.part {
  font-family: var(--font-display); font-weight: 600; font-size: 0.95rem; color: var(--muted);
  margin: 1.8rem 0 0.3rem; padding-bottom: 0.3rem; border-bottom: 1px solid var(--line);
}

/* ---- footer nav ---- */
.lesson-nav {
  margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; gap: 1rem; font-size: 0.9rem;
}
.lesson-nav a { font-weight: 500; }

/* index cards inline-overrode #fff in the old build — token now wins by element */
html[data-theme="dark"] body { background: var(--bg); }

/* the line cook's flames stay warm even after the red remap */
svg [fill="#e0892b" i], svg [fill="#fdeaa8" i], svg [fill="#fdeec0" i], svg [fill="#fdebc0" i] { /* untouched in light */ }
html[data-theme="dark"] svg [fill="#fdeaa8" i],
html[data-theme="dark"] svg [fill="#fdeec0" i],
html[data-theme="dark"] svg [fill="#fdebc0" i] { fill:#2a2412; }

/* ---- floating theme toggle ---- */
.theme-toggle {
  position: fixed; bottom: 1.1rem; right: 1.1rem; z-index: var(--z-toggle);
  font-family: var(--font-text); font-size: 0.78rem; letter-spacing: 0.01em;
  font-weight: 600; cursor: pointer;
  color: var(--accent-strong); background: var(--bg);
  border: 1px solid color-mix(in oklch, var(--accent) 50%, var(--line));
  border-radius: 999px; padding: 0.5rem 0.9rem;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05), 0 4px 12px rgba(0,0,0,0.10), 0 8px 24px rgba(0,0,0,0.06);
  transition: background .16s var(--ease), color .16s var(--ease), border-color .16s var(--ease), transform .12s var(--ease);
}
.theme-toggle:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.theme-toggle:active { transform: scale(0.96); }
html[data-theme="dark"] .theme-toggle { box-shadow: 0 2px 6px rgba(0,0,0,0.4), 0 8px 22px rgba(0,0,0,0.5); }
html[data-theme="dark"] .theme-toggle:hover { color: #14130f; }

/* ============================================================
   MOTION — quiet, ease-out only. Theme switch crossfades.
   ============================================================ */
@media (prefers-reduced-motion: no-preference) {
  body, .win, .aside, .cluster, .bridge, .analogy-note, .analogy-lead,
  .primary, .teacher, .embed, .card, .recall, .flag, code, .formula,
  svg rect, svg path, svg circle, svg line, svg polygon, svg text {
    transition: background-color .25s var(--ease), border-color .25s var(--ease),
                color .25s var(--ease), fill .25s var(--ease), stroke .25s var(--ease);
  }
}
@media (prefers-reduced-motion: reduce) {
  .flow { animation: none; }
  .card::before { transition: none; }
  .qnav-progress i { will-change: auto; }
  /* drop the tactile press-scale for users who opt out of motion */
  .theme-toggle:active, .qnav-btn:active, .qn-x:active, .tabs button:active,
  .pred .p-reveal:active, .quiz .q-retry:active, .kvc .presets button:active { transform: none; }
  * { scroll-behavior: auto; }
}

/* ============================================================
   MOBILE / small screens — readable on phones, no horizontal scroll.
   ============================================================ */
img, svg, object, canvas { max-width: 100%; }

@media (max-width: 640px) {
  body { padding: 2rem 1.05rem 4rem; font-size: 1.05rem; line-height: 1.6; }
  .subtitle { font-size: 1.08rem; }
  h2 { margin-top: 2.4rem; }
  .masthead { font-size: 0.76rem; gap: 0.2rem 1rem; }
  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  tbody th, .bridge table td:first-child, table.map td { white-space: normal; }
  .tabs { gap: 1.1rem; }
  .tabs button { font-size: 0.72rem; }
  .formula, pre { font-size: 0.82rem; }
  code { overflow-wrap: break-word; }
  .cards { grid-template-columns: 1fr; }
  .lesson-nav { flex-direction: column; gap: 0.4rem; }
  .theme-toggle { bottom: 0.8rem; right: 0.8rem; padding: 0.45rem 0.72rem; font-size: 0.72rem; }
}

/* ============================================================
   QUICK NAVIGATION (assets/nav.js) — lessons + reference only.
   Top reading-progress line + floating "Lesson N / 36" launcher +
   a filterable jump dialog. Themed via tokens; no side-stripes.
   ============================================================ */

/* top reading-progress line */
.qnav-progress {
  position: fixed; top: 0; left: 0; right: 0; height: 2px;
  z-index: var(--z-progress); pointer-events: none;
  background: color-mix(in oklch, var(--signal) 13%, transparent);
}
.qnav-progress i {
  display: block; height: 100%; width: 100%;
  transform: scaleX(0); transform-origin: 0 50%;
  will-change: transform;          /* updated every scroll frame — hint the compositor */
  background: var(--signal);
}

/* floating launcher (bottom-left, mirrors the theme toggle) */
.qnav {
  position: fixed; bottom: 1.1rem; left: 1.1rem; z-index: var(--z-launcher);
  display: flex; align-items: stretch; gap: 2px; padding: 3px;
  font-family: var(--font-text);
  background: var(--bg);
  border: 1px solid color-mix(in oklch, var(--ink) 12%, var(--line-strong));
  border-radius: 999px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05), 0 4px 12px rgba(0,0,0,0.10), 0 8px 24px rgba(0,0,0,0.06);
}
html[data-theme="dark"] .qnav { box-shadow: 0 2px 6px rgba(0,0,0,0.4), 0 8px 22px rgba(0,0,0,0.5); }
.qnav-btn {
  font: inherit; cursor: pointer; border: none; background: none; text-decoration: none;
  color: var(--muted); display: flex; align-items: center; justify-content: center; gap: 0.35rem;
  border-radius: 999px; padding: 0.46rem 0.5rem;
  transition: background .14s var(--ease), color .14s var(--ease), transform .12s var(--ease);
}
.qnav-btn svg { display: block; }
.qnav-step { padding: 0.46rem 0.6rem; }
.qnav-btn:hover { background: var(--surface-2); color: var(--accent-strong); }
.qnav-btn:not(.is-disabled):active { transform: scale(0.96); }
.qnav-open { padding: 0.36rem 0.7rem; color: var(--ink); font-weight: 600; }
.qnav-ico { display: flex; color: var(--signal); }
.qnav-count { font-family: var(--font-mono); font-size: 0.8rem; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.qnav-count b { color: var(--ink); font-weight: 700; }
.qnav-count span { color: var(--muted); }
.qnav-btn.is-disabled { opacity: 0.3; pointer-events: none; }

/* the jump dialog (native <dialog>, renders in the top layer) */
.qnav-panel {
  width: min(440px, 92vw); max-height: min(78vh, 640px);
  padding: 0; border: 1px solid var(--line-strong); border-radius: 14px;
  background: var(--bg); color: var(--ink);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08), 0 12px 28px rgba(0,0,0,0.16), 0 24px 60px rgba(0,0,0,0.22);
  overflow: hidden;
}
.qnav-panel[open] { display: flex; flex-direction: column; }
.qnav-panel::backdrop {
  background: color-mix(in oklch, var(--ink) 44%, transparent);
}
.qn-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.85rem 1rem 0.55rem; margin: 0;
}
.qn-title { font-family: var(--font-display); font-weight: 650; font-size: 1rem; display: flex; align-items: center; gap: 0.6rem; }
.qn-kbd {
  font-family: var(--font-mono); font-size: 0.6rem; color: var(--muted); letter-spacing: 0.02em;
  border: 1px solid var(--line); border-radius: 5px; padding: 0.12rem 0.38rem; white-space: nowrap;
}
.qn-x { background: none; border: none; cursor: pointer; color: var(--muted); font-size: 1.35rem; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; min-width: 36px; min-height: 36px;
  border-radius: 8px; transition: color .14s var(--ease), background .14s var(--ease), transform .12s var(--ease); }
.qn-x:hover { color: var(--ink); background: var(--surface-2); }
.qn-x:active { transform: scale(0.96); }
.qn-filter {
  margin: 0 1rem 0.5rem; padding: 0.5rem 0.7rem; font: inherit; font-size: 0.95rem;
  color: var(--ink); background: var(--surface); border: 1px solid var(--line-strong); border-radius: 9px;
}
.qn-filter::placeholder { color: var(--muted); }
.qn-list { overflow-y: auto; padding: 0.1rem 0.5rem 0.7rem; }
.qn-part {
  font-family: var(--font-mono); font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); padding: 0.7rem 0.55rem 0.25rem;
  position: sticky; top: 0; background: var(--bg);
}
.qn-item {
  display: flex; align-items: center; gap: 0.7rem; padding: 0.42rem 0.55rem;
  border-radius: 8px; text-decoration: none; color: var(--ink); scroll-margin: 2.2rem;
}
.qn-item:hover { background: var(--surface-2); }
.qn-item.sel { background: var(--surface-2); box-shadow: inset 0 0 0 1.5px color-mix(in oklch, var(--accent) 55%, transparent); }
.qn-item.current { background: color-mix(in oklch, var(--signal) 12%, var(--bg)); }
.qn-num {
  font-family: var(--font-mono); font-size: 0.8rem; color: var(--muted);
  width: 1.6rem; text-align: right; flex: none; font-variant-numeric: tabular-nums;
}
.qn-item.current .qn-num { color: var(--signal-ink); font-weight: 600; }
.qn-num-ref { color: var(--accent); }
.qn-t { font-size: 0.93rem; line-height: 1.25; }
.qn-you {
  margin-left: auto; flex: none; font-family: var(--font-mono);
  font-size: 0.58rem; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--signal-ink); background: color-mix(in oklch, var(--signal) 16%, var(--bg));
  border-radius: 999px; padding: 0.12rem 0.45rem;
}

@media (prefers-reduced-motion: no-preference) {
  .qnav-progress i { transition: transform .08s linear; }
  .qnav-panel[open] { animation: qn-in .18s var(--ease); }
  .qnav-panel::backdrop { animation: qn-bd .18s var(--ease); }
  @keyframes qn-in { from { opacity: 0; transform: translateY(6px) scale(0.99); } }
  @keyframes qn-bd { from { opacity: 0; } }
}

@media (max-width: 640px) {
  .qnav { bottom: 0.8rem; left: 0.8rem; padding: 2px; }
  .qnav-open { padding: 0.36rem 0.55rem; }
  .qnav-count { font-size: 0.74rem; }
}

/* ---- print ---- */
@media print {
  body { background: #fff; color: #000; padding: 1rem; max-width: none; }
  .teacher, .quiz button, .theme-toggle, .qnav, .qnav-progress, .qnav-panel { display: none !important; }
  .flow { animation: none; }
  a { color: #000; }
}

/* ============================================================
   CLICK-TO-ZOOM DIAGRAM POPOUT (zoom.js) — enlarge any figure
   SVG in a theme-aware modal <dialog>.
   ============================================================ */
figure.figure.zoomable { position: relative; }
figure.figure.zoomable svg { cursor: zoom-in; }
figure.figure.zoomable svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 4px; }
figure.figure.zoomable::after {
  content: "";
  position: absolute; top: 0.55rem; right: 0.55rem; width: 28px; height: 28px;
  border: 1px solid var(--line); border-radius: 8px;
  background: color-mix(in oklch, var(--surface) 82%, transparent) center / 15px 15px no-repeat;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="%23736d65" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M6 3H3v3M10 3h3v3M6 13H3v-3M10 13h3v-3"/></svg>');
  opacity: 0; transition: opacity .15s var(--ease);
  pointer-events: none;
}
html[data-theme="dark"] figure.figure.zoomable::after {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="%23a8a299" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M6 3H3v3M10 3h3v3M6 13H3v-3M10 13h3v-3"/></svg>');
}
figure.figure.zoomable:hover::after, figure.figure.zoomable:focus-within::after { opacity: 1; }
@media (hover: none) { figure.figure.zoomable::after { opacity: 0.55; } }

dialog.fig-zoom {
  border: 0; padding: 0; background: transparent; color: var(--ink);
  width: min(96vw, 1080px); max-width: 96vw; max-height: 94vh; margin: auto; overflow: visible;
}
dialog.fig-zoom::backdrop { background: rgba(0,0,0,0.55); backdrop-filter: blur(3px); }
dialog.fig-zoom .fz-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: clamp(1rem, 3vw, 1.85rem);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06), 0 16px 50px rgba(0,0,0,0.22), 0 6px 16px rgba(0,0,0,0.12);
  max-height: 94vh; overflow: auto;
  opacity: 0; transform: scale(0.96) translateY(6px);
  transition: opacity .22s var(--ease), transform .22s var(--ease);
}
dialog.fig-zoom.is-open .fz-card { opacity: 1; transform: scale(1) translateY(0); }
dialog.fig-zoom .fz-stage svg { width: 100%; height: auto; display: block; }
dialog.fig-zoom .fz-cap { color: var(--muted); font-size: 0.92rem; line-height: 1.5; text-align: center; margin-top: 0.9rem; text-wrap: pretty; }
dialog.fig-zoom .fz-cap a { color: var(--accent); }
.fz-close {
  position: fixed; top: 1rem; right: 1rem; width: 40px; height: 40px;
  display: grid; place-items: center; font-size: 1.4rem; line-height: 1;
  color: #fff; background: rgba(0,0,0,0.45); border: 1px solid rgba(255,255,255,0.28);
  border-radius: 999px; cursor: pointer;
  transition: background .14s var(--ease), transform .12s var(--ease);
}
.fz-close:hover { background: rgba(0,0,0,0.7); }
.fz-close:active { transform: scale(0.96); }
@media (prefers-reduced-motion: reduce) {
  dialog.fig-zoom .fz-card { transition: none; opacity: 1; transform: none; }
}
@media print { dialog.fig-zoom { display: none !important; } }

/* missed box-fill tints from the original diagrams — map onto --dia tokens (both themes; fixes invisible-in-dark boxes) */
svg [fill="#cdbf9f" i], svg [fill="#c9b8a0" i], svg [fill="#c9c2b4" i], svg [fill="#d9d2c4" i] { fill: var(--dia-surface-2); }
svg [fill="#dbe7f2" i] { fill: var(--dia-mem-tint); }


/* ============ interactive simulators — shared chrome (batchsim · roofline · loadsim) ============ */
.sim { border:1px solid var(--line,#e5e2dd); border-radius:10px; padding:1.1rem 1.2rem;
       margin:1.8rem 0; background:var(--surface,#f7f6f4); font-size:0.95rem; }
.sim h4 { margin:0 0 0.2rem; font-size:1rem; font-family:var(--font-display,inherit); }
.sim .hint { margin:0 0 0.9rem; font-size:0.82rem; color:var(--muted,#736d65); }
.sim .row { display:flex; flex-wrap:wrap; gap:0.5rem 0.9rem; align-items:center; margin-bottom:0.8rem; }
.sim .seg { display:inline-flex; border:1px solid color-mix(in oklch,var(--accent,#1f717f) 45%,var(--line,#e5e2dd));
       border-radius:999px; overflow:hidden; }
.sim .seg button { font:inherit; font-size:0.85rem; cursor:pointer; padding:0.34rem 0.9rem; border:0;
       background:var(--bg,#fff); color:var(--accent,#1f717f); transition:background .14s,color .14s; }
.sim .seg button.on { background:var(--accent,#1f717f); color:var(--bg,#fff); }
.sim .ctl { display:flex; align-items:center; gap:0.4rem; font-size:0.8rem; color:var(--muted,#736d65); }
.sim .ctl input[type=range] { width:118px; accent-color:var(--accent,#1f717f); }
.sim .ctl b { color:var(--ink,#2a2724); font-family:var(--font-mono,monospace); font-variant-numeric:tabular-nums; min-width:2.4ch; display:inline-block; }
.sim .btns button { font:inherit; font-size:0.82rem; cursor:pointer; padding:0.34rem 0.8rem; border-radius:999px;
       border:1px solid var(--line,#e5e2dd); background:var(--bg,#fff); color:var(--ink,#2a2724);
       transition:background .14s,transform .12s; }
.sim .btns button:hover { background:color-mix(in oklch,var(--accent,#1f717f) 10%,var(--bg,#fff)); }
.sim .btns button:active { transform:scale(0.96); }
.sim svg { width:100%; height:auto; display:block; border-radius:7px; }
.sim .out { display:grid; grid-template-columns:repeat(auto-fit,minmax(120px,1fr)); gap:0.3rem 1rem; margin-top:0.8rem; }
.sim .out div { font-size:0.85rem; color:var(--muted,#736d65); }
.sim .out b { display:block; font-size:1.15rem; color:var(--ink,#2a2724);
       font-family:var(--font-mono,monospace); font-variant-numeric:tabular-nums; }
.sim .verdict { margin-top:0.7rem; font-size:0.9rem; line-height:1.5; }
.sim .verdict b { color:var(--signal-ink,#945c1c); }
@media (prefers-reduced-motion: reduce) { .sim .btns button:active { transform:none; } }
@media (max-width:540px){ .sim .ctl input[type=range]{ width:90px; } }

/* ============================================================
   On-this-page section outline + masthead tools (built by nav.js).
   Pinned in the left margin on wide screens; a tap-to-open dropdown
   on narrow ones. Self-themes through the Readout design tokens.
   ============================================================ */
.variant h2, main h2 { scroll-margin-top: 1.5rem; }
.lesson-toc {
  display: none; position: fixed; z-index: 50;
  top: 6rem; left: 1.5rem; width: 12.5rem;
  max-height: calc(100vh - 8rem); overflow: auto;
  font-family: var(--font-text); font-size: 0.82rem; line-height: 1.35;
}
.lesson-toc.toc-empty { display: none !important; }
.lesson-toc .toc-h {
  font-family: var(--font-mono); font-size: 0.6rem; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--muted); margin: 0 0 0.5rem 0.7rem;
}
.lesson-toc ol { list-style: none; margin: 0; padding: 0; border-left: 1.5px solid var(--line); }
.lesson-toc li { margin: 0; }
.lesson-toc a {
  display: block; padding: 0.28rem 0.7rem; margin-left: -1.5px;
  border-left: 1.5px solid transparent; color: var(--muted); text-decoration: none;
  transition: color 0.14s var(--ease), border-color 0.14s var(--ease);
}
.lesson-toc a:hover { color: var(--ink); }
.lesson-toc a.cur { color: var(--accent-strong); border-left-color: var(--signal); font-weight: 600; }
@media (min-width: 1200px) { .lesson-toc:not(.toc-empty) { display: block; } }
@media (max-width: 1199px) {
  .lesson-toc.toc-open {
    display: block; top: 4.2rem; width: 15rem; max-width: calc(100vw - 3rem);
    background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
    box-shadow: 0 16px 40px rgba(0,0,0,0.18); padding: 0.7rem 0.5rem;
  }
}
.masthead button.mh-contents, .masthead button.mh-onpage {
  background: none; border: 0; padding: 0; margin: 0; font: inherit;
  color: var(--muted); cursor: pointer; transition: color 0.14s var(--ease);
}
.masthead button.mh-contents:hover, .masthead button.mh-onpage:hover { color: var(--accent-strong); }
@media (min-width: 1200px) { .masthead button.mh-onpage { display: none; } }
@media (prefers-reduced-motion: reduce) { .lesson-toc a { transition: none; } }
