/* ============================================================
   When did we know? — Observatory Logbook
   Design system + scrollytelling layout
   ============================================================ */

@import url("../assets/fonts/fonts.css");

/* ---- Tokens ------------------------------------------------ */
:root {
  /* Type */
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --font-body: "Source Serif 4", Georgia, serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;

  /* Light — archival paper */
  --paper: #f4f1e9;          /* page plane */
  --surface: #fbf9f3;        /* chart / card surface */
  --surface-sunk: #efe9dc;   /* insets, wells */
  --ink: #191712;            /* primary text */
  --ink-2: #55503f;          /* secondary text */
  --ink-3: #8a8371;          /* muted / axis */
  --rule: #ded6c3;           /* hairlines, gridlines */
  --rule-strong: #c7bda3;    /* axis, baseline */
  --accent: #2f6f5e;         /* oxidized copper — chrome accent */
  --accent-soft: #dfeae4;
  --gold: #b07a1e;           /* milestone / logbook amber */

  /* Data — Wong-style categorical (light) */
  --s1: #2a78d6;  /* blue */
  --s2: #1baf7a;  /* aqua */
  --s3: #d98a00;  /* yellow-amber (darkened for legibility) */
  --s4: #178a3a;  /* green */
  --s5: #6a4bc4;  /* violet */
  --s6: #e34948;  /* red */
  --s7: #d8548a;  /* magenta */
  --s8: #eb6834;  /* orange */

  --shadow: 0 1px 2px rgba(25, 23, 18, .05), 0 8px 30px rgba(25, 23, 18, .06);
  --maxw: 74rem;
  --step-w: 30rem;

  color-scheme: light;
}

/* On wide screens, grow the shared content width; the step column (--step-w)
   stays fixed, so all the extra space goes to the sticky chart. .wrap shares
   --maxw, keeping chapter headers aligned with the scrolly block. */
@media (min-width: 1500px) { :root { --maxw: 88rem; } }
@media (min-width: 1800px) { :root { --maxw: 98rem; } }

