* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0b1620;
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  user-select: none;
}

.hidden {
  display: none;
}

#canvas-container {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: #bcd9f0;
}

#canvas-container canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#hud {
  position: fixed;
  top: 16px;
  left: 16px;
  right: 16px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 10;
}

.hud-row {
  display: flex;
  gap: 10px;
}

.hud-box {
  background: rgba(10, 16, 24, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 8px 16px;
  min-width: 90px;
  text-align: center;
  backdrop-filter: blur(4px);
}

.hud-box .label {
  display: block;
  font-size: 11px;
  letter-spacing: 1.5px;
  color: #8fa3b3;
  font-weight: 600;
}

.hud-box .value {
  display: block;
  font-size: 20px;
  color: #f4f7fa;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

.hud-row-right {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  pointer-events: auto;
}

#flightHud:not(.hidden) {
  display: flex;
  gap: 10px;
}

#muteBtn {
  background: rgba(10, 16, 24, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 16px;
  cursor: pointer;
  backdrop-filter: blur(4px);
  transition: transform 0.15s ease, background 0.15s ease;
}

#muteBtn:hover {
  background: rgba(30, 47, 63, 0.85);
  transform: translateY(-1px);
}

#onFootLabel {
  background: rgba(10, 16, 24, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 12px 20px;
  color: #8fd3ff;
  font-weight: 800;
  letter-spacing: 2px;
  font-size: 15px;
  backdrop-filter: blur(4px);
}

#wantedIndicator {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 0, 0, 0.7);
  border: 1px solid rgba(255, 60, 60, 0.5);
  color: #ff4d4d;
  font-weight: 800;
  letter-spacing: 3px;
  font-size: 16px;
  padding: 8px 20px;
  border-radius: 8px;
  z-index: 15;
  animation: wantedPulse 0.6s infinite alternate;
}

@keyframes wantedPulse {
  from {
    opacity: 0.6;
    box-shadow: 0 0 8px rgba(255, 40, 40, 0.4);
  }
  to {
    opacity: 1;
    box-shadow: 0 0 22px rgba(255, 40, 40, 0.8);
  }
}

#actionHint {
  position: fixed;
  bottom: 34px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(10, 16, 24, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  padding: 8px 18px;
  color: #f4f7fa;
  font-size: 14px;
  font-weight: 600;
  z-index: 12;
  transition: opacity 0.2s ease;
}

#fineToast {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(40, 10, 10, 0.85);
  border: 1px solid rgba(255, 120, 80, 0.5);
  color: #ffb37a;
  font-weight: 800;
  letter-spacing: 1px;
  font-size: 14px;
  padding: 8px 18px;
  border-radius: 8px;
  z-index: 16;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

#fineToast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(-8px);
  pointer-events: none;
}

#garageBtn {
  background: rgba(10, 16, 24, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 10px 16px;
  color: #f4f7fa;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  backdrop-filter: blur(4px);
  transition: transform 0.15s ease, background 0.15s ease;
}

#garageBtn:hover {
  background: rgba(30, 47, 63, 0.85);
  transform: translateY(-1px);
}

#speedometer {
  background: rgba(10, 16, 24, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 10px 20px;
  text-align: right;
  backdrop-filter: blur(4px);
}

#speedValue {
  font-size: 32px;
  font-weight: 800;
  color: #ffd166;
  font-variant-numeric: tabular-nums;
}

.speed-unit {
  font-size: 12px;
  color: #8fa3b3;
  margin-left: 4px;
}

#boostMeter {
  width: 100%;
  height: 5px;
  margin-top: 6px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 999px;
}

#boostFill {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #33d6ff, #8bffea);
  border-radius: inherit;
  transition: width 0.08s linear;
}

.boost-label {
  display: block;
  margin-top: 3px;
  color: #69e4ff;
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 1px;
}

#message {
  position: fixed;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 48px;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
  z-index: 20;
  text-align: center;
  letter-spacing: 2px;
  transition: opacity 0.4s ease;
}

#message.hidden {
  opacity: 0;
  pointer-events: none;
}

#startScreen {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 30%, #1b2c3d, #0b1620 70%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 30;
  color: #f4f7fa;
  text-align: center;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

#startScreen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#startScreen h1 {
  font-size: 56px;
  letter-spacing: 6px;
  font-weight: 800;
  color: #ffd166;
  text-shadow: 0 6px 30px rgba(255, 209, 102, 0.35);
}

