
  :root {
    --bg: #faf9f7;
    --surface: #ffffff;
    --ink: #1a1c1e;
    --ink-soft: #4a4e54;
    --ink-faint: #8a8f96;
    --accent: #1a56db;
    --accent-soft: #e8effc;
    --rule: #e3e1dc;
    --code-bg: #14161a;
    --code-ink: #e6e9ef;
    --code-label: #7d8590;
    --key-bg: #eef4ff;
    --key-edge: #3b6fe0;
    --warn-bg: #fdf1ef;
    --warn-edge: #d9534a;
    --tip-bg: #eff8f1;
    --tip-edge: #3d9a5f;
    --ex-bg: #f7f3ea;
    --ex-edge: #b98a2e;
    --table-head: #f1efea;
  }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #101214;
      --surface: #16191c;
      --ink: #e8eaed;
      --ink-soft: #b6bac0;
      --ink-faint: #7d838b;
      --accent: #7aa2f7;
      --accent-soft: #1b2537;
      --rule: #2a2e33;
      --code-bg: #0b0d10;
      --code-ink: #dde1e8;
      --code-label: #6e757e;
      --key-bg: #16233a;
      --key-edge: #5b86e5;
      --warn-bg: #331d1b;
      --warn-edge: #e06c62;
      --tip-bg: #16291c;
      --tip-edge: #4fae72;
      --ex-bg: #2b2417;
      --ex-edge: #cda04a;
      --table-head: #1d2125;
    }
  }
  * { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: ui-serif, "New York", Georgia, "Times New Roman", serif;
    font-size: 17px;
    line-height: 1.65;
  }
  .page {
    max-width: 46rem;
    margin: 0 auto;
    padding: 0 1.25rem 6rem;
  }
  /* ---------- cover ---------- */
  .cover {
    padding: 5rem 0 3rem;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 2.5rem;
  }
  .cover .kicker {
    font-family: ui-sans-serif, -apple-system, "Helvetica Neue", sans-serif;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--accent);
    margin: 0 0 1rem;
  }
  .cover h1 {
    font-size: 2.6rem;
    line-height: 1.12;
    margin: 0 0 1rem;
    letter-spacing: -0.015em;
  }
  .cover .subtitle {
    font-size: 1.15rem;
    color: var(--ink-soft);
    margin: 0 0 1.5rem;
    max-width: 36rem;
  }
  .cover .meta {
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.82rem;
    color: var(--ink-faint);
  }
  /* ---------- headings ---------- */
  h2 {
    font-size: 1.9rem;
    line-height: 1.2;
    margin: 0 0 0.75rem;
    letter-spacing: -0.01em;
  }
  h3 {
    font-size: 1.3rem;
    line-height: 1.3;
    margin: 2.2rem 0 0.6rem;
  }
  h4 {
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.95rem;
    font-weight: 700;
    margin: 1.6rem 0 0.4rem;
  }
  .chnum {
    display: inline-block;
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 1rem;
    font-weight: 700;
    color: var(--accent);
    background: var(--accent-soft);
    border-radius: 8px;
    padding: 0.15rem 0.6rem;
    margin-right: 0.6rem;
    vertical-align: 0.35rem;
  }
  .chapter {
    padding-top: 3.5rem;
    margin-top: 3.5rem;
    border-top: 1px solid var(--rule);
  }
  .lead {
    font-size: 1.12rem;
    color: var(--ink-soft);
    font-style: italic;
  }
  p { margin: 0.9rem 0; }
  a { color: var(--accent); text-underline-offset: 2px; }
  ul, ol { padding-left: 1.4rem; }
  li { margin: 0.35rem 0; }
  strong { font-weight: 700; }
  /* ---------- code ---------- */
  pre {
    background: var(--code-bg);
    color: var(--code-ink);
    border-radius: 10px;
    padding: 1rem 1.1rem;
    overflow-x: auto;
    font-size: 0.8rem;
    line-height: 1.55;
    margin: 1.1rem 0;
    position: relative;
    -webkit-overflow-scrolling: touch;
  }
  pre[data-lang]::before {
    content: attr(data-lang);
    display: block;
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--code-label);
    margin-bottom: 0.6rem;
  }
  pre code {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    background: none;
    padding: 0;
    color: inherit;
    font-size: inherit;
  }
  code {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.84em;
    background: var(--accent-soft);
    color: var(--ink);
    border-radius: 4px;
    padding: 0.1em 0.35em;
  }
  /* ---------- callouts ---------- */
  .callout, .exercise {
    border-radius: 10px;
    padding: 0.9rem 1.1rem;
    margin: 1.3rem 0;
    font-size: 0.95rem;
  }
  .callout p, .exercise p { margin: 0.5rem 0 0; }
  .callout-title {
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
  }
  .callout.key  { background: var(--key-bg);  border-left: 4px solid var(--key-edge); }
  .callout.key .callout-title { color: var(--key-edge); }
  .callout.warning { background: var(--warn-bg); border-left: 4px solid var(--warn-edge); }
  .callout.warning .callout-title { color: var(--warn-edge); }
  .callout.tip  { background: var(--tip-bg);  border-left: 4px solid var(--tip-edge); }
  .callout.tip .callout-title { color: var(--tip-edge); }
  .exercise { background: var(--ex-bg); border-left: 4px solid var(--ex-edge); }
  .exercise .callout-title { color: var(--ex-edge); }
  /* ---------- tables ---------- */
  .tablewrap { overflow-x: auto; margin: 1.2rem 0; -webkit-overflow-scrolling: touch; }
  table {
    border-collapse: collapse;
    width: 100%;
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.85rem;
    margin: 1.2rem 0;
  }
  .tablewrap table { margin: 0; }
  th, td {
    text-align: left;
    padding: 0.55rem 0.7rem;
    border-bottom: 1px solid var(--rule);
    vertical-align: top;
  }
  thead th {
    background: var(--table-head);
    font-weight: 700;
    font-size: 0.78rem;
    letter-spacing: 0.03em;
    text-transform: uppercase;
  }
  /* ---------- TOC ---------- */
  .toc {
    background: var(--surface);
    border: 1px solid var(--rule);
    border-radius: 12px;
    padding: 1.4rem 1.6rem;
    margin: 2rem 0;
  }
  .toc h2 {
    font-size: 1.1rem;
    margin: 0 0 0.8rem;
    font-family: ui-sans-serif, -apple-system, sans-serif;
  }
  .toc ol {
    margin: 0;
    padding-left: 1.5rem;
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.95rem;
  }
  .toc li { margin: 0.45rem 0; }
  .toc a { text-decoration: none; }
  .toc a:active { text-decoration: underline; }
  .toc .toc-note {
    font-size: 0.82rem;
    color: var(--ink-faint);
    margin: 0.8rem 0 0;
  }
  /* ---------- misc ---------- */
  .intro h2 { font-size: 1.5rem; margin-top: 2rem; }
  .backtotop {
    display: block;
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.8rem;
    margin-top: 2rem;
    text-decoration: none;
  }
  footer {
    margin-top: 4rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--rule);
    font-family: ui-sans-serif, -apple-system, sans-serif;
    font-size: 0.8rem;
    color: var(--ink-faint);
  }
  @media (max-width: 480px) {
    body { font-size: 16px; }
    .cover { padding-top: 3rem; }
    .cover h1 { font-size: 2rem; }
    h2 { font-size: 1.55rem; }
    h3 { font-size: 1.2rem; }
    .page { padding-left: 1rem; padding-right: 1rem; }
  }

