/* Telegram injects --tg-theme-* variables; the fallbacks cover a plain browser. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f5);
  --text: var(--tg-theme-text-color, #15161a);
  --hint: var(--tg-theme-hint-color, #85868c);
  --link: var(--tg-theme-link-color, #2678d8);
  --btn: var(--tg-theme-button-color, #2678d8);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --line: color-mix(in srgb, var(--hint) 22%, transparent);
  --red: #e5383b;
  --red-dark: #b8242a;
  --ok: #23a455;
  --no: #d64045;
  --joke: #8f5bd6;
  --warn: #d68c00;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #16171b;
    --bg2: #22242a;
    --text: #f1f1f4;
    --hint: #8d8f97;
    --link: #5aa3f0;
    --btn: #3a8ee6;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#app { max-width: 560px; margin: 0 auto; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }
h1, h2, h3 { margin: 0; line-height: 1.15; }
h1 { font-size: 30px; font-weight: 900; letter-spacing: -0.5px; }
h2 { font-size: 19px; margin: 22px 0 10px; }
h3 { font-size: 15px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--hint); margin-bottom: 8px; }
p { margin: 8px 0; }
a { color: var(--link); }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.center-text { text-align: center; }
button { font: inherit; color: inherit; cursor: pointer; }

.screen { animation: fade 0.18s ease-out; }
.screen.center { min-height: 70vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

/* STOP octagon */
.sign {
  display: inline-grid; place-items: center; flex: none;
  background: var(--red); color: #fff; font-weight: 900; letter-spacing: 0.5px;
  clip-path: polygon(30% 0, 70% 0, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0 70%, 0 30%);
}
.sign > span {
  width: 86%; height: 86%; display: grid; place-items: center;
  border: 2px solid rgba(255, 255, 255, 0.85);
  clip-path: polygon(30% 0, 70% 0, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0 70%, 0 30%);
}

.brand { display: flex; align-items: center; gap: 14px; margin: 6px 0 18px; }
.brand .sub { margin: 2px 0 0; color: var(--hint); }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 2px; color: var(--text); cursor: pointer; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 650; text-decoration: none;
  transition: transform 0.08s, opacity 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; }
.btn.primary { background: var(--btn); color: var(--btn-text); }
.btn.secondary { background: var(--bg2); color: var(--text); }
.btn.link { background: none; color: var(--hint); font-weight: 500; display: block; margin: 14px auto 0; }
.btn.big { width: 100%; padding: 15px; font-size: 17px; border-radius: 14px; }
.btn.wide { width: 100%; }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.sticky-cta { position: sticky; bottom: calc(12px + env(safe-area-inset-bottom)); margin-top: 18px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18); z-index: 5; }

input {
  font: inherit; color: var(--text); background: var(--bg2);
  border: 1.5px solid transparent; border-radius: 12px; padding: 12px 14px; outline: none; width: 100%;
}
input:focus { border-color: var(--btn); }
input::placeholder { color: var(--hint); }
.join { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-top: 12px; }

.card { background: var(--bg2); border-radius: var(--radius); padding: 14px; margin-top: 12px; }
.card .card { background: var(--bg); }
details.card summary { cursor: pointer; font-weight: 650; }
.rules { margin-top: 22px; }
.rules p { color: var(--hint); font-size: 14px; }

