*,
*::before,
*::after { box-sizing: border-box; }

:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #1a1d29;
  --text-dim: #6b7180;
  --line: #d7dce6;
  --line-strong: #0f1220;
  --accent: #4f7cff;
  --danger: #e5484d;
  --ok: #30a46c;
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 6px 24px rgba(20, 26, 45, 0.10);
  --gap: 14px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

:root[data-theme="dark"] {
  --bg: #0f1220;
  --surface: #1a1e2e;
  --surface-2: #232839;
  --text: #eef1f7;
  --text-dim: #9aa2b5;
  --line: #2c3346;
  --line-strong: #eef1f7;
  --accent: #6d92ff;
  --danger: #ff6369;
  --ok: #3dd68c;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1220;
    --surface: #1a1e2e;
    --surface-2: #232839;
    --text: #eef1f7;
    --text-dim: #9aa2b5;
    --line: #2c3346;
    --line-strong: #eef1f7;
    --accent: #6d92ff;
    --danger: #ff6369;
    --ok: #3dd68c;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  }
}

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.app {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--sat) + 8px) calc(var(--sar) + 14px) calc(var(--sab) + 8px) calc(var(--sal) + 14px);
  max-width: 560px;
  margin: 0 auto;
}

.scene { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* Сцена игры: поле во всю ширину экрана (гасим боковой padding .app),
   боковые отступы — только safe-area (вырез/жест), иначе поле прижато к краю.
   Внутренние блоки (HUD, панель) сами задают себе небольшой inline-padding. */
.game-scene {
  margin-left: calc(-1 * (var(--sal) + 14px));
  margin-right: calc(-1 * (var(--sar) + 14px));
  padding-left: var(--sal);
  padding-right: var(--sar);
  padding-bottom: max(var(--sab), 12px);
}
/* HUD и панель управления — с боковым отступом (поле остаётся на всю ширину) */
.game-scene .sudoku-hud,
.game-scene .pad { padding-left: 10px; padding-right: 10px; }

/* Заголовки / шапки */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  margin-bottom: 10px;
}
.topbar h1 { font-size: 26px; font-weight: 800; margin: 0; letter-spacing: -0.5px; }
.topbar .spacer { flex: 1; }

.icon-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  font-size: 17px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}
.icon-btn:active { transform: scale(0.96); }
.icon-btn[disabled] { opacity: 0.4; }

/* ── SVG-иконки ── */
.ico { display: inline-flex; width: 1em; height: 1em; line-height: 0; }
.ico svg { width: 100%; height: 100%; display: block; }

/* ── Хаб: hero-шапка ── */
.hub { gap: 0; }
.hub-head {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 6px 0 18px;
}
.hub-brand h1 {
  font-size: 32px; font-weight: 800; margin: 0; letter-spacing: -0.8px;
  line-height: 1.05;
}
.hub-tag { margin: 3px 0 0; font-size: 14px; color: var(--text-dim); }
.hub-head .spacer { flex: 1; }
.icon-btn.round {
  min-width: 42px; min-height: 42px; padding: 0; border-radius: 50%;
  border: 1px solid var(--line); font-size: 20px;
}
.icon-btn.round .ico { font-size: 20px; }

/* ── Баннер установки ── */
.install-banner {
  appearance: none; text-align: left; cursor: pointer;
  display: flex; align-items: center; gap: 12px;
  padding: 14px 14px; margin-bottom: 16px;
  border-radius: var(--radius); border: 1px solid transparent;
  background: linear-gradient(135deg,
    color-mix(in srgb, var(--accent) 16%, transparent),
    color-mix(in srgb, var(--accent) 6%, transparent));
  color: var(--text);
}
.install-banner:active { transform: scale(0.99); }
.install-banner .ib-ico {
  width: 38px; height: 38px; border-radius: 11px; flex: none;
  display: grid; place-items: center; font-size: 20px;
  background: var(--accent); color: #fff;
}
.install-banner .ib-text { display: flex; flex-direction: column; line-height: 1.25; flex: 1; }
.install-banner .ib-text b { font-size: 14.5px; font-weight: 700; }
.install-banner .ib-text small { font-size: 12.5px; color: var(--text-dim); }
.install-banner .ib-arrow { font-size: 22px; color: var(--accent); font-weight: 700; }

