/* «Ночной лес»: поле — CSS-сетка 9×9, звери — слой поверх с плавным шагом. Палитра — Игротека ночью. */
:root {
  --n: 9;
  --night: #0c1f24;
  --night-deep: #061214;
  --amber: #f4b860;
  --amber-bright: #ffc978;
  --ember: #e8763a;
  --cream: #fbf1d6;
  --muted: #9cbdb1;
  --ink: #3a2614;
  --hud-h: 76px;
  --ctrl-h: 64px;
  --size: min(calc(100vw - 24px), calc(100vh - var(--hud-h) - var(--ctrl-h) - 16px), 640px);
}

html, body { margin: 0; height: 100%; overflow-x: hidden; }

body {
  display: flex; flex-direction: column; align-items: center;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(244, 184, 96, .10), transparent 55%),
    radial-gradient(ellipse at 50% 40%, #16352f, var(--night-deep) 75%);
  background-attachment: fixed;
  color: var(--cream);
  font-family: "Nunito", system-ui, sans-serif;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

header { text-align: center; padding: 8px 12px 0; height: var(--hud-h); box-sizing: border-box; }
h1 { margin: 0; font: 700 clamp(20px, 4.5vmin, 30px)/1.15 "Rubik", system-ui, sans-serif; letter-spacing: .02em; }

.stats {
  display: flex; gap: clamp(10px, 3vw, 22px); justify-content: center; align-items: center;
  margin-top: 6px; font-size: clamp(14px, 2.6vmin, 18px); font-weight: 700;
}
.hub-link {
  color: var(--cream); text-decoration: none; padding: 3px 12px; border-radius: 999px;
  background: rgba(0, 0, 0, .3); font-weight: 600;
}
.hub-link:hover, .hub-link:focus-visible { background: rgba(0, 0, 0, .5); outline: 2px solid var(--amber); outline-offset: 2px; }
.hearts { display: inline-flex; gap: 2px; }
.hearts img { width: clamp(20px, 3.6vmin, 26px); height: clamp(20px, 3.6vmin, 26px); }

/* ---------------------------------------------------------------- поле */
#board {
  position: relative;
  width: var(--size); height: var(--size);
  margin: 6px 0 10px;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 18px 40px -10px rgba(0, 0, 0, .8), 0 0 0 2px rgba(244, 184, 96, .18);
  touch-action: none;   /* свайпы по полю — ходы, а не прокрутка страницы */
}
#cells { display: grid; grid-template-columns: repeat(var(--n), 1fr); width: 100%; height: 100%; }

.cell {
  position: relative;
  background-size: 100% 100%;
  cursor: pointer;
}
.cell.tree { cursor: default; }
.cell img { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cell img[hidden] { display: none; }
.cell .berry { inset: 12%; width: 76%; height: 76%; }
.cell.pop .berry, .cell.pop { animation: pop .4s ease; }

/* рывок: куда можно прыгнуть */
.cell.target::after {
  content: ""; position: absolute; inset: 14%;
  border: 2px dashed rgba(244, 184, 96, .45); border-radius: 50%;
  pointer-events: none;
}
#board.aim-mode .cell.target::after {
  border: 3px solid var(--amber-bright); background: rgba(244, 184, 96, .18);
  animation: pulse 1s ease-in-out infinite;
}

