/* ==========================================================================
   The Measure of Surprise — shared stylesheet
   A warm "teletype at night" look: espresso-dark paper, cream ink,
   and six colours that always mean the same thing across the book:
     blue   = probability          amber  = surprise / information
     teal   = code bits (stored)   coral  = noise / errors
     violet = entropy / limits     green  = checks / repair / recovered
   ========================================================================== */

:root {
  --bg: #14110e;
  --bg-elev: #1b1713;
  --bg-card: #211c17;
  --bg-canvas: #0f0d0b;
  --border: #3a3128;
  --border-soft: rgba(239, 230, 216, 0.08);

  --text: #efe6d8;
  --text-body: #e0d6c4;
  --text-muted: #b3a690;
  --text-dim: #857963;

  --prob: #6fa8dc;
  --surprise: #f3b340;
  --code: #3ec7b5;
  --noise: #ef6a5b;
  --entropy: #b49cf2;
  --check: #95c96b;

  --font-display: 'Fraunces', 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --font-sans: 'IBM Plex Sans', 'Segoe UI', system-ui, -apple-system, Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'IBM Plex Mono', 'Cascadia Mono', Consolas, 'SFMono-Regular', Menlo, 'Courier New', monospace;

  --sidebar-w: 286px;
  --content-max: 860px;
  --radius: 14px;
  --radius-sm: 8px;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 70% 45% at 75% -5%, rgba(243, 179, 64, 0.07), transparent 60%),
    radial-gradient(ellipse 55% 40% at 10% 95%, rgba(62, 199, 181, 0.05), transparent 60%),
    var(--bg);
  color: var(--text);
}

a { color: var(--code); text-decoration: none; }
a:hover { color: var(--surprise); }
p a, li a { text-decoration: underline; text-decoration-color: rgba(62, 199, 181, 0.4); text-underline-offset: 3px; }

:focus-visible {
  outline: 2px solid var(--surprise);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Reading progress: a strip of punched tape ---------- */
.reading-progress {
  position: fixed;
  top: 0; left: 0;
  height: 4px;
  width: 0%;
  background:
    repeating-linear-gradient(90deg, rgba(20, 17, 14, 0.55) 0 2px, transparent 2px 9px),
    linear-gradient(90deg, var(--surprise), var(--code));
  z-index: 1000;
  transition: width 0.08s linear;
}

/* ---------- Mobile top bar ---------- */
.mobile-topbar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 90;
  background: rgba(20, 17, 14, 0.94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 0.65rem 1rem;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.mobile-menu-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.42rem 0.75rem;
  font: 500 0.88rem var(--font-sans);
  cursor: pointer;
}
.mobile-brand { font-family: var(--font-display); font-weight: 600; color: var(--text); font-size: 1rem; }

/* ---------- Layout ---------- */
.book-layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1.4rem 1.05rem 2.2rem;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  z-index: 95;
}
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.3rem 0.35rem 0.95rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.sidebar-brand:hover { color: var(--surprise); }
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 9px;
  background: linear-gradient(135deg, rgba(243, 179, 64, 0.25), rgba(62, 199, 181, 0.22));
  border: 1px solid rgba(243, 179, 64, 0.45);
  display: grid;
  place-items: center;
  font: 600 0.78rem var(--font-mono);
  letter-spacing: -0.04em;
  color: var(--surprise);
  flex-shrink: 0;
}
.brand-title { font-family: var(--font-display); font-weight: 600; font-size: 1.08rem; line-height: 1.15; display: block; }
.brand-sub { font-size: 0.72rem; color: var(--text-muted); display: block; margin-top: 2px; }

.course-progress-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.8rem;
}
.course-progress-label { display: flex; justify-content: space-between; font-size: 0.76rem; color: var(--text-muted); margin-bottom: 0.4rem; }
.course-progress-track { height: 6px; background: rgba(255, 255, 255, 0.07); border-radius: 99px; overflow: hidden; }
.course-progress-fill { height: 100%; background: linear-gradient(90deg, var(--surprise), var(--code)); border-radius: 99px; transition: width 0.35s ease; }

