/* The phone's 20-second loop, in four beats:

     0 s    - 5.5 s    today's games, as a glimpse
     5.5 s  - 10.5 s   your result lands in the group's chat, and they react
     10.5 s - 15 s     the leaderboard: a podium, the crown, confetti
     15 s   - 20 s     the group's insights

   Two kinds of timing are used, and both repeat every --loop:
   - whole-loop keyframes, written in per cent of the loop (1 s = 5 %);
   - small moments (a letter, a star, a message) that share one set of
     keyframes and start at their own time, given in the HTML as --d.
     Each of those hides again well before the loop comes round.
   Nothing runs for people who ask their device for reduced motion: the
   phone then rests on the chat and the beats are a plain list. */

@media (prefers-reduced-motion: no-preference) {
  .story { --loop: 20s; }

  .today { animation: beat-1 var(--loop) infinite both; }
  .talk { animation: beat-2 var(--loop) infinite both; }
  .board { animation: beat-3 var(--loop) infinite both; }
  .insights { animation: beat-4 var(--loop) infinite both; }

  /* 1. the glimpse of a game */
  .cursor { animation: cursor var(--loop) step-end infinite; }
  .clock { animation: clock var(--loop) step-end infinite; }
  .ticks { animation: ticks var(--loop) step-end infinite; }
  .cell.w1::before { animation: active var(--loop) step-end infinite; }
  .cell:not(.block) { animation: solved var(--loop) infinite, hop var(--loop) var(--h) infinite both; }
  .cell b { animation: ink var(--loop) var(--d) infinite both; }

  /* things that arrive, each at its own --d */
  .in { animation: arrive var(--loop) var(--d) infinite both; }
  .pop { animation: pop var(--loop) var(--d) infinite both; }
  .light { animation: light var(--loop) var(--d) infinite both; }
  .bump { animation: bump var(--loop) var(--d) infinite both; }
  .tap { animation: tap var(--loop) var(--d) infinite both; }
  .land { animation: land var(--loop) var(--d) infinite both; }
  .fly { animation: fly var(--loop) var(--d) infinite both; }
  .typing { animation: typing var(--loop) var(--d) infinite both; }
  .rise { animation: rise var(--loop) var(--d) infinite both; }
  .drop { animation: drop var(--loop) var(--d) infinite both; }
  .fx i { animation: burst var(--loop) var(--d) infinite both; }
  /* ...and the thing each replaces, there for the six seconds before --d.
     It starts one loop early so that it is in place when the page opens. */
  .was { animation: until var(--loop) calc(var(--d) - var(--loop)) infinite both; }

  .dots i { animation: dot 0.9s infinite; }
  .dots i:nth-child(2) { animation-delay: 0.15s; }
  .dots i:nth-child(3) { animation-delay: 0.3s; }
  .tug i { animation: lead var(--loop) infinite; }

  /* under the phone: one line at a time, and where in the loop we are */
  .progress { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
  .progress i { height: 3px; border-radius: 2px; overflow: hidden; background: var(--rule); }
  .progress i::after { content: ""; display: block; height: 100%; background: var(--ink); transform-origin: 0 50%; }
  .progress i:nth-child(1)::after { animation: bar-1 var(--loop) linear infinite; }
  .progress i:nth-child(2)::after { animation: bar-2 var(--loop) linear infinite; }
  .progress i:nth-child(3)::after { animation: bar-3 var(--loop) linear infinite; }
  .progress i:nth-child(4)::after { animation: bar-4 var(--loop) linear infinite; }
  .beats { display: grid; margin-top: 12px; font-size: 1rem; font-weight: 500; color: var(--ink); text-wrap: balance; }
  .beats li { grid-area: 1 / 1; margin: 0; }
  .beats li:nth-child(1) { animation: beat-1 var(--loop) infinite both; }
  .beats li:nth-child(2) { animation: beat-2 var(--loop) infinite both; }
  .beats li:nth-child(3) { animation: beat-3 var(--loop) infinite both; }
  .beats li:nth-child(4) { animation: beat-4 var(--loop) infinite both; }
}

/* ---------- Whole-loop keyframes (1 s = 5 %) ---------- */
@keyframes beat-1 { 0% { opacity: 0; } 2%, 26% { opacity: 1; } 27.5%, 100% { opacity: 0; } }
@keyframes beat-2 { 0%, 26.5% { opacity: 0; } 28%, 51% { opacity: 1; } 52.5%, 100% { opacity: 0; } }
@keyframes beat-3 { 0%, 51.5% { opacity: 0; } 53%, 73.5% { opacity: 1; } 75%, 100% { opacity: 0; } }
@keyframes beat-4 { 0%, 74% { opacity: 0; } 75.5%, 97.5% { opacity: 1; } 100% { opacity: 0; } }

@keyframes bar-1 { 0% { scale: 0 1; } 27.5%, 100% { scale: 1 1; } }
@keyframes bar-2 { 0%, 27.5% { scale: 0 1; } 52.5%, 100% { scale: 1 1; } }
@keyframes bar-3 { 0%, 52.5% { scale: 0 1; } 75%, 100% { scale: 1 1; } }
@keyframes bar-4 { 0%, 75% { scale: 0 1; } 100% { scale: 1 1; } }

/* The cursor steps along the first row as its letters are typed
   (at 0.7, 1.1, 1.5 and 1.9 s; build.py gives the letters the same times). */
@keyframes cursor {
  0% { opacity: 1; translate: calc(var(--pitch) * var(--c0)) 0; }
  3.5% { opacity: 1; translate: calc(var(--pitch) * (var(--c0) + 1)) 0; }
  5.5% { opacity: 1; translate: calc(var(--pitch) * (var(--c0) + 2)) 0; }
  7.5% { opacity: 1; translate: calc(var(--pitch) * (var(--c0) + 3)) 0; }
  9.5%, 100% { opacity: 0; translate: calc(var(--pitch) * (var(--c0) + 3)) 0; }
}
@keyframes active { 0% { opacity: 1; } 10.5%, 100% { opacity: 0; } }
/* The clock runs until the finished time takes its place at 2.8 s.
   It is a strip of five readings, 24 px each, shown one at a time. */
@keyframes clock { 0% { opacity: 1; } 14%, 100% { opacity: 0; } }
@keyframes ticks {
  0% { translate: 0 0; }
  4% { translate: 0 -24px; }
  8% { translate: 0 -48px; }
  10.5% { translate: 0 -72px; }
  12.25%, 100% { translate: 0 -96px; }
}
@keyframes solved { 0%, 13% { background-color: var(--card); } 14.5%, 100% { background-color: #34472c; } }
/* In the insights, today's win moves the rivalry from 3-3 to 4-3 at 16.6 s. */
@keyframes lead { 0%, 83% { scale: 0.5 1; } 86%, 100% { scale: 0.571 1; } }

/* ---------- Small moments, started at --d (or --h for a letter's hop) ---------- */
@keyframes ink { 0% { opacity: 0; scale: 0.6; } 0.8%, 50% { opacity: 1; scale: 1; } 50.1%, 100% { opacity: 0; } }
@keyframes hop { 0% { translate: 0 0; } 0.7% { translate: 0 -7px; } 1.7%, 100% { translate: 0 0; } }
@keyframes arrive { 0% { opacity: 0; translate: 0 12px; } 2.5%, 45% { opacity: 1; translate: 0 0; } 45.1%, 100% { opacity: 0; } }
@keyframes pop { 0% { opacity: 0; scale: 0.4; } 1.5% { opacity: 1; scale: 1.18; } 2.6%, 45% { opacity: 1; scale: 1; } 45.1%, 100% { opacity: 0; } }
@keyframes light {
  0% { fill: var(--star-off); scale: 1; }
  1.2% { fill: var(--gold); scale: 1.4; }
  2.4%, 45% { fill: var(--gold); scale: 1; }
  45.1%, 100% { fill: var(--star-off); }
}
@keyframes until { 0%, 69.9% { opacity: 0; } 70%, 100% { opacity: 1; } }
@keyframes bump { 0% { scale: 1; } 1.2% { scale: 1.55; } 2.8%, 100% { scale: 1; } }
@keyframes tap { 0% { opacity: 0; scale: 0.5; } 0.6% { opacity: 1; scale: 0.85; } 2.2%, 100% { opacity: 0; scale: 1.4; } }
/* Your result card comes in with a little bounce. */
@keyframes land {
  0% { opacity: 0; translate: 0 30px; scale: 0.9; }
  1.6% { opacity: 1; translate: 0 -5px; scale: 1.03; }
  2.8%, 45% { opacity: 1; translate: 0 0; scale: 1; }
  45.1%, 100% { opacity: 0; }
}
/* A reaction floats up and away; --x is how far to the side it starts. */
@keyframes fly {
  0% { opacity: 0; translate: var(--x) 0; scale: 0.4; }
  0.8% { opacity: 1; scale: 1.25; }
  4% { opacity: 1; }
  6.5%, 100% { opacity: 0; translate: calc(var(--x) * 1.4) -180px; scale: 1; }
}
/* The typing dots are there for 0.8 s, then the message takes their place. */
@keyframes typing { 0% { opacity: 0; } 0.4%, 3.6% { opacity: 1; } 4%, 100% { opacity: 0; } }
@keyframes dot { 0%, 60%, 100% { translate: 0 0; opacity: 0.5; } 30% { translate: 0 -4px; opacity: 1; } }
/* A podium block comes up from the floor with its player on top. */
@keyframes rise {
  0% { translate: 0 100%; animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1); }
  4%, 45% { translate: 0 0; }
  45.1%, 100% { translate: 0 100%; }
}
@keyframes drop {
  0% { opacity: 0; translate: 0 -44px; rotate: -28deg; }
  1.3% { opacity: 1; translate: 0 3px; rotate: 7deg; }
  2.1% { translate: 0 -5px; rotate: -4deg; }
  3%, 45% { opacity: 1; translate: 0 0; rotate: 0deg; }
  45.1%, 100% { opacity: 0; }
}
/* A piece of confetti is thrown to (--x, --y), turning, then falls. */
@keyframes burst {
  0% { opacity: 0; translate: 0 0; rotate: 0deg; animation-timing-function: cubic-bezier(0.1, 0.7, 0.3, 1); }
  0.3% { opacity: 1; }
  3.5% { opacity: 1; translate: calc(var(--x) * 0.82) var(--y); rotate: calc(var(--r) * 0.6); animation-timing-function: cubic-bezier(0.5, 0, 0.9, 0.6); }
  7.5% { opacity: 1; }
  9.5%, 100% { opacity: 0; translate: var(--x) calc(var(--y) + 300px); rotate: var(--r); }
}
