/* ==========================================================================
   The reader — KB Book's Read tab
   ==========================================================================

   Everything a `book` payload renders (reader.js). Written entirely against the
   token contract in styles.css, so a course contributes seven seed colours and
   the whole reading experience re-tones with it. There is no per-course CSS
   here, and there must never be.

   It is a separate file for the same reason reader.js is: rendering a book is
   its own concern with its own vocabulary, and styles.css is already 2,850
   lines of course-shell.

   Three rules this sheet holds to:
     1. One measure. Prose sits in a 68ch column; only wide things — diagrams,
        tables, labs, code — break out of it.
     2. Nothing scrolls the page sideways. Wide content scrolls inside its own
        container, which is what keeps a nine-column table usable at 360px.
     3. Motion is opt-in: every transition sits inside a
        prefers-reduced-motion: no-preference guard.
   ========================================================================== */

.book {
  --measure: 68ch;
  --book-gap: 26px;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--book-gap);
}

/* --- reading progress ---------------------------------------------------- */

.book-progress {
  position: sticky;
  top: 0;
  z-index: 4;
  height: 3px;
  margin: -4px 0 -3px;
  background: color-mix(in srgb, var(--border) 60%, transparent);
  border-radius: 999px;
  overflow: hidden;
}
.book-progress-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  transform-origin: 0 50%;
  transform: scaleX(0);
}

/* --- front matter -------------------------------------------------------- */

.book-front {
  max-width: var(--measure);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.book-front-kicker {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font-size: .78rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-number {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent);
}
.book-level {
  padding: 2px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  letter-spacing: .04em;
}
.book-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.55rem, 1.1rem + 1.6vw, 2.2rem);
  line-height: 1.15;
  letter-spacing: -.025em;
}
.book-outcome {
  margin: 0;
  font-size: 1.02rem;
  line-height: 1.6;
  color: var(--text-2);
}
.book-outcome span {
  display: block;
  margin-bottom: 3px;
  font-size: .74rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-facts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.book-facts li {
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: .78rem;
  color: var(--text-2);
}
.book-prereqs {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-2);
}
.book-prereqs-label {
  margin: 0 0 4px;
  font-size: .72rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-prereqs ul {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 3px;
  font-size: .92rem;
}

/* --- contents ------------------------------------------------------------ */

.book-toc {
  max-width: var(--measure);
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  box-shadow: var(--shadow-1);
}
.book-toc-label {
  margin: 0 0 8px;
  font-size: .72rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-toc ol {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: toc;
  display: grid;
  gap: 2px;
}
.book-toc li { counter-increment: toc; }
.book-toc a {
  display: block;
  padding: 4px 6px;
  border-radius: var(--r-sm);
  color: var(--text-2);
  text-decoration: none;
  font-size: .93rem;
}
.book-toc a::before {
  content: counter(toc, decimal-leading-zero) "  ";
  font-family: var(--font-mono);
  font-size: .78em;
  color: var(--text-3);
}
.book-toc a:hover { background: var(--surface-2); color: var(--text); }

/* --- prose --------------------------------------------------------------- */

.book-body { display: flex; flex-direction: column; gap: 30px; }
.book-section { display: flex; flex-direction: column; gap: 14px; }

.book-section-title {
  max-width: var(--measure);
  margin: 8px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-family: var(--font-display);
  font-size: 1.3rem;
  letter-spacing: -.02em;
  scroll-margin-top: calc(var(--topbar-h) + var(--crumbs-h) + 20px);
}
.book-h {
  max-width: var(--measure);
  margin: 10px 0 0;
  font-family: var(--font-display);
  letter-spacing: -.015em;
  scroll-margin-top: calc(var(--topbar-h) + var(--crumbs-h) + 20px);
}
.book-h3 { font-size: 1.1rem; }
.book-h4 { font-size: 1rem; color: var(--text-2); }
.book-h5, .book-h6 { font-size: .94rem; color: var(--text-2); }

.book-body p,
.book-callout-body p,
.book-reveal-body p {
  max-width: var(--measure);
  margin: 0;
  font-size: 1.02rem;
  line-height: 1.72;
}
.book-body a { color: var(--accent); text-underline-offset: 3px; }
.book-ext { margin-left: 2px; font-size: .8em; opacity: .7; }
.book-xref { text-decoration-style: dotted; }

.book-list {
  max-width: var(--measure);
  margin: 0;
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 1.02rem;
  line-height: 1.7;
}
.book-list li > .book-list { margin-top: 6px; }
.book-list li::marker { color: var(--text-3); }

.book-quote {
  max-width: var(--measure);
  margin: 0;
  padding: 12px 0 12px 18px;
  border-left: 3px solid var(--accent);
  font-size: 1.06rem;
}
.book-quote p { font-size: inherit; }

.book-rule {
  max-width: var(--measure);
  margin: 4px 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.book-caption {
  max-width: var(--measure);
  margin: 0;
  font-size: .86rem;
  font-style: italic;
  color: var(--text-3);
}

.book-body :not(pre) > code {
  padding: .12em .38em;
  border-radius: 5px;
  background: var(--surface-3);
  font-size: .88em;
  word-break: break-word;
}
.book-body kbd {
  padding: .1em .4em;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  font-family: var(--font-mono);
  font-size: .82em;
}

/* --- tables -------------------------------------------------------------- */

.book-table-wrap {
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  -webkit-overflow-scrolling: touch;
}
.book-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .93rem;
}
.book-table th,
.book-table td {
  padding: 10px 13px;
  text-align: left;
  vertical-align: top;
  line-height: 1.55;
  border-bottom: 1px solid var(--border);
}
.book-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  font-family: var(--font-display);
  font-size: .8rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-2);
  white-space: nowrap;
}
.book-table tbody tr:last-child td { border-bottom: 0; }
.book-table tbody tr:hover { background: var(--surface-2); }

