:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --fg: var(--tg-theme-text-color, #1b1b1f);
  --muted: var(--tg-theme-hint-color, #7a7d85);
  --accent: var(--tg-theme-button-color, #3b82f6);
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --surface: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --tile-border: #b9bcc4;
  --key: #d3d6dc;            /* неиспользованная клавиша: светлая */
  --key-fg: #1b1b1f;
  --key-absent: #6b6e75;     /* буквы нет в слове: заметно темнее, текст приглушён */
  --key-absent-fg: #d9dbe0;
  --correct: #5b9e5b;
  --present: #c9a63c;
  --absent: #7c7f86;
}
/* Тема: явный выбор Telegram (data-theme) важнее системной; без атрибута — по prefers-color-scheme. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --tile-border: #4d5058; --key: #82858e; --key-fg: #ffffff;
    --key-absent: #2f3035; --key-absent-fg: #8b8e96; --absent: #3a3b40;
  }
}
:root[data-theme="dark"] {
  --tile-border: #4d5058; --key: #82858e; --key-fg: #ffffff;
  --key-absent: #2f3035; --key-absent-fg: #8b8e96; --absent: #3a3b40;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; min-height: 100vh; overscroll-behavior: none;
}
.hidden { display: none !important; }

.top { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--surface); }
.brand { font-weight: 800; letter-spacing: 1px; font-size: 18px; }
.tabs { display: flex; gap: 4px; margin-left: auto; background: var(--surface); border-radius: 10px; padding: 3px; }
.tab { border: 0; background: transparent; color: var(--muted); padding: 6px 12px; border-radius: 8px; font-weight: 600; }
.tab.active { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.lang { border: 0; background: var(--surface); font-size: 20px; padding: 4px 8px; border-radius: 8px; }

.screen { flex: 1; display: flex; flex-direction: column; padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); }

.home { display: flex; flex-direction: column; gap: 12px; margin: auto 0; align-items: stretch; }
.lead { text-align: center; color: var(--muted); margin: 0 0 8px; }
.big { border: 0; border-radius: 12px; padding: 14px; font-size: 17px; font-weight: 700; background: var(--surface); color: var(--fg); }
.big.primary { background: var(--accent); color: var(--accent-fg); }
.big:disabled { opacity: .5; }
.daily-done { background: var(--surface); border-radius: 12px; padding: 10px 14px; font-size: 14px; color: var(--muted); display: flex; gap: 12px; align-items: center; }
.daily-done pre { margin: 0; font-size: 14px; line-height: 1.1; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 6px 0 0; }

.mode-line { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 14px; padding: 0 2px 6px; }
.link { border: 0; background: none; color: var(--accent); font-size: 14px; padding: 0; }

.board { display: grid; grid-template-rows: repeat(6, 1fr); gap: 6px; width: min(100%, 330px); margin: 0 auto; }
.rowt { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tile {
  aspect-ratio: 1; display: grid; place-items: center; font-size: 30px; font-weight: 800; text-transform: uppercase;
  border: 2px solid var(--tile-border); border-radius: 6px; transition: transform .1s;
}
.tile.filled { border-color: var(--muted); animation: pop .12s; }
.tile.flip { animation: flip .5s ease forwards; }
.tile.correct, .tile.present, .tile.absent { border-color: transparent; color: #fff; }
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }
.rowt.shake { animation: shake .4s; }
@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); background: var(--fg); color: var(--bg);
  padding: 8px 14px; border-radius: 8px; font-weight: 600; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 5;
}
.toast.show { opacity: 1; }

.keyboard { margin-top: auto; padding-top: 12px; display: flex; flex-direction: column; gap: 6px; user-select: none; }
.krow { display: flex; justify-content: center; gap: 4px; }
.key {
  flex: 1; max-width: 40px; height: 48px; border: 0; border-radius: 6px; background: var(--key); color: var(--key-fg);
  font-size: 15px; font-weight: 700; text-transform: uppercase; touch-action: manipulation;
}
.key.wide { max-width: 72px; flex: 1.8; font-size: 13px; }
.key.enter { background: var(--accent); color: var(--accent-fg); text-transform: none; font-size: 14px; }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent { background: var(--key-absent); color: var(--key-absent-fg); }
.key:active { filter: brightness(.85); }

.result { margin: auto 0; text-align: center; display: flex; flex-direction: column; gap: 10px; align-items: center; }
.result h2 { margin: 0; }
.result pre { margin: 0; background: var(--surface); padding: 10px 16px; border-radius: 12px; line-height: 1.15; }
.points { color: var(--accent); font-weight: 700; margin: 0; }
.row { display: flex; gap: 10px; width: 100%; }
.row .big { flex: 1; }

.periods { display: flex; gap: 4px; background: var(--surface); border-radius: 10px; padding: 3px; margin-bottom: 6px; }
.periods button { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 7px 0; border-radius: 8px; font-weight: 600; }
.periods button.active { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.leaders { list-style: none; padding: 0; margin: 14px 0 0; }
.leaders li { display: flex; align-items: center; gap: 10px; padding: 9px 6px; border-bottom: 1px solid var(--surface); }
.leaders .place { width: 28px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.leaders .name { flex: 1; }
.leaders .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.leaders li.me { background: var(--surface); border-radius: 8px; }
.me { color: var(--muted); font-size: 14px; }
.consent { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--surface); }
.switch { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); }
@media (prefers-reduced-motion: reduce) { .tile, .rowt { animation: none !important; } }

.stats { background: var(--surface); border-radius: 12px; padding: 12px; margin-bottom: 12px; }
.stat-row { display: grid; grid-template-columns: repeat(4, auto 1fr); gap: 2px 6px; align-items: baseline; font-size: 13px; color: var(--muted); }
.stat-row b { font-size: 20px; color: var(--fg); font-variant-numeric: tabular-nums; }
.dist { margin-top: 10px; display: flex; flex-direction: column; gap: 3px; font-size: 12px; }
.dist .d { display: flex; align-items: center; gap: 6px; }
.dist .d i { font-style: normal; width: 10px; color: var(--muted); }
.dist .d b { height: 16px; background: var(--correct); border-radius: 3px; color: #fff; font-size: 11px; padding: 0 5px; line-height: 16px; min-width: 18px; text-align: right; }

/* --- дуэль --- */
.lobby { display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--surface); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.card h3 { margin: 0; font-size: 15px; }
.profile-card { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; background: var(--surface); border-radius: 12px; padding: 10px 6px; text-align: center; }
.profile-card div { display: flex; flex-direction: column; }
.profile-card b { font-size: 20px; font-variant-numeric: tabular-nums; }
.profile-card span { font-size: 11px; color: var(--muted); }
.input { flex: 1; min-width: 0; border: 1px solid var(--tile-border); border-radius: 10px; padding: 12px; font-size: 18px; font-weight: 700; text-transform: uppercase; letter-spacing: 2px; background: var(--bg); color: var(--fg); }
.rooms { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rooms li { display: flex; align-items: center; gap: 8px; }
.rooms .who { flex: 1; }
.rooms button { border: 0; border-radius: 8px; padding: 8px 12px; background: var(--accent); color: var(--accent-fg); font-weight: 700; }
.room-head { display: flex; justify-content: space-between; align-items: center; }
.room-head b { font-size: 20px; letter-spacing: 3px; }
.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--bg); border-radius: 10px; }
.players .nm { flex: 1; font-weight: 600; }
.players .rt { color: var(--muted); font-variant-numeric: tabular-nums; }
.players .st { font-size: 13px; }
.players .st.ok { color: var(--correct); font-weight: 700; }
.duel-head { display: flex; justify-content: space-between; align-items: baseline; font-weight: 700; padding: 0 2px 4px; }
.timer { font-variant-numeric: tabular-nums; color: var(--muted); }
.row.small { justify-content: space-between; padding: 6px 2px 0; }
.link.danger { color: #d9534f; }
#p-draw.active { font-weight: 700; }
.countdown { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.45); z-index: 10; }
.countdown b { font-size: 96px; color: #fff; text-shadow: 0 2px 12px rgba(0,0,0,.5); animation: pop .3s; }
.hint.warn { color: var(--present); font-weight: 600; }
