html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1b1b1b;
  font-family: system-ui, sans-serif;
  color: #fff;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.hidden {
  display: none !important;
}

/* While the menu is open, hide the race UI. */
body.in-menu #hud,
body.in-menu #touch,
body.in-menu #countdown,
body.in-menu #controls {
  display: none;
}

#hud {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: 10px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 8px;
  font-size: 17px;
  line-height: 1.5;
  font-weight: 600;
  text-shadow: 0 1px 2px #000;
}

#countdown {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(80px, 22vmin, 140px);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 12px #000;
  pointer-events: none;
}

/* Screen flash when hit */
#flash {
  position: fixed;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}

/* Minimap and sound toggle, top right */
#minimap {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: 10px;
  left: auto;
  bottom: auto;
  width: 130px;
  height: 130px;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  pointer-events: none;
}

/* Sound and reset, stacked under the minimap in one column so they never overlap. */
#sideButtons {
  position: fixed;
  top: calc(max(10px, env(safe-area-inset-top)) + 146px);
  right: 10px;
  width: 130px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chip {
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(12, 12, 14, 0.62);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, background 0.2s;
}

.chip:active {
  transform: scale(0.96);
  background: rgba(255, 255, 255, 0.2);
}

body.in-menu #reset {
  display: none;
}

/* Strip along the bottom edge: the selected track and difficulty. */
#trackTag {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 30px;
  padding-bottom: env(safe-area-inset-bottom);
  box-sizing: content-box;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.35));
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.3px;
  color: #ffe082;
  pointer-events: none;
}

/* Lap banner */
#banner {
  position: fixed;
  top: 28%;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(34px, 9vmin, 64px);
  font-weight: 900;
  font-style: italic;
  color: #ffd54f;
  text-shadow: 0 4px 0 #b71c1c, 0 6px 14px #000;
  white-space: nowrap;
  pointer-events: none;
  animation: pop 0.35s ease-out;
}

@keyframes pop {
  from {
    transform: translateX(-50%) scale(1.6);
    opacity: 0;
  }
  to {
    transform: translateX(-50%) scale(1);
    opacity: 1;
  }
}

.item-slot {
  color: #ffd54f;
}

#countdown {
  transition: transform 0.1s linear;
}

.tips {
  font-size: 13px;
  opacity: 0.75;
  margin: 14px 0 0;
}

/* Results podium colours */
#standings li.place-1 {
  color: #ffd54f;
  font-weight: 800;
}

#standings li.place-2 {
  color: #e0e0e0;
}

#standings li.place-3 {
  color: #ffab91;
}

/* Difficulty menu */
#menu {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
}

#menu .panel,
#settings .panel,
#results .panel,
#pauseMenu .panel {
  width: min(92vw, 380px);
  max-height: 92vh;
  overflow-y: auto;
  padding: 24px 24px;
  background: linear-gradient(180deg, rgba(30, 30, 36, 0.96), rgba(10, 10, 12, 0.96));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
  text-align: center;
  box-sizing: border-box;
}

/* Finish pop-up: centred over the race, above the controls. */
#settings,
#results,
#pauseMenu {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
}

#menu h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 8vmin, 40px);
  color: #ffd54f;
  text-shadow: 0 3px 8px #000;
}

#menu .label {
  margin: 0;
  font-size: 15px;
  opacity: 0.85;
}

.diffs {
  display: flex;
  gap: 10px;
  margin: 14px 0 8px;
}

.diff,
.track,
.kart {
  flex: 1;
  padding: 12px 0;
  border-radius: 12px;
  border: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.06));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.3), 0 4px 10px rgba(0, 0, 0, 0.35);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.2s, background 0.2s;
}

.diff:active,
.track:active,
.kart:active {
  transform: scale(0.96);
}

.diff.active,
.track.active,
.kart.active {
  background: linear-gradient(180deg, #ffe35c, #f9a825);
  color: #2a1d00;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.6), 0 6px 18px rgba(255, 193, 7, 0.5);
}

#kartHelp {
  min-height: 22px;
  margin: 6px 0 4px;
  font-size: 14px;
  opacity: 0.85;
}

#diffHelp {
  min-height: 44px;
  margin: 6px 0 16px;
  font-size: 15px;
  opacity: 0.85;
}

#controlHelp {
  min-height: 44px;
  margin: 6px 0 16px;
  font-size: 15px;
  opacity: 0.85;
}

/* With tilt steering the left and right arrows are not needed. Fire, brake and gas stay. */
body.tilt-mode #touch .left-pad .row {
  display: none;
}

.primary,
.secondary {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.4px;
  padding: 13px 18px;
  border-radius: 12px;
  border: 0;
  color: #fff;
  cursor: pointer;
  transition: transform 0.12s;
}