/* --- code and captured output -------------------------------------------- */

.book-code,
.book-output,
.book-files-pane {
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-2);
  overflow: hidden;
}
.book-output { background: var(--surface-3); }

.book-code-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 13px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 65%, transparent);
}
.book-code-lang {
  font-family: var(--font-mono);
  font-size: .7rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-code-flag {
  padding: 1px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--hue-warn) 16%, var(--surface));
  color: color-mix(in srgb, var(--hue-warn) 75%, var(--text));
  font-size: .7rem;
  letter-spacing: .03em;
}
.book-code-flag.is-bad {
  background: color-mix(in srgb, var(--hue-risk) 16%, var(--surface));
  color: color-mix(in srgb, var(--hue-risk) 75%, var(--text));
}
.book-copy {
  margin-left: auto;
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-2);
  font: inherit;
  font-size: .74rem;
  cursor: pointer;
}
.book-copy:hover { color: var(--text); border-color: var(--border-strong); }

.book-code pre,
.book-output pre,
.book-files-pane pre {
  margin: 0;
  padding: 13px 15px;
  overflow-x: auto;
  font-size: .855rem;
  line-height: 1.62;
  tab-size: 2;
}
.book-code code,
.book-output code,
.book-files-pane code {
  font-family: var(--font-mono);
  white-space: pre;
  /* No ligatures. This book teaches syntax, and someone meeting arrow functions
     for the first time should see an equals sign and a greater-than sign, not
     the single arrow glyph JetBrains Mono draws in their place. */
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
}

/* Source keeps the app's terminal block, which every course already uses.
   Captured output must NOT: the book's whole predict-then-verify device depends
   on a reader telling at a glance which is the program and which is what it
   printed. So output is a quiet transcript panel instead. */
.book-output pre {
  color: var(--text-2);
  background: var(--surface-3);
  border: 1px solid var(--border);
}

.book-diagram-source {
  margin: 0;
  padding: 12px 14px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--r);
  background: var(--surface-2);
  overflow-x: auto;
  font-size: .8rem;
}

/* --- callouts ------------------------------------------------------------ */

.book-callout {
  max-width: var(--measure);
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 13px 16px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--hue-info);
  border-radius: var(--r);
  background: color-mix(in srgb, var(--hue-info) 7%, var(--surface));
}
.book-callout-label {
  margin: 0;
  font-family: var(--font-display);
  font-size: .76rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: color-mix(in srgb, var(--hue-info) 70%, var(--text));
}
.book-callout-body { display: flex; flex-direction: column; gap: 9px; }
.book-callout-body p { font-size: .97rem; }

