/* High Five Racers. What's special to this game: who's where, the turbo meter, and little words. */

body:not([data-screen='playing']) .turbo,
body:not([data-screen='playing']) .toast,
body:not([data-screen='playing']) .five-film { display: none; }

/* ---------- who's in front: faces in order, which swap places when someone overtakes ---------- */
.race-order {
  --row: clamp(38px, 6.4vh, 52px);
  position: relative;
  margin: 2px 0 0;
  padding: 0;
  list-style: none;
  width: calc(var(--row) * 1.75);
  height: calc(var(--row) * var(--cars, 4)); /* One row for each car in the race: see buildOrder. */
}

.order-row {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  gap: 3px;
  width: 100%;
  height: calc(var(--row) - 4px);
  padding: 2px 4px 2px 8px;
  background: var(--surface);
  border-radius: 999px;
  box-shadow: 0 3px 10px rgba(18, 48, 77, 0.18);
  transform: translateY(calc(var(--slot, 0) * var(--row)));
  transition: transform 0.35s ease-out;
}

.order-row img {
  height: 100%;
  aspect-ratio: 1;
  margin-left: auto;
  border-radius: 50%;
  background: #dff1ff;
  object-fit: cover;
}

.order-number {
  font-size: clamp(13px, 1.6vw, 19px);
  font-weight: 700;
  color: var(--ink-soft);
}

.order-row.is-you {
  background: var(--sun);
  box-shadow: 0 3px 12px rgba(219, 165, 0, 0.5);
}

.order-row.is-you .order-number { color: var(--ink); }

