/* ============================================================
   Post-Training Readbook — shared stylesheet
   Book typography: Source Serif 4 for reading, Inter for UI.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;0,8..60,500;0,8..60,600;1,8..60,400&display=swap');

:root {
  --bg: #fdfdfb;
  --bg-sidebar: #f6f5f1;
  --bg-code: #f4f3ee;
  --bg-callout: #f0f4f8;
  --bg-callout-warn: #fdf3e7;
  --bg-callout-key: #eef7ee;
  --text: #21242a;
  --text-muted: #5c6470;
  --heading: #14161a;
  --link: #14532d;
  --link-hover: #166534;
  --accent: #15803d;
  --accent-soft: #dcebdc;
  --border: #e5e3db;
  --border-strong: #cfccc2;
  --sidebar-active: #e5efe5;
  --table-stripe: #f7f6f2;
  --code-text: #24292f;
  --scrollbar: #d0cec6;
  /* chart series colors (categorical, CVD-validated for these surfaces) */
  --s1: #2a78d6;
  --s2: #008300;
  --s3: #eda100;
  --font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #16181d;
  --bg-sidebar: #1c1f26;
  --bg-code: #21242c;
  --bg-callout: #1d2531;
  --bg-callout-warn: #2c2418;
  --bg-callout-key: #1b2a1f;
  --text: #cfd4dc;
  --text-muted: #8b93a1;
  --heading: #eef1f5;
  --link: #7fd49a;
  --link-hover: #a3e6b8;
  --accent: #4ade80;
  --accent-soft: #24322a;
  --border: #2b2f38;
  --border-strong: #3a3f4b;
  --sidebar-active: #24322a;
  --table-stripe: #1b1e25;
  --code-text: #d5d9e0;
  --scrollbar: #3a3f4b;
  --s1: #3987e5;
  --s2: #008300;
  --s3: #c98500;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 18px;
  line-height: 1.72;
  color: var(--text);
  background: var(--bg);
  display: flex;
  min-height: 100vh;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--accent-soft); }

/* ---------- Reading progress ---------- */
#progress {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 0;
  background: var(--accent);
  z-index: 200;
  transition: width .08s linear;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 1.4rem 0 3rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
  font-family: var(--font-sans);
}
#sidebar::-webkit-scrollbar { width: 8px; }
#sidebar::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 4px; }

#sidebar .book-title {
  display: block;
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: -.01em;
  color: var(--heading);
  text-decoration: none;
  padding: 0 1.4rem .3rem;
  line-height: 1.35;
}
#sidebar .book-subtitle {
  padding: 0 1.4rem 1rem;
  font-size: .78rem;
  color: var(--text-muted);
}
#sidebar .part-label {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .1em;
  font-weight: 700;
  color: var(--text-muted);
  padding: 1.15rem 1.4rem .35rem;
}
#sidebar a.chapter {
  display: block;
  padding: .3rem 1.4rem;
  color: var(--text);
  text-decoration: none;
  font-size: .86rem;
  line-height: 1.45;
  border-left: 3px solid transparent;
  font-weight: 450;
}
#sidebar a.chapter:hover { background: var(--sidebar-active); }
#sidebar a.chapter.active {
  background: var(--sidebar-active);
  border-left-color: var(--accent);
  font-weight: 650;
  color: var(--heading);
}
#sidebar a.chapter .chnum {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-right: .45em;
  font-size: .82em;
  font-weight: 600;
}

/* ---------- Main content ---------- */
#content {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
}
main {
  max-width: 760px;
  width: 100%;
  padding: 3rem 3rem 5rem;
}

/* ---------- Topbar ---------- */
#topbar {
  position: fixed;
  top: .8rem;
  right: 1rem;
  display: flex;
  gap: .5rem;
  z-index: 50;
}
#topbar button, #topbar a.gh {
  background: var(--bg-sidebar);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 38px; height: 38px;
  font-size: 1.02rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  text-decoration: none;
}
#topbar button:hover, #topbar a.gh:hover { border-color: var(--accent); }
#menu-btn { display: none !important; }

/* ---------- Typography ---------- */
h1, h2, h3, h4 {
  font-family: var(--font-sans);
  color: var(--heading);
  line-height: 1.22;
  scroll-margin-top: 1.2rem;
  letter-spacing: -.015em;
}
h1 { font-size: 2.15rem; font-weight: 800; margin: 0 0 .4rem; }
h2 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-top: 2.8rem;
  padding-top: 1.4rem;
  border-top: 1px solid var(--border);
  position: relative;
}
h3 { font-size: 1.12rem; font-weight: 650; margin-top: 2rem; }
h4 { font-size: .98rem; font-weight: 650; margin-top: 1.6rem; }

