/* ==========================================================================
   figures.css — interactive figure chrome
   Companion to assets/js/figures.js.

   Every figure here is theme-aware: it reads its palette from tokens.css at
   runtime rather than baking in dark-mode hex values. The SVG diagrams this
   replaced hardcoded rgba(5,8,10,0.9) fills and #FFFFFF text, which rendered
   as black-on-cream in the light theme (the site default).

   Figures degrade in three stages:
     1. No JS          -> the <noscript>/fallback table markup stays visible.
     2. Reduced motion -> figures render one static frame, no loop, no hover.
     3. Full           -> canvas animation + pointer/keyboard interaction.
   ========================================================================== */

/* ── Shell ───────────────────────────────────────────────────────────────── */

.figure {
  position: relative;
  border: 1px solid var(--hair);
  border-radius: var(--radius-lg);
  background: var(--lift);
  overflow: hidden;
  container-type: inline-size;
}

.figure-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-12);
  padding: var(--space-12) var(--space-16);
  border-bottom: 1px solid var(--hair-faint);
  background: var(--surface);
}

.figure-label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0;
}

/* Live status pip — pulses only while the figure is actually animating. */
.figure-pip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-8);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.figure-pip::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
  animation: figure-pip-pulse 2s ease-in-out infinite;
}

@keyframes figure-pip-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.25; }
}

/* ── Canvas stage ────────────────────────────────────────────────────────── */

.figure-stage {
  position: relative;
  display: block;
  width: 100%;
  /* Height is set per-figure via --figure-h so the canvas never causes CLS:
     the box is reserved before the script runs. */
  height: var(--figure-h, 420px);
  touch-action: pan-y;
}

.figure-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Keyboard focus lands on the stage; it is a real interactive control. */
.figure-stage:focus-visible {
  outline: 2px solid var(--orange);
  outline-offset: -2px;
}

/* ── Readout: the text half of every figure ──────────────────────────────
   Diagrams that only draw shapes tell the reader nothing they can quote.
   Each figure pairs its canvas with a live readout that names what is
   currently selected and why it matters.                                    */

.figure-readout {
  border-top: 1px solid var(--hair-faint);
  padding: var(--space-16);
  min-height: 132px;
  background: var(--canvas);
}

.figure-readout-kicker {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--orange);
  margin-bottom: var(--space-8);
}

.figure-readout h4 {
  font-family: var(--font-display);
  font-weight: var(--fw-semibold);
  font-size: var(--text-subheading);
  letter-spacing: var(--tracking-heading);
  line-height: 1.2;
  margin-bottom: var(--space-8);
  color: var(--fg);
}

.figure-readout p {
  font-size: 14px;
  line-height: var(--leading-body);
  color: var(--fg-2);
  max-width: 62ch;
  margin: 0;
}

.figure-readout code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--surface-strong);
  border: 1px solid var(--hair-faint);
  border-radius: var(--radius-sm);
  padding: 1px 5px;
  color: var(--fg);
}

/* ── Hint strip ──────────────────────────────────────────────────────────── */

.figure-hint {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  padding: var(--space-8) var(--space-16);
  border-top: 1px solid var(--hair-faint);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--surface);
}

.figure-hint kbd {
  font-family: var(--font-mono);
  font-size: 10px;
  border: 1px solid var(--hair);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  padding: 1px 5px;
  color: var(--fg-2);
  background: var(--lift);
}

/* ── Fallback (no JS / reduced data) ─────────────────────────────────────
   Real content, not a placeholder: a semantic table that says the same
   thing the canvas does. Hidden once the script takes over.               */

.figure-fallback {
  padding: var(--space-16);
  font-size: 14px;
  color: var(--fg-2);
}

.figure-fallback table {
  width: 100%;
  border-collapse: collapse;
}

.figure-fallback th,
.figure-fallback td {
  text-align: left;
  padding: var(--space-8) var(--space-12);
  border-bottom: 1px solid var(--hair-faint);
  vertical-align: top;
}

.figure-fallback th {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: var(--fw-regular);
  white-space: nowrap;
}

.figure-fallback td {
  color: var(--fg-2);
}

.figure.is-live .figure-fallback {
  display: none;
}

/* Until the script upgrades it, the stage is empty space — hide it so the
   fallback table is the only thing occupying the box. */
.figure:not(.is-live) .figure-stage,
.figure:not(.is-live) .figure-readout,
.figure:not(.is-live) .figure-hint,
.figure:not(.is-live) .figure-pip {
  display: none;
}

/* ── Layout: figure + side rail ──────────────────────────────────────────── */

.figure-split {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
  gap: var(--space-32);
  align-items: start;
}

@container (max-width: 900px) {
  .figure-split { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .figure-split { grid-template-columns: 1fr; }
  .figure-stage { height: var(--figure-h-sm, 340px); }
}

/* ── Layer rail (used beside the assembly figure) ────────────────────────── */

.layer-rail {
  list-style: none;
  display: grid;
  gap: 1px;
  background: var(--hair-faint);
  border: 1px solid var(--hair-faint);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.layer-rail li {
  background: var(--canvas);
  margin: 0;
}

.layer-rail button {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: var(--space-12);
  align-items: baseline;
  width: 100%;
  padding: var(--space-12) var(--space-16);
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease;
}

.layer-rail button:hover,
.layer-rail button:focus-visible {
  background: var(--surface-strong);
}

.layer-rail button[aria-current="true"] {
  background: var(--surface-strong);
  box-shadow: inset 2px 0 0 var(--orange);
}

.layer-rail .n {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.06em;
}

.layer-rail button[aria-current="true"] .n { color: var(--orange); }

.layer-rail .t {
  font-family: var(--font-display);
  font-weight: var(--fw-medium);
  font-size: 14px;
  letter-spacing: -0.01em;
  color: var(--fg-2);
}

.layer-rail button[aria-current="true"] .t { color: var(--fg); }

/* ── Reduced motion ──────────────────────────────────────────────────────── */

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

/* ── Query tabs (routing figure) ─────────────────────────────────────────── */

.query-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  background: var(--hair-faint);
  border-bottom: 1px solid var(--hair-faint);
}

.query-tabs button {
  flex: 1 1 auto;
  min-width: 0;
  padding: var(--space-12) var(--space-16);
  border: 0;
  background: var(--lift);
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  text-align: left;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, box-shadow 0.18s ease;
}

.query-tabs button:hover {
  background: var(--surface-strong);
  color: var(--fg-2);
}

.query-tabs button[aria-selected="true"] {
  background: var(--canvas);
  color: var(--fg);
  box-shadow: inset 0 -2px 0 var(--orange);
}

.query-tabs button::before {
  content: "> ";
  color: var(--orange);
}

@media (max-width: 720px) {
  .query-tabs button { flex-basis: 100%; }
}

/* ── Screen-reader-only utility ──────────────────────────────────────────
   Table <caption>s on the figure fallbacks: crawlers and assistive tech get
   the sentence, sighted readers get the visual figure instead.            */

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ── Cross-document view transitions ────────────────────────────────────
   Same-origin navigations cross-fade instead of flashing white. Paired
   with the Speculation Rules prerender in head.html, an in-site click
   reads as a state change rather than a page load. Browsers without the
   at-rule ignore it; reduced motion turns it off.                        */

@view-transition { navigation: auto; }

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 220ms;
  animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}

/* The nav island is the one element that should not cross-fade: it is
   present on every page, so it should feel continuous across them. */
.nav-island { view-transition-name: nav-island; }

@media (prefers-reduced-motion: reduce) {
  @view-transition { navigation: none; }
}
