:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6d68;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-wash: #e6f0fc;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --train: #cde2fb;
  --test: #eb6834;
  --good: #006300;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 6px 24px rgba(11, 11, 11, 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #252523;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #a09e97;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-wash: #16263a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --train: #184f95;
    --test: #d95926;
    --good: #0ca30c;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #252523;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #a09e97;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-wash: #16263a;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --train: #184f95;
  --test: #d95926;
  --good: #0ca30c;
  --shadow: none;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: 72px; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-ink); }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

h1, h2, h3, h4 { line-height: 1.2; margin: 0 0 0.5em; }
h1 { font-size: clamp(2rem, 5vw, 3.2rem); letter-spacing: -0.02em; }
h1 em { font-style: normal; color: var(--accent-ink); }
h2 { font-size: clamp(1.5rem, 3.2vw, 2rem); letter-spacing: -0.01em; }
h3 { font-size: 1.15rem; }
h4 { font-size: 0.95rem; color: var(--text-2); font-weight: 600; }
p { margin: 0 0 1em; }
code { font-size: 0.9em; background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 4px; }

.small { font-size: 0.875rem; }
.muted { color: var(--muted); }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px max(16px, calc((100% - 1040px) / 2));
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
.topbar nav { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.topbar nav a {
  color: var(--text-2);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 6px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.topbar nav a:hover { background: var(--surface-2); color: var(--text); }
.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  flex: none;
  display: grid;
  place-items: center;
}
.theme-toggle svg { width: 18px; height: 18px; }

main { max-width: 1040px; margin: 0 auto; padding: 0 16px; }

/* Hero */
.hero { padding: clamp(48px, 10vw, 112px) 0 48px; max-width: 800px; }
.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent-ink);
}
.lead { font-size: 1.15rem; color: var(--text-2); max-width: 680px; }
.button {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  padding: 12px 22px;
  border-radius: 999px;
}
.button:hover { filter: brightness(1.08); }

