:root {
  --bg: #faf8ef;
  --text: #776e65;
  --text-strong: #5b5148;
  --board: #bbada0;
  --panel: #bbada0;
  --panel-text: #f9f6f2;
  --button: #8f7a66;
  --button-hover: #7c6957;
  --button-fg: #f9f6f2;
  --soft: #eee4da;
  --soft-hover: #e4d8ca;
  --logo: #edc22e;
  --logo-fg: #ffffff;
  --screen-bg: rgba(250, 248, 239, 0.9);
  --input-bg: #ffffff;
  --input-border: #d8ccbf;
  --row-alt: rgba(238, 228, 218, 0.6);
  --me: #f7e7a8;
  --ok: #4a8c3a;
  --err: #c0392b;
  --t2: #eee4da; --t2-fg: #776e65;
  --t4: #ede0c8; --t4-fg: #776e65;
  --t16: #f59563; --t16-fg: #ffffff;
  --joker: linear-gradient(135deg, #c86dd7, #3023ae);
  --joker-fg: #fff6c9;
  --board-w: min(100%, 560px, calc((100dvh - 200px) * 0.625));
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body { touch-action: manipulation; }

button { font: inherit; color: inherit; }

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 16px max(12px, env(safe-area-inset-bottom));
  min-height: 100dvh;
  user-select: none;
  -webkit-user-select: none;
}

/* ---- Шапка ---- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: max(var(--board-w), min(100%, 340px));
}

.logo {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  color: var(--text-strong);
  font-size: clamp(20px, 6vw, 28px);
  font-weight: 800;
  letter-spacing: -0.5px;
  white-space: nowrap;
}

.logo-tile {
  padding: 2px 7px 3px;
  border-radius: 6px;
  background: var(--logo);
  color: var(--logo-fg);
  font-size: 0.82em;
}

.stats { display: flex; align-items: center; gap: 6px; }

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 62px;
  height: 56px;
  padding: 4px 8px;
  border: 0;
  border-radius: 6px;
  background: var(--panel);
  color: var(--panel-text);
  font-size: 18px;
  font-weight: 700;
}

.stat .label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  opacity: 0.85;
}

.stat.next {
  min-width: 82px;
  height: auto;
  padding: 5px 8px 4px;
  gap: 3px;
  cursor: pointer;
}

.stat .hint { font-size: 10px; font-weight: 600; opacity: 0.8; }
.hint-touch { display: none; }

#next { width: 44px; height: 44px; }

.stat.next.bump { animation: bump 0.25s ease-out; }

@keyframes bump { 40% { transform: scale(1.12); } }

/* ---- Поле и экраны ---- */
.board-wrap {
  position: relative;
  width: var(--board-w);
  container-type: inline-size;
}

#board {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 8px;
  touch-action: none;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 4cqw, 18px);
  border-radius: 8px;
  background: var(--screen-bg);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade-in 0.18s ease-out;
}

.screen[hidden], .screen section[hidden] { display: none; }

@keyframes fade-in { from { opacity: 0; } }

.screen section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(7px, 3cqw, 12px);
  width: 100%;
  max-height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin;
  text-align: center;
}

.screen h2 {
  margin: 0;
  color: var(--text-strong);
  font-size: clamp(22px, 8cqw, 30px);
  font-weight: 800;
}

.tagline {
  max-width: 280px;
  margin: 0 0 clamp(2px, 2cqw, 8px);
  font-size: clamp(13px, 4.3cqw, 16px);
  line-height: 1.4;
}

.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.btn {
  min-width: min(120px, 38cqw);
  padding: clamp(7px, 2.6cqw, 10px) clamp(10px, 4.5cqw, 18px);
  border: 0;
  border-radius: 6px;
  background: var(--soft);
  color: var(--text-strong);
  font-size: clamp(13px, 4.2cqw, 16px);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s;
}

.btn:hover { background: var(--soft-hover); }
.btn:active { transform: scale(0.97); }