.toc-part-title {
  font-size: 0.67rem;
  text-transform: uppercase;
  letter-spacing: 0.11em;
  color: var(--text-dim);
  font-weight: 700;
  margin: 0.85rem 0 0.3rem 0.45rem;
}
.toc-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.toc-link {
  text-decoration: none !important;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.42rem 0.6rem;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.86rem;
  line-height: 1.3;
  transition: background 0.15s ease, color 0.15s ease;
}
.toc-link:hover { background: rgba(243, 179, 64, 0.08); color: var(--text); }
.toc-link.active { background: rgba(243, 179, 64, 0.14); color: var(--surprise); font-weight: 600; }
.toc-num { font-family: var(--font-mono); font-size: 0.72rem; color: var(--text-dim); width: 1.35rem; flex-shrink: 0; }
.toc-link.active .toc-num { color: var(--surprise); }
.toc-check { margin-left: auto; font-size: 0.78rem; color: var(--check); opacity: 0; }
.toc-link.completed .toc-check { opacity: 1; }

.color-key {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding-top: 0.9rem;
  font-size: 0.74rem;
  color: var(--text-muted);
}
.color-key-title { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.11em; color: var(--text-dim); font-weight: 700; margin-bottom: 0.4rem; }
.color-key ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.22rem; }
.color-key li { display: flex; align-items: center; gap: 0.5rem; }

/* ---------- Colour swatches & accent text (fixed meanings) ---------- */
.sw { display: inline-block; width: 0.72em; height: 0.72em; border-radius: 3px; flex-shrink: 0; vertical-align: -0.05em; }
.sw.prob { background: var(--prob); }
.sw.surprise { background: var(--surprise); }
.sw.code { background: var(--code); }
.sw.noise { background: var(--noise); }
.sw.entropy { background: var(--entropy); }
.sw.check { background: var(--check); }

.c-prob { color: var(--prob); font-weight: 600; }
.c-surprise { color: var(--surprise); font-weight: 600; }
.c-code { color: var(--code); font-weight: 600; }
.c-noise { color: var(--noise); font-weight: 600; }
.c-entropy { color: var(--entropy); font-weight: 600; }
.c-check { color: var(--check); font-weight: 600; }

/* ---------- Chapter content ---------- */
.chapter-content {
  flex: 1;
  min-width: 0;
  padding: 2.6rem clamp(1.1rem, 4vw, 3.6rem) 5rem;
  max-width: calc(var(--content-max) + 7.2rem);
  margin: 0 auto;
}

.chapter-header {
  margin-bottom: 2.4rem;
  padding-bottom: 1.7rem;
  border-bottom: 1px solid var(--border);
}
.chapter-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font: 600 0.76rem var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--surprise);
  margin-bottom: 0.7rem;
}
.chapter-eyebrow::before {
  content: '';
  width: 30px; height: 8px;
  background:
    radial-gradient(circle at 4px 4px, var(--surprise) 2.4px, transparent 2.8px) 0 0 / 10px 8px repeat-x;
  opacity: 0.85;
}
.chapter-header h1 {
  font-family: var(--font-display);
  font-size: clamp(2.05rem, 4.4vw, 3.1rem);
  line-height: 1.1;
  letter-spacing: -0.015em;
  margin: 0 0 0.85rem;
  color: #fff6e8;
  font-weight: 600;
}
.chapter-subtitle { font-size: 1.14rem; color: var(--text-muted); margin: 0; max-width: 64ch; line-height: 1.6; }

