:root {
  color-scheme: dark;

  /* Палитра вдохновлена blindtyping.com / monkeytype: тёмный сине-графитовый
     фон + один яркий акцент, без "неонового" перегруза — глазам спокойно. */
  --bg: #1c2128;
  --bg-soft: #20262e;
  --panel: #262c35;
  --panel-soft: #2f3640;
  --panel-elevated: #333c48;
  --border: #3a4250;
  --border-soft: #323a46;

  --text: #cbd0df;
  --text-bright: #f1f3f9;
  --text-muted: #7b8494;
  --text-dim: #545c6a;

  --accent: #4895ef;
  --accent-bright: #74b3ff;
  --accent-dim: rgba(72, 149, 239, 0.16);

  --success: #2dd4bf;
  --success-bright: #5eead4;
  --success-dim: rgba(45, 212, 191, 0.16);

  --error: #ef6c75;
  --error-dim: rgba(239, 108, 117, 0.18);

  --warm: #f5b942;
  --warm-dim: rgba(245, 185, 66, 0.16);

  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;

  --shadow: 0 24px 60px -28px rgba(0, 0, 0, 0.65), 0 0 0 1px var(--border);
  --shadow-sm: 0 10px 24px -14px rgba(0, 0, 0, 0.55);
  --font-ui: 'Plus Jakarta Sans', 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'Roboto Mono', monospace;
}

* { box-sizing: border-box; }

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

body {
  background:
    radial-gradient(circle at 10% -10%, rgba(72, 149, 239, 0.10) 0%, transparent 42%),
    radial-gradient(circle at 95% 5%, rgba(45, 212, 191, 0.08) 0%, transparent 38%),
    var(--bg);
  background-attachment: fixed;
}

button { font-family: inherit; }
img, svg { max-width: 100%; }
a { color: inherit; }

::selection { background: var(--accent-dim); color: var(--text-bright); }

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(28, 33, 40, 0.88);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-soft);
}

.topbar-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  font-size: 1.15rem;
  text-decoration: none;
  color: var(--text-bright);
  cursor: pointer;
  background: none;
  border: none;
}

.brand-emoji { font-size: 1.4rem; }

.brand-text { font-family: var(--font-mono); letter-spacing: -0.01em; }
.brand-text::after {
  content: '_';
  color: var(--accent);
  animation: caret-blink 1.1s steps(1) infinite;
  margin-left: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .brand-text::after { animation: none; }
}

.topnav {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.nav-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text-muted);
  font-weight: 700;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.92rem;
  transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.nav-btn:hover { background: var(--panel-soft); color: var(--text); transform: translateY(-1px); }
.nav-btn.active {
  background: var(--accent-dim);
  color: var(--accent-bright);
  border-color: var(--accent);
}

.player-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 6px 8px 6px 14px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  color: var(--text);
  font-family: var(--font-mono);
}

.player-pill button {
  border: none;
  background: var(--panel-soft);
  color: var(--text-muted);
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
}
.player-pill button:hover { background: var(--error-dim); color: var(--error); }

/* ---------- Layout ---------- */

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 20px 80px;
}

.screen { animation: fadein 0.25s ease; }

