/* Стили игры 2048. Цвета плиток строятся из общих токенов, поэтому тёмная тема работает сама. */
.top-actions { display: flex; gap: 8px; }

body { overscroll-behavior-y: none; }
.game {
  max-width: 520px;
  margin: 0 auto;
  padding: 4px max(16px, env(safe-area-inset-right)) max(32px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* ---------- Заголовок и счёт ---------- */
.head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.title { margin: 0; font-size: clamp(40px, 12vw, 56px); letter-spacing: -0.03em; line-height: 1; }
.scores { display: flex; gap: 8px; }
.score {
  position: relative;
  min-width: 76px;
  padding: 6px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  text-align: center;
}
.score span { display: block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.score b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.score-add {
  position: absolute; left: 0; right: 0; top: 4px;
  font-style: normal; font-weight: 700; color: var(--accent);
  opacity: 0; pointer-events: none;
}
.score-add.show { animation: float-up .6s ease-out; }
@keyframes float-up {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-28px); }
}

.subhead { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 14px 0 16px; }
.subhead .muted { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- Поле ----------
   Размеры поля задаются в cqi — процентах от ширины .board-wrap.
   Обёртка нужна, потому что элемент не может мерить сам себя: без неё
   браузер берёт ширину окна, и на широком экране сетка разъезжается. */
.board-wrap {
  container-type: inline-size;
  /* На низких экранах (телефон боком) поле уменьшается, чтобы влезть по высоте */
  width: min(100%, max(260px, 100svh - 230px));
  margin: 0 auto;
}
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: 2.5cqi;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2.5cqi;
  border-radius: 12px;
  background: var(--border);
  box-shadow: var(--shadow);
  user-select: none;
  touch-action: none;
}
.board, .demo {
  /* Основа плиток и пустых клеток: в тёмной теме пустые клетки темнее плиток */
  --slot: var(--surface-2);
  --tile-base: var(--surface);
  --slot: light-dark(var(--surface-2), var(--bg));
  --tile-base: light-dark(var(--surface), var(--surface-2));
}
.board .slot { border-radius: 7px; background: var(--slot); }
.tiles { position: absolute; inset: 0; }

.tile {
  --step: 24.375cqi; /* размер плитки + промежуток */
  position: absolute;
  top: 2.5cqi; left: 2.5cqi;
  width: 21.875cqi; height: 21.875cqi;
  transform: translate(calc(var(--c) * var(--step)), calc(var(--r) * var(--step)));
  transition: transform .11s ease-in-out;
  z-index: 2;
}
.tile.gone { z-index: 1; }
.tile-in {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  border-radius: 7px;
  font-weight: 800;
  font-size: 9.5cqi;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  background: var(--tile-base);
  color: var(--text);
}
.tile.d3 .tile-in { font-size: 8cqi; }
.tile.d4 .tile-in { font-size: 6.5cqi; }
.tile.d5 .tile-in { font-size: 5cqi; }

/* Цвета: чем больше число, тем насыщеннее акцентный цвет */
.v2 .tile-in    { background: var(--tile-base); }
.v4 .tile-in    { background: color-mix(in srgb, var(--accent) 12%, var(--tile-base)); }
.v8 .tile-in    { background: color-mix(in srgb, var(--accent) 24%, var(--tile-base)); }
.v16 .tile-in   { background: color-mix(in srgb, var(--accent) 36%, var(--tile-base)); }
.v32 .tile-in   { background: color-mix(in srgb, var(--accent) 48%, var(--tile-base)); }
.v64 .tile-in   { background: color-mix(in srgb, var(--accent) 60%, var(--tile-base)); }
.v128 .tile-in  { background: color-mix(in srgb, var(--accent) 72%, var(--tile-base)); }
.v256 .tile-in  { background: color-mix(in srgb, var(--accent) 84%, var(--tile-base)); color: var(--on-accent); }
.v512 .tile-in  { background: color-mix(in srgb, var(--accent) 92%, var(--tile-base)); color: var(--on-accent); }
.v1024 .tile-in { background: var(--accent); color: var(--on-accent); }
.v2048 .tile-in { background: var(--ok); color: var(--on-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 35%, transparent); }
.vbig .tile-in  { background: var(--text); color: var(--bg); }

.tile.new .tile-in { animation: appear .16s ease-out .08s backwards; }
.tile.pop .tile-in { animation: pop .16s ease-out; }
@keyframes appear { from { transform: scale(0); } to { transform: scale(1); } }
@keyframes pop { 0% { transform: scale(1); } 50% { transform: scale(1.14); } 100% { transform: scale(1); } }

.keys-hint, .touch-hint { color: var(--muted); font-size: 13px; text-align: center; margin: 16px 0 0; }
.keys-hint { display: none; }
@media (hover: hover) and (pointer: fine) {
  .keys-hint { display: block; }
  .touch-hint { display: none; }
}

/* ---------- Кнопки ---------- */
.primary-btn {
  display: inline-flex; justify-content: center; align-items: center;
  height: 42px; padding: 0 18px; flex-shrink: 0;
  border-radius: 12px; border: 0;
  background: var(--accent); color: var(--on-accent);
  font-weight: 600; cursor: pointer;
  touch-action: manipulation;
}
.primary-btn:hover { filter: brightness(1.06); }
.primary-btn.wide { width: 100%; height: 46px; }
.ghost-btn {
  width: 100%; height: 42px; margin-top: 6px;
  border: 0; background: none; color: var(--muted);
  border-radius: 12px; cursor: pointer; font-weight: 500;
}
.ghost-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .4; cursor: default; }

