/* ==========================================================================
   Motion primitives (driven by js/motion.js). Transform + opacity only on anything scroll-linked; clip-path only
   for the one-shot clip / scan reveals.
   Hide-before-reveal applies ONLY when <html> has .js (set inline in <head>)
   and the reader has not asked for reduced motion. A fallback in <head>
   removes .js if motion.js never boots, so nothing can stay invisible.
   See DESIGN.md "Motion recipes".
   ========================================================================== */

@media (prefers-reduced-motion: no-preference) {
  /* ---- 1. Hidden states ---- */
  html.js [data-reveal]:not(.is-in):not([data-reveal="split"], [data-reveal="clip"], [data-reveal="scan"]),
  html.js [data-reveal-stagger]:not(.is-in) > :not([data-reveal]) { opacity: 0; }

  html.js [data-reveal=""]:not(.is-in),
  html.js [data-reveal="up"]:not(.is-in),
  html.js [data-reveal-stagger]:not(.is-in) > :not([data-reveal]) { translate: 0 var(--reveal-distance, var(--reveal-rise)); }

  html.js [data-reveal="scale"]:not(.is-in) { scale: 0.94; }

  /* draw: a rule, bar or line grows from its left end (transform only) */
  html.js [data-reveal="draw"]:not(.is-in) { scale: 0 1; }
  html.js [data-reveal="draw"] { transform-origin: left center; }

  /* clip: a curtain wipe upwards; the photo inside settles from a slight zoom */
  html.js [data-reveal="clip"]:not(.is-in) { clip-path: inset(100% 0 0 0); }
  html.js :is([data-reveal="clip"], [data-reveal="scan"]):not(.is-done) > :is(img, video, picture) { transition: scale 1.6s var(--ease-out) var(--reveal-delay, 0ms); }
  html.js :is([data-reveal="clip"], [data-reveal="scan"]):not(.is-in) > :is(img, video, picture) { scale: 1.12; }

  /* scan: the MDC signature reveal. The frame is uncovered top to bottom behind a luminous scan line */
  html.js [data-reveal="scan"]:not(.is-in) { clip-path: inset(0 0 100% 0); }
  html.js [data-reveal="scan"]::before {
    content: "";
    position: absolute; inset: 0; z-index: 4; pointer-events: none;
    background: linear-gradient(180deg, var(--halo-faint) calc(100% - 90px), var(--halo-mid) calc(100% - 3px), var(--teal-400) calc(100% - 2px), var(--teal-400) 100%);
    translate: 0 -100%;
    opacity: 1;
  }
  html.js [data-reveal="scan"].is-in::before {
    translate: 0 0;
    opacity: 0;
    transition: translate var(--reveal-dur, 1300ms) var(--ease-in-out) var(--reveal-delay, 0ms),
      opacity 0.5s var(--ease-out) calc(var(--reveal-delay, 0ms) + var(--reveal-dur, 1300ms) - 200ms);
  }
  html.js [data-reveal="scan"].is-done::before { content: none; }
  /* both wipe TO inset(0), not to none: none <-> inset() cannot interpolate, so the photo would pop in at once. Once the
     reveal is done the clip goes away (none), so the frame's own shadow or focus ring is never cut */
  html.js :is([data-reveal="clip"], [data-reveal="scan"]).is-in:not(.is-done) { clip-path: inset(0 0 0 0); }

  /* split: headline words rise out of their own line masks, one after another (motion.js wraps the words) */
  html.js [data-reveal="split"]:not(.is-split) { opacity: 0; }
  html.js [data-reveal="split"] .sw-i {
    display: inline-block;
    transition: translate var(--reveal-dur, var(--dur-4)) var(--ease-out) calc(var(--reveal-delay, 0ms) + var(--i, 0) * var(--split-step)),
      opacity var(--reveal-dur, var(--dur-4)) var(--ease-out) calc(var(--reveal-delay, 0ms) + var(--i, 0) * var(--split-step));
  }
  html.js [data-reveal="split"]:not(.is-in) .sw-i { translate: 0 108%; opacity: 0; }

  /* reticle corners lock in when their frame reveals */
  html.js :is([data-reveal]:not(.is-in), [data-reveal-stagger]:not(.is-in) > *) .reticle::after,
  html.js .reticle:is([data-reveal]:not(.is-in), [data-reveal-stagger]:not(.is-in) > *)::after { opacity: 0; scale: 1.1; }

  /* ---- 2. The reveal transition (removed again by motion.js once finished, so the element's own
         hover transitions are not overridden afterwards) ---- */
  html.js [data-reveal="draw"]:not(.is-done) { --reveal-dur: 1100ms; }
  html.js [data-reveal]:not(.is-done):not([data-reveal="split"]),
  html.js [data-reveal-stagger]:not(.is-done) > :not([data-reveal]) {
    transition:
      opacity var(--reveal-dur, var(--dur-4)) var(--ease-out) var(--reveal-delay, 0ms),
      translate var(--reveal-dur, var(--dur-4)) var(--ease-out) var(--reveal-delay, 0ms),
      scale var(--reveal-dur, var(--dur-4)) var(--ease-out) var(--reveal-delay, 0ms),
      clip-path var(--reveal-dur, 1100ms) var(--ease-in-out) var(--reveal-delay, 0ms);
  }
  html.js [data-reveal="clip"]:not(.is-done) { --reveal-dur: 1100ms; }
  html.js [data-reveal="scan"]:not(.is-done) { --reveal-dur: 1300ms; }
}

