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

:root {
  --bg-0: #0f172a;
  --bg-1: #1e293b;
  --bg-2: #334155;
  --board: #f8fafc;
  --board-grid: rgba(15, 23, 42, 0.05);
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --accent: #a855f7;
  --accent-2: #ec4899;
  --border: rgba(255, 255, 255, 0.12);
  --p1: #e63946;
  --p2: #1d4ed8;
  --p3: #16a34a;
  --p4: #eab308;
  --font: 'Noto Sans KR', 'Malgun Gothic', sans-serif;
}

html, body {
  min-height: 100%;
  background: radial-gradient(ellipse at top, var(--bg-1) 0%, var(--bg-0) 60%);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.screen { display: none; }
.screen.active { display: flex; }

/* ─────────────────────────────────────────
   Setup Screen
───────────────────────────────────────── */
#setup-screen {
  flex-direction: column;
  align-items: center;
  padding: 2.2rem 1.2rem 2.5rem;
  gap: 1.4rem;
  flex: 1;
}

.setup-header {
  text-align: center;
  position: relative;
  width: 100%;
  max-width: 420px;
}

.back-link {
  position: absolute;
  left: 0;
  top: 0.2rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
}
.back-link:hover { color: var(--text); background: rgba(255,255,255,0.05); }

.setup-header h1 {
  font-size: clamp(1.8rem, 6vw, 2.4rem);
  font-weight: 900;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.setup-header p {
  margin-top: 0.4rem;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.setup-card {
  width: 100%;
  max-width: 420px;
  background: rgba(30, 41, 59, 0.7);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.4rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}

.setup-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.setup-label {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
}

.player-count-toggle {
  display: inline-flex;
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.2rem;
  gap: 0.2rem;
}
.pc-btn {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 0.45rem 0.85rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.pc-btn.active {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  color: #fff;
}

.player-slots {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.slot {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.7rem 0.8rem;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.slot-marker {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.slot-marker svg { width: 100%; height: 100%; display: block; }

.slot-info {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}
.slot-name {
  font-size: 0.92rem;
  font-weight: 700;
}
.slot-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.slot-toggle {
  display: inline-flex;
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.15rem;
}
.slot-toggle button {
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}
.slot-toggle button.active {
  background: var(--bg-2);
  color: var(--text);
}
.slot-toggle button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.primary-btn {
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #fff;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  border: none;
  border-radius: 12px;
  padding: 0.95rem 1.4rem;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(168, 85, 247, 0.35);
  transition: filter 0.15s, transform 0.05s;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { transform: translateY(1px); }

.setup-foot {
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
  max-width: 420px;
  line-height: 1.55;
}

/* ─────────────────────────────────────────
   Game Screen
───────────────────────────────────────── */
#game-screen {
  flex-direction: column;
  flex: 1;
  padding: 0.6rem 0.6rem 0.8rem;
  gap: 0.6rem;
}

#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.4rem 0.2rem;
}

.turn-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  background: rgba(30, 41, 59, 0.7);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 0.9rem 0.4rem 0.5rem;
  font-size: 0.9rem;
  font-weight: 700;
}
.turn-marker {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.turn-marker svg { width: 100%; height: 100%; display: block; }

.ghost-btn {
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.85rem;
  cursor: pointer;
}
.ghost-btn:hover { color: var(--text); border-color: var(--text-muted); }

#canvas-wrap {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#game-canvas {
  display: block;
  background: var(--board);
  border-radius: 14px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.4);
  touch-action: none;
}

.toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.55rem 0.95rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(4px);
}

.score-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
}

.score-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: rgba(30, 41, 59, 0.7);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.3rem 0.7rem 0.3rem 0.4rem;
  font-size: 0.82rem;
  font-weight: 700;
}
.score-chip.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(168, 85, 247, 0.25);
}
.score-chip .chip-marker {
  width: 18px; height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.score-chip .chip-marker svg { width: 100%; height: 100%; }

/* ─────────────────────────────────────────
   Result Modal
───────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1.2rem;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.6rem 1.3rem;
  box-shadow: 0 16px 48px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.modal-card h2 {
  font-size: 1.5rem;
  font-weight: 900;
  text-align: center;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.result-detail {
  text-align: center;
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 700;
}
.result-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.result-list li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.8rem;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
}
.result-list li.winner {
  border-color: var(--accent);
  background: rgba(168, 85, 247, 0.12);
}
.result-list .li-marker {
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.result-list .li-marker svg { width: 100%; height: 100%; }
.result-list .li-name { flex: 1; }
.result-list .li-score { color: var(--text-muted); font-weight: 700; }
.modal-actions {
  display: flex;
  justify-content: stretch;
}
.modal-actions .primary-btn { flex: 1; }

/* ─────────────────────────────────────────
   Responsive
───────────────────────────────────────── */
@media (min-height: 720px) {
  #setup-screen { padding-top: 3.4rem; }
}
