:root {
  --bg: #1b110a;
  --bg2: #2a1a0f;
  --ink: #f3e6cc;
  --muted: #bfa982;
  --gold: #d6ae5c;
  --gold2: #b48a3a;
  /* поле — светлая бумага в деревянной раме */
  --paper: #f8f0dd;
  --peer: #ecdfc0;
  --same: #e7c26c;
  --sel: #f0b93d;
  --given: #2b1a0d;
  --user: #1d58a8;
  --hinted: #2f7d3a;
  --bad: #c62828;
  --badBg: #f6cfc6;
  --thin: #cdb88f;
  --thick: #5b3a22;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: radial-gradient(ellipse at top, #3a2414 0%, var(--bg) 70%) fixed;
  background-color: var(--bg);
  color: var(--ink);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none; -webkit-user-select: none;
}
#app {
  max-width: 1180px; margin: 0 auto; height: 100%; height: 100dvh;
  display: flex; flex-direction: column;
  padding: max(6px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
.bar { display: flex; align-items: center; gap: 8px; padding: 2px 0 8px; }
.brand { font-family: Georgia, "Times New Roman", serif; font-size: 22px; color: var(--gold); letter-spacing: .5px; }
.info {
  margin: 0 auto; display: flex; gap: 12px; align-items: center; white-space: nowrap;
  background: rgba(0,0,0,.3); border: 1px solid rgba(214,174,92,.35); padding: 4px 12px; border-radius: 20px;
}
.info b { color: #fff; font-size: 16px; font-variant-numeric: tabular-nums; }
.info .clk { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); }
.info .mis { color: var(--muted); }
.info .mis.has b { color: #ff9a8a; }
.info .mis svg { vertical-align: -2px; margin-right: 3px; }
.tools { display: flex; gap: 6px; align-items: center; }
button, select {
  font: inherit; color: var(--ink); background: var(--bg2);
  border: 1px solid rgba(214,174,92,.45); border-radius: 10px; padding: 6px 10px; cursor: pointer;
}
select { padding: 6px 6px; height: 34px; }
button.icon { width: 36px; height: 34px; padding: 0; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: linear-gradient(#e2bd6d, #b48a3a); color: #2a1708; border-color: #f0d08a; font-weight: 700; }
button.secondary { background: rgba(0,0,0,.25); }
button:focus-visible, select:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
button { touch-action: manipulation; }
[hidden] { display: none !important; }

/* ===== игровая часть: поле, инструменты, цифры ===== */
.play { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; }
/* портрет: группа чуть выше середины — снизу остаётся место под палец и баннер площадки */
.play::before, .play::after { content: ''; flex: 1 1 0; }
.play::after { flex-grow: 1.8; }
.boardWrap { position: relative; display: flex; flex-direction: column; align-items: center; }
.caption { width: 100%; display: flex; justify-content: space-between; align-items: baseline; padding: 0 2px 5px; color: var(--muted); font-size: 14px; }
.caption #title { color: var(--gold); font-weight: 700; }
.board {
  --cs: 40px;
  display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr);
  width: calc(var(--cs) * 9 + 6px); height: calc(var(--cs) * 9 + 6px);
  background: var(--paper); border: 3px solid var(--thick); border-radius: 8px; overflow: hidden;
  box-shadow: 0 0 0 2px rgba(214,174,92,.55), 0 14px 40px rgba(0,0,0,.55);
  touch-action: manipulation; cursor: pointer;
}
.cell {
  position: relative; display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--thin); border-bottom: 1px solid var(--thin);
  background: var(--paper); color: var(--user);
  font-size: calc(var(--cs) * .64); font-weight: 600; line-height: 1;
  font-variant-numeric: tabular-nums; transition: background-color .12s;
}
.cell.c2, .cell.c5 { border-right: 2px solid var(--thick); }
.cell.c8 { border-right: 0; }
.cell.r2, .cell.r5 { border-bottom: 2px solid var(--thick); }
.cell.r8 { border-bottom: 0; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.given { color: var(--given); font-weight: 700; }
.cell.hinted { color: var(--hinted); }
.cell.wrong { color: var(--bad); }
.cell.wrong:not(.sel) { background: var(--badBg); }
.cell .v { display: block; }
.cell.pop .v { animation: pop .22s ease-out; }
@keyframes pop { 0% { transform: scale(.5); opacity: .3; } 70% { transform: scale(1.18); } 100% { transform: scale(1); } }
.cell.shake .v { animation: shake .3s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-12%); } 75% { transform: translateX(12%); } }
.notes { position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.notes i {
  font-style: normal; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cs) * .27); font-weight: 600; color: #6e5a3e; line-height: 1;
}
.notes i.hit { color: #2a1708; background: rgba(240,185,61,.75); border-radius: 30%; }
/* заполнена строка/столбец/квадрат — волна */
.cell.flash { animation: flash .7s ease-out both; animation-delay: var(--d, 0s); }
@keyframes flash { 0% { background: var(--paper); } 30% { background: #ffd56b; } 100% { } }
/* победа: волна от последней клетки */
.board.win .cell { animation: winWave .9s ease-out both; animation-delay: var(--d, 0s); }
@keyframes winWave { 0% { } 35% { background: #ffcf55; transform: scale(1.08); color: #3a1f05; } 100% { background: #f3dfa6; } }
.board.win .cell .v { color: #3a1f05; }

.panel { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.tools2 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  height: 58px; padding: 4px 2px; background: rgba(0,0,0,.28); border-radius: 12px; font-size: 13px; color: var(--ink);
}
.tool svg { width: 25px; height: 25px; }
.tool.on { background: linear-gradient(#e2bd6d, #b48a3a); color: #2a1708; border-color: #f0d08a; }
.badge {
  position: absolute; top: 4px; right: 6px; font-style: normal; font-size: 11px; font-weight: 800; line-height: 1;
  background: var(--gold); color: #2a1708; border-radius: 9px; padding: 3px 6px;
}

.tool.on .badge { background: #2a1708; color: #f0d08a; }
.pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
.key {
  position: relative; height: clamp(56px, 8.4vh, 76px); padding: 0;
  background: linear-gradient(#3a2414, #24160c); border-color: rgba(214,174,92,.55); border-radius: 12px;
  font-size: 32px; font-weight: 600; color: #fff; font-variant-numeric: tabular-nums;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
}
.key .cnt { font-size: 11px; color: var(--muted); font-weight: 600; margin-top: 4px; }
.key:active { background: var(--gold2); }
.key.done { opacity: .22; }
.pad.notesOn .key { color: var(--gold); font-size: 24px; }
.key.hl { border-color: #ffd36b; box-shadow: inset 0 0 0 1px #ffd36b; }

.toast {
  position: absolute; left: 50%; top: 2px; transform: translate(-50%, -8px);
  max-width: calc(100% - 24px); width: max-content; text-align: center; z-index: 3;
  background: rgba(20,12,6,.94); border: 1px solid var(--gold2); color: var(--ink);
  padding: 8px 14px; border-radius: 12px; font-size: 15px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.alert { color: #ffcf7a; }

/* Модальное окно */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10; }
.card { background: linear-gradient(#3a2414, #24160c); border: 1px solid var(--gold2); border-radius: 16px; padding: 18px 20px; max-width: 480px; width: 100%; max-height: 90vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.6); user-select: text; -webkit-user-select: text; }
.card h2 { margin: 0 0 10px; font-family: Georgia, serif; color: var(--gold); font-size: 22px; }
.card h3 { margin: 14px 0 4px; font-family: Georgia, serif; color: var(--gold); font-size: 17px; }
.card p { margin: 6px 0; }
.rules { padding-left: 18px; margin: 0; } .rules li { margin: 6px 0; }
.opt { display: flex; gap: 10px; align-items: flex-start; margin: 8px 0; cursor: pointer; }
.opt input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--gold); flex: none; }
.modalBtns { display: flex; justify-content: flex-end; gap: 8px; margin: 14px -20px -18px; padding: 10px 20px 16px; position: sticky; bottom: -18px; background: linear-gradient(rgba(36,22,12,0), #24160c 30%); }
.modalBtns button { padding: 9px 16px; font-size: 16px; }
.big { font-size: 26px; font-weight: 800; color: #fff; }
.rec { color: #ffd36b; font-weight: 700; }
.stats { border-collapse: collapse; margin: 8px 0 2px; width: 100%; }
.stats th { text-align: right; color: var(--muted); font-weight: 600; font-size: 13px; padding: 2px 0 4px 10px; }
.stats th:first-child { text-align: left; padding-left: 0; }
.stats td { padding: 4px 0; border-bottom: 1px solid rgba(214,174,92,.18); }
.stats td + td { text-align: right; padding-left: 10px; color: #fff; font-weight: 700; font-variant-numeric: tabular-nums; }
.stats tr.cur td:first-child { color: var(--gold); font-weight: 700; }
.hintSmall { color: var(--muted); font-size: 13px; }

/* Широкий экран (компьютер, планшет и телефон боком): поле слева, панель справа */
@media (min-aspect-ratio: 5/4) {
  .play { flex-direction: row; gap: clamp(16px, 3vw, 40px); }
  .play::before, .play::after { display: none; }
  .panel { width: clamp(250px, 28vw, 360px); gap: 14px; }
  .tools2 { grid-template-columns: repeat(2, 1fr); }
  .tool { height: 64px; flex-direction: row; gap: 8px; font-size: 15px; }
  .badge { top: -7px; right: -7px; }
  .pad { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .key { height: auto; aspect-ratio: 1.15; font-size: 38px; }
  .key .cnt { font-size: 12px; }
}
@media (min-aspect-ratio: 5/4) and (max-height: 540px) {
  #app { padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: max(4px, env(safe-area-inset-bottom)); }
  .bar { padding-bottom: 4px; }
  .brand { display: none; }
  .caption { display: none; }
  .panel { gap: 6px; width: clamp(220px, 34vw, 320px); }
  .tool { height: 40px; font-size: 13px; }
  .tool svg { width: 20px; height: 20px; }
  .key { aspect-ratio: auto; height: calc((100dvh - 168px) / 3); min-height: 36px; font-size: 26px; }
  .key .cnt { display: none; }
}

/* Телефон, портрет: поле во всю ширину, крупные кнопки под большой палец */
@media (max-width: 560px) {
  #app { padding-left: max(4px, env(safe-area-inset-left)); padding-right: max(4px, env(safe-area-inset-right)); padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .brand { display: none; }
  .bar { gap: 6px; padding: 2px 2px 6px; }
  .info { margin: 0 auto 0 0; gap: 10px; padding: 4px 10px; }
  .board { border-radius: 6px; }
  .pad { gap: 4px; }
  .key { font-size: 32px; border-radius: 10px; height: clamp(56px, 9.6vh, 86px); }
  .tool { height: clamp(54px, 7.4vh, 66px); }
  select { max-width: 112px; }
  button.icon { width: 34px; }
  .tools2 { gap: 6px; }
  .toast { font-size: 14px; }
}
@media (max-width: 370px) { .info .mis { display: none; } .key { font-size: 26px; } }