:root[data-theme="dark"] {
  --paper: #101418;
  --surface: #171c21;
  --surface-sunk: #0b0e11;
  --ink: #f2efe6;
  --ink-2: #b9b4a4;
  --ink-3: #837d6d;
  --rule: #262d33;
  --rule-strong: #39424a;
  --accent: #57b79f;
  --accent-soft: #16302a;
  --gold: #e0a94a;

  --s1: #3987e5;
  --s2: #22c58a;
  --s3: #e0a53a;
  --s4: #2ca84c;
  --s5: #9c8bef;
  --s6: #ec6a69;
  --s7: #e07aa8;
  --s8: #f07b4b;

  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 34px rgba(0,0,0,.4);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #101418; --surface: #171c21; --surface-sunk: #0b0e11;
    --ink: #f2efe6; --ink-2: #b9b4a4; --ink-3: #837d6d;
    --rule: #262d33; --rule-strong: #39424a;
    --accent: #57b79f; --accent-soft: #16302a; --gold: #e0a94a;
    --s1:#3987e5; --s2:#22c58a; --s3:#e0a53a; --s4:#2ca84c;
    --s5:#9c8bef; --s6:#ec6a69; --s7:#e07aa8; --s8:#f07b4b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 34px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}

/* ---- Reset-ish -------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: clamp(1.02rem, 0.55vw + 0.9rem, 1.19rem);
  line-height: 1.62;
  font-feature-settings: "onum" 1;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: clip;
}
img { max-width: 100%; display: block; }
a { color: inherit; }
::selection { background: var(--accent); color: var(--paper); }

/* ---- Top progress + controls ------------------------------ */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 60;
  display: flex; align-items: center; gap: .9rem;
  padding: .55rem clamp(.9rem, 3vw, 2rem);
  pointer-events: none;
}
.topbar__mark {
  pointer-events: auto;
  font-family: var(--font-mono); font-size: .7rem; letter-spacing: .16em;
  text-transform: uppercase; color: var(--ink-2);
  text-decoration: none; white-space: nowrap;
}
.topbar__mark b { color: var(--accent); font-weight: 500; }
.topbar__spacer { flex: 1; }
.theme-toggle {
  pointer-events: auto;
  font-family: var(--font-mono); font-size: .68rem; letter-spacing: .08em;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  color: var(--ink-2); border: 1px solid var(--rule);
  border-radius: 999px; padding: .3rem .7rem; cursor: pointer;
  backdrop-filter: blur(6px);
  transition: color .2s, border-color .2s;
}
.theme-toggle:hover { color: var(--ink); border-color: var(--rule-strong); }
.scroll-progress {
  position: fixed; top: 0; left: 0; height: 2px; z-index: 61;
  width: var(--p, 0%); background: var(--accent);
  transition: width .1s linear;
}
/* Chapter markers riding the progress bar (built by app.js) */
.chapter-rail { position: fixed; inset: 0 0 auto 0; height: 0; z-index: 62; }
.chapter-rail__mark {
  /* Hit box ends at 8px so it never sits over the topbar controls (their hit
     boxes start at y≈8.8px); the grown tick just overflows it, unclipped. */
  position: absolute; top: 0; width: 22px; height: 8px;
  transform: translateX(-50%);
  display: flex; justify-content: center; align-items: flex-start;
  text-decoration: none; border-radius: 2px;
}
.chapter-rail__tick {
  width: 2px; height: 8px;
  background: var(--ink-3);
  transition: height .18s ease, background-color .18s;
}
.chapter-rail__mark.is-passed .chapter-rail__tick { background: var(--accent); }
.chapter-rail__mark:hover .chapter-rail__tick,
.chapter-rail__mark:focus-visible .chapter-rail__tick,
.chapter-rail__mark.is-active .chapter-rail__tick { background: var(--accent); height: 12px; }
.chapter-rail__tip {
  position: absolute; top: 20px; left: 0;
  width: max-content; max-width: min(320px, 64vw);
  padding: .32rem .6rem;
  font-family: var(--font-mono); font-size: .64rem; line-height: 1.5; letter-spacing: .03em;
  color: var(--ink-2);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--rule); border-radius: 4px;
  box-shadow: 0 4px 14px rgba(25,23,18,.14);
  backdrop-filter: blur(6px);
  visibility: hidden; opacity: 0;
  transition: opacity .15s ease, visibility 0s linear .15s;
  pointer-events: none;
}
/* Hover reveal only on devices that hover — touch taps navigate immediately
   and Android's sticky :hover would otherwise pin the tip over the header. */
@media (hover: hover) {
  .chapter-rail__mark:hover .chapter-rail__tip { visibility: visible; opacity: 1; transition: opacity .15s ease; }
}
.chapter-rail__mark:focus-visible .chapter-rail__tip {
  visibility: visible; opacity: 1; transition: opacity .15s ease;
}
/* Escape suppresses tooltips until the pointer leaves the rail (app.js). */
.chapter-rail.tips-off .chapter-rail__tip { visibility: hidden !important; opacity: 0 !important; }
/* Rail clicks move focus here programmatically; no viewport-sized ring. */
.chapter-open:focus { outline: none; }

/* ---- Section rhythm --------------------------------------- */
section { position: relative; }
.wrap { max-width: var(--maxw); margin-inline: auto; padding-inline: clamp(1.1rem, 4vw, 2.4rem); }
.measure { max-width: 34rem; }

/* =====================================================================
   HERO
   ===================================================================== */