/* ── Сетка игр (bento) ── */
.hub-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--gap);
  padding-bottom: 16px;
}
.card {
  position: relative;
  border-radius: var(--radius);
  padding: 16px;
  min-height: 158px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  cursor: pointer;
  overflow: hidden;
  border: 1px solid var(--line);
  background: linear-gradient(160deg,
    color-mix(in srgb, var(--card-accent) 9%, transparent),
    transparent 70%);
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.card.ready { border-color: color-mix(in srgb, var(--card-accent) 40%, var(--line)); }
.card:active { transform: scale(0.97); }
/* «Скоро» — обесцвеченные, приглушённые: не конкурируют с играбельными */
.card.soon {
  opacity: 0.7;
  background: var(--surface);
}
.card.soon .card-icon {
  color: var(--text-dim);
  background: var(--surface-2);
  filter: grayscale(1);
}
.card.soon .card-title { color: var(--text-dim); }

.card-head { display: flex; align-items: flex-start; justify-content: space-between; }
.card-icon {
  width: 48px; height: 48px; border-radius: 14px;
  display: grid; place-items: center; font-size: 26px;
  color: var(--card-accent, var(--accent));
  background: color-mix(in srgb, var(--card-accent, var(--accent)) 16%, transparent);
}
.card-icon .ico { font-size: 26px; }
.card-icon.big { color: var(--card-accent, var(--accent)); }
.card-icon.big .ico { font-size: 42px; }

.card-body { flex: 1; }
.card-title { font-size: 18px; font-weight: 800; letter-spacing: -0.3px; }
.card-tagline { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; }

.card-sub {
  font-size: 12.5px; color: var(--text-dim);
  display: flex; align-items: center; gap: 6px;
}
.card-sub.hot { color: var(--card-accent, var(--accent)); font-weight: 700; }
.card-sub .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--card-accent, var(--accent));
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--card-accent) 60%, transparent);
  animation: pulse-dot 1.8s infinite;
}
@keyframes pulse-dot {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--card-accent) 55%, transparent); }
  70% { box-shadow: 0 0 0 6px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.badge {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.5px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim);
}

/* Экран запуска / вертикальные кнопки */
.stack { display: flex; flex-direction: column; gap: 12px; }
.big-btn {
  appearance: none; border: 1px solid var(--line);
  background: transparent; color: var(--text);
  border-radius: var(--radius); padding: 16px 18px;
  font-size: 17px; font-weight: 700; text-align: left;
  cursor: pointer;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  min-height: 56px;
}
.big-btn:active { transform: scale(0.98); }
.big-btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.big-btn small { display: block; font-size: 13px; font-weight: 500; opacity: 0.8; }

/* Судоку — HUD (один ряд, без переноса).
   Уровень — бейджем на кнопке «назад» (он статичен всю партию), поэтому в ряду
   статистики остаются только «живые» счётчики — им хватает места, ничего не режется. */
