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

:root {
  --bg: #0f0f1a;
  --bg-surface: #1a1a2e;
  --bg-elevated: #252540;
  --text: #e8e8f0;
  --text-muted: #8888aa;
  --accent: #7c5cfc;
  --accent-glow: #9d80ff;
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --letter-bg: #2a2a4a;
  --letter-active: #7c5cfc;
  --letter-size: 56px;
  --circle-radius: 110px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

/* --- Screens --- */
.screen {
  display: none;
  position: fixed;
  inset: 0;
  padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
  overflow-y: auto;
}
.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.container {
  width: 100%;
  max-width: 400px;
  text-align: center;
}

/* --- Typography --- */
.title {
  font-size: 2.4rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}
.accent { color: var(--accent); }
.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 32px;
}
.hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 8px 0 20px;
}

h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 8px;
}
h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-muted);
}

/* --- Forms --- */
.form-group {
  margin-bottom: 16px;
  text-align: left;
}
.form-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
input[type="text"] {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid var(--bg-elevated);
  border-radius: 12px;
  background: var(--bg-surface);
  color: var(--text);
  font-size: 1.1rem;
  outline: none;
  transition: border-color 0.2s;
}
input[type="text"]:focus {
  border-color: var(--accent);
}
input[type="text"]::placeholder {
  color: var(--text-muted);
  opacity: 0.6;
}

/* --- Buttons --- */
.btn {
  display: block;
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 12px;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.btn:active {
  transform: scale(0.97);
}
.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
}
.btn[hidden] { display: none; }

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 24px 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}
.divider::before, .divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--bg-elevated);
}

.error-msg {
  color: var(--danger);
  font-size: 0.9rem;
  margin-top: 12px;
  padding: 10px;
  background: rgba(248, 113, 113, 0.1);
  border-radius: 8px;
}
.error-msg[hidden] { display: none; }

/* --- Lobby --- */
.duration-picker {
  display: flex;
  gap: 8px;
}
.duration-opt {
  flex: 1;
  padding: 12px 8px;
  border: 2px solid var(--bg-elevated);
  border-radius: 10px;
  background: var(--bg-surface);
  color: var(--text-muted);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.duration-opt.selected {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(124, 92, 252, 0.1);
}
.duration-opt:active {
  transform: scale(0.97);
}
.form-group[hidden] { display: none; }

.room-code {
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  color: var(--accent);
  background: var(--bg-surface);
  border-radius: 16px;
  padding: 16px 24px;
  display: inline-block;
}
.player-list {
  list-style: none;
  margin-bottom: 24px;
}
.player-list li {
  padding: 12px 16px;
  background: var(--bg-surface);
  border-radius: 10px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}
.player-list .host-badge {
  font-size: 0.7rem;
  background: var(--accent);
  color: white;
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 700;
  text-transform: uppercase;
}

/* --- Countdown --- */
.countdown-display {
  font-size: 8rem;
  font-weight: 900;
  color: var(--accent);
  animation: countPulse 1s ease-in-out infinite;
}
@keyframes countPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.7; }
}

/* --- Game Screen --- */
#screen-game {
  padding: 0;
  display: none;
  flex-direction: column;
}
#screen-game.active {
  display: flex;
  justify-content: flex-start;
  align-items: stretch;
}

.game-header {
  padding: calc(8px + var(--safe-top)) 16px 0;
  flex-shrink: 0;
}
.timer-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-elevated);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 8px;
}
.timer-fill {
  height: 100%;
  width: 100%;
  background: var(--success);
  border-radius: 3px;
  transition: width 0.3s linear, background-color 0.5s;
}
.timer-fill.warning { background: var(--warning); }
.timer-fill.danger { background: var(--danger); }

.game-top-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px;
}
.score-display {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--accent);
}
.timer-text {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-muted);
}

.opponent-scores {
  display: flex;
  gap: 12px;
  align-items: center;
}
.opp-score {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
}
.opp-score .opp-pts {
  color: var(--text);
  font-weight: 800;
}

.current-word {
  text-align: center;
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  padding: 12px 16px;
  min-height: 56px;
  color: var(--text);
  flex-shrink: 0;
}

/* --- Letter Circle --- */
.game-area {
  position: relative;
  width: 100%;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  min-height: 280px;
  max-height: 380px;
}

.line-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}
.line-svg line {
  stroke: var(--accent-glow);
  stroke-width: 3;
  stroke-linecap: round;
  opacity: 0.6;
}
.line-svg .line-active {
  opacity: 1;
  stroke-width: 4;
}

