/* =====================================================================
   STADIA GLOBAL — motion system
   Reusable on any page: link this file + js/motion.js, then add the
   data attributes below to your markup. Nothing animates until
   motion.js adds .motion-ok to <html>, so content is never hidden
   if JavaScript fails, and nothing animates for visitors who have
   "reduce motion" switched on.

   ATTRIBUTES
   data-reveal              fade + rise 20px when scrolled into view
   data-reveal="fade"       fade only, no movement
   data-reveal="scale"      fade + settle from 1.04 scale (images)
   data-reveal-stagger      children with [data-reveal] enter one after another
   data-count="600"         count up from 0 to the value when visible
                            optional: data-count-decimals="2"
   data-parallax            media scales 1.08 → 1 as its section crosses the screen
   data-media-hover         media brightens and zooms 2% on hover

   SCROLL SCENES (scrubbed: they play forward and backward with the scroll)
   data-pin                 tall section whose .pin-stage sticks while you scroll
                            through it; set the length with style="--pin-h: 250vh"
   data-scrub="0.2 0.6"     receives --t (0 → 1) between those points of the
                            scene's progress. Inside a pin = pin progress;
                            outside = the element's own trip through the screen
   data-words               (with data-scrub) words light up one by one
   data-scrub-count="99.42" (with data-scrub) number follows the scroll
   data-flood               (with data-scrub) background blends from
                            --flood-from to --flood-to
   data-tumble="4500"       digits roll like an odometer as you scroll, and
                            roll back when you scroll up. Optional data-scrub
                            range (default "0.06 0.42") and data-decimals
   data-wipe="#00d959"      link: colored panel wipes the screen, then the next
                            page opens and the panel lifts away.
                            optional data-wipe-label="Asher’s Harvest"

   Every scrubbed style is written as var(--t, X) where X is the still
   frame shown when motion is off, so pages read correctly either way.
   ===================================================================== */

:root {
  --ease-dossier: cubic-bezier(0.16, 1, 0.3, 1);
  --dur-reveal: 0.8s;
  --dur-theme: 0.6s;
  --dur-hover: 0.7s;
  --dur-fast: 0.15s;
  --reveal-distance: 20px;
  --stagger-step: 90ms;
}

/* ---------- Scroll reveals ---------- */
.motion-ok [data-reveal] {
  opacity: 0;
  transform: translateY(var(--reveal-distance));
  transition:
    opacity var(--dur-reveal) var(--ease-dossier),
    transform var(--dur-reveal) var(--ease-dossier);
  transition-delay: calc(var(--i, 0) * var(--stagger-step));
  will-change: opacity, transform;
}
.motion-ok [data-reveal="fade"] { transform: none; }
.motion-ok [data-reveal="scale"] { transform: scale(1.04); transform-origin: center; }
.motion-ok [data-reveal].is-revealed { opacity: 1; transform: none; will-change: auto; }

/* Image frames reveal their contents, not the frame line itself */
.motion-ok .frame[data-reveal="scale"] { opacity: 1; transform: none; }
.motion-ok .frame[data-reveal="scale"] > img,
.motion-ok .frame[data-reveal="scale"] > video {
  opacity: 0; transform: scale(1.04);
  transition: opacity 1.1s var(--ease-dossier), transform 1.4s var(--ease-dossier);
}
.motion-ok .frame[data-reveal="scale"].is-revealed > img,
.motion-ok .frame[data-reveal="scale"].is-revealed > video { opacity: 1; transform: none; }

/* ---------- Rule lines draw in (spec rail, table head, chapter borders) ---------- */
.motion-ok [data-rule] { position: relative; border-bottom-color: transparent; }
.motion-ok [data-rule]::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 1px;
  background: var(--rule-color, var(--color-calibration-gray));
  transform: scaleX(0); transform-origin: left;
  transition: transform 1.1s var(--ease-dossier);
  transition-delay: calc(var(--i, 0) * var(--stagger-step));
}
.motion-ok [data-rule].is-revealed::after { transform: scaleX(1); }