.sudoku-hud {
  display: flex; align-items: center; gap: 4px;
  margin-bottom: 10px; flex-wrap: nowrap;
}
.sudoku-hud .icon-btn.hud-btn {
  width: 42px; min-width: 42px; height: 42px; min-height: 42px; padding: 0;
  flex: none; border: 1px solid var(--line); border-radius: 13px;
}
.sudoku-hud .icon-btn.hud-btn .ico { font-size: 19px; }
/* кнопка «назад» с бейджем уровня */
.hud-back { position: relative; }
.hud-back .lvl-badge {
  position: absolute; top: -5px; right: -5px;
  min-width: 18px; height: 18px; padding: 0 4px; border-radius: 9px;
  background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 800; line-height: 18px; text-align: center;
  font-variant-numeric: tabular-nums;
}
/* группа статистики — занимает всё свободное место между кнопками */
.hud-stats {
  flex: 1 1 auto; min-width: 0;
  display: flex; align-items: stretch;
}
.hud-stat {
  background: transparent;
  padding: 3px 6px;
  display: flex; flex-direction: column; align-items: center; line-height: 1.1;
  min-width: 0; flex: 1 1 0;
}
.hud-stat + .hud-stat { border-left: 1px solid var(--line); }
.hud-stat .k {
  font-size: 9.5px; color: var(--text-dim); text-transform: uppercase;
  letter-spacing: 0.4px; white-space: nowrap;
}
.hud-stat .v {
  font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums;
  white-space: nowrap; letter-spacing: -0.2px;
}
.hud-stat.mistakes .v { color: var(--danger); }
.hud-stat.mistakes .v[data-zero="1"] { color: var(--text); }

/* Судоку — канвас (во всю доступную ширину) */
.board-wrap {
  display: flex; justify-content: center; align-items: center;
  flex: 1; min-height: 0;
}
/* Карточный стол — прижат к верху (карты веером идут вниз) */
.cards-wrap { align-items: flex-start; }

/* Lines 98 — превью следующих шаров в HUD */
.lines-next {
  display: flex; align-items: center; gap: 5px;
  padding: 0 4px; flex: none;
}
.lines-dot {
  width: 15px; height: 15px; border-radius: 50%;
  box-shadow: inset 0 2px 2px rgba(255,255,255,0.45);
}
canvas.board { display: block; touch-action: none; -webkit-tap-highlight-color: transparent; border-radius: 8px; }

