:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --surface-hover: #2a2a2a;
  --text: #f5f5f5;
  --text-dim: #999;
  --accent: #e88a1a;
  --border: #333;
  --corner-border: #e6b800;
  --corner-bg: rgba(230, 184, 0, 0.15);
  --car-player: #e74c3c;
  --car-ai1: #3498db;
  --car-ai2: #2ecc71;
  --car-ai3: #f39c12;
}

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

body {
  min-height: 100vh;
  font-family: 'Press Start 2P', monospace;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-areas:
    "header header"
    "board standings"
    "roll roll";
  grid-template-columns: 1fr 200px;
  grid-template-rows: auto 1fr auto;
  gap: 8px;
  padding: 8px;
}

/* ── Header ── */
header#top-bar {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
}

.back-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 14px;
  white-space: nowrap;
}

.back-link:hover {
  color: var(--text);
}

.game-title {
  flex: 1;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
}

.lap-counter {
  font-size: 20px;
  font-weight: 700;
  white-space: nowrap;
}

/* ── Track Board ── */
#track-board {
  grid-area: board;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  width: min(80vw, 480px);
  aspect-ratio: 1;
  gap: 0px;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 8px;
  justify-self: center;
}

/* ── Track Space ── */
.track-space {
  position: relative;
  border: 2px solid var(--border);
  border-radius: 4px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  align-items: center;
  justify-items: center;
}

.track-space.corner {
  border-color: var(--corner-border);
  background: var(--corner-bg);
}

.track-space.start .start-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(min(80vw, 480px) / 6 * 0.6);
  line-height: 1;
  pointer-events: none;
  z-index: 1;
}

/* ── Car Image ── */
.car-img {
  width: 80%;
  height: 80%;
  object-fit: contain;
  display: block;
  image-rendering: pixelated;
}

.car-img.car-player {
  outline: 2px solid #ffffff;
  outline-offset: 1px;
}

/* ── Corner Callout ── */
.callout {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85);
  color: #f5f5f5;
  font-size: 14px;
  padding: 2px 8px;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 10;
  animation: callout-fade 1.5s ease forwards;
}

@keyframes callout-fade {
  0% { opacity: 1; transform: translateX(-50%) translateY(0); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-12px); }
}

/* ── Standings Panel ── */
#standings-panel {
  grid-area: standings;
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 16px;
  align-self: start;
}

#standings-panel h2 {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}

.standings-header {
  display: flex;
  flex-direction: row;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  border-bottom: 2px solid var(--border);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sh-pos {
  min-width: 32px;
}

.sh-driver {
  flex: 1;
}

.sh-lap {
  margin-left: auto;
}

.standing-row {
  display: flex;
  flex-direction: row;
  gap: 8px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.standing-row:last-child {
  border-bottom: none;
}

.position-badge {
  font-size: 16px;
  font-weight: 700;
  min-width: 32px;
}

.position-badge.first-place {
  color: var(--accent);
}

.standing-circle {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.standing-label {
  font-size: 16px;
}

.standing-lap {
  font-size: 14px;
  color: var(--text-dim);
  margin-left: auto;
}

/* ── Roll Area ── */
#roll-area {
  grid-area: roll;
  text-align: center;
  padding: 16px;
}

#roll-btn {
  background: var(--accent);
  color: var(--bg);
  font-size: 24px;
  font-weight: 700;
  padding: 12px 32px;
  border-radius: 12px;
  border: none;
  cursor: pointer;
  min-height: 44px;
  width: 100%;
  max-width: 320px;
  transition: transform 0.15s, opacity 0.15s;
}

#roll-btn:hover:not(:disabled) {
  transform: scale(1.04);
  opacity: 0.92;
}

#roll-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#dice-result {
  margin-top: 8px;
  font-size: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  justify-content: center;
}

#dice-result > div {
  display: flex;
  align-items: center;
  gap: 4px;
}

.dice-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Results Screen ── */
#results-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  z-index: 100;
}