/* ---------------------------------------------------------------- звери */
#actors { position: absolute; inset: 0; pointer-events: none; }
.actor {
  position: absolute; left: 0; top: 0;
  width: calc(100% / var(--n)); height: calc(100% / var(--n));
  transform: translate(calc(var(--x, 0) * 100%), calc(var(--y, 0) * 100%));
  transition: transform .14s ease-out, opacity .3s;
}
#actors.snap .actor { transition: none; }
.actor img { width: 100%; height: 100%; display: block; filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .55)); }
.actor.face-right img { transform: scaleX(-1); }
.actor.hurt img { animation: hurt .35s ease; }
.actor.block img { animation: block .5s ease; }
.actor.pushed { transition-duration: .2s; }
@keyframes block { 0%, 100% { filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .55)); } 40% { filter: drop-shadow(0 0 10px #ffc978) brightness(1.3); } }
.actor.gone { opacity: 0; }
.actor.hidden img { opacity: .55; filter: drop-shadow(0 0 4px rgba(111, 163, 90, .9)); }
.actor.owl.resting img { opacity: .65; transform: scale(.85); }
.actor.owl.resting.face-right img { transform: scale(-.85, .85); }
.actor.wolf img { transform: scale(1.08); }
.actor.wolf.face-right img { transform: scale(-1.08, 1.08); }

/* время суток: лёгкий оттенок поверх поля */
#board::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 2;
  transition: background-color 1s;
}
#board[data-phase="dusk"]::after { background: rgba(120, 70, 140, .10); }
#board[data-phase="night"]::after { background: rgba(10, 30, 70, .16); }
#board[data-phase="deep"]::after { background: rgba(0, 5, 20, .30); }
#board[data-phase="dawn"]::after { background: rgba(255, 170, 120, .12); }

.pips { position: absolute; top: 2%; left: 50%; transform: translateX(-50%); display: flex; gap: 3px; }
.pips i { width: 7px; height: 7px; border-radius: 50%; background: var(--ember); box-shadow: 0 0 0 1.5px var(--night-deep); }
.pips i.off { background: rgba(255, 255, 255, .15); }

#board.shake { animation: shake .3s ease; }

@keyframes hurt { 0%, 100% { filter: none; } 30% { filter: brightness(2.2) sepia(1) hue-rotate(-30deg) saturate(4); transform: translateY(-6%); } }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
@keyframes pop { 50% { transform: scale(1.25); } }
@keyframes pulse { 50% { opacity: .55; } }

/* ---------------------------------------------------------------- кнопки под полем */
.controls { display: flex; gap: 12px; height: var(--ctrl-h); align-items: flex-start; }
.ctrl {
  position: relative; display: inline-flex; align-items: center; gap: 8px;
  min-height: 46px; padding: 6px 18px;
  font: 700 16px "Rubik", system-ui, sans-serif; color: var(--cream);
  background: rgba(0, 0, 0, .35); border: 2px solid rgba(244, 184, 96, .45); border-radius: 999px;
  cursor: pointer;
}
.ctrl img { width: 30px; height: 30px; }
.ctrl:hover:not(:disabled), .ctrl:focus-visible { border-color: var(--amber); outline: none; box-shadow: 0 0 14px rgba(244, 184, 96, .35); }
.ctrl.active { background: rgba(244, 184, 96, .25); border-color: var(--amber-bright); }
.ctrl:disabled { opacity: .45; cursor: default; }
.ctrl b {
  position: absolute; top: -6px; right: -4px; min-width: 22px; height: 22px; border-radius: 11px;
  background: var(--ember); color: #fff; font-size: 13px; line-height: 22px; text-align: center;
}
.ctrl b:empty { display: none; }

/* ---------------------------------------------------------------- окна */
#banner {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .45); color: var(--cream);
  font: 700 clamp(32px, 8vmin, 60px) "Rubik", system-ui, sans-serif;
  text-shadow: 0 4px 16px rgba(0, 0, 0, .9); pointer-events: none; z-index: 50;
}
.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(2, 8, 9, .72); z-index: 100; padding: 16px; box-sizing: border-box;
}
#banner[hidden], .overlay[hidden], #error[hidden] { display: none; }