/* ============================================================
   Interactive layer — quizzes, playgrounds, diagrams, chrome
   ============================================================ */

/* ---------- accessibility + viewport corrections ----------
   Values below were derived from the automated audit: contrast
   measured against the real rendered background, tap targets
   against a 375px phone. Do not lighten --ink-faint again. */
:root {
  --ink-faint: #6a6f77;   /* 4.7:1 on --bg */
  --good-text: #2f7d4a;   /* 4.7:1 on --tip-bg  */
  --bad-text:  #bd3a30;   /* 5.0:1 on --warn-bg */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink-faint: #969ca4;
    --good-text: #7fd39b;
    --bad-text:  #ff9a90;
  }
}
:root[data-theme="dark"] {
  --ink-faint: #969ca4;
  --good-text: #7fd39b;
  --bad-text:  #ff9a90;
}

/* long identifiers in inline code must not push the page sideways */
code {
  overflow-wrap: anywhere;
  word-break: break-word;
}
/* nothing may exceed the viewport */
html, body { max-width: 100%; overflow-x: clip; }
img, svg, video, iframe { max-width: 100%; }

/* ---------- top chrome ---------- */
.topbar-inner {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  max-width: 46rem;      /* line the brand up with the prose column */
  margin: 0 auto;
  width: 100%;
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  padding: 0.5rem 1.25rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--rule);
  font-family: ui-sans-serif, -apple-system, sans-serif;
}
.topbar .brand {
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.topbar .brand a {
  color: var(--ink);
  text-decoration: none;
  display: inline-block;
  padding: 0.5rem 0;
  min-height: 34px;
}
.iconbtn {
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-soft);
  border-radius: 8px;
  padding: 0.45rem 0.7rem;
  min-height: 34px;
  min-width: 34px;
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
}
.iconbtn:hover { color: var(--ink); border-color: var(--accent); }
.iconbtn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* reading progress bar */
#readbar {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 0%;
  background: var(--accent);
  z-index: 60;
  transition: width 0.1s linear;
}

