:root {
  --bg: #000;
  --ink: #fff;
  --card: #1c1c1e;
  --panel: #2c2c2e;
  --muted: rgba(255, 255, 255, 0.72);
  --peek: 32px;
  --gap: 12px;
  --tabbar: calc(66px + env(safe-area-inset-bottom, 0px));
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --morph: 420ms;
  --morph-ease: cubic-bezier(0.32, 0.72, 0, 1);
  --fade: 0.3s;
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --morph: 1ms;
    --fade: 0.12s;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; cursor: pointer; }

/* Лента обложек */

.reels {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: min(100%, 480px);
  margin: 0 auto;
  padding: 0 0 calc(var(--gap) + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  scroll-snap-type: y mandatory;
  scroll-padding-top: calc(var(--gap) + env(safe-area-inset-top, 0px));
  scroll-padding-bottom: calc(var(--gap) + env(safe-area-inset-bottom, 0px));
  overscroll-behavior-y: contain;
  scrollbar-width: none;
}

.reels::-webkit-scrollbar { display: none; }

.reels.is-locked {
  z-index: 50;
  overflow: hidden;
}

.slide {
  height: calc(var(--screen-h, 100dvh) - var(--gap) * 2 - var(--peek) - env(safe-area-inset-top, 0px));
  padding: 0 var(--gap);
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

.slide:first-child {
  margin-top: calc(var(--gap) + env(safe-area-inset-top, 0px));
}

.slide + .slide { margin-top: var(--gap); }

.slide:not(:first-child):not(:last-child) { scroll-snap-align: center; }

.slide:last-child:not(:first-child) { scroll-snap-align: end; }

.back {
  position: fixed;
  z-index: 55;
  top: calc(var(--gap) + 14px + env(safe-area-inset-top, 0px));
  left: calc(var(--gap) + 14px);
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.48);
  color: #fff;
  text-decoration: none;
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  transition: opacity 0.2s ease, visibility 0.2s;
}

body:has(.reels.is-locked) .back {
  opacity: 0;
  visibility: hidden;
}

.game {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--device-radius, 55px) - var(--gap));
  overflow: hidden;
  background: var(--card);
  cursor: pointer;
  outline: none;
}

.game > .cover,
.game > .game-view,
.game > .game-loader {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--game-w, 100%);
  height: var(--game-h, 100%);
  transform: var(--fit, none);
  transform-origin: 0 0;
  transition: opacity var(--fade) ease, visibility var(--fade);
}

.game.is-floating {
  position: fixed;
  z-index: 60;
  border-radius: 0;
  clip-path: var(--clip, none);
  transition: clip-path var(--morph) var(--morph-ease);
}

.game.is-floating > .cover,
.game.is-floating > .game-view,
.game.is-floating > .game-loader {
  transition:
    transform var(--morph) var(--morph-ease),
    opacity var(--fade) ease,
    visibility var(--fade);
}

.game.is-open {
  cursor: default;
  touch-action: none;
}

.game.is-still,
.game.is-still > .cover,
.game.is-still > .game-view,
.game.is-still > .game-loader { transition: none; }

/* Обложки */

.game.is-loaded > .cover { visibility: hidden; }

.cover-caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 160px 32px 34px;
  background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 0) 0%,
    rgba(0, 0, 0, 0.18) 28%,
    rgba(0, 0, 0, 0.55) 62%,
    rgba(0, 0, 0, 0.82) 100%
  );
}

.cover-caption h2,
.stub-caption h2 {
  margin: 0;
  font-size: 42px;
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.cover-caption p,
.stub-caption p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 20px;
  line-height: 1.25;
}

