@font-face {
  font-family: "Obcoro Pixel";
  src: url("../../assets/fusion-pixel-12px-monospaced-zh_tw.ttf.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}

:root {
  --bg: #030909;
  --panel: #040b09f7;
  --ink: #70f995;
  --bright: #9affb1;
  --muted: #599d72;
  --line: #2a6940;
  --accent: #fa785c;
  --pad-top: max(.75rem, env(safe-area-inset-top));
  --pad-bottom: max(.75rem, env(safe-area-inset-bottom));
  --gutter: 0px;
  --apron: 0px;
  color-scheme: dark;
  font: 400 16px/1.5 "Obcoro Pixel", monospace;
  font-synthesis: none;
  color: var(--ink);
  background: var(--bg);
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: end center;
  padding: var(--pad-top) max(var(--gutter), env(safe-area-inset-right))
           var(--pad-bottom) max(var(--gutter), env(safe-area-inset-left));
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

main {
  display: flex;
  flex-direction: column;
  /* Budget for the header, feedback row and footer even in a shallow viewport. */
  width: min(100%, 39rem, calc(100dvh - var(--pad-top) - var(--pad-bottom) - 9rem));
  min-width: 0;
}
@media (min-width: 45rem), (orientation: landscape) {
  :root {
    --gutter: .75rem;
    --apron: clamp(8px, 2vw, 24px);
  }
  body { place-items: center; }
  main { width: min(100%, 39rem, calc(100dvh - var(--pad-top) - var(--pad-bottom) - 11.5rem)); }
}
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .625rem;
  padding: 0 max(0px, calc(.5rem - var(--gutter))) .625rem;
  border-bottom: 1px solid var(--line);
}
h1 {
  flex: none;
  margin: 0;
  color: var(--bright);
  font-size: clamp(1.75rem, 6vw, 2.25rem);
  line-height: 1.15;
  font-weight: 400;
  letter-spacing: .06em;
}
#level {
  display: inline-block;
  padding: 0 .42em;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #09170f;
  font-size: .875rem;
  white-space: nowrap;
}
#status {
  display: flex;
  align-self: center;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  max-width: 100%;
  margin: .25rem 0 0;
  padding: 0 .75rem;
  color: var(--muted);
  font-size: clamp(.875rem, 2.9vw, 1rem);
  text-align: center;
}
#status[data-tone="success"] { color: var(--bright); }
#status[data-tone="error"] { color: var(--accent); }
.round-action {
  min-width: 44px;
  min-height: 44px;
  padding-inline: .75rem;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.combo-mark {
  display: inline-block;
  margin-inline-start: .45em;
  color: var(--ink);
  white-space: nowrap;
  transform-origin: right center;
  animation: combo-pop 180ms cubic-bezier(.2,.75,.3,1);
}
.combo-mark::before {
  content: "";
  display: inline-block;
  width: 2px;
  height: .65em;
  margin-right: 10px;
  background: currentColor;
}
.combo-mark[data-tier="2"] { color: var(--bright); --pop: 1.09; }
.combo-mark[data-tier="3"] { color: var(--bright); --pop: 1.13; }
.combo-mark[data-tier="2"]::before { box-shadow: 3px 0 currentColor; }
.combo-mark[data-tier="3"]::before { box-shadow: 3px 0 currentColor, 6px 0 currentColor; }
@keyframes combo-pop {
  0% { opacity: .65; transform: scale(.94); }
  45% { opacity: 1; transform: scale(var(--pop, 1.06)); }
  100% { transform: scale(1); }
}
footer {
  margin-top: .25rem;
  color: var(--muted);
  font-size: .75rem;
  letter-spacing: .04em;
  text-align: center;
}

#board {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  overflow: hidden;
  padding: var(--apron);
  border-block: 1px solid var(--line);
  background: var(--panel);
  touch-action: none;
}
@media (min-width: 45rem), (orientation: landscape) {
  #board { border-inline: 1px solid var(--line); }
}
#stage {
  position: relative;
  isolation: isolate;
  width: 100%;
  aspect-ratio: 1;
}
#lot {
  --cell: calc(100% / var(--size));
  position: relative;
  isolation: isolate;
  overflow: hidden;
  width: 100%;
  height: 100%;
  outline: 1px solid var(--line);
}
#asphalt {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#asphalt, #exit-gate, .car canvas { image-rendering: pixelated; }