.chapter-content h2 {
  font-family: var(--font-display);
  font-size: 1.72rem;
  color: #fff6e8;
  margin: 2.8rem 0 0.85rem;
  letter-spacing: -0.01em;
  font-weight: 600;
  line-height: 1.2;
}
.chapter-content h3 { font-size: 1.12rem; color: var(--surprise); margin: 1.8rem 0 0.5rem; font-weight: 600; }
.chapter-content p { margin: 0 0 1.15rem; color: var(--text-body); font-size: 1.04rem; max-width: 70ch; }
.chapter-content ul, .chapter-content ol { margin: 0 0 1.3rem; padding-left: 1.4rem; max-width: 68ch; color: var(--text-body); }
.chapter-content li { margin-bottom: 0.45rem; }
.chapter-content strong { color: #fff3df; }
code, .mono { font-family: var(--font-mono); font-size: 0.92em; }
code { background: rgba(255, 255, 255, 0.05); border: 1px solid var(--border-soft); padding: 0.05em 0.35em; border-radius: 5px; color: #f6e9d2; }
.bits { font-family: var(--font-mono); letter-spacing: 0.06em; color: var(--code); }

/* A formula, shown once and read aloud */
.formula {
  margin: 1.4rem 0 1.5rem;
  padding: 1rem 1.25rem;
  background: rgba(180, 156, 242, 0.06);
  border: 1px solid rgba(180, 156, 242, 0.25);
  border-radius: var(--radius-sm);
  max-width: 70ch;
}
.formula .math { font-family: var(--font-mono); font-size: 1.08rem; color: #fff3df; display: block; text-align: center; margin-bottom: 0.45rem; overflow-x: auto; white-space: nowrap; }
.formula .aloud { font-size: 0.93rem; color: var(--text-muted); display: block; text-align: center; }

/* ---------- Callouts / principle boxes ---------- */
.callout {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--surprise);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  margin: 1.6rem 0;
  max-width: 70ch;
}
.callout-title {
  font: 700 0.72rem var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--surprise);
  margin-bottom: 0.35rem;
}
.callout p:last-child { margin-bottom: 0; }
.callout.code { border-left-color: var(--code); background: linear-gradient(90deg, rgba(62, 199, 181, 0.07), var(--bg-card) 45%); }
.callout.code .callout-title { color: var(--code); }
.callout.noise { border-left-color: var(--noise); background: linear-gradient(90deg, rgba(239, 106, 91, 0.07), var(--bg-card) 45%); }
.callout.noise .callout-title { color: var(--noise); }
.callout.entropy { border-left-color: var(--entropy); background: linear-gradient(90deg, rgba(180, 156, 242, 0.07), var(--bg-card) 45%); }
.callout.entropy .callout-title { color: var(--entropy); }
.callout.check { border-left-color: var(--check); background: linear-gradient(90deg, rgba(149, 201, 107, 0.07), var(--bg-card) 45%); }
.callout.check .callout-title { color: var(--check); }
.callout.surprise { background: linear-gradient(90deg, rgba(243, 179, 64, 0.07), var(--bg-card) 45%); }
.callout.prob { border-left-color: var(--prob); background: linear-gradient(90deg, rgba(111, 168, 220, 0.07), var(--bg-card) 45%); }
.callout.prob .callout-title { color: var(--prob); }

.principle {
  margin: 1.8rem 0;
  max-width: 70ch;
  padding: 1.05rem 1.3rem 1.05rem 1.3rem;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(243, 179, 64, 0.35);
  background:
    repeating-linear-gradient(90deg, rgba(243, 179, 64, 0.05) 0 1px, transparent 1px 14px),
    var(--bg-card);
}
.principle .callout-title { color: var(--surprise); }
.principle p { margin: 0; font-family: var(--font-display); font-size: 1.16rem; line-height: 1.5; color: #fff3df; }

/* ---------- Interactive figures ---------- */
.figure {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin: 2rem 0 2.3rem;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
}
.figure-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem 0.9rem;
  padding: 0.75rem 1.1rem;
  background: rgba(255, 255, 255, 0.02);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.figure-title { font-size: 0.92rem; font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 0.55rem; flex-wrap: wrap; }
.figure-badge {
  font: 600 0.68rem var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  padding: 0.14rem 0.5rem;
  border-radius: 99px;
  background: rgba(243, 179, 64, 0.14);
  color: var(--surprise);
  white-space: nowrap;
}
.figure-hint { font-size: 0.8rem; color: var(--text-muted); }

.legend { display: flex; flex-wrap: wrap; gap: 0.3rem 0.9rem; padding: 0.5rem 1.1rem; font-size: 0.78rem; color: var(--text-muted); border-bottom: 1px solid var(--border-soft); }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }

.canvas-wrap { position: relative; width: 100%; background: var(--bg-canvas); line-height: 0; }
.canvas-wrap canvas { display: block; width: 100%; height: 360px; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.canvas-wrap canvas.drag { touch-action: none; }

.figure-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.2rem;
  padding: 0.85rem 1.1rem;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
}
.ctl { display: inline-flex; align-items: center; gap: 0.55rem; font-size: 0.84rem; color: var(--text-muted); }
.ctl label { white-space: nowrap; }
.ctl output, .ctl .val { font-family: var(--font-mono); font-size: 0.82rem; color: var(--surprise); min-width: 3.4rem; }
.ctl.wide { flex: 1 1 260px; }
.ctl.wide input[type="range"] { flex: 1; width: auto; min-width: 120px; }

.figure-readout {
  padding: 0.7rem 1.1rem;
  font-family: var(--font-mono);
  font-size: 0.84rem;
  color: var(--text);
  background: rgba(255, 255, 255, 0.025);
  border-top: 1px solid var(--border-soft);
  line-height: 1.6;
}
.figure-readout b { color: #fff3df; font-weight: 600; }
.figure-caption {
  padding: 0.75rem 1.1rem 0.9rem;
  font-size: 0.88rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border-soft);
  line-height: 1.55;
}
.figure-caption strong { color: var(--text); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 140px;
  height: 22px;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track { height: 5px; background: var(--border); border-radius: 99px; }
input[type="range"]::-moz-range-track { height: 5px; background: var(--border); border-radius: 99px; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 17px; height: 17px;
  margin-top: -6px;
  border-radius: 50%;
  background: var(--surprise);
  border: 2px solid #2a2016;
  box-shadow: 0 0 10px rgba(243, 179, 64, 0.35);
}
input[type="range"]::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: var(--surprise); border: 2px solid #2a2016; }

.btn {
  background: rgba(239, 230, 216, 0.06);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.42rem 0.8rem;
  font: 600 0.82rem var(--font-sans);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  line-height: 1.3;
}
.btn:hover { background: rgba(243, 179, 64, 0.12); border-color: rgba(243, 179, 64, 0.5); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--surprise); color: #1b140b; border-color: var(--surprise); }
.btn.primary:hover { background: #ffc85e; }
.btn.on, .btn[aria-pressed="true"] { background: rgba(243, 179, 64, 0.2); border-color: var(--surprise); color: #fff3df; }
.btn-lg { padding: 0.7rem 1.3rem; font-size: 0.95rem; border-radius: 10px; }
.btn-group { display: inline-flex; gap: 0.35rem; flex-wrap: wrap; }

.text-input, textarea.text-input {
  background: var(--bg-canvas);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  font: 0.9rem var(--font-mono);
  width: 100%;
}
textarea.text-input { resize: vertical; min-height: 4.2rem; line-height: 1.5; }
.text-input:focus { border-color: var(--surprise); outline: none; box-shadow: 0 0 0 2px rgba(243, 179, 64, 0.25); }
.input-row { padding: 0.8rem 1.1rem 0.2rem; background: var(--bg-elev); border-top: 1px solid var(--border); }
.input-row label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.3rem; }

/* Bit toggle buttons (accessible alternatives to canvas clicks) */
.bit-row { display: flex; flex-wrap: wrap; gap: 0.3rem; align-items: center; }
.bit-btn {
  min-width: 2.2rem;
  padding: 0.3rem 0.4rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-canvas);
  color: var(--code);
  font: 600 0.86rem var(--font-mono);
  cursor: pointer;
  line-height: 1.2;
  text-align: center;
}
.bit-btn small { display: block; font-size: 0.62rem; color: var(--text-dim); font-weight: 500; }
.bit-btn.flipped { border-color: var(--noise); color: var(--noise); background: rgba(239, 106, 91, 0.1); }
.bit-btn.parity { color: var(--check); }
.bit-btn:hover { border-color: var(--surprise); }

/* Tables (code tables etc.) */
.table-wrap { overflow-x: auto; padding: 0.6rem 1.1rem 0.9rem; border-top: 1px solid var(--border-soft); max-height: 300px; overflow-y: auto; }
table.codes { border-collapse: collapse; width: 100%; font-size: 0.82rem; font-family: var(--font-mono); }
table.codes th { text-align: left; color: var(--text-dim); font-weight: 600; padding: 0.3rem 0.5rem; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--bg-card); }
table.codes td { padding: 0.22rem 0.5rem; border-bottom: 1px solid var(--border-soft); color: var(--text-body); }
table.codes td.sym { color: var(--text); }
table.codes td.cw { color: var(--code); letter-spacing: 0.05em; }
table.codes td.num { text-align: right; }

/* ---------- Intuition check ---------- */
.quiz {
  margin: 2.4rem 0;
  max-width: 72ch;
  background: var(--bg-card);
  border: 1px solid rgba(180, 156, 242, 0.35);
  border-radius: var(--radius);
  padding: 1.2rem 1.35rem 1.1rem;
}
.quiz-label { font: 700 0.72rem var(--font-mono); text-transform: uppercase; letter-spacing: 0.11em; color: var(--entropy); margin-bottom: 0.45rem; }
.quiz .quiz-q { font-size: 1.04rem; color: var(--text); margin-bottom: 0.9rem; }
.quiz-options { display: grid; gap: 0.5rem; }
.quiz-option {
  text-align: left;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text-body);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.9rem;
  font: 0.95rem var(--font-sans);
  cursor: pointer;
  line-height: 1.45;
}
.quiz-option:hover { border-color: var(--entropy); }
.quiz-option.correct { border-color: var(--check); background: rgba(149, 201, 107, 0.1); color: #fff; }
.quiz-option.wrong { border-color: var(--noise); background: rgba(239, 106, 91, 0.08); }
.quiz-feedback { margin-top: 0.75rem; font-size: 0.93rem; min-height: 1.2em; color: var(--text-muted); line-height: 1.55; }
.quiz-feedback.good { color: var(--check); }
.quiz-feedback.bad { color: var(--noise); }

/* ---------- Connections & recap ---------- */
.connections {
  margin: 2.2rem 0 1.2rem;
  max-width: 72ch;
  padding: 1rem 1.25rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.015);
}
.connections h3 { margin: 0 0 0.45rem; color: var(--code); font-size: 0.98rem; }
.connections ul { margin: 0; }
.connections li { font-size: 0.95rem; }

