/* Scaling Book Lab — layout and components.
 * Every value here comes from tokens.css. See README "Design language". */

*, *::before, *::after { box-sizing: border-box }

html { -webkit-text-size-adjust: 100% }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-read);
  font-size: var(--t-body);
  line-height: var(--lh-body);
  font-synthesis-weight: none;
}

a { color: var(--live); text-underline-offset: 2px }
a:hover { text-decoration-thickness: 2px }

/* ── Chrome ─────────────────────────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 46;   /* above the rails, including drawers */
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s4);
  padding-top: calc(var(--s3) + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: var(--hair);
  font-family: var(--font-ui);
}

.topbar__brand {
  font-weight: 600; font-size: var(--t-small); letter-spacing: .02em;
  color: var(--ink); text-decoration: none; white-space: nowrap;
}
.topbar__brand span { color: var(--ink-3); font-weight: 400 }
.topbar__spacer { flex: 1 }

/* Thin reading-progress rule across the very top of the page. */
.readbar {
  position: fixed; top: 0; left: 0; height: 2px; z-index: 50;
  background: var(--bound-cmp);
  transition: width var(--fast) linear;
}

/* ── Buttons ────────────────────────────────────────────────────────────── */

.btn {
  font-family: var(--font-ui); font-size: var(--t-small); font-weight: 500;
  min-height: 44px; padding: 0 var(--s4);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  border: 1px solid var(--rule-strong); border-radius: var(--r-sm);
  background: var(--paper-raise); color: var(--ink);
  cursor: pointer; transition: background var(--fast) var(--ease), border-color var(--fast) var(--ease);
}
.btn:hover, .btn:focus-visible { background: var(--paper-sunk); border-color: var(--ink-3) }
.btn--primary { background: var(--live); border-color: var(--live); color: #fff }
.btn--primary:hover, .btn--primary:focus-visible {
  background: color-mix(in srgb, var(--live) 88%, black); border-color: currentColor;
}
.btn--quiet { border-color: transparent; background: transparent; color: var(--ink-2) }
.btn--quiet:hover { background: var(--paper-sunk); color: var(--ink) }
.btn--sm { min-height: 36px; padding: 0 var(--s3) }

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

/* ── Page scaffold ──────────────────────────────────────────────────────── */

.page { max-width: 78rem; margin: 0 auto; padding: var(--s5) var(--s4) var(--s9) }
.column { max-width: var(--measure); margin: 0 auto }

.eyebrow {
  font-family: var(--font-mono); font-size: var(--t-micro);
  text-transform: uppercase; letter-spacing: .1em; color: var(--ink-3);
}

/* ── Contents (home) ────────────────────────────────────────────────────── */

.hero { padding: var(--s6) 0 var(--s7); border-bottom: var(--hair) }
.hero h1 {
  font-family: var(--font-ui); font-weight: 600; letter-spacing: -.02em;
  font-size: var(--t-h1); line-height: var(--lh-tight); margin: var(--s3) 0 var(--s4);
}
.hero p { margin: 0 0 var(--s4); color: var(--ink-2); font-size: var(--t-lead) }
.hero__actions { display: flex; flex-wrap: wrap; gap: var(--s3) }

.part { margin-top: var(--s7) }
.part__title {
  font-family: var(--font-ui); font-size: var(--t-small); font-weight: 600;
  letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3);
  padding-bottom: var(--s2); border-bottom: var(--hair); margin: 0 0 var(--s4);
}

.chapters { display: grid; gap: var(--s3); grid-template-columns: 1fr }
@media (min-width: 700px) { .chapters { grid-template-columns: 1fr 1fr } }

