/* Visual system — see design/visual-system.md.
   Three categorical species, state within a column, and a non-colour channel
   on every one of them. Palette validated with the dataviz validator in both
   modes under --pairs all; do not edit hues without re-running it. */

.viz-root {
  color-scheme: light;

  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6e66;   /* 5.0:1 on the light surface; #83817a was 3.8:1 */
  --text-inverse: #ffffff;

  --cp: #2872cc;      /* slot 1 — coprime column. Darkened from #2a78d6 so white
                         cell text clears 4.5:1; re-validated, all checks pass */
  --accent: #eb6834;  /* slot 2 — the current strike / selected family */
  --ncp: #1baf7a;     /* slot 3 — headed by a retired prime */
  --np: #8a8880;      /* absence; neutral by design, costs no slot */

  /* the categorical hues are validated as fills; as small text they are
     4.3:1 (blue) and 3.1:1 (orange), so text uses a darkened variant */
  --cp-ink: color-mix(in srgb, var(--cp) 72%, var(--text-primary));
  --accent-ink: color-mix(in srgb, var(--accent) 58%, var(--text-primary));
  --rule: #dedcd4;
  --cell-gap: 2px;
  --cell-radius: 4px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-root {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #96948a;
    --text-inverse: #10100f;
    --cp: #3987e5;
    --accent: #d95926;
    --ncp: #199e70;
    --np: #6f6d66;
    --cp-ink: color-mix(in srgb, var(--cp) 84%, var(--text-primary));
  --accent-ink: color-mix(in srgb, var(--accent) 78%, var(--text-primary));
  --rule: #35342f;
  }
}
:root[data-theme="dark"] .viz-root {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #96948a;
  --text-inverse: #10100f;
  --cp: #3987e5;
  --accent: #d95926;
  --ncp: #199e70;
  --np: #6f6d66;
  --cp-ink: color-mix(in srgb, var(--cp) 84%, var(--text-primary));
  --accent-ink: color-mix(in srgb, var(--accent) 78%, var(--text-primary));
  --rule: #35342f;
}

/* ---------------------------------------------------------------- page --- */

body {
  margin: 0;
  background: var(--surface-1);
  color: var(--text-primary);
  font: 16px/1.5 ui-sans-serif, -apple-system, "Segoe UI", system-ui, sans-serif;
}
.wrap { max-width: 62rem; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
h1 { font-size: clamp(1.9rem, 5vw, 3rem); line-height: 1.1; margin: 0 0 1rem; letter-spacing: -0.02em; }
h2 { font-size: 1.25rem; margin: 3rem 0 0.5rem; }
p  { max-width: 34rem; color: var(--text-secondary); }
p.lede { font-size: 1.1rem; color: var(--text-primary); }

/* --------------------------------------------------------------- array --- */

.array {
  display: grid;
  gap: var(--cell-gap);                 /* 2px surface gap: fills never touch */
  overflow-x: auto;
  padding-bottom: 0.25rem;
}

/* A wide array is the content, not an illustration inside the text column:
   let it break the measure rather than clipping or shrinking to illegibility. */
.array-block.wide {
  width: 100vw;
  margin-left: calc(50% - 50vw);
  padding: 0 1.25rem;
  box-sizing: border-box;
}
.array-row { display: contents; }

.cell {
  min-width: var(--cell-min, 2.75rem);
  padding: 0.4rem 0.5rem;
  border-radius: var(--cell-radius);
  font-variant-numeric: tabular-nums;
  font-size: 0.9rem;
  text-align: right;
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid transparent;
  cursor: default;
}

/* --- CP column states ------------------------------------------------- */
.cell.prime { background: var(--cp); color: var(--text-inverse); font-weight: 600; }

/* liar: same hue, tinted, plus a 45deg hatch — the non-colour channel */
.cell.liar {
  background:
    repeating-linear-gradient(45deg,
      color-mix(in srgb, var(--cp) 28%, transparent) 0 2px,
      transparent 2px 5px),
    color-mix(in srgb, var(--cp) 12%, var(--surface-1));
  border-color: color-mix(in srgb, var(--cp) 45%, transparent);
  color: var(--text-primary);
}
.cell.one { border: 1px dashed var(--rule); color: var(--text-muted); }

/* --- NCP column states ------------------------------------------------ */
.cell.ncp-head { background: var(--ncp); color: var(--text-inverse); font-weight: 600; }
.cell.ncp-mult {
  background:
    radial-gradient(color-mix(in srgb, var(--ncp) 42%, transparent) 0.9px, transparent 1px)
      0 0 / 5px 5px,
    color-mix(in srgb, var(--ncp) 10%, var(--surface-1));
  color: var(--text-secondary);
}

/* --- NP --------------------------------------------------------------- */
.cell.np { background: color-mix(in srgb, var(--np) 8%, var(--surface-1)); color: var(--text-muted); }

/* --- transient selection: one family, one accent ---------------------- */
.cell.selected {
  background: var(--accent) !important;
  color: var(--text-inverse) !important;
  border-color: transparent !important;
  box-shadow: 0 0 0 2px var(--surface-1), 0 0 0 4px var(--accent);
}
.cell[data-clickable] { cursor: pointer; }
.cell:focus-visible { outline: 2px solid var(--text-primary); outline-offset: 2px; }

/* ------------------------------------------------- gcd companion row --- */
/* Flush against the array (the Codex mockup got this right): the X-ray is
   attached to what it explains. It is also the accessibility channel, so it
   is on by default rather than a toggle. */
.companion { margin-top: var(--cell-gap); border-top: 2px solid var(--rule); padding-top: var(--cell-gap); }
.companion .cell { font-size: 0.78rem; color: var(--text-muted); background: transparent; }
/* the residue each column stands for — in a condensed array the gcd row is
   all 1s by construction, so the column label is what carries information */
.col-head .cell { font-size: .72rem; color: var(--text-muted); background: transparent; padding-bottom: .15rem; }
/* the fill blue is only 4.3:1 as small text — darken it for this use */
.companion .cell.is-cp { color: var(--cp-ink); font-weight: 700; }
.companion-label { font-size: 0.7rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); margin: 0.6rem 0 0.2rem; }