/* ---------- Parallax scale-down on cinematic media ---------- */
.motion-ok [data-parallax] > img,
.motion-ok [data-parallax] > video {
  transform: scale(var(--parallax-scale, 1.08));
  transition: transform 0.1s linear, opacity var(--dur-hover) var(--ease-dossier), filter var(--dur-hover) var(--ease-dossier);
}

/* ---------- Media hover (FigureAI-style evidence frames) ---------- */
[data-media-hover] > img,
[data-media-hover] > video {
  filter: grayscale(1);
  opacity: 0.6;
  transition: opacity var(--dur-hover) var(--ease-dossier), filter var(--dur-hover) var(--ease-dossier), transform var(--dur-hover) var(--ease-dossier);
}
[data-media-hover]:hover > img,
[data-media-hover]:hover > video { opacity: 0.85; }
.motion-ok [data-media-hover]:hover > img,
.motion-ok [data-media-hover]:hover > video { transform: scale(calc(var(--parallax-scale, 1) + 0.02)); }
[data-media-hover].is-paused > video { opacity: 0.3; }

/* Hover play/pause overlay (shown only when a video is present) */
.media-overlay {
  position: absolute; inset: 0; z-index: 1;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  border: 0; padding: 0; cursor: pointer; color: var(--color-lab-white);
  opacity: 0; transition: opacity 0.3s var(--ease-dossier);
}
.media-overlay:hover, .media-overlay:focus-visible, .is-paused .media-overlay { opacity: 1; }
.media-overlay__pill {
  display: inline-flex; align-items: center; gap: var(--spacing-8);
  padding: var(--spacing-16) var(--spacing-24);
  border: 1px solid var(--color-lab-white); border-radius: var(--radius-buttons);
  background: rgba(0, 0, 0, 0.8);
  font-family: var(--font-display); font-size: 12px; letter-spacing: var(--tracking-nav); text-transform: uppercase;
}
.media-overlay__dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.motion-ok .media-overlay__dot { animation: dossier-ping 1.4s var(--ease-dossier) infinite; }
.is-paused .media-overlay__dot { animation: none; border-radius: 0; }
@keyframes dossier-ping {
  0% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.6); }
  100% { box-shadow: 0 0 0 10px rgba(255, 255, 255, 0); }
}

/* ---------- Chapter surface switch (white ↔ black) ---------- */
body { transition: background-color var(--dur-theme) ease; }
.site-header,
.site-header * { transition-duration: var(--dur-theme); transition-timing-function: ease; }

/* ---------- Table rows (region switch) ---------- */
.motion-ok .results tbody tr {
  animation: row-in 0.5s var(--ease-dossier) both;
  animation-delay: calc(var(--i, 0) * 50ms);
}
@keyframes row-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Loader (first visit per session) ---------- */
.loader { display: none; }
html.is-loading .loader, .loader.is-done {
  position: fixed; inset: 0; z-index: 200;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + var(--spacing-32)) var(--gutter, 32px) calc(env(safe-area-inset-bottom, 0px) + var(--spacing-32));
  background: var(--color-lab-white); color: var(--color-figure-black);
  transition: opacity var(--dur-theme) var(--ease-dossier), visibility var(--dur-theme);
}
.loader.is-done { opacity: 0; visibility: hidden; }
.loader__row { display: flex; justify-content: space-between; align-items: center; gap: var(--spacing-16); }
.loader__label { font-family: var(--font-display); font-size: 12px; letter-spacing: var(--tracking-nav); text-transform: uppercase; color: var(--color-machine-gray); }
.loader__meter { width: min(360px, 100%); display: flex; flex-direction: column; gap: var(--spacing-16); }
.loader__track { height: 1px; background: var(--color-calibration-gray); overflow: hidden; }
.loader__bar { height: 100%; width: 100%; background: var(--color-figure-black); transform: scaleX(0); transform-origin: left; transition: transform 0.3s ease-out; }
html.is-loading, html.is-loading body { overflow: hidden; }

/* ---------- Respect reduced motion everywhere ---------- */
@media (prefers-reduced-motion: reduce) {
  html:not(.motion-force) [data-media-hover] > img, html:not(.motion-force) [data-media-hover] > video { transition: none; }
  html:not(.motion-force) .loader { display: none; }
  html:not(.motion-force) .wipe { display: none; }
}