/* Sections */
.section { padding: 56px 0; border-top: 1px solid var(--border); }
.section-lead { color: var(--text-2); max-width: 720px; font-size: 1.05rem; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-top: 24px; }
.card, .demo, .folds, .callout, .viz-root, .takeaway, .rung {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card { padding: 24px; }
.card-icon { font-size: 1.8rem; margin-bottom: 8px; }
.note { background: var(--accent-wash); border-radius: 10px; padding: 12px 14px; font-size: 0.95rem; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; padding: 0; margin: 0; }
.tags li { background: var(--surface-2); border-radius: 999px; padding: 2px 10px; font-size: 0.85rem; color: var(--text-2); }

/* N-gram demo */
.demo { padding: 24px; margin-top: 24px; }
.demo-inputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.field span { display: block; font-weight: 600; margin-bottom: 6px; }
textarea {
  width: 100%;
  resize: vertical;
  font: 0.95rem/1.5 var(--font);
  color: var(--text);
  background: var(--page);
  border: 1px solid var(--axis);
  border-radius: 10px;
  padding: 10px 12px;
}
.demo-controls { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 20px; }
.chip-button {
  border: 1px solid var(--axis);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font: 600 0.875rem var(--font);
  cursor: pointer;
}
.chip-button:hover { background: var(--surface-2); }
.demo-output { display: grid; gap: 20px; }
.meter-label { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.meter-label strong { font-size: 1.6rem; }
.meter { height: 12px; background: var(--train); border-radius: 999px; overflow: hidden; }
.meter-fill { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width 0.3s ease; }
.grams { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.gram-list { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.gram-list li {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  background: var(--surface-2);
  border-radius: 6px;
  padding: 3px 8px;
  white-space: pre;
}
.gram-list li.shared { background: var(--accent-wash); box-shadow: inset 0 0 0 1px var(--accent); }
.gram-list li b { font-weight: 600; color: var(--text-2); margin-left: 6px; }

/* Toolbox ladder */
.ladder { list-style: none; padding: 0; margin: 24px 0 0; display: grid; gap: 12px; }
.rung { padding: 20px 24px; }
.rung p { margin: 0; color: var(--text-2); }
.rung-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.badge { font-size: 0.75rem; font-weight: 700; padding: 3px 10px; border-radius: 999px; }
.badge-done { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good); }
.badge-next { background: var(--surface-2); color: var(--text-2); }
.badge-done::before { content: "✓ "; }

/* Folds */
.folds { padding: 24px; margin-top: 24px; }
.fold-row { display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px; margin: 16px 0 12px; }
.fold {
  height: 44px;
  border-radius: 6px;
  background: var(--train);
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-2);
  transition: background 0.25s ease;
}
.fold.test { background: var(--test); color: #fff; }
.fold-controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.legend-inline { display: flex; gap: 16px; margin-top: 12px; font-size: 0.875rem; color: var(--text-2); }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.swatch-train { background: var(--train); }
.swatch-test { background: var(--test); }
.callout { padding: 20px 24px; margin-top: 16px; border-left: 4px solid var(--accent); }
.callout p { margin: 0; color: var(--text-2); }

/* Results chart */
.viz-root { padding: 24px; margin: 24px 0 0; position: relative; }
.viz-root figcaption h3 { margin-bottom: 4px; }
.legend { list-style: none; padding: 0; margin: 12px 0 8px; display: flex; flex-wrap: wrap; gap: 16px; font-size: 0.875rem; color: var(--text-2); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.panels { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 24px; margin-top: 8px; }
.panel h4 { margin-bottom: 2px; color: var(--text); }
.panel .metric { font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
.panel svg { display: block; width: 100%; height: auto; overflow: visible; }
.panel .grid-line { stroke: var(--grid); stroke-width: 1; }
.panel .baseline { stroke: var(--axis); stroke-width: 1; }
.panel .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.panel .bar-label { fill: var(--text-2); font-size: 12px; }
.panel .value { fill: var(--text); font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.panel .hit { fill: transparent; cursor: default; }
.panel .bar { transition: opacity 0.15s ease; }
.panel .row:hover .bar, .panel .row:focus .bar { opacity: 0.85; }
.panel .row:focus { outline: none; }
.panel .row:focus-visible .hit { stroke: var(--accent); stroke-width: 2; }
.tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.8rem;
  line-height: 1.4;
  white-space: nowrap;
  z-index: 5;
}
.tooltip .tt-key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.table-view { margin-top: 20px; }
.table-view summary { cursor: pointer; font-weight: 600; color: var(--accent-ink); font-size: 0.9rem; }
.table-scroll { overflow-x: auto; }
table { border-collapse: collapse; margin-top: 12px; font-size: 0.875rem; min-width: 100%; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
th { color: var(--text-2); font-weight: 600; }

.takeaways { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin: 20px 0 12px; }
.takeaway { padding: 20px; }
.takeaway .big { font-size: 2.4rem; font-weight: 700; margin: 0 0 4px; color: var(--accent-ink); }
.takeaway p:last-child { margin: 0; color: var(--text-2); }

/* Timeline */
.timeline { list-style: none; padding: 0; margin: 24px 0 0; display: grid; gap: 20px; }
.timeline li { display: grid; grid-template-columns: 72px 1fr; gap: 16px; }
.timeline .when { font-weight: 700; color: var(--accent-ink); padding-top: 2px; }
.timeline p { margin: 0; color: var(--text-2); }

/* Glossary */
.glossary { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px 24px; margin: 24px 0 0; }
.glossary div { padding: 12px 0; border-bottom: 1px solid var(--grid); }
.glossary dt { font-weight: 700; }
.glossary dd { margin: 4px 0 0; color: var(--text-2); }

.footer {
  max-width: 1040px;
  margin: 48px auto 0;
  padding: 32px 16px 48px;
  border-top: 1px solid var(--border);
  color: var(--text-2);
  font-size: 0.9rem;
}

@media (max-width: 640px) {
  .topbar nav { display: none; }
  .theme-toggle { margin-left: auto; }
  .fold { height: 36px; font-size: 0.7rem; }
  .timeline li { grid-template-columns: 1fr; gap: 4px; }
}

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