/* ---------- search ---------- */
.searchwrap { position: relative; flex: 0 0 auto; }
#searchbox {
  width: 8.5rem;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  min-height: 34px;
  font: inherit;
  font-size: 0.85rem;
}
#searchbox:focus { outline: 2px solid var(--accent); outline-offset: 1px; width: 12rem; }
#searchresults {
  position: absolute;
  top: 2.4rem; right: 0;
  width: min(85vw, 26rem);
  max-height: 60vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: 0 12px 34px rgba(0,0,0,0.18);
  padding: 0.4rem;
  display: none;
}
#searchresults.open { display: block; }
#searchresults a {
  display: block;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  text-decoration: none;
  color: var(--ink);
  font-size: 0.85rem;
}
#searchresults a:hover { background: var(--accent-soft); }
#searchresults .sr-ch { display: block; font-size: 0.7rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: 0.06em; }
#searchresults .sr-none { padding: 0.6rem; color: var(--ink-faint); font-size: 0.85rem; }

/* ---------- chapter progress ---------- */
.chapter-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}
.donebtn {
  flex: 0 0 auto;
  font-family: ui-sans-serif, -apple-system, sans-serif;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-faint);
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  min-height: 34px;
  cursor: pointer;
  margin-top: 0.6rem;
  white-space: nowrap;
}
.donebtn[aria-pressed="true"] {
  background: var(--tip-bg);
  border-color: var(--tip-edge);
  color: var(--tip-edge);
}
.donebtn[aria-pressed="true"]::before { content: "✓ "; }

.toc li.done > a::after {
  content: " ✓";
  color: var(--tip-edge);
  font-weight: 700;
}
.toc-progress {
  height: 6px;
  background: var(--rule);
  border-radius: 999px;
  overflow: hidden;
  margin: 0.9rem 0 0.3rem;
}
.toc-progress > i {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--tip-edge);
  transition: width 0.3s ease;
}

/* ---------- code copy ---------- */
pre { position: relative; }
.copybtn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  border: 1px solid rgba(255,255,255,0.18);
  background: rgba(255,255,255,0.07);
  color: #c9d1d9;
  border-radius: 6px;
  font-family: ui-sans-serif, -apple-system, sans-serif;
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 0.42rem 0.6rem;
  min-height: 30px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
pre:hover .copybtn, pre:focus-within .copybtn { opacity: 1; }
.copybtn.copied { background: #2ea043; border-color: #2ea043; color: #fff; opacity: 1; }
@media (hover: none) { .copybtn { opacity: 0.65; } }

/* ============================================================
   Interactive widgets
   ============================================================ */
.widget {
  border: 1px solid var(--rule);
  background: var(--surface);
  border-radius: 12px;
  padding: 1.1rem 1.2rem;
  margin: 1.6rem 0;
  font-family: ui-sans-serif, -apple-system, sans-serif;
  font-size: 0.9rem;
}
.widget-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.15rem;
}
.widget-kind {
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 5px;
  padding: 0.15rem 0.45rem;
}
.widget-title { font-weight: 700; font-size: 0.98rem; }
.widget-sub { color: var(--ink-soft); font-size: 0.85rem; margin: 0.35rem 0 0.9rem; }
.widget button { font-family: inherit; }

