/* =====================================================================
   MOTION: Apple-style scroll storytelling, layered as a progressive
   enhancement.

   The three rules that keep this safe:
   1. Content is fully visible by default. Nothing here hides content
      unless the motion that will reveal it is guaranteed to run.
   2. Everything sits inside (prefers-reduced-motion: no-preference).
      Visitors who ask for less motion get a calm, static page.
   3. Scroll-linked effects sit inside @supports (animation-timeline: view()).
      Older browsers get the IntersectionObserver fallback in main.js
      (gated by the .motion-ready class that main.js itself adds).
   ===================================================================== */

/* ---------- Keyframes ---------------------------------------------- */
@keyframes rise-in   { from { opacity: 0; transform: translateY(40px); } to { opacity: 1; transform: none; } }
@keyframes scale-in  { from { opacity: 0.4; transform: scale(0.86); border-radius: calc(var(--r-xl) * 1.6); } to { opacity: 1; transform: none; } }
@keyframes drift     { from { transform: translateY(-7%) scale(1.14); } to { transform: translateY(7%) scale(1.14); } }
@keyframes light-up  { from { opacity: 0.22; } to { opacity: 1; } }
@keyframes story-step {
  0%       { opacity: 0; transform: translateY(48px); }
  18%, 82% { opacity: 1; transform: none; }
  100%     { opacity: 0; transform: translateY(-48px); }
}
/* First step is already showing when the section arrives (no blank screen), then hands off */
@keyframes story-step-first {
  0%, 82% { opacity: 1; transform: none; }
  100%    { opacity: 0; transform: translateY(-48px); }
}
@keyframes story-media-first {
  0%, 82% { opacity: 1; transform: none; }
  100%    { opacity: 0; transform: scale(1.04); }
}
@keyframes story-step-last {
  0%        { opacity: 0; transform: translateY(48px); }
  18%, 100% { opacity: 1; transform: none; }
}
@keyframes story-media {
  0%       { opacity: 0; transform: scale(0.94); }
  18%, 82% { opacity: 1; transform: none; }
  100%     { opacity: 0; transform: scale(1.04); }
}
@keyframes intro     { from { opacity: 0; transform: translateY(18px); filter: blur(6px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes nav-solid { to { border-bottom-color: var(--hairline); } }

@media (prefers-reduced-motion: no-preference) {

  html { scroll-behavior: smooth; }

  /* ---------- Hero intro (on load, not scroll) ---------------------- */
  /* Put .intro on the hero container. Its direct children enter one by
     one. Keep it under 1s, and never on the LCP image. */
  .intro > * {
    animation: intro 900ms var(--ease-out) both;
    animation-delay: calc(var(--i, 0) * 110ms + 80ms);
  }
  .intro > :nth-child(2) { --i: 1; }
  .intro > :nth-child(3) { --i: 2; }
  .intro > :nth-child(4) { --i: 3; }
  .intro > :nth-child(5) { --i: 4; }

  /* ---------- Hover micro-interactions ------------------------------ */
  @media (hover: hover) {
    .card--lift { transition: transform var(--dur-2) var(--ease-out), box-shadow var(--dur-2) var(--ease-out); }
    .card--lift:hover { transform: translateY(-4px); box-shadow: var(--shadow-soft); }
    .media--zoom img { transition: transform 1.2s var(--ease-out); }
    .media--zoom:hover img { transform: scale(1.04); }
  }

  /* ---------- Cross-page transitions (multi-page sites) ------------- */
  @view-transition { navigation: auto; }
  ::view-transition-old(root), ::view-transition-new(root) { animation-duration: 360ms; animation-timing-function: var(--ease-out); }

  /* ================================================================
     SCROLL-DRIVEN (native CSS, runs off the main thread)
     ================================================================ */
  @supports (animation-timeline: view()) {

    /* Nav hairline appears after the first 64px of scroll */
    .nav { animation: nav-solid linear both; animation-timeline: scroll(); animation-range: 0px 64px; }

    /* Rise in as the element enters the viewport */
    .reveal {
      animation: rise-in linear both;
      animation-timeline: view();
      animation-range: entry 0% entry 70%;
    }
    /* Children of a row arrive slightly one after another */
    .reveal-stagger > * {
      animation: rise-in linear both;
      animation-timeline: view();
      animation-range: entry calc(var(--i, 0) * 10%) entry calc(70% + var(--i, 0) * 10%);
    }
    .reveal-stagger > :nth-child(2) { --i: 1; }
    .reveal-stagger > :nth-child(3) { --i: 2; }
    .reveal-stagger > :nth-child(4) { --i: 3; }

    /* Big media grows from a rounded inset to full size */
    .scale-in {
      animation: scale-in linear both;
      animation-timeline: view();
      animation-range: entry 0% cover 45%;
    }

    /* Image drifts gently inside its frame (frame needs overflow: clip) */
    .drift img, .drift video {
      animation: drift linear both;
      animation-timeline: view();
      animation-range: cover 0% cover 100%;
    }

    /* Statement text lights up phrase by phrase as you read */
    .light-up > span {
      animation: light-up linear both;
      animation-timeline: view();
      animation-range: cover 18% cover 42%;
    }

    /* ---- Pinned story --------------------------------------------- */
    /* The section is N screens tall; the stage sticks while steps cross-fade.
       Needs: .story[style="--steps:3"] > .story__stage > .story__steps > .story__step[style="--i:0"]
       Optional: .story__visual > .story__frame[style="--i:0"] (one per step).
       Only on wide screens. On phones it falls back to the static stacked layout. */
    @media (min-width: 48rem) {
      .story {
        --steps: 3;
        height: calc(var(--steps) * 100svh);
        view-timeline: --story block;
        padding-block: 0;
      }
      .story__stage {
        position: sticky; top: 0;
        height: 100svh;
        grid-template-columns: 1fr 1fr;
        align-content: center;
        overflow: clip;
      }
      .story__steps, .story__visual { display: grid; gap: 0; }
      .story__visual { width: min(100%, 72svh); justify-self: center; }
      .story__steps > *, .story__visual > * { grid-area: 1 / 1; }
      .story__step {
        animation: story-step linear both;
        animation-timeline: --story;
        animation-range:
          contain calc(var(--i, 0) * 100% / var(--steps))
          contain calc((var(--i, 0) + 1) * 100% / var(--steps));
      }
      .story__step:first-child { animation-name: story-step-first; }
      .story__step:last-child { animation-name: story-step-last; }
      .story__frame {
        animation: story-media linear both;
        animation-timeline: --story;
        animation-range:
          contain calc(var(--i, 0) * 100% / var(--steps))
          contain calc((var(--i, 0) + 1) * 100% / var(--steps));
      }
      .story__frame:first-child { animation-name: story-media-first; }
      .story__frame:last-child { animation-name: story-step-last; }
    }
  }

  /* ================================================================
     FALLBACK for browsers without scroll timelines.
     main.js adds .motion-ready to <html> and .is-in to elements as they
     enter, so a failed script never leaves content hidden.
     ================================================================ */
  @supports not (animation-timeline: view()) {
    .motion-ready .reveal,
    .motion-ready .reveal-stagger > *,
    .motion-ready .scale-in {
      opacity: 0; transform: translateY(32px);
      transition: opacity var(--dur-3) var(--ease-out), transform var(--dur-3) var(--ease-out);
      transition-delay: calc(var(--i, 0) * 90ms);
    }
    .motion-ready .scale-in { transform: scale(0.92); }
    .motion-ready .reveal-stagger > :nth-child(2) { --i: 1; }
    .motion-ready .reveal-stagger > :nth-child(3) { --i: 2; }
    .motion-ready .reveal-stagger > :nth-child(4) { --i: 3; }
    .motion-ready .is-in { opacity: 1; transform: none; }
  }
}
