:root {
  --bg-base: #16141f;
  --bg-elevated: #1f1b2eCC;
  --bg-card: #211d31;
  --bg-card-hover: #282239;
  --border-soft: #ffffff14;
  --accent: #f4a340;
  --accent-dim: #f4a34033;
  --accent2: #6ee7b7;
  --accent2-dim: #6ee7b733;
  --error: #f2545b;
  --error-dim: #f2545b33;
  --text-primary: #ede9f5;
  --text-muted: #9891ab;
  --text-faint: #6b6480;
  --font-ui: "Space Grotesk", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;
  --shadow-card: 0 8px 30px #00000055;
}

* { box-sizing: border-box; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-ui);
}

body {
  position: relative;
  overflow-x: hidden;
}

.bg-glow {
  position: fixed;
  inset: -20%;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(45% 35% at 18% 12%, #f4a34022, transparent 60%),
    radial-gradient(40% 30% at 85% 20%, #6ee7b71a, transparent 60%),
    radial-gradient(50% 40% at 50% 100%, #7c5cff1a, transparent 60%);
  animation: drift 24s ease-in-out infinite alternate;
}
@keyframes drift {
  from { transform: translate3d(0,0,0) scale(1); }
  to   { transform: translate3d(-2%, 1%, 0) scale(1.05); }
}

a { color: inherit; }
button { font-family: inherit; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 14px 28px;
  background: #16141fcc;
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-soft);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.15rem;
  text-decoration: none;
  letter-spacing: -0.02em;
  background: none;
  border: none;
  cursor: pointer;
}
.brand-mark { color: var(--accent); font-size: 1.3rem; }

.topnav {
  display: flex;
  gap: 4px;
  flex: 1;
}

.navlink {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 500;
  transition: color .15s, background .15s;
}
.navlink:hover { color: var(--text-primary); background: #ffffff0a; }
.navlink.active { color: var(--bg-base); background: var(--accent); font-weight: 600; }

.topbar-right { display: flex; align-items: center; gap: 10px; }

.icon-btn {
  position: relative;
  background: #ffffff0a;
  border: 1px solid var(--border-soft);
  color: var(--text-primary);
  width: 38px; height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 1rem;
}
.icon-btn:hover { background: #ffffff14; }

.dot {
  position: absolute;
  top: 4px; right: 5px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--error);
}
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app {
  position: relative;
  z-index: 1;
  max-width: 1080px;
  margin: 0 auto;
  padding: 40px 24px 80px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 24px;
}

.glass {
  background: var(--bg-elevated);
  backdrop-filter: blur(14px);
}

/* ---------- Buttons & fields ---------- */
.btn {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.92rem;
  border-radius: var(--radius-sm);
  padding: 11px 18px;
  border: 1px solid var(--border-soft);
  background: #ffffff0a;
  color: var(--text-primary);
  cursor: pointer;
  transition: transform .12s ease, background .15s, border-color .15s;
}
.btn:hover { background: #ffffff14; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #1a1206; border-color: transparent; }
.btn-primary:hover { background: #ffb35c; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text-primary); background: #ffffff0a; }
.btn-full { width: 100%; }
.btn-sm { padding: 7px 12px; font-size: 0.82rem; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 0.85rem; color: var(--text-muted); }
.field input, .field textarea, .field select {
  font-family: var(--font-ui);
  background: #ffffff0a;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text-primary);
  font-size: 0.95rem;
}
.field input:focus, .field textarea:focus, .field select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.form-error {
  background: var(--error-dim);
  border: 1px solid #f2545b55;
  color: #ffb3b6;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

/* ---------- Modal ---------- */
.modal {
  border: none;
  border-radius: var(--radius-lg);
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 0;
  box-shadow: 0 20px 60px #000000aa;
  max-width: 380px;
  width: 92vw;
}
.modal::backdrop { background: #0a0912cc; backdrop-filter: blur(4px); }
.modal-card { padding: 26px; }
.modal-tabs { display: flex; gap: 6px; margin-bottom: 18px; background: #ffffff0a; border-radius: var(--radius-sm); padding: 4px; }
.modal-tab { flex: 1; background: transparent; border: none; color: var(--text-muted); padding: 8px; border-radius: 7px; cursor: pointer; font-weight: 600; font-size: 0.85rem; }
.modal-tab.active { background: var(--accent); color: #1a1206; }

/* ---------- Typing test ---------- */
.test-config {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  padding: 10px;
}
.chip-group { display: flex; gap: 4px; background: #ffffff08; border-radius: var(--radius-sm); padding: 4px; }
.chip {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 7px 13px;
  border-radius: 7px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.chip.active { background: var(--accent-dim); color: var(--accent); }
.chip-divider { width: 1px; align-self: stretch; background: var(--border-soft); margin: 4px 2px; }

.test-stage {
  position: relative;
  padding: 8px 4px 4px;
}

.live-stats {
  display: flex;
  justify-content: center;
  gap: 36px;
  margin-bottom: 18px;
  font-family: var(--font-mono);
}
.live-stat { text-align: center; }
.live-stat .val { font-size: 2rem; font-weight: 700; color: var(--accent); line-height: 1; }
.live-stat .lbl { font-size: 0.72rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: .08em; }

.text-display {
  font-family: var(--font-mono);
  font-size: 1.55rem;
  line-height: 2.1;
  letter-spacing: 0.01em;
  color: var(--text-faint);
  max-height: 8.4em;
  overflow: hidden;
  padding: 6px 2px;
  user-select: none;
  outline: none;
}
.text-display .char { position: relative; border-radius: 2px; }
.text-display .char.correct { color: var(--text-primary); }
.text-display .char.incorrect { color: var(--error); background: var(--error-dim); }
.text-display .char.current { color: var(--text-primary); }
.text-display .char.current::before {
  content: '';
  position: absolute;
  left: -1px; top: -2px; bottom: -2px;
  width: 2px;
  background: var(--accent);
  border-radius: 2px;
  animation: caret-blink 1s step-end infinite;
  box-shadow: 0 0 8px var(--accent);
}
@keyframes caret-blink { 50% { opacity: 0.15; } }
.text-display .word { display: inline-block; margin-right: 0.55ch; }

.hidden-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px; height: 1px;
}

.test-footer {
  display: flex;
  justify-content: center;
  margin-top: 18px;
}

.focus-hint {
  text-align: center;
  color: var(--text-faint);
  font-size: 0.82rem;
  margin-top: 8px;
}

/* ---------- Results ---------- */
.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 14px;
  margin: 18px 0;
}
.result-tile {
  background: #ffffff08;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  padding: 16px;
  text-align: center;
}
.result-tile .val { font-family: var(--font-mono); font-size: 1.8rem; font-weight: 700; color: var(--accent2); }
.result-tile .lbl { font-size: 0.72rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: .06em; margin-top: 4px; }

.achievement-toast {
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(135deg, var(--accent-dim), var(--accent2-dim));
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  animation: pop .35s ease;
}
@keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- Confetti ---------- */
.confetti-piece {
  position: fixed;
  top: -10px;
  width: 8px; height: 14px;
  z-index: 100;
  pointer-events: none;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(105vh) rotate(540deg); opacity: 0.4; }
}

/* ---------- Leaderboard ---------- */
.lb-controls { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; align-items: center; }
.lb-search { flex: 1; min-width: 160px; }
.lb-search input { width: 100%; }

table.lb-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
table.lb-table th {
  text-align: left;
  color: var(--text-faint);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-soft);
}
table.lb-table td { padding: 10px; border-bottom: 1px solid #ffffff08; }
table.lb-table tr:hover td { background: #ffffff06; }
.rank-cell { font-family: var(--font-mono); font-weight: 700; color: var(--text-faint); width: 40px; }
.rank-cell.gold { color: #ffd166; }
.rank-cell.silver { color: #d9d9e3; }
.rank-cell.bronze { color: #e0a970; }
.user-cell { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: grid; place-items: center;
  font-weight: 700; font-size: 0.8rem;
  color: #1a1206;
  flex-shrink: 0;
}
.pagination { display: flex; justify-content: center; gap: 8px; margin-top: 16px; }

/* ---------- Profile ---------- */
.profile-header {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.avatar-lg {
  width: 84px; height: 84px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: grid; place-items: center;
  font-size: 2rem; font-weight: 700;
  color: #1a1206;
}
.profile-name { font-size: 1.5rem; font-weight: 700; }
.profile-meta { color: var(--text-muted); font-size: 0.88rem; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 12px;
  margin-top: 20px;
}
.stat-card { background: #ffffff08; border-radius: var(--radius-md); padding: 14px; text-align: center; }
.stat-card .val { font-family: var(--font-mono); font-size: 1.4rem; font-weight: 700; }
.stat-card .lbl { font-size: 0.7rem; color: var(--text-faint); text-transform: uppercase; }

.achv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-top: 12px; }
.achv-card { background: #ffffff08; border: 1px solid var(--border-soft); border-radius: var(--radius-md); padding: 14px; text-align: center; }
.achv-card .icon { font-size: 1.6rem; }
.achv-card .name { font-weight: 600; font-size: 0.85rem; margin-top: 4px; }
.achv-card .desc { font-size: 0.72rem; color: var(--text-faint); margin-top: 2px; }
.achv-card.locked { opacity: 0.35; filter: grayscale(1); }

canvas#wpmChart { width: 100%; height: 180px; margin-top: 8px; }

/* ---------- Toasts ---------- */
.toast-root {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.toast {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  box-shadow: var(--shadow-card);
  font-size: 0.85rem;
  animation: pop .3s ease;
  max-width: 280px;
}
.toast.error { border-color: var(--error); }

/* ---------- Section headings ---------- */
.section-title { font-size: 1.1rem; font-weight: 700; margin: 0 0 4px; }
.section-sub { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 16px; }

/* ---------- Daily challenge ---------- */
.daily-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent2-dim);
  color: var(--accent2);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

/* ---------- Focus visibility ---------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .topbar { padding: 12px 14px; gap: 12px; }
  .topnav { gap: 0; overflow-x: auto; }
  .navlink { padding: 7px 10px; font-size: 0.82rem; white-space: nowrap; }
  .text-display { font-size: 1.15rem; line-height: 1.9; }
  .live-stats { gap: 20px; }
  .live-stat .val { font-size: 1.4rem; }
  .app { padding: 20px 14px 60px; }
}