/* anchor links on h2 */
h2 .hlink {
  opacity: 0;
  margin-left: .4rem;
  font-size: .85em;
  text-decoration: none;
  color: var(--text-muted);
  transition: opacity .12s;
}
h2:hover .hlink { opacity: 1; }

.chapter-eyebrow {
  font-family: var(--font-sans);
  font-size: .76rem;
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: .7rem;
}
.chapter-lede {
  font-size: 1.16rem;
  font-style: italic;
  color: var(--text-muted);
  margin: .4rem 0 1.8rem;
  line-height: 1.6;
}
.chapter-meta {
  font-family: var(--font-sans);
  font-size: .78rem;
  color: var(--text-muted);
  margin-bottom: 2rem;
  padding-bottom: 1.4rem;
  border-bottom: 1px solid var(--border);
}

p { margin: 1.05rem 0; }
a { color: var(--link); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2.5px; text-decoration-color: color-mix(in srgb, var(--link) 45%, transparent); }
a:hover { color: var(--link-hover); text-decoration-color: var(--link-hover); }
strong { color: var(--heading); font-weight: 600; }
ul, ol { padding-left: 1.5rem; }
li { margin: .4rem 0; }
hr { border: none; border-top: 1px solid var(--border); margin: 2.5rem 0; }

blockquote {
  margin: 1.2rem 0;
  padding: .2rem 1.2rem;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
  font-style: italic;
}

/* ---------- In-page TOC (auto-generated) ---------- */
.page-toc {
  font-family: var(--font-sans);
  font-size: .84rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: .9rem 1.2rem;
  margin: 1.6rem 0 2rem;
  background: var(--bg-sidebar);
}
.page-toc .page-toc-title {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .09em;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: .4rem;
}
.page-toc ol {
  margin: 0;
  padding-left: 1.2rem;
  columns: 2;
  column-gap: 2rem;
}
.page-toc li { margin: .2rem 0; break-inside: avoid; }
.page-toc a { text-decoration: none; color: var(--text); }
.page-toc a:hover { color: var(--accent); }

/* ---------- Code ---------- */
code {
  font-family: var(--font-mono);
  font-size: .82em;
  background: var(--bg-code);
  color: var(--code-text);
  padding: .12em .38em;
  border-radius: 5px;
}
pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.05rem 1.25rem;
  overflow-x: auto;
  line-height: 1.55;
  margin: 1.3rem 0;
  scrollbar-width: thin;
}
pre code { background: none; padding: 0; font-size: .83rem; display: block; }