#startScreen p {
  margin-top: 12px;
  color: #a9bccb;
  font-size: 16px;
}

.controls {
  margin-top: 28px;
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 10px 32px;
  font-size: 14px;
  color: #cdd8e2;
}

.controls-note {
  grid-column: 1 / -1;
  max-width: 460px;
  line-height: 1.5;
  color: #9fb3c3;
  font-size: 13px;
}

.controls kbd {
  background: #1e2f3f;
  border: 1px solid #3a4f63;
  border-radius: 5px;
  padding: 2px 7px;
  margin-right: 4px;
  font-size: 12px;
  color: #f4f7fa;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.3);
}

#startBtn {
  margin-top: 36px;
  background: linear-gradient(180deg, #ffd166, #f4a531);
  border: none;
  border-radius: 10px;
  padding: 14px 42px;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #2b1c00;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(244, 165, 49, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

#startBtn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 28px rgba(244, 165, 49, 0.45);
}

#startBtn:active {
  transform: translateY(0);
}

#garageOverlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 10, 16, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

#garageOverlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#garagePanel {
  background: linear-gradient(180deg, #16232f, #0f1a24);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 24px 28px;
  width: min(880px, 92vw);
  max-height: 84vh;
  overflow-y: auto;
  color: #f4f7fa;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

#garageHeader {
  display: flex;
  align-items: center;
  gap: 16px;
}

#garageHeader h2 {
  font-size: 26px;
  letter-spacing: 3px;
  color: #ffd166;
  font-weight: 800;
  flex: 1;
}

#garageCoins {
  background: rgba(255, 209, 102, 0.12);
  border: 1px solid rgba(255, 209, 102, 0.3);
  border-radius: 8px;
  padding: 6px 14px;
  text-align: center;
}

#garageCoins .label {
  display: block;
  font-size: 10px;
  letter-spacing: 1.5px;
  color: #d8b25a;
  font-weight: 700;
}

#garageCoins .value {
  display: block;
  font-size: 18px;
  font-weight: 800;
  color: #ffd166;
}

#garageCloseBtn {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  color: #f4f7fa;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

#garageCloseBtn:hover {
  background: rgba(255, 255, 255, 0.16);
}

.garageHint {
  margin-top: 8px;
  margin-bottom: 20px;
  color: #8fa3b3;
  font-size: 13px;
}

#garageGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 16px;
}