.primary:active,
.secondary:active {
  transform: scale(0.96);
}

.primary {
  background: linear-gradient(180deg, #ff5a52, #c62828);
  box-shadow: 0 6px 16px rgba(229, 57, 53, 0.45);
}

.secondary {
  background: rgba(255, 255, 255, 0.14);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.3);
}

/* Results */
#settings h2,
#results h2,
#pauseMenu h2 {
  margin: 0 0 8px;
  font-size: clamp(26px, 7vmin, 34px);
  color: #ffd54f;
  text-shadow: 0 3px 8px #000;
}

#resultStatus {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 700;
}

#resultTrack {
  margin: 0 0 14px;
  font-size: 14px;
  opacity: 0.7;
}

#standings {
  list-style: none;
  text-align: left;
  padding-left: 0;
  margin: 0 0 18px;
  font-size: 18px;
  line-height: 1.7;
}

#results .row,
#pauseMenu .row {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

#results .row button,
#pauseMenu .row button {
  flex: 1 1 140px;
}

#pauseMenu .volume {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 14px 0;
  color: #fff;
  font-weight: 600;
}

#pauseMenu input[type='range'] {
  width: 100%;
}

/* On-screen controls for phones and tablets. */
#touch {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(30px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  pointer-events: none;
}

#touch .pad {
  display: flex;
  gap: 14px;
}

/* Left side: the fire button sits on top of the steering arrows. */
#touch .left-pad {
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#touch .left-pad .row {
  display: flex;
  gap: 14px;
}

/* Fire shows the active item's name; the round swap button beside it swaps the two items. */
#touch .item-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.btn.fire {
  width: clamp(96px, 28vmin, 124px);
  height: clamp(48px, 13vmin, 60px);
  border-radius: 28px;
  background: rgba(255, 143, 0, 0.7);
  font-size: clamp(13px, 3.6vmin, 16px);
  letter-spacing: 0;
  white-space: nowrap;
  padding: 0 6px;
}

.btn.swap {
  width: clamp(48px, 13vmin, 60px);
  height: clamp(48px, 13vmin, 60px);
  font-size: clamp(22px, 6vmin, 28px);
  background: rgba(30, 136, 229, 0.6);
}

/* The ability button: a ring that fills over 15 s, and glows when the ability is ready. */
.btn.ability {
  position: relative;
  width: clamp(54px, 15vmin, 68px);
  height: clamp(54px, 15vmin, 68px);
  border: none;
  padding: 0;
  background: conic-gradient(#ffd54f calc(var(--fill, 0) * 1turn), rgba(255, 255, 255, 0.25) 0);
  font-size: clamp(10px, 2.8vmin, 12px);
  letter-spacing: 0;
}

.btn.ability::before {
  content: '';
  position: absolute;
  inset: 5px;
  border-radius: 50%;
  background: rgba(20, 22, 34, 0.9);
}

.btn.ability span {
  position: relative;
  opacity: 0.6;
}

.btn.ability.ready {
  box-shadow: 0 0 14px 4px rgba(255, 213, 79, 0.9);
}

.btn.ability.ready span {
  opacity: 1;
}

.btn:disabled {
  opacity: 0.35;
  filter: grayscale(1);
}

.btn {
  width: clamp(64px, 20vmin, 96px);
  height: clamp(64px, 20vmin, 96px);
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: clamp(16px, 4.5vmin, 22px);
  font-weight: 800;
  pointer-events: auto;
  touch-action: none;
  -webkit-touch-callout: none;
  cursor: pointer;
}

.btn.gas {
  background: rgba(67, 160, 71, 0.6);
}

.btn.brake {
  background: rgba(229, 57, 53, 0.55);
}

.btn.active {
  filter: brightness(1.6);
  transform: scale(0.95);
}

#controls {
  position: fixed;
  bottom: calc(40px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 6px;
  font-size: 14px;
  opacity: 0.85;
  white-space: nowrap;
}

/* Touch devices get on-screen buttons, so the keyboard hint is not needed there. */
@media (pointer: coarse) {
  #controls {
    display: none;
  }
}

/* Settings screen, opened from the gear on the main menu. It sits above the menu. */
#settings {
  z-index: 45;
}

#menu .panel,
#settings .panel {
  position: relative;
}

.gear {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 24px;
  line-height: 44px;
  padding: 0;
  cursor: pointer;
}

.gear:active {
  transform: scale(0.94);
}

#settings .field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 14px 0;
  color: #fff;
  font-weight: 600;
  text-align: left;
}

#nameInput {
  font-size: 18px; /* 16px or more stops iOS from zooming in on focus */
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

#settingsDone {
  width: 100%;
  margin-top: 12px;
}
