/* Chapter 4: compaction. Builds on ../style.css (tokens, grid, prose, legend, stats, colophon,
   chapter navigation); only what this chapter adds lives here. Squares are one per 100 tokens, the
   same unit and the same kind colours as chapter 2; cache colours are chapter 3's. */
:root {
  --cell: 14px;
  --gap: 2px;
  /* What was left after compaction: one colour, since the recording doesn't say what's in it. */
  --k-after: var(--ink);
  /* A size for comparison only (the conversation before), and a total that isn't split. */
  --k-pre: var(--ink-3);
  --k-gone: color-mix(in srgb, var(--ink-3) 45%, var(--rule));
  --hl: color-mix(in srgb, var(--you) 24%, var(--card));
  --hl-on: color-mix(in srgb, var(--you) 42%, var(--card));
}
@media (prefers-color-scheme: dark) {
  :root {
    --hl: color-mix(in srgb, var(--you) 22%, var(--card));
    --hl-on: color-mix(in srgb, var(--you) 36%, var(--card));
  }
}

/* ---------- what we did ---------- */
.notice {
  padding: 0.95rem 1.1rem;
  background: var(--paper-2);
  border-left: 3px solid var(--you);
  border-radius: 0 10px 10px 0;
}

/* ---------- a figure panel ---------- */
.fig {
  margin: 1.75rem 0 2rem;
  padding: clamp(1rem, 2.5vw, 1.6rem);
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 14px;
}
.fig-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem 1.5rem; margin-bottom: 1rem; }
.fig-title { font: 600 1.15rem/1.3 var(--display); color: var(--ink); }
.fig-legend { margin-top: 1rem; }
.fig-note { margin: 0.9rem 0 0 !important; font-size: 0.9rem; line-height: 1.55; color: var(--ink-2); }
.legend .sw { display: inline-block; width: 0.75rem; height: 0.75rem; border-radius: 3px; margin-right: 0.45rem; vertical-align: -0.1rem; }

/* the kinds of square */
.k-sys { background: var(--sys); }
.k-model { background: var(--model); }
.k-back { background: var(--tex-teal) var(--result); }
.k-after { background: var(--k-after); }
.k-pre { background: var(--k-pre); }
.k-cached { background: var(--tex-cached) var(--c-cached); }
.k-fresh { background: var(--c-fresh); }
.k-out { background: var(--c-out); }
.k-gone { background: transparent; box-shadow: inset 0 0 0 1px var(--k-gone); }
.legend .sw.k-gone { box-shadow: inset 0 0 0 1px var(--ink-3); }

/* the before / after switch (as chapter 3's cost toggle) */
.seg-opt input { position: absolute; opacity: 0; width: 1px; height: 1px; }
fieldset.mode { display: inline-flex; margin: 0; padding: 3px; border: 1px solid var(--rule); border-radius: 10px; background: var(--paper); }
.seg-opt { position: relative; display: block; }
.seg-opt span {
  display: inline-flex; align-items: center; min-height: 2.75rem; padding: 0 1rem;
  font: 600 0.82rem/1.2 var(--mono); color: var(--ink-2); border-radius: 7px; cursor: pointer;
  transition: transform var(--dur-press) var(--ease-out), background-color var(--dur-fast) ease, color var(--dur-fast) ease;
}
.seg-opt span:active { transform: scale(0.97); }
.seg-opt input:checked + span { background: var(--ink); color: var(--card); }
.seg-opt input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (hover: hover) and (pointer: fine) {
  .seg-opt input:not(:checked) + span:hover { color: var(--ink); background: var(--paper-2); }
}

.big { margin: 0 0 1rem !important; }
.big-n { display: block; font: 700 clamp(2.4rem, 6vw, 3.4rem)/1 var(--display); letter-spacing: -0.02em; font-variation-settings: "opsz" 144; color: var(--ink); font-variant-numeric: lining-nums tabular-nums; }
.big-l { display: block; margin-top: 0.35rem; font: 500 0.76rem/1.35 var(--mono); color: var(--ink-2); }

