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

:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --border: #e4e7ed;
  --border-strong: #cdd3db;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #eff6ff;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --gold: #f59e0b;
  --gold-soft: #fffbeb;
  --silver: #9ca3af;
  --silver-soft: #f3f4f6;
  --bronze: #b45309;
  --bronze-soft: #fef3e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15,23,42,.04), 0 4px 12px rgba(15,23,42,.04);
  --shadow-lg: 0 4px 12px rgba(15,23,42,.06), 0 12px 32px rgba(15,23,42,.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Navigation ───────────────────────────────────────── */
nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 58px;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(8px);
}

.nav-brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -.3px;
  display: flex; align-items: center; gap: .4rem;
}

.nav-links { display: flex; gap: .15rem; align-items: center; }

.nav-links a {
  color: var(--text-muted);
  text-decoration: none;
  padding: .4rem .8rem;
  border-radius: 7px;
  font-size: .9rem;
  transition: background .15s, color .15s;
}

.nav-links a:hover { background: var(--bg); color: var(--text); }
.nav-links a.active { color: var(--accent); font-weight: 600; background: var(--accent-soft); }

#nav-user {
  font-size: .85rem;
  color: var(--text);
  font-weight: 500;
  padding: 0 .5rem;
  border-left: 1px solid var(--border);
  margin-left: .4rem;
}

#nav-admin { color: var(--accent); font-weight: 600; }

#nav-logout {
  cursor: pointer;
  background: none;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: .35rem .75rem;
  font-size: .85rem;
  color: var(--text-muted);
  transition: border-color .15s, color .15s;
  margin-left: .3rem;
}
#nav-logout:hover { border-color: var(--danger); color: var(--danger); }

/* ── Layout ───────────────────────────────────────────── */
.container { max-width: 920px; margin: 0 auto; padding: 2.25rem 1.5rem 4rem; }

h1.page-title { font-size: 1.75rem; font-weight: 800; letter-spacing: -.5px; margin-bottom: .25rem; }
.page-subtitle { color: var(--text-muted); margin-bottom: 1.75rem; font-size: .95rem; }

h2 { font-size: 1.1rem; font-weight: 700; }
h3 { font-size: 1rem; font-weight: 600; }

/* ── Cards ────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

/* ── Buttons ──────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: .55rem 1.15rem;
  border-radius: 8px;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background .15s, transform .15s, opacity .15s, box-shadow .15s;
  text-decoration: none;
  font-family: inherit;
}
.btn:active { transform: scale(.98); }

.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(37,99,235,.2); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); border-color: var(--border-strong); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-lg { padding: .8rem 1.6rem; font-size: 1rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Forms ────────────────────────────────────────────── */
.form-group { margin-bottom: 1rem; }
label { display: block; font-size: .825rem; font-weight: 500; margin-bottom: .35rem; color: var(--text); }
input[type=text], input[type=password], input[type=number], select {
  width: 100%;
  padding: .55rem .85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .95rem;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
  font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }

.error-msg { color: var(--danger); font-size: .85rem; margin-top: .5rem; }
.success-msg { color: var(--success); font-size: .85rem; margin-top: .5rem; }

/* ── Tabs ─────────────────────────────────────────────── */
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 1.5rem; }
.tab-btn {
  padding: .65rem 1.2rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: .9rem;
  color: var(--text-muted);
  margin-bottom: -1px;
  transition: color .15s, border-color .15s;
  font-family: inherit;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ── Game Cards ───────────────────────────────────────── */
.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: .9rem;
  margin-bottom: 2.25rem;
}

.game-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  transition: transform .2s, box-shadow .2s, border-color .2s;
  text-decoration: none;
  color: inherit;
  display: block;
  position: relative;
  overflow: hidden;
}
.game-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--game-color, var(--accent));
  opacity: 0;
  transition: opacity .2s;
}
.game-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
  border-color: var(--game-color, var(--accent));
}
.game-card:hover::before { opacity: 1; }
.game-card .icon {
  font-size: 2.25rem;
  margin-bottom: .6rem;
  display: inline-block;
  transition: transform .2s;
}
.game-card:hover .icon { transform: scale(1.1) rotate(-3deg); }
.game-card h3 { font-size: 1rem; margin-bottom: .25rem; }
.game-card p { font-size: .825rem; color: var(--text-muted); line-height: 1.45; }

