/* ── Type (self-hosted, no CDN) ───────────────────────────────────────────────
   Two deliberate roles that mirror the rope itself, a precise solver and a
   readable explanation: Space Grotesk carries the structure (display), IBM Plex
   Mono is the machine voice (the title's tag, labels, code, every winding angle). */
@font-face { font-family: "Space Grotesk"; font-weight: 500; font-display: swap;
  src: url("../fonts/space-grotesk-latin-500-normal.woff2") format("woff2"); }
@font-face { font-family: "Space Grotesk"; font-weight: 700; font-display: swap;
  src: url("../fonts/space-grotesk-latin-700-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Mono"; font-weight: 400; font-display: swap;
  src: url("../fonts/ibm-plex-mono-latin-400-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Mono"; font-weight: 500; font-display: swap;
  src: url("../fonts/ibm-plex-mono-latin-500-normal.woff2") format("woff2"); }

/* ── Palette, derived from the demos' own materials, not a neutral default:
   the rope's violet is the signature; the hand's gold and the sim's cyan are the
   only accents; everything sits on a violet-tinted ink so the dark canvases belong. */
:root {
  --bg:      #0c0d15;   /* violet-tinted ink (the rope's ground plane) */
  --bg2:     #15131f;   /* surface: cards, code */
  --bg3:     #221d33;   /* raised surface */
  --line:    #2b2742;   /* hairline borders */
  --fg:      #e9e7f2;   /* paper, faint violet warmth */
  --fg2:     #928eaa;   /* muted */
  --rope:    #996EFF;   /* the rope, the one signature colour */
  --rope-dim:#6f54b8;
  --tree:    #3a6b4a;   /* trunks */
  --gold:    #f0c040;   /* the hand */
  --cyan:    #4fd0e0;   /* the slack sim */
  --warn:    #f85149;
  --ok:      #3fb950;
  --display: "Space Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --mono:    "IBM Plex Mono", ui-monospace, "SF Mono", Consolas, monospace;
  --font:    -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;

  --rail-w:  180px;   /* side progress rail width on wide viewports */
  --col:     62ch;    /* prose column max-width (readable line length) */
  --fig-w:   380px;   /* figure column width */
  --content-w: 1040px;/* overall content width: wide enough for readable prose + a roomy figure */
  --gap:     clamp(2rem, 6vw, 4rem);
}

h1, h2, h3, h4 { font-family: var(--display); }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  scroll-behavior: smooth;
}

/* ── Page header ─────────────────────────────────────────────────────────── */
#page-header {
  max-width: var(--content-w);
  margin: 0 auto;
  padding: clamp(2.5rem, 7vw, 5rem) var(--pad-x, 2rem) clamp(1.25rem, 3vw, 2rem);
  text-align: center;
}

.eyebrow {
  font: 500 .72rem var(--mono);
  letter-spacing: .26em;
  text-transform: uppercase;
  color: var(--rope);
  margin: 0 0 1rem;
}

#page-header h1 {
  font: 700 clamp(2.2rem, 6vw, 3.8rem)/1 var(--display);
  letter-spacing: -.02em;
  color: var(--fg);
  margin: 0 0 1rem;
}

.hero-sub {
  font-size: clamp(1.02rem, 1.7vw, 1.18rem);
  color: var(--fg2);
  line-height: 1.55;
  margin: 0 auto;
  max-width: 50ch;
  text-wrap: balance;
}

/* ── Layout: content column ───────────────────────────────────────────────── */
:root { --pad-x: clamp(1.25rem, 3vw, 2.5rem); }
#content {
  max-width: var(--content-w);
  margin: 0 auto;
  padding: 0 var(--pad-x) clamp(4rem, 10vw, 8rem);
}

/* ── Progress rail ────────────────────────────────────────────────────────── */
#progress-rail {
  position: fixed;
  top: 50%;
  right: clamp(1rem, 2vw, 2rem);
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: .75rem;
  z-index: 100;
}

