/* RL Study Guide — shared styles
   Tokens first; light is default, dark via OS setting or the theme toggle. */
:root {
  color-scheme: light;
  --bg: #f8f5ef;
  --surface: #fffdf9;
  --surface-2: #f2ede4;
  --border: #e6dfd3;
  --text: #24211c;
  --text-2: #56514a;
  --text-3: #7d776d;
  --accent: #2a78d6;
  --accent-soft: #e3eefb;
  --key: #2a78d6;      --key-bg: #eef4fc;
  --exam: #b26b00;     --exam-bg: #fdf4e3;
  --pit: #c23b3a;      --pit-bg: #fcecec;
  --def: #1b8a63;      --def-bg: #e8f6f0;
  --code-bg: #f4f2ed;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --grid: #ebe9e4;
  --node-fill: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
  --sidebar-w: 270px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Source Serif 4", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --read-size: 18px;
  --chap: var(--accent);
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #151412; --surface: #1c1b18; --surface-2: #252320; --border: #35322c;
    --text: #ece7dd; --text-2: #c4beb2; --text-3: #8f897e;
    --accent: #5598e7; --accent-soft: #1c2a3d;
    --key: #5598e7;  --key-bg: #17222f;
    --exam: #e0a33a; --exam-bg: #2a2114;
    --pit: #e66767;  --pit-bg: #2c1919;
    --def: #3cc28f;  --def-bg: #14261f;
    --code-bg: #202020;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --grid: #2c2a26; --node-fill: #1c1b18;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151412; --surface: #1c1b18; --surface-2: #252320; --border: #35322c;
  --text: #ece7dd; --text-2: #c4beb2; --text-3: #8f897e;
  --accent: #5598e7; --accent-soft: #1c2a3d;
  --key: #5598e7;  --key-bg: #17222f;
  --exam: #e0a33a; --exam-bg: #2a2114;
  --pit: #e66767;  --pit-bg: #2c1919;
  --def: #3cc28f;  --def-bg: #14261f;
  --code-bg: #202020;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --grid: #2c2a26; --node-fill: #1c1b18;
  --shadow: none;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 72px; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.65 var(--font);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img, svg { max-width: 100%; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 650; color: var(--text); white-space: nowrap; }
.topbar .brand span { color: var(--text-3); font-weight: 500; }
.topbar .spacer { flex: 1; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: 36px; min-width: 36px; padding: 0 10px; gap: 6px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text-2); font: inherit; font-size: 14px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--text-3); }
.menu-btn { display: none; }