.btn.primary {
  min-width: min(180px, 70cqw);
  padding: clamp(9px, 3.2cqw, 13px) clamp(16px, 6.5cqw, 26px);
  background: var(--button);
  color: var(--button-fg);
  font-size: clamp(16px, 5.4cqw, 20px);
}

.btn.primary:hover { background: var(--button-hover); }

.btn.link {
  min-width: 0;
  padding: 4px 8px;
  background: none;
  text-decoration: underline;
  font-weight: 600;
}

.btn:disabled { opacity: 0.5; cursor: default; }
.btn[hidden], .install-hint[hidden] { display: none; }

.install-hint {
  max-width: 260px;
  margin: 0;
  font-size: clamp(11px, 3.4cqw, 13px);
  line-height: 1.35;
  opacity: 0.85;
}

/* Как играть */
.rules {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
  text-align: left;
  font-size: clamp(11.5px, 3.6cqw, 14px);
  line-height: 1.35;
}

.rules li { display: flex; align-items: center; gap: 8px; }

.mini {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 5px;
  font-size: 13px;
  font-weight: 800;
}

.mini + .mini { margin-left: -4px; }
.mini.t2 { background: var(--t2); color: var(--t2-fg); }
.mini.t4 { background: var(--t4); color: var(--t4-fg); }
.mini.t16 { background: var(--t16); color: var(--t16-fg); }
.mini.key { background: var(--board); color: var(--panel-text); font-size: 16px; }
.mini.joker { background: var(--joker); color: var(--joker-fg); font-size: 16px; }

.controls {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 8px;
  margin: 0;
  font-size: clamp(10.5px, 3.1cqw, 12px);
  text-align: left;
}

.controls dt { font-weight: 700; color: var(--text-strong); }
.controls dd { margin: 0; }

/* Стартовый экран: «Играть» и под ним переключатель режима */
.btn.play-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  max-width: 320px;
  padding: clamp(11px, 4cqw, 16px) 16px;
  font-size: clamp(18px, 6.4cqw, 24px);
}

.btn.play-btn svg { width: 1em; height: 1em; fill: currentColor; }

.mode-tabs { width: 100%; max-width: 320px; }

.mode-tabs .tab {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 4px;
  font-size: clamp(13px, 4.2cqw, 15px);
}

.mode-tabs small { font-size: 0.78em; font-weight: 600; opacity: 0.8; }

/* Испытание дня */
.btn.daily-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  width: 100%;
  max-width: 320px;
  padding: clamp(7px, 2.6cqw, 10px) 8px;
  border: 2px dashed var(--logo);
  background: transparent;
  font-size: clamp(14px, 4.6cqw, 17px);
}

.btn.daily-btn:hover { background: var(--soft); }
.btn.daily-btn small { font-size: 0.7em; font-weight: 600; opacity: 0.85; }
.btn.daily-btn small[hidden] { display: none; }
.btn.daily-btn.done { border-style: solid; border-color: var(--soft-hover); opacity: 0.85; }

/* В испытании одна попытка — «Заново» нет. */
body.mode-daily .pause-bar [data-action="restart"] { display: none; }

.btn.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
}

.btn.share-btn svg { width: 18px; height: 18px; }

.mode-label {
  margin: -6px 0 0;
  font-size: clamp(11px, 3.4cqw, 13px);
  opacity: 0.8;
}

.tabs.small .tab { padding: 4px 12px; font-size: 13px; }

/* Оформление */
.themes {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: clamp(5px, 2cqw, 8px);
  width: 100%;
  max-width: 340px;
}

.theme-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  padding: 5px;
  border: 2px solid transparent;
  border-radius: 8px;
  background: var(--soft);
  color: var(--text-strong);
  font-size: clamp(11px, 3.4cqw, 13px);
  font-weight: 700;
  cursor: pointer;
}

.theme-card:hover { background: var(--soft-hover); }
.theme-card.active { border-color: var(--button); }

