:root {
  --bg: #fbfaf7;
  --fg: #1d1f24;
  --muted: #666b75;
  --card: #ffffff;
  --border: #dedbd3;
  --viz-bg: #f4f2ec;
  --accent: #c2410c;      /* hero path, particle */
  --accent-2: #2f6fb0;    /* histograms, trails */
  --accent-3: #7c3aed;    /* second interval / series */
  --faint: rgba(29, 31, 36, 0.18);
  --molecule: rgba(47, 111, 176, 0.45);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b;
    --fg: #e8e6e1;
    --muted: #9aa0aa;
    --card: #1d2025;
    --border: #33373e;
    --viz-bg: #111316;
    --accent: #f08a4b;
    --accent-2: #6aa6e0;
    --accent-3: #b594f5;
    --faint: rgba(232, 230, 225, 0.2);
    --molecule: rgba(106, 166, 224, 0.5);
  }
}
:root[data-theme="dark"] {
  --bg: #15171b;
  --fg: #e8e6e1;
  --muted: #9aa0aa;
  --card: #1d2025;
  --border: #33373e;
  --viz-bg: #111316;
  --accent: #f08a4b;
  --accent-2: #6aa6e0;
  --accent-3: #b594f5;
  --faint: rgba(232, 230, 225, 0.2);
  --molecule: rgba(106, 166, 224, 0.5);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.6 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent-2); }

.site-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  max-width: 880px;
  margin: 0 auto;
  padding: 16px;
  font-size: 14px;
  color: var(--muted);
}
.site-header a { color: var(--fg); font-weight: 600; text-decoration: none; }

main { max-width: 880px; margin: 0 auto; padding: 0 16px 64px; }
h1 { font-size: 2.2rem; line-height: 1.2; margin: 16px 0 8px; }
.lede { color: var(--muted); font-size: 1.1rem; margin: 0 0 32px; }

.section {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 32px;
}
.section h2 { margin: 0 0 8px; font-size: 1.4rem; }
.section h2 .num { color: var(--muted); font-weight: 400; margin-right: 8px; }
.section p { margin: 0 0 12px; }
.formula { overflow-x: auto; margin: 8px 0 16px; }

.symbols {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 6px 16px;
  margin: 0 0 16px;
  padding: 12px 16px;
  background: var(--viz-bg);
  border-radius: 8px;
  font-size: 15px;
}
.symbols dt { text-align: right; white-space: nowrap; }
.symbols dd { margin: 0; }
.plain { margin-bottom: 16px !important; }
@media (max-width: 560px) {
  .symbols { grid-template-columns: 1fr; gap: 2px; }
  .symbols dt { text-align: left; margin-top: 8px; }
}

.viz canvas { display: block; width: 100%; border-radius: 8px; }
.viz-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; }
.viz-row.even canvas { flex: 1 1 260px; width: auto; min-width: 0; }
.viz-row .inc-paths { flex: 3 1 320px; width: auto; min-width: 0; touch-action: pan-y; }
.viz-row .inc-scatter { flex: 2 1 220px; width: auto; min-width: 0; max-width: 340px; }
.caption { font-size: 13px; color: var(--muted); margin: 8px 0 4px; }

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 24px;
  margin: 16px 0 8px;
  font-size: 15px;
}
.controls label { display: flex; align-items: center; gap: 8px; }
.controls input[type=range] { width: 140px; accent-color: var(--accent); }
.controls input[type=checkbox] { accent-color: var(--accent); }
.controls output { min-width: 3.5em; font-variant-numeric: tabular-nums; color: var(--muted); }
.controls button {
  font: inherit;
  padding: 4px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}
.controls button:hover { border-color: var(--accent); }

.stats { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.takeaway {
  border-left: 3px solid var(--accent);
  padding: 4px 0 4px 12px;
  margin-top: 12px !important;
}

.chapters { list-style: none; padding: 0; display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.chapters li { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.chapters li.soon { color: var(--muted); }
.chapters .num { display: block; font-size: 13px; color: var(--muted); }

@media (max-width: 560px) {
  .section { padding: 16px; }
  .controls input[type=range] { width: 110px; }
}

.radio-group { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.radio-group input { accent-color: var(--accent); }

.chapter-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 12px 24px;
  font-size: 15px;
}
.chapter-nav .soon { color: var(--muted); }

canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Predict first: commit to an answer before touching the chart. */
.predict {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  margin: 12px 0 16px;
}
.predict p { margin: 0 0 8px !important; }
.predict-options { display: flex; flex-wrap: wrap; gap: 8px; }
.predict-options button {
  font: inherit;
  font-size: 15px;
  text-align: left;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}
.predict-options button:hover:not(:disabled) { border-color: var(--accent); }
.predict-options button:disabled { cursor: default; opacity: 0.6; }
.predict-options button.chosen { opacity: 1; border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.predict-result { margin: 10px 0 0 !important; font-size: 15px; }

/* Going deeper / Show the code drawers. */
details.drawer {
  border-top: 1px solid var(--border);
  margin-top: 16px;
  padding-top: 10px;
}
details.drawer + details.drawer { margin-top: 0; }
details.drawer summary { cursor: pointer; font-weight: 600; font-size: 15px; color: var(--muted); }
details.drawer summary:hover { color: var(--fg); }
details.drawer[open] summary { margin-bottom: 8px; }
details.drawer p, details.drawer li { font-size: 15px; }
details.drawer .ref { color: var(--muted); font-size: 14px; }
details.drawer pre {
  overflow-x: auto;
  background: var(--viz-bg);
  border-radius: 8px;
  padding: 12px 14px;
  margin: 0;
  font: 13px/1.5 ui-monospace, "Cascadia Code", Consolas, monospace;
}

/* Notation index. */
.notation { width: 100%; border-collapse: collapse; font-size: 15px; }
.notation th, .notation td { text-align: left; vertical-align: top; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.notation th { font-size: 13px; color: var(--muted); font-weight: 600; }
.notation td:first-child { white-space: nowrap; }
.notation td:last-child { white-space: nowrap; font-size: 14px; }
@media (max-width: 560px) {
  .notation td:first-child, .notation td:last-child { white-space: normal; }
}
.notation tr.group th { font-size: 15px; color: var(--fg); padding-top: 24px; border-bottom: 2px solid var(--border); }
.site-header .links { display: flex; flex-wrap: wrap; gap: 4px 12px; justify-content: flex-end; }
.site-header .links a { font-weight: 400; color: var(--accent-2); }
.theme-toggle {
  font: inherit;
  font-size: 13px;
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--fg);
  cursor: pointer;
}
.theme-toggle:hover { border-color: var(--accent); }
.site-header .links { align-items: center; }

/* Wide formulas scroll sideways; edge shadows show there is more (they vanish at either end). */
.formula {
  background:
    linear-gradient(to right, var(--card) 40%, transparent) left / 32px 100% no-repeat local,
    linear-gradient(to left, var(--card) 40%, transparent) right / 32px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.22), transparent) left / 12px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.22), transparent) right / 12px 100% no-repeat scroll;
}
@media (max-width: 560px) {
  .formula { font-size: 0.86em; }
  .symbols dt { white-space: normal; overflow-x: auto; max-width: 100%; }
}
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

