/* ===== CSS Variables ===== */
:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --border: #334155;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-light: rgba(99, 102, 241, 0.15);
  --accent: #22d3ee;
  --correct: #22c55e;
  --correct-bg: rgba(34, 197, 94, 0.1);
  --incorrect: #f87171;
  --incorrect-bg: rgba(248, 113, 113, 0.15);
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --transition: 0.2s ease;
}

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; scroll-behavior: smooth; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  line-height: 1.6;
  min-height: 100vh;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button { cursor: pointer; border: none; font-family: inherit; }
input, textarea, select { font-family: inherit; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--surface); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ===== Layout ===== */
.container { max-width: 900px; margin: 0 auto; padding: 0 1.5rem; }
.container-wide { max-width: 1200px; margin: 0 auto; padding: 0 1.5rem; }

/* ===== Header ===== */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 0;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
}
.logo {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.logo span { color: var(--primary); }
.header-nav { display: flex; align-items: center; gap: 1rem; }
.nav-link {
  color: var(--text-muted);
  font-size: 0.9rem;
  transition: color var(--transition);
}
.nav-link:hover { color: var(--text); text-decoration: none; }

/* ===== Cards ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.card-header {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.2rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  transition: all var(--transition);
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); transform: translateY(-1px); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 0.35rem 0.8rem; font-size: 0.82rem; }
.btn-lg { padding: 0.85rem 2rem; font-size: 1.05rem; }
.btn-full { width: 100%; justify-content: center; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ===== Forms ===== */
.form-group { margin-bottom: 1.1rem; }
.form-label { display: block; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.4rem; font-weight: 500; }
.form-control {
  width: 100%;
  padding: 0.6rem 0.9rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  transition: border-color var(--transition);
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.form-control::placeholder { color: var(--text-dim); }
textarea.form-control { resize: vertical; min-height: 120px; }
.form-hint { font-size: 0.78rem; color: var(--text-dim); margin-top: 0.3rem; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

/* ===== Alerts ===== */
.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.alert-error { background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.3); color: #fca5a5; }
.alert-success { background: rgba(34, 197, 94, 0.1); border: 1px solid rgba(34, 197, 94, 0.3); color: #86efac; }
.alert-info { background: var(--primary-light); border: 1px solid rgba(99, 102, 241, 0.3); color: #a5b4fc; }

/* ===== Badges ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-success { background: var(--correct-bg); color: var(--correct); }
.badge-warning { background: rgba(245, 158, 11, 0.15); color: var(--warning); }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); border: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th {
  background: var(--surface-2);
  color: var(--text-muted);
  font-weight: 600;
  text-align: left;
  padding: 0.7rem 1rem;
  white-space: nowrap;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
td { padding: 0.75rem 1rem; border-top: 1px solid var(--border); }
tr:hover td { background: var(--surface-2); }
.rank-1 td:first-child { color: #fbbf24; font-weight: 700; }
.rank-2 td:first-child { color: var(--text-muted); font-weight: 600; }
.rank-3 td:first-child { color: #cd7c3a; font-weight: 600; }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 1.5rem; }
.tab-btn {
  padding: 0.75rem 1.25rem;
  background: none;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: all var(--transition);
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
  padding: 1rem;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow);
}
.modal-title { font-size: 1.2rem; font-weight: 700; margin-bottom: 1.5rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.75rem; margin-top: 1.5rem; }
.hidden { display: none !important; }

/* ===== Landing page ===== */
.hero {
  text-align: center;
  padding: 5rem 1.5rem 3rem;
}
.hero-title {
  font-size: 3rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 1rem;
}
.hero-sub { color: var(--text-muted); font-size: 1.1rem; max-width: 500px; margin: 0 auto; }
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1.25rem; padding: 2rem 0; }
.game-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  transition: border-color var(--transition), transform var(--transition);
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  display: block;
}
.game-card:hover { border-color: var(--primary); transform: translateY(-2px); text-decoration: none; }
.game-card-title { font-size: 1.15rem; font-weight: 700; margin-bottom: 0.5rem; }
.game-card-desc { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem; min-height: 2.5rem; }
.game-card-meta { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* ===== Login page ===== */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
}
.login-box { width: 100%; max-width: 420px; }
.login-title { font-size: 1.6rem; font-weight: 700; margin-bottom: 0.5rem; }
.login-sub { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 2rem; }

/* ===== Dashboard layout ===== */
.dashboard { padding: 2rem 0; }
.dashboard-title { font-size: 1.6rem; font-weight: 700; margin-bottom: 0.25rem; }
.dashboard-sub { color: var(--text-muted); margin-bottom: 2rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }

/* ===== Game page ===== */
.game-page { min-height: 100vh; display: flex; flex-direction: column; }
.game-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.game-title-text { font-size: 1.1rem; font-weight: 700; }
.game-admin-name { font-size: 0.8rem; color: var(--text-muted); }

/* Pre-game screen */
.pregame {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1.5rem;
  padding: 3rem 1.5rem;
}
.pregame-title { font-size: 2rem; font-weight: 800; text-align: center; }
.pregame-desc { color: var(--text-muted); text-align: center; max-width: 500px; }
.pregame-info { display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; }
.info-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.35rem 1rem;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.pregame-name-wrap { display: flex; gap: 0.75rem; align-items: center; }
.pregame-name-input {
  padding: 0.75rem 1.2rem;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1rem;
  width: 260px;
  transition: border-color var(--transition);
}
.pregame-name-input:focus { outline: none; border-color: var(--primary); }

/* Active game screen */
.game-arena {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}
.stats-bar {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 2rem;
  align-items: center;
}
.stat-item { text-align: center; }
.stat-value {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--primary);
  line-height: 1;
}
.stat-value.warning { color: var(--warning); }
.stat-value.danger { color: var(--danger); }
.stat-label { font-size: 0.72rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.07em; margin-top: 0.2rem; }
.stat-divider { width: 1px; height: 40px; background: var(--border); }

/* Words display */
.words-outer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 2rem;
  margin-bottom: 1rem;
  cursor: text;
  position: relative;
}
.words-container {
  height: 6rem;
  overflow-y: scroll;
  scrollbar-width: none;
}
.words-container::-webkit-scrollbar { display: none; }
.words-display {
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 1.5rem;
  line-height: 2rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding-bottom: 4rem; /* allow scrolling last words into view */
}
.word { display: inline-flex; position: relative; }
.word.done .char { color: var(--text-dim); }
.word.current { outline: 2px solid var(--primary); border-radius: 3px; outline-offset: 2px; }
.char { color: var(--text-muted); transition: color 0.05s; }
.char.correct { color: var(--correct); }
.char.incorrect { color: var(--incorrect); text-decoration: underline; text-decoration-color: var(--incorrect); }
.char.extra { color: var(--incorrect); }
.caret {
  position: absolute;
  width: 2px;
  height: 1.5rem;
  background: var(--primary);
  animation: blink 1s step-end infinite;
  border-radius: 1px;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.typing-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
}
.click-hint { font-size: 0.8rem; color: var(--text-dim); text-align: center; margin-top: 0.75rem; }
.click-hint.active-hint { color: var(--primary); }

/* Progress bar */
.progress-bar {
  height: 3px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 1.5rem;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* Result screen */
.result-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 2rem;
  padding: 3rem 1.5rem;
}
.result-main { text-align: center; }
.result-wpm { font-size: 5rem; font-weight: 800; color: var(--primary); line-height: 1; }
.result-wpm-label { font-size: 1rem; color: var(--text-muted); margin-top: 0.25rem; }
.result-stats { display: flex; gap: 2rem; justify-content: center; flex-wrap: wrap; }
.result-stat { text-align: center; }
.result-stat-value { font-size: 1.8rem; font-weight: 700; }
.result-stat-label { font-size: 0.8rem; color: var(--text-muted); }
.result-actions { display: flex; gap: 1rem; }

/* Ranking section */
.ranking-section { width: 100%; max-width: 600px; }
.ranking-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.8rem;
}
.ranking-row {
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  gap: 1rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  align-items: center;
  font-size: 0.9rem;
}
.ranking-row:last-child { border-bottom: none; }
.ranking-rank { color: var(--text-dim); font-weight: 600; text-align: center; }
.ranking-rank.gold { color: #fbbf24; }
.ranking-rank.silver { color: #94a3b8; }
.ranking-rank.bronze { color: #cd7c3a; }
.ranking-player { font-weight: 500; }
.ranking-wpm { color: var(--primary); font-weight: 700; font-variant-numeric: tabular-nums; }
.ranking-highlight { background: var(--primary-light); border-radius: var(--radius-sm); }

/* ===== Admin cards ===== */
.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.admin-card-info { flex: 1; min-width: 0; }
.admin-card-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.admin-card-username { font-size: 0.8rem; color: var(--text-muted); }
.admin-card-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .hero-title { font-size: 2rem; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .words-display { font-size: 1.2rem; }
  .result-wpm { font-size: 3.5rem; }
  .stats-bar { gap: 1rem; }
  .stat-value { font-size: 1.5rem; }
  .admin-card { flex-direction: column; align-items: flex-start; }
  .admin-card-actions { width: 100%; }
}

/* ===== Utilities ===== */
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 0.85rem; }
.text-center { text-align: center; }
.flex { display: flex; }
.flex-center { display: flex; align-items: center; justify-content: center; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }
.w-full { width: 100%; }
