:root {
  --bg: #0d0f14;
  --panel: #14171f;
  --panel-2: #1a1e28;
  --line: #262b38;
  --text: #e6e9f0;
  --muted: #8a93a6;
  --dim: #454c5e;
  --accent: #6fe3a1;
  --accent-2: #62b6ff;
  --bad: #ff5f6d;
  --mono: "JetBrains Mono", "Fira Code", "Cascadia Code", Consolas, "SF Mono", Menlo, monospace;

  /* content width on a desktop; every screen centres on this */
  --page: 1300px;
  --gutter: 24px;
  --scroll-thumb: #2a3040;
  --scroll-thumb-hover: #3a4356;
}

* { box-sizing: border-box; }

/* The default OS scrollbar is a bright slab against a dark page. */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) transparent;
}
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border: 2px solid transparent;
  background-clip: content-box;
  border-radius: 999px;
}
*::-webkit-scrollbar-thumb:hover { background: var(--scroll-thumb-hover); background-clip: content-box; }
*::-webkit-scrollbar-corner { background: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", system-ui, -apple-system, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden { display: none !important; }

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
button:hover { border-color: #3a4154; background: #222736; }
button:active { transform: translateY(1px); }
button.primary { background: #1e2c42; border-color: #2f4767; color: #cfe4ff; }
button.primary:hover { background: #25374f; }
button.accent { background: #1c3b2b; border-color: #2d6446; color: var(--accent); }
button.accent:hover { background: #224833; }
button.ghost { background: transparent; }
button.big { padding: 11px 20px; font-size: 15px; }
button:disabled { opacity: .45; cursor: default; }

input, select {
  font: inherit;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
}
input:focus, select:focus { border-color: #3d6ea8; }

/* ---------- topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: #0f121a;
  flex: 0 0 auto;
}
.brand {
  font-weight: 700;
  letter-spacing: .5px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}
.top-right { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.name-field { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.name-field input { width: 150px; max-width: 40vw; }

/* ---------- screens ---------- */
.screen { flex: 1 1 auto; min-height: 0; }

#screen-home {
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;  /* no width jump when the bar appears */
}

/* ---------- home ---------- */
.home-wrap {
  width: 100%;
  max-width: var(--page);
  margin: 0 auto;
  padding: clamp(20px, 4vw, 44px) var(--gutter) 40px;
}
.home-wrap h1 { font-size: clamp(24px, 3.4vw, 34px); margin: 0 0 8px; line-height: 1.15; }
.sub { color: var(--muted); margin: 0 0 28px; }

/* level / topics / race time share a row while there is width for it */
.opt-grid {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) minmax(300px, 2fr) minmax(170px, 1fr);
  gap: 18px;
  align-items: start;
}
.opt-grid .panel { margin-bottom: 0; height: 100%; }
.opt-grid { margin-bottom: 20px; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  margin-bottom: 20px;
}
.panel h2 {
  margin: 0 0 14px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: var(--muted);
}
.lang-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 8px;
}
.lang-grid button {
  font-family: var(--mono);
  font-size: 13px;
  padding: 10px;
}
.lang-grid button.on {
  background: #1c3b2b;
  border-color: var(--accent);
  color: var(--accent);
}
.actions { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 20px; }
.join { display: flex; gap: 8px; }
.join input {
  width: 110px;
  font-family: var(--mono);
  text-transform: uppercase;
  letter-spacing: 3px;
  text-align: center;
}
.err { color: var(--bad); min-height: 20px; margin-top: 14px; }

/* ---------- room layout ---------- */
#screen-room { display: flex; overflow: hidden; }

.chat {
  width: 288px;
  flex: 0 0 288px;
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.chat-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}
.room-code { font-family: var(--mono); color: var(--accent); letter-spacing: 2px; }
.chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  font-size: 14px;
}
.msg .who { color: var(--accent-2); font-weight: 600; margin-right: 6px; }
.msg.me .who { color: var(--accent); }
.msg .body { color: #d6dae4; word-break: break-word; }
.msg.sys { color: var(--muted); font-style: italic; font-size: 13px; }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.chat-form input { flex: 1 1 auto; min-width: 0; }
.chat-form button { padding: 8px 12px; }

.main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 16px 22px 22px;
  overflow-y: auto;
  scrollbar-gutter: stable;
}
/* keep the race column readable on a very wide monitor */
.main > * {
  width: 100%;
  max-width: var(--page);
  margin-left: auto;
  margin-right: auto;
}
.room-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.room-meta { display: flex; align-items: center; gap: 10px; }
.label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 1.4px; }
.code-pill {
  font-family: var(--mono);
  letter-spacing: 3px;
  color: var(--accent);
  background: #14231b;
  border-color: #2d6446;
}
.lang-select { font-family: var(--mono); font-size: 13px; }
.room-ctl { display: flex; align-items: center; gap: 10px; }
.badge {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 12px;
}
.badge.racing { color: var(--accent); border-color: #2d6446; }
.badge.countdown { color: #ffc46b; border-color: #6b5326; }

/* ---------- racers ---------- */
.racers { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.racer {
  display: grid;
  grid-template-columns: 150px 1fr 150px;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
}
.racer.me { border-color: #2d6446; }
.racer .who { font-weight: 600; display: flex; align-items: center; gap: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.racer .tag { font-size: 10px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; }
.racer .tag.ready { color: var(--accent); border-color: #2d6446; }
.bar { height: 10px; background: #0b0d12; border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.bar > i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  transition: width .12s linear;
}
.racer .stat { font-family: var(--mono); font-size: 12px; color: var(--muted); text-align: right; }
.racer .stat b { color: var(--text); }

/* ---------- stage ---------- */
.stage { position: relative; }
.hud { display: flex; flex-wrap: wrap; gap: 10px 22px; margin-bottom: 10px; }
.hud-item { display: flex; align-items: baseline; gap: 6px; }
.hud-item b { font-family: var(--mono); font-size: 20px; color: var(--accent); }
.hud-item span { font-size: 11px; text-transform: uppercase; letter-spacing: 1.3px; color: var(--muted); }

.code-box {
  background: #101219;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px 20px;
  max-height: clamp(180px, 46vh, 640px);
  overflow-y: auto;
  position: relative;
}
.code-box.locked { opacity: .5; filter: saturate(.4); }
.code-box.focus { border-color: #3d6ea8; box-shadow: 0 0 0 1px #3d6ea855; }

.code-area {
  margin: 0;
  font-family: var(--mono);
  font-size: 16.5px;
  line-height: 1.75;
  white-space: pre;
  tab-size: 4;
}

/* per-character states — must come after prism theme to beat token colors */
.code-area .ch { color: var(--dim); position: relative; }
.code-area .ch.done { color: inherit; }
.code-area .ch.cur { background: rgba(98, 182, 255, .18); border-radius: 2px; }
.code-area .ch.cur::after {
  content: "";
  position: absolute;
  left: -1px; top: 4%;
  width: 2px; height: 92%;
  background: var(--accent-2);
  animation: blink 1s steps(1) infinite;
}
.code-area .ch.bad { background: rgba(255, 95, 109, .3); color: #ffd7da; border-radius: 2px; }
.code-area .ch.bad::after { background: var(--bad); }
.code-area .nl::before { content: "¬"; color: #2c3242; }
@keyframes blink { 50% { opacity: 0; } }

.trap { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }
.hint { color: var(--muted); font-size: 13px; margin: 10px 2px 0; }

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  background: rgba(13, 15, 20, .82);
  border-radius: 12px;
}
.countdown span {
  font-family: var(--mono);
  font-size: 88px;
  color: var(--accent);
  text-shadow: 0 0 40px rgba(111, 227, 161, .35);
}

/* ---------- results ---------- */
.results {
  margin-top: 18px;
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 18px;
}
.results h3 { margin: 0 0 12px; font-size: 14px; letter-spacing: 1.2px; text-transform: uppercase; color: var(--muted); }
.results table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 14px; }
.results th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding-bottom: 8px; }
.results td { padding: 6px 0; border-top: 1px solid var(--line); }
.results tr.me td { color: var(--accent); }

/* ---------- responsive ---------- */

/* laptops: the three option panels no longer fit side by side comfortably */
@media (max-width: 1100px) {
  :root { --gutter: 18px; }
  .opt-grid { grid-template-columns: 1fr 1fr; }
  .opt-grid .opt-wide { grid-column: 1 / -1; order: -1; }
}

@media (max-width: 960px) {
  .chat { width: 240px; flex-basis: 240px; }
  .racer { grid-template-columns: 120px 1fr 120px; }
  .main { padding: 14px 16px 18px; }
}

/* tablets and phones: one column of options, chat under the race */
@media (max-width: 820px) {
  :root { --gutter: 14px; }
  .opt-grid { grid-template-columns: 1fr; gap: 14px; }
  .opt-grid .opt-wide { order: 0; }
  .opt-grid .panel { height: auto; }

  #screen-room {
    flex-direction: column;
    overflow-y: auto;
    scrollbar-gutter: stable;
  }
  .main { order: 1; overflow: visible; padding: 14px 14px 18px; }
  .chat {
    order: 2;
    width: 100%;
    flex: 0 0 auto;
    max-height: 42vh;
    border-right: 0;
    border-top: 1px solid var(--line);
  }
  .code-area { font-size: 15px; }
  .countdown span { font-size: 64px; }
  .hud-item b { font-size: 18px; }
}

@media (max-width: 620px) {
  .home-wrap h1 { font-size: 22px; }
  .sub { margin-bottom: 20px; }
  .panel { padding: 14px; border-radius: 12px; }
  .lang-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
  .lang-grid button { padding: 9px 6px; font-size: 12px; }

  /* name and bar on one line, progress bar across the row below */
  .racer {
    grid-template-columns: 1fr auto;
    grid-template-areas: "who stat" "bar bar";
    row-gap: 8px;
  }
  .racer .who { grid-area: who; }
  .racer .stat { grid-area: stat; }
  .racer .bar { grid-area: bar; }

  .room-top { gap: 10px; }
  .room-ctl { flex-wrap: wrap; }
  .room-filters .fl { flex-direction: column; gap: 6px; }
  .room-filters .fl-label { padding-top: 0; }

  .actions { flex-direction: column; align-items: stretch; }
  .actions .join { width: 100%; }
  .actions .join input { flex: 1 1 auto; width: auto; }
  .actions button.big { width: 100%; }

  .code-area { font-size: 14px; line-height: 1.7; }
  .code-box { padding: 14px; }
  .countdown span { font-size: 52px; }
  .chip { padding: 5px 8px; font-size: 12px; }
  .lb-row { grid-template-columns: 20px 20px 1fr auto; }
  .lb-row .lb-acc { display: none; }
  .modal-card { padding: 18px; }
}

/* phones in landscape: keep the code area usable */
@media (max-height: 520px) and (orientation: landscape) {
  .code-box { max-height: 52vh; }
  .home-wrap { padding-top: 16px; }
}

/* respect a reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
  .bar > i { transition: none; }
}

/* ---------- filters: level + topic chips ---------- */
.muted { color: var(--muted); font-weight: 400; font-size: 12px; }
.panel h2 .head-actions { margin-left: auto; display: flex; gap: 8px; }
.panel h2 { display: flex; align-items: center; gap: 8px; }

.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent-2);
  font-size: 12px;
}
.link:hover { background: none; text-decoration: underline; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 13px;
  color: var(--muted);
  background: var(--panel-2);
}
.chip .chip-n {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--dim);
  background: #11141b;
  border-radius: 4px;
  padding: 1px 5px;
}
.chip.on { color: var(--accent); border-color: #2d6446; background: #1c3b2b; }
.chip.on .chip-n { color: var(--accent); background: #16301f; }
.chip.off { opacity: .35; cursor: default; }

.warn {
  margin: 10px 0 0;
  font-size: 13px;
  color: #ffc46b;
}

/* ---------- room filter tray ---------- */
.room-filters {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}
.room-filters .fl { display: flex; align-items: flex-start; gap: 10px; }
.room-filters .fl-label {
  min-width: 52px;
  padding-top: 7px;
  font-size: 12px;
  color: var(--muted);
}
.room-filters p { margin: 0; font-size: 12px; }

.snip-meta {
  font-size: 11px;
  font-family: var(--mono);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 3px 7px;
}
.snip-meta.lvl-very-easy { color: var(--accent-2); border-color: #2f4767; }
.snip-meta.lvl-easy { color: var(--accent); border-color: #2d6446; }
.snip-meta.lvl-medium { color: #ffc46b; border-color: #6b5326; }
.snip-meta.lvl-hard { color: var(--bad); border-color: #6b2c31; }

/* ---------- avatars + profile ---------- */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  flex: 0 0 26px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel-2) center / cover no-repeat;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  overflow: hidden;
}
.avatar.has-img { color: transparent; border-color: #2d6446; }
.avatar.sm { width: 20px; height: 20px; flex-basis: 20px; font-size: 10px; }
.avatar.big { width: 72px; height: 72px; flex-basis: 72px; font-size: 28px; }

.profile { display: flex; align-items: center; gap: 8px; }
.profile .pname { font-weight: 600; font-size: 14px; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.msg { display: flex; align-items: baseline; gap: 6px; }
.msg .avatar { align-self: center; }

/* ---------- leaderboard ---------- */
.lb { display: flex; flex-direction: column; gap: 6px; }
.lb-row {
  display: grid;
  grid-template-columns: 22px 20px 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  font-size: 13px;
}
.lb-row.me { border-color: #2d6446; }
.lb-row .lb-i { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.lb-row .lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .lb-wpm { font-family: var(--mono); color: var(--accent); }
.lb-row .lb-acc { font-family: var(--mono); font-size: 11px; color: var(--muted); }

/* ---------- profile modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(6, 8, 12, .72);
}
.modal-card {
  width: 100%;
  max-width: 420px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(0, 0, 0, .55);
}
.modal-card h3 { margin: 0 0 6px; font-size: 18px; }
.modal-card > .muted { display: block; margin-bottom: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.field em { font-style: normal; }
.field input[type="file"] { padding: 7px; font-size: 12px; }

.pf-preview { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.modal-ctl { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
