:root {
  --bg: #ffffff;
  --fg: #1a1a1b;
  --muted: #787c7e;
  --border: #d3d6da;
  --tile-border: #d3d6da;
  --tile-border-filled: #878a8c;
  --key-bg: #e4e6ea;
  --key-fg: #1a1a1b;
  --correct: #6aab5b;
  --present: #cbb43f;
  --absent: #7c8085;
  --header-border: #e9e9e9;
}

body.dark {
  --bg: #121213;
  --fg: #ffffff;
  --muted: #818384;
  --border: #3a3a3c;
  --tile-border: #3a3a3c;
  --tile-border-filled: #565758;
  --key-bg: #818384;
  --key-fg: #ffffff;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --header-border: #2a2a2b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ── Шапка ─────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--header-border);
  flex: 0 0 auto;
}
.title {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 1px;
  margin: 0;
  text-align: center;
}
.side { display: flex; gap: 6px; width: 96px; }
.side.right { justify-content: flex-end; }
.icon {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: var(--muted);
  width: 34px;
  height: 34px;
}
.icon svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon svg .dot { fill: currentColor; stroke-width: 3; }

/* ── Поле ──────────────────────────────────────── */
.board-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 8px;
}
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  height: min(100%, 420px);
  aspect-ratio: 5 / 6;
}
.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--tile-border);
  font-size: clamp(22px, 8vw, 34px);
  font-weight: 700;
  text-transform: uppercase;
  color: var(--fg);
  aspect-ratio: 1 / 1;
  backface-visibility: hidden;
}
.tile.filled {
  border-color: var(--tile-border-filled);
  animation: pop .12s ease-out;
}
@keyframes pop { 0% { transform: scale(.86); } 100% { transform: scale(1); } }

