/*
 * Battle of Trains global motion rules (task 6.1; design "Motion System", Req 18 and 19).
 * Loaded last, so the reduced-motion and paused rules win over every other stylesheet.
 *
 * - Continuous_Animations (loops) are declared only inside
 *   @media (prefers-reduced-motion: no-preference), under an .is-continuous root, in the page
 *   stylesheet that owns them. This file holds none of them and no page-specific animation.
 * - One-shot animations (reveal, badge pop, toast, flip, button sheen, menu sheet) keep running
 *   while the Motion_Toggle is paused; under reduced motion they become fades of 150 ms or less.
 * - Only transform and opacity move.
 */

/* ---------------------------------------------------------------------------------------------
   Page_Section reveal (reveal.js): sections below the first viewport get data-reveal="pending"
   and then "shown" on first intersection. Without the script nothing is pending, so every
   section stays visible (Req 18.12); under reduced motion the pending state does not exist.
   --------------------------------------------------------------------------------------------- */

@media (prefers-reduced-motion: no-preference) {
  [data-reveal="pending"] {
    opacity: 0;
    transform: translateY(28px);
  }

  [data-reveal="shown"] {
    transition:
      opacity var(--t-reveal) var(--ease-out),
      transform var(--t-reveal) var(--ease-out);
  }
}

/* ---------------------------------------------------------------------------------------------
   Motion_Toggle paused: every loop under an .is-continuous root stops in place (Req 18.15).
   --------------------------------------------------------------------------------------------- */

html[data-motion="paused"] .is-continuous,
html[data-motion="paused"] .is-continuous::before,
html[data-motion="paused"] .is-continuous::after,
html[data-motion="paused"] .is-continuous *,
html[data-motion="paused"] .is-continuous *::before,
html[data-motion="paused"] .is-continuous *::after {
  animation-play-state: paused !important;
}

/* ---------------------------------------------------------------------------------------------
   Reduced motion: moves become instant and only fades remain, at most 150 ms (Req 19.1, 19.4).
   --------------------------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-property: opacity, color, background-color, border-color, outline-color, box-shadow !important;
    transition-duration: var(--t-fade) !important;
    scroll-behavior: auto !important;
  }
}

/* ---------------------------------------------------------------------------------------------
   Shared one-shot keyframes
   --------------------------------------------------------------------------------------------- */

/* Fade-and-rise for content that scripts insert (the [data-reveal] sections use the transition
   above): animation: reveal var(--t-reveal) var(--ease-out) both; inside no-preference. */
@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(28px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

/* Plain fades: the reduced-motion stand-ins for every moving one-shot, and crossfades. */
@keyframes fade-in {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes fade-out {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

/* Cart badge pop: scale 1 → 1.4 → 1 over 450 ms (Req 18.13). */
@keyframes badge-pop {
  0% {
    transform: scale(1);
    animation-timing-function: var(--ease-out);
  }

  35% {
    transform: scale(1.4);
    animation-timing-function: var(--ease-inout);
  }

  100% {
    transform: scale(1);
  }
}

/* Reduced-motion badge: a 150 ms opacity flash that ends fully visible. */
@keyframes badge-flash {
  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.35;
  }
}

/* Toast entrance and exit (chrome.js adds .is-leaving and removes the toast when the exit ends).
   The toast sets --toast-from-x / --toast-from-y for its slide direction. */
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate3d(var(--toast-from-x), var(--toast-from-y), 0) scale(0.98);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes toast-out {
  from {
    opacity: 1;
    transform: none;
  }

  to {
    opacity: 0;
    transform: translate3d(var(--toast-from-x), var(--toast-from-y), 0) scale(0.98);
  }
}

/* Card flip from the front (0°) to the back (180°) on the 3D card body; play it in reverse to
   flip back. Front and back faces use backface-visibility: hidden. */
@keyframes flip {
  from {
    transform: rotateY(0deg);
  }

  to {
    transform: rotateY(180deg);
  }
}

/* Button sheen: a light band sweeps across once (primary buttons, on hover, no-preference only). */
@keyframes sheen {
  from {
    transform: translateX(-130%) skewX(-20deg);
  }

  to {
    transform: translateX(260%) skewX(-20deg);
  }
}

/* ---------------------------------------------------------------------------------------------
   Shared one-shot animations of the shell
   --------------------------------------------------------------------------------------------- */

@media (prefers-reduced-motion: no-preference) {
  .cart-count.is-popping {
    animation: badge-pop var(--t-pop) both;
  }

  .toast {
    animation: toast-in var(--t-toast) var(--ease-out) both;
  }

  .toast.is-leaving {
    animation: toast-out var(--t-base) var(--ease-in) both;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cart-count.is-popping {
    animation: badge-flash var(--t-fade) linear both;
  }

  .toast {
    animation: fade-in var(--t-fade) linear both;
  }

  .toast.is-leaving {
    animation: fade-out var(--t-fade) linear both;
  }
}