.recap {
  margin: 2.4rem 0 1rem;
  max-width: 72ch;
  padding: 1.2rem 1.4rem;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(243, 179, 64, 0.08), rgba(62, 199, 181, 0.06));
  border: 1px solid rgba(243, 179, 64, 0.3);
}
.recap h2 { margin-top: 0 !important; font-size: 1.4rem !important; }
.recap ul { margin-bottom: 0; }

/* ---------- Footer (complete + prev/next) ---------- */
.chapter-footer { margin-top: 3rem; padding-top: 1.8rem; border-top: 1px solid var(--border); }
.complete-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1.4rem;
  flex-wrap: wrap;
}
.complete-bar p { margin: 0; font-size: 0.92rem; color: var(--text-muted); }
.complete-bar .btn.done { background: rgba(149, 201, 107, 0.18); border-color: var(--check); color: var(--check); }
.chapter-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.nav-card {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 1rem 1.2rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.15s ease, transform 0.15s ease;
  text-decoration: none !important;
}
.nav-card:hover { border-color: var(--surprise); transform: translateY(-2px); }
.nav-card.next { text-align: right; align-items: flex-end; }
.nav-dir { font: 700 0.7rem var(--font-mono); text-transform: uppercase; letter-spacing: 0.09em; color: var(--text-dim); }
.nav-title { font-size: 1rem; font-weight: 600; color: var(--text); }