.book-callout.is-tip {
  border-left-color: var(--hue-good);
  background: color-mix(in srgb, var(--hue-good) 7%, var(--surface));
}
.book-callout.is-tip .book-callout-label { color: color-mix(in srgb, var(--hue-good) 70%, var(--text)); }

.book-callout.is-warning {
  border-left-color: var(--hue-warn);
  background: color-mix(in srgb, var(--hue-warn) 8%, var(--surface));
}
.book-callout.is-warning .book-callout-label { color: color-mix(in srgb, var(--hue-warn) 72%, var(--text)); }

.book-callout.is-caution {
  border-left-color: var(--hue-risk);
  background: color-mix(in srgb, var(--hue-risk) 8%, var(--surface));
}
.book-callout.is-caution .book-callout-label { color: color-mix(in srgb, var(--hue-risk) 72%, var(--text)); }

.book-callout.is-important {
  border-left-color: var(--accent);
  background: var(--accent-soft);
}
.book-callout.is-important .book-callout-label { color: var(--accent); }

/* --- predict then verify -------------------------------------------------- */

.book-reveal {
  max-width: var(--measure);
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  overflow: hidden;
}
.book-reveal > summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  cursor: pointer;
  list-style: none;
  font-size: .93rem;
  color: var(--text-2);
}
.book-reveal > summary::-webkit-details-marker { display: none; }
.book-reveal > summary::after {
  content: "";
  flex: none;
  margin-left: auto;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-3);
  border-bottom: 2px solid var(--text-3);
  transform: rotate(45deg);
}
.book-reveal[open] > summary::after { transform: rotate(-135deg); }
.book-reveal[open] > summary {
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.book-reveal-label {
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: .72rem;
  letter-spacing: .05em;
  text-transform: uppercase;
  white-space: nowrap;
}
.book-reveal-summary { color: var(--text-2); }
.book-reveal-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
}
.book-reveal.is-belief .book-reveal-label {
  background: color-mix(in srgb, var(--hue-warn) 16%, var(--surface));
  color: color-mix(in srgb, var(--hue-warn) 78%, var(--text));
}
.book-reveal.is-deeper .book-reveal-label { background: var(--surface-3); color: var(--text-2); }

/* --- figures ------------------------------------------------------------- */

.book-figure {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--dgm-bg);
  scroll-margin-top: calc(var(--topbar-h) + var(--crumbs-h) + 20px);
}
.book-diagram-stage {
  display: flex;
  justify-content: center;
  overflow-x: auto;
  color: var(--dgm-text);
}
.book-diagram-stage svg { max-width: 100%; height: auto; }
/* An animated figure is authored at a fixed size and has no reason to fill the
   column either; it is capped, not stretched. */
.book-animated .book-diagram-stage svg { width: 100%; max-width: 760px; }

/* Mermaid's own stylesheet is stripped at build time, so the type and lines it
   emits are coloured here once — for all 301 diagrams, in both themes.

   Only colour and face: anchoring, size and weight are baked onto each text
   node during the render, because they are layout, and the boxes were measured
   against them. */
.book-diagram-stage .mermaid-svg text,
.book-diagram-stage .mermaid-svg tspan {
  fill: var(--dgm-text);
  font-family: var(--font-body);
}
.book-diagram-stage .mermaid-svg .edgeLabel text,
.book-diagram-stage .mermaid-svg .edgeLabel tspan { fill: var(--text-2); }
.book-diagram-stage .mermaid-svg .edgeLabel rect { fill: var(--dgm-bg); }
.book-diagram-stage .mermaid-svg .cluster-label text,
.book-diagram-stage .mermaid-svg .cluster-label tspan { fill: var(--text-2); }
.book-diagram-stage .mermaid-svg .marker,
.book-diagram-stage .mermaid-svg .arrowMarkerPath {
  fill: var(--dgm-stroke);
  stroke: var(--dgm-stroke);
}
.book-diagram-stage .mermaid-svg .flowchart-link,
.book-diagram-stage .mermaid-svg .transition,
.book-diagram-stage .mermaid-svg .messageLine0,
.book-diagram-stage .mermaid-svg .messageLine1 {
  fill: none;
  stroke: var(--dgm-stroke);
}
.book-diagram-stage .mermaid-svg .cluster rect,
.book-diagram-stage .mermaid-svg .label-container {
  fill: var(--dgm-node);
  stroke: var(--dgm-stroke);
}