/* =====================================================================
   SCROLL SCENES
   ===================================================================== */
[data-pin] { position: relative; }
.pin-stage { position: relative; overflow: hidden; min-height: 100vh; min-height: 100svh; }
.motion-ok [data-pin] { height: var(--pin-h, 250vh); }
.motion-ok [data-pin] > .pin-stage { position: sticky; top: 0; height: 100vh; height: 100svh; }

/* Words that light up as you scroll */
[data-words] .w {
  opacity: clamp(0.16, calc(var(--t, 1) * var(--n, 1) - var(--w, 0) + 0.4), 1);
  transition: opacity 0.12s linear;
}

/* Background color flood */
[data-flood] {
  background-color: color-mix(in srgb, var(--flood-to) calc(var(--t, 1) * 100%), var(--flood-from));
}

/* Generic scrub helpers — combine on any element inside a scene */
.scrub-fade-in  { opacity: var(--t, 1); }
.scrub-fade-out { opacity: calc(1 - var(--t, 0)); }
.scrub-rise     { opacity: var(--t, 1); transform: translateY(calc((1 - var(--t, 1)) * 40px)); }
.scrub-lift-out { opacity: calc(1 - var(--t, 0)); transform: translateY(calc(var(--t, 0) * -60px)); }
.scrub-grow     { clip-path: inset(calc((1 - var(--t, 1)) * 10%) calc((1 - var(--t, 1)) * 14%) round var(--grow-radius, 28px)); }
.scrub-line     { transform: scaleX(var(--t, 1)); transform-origin: left; }

/* Horizontal track inside a pin: slides left as you scroll down */
.h-track { display: flex; width: max-content; will-change: transform; }
/* Slides only as far as the track overflows the screen (never moves right if it already fits) */
.motion-ok .h-track { transform: translateX(calc(var(--t, 0) * min(0px, 100vw - 100%))); }
html:not(.motion-ok) .h-track-wrap { overflow-x: auto; }

/* =====================================================================
   PAGE WIPE (between pages)
   ===================================================================== */
.wipe {
  position: fixed; inset: 0; z-index: 300;
  display: flex; align-items: center; justify-content: center;
  background: var(--wipe-color, #000);
  color: var(--wipe-ink, #fff);
  transform: translateY(100%);
  pointer-events: none;
  font-family: var(--font-display, system-ui, sans-serif);
  font-size: clamp(28px, 5vw, 64px); letter-spacing: -0.02em; text-transform: uppercase;
}
.wipe.is-covering { transform: translateY(0); transition: transform 0.55s cubic-bezier(0.76, 0, 0.24, 1); pointer-events: auto; }
html.wipe-in .wipe { transform: translateY(0); }
.wipe.is-leaving { transform: translateY(-100%); transition: transform 0.7s cubic-bezier(0.76, 0, 0.24, 1); }
.wipe__label { opacity: 0; transform: translateY(16px); transition: opacity .3s ease .2s, transform .5s var(--ease-dossier) .2s; }
.wipe.is-covering .wipe__label, html.wipe-in .wipe__label { opacity: 1; transform: none; }
.wipe.is-leaving .wipe__label { opacity: 0; transition-delay: 0s; }
/* Cover the page before first paint when arriving through a wipe */
html.wipe-in::after { content: ""; position: fixed; inset: 0; z-index: 299; background: var(--wipe-color, #000); }

/* =====================================================================
   TUMBLING DIGITS (data-tumble)
   ===================================================================== */
.tumble { display: inline-flex; align-items: flex-start; height: 1.11em; line-height: 1.11em; overflow: hidden; vertical-align: bottom; font-variant-numeric: tabular-nums; }
.tumble__col { display: block; height: 1.11em; overflow: hidden; }
.tumble__strip {
  display: flex; flex-direction: column;
  transform: translateY(calc(var(--t, 1) * (var(--s) * 10 + var(--d)) * -1.11em));
  transition: transform 0.45s cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform;
}
.tumble__strip > span { display: block; height: 1.11em; text-align: center; }
.tumble__sep { display: block; }
@media (prefers-reduced-motion: reduce) { html:not(.motion-force) .tumble__strip { transition: none; } }
