:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --success: #16a34a;
  --warning: #f59e0b;
  --danger: #dc2626;
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 14px;
  --shadow: 0 4px 24px rgba(15, 23, 42, 0.08);
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

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

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

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

.screen { display: none; flex-direction: column; min-height: 100dvh; }
.screen.active { display: flex; }

/* ---------- Header ---------- */
.header {
  padding: 2rem 1.25rem 1.5rem;
  text-align: center;
  background: linear-gradient(135deg, var(--primary) 0%, #7c3aed 100%);
  color: #fff;
}
.header-compact {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.9rem 1.25rem; text-align: left;
}
.header-game {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.9rem 1.25rem; text-align: left;
}
.logo { font-size: 2.75rem; font-weight: 700; letter-spacing: -0.03em; }
.logo span { color: #fef08a; }
.tagline { opacity: 0.9; font-size: 0.95rem; margin-top: 0.25rem; }

/* ---------- Layout ---------- */
.main-content {
  flex: 1; width: 100%; max-width: 900px; margin: 0 auto;
  padding: 1.25rem; display: flex; flex-direction: column; gap: 1.25rem;
}
.game-main { position: relative; max-width: 1000px; }

.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.5rem;
}
.card h2 { font-size: 1.35rem; margin-bottom: 1rem; }
.card h3 { font-size: 1.05rem; margin-bottom: 0.75rem; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 0.5rem; background: var(--bg); padding: 0.35rem; border-radius: 10px; margin-bottom: 1.25rem; }
.tab {
  flex: 1; padding: 0.6rem; border: none; background: transparent;
  border-radius: 8px; font-weight: 600; font-size: 0.9rem; cursor: pointer;
  color: var(--text-muted); font-family: inherit; transition: all 0.15s;
}
.tab.active { background: var(--surface); color: var(--primary); box-shadow: 0 1px 4px rgba(0,0,0,0.08); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 1rem; }
.form-group { display: flex; flex-direction: column; gap: 0.4rem; }
.form-group label { font-weight: 600; font-size: 0.875rem; }
.form-group input[type="text"],
.form-group input[type="number"] {
  padding: 0.7rem 0.85rem; border: 1.5px solid var(--border); border-radius: 10px;
  font-family: inherit; font-size: 1rem; transition: border-color 0.15s;
  background: var(--surface); width: 100%;
}
.form-group input:focus { outline: none; border-color: var(--primary); }
.hint { font-size: 0.78rem; color: var(--text-muted); }

.categories-selector { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.cat-chip {
  padding: 0.45rem 0.75rem; border: 1.5px solid var(--border); border-radius: 20px;
  font-size: 0.82rem; font-weight: 500; cursor: pointer; user-select: none;
  background: var(--surface); transition: all 0.15s;
}
.cat-chip.selected { background: var(--primary-light); border-color: var(--primary); color: var(--primary-dark); }
.cat-chip.disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Buttons ---------- */
.btn {
  padding: 0.7rem 1.25rem; border: none; border-radius: 10px;
  font-family: inherit; font-size: 0.95rem; font-weight: 600;
  cursor: pointer; transition: all 0.15s; display: inline-flex;
  align-items: center; justify-content: center; gap: 0.4rem;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--primary-dark); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:not(:disabled):hover { background: #cbd5e1; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-lg { padding: 0.9rem 1.5rem; font-size: 1rem; width: 100%; }
.btn-copy {
  background: rgba(255,255,255,0.2); border: none; color: #fff;
  padding: 0.4rem; border-radius: 8px; cursor: pointer; display: inline-flex;
}
.btn-leave { background: rgba(255,255,255,0.2); border: none; color: #fff; padding: 0.4rem 0.8rem; border-radius: 8px; font-family: inherit; font-size: 0.85rem; cursor: pointer; }

/* ---------- Room code ---------- */
.room-info { display: flex; align-items: center; gap: 0.5rem; }
.room-code {
  font-family: 'SF Mono', 'Courier New', monospace;
  font-size: 1.3rem; font-weight: 700; letter-spacing: 0.15em;
  background: rgba(255,255,255,0.2); padding: 0.3rem 0.7rem; border-radius: 8px;
}
.round-info { font-size: 0.85rem; opacity: 0.9; }

/* ---------- Players list ---------- */
.players-list { list-style: none; display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1.5rem; }
.players-list li {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.75rem; background: var(--bg); border-radius: 10px;
}
.player-avatar {
  width: 38px; height: 38px; border-radius: 50%; display: flex;
  align-items: center; justify-content: center; font-weight: 700;
  color: #fff; font-size: 0.9rem; flex-shrink: 0;
}
.player-name { font-weight: 600; flex: 1; }
.player-badge { font-size: 0.7rem; padding: 0.15rem 0.5rem; border-radius: 6px; background: var(--primary-light); color: var(--primary-dark); font-weight: 600; }
.player-badge.host { background: #fef3c7; color: #b45309; }
.player-ready { font-size: 0.75rem; color: var(--success); font-weight: 600; }

/* ---------- Lobby settings ---------- */
.lobby-settings { border-top: 1px solid var(--border); padding-top: 1.25rem; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }
.settings-grid .form-group:first-child { grid-column: 1 / -1; }
.lobby-waiting { text-align: center; padding: 2rem 1rem; color: var(--text-muted); border-top: 1px solid var(--border); margin-top: 1rem; }

/* ---------- Game screen ---------- */
.letter-display {
  display: flex; align-items: center; justify-content: center; gap: 0.75rem;
  background: linear-gradient(135deg, var(--primary) 0%, #7c3aed 100%);
  color: #fff; padding: 1rem; border-radius: var(--radius); margin-bottom: 1rem;
}
.letter-label { font-size: 0.9rem; opacity: 0.9; }
.letter-char { font-size: 2rem; font-weight: 700; text-transform: uppercase; }

.categories-grid { display: flex; flex-direction: column; gap: 0.85rem; margin-bottom: 1.25rem; }
.category-row { display: flex; flex-direction: column; gap: 0.35rem; }
.category-label { font-weight: 600; font-size: 0.85rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.category-row input {
  padding: 0.75rem 0.85rem; border: 1.5px solid var(--border); border-radius: 10px;
  font-family: inherit; font-size: 1rem; width: 100%;
}
.category-row input:focus { outline: none; border-color: var(--primary); }
.category-row input.valid { border-color: var(--success); background: #f0fdf4; }
.category-row input.invalid { border-color: var(--danger); background: #fef2f2; }

.game-actions { display: flex; gap: 0.75rem; position: sticky; bottom: 0; background: var(--bg); padding: 0.75rem 0; }
.game-actions .btn { flex: 1; }
#btn-stop { background: var(--danger); color: #fff; font-size: 1.1rem; font-weight: 700; letter-spacing: 0.05em; }
#btn-stop:not(:disabled):hover { background: #b91c1c; }

/* ---------- Timer ---------- */
.timer-container { position: relative; width: 56px; height: 56px; flex-shrink: 0; }
.timer-svg { transform: rotate(-90deg); width: 100%; height: 100%; }
.timer-bg { fill: none; stroke: rgba(255,255,255,0.25); stroke-width: 8; }
.timer-progress {
  fill: none; stroke: #fef08a; stroke-width: 8; stroke-linecap: round;
  stroke-dasharray: 283; stroke-dashoffset: 0; transition: stroke-dashoffset 1s linear, stroke 0.3s;
}
.timer-text {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.95rem; color: #fff;
}
.timer-container.urgent .timer-progress { stroke: #fca5a5; }

/* ---------- Letter Draw Animation ---------- */
.letter-draw-main { flex: 1; display: flex; align-items: center; justify-content: center; }
.letter-draw-container { text-align: center; }
.letter-draw-label { font-size: 1.1rem; color: var(--text-muted); margin-bottom: 2rem; }
.letter-draw-animation {
  width: 180px; height: 180px; margin: 0 auto 2rem;
  border-radius: 50%; background: linear-gradient(135deg, var(--primary) 0%, #7c3aed 100%);
  display: flex; align-items: center; justify-content: center;
  animation: letterDrawPulse 1s ease-in-out infinite alternate;
}
@keyframes letterDrawPulse {
  from { transform: scale(1); box-shadow: 0 0 30px rgba(37, 99, 235, 0.4); }
  to { transform: scale(1.05); box-shadow: 0 0 60px rgba(124, 58, 237, 0.6); }
}
.letter-draw-char {
  font-size: 5rem; font-weight: 800; color: #fff; text-transform: uppercase;
  animation: letterDrawFlip 0.5s ease-out;
}
@keyframes letterDrawFlip {
  from { transform: rotateX(90deg); opacity: 0; }
  to { transform: rotateX(0); opacity: 1; }
}
.letter-draw-hint { font-size: 1rem; color: var(--text-muted); }

/* ---------- Stop Alert ---------- */
.stop-alert-card { text-align: center; padding: 3rem 1.5rem !important; }
.stop-alert-icon { font-size: 4rem; margin-bottom: 1rem; animation: stopAlertBounce 0.5s ease-out; }
@keyframes stopAlertBounce {
  0% { transform: scale(0); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
.stop-alert-card h2 { font-size: 2.5rem; color: var(--danger); margin-bottom: 0.5rem; }
.stop-alert-card p { font-size: 1.1rem; color: var(--text-muted); margin-bottom: 0.25rem; }
.stop-alert-hint { color: var(--success) !important; font-weight: 600; margin-top: 1rem !important; }

/* ---------- Overlay ---------- */
.stop-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,0.75);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 1.25rem; backdrop-filter: blur(4px);
}
.stop-overlay.hidden { display: none; }
.stop-modal {
  background: var(--surface); border-radius: var(--radius); padding: 2rem;
  text-align: center; max-width: 380px; width: 100%;
}
.stop-modal h2 { font-size: 2rem; color: var(--danger); margin-bottom: 0.75rem; }
.stop-modal p { color: var(--text-muted); margin-bottom: 0.5rem; }
.stop-modal strong { color: var(--text); }

/* ---------- Voting ---------- */
.voting-header { text-align: center; margin-bottom: 1.25rem; }
.voting-letter { color: var(--text-muted); margin-top: 0.25rem; }
.voting-letter strong { color: var(--primary); font-size: 1.2rem; text-transform: uppercase; }
.voting-players-status { display: flex; justify-content: center; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem; padding: 0.5rem; background: var(--bg); border-radius: 8px; font-size: 0.8rem; }
.voting-player-status { display: flex; align-items: center; gap: 0.3rem; padding: 0.2rem 0.5rem; border-radius: 20px; background: var(--surface); border: 1px solid var(--border); }
.voting-player-status.voted { background: #dcfce7; border-color: var(--success); color: #15803d; }
.voting-player-status .status-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); }
.voting-player-status.voted .status-dot { background: var(--success); }
.voting-content { display: flex; flex-direction: column; gap: 1.25rem; }
.vote-category { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.vote-category-header { background: var(--bg); padding: 0.6rem 0.85rem; font-weight: 700; font-size: 0.9rem; }
.vote-answers { display: flex; flex-direction: column; }
.vote-answer { display: flex; align-items: center; justify-content: space-between; padding: 0.6rem 0.85rem; border-top: 1px solid var(--border); gap: 0.75rem; }
.vote-answer:first-child { border-top: none; }
.vote-answer-text { flex: 1; font-size: 0.95rem; word-break: break-word; }
.vote-answer-text.empty { color: var(--text-muted); font-style: italic; }
.vote-answer-text.invalid { color: var(--danger); text-decoration: line-through; }
.vote-points { font-weight: 700; font-size: 0.85rem; padding: 0.15rem 0.5rem; border-radius: 6px; white-space: nowrap; }
.vote-points.unique { background: #dcfce7; color: #15803d; }
.vote-points.dup { background: #fef9c3; color: #a16207; }
.vote-points.zero { background: #fee2e2; color: #b91c1c; }
.vote-actions { display: flex; gap: 0.5rem; margin-left: 0.5rem; }
.vote-btn {
  border: 1.5px solid var(--border); background: var(--surface); border-radius: 6px;
  padding: 0.25rem 0.5rem; cursor: pointer; font-size: 0.75rem; font-weight: 600; font-family: inherit;
}
.vote-btn.active-valid { background: #dcfce7; border-color: var(--success); color: #15803d; }
.vote-btn.active-invalid { background: #fee2e2; border-color: var(--danger); color: #b91c1c; }
.voting-actions { display: flex; flex-direction: column; gap: 0.75rem; margin-top: 1.5rem; }

/* ---------- Scoreboard ---------- */
.scoreboard-table { width: 100%; border-collapse: collapse; margin-bottom: 1.5rem; }
.scoreboard-table th, .scoreboard-table td { padding: 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
.scoreboard-table th { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; }
.scoreboard-table tr.winner td { background: #fef9c3; font-weight: 700; }
.scoreboard-table td:first-child { width: 48px; font-weight: 700; color: var(--text-muted); }
.scoreboard-table td:last-child { text-align: right; font-weight: 700; }
.round-breakdown { display: flex; flex-direction: column; gap: 0.75rem; margin-bottom: 1.5rem; }
.round-breakdown details { border: 1px solid var(--border); border-radius: 10px; padding: 0.75rem; }
.round-breakdown summary { cursor: pointer; font-weight: 600; font-size: 0.9rem; }
.final-actions { display: flex; flex-direction: column; gap: 0.75rem; }

/* ---------- Features ---------- */
.features { display: flex; justify-content: center; gap: 1.5rem; flex-wrap: wrap; }
.feature { display: flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; color: var(--text-muted); font-weight: 500; }
.feature-icon { font-size: 1.1rem; }

/* ---------- Toast ---------- */
.toast-container { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 0.5rem; z-index: 200; width: calc(100% - 2.5rem); max-width: 380px; }
.toast {
  background: var(--text); color: #fff; padding: 0.75rem 1rem; border-radius: 10px;
  font-size: 0.875rem; box-shadow: var(--shadow); animation: toastIn 0.25s ease;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }
@keyframes toastIn { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Modal ---------- */
.modal { border: none; border-radius: var(--radius); padding: 0; max-width: 400px; width: calc(100% - 2.5rem); }
.modal::backdrop { background: rgba(15,23,42,0.6); }
.modal-content { padding: 1.5rem; }
.modal-content h3 { margin-bottom: 0.5rem; }
.modal-content p { color: var(--text-muted); margin-bottom: 1.25rem; }

/* ---------- Spinner ---------- */
.spinner {
  width: 28px; height: 28px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%;
  animation: spin 0.8s linear infinite; margin: 1rem auto 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Responsive ---------- */
@media (min-width: 640px) {
  .header { padding: 3rem 1.5rem 2rem; }
  .logo { font-size: 3.25rem; }
  .card { padding: 2rem; }
  .categories-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
  .game-actions { position: static; flex-direction: row; }
  .voting-actions { flex-direction: row; }
  .voting-actions .btn { flex: 1; }
  .final-actions { flex-direction: row; }
  .final-actions .btn { flex: 1; }
}

@media (min-width: 900px) {
  .categories-grid { grid-template-columns: 1fr 1fr 1fr; }
}

@media (max-width: 380px) {
  .logo { font-size: 2.25rem; }
  .settings-grid { grid-template-columns: 1fr; }
}