* { 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: 720px;
  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;
}

.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;
  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; }

/* ---------- Поле ---------- */
.board {
  display: grid;
  gap: 8px;
  margin: 0 auto 18px;
  width: 100%;
  max-width: 540px;
  touch-action: manipulation;
}

.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  perspective: 700px;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
}

.cell:disabled { cursor: default; }

.cell-inner {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  transform-style: preserve-3d;
  transition: transform .4s cubic-bezier(.4,.0,.2,1);
}

.cell.flipped .cell-inner,
.cell.matched .cell-inner { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border: 1.5px solid var(--border-soft);
  user-select: none;
}

/* рубашка */
.face-back {
  background: linear-gradient(135deg, #5a8835 0%, #4a6b2a 100%);
  border-color: var(--green-dark);
  color: #fffdf5;
  font-size: clamp(20px, 7vw, 34px);
  box-shadow: inset 0 0 0 4px rgba(255,253,245,.25);
}

/* лицо с эмодзи */
.face-front {
  background: #fff;
  border-color: var(--border-gold);
  transform: rotateY(180deg);
  font-size: clamp(26px, 11vw, 52px);
  line-height: 1;
}

.cell:not(:disabled):hover .face-back { filter: brightness(1.08); }

.cell:focus-visible {
  outline: 3px solid var(--border-gold);
  outline-offset: 3px;
  border-radius: 14px;
}

/* совпадение — зелёная вспышка */
.cell.matched .face-front {
  background: #d8ecc0;
  border-color: var(--green-mid);
  animation: pop .45s ease;
}

@keyframes pop {
  0%   { transform: rotateY(180deg) scale(1); }
  40%  { transform: rotateY(180deg) scale(1.12); box-shadow: 0 0 0 6px rgba(90,136,53,.45); }
  100% { transform: rotateY(180deg) scale(1); box-shadow: 0 0 0 0 rgba(90,136,53,0); }
}

/* несовпадение — лёгкое дрожание */
.cell.wrong .face-front { animation: shake .4s ease; }
@keyframes shake {
  0%,100% { transform: rotateY(180deg) translateX(0); }
  25% { transform: rotateY(180deg) translateX(-4px); }
  75% { transform: rotateY(180deg) translateX(4px); }
}

.cell.matched { opacity: .92; }

/* ---------- Кнопки игры ---------- */
.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-actions { display: flex; flex-direction: column; gap: 10px; }
.win-actions .btn-start { margin-top: 0; }

/* ---------- Уважение к prefers-reduced-motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .cell-inner { transition: none; }
  .cell.matched .face-front,
  .cell.wrong .face-front,
  .win-emoji,
  .win-overlay,
  .win-box { animation: none; }
}

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