:root {
  --bg: #f7f5f0;
  --surface: #fffdf9;
  --surface2: #efece4;
  --text: #1c1b19;
  --muted: #5f5b53;
  --line: #ddd8cc;
  --accent: #1f5fbf;
  --accent-ink: #ffffff;
  --accent-soft: #e2ebf8;
  --warn: #b3261e;
  --ok: #1d7a4a;
  --shadow: 0 1px 2px rgba(40, 30, 10, .05), 0 6px 18px rgba(40, 30, 10, .05);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, Menlo, Consolas, "DejaVu Sans Mono", monospace;
  /* plot inks */
  --c-f: #1f5fbf;
  --c-d: #d0570f;
  --c-c: #7a3fb0;
  --c-cd: #b0368a;
  --c-best: #8a857a;
  --c-cur: #1f5fbf;
  --c-batch: #d0570f;
  --o1: #d1495b; --o2: #1f7ae0; --o3: #1d9a6c; --o4: #b8860b; --o5: #8e44ad; --o6: #e8590c;
  --in: #1d9a6c;
  --out: #d1495b;
  --map-ink: #1c1b19;
  --band: rgba(31, 95, 191, .13);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #121316;
  --surface: #1a1c20;
  --surface2: #23262c;
  --text: #ebe8e1;
  --muted: #a39f95;
  --line: #31343b;
  --accent: #6aa5ff;
  --accent-ink: #0d0f12;
  --accent-soft: #1d2b42;
  --warn: #ff8a80;
  --ok: #6fd3a0;
  --shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 8px 24px rgba(0, 0, 0, .25);
  --c-f: #6aa5ff;
  --c-d: #ffa64d;
  --c-c: #c49bff;
  --c-cd: #ff8fcf;
  --c-best: #9a968c;
  --c-cur: #6aa5ff;
  --c-batch: #ffa64d;
  --o1: #ff6b7d; --o2: #5fa8ff; --o3: #3ddc97; --o4: #ffd166; --o5: #c792ea; --o6: #ff9f43;
  --in: #3ddc97;
  --out: #ff6b7d;
  --map-ink: #ffffff;
  --band: rgba(106, 165, 255, .16);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--sans); -webkit-font-smoothing: antialiased; overflow-x: hidden; }
a { color: var(--accent); }
.small { font-size: 13px; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.hidden { display: none !important; }
.grow { flex: 1 1 160px; }
.row { display: flex; align-items: flex-end; gap: 12px; }
.row.wrap { flex-wrap: wrap; }
.gap { gap: 10px; }
.col { display: flex; flex-direction: column; gap: 10px; }

/* header */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; white-space: nowrap; }
.backlink { text-decoration: none; color: var(--muted); }
.backlink:hover { color: var(--text); }
.sep { color: var(--line); }
.topactions { display: flex; align-items: center; gap: 10px; }
.badge { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; white-space: nowrap; }

main { max-width: 1180px; margin: 0 auto; padding: 0 20px 40px; }
.hero { padding: 36px 0 12px; max-width: 760px; }
.kicker { text-transform: uppercase; letter-spacing: .12em; font-size: 12px; color: var(--accent); margin: 0 0 6px; font-weight: 600; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: clamp(30px, 5vw, 46px); line-height: 1.08; margin: 0 0 12px; }
h2 { font-size: clamp(22px, 3vw, 30px); margin: 0 0 6px; }
h3 { font-size: 19px; margin: 0 0 10px; }
h4 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; margin: 14px 0 6px; color: var(--muted); font-family: var(--sans); }
.lede { font-size: 18px; color: var(--muted); margin: 0; font-family: var(--serif); }