/* word wrappers made by motion.js for [data-reveal="split"]: each word sits in its own clipping line box.
   Extra room below and to the right keeps descenders and italic overhangs from being cut */
.sw { display: inline-block; overflow: clip; overflow-clip-margin: 0.08em; vertical-align: top; padding: 0 0.06em 0.14em 0; margin: 0 -0.06em -0.14em 0; }

/* ---- 3. Parallax: motion.js writes `translate` on [data-parallax] (never on reduced motion) ---- */
[data-parallax].is-parallax { will-change: translate; }

/* ---- 4. Scroll-scrubbed primitives: motion.js writes --p (0 -> 1) on [data-scrub] as it crosses the viewport
       (0 when its top enters the bottom edge, 1 when its centre reaches the middle). Tokens, combinable:
         zoom   the frame's <img> settles from 1.16 to 1 as it arrives (Neko's scan-screen zoom)
         inset  the frame grows from 93% to its full size (scale, so it stays on the compositor)
         rise   the element drifts up 56px into place
         words  every word fills from faint to full, one after another (motion.js wraps the words; latched: once
                lit, a word stays lit, so a page captured after scrolling reads normally)
       Only under html.js and no-preference: without JS, reduced motion or print, everything is in its final state. */
@media (prefers-reduced-motion: no-preference) {
  html.js .is-scrub:is([data-scrub~="zoom"]) > :is(img, video, picture) { scale: calc(1.16 - 0.16 * var(--p, 1)); transform-origin: 50% 40%; }
  html.js .is-scrub[data-scrub~="inset"] { scale: calc(0.93 + 0.07 * var(--p, 1)); }
  html.js .is-scrub[data-scrub~="rise"] { translate: 0 calc((1 - var(--p, 1)) * 56px); }
  html.js .is-scrub[data-scrub~="words"] .sw-w { opacity: clamp(0.18, calc(0.18 + 0.82 * (var(--p, 1) * (var(--n, 1) + 3) - var(--i, 0)) / 3), 1); transition: opacity var(--dur-2) linear; }
}

/* ---- 5. Sticky rail: while the rail is being read (.is-tracking), the steps that are not current rest quieter.
       Only their media dims: captions stay at full contrast. Outside the rail (top of page, full-page captures) all
       steps are at full strength. ---- */
@media (min-width: 981px) and (prefers-reduced-motion: no-preference) {
  html.js .is-tracking [data-step] .step-media { transition: opacity var(--dur-4) var(--ease-out), scale 1.2s var(--ease-out); }
  html.js .is-tracking [data-step]:not(.is-current) .step-media { opacity: 0.42; }
}

/* ---- 6. Anchor target highlight (motion.js adds .is-target + .is-target-flash on landing) ---- */
.is-target-flash { animation: target-flash 2.2s var(--ease-out) both; }
@keyframes target-flash {
  0%   { box-shadow: var(--ring-start); }
  25%  { box-shadow: var(--ring-pulse); }
  100% { box-shadow: var(--ring-strong); }
}
@media (prefers-reduced-motion: reduce) {
  .is-target-flash { animation: none; box-shadow: var(--ring-strong); }
}