@keyframes fadein {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes caret-blink {
  0%, 45% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

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

.center-screen {
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Login ---------- */

.login-card {
  max-width: 420px;
  width: 100%;
  text-align: center;
}

.login-emoji { font-size: 3rem; margin-bottom: 4px; }

.login-card h1 {
  font-size: 1.5rem;
  margin: 0 0 6px;
  color: var(--text-bright);
}

.login-card p.sub {
  color: var(--text-muted);
  margin: 0 0 22px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.field { text-align: left; margin-bottom: 14px; }

.field label {
  display: block;
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.field input {
  width: 100%;
  padding: 13px 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  font-size: 1.05rem;
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--text-bright);
  background: var(--bg-soft);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input::placeholder { color: var(--text-dim); font-weight: 500; }

.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 13px 22px;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: #0d1420;
  width: 100%;
  box-shadow: 0 12px 28px -14px rgba(72, 149, 239, 0.65);
}
.btn-primary:hover { background: var(--accent-bright); }

.btn-ghost {
  background: var(--panel-soft);
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--panel-elevated); border-color: var(--text-dim); }

.btn-success {
  background: var(--success);
  color: #08221d;
  box-shadow: 0 12px 28px -14px rgba(45, 212, 191, 0.6);
}
.btn-success:hover { background: var(--success-bright); }

.btn-block { width: 100%; }

.form-msg {
  margin-top: 12px;
  font-size: 0.9rem;
  font-weight: 700;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  border: 1px solid transparent;
}
.form-msg.error { background: var(--error-dim); color: var(--error); border-color: rgba(239, 108, 117, 0.35); }
.form-msg.info { background: var(--accent-dim); color: var(--accent-bright); border-color: rgba(72, 149, 239, 0.35); }

.code-box { margin-top: 8px; animation: fadein 0.2s ease; }

/* ---------- Level grid ---------- */

.section-title {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 30px 0 14px;
}
.section-title:first-child { margin-top: 0; }

.section-title .emoji { font-size: 1.3rem; }
.section-title h2 { font-size: 1.05rem; margin: 0; color: var(--text-bright); }
.section-title .count { color: var(--text-dim); font-size: 0.82rem; font-weight: 700; font-family: var(--font-mono); }

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 12px;
}

.level-card {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--panel);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-weight: 800;
  font-family: var(--font-mono);
  color: var(--text);
  transition: transform 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
}

.level-card:hover:not(:disabled) {
  transform: translateY(-3px);
  border-color: var(--text-dim);
}
.level-card:disabled { cursor: default; opacity: 0.45; }

.level-card .lv-num { font-size: 1.3rem; }
.level-card .lv-score { font-size: 0.7rem; color: var(--success-bright); font-weight: 700; }
.level-card .lv-lock { font-size: 1.05rem; color: var(--text-dim); }

.level-card.done { border-color: rgba(45, 212, 191, 0.4); background: var(--success-dim); }
.level-card.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim), 0 0 20px -6px rgba(72, 149, 239, 0.5);
}

/* ---------- Game screen ---------- */

.game-top {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.game-title { font-weight: 800; font-size: 1.1rem; color: var(--text-bright); }
.game-title .sub { display: block; font-weight: 600; color: var(--text-muted); font-size: 0.82rem; }

.stat-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.stat-chip {
  background: var(--panel-soft);
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 700;
  font-family: var(--font-mono);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  min-width: 76px;
  text-align: center;
}

.progress-track {
  height: 8px;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 22px;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--success));
  width: 0%;
  transition: width 0.15s ease;
}

.terminal-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  padding: 10px 14px;
}
.terminal-bar .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.terminal-bar .dot.r { background: #ef6c75; }
.terminal-bar .dot.y { background: #f5b942; }
.terminal-bar .dot.g { background: #2dd4bf; }
.terminal-bar .path {
  margin-left: 10px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-dim);
}

.typing-box {
  font-family: var(--font-mono);
  font-size: clamp(1.1rem, 2.5vw, 1.6rem);
  line-height: 2.05;
  letter-spacing: 0.3px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  padding: 26px 28px;
  word-wrap: break-word;
  user-select: none;
  min-height: 140px;
}

.typing-box .ch { border-radius: 3px; padding: 1px 0; color: var(--text-dim); transition: color 0.05s ease; }
.typing-box .ch.correct { color: var(--text-bright); }
.typing-box .ch.wrong { color: var(--error); background: var(--error-dim); }
.typing-box .ch.current { background: var(--accent); color: #0d1420; border-radius: 3px; }
.typing-box .ch.space.current { background: var(--accent); }

.game-actions { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }

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

.mobile-note { display: none; margin-top: 14px; font-size: 0.85rem; color: var(--text-dim); text-align: center; }

@media (pointer: coarse) { .mobile-note { display: block; } }

/* ---------- Results ---------- */

.results-card { max-width: 480px; width: 100%; text-align: center; }

.results-emoji { font-size: 3rem; margin-bottom: 6px; }
.results-card h1 { color: var(--text-bright); }

.results-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 20px 0;
}

.results-stat {
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 8px;
}
.results-stat .v { font-size: 1.4rem; font-weight: 800; color: var(--accent-bright); font-family: var(--font-mono); }
.results-stat .l { font-size: 0.72rem; color: var(--text-dim); font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }

.badge-record {
  display: inline-block;
  background: var(--warm-dim);
  border: 1px solid rgba(245, 185, 66, 0.4);
  color: var(--warm);
  font-weight: 800;
  padding: 8px 18px;
  border-radius: 999px;
  margin-bottom: 14px;
  font-size: 0.9rem;
}

/* ---------- Leaderboard ---------- */

.lb-tabs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }

.lb-select {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  font-weight: 700;
  color: var(--text);
  background: var(--bg-soft);
}

.lb-controls { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; color: var(--text-muted); }

table.lb-table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: var(--radius-md);
}