.theme-preview {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 3px;
  padding: 4px;
  border-radius: 5px;
}

.theme-preview i {
  aspect-ratio: 1;
  border-radius: 3px;
}

/* Итоги */
.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: 100%;
  max-width: 300px;
}

.summary div {
  display: flex;
  flex-direction: column;
  padding: 6px;
  border-radius: 6px;
  background: var(--soft);
}

.summary .label { font-size: 11px; font-weight: 600; text-transform: uppercase; }
.summary b { color: var(--text-strong); font-size: clamp(16px, 5.4cqw, 20px); }

.name-form { display: flex; gap: 6px; width: 100%; max-width: 300px; }
.name-form[hidden] { display: none; }

.name-form input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 2px solid var(--input-border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text-strong);
  font: inherit;
  font-size: 16px;
  user-select: text;
  -webkit-user-select: text;
}

.name-form input:focus { outline: none; border-color: var(--button); }
.name-form .btn { min-width: 0; padding: 10px 12px; }

.status { min-height: 18px; margin: -4px 0 0; font-size: 14px; }
.status.ok { color: var(--ok); font-weight: 600; }
.status.err { color: var(--err); }

/* Таблица рекордов */
.tabs { display: flex; gap: 4px; padding: 3px; border-radius: 8px; background: var(--soft); }

.tab {
  padding: 6px 14px;
  border: 0;
  border-radius: 6px;
  background: none;
  font-weight: 700;
  cursor: pointer;
}

.tab.active { background: var(--button); color: var(--button-fg); }

.scores {
  flex: 1 1 auto;
  width: 100%;
  max-width: 320px;
  min-height: 60px;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
  counter-reset: rank;
}

.scores li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 15px;
  counter-increment: rank;
}

.scores li:nth-child(odd) { background: var(--row-alt); }
.scores li.me { background: var(--me); font-weight: 700; }

.scores li::before {
  content: counter(rank);
  width: 22px;
  color: var(--text);
  font-weight: 800;
  text-align: right;
}

.scores li:nth-child(1)::before { content: "🥇"; }
.scores li:nth-child(2)::before { content: "🥈"; }
.scores li:nth-child(3)::before { content: "🥉"; }
.scores li.muted { justify-content: center; font-style: italic; }
.scores li.muted::before { content: none; }

.scores .name {
  flex: 1;
  overflow: hidden;
  color: var(--text-strong);
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scores .tile {
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 800;
}

.scores .pts { min-width: 56px; color: var(--text-strong); font-weight: 800; text-align: right; }

/* ---- Панель кнопок ---- */
.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  width: var(--board-w);
}

.tool {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 4px;
  border: 0;
  border-radius: 6px;
  background: var(--soft);
  color: var(--text-strong);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.tool:hover { background: var(--soft-hover); }
.tool svg { width: 22px; height: 22px; fill: currentColor; }

/* ---- Сенсорные кнопки и пауза в шапке (видны только на сенсорных экранах) ---- */
.pad, .icon-btn { display: none; }

.icon-btn {
  flex: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--soft);
  color: var(--text-strong);
  cursor: pointer;
}

.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

.pad {
  grid-template-columns: 1fr 1.2fr 1fr; /* ← · бросить · → */
  gap: 8px;
  width: max(var(--board-w), min(100%, 340px));
  touch-action: none;
}

.pad-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  height: 64px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: var(--soft);
  color: var(--text-strong);
  font-size: 11px;
  font-weight: 700;
  touch-action: none;
  -webkit-touch-callout: none;
  transition: background 0.06s, transform 0.06s;
}

.pad-btn svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.pad-btn[data-pad="drop"] { background: var(--button); color: var(--button-fg); }
.pad-btn.pressed { background: var(--soft-hover); transform: scale(0.96); }
.pad-btn[data-pad="drop"].pressed { background: var(--button-hover); }

