:root {
  --bg: #07090d;
  --panel: #0e131a;
  --panel-2: #141b25;
  --line: rgba(255, 255, 255, 0.08);
  --text: #f2f4f7;
  --muted: #9aa6b5;
  --accent: #e8b84a;
  --accent-2: #3ec7ff;
  --danger: #ff4d5a;
  --ok: #3dce8a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

.game-root {
  min-height: 100dvh;
  min-height: 100svh;
  width: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  position: relative;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(62, 199, 255, 0.08), transparent 60%),
    radial-gradient(900px 500px at 90% 100%, rgba(232, 184, 74, 0.06), transparent 50%),
    var(--bg);
}

.screen {
  display: none;
  position: absolute;
  inset: 0;
  padding:
    calc(12px + var(--safe-top))
    calc(14px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(14px + var(--safe-left));
  flex-direction: column;
  gap: 12px;
}

.screen.active {
  display: flex;
}

.select-header {
  flex: 0 0 auto;
  text-align: center;
  max-width: 920px;
  margin: 0 auto;
  width: 100%;
}

.select-header.compact h1 {
  font-size: clamp(1.4rem, 4vw, 2rem);
}

.badge {
  display: inline-block;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  border: 1px solid rgba(232, 184, 74, 0.35);
  padding: 4px 10px;
  margin-bottom: 8px;
  background: rgba(232, 184, 74, 0.08);
}

h1 {
  margin: 0 0 6px;
  font-size: clamp(1.5rem, 4.5vw, 2.2rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.sub {
  margin: 0 auto 12px;
  color: var(--muted);
  font-size: 0.95rem;
  max-width: 36rem;
  line-height: 1.4;
}

.slot-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 8px 0 4px;
}

.slot {
  width: min(28vw, 120px);
  height: 56px;
  border: 1px dashed rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.03);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  overflow: hidden;
  padding: 6px 8px;
}

.slot span {
  color: var(--muted);
  font-size: 0.85rem;
}

.slot.filled {
  border-style: solid;
  border-color: rgba(62, 199, 255, 0.45);
  background: rgba(62, 199, 255, 0.08);
}

.slot img {
  width: 36px;
  height: 36px;
  object-fit: cover;
  flex-shrink: 0;
}

.slot .slot-name {
  font-size: 0.68rem;
  line-height: 1.15;
  text-align: left;
  color: var(--text);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.select-count {
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.team-grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 4px 2px 8px;
  -webkit-overflow-scrolling: touch;
}

.team-card {
  position: relative;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  padding: 12px 10px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  transition: border-color 0.15s, transform 0.12s, box-shadow 0.15s;
  min-height: 148px;
  text-align: center;
}

.team-card:hover,
.team-card:focus-visible {
  border-color: rgba(62, 199, 255, 0.5);
  outline: none;
  transform: translateY(-1px);
}

.team-card.selected {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 1px rgba(62, 199, 255, 0.35), 0 8px 24px rgba(0, 0, 0, 0.35);
}

.team-card.selected::after {
  content: "SELECTED";
  position: absolute;
  top: 6px;
  right: 6px;
  font-size: 0.58rem;
  letter-spacing: 0.08em;
  color: #041018;
  background: var(--accent-2);
  padding: 2px 5px;
  font-weight: 700;
}

.team-card.locked:not(.selected) {
  opacity: 0.42;
  cursor: not-allowed;
}

.team-card img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  background: #000;
}

.team-card .name {
  font-size: 0.82rem;
  font-weight: 650;
  line-height: 1.2;
}

.team-card .tag {
  font-size: 0.68rem;
  color: var(--muted);
  line-height: 1.25;
}

.team-card .swatch {
  width: 100%;
  height: 4px;
  margin-top: auto;
}

.select-footer {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  gap: 10px;
  padding-top: 4px;
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
}

.btn {
  min-height: 48px;
  min-width: 120px;
  padding: 0 18px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 650;
  letter-spacing: 0.02em;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.btn.primary {
  background: linear-gradient(180deg, #f0c45d, #c99420);
  color: #1a1204;
  border-color: rgba(255, 220, 140, 0.45);
}

.btn.primary:not(:disabled):hover {
  filter: brightness(1.05);
}

.btn.ghost:not(:disabled):hover {
  border-color: rgba(255, 255, 255, 0.25);
}

.driver-picks {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  align-content: center;
}

.driver-card {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  min-height: 200px;
  touch-action: manipulation;
}

.driver-card:hover,
.driver-card:focus-visible {
  border-color: rgba(232, 184, 74, 0.55);
  outline: none;
}

.driver-card.chosen {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(232, 184, 74, 0.4);
}

.driver-card img.logo {
  width: 88px;
  height: 88px;
  object-fit: cover;
}

.driver-card img.car {
  height: 96px;
  width: auto;
  image-rendering: auto;
}

.driver-card .name {
  font-weight: 700;
  font-size: 0.95rem;
  text-align: center;
}

/* RACE */
#screen-race {
  padding: 0;
}

.race-stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  touch-action: none;
  user-select: none;
}

#race-canvas {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: #05070b;
}

.race-hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  padding:
    calc(10px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(10px + var(--safe-bottom))
    calc(12px + var(--safe-left));
  z-index: 2;
}

.hud-left, .hud-right {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.hud-right {
  align-items: flex-end;
}

.hud-lap, .hud-pos {
  display: inline-flex;
  align-self: flex-start;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 6px 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  font-size: 0.9rem;
  backdrop-filter: blur(4px);
}

.hud-pos {
  font-size: 1.25rem;
  color: var(--accent);
}

.standings {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
}

.standing-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 4px 6px;
  font-size: 0.72rem;
  backdrop-filter: blur(4px);
}

.standing-row.you {
  border-color: rgba(232, 184, 74, 0.55);
}

.standing-row img {
  width: 22px;
  height: 22px;
  object-fit: cover;
}

.standing-row .p {
  width: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.countdown-overlay {
  position: absolute;
  left: 50%;
  top: 38%;
  transform: translate(-50%, -50%);
  width: min(70vw, 420px);
  text-align: center;
  pointer-events: none;
}

.countdown-overlay.hidden {
  display: none;
}

.countdown-overlay img {
  width: 100%;
  height: auto;
  opacity: 0.92;
  display: block;
}

.countdown-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(3rem, 12vw, 5.5rem);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 0 24px rgba(255, 60, 60, 0.8), 0 4px 12px #000;
  letter-spacing: 0.04em;
}

.touch-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: none;
  justify-content: center;
  align-items: flex-end;
  gap: 12px;
  padding: 0 calc(12px + var(--safe-right)) calc(14px + var(--safe-bottom)) calc(12px + var(--safe-left));
  z-index: 3;
  pointer-events: none;
}

.touch-controls .touch-btn {
  pointer-events: auto;
  min-width: 72px;
  min-height: 56px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(10, 14, 20, 0.72);
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  backdrop-filter: blur(6px);
  touch-action: none;
  user-select: none;
}

.touch-controls .boost {
  min-width: 110px;
  background: rgba(232, 184, 74, 0.28);
  border-color: rgba(232, 184, 74, 0.55);
  color: #ffe7a8;
  letter-spacing: 0.08em;
  font-size: 0.85rem;
}

.touch-controls .touch-btn:active,
.touch-controls .touch-btn.active {
  background: rgba(62, 199, 255, 0.35);
  border-color: rgba(62, 199, 255, 0.7);
}

.desktop-hint {
  position: absolute;
  left: 50%;
  bottom: calc(12px + var(--safe-bottom));
  transform: translateX(-50%);
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.55);
  background: rgba(0, 0, 0, 0.4);
  padding: 4px 10px;
  z-index: 2;
  pointer-events: none;
  white-space: nowrap;
}

@media (hover: none), (max-width: 900px) {
  .touch-controls {
    display: flex;
  }
  .desktop-hint {
    display: none;
  }
}

/* RESULTS */
#screen-results {
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(800px 400px at 50% 20%, rgba(232, 184, 74, 0.12), transparent 55%),
    var(--bg);
}

.results-card {
  width: min(520px, 100%);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--line);
  padding: 22px 18px 16px;
  text-align: center;
}

.podium {
  list-style: none;
  margin: 18px 0 8px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.podium li {
  display: grid;
  grid-template-columns: 36px 48px 1fr auto;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 10px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line);
}

.podium li.you {
  border-color: rgba(232, 184, 74, 0.5);
  background: rgba(232, 184, 74, 0.08);
}

.podium .place {
  font-weight: 800;
  color: var(--accent);
  font-size: 1.1rem;
}

.podium img {
  width: 48px;
  height: 48px;
  object-fit: cover;
}

.podium .meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.podium .meta strong {
  font-size: 0.92rem;
}

.podium .meta span {
  font-size: 0.72rem;
  color: var(--muted);
}

.podium .time {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.8rem;
}

.results-actions {
  margin-top: 8px;
}

@media (max-width: 520px) {
  .team-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .slot {
    width: 30vw;
    height: 50px;
  }
  .slot .slot-name {
    display: none;
  }
  .standings {
    min-width: 120px;
  }
}

@media (orientation: landscape) and (max-height: 480px) {
  .select-header .sub {
    display: none;
  }
  .team-card {
    min-height: 120px;
    padding: 8px;
  }
  .team-card img {
    width: 52px;
    height: 52px;
  }
  .driver-card {
    min-height: 150px;
    padding: 10px;
  }
  .driver-card img.car {
    height: 64px;
  }
}