.chapcard {
  display: grid; grid-template-columns: auto 1fr; gap: var(--s4);
  align-items: start; text-decoration: none; color: inherit;
  padding: var(--s4); border: var(--hair); border-radius: var(--r-md);
  background: var(--paper-raise);
  transition: border-color var(--fast) var(--ease), transform var(--fast) var(--ease);
}
.chapcard:hover { border-color: var(--ink-3); transform: translateY(-1px) }
.chapcard__num {
  font-family: var(--font-mono); font-size: var(--t-display); font-weight: 500;
  line-height: 1; color: var(--rule-strong); font-variant-numeric: tabular-nums;
}
.chapcard__title {
  font-family: var(--font-ui); font-weight: 600; font-size: var(--t-h3);
  line-height: var(--lh-tight); margin: 0 0 var(--s2); letter-spacing: -.01em;
}
.chapcard__sub { font-size: var(--t-small); color: var(--ink-2); margin: 0; line-height: 1.5 }

.meter { margin-top: var(--s3); display: flex; align-items: center; gap: var(--s2) }
.meter__track { flex: 1; height: 3px; background: var(--rule); border-radius: 2px; overflow: hidden }
.meter__fill { height: 100%; background: var(--bound-cmp); transition: width var(--slow) var(--ease) }
.meter__label {
  font-family: var(--font-mono); font-size: var(--t-micro); color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

/* ── Chapter ────────────────────────────────────────────────────────────── */

.chapter__head { padding: var(--s5) 0 var(--s6); border-bottom: var(--hair); margin-bottom: var(--s6) }
.chapter__head h1 {
  font-family: var(--font-ui); font-weight: 600; font-size: var(--t-h1);
  line-height: var(--lh-tight); letter-spacing: -.02em; margin: var(--s3) 0 var(--s4);
}
.standfirst { font-size: var(--t-lead); color: var(--ink-2); margin: 0 }

/* Clear the sticky header when jumping to an anchor. Uses the measured height
   so it can't drift out of step with the header itself. */
.section { margin: 0 0 var(--s8); scroll-margin-top: calc(var(--topbar-h) + var(--s3)) }
.section > h2 {
  font-family: var(--font-ui); font-weight: 600; font-size: var(--t-h2);
  line-height: var(--lh-tight); letter-spacing: -.01em;
  margin: var(--s7) 0 var(--s4); padding-top: var(--s2);
}
.section p { margin: 0 0 var(--s4) }
.section h3 {
  font-family: var(--font-ui); font-weight: 600; font-size: var(--t-h3);
  margin: var(--s6) 0 var(--s3);
}
.section ul, .section ol { margin: 0 0 var(--s4); padding-left: var(--s5) }
.section li { margin-bottom: var(--s2) }
.section strong { font-weight: 600 }

/* Numbers, shapes and identifiers are always mono — this book is full of
   them and mixing them into serif prose makes them hard to scan. */
code, .num {
  font-family: var(--font-mono); font-size: .88em;
  background: var(--paper-sunk); padding: .1em .3em; border-radius: var(--r-sm);
  font-variant-numeric: tabular-nums;
}

pre {
  font-family: var(--font-mono); font-size: var(--t-small); line-height: 1.6;
  background: var(--paper-sunk); border: var(--hair); border-radius: var(--r-md);
  padding: var(--s4); overflow-x: auto; margin: 0 0 var(--s4);
}
pre code { background: none; padding: 0; font-size: inherit }

/* ── Math ───────────────────────────────────────────────────────────────── */

.mathblock { margin: var(--s5) 0; overflow-x: auto; overflow-y: hidden; padding: var(--s2) 0 }
.mathblock .katex-display { margin: 0 }
.math-raw { font-family: var(--font-mono); font-size: var(--t-small); color: var(--ink-2) }
.mathblock__caption {
  font-family: var(--font-ui); font-size: var(--t-small); color: var(--ink-2);
  margin-top: var(--s2); line-height: 1.5;
}

/* ── Callouts ───────────────────────────────────────────────────────────── */

.callout {
  margin: var(--s5) 0; padding: var(--s4);
  border-left: 3px solid var(--rule-strong); border-radius: 0 var(--r-sm) var(--r-sm) 0;
  background: var(--paper-sunk);
}
.callout p:last-child { margin-bottom: 0 }
.callout__title {
  font-family: var(--font-ui); font-size: var(--t-micro); font-weight: 600;
  text-transform: uppercase; letter-spacing: .08em; margin: 0 0 var(--s2);
}
.callout--takeaway { border-left-color: var(--bound-cmp); background: var(--bound-cmp-bg) }
.callout--takeaway .callout__title { color: var(--bound-cmp) }
.callout--why { border-left-color: var(--live); background: var(--live-bg) }
.callout--why .callout__title { color: var(--live) }
.callout--note .callout__title { color: var(--ink-3) }

/* ── Figures ────────────────────────────────────────────────────────────── */

.figure {
  margin: var(--s6) 0; border: var(--hair); border-radius: var(--r-md);
  background: var(--paper-raise); overflow: hidden;
}
.figure__head {
  display: flex; align-items: baseline; gap: var(--s3);
  padding: var(--s3) var(--s4); border-bottom: var(--hair); background: var(--paper-sunk);
}
.figure__title {
  font-family: var(--font-ui); font-size: var(--t-small); font-weight: 600; margin: 0;
}
.figure__badge {
  font-family: var(--font-mono); font-size: var(--t-micro);
  padding: 2px var(--s2); border-radius: var(--r-sm);
  background: var(--bound-cmp-bg); color: var(--bound-cmp); white-space: nowrap;
}
.figure__body { padding: var(--s4) }
.figure__caption {
  font-family: var(--font-ui); font-size: var(--t-small); color: var(--ink-2);
  padding: 0 var(--s4) var(--s4); margin: 0; line-height: 1.55;
}

/* Controls inside figures — the only place --live is used for chrome. */
.ctrl { display: grid; gap: var(--s1); margin-bottom: var(--s3) }
.ctrl__row { display: flex; justify-content: space-between; align-items: baseline; gap: var(--s3) }
.ctrl__label { font-family: var(--font-ui); font-size: var(--t-small); color: var(--ink-2) }
.ctrl__value {
  font-family: var(--font-mono); font-size: var(--t-small); font-weight: 500;
  font-variant-numeric: tabular-nums; color: var(--ink);
}
input[type=range] { width: 100%; accent-color: var(--live); min-height: 44px }

.readout { display: grid; gap: var(--s2); margin-top: var(--s4) }
.readout__item {
  display: flex; justify-content: space-between; align-items: baseline; gap: var(--s3);
  padding: var(--s2) var(--s3); background: var(--paper-sunk); border-radius: var(--r-sm);
  font-family: var(--font-mono); font-size: var(--t-small); font-variant-numeric: tabular-nums;
}
.readout__key { color: var(--ink-2) }
.readout__val { font-weight: 500 }
.is-mem { color: var(--bound-mem) } .is-mix { color: var(--bound-mix) } .is-cmp { color: var(--bound-cmp) }

/* ── Exercises ──────────────────────────────────────────────────────────── */

.ex {
  margin: var(--s6) 0; border: 1px solid var(--rule-strong); border-radius: var(--r-md);
  background: var(--paper-raise); overflow: hidden;
}
.ex__head {
  padding: var(--s3) var(--s4); background: var(--paper-sunk); border-bottom: var(--hair);
  display: flex; align-items: center; gap: var(--s3);
}
.ex__label {
  font-family: var(--font-mono); font-size: var(--t-micro); text-transform: uppercase;
  letter-spacing: .08em; color: var(--ink-3);
}
.ex__status { margin-left: auto; font-family: var(--font-mono); font-size: var(--t-micro) }
.ex__body { padding: var(--s4) }
.ex__prompt { margin: 0 0 var(--s4) }
.ex--solved { border-color: var(--bound-cmp) }
.ex--solved .ex__head { background: var(--bound-cmp-bg) }

.expart { margin-bottom: var(--s4) }
.expart__label {
  display: block; font-family: var(--font-ui); font-size: var(--t-small);
  color: var(--ink-2); margin-bottom: var(--s2);
}
.expart__input {
  font-family: var(--font-mono); font-size: var(--t-body);
  min-height: 44px; width: 100%; max-width: 20rem; padding: var(--s2) var(--s3);
  border: 1px solid var(--rule-strong); border-radius: var(--r-sm);
  background: var(--paper); color: var(--ink);
}
.expart__input:focus { border-color: var(--live) }
.expart--right .expart__input { border-color: var(--bound-cmp); background: var(--bound-cmp-bg) }
.expart--wrong .expart__input { border-color: var(--bound-mem); background: var(--bound-mem-bg) }
.expart__unit { font-family: var(--font-mono); font-size: var(--t-small); color: var(--ink-3); margin-left: var(--s2) }
.expart__feedback { font-family: var(--font-ui); font-size: var(--t-small); margin-top: var(--s2) }

.choice { display: grid; gap: var(--s2) }
.choice label {
  display: flex; gap: var(--s3); align-items: flex-start; min-height: 44px;
  padding: var(--s2) var(--s3); border: var(--hair); border-radius: var(--r-sm);
  font-size: var(--t-small); font-family: var(--font-ui); cursor: pointer;
}
.choice label:hover { background: var(--paper-sunk) }

.ex__actions { display: flex; flex-wrap: wrap; gap: var(--s3); margin-top: var(--s4) }
.ex__solution {
  margin-top: var(--s4); padding-top: var(--s4); border-top: var(--hair);
  font-size: var(--t-small);
}

/* ── Tables ─────────────────────────────────────────────────────────────── */

/* This book is full of spec tables. Datasheet treatment: hairline rules, no
   zebra striping, and every number tabular so columns align on the decimal. */
.section table {
  width: 100%; border-collapse: collapse; margin: var(--s5) 0;
  font-family: var(--font-ui); font-size: var(--t-small);
  display: block; overflow-x: auto; white-space: nowrap;
}
.section th, .section td {
  text-align: left; padding: var(--s2) var(--s3);
  border-bottom: var(--hair); vertical-align: baseline;
}
.section th {
  font-weight: 600; color: var(--ink-2); font-size: var(--t-micro);
  text-transform: uppercase; letter-spacing: .06em;
  border-bottom: 1px solid var(--rule-strong); white-space: nowrap;
}
/* Numeric cells read as data, not prose. */
.section td code, .section td .num { background: none; padding: 0 }
.section td:not(:first-child) {
  font-family: var(--font-mono); font-variant-numeric: tabular-nums;
}
.section tbody tr:last-child td { border-bottom: none }

/* ── Glossary terms ─────────────────────────────────────────────────────── */

/* Dotted underline, not a link colour — these are definitions, not navigation.
   Padding gives touch users a real target without disturbing line rhythm. */
.term {
  border-bottom: 1.5px dotted var(--ink-3);
  cursor: help;
  padding: 2px 0;
  transition: border-color var(--fast) var(--ease), background var(--fast) var(--ease);
}
.term:hover, .term:focus-visible {
  border-bottom-color: var(--live);
  background: var(--live-bg);
}

.termcard {
  position: absolute; z-index: 60;
  background: var(--paper-raise); border: 1px solid var(--rule-strong);
  border-radius: var(--r-md); padding: var(--s3) var(--s4);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.10), 0 1px 3px rgb(0 0 0 / 0.06);
  font-family: var(--font-ui); font-size: var(--t-small); line-height: 1.55;
}
.termcard__label {
  font-family: var(--font-mono); font-size: var(--t-micro);
  text-transform: uppercase; letter-spacing: .08em;
  color: var(--ink-3); margin-bottom: var(--s2);
}
.termcard__body { color: var(--ink) }
.termcard__body em { color: var(--ink-2) }
.termcard__link {
  display: inline-block; margin-top: var(--s3);
  font-family: var(--font-mono); font-size: var(--t-micro);
  color: var(--live); text-decoration: none;
}
.termcard__link:hover { text-decoration: underline }