/* lobby */
.players { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.player { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--bg2); border-radius: 12px; }
.player.away { opacity: 0.55; }
.pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.you { color: var(--hint); font-weight: 400; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--btn); color: var(--btn-text); }
.tag.dim { background: var(--line); color: var(--hint); }
.avatar {
  position: relative; flex: none; border-radius: 50%; overflow: hidden; display: inline-grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px;
}
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.settings .lbl { display: block; font-size: 13px; font-weight: 650; color: var(--hint); margin: 14px 0 6px; }
.settings .lbl:first-child { margin-top: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 6px 6px 6px 11px; border-radius: 99px;
  background: var(--bg); border: 1px solid var(--line); font-size: 14px;
}
.chip .x { border: 0; background: none; color: var(--hint); font-size: 17px; line-height: 1; padding: 0 5px; }
.chips.suggest { margin-top: 10px; }
.chip.add { padding-right: 11px; color: var(--hint); border-style: dashed; background: none; }
.stepper { display: inline-flex; align-items: center; gap: 4px; background: var(--bg); border-radius: 12px; padding: 4px; }
.stepper button { width: 40px; height: 36px; border: 0; border-radius: 9px; background: var(--bg2); font-size: 20px; }
.stepper button:disabled { opacity: 0.35; }
.stepper b { min-width: 36px; text-align: center; font-size: 17px; }
.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 9px; padding: 8px 2px; font-size: 14px; font-weight: 600; color: var(--hint); }
.seg button.on { background: var(--btn); color: var(--btn-text); }
.toggle { display: flex; align-items: center; gap: 10px; margin-top: 16px; font-size: 15px; }
.toggle input { width: 20px; height: 20px; accent-color: var(--btn); }
.wait { text-align: center; color: var(--hint); margin: 22px 0 0; }

/* countdown */
.roll {
  width: 150px; height: 150px; display: grid; place-items: center; margin: 10px 0;
  font-size: 96px; font-weight: 900; color: #fff; background: var(--red); border-radius: 36px;
  box-shadow: 0 12px 30px color-mix(in srgb, var(--red) 40%, transparent);
}
.roll.landed { animation: pop 0.45s cubic-bezier(0.2, 1.6, 0.4, 1); }
@keyframes pop { from { transform: scale(0.6); } }

/* play */
.playbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px;
  margin: -16px -16px 0; padding: 10px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
}
.letter-badge {
  flex: none; width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center;
  background: var(--red); color: #fff; font-size: 30px; font-weight: 900;
}
.letter-badge.big { width: 96px; height: 96px; font-size: 60px; border-radius: 26px; }
.timer { flex: 1; position: relative; height: 34px; border-radius: 10px; background: var(--bg2); overflow: hidden; }
.timer-fill { position: absolute; inset: 0 auto 0 0; background: color-mix(in srgb, var(--btn) 28%, transparent); transition: width 0.1s linear; }
.timer-fill.low { background: color-mix(in srgb, var(--red) 35%, transparent); }
.timer span { position: relative; display: grid; place-items: center; height: 100%; font-weight: 700; font-variant-numeric: tabular-nums; }
.stop-btn {
  flex: none; width: 64px; height: 64px; border: 0; color: #fff; font-weight: 900; font-size: 14px; letter-spacing: 0.5px;
  background: #9a9ba1; opacity: 0.55;
  clip-path: polygon(30% 0, 70% 0, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0 70%, 0 30%);
  transition: background 0.2s, opacity 0.2s, transform 0.08s;
}
.stop-btn.ready { background: var(--red); opacity: 1; animation: throb 1.2s ease-in-out infinite; }
.stop-btn:active { transform: scale(0.92); }
.stop-btn:disabled { animation: none; opacity: 0.4; }
@keyframes throb { 50% { transform: scale(1.06); } }
.stop-banner {
  margin: 10px 0 0; padding: 10px 14px; border-radius: 12px; text-align: center; font-weight: 800;
  background: var(--red); color: #fff; animation: pulse 0.9s ease-in-out infinite alternate;
}
@keyframes pulse { to { background: var(--red-dark); } }
.opponents { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 2px; min-height: 4px; justify-content: center; }
.opp { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 3px; border-radius: 99px; background: var(--bg2); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.opp.full { background: color-mix(in srgb, var(--red) 18%, var(--bg2)); color: var(--red); }
.answers { display: grid; gap: 10px; margin-top: 10px; }
.field { position: relative; display: block; }
.field .cat { display: block; font-size: 13px; font-weight: 650; color: var(--hint); margin: 0 0 4px 4px; }
.field input { padding-right: 46px; font-size: 18px; }
.field .mark { position: absolute; right: 14px; bottom: 13px; font-weight: 800; font-size: 15px; }
.field.ok .mark { color: var(--ok); }
.field.ok input { border-color: color-mix(in srgb, var(--ok) 45%, transparent); }
.field.bad .mark { color: var(--warn); }
.field.bad input { border-color: color-mix(in srgb, var(--warn) 60%, transparent); }
.field input:disabled { opacity: 0.6; }

/* judging */
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--btn); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: min(280px, 80%); height: 8px; border-radius: 99px; background: var(--bg2); overflow: hidden; margin-top: 8px; }
.progress div { height: 100%; background: var(--red); transition: width 0.3s; }
.screen.center .sign { animation: wobble 1.6s ease-in-out infinite; }
@keyframes wobble { 25% { transform: rotate(-6deg); } 75% { transform: rotate(6deg); } }