/* ---------- Home page ---------- */
.hero { padding: 1.6rem 0 2.4rem; border-bottom: 1px solid var(--border); margin-bottom: 2rem; }
.hero-grid { display: grid; grid-template-columns: 1.05fr 1fr; gap: 2.2rem; align-items: center; }
.hero h1 {
  font-family: var(--font-display);
  font-size: clamp(2.5rem, 5vw, 3.7rem);
  line-height: 1.04;
  margin: 0.4rem 0 0.9rem;
  color: #fff6e8;
  letter-spacing: -0.02em;
  font-weight: 600;
}
.hero-sub { font-family: var(--font-display); font-size: 1.22rem; color: var(--surprise); margin: 0 0 1rem; line-height: 1.4; }
.hero-lead { font-size: 1.06rem; color: var(--text-muted); margin: 0 0 1.5rem; line-height: 1.65; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 0.8rem; }
.hero-toy { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45); }
.hero-toy .canvas-wrap canvas { height: 250px; }

.progress-banner {
  display: flex;
  align-items: center;
  gap: 1.2rem;
  flex-wrap: wrap;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin: 0 0 2rem;
}
.progress-banner .pb-text { flex: 0 1 auto; }
.progress-banner .pb-text strong { display: block; color: var(--text); font-size: 0.95rem; }
.progress-banner .pb-text span { font-size: 0.86rem; color: var(--text-muted); }
.progress-banner .pb-track { flex: 1 1 160px; height: 8px; background: rgba(255, 255, 255, 0.07); border-radius: 99px; overflow: hidden; }
.progress-banner .pb-fill { height: 100%; background: linear-gradient(90deg, var(--surprise), var(--code)); width: 0%; transition: width 0.4s ease; }