/* ---------- squares ---------- */
.cells { line-height: 0; font-size: 0; }
.cells .c {
  display: inline-block; vertical-align: top;
  width: var(--cell); height: var(--cell);
  margin: 0 var(--gap) var(--gap) 0;
  border-radius: 2px;
}
/* Three layers in the same place: outlines for everything, the conversation, and what was left. */
.cells-wrap { position: relative; }
.cells-wrap .live, .cells-wrap .after { position: absolute; inset: 0; }
.cells-wrap[data-state="after"] .live .c { opacity: 0; }
.cells-wrap[data-state="before"] .after .c { opacity: 0; }

/* ---------- "What survived?" ---------- */
.survive { margin-top: 1.5rem; }
.sv-bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem 1.5rem;
  padding: 0.75rem 0 1rem; border-bottom: 1px solid var(--rule);
}
.score { margin: 0 !important; font: 600 0.85rem/1.3 var(--mono); color: var(--ink); font-variant-numeric: tabular-nums; }
.btn {
  display: inline-flex; align-items: center; min-height: 2.75rem; padding: 0.4rem 1rem;
  font: 600 0.82rem/1.25 var(--mono); color: var(--ink);
  background: var(--paper); border: 1px solid var(--rule); border-radius: 8px; cursor: pointer;
  transition: transform var(--dur-press) var(--ease-out), border-color var(--dur-fast) ease, background-color var(--dur-fast) ease;
}
.btn:active { transform: scale(0.97); }
@media (hover: hover) and (pointer: fine) {
  .btn:hover { border-color: var(--ink-3); background: var(--card); }
}

.events { list-style: none; margin: 0; padding: 0; counter-reset: ev; }
.ev {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(16rem, 21rem); gap: 1.25rem clamp(1.25rem, 3vw, 2.25rem);
  padding: 1.5rem 0; border-bottom: 1px solid var(--rule);
}
.ev-call { margin: 0 0 0.3rem !important; font: 600 0.72rem/1.3 var(--mono); letter-spacing: 0.06em; text-transform: uppercase; color: var(--model-text); }
.prose .ev-title { margin: 0 0 0.45rem; font: 650 1.25rem/1.25 var(--display); text-wrap: balance; }
.ev-text { margin: 0 0 0.8rem !important; color: var(--ink-2); }
.ev-text code, .box-call code { font-size: 0.84em; }
.ev-proof > span, .ev-q, .q > span {
  display: block; font: 600 0.68rem/1.3 var(--mono); letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-2);
}
.ev-proof pre, .hl-box pre {
  margin: 0.35rem 0 0; padding: 0.55rem 0.7rem;
  font-size: 0.74rem; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--paper-2); border-left: 3px solid var(--result); border-radius: 0 6px 6px 0; color: var(--ink);
}

/* The way through: session, summary, answer. Filled up to where the event got. */
.track { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 1rem; padding-right: 0.2rem; }
.tn { display: inline-flex; align-items: center; gap: 0.35rem; font: 500 0.7rem/1 var(--mono); color: var(--ink-2); white-space: nowrap; }
.tn b { width: 0.8rem; height: 0.8rem; border-radius: 50%; box-shadow: inset 0 0 0 2px var(--ink-3); flex: none; }
.tn:first-of-type b { background: var(--ink); box-shadow: none; }
.tl { flex: 1 1 0; min-width: 0.75rem; height: 2px; border-radius: 1px; background: var(--rule); position: relative; overflow: hidden; }
.tl s { position: absolute; inset: 0; background: var(--ink); transform: scaleX(0); transform-origin: 0 50%; }
.ev[data-reach="2"] .tl:nth-of-type(1) s,
.ev[data-reach="3"] .tl s { transform: none; }
.ev[data-reach="2"] .tn:nth-of-type(2) b,
.ev[data-reach="3"] .tn b { background: var(--ink); box-shadow: none; }
.ev[data-reach="2"] .tn:nth-of-type(-n + 2),
.ev[data-reach="3"] .tn { color: var(--ink); font-weight: 600; }
/* The part of the way it didn't get: dashed. */
.ev.done .tl { background: transparent; background-image: linear-gradient(90deg, var(--ink-3) 50%, transparent 0); background-size: 6px 2px; }
.ev[data-reach="2"] .tl:nth-of-type(1), .ev[data-reach="3"] .tl { background-image: none; }

