:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f2f6);
  --text: var(--tg-theme-text-color, #16181d);
  --hint: var(--tg-theme-hint-color, #8a8f9c);
  --accent: var(--tg-theme-button-color, #2f7cf6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --ok: #22a660;
  --bad: #e5484d;
  --line: rgba(127, 127, 127, .22);
  --radius: 14px;
  --p1: #2f7cf6;  /* цвет создателя комнаты */
  --p2: #f76707;  /* цвет второго игрока */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17191e);
    --bg2: var(--tg-theme-secondary-bg-color, #22252c);
    --text: var(--tg-theme-text-color, #eef0f4);
    --hint: var(--tg-theme-hint-color, #8d93a1);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none; }
body { -webkit-user-select: none; user-select: none; }
button, input { font: inherit; color: inherit; }
#app { height: var(--app-h, 100dvh); min-height: 440px; max-width: 560px; margin: 0 auto; position: relative; }

.screen { display: none; height: 100%; flex-direction: column; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); gap: 8px; }
.screen.active { display: flex; }
.center { margin: auto; display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center; width: 100%; }
.center-text { text-align: center; }
.scroll { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.footer { display: flex; flex-direction: column; gap: 8px; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.hidden { display: none !important; }

.btn { border: 0; border-radius: 12px; padding: 12px 16px; background: var(--bg2); font-weight: 600; cursor: pointer; transition: transform .08s, opacity .2s; }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; pointer-events: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; color: var(--hint); }
.btn.big { padding: 15px; font-size: 17px; }

.card { background: var(--bg2); border-radius: var(--radius); padding: 14px; }
.card-title { font-weight: 700; margin-bottom: 8px; }

/* меню */
.menu { max-width: 340px; }
.logo { font-size: 44px; font-weight: 800; letter-spacing: -1px; transform: rotate(-3deg);
  background: linear-gradient(90deg, #ff6b3d, #f6b93b, #2f7cf6); -webkit-background-clip: text; background-clip: text; color: transparent; }
.tagline { margin-top: -4px; margin-bottom: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; width: 100%; text-align: left; font-size: 13px; color: var(--hint); }
input { width: 100%; border: 1px solid var(--line); background: var(--bg2); border-radius: 12px; padding: 12px; outline: none; -webkit-user-select: text; user-select: text; }
input:focus { border-color: var(--accent); }
.menu .btn { width: 100%; }
.join-row { display: flex; gap: 8px; width: 100%; }
.join-row input { text-transform: uppercase; letter-spacing: 3px; text-align: center; font-weight: 700; }

/* лобби */
.code-card { text-align: center; display: flex; flex-direction: column; gap: 8px; }
.room-code { font-size: 40px; font-weight: 800; letter-spacing: 6px; -webkit-user-select: text; user-select: text; }
.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; flex: none;
  box-shadow: 0 0 0 2px var(--bg2), 0 0 0 4px var(--ring, transparent); }
.p-name { font-weight: 600; color: var(--ring, inherit); }
.players .tag { margin-left: auto; font-size: 12px; color: var(--hint); }
.players .empty { color: var(--hint); font-style: italic; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.setting:first-of-type { border-top: 0; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper b { min-width: 64px; text-align: center; }
.stepper button { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--bg); font-size: 20px; cursor: pointer; }
.stepper button:disabled { opacity: .3; }

/* отсчёт */
.countdown { font-size: 120px; font-weight: 800; line-height: 1; color: var(--accent); animation: pop .9s ease-out; }
@keyframes pop { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } to { transform: scale(1); } }

/* верхняя панель */
.topbar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.topbar .opp-status { text-align: right; }
.timer { font-weight: 800; font-size: 20px; font-variant-numeric: tabular-nums; min-width: 56px; text-align: center; }
.timer.low { color: var(--bad); }
.timebar { height: 4px; background: var(--bg2); border-radius: 2px; overflow: hidden; flex: none; }
.timebar > div { height: 100%; width: 100%; background: var(--accent); transform-origin: left; }
.timebar.low > div { background: var(--bad); }
.opp-status.ready { color: var(--ok); }

/* рисование */
.current-word { text-align: center; font-size: 22px; font-weight: 800; min-height: 30px; }
.canvas-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; position: relative; }
#canvas { background: #fff; border-radius: var(--radius); box-shadow: 0 0 0 1px var(--line); touch-action: none; display: block; }
.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.overlay > div { background: rgba(0,0,0,.72); color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 600; }
.tools { display: flex; align-items: center; gap: 8px; flex: none; }
.colors { display: flex; justify-content: space-between; gap: 4px; flex: none; padding: 2px; }
.color { flex: 0 1 30px; aspect-ratio: 1; min-width: 0; padding: 0; border-radius: 50%; border: 0; cursor: pointer; box-shadow: 0 0 0 1px var(--line); }
.color.active { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.tool-btns { display: flex; gap: 6px; flex: 1; }
.tool { width: 44px; height: 44px; border-radius: 10px; border: 0; background: var(--bg2); cursor: pointer; font-size: 17px; display: grid; place-items: center; }
.tool.active { background: var(--accent); color: var(--accent-text); }
.tool .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: block; }
.btn.done { width: 76px; height: 44px; padding: 0; font-size: 22px; }

/* сетка слов 3×3 */
.word-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; flex: none; }
.word { position: relative; border: 0; border-radius: 10px; background: var(--bg2); padding: 8px 4px; min-height: 42px; font-size: 13px; font-weight: 600;
  cursor: pointer; overflow: hidden; display: flex; align-items: center; justify-content: center; text-align: center; line-height: 1.15; }
.word.selected { background: var(--accent); color: var(--accent-text); }
.word.drawn { opacity: .55; }
.word.drawn::after { content: "✓"; position: absolute; top: 2px; right: 5px; font-size: 11px; color: var(--ok); }
.word.used { opacity: .35; }
.word:disabled { cursor: default; }

/* угадывание */
.hint { text-align: center; }
.img-grid-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
.img-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.cell { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #fff; border: 0; padding: 0; cursor: pointer;
  box-shadow: 0 0 0 1px var(--line); }
.cell img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cell .noimg { position: absolute; inset: 0; display: grid; place-items: center; color: #aaa; font-size: 12px; }
.cell.selected { box-shadow: 0 0 0 3px var(--accent); }
.cell .label { position: absolute; left: 3px; right: 3px; bottom: 3px; background: var(--accent); color: var(--accent-text);
  font-size: 11px; font-weight: 700; border-radius: 6px; padding: 2px 4px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cell .num { position: absolute; top: 3px; left: 5px; font-size: 11px; color: #999; font-weight: 700; }

/* результаты */
.score-card { text-align: center; }
.winner { font-size: 22px; font-weight: 800; margin: 4px 0 8px; }
.scoreline { display: flex; justify-content: center; align-items: center; gap: 24px; }
.big-score { font-size: 42px; font-weight: 800; line-height: 1.1; }
.vs { font-size: 32px; color: var(--hint); }
.plus { color: var(--ok); font-weight: 700; }
.tabs { display: flex; background: var(--bg2); border-radius: 12px; padding: 3px; }
.tab { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 10px; font-weight: 600; font-size: 14px; cursor: pointer; color: var(--hint); }
.tab.active { background: var(--bg); color: var(--text); }
.res-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; padding: 4px; }
.res-item { display: flex; flex-direction: column; gap: 3px; font-size: 12px; text-align: center; }
.res-item .cell { cursor: default; }
.res-item.ok .cell { box-shadow: 0 0 0 3px var(--ok); }
.res-item.bad .cell { box-shadow: 0 0 0 3px var(--bad); }
.res-item .w { font-weight: 700; }
.res-item .g { color: var(--bad); text-decoration: line-through; }
.res-item.ok .g { display: none; }

/* прочее */
.spinner { width: 36px; height: 36px; border: 3px solid var(--bg2); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top)); pointer-events: none; transform: translateX(-50%); background: rgba(0,0,0,.82); color: #fff;
  padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 10; max-width: 90vw; text-align: center; }

/* таблица лидеров */
.card-title.row { display: flex; align-items: center; justify-content: space-between; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; padding: 2px 0; }
.board { width: 100%; border-collapse: collapse; font-size: 14px; }
.board th { font-size: 12px; font-weight: 600; color: var(--hint); text-align: right; padding: 4px 0 6px; }
.board th:nth-child(2) { text-align: left; }
.board td { padding: 6px 0; border-top: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.board td:first-child { width: 22px; min-width: 22px; padding-right: 8px; text-align: left; color: var(--hint); font-weight: 700; }
.board td:nth-child(2) { text-align: left; max-width: 0; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 8px; }
.board th:nth-child(n+3), .board td:nth-child(n+3) { padding-left: 12px; white-space: nowrap; }
.board tr.leader td { font-weight: 700; }
.board tr.leader td:first-child { color: #e0a100; }
.board tr.away td { opacity: .5; }

/* переключатель сложности */
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 10px; padding: 3px; gap: 2px; margin: 2px 0 6px; }
.segmented button { flex: 1; border: 0; background: transparent; padding: 8px 4px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--hint); cursor: pointer; }
.segmented button.active { background: var(--accent); color: var(--accent-text); }
.segmented button:disabled { cursor: default; }
.setting + .segmented { margin-top: -4px; }

/* прогресс рисования */
.progress { display: flex; justify-content: center; gap: 8px; padding: 6px 0 2px; flex: none; }
.progress span { width: 12px; height: 12px; border-radius: 50%; background: var(--bg2); box-shadow: inset 0 0 0 1px var(--line); }
.progress span.done { background: var(--ok); box-shadow: none; }
.progress span.cur { background: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent); }

/* ввод своих слов */
.words-title { font-size: 20px; font-weight: 800; text-align: center; }
.word-inputs { display: flex; flex-direction: column; gap: 8px; }
.word-inputs label { display: flex; align-items: center; gap: 10px; }
.word-inputs .n { width: 24px; text-align: right; color: var(--hint); font-weight: 700; flex: none; }
.word-inputs input.dup { border-color: var(--bad); }

/* пауза */
.pause { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 20; padding: 16px; }
.pause-box { background: var(--bg); border-radius: 16px; padding: 22px; display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; max-width: 320px; }
.pause-title { font-size: 20px; font-weight: 800; }

/* переключатель «Скрывать буквы» */
.toggle-row { cursor: pointer; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 15px; background: var(--line); transition: background .2s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:disabled { cursor: default; }
.segmented.two { grid-template-columns: 1fr 1fr; }