/* ---------- quiz ---------- */
.quiz-q { font-weight: 600; margin: 1.1rem 0 0.6rem; line-height: 1.45; }
.quiz-q .qn {
  display: inline-block;
  min-width: 1.5rem;
  color: var(--ink-faint);
  font-weight: 700;
}
.quiz-opts { display: grid; gap: 0.4rem; margin-bottom: 0.5rem; }
.quiz-opt {
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  text-align: left;
  width: 100%;
  border: 1px solid var(--rule);
  background: var(--bg);
  color: var(--ink);
  border-radius: 9px;
  padding: 0.55rem 0.7rem;
  cursor: pointer;
  font-size: 0.88rem;
  line-height: 1.45;
  transition: border-color 0.12s, background 0.12s;
}
.quiz-opt:hover:not(:disabled) { border-color: var(--accent); }
.quiz-opt .k {
  flex: 0 0 auto;
  font-weight: 700;
  color: var(--ink-faint);
  font-size: 0.78rem;
  border: 1px solid var(--rule);
  border-radius: 5px;
  width: 1.4rem;
  height: 1.4rem;
  display: grid;
  place-items: center;
}
.quiz-opt.correct { background: var(--tip-bg); border-color: var(--tip-edge); }
.quiz-opt.correct .k { background: var(--tip-edge); border-color: var(--tip-edge); color: #fff; }
.quiz-opt.wrong { background: var(--warn-bg); border-color: var(--warn-edge); }
.quiz-opt.wrong .k { background: var(--warn-edge); border-color: var(--warn-edge); color: #fff; }
.quiz-opt:disabled { cursor: default; }
.quiz-explain {
  display: none;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
  padding: 0.6rem 0.8rem;
  margin-top: 0.5rem;
  font-size: 0.86rem;
  line-height: 1.55;
}
.quiz-explain.show { display: block; }
.quiz-explain b { display: block; margin-bottom: 0.2rem; font-size: 0.7rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); }
.quiz-score {
  margin-top: 1rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--rule);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  font-size: 0.85rem;
}
.quiz-score .pct { font-weight: 700; font-size: 1.1rem; }
.quiz-reset {
  border: 1px solid var(--rule);
  background: var(--bg);
  color: var(--ink-soft);
  border-radius: 7px;
  padding: 0.45rem 0.8rem;
  min-height: 32px;
  font-size: 0.8rem;
  cursor: pointer;
}