/* ── Cross-references ───────────────────────────────────────────────────── */

/* "This goes somewhere else" — an aside that invites a detour rather than
   interrupting. Deliberately quieter than a callout. */
.detour {
  display: flex; gap: var(--s3); align-items: flex-start;
  margin: var(--s5) 0; padding: var(--s3) var(--s4);
  border: 1px dashed var(--rule-strong); border-radius: var(--r-md);
  font-family: var(--font-ui); font-size: var(--t-small); color: var(--ink-2);
  background: var(--paper-sunk);
}
.detour::before {
  content: "↗"; color: var(--live); font-size: 15px; line-height: 1.4; flex: none;
}
.detour a { font-weight: 500 }

/* ── States ─────────────────────────────────────────────────────────────── */

.empty, .loading {
  text-align: center; padding: var(--s8) var(--s4); color: var(--ink-2);
  font-family: var(--font-ui);
}
.empty h2 { font-size: var(--t-h3); color: var(--ink); margin: 0 0 var(--s2) }
.empty p { margin: 0 auto var(--s4); max-width: 30rem; font-size: var(--t-small) }

.errorbox {
  border: 1px solid var(--bound-mem); background: var(--bound-mem-bg);
  border-radius: var(--r-md); padding: var(--s4); margin: var(--s5) 0;
  font-family: var(--font-ui); font-size: var(--t-small);
}