/* Hide the rail when the viewport gutter is too narrow to host it without
   overlapping content. content is --content-w (1040px) centered; the rail needs
   ~110px of clear gutter, so it only shows at ≥ ~1260px. */
@media (max-width: 1259px) {
  #progress-rail { display: none; }
}

.rail-item {
  display: flex;
  align-items: center;
  gap: .55rem;
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
  text-align: left;
  text-decoration: none;
}

.rail-dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--fg2);
  opacity: .4;
  transition: opacity .2s, background .2s, transform .2s;
}

.rail-label {
  font-size: .72rem;
  color: var(--fg2);
  opacity: .5;
  transition: opacity .2s, color .2s;
  white-space: nowrap;
  pointer-events: none;
  font-family: var(--mono);
  letter-spacing: .03em;
}

.rail-item:hover .rail-dot,
.rail-item.active .rail-dot {
  background: var(--rope);
  opacity: 1;
  transform: scale(1.35);
}

.rail-item:hover .rail-label,
.rail-item.active .rail-label {
  opacity: 1;
  color: var(--rope);
}

/* ── Hero banner ──────────────────────────────────────────────────────────── */
.hero-banner {
  max-width: min(900px, 92vw);
  margin: 0 auto clamp(2rem, 5vw, 3.5rem);
  padding: 0;
  text-align: center;
}

.hero-banner img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 14px;
}

.hero-banner figcaption {
  font-size: .8rem;
  color: var(--fg2);
  margin-top: .5rem;
  line-height: 1.45;
}

/* ── Section ──────────────────────────────────────────────────────────────── */
.sec {
  display: grid;
  /* minmax(0,1fr) lets columns shrink below their content's min-width, so a wide
     <pre> (game-loop recipe) scrolls inside its own box instead of overflowing the
     section and shoving the figure under the rail. */
  grid-template-columns: minmax(0, 1fr) minmax(0, var(--fig-w));
  gap: clamp(1.5rem, 3vw, 2.5rem);
  align-items: start;
  margin-bottom: clamp(4rem, 12vw, 8rem);
  padding-top: clamp(1rem, 3vw, 2rem);
  /* scroll-margin-top so rail links land below the top of the viewport */
  scroll-margin-top: 2rem;
}

/* Narrow: stack prose then figure */
@media (max-width: 780px) {
  .sec { grid-template-columns: 1fr; }
  #progress-rail { display: none; }
}

/* The fix: a full-width turning point. Its own section so it reads as the resolution of
   all three failures, not a footnote to the third; single column with centered prose
   breaks the two-column demo rhythm to mark the pivot. */
.sec-fix { grid-template-columns: 1fr; }
.sec-fix .prose { margin-inline: auto; }

/* ── Prose column ─────────────────────────────────────────────────────────── */
.prose {
  max-width: var(--col);
  /* Grid item: default min-width:auto lets a wide child force the column past the
     viewport (h-scroll at ~360px). min-width:0 lets it shrink; the <pre> scrolls
     internally and long inline <code> tokens wrap (below). */
  min-width: 0;
}

.prose h2 {
  font-size: clamp(1.3rem, 3vw, 2rem);
  color: var(--rope);
  margin: 0 0 .75rem;
}

.prose p {
  font-size: clamp(.9rem, 1.6vw, 1.05rem);
  color: var(--fg);
  line-height: 1.65;
  margin: 0 0 .85rem;
}

.prose p:last-child { margin-bottom: 0; }

.prose em { color: var(--fg); font-style: italic; }

/* In-prose links (section cross-refs like §Synthesis): themed, not browser-blue. */
.prose a {
  color: var(--rope);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--rope) 40%, transparent);
  transition: border-color .15s, color .15s;
}
.prose a:hover,
.prose a:focus-visible {
  color: #b79bff;
  border-bottom-color: var(--rope);
}
.prose a:focus-visible {
  outline: 2px solid var(--rope);
  outline-offset: 2px;
  border-radius: 2px;
}

