/* Motion: everything on the site that moves lives here.
 *
 * Rules:
 *  1. Content is visible with no script and no animation support. A thing is hidden only when
 *     <html> has "reveal-ready", which reveal.js adds after it has checked it can show everything.
 *  2. Only opacity, transform and stroke-dashoffset animate. Nothing moves layout.
 *  3. prefers-reduced-motion: reduce switches everything off. The "Pause motion" button (class
 *     "motion-paused" on <html>) does the same for the current page and stores nothing.
 */

/* ---------------------------------------------------------------- page-to-page transition */
@media (prefers-reduced-motion: no-preference) {
  @view-transition { navigation: auto; }
}
/* The header holds still while the page under it crossfades. */
::view-transition-group(site-header) { animation-duration: 0s; }
::view-transition-old(root), ::view-transition-new(root) { animation-duration: 220ms; }

/* ---------------------------------------------------------------- reveal on scroll */
@keyframes reveal-in {
  from { opacity: 0; transform: translateY(18px); }
  to { opacity: 1; transform: none; }
}
/* The hidden state also carries its own way out: if the script dies after adding reveal-ready, this
 * animation shows the block anyway 1.6 s later (reveal.js normally does it by 1.2 s). */
@keyframes show-failsafe { to { opacity: 1; } }
@keyframes draw-failsafe { to { stroke-dashoffset: 0; } }
html.reveal-ready [data-reveal]:not(.is-in) { opacity: 0; animation: show-failsafe 0s linear 1.6s forwards; }
html.reveal-ready [data-reveal].is-in { animation: reveal-in var(--t-slow) var(--ease) backwards; }
html.reveal-ready .reveal-d1 { animation-delay: 70ms; }
html.reveal-ready .reveal-d2 { animation-delay: 140ms; }
html.reveal-ready .reveal-d3 { animation-delay: 210ms; }
html.reveal-ready .reveal-d4 { animation-delay: 280ms; }

/* ---------------------------------------------------------------- menu panels */
@keyframes panel-in {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .nav-item.is-open > .nav-panel { animation: panel-in var(--t-fast) var(--ease); }
  .card:has(.card__link:hover) { transform: translateY(-2px); }
}

/* ---------------------------------------------------------------- reading progress bar */
.scroll-progress { display: none; }
@keyframes progress-grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}
@supports (animation-timeline: scroll()) {
  @media (prefers-reduced-motion: no-preference) {
    .scroll-progress {
      display: block;
      position: fixed;
      inset: 0 0 auto 0;
      z-index: var(--z-progress);
      height: 3px;
      background: var(--accent);
      transform-origin: 0 50%;
      animation: progress-grow linear both;
      animation-timeline: scroll(root);
    }
  }
}

/* ---------------------------------------------------------------- animated diagrams (inline SVG)
 * Wrap a diagram in an element with data-play. reveal.js gives it .is-playing only while it is on
 * screen, so loops pause off screen. Put the classes below on SVG parts.
 *   .flow-line   a dashed line whose dashes travel along it
 *   .draw        a path that draws itself on (give the path pathLength="1")
 *   .pop         a shape that pops in
 *   .travel      a dot that moves along an offset-path you set in the page's own css
 *   .pulse       a ring that grows and fades (an LED, a signal)
 *   .blink       a light that blinks
 *   .float       a gentle up-and-down bob
 * Add .d1 to .d4 for a delay. Always label the diagram with text; motion is never the only signal.
 */
@keyframes flow { to { stroke-dashoffset: -14; } }
@keyframes draw { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }
@keyframes pop { from { opacity: 0; transform: scale(0.82); } to { opacity: 1; transform: none; } }
@keyframes travel { from { offset-distance: 0%; } to { offset-distance: 100%; } }
@keyframes pulse { 0% { opacity: 0.55; transform: scale(0.6); } 100% { opacity: 0; transform: scale(1.6); } }
@keyframes blink { 0%, 49% { opacity: 1; } 50%, 100% { opacity: 0.25; } }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }

.flow-line { fill: none; stroke: var(--line-strong); stroke-width: 2; stroke-dasharray: 6 8; }
.draw { stroke-dasharray: 1; stroke-dashoffset: 0; }
.pop, .pulse { transform-box: fill-box; transform-origin: center; }

@media (prefers-reduced-motion: no-preference) {
  [data-play] .flow-line { animation: flow 1.4s linear infinite; }
  [data-play].is-playing .draw { animation: draw 1.2s var(--ease) backwards; }
  [data-play].is-playing .pop { animation: pop 0.5s var(--ease) backwards; }
  [data-play] .travel { animation: travel 5s ease-in-out infinite; }
  [data-play] .pulse { animation: pulse 1.8s ease-out infinite; }
  [data-play] .blink { animation: blink 1.2s steps(1, end) infinite; }
  [data-play] .float { animation: float 4s ease-in-out infinite; }
  /* The two-class form (.is-playing) is needed so a delay is not lost to the animation shorthand on .draw and .pop. */
  [data-play] .d1, [data-play].is-playing .d1 { animation-delay: 0.15s; }
  [data-play] .d2, [data-play].is-playing .d2 { animation-delay: 0.3s; }
  [data-play] .d3, [data-play].is-playing .d3 { animation-delay: 0.45s; }
  [data-play] .d4, [data-play].is-playing .d4 { animation-delay: 0.6s; }

  /* Before the script says a diagram is on screen, a hidden start state and paused loops. */
  /* Only a drawing that is far below and has not been near the screen yet starts hidden. reveal.js
   * adds .is-shown to a drawing that is near, has been seen, or has waited 1.2 s; from then on its
   * parts rest in their finished, still state. The failsafe animations do it too if the script dies. */
  html.reveal-ready [data-play]:not(.is-playing):not(.is-shown) .draw { stroke-dashoffset: 1; animation: draw-failsafe 0s linear 1.6s forwards; }
  html.reveal-ready [data-play]:not(.is-playing):not(.is-shown) .pop { opacity: 0; animation: show-failsafe 0s linear 1.6s forwards; }
  html.reveal-ready [data-play]:not(.is-playing) :not(.pop):not(.draw) { animation-play-state: paused; }
}

/* ---------------------------------------------------------------- stepper scene entrance */
@keyframes scene-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .stepper.is-enhanced .stepper__step.is-current { animation: scene-in var(--t-base) var(--ease); }
}

/* ---------------------------------------------------------------- Pause motion (button in the footer) */
html.motion-paused *,
html.motion-paused *::before,
html.motion-paused *::after {
  animation-play-state: paused !important;
  transition-duration: 0s !important;
  scroll-behavior: auto !important;
}
html.motion-paused [data-reveal] { opacity: 1 !important; animation: none !important; }
html.motion-paused .draw { stroke-dashoffset: 0 !important; animation: none !important; }
/* A drawing whose parts were frozen part-way through a loop could be left half-finished, so under Pause
 * motion every animated part of a drawing goes back to its base, finished, still state. */
html.motion-paused [data-play] *,
html.motion-paused [data-play] *::before,
html.motion-paused [data-play] *::after { animation: none !important; }
html.motion-paused .pop { opacity: 1 !important; animation: none !important; }
html.motion-paused .stepper__step.is-current { animation: none !important; }
/* The menu panel fades in from invisible. Frozen at its first frame it would stay invisible, so it must not animate at all. */
html.motion-paused .nav-item.is-open > .nav-panel,
html.motion-paused .nav-panel { animation: none !important; opacity: 1 !important; }

/* ---------------------------------------------------------------- reduced motion: no movement at all */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    animation-delay: 0s !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------------------------------------------------------------- the moving "Today" line
   It drifts a night either way while its drawing is on screen. Starts visible; stilled by reduced
   motion and by Pause motion like everything else. */
@keyframes sweep { from { transform: translateX(-21px); } to { transform: translateX(42px); } }
@media (prefers-reduced-motion: no-preference) {
  [data-play].is-playing .sweep { animation: sweep 5s ease-in-out infinite alternate; }
}
html.motion-paused .sweep { animation: none; }
