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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, #0f1419 0%, #1a2332 50%, #0d1117 100%);
  color: #e6edf3;
  min-height: 100vh;
}

#app {
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.5rem;
}

header {
  text-align: center;
  margin-bottom: 1.5rem;
}

header h1 {
  font-size: 2.4rem;
  letter-spacing: 0.05em;
}

.subtitle {
  color: #8b949e;
  margin-top: 0.3rem;
}

.panel {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 1.2rem;
  margin-bottom: 1rem;
}

.lobby-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: #8b949e;
}

input {
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid #30363d;
  background: #0d1117;
  color: #e6edf3;
  font-size: 1rem;
  min-width: 140px;
}

.btn {
  padding: 0.55rem 1.2rem;
  border-radius: 8px;
  border: 1px solid #30363d;
  background: #21262d;
  color: #e6edf3;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.15s;
}

.btn:hover {
  background: #30363d;
}

.btn.primary {
  background: #238636;
  border-color: #2ea043;
}

.btn.primary:hover {
  background: #2ea043;
}

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

.btn.ready-on {
  background: #1f6feb;
  border-color: #388bfd;
}

.hidden {
  display: none !important;
}

#lobby-status h2 {
  font-size: 1.1rem;
  margin: 1rem 0 0.5rem;
  color: #8b949e;
}

#player-list {
  list-style: none;
  margin: 0.5rem 0 1rem;
}

#player-list li {
  padding: 0.4rem 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.player-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.ready-badge {
  font-size: 0.75rem;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: #238636;
  margin-left: auto;
}

.lobby-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

#game-area {
  position: relative;
  margin-bottom: 1rem;
}

#track-canvas {
  width: 100%;
  height: auto;
  border-radius: 12px;
  border: 2px solid #30363d;
  background: #d4b896;
  display: block;
}

.team-names-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  margin: 0.75rem 0;
}

#teams-display {
  display: flex;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.team-score {
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  font-size: 0.9rem;
  background: rgba(0,0,0,0.2);
}

.team-score.team-a { border-left: 3px solid #e63946; }
.team-score.team-b { border-left: 3px solid #2a9d8f; }

#score-board {
  background: rgba(0, 0, 0, 0.55);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
  text-align: right;
}

.heat-results { margin: 1rem 0; font-size: 1rem; line-height: 1.8; }
.heat-totals { font-size: 1.1rem; margin-top: 0.5rem; }
.final-score { font-size: 1.3rem; margin: 0.5rem 0; }
.hint { font-size: 0.85rem; color: #8b949e; margin-top: 0.5rem; }

#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  pointer-events: none;
}

#speed-dials-hud {
  position: absolute;
  bottom: 12px;
  left: 12px;
  right: 12px;
  z-index: 10;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  pointer-events: auto;
}

#race-info, #lap-board {
  background: rgba(0, 0, 0, 0.55);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  font-size: 0.9rem;
  line-height: 1.5;
}

#controls-help h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.control-item {
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.2);
  font-size: 0.9rem;
}

.control-item.red { border-left: 3px solid #e63946; }
.control-item.blue { border-left: 3px solid #457b9d; }
.control-item.green { border-left: 3px solid #2a9d8f; }
.control-item.yellow { border-left: 3px solid #e9c46a; }

kbd {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  background: #21262d;
  border: 1px solid #484f58;
  font-family: inherit;
  font-size: 0.85rem;
  margin-right: 0.3rem;
}

.rules {
  font-size: 0.85rem;
  color: #8b949e;
  line-height: 1.5;
}

.setup-hint {
  font-size: 0.85rem;
  color: #8b949e;
  margin-bottom: 1rem;
}

.track-picker {
  margin: 1rem 0;
  padding: 1rem;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid #30363d;
}

.track-picker h3 {
  margin: 0 0 0.35rem;
  font-size: 1rem;
}

.track-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}

.track-card-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.track-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  padding: 0.65rem;
  border-radius: 10px;
  border: 2px solid #30363d;
  background: #161b22;
  color: #e6edf3;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, transform 0.15s;
}

.track-card:hover {
  border-color: #58a6ff;
  transform: translateY(-1px);
}

.track-card.selected {
  border-color: #2ea043;
  box-shadow: 0 0 0 1px #2ea043;
}

.track-card-preview {
  display: block;
  width: 100%;
  aspect-ratio: 10 / 7;
  border-radius: 8px;
  background: linear-gradient(135deg, #7a5230 0%, #4a9e4f 45%, #d4b896 100%);
  background-size: cover;
  background-position: center;
  border: 1px solid #30363d;
}

.track-card-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.track-card-desc {
  font-size: 0.75rem;
  color: #8b949e;
  line-height: 1.35;
}

.track-custom-badge {
  font-size: 0.65rem;
  font-weight: 600;
  color: #3fb950;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.track-official-badge {
  font-size: 0.65rem;
  font-weight: 600;
  color: #d2a8ff;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.promote-track-card {
  margin-bottom: 0.75rem;
  padding: 0.85rem 1rem;
}

.import-command {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  font-size: 0.8rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.track-card-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}

.track-card-edit {
  font-size: 0.72rem;
  text-align: center;
  color: #58a6ff;
  text-decoration: none;
}

.track-card-edit:hover {
  text-decoration: underline;
}

.track-card-delete {
  font-size: 0.72rem;
  padding: 0;
  border: none;
  background: none;
  color: #f85149;
  cursor: pointer;
  text-decoration: underline;
}

.track-card-delete:hover {
  color: #ff7b72;
}

.setup-hint a {
  color: #58a6ff;
}

.riders-grid {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.rider-row {
  display: grid;
  grid-template-columns: 64px 1fr 130px 72px;
  gap: 0.75rem;
  align-items: center;
}

.rider-key {
  text-align: center;
}

.rider-row .rider-name {
  min-width: 0;
  width: 100%;
}

.rider-row select {
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid #30363d;
  background: #0d1117;
  color: #e6edf3;
}

.speed-dial {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  cursor: pointer;
  user-select: none;
}

.speed-dial-face {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(145deg, #2d333b, #1c2128);
  border: 2px solid #484f58;
  position: relative;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
}

.speed-dial-knob {
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  background: #21262d;
  border: 1px solid #30363d;
}

.speed-dial-knob::after {
  content: '';
  position: absolute;
  top: 4px;
  left: 50%;
  width: 3px;
  height: 10px;
  margin-left: -1.5px;
  background: #58a6ff;
  border-radius: 2px;
  transform-origin: center 14px;
}

.speed-dial[data-level="0"] .speed-dial-knob::after { transform: rotate(-60deg); }
.speed-dial[data-level="1"] .speed-dial-knob::after { transform: rotate(-20deg); }
.speed-dial[data-level="2"] .speed-dial-knob::after { transform: rotate(20deg); }
.speed-dial[data-level="3"] .speed-dial-knob::after { transform: rotate(60deg); }

.speed-dial-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: #8b949e;
}

.speed-dials-hud {
  pointer-events: auto;
}

.speed-dial-hud-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(0, 0, 0, 0.72);
  padding: 0.4rem 0.65rem;
  border-radius: 8px;
  font-size: 0.8rem;
  flex: 1 1 200px;
  max-width: 240px;
  justify-content: space-between;
}

.speed-dials-hud .speed-dial-face {
  width: 36px;
  height: 36px;
}

.speed-dials-hud .speed-dial-label {
  min-width: 2.5rem;
}

.score-table {
  margin: 0.75rem 0;
  font-size: 0.9rem;
  line-height: 1.6;
}

.overlay-title { font-size: 1.4rem; margin-bottom: 0.5rem; }

.overlay-actions {
  margin-top: 1.5rem;
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}

.overlay-btn {
  font-size: 1.1rem;
  padding: 0.75rem 1.5rem;
  cursor: pointer;
}

#overlay-content {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 16px;
  padding: 2rem 3rem;
  text-align: center;
  font-size: 1.2rem;
  max-width: 90vw;
  pointer-events: auto;
}

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  pointer-events: auto;
}

#overlay-content .countdown {
  font-size: 5rem;
  font-weight: bold;
  color: #f0c040;
}

#overlay-content .winner {
  font-size: 2rem;
  margin-top: 0.5rem;
}

@media (max-width: 600px) {
  header h1 { font-size: 1.8rem; }
  .lobby-row { flex-direction: column; align-items: stretch; }
}