.lb-table th, .lb-table td {
  padding: 12px 14px;
  text-align: left;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border-soft);
}

.lb-table thead th {
  background: var(--bg-soft);
  color: var(--text-muted);
  font-weight: 800;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border);
}

.lb-table tbody td { font-family: var(--font-mono); }
.lb-table tbody td:nth-child(2) { font-family: var(--font-ui); font-weight: 600; color: var(--text-bright); }

.lb-table tbody tr:hover { background: var(--bg-soft); }
.lb-table tbody tr.me { background: var(--accent-dim); }
.lb-table tbody tr.me td { color: var(--accent-bright); font-weight: 700; }

.lb-rank { font-weight: 800; color: var(--text-dim); width: 32px; }
.lb-rank.top1 { color: var(--warm); }
.lb-rank.top2 { color: #b8c0cc; }
.lb-rank.top3 { color: #d68a4c; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); font-weight: 600; }

/* ---------- Toasts ---------- */

.toast-root {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.toast {
  background: var(--panel-elevated);
  border: 1px solid var(--border);
  color: var(--text-bright);
  font-weight: 700;
  padding: 12px 20px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  animation: fadein 0.2s ease;
}

/* ---------- Misc ---------- */

.back-link {
  border: none;
  background: none;
  color: var(--text-muted);
  font-weight: 700;
  cursor: pointer;
  padding: 6px 0;
  margin-bottom: 10px;
  display: inline-flex;
  gap: 6px;
  align-items: center;
}
.back-link:hover { color: var(--accent-bright); }

.spinner-wrap { display: flex; justify-content: center; padding: 60px 0; font-size: 1.5rem; color: var(--text-dim); }

input[type="radio"] { accent-color: var(--accent); }

/* ---------- Эффекты: тряска, конфетти, вспышка ошибки ---------- */

.fx-confetti-canvas {
  position: fixed;
  inset: 0;
  z-index: 300;
  pointer-events: none;
}

@keyframes fx-shake-kf {
  0% { transform: translate(0, 0) rotate(0); }
  15% { transform: translate(-8px, 2px) rotate(-0.6deg); }
  30% { transform: translate(7px, -3px) rotate(0.5deg); }
  45% { transform: translate(-6px, 3px) rotate(-0.4deg); }
  60% { transform: translate(5px, -2px) rotate(0.3deg); }
  75% { transform: translate(-3px, 2px) rotate(-0.2deg); }
  100% { transform: translate(0, 0) rotate(0); }
}
.fx-shake { animation: fx-shake-kf 0.5s ease; }

.fx-error-overlay {
  position: fixed;
  inset: 0;
  z-index: 250;
  pointer-events: none;
  background: radial-gradient(circle, transparent 35%, rgba(239, 108, 117, 0.4) 100%);
  opacity: 0;
}
@keyframes fx-error-flash-kf {
  0% { opacity: 0; }
  15% { opacity: 1; }
  100% { opacity: 0; }
}
.fx-error-flash { animation: fx-error-flash-kf 0.35s ease-out; }

@media (prefers-reduced-motion: reduce) {
  .fx-shake { animation: none; }
}

@media (max-width: 560px) {
  .app { padding: 18px 14px 70px; }
  .card { padding: 20px; }
  .typing-box { padding: 18px 16px; }
  .results-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
}