.cover-coloring { background: #f2c48a; }
.cover-alphabet { background: #f4b4c4; }
.cover-numbers { background: #f6d36b; }
.cover-memory {
  background:
    radial-gradient(circle at 28% 32%, #ffd166 0 18%, transparent 19%),
    radial-gradient(circle at 72% 28%, #4cc9f0 0 16%, transparent 17%),
    radial-gradient(circle at 50% 62%, #ff4d6a 0 20%, transparent 21%),
    linear-gradient(160deg, #7b2cbf, #ff6b6b 55%, #ffd166);
}

.cover-coloring img,
.cover-alphabet img,
.cover-numbers img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cover-stub {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--stub);
}

.stub-caption {
  padding: 0 24px;
  text-align: center;
}

.cover-stub ~ .game-view .soon-screen { background: var(--stub); }

/* Загрузка */

.game-loader {
  z-index: 5;
  display: flex;
  opacity: 0;
  visibility: hidden;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(0, 0, 0, 0.55);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
}

.game.is-loading > .game-loader {
  opacity: 1;
  visibility: visible;
}

.game-loader p {
  margin: 0;
  font-size: 15px;
  font-weight: 500;
}

.progress {
  width: 160px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.2);
  overflow: hidden;
}

.progress i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: #fff;
}

.game.is-loading .progress i { animation: fill 1.3s var(--ease) forwards; }

@keyframes fill { to { width: 100%; } }

/* Игра */

.game-view {
  opacity: 0;
  visibility: hidden;
}

.game.is-loaded > .game-view {
  opacity: 1;
  visibility: visible;
}

.game:not(.is-open):not(.is-inline) .game-view,
.game:not(.is-open):not(.is-inline) .game-view * { pointer-events: none; }

.game.is-inline {
  cursor: default;
  touch-action: pan-y;
}

.game.is-inline > .game-view {
  opacity: 1;
  visibility: visible;
}

.game.is-inline img {
  -webkit-user-drag: none;
  user-select: none;
  -webkit-user-select: none;
}

.soon-screen {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: #3a3a3c;
  text-align: center;
}

.soon-screen p {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
}

.soon-screen span {
  color: var(--muted);
  font-size: 15px;
}

/* Раскраска */

.paint {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--card);
}

.paint-pages {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 8px;
}

.paint-pages { gap: 14px; }

.page-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: #fff;
  opacity: 0.6;
  transition: opacity 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

.page-thumb canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
}

.page-thumb.is-active {
  opacity: 1;
  box-shadow: 0 0 0 3px var(--card), 0 0 0 5px #fff;
}

.done-badge {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 22px;
  height: 22px;
  display: none;
  place-items: center;
  border-radius: 50%;
  background: #ffd43b;
  box-shadow: 0 0 0 2px var(--card);
  color: #3a2a00;
  font-size: 13px;
  line-height: 1;
}

.page-thumb.is-done .done-badge { display: grid; }

.paint { position: relative; }

.paint-toast {
  position: absolute;
  z-index: 2;
  left: 50%;
  bottom: calc(232px + env(safe-area-inset-bottom, 0px));
  margin: 0;
  padding: 10px 16px;
  border-radius: 999px;
  background: #ffd43b;
  color: #2b1f00;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}

.paint-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.paint-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  padding: 4px 12px 12px;
}

.paper {
  position: relative;
  flex: 1;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border-radius: calc(var(--device-radius, 55px) - var(--gap));
  overflow: hidden;
  background: #fff;
}

.paper canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.paper img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.paint-dock {
  --dock-inset: var(--gap);
  padding: 8px var(--dock-inset) var(--dock-inset);
}

/* Радиус плашки концентричен углу рамки: 55 − её реальный отступ от края экрана */
.dock-panel {
  --pencil-w: 52px;
  --lift: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 16px 0;
  overflow: hidden;
  border-radius: calc(var(--device-radius, 55px) - var(--dock-inset, var(--gap)));
  background: var(--panel);
}

.thickness {
  position: relative;
  height: 36px;
  margin: 0 12px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  touch-action: none;
  cursor: pointer;
  outline: none;
}

.thickness-track {
  flex: 1;
  height: 14px;
  background: linear-gradient(to right, var(--ink) calc(var(--thickness) * 100%), rgba(255, 255, 255, 0.2) 0);
  clip-path: polygon(0 40%, 100% 0, 100% 100%, 0 60%);
  pointer-events: none;
}

.thickness-thumb {
  position: absolute;
  top: 50%;
  left: calc(var(--thickness) * (100% - 28px));
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  margin-top: -14px;
  border-radius: 50%;
  background: #fff;
  pointer-events: none;
}

.thickness-thumb::after {
  content: "";
  width: calc(6px + var(--thickness) * 14px);
  height: calc(6px + var(--thickness) * 14px);
  border-radius: 50%;
  background: var(--ink);
}

.thickness:focus-visible .thickness-thumb { outline: 3px solid rgba(255, 255, 255, 0.5); }

.pencils {
  display: flex;
  justify-content: center;
  gap: 8px;
  height: 132px;
}

.pencil {
  position: relative;
  flex: 0 1 var(--pencil-w);
  min-width: 0;
  height: 100%;
  padding: 0;
  border: 0;
  background: none;
}

.pencil img {
  position: absolute;
  left: 0;
  top: var(--lift);
  width: 100%;
  pointer-events: none;
  transition: transform 0.22s var(--ease);
}

.pencil-eraser {
  flex-basis: calc(var(--pencil-w) * 1.48);
  margin-left: 4px;
}

.pencil-eraser img { top: calc(var(--lift) + var(--pencil-w) * 0.75); }

.pencil.is-active img { transform: translateY(calc(var(--lift) * -1)); }

.memory {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 72px 16px 20px;
  background: #1f6b32 url("../assets/games/memory-bg.png?v=2") center top / cover no-repeat;
  color: #fff;
  overflow: hidden;
  touch-action: pan-y;
}

.memory-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 28px;
  padding-left: 68px;
  font-size: 16px;
  font-weight: 600;
}

.memory-status { color: #ffd166; font-weight: 700; }

.memory-board[hidden] { display: none; }

.memory-board {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 10px;
  place-content: center;
  justify-items: stretch;
}

.memory[data-cols="2"] .memory-board { grid-template-columns: repeat(2, minmax(0, 136px)); }
.memory[data-cols="3"] .memory-board { grid-template-columns: repeat(3, minmax(0, 108px)); }
.memory[data-cols="4"] .memory-board { grid-template-columns: repeat(4, minmax(0, 82px)); }

.memory-card {
  width: 100%;
  aspect-ratio: 5 / 8;
  padding: 0;
  border: 0;
  background: transparent;
  perspective: 800px;
  cursor: pointer;
  touch-action: manipulation;
}

.memory-card.is-matched { cursor: default; }

.memory-card-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 18px;
  transform-style: preserve-3d;
  transition: transform 0.32s ease;
}

.memory-card.is-up .memory-card-inner { transform: rotateY(180deg); }

.memory-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: inherit;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.memory-back { background: #b7e3a8; }

.memory-front {
  transform: rotateY(180deg);
  background: #fff;
  overflow: hidden;
}

.memory-front img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

.memory-label {
  position: relative;
  z-index: 1;
  font-size: 42px;
  font-weight: 800;
  line-height: 1;
}

.memory[data-cols="3"] .memory-label,
.memory[data-cols="4"] .memory-label { font-size: 32px; }

.memory-shape {
  position: absolute;
  width: 46%;
  height: 34%;
  opacity: 0.35;
}

.memory-shape-circle { border-radius: 50%; background: #fff; }
.memory-shape-square { border-radius: 8px; background: #fff; }
.memory-shape-triangle {
  width: 0;
  height: 0;
  background: transparent;
  border-left: 22px solid transparent;
  border-right: 22px solid transparent;
  border-bottom: 38px solid #fff;
}
.memory-shape-diamond {
  background: #fff;
  transform: rotate(45deg) scale(0.72);
  border-radius: 4px;
}
.memory-shape-star {
  background: #fff;
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.memory-shape-heart {
  background: #fff;
  border-radius: 50% 50% 0 0;
  transform: rotate(-45deg) scale(0.7);
}

.memory-done[hidden] { display: none; }

.memory-done {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 0 24px;
  text-align: center;
}

.memory-praise {
  margin: 0;
  max-width: 360px;
  font-size: 60px;
  font-weight: 800;
  line-height: 1.05;
  text-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}

.memory-again {
  width: 260px;
  height: 72px;
  border: 0;
  border-radius: 36px;
  padding: 0 28px;
  background: #ffd166;
  color: #1c1c1e;
  font: 700 28px/1 "Inter", sans-serif;
  cursor: pointer;
}
