/* Shared design tokens and base styles. Topics use these; they don't invent colours. */

:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1d1c1a;
  --muted: #6b6862;
  --border: #dcd9d2;
  --accent: #2f5fd0;
  --accent-soft: #e3eafb;
  --good: #1f8a4c;
  --good-soft: #e1f3e8;
  --bad: #b8432f;
  --bad-soft: #f9e6e2;

  /* Tree nodes */
  --node-red: #d23a4a;
  --node-red-text: #ffffff;
  --node-black: #26241f;
  --node-black-text: #ffffff;
  --node-neutral: #47608f;
  --node-warn: #d9531e;
  --cell-low: #1d7f86;
  --cell-high: #8a55bf;
  --node-stroke: #ffffff;
  --edge: #a9a49a;
  --highlight: #f2b632;
  --mark: #2f5fd0;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 2px 8px rgba(0, 0, 0, 0.04);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161614;
    --surface: #1f1f1c;
    --surface-2: #2a2a26;
    --text: #ecebe7;
    --muted: #a19e96;
    --border: #3a3934;
    --accent: #7ea2f2;
    --accent-soft: #24304d;
    --good: #5cc98a;
    --good-soft: #1d3326;
    --bad: #f08a76;
    --bad-soft: #3d241f;
    --node-red: #e04b5a;
    --node-black: #0b0b0a;
    --node-neutral: #4f6aa0;
    --node-warn: #e8692f;
    --cell-low: #25959c;
    --cell-high: #9c6ad0;
    --node-stroke: #8d8a82;
    --edge: #6f6b63;
    --mark: #7ea2f2;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #161614;
  --surface: #1f1f1c;
  --surface-2: #2a2a26;
  --text: #ecebe7;
  --muted: #a19e96;
  --border: #3a3934;
  --accent: #7ea2f2;
  --accent-soft: #24304d;
  --good: #5cc98a;
  --good-soft: #1d3326;
  --bad: #f08a76;
  --bad-soft: #3d241f;
  --node-red: #e04b5a;
  --node-black: #0b0b0a;
  --node-neutral: #4f6aa0;
  --node-warn: #e8692f;
  --cell-low: #25959c;
  --cell-high: #9c6ad0;
  --node-stroke: #8d8a82;
  --edge: #6f6b63;
  --mark: #7ea2f2;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
}

a { color: var(--accent); }
code, kbd { font-family: var(--mono); font-size: 0.9em; }
kbd {
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3em;
  background: var(--surface);
}

.page { max-width: 1080px; margin: 0 auto; padding: 24px 16px 64px; }