.book-diagram-pending {
  width: 100%;
  min-height: 120px;
  border-radius: var(--r);
  background: color-mix(in srgb, var(--border) 40%, transparent);
}
.book-diagram-failed { margin: 0; font-size: .9rem; color: var(--text-3); }

.book-figure-tools { display: flex; gap: 8px; }
.book-figure-btn {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-2);
  font: inherit;
  font-size: .76rem;
  text-decoration: none;
  cursor: pointer;
}
.book-figure-btn:hover { color: var(--text); border-color: var(--border-strong); }
.book-figure-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* The lab card's "Open in a tab" carries a glyph, so its pill is a flex row and
   a real 36px target rather than a 4px-padded link. */
.book-lab-tools .book-figure-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
}

.book-figure.is-targeted,
.book-lab.is-targeted,
.book-files.is-targeted { outline: 2px solid var(--accent); outline-offset: 4px; }

/* --- interactive labs ---------------------------------------------------- */

.book-lab {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-1);
  scroll-margin-top: calc(var(--topbar-h) + var(--crumbs-h) + 20px);
}
.book-lab-head {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-start;
  justify-content: space-between;
}
.book-lab-kicker {
  margin: 0 0 2px;
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--accent);
}
.book-lab-title { margin: 0; font-family: var(--font-display); font-size: 1.04rem; }
.book-lab-sub { margin: 4px 0 0; max-width: 60ch; font-size: .88rem; color: var(--text-2); }
.book-lab-tools { display: flex; gap: 8px; }
.book-lab-intro { display: flex; flex-direction: column; gap: 8px; }
.book-lab-intro p { font-size: .95rem; }

/* The card's stage is a poster now, not a frame holder: one control, sized so
   the whole row is a comfortable target rather than a link-sized one. */
.book-lab-stage {
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  background: linear-gradient(135deg,
    color-mix(in srgb, var(--accent) 7%, var(--surface-2)),
    var(--surface-2) 62%);
}
.book-lab-start {
  display: flex;
  align-items: center;
  gap: var(--space-3, 14px);
  width: 100%;
  min-height: 96px;
  padding: 18px 20px;
  border: 0;
  background: none;
  color: var(--text-2);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background .18s ease, color .18s ease;
}
.book-lab-start-icon {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent, #fff);
  box-shadow: 0 6px 18px -8px color-mix(in srgb, var(--accent) 70%, transparent);
  transition: transform .18s var(--ease-out, ease);
}
.book-lab-start-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.book-lab-start-label { font-weight: 600; font-size: 1rem; color: var(--text); }
.book-lab-start-note { font-size: .82rem; line-height: 1.45; color: var(--text-2); }
.book-lab-start:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.book-lab-start:hover .book-lab-start-icon { transform: scale(1.05); }
.book-lab-start:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.book-lab.is-running .book-lab-start-label::after {
  content: " — running";
  color: var(--text-3);
  font-weight: 400;
}

/* --- the full-viewport lab shell ----------------------------------------- */

/* Above the PWA toast (9000) and the TTS player (8500): the point of the shell
   is that nothing else is on the screen. */
.lab-shell {
  position: fixed;
  inset: 0;
  z-index: 9500;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--bg);
  color: var(--text);
  /* Above 761px the shell's scroller is `.main`, not the body, and it is still
     there behind the overlay. Contain the chain so a wheel that reaches the
     title bar does not scroll the chapter you cannot see. */
  overscroll-behavior: contain;
}
.lab-shell[hidden] { display: none; }
/* Only bites below 761px; above it the app already pins the body and scrolls
   `.main` instead. */
