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

:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --border-filled: #9ca3af;

  --correct: #22c55e;
  --correct-light: #dcfce7;
  --present: #eab308;
  --present-light: #fef9c3;
  --absent: #9ca3af;
  --absent-dark: #6b7280;

  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-light: #e0e7ff;

  --danger: #ef4444;
  --success: #22c55e;
  --info: #6366f1;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 12px rgba(0,0,0,0.04);
  --shadow-lg: 0 4px 24px rgba(0,0,0,0.12);

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', monospace;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ===== Screens ===== */
.screen {
  display: none;
  min-height: 100dvh;
}

.screen.active {
  display: flex;
}

/* ===== Toast System ===== */
#toast-container {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: min(400px, 90vw);
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transform: translateY(-20px);
}

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

.toast.toast-success { border-left: 3px solid var(--success); }
.toast.toast-error { border-left: 3px solid var(--danger); }
.toast.toast-info { border-left: 3px solid var(--info); }

.toast-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  line-height: 1;
  flex-shrink: 0;
  transition: color 0.15s;
}

.toast-close:hover {
  color: var(--text);
}

/* ===== Auth Screen ===== */
#auth-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e0e7ff 0%, #f8f9fa 50%, #dcfce7 100%);
}

.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 48px 40px;
  box-shadow: var(--shadow-lg);
  text-align: center;
  width: min(380px, 90vw);
}

.logo {
  font-size: 48px;
  font-weight: 800;
  letter-spacing: -2px;
  background: linear-gradient(135deg, var(--primary), var(--correct));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.logo.small { font-size: 32px; }
.logo.tiny { font-size: 22px; }

.subtitle {
  color: var(--text-muted);
  margin: 8px 0 32px;
  font-size: 15px;
}

#auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg);
}

input:focus {
  border-color: var(--primary);
}

.error {
  color: var(--danger);
  font-size: 14px;
  margin-top: 8px;
}

.hidden { display: none !important; }

/* ===== Buttons ===== */
.btn {
  font-family: var(--font);
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
  font-size: 15px;
  padding: 12px 24px;
}

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

.btn-primary:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-small {
  padding: 6px 14px;
  font-size: 14px;
}

.btn-large {
  width: 100%;
  padding: 14px 24px;
  font-size: 16px;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
}

.btn-ghost:hover {
  background: var(--border);
  color: var(--text);
}

/* ===== Lobby ===== */
#lobby-screen {
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
}

.lobby-container {
  width: min(460px, 100%);
  text-align: center;
}

.lobby-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
  margin-top: 20px;
  text-align: left;
}

/* ===== Mode Selection ===== */
.mode-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.mode-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 20px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font);
  text-align: center;
}

.mode-card:hover {
  border-color: var(--primary);
  background: var(--primary-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.mode-card:active {
  transform: translateY(0);
}

.mode-card {
  position: relative;
}

.daily-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 12px;
  letter-spacing: 0.3px;
}

.daily-badge.perfect {
  background: var(--correct-light);
  color: #166534;
}

.daily-badge.partial {
  background: var(--present-light);
  color: #854d0e;
}

.daily-badge.none {
  background: var(--bg);
  color: var(--text-muted);
}

.mode-icon {
  font-size: 32px;
  line-height: 1;
}

.mode-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
}

.mode-desc {
  font-size: 13px;
  color: var(--text-muted);
}

.quick-stats {
  margin-top: 16px;
  padding: 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  text-align: center;
}

.quick-stats .stats-grid {
  grid-template-columns: repeat(3, 1fr);
}

.section-back {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.section-back h3 {
  font-size: 17px;
  flex: 1;
}

.daily-date {
  font-size: 15px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.daily-word-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  font-size: 14px;
}

.daily-word-row .dw-word {
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2px;
  flex: 1;
}

.daily-word-row .dw-status {
  font-size: 13px;
  font-weight: 600;
}

.daily-word-row .dw-status.won {
  color: var(--success);
}

.daily-word-row .dw-status.lost {
  color: var(--danger);
}

.player-setup {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.color-palette {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.color-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
}

.color-swatch:hover {
  transform: scale(1.15);
}

.color-swatch.selected {
  border-color: var(--text);
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text);
}

.rounds-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.rounds-setting label {
  font-weight: 600;
  font-size: 15px;
}

.rounds-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

#rounds-display {
  font-size: 20px;
  font-weight: 700;
  min-width: 24px;
  text-align: center;
}

.players-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.player-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 14px;
}

.player-item .color-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.player-item .player-status {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-muted);
}

.lobby-info {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.connection-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 16px;
  font-size: 12px;
  color: var(--text-muted);
}

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

.conn-dot.connected {
  background: var(--success);
  box-shadow: 0 0 4px var(--success);
}

.conn-dot.disconnected {
  background: var(--danger);
  box-shadow: 0 0 4px var(--danger);
}

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

/* ===== Game ===== */
#game-screen {
  flex-direction: column;
  align-items: center;
}