/* "softlink": reads as plain text at rest, reveals itself as a link on hover/focus (an easter egg). */
.prose a.softlink { color: inherit; border-bottom: none; }
.prose a.softlink:hover,
.prose a.softlink:focus-visible { color: #b79bff; border-bottom: 1px solid var(--rope); }

.prose code {
  font-family: var(--mono);
  font-size: .88em;
  color: var(--rope);
  background: var(--bg2);
  border-radius: 4px;
  padding: .1em .3em;
  /* Long inline tokens (e.g. decomposeIntoArcAndFreeSpans) wrap instead of forcing
     the prose column wider than a narrow viewport. Block code (.prose pre code)
     overrides this with white-space:pre and scrolls instead. */
  overflow-wrap: break-word;
}

/* Block code (recipes, pseudocode): scroll long lines WITHIN the block so they
   never push the page wider than the viewport. */
.prose pre {
  max-width: 100%;
  overflow-x: auto;
  background: var(--bg2);
  border-radius: 8px;
  padding: .8rem 1rem;
  margin: 1rem 0;
  font-size: .82rem;
  line-height: 1.5;
}
.prose pre code {
  background: none;
  padding: 0;
  color: var(--fg);
  white-space: pre;
}

/* Set-apart callouts (key takeaways + technique asides) share one gold tab so the highlight
   reads consistently; gold stands out better than violet against the page, and the rope's
   violet stays reserved for the rope itself. */
.key-insight {
  border-left: 3px solid var(--gold);
  background: var(--bg2);
  padding: .6rem 1rem;
  border-radius: 0 8px 8px 0;
  margin-top: 1rem;
  color: var(--fg2);
  font-size: .9rem;
  line-height: 1.55;
}

.tech-note {
  border-left: 3px solid var(--gold);
  background: var(--bg2);
  padding: .6rem 1rem;
  border-radius: 0 8px 8px 0;
  margin-top: 1rem;
  color: var(--fg2);
  font-size: .9rem;
  line-height: 1.55;
}
.tech-note > strong:first-child { color: var(--gold); }

/* ── Figure / demo column ─────────────────────────────────────────────────── */
.fig {
  position: sticky;
  top: 2rem;
}

.demo-root {
  position: relative;
  background: var(--bg2);
  border: 1px solid var(--bg3);
  border-radius: 8px;
  min-height: 280px;
  overflow: hidden;
}

/* Canvases placed by demos fill the root */
.demo-root canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.fig-note {
  margin-top: .65rem;
  color: var(--fg2);
  font-size: .82rem;
  line-height: 1.5;
}

.fig-note strong { color: var(--fg); }

/* ── Demo expand affordance ───────────────────────────────────────────────── */
/* The whole thumbnail is the expand button: a transparent full-area cover (click anywhere to
   expand). It sits above the demo canvas but below the "interactive" chip (z-index 3). The ⤢
   glyph is pinned in the corner as a cue and lights up when the figure is hovered/focused. */
.demo-expand {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: transparent;
  border: 0;
  cursor: zoom-in;
}
.demo-expand-icon {
  position: absolute;
  right: .45rem;
  bottom: .45rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  font-size: 1rem;
  line-height: 1;
  color: var(--fg2);
  background: rgba(13, 17, 23, .82);
  border: 1px solid var(--bg3);
  border-radius: 6px;
  transition: color .15s, border-color .15s, background .15s;
}
.demo-expand:hover .demo-expand-icon,
.demo-expand:focus-visible .demo-expand-icon {
  color: var(--rope);
  border-color: var(--rope-dim);
  background: var(--bg3);
}
.demo-expand:focus-visible { outline: 2px solid var(--rope-dim); outline-offset: -3px; }

/* ── Interactive-figure badge ─────────────────────────────────────────────── */
/* Marks thumbnails whose expanded demo is hands-on (draggable), to set them apart
   from the figures that only animate or are static. Sits opposite the expand button. */
.demo-tag {
  position: absolute;
  left: .45rem;
  top: .45rem;
  z-index: 3;
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  font: 600 .64rem var(--mono);
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--rope);
  background: color-mix(in srgb, var(--rope) 14%, rgba(13, 17, 23, .86));
  border: 1px solid var(--rope-dim);
  border-radius: 999px;
  padding: .16rem .5rem;
  pointer-events: none;   /* never steal pointer events from the demo canvas */
  user-select: none;
}
.demo-tag-glyph { font-size: .82rem; line-height: 1; }

