/* Scaling Book Lab — compact layouts for the interactive components.
 *
 * Loaded after app.css and deliberately overrides it. Two goals:
 *   1. Take less vertical space. A peek that pushes three paragraphs off the
 *      screen stops being "inline".
 *   2. Split code and output side by side WHEN THERE IS ROOM.
 *
 * Room is decided by CONTAINER queries, not viewport ones. With the outline and
 * notes rails docked, a figure inside the reading column can be far narrower
 * than the window — a viewport media query would split it into two unusable
 * 200px columns. The component measures itself instead. */

.figure, .peek { container-type: inline-size }

/* ── Editorial layout: narrow prose, wide media ──────────────────────────── */

/*
 * Prose wants ~34rem for comfortable reading; a lab wants every pixel it can
 * get. Previously both were capped at the reading measure, which squeezed the
 * figures AND meant a lab could never reach the 640px it needs to put the
 * editor beside its output — the split simply never fired.
 *
 * So the section is a three-track grid: text sits in the centre track at the
 * reading measure, and anything interactive spans all three.
 */
.column { max-width: 58rem }

.chapter__head, .chapnav {
  max-width: var(--measure); margin-inline: auto; width: 100%;
}

.section {
  display: grid;
  grid-template-columns:
    minmax(0, 1fr) minmax(0, var(--measure)) minmax(0, 1fr);
  column-gap: 0;
}
.section > * { grid-column: 2 }

/* The interactive surfaces break out of the text column. */
.section > .figure,
.section > .ex,
.section > .peek { grid-column: 1 / -1 }

/* Below the point where there is spare width, everything shares one track. */
@media (max-width: 900px) {
  .section { display: block }
  .column { max-width: var(--measure) }
}

/* ── Code blocks that cannot run ──────────────────────────────────────────── */

.codebadge {
  display: flex; flex-wrap: wrap; gap: var(--s2) var(--s3); align-items: baseline;
  font-family: var(--font-ui); font-size: var(--t-micro);
  color: var(--ink-2); background: var(--bound-mix-bg);
  border: 1px solid color-mix(in srgb, var(--bound-mix) 35%, transparent);
  border-bottom: none;
  border-radius: var(--r-md) var(--r-md) 0 0;
  padding: var(--s2) var(--s3); margin: var(--s4) 0 0;
}
.codebadge a { color: var(--live); text-decoration: none; font-weight: 500; margin-left: auto }
.codebadge a:hover { text-decoration: underline }
/* The block below a badge joins onto it. */
.codebadge + pre { margin-top: 0; border-radius: 0 0 var(--r-md) var(--r-md) }

/* ── Peeks: one header row, then a split body ─────────────────────────────── */