/* lesson tabs */
.lessons { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 18px 0 22px; overflow-x: auto; scrollbar-width: none; }
.lessons::-webkit-scrollbar { display: none; }
.lessons [role=tab] { appearance: none; background: none; border: 0; border-bottom: 3px solid transparent; color: var(--muted); font: 600 15px var(--sans); padding: 10px 14px 9px; cursor: pointer; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.lessons [role=tab]:hover { color: var(--text); }
.lessons [role=tab][aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.lessons .num { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; font-size: 12px; background: var(--surface2); color: var(--muted); }
.lessons [aria-selected="true"] .num { background: var(--accent); color: var(--accent-ink); }
.lessons [role=tab]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 6px; }

.lesson-head { max-width: 820px; margin-bottom: 12px; }
.lesson-head p { margin: 0; font-size: 16px; }

/* layout */
.grid2 { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); gap: 18px; margin-bottom: 18px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card + .card { margin-top: 0; }
.lesson > .card { margin-bottom: 18px; }
.controls { margin: 10px 0; }

/* controls */
button { font: 600 14px var(--sans); border-radius: 9px; padding: 8px 14px; cursor: pointer; border: 1px solid var(--line); background: var(--surface); color: var(--text); min-height: 38px; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); min-width: 84px; }
button.primary:hover { filter: brightness(1.08); }
button.secondary { background: var(--surface2); }
button.secondary:hover, button.ghost:hover { border-color: var(--muted); }
button.ghost { background: transparent; }
.iconbtn { padding: 4px 10px; min-height: 34px; font-size: 16px; }
.field { display: block; font-size: 13px; color: var(--muted); min-width: 0; }
.field select, .field input { display: block; margin-top: 4px; }
.field select { width: 100%; }
.field input[type=number] { width: 100%; }
.field output { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
select, input[type=number] { font: 14px var(--sans); color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 7px 8px; min-height: 38px; max-width: 100%; }
input[type=range] { width: 100%; accent-color: var(--accent); min-height: 26px; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); cursor: pointer; min-height: 38px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.examples { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 16px; }
.chip { border-radius: 999px; padding: 6px 13px; font-weight: 500; font-size: 13px; min-height: 34px; background: var(--surface); }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.seg { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.segbtn { font-weight: 500; font-size: 13px; padding: 6px 11px; min-height: 34px; }
.segbtn.on { background: var(--text); color: var(--bg); border-color: var(--text); }

/* plots */
.plotwrap { width: 100%; min-width: 0; }
svg.plot { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
.plot .grid { stroke: var(--line); stroke-width: 1; }
.plot .zero { stroke: var(--muted); stroke-width: 1; opacity: .6; }
.plot .plotframe { fill: none; stroke: var(--line); }
.plot .tick { fill: var(--muted); font: 11px var(--sans); font-variant-numeric: tabular-nums; }
.plot .axlabel { fill: var(--muted); font: 12px var(--sans); }
.plot .curve { fill: none; stroke-width: 2.4; stroke-linejoin: round; stroke-linecap: round; }
.plot .s-f { stroke: var(--c-f); }
.plot .s-d { stroke: var(--c-d); stroke-dasharray: 7 5; }
.plot .s-c { stroke: var(--c-c); }
.plot .s-cd { stroke: var(--c-cd); stroke-dasharray: 2 4; }
.plot .s-best { stroke: var(--c-best); stroke-dasharray: 6 4; stroke-width: 1.8; }
.plot .s-cur { stroke: var(--c-cur); }
.plot .s-ghost { stroke: var(--muted); opacity: .45; stroke-width: 1.6; }
.plot .s-normal { stroke: var(--c-d); stroke-width: 2; }
.plot .pt.s-f { fill: var(--c-f); stroke: var(--surface); stroke-width: 2; }
.plot .pt.s-d { fill: var(--c-d); stroke: var(--surface); stroke-width: 2; }
.plot .tangent { fill: none; stroke: var(--text); stroke-width: 1.4; opacity: .55; }
.plot .probe { stroke: var(--muted); stroke-dasharray: 3 3; }
.plot .hit { fill: transparent; cursor: crosshair; }
.plot .pt-data { fill: var(--muted); opacity: .75; }
.plot .pt-batch { fill: var(--c-batch); stroke: var(--surface); stroke-width: 2; }
.plot .resid { stroke: var(--c-batch); stroke-width: 1.5; stroke-dasharray: 3 3; fill: none; }
.plot .band { fill: var(--band); stroke: none; }
.plot .bar { fill: var(--c-f); opacity: .55; }

.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 13px; color: var(--muted); margin: 6px 0; }
.key { display: inline-block; width: 22px; height: 0; border-top: 3px solid; vertical-align: middle; margin-left: 6px; }
.key:first-child { margin-left: 0; }
.legitem { display: inline-flex; align-items: center; gap: 6px; margin-right: 10px; }
.legitem .key { margin-left: 0; }
.k-f { border-color: var(--c-f); }
.k-d { border-top-style: dashed; border-color: var(--c-d); }
.k-c { border-color: var(--c-c); }
.k-cd { border-top-style: dotted; border-color: var(--c-cd); }
.k-t { border-color: var(--text); opacity: .55; border-top-width: 2px; }
.k-best { border-top-style: dashed; border-color: var(--c-best); }
.k-cur { border-color: var(--c-cur); }
.k-batch { width: 12px; height: 12px; border: 0; border-radius: 50%; background: var(--c-batch); }

.readout { font: 13px/1.6 var(--mono); background: var(--surface2); border-radius: 10px; padding: 10px 12px; margin: 10px 0 0; overflow-wrap: anywhere; }
.readout .warn { color: var(--warn); font-family: var(--sans); font-weight: 600; margin-top: 4px; }
.readout .note { color: var(--text); font-family: var(--sans); font-weight: 600; margin-top: 4px; }
.readout .ok { color: var(--ok); font-family: var(--sans); font-weight: 600; margin-top: 4px; }

/* notes */
.notes p { margin: 0 0 10px; }
.formulas { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 12px; font-size: 14px; }
.formulas dt { color: var(--muted); }
.formulas dd { margin: 0; overflow-wrap: anywhere; }
.proscons { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.proscons ul { margin: 0; padding-left: 18px; font-size: 14px; }
.proscons li { margin-bottom: 4px; }
.vanish { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 4px; }

/* maps */
.mapwrap { width: 100%; max-width: 620px; margin: 0 auto; }
.mapwrap.small-map { max-width: 420px; margin: 0 0 6px; }
.mapwrap canvas { display: block; border-radius: 10px; cursor: crosshair; touch-action: manipulation; }
#mcMap { cursor: default; }

.optlist { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.optrow { display: flex; align-items: center; gap: 8px; }
.optrow .check { flex: 1 1 auto; }
.optrow .lr { width: 92px; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
#optTable td:last-child { white-space: normal; min-width: 120px; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
td.num { font-variant-numeric: tabular-nums; }
#actTable tbody tr { cursor: pointer; }
#actTable tbody tr:hover { background: var(--surface2); }
.cheat summary { cursor: pointer; font-family: var(--serif); font-size: 18px; font-weight: 600; }
.cheat[open] summary { margin-bottom: 10px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-top: 12px; }
.stats div { background: var(--surface2); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stats span { font-size: 12px; color: var(--muted); }
.stats strong { font: 600 14px var(--mono); overflow-wrap: anywhere; }

.foot { max-width: 1180px; margin: 0 auto; padding: 24px 20px 40px; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.foot p { margin: 4px 0; }

@media (max-width: 900px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 560px) {
  main { padding: 0 16px 32px; }
  .topbar { padding: 8px 16px; }
  .badge { display: none; }
  .hero { padding-top: 22px; }
  .card { padding: 14px; border-radius: 12px; }
  .proscons { grid-template-columns: 1fr; }
  .lessons [role=tab] { padding: 10px 10px 9px; font-size: 14px; }
  .foot { padding: 20px 16px 32px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