#exit-gate {
  position: absolute;
  z-index: 2;
  width: calc(82% / var(--size));
  height: auto;
  transform-origin: 50% 0;
  transform: translateX(-50%) rotate(var(--turn));
  pointer-events: none;
}
#exit-gate.spent { opacity: 0; transition: opacity 150ms ease-out; }

.car {
  --angle: 0deg;
  appearance: none;
  position: absolute;
  left: calc(var(--x) * var(--cell));
  top: calc(var(--y) * var(--cell));
  width: calc(2 * var(--cell));
  height: var(--cell);
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: pointer;
  touch-action: none;
}
.west { --angle: 180deg; }
.north { --angle: -90deg; }
.south { --angle: 90deg; }
.north, .south { width: var(--cell); height: calc(2 * var(--cell)); }

/* Keep the complete two-cell button as the hit target in all four directions. */
.car canvas {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 100%;
  height: 100%;
  transform: translate(-50%, -50%) rotate(var(--angle));
  pointer-events: none;
}
:is(.north, .south) canvas { width: 200%; height: 50%; }
.car:focus-visible {
  z-index: 1;
  outline: 2px solid var(--bright);
  outline-offset: -2px;
}
@media (hover: hover) {
  .car:hover:not(:focus-visible) { outline: 1px solid var(--muted); outline-offset: -1px; }
}
.car.pressed canvas { animation: car-press 158ms cubic-bezier(.22,.7,.26,1); }
@keyframes car-press {
  0% { transform: translate(-50%, -50%) rotate(var(--angle)) scale(.90); }
  54% { transform: translate(-50%, -50%) rotate(var(--angle)) scale(1.035); }
}
.car.moving { z-index: 1; }
.bumped { animation: bumper-jolt 116ms ease-out; }
@keyframes bumper-jolt {
  30% { transform: translate(var(--nudge-x), var(--nudge-y)); }
  100% { transform: translate(0, 0); }
}
#board.impact { animation: lot-shake 125ms ease-out; }
@keyframes lot-shake {
  0%, 100% { transform: translate(0, 0); }
  22% { transform: translate(var(--shake-x), var(--shake-y)); }
  55% { transform: translate(calc(-.55 * var(--shake-x)), calc(-.55 * var(--shake-y))); }
}
@media (prefers-reduced-motion: reduce) {
  .car.pressed canvas { animation: still-press 90ms ease-out; }
  @keyframes still-press { from { opacity: .68; } to { opacity: 1; } }
  #board.impact, .bumped { animation: none; }
  #exit-gate.spent { transition-duration: 75ms; }
  .combo-mark { animation: reveal 90ms ease-out; }
  @keyframes reveal { from { opacity: .5; } to { opacity: 1; } }
}

.fx-canvas, .fx-labels {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}
.fx-canvas { z-index: 2; }
.fx-labels { z-index: 3; }
.score-float {
  position: absolute;
  padding: 1px .16em;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  color: var(--bright);
  font-size: clamp(.875rem, 4vw, 1.125rem);
  line-height: 1.15;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, -50%);
}
.score-float:not([hidden]) { animation: score-lift var(--lifetime) ease-out; }
@keyframes score-lift {
  from { opacity: 1; transform: translate(-50%, -38%) scale(var(--punch)); }
  22% { opacity: 1; transform: translate(-50%, -50%); }
  69% { opacity: 1; transform: translate(-50%, calc(-50% - var(--rise))); }
  to { opacity: 0; transform: translate(-50%, calc(-50% - var(--rise))); }
}
@media (prefers-reduced-motion: reduce) {
  .score-float:not([hidden]) { animation: score-fade var(--lifetime) ease-out; }
  @keyframes score-fade { 0%, 65% { opacity: 1; } to { opacity: 0; } }
}