.key-strip { display: flex; flex-wrap: wrap; gap: 0.45rem 1.1rem; font-size: 0.86rem; color: var(--text-muted); margin: 0.2rem 0 0; }
.key-strip span { display: inline-flex; align-items: center; gap: 0.4rem; }

.part-heading { display: flex; align-items: baseline; gap: 0.75rem; margin: 2.2rem 0 1rem; flex-wrap: wrap; }
.part-badge { font: 600 0.74rem var(--font-mono); color: var(--surprise); background: rgba(243, 179, 64, 0.12); border: 1px solid rgba(243, 179, 64, 0.3); padding: 0.16rem 0.55rem; border-radius: 99px; }
.part-heading h2 { margin: 0 !important; font-family: var(--font-display); font-size: 1.5rem !important; }
.chapter-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 1rem; }
.chapter-card {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
  color: var(--text);
  text-decoration: none !important;
}
.chapter-card:hover { border-color: var(--surprise); transform: translateY(-3px); box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35); color: var(--text); }
.chapter-card-top { display: flex; justify-content: space-between; align-items: center; }
.chapter-card-num { font: 600 0.76rem var(--font-mono); color: var(--surprise); }
.chapter-card-status { font-size: 0.74rem; color: var(--check); font-weight: 600; }
.chapter-card h3 { margin: 0.05rem 0 0 !important; font-size: 1.06rem !important; color: #fff6e8 !important; font-weight: 600; font-family: var(--font-display); }
.chapter-card p { margin: 0 !important; font-size: 0.87rem !important; color: var(--text-muted) !important; line-height: 1.5; }
.chapter-card-meta { margin-top: auto; padding-top: 0.35rem; font: 500 0.74rem var(--font-mono); color: var(--code); }

.site-foot { margin-top: 3rem; padding-top: 1.4rem; border-top: 1px solid var(--border); font-size: 0.84rem; color: var(--text-dim); }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .mobile-topbar { display: flex; }
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 12px 0 36px rgba(0, 0, 0, 0.6);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop { display: none; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 92; }
  .sidebar-backdrop.open { display: block; }
  .hero-grid { grid-template-columns: 1fr; }
  .chapter-nav { grid-template-columns: 1fr; }
  .nav-card.next { text-align: left; align-items: flex-start; }
}

@media (max-width: 560px) {
  .chapter-content { padding: 1.6rem 0.95rem 4rem; }
  .chapter-content p { font-size: 1rem; }
  .figure { margin-left: -0.35rem; margin-right: -0.35rem; border-radius: 10px; }
  .figure-controls { padding: 0.75rem 0.8rem; gap: 0.65rem 0.9rem; }
  .figure-head, .figure-readout, .figure-caption, .legend, .input-row, .table-wrap { padding-left: 0.8rem; padding-right: 0.8rem; }
  .ctl { flex-wrap: wrap; }
  .ctl input[type="range"] { width: 150px; }
  .formula .math { font-size: 0.95rem; white-space: normal; overflow-wrap: anywhere; }
  .principle p { font-size: 1.06rem; }
}

/* ---------- On-screen letter keyboard (guessing games) ---------- */
.keyboard {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(2.2rem, 1fr));
  gap: 0.3rem;
  padding: 0.8rem 1.1rem;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
}
.key {
  font: 600 0.92rem var(--font-mono);
  padding: 0.42rem 0;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-canvas);
  color: var(--text);
  cursor: pointer;
  line-height: 1.2;
}
.key:hover:not(:disabled) { border-color: var(--surprise); color: var(--surprise); }
.key:disabled { opacity: 0.22; cursor: default; text-decoration: line-through; }
.key.space { grid-column: span 3; }
.sr-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
@media (max-width: 560px) { .keyboard { padding: 0.7rem 0.8rem; grid-template-columns: repeat(auto-fill, minmax(1.95rem, 1fr)); } }
