/* Daam motion. Link after styles.css, desktop.css and charts.css. Helpers live in motion.js.

   House rules
   - Entrances animate FROM a hidden pose TO the element's own resting style (from-only
     keyframes, fill-mode backwards so nothing lingers afterwards), so switching motion
     off simply shows everything where it belongs. Only exits hold their end state.
   - Movement uses the individual translate / scale properties, never transform, so the
     stamp, sticker and pakka rotations and the desktop sheet's centring transform survive.
   - 150 to 450ms. var(--ease) for movement, var(--spring) for small pops only.
   - Everything here is off under prefers-reduced-motion and html[data-motion="fast"]
     (see the end of the file). */

@property --m-delay { syntax: "<time>"; inherits: false; initial-value: 0ms; }
@property --m-dir { syntax: "<number>"; inherits: false; initial-value: 1; }

/* ---------- Entrance utilities (staggerIn sets --m-delay per element) ---------- */
.m-rise { animation: m-rise 320ms var(--ease) var(--m-delay, 0ms) backwards; }
.m-fade { animation: m-fade 240ms var(--ease) var(--m-delay, 0ms) backwards; }
.m-slide { animation: m-slide 280ms var(--ease) var(--m-delay, 0ms) backwards; }
.m-pop { animation: m-pop 300ms var(--spring) var(--m-delay, 0ms) backwards; }
/* Receipt line print-in: the paper feeds up a hair while the thermal head prints left to right */
.m-print, .ln.m-print, .rl.m-print {
  animation:
    m-feed 260ms var(--ease) var(--m-delay, 0ms) backwards,
    m-clip 300ms steps(12, jump-none) var(--m-delay, 0ms) backwards;
}
/* Keyframe bar growth, for bars that do not use the charts.css ".in" transition */
.m-bar-y { transform-origin: 50% 100%; animation: m-draw-y 380ms var(--ease) var(--m-delay, 0ms) backwards; }
.m-bar-x { transform-origin: 0 50%; animation: m-draw-x 380ms var(--ease) var(--m-delay, 0ms) backwards; }
svg .m-bar-y, svg .m-bar-x, svg .m-blip, svg .m-grow-x, svg .m-grow-y { transform-box: fill-box; }