.peek { margin: var(--s4) 0; padding-left: var(--s3) }
.peek__head {
  font-size: var(--t-micro); margin-bottom: var(--s2);
}
.peek__source {
  font-size: 12px; line-height: 1.5; padding: var(--s2) var(--s3);
  margin: 0; white-space: pre-wrap;
}
.peek__tabs { gap: 2px; margin: 0 0 var(--s2) }
.peek__tab { min-height: 26px; padding: 0 var(--s2); font-size: 10.5px }
/* The hint is guidance, not content — keep it to one quiet line. */
.peek__hint {
  font-size: 10.5px; margin: 0 0 var(--s2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.peek__out {
  font-size: 12px; padding: var(--s2) var(--s3);
  max-height: 200px; margin: 0;
}

@container (min-width: 540px) {
  .peek__body {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
    grid-template-areas:
      "tabs   tabs"
      "source out"
      "hint   out";
    gap: var(--s2) var(--s3);
    align-items: start;
  }
  .peek__tabs  { grid-area: tabs; margin: 0 }
  .peek__source { grid-area: source; align-self: start }
  .peek__hint  { grid-area: hint; margin: 0; white-space: normal }
  .peek__out   { grid-area: out; max-height: 240px; height: 100% }
}

/* ── Diff peeks ───────────────────────────────────────────────────────────── */

.diff { gap: var(--s2); margin-bottom: var(--s2) }
@container (min-width: 480px) { .diff { grid-template-columns: 1fr 1fr } }

/* ── Labs: stepper, then editor beside output ─────────────────────────────── */

.lab__stepper { gap: var(--s1); margin-bottom: var(--s3) }
.lab__step { min-height: 30px; padding: 0 var(--s2); font-size: 10.5px }
.lab__instruction { font-size: 12.5px; margin-bottom: var(--s2) }
.lab__editor { font-size: 12.5px; padding: var(--s2) var(--s3) }
.lab__output { font-size: 12.5px; padding: var(--s3); max-height: 260px }
.lab__bar { margin: var(--s2) 0 }

@container (min-width: 640px) {
  /* The lab's children are appended flat into .figure__body, so the split is
     done with named areas rather than by wrapping them in extra elements. */
  .figure__body:has(.lab__editor) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "stepper stepper"
      "instr   instr"
      "editor  output"
      "bar     output"
      "notice  notice";
    column-gap: var(--s3);
    align-items: start;
  }
  .lab__stepper     { grid-area: stepper }
  .lab__instruction { grid-area: instr }
  .lab__editor      { grid-area: editor }
  .lab__bar         { grid-area: bar; margin: var(--s2) 0 0 }
  .lab__output      { grid-area: output; max-height: none; min-height: 100%;
                      align-self: stretch }
  .figure__body:has(.lab__editor) > .callout { grid-area: notice }
}

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

.figure__body { padding: var(--s3) var(--s4) }
.figure__head { padding: var(--s2) var(--s4) }
.figure__caption { padding: 0 var(--s4) var(--s3); font-size: 12px }
.readout { gap: 1px; margin-top: var(--s3) }
.readout__item { padding: var(--s1) var(--s3); font-size: 12px }
.ctrl { margin-bottom: var(--s2) }

/* -- Attention + Transformer diagrams ------------------------------------ */
.attention__controls, .flow__controls {
  display: flex; flex-wrap: wrap; align-items: end; gap: var(--s2) var(--s3);
  margin-bottom: var(--s3);
}
.attention__controls .ctrl { min-width: 150px; flex: 1 1 170px; }
.attention__controls select { min-height: 36px; width: 100%; font: inherit; font-size: var(--t-small); }
.attention__picture, .flow__picture {
  overflow-x: auto; overscroll-behavior: contain; border: var(--hair);
  background: var(--paper); padding: var(--s2); margin-top: var(--s3);
}
.attention__picture svg, .flow__picture svg { display: block; width: 100%; min-width: 680px; height: auto; }
.attention__label { fill: var(--ink-3); font-family: var(--font-mono); font-size: 12px; letter-spacing: .04em; }
.attention__celltext { fill: var(--paper-raise); font-family: var(--font-mono); font-size: 11px; text-anchor: middle; }
.attention__arc { transition: opacity var(--fast) var(--ease), stroke-width var(--fast) var(--ease); }
.attention__token { fill: var(--paper-raise); stroke: var(--rule-strong); stroke-width: 1; }
.attention__token--active { fill: var(--live-bg); stroke: var(--live); stroke-width: 2; }
.attention__tokentext { fill: var(--ink-2); font-family: var(--font-ui); font-size: 14px; text-anchor: middle; }
.attention__tokentext--active { fill: var(--live); font-weight: 600; }
.attention__q { fill: var(--live); font-family: var(--font-mono); font-size: 13px; text-anchor: middle; font-weight: 600; }
.flow__arrow { fill: none; stroke: var(--rule-strong); stroke-width: 1.5; }
.flow__node { fill: var(--paper-raise); stroke: var(--rule-strong); stroke-width: 1; }
.flow__node--active { fill: var(--live-bg); stroke: var(--live); stroke-width: 2; }
.flow__title, .flow__shape { fill: var(--ink-2); font-family: var(--font-ui); text-anchor: middle; }
.flow__title { font-size: 12px; font-weight: 600; }
.flow__title--active { fill: var(--live); }
.flow__shape { font-family: var(--font-mono); font-size: 10px; }
.flow__dot { fill: var(--live); animation: flow-pulse 900ms var(--ease) infinite alternate; }
.flow__explanation { font-family: var(--font-ui); font-size: var(--t-small); color: var(--ink-2); line-height: 1.55; margin-top: var(--s3); }
.flow__explanation strong { color: var(--live); }
@keyframes flow-pulse { from { opacity: .4; transform: scale(.75); transform-origin: center; } to { opacity: 1; transform: scale(1.2); transform-origin: center; } }

@media (prefers-reduced-motion: reduce) {
  .flow__dot { animation: none; }
}

/* ── Chapter compass: the learning contract for every chapter ───────────── */

.chapter-guide {
  max-width: var(--measure);
  width: 100%;
  margin: 0 auto var(--s7);
  border-block: var(--hair);
  background: linear-gradient(90deg, var(--live-bg), transparent 38%);
  padding: var(--s4) 0;
}
.chapter-guide__head {
  display: grid;
  gap: var(--s1);
  margin-bottom: var(--s4);
}
.chapter-guide__eyebrow,
.chapter-guide__cell > span {
  font-family: var(--font-mono);
  color: var(--live);
  font-size: var(--t-micro);
  letter-spacing: .08em;
  text-transform: uppercase;
}
.chapter-guide__head strong {
  font-family: var(--font-reading);
  font-size: clamp(1.05rem, 2.2vw, 1.3rem);
  line-height: 1.35;
  font-weight: 600;
}
.chapter-guide__grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: var(--hair);
  background: var(--paper-raise);
}
.chapter-guide__cell {
  padding: var(--s3);
  min-width: 0;
}
.chapter-guide__cell + .chapter-guide__cell { border-left: var(--hair); }
.chapter-guide__cell p {
  margin: var(--s1) 0 0;
  font-family: var(--font-ui);
  font-size: var(--t-small);
  line-height: 1.55;
}
.chapter-guide__stats {
  font-family: var(--font-mono);
  color: var(--ink-3);
  font-size: var(--t-micro);
  padding: var(--s2) 0;
}
.chapter-guide__path {
  display: flex;
  gap: var(--s1);
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  padding-bottom: var(--s2);
}
.chapter-guide__path a {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  min-height: 44px;
  max-width: 16rem;
  border: var(--hair);
  padding: var(--s2) var(--s3);
  color: var(--ink-2);
  background: var(--paper-raise);
  font: 500 var(--t-micro)/1.25 var(--font-ui);
  text-decoration: none;
}
.chapter-guide__path a:hover,
.chapter-guide__path a:focus-visible {
  border-color: var(--live);
  color: var(--live);
  background: var(--live-bg);
}
.chapter-guide__source {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  margin-top: var(--s1);
  color: var(--live);
  font: 500 var(--t-small)/1.4 var(--font-ui);
}