header.site {
  display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap;
  margin-bottom: 20px;
}
header.site h1 { margin: 0; font-size: 1.6rem; }
header.site .crumb { color: var(--muted); text-decoration: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; flex-wrap: wrap; }
.tabs button {
  appearance: none; border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 600; padding: 10px 14px; cursor: pointer;
  border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tabs button:hover { color: var(--text); }
[role="tabpanel"][hidden] { display: none; }

/* Buttons and inputs */
button.btn, .btn {
  appearance: none; font: inherit; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 7px 14px; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary kbd { background: transparent; color: inherit; border-color: rgba(255, 255, 255, 0.55); }
.btn:focus-visible, .option:focus-visible, .tabs button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input[type="number"], select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
}
input[type="number"] { width: 5.5em; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar .spacer { flex: 1; }
.muted { color: var(--muted); }

/* Key chips used in narration: [[42]] becomes one of these */
.chip {
  display: inline-block; min-width: 1.9em; padding: 0 0.4em; border-radius: 999px;
  font-weight: 700; font-size: 0.88em; text-align: center; line-height: 1.6;
  font-variant-numeric: tabular-nums;
}
.chip.red { background: var(--node-red); color: var(--node-red-text); }
.chip.black { background: var(--node-black); color: var(--node-black-text); box-shadow: inset 0 0 0 1px var(--node-stroke); }
.chip.plain { background: var(--surface-2); color: var(--text); }
.chip.neutral { background: var(--node-neutral); color: #fff; }
.chip.warn, .chip.pivot { background: var(--node-warn); color: #fff; }
.chip.low { background: var(--cell-low); color: #fff; }
.chip.high { background: var(--cell-high); color: #fff; }
.chip.done { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1.5px var(--good); }
.chip.out { background: var(--node-neutral); color: #fff; opacity: 0.55; }

/* Multiple-choice options */
.options { display: grid; gap: 8px; margin: 12px 0; }
.option {
  appearance: none; text-align: left; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 12px; cursor: pointer; display: flex; gap: 10px; align-items: baseline;
}
.option:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.option:disabled { cursor: default; }
.option .num { color: var(--muted); font-family: var(--mono); font-size: 0.85em; }
.option.correct { border-color: var(--good); background: var(--good-soft); }
.option.chosen-wrong { border-color: var(--bad); background: var(--bad-soft); }

.feedback { border-radius: 8px; padding: 10px 12px; margin-top: 8px; }
.feedback.good { background: var(--good-soft); border: 1px solid var(--good); }
.feedback.bad { background: var(--bad-soft); border: 1px solid var(--bad); }
.feedback p { margin: 0.3em 0; }

/* Tree view (shared/tree-view.js) */
svg.tree { width: 100%; height: auto; display: block; overflow: visible; }
svg.tree .edge { stroke: var(--edge); stroke-width: 2; }
svg.tree .node circle.body {
  stroke: var(--node-stroke); stroke-width: 2;
  transition: fill 0.45s ease;
}
svg.tree .node.red circle.body { fill: var(--node-red); }
svg.tree .node.black circle.body { fill: var(--node-black); }
svg.tree .node text.key {
  fill: #fff; font: 700 16px var(--font); text-anchor: middle; dominant-baseline: central;
  font-variant-numeric: tabular-nums;
}
svg.tree .node .ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.tree .node.hl .ring { opacity: 1; }
svg.tree .node.floating circle.body { stroke: var(--highlight); stroke-dasharray: 4 3; }
svg.tree .mark { fill: var(--mark); font: 700 14px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.tree .nil rect { fill: var(--node-black); stroke: var(--node-stroke); stroke-width: 1.5; }
svg.tree .nil .edge { stroke-dasharray: 4 4; }
svg.tree .nil text.nil-label { fill: var(--muted); font: 600 10px var(--font); text-anchor: middle; }
svg.tree .entering { animation: fade-in 0.35s ease; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  svg.tree .node circle.body, svg.tree .node .ring { transition: none; }
  svg.tree .entering { animation: none; }
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

svg.tree .mark-bg { fill: var(--surface); stroke: var(--mark); stroke-width: 1.5; }
svg.tree .node.neutral circle.body { fill: var(--node-neutral); }
svg.tree .node.warn circle.body { fill: var(--node-warn); }
svg.tree .note { fill: var(--muted); font: 600 11px var(--font); text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }

/* Array view (shared/array-view.js) */
svg.array { width: 100%; height: auto; display: block; overflow: visible; }
svg.array .cell rect.body { fill: var(--node-neutral); stroke: var(--node-stroke); stroke-width: 2; transition: fill 0.45s ease; }
svg.array .cell.sorted rect.body { fill: var(--good); }
svg.array .cell.low rect.body { fill: var(--cell-low); }
svg.array .cell.high rect.body { fill: var(--cell-high); }
svg.array .cell.pivot rect.body { fill: var(--node-warn); }
svg.array .cell.done rect.body { fill: var(--surface-2); stroke: var(--good); }
svg.array .cell.done text.val { fill: var(--text); }
svg.array .cell.out { opacity: 0.4; }
svg.array .hole { fill: none; stroke: var(--border); stroke-width: 2; stroke-dasharray: 5 4; }
svg.array .slot { fill: var(--surface-2); stroke: var(--border); stroke-width: 1.5; }
svg.array .row-title { fill: var(--muted); font: 700 12px var(--font); letter-spacing: 0.04em; }
svg.array .pointer { fill: var(--text); font: 700 12px var(--mono); text-anchor: middle; dominant-baseline: central; }
svg.array .cell rect.ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.array .cell.hl rect.ring { opacity: 1; }
svg.array .cell text.val {
  fill: #fff; font: 700 16px var(--font); text-anchor: middle; dominant-baseline: central;
  font-variant-numeric: tabular-nums;
}
svg.array .mark { fill: var(--mark); font: 700 13px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.array .mark-bg { fill: var(--surface); stroke: var(--mark); stroke-width: 1.5; }
svg.array .index { fill: var(--muted); font: 600 12px var(--mono); text-anchor: middle; dominant-baseline: central; }
svg.array .divider { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 4 3; }
svg.array .region { fill: var(--muted); font: 600 11px var(--font); text-anchor: middle; text-transform: uppercase; letter-spacing: 0.05em; }
svg.array .entering { animation: fade-in 0.35s ease; }
@media (prefers-reduced-motion: reduce) {
  svg.array .cell rect.body, svg.array .cell rect.ring { transition: none; }
  svg.array .entering { animation: none; }
}

/* Graph view (shared/graph-view.js) */
svg.graph { width: 100%; height: auto; display: block; overflow: visible; }
svg.graph .edge line { stroke: var(--edge); stroke-width: 2.5; transition: stroke 0.35s, stroke-width 0.35s; }
svg.graph .edge.tree line { stroke: var(--accent); stroke-width: 5; }
svg.graph .edge.active line { stroke: var(--highlight); stroke-width: 5; }
svg.graph .edge.candidate line { stroke: var(--accent); stroke-width: 3; stroke-dasharray: 7 6; }
svg.graph .edge.candidate .arrow { fill: var(--accent); }
svg.graph .edge.dim line, svg.graph .edge.dim .arrow { opacity: 0.3; }
svg.graph .edge .arrow { fill: var(--edge); transition: fill 0.35s; }
svg.graph .edge.tree .arrow { fill: var(--accent); }
svg.graph .edge.active .arrow { fill: var(--highlight); }
svg.graph .weight-bg { fill: var(--surface); stroke: var(--edge); stroke-width: 1.5; }
svg.graph .weight { fill: var(--text); font: 700 13px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.graph .gnode circle.body { fill: var(--surface); stroke: var(--node-neutral); stroke-width: 2.5; transition: fill 0.35s, stroke 0.35s; }
svg.graph .gnode text.label { fill: var(--text); font: 700 17px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.graph .gnode.frontier circle.body { fill: var(--accent-soft); stroke: var(--accent); }
svg.graph .gnode.current circle.body { fill: var(--node-warn); stroke: var(--node-warn); }
svg.graph .gnode.current text.label { fill: #fff; }
svg.graph .gnode.visited circle.body { fill: var(--node-neutral); stroke: var(--node-neutral); }
svg.graph .gnode.visited text.label { fill: #fff; }
svg.graph .gnode .ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.graph .gnode.hl .ring { opacity: 1; }
svg.graph .mark { fill: var(--mark); font: 700 13px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.graph .mark-bg { fill: var(--surface); stroke: var(--mark); stroke-width: 1.5; }
svg.graph .note { fill: var(--muted); font: 700 14px var(--font); text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: var(--surface); stroke-width: 5px; stroke-linejoin: round; }
svg.graph .panel-title { fill: var(--muted); font: 700 14px var(--font); letter-spacing: 0.04em; }
svg.graph .panel-note { fill: var(--text); font: 700 15px var(--font); text-anchor: end; }
svg.graph .panel-empty { fill: var(--muted); font: italic 15px var(--font); }
svg.graph .panel-item rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.5; }
svg.graph .panel-item.next rect { stroke-width: 3.5; }
svg.graph .panel-item text { fill: var(--text); font: 700 17px var(--font); text-anchor: middle; dominant-baseline: central; }
@media (prefers-reduced-motion: reduce) {
  svg.graph .edge line, svg.graph .gnode circle.body, svg.graph .gnode .ring { transition: none; }
}
.chip.frontier { background: var(--accent-soft); color: var(--text); box-shadow: inset 0 0 0 1.5px var(--accent); }
.chip.current { background: var(--node-warn); color: #fff; }
.chip.visited { background: var(--node-neutral); color: #fff; }

/* Table view (shared/table-view.js) */
svg.table { width: 100%; height: auto; display: block; overflow: visible; }
svg.table .slot rect { fill: var(--surface); stroke: var(--border); stroke-width: 2; transition: stroke 0.25s; }
svg.table .slot.probe rect { stroke: var(--highlight); stroke-width: 4; }
svg.table .slot.found rect { stroke: var(--good); stroke-width: 4; }
svg.table .index { fill: var(--muted); font: 600 13px var(--mono); text-anchor: middle; dominant-baseline: central; }
svg.table .nil { fill: var(--muted); font: 600 16px var(--font); text-anchor: middle; dominant-baseline: central; opacity: 0.6; }
svg.table .pointer { fill: var(--edge); }
svg.table .tomb { fill: var(--bad); font: 700 12px var(--mono); text-anchor: middle; dominant-baseline: central; letter-spacing: 0.04em; }
svg.table .link { stroke: var(--edge); stroke-width: 2; }
svg.table .key rect.body { fill: var(--node-neutral); stroke: var(--node-stroke); stroke-width: 1.5; }
svg.table .key.floating rect.body { fill: var(--node-warn); stroke-dasharray: 4 3; }
svg.table .key rect.ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.table .key.hl rect.ring { opacity: 1; }
svg.table .key text.val { fill: #fff; font: 700 16px var(--font); text-anchor: middle; dominant-baseline: central; font-variant-numeric: tabular-nums; }
svg.table .entering { animation: fade-in 0.35s ease; }
@media (prefers-reduced-motion: reduce) {
  svg.table .slot rect, svg.table .key rect.ring { transition: none; }
  svg.table .entering { animation: none; }
}

/* Forest view (shared/forest-view.js) */
svg.forest { width: 100%; height: auto; display: block; overflow: visible; }
svg.forest .pointer { stroke: var(--edge); stroke-width: 2; marker-end: none; }
svg.forest .fnode circle.body { fill: var(--surface); stroke: var(--node-neutral); stroke-width: 2.5; }
svg.forest .fnode.root circle.body { fill: var(--node-neutral); }
svg.forest .fnode text.label { fill: var(--text); font: 700 15px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.forest .fnode.root text.label { fill: #fff; }
svg.forest .fnode .ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.forest .fnode.hl .ring { opacity: 1; }
svg.forest .note { fill: var(--muted); font: 700 12px var(--font); text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; }
.stage .forest-caption { margin: 10px 4px 0; font-size: 0.85rem; color: var(--muted); font-weight: 600; letter-spacing: 0.03em; }
.chip.sorted { background: var(--good); color: #fff; }

/* Grid view (shared/grid-view.js) */
svg.grid { width: 100%; height: auto; display: block; }
svg.grid text { text-anchor: middle; dominant-baseline: central; font-family: var(--font); font-weight: 700; fill: var(--text); font-variant-numeric: tabular-nums; }
svg.grid .head rect { fill: var(--surface-2); stroke: none; }
svg.grid .head.marked rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
svg.grid .cell rect { fill: var(--surface); stroke: var(--border); stroke-width: 1.5; transition: fill 0.3s, stroke 0.3s; }
svg.grid .cell.empty rect { fill: var(--bg); }
svg.grid .cell.source rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2.5; }
svg.grid .cell.current rect { fill: var(--surface); stroke: var(--highlight); stroke-width: 4; }
svg.grid .cell.path rect { fill: var(--good-soft); stroke: var(--good); stroke-width: 2.5; }
svg.grid .cell.current.path rect { stroke: var(--highlight); stroke-width: 4; }
svg.grid text.footer { text-anchor: start; fill: var(--muted); font-weight: 600; }
@media (prefers-reduced-motion: reduce) { svg.grid .cell rect { transition: none; } }

/* B-tree view (shared/btree-view.js) */
svg.btree { width: 100%; height: auto; display: block; overflow: visible; }
svg.btree .bnode { fill: var(--node-neutral); stroke: var(--node-stroke); stroke-width: 2; transition: fill 0.3s; }
svg.btree .bnode.current { stroke: var(--highlight); stroke-width: 4; }
svg.btree .bnode.overflow { fill: var(--node-warn); }
svg.btree .bsep { stroke: rgba(255, 255, 255, 0.45); stroke-width: 1.5; }
svg.btree .bedge { stroke: var(--edge); stroke-width: 2; }
svg.btree .bkey text { fill: #fff; font: 700 16px var(--font); text-anchor: middle; dominant-baseline: central; font-variant-numeric: tabular-nums; }
svg.btree .bkey .ring { fill: none; stroke: var(--highlight); stroke-width: 3; opacity: 0; }
svg.btree .bkey.hl .ring { opacity: 1; }
svg.btree .bkey.floating .ring { opacity: 1; fill: var(--node-warn); stroke-dasharray: 4 3; }
svg.btree .entering { animation: fade-in 0.35s ease; }
@media (prefers-reduced-motion: reduce) { svg.btree .bnode { transition: none; } svg.btree .entering { animation: none; } }

/* Trie view (shared/trie-view.js) */
svg.trie { width: 100%; height: auto; display: block; overflow: visible; }
svg.trie .tnode { transition: transform 0.5s ease; }
svg.trie .tnode circle.body { fill: var(--surface); stroke: var(--node-neutral); stroke-width: 2.5; transition: fill 0.3s, stroke 0.3s; }
svg.trie .tnode.root circle.body { fill: var(--node-neutral); }
svg.trie .tnode circle.end { fill: none; }
svg.trie .tnode.is-end circle.end { fill: var(--node-neutral); }
svg.trie .tnode .ring { fill: none; stroke: var(--highlight); stroke-width: 4; opacity: 0; transition: opacity 0.25s; }
svg.trie .tnode.current .ring { opacity: 1; }
svg.trie .tnode.path circle.body, svg.trie .tnode.current circle.body { stroke: var(--accent); fill: var(--accent-soft); }
svg.trie .tnode.match circle.body { stroke: var(--good); fill: var(--good-soft); }
svg.trie .tnode.removed circle.body { stroke: var(--bad); stroke-dasharray: 4 3; }
svg.trie .tnode.removed { opacity: 0.55; }
svg.trie .tedge line { stroke: var(--edge); stroke-width: 2; }
svg.trie .tedge.path line, svg.trie .tedge.current line { stroke: var(--accent); stroke-width: 4; }
svg.trie .tedge.match line { stroke: var(--good); stroke-width: 3; }
svg.trie .tedge.removed line { stroke: var(--bad); stroke-dasharray: 5 4; }
svg.trie .tedge text { fill: var(--text); font: 700 15px var(--font); text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: var(--surface); stroke-width: 5px; stroke-linejoin: round; }
svg.trie .wletter rect { fill: var(--surface-2); stroke: var(--border); stroke-width: 1.5; }
svg.trie .wletter.done rect { fill: var(--accent-soft); stroke: var(--accent); }
svg.trie .wletter.at rect { fill: var(--surface); stroke: var(--highlight); stroke-width: 3.5; }
svg.trie .wletter text { fill: var(--text); font: 700 17px var(--font); text-anchor: middle; dominant-baseline: central; }
svg.trie .entering { animation: fade-in 0.35s ease; }
@media (prefers-reduced-motion: reduce) {
  svg.trie .tnode, svg.trie .tnode circle.body, svg.trie .tnode .ring { transition: none; }
  svg.trie .entering { animation: none; }
}
