/* Fifty Lectures, interactive companion.
   Layout idea: fixed chapter rail on the left, one reading column with wide interactive "bench" cards on graph paper. */
:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --sunk: #eaeef5;
  --ink: #16213a;
  --muted: #5b667d;
  --line: #d5dbe6;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --c1: #1d4ed8;
  --c2: #d97706;
  --c3: #0d9488;
  --c4: #c0392b;
  --c5: #7c3aed;
  --good: #15803d;
  --bad: #b91c1c;
  --grid: rgba(29, 78, 216, 0.09);
  --font-display: "Bricolage Grotesque", "Avenir Next", "Segoe UI", sans-serif;
  --font-body: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-ui: "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1524; --surface: #171f33; --sunk: #111829; --ink: #e6eaf4; --muted: #9aa5bd;
    --line: #2b3650; --accent: #7aa2ff; --accent-ink: #0b1020;
    --c1: #7aa2ff; --c2: #f5b041; --c3: #3fcfc0; --c4: #ff7b6b; --c5: #b79bff;
    --good: #4ade80; --bad: #f87171; --grid: rgba(122, 162, 255, 0.10);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1524; --surface: #171f33; --sunk: #111829; --ink: #e6eaf4; --muted: #9aa5bd;
  --line: #2b3650; --accent: #7aa2ff; --accent-ink: #0b1020;
  --c1: #7aa2ff; --c2: #f5b041; --c3: #3fcfc0; --c4: #ff7b6b; --c5: #b79bff;
  --good: #4ade80; --bad: #f87171; --grid: rgba(122, 162, 255, 0.10);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font-family: var(--font-body); font-size: 17px; line-height: 1.6;
}
h1, h2, h3, h4 { font-family: var(--font-display); line-height: 1.15; text-wrap: balance; margin: 0; }
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
img, svg, canvas { max-width: 100%; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 280px minmax(0, 1fr); min-height: 100vh; }
.rail {
  position: sticky; top: 0; align-self: start; height: 100vh; overflow-y: auto;
  border-right: 1px solid var(--line); background: var(--surface); padding: 20px 14px 32px;
}
.brand { display: block; text-decoration: none; color: var(--ink); padding: 0 8px 14px; }
.brand b { font-family: var(--font-display); font-size: 20px; display: block; }
.brand span { font-family: var(--font-ui); font-size: 11px; color: var(--muted); letter-spacing: .04em; }
.rail h3 {
  font-family: var(--font-ui); font-weight: 500; font-size: 11px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); margin: 18px 8px 6px;
}
.rail a.ch {
  display: grid; grid-template-columns: 30px 1fr; gap: 6px; align-items: baseline;
  padding: 5px 8px; border-radius: 6px; text-decoration: none; color: var(--ink);
  font-family: var(--font-display); font-size: 14.5px; line-height: 1.3;
}
.rail a.ch i { font-style: normal; font-family: var(--font-ui); font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.rail a.ch:hover { background: var(--sunk); }
.rail a.ch[aria-current="page"] { background: var(--accent); color: var(--accent-ink); }
.rail a.ch[aria-current="page"] i { color: inherit; opacity: .8; }
.menu-btn { display: none; }
main { min-width: 0; padding: 36px clamp(16px, 4vw, 56px) 80px; }
.col { max-width: 880px; margin: 0 auto; }

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .rail { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 12px 16px; }
  .rail .brand { padding: 0; display: inline-block; }
  .rail nav { display: none; }
  .rail.open nav { display: block; }
  .menu-btn {
    display: inline-block; float: right; background: var(--sunk); border: 1px solid var(--line);
    border-radius: 6px; padding: 6px 12px; font-family: var(--font-ui); font-size: 13px; cursor: pointer;
  }
  main { padding-top: 22px; }
}