.letter-circle {
  position: relative;
  width: calc(var(--circle-radius) * 2 + var(--letter-size));
  height: calc(var(--circle-radius) * 2 + var(--letter-size));
}

.letter-tile {
  position: absolute;
  width: var(--letter-size);
  height: var(--letter-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--letter-bg);
  border: 2px solid transparent;
  border-radius: 50%;
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s, background 0.15s, border-color 0.15s, box-shadow 0.15s;
  z-index: 2;
  text-transform: uppercase;
}
.letter-tile.selected {
  background: var(--letter-active);
  border-color: var(--accent-glow);
  transform: scale(1.15);
  box-shadow: 0 0 20px rgba(124, 92, 252, 0.5);
  color: white;
}
.letter-tile.last-selected {
  box-shadow: 0 0 30px rgba(124, 92, 252, 0.8);
}

/* --- Word Reveal Animation --- */
.letter-tile.revealed {
  background: linear-gradient(135deg, #7c5cfc, #a78bfa);
  border-color: #fbbf24;
  border-radius: 12px;
  color: white;
  transform: scale(1.05);
  box-shadow: 0 0 24px rgba(251, 191, 36, 0.4), 0 0 48px rgba(124, 92, 252, 0.3);
}

.game-area.revealing {
  background: radial-gradient(ellipse at center, rgba(124, 92, 252, 0.06) 0%, transparent 70%);
}

/* Smooth fade for chrome elements during reveal */
.game-header,
.current-word,
.found-words {
  transition: opacity 0.4s ease;
}

/* --- Found Words --- */
.found-words {
  flex-shrink: 0;
  max-height: 120px;
  overflow-y: auto;
  padding: 8px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--bg-elevated);
}
.found-words-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}
.word-chip {
  display: inline-block;
  padding: 4px 10px;
  background: var(--bg-elevated);
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}
.word-chip.bonus {
  background: rgba(124, 92, 252, 0.2);
  color: var(--accent-glow);
}
.word-chip .chip-score {
  color: var(--accent);
  margin-left: 4px;
  font-size: 0.75rem;
}

/* --- Word Feedback --- */
.word-feedback {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 700;
  pointer-events: none;
  z-index: 100;
  animation: feedbackPop 0.6s ease-out forwards;
}
.word-feedback[hidden] { display: none; }
.word-feedback.valid {
  background: rgba(52, 211, 153, 0.2);
  color: var(--success);
}
.word-feedback.invalid {
  background: rgba(248, 113, 113, 0.15);
  color: var(--danger);
}
.word-feedback.duplicate {
  background: rgba(251, 191, 36, 0.15);
  color: var(--warning);
}
@keyframes feedbackPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.05); }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}

/* --- Results --- */
.results-original {
  margin: 16px 0 24px;
  padding: 16px;
  background: var(--bg-surface);
  border-radius: 12px;
}
.results-original .orig-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}
.results-original .orig-word {
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.leaderboard {
  margin-bottom: 24px;
}
.leaderboard-entry {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-surface);
  border-radius: 10px;
  margin-bottom: 6px;
}
.leaderboard-entry.you {
  border: 2px solid var(--accent);
}
.lb-rank {
  width: 30px;
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--text-muted);
}
.lb-rank.gold { color: #fbbf24; }
.lb-rank.silver { color: #94a3b8; }
.lb-rank.bronze { color: #d97706; }
.lb-name {
  flex: 1;
  font-weight: 600;
  text-align: left;
}
.lb-words {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-right: 12px;
}
.lb-score {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--accent);
}

.results-section {
  margin: 24px 0;
  text-align: left;
}
.word-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* Results screen scrollable */
#screen-results {
  overflow-y: auto;
}
#screen-results .container {
  padding-bottom: 40px;
}
#screen-results .btn {
  margin-top: 8px;
}
#btn-play-again {
  background: var(--bg-elevated);
  color: var(--text);
}

/* --- Small screens --- */
@media (max-width: 360px) {
  :root {
    --letter-size: 48px;
    --circle-radius: 90px;
  }
  .title { font-size: 2rem; }
  .current-word { font-size: 1.4rem; }
}

@media (max-height: 600px) {
  :root {
    --circle-radius: 85px;
    --letter-size: 46px;
  }
  .current-word {
    font-size: 1.3rem;
    padding: 6px 16px;
    min-height: 40px;
  }
  .found-words { max-height: 80px; }
  .game-area { min-height: 230px; }
}

/* --- Larger screens --- */
@media (min-width: 500px) {
  :root {
    --letter-size: 64px;
    --circle-radius: 130px;
  }
}
