* { box-sizing: border-box; }
/* атрибут hidden должен скрывать элемент, даже если у класса задан display:flex/grid */
[hidden] { display: none !important; }

:root {
  --green: #4a6b2a;
  --green-dark: #3a5520;
  --green-mid: #5a8835;
  --cream: #fffdf5;
  --cream-2: #f3f0e4;
  --border-gold: #b8862c;
  --border-soft: #b8a86c;
  --ink: #3a3320;
}

body {
  margin: 0;
  min-height: 100vh;
  padding: 24px 14px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: linear-gradient(180deg, var(--cream-2) 0%, #e7e0c8 100%);
  font-family: Georgia, 'Times New Roman', serif;
  color: var(--ink);
}

.card {
  width: 100%;
  max-width: 640px;
  background: var(--cream);
  border: 1.5px solid var(--border-gold);
  border-radius: 14px;
  padding: 24px 26px 28px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

h1 { margin: 0 0 6px; color: var(--green); font-size: 28px; }

.hint { margin: 0 0 20px; color: #8a7a4a; font-size: 14px; line-height: 1.5; }

/* ---------- Настройки ---------- */
.control { margin-bottom: 18px; }

.control-label {
  display: block;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--green);
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  font-family: inherit;
  font-size: 15px;
  padding: 8px 14px;
  border: 1.5px solid var(--border-soft);
  border-radius: 999px;
  background: #fff;
  color: #5a4a20;
  cursor: pointer;
  user-select: none;
  transition: background .15s, border-color .15s, color .15s;
}

.chip:hover { border-color: var(--green-mid); }

.chip[aria-checked="true"] {
  background: #c8d8a4;
  border-color: var(--green-mid);
  color: var(--green-dark);
  font-weight: 700;
}

.chip:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }

.chip-ico { font-size: 18px; margin-right: 4px; }

/* переключатель «показывать номера» */
.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 14px;
  font-size: 14px;
  color: #6a5a30;
  cursor: pointer;
}
.switch input { width: 18px; height: 18px; accent-color: var(--green-mid); cursor: pointer; }

.best-line { margin: 0 0 12px; font-size: 14px; color: var(--green-dark); min-height: 1em; }
.best-line b { color: var(--green); }

.btn-start {
  margin-top: 6px;
  width: 100%;
  font-family: inherit;
  font-size: 17px;
  font-weight: 700;
  padding: 13px;
  border: 1.5px solid var(--green-mid);
  border-radius: 10px;
  background: #c8d8a4;
  color: var(--green-dark);
  cursor: pointer;
  transition: background .15s;
}
.btn-start:hover { background: #d6e4b6; }
.btn-start:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }

/* ---------- Статистика ---------- */
.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
}

.stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--cream-2);
  border: 1.5px solid var(--border-soft);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 15px;
  color: var(--green-dark);
}
.stat-ico { font-size: 18px; }
.stat-val { font-weight: 700; }

.goal { padding: 5px 10px; }
.goal-img {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1.5px solid var(--border-soft);
  display: block;
}

/* ---------- Поле ---------- */
.board {
  position: relative;
  width: 100%;
  max-width: 440px;
  aspect-ratio: 1 / 1;
  margin: 0 auto 18px;
  background: var(--green-dark);
  border: 3px solid var(--green-dark);
  border-radius: 14px;
  overflow: hidden;
  touch-action: manipulation;
}

.tile {
  position: absolute;
  width: calc(100% / var(--n));
  height: calc(100% / var(--n));
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  font-family: inherit;
  transition: left .16s ease, top .16s ease;
}
.tile:disabled { cursor: default; }

.tile-face {
  position: absolute;
  inset: 2px;
  border-radius: 9px;
  background-repeat: no-repeat;
  box-shadow: inset 0 0 0 1.5px rgba(255,253,245,.55), 0 1px 3px rgba(0,0,0,.25);
}

.tile-num {
  position: absolute;
  left: 5px;
  bottom: 4px;
  font-size: clamp(11px, 3.4vw, 16px);
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,.7), 0 0 3px rgba(0,0,0,.6);
  pointer-events: none;
}

.tile.movable .tile-face { box-shadow: inset 0 0 0 2px #ffe9a8, 0 1px 4px rgba(0,0,0,.3); }
.tile:not(:disabled):hover .tile-face { filter: brightness(1.06); }
.tile:focus-visible { outline: 3px solid var(--border-gold); outline-offset: -1px; border-radius: 11px; }

/* короткая вспышка при победе */
.board.solved .tile-face { animation: glow .7s ease; }
@keyframes glow {
  0%, 100% { box-shadow: inset 0 0 0 1.5px rgba(255,253,245,.55), 0 1px 3px rgba(0,0,0,.25); }
  50% { box-shadow: inset 0 0 0 2px #ffe9a8, 0 0 10px 2px rgba(255,220,120,.7); }
}

/* ---------- Кнопки игры ---------- */
.game-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.btn-secondary {
  font-family: inherit;
  font-size: 15px;
  padding: 10px 18px;
  border: 1.5px solid var(--border-soft);
  border-radius: 10px;
  background: #fff;
  color: var(--green-dark);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.btn-secondary:hover { border-color: var(--green-mid); background: var(--cream-2); }
.btn-secondary:focus-visible { outline: 3px solid var(--green-mid); outline-offset: 2px; }

/* ---------- Победа ---------- */
.win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(58, 51, 32, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
  animation: fadein .25s ease;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

.win-box {
  background: var(--cream);
  border: 1.5px solid var(--border-gold);
  border-radius: 16px;
  padding: 28px 30px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
  animation: rise .3s ease;
}
@keyframes rise { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.win-emoji { font-size: 56px; animation: bounce 1s ease infinite; }
@keyframes bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }

.win-box h2 { margin: 8px 0 6px; color: var(--green); font-size: 26px; }
.win-text { margin: 0 0 20px; color: #6a5a30; font-size: 16px; line-height: 1.5; }
.win-text b { color: var(--green); }

.win-actions { display: flex; flex-direction: column; gap: 10px; }
.win-actions .btn-start { margin-top: 0; }

@media (prefers-reduced-motion: reduce) {
  .tile { transition: none; }
  .board.solved .tile-face, .win-emoji, .win-overlay, .win-box { animation: none; }
}

@media (max-width: 420px) {
  .card { padding: 18px 14px 22px; }
  h1 { font-size: 24px; }
}