/* ── Expand modal ─────────────────────────────────────────────────────────── */
body.modal-open { overflow: hidden; }

.demo-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 3vmin;
  background: rgba(0, 0, 0, .74);
  animation: demo-modal-in .15s ease;
}
@keyframes demo-modal-in { from { opacity: 0; } to { opacity: 1; } }

.demo-modal-panel {
  width: min(92vw, 1040px);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: var(--bg2);
  border: 1px solid var(--bg3);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .6);
}

.demo-modal-bar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .55rem .9rem;
  border-bottom: 1px solid var(--bg3);
}
.demo-modal-title { font: 600 .95rem var(--font); color: var(--rope); }
.demo-modal-hint  { font: .72rem var(--mono); color: var(--fg2); margin-left: auto; }
.demo-modal-close {
  background: none;
  border: none;
  color: var(--fg2);
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  padding: .25rem .45rem;
  border-radius: 6px;
}
.demo-modal-close:hover,
.demo-modal-close:focus-visible { color: var(--fg); background: var(--bg3); }
.demo-modal-close:focus-visible { outline: 2px solid var(--rope); outline-offset: 2px; }

.demo-modal-root {
  width: 100%;
  aspect-ratio: 760 / 480;
  max-height: calc(92vh - 3rem);
  min-height: 0;
  border: none;
  border-radius: 0;
}
.demo-error { color: var(--warn); font: .85rem var(--mono); padding: 1rem; }

@media (max-width: 780px) {
  .demo-modal { padding: 0; }
  .demo-modal-panel { width: 100vw; height: 100dvh; max-height: none; border-radius: 0; }
  .demo-modal-root { aspect-ratio: auto; flex: 1; max-height: none; }
  .demo-modal-hint { display: none; }
}

/* ── Synopsis: hero image ─────────────────────────────────────────────────── */
.hero-fig {
  margin: 0 0 1rem;
  padding: 0;
}

.hero-img {
  display: block;
  width: 100%;
  max-width: 100%;
  border-radius: 8px;
  border: 1px solid var(--bg3);
}

.hero-fig figcaption {
  font-size: .8rem;
  color: var(--fg2);
  margin-top: .4rem;
  text-align: center;
  line-height: 1.45;
}

/* ── Glossary ─────────────────────────────────────────────────────────────── */
.prose h3 {
  font-size: clamp(1rem, 2vw, 1.25rem);
  color: var(--fg);
  margin: 1.25rem 0 .5rem;
  font-weight: 600;
}

dl.glossary {
  margin: 0 0 1rem;
  padding: 0;
}

dl.glossary dt {
  font-weight: 700;
  color: var(--rope);
  font-size: .95rem;
  margin-top: .7rem;
  font-family: var(--mono);
}

dl.glossary dd {
  margin: .2rem 0 0 1rem;
  color: var(--fg);
  font-size: .92rem;
  line-height: 1.55;
}

dl.glossary dd em {
  color: var(--fg2);
  font-style: italic;
}

/* ── Synopsis prose list ──────────────────────────────────────────────────── */
.prose ul {
  margin: .5rem 0 1rem 1.2rem;
  padding: 0;
}

.prose ol {
  margin: .5rem 0 1rem 1.25rem;
  padding: 0;
}

.prose ul li {
  font-size: clamp(.9rem, 1.6vw, 1.05rem);
  color: var(--fg);
  line-height: 1.6;
  margin-bottom: .4rem;
}

.prose ol li {
  font-size: clamp(.9rem, 1.6vw, 1.05rem);
  color: var(--fg);
  line-height: 1.6;
  margin-bottom: .45rem;
  padding-left: .15rem;
}


/* ── §Synthesis pipeline diagram ─────────────────────────────────────────── */
.pipeline-diagram {
  margin: 1.5rem 0;
}