body.lab-shell-open { overflow: hidden; }

.lab-shell-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3, 14px);
  padding: 12px clamp(14px, 2.5vw, 28px);
  padding-left: max(clamp(14px, 2.5vw, 28px), env(safe-area-inset-left));
  padding-right: max(clamp(14px, 2.5vw, 28px), env(safe-area-inset-right));
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-1);
}
.lab-shell-id { min-width: 0; }
.lab-shell-kicker {
  margin: 0;
  font-size: .68rem;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--accent);
}
.lab-shell-title {
  margin: 1px 0 0;
  font-family: var(--font-display);
  font-size: 1.06rem;
  line-height: 1.25;
  color: var(--text);
}
.lab-shell-context {
  margin: 2px 0 0;
  font-size: .8rem;
  color: var(--text-3);
}
.lab-shell-context[hidden] { display: none; }
.lab-shell-tools { display: flex; flex-wrap: wrap; gap: var(--space-2, 8px); }

.lab-shell-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-2);
  color: var(--text-2);
  font: inherit;
  font-size: .84rem;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background .16s ease, border-color .16s ease, color .16s ease;
}
.lab-shell-btn:hover { color: var(--text); border-color: var(--border-strong); background: var(--surface-3); }
.lab-shell-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.lab-shell-btn .lab-icon { flex: 0 0 auto; }
.lab-shell-btn kbd {
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  font-family: var(--font-mono);
  font-size: .68rem;
  color: var(--text-3);
}
.lab-shell-btn.is-exit { border-color: var(--border-strong); color: var(--text); }

.lab-shell-stage { min-height: 0; background: var(--surface); }
.lab-shell-stage .book-lab-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--surface);
}

/* The exit confirmation. It scrims the lab rather than replacing it, so what
   you are being asked to leave is still visible behind the question. */
.lab-shell-veil {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(16px, 5vw, 48px);
  background: rgba(8, 10, 18, .62);
  backdrop-filter: blur(3px);
}
.lab-shell-veil[hidden] { display: none; }
.lab-shell-confirm {
  width: min(440px, 100%);
  padding: var(--space-4, 20px);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-2);
}
.lab-shell-confirm-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.12rem;
  color: var(--text);
}
.lab-shell-confirm-note {
  margin: 8px 0 0;
  font-size: .9rem;
  line-height: 1.5;
  color: var(--text-2);
}
/* "Leave" sits apart from "Keep working", and does not take focus first: the
   confirmation exists to make leaving deliberate. */
