@import url("fonts.css");
:root {
  color-scheme: light;
  --red: #eb4247;
  --inactive: #e5d3d4;
  --reject: #832528;
  --left-silhouette: polygon(0 50%, 100% 0, 100% 100%);
  --right-silhouette: polygon(100% 50%, 0 0, 0 100%);
  --delivery-easing: ease-out;
  -webkit-font-smoothing: antialiased;
  font-family: var(--font-body);
}

@supports (animation-timing-function: linear(0, 1)) {
  :root {
    /* Spring with stiffness 320, damping 40 and mass 1, sampled over 625 ms. */
    --delivery-easing: linear(0, .0726, .2148, .365, .4994, .6113, .701, .7714, .8259,
      .8678, .8999, .9243, .9429, .9571, .9678, .976, .9822, .9869, .9904, .9931,
      .9952, .9967, .9979, .9988, .9995, 1);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body { margin: 0; background: #bf382e; }
body:has(.score-page.after-shift:not([hidden])) { background-color: #ece4d4; }

.play-page {
  --field-width: min(100vw, 440px);
  max-width: var(--field-width);
  margin-inline: auto;
}

.game {
  --viewport-height: 100vh;
  --field-height: max(520px, var(--viewport-height));
  --gutter: 32px;
  --belt-width: 28px;
  --cook-size: 56px;
  --station-width: min(80px, calc(var(--field-width, 100vw) / 2 - var(--gutter) - var(--cook-size) - var(--belt-width) / 2));
  --queue-padding: 4px;
  --queue-gap: 4px;
  --order-size: calc((var(--station-width) - 2 * (var(--queue-padding) + var(--queue-gap))) / 3);
  --queue-height: calc(var(--order-size) + 2 * var(--queue-padding));
  --guide-space: 44px;
  --belt-top: calc(435px + (var(--field-height) - 874px) * .36);
  --customer-step: min(56px, calc((var(--belt-top) - 169px) / 4.75));
  --customer-height: calc(var(--customer-step) * .75);
  position: relative;
  height: var(--field-height);
  touch-action: none;
}

.game, .game * {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
}

@supports (height: 100dvh) {
  .game { --viewport-height: 100dvh; }
}

.lives {
  position: absolute;
  z-index: 1;
  top: var(--gutter);
  left: var(--gutter);
  display: flex;
  gap: 8px;
}
.lives span {
  width: 32px;
  height: 32px;
  background: url("assets/heart.svg") center no-repeat;
}
.lives .lost { background-image: url("assets/heart-outline.svg"); }

.score {
  position: absolute;
  z-index: 1;
  top: var(--gutter);
  right: var(--gutter);
  color: var(--red);
  font-size: 24px;
  font-weight: 750;
  line-height: 32px;
  font-variant-numeric: tabular-nums;
}
.score-digits {
  display: flex;
  /* Reserve three hearts and an 8px gap even on a narrow, long-running game. */
  font-size: min(24px, calc((100vw - 2 * var(--gutter) - 120px) * 1.45 / var(--score-length, 1)));
}
.score-digits:empty::after { content: "0"; }
.score-digit {
  position: relative;
  width: 1ch;
  height: 32px;
  overflow: hidden;
  text-align: center;
}
.score-digit span { display: block; }
.score-previous { position: absolute; inset: 0; }
.score-digit[data-changing] .score-previous { transform: translateY(calc(-100% * var(--roll))); }
.score-digit[data-changing] .score-current { transform: translateY(calc(100% * (1 - var(--roll)))); }

.game-hud {
  position: absolute;
  z-index: 4;
  top: max(calc(var(--gutter) - 6px), env(safe-area-inset-top, 0px));
  inset-inline: max(var(--gutter), env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  pointer-events: none;
}
.game-hud .lives, .game-hud .score { position: static; }
.game-hud .score { min-width: 0; container-type: inline-size; }
.game-hud .score-digits {
  font-size: min(24px, calc((var(--field-width, 100vw) / 2 - var(--gutter) - 64px) * 1.45 / var(--score-length, 1)));
  font-size: min(24px, calc(145cqi / var(--score-length, 1)));
}
#sound-toggle {
  justify-self: end;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-inline-end: -10px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--red);
  appearance: none;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
}
#sound-toggle[aria-pressed="false"] { color: #786c6e; }
#sound-toggle[aria-pressed="true"] .sound-off,
#sound-toggle[aria-pressed="false"] .sound-waves { display: none; }
#sound-toggle:active { background: #f6ecec; }
#sound-toggle:focus-visible { outline: 2px solid var(--reject); outline-offset: 1px; }

.conveyor--inlet {
  position: absolute;
  top: var(--belt-top);
  bottom: calc(var(--gutter) + var(--guide-space));
  left: calc(50% - var(--belt-width) / 2);
  width: var(--belt-width);
  background: var(--inactive);
}
.branch { --branch-color: var(--inactive); }
.game:not([data-phase="ready"])[data-route="0"] .branch--left,
.game:not([data-phase="ready"])[data-route="1"] .branch--right { --branch-color: var(--red); }

.station {
  position: absolute;
  top: calc(var(--belt-top) + (var(--belt-width) - var(--queue-height)) / 2);
  width: var(--station-width);
}
/* From the fork to the cook, every ticket center is one cell pitch apart. */
.branch--left .station { right: calc(50% + var(--order-size) / 2); }
.branch--right .station { left: calc(50% + var(--order-size) / 2); }
.cook, .queue {
  background-color: var(--branch-color);
  transition: background-color 73ms var(--delivery-easing);
}
.cook {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: var(--cook-size);
  height: var(--cook-size);
}
.branch--left .cook { right: 100%; clip-path: var(--left-silhouette); }
.branch--right .cook { left: 100%; clip-path: var(--right-silhouette); }

.tip-paid {
  position: absolute;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tip-paid {
  bottom: calc(50% + var(--cook-size) / 2 + 5px);
  width: var(--cook-size);
  color: var(--red);
  font-size: 19px;
  font-weight: 750;
  line-height: 1;
  text-align: center;
}
.branch--left .tip-paid { right: 100%; }
.branch--right .tip-paid { left: 100%; }

.branch--left .service { justify-self: end; }
.branch--right .service { justify-self: start; }
.service {
  position: relative;
  width: var(--order-size);
  height: var(--order-size);
  background: white;
  clip-path: var(--ticket-shape, none);
}
.service span {
  position: absolute;
  inset: 0;
  background: var(--reject);
  clip-path: inset(calc((1 - var(--progress, 0)) * 100%) 0 0);
}
.queue {
  display: flex;
  gap: var(--queue-gap);
  padding: var(--queue-padding);
}
.queue span {
  flex: 1;
  aspect-ratio: 1;
  background: rgb(255 255 255 / 44%);
}
.queue .occupied { background: white; }
.queue .occupied:not([data-shifting]) { clip-path: var(--ticket-shape, none); }
.queue .occupied[data-shifting] { background: rgb(255 255 255 / 44%); }

.customers {
  position: absolute;
  inset: 0;
  clip-path: inset(74px 0 0);
  pointer-events: none;
}
.customer {
  position: absolute;
  top: calc(var(--belt-top) - 95px - var(--customer-height));
  left: calc(50% - 28px);
  width: 56px;
  height: var(--customer-step);
}
.customer img {
  display: block;
  width: auto;
  height: var(--customer-height);
  margin-inline: auto;
}

.tickets { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
[data-required-route="0"] {
  --ticket-shape: var(--left-silhouette);
  /* Inset a triangle perpendicular to its three edges for the retry outline. */
  --retry-inset: 3px 2px 3px 4px;
}
[data-required-route="1"] {
  --ticket-shape: var(--right-silhouette);
  --retry-inset: 3px 4px 3px 2px;
}
.ticket {
  position: absolute;
  top: 0;
  left: 0;
  width: 20px;
  height: 20px;
}
/* Keep the travel transform, entrance crop and lock indicator on the outer box. */
.ticket::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--ticket-fill, white);
  clip-path: var(--ticket-shape, none);
}
/* A ticket held at the fork keeps the side it was assigned. */
.ticket[data-locked-route]::after {
  content: "";
  position: absolute;
  top: -7px;
  width: 6px;
  height: 7px;
  background: var(--reject);
}
.ticket[data-locked-route="0"]::after { left: 0; clip-path: var(--left-silhouette); }
.ticket[data-locked-route="1"]::after { right: 0; clip-path: var(--right-silhouette); }
.ticket[data-kind="served"], .ticket[data-kind="retry"], .rejection { --ticket-fill: var(--reject); }
.ticket[data-kind="retry"]::after, .rejection::after {
  content: "";
  position: absolute;
  inset: var(--retry-inset, 2px);
  background: #fff0bf;
  clip-path: var(--ticket-shape, none);
}
.rejection {
  width: var(--order-size);
  height: var(--order-size);
}

@media (prefers-reduced-motion: reduce) {
  .cook, .queue { transition: none; }
}

.effects {
  position: absolute;
  z-index: 2;
  inset: 0;
  pointer-events: none;
}
.gate {
  position: absolute;
  top: calc(var(--entry-y) - 17px);
  left: calc(var(--entry-x) - 2.5px);
  width: 5px;
  height: 32px;
  border: 1px solid white;
  background: var(--reject);
  transform-origin: top center;
}
.gate-count {
  position: absolute;
  top: calc(var(--entry-y) - 43px);
  left: calc(var(--entry-x) + 5px);
  padding: 3px 5px;
  border-radius: 4px;
  background: var(--reject);
  color: white;
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.refusal-effect[data-route="1"] .gate-count {
  left: auto;
  right: calc(100% - var(--entry-x) + 5px);
}
.signal {
  position: absolute;
  top: -4px;
  left: -4px;
  width: 8px;
  height: 8px;
  border: 1px solid white;
  background: var(--reject);
}
.heart-ring {
  position: absolute;
  width: 38px;
  height: 38px;
  border: 2px solid var(--reject);
  border-radius: 5px;
}

.control-guide {
  position: absolute;
  z-index: 3;
  inset-inline: var(--gutter);
  bottom: var(--gutter);
  display: grid;
  justify-items: center;
  gap: 12px;
  color: var(--reject);
  font-size: 14px;
  line-height: 1.35;
  text-align: center;
  pointer-events: none;
}

.guide-prompt, .guide-mobile { display: none; }
.control-guide[data-prompt="start"] .guide-start,
.control-guide[data-prompt="replay"] .guide-replay { display: block; }
.guide-prompt {
  padding: 8px 12px;
  border: 1px solid var(--inactive);
  border-radius: 8px;
  background: white;
  font-weight: 600;
  text-wrap: balance;
}
.guide-prompt p { margin: 0; }
.guide-prompt .guide-addresses { margin-top: 4px; font-size: 13px; }
.guide-key { white-space: nowrap; }
.guide-prompt .guide-tips { margin-top: 4px; font-size: 12px; font-weight: 500; }
.guide-replay p { margin-top: 4px; }

.guide-sides {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: 100%;
  gap: 32px;
}
.guide-side {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.guide-side kbd {
  display: grid;
  place-items: center;
  flex: 0 0 32px;
  height: 32px;
  border: 1px solid var(--inactive);
  border-radius: 6px;
  background: white;
  font: 600 20px/1 var(--font-body);
  transition: background-color 120ms ease-out, color 120ms ease-out;
}
.game[data-phase="running"][data-route="0"] .guide-side--left kbd,
.game[data-phase="running"][data-route="1"] .guide-side--right kbd {
  background: var(--reject);
  border-color: var(--reject);
  color: white;
}
.guide-symbols { position: absolute; }

@media (pointer: coarse) {
  .game { --guide-space: 0px; }
  .control-guide {
    bottom: calc(var(--gutter) + 36px + var(--field-height) - var(--viewport-height));
  }
  .control-guide[data-prompt="replay"] {
    bottom: calc(var(--gutter) + var(--field-height) - var(--viewport-height));
  }
  .control-guide[data-prompt="none"], .control-guide .guide-desktop { display: none; }
  .guide-mobile { display: block; }
  .guide-side {
    flex-direction: column;
    padding: 12px 6px 14px;
    border: 1px dashed var(--red);
    border-radius: 12px;
    background: #fff4f4;
    font-weight: 650;
  }
  .guide-finger {
    width: 58px;
    height: 58px;
    animation: guide-tap 1.32s ease-in-out 3;
  }
  .guide-side--right .guide-finger { animation-delay: .36s; }
}

@media (pointer: coarse) and (max-height: 630px) {
  .control-guide[data-prompt="start"],
  .control-guide[data-prompt="replay"] {
    top: calc(var(--gutter) + 56px);
    bottom: auto;
  }
  .guide-sides { gap: 16px; }
  .control-guide { gap: 8px; }
  .guide-side { padding: 6px 4px 8px; gap: 3px; }
  .guide-finger { width: 38px; height: 38px; }
}

@keyframes guide-tap {
  0%, 65%, 100% { transform: translateY(0); }
  22% { transform: translateY(-4px); }
  42% { transform: translateY(0) scale(.94); }
}

@media (prefers-reduced-motion: reduce) {
  .guide-finger { animation: none; }
  .guide-side kbd { transition: none; }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