/* ── Chapter footer nav ─────────────────────────────────────────────────── */

.chapnav {
  display: flex; gap: var(--s3); justify-content: space-between;
  margin-top: var(--s8); padding-top: var(--s5); border-top: var(--hair);
}

/* Keep primary actions reachable while a keyboard is up. */
@supports (padding: env(safe-area-inset-bottom)) {
  .page { padding-bottom: calc(var(--s9) + env(safe-area-inset-bottom)) }
}

/* -- Runnable labs -------------------------------------------------------- */

/* The stepper is a journey, not a tab bar: ordered, and it reads left to
   right like the stages of a pipeline. */
.lab__stepper { display: flex; flex-wrap: wrap; gap: var(--s2); margin-bottom: var(--s4) }
.lab__step {
  font-family: var(--font-mono); font-size: var(--t-micro);
  min-height: 36px; padding: 0 var(--s3);
  border: var(--hair); border-radius: var(--r-sm);
  background: var(--paper); color: var(--ink-2); cursor: pointer;
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease),
              border-color var(--fast) var(--ease);
}
.lab__step:hover { border-color: var(--ink-3); color: var(--ink) }
.lab__step.is-active { background: var(--ink); border-color: var(--ink); color: var(--paper) }

.lab__instruction {
  font-family: var(--font-ui); font-size: var(--t-small);
  color: var(--ink-2); line-height: 1.6; margin-bottom: var(--s3);
}
.lab__instruction strong { color: var(--ink) }
.lab__instruction code { font-size: .92em }

