/* Sunset Circuit — a sunset arcade racer.
   The stage is a fixed-aspect box sized in script (see main.js): there is no
   robust pure-CSS way to fit a ratio inside a box bounded on both axes, and a
   flex item that grows makes `aspect-ratio` inert. Overlay chrome is then sized
   in container units so it scales against the stage rather than the viewport,
   which is what keeps the HUD off the road on a phone. */

/* `hidden` is only a user-agent rule, so any author `display` beats it. This
   one line is what keeps a panel with a layout class from staying on screen
   while `el.hidden` reads true. */
[hidden] {
  display: none !important;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --ink: #eef1fb;
  --dim: #a7a3c4;
  --pink: #ff4d6d;
  --cyan: #6ff0ff;
  --amber: #ffc857;
  --panel: rgba(17, 10, 38, 0.88);
  --edge: rgba(111, 240, 255, 0.28);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: #0a0620;
  color: var(--ink);
  font-family: var(--font);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

#app {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100dvh;
  padding: clamp(0px, 1.2vmin, 14px);
  background:
    radial-gradient(120% 90% at 50% 0%, #1e1044 0%, #0a0620 62%, #05030f 100%);
}

.stage {
  position: relative;
  /* Both dimensions are set from script; the ratio is enforced there. */
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  border-radius: clamp(6px, 1.4vmin, 16px);
  background: #140a2e;
  box-shadow:
    0 0 0 1px rgba(111, 240, 255, 0.16),
    0 24px 70px -24px rgba(0, 0, 0, 0.9);
  /* Overlay chrome sizes against this box, not the viewport. */
  container-type: size;
  container-name: stage;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#view {
  display: block;
  width: 100%;
  height: 100%;
}

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

/* ---------------------------------------------------------------- HUD --- */

.hud {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 2cqh;
  padding: clamp(8px, 3cqh, 26px);
  pointer-events: none;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
}

.hud-cell {
  display: flex;
  flex-direction: column;
  line-height: 1.05;
}

.hud-cell .label {
  font-size: clamp(8px, 2.4cqh, 13px);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--dim);
}

.hud-cell strong {
  font-size: clamp(18px, 6.6cqh, 42px);
  font-weight: 800;
  letter-spacing: -0.01em;
}

.hud-cell .sub {
  font-size: clamp(9px, 2.7cqh, 15px);
  color: var(--cyan);
  white-space: nowrap;
}

.hud-cell .sub.dim {
  color: var(--dim);
}

.hud-lap {
  grid-column: 1;
  grid-row: 1;
  align-items: flex-start;
}

.hud-clock {
  grid-column: 2;
  grid-row: 1;
  align-items: center;
  text-align: center;
}

.hud-clock strong {
  font-size: clamp(24px, 9cqh, 58px);
  color: var(--ink);
  transition: color 0.2s ease;
}

.hud-clock.is-low strong {
  color: var(--pink);
  animation: pulse 0.7s ease-in-out infinite;
}

.hud-clock .bar {
  width: clamp(90px, 26cqw, 260px);
  height: clamp(4px, 1.2cqh, 8px);
  margin-top: 0.6cqh;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  overflow: hidden;
}

.hud-clock .bar i {
  display: block;
  height: 100%;
  width: 100%;
  transform-origin: left center;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--cyan), var(--amber));
  transition: transform 0.12s linear, background 0.3s ease;
}

.hud-clock.is-low .bar i {
  background: linear-gradient(90deg, var(--pink), #ff9a5b);
}

.hud-score {
  grid-column: 3;
  grid-row: 1;
  align-items: flex-end;
  text-align: right;
}

.hud-score .sub {
  color: var(--amber);
  font-weight: 700;
}

.hud-speed {
  grid-column: 3;
  grid-row: 3;
  align-items: flex-end;
  text-align: right;
}

.hud-speed strong {
  font-size: clamp(26px, 10cqh, 64px);
  color: var(--cyan);
}

.hud-speed .sub {
  color: var(--amber);
}

@keyframes pulse {
  50% {
    opacity: 0.45;
  }
}

/* ------------------------------------------------------------- toasts --- */

.toasts {
  position: absolute;
  left: 50%;
  top: 26cqh;
  translate: -50% 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8cqh;
  pointer-events: none;
  z-index: 3;
}

.toast {
  padding: 0.7cqh 2cqh;
  border-radius: 999px;
  font-size: clamp(11px, 3.2cqh, 20px);
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: rgba(10, 6, 32, 0.72);
  border: 1px solid var(--edge);
  color: var(--cyan);
  animation: rise 0.35s ease-out both;
  white-space: nowrap;
}

.toast.gain {
  color: var(--amber);
  border-color: rgba(255, 200, 87, 0.4);
}

.toast.bad {
  color: var(--pink);
  border-color: rgba(255, 77, 109, 0.45);
}

.toast.leaving {
  animation: fade 0.35s ease-in both;
}

@keyframes rise {
  from {
    opacity: 0;
    translate: 0 14px;
  }
}

@keyframes fade {
  to {
    opacity: 0;
    translate: 0 -12px;
  }
}

/* ---------------------------------------------------------- countdown --- */

.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(48px, 30cqh, 190px);
  font-weight: 900;
  color: var(--ink);
  text-shadow: 0 0 40px rgba(255, 77, 109, 0.7);
  pointer-events: none;
  z-index: 4;
}

/* ------------------------------------------------------------- panels --- */

.panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2.2cqh;
  padding: clamp(14px, 5cqh, 44px);
  text-align: center;
  background: linear-gradient(180deg, rgba(10, 6, 32, 0.72), rgba(10, 6, 32, 0.93));
  backdrop-filter: blur(6px);
  z-index: 5;
}