/* Progress: finished-section badge, home-page bars and summary. */
h2 .done { font-size: 13px; font-weight: 600; color: var(--accent-2); margin-left: 6px; white-space: nowrap; }
.progress { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 13px; color: var(--muted); }
.progress .bar { flex: 1; height: 6px; border-radius: 3px; background: var(--viz-bg); overflow: hidden; }
.progress .bar > span { display: block; height: 100%; width: 0; background: var(--accent-2); }
.progress-summary { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin: 0 0 16px; color: var(--muted); font-size: 15px; }
.progress-summary button, .pdf-link {
  font: inherit; font-size: 13px; padding: 2px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--card); color: var(--fg); cursor: pointer; text-decoration: none;
}

/* End-of-chapter recap. */
.recap h2 { margin-bottom: 4px; }
.recap .quiz { margin: 16px 0 0; }
.quiz-score { font-weight: 600; margin-top: 16px !important; }

/* Printing and PDF export: paper-friendly, no controls. */
@page { margin: 14mm 14mm 16mm; }
@media print {
  :root { --bg: #fff; --card: #fff; }
  body { font-size: 12pt; }
  .site-header .links, .controls, .chapter-nav, .pdf-link, .theme-toggle, .quiz-score { display: none !important; }
  .predict, .quiz { break-inside: avoid; }
  .section { border: none; padding: 0; margin-bottom: 24pt; }
  .viz, .formula, .symbols, canvas, pre { break-inside: avoid; }
  details.drawer summary { list-style: none; color: var(--fg); }
  details.drawer summary::-webkit-details-marker { display: none; }
  a { color: inherit; }
  pre { white-space: pre-wrap; overflow: visible; }
  .formula { background: none; }
}

/* "Run it here" editor for the code drawers. */
.code-edit {
  display: block;
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  background: var(--viz-bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  font: 13px/1.5 ui-monospace, "Cascadia Code", Consolas, monospace;
  overflow-x: auto;
  white-space: pre;
  tab-size: 4;
}
.code-edit:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.runbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin: 10px 0 6px; font-size: 14px; }
.runbar button {
  font: inherit; padding: 4px 12px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--fg); cursor: pointer;
}
.runbar button.run { border-color: var(--accent); }
.runbar button:disabled { opacity: 0.55; cursor: default; }
.run-status { color: var(--muted); }
.run-output { margin-bottom: 10px !important; }

/* Site footer. */
.site-footer {
  max-width: 880px;
  margin: 0 auto;
  padding: 24px 16px 40px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 24px;
  font-size: 13px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.site-footer a { color: var(--muted); }
.about ul { padding-left: 20px; }
.about li { margin-bottom: 8px; }
@media print { .site-footer { border: none; } .site-footer a { color: inherit; } }

/* Interview practice. */
.practice-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 8px 0 24px; position: sticky; top: 0; z-index: 2; background: var(--bg); padding: 8px 0; }
.practice-bar .solved-count { font-weight: 600; margin-right: 8px; }
.practice-bar button {
  font: inherit; font-size: 14px; padding: 4px 12px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--card); color: var(--fg); cursor: pointer;
}
.practice-bar button.random { border-color: var(--accent); }
.question { border-top: 1px solid var(--border); padding: 18px 0 8px; scroll-margin-top: 56px; transition: background 0.4s; }
.question:first-of-type { border-top: none; }
.question h3 { margin: 0 0 6px; font-size: 1.1rem; }
.question .qnum { color: var(--muted); font-weight: 400; margin-right: 6px; }
.question .q { margin-bottom: 6px !important; }
.question.flash { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.question.is-solved h3 { color: var(--muted); }
.level { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; margin-left: 6px; vertical-align: middle; border: 1px solid var(--border); }
.level.easy { color: var(--accent-2); }
.level.medium { color: var(--accent); }
.level.hard { color: var(--accent-3); }
.solved { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); margin-top: 8px; }
.solved input { accent-color: var(--accent); }
@media print { .practice-bar, .solved { display: none !important; } }