/* ---------- generic controls ---------- */
.ctl-row {
  display: grid;
  grid-template-columns: 8.5rem 1fr 3.5rem;
  align-items: center;
  gap: 0.7rem;
  margin: 0.5rem 0;
  font-size: 0.85rem;
}
.ctl-row label { color: var(--ink-soft); }
.ctl-row input[type=range] {
  width: 100%;
  height: 32px;
  accent-color: var(--accent);
  cursor: pointer;
}
.ctl-row output {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-weight: 700;
  text-align: right;
  font-size: 0.9rem;
}
.btnrow { display: flex; flex-wrap: wrap; gap: 0.45rem; margin: 0.9rem 0 0.4rem; }
.btn {
  border: 1px solid var(--rule);
  background: var(--bg);
  color: var(--ink);
  border-radius: 8px;
  padding: 0.42rem 0.7rem;
  font-size: 0.82rem;
  cursor: pointer;
  font-family: inherit;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
.btn.danger { border-color: var(--warn-edge); color: var(--warn-edge); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- readouts ---------- */
.readout {
  background: var(--code-bg);
  color: var(--code-ink);
  border-radius: 9px;
  padding: 0.8rem 0.9rem;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.76rem;
  line-height: 1.6;
  margin-top: 0.9rem;
  overflow-x: auto;
  white-space: pre;
  min-height: 3rem;
}
.readout .ok { color: #6fd48b; }
.readout .bad { color: #ff8a80; }
.readout .dim { color: #6e757e; }
.readout .hl { color: #ffd479; }

.bignum {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0.9rem 0 0.2rem;
}
.bignum .v {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 2.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
}
.bignum .v.good { color: var(--tip-edge); }
.bignum .v.bad { color: var(--warn-edge); }
.bignum .lbl { font-size: 0.78rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: 0.08em; }

/* mini table inside widgets */
.wtablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 0.7rem; }
.wtable { width: 100%; border-collapse: collapse; font-size: 0.78rem; }
.wtable th, .wtable td { padding: 0.32rem 0.45rem; border-bottom: 1px solid var(--rule); text-align: left; }
.wtable th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint); }
.wtable td.num { text-align: right; font-family: ui-monospace, Menlo, monospace; }
.wtable tr.neg td.num { color: var(--warn-edge); }
.wtable tr.pos td.num { color: var(--tip-edge); }
.wtable .empty { color: var(--ink-faint); font-style: italic; }

/* ---------- two-session race sim ---------- */
.sessions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; margin-top: 0.8rem; }
@media (max-width: 520px) { .sessions { grid-template-columns: 1fr; } }
.session {
  border: 1px solid var(--rule);
  border-radius: 9px;
  padding: 0.6rem 0.7rem;
  background: var(--bg);
}
.session h5 {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
}
.session ol { margin: 0; padding-left: 1.1rem; font-size: 0.78rem; line-height: 1.6; }
.session li { color: var(--ink-faint); transition: color 0.2s; }
.session li.active { color: var(--accent); font-weight: 700; }
.session li.done { color: var(--ink); }
.session li.blocked { color: var(--warn-edge); font-style: italic; }

/* ---------- isolation matrix ---------- */
.matrixwrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 0.8rem; }
.matrix { width: 100%; min-width: 30rem; border-collapse: collapse; font-size: 0.78rem; }
.matrix th, .matrix td { padding: 0.4rem 0.35rem; border: 1px solid var(--rule); text-align: center; }
.matrix th:first-child, .matrix td:first-child { text-align: left; }
.matrix td.no { background: var(--tip-bg); color: var(--good-text); font-weight: 700; }
.matrix td.yes { background: var(--warn-bg); color: var(--bad-text); font-weight: 700; }
.matrix tr.sel td { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ============================================================
   Diagrams
   ============================================================ */
figure.diagram {
  margin: 1.8rem 0;
  padding: 0;
}
figure.diagram svg {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid var(--rule);
  border-radius: 12px;
  background: var(--surface);
}
figure.diagram figcaption {
  font-family: ui-sans-serif, -apple-system, sans-serif;
  font-size: 0.78rem;
  color: var(--ink-faint);
  margin-top: 0.55rem;
  line-height: 1.5;
}
figure.diagram figcaption b { color: var(--ink-soft); }

/* SVG theming hooks */
.dg-box { fill: var(--surface); stroke: var(--rule); stroke-width: 1.5; }
.dg-box-accent { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.5; }
.dg-box-warn { fill: var(--warn-bg); stroke: var(--warn-edge); stroke-width: 1.5; }
.dg-box-good { fill: var(--tip-bg); stroke: var(--tip-edge); stroke-width: 1.5; }
.dg-t { fill: var(--ink); font-family: ui-sans-serif, -apple-system, sans-serif; font-size: 11px; }
.dg-t.b { font-weight: 700; }
.dg-t.s { font-size: 9.5px; fill: var(--ink-faint); }
.dg-t.c { text-anchor: middle; }
.dg-line { stroke: var(--ink-faint); stroke-width: 1.5; fill: none; }
.dg-line.accent { stroke: var(--accent); stroke-width: 2; }
.dg-line.dash { stroke-dasharray: 4 3; }
.dg-arrowhead { fill: var(--ink-faint); }
.dg-arrowhead.accent { fill: var(--accent); }

/* animated flow along a path */
@keyframes dg-dash { to { stroke-dashoffset: -60; } }
.dg-flow {
  stroke-dasharray: 6 6;
  animation: dg-dash 1.6s linear infinite;
}
@keyframes dg-pulse {
  0%, 100% { opacity: 0.25; r: 4; }
  50%      { opacity: 1;    r: 6; }
}
.dg-pulse { animation: dg-pulse 1.8s ease-in-out infinite; }
@keyframes dg-fade-cycle {
  0%, 18%  { opacity: 0; }
  25%, 92% { opacity: 1; }
  100%     { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .dg-flow, .dg-pulse { animation: none; }
  .dg-flow { stroke-dasharray: none; }
}

/* ---------- print ---------- */
@media print {
  .topbar, #readbar, .copybtn, .donebtn, .quiz-reset, .btnrow { display: none !important; }
  .widget, figure.diagram svg { break-inside: avoid; }
  .chapter { break-before: page; }
  body { font-size: 11pt; }
}

/* On a standalone chapter page the chapter rule and its 3.5rem of air are an
   orphan divider — that separator only earns its place in the single-file
   edition, where chapters run together. */
body.page-chapter .chapter {
  border-top: none;
  margin-top: 0.5rem;
  padding-top: 0;
}
body.page-chapter .partlabel { margin-bottom: 1.5rem; }

/* stat pairs: stop the caption wrapping into a ragged column on a phone */
.bignum { flex-wrap: wrap; gap: 0.4rem 0.5rem; }
.bignum .lbl { letter-spacing: 0.04em; text-transform: none; font-size: 0.74rem; line-height: 1.3; }
@media (max-width: 430px) {
  .bignum { flex-direction: column; align-items: flex-start; gap: 0.15rem; }
  .bignum .v { font-size: 1.8rem; }
}

/* long chapter titles should not be crushed by the completion button */
@media (max-width: 560px) {
  .chapter-head { flex-direction: column; align-items: flex-start; gap: 0.2rem; }
  .chapter-head .donebtn { margin-top: 0.1rem; }
}


/* ============================================================
   Reading rhythm
   Measured problem: the prose column ran ~86 characters, the
   median paragraph was 73 words, and some sections went 1,700
   words without a subheading. These rules shorten the line,
   open up the space between paragraphs, and make headings read
   as real stops rather than slightly bigger text.
   ============================================================ */
.page { max-width: 40rem; }          /* ~72 characters at 17px */
.topbar-inner { max-width: 40rem; }

body { line-height: 1.72; }
p { margin: 1.15rem 0; }
li { margin: 0.5rem 0; line-height: 1.65; }
ul, ol { margin: 1.15rem 0; }

/* headings become visible resting points */
h3 {
  margin: 3rem 0 0.9rem;
  padding-top: 1.4rem;
  border-top: 1px solid var(--rule);
  line-height: 1.25;
}
.chapter > h3:first-of-type { border-top: none; padding-top: 0; margin-top: 2.2rem; }
h4 { margin: 2.1rem 0 0.5rem; font-size: 1rem; }

/* the opening line of a chapter gets room to breathe */
.lead { margin: 1.2rem 0 2rem; line-height: 1.6; }

/* callouts, figures and widgets are the other rest stops — give
   them air so they visually punctuate the column */
.callout, .exercise { margin: 1.8rem 0; }
figure.diagram { margin: 2.2rem 0; }
.widget { margin: 2.2rem 0; }
pre { margin: 1.4rem 0; }
.tablewrap { margin: 1.6rem 0; }

/* a paragraph immediately after a code block is a walkthrough —
   tie it visually to the block it explains */
pre + p { margin-top: 0.9rem; }

@media (max-width: 480px) {
  body { font-size: 16.5px; line-height: 1.7; }
  h3 { margin-top: 2.4rem; padding-top: 1.1rem; }
  p { margin: 1.05rem 0; }
}

/* ============================================================
   In-chapter navigation
   The median chapter runs past 16,000 words. Without a way to
   see its shape, the reader is scrolling blind.
   ============================================================ */
.chaptoc {
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: var(--surface);
  margin: 1.6rem 0 2rem;
  font-family: ui-sans-serif, -apple-system, sans-serif;
}
.chaptoc > summary {
  cursor: pointer;
  padding: 0.75rem 1rem;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  list-style: none;
  min-height: 44px;
  box-sizing: border-box;
}
.chaptoc > summary::-webkit-details-marker { display: none; }
.chaptoc > summary::after {
  content: "▾";
  color: var(--ink-faint);
  transition: transform 0.15s;
  margin-left: auto;
}
.chaptoc[open] > summary::after { transform: rotate(180deg); }
.ct-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
}
.ct-meta { font-size: 0.78rem; color: var(--ink-faint); }
.chaptoc ol {
  margin: 0;
  padding: 0 1rem 0.9rem 2.2rem;
  font-size: 0.88rem;
  line-height: 1.5;
}
.chaptoc li { margin: 0.4rem 0; }
.chaptoc a { text-decoration: none; }
.chaptoc a:hover { text-decoration: underline; }

/* cross-chapter references in prose */
a.xref {
  text-decoration: none;
  border-bottom: 1px dotted var(--accent);
  white-space: nowrap;
}
a.xref:hover { border-bottom-style: solid; }

/* click-to-copy anchor beside each heading */
.anchorlink {
  opacity: 0;
  margin-left: 0.2rem;
  font-size: 0.8em;
  text-decoration: none;
  color: var(--ink-faint);
  font-family: ui-sans-serif, -apple-system, sans-serif;
  display: inline-block;
  padding: 0.35rem 0.5rem;
  min-width: 32px;
  min-height: 32px;
  line-height: 1.4;
  vertical-align: middle;
}
h3:hover .anchorlink, h4:hover .anchorlink,
h3:focus-within .anchorlink, h4:focus-within .anchorlink { opacity: 1; }
.anchorlink:hover { color: var(--accent); }
.anchorlink.copied { opacity: 1; color: var(--tip-edge); }
@media (hover: none) { .anchorlink { opacity: 0.4; } }

/* deep links must not land under the sticky bar */
:target { scroll-margin-top: 4.5rem; }
h2, h3, h4 { scroll-margin-top: 4.5rem; }

/* keyboard selection in search results */
#searchresults a.sel {
  background: var(--accent-soft);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* visible focus everywhere — keyboard users need to see where they are */
a:focus-visible, button:focus-visible, input:focus-visible,
summary:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* skip link for screen-reader and keyboard users */
.skiplink {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: #fff;
  padding: 0.6rem 1rem;
  z-index: 100;
  border-radius: 0 0 8px 0;
  font-family: ui-sans-serif, -apple-system, sans-serif;
  font-size: 0.9rem;
}
.skiplink:focus { left: 0; }

@media print {
  .chaptoc, .anchorlink { display: none !important; }
  a.xref { border-bottom: none; }
}

/* ---------- syntax highlighting ---------- */
.tk-k { color: #79b8ff; }                 /* keywords   */
.tk-s { color: #9ecbff; }                 /* strings    */
.tk-c { color: #8b949e; font-style: italic; }  /* comments */
.tk-n { color: #f8c555; }                 /* numbers    */
.tk-v { color: #ffab70; }                 /* variables  */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .tk-k { color: #7fb3ff; }
  :root:not([data-theme="dark"]) .tk-s { color: #a5d6ff; }
  :root:not([data-theme="dark"]) .tk-c { color: #9aa4af; }
}

/* ============================================================
   Navigation: continue-reading, breadcrumb, per-chapter progress
   ============================================================ */
.resume-card {
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 12px;
  padding: 1rem 1.2rem;
  margin: 2rem 0;
  font-family: ui-sans-serif, -apple-system, sans-serif;
}
.rc-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.35rem;
}
.rc-link {
  display: inline-block;
  font-size: 1.05rem;
  font-weight: 700;
  text-decoration: none;
  color: var(--ink);
  line-height: 1.3;
  min-height: 32px;
}
.rc-link:hover { color: var(--accent); }
.rc-sec { font-size: 0.85rem; color: var(--ink-soft); margin-top: 0.2rem; }
.rc-bar {
  height: 6px;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  border-radius: 999px;
  overflow: hidden;
  margin: 0.7rem 0 0.3rem;
}
.rc-bar > i { display: block; height: 100%; background: var(--accent); }
.rc-pct { font-size: 0.75rem; color: var(--ink-faint); }

/* breadcrumb showing where you are while scrolling */
.crumb {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.75rem;
  color: var(--ink-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-left: 0.6rem;
  border-left: 1px solid var(--rule);
  margin-left: 0.2rem;
}
@media (max-width: 620px) {
  .crumb { display: none !important; }
  .topbar .brand { flex: 0 1 auto; }
}

/* percentage beside a chapter in the contents list */
.toc-pct {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 0.1rem 0.45rem;
  margin-left: 0.5rem;
  vertical-align: 1px;
}

/* single-file edition: one chapter at a time */
.chapter-page {
  border-top: none;
  margin-top: 0.5rem;
  padding-top: 0;
}
.chapnav .nx-home {
  text-align: center;
  margin: 0 auto;
  max-width: 30%;
}
body.at-contents .topbar .crumb { display: none; }

/* the contents list is the home screen in the single file — make it scannable */
.front-matter .toc ol { padding-left: 0; list-style: none; }
.front-matter .toc li { margin: 0.55rem 0; }
.front-matter .toc a {
  display: inline-block;
  padding: 0.25rem 0;
  min-height: 30px;
}

/* ============================================================
   Chapter drawer
   ============================================================ */
.navbtn { font-size: 1rem; flex: 0 0 auto; }

.nd-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s;
  z-index: 70;
}
body.nav-open .nd-backdrop { opacity: 1; pointer-events: auto; }

.navdrawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(20rem, 86vw);
  background: var(--surface);
  border-right: 1px solid var(--rule);
  z-index: 80;
  transform: translateX(-102%);
  transition: transform 0.2s ease;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  font-family: ui-sans-serif, -apple-system, sans-serif;
  padding-bottom: 2rem;
  overscroll-behavior: contain;
}
body.nav-open .navdrawer { transform: translateX(0); }

.nd-head {
  position: sticky;
  top: 0;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem 0.7rem;
  border-bottom: 1px solid var(--rule);
  z-index: 2;
}
.nd-title { font-weight: 700; font-size: 0.95rem; }
.nd-close {
  border: 1px solid var(--rule);
  background: var(--bg);
  color: var(--ink-soft);
  border-radius: 8px;
  min-width: 34px; min-height: 34px;
  cursor: pointer;
  font-size: 0.9rem;
}
.nd-home {
  display: block;
  padding: 0.7rem 1rem;
  font-size: 0.85rem;
  color: var(--ink-soft);
  text-decoration: none;
  border-bottom: 1px solid var(--rule);
  min-height: 38px;
}
.nd-home:hover { color: var(--accent); background: var(--accent-soft); }

.nd-list { padding: 0.4rem 0; }
.nd-part {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-faint);
  padding: 0.9rem 1rem 0.3rem;
}
.nd-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0 0.6rem 0 0;
}
.nd-row.current { background: var(--accent-soft); }
.nd-row.current .nd-ch { font-weight: 700; color: var(--accent); }
.nd-ch {
  flex: 1;
  display: block;
  padding: 0.55rem 0.6rem 0.55rem 1rem;
  font-size: 0.86rem;
  line-height: 1.35;
  color: var(--ink);
  text-decoration: none;
  min-height: 40px;
  box-sizing: border-box;
}
.nd-ch:hover { color: var(--accent); }
.nd-badge {
  font-size: 0.62rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 0.1rem 0.4rem;
  flex: 0 0 auto;
}
.nd-badge.done { color: #fff; background: var(--tip-edge); }

.nd-subs {
  padding: 0.1rem 0 0.5rem 1.6rem;
  border-left: 2px solid var(--accent);
  margin-left: 1rem;
  background: color-mix(in srgb, var(--accent-soft) 45%, transparent);
}
.nd-sub {
  display: block;
  padding: 0.35rem 0.6rem 0.35rem 0;
  font-size: 0.79rem;
  line-height: 1.35;
  color: var(--ink-soft);
  text-decoration: none;
  min-height: 30px;
}
.nd-sub:hover { color: var(--accent); }

/* on a wide screen there is room to simply leave it open */
@media (min-width: 1180px) {
  .navdrawer {
    transform: none;
    top: 3.4rem;
    border-right: 1px solid var(--rule);
    width: 18rem;
  }
  .nd-backdrop { display: none; }
  .navbtn { display: none; }
  .nd-close { display: none; }
  body { padding-left: 18rem; }
  .topbar { padding-left: calc(18rem + 1.25rem); }
  #readbar { left: 18rem; }
}

/* back to top */
.totop {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 1.1rem;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 60;
  box-shadow: 0 4px 14px rgba(0,0,0,0.14);
}
.totop.show { opacity: 1; pointer-events: auto; }
.totop:hover { color: var(--accent); border-color: var(--accent); }

@media print { .navdrawer, .nd-backdrop, .totop, .navbtn { display: none !important; } }

/* site nav additions */
.toc li.current > a { font-weight: 700; color: var(--ink); }
.chapnav { display:flex; justify-content:space-between; gap:1rem; margin:3rem 0 0;
           padding-top:1.5rem; border-top:1px solid var(--rule);
           font-family: ui-sans-serif,-apple-system,sans-serif; font-size:.85rem; }
.chapnav a { display:block; max-width:46%; text-decoration:none; }
.chapnav .dir { display:block; font-size:.68rem; letter-spacing:.1em; text-transform:uppercase; color:var(--ink-faint); }
.chapnav .nx { text-align:right; margin-left:auto; }
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

:root[data-theme="dark"] {
      --bg: #101214;
      --surface: #16191c;
      --ink: #e8eaed;
      --ink-soft: #b6bac0;
      --ink-faint: #7d838b;
      --accent: #7aa2f7;
      --accent-soft: #1b2537;
      --rule: #2a2e33;
      --code-bg: #0b0d10;
      --code-ink: #dde1e8;
      --code-label: #6e757e;
      --key-bg: #16233a;
      --key-edge: #5b86e5;
      --warn-bg: #331d1b;
      --warn-edge: #e06c62;
      --tip-bg: #16291c;
      --tip-edge: #4fae72;
      --ex-bg: #2b2417;
      --ex-edge: #cda04a;
      --table-head: #1d2125;
    }