.lab-shell-confirm-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-3, 14px);
  margin-top: var(--space-4, 20px);
}
.lab-shell-btn.is-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--on-accent, #fff);
  font-weight: 600;
}
.lab-shell-btn.is-primary:hover {
  background: var(--accent-strong, var(--accent));
  color: var(--on-accent, #fff);
  border-color: transparent;
}
.lab-shell-btn.is-leave {
  border-color: color-mix(in srgb, var(--red) 40%, var(--border));
  background: transparent;
  color: var(--red);
}
.lab-shell-btn.is-leave:hover {
  background: color-mix(in srgb, var(--red) 12%, transparent);
  border-color: var(--red);
  color: var(--red);
}
.lab-shell-btn.is-leave:focus-visible { outline-color: var(--red); }
/* The confirmation takes focus programmatically, so `:focus-visible` never
   fires for it. A modal that has stolen focus must still show where it went. */
.lab-shell-confirm .lab-shell-btn:focus { outline: 2px solid var(--accent); outline-offset: 2px; }
.lab-shell-confirm .lab-shell-btn.is-leave:focus { outline-color: var(--red); }

@media (prefers-reduced-motion: no-preference) {
  .lab-shell { animation: lab-shell-in .22s ease both; }
  .lab-shell-confirm { animation: lab-confirm-in .18s var(--ease-out, ease) both; }
}
@keyframes lab-shell-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes lab-confirm-in {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 620px) {
  .lab-shell-bar { padding-bottom: 10px; }
  .lab-shell-tools { width: 100%; }
  .lab-shell-btn { flex: 1 1 auto; justify-content: center; padding: 0 10px; }
  .lab-shell-btn kbd { display: none; }
  .lab-shell-confirm-row .lab-shell-btn { flex: 1 1 140px; }
}

/* --- talks --------------------------------------------------------------- */

.book-video {
  display: grid;
  grid-template-columns: minmax(240px, 1.15fr) 1fr;
  gap: 18px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.book-video-frame {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--r);
  overflow: hidden;
  background: var(--surface-3);
}
.book-video-play {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}
.book-video-poster { display: block; width: 100%; height: 100%; object-fit: cover; }
.book-video-badge {
  position: absolute;
  inset: 50% auto auto 50%;
  translate: -50% -50%;
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: color-mix(in srgb, #000 55%, transparent);
  color: #fff;
  font-size: 1.1rem;
}
.book-video-embed { width: 100%; height: 100%; border: 0; }
.book-video-meta { display: flex; flex-direction: column; gap: 6px; }
.book-video-kicker {
  margin: 0;
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--accent);
}
.book-video-title { margin: 0; font-family: var(--font-display); font-size: 1.04rem; }
.book-video-byline { margin: 0; font-size: .85rem; color: var(--text-3); }
.book-video-why { margin: 0; font-size: .93rem; line-height: 1.6; color: var(--text-2); }
.book-video-links {
  margin: auto 0 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  font-size: .88rem;
}
.book-video-links a { color: var(--accent); }
.book-video-offline { display: none; color: var(--text-3); }
.book-video.is-offline .book-video-offline { display: inline; }

/* --- runnable lab sets ---------------------------------------------------- */

.book-files {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  scroll-margin-top: calc(var(--topbar-h) + var(--crumbs-h) + 20px);
}
.book-files-body {
  display: grid;
  grid-template-columns: minmax(140px, 210px) 1fr;
  gap: 12px;
}
.book-files-tabs {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 420px;
  padding-right: 4px;
  overflow-y: auto;
}
.book-files-tab {
  padding: 6px 10px;
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--text-2);
  font-family: var(--font-mono);
  font-size: .78rem;
  text-align: left;
  word-break: break-all;
  cursor: pointer;
}
.book-files-tab:hover { background: var(--surface-2); color: var(--text); }
.book-files-tab.is-active { background: var(--accent-soft); color: var(--accent); }
.book-files-pane { display: none; }
.book-files-pane.is-active { display: block; }
.book-files-pane pre { max-height: 420px; }

/* --- sources -------------------------------------------------------------- */

.book-sources {
  max-width: var(--measure);
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: src;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.book-source {
  position: relative;
  counter-increment: src;
  display: grid;
  gap: 2px;
  padding-left: 30px;
}
.book-source::before {
  content: counter(src);
  position: absolute;
  left: 0;
  top: 1px;
  display: grid;
  place-items: center;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: var(--surface-3);
  color: var(--text-3);
  font-family: var(--font-mono);
  font-size: .68rem;
}
.book-source-claim { margin: 0; font-size: .93rem; line-height: 1.55; }
.book-source-where {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: baseline;
  font-size: .85rem;
  color: var(--text-3);
}
.book-source-where a { color: var(--accent); }
.book-source-type {
  padding: 1px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: .68rem;
  letter-spacing: .05em;
  text-transform: uppercase;
}

/* --- chapter footer ------------------------------------------------------- */

.book-foot {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.book-move {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 13px 15px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}
.book-move:hover { border-color: var(--accent); }
.book-move.is-next { text-align: right; }
.book-move-dir {
  font-size: .7rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-3);
}
.book-move-title { font-family: var(--font-display); font-size: .95rem; }

.book-empty { color: var(--text-3); }
.book-inline-img { max-width: 100%; height: auto; }

/* --- responsive ----------------------------------------------------------- */

@media (max-width: 860px) {
  .book { --book-gap: 22px; }
  .book-video { grid-template-columns: 1fr; }
  .book-files-body { grid-template-columns: 1fr; }
  .book-files-tabs { flex-direction: row; max-height: none; overflow-x: auto; }
  .book-files-tab { white-space: nowrap; word-break: normal; }
}

@media (max-width: 620px) {
  .book-body p, .book-list { font-size: .98rem; }
  .book-figure, .book-lab, .book-files, .book-video { padding: 12px; border-radius: var(--r); }
  .book-foot { grid-template-columns: 1fr; }
  .book-move.is-next { text-align: left; }

  /* Below this width a wide table cannot be read as a grid, so each row becomes
     a small stack of label/value pairs — the treatment .compare-block already
     uses, driven by the same data-label attribute. */
  .book-table thead { display: none; }
  .book-table,
  .book-table tbody,
  .book-table tr,
  .book-table td { display: block; width: 100%; }
  .book-table tr { padding: 10px 12px; border-bottom: 1px solid var(--border); }
  .book-table td { padding: 3px 0; border: 0; }
  .book-table td::before {
    content: attr(data-label);
    display: block;
    font-size: .68rem;
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--text-3);
  }
  .book-table td:empty { display: none; }
}