@keyframes m-rise { from { opacity: 0; translate: 0 8px; } }
@keyframes m-fade { from { opacity: 0; } }
@keyframes m-slide { from { opacity: 0; translate: calc(var(--m-dir, 1) * 14px) 0; } }
@keyframes m-pop { from { opacity: 0; scale: .5; } }
@keyframes m-feed { from { opacity: 0; translate: 0 5px; } }
@keyframes m-clip { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
@keyframes m-draw-x { from { scale: 0 1; } }
@keyframes m-draw-y { from { scale: 1 0; } }

/* ---------- Transition-based chart grow (growBars adds "in" to the root) ---------- */
.m-grow-y { transform-origin: 50% 100%; transition: scale 420ms var(--ease) var(--m-delay, 0ms); }
.m-grow-x { transform-origin: 0 50%; transition: scale 420ms var(--ease) var(--m-delay, 0ms); }
.m-chart:not(.in) .m-grow-y { scale: 1 0; }
.m-chart:not(.in) .m-grow-x { scale: 0 1; }

/* ---------- Play: first print of the day ---------- */
/* Add .m-enter to section.play on the first render after load (not when advancing). */
.play.m-enter > .slip-wrap { animation: m-unroll 440ms var(--ease) backwards; }
.play.m-enter > .panel { animation: m-rise 340ms var(--ease) 140ms backwards; }
@keyframes m-unroll {
  from { clip-path: inset(-12px -12px 100% -12px); translate: 0 -8px; }
  to { clip-path: inset(-12px -12px -12px -12px); translate: 0 0; }
}
/* The item icon settles a beat after the card (complements the existing .card.enter) */
.card.enter .pic svg { animation: m-pop 340ms var(--spring) 90ms backwards; }

/* ---------- Reveal (add .m-reveal to #rv) ---------- */
.panel .reveal.m-reveal { animation: none; }
.m-reveal > .rv-top { animation: m-rise 260ms var(--ease) backwards; }
.m-reveal > .price { animation: m-rise 300ms var(--ease) 40ms backwards; }
.m-reveal > .cta { animation: m-fade 240ms var(--ease) 140ms backwards; }
.m-reveal .tape-line { transform-origin: 50% 50%; animation: m-draw-x 420ms var(--ease) 60ms backwards; }
.m-reveal .notch { animation: m-draw-y 320ms var(--spring) 160ms backwards; }
.m-reveal .tick, .m-reveal .tick-l { animation: m-fade 260ms var(--ease) 180ms backwards; }
.m-reveal .fx { transition: opacity 240ms var(--ease), transform 380ms var(--spring); }
.m-reveal .score.on .bandw i { animation: m-pop 300ms var(--spring) 90ms backwards; }
/* "Nailed it" used to play while the score was still hidden; now it lands when the score shows */
.m-reveal .score:not(.on) .pakka { animation: none; }
.m-reveal .score.on .pakka { animation: m-stamp 520ms linear 80ms backwards, m-ink 720ms var(--ease) 80ms backwards; }

/* ---------- Stamp press with ink settle (stamp() in motion.js) ---------- */
/* Impact at 45% of 520ms = 234ms (STAMP_HIT). m-ink peaks at the same moment. */
:is(.stamp, .pakka).m-stamp {
  animation:
    m-stamp 520ms linear var(--m-delay, 0ms) backwards,
    m-ink 720ms var(--ease) var(--m-delay, 0ms) backwards;
}
@keyframes m-stamp {
  /* accelerate into the paper, then settle (second curve is the --ease curve) */
  0% { opacity: 0; scale: 1.45; filter: blur(.8px); animation-timing-function: cubic-bezier(.55, 0, 1, .45); }
  45% { opacity: 1; scale: .955; filter: blur(0); animation-timing-function: cubic-bezier(.2, .8, .2, 1); }
  70% { scale: 1.012; animation-timing-function: cubic-bezier(.2, .8, .2, 1); }
  100% { scale: 1; }
}
@keyframes m-ink {
  0%, 30% { text-shadow: none; box-shadow: none; }
  33% { text-shadow: 0 0 .8px currentColor; box-shadow: 0 0 0 .8px currentColor, inset 0 0 0 .6px currentColor; }
}
/* The receipt takes the hit */
.m-impact { animation: m-impact 220ms var(--ease); }
@keyframes m-impact { 30% { translate: 0 1.5px; } }

/* ---------- Charts: small finishing touches on charts.css marks ---------- */
/* Your bar gives a little nod once it has grown (histogram and latest history bar) */
.in .ch-bar.ch-hi { animation: m-blip 360ms var(--ease) calc(var(--ch-delay, 0ms) + var(--ch-i, 0) * 24ms + 640ms) backwards; }
/* Your leaderboard row catches the eye once, right after it slides in */
.in .ch-lb-row.ch-me { animation: m-flash 450ms var(--ease) calc(var(--ch-delay, 0ms) + var(--ch-i, 0) * 32ms + 260ms) backwards; }
.m-blip { transform-origin: 50% 100%; animation: m-blip 360ms var(--ease) var(--m-delay, 0ms) backwards; }
.m-flash { animation: m-flash 450ms var(--ease) var(--m-delay, 0ms) backwards; }
@keyframes m-blip { 45% { scale: 1 1.1; } }
@keyframes m-flash { from { background-color: color-mix(in srgb, var(--stamp) 22%, var(--paper-2)); } }

/* ---------- Results ---------- */
/* Add .m-enter to section.result when it is NOT fresh (returning to a finished bill).
   Fresh results use tear + staggered .m-print rows + stamp() instead. */
.result.m-enter .slip-wrap { animation: m-rise 340ms var(--ease) backwards; }
.result.m-enter .res-side > * { animation: m-rise 340ms var(--ease) 80ms backwards; }
.result.m-enter .res-side > :nth-child(2) { animation-delay: 130ms; }
.result.m-enter .res-side > :nth-child(3) { animation-delay: 180ms; }
.result.m-enter .res-side > :nth-child(n + 4) { animation-delay: 230ms; }

/* ---------- Buttons ---------- */
/* Touch press for chips and segmented buttons (cta, ghost, icon-btn and keys already have one).
   The colour timings match desktop.css so its hover feel is unchanged. */
.chip, .seg button {
  transition: background-color .15s var(--ease), color .15s var(--ease), border-color .15s var(--ease), scale .16s var(--ease);
}
.chip:active, .seg button:active { scale: .96; transition-duration: .15s, .15s, .15s, 50ms; }
/* Keyboard press feedback (tap() in motion.js): Enter on the CTA, R/K/L/C on chips, tab clicks */
.m-tap { animation: m-tap 180ms var(--ease); }
.m-tap kbd { animation: m-kbd 180ms var(--ease); }
@keyframes m-tap { 35% { scale: .965; } }
@keyframes m-kbd { 35% { translate: 0 1px; opacity: 1; } }

/* ---------- Focus: rings snap in rather than blink in ---------- */
:is(button, a):focus-visible { animation: m-focus 200ms var(--ease); }
.m-tap:focus-visible { animation: m-tap 180ms var(--ease); }
@keyframes m-focus { from { outline-offset: 6px; outline-color: transparent; } }

/* ---------- Leaving (leave() in motion.js hides the element afterwards) ---------- */
.sheet.m-sheet-out { animation: m-sheet-out 200ms var(--ease) both; pointer-events: none; }
.scrim.m-fade-out { animation: m-fade-out 200ms var(--ease) both; pointer-events: none; }
.m-leave { animation: m-leave 180ms var(--ease) both; pointer-events: none; }
@keyframes m-sheet-out { to { opacity: 0; translate: 0 14px; scale: .985; } }
@keyframes m-fade-out { to { opacity: 0; } }
@keyframes m-leave { to { opacity: 0; translate: 0 6px; } }

/* ---------- Pending states ---------- */
/* .m-busy on a button while a request is in flight (pair with aria-busy="true") */
.m-busy { position: relative; overflow: hidden; pointer-events: none; }
.m-busy::after {
  content: "";
  position: absolute; left: 0; bottom: 0;
  width: 100%; height: 2px;
  background: currentColor;
  opacity: .6;
  transform-origin: 0 50%;
  animation: m-busy 1s var(--ease) infinite;
}
@keyframes m-busy { from { translate: -40% 0; scale: .4 1; } to { translate: 100% 0; scale: .4 1; } }
/* .m-skel placeholder line while stats load (set width / height inline as needed) */
.m-skel { display: block; position: relative; overflow: hidden; height: 10px; border-radius: 2px; background: var(--paper-2); }
.m-skel::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--slip) 70%, transparent), transparent);
  translate: -100% 0;
  animation: m-shimmer 1.3s var(--ease) infinite;
}
@keyframes m-shimmer { to { translate: 100% 0; } }