/* ── Leaderboard table ────────────────────────────────── */
.rank-table, .overall-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 1rem;
}
.rank-table th, .overall-table th {
  padding: .85rem 1.1rem;
  text-align: left;
  font-size: .76rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--bg);
  font-weight: 600;
}
.rank-table td, .overall-table td {
  padding: 1rem 1.1rem;
  border-bottom: 1px solid #f3f4f6;
  vertical-align: middle;
}
.rank-cell { font-weight: 800; font-size: 1.15rem; text-align: center; }
.rank-table tbody tr:hover, .overall-table tbody tr:hover { background: #fafbfc; }
.rank-table tbody tr:last-child td, .overall-table tbody tr:last-child td { border-bottom: none; }
.rank-table tbody tr.me td, .overall-table tbody tr.me td { background: var(--accent-soft); }

.medal-gold   { color: var(--gold);   font-weight: 700; }
.medal-silver { color: var(--silver); font-weight: 700; }
.medal-bronze { color: var(--bronze); font-weight: 700; }
.cell-gold   { background: var(--gold-soft) !important; }
.cell-silver { background: var(--silver-soft) !important; }
.cell-bronze { background: var(--bronze-soft) !important; }

.score-cell { text-align: right; white-space: nowrap; }
.rank-num { font-weight: 800; }
.points-badge {
  display: inline-block; background: var(--accent); color: #fff;
  border-radius: 999px; padding: .15rem .6rem; font-size: .78rem; font-weight: 700;
}

/* ── Compact leaderboard (in side panels) ─────────────── */
.leaderboard table {
  width: 100%; border-collapse: collapse;
}
.leaderboard tr.highlight td { background: var(--accent-soft); font-weight: 600; }
.rank { font-weight: 700; color: var(--text-muted); }
.rank-1 { color: var(--gold); }
.rank-2 { color: var(--silver); }
.rank-3 { color: var(--bronze); }

/* ── Game pages ───────────────────────────────────────── */
.game-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1.5rem; }
.game-header h1 { font-size: 1.4rem; }

.score-display {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.5rem;
  text-align: center;
  margin-bottom: 1rem;
}
.score-display .score-val { font-size: 2.5rem; font-weight: 800; color: var(--accent); line-height: 1; }
.score-display .score-label { font-size: .75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; margin-top: .3rem; }

.game-area {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Reaction game */
#reaction-area {
  width: 100%;
  min-height: 280px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  border-radius: var(--radius);
  transition: background .15s;
}
.reaction-text { font-size: 1.4rem; font-weight: 600; color: var(--text-muted); text-align: center; padding: 2rem; }

/* Typing game */
#typing-text {
  font-size: 1.15rem;
  line-height: 2;
  letter-spacing: .02em;
  padding: 1.25rem;
  border-radius: var(--radius);
  background: var(--bg);
  border: 1px solid var(--border);
  margin-bottom: 1rem;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
}
#typing-text .correct { color: var(--success); }
#typing-text .wrong { color: var(--danger); background: #fee2e2; border-radius: 2px; }
#typing-text .cursor { border-left: 2px solid var(--accent); margin-left: -1px; }
#typing-text .remaining { color: var(--text-muted); }

#typing-input {
  width: 100%;
  padding: .65rem .9rem;
  font-size: 1rem;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  border: 1px solid var(--border);
  border-radius: 8px;
}
#typing-input:focus { outline: none; border-color: var(--accent); }
#typing-input:disabled { background: var(--bg); opacity: .6; }

/* Result overlay */
.result-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
  text-align: center;
  margin-top: 1rem;
  box-shadow: var(--shadow);
}
.result-box h2 { font-size: 1.4rem; margin-bottom: .5rem; }
.result-score { font-size: 3rem; font-weight: 800; color: var(--accent); margin: .5rem 0; letter-spacing: -1px; }
.result-box p { color: var(--text-muted); margin-bottom: 1rem; }

/* Auth page */
.auth-wrap { max-width: 400px; margin: 4rem auto; padding: 0 1rem; }

/* Badges */
.badge { display: inline-block; padding: .2rem .55rem; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.badge-blue { background: var(--accent-soft); color: var(--accent); }
.badge-green { background: #dcfce7; color: #15803d; }
.badge-yellow { background: #fef3c7; color: #b45309; }

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

/* Responsive */
@media (max-width: 640px) {
  nav { padding: 0 1rem; }
  .nav-links { gap: .1rem; }
  .nav-links a { padding: .4rem .6rem; font-size: .85rem; }
  #nav-user { padding: 0 .35rem; margin-left: .2rem; }
  .container { padding: 1.5rem 1rem 3rem; }
}
