/* The basic animations every crafted site gets. Subtle, with weight, never needed to
   read the page, and all of it off for anyone whose device asks for less motion.
   Tune the variables in site.css. See each one moving at /__tools/motion.html, and
   read references/motion.md for which to use where.

   In the HTML:
     data-arrive            the first screen's parts come in one after another (once per visit)
     data-arrive="wipe"     the same, each part wiping in from the left: for large type
     data-reveal            rises gently into place as it scrolls into view
     data-reveal="fade"     fades in
     data-reveal="open"     a picture opens upward from its bottom edge, like a blind
     data-reveal="settle"   a picture settles from slightly large to its size, inside its frame
     data-reveal="draw"     a rule or line draws itself from the left
     data-reveal-group      its children rise in turn, a beat apart
     data-drift             a picture drifts slowly inside its frame as the page scrolls
     data-hover="zoom"      a picture inside a link eases a little closer on hover
     data-hover="underline" a link's underline draws in on hover (for menus, not body text)
     data-hover="lift"      a button lifts a little on hover and presses on tap
     data-step-aside        on .bar: it moves out of the way while scrolling down, back on the way up */

:root {
  --ease-out: cubic-bezier(.2, .7, .2, 1);
  --ease-in-out: cubic-bezier(.6, 0, .2, 1);
  --quick: 180ms;        /* hovers, presses */
  --gentle: 320ms;       /* menus, questions opening, page changes */
  --reveal: 800ms;       /* things coming into view */
  --arrive: 850ms;       /* the first screen */
  --stagger: 90ms;       /* the beat between parts */
  --rise: 22px;          /* how far things travel */
  --drift: 5%;           /* how far a picture drifts in its frame */
}

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

  /* -------------------------------------------------------------- arrival --
     Pure CSS with backwards fill: where animation is off, everything is simply
     there. The head script marks later pages of a visit, which skip it. */
  [data-arrive] > * { animation: arrive var(--arrive) var(--ease-out) backwards; }
  [data-arrive] > :nth-child(2) { animation-delay: calc(var(--stagger) * 1); }
  [data-arrive] > :nth-child(3) { animation-delay: calc(var(--stagger) * 2); }
  [data-arrive] > :nth-child(4) { animation-delay: calc(var(--stagger) * 3); }
  [data-arrive] > :nth-child(5) { animation-delay: calc(var(--stagger) * 4); }
  [data-arrive] > :nth-child(n+6) { animation-delay: calc(var(--stagger) * 5); }
  [data-arrive="wipe"] > * { animation-name: arrive-wipe; animation-timing-function: var(--ease-in-out); animation-duration: calc(var(--arrive) * 1.2); }
  html[data-seen] [data-arrive] > * { animation: none; }

  /* -------------------------------------------------------------- reveals --
     motion.js holds back only what is below the screen when the page loads, and
     brings each in as it arrives. If the script never runs, nothing is held back. */
  .is-waiting { opacity: 0; translate: 0 var(--rise); }
  [data-reveal="fade"].is-waiting { translate: none; }
  [data-reveal="open"].is-waiting { opacity: 1; translate: none; clip-path: inset(100% 0 0 0); }
  [data-reveal="draw"] { transform-origin: left center; }
  [data-reveal="draw"].is-waiting { opacity: 1; translate: none; scale: 0 1; }
  [data-reveal="settle"] { overflow: clip; }
  [data-reveal="settle"].is-waiting { translate: none; }
  [data-reveal="settle"].is-waiting img { scale: 1.08; }

  .is-in {
    transition:
      opacity var(--reveal) var(--ease-out),
      translate var(--reveal) var(--ease-out),
      clip-path calc(var(--reveal) * 1.3) var(--ease-in-out),
      scale calc(var(--reveal) * 1.2) var(--ease-in-out);
    transition-delay: var(--delay, 0ms);
  }
  [data-reveal="settle"].is-in img { transition: scale calc(var(--reveal) * 1.8) var(--ease-out) var(--delay, 0ms); }

  /* pictures that load late fade in instead of popping */
  img.is-loading { opacity: 0; }
  img.is-loaded { transition: opacity 500ms var(--ease-out); }

  /* ---------------------------------------------------------------- drift --
     Driven by the scroll itself, in browsers that can; elsewhere it is still. */
  @supports (animation-timeline: view()) {
    [data-drift] { overflow: clip; }
    [data-drift] img {
      scale: 1.12;
      animation: drift linear both;
      animation-timeline: view();
    }
  }

  /* --------------------------------------------------------------- hovers --
     Only where there is a real pointer. */
  @media (hover: hover) {
    [data-hover="zoom"] { overflow: clip; }
    [data-hover="zoom"] img { transition: scale 900ms var(--ease-out); }
    [data-hover="zoom"]:hover img, [data-hover="zoom"]:focus-visible img { scale: 1.04; }

    [data-hover="underline"] {
      text-decoration: none;
      background: linear-gradient(currentColor, currentColor) 0 100% / 0 1px no-repeat;
      transition: background-size 420ms var(--ease-out);
    }
    [data-hover="underline"]:hover, [data-hover="underline"]:focus-visible,
    [data-hover="underline"][aria-current="page"] { background-size: 100% 1px; }

    [data-hover="lift"] { transition: translate var(--quick) var(--ease-out), background-color var(--quick) var(--ease-out), box-shadow var(--quick) var(--ease-out); }
    [data-hover="lift"]:hover { translate: 0 -2px; box-shadow: 0 6px 14px -8px rgb(0 0 0 / .35); }
  }
  [data-hover="lift"]:active { translate: 0 0; scale: .98; box-shadow: none; transition-duration: 80ms; }

  /* ---------------------------------------------------------- the bar ---- */
  .bar[data-step-aside] { transition: translate 420ms var(--ease-out), box-shadow var(--quick) var(--ease-out); }
  html[data-bar-away] .bar[data-step-aside]:not(:focus-within) { translate: 0 -100%; }

  /* -------------------------------------------------- the phone menu ---- */
  @media (max-width: 55.99em) { @supports selector(:popover-open) {
    .menu[popover] {
      transition: opacity var(--gentle) var(--ease-out), translate var(--gentle) var(--ease-out),
                  display var(--gentle) allow-discrete, overlay var(--gentle) allow-discrete;
      opacity: 0;
      translate: 0 -8px;
    }
    .menu[popover]:popover-open { opacity: 1; translate: 0 0; }
    @starting-style { .menu[popover]:popover-open { opacity: 0; translate: 0 -8px; } }
  } }

  /* ------------------------------------------- questions opening in place -- */
  @supports (interpolate-size: allow-keywords) {
    :root { interpolate-size: allow-keywords; }
    details::details-content {
      block-size: 0;
      overflow: clip;
      transition: block-size var(--gentle) var(--ease-out), content-visibility var(--gentle) allow-discrete;
    }
    details[open]::details-content { block-size: auto; }
  }

  /* --------------------------------------------------------- page changes --
     A soft cross-fade with a slight rise, in browsers that can. Give a picture the
     same view-transition-name on two pages and it moves from one to the other. */
  @view-transition { navigation: auto; }
  ::view-transition-old(root) { animation: page-out var(--gentle) var(--ease-in-out) both; }
  ::view-transition-new(root) { animation: page-in calc(var(--gentle) * 1.5) var(--ease-out) both; }
  ::view-transition-group(*) { animation-duration: 450ms; animation-timing-function: var(--ease-in-out); }
}

@media (prefers-reduced-motion: reduce) {
  @view-transition { navigation: none; }
}

@keyframes arrive { from { opacity: 0; translate: 0 var(--rise); } }
@keyframes arrive-wipe { from { clip-path: inset(-10% 100% -10% 0); translate: 12px 0; } to { clip-path: inset(-10% 0 -10% 0); } }
@keyframes drift { from { translate: 0 calc(var(--drift) * -1); } to { translate: 0 var(--drift); } }
@keyframes page-out { to { opacity: 0; } }
@keyframes page-in { from { opacity: 0; translate: 0 10px; } }

@media print {
  [data-arrive] > *, .is-waiting, img.is-loading { animation: none !important; opacity: 1 !important; translate: none !important; clip-path: none !important; scale: none !important; }
}