/* ---------- Диалоги ---------- */
.dlg {
  width: min(92vw, 360px);
  padding: 22px 20px 14px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.dlg::backdrop { background: rgba(10, 10, 12, .45); backdrop-filter: blur(2px); }
.dlg h2 { margin: 0 0 6px; font-size: 22px; text-align: center; }
.dlg .muted { color: var(--muted); text-align: center; margin: 0 0 16px; font-size: 14px; }
.dlg-mark {
  width: 72px; height: 72px; margin: 0 auto 12px;
  border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 22px;
}
.dlg-mark.ok { background: var(--ok); color: var(--on-accent); }

/* ---------- Правила: мини-анимация как на экранах онбординга ----------
   Цикл 4 с: палец свайпает влево → две «2» съезжают и сливаются в «4» → появляется новая «2». */
.dlg-rules { width: min(92vw, 400px); max-height: 92svh; }
.demo { --cell: 46px; --gap: 8px; --step: calc(var(--cell) + var(--gap)); margin: 4px 0 14px; }
.demo-row {
  position: relative;
  width: calc(4 * var(--cell) + 5 * var(--gap));
  margin: 0 auto;
  padding: var(--gap);
  display: grid; grid-template-columns: repeat(4, var(--cell)); gap: var(--gap);
  border-radius: 10px;
  background: var(--border);
}
.demo .slot { height: var(--cell); border-radius: 6px; background: var(--slot); }
.demo-tile {
  position: absolute;
  top: var(--gap); left: var(--gap);
  width: var(--cell); height: var(--cell);
  animation: 4s ease-in-out infinite both;
}
.demo-tile .tile-in { font-size: 22px; border-radius: 6px; }
.demo-a { --from: 1; animation-name: demo-slide; z-index: 2; }
.demo-b { --from: 3; animation-name: demo-slide; z-index: 1; }
.demo-m { animation-name: demo-merge; z-index: 3; }
.demo-n { transform: translateX(calc(3 * var(--step))); animation-name: demo-new; }
@keyframes demo-slide {
  0%, 30% { transform: translateX(calc(var(--from) * var(--step))); opacity: 1; }
  42% { transform: translateX(0); opacity: 1; }
  43%, 100% { transform: translateX(0); opacity: 0; }
}
@keyframes demo-merge {
  0%, 42% { opacity: 0; transform: scale(1); }
  43% { opacity: 1; transform: scale(1); }
  50% { transform: scale(1.18); }
  57%, 88% { opacity: 1; transform: scale(1); }
  96%, 100% { opacity: 0; }
}
@keyframes demo-new {
  0%, 55% { opacity: 0; scale: 0; }
  64%, 88% { opacity: 1; scale: 1; }
  96%, 100% { opacity: 0; scale: 1; }
}
.demo-swipe {
  position: relative;
  width: calc(4 * var(--cell) + 5 * var(--gap));
  height: 22px; margin: 8px auto 0;
}
.demo-finger {
  position: absolute; top: 3px; left: 0;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent);
  animation: demo-finger 4s ease-in-out infinite both;
}
@keyframes demo-finger {
  0%, 6% { opacity: 0; transform: translateX(190px) scale(.6); }
  12% { opacity: .9; transform: translateX(190px) scale(1); }
  32% { opacity: .9; transform: translateX(20px) scale(1); }
  38%, 100% { opacity: 0; transform: translateX(20px) scale(.6); }
}
.rules-list { margin: 10px 0 18px; padding-left: 20px; font-size: 14px; line-height: 1.5; }
.rules-list li { margin-bottom: 6px; }
.rules-list li::marker { color: var(--accent); }

@media (max-width: 420px) {
  .brand-text { display: none; }
  .subhead .muted { font-size: 13px; }
  .score { min-width: 64px; padding: 5px 8px; }
  .score b { font-size: 18px; }
  .board .slot, .tile-in { border-radius: 6px; }
}
@media (max-width: 350px) {
  .subhead { flex-direction: column; align-items: stretch; gap: 10px; }
  .subhead .primary-btn { width: 100%; }
  .score { min-width: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile { transition: none; }
  .tile.new .tile-in, .tile.pop .tile-in, .score-add.show { animation: none; }
  /* Без анимации показываем итог: «4» и новая «2» */
  .demo-tile, .demo-finger { animation: none; }
  .demo-a, .demo-b, .demo-finger { opacity: 0; }
}

/* Телефон боком: счёт и кнопки слева, поле справа, всё помещается без прокрутки */
@media (orientation: landscape) and (max-height: 500px) {
  .topbar { padding-top: 8px; padding-bottom: 8px; }
  .game {
    max-width: 900px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "head board" "sub board" "hint board";
    grid-template-rows: auto auto 1fr;
    column-gap: 24px;
    align-items: start;
    padding-bottom: 12px;
  }
  .head { grid-area: head; }
  .subhead { grid-area: sub; flex-direction: column; align-items: stretch; }
  .subhead .primary-btn { width: 100%; }
  .keys-hint, .touch-hint { grid-area: hint; text-align: left; margin-top: 0; }
  .board-wrap { grid-area: board; width: max(220px, 100svh - 96px); }
}