/* staggerIn(..., { whenVisible: true }) holds entrances until the container scrolls into view.
   Keep this after every entrance rule above. */
.m-wait :is(.m-rise, .m-fade, .m-slide, .m-pop, .m-print, .m-bar-x, .m-bar-y) { animation-play-state: paused; }

/* ---------- View transitions ---------- */
/* Names exist only while motion.js transition() runs one: it sets html[data-vt="<type>"]
   and html[data-vt-phase="old" | "new"]. The header never moves. */
:root[data-vt] .top { view-transition-name: daam-top; }
/* type "next": the old card lifts away (old state only, so the new card keeps its own
   .card.enter entrance), the reveal panel hands over to the keypad, the rest swaps in place */
:root[data-vt="next"][data-vt-phase="old"] .play .card { view-transition-name: daam-card; }
:root[data-vt="next"] .play > .panel { view-transition-name: daam-panel; }

::view-transition-group(daam-top) { animation-duration: 240ms; }
:root[data-vt="next"]::view-transition-old(root),
:root[data-vt="next"]::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
::view-transition-old(daam-card) { animation: m-vt-out-up 180ms var(--ease) both; }
::view-transition-group(daam-panel) { animation-duration: 300ms; animation-timing-function: var(--ease); }
::view-transition-old(daam-panel) { animation: m-vt-out 140ms var(--ease) both; }
::view-transition-new(daam-panel) { animation: m-vt-in-up 260ms var(--ease) 70ms both; }
/* type "screen" (finish, moving between bills) and "theme": the browser cross-fade, retimed */
:root[data-vt="screen"]::view-transition-group(root) { animation-duration: 280ms; }
:root[data-vt="theme"]::view-transition-group(root) { animation-duration: 360ms; }