/* Vertical stack: equal-width boxes, down-arrows centered between them.
   (Lives in the prose column, which is too narrow for three boxes side by side.) */
.pipe-row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: .4rem;
  max-width: 22rem;
}

.pipe-box {
  padding: .7rem 1rem;
  border-radius: 8px;
  border: 1.5px solid var(--rope-dim);
  background: var(--bg2);
  display: flex;
  flex-direction: column;
  gap: .3rem;
  text-align: center;
}

.pipe-box strong {
  color: var(--rope);
  font-size: .95rem;
}

.pipe-box span {
  color: var(--fg2);
  font-size: .78rem;
  line-height: 1.5;
  font-family: var(--mono);
}

.pipe-arrow {
  color: var(--rope-dim);
  font-size: 1.3rem;
  line-height: 1;
  text-align: center;
}

.pipeline-diagram figcaption {
  margin-top: .75rem;
  font-size: .82rem;
  color: var(--fg2);
  font-style: italic;
}

/* ── §Synthesis build-map ────────────────────────────────────────────────── */
.build-map {
  margin: 1rem 0 1.5rem;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: .5rem 1.5rem;
  align-items: start;
}

.build-map dt {
  grid-column: 1;
  white-space: nowrap;
}

.build-map dt a {
  font-family: var(--mono);
  font-size: .88rem;
  color: var(--rope);
  text-decoration: none;
  border-bottom: 1px solid var(--rope-dim);
}

.build-map dt a:hover {
  color: #fff;
  border-color: #fff;
}

.build-map dd {
  grid-column: 2;
  margin: 0;
  font-size: .88rem;
  color: var(--fg2);
  line-height: 1.6;
}

@media (max-width: 560px) {
  .build-map { grid-template-columns: 1fr; }
  .build-map dt { white-space: normal; }
}

/* ── Jargon term spans ────────────────────────────────────────────────────── */
.term {
  text-decoration: underline dotted currentColor;
  text-underline-offset: 3px;
  cursor: help;
  color: inherit;
  outline-offset: 2px;
}

.term:focus-visible {
  outline: 2px solid var(--rope);
  border-radius: 2px;
}

/* ── Term reference popover ──────────────────────────────────────────────── */
.term-popover {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 9999;
  max-width: min(340px, calc(100vw - 24px));
  background: var(--bg2);
  border: 1px solid var(--rope-dim);
  border-radius: 8px;
  padding: .65rem .9rem .6rem;
  box-shadow: 0 4px 20px rgba(0,0,0,.55);
  pointer-events: none;   /* becomes interactive on mouse entry via JS */
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .15s ease, transform .15s ease;
  /* hide when not .--visible (avoids flicker on first creation) */
  visibility: hidden;
}

.term-popover--visible {
  pointer-events: auto;
  opacity: 1;
  transform: translateY(0);
  visibility: visible;
}

/* Slight offset direction reversal when flipped above */
.term-popover--above {
  transform: translateY(-4px);
}
.term-popover--above.term-popover--visible {
  transform: translateY(0);
}

.term-popover-def {
  margin: 0 0 .45rem;
  font-size: .82rem;
  color: var(--fg);
  line-height: 1.5;
}

.term-popover-link {
  display: inline-block;
  font-size: .76rem;
  color: var(--rope);
  text-decoration: none;
  border-bottom: 1px solid var(--rope-dim);
  line-height: 1.3;
}

.term-popover-link:hover {
  color: #fff;
  border-color: #fff;
}

/* ── Reduced motion ──────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ── Page footer ─────────────────────────────────────────────────────────── */
.page-foot {
  max-width: var(--content-w);
  margin: 3rem auto 0;
  padding: 2.5rem var(--gap) 4rem;
  border-top: 1px solid var(--line);
  color: var(--fg2);
  font-family: var(--mono);
  font-size: .8rem;
  text-align: center;
}
.page-foot a {
  color: var(--rope);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--rope) 40%, transparent);
}
.page-foot a:hover { border-bottom-color: var(--rope); }