.carCard {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.carCard.selected {
  border-color: #ffd166;
  box-shadow: 0 0 0 1px rgba(255, 209, 102, 0.4);
}

.carCard.locked {
  opacity: 0.85;
}

.carSwatch {
  width: 100%;
  height: 54px;
  border-radius: 8px;
  position: relative;
  overflow: hidden;
}

.carSwatch .cabin {
  position: absolute;
  top: 12px;
  left: 26%;
  width: 48%;
  height: 22px;
  background: rgba(20, 30, 40, 0.55);
  border-radius: 5px 5px 0 0;
}

.carName {
  font-size: 15px;
  font-weight: 800;
  color: #f4f7fa;
}

.carStat {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
  color: #8fa3b3;
}

.carStat .statLabel {
  width: 44px;
  flex-shrink: 0;
  letter-spacing: 0.5px;
}

.carStat .statBar {
  flex: 1;
  height: 6px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
  overflow: hidden;
}

.carStat .statFill {
  height: 100%;
  background: linear-gradient(90deg, #ffd166, #f4a531);
  border-radius: 4px;
}

.carActionRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
}

.carCost {
  font-size: 12px;
  color: #ffd166;
  font-weight: 700;
}

.carBtn {
  border: none;
  border-radius: 7px;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  letter-spacing: 0.5px;
}

.carBtn.select {
  background: linear-gradient(180deg, #ffd166, #f4a531);
  color: #2b1c00;
}

.carBtn.selected {
  background: rgba(255, 209, 102, 0.15);
  color: #ffd166;
  border: 1px solid rgba(255, 209, 102, 0.4);
  cursor: default;
}

.carBtn.unlock {
  background: rgba(255, 255, 255, 0.1);
  color: #f4f7fa;
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.carBtn.unlock:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Touch controls stay out of desktop play but become the primary interface
   on phones, tablets, and narrow embedded browser windows. */
#touchControls {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 18;
  pointer-events: none;
}

.flight-only {
  display: none;
}

body[data-mode="flying"] .flight-only {
  display: inline-flex;
}

body[data-mode="flying"] #touchControls > .touch-actions .drive-only,
body[data-mode="flying"] .touch-boost .drive-only {
  display: none;
}

.touch-cluster,
.touch-actions {
  position: absolute;
  display: flex;
  gap: 10px;
  pointer-events: none;
}

.touch-steering {
  left: max(14px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
}

.touch-pedals {
  right: max(14px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: repeat(2, 68px);
}

.touch-actions {
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
}

.touch-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 68px;
  height: 68px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 18px;
  background: rgba(9, 18, 29, 0.7);
  color: #fff;
  font-size: 24px;
  font-weight: 900;
  backdrop-filter: blur(5px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-btn.active {
  background: rgba(255, 209, 102, 0.9);
  border-color: #fff0bc;
  color: #302000;
  transform: scale(0.94);
}

.touch-btn.flight-only,
.touch-btn .flight-only {
  display: none;
}

body[data-mode="flying"] .touch-btn.flight-only,
body[data-mode="flying"] .touch-btn .flight-only {
  display: inline-flex;
}

.touch-small {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  font-size: 14px;
}

.touch-wide {
  font-size: 16px;
}

@media (pointer: coarse), (max-width: 800px) {
  #touchControls {
    display: block;
  }

  #hud {
    top: max(8px, env(safe-area-inset-top));
    left: 8px;
    right: 8px;
  }

  .hud-row {
    gap: 4px;
  }

  .hud-box {
    min-width: 0;
    padding: 6px 8px;
    border-radius: 8px;
  }

  .hud-box .label {
    font-size: 8px;
    letter-spacing: 0.8px;
  }

  .hud-box .value {
    font-size: 14px;
  }

  #hud > .hud-row .hud-box:nth-child(2) {
    display: none;
  }

  .hud-row-right {
    gap: 4px;
  }

  #muteBtn,
  #garageBtn {
    height: 38px;
    padding: 7px 9px;
    border-radius: 8px;
  }

  #garageBtn {
    width: 40px;
    overflow: hidden;
    font-size: 0;
  }

  #garageBtn::before {
    content: "🚗";
    font-size: 17px;
  }

  #speedometer {
    padding: 5px 9px;
    border-radius: 8px;
  }

  #speedValue {
    font-size: 22px;
  }

  .boost-label {
    font-size: 6px;
  }

  #wantedIndicator {
    top: 58px;
    font-size: 12px;
  }

  #actionHint {
    bottom: 102px;
    max-width: 70vw;
    padding: 6px 12px;
    font-size: 11px;
    text-align: center;
  }

  #startScreen {
    padding: 18px;
  }

  #startScreen h1 {
    font-size: clamp(36px, 13vw, 56px);
    letter-spacing: 4px;
  }

  #startScreen p {
    font-size: 13px;
  }

  .controls {
    margin-top: 18px;
    gap: 8px 14px;
    font-size: 11px;
  }

  .controls-note {
    max-width: 90vw;
    font-size: 10px;
  }

  #startBtn {
    margin-top: 20px;
    padding: 12px 34px;
  }

  #garagePanel {
    width: 96vw;
    max-height: 90vh;
    padding: 18px 14px;
  }

  #garageGrid {
    grid-template-columns: repeat(2, minmax(130px, 1fr));
    gap: 10px;
  }
}

@media (max-width: 560px) {
  .touch-actions {
    bottom: max(96px, calc(env(safe-area-inset-bottom) + 82px));
  }

  .touch-small {
    width: 46px;
    height: 46px;
  }

  .touch-steering {
    gap: 7px;
  }

  .touch-btn {
    width: 60px;
    height: 60px;
  }

  .touch-pedals {
    grid-template-columns: repeat(2, 60px);
    gap: 7px;
  }
}

@media (max-height: 600px) and (orientation: landscape) {
  #startScreen {
    justify-content: flex-start;
    overflow-y: auto;
  }

  #startScreen h1 {
    margin-top: 8px;
    font-size: 34px;
  }

  .controls-note {
    display: none;
  }

  #startBtn {
    margin: 14px 0 20px;
  }

  .touch-btn {
    width: 54px;
    height: 54px;
  }

  .touch-pedals {
    grid-template-columns: repeat(2, 54px);
  }
}