.game-container {
  width: min(500px, 100%);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: 0 8px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-center {
  display: flex;
  align-items: center;
  gap: 8px;
}

.round-badge {
  background: var(--primary-light);
  color: var(--primary);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
}

.game-timer {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.spectator-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--primary-light);
  border-radius: var(--radius-sm);
  margin: 4px 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--primary);
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
}

.spectate-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.spectate-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.spectate-player-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  gap: 8px;
}

.spc-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.spc-name {
  font-weight: 600;
  font-size: 14px;
}

.spc-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.spc-status {
  font-size: 12px;
  color: var(--text-muted);
}

.spc-status.playing {
  color: var(--primary);
  font-weight: 600;
}

.spc-status.done {
  color: var(--success);
}

.spc-watch {
  padding: 4px 12px !important;
  font-size: 12px !important;
}

/* ===== Board ===== */
.board {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 0;
  flex: 1;
  justify-content: center;
  max-height: calc(100dvh - 260px);
}

.board-row {
  display: flex;
  gap: 6px;
}

.tile {
  width: 58px;
  height: 58px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
  text-transform: uppercase;
  transition: border-color 0.1s, transform 0.1s;
  user-select: none;
  cursor: pointer;
  background: var(--surface);
}

.tile.filled {
  border-color: var(--border-filled);
}

.tile.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.tile.correct {
  background: var(--correct);
  border-color: var(--correct);
  color: white;
}

.tile.present {
  background: var(--present);
  border-color: var(--present);
  color: white;
}

.tile.absent {
  background: var(--absent);
  border-color: var(--absent);
  color: white;
}

.tile {
  perspective: 500px;
}

/* ===== Keyboard ===== */
.keyboard {
  padding: 6px 4px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  flex-shrink: 0;
  margin-top: auto;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 5px;
  margin-bottom: 6px;
}

.key {
  height: 58px;
  min-width: 34px;
  padding: 0 8px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--border);
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  font-family: var(--font);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
  user-select: none;
  text-transform: uppercase;
  -webkit-tap-highlight-color: transparent;
  flex: 1;
  max-width: 44px;
}

.key:active {
  transform: scale(0.95);
}

.key.wide {
  min-width: 60px;
  max-width: 72px;
  font-size: 13px;
  letter-spacing: 0.5px;
}

.key.correct {
  background: var(--correct);
  color: white;
}

.key.present {
  background: var(--present);
  color: white;
}

.key.absent {
  background: var(--absent-dark);
  color: white;
}

/* ===== Overlays ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 900;
  padding: 16px;
}

.overlay-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: var(--shadow-lg);
  width: min(420px, 100%);
  text-align: center;
}

.overlay-card h2 {
  font-size: 22px;
  margin-bottom: 12px;
}

#result-word {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 4px;
  text-transform: uppercase;
  color: var(--primary);
  margin-bottom: 20px;
}

.result-next {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 16px;
}

.scoreboard {
  text-align: left;
}

.scoreboard-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  font-size: 14px;
}

.scoreboard-row .rank {
  font-weight: 800;
  font-size: 16px;
  width: 24px;
  text-align: center;
}

.scoreboard-row .sb-color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.scoreboard-row .sb-name {
  font-weight: 600;
  flex: 1;
}

.scoreboard-row .sb-score {
  font-weight: 700;
  color: var(--primary);
}

.scoreboard-row .sb-detail {
  font-size: 12px;
  color: var(--text-muted);
}

/* Local stats */
.local-stats {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  text-align: left;
}

.local-stats h3 {
  font-size: 15px;
  margin-bottom: 10px;
  text-align: center;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

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

.stat-value {
  font-size: 20px;
  font-weight: 800;
}

.stat-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ===== Settings ===== */
.lobby-header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.lobby-header .btn {
  position: absolute;
  right: 0;
}

.header-actions {
  display: flex;
  gap: 4px;
  align-items: center;
}

.settings-card {
  text-align: left;
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.settings-header h2 {
  margin-bottom: 0;
}

.settings-section {
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
}

.settings-section:last-child {
  border-bottom: none;
}

.settings-section h3 {
  font-size: 14px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
}

.settings-profile {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.settings-profile input[type="text"] {
  width: 100%;
}

.version-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.version-badge {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 600;
  background: var(--bg);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
}

.update-status {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
}

.update-status.success {
  color: var(--success);
}

.update-status.checking {
  color: var(--primary);
}

/* ===== Responsive ===== */
@media (max-width: 400px) {
  .tile {
    width: 50px;
    height: 50px;
    font-size: 22px;
  }

  .key {
    height: 52px;
    font-size: 14px;
  }

  .key.wide {
    font-size: 11px;
  }

  .auth-card {
    padding: 32px 24px;
  }

  .logo { font-size: 36px; }
}

@media (max-height: 650px) {
  .tile {
    width: 46px;
    height: 46px;
    font-size: 20px;
  }

  .board {
    padding: 4px 0;
    gap: 3px;
  }

  .key {
    height: 48px;
  }
}
