/* Acheuli motion: scroll-driven, CSS-only, transform/opacity only.
   Motion is light and material, never decoration. Rules: design-system/MASTER.md §7
   Everything here is progressive enhancement. Outside the @media/@supports gate the page
   is static and complete: hearth at a fixed glow, axe finished, all content visible. */

@media (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: scroll()) {

    /* 1. Hearth: the page warms as you move down it, like drawing closer to a fire */
    @keyframes hearth-grow {
      from { opacity: .3; transform: translateY(7%) scale(.94); }
      to   { opacity: 1;  transform: none; }
    }
    .hearth {
      animation: hearth-grow linear both;
      animation-timeline: scroll(root block);
    }

    /* Scroll cue: three chevrons pulse in a downward wave; the whole cue fades out as soon as you scroll */
    @keyframes cue-out {
      to { opacity: 0; transform: translateY(8px); }
    }
    @keyframes cue-pulse {
      0%, 100% { opacity: .18; }
      45%      { opacity: 1; }
    }
    .scroll-cue {
      display: block;
      margin-block-start: var(--space-2);
      color: var(--text-muted);
      animation: cue-out linear both;
      animation-timeline: scroll(root block);
      animation-range: 0px 140px;
    }
    .scroll-cue path { animation: cue-pulse 1.8s ease-in-out infinite; }
    .scroll-cue path:nth-child(2) { animation-delay: .2s; }
    .scroll-cue path:nth-child(3) { animation-delay: .4s; }
    @media (max-width: 47.99em) and (max-height: 700px), (max-height: 420px) {
      .scroll-cue { display: none; } /* no room on small portrait phones or landscape phones */
    }

    /* Sticky header: transparent over the hero at the top, solid bar once you scroll */
    @keyframes header-bg {
      from { opacity: 0; }
      to   { opacity: 1; }
    }
    .site-header::before {
      animation: header-bg linear both;
      animation-timeline: scroll(root block);
      animation-range: 0px 96px;
    }

    /* 2. Reveals: rise + fade as an element enters the viewport */
    @keyframes reveal {
      from { opacity: 0; transform: translateY(var(--reveal-distance)); }
      to   { opacity: 1; transform: none; }
    }
    .reveal {
      animation: reveal var(--ease) both;
      animation-timeline: view();
      animation-range: entry 0% entry 55%;
    }

    /* 3. Flaking (signature moment, max one per page).
       The tall .flaking section owns the timeline; a sticky stage keeps the figure on
       screen while normal page scroll chips facets away, revealing the hand-axe. */
    @keyframes flake-away {
      from { opacity: 1; transform: translate(0, 0) rotate(0); }
      to   { opacity: 0; transform: translate(var(--dx, 0), var(--dy, 0)) rotate(var(--rot, 0deg)); }
    }
    .flaking {
      block-size: 320vh;
      view-timeline: --flake block;
    }
    .flaking + .section { padding-block-start: var(--space-section); }
    .flaking__stage {
      position: sticky;
      inset-block-start: 0;
      block-size: 100svh;
      padding-block: calc(var(--header-h) + var(--space-3)) var(--space-5);
    }

    /* Copy steps stack in one cell and cross-fade, each tied to a group of flakes */
    .flaking__copy { gap: 0; }
    @media (max-width: 47.99em) {
      /* Phones: the pinned stage must hold the figure plus the whole hero step (h1, lede, both
         buttons), so the axe gives up height before the copy is pushed below the fold */
      .flaking__art { height: clamp(9rem, calc(100svh - 35rem), min(68svh, 76vw, 28rem)); }
    }
    .step {
      grid-area: 1 / 1;
      animation: step-mid linear both;
      animation-timeline: --flake;
      pointer-events: none;
    }
    @keyframes step-first {
      0%, 80% { opacity: 1; transform: none; }
      100%    { opacity: 0; transform: translateY(calc(var(--reveal-distance) * -.75)); }
    }
    @keyframes step-mid {
      0%   { opacity: 0; transform: translateY(var(--reveal-distance)); }
      20%, 80% { opacity: 1; transform: none; }
      100% { opacity: 0; transform: translateY(calc(var(--reveal-distance) * -.75)); }
    }
    @keyframes step-last {
      0%   { opacity: 0; transform: translateY(var(--reveal-distance)); }
      25%, 100% { opacity: 1; transform: none; }
    }
    /* Ranges line up with flake groups: raw block (hero), first strikes (i0-3), shaping (i4-12), finished */
    .step:nth-child(1) { animation-name: step-first; animation-range: cover 25% cover 36%; }
    .step:nth-child(2) { animation-range: cover 34% cover 47%; }
    .step:nth-child(3) { animation-range: cover 46% cover 61%; }
    .step:nth-child(4) { animation-name: step-last; animation-range: cover 60% cover 72%; }
    /* The hero step holds links, so it takes pointer events while shown; once faded out it also
       turns visibility off (a discrete switch at the end of the fade, no layout) so its invisible
       links cannot catch clicks or keyboard focus over the later steps. */
    .step.step--hero { pointer-events: auto; animation-name: hero-out; } /* beats .step:nth-child(1) */
    @keyframes hero-out {
      0%, 80% { opacity: 1; transform: none; visibility: visible; }
      100%    { opacity: 0; transform: translateY(calc(var(--reveal-distance) * -.75)); visibility: hidden; }
    }
    /* Figure captions: Fig. 1 to Fig. 4 follow the stone (raw, first flakes, emerging, finished),
       on their own ranges, so they are independent of the step count */
    .flaking__figure .caption { display: grid; justify-items: center; text-align: center; }
    .flaking__figure .cap[aria-hidden="true"] { display: block; }
    .flaking__figure .cap {
      grid-area: 1 / 1;
      --reveal-distance: 6px;
      animation: step-mid linear both;
      animation-timeline: --flake;
    }
    .flaking__figure .cap:nth-child(1) { animation-name: step-first; animation-range: cover 25% cover 36%; }
    .flaking__figure .cap:nth-child(2) { animation-range: cover 34% cover 47%; }
    .flaking__figure .cap:nth-child(3) { animation-range: cover 46% cover 61%; }
    .flaking__figure .cap:nth-child(4) { animation-name: step-last; animation-range: cover 60% cover 72%; }
    .flake {
      opacity: 1; /* start as solid stone; the animation takes it from here */
      transform-box: fill-box;
      transform-origin: center;
      animation: flake-away linear both;
      animation-timeline: --flake;
      /* --i staggers each facet along the scroll range */
      animation-range:
        cover calc(30% + var(--i, 0) * 2.6%)
        cover calc(38% + var(--i, 0) * 2.6%);
    }
  }
}