#results-screen .results-inner {
  background: var(--surface);
  padding: 24px;
  border-radius: 12px;
  text-align: center;
  max-width: 400px;
  width: 90%;
}

#results-screen h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
}

.result-row {
  display: flex;
  flex-direction: row;
  gap: 8px;
  padding: 8px 0;
  font-size: 16px;
  align-items: center;
}

.result-row.first-place {
  color: var(--accent);
}


.hidden {
  display: none !important;
}

/* ── Home View ── */
.home-view {
  grid-area: board;
  max-width: 800px;
  width: 100%;
  justify-self: center;
  align-self: start;
}

.home-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

.home-card {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 24px;
}

.home-card h2 {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 16px;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-item {
  text-align: center;
  padding: 12px 8px;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border);
}

.stat-emoji {
  display: block;
  font-size: 28px;
  line-height: 1;
  margin-bottom: 6px;
}

.stat-value {
  display: block;
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 2px;
}

.stat-sub {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.medals-grid {
  grid-template-columns: 1fr 1fr 1fr;
  margin-bottom: 12px;
}

.race-grid {
  grid-template-columns: 1fr 1fr;
}

.home-actions {
  display: flex;
  gap: 12px;
}

.home-btn {
  flex: 1;
  background: var(--surface-hover);
  color: var(--text);
  font-size: 20px;
  font-weight: 700;
  padding: 14px 24px;
  border-radius: 12px;
  border: 1px solid var(--border);
  cursor: pointer;
  min-height: 44px;
  transition: transform 0.15s, opacity 0.15s;
}

.home-btn:hover {
  transform: scale(1.04);
  opacity: 0.92;
}

.home-btn-primary {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}

/* ── Shop View ── */
.shop-view {
  grid-area: board;
  max-width: 520px;
  width: 100%;
  justify-self: center;
  padding: 16px 0;
}

.shop-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

.shop-card {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 20px;
  text-align: center;
}

.shop-card h3 {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 12px;
}

.shop-card-emoji {
  font-size: 28px;
  line-height: 1;
}

.shop-current {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.shop-next {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 16px;
  min-height: 48px;
}

.shop-buy-btn {
  background: var(--accent);
  color: var(--bg);
  font-size: 18px;
  font-weight: 700;
  padding: 10px 24px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 100%;
  min-height: 44px;
  transition: transform 0.15s, opacity 0.15s;
}

.shop-buy-btn:hover:not(:disabled) {
  transform: scale(1.04);
  opacity: 0.92;
}

.shop-buy-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.shop-maxed {
  color: var(--accent);
  font-weight: 700;
  font-size: 16px;
}

/* ── Race-Active Viewport Lock ── */
body.race-active {
  height: 100dvh;
  overflow: hidden;
}

/* ── Mobile Breakpoint ── */
@media (max-width: 599px) {
  body {
    grid-template-areas:
      "header"
      "board"
      "standings"
      "roll";
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    min-height: auto;
  }

  #track-board {
    width: min(calc(100vw - 16px), 220px);
    margin: 0 auto;
  }

  #standings-panel {
    padding: 4px 8px;
  }

  #standings-panel h2 {
    font-size: 12px;
    margin-bottom: 4px;
  }

  .standing-row {
    padding: 4px 0;
  }

  .standings-header {
    font-size: 10px;
  }

  .standing-label {
    font-size: 12px;
  }

  .position-badge {
    font-size: 12px;
  }

  #standings-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0 16px;
  }

  .game-title {
    font-size: 14px;
  }

  .lap-counter {
    font-size: 14px;
  }

  #roll-area {
    padding: 8px;
  }

  #roll-btn {
    width: 100%;
    max-width: none;
    font-size: 18px;
    padding: 8px 24px;
  }

  .home-cards {
    grid-template-columns: 1fr;
  }

  .shop-cards {
    grid-template-columns: 1fr;
  }

  .shop-view {
    max-width: none;
    padding: 8px;
  }
}