.modal {
  background: var(--cream); color: var(--ink);
  border: 4px solid #4a3420; border-radius: 20px;
  padding: clamp(16px, 4vmin, 28px) clamp(18px, 5vmin, 34px);
  width: min(100%, 400px); box-sizing: border-box; text-align: center;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .7), 0 0 0 2px #dcc79c;
}
.modal h2 { margin: 0; font: 700 clamp(24px, 5vmin, 32px) "Rubik", system-ui, sans-serif; }
.subtitle { margin: 2px 0 14px; color: #8a5a2b; font-weight: 700; }
.rules { text-align: left; margin: 0 0 12px; padding-left: 20px; line-height: 1.45; }
.rules b { color: #c4442a; }
.keys { font-size: 13px; color: #6b5440; margin: 0 0 8px; }
.best { font-weight: 700; margin: 0 0 14px; }
.summary-lines { margin: 14px 0 18px; font-size: 17px; font-weight: 700; line-height: 1.5; }
.summary-lines p { margin: 0; }
.actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.btn {
  font: 700 16px "Rubik", system-ui, sans-serif; padding: 10px 22px; border-radius: 999px;
  border: 2px solid #4a3420; background: #dcc79c; color: var(--ink); text-decoration: none; cursor: pointer;
}
.btn-primary { background: var(--ember); color: #fff; }
.btn:hover, .btn:focus-visible { filter: brightness(1.08); outline: 2px solid var(--amber); outline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: wait; }

/* выбор зверя */
.animals { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 0 10px; }
.animal-card {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 8px 6px; border: 3px solid #dcc79c; border-radius: 14px; background: #fff8e8;
  font: 12px/1.25 "Nunito", system-ui, sans-serif; color: var(--ink); cursor: pointer;
  transition: border-color .15s, transform .15s, box-shadow .15s;
}
.animal-card > img { width: 64px; height: 64px; }
.animal-card b { font: 700 15px "Rubik", system-ui, sans-serif; }
.animal-card .hp { color: var(--ember); letter-spacing: 1px; font-size: 12px; }
.animal-card .ab { display: block; }
.animal-card .ab img { width: 18px; height: 18px; vertical-align: -4px; margin-right: 3px; }
.animal-card .rec { font-weight: 700; color: #8a5a2b; }
.animal-card.chosen { border-color: var(--ember); box-shadow: 0 0 0 2px var(--ember), 0 6px 14px rgba(58, 38, 20, .2); }
.animal-card:not(:disabled):hover, .animal-card:focus-visible { transform: translateY(-2px); outline: none; border-color: var(--ember); }
.animal-card.locked { cursor: default; opacity: .75; }
.animal-card.locked > img { filter: brightness(0); opacity: .45; }
.unlock { color: #2d7a4e; }
@media (max-width: 420px) {
  .animal-card { padding: 6px 4px; font-size: 11px; }
  .animal-card > img { width: 48px; height: 48px; }
  .animal-card .ab { display: none; }
  .animal-card.chosen .ab, .animal-card.locked .ab { display: block; }
}

/* выбор улучшения */
.modal-wide { width: min(100%, 620px); }
.perk-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 12px; }
.perk-card {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 12px 10px; border: 3px solid #dcc79c; border-radius: 16px; background: #fff8e8;
  font: 14px/1.3 "Nunito", system-ui, sans-serif; color: var(--ink); cursor: pointer;
  transition: transform .15s, border-color .15s, box-shadow .15s;
}
.perk-card img { width: 72px; height: 72px; }
.perk-card div { display: flex; flex-direction: column; gap: 4px; }
.perk-card b { font: 700 16px "Rubik", system-ui, sans-serif; }
.perk-card kbd {
  position: absolute; top: 6px; left: 8px; font: 700 12px "Rubik", sans-serif; color: #8a5a2b; opacity: .7;
}
.perk-card:hover, .perk-card:focus-visible { transform: translateY(-3px); border-color: var(--ember); box-shadow: 0 8px 18px rgba(58, 38, 20, .25); outline: none; }
@media (max-width: 520px) {
  .perk-list { grid-template-columns: 1fr; gap: 8px; }
  .perk-card { flex-direction: row; text-align: left; padding: 8px 12px; }
  .perk-card img { width: 48px; height: 48px; flex: none; }
  .perk-card div { gap: 1px; }
  .perk-card span { font-size: 13px; }
  .perk-card kbd { display: none; }
}

#error {
  position: fixed; left: 50%; top: 40%; transform: translateX(-50%);
  background: #4a1d14; color: #fff; padding: 14px 20px; border-radius: 12px; z-index: 200;
}

@media (prefers-reduced-motion: reduce) {
  .actor, .actor img { transition: none !important; animation: none !important; }
  #board.shake, .cell.pop, #board.aim-mode .cell.target::after { animation: none; }
}