/* --------------------------------------------------------------- chrome --- */
.legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; margin: 1rem 0; padding: 0; list-style: none; font-size: 0.85rem; color: var(--text-secondary); }
.legend li { display: flex; align-items: center; gap: 0.4rem; }
.swatch { width: 1.1rem; height: 1.1rem; border-radius: 3px; display: inline-block; }
.glyph { font-size: 0.75rem; color: var(--text-muted); }

.stepper { display: flex; gap: 0.4rem; align-items: center; margin: 1.5rem 0 1rem; flex-wrap: wrap; }
.stepper button {
  font: inherit; font-variant-numeric: tabular-nums;
  min-width: 2.5rem; padding: 0.35rem 0.6rem;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--rule); border-radius: 6px; cursor: pointer;
}
.stepper button[aria-pressed="true"] { background: var(--text-primary); color: var(--surface-1); border-color: var(--text-primary); }
.stepper button:focus-visible { outline: 2px solid var(--cp); outline-offset: 2px; }

.readout { min-height: 3.2rem; margin: 0.75rem 0 0; font-size: 0.95rem; color: var(--text-primary); }
.readout code { font-size: 0.9em; color: var(--text-secondary); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media print {
  .stepper, .chevron, .readout, #beat-nav, .theme, .bridge, .tools { display: none; }
  /* an instruction to tap something is meaningless on paper */
  .cell { border: 1px solid #999 !important; background: #fff !important; color: #000 !important; }
  .cell.prime::after { content: " \25CF"; }
  .cell.liar::after  { content: " \2571"; }
  .cell.ncp-head::after { content: " \25B2"; }
}

/* ============================================================ chrome ===== */
#chrome {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem;
  padding: 0.6rem 1.25rem; background: var(--surface-1);
  border-bottom: 1px solid var(--rule); flex-wrap: wrap;
}
.wordmark { font-weight: 700; font-size: .8rem; letter-spacing: .05em; text-transform: uppercase; color: var(--text-primary); text-decoration: none; }
#beat-nav { display: flex; gap: .75rem; flex: 1 1 0; min-width: 0; overflow-x: auto; font-size: .8rem; scrollbar-width: none; }
#beat-nav a { color: var(--text-muted); text-decoration: none; white-space: nowrap; }
#beat-nav a:hover, #beat-nav a:focus-visible { color: var(--cp-ink); }
#beat-nav .nav-paper { color: var(--text-secondary); font-weight: 600; }
.btn {
  font: inherit; font-size: .85rem; padding: .35rem .8rem; cursor: pointer;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--rule); border-radius: 6px; text-decoration: none; display: inline-block;
}
.btn:hover { border-color: var(--cp); color: var(--cp-ink); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.theme { font: inherit; background: none; border: 1px solid var(--rule); border-radius: 6px; cursor: pointer; padding: .25rem .5rem; color: var(--text-secondary); }

.beat { margin: 0 0 4.5rem; scroll-margin-top: 5.5rem; }
.beat h2 { margin-top: 0; }
.small { font-size: .82rem; color: var(--text-muted); }
footer { border-top: 1px solid var(--rule); padding-top: 1.5rem; }

/* ============================================================ glossary === */
.gloss { border-bottom: 1px dotted var(--cp); cursor: help; position: relative; }
/* display:none, not opacity:0 — an absolutely positioned tooltip that is merely
   transparent still contributes scrollable overflow, which quietly widened the
   whole document on narrow screens. */
.gloss-note {
  display: none;
  position: absolute; left: 0; top: 1.6em; z-index: 20;
  width: max-content; max-width: min(18rem, calc(100vw - 2rem));
  background: var(--text-primary); color: var(--surface-1);
  padding: .45rem .7rem; border-radius: 6px; font-size: .8rem; line-height: 1.4;
  pointer-events: none;
}
.gloss:hover .gloss-note, .gloss:focus .gloss-note, .gloss:focus-within .gloss-note { display: block; }

/* ============================================================== cards ==== */
.cards { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr)); }
.card { border: 1px solid var(--rule); border-radius: 10px; padding: 1.1rem 1.2rem; }
.card h3 { margin: .2rem 0 .6rem; font-size: 1.05rem; }
.eyebrow { font-size: .68rem; letter-spacing: .1em; text-transform: uppercase; color: var(--text-muted); margin: 0; }
.card-statement { font-size: .92rem; }
.card-foot { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .9rem; }
.verify-out { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .78rem; color: var(--text-muted); margin: .6rem 0 0; display: none; }
.verify-out.shown { display: block; }
.demo-rows { display: flex; flex-direction: column; gap: .25rem; margin: .6rem 0; }
.demo-row { display: flex; gap: .4rem; align-items: baseline; font-size: .85rem; font-variant-numeric: tabular-nums; }
.demo-row code, .chip { background: color-mix(in srgb, var(--cp) 10%, var(--surface-1)); padding: .05rem .35rem; border-radius: 4px; }
.demo-row .op { color: var(--text-muted); }
.demo-note { font-size: .82rem; color: var(--text-muted); margin: .5rem 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: .3rem; margin: .5rem 0; }
.chip { font-size: .78rem; font-variant-numeric: tabular-nums; }
.chip-btn { font: inherit; font-size: .78rem; padding: .2rem .55rem; border: 1px solid var(--rule); border-radius: 999px; background: var(--surface-1); color: var(--text-secondary); cursor: pointer; }
.chip-btn:hover { border-color: var(--cp); color: var(--cp-ink); }