/* Панель цифр + инструменты */
.pad { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.tools-row, .num-row { display: grid; gap: 5px; }
.num-row { grid-template-columns: repeat(9, minmax(0, 1fr)); }
.tools-row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.pad-btn {
  appearance: none; border: 1px solid var(--line);
  background: transparent; color: var(--text);
  border-radius: 11px; min-height: 48px; min-width: 0;
  font-size: 21px; font-weight: 700; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  position: relative; user-select: none;
  font-variant-numeric: tabular-nums; overflow: hidden;
}
.pad-btn:active { transform: scale(0.94); }
.pad-btn.active { background: var(--accent); color: #fff; border-color: transparent; }
.pad-btn small {
  font-size: 10.5px; font-weight: 600; margin-top: 3px;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pad-btn.tool { font-weight: 600; padding: 4px 2px; }
.pad-btn.tool .tool-ico { font-size: 21px; }
.pad-btn.tool .tool-ico svg { width: 21px; height: 21px; }
.pad-btn .count {
  position: absolute; top: 4px; right: 6px; font-size: 10px;
  color: var(--text-dim); font-weight: 700;
}

/* Оверлеи */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--sat) + 20px) 20px calc(var(--sab) + 20px);
  background: rgba(10, 12, 22, 0.55);
  backdrop-filter: blur(6px);
}
.overlay .sheet {
  background: var(--surface); color: var(--text);
  border-radius: 22px; padding: 24px 20px; width: 100%; max-width: 360px;
  box-shadow: var(--shadow); text-align: center;
  animation: pop 0.18s ease;
}
@keyframes pop { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.overlay h2 { margin: 0 0 6px; font-size: 24px; }
.overlay p { margin: 4px 0; color: var(--text-dim); }
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.result-grid .cell { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.result-grid .cell .k { font-size: 12px; color: var(--text-dim); }
.result-grid .cell .v { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.record-flag { color: var(--ok); font-weight: 800; margin-top: 6px; }

/* Настройки */
.settings-list { display: flex; flex-direction: column; gap: 0; }
.setting {
  background: transparent; border-radius: 0;
  padding: 14px 4px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.setting:last-child { border-bottom: none; }
.setting .label { font-weight: 600; }
.setting .sub { font-size: 12px; color: var(--text-dim); }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.seg button {
  appearance: none; border: none; background: transparent; color: var(--text-dim);
  padding: 7px 12px; border-radius: 999px; font-size: 14px; font-weight: 600; cursor: pointer;
  min-height: 34px;
}
.seg button.on { background: var(--accent); color: #fff; }
.switch {
  width: 52px; height: 32px; border-radius: 999px; background: var(--line);
  position: relative; cursor: pointer; border: none; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px;
  border-radius: 50%; background: #fff; transition: transform 0.16s ease;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); }

/* Оверлей настроек игры — прокручиваемый лист, инфо слева */
.settings-sheet { text-align: left; max-height: 80vh; display: flex; flex-direction: column; }
.settings-sheet h2 { text-align: center; }
.settings-sheet .settings-list { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.settings-sheet .setting { gap: 10px; align-items: center; }
.setting-info { flex: 1 1 auto; min-width: 0; }
.setting-info .sub { white-space: normal; }
.settings-sheet .seg { flex: 0 0 auto; }
/* компактнее сегмент внутри настроек, чтобы влезал с подписью */
.settings-sheet .seg button { padding: 7px 9px; font-size: 13px; }

.muted { color: var(--text-dim); }
.center { text-align: center; }
.grow { flex: 1; }

/* Кнопка установки PWA (в шапке хаба) */
.install-btn {
  appearance: none; border: 1px solid var(--accent);
  background: transparent; color: var(--accent);
  min-height: 40px; padding: 0 14px; border-radius: 999px;
  font-size: 14px; font-weight: 700; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.install-btn .ico { font-size: 17px; }
.install-btn:active { transform: scale(0.96); }

/* Тост обновления */
.update-toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sab) + 16px); z-index: 60;
  display: flex; align-items: center; gap: 10px;
  background: var(--text); color: var(--bg);
  padding: 10px 12px 10px 16px; border-radius: 999px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.3); max-width: calc(100vw - 32px);
  animation: pop 0.2s ease;
}
.update-toast span { font-size: 14px; font-weight: 600; }
.update-btn {
  appearance: none; border: none; background: var(--accent); color: #fff;
  padding: 7px 14px; border-radius: 999px; font-size: 14px; font-weight: 700; cursor: pointer;
}
.update-x {
  appearance: none; border: none; background: transparent; color: var(--bg);
  font-size: 14px; cursor: pointer; opacity: 0.7; padding: 4px 6px;
}

/* Экран-заглушка несовместимости (вставляется из index.html) */
.compat-screen {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 32px 24px; text-align: center; gap: 14px;
  background: #0f1220; color: #eef1f7;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.compat-screen h2 { font-size: 22px; margin: 0; }
.compat-screen p { font-size: 15px; line-height: 1.6; color: #9aa2b5; margin: 0; max-width: 420px; }
.compat-screen ol { text-align: left; line-height: 1.8; max-width: 420px; padding-left: 20px; }
.compat-screen b { color: #eef1f7; }

/* Инструкция установки PWA (пошаговая) */
.install-guide { text-align: left; }
.install-guide h2 { text-align: center; margin: 0 0 6px; font-size: 22px; }
.ig-lead { text-align: center; color: var(--text-dim); font-size: 14px; margin: 0 0 18px; line-height: 1.5; }
.ig-steps { display: flex; flex-direction: column; gap: 12px; }
.ig-step { display: flex; align-items: center; gap: 12px; }
.ig-num {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 13px; font-weight: 800;
  display: grid; place-items: center;
}
.ig-ico {
  flex: none; width: 40px; height: 40px; border-radius: 11px;
  background: var(--surface-2); color: var(--accent);
  display: grid; place-items: center;
}
.ig-ico .ico, .ig-ico svg { width: 22px; height: 22px; }
.ig-txt { font-size: 14px; line-height: 1.35; }
.ig-txt b { font-weight: 700; }