pre .c  { color: #6a737d; font-style: italic; }
pre .k  { color: #b45309; }
pre .s  { color: #0e7490; }
pre .f  { color: #7c3aed; }
[data-theme="dark"] pre .c { color: #7d8590; }
[data-theme="dark"] pre .k { color: #f0b46c; }
[data-theme="dark"] pre .s { color: #67c5db; }
[data-theme="dark"] pre .f { color: #b79ef7; }

/* ---------- Callouts ---------- */
.callout {
  border-radius: 12px;
  padding: 1rem 1.3rem;
  margin: 1.5rem 0;
  background: var(--bg-callout);
  border: 1px solid var(--border);
}
.callout .callout-title {
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  margin-bottom: .3rem;
  color: var(--text-muted);
}
.callout p:first-of-type { margin-top: .2rem; }
.callout p:last-child { margin-bottom: .2rem; }
.callout.key   { background: var(--bg-callout-key); }
.callout.key .callout-title { color: var(--accent); }
.callout.warn  { background: var(--bg-callout-warn); }
.callout.warn .callout-title { color: #b45309; }
[data-theme="dark"] .callout.warn .callout-title { color: #f0b46c; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 1.5rem 0; border: 1px solid var(--border); border-radius: 12px; }
table {
  border-collapse: collapse;
  width: 100%;
  font-family: var(--font-sans);
  font-size: .85rem;
  line-height: 1.5;
}
th, td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: .6rem .8rem;
  text-align: left;
  vertical-align: top;
}
th:last-child, td:last-child { border-right: none; }
tbody tr:last-child td { border-bottom: none; }
th { background: var(--bg-sidebar); color: var(--heading); font-weight: 650; }
tbody tr:nth-child(even) { background: var(--table-stripe); }
tbody tr:hover { background: var(--sidebar-active); }

/* ---------- Figures / diagrams ---------- */
figure { margin: 2rem 0; text-align: center; }
figure svg { max-width: 100%; height: auto; }
figcaption {
  font-family: var(--font-sans);
  font-size: .82rem;
  color: var(--text-muted);
  margin-top: .7rem;
  text-align: center;
  line-height: 1.55;
  max-width: 60ch;
  margin-left: auto;
  margin-right: auto;
}
.diagram {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.4rem 1rem;
}

svg .d-text  { fill: var(--text); }
svg .d-muted { fill: var(--text-muted); }
svg .d-box   { fill: var(--bg); stroke: var(--border-strong); }
svg .d-box-accent { fill: var(--sidebar-active); stroke: var(--accent); }
svg .d-arrow { stroke: var(--text-muted); fill: none; }
svg .d-arrowhead { fill: var(--text-muted); }
svg .d-grid  { stroke: var(--border); stroke-width: 1; }
svg .d-axis  { stroke: var(--border-strong); stroke-width: 1.2; fill: none; }
svg .d-l1 { stroke: var(--s1); stroke-width: 2; fill: none; }
svg .d-l2 { stroke: var(--s2); stroke-width: 2; fill: none; }
svg .d-l3 { stroke: var(--s3); stroke-width: 2; fill: none; }
svg .d-l1d { stroke: var(--s1); stroke-width: 2; fill: none; stroke-dasharray: 4 4; }
svg .d-l2d { stroke: var(--s2); stroke-width: 2; fill: none; stroke-dasharray: 4 4; }
svg .d-tok-mask  { fill: var(--bg-code); stroke: var(--border); }
svg .d-tok-train { fill: var(--bg-callout-key); stroke: var(--accent); stroke-width: 1.4; }
svg .d-tok-special { fill: var(--bg-callout); stroke: var(--border-strong); }
svg text { font-family: var(--font-sans); }

/* Scroll-triggered draw-in for tagged figure curves (pathLength=100 required on the path) */
@media (prefers-reduced-motion: no-preference) {
  figure svg .anim-draw {
    stroke-dasharray: 100;
    stroke-dashoffset: 100;
  }
  figure.in-view svg .anim-draw {
    animation: rb-drawline 1.1s ease-out forwards;
  }
  figure.in-view svg .anim-draw.anim-delay { animation-delay: .45s; }
  @keyframes rb-drawline { to { stroke-dashoffset: 0; } }
  figure svg .anim-pop { opacity: 0; transform-origin: center; }
  figure.in-view svg .anim-pop { animation: rb-pop .4s ease-out forwards; animation-delay: 1.1s; }
  @keyframes rb-pop { from { opacity: 0; } to { opacity: 1; } }
}

/* ---------- Interactive widgets ---------- */
.widget {
  border: 1px solid var(--border);
  border-radius: 14px;
  margin: 2rem 0;
  background: var(--bg-sidebar);
  overflow: hidden;
  font-family: var(--font-sans);
}
.widget .widget-head {
  padding: .85rem 1.3rem .7rem;
  border-bottom: 1px solid var(--border);
}
.widget .widget-title {
  font-weight: 700;
  font-size: .92rem;
  color: var(--heading);
}
.widget .widget-title::before {
  content: "⟲ ";
  color: var(--accent);
}
.widget .widget-sub {
  font-size: .78rem;
  color: var(--text-muted);
  margin-top: .15rem;
}
.widget .widget-body { padding: 1rem 1.3rem 1.2rem; }
.widget .controls {
  display: flex;
  flex-wrap: wrap;
  gap: .9rem 1.8rem;
  margin-bottom: .9rem;
}
.widget .ctl { display: flex; flex-direction: column; gap: .25rem; min-width: 160px; flex: 1; }
.widget .ctl label {
  font-size: .76rem;
  font-weight: 600;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}
.widget .ctl label output {
  color: var(--heading);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}
.widget input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}
.widget .readouts {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem 1.6rem;
  margin-top: .8rem;
  font-size: .8rem;
}
.widget .readout b {
  color: var(--heading);
  font-variant-numeric: tabular-nums;
}
.widget .readout .tag {
  display: inline-block;
  padding: .05rem .5rem;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
}
.widget .tag.ok   { background: var(--bg-callout-key); color: var(--accent); }
.widget .tag.dead { background: var(--bg-callout-warn); color: #b45309; }
[data-theme="dark"] .widget .tag.dead { color: #f0b46c; }
.widget svg { display: block; width: 100%; height: auto; }
.widget .toggle-row { display: flex; gap: .45rem; flex-wrap: wrap; }
.widget .tok-toggle {
  font-family: var(--font-sans);
  font-size: .8rem;
  font-weight: 700;
  width: 44px; height: 36px;
  border-radius: 9px;
  border: 1.5px solid var(--border-strong);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
}
.widget .tok-toggle.on {
  background: var(--bg-callout-key);
  border-color: var(--accent);
  color: var(--accent);
}
.widget-note {
  font-size: .78rem;
  color: var(--text-muted);
  margin-top: .7rem;
  line-height: 1.5;
}

/* ---------- Math ---------- */
.katex-display { overflow-x: auto; overflow-y: hidden; padding: .4rem 0; }
.katex { font-size: 1.05em; }

/* ---------- Chapter nav (prev/next) ---------- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-sans);
}
.chapter-nav a {
  display: block;
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: .85rem 1.15rem;
  max-width: 48%;
  color: var(--text);
  transition: border-color .12s, transform .12s;
}
.chapter-nav a:hover { border-color: var(--accent); transform: translateY(-1px); }
.chapter-nav .nav-dir {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  font-weight: 700;
}
.chapter-nav .nav-title { font-weight: 650; color: var(--heading); font-size: .9rem; }
.chapter-nav a.next { margin-left: auto; text-align: right; }

/* ---------- Cover / index page ---------- */
.cover { padding-top: 1.5rem; }
.cover h1 {
  font-size: 2.9rem;
  line-height: 1.08;
  background: linear-gradient(120deg, var(--heading) 55%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.cover .cover-tagline {
  font-size: 1.22rem;
  color: var(--text-muted);
  max-width: 42rem;
  font-style: italic;
  line-height: 1.6;
}
.cover-stats {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem;
  margin: 1.6rem 0 .4rem;
  font-family: var(--font-sans);
}
.cover-stats .stat {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: .3rem .95rem;
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg-sidebar);
}
.cover-stats .stat b { color: var(--accent); }

.toc-part { margin-top: 2.2rem; }
.toc-part h2 { border: none; padding-top: 0; margin-top: 0; font-size: .82rem; text-transform: uppercase; letter-spacing: .11em; color: var(--accent); }
.toc-list { list-style: none; padding: 0; margin: .6rem 0 0; font-family: var(--font-sans); }
.toc-list li { margin: 0; }
.toc-list a {
  display: flex;
  gap: .9rem;
  padding: .72rem .9rem;
  border-radius: 12px;
  text-decoration: none;
  color: var(--text);
  transition: background .1s;
}
.toc-list a:hover { background: var(--bg-sidebar); }
.toc-list .toc-num {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  font-weight: 700;
  min-width: 1.7rem;
}
.toc-list .toc-title { font-weight: 650; color: var(--heading); }
.toc-list .toc-desc { font-size: .84rem; color: var(--text-muted); display: block; line-height: 1.5; }

/* ---------- Paper citation chips ---------- */
.paper-ref { font-family: var(--font-sans); font-size: .82rem; color: var(--text-muted); }
.refs { font-size: .92rem; padding-left: 1.4rem; }
.refs li { margin: .55rem 0; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  body { font-size: 17px; }
  #sidebar {
    position: fixed;
    left: 0; top: 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    z-index: 100;
    box-shadow: 4px 0 24px rgba(0,0,0,.15);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  #menu-btn { display: flex !important; }
  main { padding: 3.8rem 1.3rem 4rem; }
  body.sidebar-open #overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 90;
  }
  .page-toc ol { columns: 1; }
  .cover h1 { font-size: 2.2rem; }
}

@media print {
  #sidebar, #topbar, .chapter-nav, #progress, .page-toc { display: none; }
  main { max-width: 100%; padding: 0; }
  .widget { display: none; }
  /* animations must not leave un-scrolled figures blank on paper */
  figure svg .anim-draw { animation: none; stroke-dasharray: none; stroke-dashoffset: 0; }
  figure svg .anim-pop  { animation: none; opacity: 1; }
}
