:root {
  --ink: #1d2733;
  --ink-soft: #43536b;
  --panel: rgba(255, 255, 255, 0.82);
  --panel-border: rgba(29, 39, 51, 0.1);
  --accent: #e8552f;
  --accent-dark: #b83c1e;
  --fuel: #59b35c;
  --fuel-low: #e8552f;
  --shadow: 0 6px 18px rgba(29, 39, 51, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #0f1720;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
}

#app {
  height: 100dvh;
  display: flex;
}

.stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  touch-action: none;
}

canvas[data-testid='game-canvas'] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

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

/* ---------- HUD ---------- */
.hud {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
}

.hud-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: stretch;
  min-width: 0;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 7px 12px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 74px;
}

.stat-label {
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-weight: 700;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  white-space: nowrap;
}

.stat-value.seed {
  font-size: 0.85rem;
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
}

[data-status='crashed'] .stat-value.status {
  color: var(--accent-dark);
}
[data-status='out-of-fuel'] .stat-value.status {
  color: #b97f17;
}

.fuel-track {
  margin-top: 3px;
  height: 6px;
  border-radius: 3px;
  background: rgba(29, 39, 51, 0.12);
  overflow: hidden;
}

.fuel-fill {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 3px;
  background: var(--fuel);
  transform-origin: left center;
  transition: transform 0.15s linear;
}

.fuel-fill.low {
  background: var(--fuel-low);
}

.restart {
  pointer-events: auto;
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0 16px;
  font: inherit;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, background 0.15s ease;
}

.restart:hover {
  background: #2c3b4e;
}
.restart:active {
  transform: scale(0.95);
}
.restart:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Driving controls ---------- */
.controls {
  position: absolute;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.control {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 116px;
  padding: 16px 22px;
  border-radius: 18px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow);
  font: inherit;
  font-weight: 800;
  font-size: 1rem;
  color: var(--ink);
  cursor: pointer;
  touch-action: none;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.control-icon {
  font-size: 1.1rem;
}

.control.accelerate {
  background: rgba(232, 85, 47, 0.92);
  border-color: rgba(184, 60, 30, 0.5);
  color: #fff;
}

.control.accelerate .control-icon {
  transform: rotate(-90deg);
}

.control.pressed,
.control:active {
  transform: scale(0.94);
  box-shadow: 0 2px 8px rgba(29, 39, 51, 0.25);
}

.control.accelerate.pressed {
  background: var(--accent-dark);
}
.control.brake.pressed {
  background: rgba(200, 210, 224, 0.95);
}

.control:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- End overlay ---------- */
.end-overlay[hidden] {
  display: none;
}

.end-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(15, 23, 32, 0.35);
  backdrop-filter: blur(2px);
  animation: fade-in 0.35s ease;
}

.end-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 26px 34px;
  text-align: center;
  max-width: min(420px, 86vw);
  animation: pop-in 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.end-card h2 {
  margin: 0 0 8px;
  font-size: 1.7rem;
  color: var(--accent-dark);
}

.end-card p {
  margin: 0 0 10px;
  color: var(--ink-soft);
  font-weight: 600;
}

.end-hint {
  font-size: 0.82rem;
  opacity: 0.75;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}
@keyframes pop-in {
  from {
    transform: scale(0.85) translateY(14px);
    opacity: 0;
  }
}

/* ---------- Phone-width layout ---------- */
@media (max-width: 560px) {
  .hud {
    flex-direction: column;
    align-items: stretch;
  }
  .hud-group {
    justify-content: space-between;
  }
  .hud-group.meta {
    align-items: center;
  }
  .stat {
    min-width: 0;
    flex: 1;
    padding: 5px 9px;
  }
  .stat-value {
    font-size: 0.92rem;
  }
  .restart {
    padding: 10px 14px;
  }
  .control {
    min-width: 96px;
    padding: 14px 16px;
    font-size: 0.9rem;
  }
}
