:root {
  --color-bg: #111216;
  --color-surface: #191B1F;
  --color-surface-elevated: #23252B;
  --color-card: #2B2D33;
  --color-card-hover: #393B44;
  --color-hairline: rgba(255, 255, 255, 0.08);
  /* Also read directly by js/game.js (cssVar) as the canvas obstacle fill —
     keep it clearly visible against --color-bg, not a subtle UI hairline. */
  --color-border: #5A5E68;
  --color-text: #FFFFFF;
  --color-text-muted: #9B9D9F;
  /* Also read directly by js/game.js (cssVar) as the canvas finish-line stroke. */
  --color-accent: #00B06F;
  --color-accent-hover: #00c97d;
  --color-accent-active: #00945c;
  --color-backdrop: rgba(0, 0, 0, 0.7);
  --font-base: 'Pretendard Variable', 'Pretendard', 'Montserrat', -apple-system, BlinkMacSystemFont, 'Malgun Gothic', sans-serif;
  --font-display: 'Montserrat', 'Pretendard Variable', 'Pretendard', sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-base);
  font-size: 16px;
  background: var(--color-bg);
  color: var(--color-text);
}

button, input, select {
  font-family: inherit;
  font-size: 16px;
}

.app {
  position: relative;
  min-height: 100vh;
}

/* ---------- Main game area ---------- */
.game-area {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
}

.hud-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  max-width: 380px;
}

.hud-pill {
  margin: 0;
  padding: 8px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid var(--color-hairline);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.status-text {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 800;
  color: var(--color-text);
}

.canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 380px;
}

.race-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 380 / 600;
  /* Idle-state artwork, shown before the first race starts. js/game.js
     clears this (backgroundImage='none') the moment startRace() runs, so
     it never shows through the opaque per-frame canvas drawing. */
  background: var(--color-surface) url('../assets/title-illustration.jpg') center / cover no-repeat;
  border-radius: 16px;
  border: 1px solid var(--color-hairline);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  touch-action: none;
}

.live-rank-overlay {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  pointer-events: none;
}

.live-rank-overlay:empty {
  display: none;
}

.live-rank-row {
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid var(--color-hairline);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  font-size: 11px;
  font-weight: 800;
  color: var(--color-text);
  text-shadow:
    0 0 2px #fff,
    0 0 6px var(--rank-color, var(--color-accent)),
    0 0 12px var(--rank-color, var(--color-accent)),
    0 0 20px var(--rank-color, var(--color-accent));
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.minimap-canvas {
  display: none;
  position: absolute;
  top: 12px;
  right: 12px;
  width: auto;
  height: calc(100% - 24px);
  border-radius: 8px;
  border: 1px solid var(--color-hairline);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  opacity: 0.9;
  pointer-events: none;
}

.minimap-canvas.is-visible {
  display: block;
}

/* ---------- Results modal ("Game Over") ---------- */
.race-results {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--color-backdrop);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.race-results[hidden] {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 340px;
  background: var(--color-surface-elevated);
  border: 1px solid var(--color-hairline);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

.modal-card h2 {
  margin: 0 0 14px;
  font-size: 20px;
  font-weight: 900;
  text-align: center;
  color: var(--color-text);
}

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

.rank-list li {
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--color-card);
  border: 1px solid var(--color-hairline);
  text-align: left;
  font-weight: 700;
  color: var(--color-text);
}

.rank-list li.is-winner {
  background: rgba(0, 176, 111, 0.16);
  border-color: var(--color-accent);
  color: var(--color-text);
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 18px;
}

/* ---------- History modal ("역대 순위") ---------- */
.history-modal-card {
  max-width: 480px;
}

.history-table-wrap {
  max-height: 50vh;
  overflow: auto;
  border-radius: 12px;
  border: 1px solid var(--color-hairline);
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  white-space: nowrap;
}

.history-table th,
.history-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--color-hairline);
}

.history-table th {
  position: sticky;
  top: 0;
  background: var(--color-card);
  color: var(--color-text-muted);
  font-weight: 800;
}

.history-table tbody tr:last-child td {
  border-bottom: none;
}

.history-table-empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--color-text-muted);
  font-weight: 600;
}

.history-modal-actions {
  grid-template-columns: 1fr 1fr;
}

/* ---------- Floating settings button (mobile) ---------- */
.fab-settings {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 20;
  height: 48px;
  padding: 0 18px;
  border: 1px solid var(--color-hairline);
  border-radius: 999px;
  background: var(--color-card);
  color: var(--color-text);
  font-weight: 800;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}

.fab-settings:hover {
  background: var(--color-card-hover);
}

.fab-settings:active {
  transform: scale(0.97);
}

/* ---------- Settings panel ---------- */
.settings-panel {
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  z-index: 30;
}