.lab__editor {
  width: 100%; display: block;
  font-family: var(--font-mono); font-size: 13px; line-height: 1.6;
  padding: var(--s3); border: 1px solid var(--rule-strong);
  border-radius: var(--r-md); background: var(--paper-sunk); color: var(--ink);
  resize: vertical; tab-size: 2;
}
.lab__editor:focus { border-color: var(--live); outline: none }
.lab__editor[readonly] { opacity: .8 }

.lab__bar { display: flex; align-items: center; gap: var(--s3); margin: var(--s3) 0 }
.lab__status {
  font-family: var(--font-mono); font-size: var(--t-micro); color: var(--ink-3);
}

.lab__output {
  font-family: var(--font-mono); font-size: 13px; line-height: 1.65;
  background: var(--ink); color: #e8eae6;
  border: none; border-radius: var(--r-md);
  padding: var(--s4); margin: 0; max-height: 340px; overflow: auto;
  white-space: pre-wrap; word-break: break-word;
}

@media (max-width: 640px) {
  .lab__output { font-size: 12px }
}

/* -- Inline peeks --------------------------------------------------------- */

/* Deliberately quieter than a figure: this sits inside the argument rather
   than interrupting it. Left rule ties it to the prose it belongs to. */
.peek {
  margin: var(--s4) 0;
  border-left: 2px solid var(--rule-strong);
  padding-left: var(--s4);
}
.peek__head {
  font-family: var(--font-mono); font-size: var(--t-micro);
  text-transform: uppercase; letter-spacing: .08em;
  color: var(--ink-3); margin-bottom: var(--s2);
}
.peek__source {
  font-family: var(--font-mono); font-size: 12.5px; line-height: 1.55;
  background: var(--paper-sunk); border: var(--hair); border-radius: var(--r-sm);
  padding: var(--s2) var(--s3); margin: 0 0 var(--s2); overflow-x: auto;
}
.peek__tabs { display: flex; flex-wrap: wrap; gap: var(--s1); margin-bottom: var(--s2) }
.peek__tab {
  font-family: var(--font-mono); font-size: var(--t-micro);
  min-height: 32px; padding: 0 var(--s3);
  border: var(--hair); border-radius: var(--r-sm);
  background: var(--paper); color: var(--ink-2); cursor: pointer;
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease);
}
.peek__tab:hover { border-color: var(--ink-3); color: var(--ink) }
.peek__tab.is-active { background: var(--live); border-color: var(--live); color: #fff }
.peek__hint {
  font-family: var(--font-ui); font-size: var(--t-micro);
  color: var(--ink-3); margin-bottom: var(--s2);
}
.peek__out {
  font-family: var(--font-mono); font-size: 12.5px; line-height: 1.6;
  background: var(--ink); color: #e8eae6;
  border-radius: var(--r-sm); padding: var(--s3); margin: 0;
  max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-word;
}

/* Diff peek: two variants, side by side where there is room. */
.diff { display: grid; gap: var(--s3); margin-bottom: var(--s3) }
@media (min-width: 620px) { .diff { grid-template-columns: 1fr 1fr } }
.diff__name {
  font-family: var(--font-mono); font-size: var(--t-micro);
  color: var(--ink-2); margin-bottom: var(--s1);
}
.diff__cell .peek__source { margin-bottom: 0 }

/* -- Shape annotations ---------------------------------------------------- */

/* Shapes are data, so they read as mono; the dashed underline says "there is
   more here" without competing with glossary terms (dotted) or links. */
.shapetag {
  font-family: var(--font-mono); font-size: .9em;
  border-bottom: 1.5px dashed var(--live);
  cursor: pointer; padding: 1px 2px; border-radius: 2px;
  transition: background var(--fast) var(--ease);
}
.shapetag:hover, .shapetag:focus-visible { background: var(--live-bg) }

.shapecard {
  position: absolute; z-index: 60;
  background: var(--paper-raise); border: 1px solid var(--rule-strong);
  border-radius: var(--r-md); padding: var(--s3) var(--s4);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.10), 0 1px 3px rgb(0 0 0 / 0.06);
}
.shapecard__title {
  font-family: var(--font-mono); font-size: var(--t-small);
  color: var(--ink); margin-bottom: var(--s3);
}
.shapecard__grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  gap: var(--s2); margin-bottom: var(--s3);
}
.shapecard__sym {
  font-family: var(--font-mono); font-size: var(--t-micro); color: var(--ink-3);
  display: flex; flex-direction: column; gap: 2px;
}
.shapecard__sym input {
  font-family: var(--font-mono); font-size: var(--t-small);
  width: 100%; min-height: 32px; padding: 2px var(--s2);
  border: var(--hair); border-radius: var(--r-sm);
  background: var(--paper); color: var(--ink);
}
.shapecard__sym input:focus { border-color: var(--live); outline: none }
.shapecard__totals {
  font-family: var(--font-mono); font-size: var(--t-small);
  white-space: pre-line; color: var(--ink);
  background: var(--paper-sunk); border-radius: var(--r-sm); padding: var(--s2) var(--s3);
}
.shapecard__note {
  font-family: var(--font-ui); font-size: var(--t-micro);
  color: var(--ink-3); margin-top: var(--s2);
}