.hero {
  min-height: 100svh;
  display: grid; grid-template-rows: 1fr auto;
  position: relative; overflow: hidden;
  border-bottom: 1px solid var(--rule);
}
#hero-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.hero__grad {
  position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(120% 80% at 50% -10%, transparent 40%, var(--paper) 92%);
}
.hero__inner {
  grid-row: 1; align-self: center; position: relative; z-index: 2;
  padding-top: 4rem;
}
.hero__eyebrow {
  font-family: var(--font-mono); font-size: .74rem; letter-spacing: .28em;
  text-transform: uppercase; color: var(--accent); margin: 0 0 1.4rem;
}
.hero h1 {
  font-family: var(--font-display);
  font-optical-sizing: auto;
  font-weight: 500;
  font-size: clamp(2.9rem, 8.5vw, 7.2rem);
  line-height: .98; letter-spacing: -0.02em;
  margin: 0; max-width: 15ch;
  font-variation-settings: "SOFT" 0, "WONK" 1;
}
.hero h1 em { font-style: italic; color: var(--accent); font-variation-settings: "WONK" 1; }
.hero__sub {
  margin: 1.8rem 0 0; max-width: 40ch;
  font-size: clamp(1.1rem, 1.4vw, 1.35rem);
  color: var(--ink-2);
}
.hero__foot {
  grid-row: 2; position: relative; z-index: 2;
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 1rem; padding-bottom: 1.6rem; flex-wrap: wrap;
}
.hero__byline { font-family: var(--font-mono); font-size: .72rem; letter-spacing: .05em; color: var(--ink-3); max-width: 44ch; }
.scroll-hint {
  font-family: var(--font-mono); font-size: .7rem; letter-spacing: .18em;
  text-transform: uppercase; color: var(--ink-2);
  display: inline-flex; align-items: center; gap: .5rem;
}
.scroll-hint span { animation: bob 1.8s ease-in-out infinite; }
@keyframes bob { 0%,100%{transform:translateY(0)} 50%{transform:translateY(4px)} }
@media (prefers-reduced-motion: reduce){ .scroll-hint span { animation:none } }

/* =====================================================================
   INTERSTITIAL / PROLOGUE — centered narrative beats
   ===================================================================== */
.beats { padding: clamp(5rem, 14vh, 11rem) 0; }
.beats__inner { max-width: 40rem; margin-inline: auto; }
.beat {
  font-family: var(--font-display); font-weight: 400;
  font-size: clamp(1.4rem, 2.6vw, 2rem); line-height: 1.34;
  letter-spacing: -0.01em;
  opacity: .28; transition: opacity .5s ease;
  margin: 0 0 clamp(2rem, 6vh, 4rem);
}
.beat.is-active { opacity: 1; }
.beat:last-child { margin-bottom: 0; }
.beat em { font-style: italic; color: var(--accent); }
.beat__note {
  display: block; font-family: var(--font-mono);
  font-size: .74rem; line-height: 1.5; letter-spacing: .02em;
  color: var(--ink-3); text-transform: none; margin-top: 1.1rem;
}

/* =====================================================================
   CHAPTER OPENER
   ===================================================================== */
.chapter-open {
  padding: clamp(4.5rem, 13vh, 9rem) 0 clamp(2rem, 6vh, 3.5rem);
  border-top: 1px solid var(--rule);
}
.chapter-open__num {
  font-family: var(--font-mono); font-size: .8rem; letter-spacing: .2em;
  color: var(--accent); display: inline-flex; gap: .8rem; align-items: baseline;
}
.chapter-open__kicker { color: var(--ink-3); text-transform: uppercase; }
.chapter-open h2 {
  font-family: var(--font-display); font-weight: 500;
  font-size: clamp(2.2rem, 5.2vw, 4rem); line-height: 1.02;
  letter-spacing: -0.02em; margin: 1.1rem 0 0; max-width: 18ch;
  font-variation-settings: "SOFT" 0, "WONK" 0;
}
.chapter-open__stand {
  margin: 1.5rem 0 0; max-width: 40ch; color: var(--ink-2);
  font-size: clamp(1.08rem, 1.3vw, 1.28rem);
}

/* =====================================================================
   SCROLLYTELLING — sticky graphic + stepped text
   ===================================================================== */
