:root {
  --bg: #140d1d;
  --bg-2: #1f1230;
  --card: rgba(255, 255, 255, 0.04);
  --border: rgba(255, 255, 255, 0.09);
  --text: #f9f2ff;
  --muted: #b9a8cc;
  --orange: #ff8a3d;
  --orange-2: #ffb347;
  --purple: #8f5cff;
  --good: #3ecf72;
  --bad: #ff5d73;
  --a0: #e8590c;
  --a1: #7048e8;
  --a2: #2f9e44;
  --a3: #c2255c;
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 20% 0%, #2a1640 0%, transparent 50%), linear-gradient(160deg, var(--bg) 0%, var(--bg-2) 100%) fixed;
  -webkit-tap-highlight-color: transparent;
}

main { max-width: 1000px; margin: 0 auto; padding: 16px 16px 48px; }

h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: clamp(1.8rem, 5vw, 2.8rem); }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

/* ----- top bar ----- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10; background: rgba(20, 13, 29, 0.9); backdrop-filter: blur(8px);
}
.brand { font-weight: 800; font-size: 1.1rem; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.badge { font-family: ui-monospace, monospace; font-weight: 700; letter-spacing: 2px; padding: 4px 10px; border-radius: 999px; background: rgba(255, 138, 61, 0.18); color: var(--orange-2); }
.icon-btn { background: transparent; border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 6px 10px; font: inherit; cursor: pointer; }
.icon-btn:hover { border-color: var(--orange); }

.connection { position: sticky; top: 56px; z-index: 9; text-align: center; padding: 8px; background: #8a5a00; font-weight: 600; }

/* ----- layout ----- */
.screen { animation: fade 0.25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.hero { text-align: center; margin: 16px 0 24px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 760px) { .grid-2 { grid-template-columns: 1.1fr 0.9fr; } }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.card + .card { margin-top: 0; }

/* ----- forms ----- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 0.9rem; color: var(--muted); }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; }
.field input, .field select {
  font: inherit; font-size: 1.05rem; color: var(--text);
  background: rgba(0, 0, 0, 0.3); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px;
}
.field input:focus, .field select:focus { outline: 2px solid var(--orange); outline-offset: 1px; }
.field select { padding: 8px 12px; }
.code-input { text-transform: uppercase; letter-spacing: 6px; font-family: ui-monospace, monospace; font-weight: 700; }
.join-row { display: flex; gap: 10px; align-items: flex-end; }
.join-row .field { margin-bottom: 0; }
.grow { flex: 1; }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 10px 0 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--orange); }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 18px 0 6px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

/* ----- buttons ----- */
.btn {
  font: inherit; font-weight: 800; font-size: 1rem; cursor: pointer;
  border-radius: 12px; padding: 12px 20px; border: 1px solid transparent; color: var(--text);
  transition: transform 0.1s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, var(--orange), var(--orange-2)); color: #210d0a; }
.btn.secondary { background: linear-gradient(135deg, var(--purple), #b88efb); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.danger-ghost { background: transparent; border-color: rgba(255, 93, 115, 0.5); color: var(--bad); }
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn.wide { width: 100%; }

/* ----- lobby ----- */
.big-code { font-family: ui-monospace, monospace; font-size: clamp(3rem, 14vw, 5rem); font-weight: 900; letter-spacing: 10px; color: var(--orange-2); margin: 8px 0 16px; }
.qr { display: flex; justify-content: center; margin-bottom: 16px; }
.qr img, .qr canvas { padding: 8px; background: #fff; border-radius: 12px; }
.pill { display: inline-block; padding: 4px 10px; border-radius: 999px; background: rgba(255, 255, 255, 0.08); font-size: 0.85rem; font-weight: 700; }

.player-list { list-style: none; padding: 0; margin: 0 0 16px; display: flex; flex-direction: column; gap: 6px; }
.player-list li { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.03); animation: fade 0.25s ease; }
.player-list li.offline { opacity: 0.5; }
.player-list .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: rgba(143, 92, 255, 0.25); color: #d6c6ff; }
.tag.you { background: rgba(255, 138, 61, 0.2); color: var(--orange-2); }
.kick { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 4px 8px; border-radius: 8px; }
.kick:hover { color: var(--bad); background: rgba(255, 93, 115, 0.12); }

.settings { border-top: 1px solid var(--border); padding-top: 16px; }

/* ----- question ----- */
.q-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }
.timer { font-size: 2rem; font-weight: 900; min-width: 3ch; text-align: center; font-variant-numeric: tabular-nums; }
.timer.urgent { color: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.15); } }
.timebar { height: 8px; border-radius: 999px; background: rgba(255, 255, 255, 0.08); overflow: hidden; margin-bottom: 20px; }
.timebar div { height: 100%; width: 100%; background: linear-gradient(90deg, var(--orange), var(--purple)); }

