:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --text: #1c1e2b;
  --muted: #6b6f85;
  --border: #e1e3ee;
  --brand: #4f46e5;
  --brand-soft: #e8e7fd;
  --select: #2563eb;
  --select-soft: #dbe7ff;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --gold: #f5b301;
  --silver: #a7afbe;
  --bronze: #c47a3a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 20, 50, .06), 0 4px 16px rgba(20, 20, 50, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12131c;
    --surface: #1c1e2b;
    --text: #eceef7;
    --muted: #9a9fb8;
    --border: #2d3042;
    --brand: #8b85ff;
    --brand-soft: #2a2850;
    --select: #60a5fa;
    --select-soft: #1d3157;
    --ok: #4ade80;
    --ok-soft: #173a26;
    --bad: #f87171;
    --bad-soft: #42191c;
    --warn: #fbbf24;
    --warn-soft: #3d2e0d;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
button, input, textarea, select { font: inherit; color: inherit; }
img { max-width: 100%; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* ---- layout ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--brand); }
.topbar .spacer { flex: 1; }
.wrap { max-width: 720px; margin: 0 auto; padding: 20px 16px 48px; }
.wrap.wide { max-width: 1100px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.center { text-align: center; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---- controls ---- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { border-color: var(--brand); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-danger { color: var(--bad); }
.btn-danger:hover { border-color: var(--bad); }
.btn-big { padding: 14px 22px; font-size: 1.1rem; }
.btn-link { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px; }
input[type=text], input[type=password], input[type=url], textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
}
input:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
.error-text { color: var(--bad); min-height: 1.2em; font-size: .9rem; }

.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: .8rem; font-weight: 700; letter-spacing: .02em;
  background: var(--brand-soft); color: var(--brand);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

/* ---- connection banner & toasts ---- */
#conn-banner {
  display: none;
  position: fixed; left: 0; right: 0; top: 0; z-index: 50;
  background: var(--warn); color: #fff; text-align: center;
  font-size: .85rem; font-weight: 600; padding: 4px;
}
body.offline #conn-banner { display: block; }
#toasts {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  z-index: 60; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px);
}
.toast {
  background: var(--text); color: var(--bg);
  border-radius: 10px; padding: 10px 14px; box-shadow: var(--shadow);
  animation: rise .2s ease-out;
}
.toast-error { background: var(--bad); color: #fff; }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }

/* ---- player: question ---- */
.q-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: .9rem; margin-bottom: 8px; }
.q-text { font-size: 1.45rem; font-weight: 700; margin: 0 0 14px; }
.q-image { display: block; width: 100%; max-height: 300px; object-fit: contain; border-radius: 10px; margin-bottom: 14px; background: var(--bg); }
.options { display: grid; gap: 10px; }
@media (min-width: 600px) { .options { grid-template-columns: 1fr 1fr; } }
.option {
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 64px; text-align: left;
  padding: 12px 14px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color .12s, background .12s, transform .06s;
}
.option:active:not(:disabled) { transform: scale(.98); }
.option .letter {
  flex: none; display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 9px;
  background: var(--bg); font-weight: 800;
}
.option.selected { border-color: var(--select); background: var(--select-soft); }
.option.selected .letter { background: var(--select); color: #fff; }
.option:disabled { cursor: default; opacity: .55; }
.option.correct { opacity: 1; border-color: var(--ok); background: var(--ok-soft); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 25%, transparent); }
.option.correct .letter { background: var(--ok); color: #fff; }
.option.wrong { opacity: 1; border-color: var(--bad); background: var(--bad-soft); }
.option.wrong .letter { background: var(--bad); color: #fff; }
.hint { color: var(--muted); font-size: .875rem; margin-top: 12px; text-align: center; }

.verdict { font-size: 1.2rem; font-weight: 700; margin-top: 16px; text-align: center; }
.verdict.ok { color: var(--ok); }
.verdict.bad { color: var(--bad); }
.note {
  margin-top: 14px; padding: 14px; border-radius: 10px;
  background: var(--brand-soft); border-left: 4px solid var(--brand);
  animation: drop .25s ease-out;
}
@keyframes drop { from { opacity: 0; transform: translateY(-6px); } }

/* ---- player: waiting ---- */
.pulse {
  width: 18px; height: 18px; margin: 18px auto; border-radius: 50%;
  background: var(--brand); animation: pulse 1.6s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--brand) 55%, transparent); }
  70% { box-shadow: 0 0 0 18px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

/* ---- leaderboard ---- */
.podium { display: flex; align-items: flex-end; justify-content: center; gap: 10px; margin: 24px 0; }
.podium .place { flex: 1; max-width: 160px; text-align: center; }
.podium .name { font-weight: 700; overflow-wrap: anywhere; }
.podium .block {
  margin-top: 8px; border-radius: 10px 10px 0 0; color: #fff;
  display: grid; place-items: center; font-size: 1.6rem; font-weight: 800;
}
.podium .p1 .block { height: 130px; background: var(--gold); }
.podium .p2 .block { height: 95px; background: var(--silver); }
.podium .p3 .block { height: 70px; background: var(--bronze); }
.ranking { list-style: none; padding: 0; margin: 0; }
.ranking li {
  display: flex; gap: 12px; align-items: center;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.ranking li.me { background: var(--select-soft); border-radius: 10px; font-weight: 700; }
.ranking .rank { width: 2.2em; color: var(--muted); font-variant-numeric: tabular-nums; }
.ranking .nm { flex: 1; overflow-wrap: anywhere; }
.ranking .sc { font-weight: 700; font-variant-numeric: tabular-nums; }

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 5; }
.confetti i {
  position: absolute; top: -12px; width: 8px; height: 14px; border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }
@media (prefers-reduced-motion: reduce) {
  .confetti, .pulse { animation: none; display: none; }
}

/* ---- moderator ---- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tab { background: none; border: 0; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 3px solid transparent; }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }
.grid-2 { display: grid; gap: 16px; }
@media (min-width: 900px) { .grid-2 { grid-template-columns: 3fr 2fr; } }
.controls { display: flex; flex-wrap: wrap; gap: 10px; }
.tally-bar { display: grid; grid-template-columns: 34px 1fr 3em; gap: 8px; align-items: center; margin: 6px 0; }
.tally-bar .bar { height: 22px; border-radius: 6px; background: var(--bg); overflow: hidden; }
.tally-bar .fill { height: 100%; background: var(--select); transition: width .25s; }
.tally-bar.is-correct .fill { background: var(--ok); }
.tally-bar .count { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.big-number { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.players-table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.players-table td { padding: 7px 4px; border-bottom: 1px solid var(--border); }
.players-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--border); }
.dot.on { background: var(--ok); }
.preview-option { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); margin: 6px 0; }
.preview-option.correct { border-color: var(--ok); background: var(--ok-soft); font-weight: 700; }
.q-editor { margin-bottom: 16px; }
.q-editor .opts { display: grid; gap: 8px; }
.q-editor .opt-row { display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: center; }
.q-editor .opt-row input[type=radio] { width: 20px; height: 20px; accent-color: var(--ok); }
.q-editor .thumb { max-height: 120px; border-radius: 8px; margin-top: 8px; }
.q-editor .warn-box { background: var(--warn-soft); color: var(--warn); padding: 8px 12px; border-radius: 8px; font-size: .9rem; }
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); max-width: 420px; padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