.panel h2 {
  margin: 0;
  font-size: clamp(20px, 7cqh, 44px);
  letter-spacing: 0.02em;
}

.wordmark {
  margin: 0;
  font-size: clamp(30px, 13cqh, 92px);
  font-weight: 900;
  letter-spacing: 0.02em;
  line-height: 0.92;
  display: flex;
  flex-direction: column;
}

.wordmark span {
  color: var(--cyan);
  text-shadow: 0 0 26px rgba(111, 240, 255, 0.55);
}

.wordmark em {
  font-style: normal;
  color: var(--pink);
  text-shadow: 0 0 26px rgba(255, 77, 109, 0.6);
}

.tagline {
  margin: 0;
  max-width: 46ch;
  font-size: clamp(11px, 3.2cqh, 17px);
  line-height: 1.5;
  color: var(--dim);
}

button.primary,
button.ghost {
  font: inherit;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 1.6cqh 4cqh;
  font-size: clamp(11px, 3.2cqh, 18px);
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.2s ease, background 0.2s ease;
}

button.primary {
  border: 0;
  color: #14082a;
  background: linear-gradient(135deg, var(--cyan), #9ff7c9);
  box-shadow: 0 10px 30px -10px rgba(111, 240, 255, 0.8);
}

button.ghost {
  border: 1px solid var(--edge);
  background: transparent;
  color: var(--ink);
}

button.primary:hover,
button.ghost:hover {
  transform: translateY(-1px);
}

button.primary:active,
button.ghost:active {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 3px;
}

.keys {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6cqh 3cqh;
  margin: 0;
  font-size: clamp(9px, 2.7cqh, 14px);
  color: var(--dim);
  width: min(40ch, 100%);
}

.keys > div {
  display: flex;
  justify-content: space-between;
  gap: 1.4cqh;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 0.4cqh;
}

.keys dt {
  font-weight: 700;
  color: var(--ink);
}

.keys dd {
  margin: 0;
}

.record {
  margin: 0;
  font-size: clamp(10px, 2.9cqh, 15px);
  color: var(--amber);
  font-weight: 700;
}

.final-score {
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 0.8ch;
  font-size: clamp(30px, 12cqh, 76px);
  font-weight: 900;
  color: var(--amber);
  font-variant-numeric: tabular-nums;
}

.final-score small {
  font-size: clamp(10px, 2.8cqh, 15px);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--dim);
}

.summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1cqh 3cqh;
  margin: 0;
  width: min(40ch, 100%);
  font-size: clamp(10px, 2.9cqh, 15px);
}

.summary > div {
  display: flex;
  justify-content: space-between;
  gap: 1.4cqh;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 0.4cqh;
}

.summary dt {
  color: var(--dim);
}

.summary dd {
  margin: 0;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------ touch controls --- */

/* Steering on one side, brake on the other: the middle of the bottom edge is
   where the player's own car is drawn, and a control there hides it. */
.pads {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 2cqw;
  padding: 2cqh 2.5cqw calc(2cqh + env(safe-area-inset-bottom, 0px));
  z-index: 2;
}

.pad-cluster {
  display: flex;
  gap: 1.6cqw;
}

.pad {
  font: inherit;
  font-weight: 800;
  color: var(--ink);
  background: rgba(20, 12, 48, 0.45);
  border: 1px solid var(--edge);
  border-radius: 16px;
  width: clamp(52px, 15cqw, 92px);
  height: clamp(46px, 12cqh, 78px);
  font-size: clamp(14px, 4cqh, 24px);
  backdrop-filter: blur(3px);
  cursor: pointer;
  touch-action: none;
}

.pad-brake {
  font-size: clamp(9px, 2.4cqh, 14px);
  letter-spacing: 0.08em;
  width: clamp(66px, 19cqw, 110px);
  border-color: rgba(255, 77, 109, 0.45);
  color: var(--pink);
}

.pad.is-down {
  background: rgba(111, 240, 255, 0.22);
}

.corner-controls {
  position: absolute;
  right: clamp(8px, 3cqh, 26px);
  bottom: clamp(8px, 3cqh, 26px);
  display: flex;
  gap: 1.4cqh;
  z-index: 3;
}

.hud:not([hidden]) ~ .corner-controls {
  /* Every HUD corner is spoken for except this one: lap top-left, clock top
     centre, score top-right, speedometer bottom-right. */
  right: auto;
  left: clamp(8px, 3cqh, 26px);
}

/* With touch pads on screen the bottom strip belongs to them, so the HUD's
   bottom row and the small buttons both move up out of the way. */
.has-pads .hud {
  padding-bottom: calc(clamp(60px, 16cqh, 100px) + env(safe-area-inset-bottom, 0px));
}

.has-pads .corner-controls {
  bottom: calc(clamp(60px, 16cqh, 100px) + env(safe-area-inset-bottom, 0px));
}

.icon-btn {
  font: inherit;
  display: grid;
  place-items: center;
  width: clamp(26px, 7cqh, 42px);
  height: clamp(26px, 7cqh, 42px);
  font-size: clamp(11px, 3cqh, 17px);
  color: var(--ink);
  background: rgba(20, 12, 48, 0.6);
  border: 1px solid var(--edge);
  border-radius: 50%;
  cursor: pointer;
}

.icon-btn[aria-pressed="true"] {
  color: var(--dim);
  border-color: rgba(255, 255, 255, 0.14);
  text-decoration: line-through;
}

@media (prefers-reduced-motion: reduce) {
  .toast,
  .hud-clock.is-low strong {
    animation: none;
  }

  button.primary,
  button.ghost {
    transition: none;
  }
}