/* ── MoE router: actual returned arrays become the diagram ──────────────── */

.moe__controls {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: var(--s3);
}
.moe__controls .ctrl { flex: 1 1 16rem; margin: 0; }
.moe__code {
  margin: var(--s3) 0 0;
  max-height: 15rem;
  font-size: 12px;
}
.moe__status {
  color: var(--ink-3);
  font: var(--t-small)/1.5 var(--font-ui);
  margin: var(--s3) 0;
}
.moe__visual { display: grid; gap: var(--s4); }
.moe__matrix {
  display: grid;
  grid-template-columns: minmax(3rem, .65fr) repeat(3, minmax(4.25rem, 1fr));
  gap: 1px;
  background: var(--rule);
  border: var(--hair);
}
.moe__matrix > * {
  display: grid;
  place-items: center;
  min-height: 2.65rem;
  padding: var(--s1);
  background: var(--paper-raise);
  font: var(--t-micro)/1 var(--font-mono);
}
.moe__matrix > strong { color: var(--ink-2); }
.moe__prob {
  background: color-mix(in srgb, var(--live) calc(var(--p) * 72%), var(--paper-raise));
  color: var(--ink);
  transition: background var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
}
.moe__prob.is-routed {
  box-shadow: inset 0 0 0 2px var(--live);
  font-weight: 700;
}
.moe__experts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--s2);
}
.moe__expert {
  border: var(--hair);
  padding: var(--s3);
  min-width: 0;
}
.moe__expert > strong {
  font: 600 var(--t-small)/1.3 var(--font-ui);
}
.moe__tokens {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s1);
  min-height: 2rem;
  margin-top: var(--s2);
}
.moe__tokens span {
  display: inline-grid;
  place-items: center;
  min-width: 2rem;
  min-height: 2rem;
  background: var(--live-bg);
  border: 1px solid var(--live);
  color: var(--live);
  font: var(--t-micro)/1 var(--font-mono);
}