/* ---------- the turbo meter ---------- */
.turbo {
  position: fixed;
  z-index: 2;
  bottom: 14px;
  left: calc(50% + (var(--meter-size) + clamp(8px, 2vw, 24px) + clamp(46px, 6vw, 74px)) / 2 + 12px);
  display: flex;
  align-items: center;
  gap: 8px;
  width: clamp(180px, 25vw, 330px);
  padding: 8px 14px 8px 10px;
  background: var(--surface);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.turbo-flame {
  flex: none;
  width: clamp(30px, 3.6vw, 46px);
  height: auto;
  fill: #b7c3cf;
  transform-origin: 50% 90%;
}

.turbo-flame-core { fill: #e6ecf1; }

/* Until it's turbo, the meter shows a hand spread wide, which is how to fill it. Once it's turbo, the flame. */
.turbo:not([data-state='on']) .turbo-flame { display: none; }
.turbo[data-state='on'] .turbo-hand { display: none; }

.turbo-hand {
  flex: none;
  width: clamp(30px, 3.6vw, 46px);
  height: auto;
}

.turbo-hand rect { fill: var(--good); stroke: var(--ink); stroke-width: 2.5; }
.turbo-hand path { fill: none; stroke: var(--good); stroke-width: 7; stroke-linecap: round; }

/* The fingers spread a little wider and back, slowly, to say "wider". Only ever a turn, never a colour. */
.turbo-finger { transform-box: view-box; transform-origin: 26px 40px; animation: turbo-spread 2.4s ease-in-out infinite; }
.turbo-finger-1 { --spread: -9deg; }
.turbo-finger-2 { --spread: -4deg; }
.turbo-finger-3 { --spread: 4deg; }
.turbo-finger-4, .turbo-thumb { --spread: 9deg; }

@keyframes turbo-spread {
  0%, 100% { transform: rotate(0); }
  50% { transform: rotate(var(--spread)); }
}

.turbo-parts {
  flex: 1;
  display: grid;
  gap: 5px;
  min-width: 0;
}

.turbo-words {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-weight: 700;
  line-height: 1;
}

.turbo-word { font-size: clamp(15px, 1.8vw, 23px); white-space: nowrap; }

.turbo-speed {
  font-size: clamp(15px, 1.8vw, 22px);
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.turbo-track {
  height: clamp(12px, 1.7vw, 20px);
  background: var(--accent-track);
  border-radius: 999px;
  overflow: hidden;
}

.turbo-fill {
  height: 100%;
  background: linear-gradient(90deg, #ffb703, #ff7a1f);
  border-radius: 999px;
  transform: scaleX(0);
  transform-origin: left;
}

.turbo[data-state='filling'] .turbo-flame { fill: #ffb703; }
.turbo[data-state='filling'] .turbo-flame-core { fill: #ffe7a0; }

/* Turbo: a bigger flame and a coloured frame, in steady colours. Nothing here blinks. */
.turbo[data-state='on'] {
  box-shadow: 0 0 0 4px #ff7a1f, var(--shadow);
}

.turbo[data-state='on'] .turbo-flame {
  fill: #ff7a1f;
  animation: turbo-flame 0.9s ease-in-out infinite;
}

.turbo[data-state='on'] .turbo-flame-core { fill: #ffd84a; }
.turbo[data-state='on'] .turbo-word { color: #d9520a; }

@keyframes turbo-flame {
  0%, 100% { transform: scale(1.12) rotate(-3deg); }
  50% { transform: scale(1.28) rotate(3deg); }
}

/* ---------- a little film by the meter: the other hand makes a fist and opens, and the car flies ---------- */
.fly-film {
  display: none;
  position: fixed;
  z-index: 3;
  bottom: calc(14px + 84px);
  left: calc(50% + (var(--meter-size) + clamp(8px, 2vw, 24px) + clamp(46px, 6vw, 74px)) / 2 + 12px);
  width: clamp(180px, 25vw, 330px);
  padding: 6px 10px 10px;
  background: linear-gradient(#cfeeff, #f4fbff);
  border: 5px solid var(--sun);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

body[data-screen='playing'][data-fly-film] .fly-film { display: block; }
/* The other hand is on its own side of the picture, as in a mirror. The film is drawn for the left. */
body[data-fly-film='right'] .fly-film svg { transform: scaleX(-1); }
.fly-film svg { display: block; width: 100%; height: auto; }

.fly-film-hand rect { fill: #ffc21a; stroke: var(--ink); stroke-width: 2.5; }
.fly-film-finger { fill: none; stroke: #ffc21a; stroke-width: 7; stroke-linecap: round; transform-box: fill-box; transform-origin: bottom center; animation: fly-fist 3s ease-in-out infinite; }
.fly-film-thumb { transform-origin: bottom left; }
.fly-film-arrow, .fly-film-ground { fill: none; stroke: var(--ink-soft); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.fly-film-car rect { fill: #ff5a4f; stroke: var(--ink); stroke-width: 2.5; }
.fly-film-car .fly-film-cab { fill: #ffd23f; }
.fly-film-car circle { fill: var(--ink); }
.fly-film-flame { fill: #ff9f45; }
.fly-film-car { animation: fly-up 3s ease-in-out infinite; }

.fly-film-words {
  margin: 2px 0 0;
  text-align: center;
  font-weight: 700;
  font-size: clamp(14px, 1.6vw, 20px);
  line-height: 1.15;
}

/* Open, a fist, open again: then the car takes off, and comes down. */
@keyframes fly-fist {
  0%, 12% { transform: scaleY(1); }
  26%, 44% { transform: scaleY(0.3); }
  58%, 100% { transform: scaleY(1); }
}

@keyframes fly-up {
  0%, 56% { transform: translateY(0); }
  76% { transform: translateY(-22px); }
  90%, 100% { transform: translateY(0); }
}

/* ---------- little words beside the action ---------- */
.toast {
  position: fixed;
  z-index: 3;
  left: 50%;
  /* Just under where a prompt goes, so that the two can show together. */
  top: calc(12px + var(--camera-small) / var(--camera-aspect) + 18px + clamp(22px, 3.3vw, 42px) * 1.15 + 34px);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 22px 8px;
  background: var(--surface);
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: clamp(20px, 3vw, 36px);
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, 0);
  pointer-events: none;
}

.toast.is-showing { animation: toast 1.5s ease-out both; }

.toast-face {
  width: clamp(36px, 4vw, 52px);
  height: clamp(36px, 4vw, 52px);
  margin-left: -14px;
  border-radius: 50%;
  background: #dff1ff;
  object-fit: cover;
}

.toast-face.is-hidden { display: none; }

@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 14px) scale(0.8); }
  14% { opacity: 1; transform: translate(-50%, 0) scale(1.06); }
  24% { transform: translate(-50%, 0) scale(1); }
  76% { opacity: 1; transform: translate(-50%, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -14px) scale(1); }
}

/* ---------- a little film: an open hand going over to the big one, and slapping it ---------- */
.five-film {
  display: none;
  position: fixed;
  z-index: 3;
  left: 16px;
  bottom: 16px;
  width: min(19vw, 30vh);
  min-width: 110px;
  background: linear-gradient(#cfeeff, #f4fbff);
  border: 5px solid var(--sun);
  border-radius: 24px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

body[data-screen='playing'][data-five-film] .five-film { display: block; }
body[data-five-film='left'] .five-film svg { transform: scaleX(-1); }
.five-film svg { display: block; width: 100%; height: auto; }

.five-film path,
.five-film rect { stroke-linecap: round; stroke-linejoin: round; }

.five-film-big rect { fill: #ffd23f; stroke: var(--ink); stroke-width: 2.5; }
.five-film-big path { fill: none; stroke: #ffd23f; stroke-width: 8; }
.five-film-small rect { fill: var(--good); stroke: var(--ink); stroke-width: 2; }
.five-film-small path { fill: none; stroke: var(--good); stroke-width: 6; }
.five-film-burst path { fill: #ff7a1f; stroke: var(--ink); stroke-width: 2; }

.five-film-small { animation: film-reach 2.2s ease-in infinite; }
.five-film-big { transform-box: fill-box; transform-origin: 50% 100%; animation: film-swing 2.2s ease-out infinite; }
.five-film-burst { transform-box: fill-box; transform-origin: center; animation: film-burst 2.2s ease-out infinite; }

@keyframes film-reach {
  0%, 10% { transform: translate(0, 0); }
  58%, 70% { transform: translate(34px, -8px); }
  100% { transform: translate(0, 0); }
}

@keyframes film-swing {
  0%, 56% { transform: rotate(0); }
  64% { transform: rotate(16deg); }
  76% { transform: rotate(-5deg); }
  88%, 100% { transform: rotate(0); }
}

@keyframes film-burst {
  0%, 56% { opacity: 0; transform: scale(0.3); }
  62% { opacity: 1; transform: scale(1); }
  80% { opacity: 1; transform: scale(1.15); }
  92%, 100% { opacity: 0; transform: scale(1.3); }
}

@media (max-height: 560px) {
  /* Smaller with more cars, so that the list stays above the film in the corner. Four cars are as before. */
  .race-order { --row: min(clamp(28px, 9vh, 38px), calc(37vh / var(--cars, 4))); }
  /* Smaller, so that it stays clear of the prompt. */
  .fly-film { width: 160px; bottom: calc(14px + 62px); padding: 4px 8px 6px; }
  .fly-film-words { font-size: 13px; }
}

/* On a narrow screen there's no room beside the camera's picture, so all of this goes under it. */
@media (max-width: 700px) {
  .turbo {
    left: auto;
    right: 10px;
    bottom: calc(var(--meter-size) + 54px);
    width: 170px;
  }

  .fly-film {
    left: auto;
    right: 10px;
    bottom: calc(var(--meter-size) + 54px + 76px);
    width: 170px;
  }

  .hud-top { top: calc(12px + var(--camera-small) / var(--camera-aspect) + 14px); }
  .race-order { --row: min(34px, calc(180px / var(--cars, 4))); }
  body[data-screen='playing'] .prompt { top: calc(12px + var(--camera-small) / var(--camera-aspect) + 96px); }

  .toast {
    top: calc(12px + var(--camera-small) / var(--camera-aspect) + 176px);
    max-width: calc(100vw - 32px);
    white-space: normal;
    text-align: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  .order-row { transition: none; }
  .turbo-finger { animation: none; transform: rotate(var(--spread)); }
  .fly-film-finger, .fly-film-car { animation-duration: 4.5s; }
  .turbo[data-state='on'] .turbo-flame { animation: none; }
  .toast.is-showing { animation-name: toast-still; }
  .five-film-small,
  .five-film-big,
  .five-film-burst { animation-duration: 3.4s; }
}

@keyframes toast-still {
  0%, 85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, 0); }
}