@media (pointer: coarse) {
  .app { --board-w: min(100%, 560px, calc((100dvh - 216px) * 0.625)); }
  .toolbar { display: none; }
  .pad { display: grid; }
  .icon-btn { display: flex; }
  .hint-key { display: none; }
  .hint-touch { display: inline; }
}

@media (pointer: coarse) and (max-width: 560px) { /* в игре шапке место нужнее логотипа */
  .top .logo { display: none; }
}

/* ---- Стартовый экран (партии ещё не было): в шапке логотип и рекорд, под полем — панель кнопок без паузы ---- */
body.menu .top .logo { display: flex; }
body.menu .score-stat, body.menu .stat.next, body.menu .icon-btn, body.menu #btn-pause { display: none; }
.menu-top { display: none; }

@media (pointer: coarse) {
  body.menu .toolbar { display: flex; }
  body.menu .pad { display: none; }
}

/* ---- Пауза: панель на месте кнопок управления ---- */
.pause-bar {
  display: grid;
  /* «Продолжить» — 3 доли, каждый видимый значок — 1 (скрытый «Заново» в испытании места не занимает). */
  grid-template-columns: 3fr;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
  width: max(var(--board-w), min(100%, 340px));
}

.pause-bar[hidden] { display: none; }
body.paused .pad, body.paused .toolbar { display: none !important; }

.pause-bar .resume {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
  height: 52px;
  padding: 0 8px;
  font-size: 17px;
}

.pause-bar .resume svg { width: 22px; height: 22px; fill: currentColor; }

.pb {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  height: 52px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--soft);
  color: var(--text-strong);
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
}

.pb:hover { background: var(--soft-hover); }
.pb svg { width: 22px; height: 22px; fill: currentColor; }
.pb span { display: none; }

.icon-btn .i-play, body.paused .icon-btn .i-pause { display: none; }
body.paused .icon-btn .i-play { display: inline; }

.music-btn .cross { display: none; }
.music-btn.off .cross { display: inline; }
.music-btn.off path:first-child, .music-btn.off circle { opacity: 0.45; }

.privacy { max-width: 300px; margin: 0; font-size: clamp(10px, 3cqw, 11.5px); opacity: 0.7; }

#btn-sound .cross, .sound-btn .cross { display: none; }
#btn-sound.off .cross, .sound-btn.off .cross { display: inline; }
#btn-sound.off .wave, .sound-btn.off .wave { display: none; }

@media (max-width: 400px) {
  .top { gap: 8px; }
  .logo { font-size: 20px; }
  .stats { gap: 4px; }
  .stat { min-width: 52px; padding: 4px 6px; }
  .stat.next { min-width: 70px; }
  .stat .label { font-size: 10px; }
  .screen { padding: 12px; }
  .screen section { gap: 10px; }
  .rules { font-size: 13px; }
}

/* Окно не уже 4:5 (ноутбуки, мониторы, в том числе квадратные): поле на всю высоту, колонка справа.
   Телефоны и планшеты в портрете (уже 4:5) — кнопки под полем. */