/* ---------- home ---------- */
.hero {
  padding: 28px; border: 1px solid var(--line); border-radius: var(--radius); background-color: var(--surface);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
}
.hero h1 { font-size: clamp(30px, 5vw, 46px); font-weight: 700; letter-spacing: -.01em; }
.hero p { max-width: 62ch; margin: 12px 0 0; }
.hero .zh { font-family: var(--font-ui); font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.src-note { font-family: var(--font-ui); font-size: 12px; color: var(--muted); max-width: 72ch; }
.hero .src-note { margin-top: 10px; }
.vol { margin-top: 36px; }
.vol-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; border-bottom: 2px solid var(--ink); padding-bottom: 6px; }
.vol-head h2 { font-size: 22px; }
.vol-head span { font-family: var(--font-ui); font-size: 12px; color: var(--muted); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; margin-top: 14px; }
.card {
  display: flex; flex-direction: column; gap: 6px; padding: 14px 16px; text-decoration: none; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  border-left: 4px solid var(--vc, var(--accent));
}
.card:hover { border-color: var(--vc, var(--accent)); }
.card i { font-style: normal; font-family: var(--font-ui); font-size: 12px; color: var(--muted); }
.card b { font-family: var(--font-display); font-size: 18px; line-height: 1.2; }
.card span { font-size: 14.5px; color: var(--muted); line-height: 1.45; }