/* ---- 7. Results kit: draw on reveal. motion.js adds .is-drawn to a .range / .meter / .gauge / .chart as it enters the
       viewport (and again when its tab panel is shown). Transform + opacity, plus a one-shot clip-path wipe for the
       chart line and stroke-dashoffset for the gauge ring. No JS, reduced motion and print: drawn. ---- */
@media (prefers-reduced-motion: no-preference) {
  html.js .range:not(.is-drawn) .range-band { scale: 0 1; }
  html.js .range:not(.is-drawn) .range-dot { translate: calc(((var(--lo) + var(--hi)) / 2 - var(--v)) * 100cqi) 0; scale: 0.4; opacity: 0; }
  html.js .range.is-drawn .range-band { transition: scale 0.9s var(--ease-out) var(--draw-delay, 0ms); }
  html.js .range.is-drawn .range-dot { transition: translate 1.2s var(--ease-out) calc(var(--draw-delay, 0ms) + 250ms), scale 0.6s var(--ease-spring) calc(var(--draw-delay, 0ms) + 250ms), opacity 0.3s linear calc(var(--draw-delay, 0ms) + 250ms); }

  html.js .meter:not(.is-drawn) .meter-track > i { scale: 0 1; }
  html.js .meter.is-drawn .meter-track > i { transition: scale 1.3s var(--ease-out) var(--draw-delay, 0ms); }

  html.js .gauge:not(.is-drawn) .gauge-arc { stroke-dashoffset: 1; }
  html.js .gauge.is-drawn .gauge-arc { transition: stroke-dashoffset 1.4s var(--ease-out) var(--draw-delay, 0ms); }

  html.js .chart:not(.is-drawn) .chart-plot > svg { clip-path: inset(-12px 100% -12px -12px); }
  html.js .chart:not(.is-drawn) .chart-band { opacity: 0; }
  html.js .chart:not(.is-drawn) .chart-dot { scale: 0; }
  html.js .chart:not(.is-drawn) .chart-bars > i { scale: 1 0; }
  html.js .chart.is-drawn .chart-plot > svg { transition: clip-path 1.5s var(--ease-in-out) calc(var(--draw-delay, 0ms) + 150ms); }
  html.js .chart.is-drawn .chart-band { transition: opacity 0.6s var(--ease-out) var(--draw-delay, 0ms); }
  html.js .chart.is-drawn .chart-dot { transition: scale 0.5s var(--ease-spring) calc(var(--draw-delay, 0ms) + 1500ms); }
  html.js .chart.is-drawn .chart-bars > i { transition: scale 0.9s var(--ease-out) calc(var(--draw-delay, 0ms) + var(--i, 0) * 45ms); }
}

/* ---- 8. Tabs: the new panel rises in while the old one fades out on top of it (motion.js sets the classes) ---- */
@media (prefers-reduced-motion: no-preference) {
  html.js [role="tabpanel"].is-entering { opacity: 0; translate: 0 12px; }
  html.js [role="tabpanel"].is-entered { transition: opacity var(--dur-3) var(--ease-out) 80ms, translate var(--dur-4) var(--ease-out) 80ms; }
  html.js [role="tabpanel"].is-leaving {
    position: absolute; inset: 0 0 auto 0; z-index: 1; pointer-events: none;
    opacity: 0; transition: opacity var(--dur-2) var(--ease-out);
  }
}

/* ---- 9. Pinned scene: a frame's photo settles from 1.08 as the frame rises into place (--arrive 0 -> 1), and while the
       next frame slides over it the frame recedes (--cover 0 -> 1): it scales back 5% and its content fades into the
       card colour (an overlay's opacity, so reveals inside the frame keep their own opacity).
       motion.js writes both from cached positions, so a scroll frame only writes. Transform + opacity only ---- */
@media (min-width: 981px) and (prefers-reduced-motion: no-preference) {
  html.js .scene-frame.is-scened { scale: calc(1 - var(--cover, 0) * 0.05); }
  html.js .scene-frame.is-scened::after {
    content: ""; position: absolute; inset: 0; z-index: 5; border-radius: inherit; pointer-events: none;
    background: var(--card-bg, var(--tint)); opacity: calc(var(--cover, 0) * 0.85);
  }
  html.js .scene-frame.is-scened .scene-media > img { scale: calc(1.08 - var(--arrive, 1) * 0.08); transform-origin: 50% 40%; }
}