/* ── Live JAXPR → HLO-shaped bridge ─────────────────────────────────── */

.hlo-bridge__banner {
  display: grid;
  gap: var(--s1);
  border-left: 3px solid var(--bound-mix);
  background: var(--bound-mix-bg);
  padding: var(--s3);
  font: var(--t-small)/1.45 var(--font-ui);
}
.hlo-bridge__banner strong {
  color: var(--bound-mix);
  font: 600 var(--t-micro)/1.3 var(--font-mono);
  letter-spacing: .06em;
}
.hlo-bridge__controls {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
  margin: var(--s3) 0;
}
.hlo-bridge__controls select {
  flex: 1 1 15rem;
  min-height: 44px;
  border: var(--hair);
  background: var(--paper-raise);
  color: var(--ink);
  padding: 0 var(--s3);
  font: var(--t-small)/1 var(--font-ui);
}
.hlo-bridge__source {
  margin: 0 0 var(--s3);
  font-size: 12px;
}
.hlo-bridge__columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s2);
}
.hlo-bridge__columns section {
  min-width: 0;
  border: var(--hair);
  background: var(--paper-raise);
}
.hlo-bridge__columns h4 {
  margin: 0;
  border-bottom: var(--hair);
  padding: var(--s2) var(--s3);
  font: 600 var(--t-micro)/1.3 var(--font-ui);
  color: var(--ink-2);
}
.hlo-bridge__list {
  max-height: 20rem;
  overflow: auto;
  overscroll-behavior: contain;
}
.hlo-bridge__list button {
  display: block;
  width: 100%;
  min-height: 44px;
  border: 0;
  border-bottom: var(--hair);
  background: transparent;
  color: var(--ink-2);
  padding: var(--s2) var(--s3);
  text-align: left;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  font: 11px/1.45 var(--font-mono);
  cursor: pointer;
}
.hlo-bridge__list button:hover,
.hlo-bridge__list button.is-selected {
  background: var(--live-bg);
  color: var(--live);
  box-shadow: inset 3px 0 0 var(--live);
}
.hlo-bridge__detail {
  margin-top: var(--s3);
  border: var(--hair);
  padding: var(--s3);
}
.hlo-bridge__detail strong {
  font: 600 var(--t-small)/1.3 var(--font-ui);
}
.hlo-bridge__detail pre {
  margin: var(--s2) 0 0;
  padding: 0;
  border: 0;
  background: transparent;
  white-space: pre-wrap;
  font-size: 12px;
}

@media (max-width: 700px) {
  .hlo-bridge__columns { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .chapter-guide__grid { grid-template-columns: 1fr; }
  .chapter-guide__cell + .chapter-guide__cell {
    border-left: 0;
    border-top: var(--hair);
  }
  .moe__experts { grid-template-columns: 1fr; }
  .moe__matrix {
    grid-template-columns: minmax(2.5rem, .55fr) repeat(3, minmax(3.5rem, 1fr));
  }
}