/* --- motion --------------------------------------------------------------- */

@media (prefers-reduced-motion: no-preference) {
  .book-progress-fill { transition: transform .12s linear; }
  .book-copy,
  .book-figure-btn,
  .book-move,
  .book-toc a,
  .book-files-tab { transition: color .16s ease, background-color .16s ease, border-color .16s ease; }
  .book-figure.is-ready .book-diagram-stage { animation: bookFadeIn .3s ease both; }

  @keyframes bookFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }
}

/* ==========================================================================
   Part-grouped sidebar
   ==========================================================================
   Only rendered for a course that declares `parts`. A flat list of 117 chapters
   is a scroll, not a navigation; a course without parts keeps the flat list.
   ========================================================================== */

.nav-part { border-bottom: 1px solid var(--border); }
.nav-part:last-of-type { border-bottom: 0; }
.nav-part > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
  list-style: none;
  font-family: var(--font-display);
  font-size: .78rem;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-3);
}
.nav-part > summary::-webkit-details-marker { display: none; }
.nav-part > summary::before {
  content: "";
  flex: none;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
}
.nav-part[open] > summary::before { transform: rotate(45deg); }
.nav-part > summary:hover { color: var(--text-2); background: var(--surface-2); }
.nav-part-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-part-count {
  font-family: var(--font-mono);
  font-size: .72rem;
  letter-spacing: 0;
  color: var(--text-3);
}
.nav-part-body { padding-bottom: 4px; }

/* ==========================================================================
   Course overview additions for a book course
   ==========================================================================
   Part grouping on the chapter map, and the reading routes the book itself
   recommends. Both only appear for a course that declares them.
   ========================================================================== */

.map-part { display: flex; flex-direction: column; gap: 14px; }
.map-part + .map-part { margin-top: 30px; padding-top: 24px; border-top: 1px solid var(--border); }
.map-part-head { display: flex; flex-direction: column; gap: 2px; }
.map-part-index {
  font-family: var(--font-mono);
  font-size: .72rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--accent);
}
.map-part-head h4 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.12rem;
  letter-spacing: -.02em;
}
.map-part-meta { margin: 0; font-size: .84rem; color: var(--text-3); }

.course-paths { display: flex; flex-direction: column; gap: 16px; }
.paths-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}
.path-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.path-card h4 { margin: 0; font-family: var(--font-display); font-size: 1rem; }
.path-time {
  margin: 0;
  font-family: var(--font-mono);
  font-size: .76rem;
  color: var(--accent);
}
.path-route { margin: 0; font-size: .88rem; line-height: 1.55; color: var(--text-2); }
.path-go {
  align-self: flex-start;
  margin-top: auto;
  padding: 5px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  font: inherit;
  font-size: .8rem;
  cursor: pointer;
}
.path-go:hover { color: var(--accent); border-color: var(--accent); }

@media (prefers-reduced-motion: no-preference) {
  .path-go { transition: color .16s ease, border-color .16s ease; }
}

/* A talk that cannot play says so before it is clicked, not after. */
.book-video.is-offline .book-video-play { cursor: not-allowed; }
.book-video.is-offline .book-video-poster { filter: grayscale(1); opacity: .5; }
.book-video.is-offline .book-video-badge { opacity: .35; }