.ev-q { margin: 0 0 0.45rem !important; }
.ev-opts { display: grid; gap: 0.4rem; }
.opt {
  display: flex; align-items: center; gap: 0.5rem; width: 100%; min-height: 2.75rem; padding: 0.5rem 0.85rem;
  text-align: left; font: 500 0.82rem/1.3 var(--mono); color: var(--ink);
  background: var(--card); border: 1px solid var(--rule); border-radius: 10px; cursor: pointer;
  transition: transform var(--dur-press) var(--ease-out), border-color var(--dur-fast) ease, background-color var(--dur-fast) ease;
}
.opt:not([aria-disabled]):active { transform: scale(0.98); }
@media (hover: hover) and (pointer: fine) {
  .opt:not([aria-disabled]):hover { border-color: var(--ink-3); background: var(--paper); }
}
.opt[aria-disabled] { cursor: default; color: var(--ink-2); }
.opt[aria-pressed="true"] { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); color: var(--ink); }
.opt.is-true { background: color-mix(in srgb, var(--result) 14%, var(--card)); border-color: var(--result); color: var(--ink); }
.opt.is-true::after { content: "✓"; margin-left: auto; font-weight: 600; color: var(--result-text); }

.ev-reveal { margin-top: 0.9rem; }
.verdict { margin: 0 0 0.6rem !important; font: 600 1.02rem/1.4 var(--display); color: var(--ink); }
.verdict .ok { color: var(--result-text); }
.verdict .no { color: var(--model-text); }
.q { margin: 0.6rem 0 0; padding: 0.45rem 0 0.45rem 0.75rem; border-left: 3px solid var(--model); font-size: 0.92rem; line-height: 1.5; }
.q > span { margin-bottom: 0.2rem; }
.q code { font-size: 0.84em; background: var(--paper-2); padding: 0.05em 0.3em; border-radius: 4px; }
.q-none { margin: 0.6rem 0 0 !important; padding-left: 0.75rem; border-left: 3px dashed var(--ink-3); font-size: 0.92rem; color: var(--ink-2); font-style: italic; }
.q-note { margin: 0.6rem 0 0 !important; font-size: 0.88rem; line-height: 1.5; color: var(--ink-2); }
.tally { margin: 1.25rem 0 0 !important; font: 500 1.1rem/1.45 var(--display); color: var(--ink); }
.tally:focus { outline: none; }