/* ============================================================== tables === */
table.data { border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; margin: 1rem 0; width: 100%; }
table.data th, table.data td { border-bottom: 1px solid var(--rule); padding: .4rem .6rem; text-align: right; }
table.data th { color: var(--text-muted); font-weight: 500; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data tr.zeroed td { color: var(--text-muted); }
.ratio-cell { display: inline-flex; align-items: center; gap: .5rem; justify-content: flex-end; }
.ratio-track { width: 7rem; height: .5rem; background: color-mix(in srgb, var(--np) 14%, transparent); border-radius: 2px; overflow: hidden; display: inline-block; }
.ratio-bar { height: .5rem; background: var(--cp); border-radius: 2px; min-width: 2px; display: block; }
.ratio-num { min-width: 5.5rem; text-align: right; }
figure { margin: 1.25rem 0; }
figcaption { font-size: .8rem; color: var(--text-muted); margin-top: .5rem; max-width: 40rem; }

/* ============================================================== charts === */
.bars { display: flex; flex-direction: column; gap: 3px; max-width: 34rem; }
.bar-row { display: flex; align-items: center; gap: .5rem; font-size: .78rem; font-variant-numeric: tabular-nums; }
.bar-label { width: 1.6rem; text-align: right; color: var(--text-muted); }
.bar { height: .65rem; background: color-mix(in srgb, var(--cp) 45%, transparent); border-radius: 2px; }
.bar.is-champ { background: var(--accent); }
.bar-val { color: var(--text-secondary); }

/* ============================================================== tuples === */
.pattern-input { font: inherit; font-variant-numeric: tabular-nums; padding: .35rem .6rem; border: 1px solid var(--rule); border-radius: 6px; background: var(--surface-1); color: var(--text-primary); width: 10rem; }
.verdict { font-size: .9rem; padding: .55rem .8rem; border-radius: 6px; max-width: 40rem; }
.verdict.ok { background: color-mix(in srgb, var(--ncp) 12%, var(--surface-1)); border: 1px solid color-mix(in srgb, var(--ncp) 40%, transparent); }
.verdict.dead { background: color-mix(in srgb, var(--accent) 12%, var(--surface-1)); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); }