@keyframes m-vt-out-up { to { opacity: 0; transform: translateY(-10px); } }
@keyframes m-vt-out { to { opacity: 0; } }
@keyframes m-vt-in-up { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Motion off: prefers-reduced-motion, or Animations: Fast ---------- */
@media (prefers-reduced-motion: reduce) {
  :is(.m-rise, .m-fade, .m-slide, .m-pop, .m-print, .m-bar-x, .m-bar-y, .m-stamp, .m-impact, .m-tap, .m-blip, .m-flash, .m-sheet-out, .m-fade-out, .m-leave),
  .m-tap kbd, .card.enter .pic svg,
  .m-reveal > *, .m-reveal .tape-line, .m-reveal .notch, .m-reveal .tick, .m-reveal .tick-l, .m-reveal .bandw i, .m-reveal .pakka,
  .play.m-enter > *, .result.m-enter .slip-wrap, .result.m-enter .res-side > *,
  .in .ch-bar.ch-hi, .in .ch-lb-row.ch-me,
  :is(button, a):focus-visible { animation: none !important; }
  .m-reveal .fx, .chip, .seg button, .m-grow-x, .m-grow-y { transition: none !important; }
  .chip:active, .seg button:active, .m-chart:not(.in) :is(.m-grow-x, .m-grow-y) { scale: none !important; }
  .m-busy::after { animation: none !important; translate: none; scale: 1 1; opacity: .3; }
  .m-skel::after { display: none; }
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
}
:root[data-motion="fast"] :is(.m-rise, .m-fade, .m-slide, .m-pop, .m-print, .m-bar-x, .m-bar-y, .m-stamp, .m-impact, .m-tap, .m-blip, .m-flash, .m-sheet-out, .m-fade-out, .m-leave),
:root[data-motion="fast"] .m-tap kbd, :root[data-motion="fast"] .card.enter .pic svg,
:root[data-motion="fast"] .m-reveal > *, :root[data-motion="fast"] .m-reveal .tape-line, :root[data-motion="fast"] .m-reveal .notch,
:root[data-motion="fast"] .m-reveal .tick, :root[data-motion="fast"] .m-reveal .tick-l, :root[data-motion="fast"] .m-reveal .bandw i, :root[data-motion="fast"] .m-reveal .pakka,
:root[data-motion="fast"] .play.m-enter > *, :root[data-motion="fast"] .result.m-enter .slip-wrap, :root[data-motion="fast"] .result.m-enter .res-side > *,
:root[data-motion="fast"] .in .ch-bar.ch-hi, :root[data-motion="fast"] .in .ch-lb-row.ch-me,
:root[data-motion="fast"] :is(button, a):focus-visible { animation: none !important; }
:root[data-motion="fast"] .m-reveal .fx, :root[data-motion="fast"] .chip, :root[data-motion="fast"] .seg button,
:root[data-motion="fast"] .m-grow-x, :root[data-motion="fast"] .m-grow-y { transition: none !important; }
:root[data-motion="fast"] .chip:active, :root[data-motion="fast"] .seg button:active,
:root[data-motion="fast"] .m-chart:not(.in) :is(.m-grow-x, .m-grow-y) { scale: none !important; }
:root[data-motion="fast"] .m-busy::after { animation: none !important; translate: none; scale: 1 1; opacity: .3; }
:root[data-motion="fast"] .m-skel::after { display: none; }
:root[data-motion="fast"]::view-transition-group(*),
:root[data-motion="fast"]::view-transition-old(*),
:root[data-motion="fast"]::view-transition-new(*) { animation: none !important; }