.scrolly { position: relative; }
.scrolly__inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--step-w);
  gap: clamp(1rem, 4vw, 3.5rem);
  max-width: var(--maxw); margin-inline: auto;
  padding-inline: clamp(1.1rem, 4vw, 2.4rem);
  align-items: start;
}
/* graphic sticky in the LEFT column (wider) */
.scrolly__graphic {
  position: sticky; top: 0;
  height: 100svh;
  display: flex; align-items: center; justify-content: center;
  grid-column: 1;
}
.figure-frame {
  width: 100%; height: min(84svh, 720px);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow);
  position: relative; overflow: hidden;
  display: flex; flex-direction: column;
}
.figure-frame__head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 1rem; padding: .8rem 1.1rem .4rem;
  border-bottom: 1px solid var(--rule);
}
.figure-frame__title {
  font-family: var(--font-mono); font-size: .72rem; letter-spacing: .06em;
  text-transform: uppercase; color: var(--ink-2); margin: 0;
}
.figure-frame__tag {
  font-family: var(--font-mono); font-size: .66rem; letter-spacing: .08em;
  color: var(--accent);
}
.figure-frame__body { flex: 1; position: relative; min-height: 0; }
.figure-frame__body svg { display: block; width: 100%; height: 100%; }
.figure-frame__source {
  font-family: var(--font-mono); font-size: .6rem; line-height: 1.35; letter-spacing: .02em;
  color: var(--ink-3); padding: .4rem 1.1rem .5rem; border-top: 1px solid var(--rule);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

.scrolly__steps { grid-column: 2; padding: 42svh 0; }
.step {
  min-height: 62svh;
  display: flex; flex-direction: column; justify-content: center;
  margin-bottom: 8svh;
}
.step__card {
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border: 1px solid var(--rule);
  border-left: 2px solid var(--rule);
  border-radius: 8px;
  padding: 1.3rem 1.4rem;
  box-shadow: var(--shadow);
  opacity: .5; transform: translateY(8px);
  transition: opacity .45s ease, transform .45s ease, border-color .45s ease;
  backdrop-filter: blur(3px);
}
.step.is-active .step__card {
  opacity: 1; transform: none; border-left-color: var(--accent);
}
.step__body { margin: 0; }
.step__body em { color: var(--accent); font-style: italic; }
.step__note {
  display: block; margin-top: 1rem; padding-top: .8rem;
  border-top: 1px dotted var(--rule-strong);
  font-family: var(--font-mono); font-size: .72rem; line-height: 1.5;
  letter-spacing: .01em; color: var(--ink-3);
}

/* chart-internal text conventions (set in JS but themed here) */
.viz text { font-family: var(--font-mono); }
.viz .ax-label { fill: var(--ink-3); font-size: 10px; letter-spacing: .02em; }
.viz .ax-title { fill: var(--ink-2); font-size: 11px; }
.viz .grid line { stroke: var(--rule); stroke-width: 1; shape-rendering: crispEdges; }
.viz .grid path { display: none; }
.viz .domain { stroke: var(--rule-strong); }
.viz .tick line { stroke: var(--rule-strong); }
.viz .series-label { font-size: 11.5px; font-family: var(--font-mono); }
.viz .anno-line { stroke: var(--ink-3); stroke-dasharray: 3 3; stroke-width: 1; }
.viz .anno-text { fill: var(--ink-2); font-size: 10.5px; }
.viz .anno-year { fill: var(--gold); font-size: 11px; }
.viz .panel-title { fill: var(--ink); font-family: var(--font-display); font-size: 14px; font-weight: 500; }

/* hover tooltip */
.viz-tip {
  z-index: 5; background: var(--ink); color: var(--paper);
  font-family: var(--font-mono); font-size: .7rem; line-height: 1.4;
  padding: .35rem .5rem; border-radius: 5px; transform: translate(-50%, -100%);
  box-shadow: 0 4px 14px rgba(0,0,0,.25); white-space: nowrap;
}
.viz-tip .tip-x { color: var(--gold); font-weight: 500; margin-bottom: .1rem; }
.viz-tip .tip-row b { font-size: .8rem; }

/* legend chip row under charts */
.viz-legend {
  position: absolute; left: 1.1rem; top: .7rem; z-index: 3;
  display: flex; flex-wrap: wrap; gap: .3rem .9rem; max-width: 80%;
  pointer-events: none;
}
.viz-legend .lg {
  display: inline-flex; align-items: center; gap: .38rem;
  font-family: var(--font-mono); font-size: .66rem; color: var(--ink-2);
  opacity: 0; transform: translateY(-3px); transition: opacity .4s, transform .4s;
}
.viz-legend .lg.on { opacity: 1; transform: none; }
.viz-legend .lg i { width: 14px; height: 3px; border-radius: 2px; background: currentColor; display: inline-block; }
.viz-legend .lg.dash i { background: repeating-linear-gradient(90deg, currentColor 0 4px, transparent 4px 7px); }

/* =====================================================================
   PHOTO INTERLUDE
   ===================================================================== */
.interlude {
  position: relative; min-height: 92svh;
  display: grid; align-items: end;
  background: var(--surface-sunk);
  border-block: 1px solid var(--rule);
  overflow: hidden;
}
.interlude__img { position: absolute; inset: 0; }
.interlude__img img { width: 100%; height: 100%; object-fit: cover; filter: saturate(.92); }
/* Paired (before/after) interlude: two frames share the bleed. */
.interlude--pair .interlude__img { display: flex; }
.interlude--pair .interlude__img img { flex: 1 1 50%; min-width: 0; }
@media (max-width: 560px) {
  .interlude--pair .interlude__img { flex-direction: column; }
  .interlude--pair .interlude__img img { flex: 1 1 50%; min-height: 0; }
  .interlude__cap { padding-bottom: 2.6rem; } /* keep clear of the credit line */
}
.interlude__scrim {
  position: absolute; inset: 0;
  background: linear-gradient(0deg, rgba(8,10,12,.82) 0%, rgba(8,10,12,.30) 45%, rgba(8,10,12,.12) 100%);
}
.interlude__cap {
  position: relative; z-index: 2; color: #f4f1e9;
  max-width: 40rem; padding: clamp(1.4rem, 5vw, 3.4rem);
}
.interlude__cap h3 {
  font-family: var(--font-display); font-weight: 500;
  font-size: clamp(1.5rem, 3.6vw, 2.6rem); line-height: 1.1; margin: 0 0 .7rem;
}
.interlude__cap p { margin: 0; color: #ded9cc; max-width: 40ch; }
.interlude__credit {
  position: absolute; right: .8rem; bottom: .6rem; z-index: 2;
  font-family: var(--font-mono); font-size: .58rem; letter-spacing: .02em;
  color: rgba(244,241,233,.62); text-align: right; max-width: 60%;
}

/* small inline archival image inside a step */
.step__figure { margin: 0 0 1.1rem; }
/* height:auto — the width/height ATTRIBUTES (set from CT_IMGMETA) must only
   feed the intrinsic aspect ratio, not fix the rendered height. */
.step__figure img { width: 100%; height: auto; border-radius: 6px; border: 1px solid var(--rule); }
.step__figure figcaption {
  font-family: var(--font-mono); font-size: .62rem; color: var(--ink-3);
  margin-top: .4rem; letter-spacing: .01em;
}

/* =====================================================================
   MASTER TIMELINE (convergence)
   ===================================================================== */
.timeline-wrap { padding-block: clamp(1rem, 4vh, 2rem) clamp(4rem, 10vh, 7rem); }
#master-timeline { width: 100%; }

/* =====================================================================
   COLOPHON
   ===================================================================== */
.colophon {
  border-top: 1px solid var(--rule);
  padding-block: clamp(4rem, 10vh, 7rem);
  background: var(--surface-sunk);
}
.colophon h2 {
  font-family: var(--font-display); font-weight: 500;
  font-size: clamp(1.8rem, 4vw, 2.8rem); margin: 0 0 1.4rem; letter-spacing: -.01em;
}
.colophon__grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
  gap: 2rem 2.4rem; margin-top: 2.5rem;
}
.colophon__grid h4 {
  font-family: var(--font-mono); font-size: .72rem; letter-spacing: .12em;
  text-transform: uppercase; color: var(--accent); margin: 0 0 .7rem;
}
.colophon__grid p, .colophon__grid li { color: var(--ink-2); font-size: .92rem; line-height: 1.5; }
.colophon__grid ul { margin: 0; padding: 0; list-style: none; }
.colophon__grid li { padding: .18rem 0; }
.colophon a { color: var(--accent); text-underline-offset: 2px; }
.credits-list { columns: 2; column-gap: 2rem; font-size: .78rem; }
.credits-list li { break-inside: avoid; color: var(--ink-3); font-family: var(--font-mono); font-size: .68rem; padding: .12rem 0; }

/* =====================================================================
   RESPONSIVE
   ===================================================================== */
@media (max-width: 900px) {
  :root { --step-w: 0; }
  .scrolly__inner { display: block; }
  /* Give the fixed header an opaque bar so the pinned chart clears it. */
  .topbar { background: var(--paper); border-bottom: 1px solid var(--rule); }
  /* Chart pins below the header as an opaque panel; cards scroll through the
     lower half and tuck under it. z-index keeps the chart above outgoing cards. */
  .scrolly__graphic {
    position: sticky; top: 2.6rem; height: 47svh; z-index: 3;
    display: flex; align-items: flex-start; justify-content: center;
    padding-top: .3rem;
    background: var(--paper);
    border-bottom: 1px solid var(--rule);
  }
  .figure-frame { height: 45svh; box-shadow: 0 6px 16px rgba(25,23,18,.10); }
  .scrolly__steps { position: relative; z-index: 1; padding: 6svh 0 10svh; }
  .step {
    min-height: 58svh; margin-bottom: 0;
    display: flex; align-items: center; justify-content: center;
  }
  .step__card {
    opacity: 1; transform: none; width: 100%;
    background: var(--surface); backdrop-filter: none;
    box-shadow: 0 6px 22px rgba(25,23,18,.16);
  }
  .step.is-active .step__card { border-left-color: var(--accent); }
  .step__figure { display: none; } /* keep mobile cards compact; the chart is the focus */
  .hero h1 { font-size: clamp(2.6rem, 13vw, 4.6rem); }
  .credits-list { columns: 1; }
  /* Keep rail ticks clear of the opaque header's title text. */
  .chapter-rail__tick { height: 6px; }
  .chapter-rail__mark:hover .chapter-rail__tick,
  .chapter-rail__mark:focus-visible .chapter-rail__tick,
  .chapter-rail__mark.is-active .chapter-rail__tick { height: 9px; }
  /* Enlarge the toggle's touch area so tap adjustment near marks 6–7 prefers
     it over the tiny rail targets above. */
  .theme-toggle { position: relative; }
  .theme-toggle::after { content: ""; position: absolute; left: -8px; right: -8px; top: -9px; bottom: -6px; }
}
@media (max-width: 560px) {
  .figure-frame__source { font-size: .55rem; }
  .colophon__grid { grid-template-columns: 1fr; }
}

/* print / forced-colors niceties */
@media print { .topbar, .scroll-progress, .chapter-rail, .scroll-hint { display: none; } }
@media (forced-colors: active) {
  /* background-color is otherwise forced to Canvas — keep ticks visible. */
  .chapter-rail__tick { background: LinkText; }
  .chapter-rail__mark.is-passed .chapter-rail__tick,
  .chapter-rail__mark.is-active .chapter-rail__tick { background: Highlight; }
  .scroll-progress { background: Highlight; }
}