.layout { display: flex; align-items: flex-start; }
.sidebar {
  position: sticky; top: 56px; flex: 0 0 var(--sidebar-w);
  height: calc(100vh - 56px); overflow-y: auto;
  padding: 18px 12px 40px; border-right: 1px solid var(--border);
  background: var(--surface);
}
.sidebar h4 {
  margin: 18px 10px 6px; font-size: 11px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--text-3); font-weight: 650;
}
.sidebar h4:first-child { margin-top: 0; }
.sidebar a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: 7px; color: var(--text-2); font-size: 14.5px;
}
.sidebar a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.sidebar a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sidebar .num {
  flex: 0 0 24px; height: 24px; border-radius: 6px; display: inline-grid; place-items: center;
  font-size: 12px; font-weight: 650; background: var(--surface-2); color: var(--text-2);
}
.sidebar a.active .num { background: var(--accent); color: #fff; }
.sidebar .done-dot { margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: var(--def); }
.sidebar .page-toc { margin: 2px 0 8px 34px; border-left: 1px solid var(--border); }
.sidebar .page-toc a { font-size: 13px; padding: 4px 10px; border-radius: 0 6px 6px 0; }
.sidebar .page-toc a.current { color: var(--accent); }

.content {
  flex: 1; min-width: 0; max-width: 920px; margin: 0 auto;
  padding: 32px 40px 96px;
}

/* ---------- Typography ---------- */
h1, h2, h3, h4 { line-height: 1.25; text-wrap: balance; }
h1 { font-size: 2.1rem; margin: 0 0 8px; letter-spacing: -.01em; }
h2 {
  font-size: 1.5rem; margin: 56px 0 14px; padding-top: 14px;
  border-top: 1px solid var(--border);
}
h3 { font-size: 1.17rem; margin: 32px 0 10px; }
h4 { font-size: 1rem; margin: 22px 0 6px; }
p, li { color: var(--text); }
.lead { font-size: 1.12rem; color: var(--text-2); margin: 0 0 20px; }
.muted { color: var(--text-3); }
.small { font-size: 14px; }
strong { font-weight: 650; }
code, kbd {
  font-family: var(--mono); font-size: .88em; background: var(--code-bg);
  padding: .1em .35em; border-radius: 4px;
}
hr { border: 0; border-top: 1px solid var(--border); margin: 32px 0; }
.sec-tag {
  display: inline-block; font-size: 12px; font-weight: 600; color: var(--text-3);
  border: 1px solid var(--border); border-radius: 999px; padding: 1px 9px; margin-left: 8px;
  vertical-align: middle; letter-spacing: .02em;
}

/* ---------- Chapter hero ---------- */
.hero {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 26px 28px; box-shadow: var(--shadow); margin-bottom: 28px;
}
.hero .kicker { font-size: 13px; font-weight: 650; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); }
.hero .meta { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border);
}
.done-toggle { margin-top: 16px; }
.done-toggle.is-done { background: var(--def-bg); color: var(--def); border-color: var(--def); }

