:root {
  --ink: #4a2c1a;
  --cream: #fff8ea;
  --paper: #fffdf7;
  --green: #4cc46a;
  --green-d: #2f9a4c;
  --orange: #ffa53a;
  --orange-d: #e07d10;
  --red: #ff6b6b;
  --red-d: #d94848;
  --sky: #bfe8ff;
  --shadow: 0 4px 0 rgba(74, 44, 26, .25);
  --safe-top: max(env(safe-area-inset-top), var(--tg-safe-area-inset-top, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
  --tg-top: var(--tg-content-safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--sky);
  color: var(--ink);
  font-family: 'Nunito', system-ui, sans-serif;
  font-weight: 800;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
img { -webkit-user-drag: none; user-select: none; }
[hidden] { display: none !important; }

.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  max-width: 560px; margin: 0 auto;
  padding-top: calc(var(--safe-top) + var(--tg-top));
  padding-bottom: var(--safe-bottom);
  animation: screen-in .28s ease-out;
}
@keyframes screen-in { from { opacity: 0; transform: translateY(14px); } }

/* ---------- кнопки ---------- */
.big-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; min-height: 68px; border-radius: 24px;
  font-size: 26px; font-weight: 900; color: #fff;
  text-shadow: 0 2px 0 rgba(0,0,0,.18);
  border: 3px solid var(--ink);
  box-shadow: 0 6px 0 var(--ink);
  transition: transform .08s, box-shadow .08s;
}
.big-btn:active, .mid-btn:active, .round-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.big-btn.green { background: var(--green); }
.big-btn.orange { background: var(--orange); }
.count {
  min-width: 36px; height: 36px; padding: 0 8px; border-radius: 18px;
  background: #fff; color: var(--orange-d); font-size: 20px; line-height: 36px; text-shadow: none;
}
.mid-btn {
  min-height: 52px; padding: 0 18px; border-radius: 18px;
  background: #fff; border: 3px solid var(--ink); box-shadow: 0 4px 0 var(--ink);
  font-size: 19px; font-weight: 900;
}
.mid-btn.red { background: var(--red); color: #fff; }
.round-btn {
  width: 52px; height: 52px; flex: none; border-radius: 50%;
  background: #fff; border: 3px solid var(--ink); box-shadow: 0 4px 0 var(--ink);
  font-size: 26px; font-weight: 900; line-height: 1;
}
.round-btn.ok { background: var(--green); color: #fff; font-size: 30px; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px; position: relative; z-index: 5;
}
.top h2 { margin: 0; font-size: 26px; font-weight: 900; text-align: center; flex: 1; }
.top > span { width: 52px; }
.top.floating { z-index: 300; position: absolute; left: 0; right: 0; top: calc(var(--safe-top) + var(--tg-top)); }
.top.floating h2 { color: #fff; text-shadow: 0 2px 0 var(--ink), 0 0 6px rgba(74,44,26,.6); }

/* ---------- главная ---------- */
#home { justify-content: space-between; overflow: hidden; }
.home-bg {
  position: absolute; inset: 0; z-index: -1;
  background: url(img/bg_candy.webp) center / cover no-repeat, #ffe3f1;
}
.logo {
  margin: 28px 16px 0; text-align: center;
  font-size: clamp(44px, 13vw, 64px); line-height: .95; font-weight: 900; color: #fff;
  -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  text-shadow: 0 6px 0 var(--ink);
  transform: rotate(-3deg);
  animation: wobble 4s ease-in-out infinite;
}
@keyframes wobble { 50% { transform: rotate(2deg) scale(1.03); } }
.home-parade { flex: 1; position: relative; }
.home-parade img {
  position: absolute; bottom: 8%;
  filter: drop-shadow(0 6px 0 rgba(74,44,26,.18));
  animation: hop 1.6s ease-in-out infinite;
}
@keyframes hop { 0%, 100% { transform: translateY(0); } 45% { transform: translateY(-16px) rotate(-3deg); } }
.home-buttons { display: grid; gap: 16px; padding: 0 20px 26px; }

/* ---------- выбор зверя ---------- */
#pick { background: linear-gradient(#bfe8ff, #e9f8d8); }
.pick-grid {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px;
  padding: 6px 16px 24px; align-content: start;
}
.pick-card {
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  aspect-ratio: .86; padding: 8px 6px 6px; border-radius: 22px;
  background: #fff; border: 3px solid var(--ink); box-shadow: var(--shadow);
  font-size: 15px; animation: pop-in .35s backwards;
}
.pick-card img { width: 100%; height: 76%; object-fit: contain; }
.pick-card:active { transform: scale(.94); }
@keyframes pop-in { from { transform: scale(.6); opacity: 0; } }

/* ---------- зверь (общий рендер) ---------- */
.animal { position: relative; isolation: isolate; }
.animal > .body { display: block; width: 100%; height: auto; pointer-events: none; }
.animal.tinted > .body { filter: grayscale(1) brightness(1.12) contrast(1.05); }
.animal .tint, .animal .pattern {
  position: absolute; inset: 0; pointer-events: none;
  -webkit-mask-size: 100% 100%; mask-size: 100% 100%;
  -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
}
.animal .tint { mix-blend-mode: multiply; }
.animal .pattern { opacity: .8; }
.animal .acc {
  position: absolute; transform-origin: center; touch-action: none;
  transform: translate(-50%, -50%) rotate(var(--r, 0deg)) scaleX(var(--f, 1));
}
.animal .acc img { display: block; width: 100%; pointer-events: none; }
.animal .acc.back { z-index: -1; }
.animal .acc.sel { filter: drop-shadow(0 0 3px #fff) drop-shadow(0 0 3px #3fa9ff); }

/* ---------- мастерская ---------- */
#workshop { background: var(--cream); }
.name-box {
  flex: 1; min-width: 0; display: flex; align-items: center;
  background: #fff; border: 3px solid var(--ink); border-radius: 18px; box-shadow: var(--shadow);
  height: 52px; padding-left: 12px;
}
.name-box input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: none;
  font: inherit; font-size: 22px; font-weight: 900; color: var(--ink); text-align: center;
}
.dice { font-size: 28px; width: 48px; height: 100%; }
.dice:active { transform: rotate(90deg); }
.stage {
  flex: 1; position: relative; margin: 0 12px; overflow: hidden;
  border-radius: 28px; border: 3px solid var(--ink); box-shadow: var(--shadow);
}
.stage-bg { position: absolute; inset: 0; background: #cfe9ff center / cover no-repeat; transition: background-image .3s; }
.stage-animal { position: absolute; inset: 0; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 7%; }
.stage-animal .animal { filter: drop-shadow(0 8px 4px rgba(0,0,0,.18)); }
.stage-animal .animal.bounce { animation: bounce .45s; }
@keyframes bounce { 30% { transform: scale(1.08, .92); } 60% { transform: scale(.95, 1.06) translateY(-12px); } }
.sticker-tools {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); z-index: 4;
  display: flex; gap: 6px; padding: 6px; border-radius: 20px;
  background: rgba(255,255,255,.95); border: 3px solid var(--ink); box-shadow: var(--shadow);
}
.sticker-tools button { width: 46px; height: 46px; border-radius: 14px; font-size: 24px; background: var(--cream); }
.sticker-tools button:active { transform: scale(.9); }

.tabs { display: flex; gap: 6px; padding: 10px 12px 6px; }
.tabs button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 0; border-radius: 16px; font-size: 13px;
  background: #fff; border: 3px solid transparent; opacity: .75;
}
.tabs button b { font-size: 26px; line-height: 1.1; }
.tabs button.on { border-color: var(--ink); opacity: 1; box-shadow: var(--shadow); background: #fff3c4; }
.tray {
  display: flex; gap: 10px; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  padding: 6px 12px 14px; min-height: 104px; align-items: center;
}
.tray::-webkit-scrollbar { display: none; }
.chip {
  flex: none; width: 80px; height: 80px; border-radius: 22px; position: relative;
  background: #fff center / cover no-repeat; border: 3px solid var(--ink); box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  animation: pop-in .25s backwards;
}
.chip:active { transform: scale(.92); }
.chip.on { outline: 4px solid #3fa9ff; outline-offset: 2px; }
.chip img { width: 84%; height: 84%; object-fit: contain; }
.chip .label {
  position: absolute; left: 0; right: 0; bottom: 0; font-size: 11px; text-align: center;
  background: rgba(255,255,255,.85); padding: 1px 0;
}
.chip.round { border-radius: 50%; }
.chip.text { font-size: 15px; text-align: center; line-height: 1.1; padding: 4px; }
.chip .pat { position: absolute; inset: 0; }
.chip.magic { width: auto; padding: 0 18px; font-size: 18px; gap: 8px; background: #fff3c4; }
.chip.magic b { font-size: 30px; }

/* ---------- двор ---------- */
#yard { overflow: hidden; }
.yard { position: absolute; inset: 0; background: url(img/bg_yard.webp) center bottom / cover no-repeat, #9be07a; }
.pet {
  position: absolute; transform: translateX(-50%);
  transition: left var(--walk, 3s) linear;
}
.pet .flip { transition: transform .25s; }
.pet.right .flip { transform: scaleX(-1); }
.pet .animal { filter: drop-shadow(0 6px 3px rgba(0,0,0,.2)); }
.pet.walking .animal { animation: waddle .5s ease-in-out infinite; }
@keyframes waddle { 0%, 100% { transform: translateY(0) rotate(0); } 25% { transform: translateY(-5px) rotate(-2deg); } 75% { transform: translateY(-5px) rotate(2deg); } }
.pet.jump .animal { animation: jump .6s ease-out; }
@keyframes jump { 40% { transform: translateY(-38%) scale(1.05); } 70% { transform: translateY(0) scale(1.1, .9); } }
.bubble {
  position: absolute; left: 50%; bottom: 100%; transform: translateX(-50%);
  white-space: nowrap; padding: 6px 12px; border-radius: 16px; font-size: 17px;
  background: #fff; border: 3px solid var(--ink); box-shadow: var(--shadow);
  animation: pop-in .25s;
}
.bubble::after {
  content: ''; position: absolute; left: 50%; top: 100%; margin-left: -8px;
  border: 8px solid transparent; border-top-color: var(--ink);
}
.yard-empty {
  position: absolute; left: 20px; right: 20px; top: 34%; text-align: center;
  padding: 20px; border-radius: 28px; background: rgba(255,255,255,.92); border: 3px solid var(--ink); box-shadow: var(--shadow);
}
.yard-empty p { font-size: 22px; margin: 0 0 16px; }
.pet-card {
  position: absolute; left: 16px; right: 16px; bottom: calc(16px + var(--safe-bottom)); z-index: 300;
  padding: 14px; border-radius: 26px; background: #fff; border: 3px solid var(--ink); box-shadow: var(--shadow);
  animation: screen-in .2s;
}
.pet-name { font-size: 26px; text-align: center; margin-bottom: 10px; }
.pet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- подтверждение и праздник ---------- */
.confirm {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center;
  background: rgba(40, 24, 10, .45); padding: 16px;
}
.confirm-box {
  width: 100%; max-width: 360px; padding: 22px; border-radius: 28px; text-align: center;
  background: #fff; border: 3px solid var(--ink); box-shadow: 0 6px 0 var(--ink);
}
.confirm-box p { font-size: 22px; margin: 0 0 18px; }
.confirm-box div { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.confetti {
  position: fixed; z-index: 200; pointer-events: none; font-size: 30px;
  animation: fly 1.1s ease-out forwards;
}
@keyframes fly {
  to { transform: translate(var(--dx), var(--dy)) rotate(var(--rot)); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
