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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --accent2: #533483;
  --text: #eaeaea;
  --text-muted: #8892a4;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ─── Home View ─── */
.home-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px 16px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
}

.home-view h1 {
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 800;
  letter-spacing: -1px;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--text) 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.home-view .tagline {
  color: var(--text-muted);
  margin-bottom: 48px;
  font-size: 1rem;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.card h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  font-size: 0.8rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

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

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

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
}

.btn-ghost {
  background: transparent;
  border: 2px solid var(--surface2);
  color: var(--text);
}

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

.input-group input {
  flex: 1;
  padding: 14px 16px;
  border-radius: 8px;
  border: 2px solid var(--surface2);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 1px;
  text-transform: uppercase;
  outline: none;
  transition: border-color 0.15s;
}

.input-group input::placeholder {
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
  color: var(--text-muted);
}

.input-group input:focus {
  border-color: var(--accent);
}

.input-group .btn {
  width: auto;
  padding: 14px 20px;
  flex-shrink: 0;
}

/* Waiting state */
.code-display {
  text-align: center;
}

.code-display .game-code {
  font-size: clamp(2rem, 10vw, 3.5rem);
  font-weight: 900;
  letter-spacing: 2px;
  color: var(--accent);
  margin: 16px 0;
  font-family: 'Courier New', monospace;
}

.code-display .instructions {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.waiting-msg {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface2);
  color: var(--text);
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  transition: transform 0.3s ease;
  z-index: 1000;
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
}

.error-msg {
  color: var(--accent);
  font-size: 0.85rem;
  margin-top: 8px;
  text-align: center;
}

/* ─── Game View ─── */
.game-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  min-height: 100vh;
  padding: 8px;
}

.player-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 520px;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: var(--radius);
  margin: 4px 0;
}

.player-bar .player-name {
  font-weight: 600;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.player-bar .piece-color {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--text-muted);
}

.piece-color.white { background: #f0f0f0; }
.piece-color.black { background: #1a1a1a; }

.turn-indicator {
  font-size: 0.8rem;
  padding: 4px 10px;
  border-radius: 20px;
  font-weight: 600;
}

.turn-indicator.active {
  background: var(--accent);
  color: white;
}

.turn-indicator.waiting {
  background: var(--surface2);
  color: var(--text-muted);
}

.game-controls {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 520px;
  padding: 8px 0;
}

.game-controls .btn {
  font-size: 0.85rem;
  padding: 10px;
}

.status-bar {
  width: 100%;
  max-width: 520px;
  text-align: center;
  padding: 6px;
  font-size: 0.85rem;
  color: var(--text-muted);
  min-height: 28px;
}

.status-bar.check { color: #f0a500; font-weight: 600; }
.status-bar.info  { color: var(--accent); font-weight: 500; }

/* Game over overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
}

.overlay-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: var(--shadow);
}

.overlay-card .result-emoji {
  font-size: 3rem;
  margin-bottom: 12px;
}

.overlay-card h2 {
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 8px;
}

.overlay-card p {
  color: var(--text-muted);
  margin-bottom: 24px;
}

/* Move history */
.move-history {
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  margin-top: 8px;
  max-height: 120px;
  overflow-y: auto;
}

.move-history h3 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.move-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  font-size: 0.85rem;
  font-family: monospace;
}

.move-list .move-number {
  color: var(--text-muted);
  margin-right: 2px;
}

.move-list .move-item {
  padding: 2px 6px;
  border-radius: 4px;
  cursor: default;
}

.move-list .move-item:hover {
  background: var(--surface2);
}

/* ─── Connection status indicator ─── */
.conn-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background-color 0.3s;
}

.conn-dot.connected   { background: #4caf50; }
.conn-dot.reconnecting {
  background: #f0a500;
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}

/* ─── Reconnect overlay (shown on page refresh while WS reconnects) ─── */
.reconnect-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 400;
  backdrop-filter: blur(4px);
}

.reconnect-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow);
}

.reconnect-card p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin: 0;
}

/* Disconnected banner */
.disconnected-banner {
  display: none;
  width: 100%;
  max-width: 520px;
  background: #7c3600;
  color: #ffb347;
  padding: 8px 16px;
  border-radius: 8px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 500;
  margin: 4px 0;
}

.disconnected-banner.visible { display: block; }

/* Promotion modal */
.promotion-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 600;
}

.promotion-choices {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  gap: 12px;
}

.promotion-choices button {
  width: 64px;
  height: 64px;
  font-size: 2.5rem;
  background: var(--surface2);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  line-height: 1;
}

.promotion-choices button:hover {
  background: var(--accent2);
}

@media (min-width: 600px) {
  .game-view {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
    padding: 16px;
    gap: 16px;
  }

  .game-view .board-container {
    flex-shrink: 0;
  }

  .game-view .sidebar {
    width: 200px;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .move-history {
    max-height: 400px;
  }
}