.settings-body {
  overflow-y: auto;
  padding: 12px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sheet-handle {
  width: 40px;
  height: 5px;
  border-radius: 999px;
  background: var(--color-hairline);
  margin: 10px auto 0;
  flex-shrink: 0;
}

.sheet-close {
  position: absolute;
  top: 8px;
  right: 12px;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 18px;
  cursor: pointer;
}

.settings-header {
  text-align: center;
  padding-top: 4px;
}

.settings-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 900;
  color: var(--color-text);
  cursor: pointer;
  user-select: none;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.settings-header h1:hover {
  opacity: 0.85;
}

.settings-header h1:active {
  transform: scale(0.97);
}

.settings-header p {
  margin: 4px 0 0;
  color: var(--color-text-muted);
  font-size: 13px;
  font-weight: 600;
}

.action-row {
  display: flex;
  gap: 8px;
}

.action-row button {
  flex: 1 1 0;
  height: 48px;
  border: 1px solid var(--color-hairline);
  border-radius: 14px;
  background: var(--color-card);
  color: var(--color-text);
  font-weight: 800;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}

.action-row button:hover {
  background: var(--color-card-hover);
  transform: scale(1.02);
}

.action-row button:active {
  transform: scale(0.97);
}

.action-row #startGameBtn {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #06120C;
}

.action-row #startGameBtn:hover {
  background: var(--color-accent-hover);
}

.action-row #startGameBtn:active {
  background: var(--color-accent-active);
}

.settings-section {
  border: none;
  padding: 14px;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--color-card);
  border-radius: 16px;
}

.settings-section > label,
.settings-section > legend {
  font-weight: 800;
  color: var(--color-text-muted);
  font-size: 13px;
  padding: 0;
}

select,
input[type="number"],
input[type="text"] {
  height: 44px;
  border: 1px solid var(--color-hairline);
  border-radius: 12px;
  padding: 0 12px;
  background: var(--color-surface);
  color: var(--color-text);
  font-weight: 700;
}

select:focus,
input[type="number"]:focus,
input[type="text"]:focus {
  outline: none;
  border-color: var(--color-accent);
}

.participant-total {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-muted);
}

.participant-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.participant-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.participant-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 44px;
}

.btn-random,
.btn-random-all,
.btn-reset,
.btn-clear-all {
  flex: 0 0 auto;
  height: 44px;
  padding: 0 10px;
  border: 1px solid var(--color-hairline);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  background: var(--color-surface);
  color: var(--color-text);
  transition: background 0.15s ease, transform 0.15s ease;
}

/* Random actions sit one step brighter than reset/clear actions at rest,
   not just on hover, so the two are told apart at a glance. */
.btn-random,
.btn-random-all {
  background: var(--color-card);
}

.btn-random:hover,
.btn-random-all:hover,
.btn-reset:hover,
.btn-clear-all:hover {
  background: var(--color-card-hover);
  transform: scale(1.02);
}

.btn-random:active,
.btn-random-all:active,
.btn-reset:active,
.btn-clear-all:active {
  transform: scale(0.97);
}

.btn-random:disabled,
.btn-random-all:disabled,
.btn-reset:disabled,
.btn-clear-all:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-random:disabled:hover,
.btn-random-all:disabled:hover,
.btn-reset:disabled:hover,
.btn-clear-all:disabled:hover {
  transform: none;
}

.btn-random-all,
.btn-clear-all {
  padding: 0 16px;
  border-radius: 999px;
}

.all-actions-row {
  display: flex;
  gap: 8px;
  align-self: flex-start;
}

.map-select-row {
  display: flex;
  gap: 8px;
}

.map-select-row select {
  flex: 1 1 auto;
  min-width: 0;
}

.pill-group {
  display: flex;
  gap: 8px;
}

.pill-group input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.pill-group label {
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--color-hairline);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.pill-group input[type="radio"]:checked + label {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #06120C;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-row label {
  flex: 0 0 auto;
  font-weight: 800;
  color: var(--color-text-muted);
  font-size: 13px;
}

.slider-row input[type="range"] {
  flex: 1 1 auto;
  accent-color: var(--color-accent);
}

.slider-row span {
  flex: 0 0 44px;
  text-align: right;
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text-muted);
}

.settings-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--color-text-muted);
  font-size: 12px;
  font-weight: 600;
  border-top: 1px solid var(--color-hairline);
  padding-top: 12px;
}

.settings-footer button {
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-weight: 800;
  cursor: pointer;
}

/* ---------- Mobile bottom sheet ---------- */
@media (max-width: 767px) {
  .settings-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 85dvh;
    border-radius: 24px 24px 0 0;
    transform: translateY(100%);
    transition: transform 0.3s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .settings-panel.is-open {
    transform: translateY(0);
  }

  .sheet-backdrop {
    position: fixed;
    inset: 0;
    background: var(--color-backdrop);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease;
    z-index: 25;
  }

  .sheet-backdrop.is-open {
    opacity: 1;
    visibility: visible;
  }
}

/* ---------- Desktop static sidebar ---------- */
@media (min-width: 768px) {
  .app {
    display: flex;
  }

  .settings-panel {
    position: static;
    width: 340px;
    height: 100vh;
    transform: none;
    border-right: 1px solid var(--color-hairline);
  }

  .game-area {
    flex: 1 1 auto;
  }

  .fab-settings,
  .sheet-backdrop,
  .sheet-handle,
  .sheet-close {
    display: none;
  }
}