.tile.correct { background: var(--correct); border-color: var(--correct); color: #fff; }
.tile.present { background: var(--present); border-color: var(--present); color: #fff; }
.tile.absent  { background: var(--absent);  border-color: var(--absent);  color: #fff; }

/* Плавный переворот: цвет меняется в фазе «ребром» (45–55%), поэтому без снапа. */
.tile.flip {
  animation: flip .58s ease forwards;
  will-change: transform;
}
@keyframes flip {
  0%   { transform: rotateX(0deg); }
  45%  { transform: rotateX(-90deg); }
  55%  { transform: rotateX(-90deg); }
  100% { transform: rotateX(0deg); }
}
.tile.win { animation: bounce .55s ease; }
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  35%      { transform: translateY(-16px); }
  65%      { transform: translateY(-5px); }
}
.row.shake { animation: shake .5s; }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}

/* ── Клавиатура ────────────────────────────────── */
.keyboard {
  flex: 0 0 auto;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kb-row {
  display: flex;
  gap: 4px;
  justify-content: center;
}
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  border: none;
  border-radius: 5px;
  background: var(--key-bg);
  color: var(--key-fg);
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.key:active { filter: brightness(.92); }
.key.wide { flex: 1.5 1 0; font-size: 12px; }
.kb-spacer { flex: 0.5 1 0; }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent  { background: var(--absent);  color: #fff; }
.key svg { width: 24px; height: 24px; flex: 0 0 auto; display: block; }
.key .kb-body { fill: currentColor; }
.key .kb-x { fill: none; stroke: var(--key-bg); stroke-width: 2.4; stroke-linecap: round; }
.key .kb-arrow { fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.key.enter-key { background: #4a90e2; }
.key.enter-key:active { filter: brightness(.9); }

/* ── Тост ──────────────────────────────────────── */
.toast {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 50;
  white-space: nowrap;
}
.toast.show { opacity: 1; }

/* ── Модалки ───────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--bg);
  color: var(--fg);
  border-radius: 14px;
  padding: 22px 20px;
  width: 100%;
  max-width: 360px;
  position: relative;
  max-height: 84vh;
  overflow: auto;
}
.modal-card h2 { margin: 0 0 12px; font-size: 20px; }
.modal-card h3 { margin: 18px 0 8px; font-size: 15px; }
.modal-close {
  position: absolute;
  top: 10px; right: 12px;
  background: none; border: none;
  font-size: 20px; cursor: pointer; color: var(--muted);
}
.legend { list-style: none; padding: 0; margin: 12px 0; }
.legend li { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.chip {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; border-radius: 3px;
}
.chip.correct { background: var(--correct); }
.chip.present { background: var(--present); }
.chip.absent  { background: var(--absent); }

.stats-row { display: flex; justify-content: space-between; text-align: center; margin: 8px 0; }
.stat { flex: 1; }
.stat b { display: block; font-size: 26px; }
.stat span { font-size: 11px; color: var(--muted); }
.dist { display: flex; flex-direction: column; gap: 5px; }
.dist .bar-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.dist .bar-row > b { width: 12px; }
.dist .bar {
  background: var(--absent); color: #fff;
  padding: 2px 8px; border-radius: 3px; min-width: 22px;
  text-align: right; font-weight: 600;
}
.dist .bar.hi { background: var(--correct); }

.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--border); }
.switch-row input { width: 20px; height: 20px; }
.muted { color: var(--muted); font-size: 13px; }
.danger-btn { background: #d9534f; color: #fff; border: none; border-radius: 8px; padding: 8px 16px; font-weight: 600; cursor: pointer; flex: 0 0 auto; }
.danger-btn:active { filter: brightness(.9); }
.admin-row small { font-size: 11px; }

/* ── Модалка результата ────────────────────────── */
.result-card { text-align: center; }
.result-card h2 { text-align: center; }
.result-pct { font-size: 15px; color: var(--fg); margin: 4px 0 14px; }
.result-grid { display: flex; flex-direction: column; gap: 4px; align-items: center; margin: 6px 0 16px; }
.result-row { display: flex; gap: 4px; }
.result-cell { width: 26px; height: 26px; border-radius: 4px; background: var(--absent); }
.result-cell.correct { background: var(--correct); }
.result-cell.present { background: var(--present); }
.result-cell.absent  { background: var(--absent); }
.result-next { margin: 4px 0 14px; }
.result-next .muted { display: block; font-size: 13px; margin-bottom: 2px; }
.countdown { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.remind-btn, .share-btn {
  display: block; width: 100%; border: none; border-radius: 10px;
  padding: 13px; font-size: 15px; font-weight: 600; cursor: pointer; margin-top: 10px;
}
.remind-btn { background: var(--key-bg); color: var(--fg); }
.remind-btn.on { background: var(--correct); color: #fff; }
.share-btn { background: var(--correct); color: #fff; }
.remind-btn:active, .share-btn:active { filter: brightness(.93); }

/* ── Топ игроков ───────────────────────────────── */
.leaderboard { margin: 2px 0 14px; text-align: left; }
.leaderboard h3 { text-align: center; font-size: 14px; margin: 0 0 8px; }
.lead-row { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 8px; font-size: 14px; }
.lead-row + .lead-row { margin-top: 2px; }
.lead-row.me { background: rgba(106, 171, 91, .18); font-weight: 600; }
.lead-rank { width: 22px; text-align: center; flex: 0 0 auto; font-weight: 700; }
.lead-name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-stat { flex: 0 0 auto; font-weight: 700; white-space: nowrap; min-width: 44px; text-align: right; }
.lead-legend { text-align: center; font-size: 11px; color: var(--muted); margin: 8px 0 0; }

/* ── Мост для анонимного inline-запуска ────────── */
.gate {
  position: fixed; inset: 0; z-index: 200;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 28px;
}
.gate[hidden] { display: none; }
.gate-box { text-align: center; max-width: 300px; }
.gate-logo { font-size: 54px; margin-bottom: 14px; }
.gate-box p { font-size: 15px; color: var(--fg); margin: 0 0 20px; line-height: 1.5; }