@media (min-aspect-ratio: 4/5) and (min-width: 640px) {
  /* Страница центрируется снаружи; сетка — ровно по высоте поля: очки вровень с верхом поля, кнопки — с низом.
     (Если дать сетке min-height на всё окно, строка 1fr растянется и кнопки уедут к краю окна.) */
  body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
  }
  .app {
    --side-w: clamp(210px, calc(var(--board-w) * 0.4), 280px);
    --board-w: min(760px, calc((100dvh - 24px) * 0.625), calc(100vw - 300px));
    display: grid;
    grid-template-columns: var(--board-w) var(--side-w);
    grid-template-rows: auto 1fr;
    grid-template-areas: "board top" "board tools";
    gap: 14px 22px;
    min-height: 0;
    padding: 12px 16px;
  }
  .board-wrap { grid-area: board; }
  .top {
    grid-area: top;
    flex-direction: column;
    align-items: stretch;
    align-self: start;
    gap: 14px;
    width: 100%;
  }
  .logo { justify-content: center; font-size: 30px; }
  .stats { flex-direction: column; align-items: stretch; gap: 8px; }
  .stat { width: 100%; height: 64px; font-size: 24px; }
  .stat.next { padding: 8px; }
  #next { width: 56px; height: 56px; }
  .toolbar {
    grid-area: tools;
    align-self: end;
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }
  .tool { padding: 8px 4px; font-size: 12px; }
  .toolbar .tool:last-child:nth-child(odd) { grid-column: span 2; }
  .pad { grid-area: tools; align-self: end; grid-template-columns: 1fr 1fr; width: 100%; }
  .pad-btn[data-pad="drop"] { grid-column: 1 / -1; order: 3; }
  .top .icon-btn { align-self: flex-start; }
  body.menu .toolbar { display: grid; }
  body.menu .toolbar .tool:last-child { grid-column: span 2; }
  body.menu .menu-top { display: flex; flex-direction: column; gap: 6px; }
  body.menu .menu-top.empty { display: none; }
  .menu-top-title { margin: 0; font-size: 12px; font-weight: 700; text-align: center; text-transform: uppercase; opacity: 0.85; }
  .menu-top .scores { flex: none; max-width: none; min-height: 0; overflow: visible; }
  .menu-top .scores li { gap: 6px; padding: 4px 6px; font-size: 13px; }
  .menu-top .scores li::before { width: 18px; }
  .menu-top .scores .pts { min-width: 0; }
  .pause-bar { grid-area: tools; align-self: end; grid-template-columns: 1fr 1fr; grid-auto-flow: row; width: 100%; }
  .pause-bar .resume { grid-column: 1 / -1; }
  .pb span { display: inline; }
  .pb:last-child:nth-child(even) { grid-column: 1 / -1; }
}

/* Пауза на телефоне: той же высоты, что сенсорные кнопки (правило после общих стилей .pause-bar). */
@media (pointer: coarse) {
  .pause-bar .resume, .pb { height: 64px; border-radius: 12px; }
}

/* Узкий экран: «Продолжить» без значка, чтобы надпись влезла. */
@media (max-width: 420px) {
  .pause-bar .resume { font-size: 15px; padding: 0 4px; }
  .pause-bar .resume svg { display: none; }
}

/* Невысокое широкое окно (крупный масштаб браузера, ноутбук): колонка справа не должна быть выше поля.
   «Очки» и «Рекорд» в ряд, «Следующий» строкой, кнопки — три в ряд, только значки. */
@media (min-aspect-ratio: 4/5) and (min-width: 640px) and (max-height: 560px) {
  .top { gap: 8px; }
  .logo { font-size: 22px; }
  .stats { flex-direction: row; flex-wrap: wrap; gap: 6px; }
  .stat { flex: 1; min-width: 0; height: 50px; padding: 4px; font-size: 18px; }
  .stat .label { font-size: 10px; }
  .stat.next { flex-basis: 100%; flex-direction: row; justify-content: center; gap: 10px; height: auto; padding: 6px; }
  #next { width: 40px; height: 40px; }
  .toolbar, .pad { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .pad-btn[data-pad="drop"] { grid-column: auto; order: 0; }
  .toolbar .tool:last-child:nth-child(odd) { grid-column: auto; }
  .tool { padding: 7px 2px; }
  .tool span { display: none; }
  .pause-bar { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .pause-bar .resume { height: 42px; }
  .pb { height: 40px; }
  .pb span { display: none; }
  .pb:last-child:nth-child(even) { grid-column: auto; }
  .pad-btn { height: 46px; }
  body.menu .toolbar .tool:last-child { grid-column: auto; }
  body.menu .menu-top { display: none; }
}

@media (min-aspect-ratio: 4/5) and (min-width: 640px) and (max-height: 420px) {
  .top .logo { display: none; }
}

.privacy a { color: inherit; }