/* ---------- chapter ---------- */
.ch-head .eyebrow { font-family: var(--font-ui); font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.ch-head h1 { font-size: clamp(30px, 5vw, 44px); font-weight: 700; margin-top: 6px; }
.ch-head .intro { max-width: 66ch; margin-top: 12px; }
.chips {
  position: sticky; top: 0; z-index: 5; display: flex; gap: 6px; overflow-x: auto; padding: 10px 0; margin: 18px 0 0;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.chips button {
  flex: none; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px;
  font-family: var(--font-display); font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.chips button:hover { border-color: var(--accent); color: var(--accent); }
.sec { padding-top: 40px; scroll-margin-top: 50px; }
.sec > h2 { font-size: 26px; display: flex; gap: 12px; align-items: baseline; }
.sec > h2 i { font-style: normal; font-family: var(--font-ui); font-size: 13px; color: var(--accent); font-weight: 500; }
.sec-body > p, .sec-body > ul, .sec-body > ol { max-width: 68ch; }
.sec-body h3 { font-size: 19px; margin: 26px 0 6px; }
.sec-body li { margin: 4px 0; }
.pager { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 56px; padding-top: 18px; border-top: 1px solid var(--line); font-family: var(--font-display); }
.error { border: 1px solid var(--bad); color: var(--bad); border-radius: var(--radius); padding: 12px 16px; font-family: var(--font-ui); font-size: 13px; }

/* ---------- content blocks ---------- */
.rule, .note, .trap {
  margin: 16px 0; padding: 12px 16px; border-radius: var(--radius); background: var(--surface);
  border: 1px solid var(--line); border-left: 4px solid var(--c1); max-width: 72ch; overflow-x: auto;
}
.note { border-left-color: var(--c3); }
.trap { border-left-color: var(--c4); }
.rule > b:first-child, .note > b:first-child, .trap > b:first-child {
  display: block; font-family: var(--font-ui); font-size: 11.5px; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); font-weight: 500; margin-bottom: 2px;
}
.formulas { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; margin: 16px 0; }
.formulas > div { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; overflow-x: auto; }
.formulas > div small { display: block; font-family: var(--font-ui); font-size: 11.5px; color: var(--muted); }
.katex { font-size: 1.05em; }
.katex-display { margin: .6em 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
table.t { border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 15px; }
table.t th, table.t td { border: 1px solid var(--line); padding: 4px 10px; text-align: center; }
table.t th { background: var(--sunk); font-family: var(--font-display); font-weight: 600; }
.scroll-x { overflow-x: auto; }

/* ---------- widget (interactive bench) ---------- */
.widget { margin: 20px 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.widget > header { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--sunk); }
.widget > header b { font-family: var(--font-display); font-size: 16px; }
.widget > header span { font-size: 14px; color: var(--muted); }
.widget > header::before { content: "TRY IT"; font-family: var(--font-ui); font-size: 10.5px; letter-spacing: .08em; color: var(--accent-ink); background: var(--accent); border-radius: 4px; padding: 1px 6px; align-self: center; }
.w-stage { padding: 14px 16px; overflow-x: auto; }
.w-stage svg { display: block; margin: 0 auto; }
.w-controls { display: flex; flex-wrap: wrap; gap: 10px 22px; align-items: center; padding: 12px 16px; border-top: 1px solid var(--line); }
.w-controls:empty, .w-readout:empty { display: none; }
.w-readout { padding: 12px 16px; border-top: 1px solid var(--line); background: var(--sunk); font-size: 16px; overflow-x: auto; }
.ctl { display: flex; align-items: center; gap: 8px; font-family: var(--font-ui); font-size: 13px; }
.ctl output { min-width: 3.2em; font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 500; }
.ctl input[type="range"] { width: 170px; accent-color: var(--accent); }
.ctl input[type="number"], .ctl input[type="text"], .ctl select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px; font-family: var(--font-ui); font-size: 13px; width: 7em;
}
.ctl select { width: auto; }
.btn {
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 5px 12px;
  font-family: var(--font-ui); font-size: 13px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { color: var(--accent-ink); filter: brightness(1.1); }
.btn:disabled { opacity: .45; cursor: default; }

/* ---------- steps ---------- */
.steps { margin: 20px 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.steps > header { padding: 10px 16px; border-bottom: 1px solid var(--line); font-family: var(--font-display); font-weight: 600; }
.steps ol { margin: 0; padding: 8px 16px 8px 44px; }
.steps li { padding: 6px 0; overflow-x: auto; }
.steps li[hidden] { display: none; }
.steps > footer { display: flex; gap: 8px; align-items: center; padding: 10px 16px; border-top: 1px solid var(--line); font-family: var(--font-ui); font-size: 12.5px; color: var(--muted); }

/* ---------- quiz ---------- */
.quiz { margin: 16px 0; padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.quiz .q { margin: 0 0 10px; }
.quiz .opts { display: flex; flex-wrap: wrap; gap: 8px; }
.quiz .opts button { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 14px; cursor: pointer; min-width: 64px; }
.quiz .opts button:hover:not(:disabled) { border-color: var(--accent); }
.quiz .opts button.right { border-color: var(--good); color: var(--good); font-weight: 600; }
.quiz .opts button.wrong { border-color: var(--bad); color: var(--bad); text-decoration: line-through; }
.quiz .fb { margin-top: 10px; font-size: 15.5px; overflow-x: auto; }
.quiz .fb.ok::before { content: "Correct. "; color: var(--good); font-weight: 600; }
.quiz .fb.no::before { content: "Not quite. "; color: var(--bad); font-weight: 600; }

/* ---------- SVG drawing classes (theme aware) ---------- */
svg text { fill: var(--ink); font-family: var(--font-ui); font-size: 12px; }
svg .grid { stroke: var(--line); stroke-width: 1; opacity: .7; }
svg .axis { stroke: var(--ink); stroke-width: 1.4; }
svg .tick { fill: var(--muted); font-size: 10.5px; }
svg .s0 { stroke: var(--ink); } svg .s1 { stroke: var(--c1); } svg .s2 { stroke: var(--c2); } svg .s3 { stroke: var(--c3); } svg .s4 { stroke: var(--c4); } svg .s5 { stroke: var(--c5); }
svg .sm { stroke: var(--muted); }
svg .f0 { fill: var(--ink); } svg .f1 { fill: var(--c1); } svg .f2 { fill: var(--c2); } svg .f3 { fill: var(--c3); } svg .f4 { fill: var(--c4); } svg .f5 { fill: var(--c5); }
svg .fm { fill: var(--muted); } svg .fbg { fill: var(--surface); }
svg .t1 { fill: var(--c1); } svg .t2 { fill: var(--c2); } svg .t3 { fill: var(--c3); } svg .t4 { fill: var(--c4); } svg .t5 { fill: var(--c5); } svg .tm { fill: var(--muted); }
svg .soft { fill-opacity: .16; }
svg .nofill { fill: none; }
svg .nostroke { stroke: none; }
svg .w1 { stroke-width: 1; } svg .w2 { stroke-width: 2; } svg .w3 { stroke-width: 3; }
svg .dash { stroke-dasharray: 5 4; }
svg .big { font-size: 15px; } svg .bold { font-weight: 700; }
svg .handle { cursor: grab; touch-action: none; }
svg .handle circle { stroke: var(--surface); stroke-width: 2; }
svg .handle:focus-visible circle { stroke: var(--ink); }
svg .handle:active { cursor: grabbing; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }
