/* ============================================================
   COURSE DE BILLES - Stylesheet
   ============================================================ */

/* ---------- Reset & Base ---------- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-dark: #1a1a2e;
  --bg-darker: #12122a;
  --bg-card: #22224a;
  --bg-card-alt: #2a2a52;
  --text-primary: #eef0ff;
  --text-secondary: #a0a4c8;
  --accent: #ff6b6b;
  --accent-hover: #ff8787;
  --primary: #4488ff;
  --primary-hover: #6aa4ff;
  --gold: #ffd700;
  --silver: #c0c0c0;
  --bronze: #cd7f32;
  --hud-bg: rgba(18, 18, 42, 0.85);
  --transition-speed: 0.35s;
  --font-display: 'Fredoka One', cursive;
  --font-body: 'Nunito', system-ui, -apple-system, sans-serif;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-dark);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
}

/* ---------- Canvas ---------- */
#gameCanvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
  display: block;
}

/* ---------- Screens (common) ---------- */
.screen {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
  background: var(--bg-dark);
  opacity: 0;
  transition: opacity var(--transition-speed) ease;
}

.screen.active {
  display: flex;
  opacity: 1;
  animation: screenFadeIn var(--transition-speed) ease forwards;
  overflow-y: auto;
}

.screen-transparent {
  background: rgba(18, 18, 42, 0.35);
  pointer-events: none;
}

.screen-transparent .placement-top-bar,
.screen-transparent .btn,
.screen-transparent .race-hud {
  pointer-events: auto;
}

@keyframes screenFadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ---------- Screen Content ---------- */
.screen-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  padding: 32px 24px;
  max-width: 520px;
  width: 100%;
  margin-top: auto;
  margin-bottom: auto;
  flex-shrink: 0;
}

.screen-content-wide {
  max-width: 700px;
  margin-top: auto;
  margin-bottom: auto;
  flex-shrink: 0;
}

/* ============================================================
   WELCOME SCREEN
   ============================================================ */
.game-title {
  font-family: var(--font-display);
  font-size: clamp(2.4rem, 7vw, 4.2rem);
  text-align: center;
  background: linear-gradient(135deg, #ff6b6b, #ffd700, #44dd44, #4488ff, #dd44ff);
  background-size: 300% 300%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: titleShimmer 6s ease infinite;
  filter: drop-shadow(0 0 20px rgba(255, 107, 107, 0.3));
}

@keyframes titleShimmer {
  0%, 100% { background-position: 0% 50%; }
  50%      { background-position: 100% 50%; }
}

.subtitle {
  text-align: center;
  font-size: 1.05rem;
  color: var(--text-secondary);
  max-width: 380px;
}

.config-link {
  display: inline-block;
  margin-top: 12px;
  color: var(--text-secondary);
  font-size: 0.85rem;
  text-decoration: none;
  transition: color var(--transition-speed);
}
.config-link:hover {
  color: var(--primary);
}

/* Commentary toggle */
.commentary-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text-secondary);
  user-select: none;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--bg-card-alt);
  border-radius: 26px;
  transition: background 0.3s ease;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  bottom: 3px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform 0.3s ease, background 0.3s ease;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--primary);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(22px);
  background: #fff;
}

/* Floating commentary toggle */
.commentary-floating-toggle {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
  background: var(--bg-card);
  padding: 8px 16px;
  border-radius: 30px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  opacity: 0.75;
  transition: opacity 0.2s ease;
}

.commentary-floating-toggle:hover {
  opacity: 1;
}

.commentary-floating-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.commentary-floating-text {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* Welcome settings grid */
.welcome-settings {
  display: flex;
  justify-content: center;
  gap: 32px;
  flex-wrap: wrap;
  margin-top: 8px;
}

/* Player count */
.player-count-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

.player-count-label {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.player-count-control {
  display: flex;
  align-items: center;
  gap: 18px;
}

.player-count-display {
  font-family: var(--font-display);
  font-size: 2.6rem;
  width: 64px;
  text-align: center;
  color: var(--primary);
}

.btn-round {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid var(--primary);
  background: transparent;
  color: var(--primary);
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.btn-round:hover {
  background: var(--primary);
  color: var(--bg-dark);
  transform: scale(1.1);
}

.btn-round:active {
  transform: scale(0.95);
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 32px;
  border: none;
  border-radius: 12px;
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
}

.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(rgba(255,255,255,0.15), transparent);
  pointer-events: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 16px rgba(68, 136, 255, 0.35);
}

.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: 0 6px 24px rgba(68, 136, 255, 0.5);
  transform: translateY(-2px);
}

.btn-secondary {
  background: #555;
  color: #fff;
  box-shadow: 0 4px 16px rgba(100, 100, 100, 0.25);
}

.btn-secondary:hover {
  background: #777;
  transform: translateY(-2px);
}

.btn-accent {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 16px rgba(255, 107, 107, 0.35);
}

.btn-accent:hover {
  background: var(--accent-hover);
  box-shadow: 0 6px 24px rgba(255, 107, 107, 0.5);
  transform: translateY(-2px);
}

.btn-large {
  padding: 16px 48px;
  font-size: 1.15rem;
  border-radius: 16px;
}

.btn:active {
  transform: translateY(0) scale(0.97);
}

/* ============================================================
   SETUP SCREEN
   ============================================================ */
.screen-title {
  font-family: var(--font-display);
  font-size: clamp(1.5rem, 4vw, 2.2rem);
  text-align: center;
}

.setup-player-label {
  font-family: var(--font-display);
  font-size: 1.5rem;
  color: var(--primary);
}

.input-group {
  width: 100%;
}

.text-input {
  width: 100%;
  padding: 14px 18px;
  border: 2px solid var(--bg-card-alt);
  border-radius: 12px;
  background: var(--bg-card);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 1.05rem;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.text-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(68, 136, 255, 0.2);
}

.text-input::placeholder {
  color: var(--text-secondary);
  opacity: 0.6;
}

/* Color picker */
.color-picker {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.color-picker-label {
  font-size: 0.9rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.color-swatches {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.color-swatch {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--swatch-color);
  border: 3px solid transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
}

.color-swatch:hover {
  transform: scale(1.15);
  box-shadow: 0 0 16px var(--swatch-color);
}

.color-swatch.selected {
  border-color: #fff;
  box-shadow: 0 0 20px var(--swatch-color), 0 0 0 3px var(--swatch-color);
  transform: scale(1.15);
}

.color-swatch.taken {
  opacity: 0.25;
  cursor: not-allowed;
  transform: scale(1);
}

.color-swatch.taken:hover {
  transform: scale(1);
  box-shadow: none;
}

.color-swatch.taken::before,
.color-swatch.taken::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60%;
  height: 3px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 2px;
}

.color-swatch.taken::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.color-swatch.taken::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.setup-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  width: 100%;
  margin-top: 8px;
}

/* ============================================================
   PLACEMENT SCREEN
   ============================================================ */
.placement-top-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 16px 24px;
  background: var(--hud-bg);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
}

.placement-info-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.badge {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 700;
  background: var(--bg-card);
  color: var(--text-primary);
}

.badge-player {
  background: var(--primary);
}

.placement-instructions {
  font-size: 1rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.btn-start-race {
  position: absolute;
  bottom: 48px;
  z-index: 20;
  animation: pulseBtn 1.5s ease-in-out infinite;
}

@keyframes pulseBtn {
  0%, 100% { box-shadow: 0 4px 16px rgba(255, 107, 107, 0.35); }
  50%      { box-shadow: 0 4px 32px rgba(255, 107, 107, 0.7), 0 0 60px rgba(255, 107, 107, 0.3); }
}

/* Starting zone highlight */
.starting-zone-highlight {
  position: absolute;
  border: 2px dashed rgba(255, 255, 255, 0.35);
  border-radius: 8px;
  animation: zoneGlow 2s ease-in-out infinite;
  pointer-events: none;
  z-index: 5;
}

@keyframes zoneGlow {
  0%, 100% { box-shadow: inset 0 0 20px rgba(68, 136, 255, 0.1); }
  50%      { box-shadow: inset 0 0 40px rgba(68, 136, 255, 0.25); }
}

/* ============================================================
   RACING SCREEN
   ============================================================ */
.race-hud {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 20;
  min-width: 180px;
  max-width: 220px;
}

.hud-race-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--hud-bg);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  padding: 10px 14px;
}

.hud-label {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-primary);
}

.position-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--hud-bg);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  padding: 10px 12px;
  max-height: 50vh;
  overflow-y: auto;
}

.position-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.8rem;
  font-weight: 600;
  transition: background 0.2s;
}

.position-card:first-child {
  background: rgba(255, 215, 0, 0.12);
}

.position-dot,
.position-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.marble-time {
  font-size: 0.7rem;
  color: var(--gold);
  font-weight: 700;
}

.marble-lap {
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.position-card.finished {
  background: rgba(255, 215, 0, 0.08);
}

.position-rank {
  min-width: 18px;
  font-weight: 800;
  color: var(--text-secondary);
}

.position-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.position-lap {
  font-size: 0.7rem;
  color: var(--text-secondary);
}

/* ============================================================
   COUNTDOWN OVERLAY
   ============================================================ */
.countdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 26, 0.7);
  backdrop-filter: blur(4px);
}

.countdown-overlay.active {
  display: flex;
}

.countdown-text {
  font-family: var(--font-display);
  font-size: clamp(5rem, 18vw, 12rem);
  color: #fff;
  text-shadow: 0 0 40px var(--primary), 0 0 80px var(--primary);
  animation: countdownPop 0.8s ease;
}

@keyframes countdownPop {
  0%   { transform: scale(2.5); opacity: 0; }
  40%  { transform: scale(0.9); opacity: 1; }
  60%  { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

/* "GO!" specific style */
.countdown-text.go {
  color: var(--gold);
  text-shadow: 0 0 40px var(--gold), 0 0 80px var(--gold);
}

/* ============================================================
   RESULTS SCREEN
   ============================================================ */
.table-container {
  width: 100%;
  overflow-x: auto;
  border-radius: 12px;
  background: var(--bg-card);
}

.results-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}

.results-table th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 800;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  background: var(--bg-darker);
  border-bottom: 2px solid var(--bg-card-alt);
}

.results-table td {
  padding: 10px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.results-table tr:nth-child(even) td {
  background: rgba(255, 255, 255, 0.02);
}

.results-table tr:hover td {
  background: rgba(68, 136, 255, 0.06);
}

/* Medal highlights */
.results-table tr.rank-1 td:first-child { color: var(--gold); }
.results-table tr.rank-2 td:first-child { color: var(--silver); }
.results-table tr.rank-3 td:first-child { color: var(--bronze); }
.results-table tr.gold td { background: rgba(255, 215, 0, 0.08); }
.results-table tr.gold td:first-child { color: var(--gold); font-weight: 800; }
.results-table tr.silver td { background: rgba(192, 192, 192, 0.06); }
.results-table tr.silver td:first-child { color: var(--silver); font-weight: 800; }
.results-table tr.bronze td { background: rgba(205, 127, 50, 0.06); }
.results-table tr.bronze td:first-child { color: var(--bronze); font-weight: 800; }
.results-table tr.champion td { background: rgba(255, 215, 0, 0.12); font-weight: 700; }

.color-dot {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  vertical-align: middle;
  box-shadow: 0 0 4px rgba(255, 255, 255, 0.2);
}

.standings-subtitle {
  font-family: var(--font-display);
  font-size: 1.2rem;
  color: var(--text-secondary);
  margin-top: 8px;
}

.results-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
}

/* ============================================================
   CHAMPIONSHIP SCREEN
   ============================================================ */
.championship-header {
  text-align: center;
}

.championship-title {
  font-family: var(--font-display);
  font-size: clamp(1.8rem, 6vw, 3.2rem);
  background: linear-gradient(135deg, var(--gold), #fff5a0, var(--gold));
  background-size: 200% 200%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: titleShimmer 4s ease infinite;
  filter: drop-shadow(0 0 12px rgba(255, 215, 0, 0.4));
}

/* ============================================================
   SCROLLBAR
   ============================================================ */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-card-alt);
  border-radius: 3px;
}

/* ============================================================
   ERROR ANIMATIONS
   ============================================================ */
.text-input.error {
  border-color: var(--accent) !important;
  animation: shake 0.4s ease;
}

.color-swatches.error {
  animation: shake 0.4s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-4px); }
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 600px) {
  .screen-content {
    padding: 24px 16px;
    gap: 18px;
  }

  .btn-large {
    padding: 14px 32px;
    font-size: 1rem;
  }

  .color-swatch {
    width: 42px;
    height: 42px;
  }

  .race-hud {
    min-width: 150px;
    max-width: 170px;
    top: 8px;
    right: 8px;
  }

  .hud-label {
    font-size: 0.75rem;
  }

  .position-card {
    font-size: 0.72rem;
    padding: 4px 6px;
  }

  .results-table {
    font-size: 0.82rem;
  }

  .results-table th,
  .results-table td {
    padding: 8px 10px;
  }
}

@media (max-width: 380px) {
  .color-swatch {
    width: 36px;
    height: 36px;
  }

  .color-swatches {
    gap: 8px;
  }

  .player-count-display {
    font-size: 2rem;
  }

  .btn-round {
    width: 40px;
    height: 40px;
    font-size: 1.3rem;
  }
}