/* ========================================================== exercises ==== */
.exercises { padding-left: 1.2rem; }
.exercises li { margin-bottom: 1.4rem; max-width: 40rem; }
.answer { display: none; font-size: .88rem; border-left: 3px solid var(--cp); padding-left: .8rem; margin-top: .6rem; }
.answer.shown { display: block; }
.sources { font-size: .85rem; padding-left: 1.1rem; }
.sources a { color: var(--cp-ink); }

/* ======================================================= dynamic X-ray === */
.companion.dynamic .cell { color: var(--text-muted); }
.companion.dynamic .cell.is-strike { background: var(--accent); color: var(--text-inverse); font-weight: 700; border-radius: var(--cell-radius); }

/* ============================================================== dialog === */
dialog { border: 1px solid var(--rule); border-radius: 12px; max-width: 34rem; padding: 1.5rem; background: var(--surface-1); color: var(--text-primary); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog h2 { margin-top: 0; }

.boot-note {
  border: 1px solid var(--rule); border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 1rem 1.2rem; max-width: 40rem; margin-top: 2rem;
  font-size: .92rem; color: var(--text-primary);
}
.boot-note code { background: color-mix(in srgb, var(--np) 12%, var(--surface-1)); padding: .1rem .35rem; border-radius: 4px; }

/* --- the strike, animated (story N4) --------------------------------- */
.cell.striking {
  background: var(--accent) !important; color: var(--text-inverse) !important;
  transform: scale(1.06); transition: background .18s, transform .18s;
  box-shadow: 0 0 0 2px var(--surface-1), 0 0 0 4px var(--accent);
}
.cell.struck-out {
  opacity: .12; transform: scale(.9);
  transition: opacity .45s ease-out, transform .45s ease-out;
}
@media (prefers-reduced-motion: reduce) {
  .cell.striking, .cell.struck-out { transition: none; transform: none; }
}
.tools { display: flex; gap: .5rem; flex-wrap: wrap; margin: .9rem 0 0; }

.citation { border: 1px solid var(--rule); border-radius: 10px; padding: 1rem 1.2rem; margin-top: 1.5rem; max-width: 40rem; }
.citation h3 { margin: 0 0 .5rem; font-size: .95rem; }
.cite-ref { font-size: .88rem; color: var(--text-primary); }

/* --- presentation mode: the back row has to read it too (story O2) --- */
body.presenting { font-size: 20px; }
body.presenting #beat-nav,
body.presenting footer,
body.presenting .tools,
body.presenting .citation { display: none; }
body.presenting .wrap { max-width: none; }
body.presenting .cell { font-size: 1.15rem; padding: .6rem .7rem; min-width: 3.4rem; }
body.presenting .companion .cell { font-size: 1rem; }
body.presenting .col-head .cell { font-size: .95rem; }
body.presenting .cell.liar { box-shadow: inset 0 0 0 2px var(--cp); }
body.presenting .legend { font-size: 1rem; }
body.presenting .lede { font-size: 1.3rem; }

/* --- formal statement, in place --------------------------------------- */
.formal { margin-top: .9rem; border-top: 1px solid var(--rule); padding-top: .6rem; }
.formal summary { cursor: pointer; font-size: .82rem; color: var(--text-muted); }
.formal summary:hover { color: var(--cp-ink); }
.formal-body { margin-top: .6rem; font-size: .86rem; overflow-x: auto; }
.formal-body p { max-width: none; }
.formal-body math { font-size: 1em; }
.verify-out.good { color: var(--text-primary); }
.verify-out.bad { color: var(--accent-ink); }

/* ============================================== acts & connective tissue == */
/* Twelve demonstrations in a row do not accumulate into an argument. The act
   divider says what the next few beats are for; the bridge earns the next one. */
.act { margin: 4rem 0 2.5rem; padding-top: 1.5rem; border-top: 2px solid var(--text-primary); }
.act:first-child { margin-top: 1rem; }
.act-n { font-size: .72rem; letter-spacing: .14em; text-transform: uppercase; color: var(--accent-ink); margin: 0 0 .3rem; font-weight: 700; }
.act-title { font-size: 1.5rem; margin: 0 0 .5rem; letter-spacing: -.01em; }
.act-premise { max-width: 38rem; color: var(--text-secondary); }

.bridge {
  margin: 1.6rem 0 0; padding-left: 1rem;
  border-left: 2px solid var(--accent);
  color: var(--text-primary); font-size: 1rem; max-width: 34rem;
}

#beat-nav a.here { color: var(--text-primary); font-weight: 700; }

/* ================================================= the Act I sticky stage == */
.stage { display: grid; gap: 2.5rem; align-items: start; margin-bottom: 4rem; }
.stage-figure { min-width: 0; }
.stage-steps > .beat:last-child { margin-bottom: 0; }

@media (min-width: 62rem) {
  .stage { grid-template-columns: minmax(0, 1.15fr) minmax(17rem, .85fr); }
  /* the array is the subject of everything to its right, so it stays put */
  .stage-figure { position: sticky; top: 4.75rem; }
  .stage .array-block.wide { width: auto; margin-left: 0; padding: 0; }
}
@media (max-width: 61.999rem) {
  .stage-figure {
    position: sticky; top: 3.6rem; z-index: 4;
    background: var(--surface-1); padding-bottom: .6rem;
    box-shadow: 0 6px 12px -10px rgb(0 0 0 / .4);
  }
  .stage-figure .legend { font-size: .78rem; }
}
body.presenting .stage-figure { position: static; }

/* --- "Why is this true?" — the reader's actual question at a rule ------ */
.why { margin: .9rem 0 0; padding: .8rem 1rem; border-radius: 8px;
       background: color-mix(in srgb, var(--cp) 6%, var(--surface-1));
       border: 1px solid color-mix(in srgb, var(--cp) 22%, transparent); }
.why p { margin: 0; font-size: .88rem; max-width: none; color: var(--text-primary); }
.why-label { font-weight: 700; margin-bottom: .35rem !important; font-size: .82rem !important; }
.breaker { padding-top: .6rem; }
.breaker input[type="range"] { width: 100%; margin: .4rem 0; accent-color: var(--accent); }

/* ==================================================== narrow viewports ==== */
/* The stage's full-bleed rule is for a wide text column; inside the stage it
   only pushes the page sideways, at any width. */
.stage .array-block.wide { width: auto; margin-left: 0; padding: 0; }

@media (max-width: 48rem) {
  .wrap { padding-left: 1rem; padding-right: 1rem; }
  .array-block { --cell-min: 2rem; }
  .cell { font-size: .78rem; padding: .3rem .35rem; }
  .col-head .cell, .companion .cell { font-size: .66rem; }
  /* full-bleed is for breaking out of a narrow text column; on a phone the
     text column already is the screen, so it only pushes the page sideways */
  .array-block.wide { width: auto; margin-left: 0; padding: 0; }
  .act { margin-top: 2.5rem; }
  .act-title { font-size: 1.25rem; }
  h1 { font-size: 1.8rem; }
  /* In a single-column grid each item's containing block is its own row, so a
     sticky child has nowhere to travel. Normal flow gives it the whole stage. */
  .stage { display: block; }
  .stage-steps { margin-top: 1.5rem; }
  /* a sticky panel taller than the screen is worse than no sticky panel */
  .stage-figure { max-height: 42vh; overflow-y: auto; overscroll-behavior: contain; }
  #chrome { gap: .5rem .75rem; padding: .5rem .75rem; }
  #beat-nav { order: 3; flex-basis: 100%; }
  .wordmark { font-size: .7rem; }
  .cards { grid-template-columns: minmax(0, 1fr); }
}
/* Grid and flex children default to min-width:auto, which stops them shrinking
   and silently widens the document. */
.beat, .card, .stage, .stage-steps, .stage-figure, figure, .cards,
.array-block, .tools, .card-foot, .why, .breaker, .formal, .formal-body { min-width: 0; }
.array, .array-block, .canvas, .breaker input { max-width: 100%; }
.btn, .chip, .cite-ref { overflow-wrap: anywhere; max-width: 100%; }
.scroll-x { overflow-x: auto; max-width: 100%; }
.scroll-x > table.data { margin-top: 0; }
.demo-rows, .chips, .citation, .sources { min-width: 0; overflow-wrap: anywhere; }

/* No overflow-x:hidden on html/body — it makes them a scroll container and
   silently kills position:sticky. Sideways scroll is prevented at the source
   instead: .stage resets the full-bleed rule, and wide arrays scroll inside
   their own .array box. */