.question-text { font-size: clamp(1.3rem, 4.5vw, 2rem); text-align: center; margin: 12px 0 24px; }
.question-text.small { font-size: clamp(1.1rem, 3.5vw, 1.4rem); margin-bottom: 16px; }

.answers { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .answers { grid-template-columns: 1fr 1fr; } }
.answer {
  position: relative; overflow: hidden;
  display: flex; align-items: center; gap: 14px; text-align: left;
  min-height: 76px; padding: 14px 18px; border-radius: var(--radius); border: 3px solid transparent;
  font: inherit; font-size: 1.1rem; font-weight: 700; color: #fff; cursor: pointer;
  transition: transform 0.1s ease, opacity 0.2s ease;
}
.answer:active:not(:disabled) { transform: scale(0.98); }
.answer:disabled { cursor: default; }
.answer .shape { font-size: 1.4rem; opacity: 0.9; }
.answer .label { flex: 1; position: relative; z-index: 1; }
.answer.a0 { background: var(--a0); }
.answer.a1 { background: var(--a1); }
.answer.a2 { background: var(--a2); }
.answer.a3 { background: var(--a3); }
.answer.dim { opacity: 0.35; }
.answer.picked { border-color: #fff; opacity: 1; }
.answer.correct { opacity: 1; box-shadow: 0 0 0 3px var(--good), 0 0 24px rgba(62, 207, 114, 0.5); }
.answers.reveal .answer::before {
  content: ''; position: absolute; inset: 0; width: var(--fill, 0%);
  background: rgba(255, 255, 255, 0.15); transition: width 0.6s ease;
}
.answer .count { position: relative; z-index: 1; font-size: 1.3rem; font-weight: 900; }

.status { text-align: center; color: var(--muted); font-size: 1.05rem; margin-top: 20px; min-height: 1.5em; }
.host-bar { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 24px; }

/* ----- reveal ----- */
.banner { text-align: center; font-size: clamp(1.3rem, 5vw, 1.9rem); font-weight: 900; padding: 16px; border-radius: var(--radius); margin-bottom: 16px; }
.banner.good { background: rgba(62, 207, 114, 0.18); color: #8ff0b0; }
.banner.bad { background: rgba(255, 93, 115, 0.16); color: #ffb3bf; }
.banner.neutral { background: rgba(143, 92, 255, 0.18); }
#screen-reveal .card { margin-top: 20px; }

.rank-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.rank-list li { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.03); }
.rank-list li.me { background: rgba(255, 138, 61, 0.16); outline: 1px solid rgba(255, 138, 61, 0.4); }
.rank-list .rank { width: 2.2ch; color: var(--muted); font-variant-numeric: tabular-nums; }
.rank-list .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-list strong { font-variant-numeric: tabular-nums; }

/* ----- finished ----- */
.podium { display: grid; grid-template-columns: repeat(3, 1fr); align-items: end; gap: 10px; margin: 24px 0; }
.step { text-align: center; border-radius: var(--radius) var(--radius) 0 0; padding: 14px 8px; background: var(--card); border: 1px solid var(--border); min-width: 0; }
.step.empty { visibility: hidden; }
.step1 { min-height: 190px; background: linear-gradient(180deg, rgba(255, 179, 71, 0.3), rgba(255, 179, 71, 0.05)); }
.step2 { min-height: 150px; }
.step3 { min-height: 120px; }
.step .medal { font-size: 2.2rem; }
.step .name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin: 6px 0; }
.step .score { color: var(--muted); font-size: 0.9rem; }
.you-line { font-size: 1.1rem; margin-bottom: 16px; }

/* ----- toast ----- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 20;
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px;
  background: #2b1d3d; border: 1px solid var(--orange); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

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

/* ----- admin ----- */
.small { font-size: 0.85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
a.icon-btn { text-decoration: none; }
.narrow { max-width: 420px; margin: 40px auto; }

.tabs { display: flex; gap: 6px; margin-bottom: 16px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab { background: none; border: none; border-bottom: 3px solid transparent; color: var(--muted); font: inherit; font-weight: 700; padding: 10px 14px; cursor: pointer; }
.tab.active { color: var(--text); border-bottom-color: var(--orange); }
.tab-panel { display: flex; flex-direction: column; gap: 16px; }
.tab-panel.hidden { display: none; }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.stat-value { font-size: 1.8rem; font-weight: 900; color: var(--orange-2); font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 0.85rem; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.table th { text-align: left; color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.5px; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table td { padding: 10px; border-bottom: 1px solid rgba(255, 255, 255, 0.05); vertical-align: middle; }
.table tr.inactive td { opacity: 0.5; }
.table .names { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.small-btn { padding: 6px 12px; font-size: 0.85rem; margin-left: 6px; }

.search { width: 100%; font: inherit; color: var(--text); background: rgba(0, 0, 0, 0.3); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; margin-bottom: 12px; }

.question-form { border: 1px solid var(--orange); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; background: rgba(255, 138, 61, 0.05); }
.choice-grid { display: grid; grid-template-columns: 1fr; gap: 8px; margin-bottom: 8px; }
@media (min-width: 640px) { .choice-grid { grid-template-columns: 1fr 1fr; } }
.choice-input { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 12px; }
.choice-input.a0 { background: var(--a0); } .choice-input.a1 { background: var(--a1); }
.choice-input.a2 { background: var(--a2); } .choice-input.a3 { background: var(--a3); }
.choice-input input[type="radio"] { width: 20px; height: 20px; accent-color: #fff; }
.choice-input input[type="text"] { flex: 1; min-width: 0; font: inherit; color: #fff; background: rgba(0, 0, 0, 0.25); border: none; border-radius: 8px; padding: 8px 10px; }
.choice-input input[type="text"]::placeholder { color: rgba(255, 255, 255, 0.6); }
.row-actions { display: flex; gap: 10px; }

.switch { position: relative; display: inline-block; width: 42px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: rgba(255, 255, 255, 0.15); border-radius: 999px; cursor: pointer; transition: background 0.2s; }
.switch .slider::before { content: ''; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform 0.2s; }
.switch input:checked + .slider { background: var(--good); }
.switch input:checked + .slider::before { transform: translateX(18px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--orange); }

.games { display: flex; flex-direction: column; gap: 8px; }
.game { border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; background: rgba(255, 255, 255, 0.02); }
.game summary { cursor: pointer; list-style: none; }
.game summary::-webkit-details-marker { display: none; }
.game[open] summary { margin-bottom: 10px; }
.game .rank-list li { padding: 6px 10px; }
@media (max-width: 600px) { #whoami { display: none; } }
.home-card { max-width: 520px; margin: 0 auto; }
.final-boards { margin-top: 8px; }
.final-boards h2 { font-size: 1.1rem; }

/* ----- phones ----- */
html { background: var(--bg); }
.join-row .field { min-width: 0; }
.join-row .field input { width: 100%; min-width: 0; }
.join-row .btn { flex-shrink: 0; }
.topbar { padding-top: max(12px, env(safe-area-inset-top)); }
.topbar-actions { min-width: 0; }

@media (max-width: 760px) {
  body { background-attachment: scroll; }
  main { padding: 12px 12px calc(40px + env(safe-area-inset-bottom)); }
  .card { padding: 16px; }
  .hero { margin: 8px 0 16px; }

  /* Lobby: keep the code readable but compact, and keep Start always reachable */
  #screen-lobby .card.center p { margin: 0; }
  .big-code { margin: 4px 0 10px; letter-spacing: 6px; }
  .qr img, .qr canvas { width: 132px !important; height: 132px !important; }
  .qr { margin-bottom: 10px; }
  body.has-action-bar main { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
  #startBtn {
    position: fixed; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
    width: auto; z-index: 5; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  }

  /* Question: bigger tap targets, timer always visible */
  .answer { min-height: 64px; font-size: 1.05rem; }
  .question-text { margin: 8px 0 16px; }
  .host-bar .btn { flex: 1; }
}

@media (max-width: 420px) {
  .brand span { display: none; }
  .badge { letter-spacing: 1px; padding: 4px 8px; }
  .icon-btn { padding: 6px 8px; }
  .field.inline { flex-wrap: wrap; gap: 6px; }
  .podium { gap: 6px; }
  .step .name { font-size: 0.9rem; }
  .step .medal { font-size: 1.8rem; }
  .rank-list li { padding: 8px 10px; gap: 8px; }
}
.podium { display: flex; justify-content: center; }
.podium .step { flex: 1; max-width: 220px; }