/* results */
.results-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.results-head h2 { margin: 0; }
.results-head p { margin: 2px 0 0; }
.scoreboard { list-style: none; padding: 6px 14px; }
.scoreboard li { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.scoreboard li:last-child { border-bottom: 0; }
.scoreboard li.mine .pname { color: var(--link); }
.scoreboard .rank { width: 18px; color: var(--hint); font-weight: 700; text-align: center; }
.scoreboard .delta { color: var(--ok); font-weight: 700; font-size: 14px; }
.scoreboard .score { min-width: 40px; text-align: right; font-size: 17px; font-variant-numeric: tabular-nums; }
.cat-card { padding: 12px 14px 8px; }
.res-row {
  display: grid; grid-template-columns: minmax(64px, 30%) 1fr auto; column-gap: 10px; align-items: baseline;
  padding: 7px 0; border-top: 1px solid var(--line);
}
.res-row:first-of-type { border-top: 0; }
.res-row .who { color: var(--hint); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res-row.mine .who { color: var(--link); font-weight: 650; }
.res-row .ans { font-weight: 650; overflow-wrap: anywhere; }
.res-row .icon { display: inline-block; width: 22px; font-weight: 800; }
.res-row .pts { font-weight: 800; font-variant-numeric: tabular-nums; }
.res-row .why { grid-column: 2 / 4; font-size: 13px; color: var(--hint); margin-top: 2px; }
.res-row.ok .icon, .res-row.ok .pts { color: var(--ok); }
.res-row.joke .pts { color: var(--joke); }
.res-row.joke .ans { color: var(--joke); }
.res-row.warn .icon, .res-row.warn .pts { color: var(--warn); }
.res-row.no .icon { color: var(--no); }
.res-row.no .ans { text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--no) 70%, transparent); color: var(--hint); }
.res-row.no .pts, .res-row.none .pts, .res-row.none .ans { color: var(--hint); }

/* final */
.trophy { font-size: 64px; text-align: center; margin-top: 8px; animation: pop 0.6s cubic-bezier(0.2, 1.6, 0.4, 1); }
.podium { display: flex; align-items: flex-end; justify-content: center; gap: 10px; margin: 18px 0 6px; }
.step { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 30%; max-width: 130px; text-align: center; }
.step .pname { max-width: 100%; font-size: 14px; }
.step .block {
  width: 100%; border-radius: 12px 12px 4px 4px; display: grid; place-items: center;
  color: #fff; font-weight: 900; font-size: 22px; background: var(--btn);
}
.step.p1 .block { height: 96px; background: var(--red); }
.step.p2 .block { height: 68px; }
.step.p3 .block { height: 48px; opacity: 0.8; }

/* chrome */
#net {
  position: fixed; top: 8px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--warn); color: #fff; padding: 6px 14px; border-radius: 99px; font-size: 13px; font-weight: 700;
}
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60;
  max-width: calc(100% - 32px); background: rgba(20, 20, 24, 0.92); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 14px; text-align: center; animation: fade 0.18s ease-out;
}
[hidden] { display: none !important; }
