* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #111;
  font-family: Nunito, "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
}

.hidden { display: none !important; }

/* ---------- Screens ---------- */

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 20px;
  background: radial-gradient(ellipse at center, rgba(8, 10, 24, 0.2), rgba(0, 0, 0, 0.62));
  overflow-y: auto;
  animation: rise 0.35s ease;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: none; }
}

.logo, .panel h2, .hud-pos, #countdown, #toast, .code, #hudLap {
  font-family: Teko, Impact, "Arial Narrow", sans-serif;
}

.logo {
  margin: 0;
  font-size: clamp(42px, 8vw, 84px);
  font-style: italic;
  letter-spacing: 3px;
  color: #ffd83b;
  text-shadow: 4px 4px 0 #d62828, 8px 8px 0 rgba(0, 0, 0, 0.35);
}
.logo span { color: #fff; }

.panel {
  position: relative;
  width: min(380px, 100%);
  background: linear-gradient(180deg, rgba(22, 16, 40, 0.94), rgba(10, 12, 28, 0.94));
  border: 3px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}
.panel::before {
  content: "";
  height: 8px;
  margin: -20px -22px 4px;
  background: repeating-linear-gradient(90deg, #e53935 0 18px, #ffd83b 18px 36px, #1e88e5 36px 54px);
}
.panel.wide { width: min(560px, 100%); }
.panel h2 { margin: 0; text-align: center; font-size: 30px; color: #ffd83b; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #aab4d4;
}

input, select {
  font: inherit;
  font-size: 18px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 2px solid #3a4470;
  background: #0c1024;
  color: #fff;
  outline: none;
  text-transform: none;
  letter-spacing: normal;
}
input:focus, select:focus { border-color: #ffd83b; }

.colors { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  padding: 0;
}
.swatch.selected { border-color: #fff; transform: scale(1.12); }

.btn {
  font: inherit;
  font-weight: bold;
  font-size: 18px;
  padding: 12px 18px;
  border: none;
  border-radius: 12px;
  background: #ffd83b;
  color: #1b1b1b;
  cursor: pointer;
  box-shadow: 0 4px 0 #b08d00;
  transition: transform 0.05s;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #b08d00; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.big { font-size: 22px; padding: 14px; background: #ff4b3e; color: #fff; box-shadow: 0 5px 0 #a3231a; }
.btn.small { font-size: 14px; padding: 8px 12px; }
.btn.ghost { background: transparent; color: #aab4d4; box-shadow: none; border: 2px solid #3a4470; }

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #7c86a8;
  font-size: 13px;
}
.divider::before, .divider::after { content: ""; flex: 1; height: 2px; background: #2a3258; }

.join-row { display: flex; gap: 8px; }
.join-row input {
  flex: 1;
  min-width: 0;
  text-transform: uppercase;
  letter-spacing: 6px;
  font-size: 22px;
  text-align: center;
  font-weight: bold;
}

.status { min-height: 18px; font-size: 14px; color: #ff9d8f; text-align: center; }
.status.ok { color: #8fffb0; }

.menu-row { display: flex; gap: 8px; }
.menu-row .btn { flex: 1; }
.picker-row { display: flex; flex-wrap: wrap; gap: 6px; }
.pick {
  border: 2px solid #3a4470;
  background: #0c1024;
  color: #fff;
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}
.pick.selected { border-color: #ffd83b; background: rgba(255, 216, 59, 0.15); }
.stats { display: grid; gap: 6px; font-size: 12px; text-transform: uppercase; color: #aab4d4; }
.stats div { display: grid; grid-template-columns: 70px 1fr; align-items: center; gap: 8px; }
.stats i {
  display: block;
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(90deg, #ff4b3e, #ffd83b);
  width: 50%;
}
.field.check { flex-direction: row; align-items: center; gap: 10px; text-transform: none; }
.bar { height: 12px; background: #0c1024; border-radius: 8px; overflow: hidden; border: 2px solid #3a4470; }
.bar i { display: block; height: 100%; width: 0%; background: #ffd83b; transition: width 0.2s; }
#resultsPodium { display: flex; justify-content: center; gap: 12px; align-items: flex-end; min-height: 90px; margin: 8px 0 14px; }
#resultsPodium .pod {
  width: 90px; text-align: center; border-radius: 12px 12px 0 0; padding: 10px 6px; background: rgba(255,255,255,0.08);
}
#resultsPodium .pod.p1 { height: 90px; background: rgba(255,216,59,0.25); }
#resultsPodium .pod.p2 { height: 70px; }
#resultsPodium .pod.p3 { height: 55px; }
#resultsPodium .dot { width: 18px; height: 18px; border-radius: 50%; display: inline-block; }
.help {
  text-align: center;
  font-size: 13px;
  color: #dfe5ff;
  background: rgba(0, 0, 0, 0.45);
  padding: 8px 14px;
  border-radius: 10px;
  line-height: 1.7;
}

.code-label { text-align: center; color: #aab4d4; text-transform: uppercase; font-size: 13px; letter-spacing: 2px; }
.code {
  text-align: center;
  font-size: 56px;
  font-weight: bold;
  letter-spacing: 12px;
  color: #ffd83b;
  text-shadow: 3px 3px 0 #000;
  user-select: text;
  -webkit-user-select: text;
}
.hint { text-align: center; font-size: 14px; color: #aab4d4; }

.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.06);
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 18px;
}
.players .dot { width: 18px; height: 18px; border-radius: 50%; flex: none; border: 2px solid rgba(255, 255, 255, 0.6); }
.players .tag { margin-left: auto; font-size: 12px; color: #ffd83b; text-transform: uppercase; }

.host-controls { display: flex; flex-direction: column; gap: 10px; }

#resultsTable { width: 100%; border-collapse: collapse; font-size: 18px; }
#resultsTable th { text-align: left; color: #aab4d4; font-size: 12px; text-transform: uppercase; padding: 4px 8px; }
#resultsTable td { padding: 8px; border-top: 1px solid rgba(255, 255, 255, 0.08); }
#resultsTable tr.me td { background: rgba(255, 216, 59, 0.15); }
#resultsTable .place { font-weight: bold; font-size: 22px; width: 50px; }
#resultsTable .dot { display: inline-block; width: 14px; height: 14px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }
#resultsTable .time { text-align: right; font-family: monospace; font-size: 17px; }

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

#hud { position: fixed; inset: 0; pointer-events: none; }

.hud-pos {
  position: absolute;
  right: 24px;
  bottom: 20px;
  font-size: 72px;
  font-weight: bold;
  font-style: italic;
  color: #fff;
  text-shadow: 4px 4px 0 #000;
}
.hud-pos.p1 { color: #ffd83b; }
.hud-pos.p2 { color: #d9e2ec; }
.hud-pos.p3 { color: #e0955a; }

.hud-top {
  position: absolute;
  top: 14px;
  right: 20px;
  text-align: right;
  font-size: 26px;
  font-weight: bold;
  text-shadow: 3px 3px 0 #000;
}
#hudTime { font-family: monospace; font-size: 22px; color: #ffd83b; }

.hud-item {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  width: 84px;
  height: 84px;
  border-radius: 18px;
  border: 4px solid rgba(255, 255, 255, 0.85);
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 50px;
}

#minimap {
  position: absolute;
  left: 14px;
  bottom: 14px;
  width: 170px;
  height: 170px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 14px;
}

#countdown {
  position: absolute;
  top: 34%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 140px;
  font-weight: bold;
  font-style: italic;
  color: #ffd83b;
  text-shadow: 6px 6px 0 #d62828, 10px 10px 0 rgba(0, 0, 0, 0.4);
}

#wrongWay {
  position: absolute;
  top: 22%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 48px;
  font-weight: bold;
  color: #ff4b3e;
  text-shadow: 3px 3px 0 #000;
  display: none;
}
#wrongWay.show { display: block; animation: blink 0.5s infinite alternate; }

#toast {
  position: absolute;
  top: 20%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 54px;
  font-weight: bold;
  font-style: italic;
  color: #fff;
  text-shadow: 4px 4px 0 #1e88e5, 7px 7px 0 rgba(0, 0, 0, 0.4);
}

@keyframes blink { from { opacity: 1; } to { opacity: 0.3; } }

/* ---------- Touch controls ---------- */

#touch { position: fixed; inset: 0; pointer-events: none; }
#touch button {
  pointer-events: auto;
  touch-action: none;
  font: inherit;
  font-weight: bold;
  color: #fff;
  border: 3px solid rgba(255, 255, 255, 0.6);
  background: rgba(0, 0, 0, 0.35);
  border-radius: 50%;
}
#touch button.on { background: rgba(255, 216, 59, 0.55); }
.touch-left { position: absolute; left: 200px; bottom: 24px; display: flex; gap: 16px; }
.touch-left button { width: 90px; height: 90px; font-size: 34px; }
.touch-right {
  position: absolute;
  right: 150px;
  bottom: 24px;
  display: grid;
  grid-template-columns: 80px 100px;
  gap: 12px;
  align-items: end;
}
.touch-right button { width: 80px; height: 80px; font-size: 14px; }
.touch-right .t-gas { width: 100px; height: 100px; font-size: 18px; background: rgba(67, 160, 71, 0.5); }
.touch-right .t-brake { background: rgba(229, 57, 53, 0.45); }
.touch-right .t-item { background: rgba(142, 36, 170, 0.5); }

.hud-item.rolling { animation: roulette 0.12s linear infinite; }
@keyframes roulette { from { transform: translateX(-50%) rotate(-6deg); } to { transform: translateX(-50%) rotate(6deg); } }

#hudLap.pulse { animation: lapPulse 0.45s ease; }
@keyframes lapPulse { from { transform: scale(1.25); color: #ffd83b; } to { transform: none; } }

#hud.shake { animation: hitShake 0.35s linear; }
@keyframes hitShake {
  0% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  100% { transform: translateX(0); }
}

.hud-speed {
  --p: 0%;
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  width: 120px;
  height: 72px;
  border-radius: 80px 80px 16px 16px;
  background:
    radial-gradient(circle at 50% 100%, transparent 54%, rgba(0,0,0,0.45) 55%),
    conic-gradient(from 220deg, #ff4b3e, #ffd83b var(--p), rgba(255,255,255,0.15) var(--p));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 8px;
  text-shadow: 2px 2px 0 #000;
}
.hud-speed b { font-family: Teko, Impact, sans-serif; font-size: 42px; line-height: 0.8; }
.hud-speed small { font-size: 11px; letter-spacing: 2px; color: #ffd83b; }

.map-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.map-grid.locked { pointer-events: none; }
.map-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
  border: 2px solid #3a4470;
  background: #12101c;
  color: #fff;
  border-radius: 12px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  font: inherit;
}
.map-card img, .map-fallback {
  width: 100%;
  height: 64px;
  object-fit: cover;
  display: block;
  background: #222;
}
.map-card[data-theme="Grass"] .map-fallback, .map-card[data-theme="Grass"] { background: #1b5e20; }
.map-card[data-theme="Desert"] { background: #8d6e3b; }
.map-card[data-theme="Volcano"] { background: #6d1b1b; }
.map-card[data-theme="Snow"] { background: #4f6f86; }
.map-card[data-theme="Night"] { background: #24143d; }
.map-card[data-theme="Any"] { background: linear-gradient(135deg, #e53935, #ffd83b, #1e88e5); }
.map-card div { padding: 4px 8px 8px; }
.map-card b { display: block; font-size: 14px; }
.map-card small { color: #d5d8ee; font-size: 11px; }
.map-card.selected { border-color: #ffd83b; box-shadow: 0 0 0 2px rgba(255, 216, 59, 0.35); }
.map-card:hover { filter: brightness(1.08); }

#touch button { border-radius: 16px; border-color: rgba(255, 216, 59, 0.7); }

@media (max-width: 800px) {
  .panel.wide { width: min(100%, 560px); }
  .map-card img, .map-fallback { height: 48px; }
  .hud-speed { width: 96px; height: 60px; bottom: 8px; }
  .hud-speed b { font-size: 32px; }
  .logo { letter-spacing: 1px; }
  .touch-left { left: 12px; bottom: 200px; }
  .touch-right { right: 12px; bottom: 120px; }
  .hud-pos { font-size: 52px; }
}

.menu-row { display: flex; gap: 8px; }
.menu-row .btn { flex: 1; }
.picker-row { display: flex; flex-wrap: wrap; gap: 6px; }
.pick {
  border: 2px solid #3a4470;
  background: #0c1024;
  color: #fff;
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}
.pick.selected { border-color: #ffd83b; background: rgba(255, 216, 59, 0.15); }
.stats { display: grid; gap: 6px; font-size: 12px; text-transform: uppercase; color: #aab4d4; }
.stats div { display: grid; grid-template-columns: 70px 1fr; align-items: center; gap: 8px; }
.stats i {
  display: block;
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(90deg, #ff4b3e, #ffd83b);
  width: 50%;
}
.field.check { flex-direction: row; align-items: center; gap: 10px; text-transform: none; }
.bar { height: 12px; background: #0c1024; border-radius: 8px; overflow: hidden; border: 2px solid #3a4470; }
.bar i { display: block; height: 100%; width: 0%; background: #ffd83b; transition: width 0.2s; }
#resultsPodium { display: flex; justify-content: center; gap: 12px; align-items: flex-end; min-height: 90px; margin: 8px 0 14px; }
#resultsPodium .pod {
  width: 90px; text-align: center; border-radius: 12px 12px 0 0; padding: 10px 6px; background: rgba(255,255,255,0.08);
}
#resultsPodium .pod.p1 { height: 90px; background: rgba(255,216,59,0.25); }
#resultsPodium .pod.p2 { height: 70px; }
#resultsPodium .pod.p3 { height: 55px; }
#resultsPodium .dot { width: 18px; height: 18px; border-radius: 50%; display: inline-block; }