@media (max-width: 48rem) {
  .ev { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- the summary and the answers, as documents ---------- */
.doc {
  margin: 1.5rem 0 1.75rem;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 14px;
  overflow: hidden;
}
.doc-head {
  margin: 0 !important; padding: 0.55rem 1rem;
  font: 600 0.72rem/1.3 var(--mono); letter-spacing: 0.06em; text-transform: uppercase;
  background: var(--model); color: var(--on-fill);
}
.doc-body { padding: 1rem clamp(1rem, 3vw, 1.75rem) 1.25rem; font-size: 0.97rem; line-height: 1.6; color: var(--say, var(--ink)); }
.doc-body p { margin: 0 0 0.35rem !important; overflow-wrap: anywhere; }
.doc-body code { font-size: 0.84em; background: var(--paper-2); padding: 0.05em 0.3em; border-radius: 4px; overflow-wrap: anywhere; }
.d-gap { height: 0.5rem; }
.d-h { margin-top: 1rem !important; font: 650 1.08rem/1.35 var(--display); color: var(--ink); }
.d-h:first-child { margin-top: 0 !important; }
.d-li { position: relative; padding-left: calc(1.1rem + var(--lvl, 0) * 1.25rem); }
.d-li::before { content: ""; position: absolute; left: calc(0.3rem + var(--lvl, 0) * 1.25rem); top: 0.7em; width: 5px; height: 5px; border-radius: 50%; background: var(--ink-3); }
.d-in { padding-left: 1.1rem; }
.d-tag { font: 400 0.76rem/1.5 var(--mono); color: var(--ink-2); }
.d-code {
  margin: 0.3rem 0 0.6rem 1.1rem; padding: 0.6rem 0.8rem;
  font-size: 0.74rem; line-height: 1.55; overflow-x: auto;
  background: var(--paper-2); border-radius: 6px; color: var(--ink);
}
.d-fold { margin: 0.2rem 0 0.5rem 1.1rem; }
.d-fold > summary {
  display: inline-flex; align-items: center; gap: 0.45rem; min-height: 2.75rem; padding: 0 0.2rem;
  font: 600 0.74rem/1.2 var(--mono); color: var(--ink-2); cursor: pointer; list-style: none;
}
.d-fold > summary::-webkit-details-marker { display: none; }
.d-fold > summary::before {
  content: ""; width: 0.4rem; height: 0.4rem; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(-45deg); transition: transform var(--dur-press) var(--ease-out); flex: none;
}
.d-fold[open] > summary::before { transform: rotate(45deg) translate(-1px, -1px); }
.d-fold .d-code { margin-left: 0; }
@media (hover: hover) and (pointer: fine) { .d-fold > summary:hover { color: var(--ink); } }

/* Passages that refer to the session: select one to open what happened. */
.hl {
  color: inherit; text-decoration: none;
  background: var(--hl); border-bottom: 2px solid var(--you);
  border-radius: 3px 3px 0 0; padding: 0.05em 0.1em;
  box-decoration-break: clone; -webkit-box-decoration-break: clone;
  cursor: pointer;
  transition: background-color var(--dur-fast) ease;
}
.hl-note { background: color-mix(in srgb, var(--sys) 16%, var(--card)); border-bottom: 2px dashed var(--sys); }
.hl[aria-expanded="true"] { background: var(--hl-on); }
.hl-note[aria-expanded="true"] { background: color-mix(in srgb, var(--sys) 30%, var(--card)); }
@media (hover: hover) and (pointer: fine) {
  .hl:hover { background: var(--hl-on); }
  .hl-note:hover { background: color-mix(in srgb, var(--sys) 30%, var(--card)); }
}
.hl-box {
  margin: 0.4rem 0 0.8rem; padding: 0.75rem 0.9rem;
  font: 400 0.9rem/1.5 var(--body); color: var(--ink);
  background: var(--paper); border: 1px solid var(--rule); border-radius: 10px;
}
.hl-box p { margin: 0 0 0.4rem !important; }
.box-call { font: 600 0.72rem/1.4 var(--mono) !important; letter-spacing: 0.04em; color: var(--model-text); }

.answer .doc-body { font-size: 1rem; }
.doc.kept { margin: 0.75rem 0 0; }
.doc.kept .doc-body { font-size: 0.95rem; }

/* ---------- figure 2: three sizes ---------- */
.size-row + .size-row { margin-top: 1.1rem; }
.size-l { margin: 0 0 0.4rem !important; font: 500 0.8rem/1.35 var(--mono); color: var(--ink-2); font-variant-numeric: tabular-nums; }

.prose h3 { margin-top: 1.75rem; }
.files { list-style: none; margin: 0.5rem 0 1.25rem; padding: 0; display: grid; gap: 0.4rem; }
.file {
  display: flex; flex-direction: column; gap: 0.15rem;
  padding: 0.6rem 0.85rem; border: 1px solid var(--rule); border-radius: 10px; background: var(--card);
}
.file.is-stub { background: transparent; border-style: dashed; }
.file-path { overflow-wrap: anywhere; font: 400 0.8rem/1.4 var(--mono); }
.file-dir { color: var(--ink-2); }
.file-path code { font-size: 1em; font-weight: 600; color: var(--ink); background: none; padding: 0; }
.file-kind { font: 500 0.74rem/1.4 var(--mono); color: var(--ink-2); }
.file.is-full .file-kind { color: var(--result-text); }

/* ---------- the answer's table ---------- */
.matrix-wrap { margin: 1.25rem 0 1.5rem; overflow-x: auto; border: 1px solid var(--rule); border-radius: 12px; background: var(--card); }
.matrix-wrap:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
.matrix { width: 100%; border-collapse: collapse; font: 400 0.82rem/1.4 var(--mono); }
.matrix caption { text-align: left; padding: 0.75rem 0.9rem 0.25rem; font: 600 1.02rem/1.35 var(--display); color: var(--ink); }
.matrix th, .matrix td { padding: 0.55rem 0.9rem; border-top: 1px solid var(--rule); vertical-align: top; text-align: left; }
.matrix thead th { font-weight: 600; color: var(--ink-2); font-size: 0.74rem; letter-spacing: 0.02em; vertical-align: bottom; }
.matrix tbody th { font: 400 0.95rem/1.45 var(--body); color: var(--ink); min-width: 13rem; }
.matrix tbody th code { font-size: 0.82em; background: var(--paper-2); padding: 0.05em 0.3em; border-radius: 4px; }
.matrix td { white-space: nowrap; color: var(--ink-2); }
.dot { display: inline-block; width: 0.7rem; height: 0.7rem; border-radius: 50%; margin-right: 0.45rem; vertical-align: -0.05rem; }
.dot.yes { background: var(--ink); }
.dot.no { box-shadow: inset 0 0 0 1.5px var(--ink-3); }
/* Phones: one block per point, each cell labelled with its column. */
@media (max-width: 34rem) {
  .matrix, .matrix tbody, .matrix tr, .matrix th, .matrix td { display: block; }
  .matrix thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .matrix caption { display: block; padding-bottom: 0.5rem; }
  .matrix tr { padding: 0.7rem 0.9rem; border-top: 1px solid var(--rule); }
  .matrix th, .matrix td { padding: 0; border: 0; min-width: 0 !important; }
  .matrix tbody th { margin-bottom: 0.4rem; }
  .matrix td { display: flex; align-items: center; padding: 0.15rem 0; white-space: normal; }
  .matrix td::before { content: attr(data-l); flex: none; width: 10.75rem; padding-right: 0.5rem; font-size: 0.74rem; color: var(--ink-2); }
}

/* the run's last message, folded */
details.more { margin: 0 0 1.4rem; }
details.more > summary {
  display: inline-flex; align-items: center; gap: 0.5rem; min-height: 2.75rem; padding: 0.4rem 0.95rem;
  font: 600 0.82rem/1.25 var(--mono); color: var(--ink);
  background: var(--paper); border: 1px solid var(--rule); border-radius: 8px; cursor: pointer; list-style: none;
  transition: border-color var(--dur-fast) ease, background-color var(--dur-fast) ease;
}
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before {
  content: ""; width: 0.45rem; height: 0.45rem; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(-45deg); transition: transform var(--dur-press) var(--ease-out); flex: none;
}
details.more[open] > summary::before { transform: rotate(45deg) translate(-1px, -1px); }
@media (hover: hover) and (pointer: fine) {
  details.more > summary:hover { border-color: var(--ink-3); background: var(--card); }
}

/* ---------- figure 3: two costs ---------- */
.bar-row + .bar-row { margin-top: 1rem; }
.bar { display: flex; gap: 2px; height: 18px; }
.bar i { display: block; height: 100%; min-width: 2px; }
.bar i:first-child { border-radius: 4px 0 0 4px; }
.bar i:last-child { border-radius: 0 4px 4px 0; }
.bar i:only-child { border-radius: 4px; }

/* ---------- colophon ---------- */
.flags span { white-space: nowrap; }
.flags {
  margin: 0 0 0.85rem; padding: 0.75rem 0.9rem; white-space: normal;
  font-size: 0.74rem; line-height: 1.6; background: var(--paper-2); border-radius: 8px; color: var(--ink);
}

/* ---------- phones ---------- */
@media (max-width: 30rem) {
  :root { --cell: 8px; }
  .fig-head { flex-direction: column; align-items: flex-start; }
  .seg-opt span { padding: 0 0.8rem; }
}

/* Forced colours: squares keep their outlines, the kinds stay apart by system colours. */
@media (forced-colors: active) {
  .cells .c { forced-color-adjust: none; }
  .k-gone { box-shadow: inset 0 0 0 1px GrayText; }
  .hl { border-bottom-color: Highlight; }
}