/* ---------- Callouts ---------- */
.box {
  border-left: 4px solid var(--key); background: var(--key-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 14px 18px; margin: 18px 0;
}
.box > :first-child { margin-top: 0; }
.box > :last-child { margin-bottom: 0; }
.box .box-title {
  display: block; font-size: 13.5px; font-weight: 700; letter-spacing: .01em;
  margin-bottom: 6px; color: var(--key);
}
.box.exam { border-color: var(--exam); background: var(--exam-bg); }
.box.exam .box-title { color: var(--exam); }
.box.pitfall { border-color: var(--pit); background: var(--pit-bg); }
.box.pitfall .box-title { color: var(--pit); }
.box.def { border-color: var(--def); background: var(--def-bg); }
.box.def .box-title { color: var(--def); }

/* ---------- Equations ---------- */
.eq {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 18px; margin: 16px 0;
  overflow-x: auto; box-shadow: var(--shadow);
}
.eq .eq-name {
  display: flex; justify-content: space-between; gap: 12px;
  font-size: 13px; font-weight: 650; color: var(--text-3);
  margin-top: 4px;
}
.eq .eq-name .ref { font-family: var(--mono); font-weight: 500; }
.eq .katex-display { margin: .6em 0; }
.eq-note { font-size: 14.5px; color: var(--text-2); margin: -6px 0 6px; }
.katex { font-size: 1.07em; }

/* ---------- Algorithm boxes ---------- */
.algo {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin: 20px 0; box-shadow: var(--shadow); overflow: hidden;
}
.algo-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 10px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.algo-head .name { font-weight: 700; }
.algo-head .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.tag {
  font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
  white-space: nowrap;
}
.tag.on { color: var(--s1); border-color: var(--s1); }
.tag.off { color: var(--s2); border-color: var(--s2); }
.tag.model { color: var(--s7); border-color: var(--s7); }
.tag.boot { color: var(--def); border-color: var(--def); }
.algo pre {
  margin: 0; padding: 14px 18px; font: 13.5px/1.7 var(--mono); white-space: pre-wrap;
  overflow-x: auto; color: var(--text); background: transparent;
}
.algo pre .c { color: var(--text-3); font-style: italic; }
.algo pre .k { color: var(--accent); font-weight: 600; }
.algo .algo-foot { padding: 10px 16px; border-top: 1px solid var(--border); font-size: 14.5px; }
.algo .algo-foot ul { margin: 4px 0; padding-left: 20px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 16px 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
th, td { text-align: left; vertical-align: top; padding: 9px 12px; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); font-weight: 650; color: var(--text); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td .katex { font-size: 1em; }
.yes { color: var(--def); font-weight: 650; }
.no { color: var(--pit); font-weight: 650; }

/* ---------- Grids & cards ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.card h4 { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
a.card { display: block; color: inherit; }
a.card:hover { border-color: var(--accent); text-decoration: none; }

/* ---------- Self-test ---------- */
.quiz { counter-reset: q; }
.quiz details {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin: 10px 0; padding: 0;
}
.quiz summary {
  cursor: pointer; padding: 12px 16px 12px 48px; position: relative; list-style: none; font-weight: 550;
}
.quiz summary::-webkit-details-marker { display: none; }
.quiz summary::before {
  counter-increment: q; content: counter(q);
  position: absolute; left: 14px; top: 11px; width: 24px; height: 24px; border-radius: 50%;
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
  background: var(--surface-2); color: var(--text-2);
}
.quiz details[open] summary::before { background: var(--accent); color: #fff; }
.quiz .ans { padding: 0 16px 14px 48px; color: var(--text); border-top: 1px dashed var(--border); padding-top: 12px; }
.quiz .ans > :first-child { margin-top: 0; }
.quiz .ans > :last-child { margin-bottom: 0; }
.quiz-tools { display: flex; gap: 8px; margin: 8px 0 4px; }

/* ---------- Diagrams ---------- */
figure { margin: 22px 0; }
figure.diagram {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; text-align: center;
}
figcaption { font-size: 14px; color: var(--text-2); margin-top: 10px; text-align: left; }
.diagram svg text { fill: var(--text); font-family: var(--font); }
.diagram svg .muted-t { fill: var(--text-3); }
.backup-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 22px 34px; align-items: flex-start; }
.backup-row .bk { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.backup-row .bk-label { font-size: 13px; font-weight: 600; color: var(--text-2); max-width: 150px; text-align: center; line-height: 1.3; }

/* ---------- Interactive demos ---------- */
.demo {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 18px 20px; margin: 22px 0; box-shadow: var(--shadow);
}
.demo-title { font-weight: 700; display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.demo-title::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--s3); }
.controls { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: flex-end; margin: 12px 0; }
.control { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
.control input[type=range] { width: 150px; accent-color: var(--accent); }
.control select, .control input[type=number] {
  font: inherit; font-size: 14px; padding: 5px 8px; border-radius: 7px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.control .val { font-family: var(--mono); color: var(--text); }
.btn {
  font: inherit; font-size: 14px; font-weight: 600; padding: 7px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
}
.btn.ghost { background: transparent; color: var(--accent); }
.btn:disabled { opacity: .55; cursor: progress; }
.demo-out { font-size: 14px; color: var(--text-2); margin-top: 8px; }

/* charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-family: var(--font); }
.chart .axis line, .chart .axis path { stroke: var(--grid); }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .ax-title { fill: var(--text-2); font-size: 12px; }
.chart .series path { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .xhair { stroke: var(--text-3); stroke-width: 1; }
.chart .tip {
  position: absolute; pointer-events: none; z-index: 5; min-width: 140px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12); padding: 8px 10px; font-size: 12.5px; color: var(--text);
}
.chart .tip .row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.chart .tip .sw { width: 10px; height: 3px; border-radius: 2px; display: inline-block; margin-right: 4px; }
.chart .tip .v { font-family: var(--mono); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--text-2); margin: 4px 0 6px; }
.legend .li { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 16px; height: 3px; border-radius: 2px; }

/* grid-world cells */
.gw { display: inline-grid; gap: 2px; background: var(--border); border: 2px solid var(--border); border-radius: 6px; overflow: hidden; }
.gw .cell {
  width: 56px; height: 56px; display: grid; place-items: center; position: relative;
  font: 12.5px var(--mono); background: var(--surface); color: var(--text);
}
.gw .cell.term { background: var(--surface-2); color: var(--text-3); }
.gw .cell .arrow { position: absolute; bottom: 2px; font-size: 13px; color: var(--accent); font-family: var(--font); }

/* ---------- Pager ---------- */
.pager { display: flex; justify-content: space-between; gap: 16px; margin-top: 64px; }
.pager a {
  flex: 1; border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px;
  background: var(--surface); color: var(--text);
}
.pager a:hover { border-color: var(--accent); text-decoration: none; }
.pager .dir { font-size: 12px; color: var(--text-3); text-transform: uppercase; letter-spacing: .06em; }
.pager .next { text-align: right; }

/* ---------- Search ---------- */
.search-wrap { position: relative; }
.search-input {
  width: 260px; height: 36px; padding: 0 12px; border-radius: 8px; font: inherit; font-size: 14px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.search-results {
  position: absolute; right: 0; top: 42px; width: min(420px, calc(100vw - 32px)); max-height: 60vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.16); display: none; padding: 6px;
}
.search-results.open { display: block; }
.search-results a { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); font-size: 14px; }
.search-results a:hover, .search-results a.sel { background: var(--surface-2); text-decoration: none; }
.search-results .where { font-size: 12px; color: var(--text-3); }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .menu-btn { display: inline-flex; }
  .sidebar {
    position: fixed; left: 0; top: 56px; z-index: 25; height: calc(100vh - 56px);
    transform: translateX(-100%); transition: transform .2s ease;
  }
  body.nav-open .sidebar { transform: none; box-shadow: 0 0 24px rgba(0,0,0,.2); }
  .content { padding: 24px 16px 80px; }
  .search-input { width: 160px; }
}
@media (max-width: 700px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  h1 { font-size: 1.7rem; }
  .hero { padding: 20px 18px; }
  .topbar .brand span { display: none; }
  .search-input { width: 120px; }
  .gw .cell { width: 44px; height: 44px; font-size: 11px; }
  .pager { flex-direction: column; }
}
@media print {
  .topbar, .sidebar, .pager, .demo, .done-toggle, .quiz-tools { display: none !important; }
  .content { max-width: none; padding: 0; }
  .quiz details { break-inside: avoid; }
  .quiz .ans { display: block; }
}
a.card.soon { opacity: .5; pointer-events: none; }
a.card.soon::after { content: "Coming soon"; display: inline-block; margin-top: 6px; font-size: 12px; color: var(--text-3); }

/* Long inline formulas can't wrap; on phones let each one scroll on its own
   instead of widening the whole page. */
@media (max-width: 700px) {
  p .katex, li .katex, td .katex, .ans .katex, summary .katex {
    display: inline-block; max-width: 100%; overflow-x: auto; overflow-y: hidden;
    vertical-align: middle; padding: 2px 0;
  }
  .hero .meta { overflow: hidden; }
}
/* KaTeX's hidden MathML layer is absolutely positioned; anchor it to its formula
   so it can't escape scroll containers (tables) and widen the page. */
.katex { position: relative; }
.table-wrap, .eq { position: relative; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

/* =====================================================================
   Reading theme — typography, chapter accents and reading aids
   ===================================================================== */
@font-face { font-family: "Source Serif 4"; font-style: normal; font-weight: 400; font-display: swap; src: url("vendor/fonts/source-serif-4-400-normal.woff2") format("woff2"); }
@font-face { font-family: "Source Serif 4"; font-style: italic; font-weight: 400; font-display: swap; src: url("vendor/fonts/source-serif-4-400-italic.woff2") format("woff2"); }
@font-face { font-family: "Source Serif 4"; font-style: normal; font-weight: 600; font-display: swap; src: url("vendor/fonts/source-serif-4-600-normal.woff2") format("woff2"); }
@font-face { font-family: "Source Serif 4"; font-style: italic; font-weight: 600; font-display: swap; src: url("vendor/fonts/source-serif-4-600-italic.woff2") format("woff2"); }
@font-face { font-family: "Inter"; font-style: normal; font-weight: 400; font-display: swap; src: url("vendor/fonts/inter-400.woff2") format("woff2"); }
@font-face { font-family: "Inter"; font-style: normal; font-weight: 500; font-display: swap; src: url("vendor/fonts/inter-500.woff2") format("woff2"); }
@font-face { font-family: "Inter"; font-style: normal; font-weight: 600; font-display: swap; src: url("vendor/fonts/inter-600.woff2") format("woff2"); }
@font-face { font-family: "Inter"; font-style: normal; font-weight: 700; font-display: swap; src: url("vendor/fonts/inter-700.woff2") format("woff2"); }

/* One accent hue per chapter (categorical palette), used for the hero,
   section markers and the reading-progress bar. Links stay blue. */
body[data-page="ch01"] { --chap: var(--s1); }
body[data-page="ch02"] { --chap: var(--s2); }
body[data-page="ch03"] { --chap: var(--s3); }
body[data-page="ch04"] { --chap: var(--s7); }
body[data-page="ch05"] { --chap: var(--s5); }
body[data-page="ch06"] { --chap: #12864f; }
:root[data-theme="dark"] body[data-page="ch06"] { --chap: #3fbf74; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) body[data-page="ch06"] { --chap: #3fbf74; } }
body[data-page="ch07"] { --chap: var(--s4); }
body[data-page="ch09"] { --chap: var(--s8); }

body { text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; }
::selection { background: color-mix(in srgb, var(--chap) 28%, transparent); }

/* Reading text in the serif; UI chrome, tables, code and controls stay in Inter. */
.content { max-width: 880px; }
.content > p, .content > ul > li, .content > ol > li,
.box p, .box li, .card p, .ans p, .ans li, .algo-foot, .lead, figcaption {
  font-family: var(--serif);
}
.content > p, .content > ul > li, .content > ol > li, .box p, .box li, .ans p, .ans li {
  font-size: var(--read-size); line-height: 1.72;
}
.content > p { margin: 0 0 1.05em; }
.content > ul, .content > ol { padding-left: 1.4em; margin: 0 0 1.1em; }
.content > ul > li, .content > ol > li { margin: .32em 0; }
.content > ul > li::marker { color: var(--chap); }
.content > ol > li::marker { color: var(--chap); font-family: var(--font); font-weight: 600; font-size: .85em; }
.lead { font-size: calc(var(--read-size) + 2px); line-height: 1.6; color: var(--text-2); }
.content > p.small, .card p.small, p.small { font-size: calc(var(--read-size) - 3px); }
a { text-underline-offset: 3px; }
.content > p a, .box a, .ans a { text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.content > p a:hover, .box a:hover { text-decoration-color: var(--accent); }

/* Headings */
h1, h2, h3, h4 { font-family: var(--font); color: var(--text); }
h1 { font-size: 2.35rem; font-weight: 700; letter-spacing: -.022em; }
h2 {
  font-size: 1.55rem; font-weight: 700; letter-spacing: -.015em;
  border-top: 0; padding-top: 0; margin-top: 64px;
}
h2::before {
  content: ""; display: block; width: 44px; height: 4px; border-radius: 2px;
  background: var(--chap); margin-bottom: 14px;
}
h3 { font-size: 1.2rem; font-weight: 650; letter-spacing: -.01em; margin-top: 36px; }
h4 { font-size: 1.02rem; font-weight: 650; }

/* Chapter hero */
.hero {
  position: relative; overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--chap) 22%, var(--border));
  background:
    radial-gradient(120% 140% at 100% 0%, color-mix(in srgb, var(--chap) 13%, transparent) 0%, transparent 55%),
    var(--surface);
  padding: 34px 36px 30px; border-radius: 18px;
}
.hero .kicker { color: var(--chap); font-family: var(--font); }
.hero::after {
  content: attr(data-num); position: absolute; right: 26px; top: -18px;
  font: 700 150px/1 var(--font); letter-spacing: -.06em;
  color: color-mix(in srgb, var(--chap) 12%, transparent); pointer-events: none;
}
.hero > * { position: relative; z-index: 1; }
.hero .chip { background: color-mix(in srgb, var(--chap) 7%, var(--surface)); border-color: color-mix(in srgb, var(--chap) 20%, var(--border)); }
.hero .chip.read { background: transparent; color: var(--text-3); }

/* Callouts: softer, with a small icon */
.box { border-left-width: 3px; border-radius: 0 14px 14px 0; padding: 16px 20px; }
.box .box-title { font-family: var(--font); font-size: 14px; display: flex; align-items: center; gap: 8px; }
.box .box-title::before {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  font-size: 12px; line-height: 1; color: #fff; background: var(--key); content: "✦"; flex: 0 0 22px;
}
.box.exam .box-title::before { background: var(--exam); content: "★"; }
.box.pitfall .box-title::before { background: var(--pit); content: "!"; font-weight: 700; }
.box.def .box-title::before { background: var(--def); content: "§"; }

/* Equation cards */
.eq { border-radius: 14px; padding: 12px 20px 10px; }
.eq .eq-name { font-family: var(--font); }

/* Tables: gentle zebra rows, rounded frame */
.table-wrap { border-radius: 14px; }
th { font-family: var(--font); font-size: 13.5px; }
td { font-family: var(--font); }
tr:nth-child(even) td { background: color-mix(in srgb, var(--surface-2) 45%, transparent); }

/* Cards, quiz, demos */
.card { border-radius: 14px; }
a.card { transition: border-color .15s, transform .15s, box-shadow .15s; }
a.card:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,.06); }
.quiz details { border-radius: 12px; transition: border-color .15s; }
.quiz details[open] { border-color: color-mix(in srgb, var(--chap) 35%, var(--border)); }
.quiz summary { font-family: var(--font); }
.quiz details[open] summary::before { background: var(--chap); }
.demo { border-radius: 18px; }
.demo-title::before { background: var(--chap); }

/* Sidebar active item follows the chapter accent */
.sidebar a.active { background: color-mix(in srgb, var(--chap) 12%, var(--surface)); color: var(--text); }
.sidebar a.active .num { background: var(--chap); }
.sidebar .page-toc { border-left-color: color-mix(in srgb, var(--chap) 30%, var(--border)); }
.sidebar .page-toc a.current { color: var(--text); font-weight: 600; box-shadow: inset 2px 0 0 var(--chap); }

/* Reading progress bar */
.read-progress { position: fixed; left: 0; top: 0; height: 3px; width: 0; z-index: 40; background: var(--chap); }

/* Back to top */
.to-top {
  position: fixed; right: 22px; bottom: 22px; z-index: 35; width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2); font-size: 18px; cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,.10); opacity: 0; transform: translateY(10px); pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.to-top.show { opacity: 1; transform: none; pointer-events: auto; }
.to-top:hover { color: var(--text); border-color: var(--text-3); }

/* Text-size and focus controls in the top bar */
.reader-tools { display: inline-flex; gap: 4px; }
.reader-tools .icon-btn { min-width: 34px; padding: 0 8px; font-weight: 600; }
.reader-tools .icon-btn.on { background: var(--surface-2); color: var(--text); }
body.focus .sidebar { display: none; }
body.focus .content { max-width: 780px; }

@media (prefers-reduced-motion: no-preference) {
  .hero { animation: rise .45s ease-out both; }
  @keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
}

@media (max-width: 980px) { .reader-tools .focus-btn { display: none; } }
@media (max-width: 700px) {
  :root { --read-size: 17px; }
  .hero { padding: 24px 20px 22px; border-radius: 14px; }
  .hero::after { font-size: 96px; right: 12px; top: -8px; }
  h1 { font-size: 1.8rem; }
  .reader-tools .size-btn { display: none; }
  .to-top { right: 14px; bottom: 14px; }
}
@media print {
  .read-progress, .to-top, .reader-tools { display: none !important; }
  .hero::after { display: none; }
}

/* ---------- Scored chapter quiz ---------- */
.cq { margin: 18px 0; }
.cq-head {
  position: sticky; top: 64px; z-index: 5; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(6px);
  border: 1px solid var(--border); border-radius: 14px; padding: 12px 16px; margin-bottom: 14px;
}
.cq-score { font-family: var(--font); font-size: 15px; }
.cq-q {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px 18px; margin: 12px 0; transition: border-color .2s;
}
.cq-q.ok { border-color: color-mix(in srgb, var(--def) 55%, var(--border)); }
.cq-q.bad { border-color: color-mix(in srgb, var(--pit) 55%, var(--border)); }
.cq-text { display: flex; gap: 12px; font-family: var(--serif); font-size: var(--read-size); line-height: 1.6; }
.cq-n {
  flex: 0 0 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; margin-top: 2px;
  font: 700 12px var(--font); background: color-mix(in srgb, var(--chap) 14%, var(--surface)); color: var(--text);
}
.cq-opts { display: grid; gap: 8px; margin: 12px 0 0 38px; }
.cq-opt {
  text-align: left; font: inherit; font-family: var(--font); font-size: 15px; line-height: 1.45;
  padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; transition: border-color .15s;
}
.cq-opt:hover:not(:disabled) { border-color: var(--chap); background: color-mix(in srgb, var(--chap) 6%, var(--surface)); }
.cq-opt:disabled { cursor: default; }
.cq-opt.right { border-color: var(--def); background: var(--def-bg); }
.cq-opt.wrong { border-color: var(--pit); background: var(--pit-bg); }
.cq-num { display: flex; gap: 8px; margin: 12px 0 0 38px; }
.cq-num input {
  width: 160px; font: inherit; font-family: var(--mono); font-size: 15px; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.cq-fb { margin: 12px 0 0 38px; font-family: var(--serif); font-size: calc(var(--read-size) - 1px); line-height: 1.6; color: var(--text-2); }
.cq-final {
  margin-top: 16px; padding: 16px 18px; border-radius: 14px; font-family: var(--serif); font-size: var(--read-size);
  background: color-mix(in srgb, var(--chap) 9%, var(--surface)); border: 1px solid color-mix(in srgb, var(--chap) 30%, var(--border));
}
.quiz-best-chip { font-variant-numeric: tabular-nums; }
@media (max-width: 700px) {
  .cq-opts, .cq-num, .cq-fb { margin-left: 0; }
  .cq-head { top: 60px; flex-direction: column; align-items: flex-start; }
}
@media print { .cq { display: none; } }

/* Exam-style quiz questions: badge and small data tables inside the question */
.cq-tag {
  display: inline-block; margin: 0 8px 4px 0; padding: 1px 8px; border-radius: 999px; vertical-align: 2px;
  font: 700 11px/1.6 var(--font); letter-spacing: .03em; color: var(--exam); background: var(--exam-bg);
  border: 1px solid color-mix(in srgb, var(--exam) 35%, transparent);
}
.cq-q.cq-exam { border-left: 3px solid var(--exam); }
.cq-text table { width: auto; margin: 10px 0 4px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; border-collapse: separate; border-spacing: 0; }
.cq-text th, .cq-text td { padding: 5px 12px; font-size: 14px; text-align: center; font-variant-numeric: tabular-nums; }
.cq-text .tw { overflow-x: auto; max-width: 100%; }
