:root {
  --bg: #ffffff;
  --text: #2b2118;
  --hint: #8e8e93;
  --accent: var(--tg-theme-button-color, #e5484d);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e5484d;
  /* в полноэкранном режиме Telegram (запуск из списка чатов) сверху его кнопки и строка состояния */
  --safe-top: calc(max(var(--tg-safe-area-inset-top, 0px), env(safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: calc(max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px)) + var(--tg-content-safe-area-inset-bottom, 0px));
  --safe-left: calc(var(--tg-safe-area-inset-left, 0px) + var(--tg-content-safe-area-inset-left, 0px));
  --safe-right: calc(var(--tg-safe-area-inset-right, 0px) + var(--tg-content-safe-area-inset-right, 0px));
}

* { box-sizing: border-box; margin: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.app {
  position: relative;
  max-width: 520px;
  height: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

.hud { display: flex; justify-content: space-between; align-items: flex-end; padding: 12px 16px; }
.score span { font-size: 40px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.score small { margin-left: 6px; color: var(--hint); }
.best { color: var(--hint); font-size: 14px; }

.field {
  position: relative;
  flex: 1;
  overflow: hidden;
  touch-action: none;
  cursor: none;
}

.dog {
  position: absolute;
  left: 50%;
  bottom: 8%;
  height: 82%;
  aspect-ratio: 790 / 1189;
  max-width: 92%;
  transform: translateX(-50%);
  transform-origin: 50% 100%;
}
.dog .body { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.dog .head {
  position: absolute;
  left: 37%;
  top: -3%;
  width: 68%;
  opacity: 0;
  transform: scale(.6) rotate(-12deg);
  transition: opacity .12s, transform .18s cubic-bezier(.3, 1.6, .5, 1);
  pointer-events: none;
}

.field.warn .dog { animation: shiver .12s linear infinite; }
@keyframes shiver {
  0%, 100% { transform: translateX(-50%) rotate(0); }
  25% { transform: translateX(-50%) rotate(-1.2deg); }
  75% { transform: translateX(-50%) rotate(1.2deg); }
}
.field.look .dog .head { opacity: 1; transform: scale(1) rotate(0); }

.alert {
  position: absolute;
  left: 50%;
  top: 4%;
  transform: translateX(-50%) scale(0);
  font-size: 56px;
  font-weight: 900;
  color: var(--danger);
  transition: transform .12s;
  pointer-events: none;
}
.field.warn .alert, .field.look .alert { transform: translateX(-50%) scale(1); }

.comb {
  position: absolute;
  left: 0;
  top: 0;
  width: 128px;
  opacity: 0;
  pointer-events: none;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, .25));
  z-index: 5;
}
.comb.on { opacity: 1; }

.hair {
  position: absolute;
  width: 26px;
  height: 26px;
  pointer-events: none;
  z-index: 4;
}

.floor { position: absolute; left: 0; right: 0; bottom: 0; height: 8%; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: calc(24px + var(--safe-top)) calc(24px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(24px + var(--safe-left));
  text-align: center;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(4px);
}
.overlay h1 { font-size: 30px; }
.overlay p { max-width: 320px; }
.overlay .big { font-size: 24px; font-weight: 700; }
.overlay button {
  margin-top: 8px;
  border: 0;
  border-radius: 14px;
  padding: 14px 40px;
  font-size: 18px;
  font-weight: 700;
  background: var(--accent);
  color: var(--accent-text);
}
.hidden { display: none !important; }

.overlay button.ghost { margin-top: 0; background: transparent; color: var(--text); font-weight: 600; }

#top, #shop, #pet { justify-content: flex-start; padding-top: calc(32px + var(--safe-top)); background: var(--bg); }
.board {
  list-style: none;
  padding: 0;
  width: 100%;
  max-width: 360px;
  flex: 1;
  overflow-y: auto;
}
.board li {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  text-align: left;
}
.board li:nth-child(odd) { background: #f5f5f7; }
.board li.me { background: #ffe9e9; font-weight: 700; }
.board .place { color: var(--hint); font-variant-numeric: tabular-nums; }
.board .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.board .empty { display: block; text-align: center; color: var(--hint); background: none; }
.mine { color: var(--hint); font-size: 14px; }

.overlay .row { display: flex; gap: 4px; }
.comb img { display: block; width: 100%; max-height: 120px; object-fit: contain; object-position: center bottom; }

.shop { list-style: none; padding: 0; width: 100%; max-width: 380px; flex: 1; overflow-y: auto; overflow-x: hidden; display: flex; flex-direction: column; gap: 10px; }
.shop li {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 16px;
  background: #f5f5f7;
  text-align: left;
}
.shop li.current { background: #ffe9e9; }
.shop .pic { width: 72px; height: 56px; object-fit: contain; display: block; }
.shop .title { font-weight: 700; }
.shop .power { color: var(--hint); font-size: 13px; }
.overlay .shop button { margin: 0; padding: 10px 10px; font-size: 14px; border-radius: 12px; white-space: nowrap; }
.overlay .shop button:disabled { background: #d8d8dc; color: #8e8e93; }
.overlay .shop button.owned { background: transparent; color: var(--text); border: 1.5px solid #d8d8dc; }

.side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.coins { font-size: 16px; }
.hud .row { display: flex; gap: 6px; }
.icon { border: 0; width: 40px; height: 40px; border-radius: 12px; background: #f5f5f7; font-size: 20px; }

.tip {
  position: absolute;
  left: 0;
  right: 0;
  top: 1%;
  text-align: center;
  color: var(--hint);
  font-size: 14px;
  pointer-events: none;
  transition: opacity .4s;
}
.tip.gone { opacity: 0; }

.toast {
  position: absolute;
  left: 50%;
  top: 64%;
  transform: translate(-50%, -50%) scale(.6);
  opacity: 0;
  padding: 14px 22px;
  border-radius: 18px;
  background: rgba(43, 33, 24, .88);
  color: #fff;
  font-size: 22px;
  font-weight: 800;
  text-align: center;
  pointer-events: none;
  transition: opacity .15s, transform .2s cubic-bezier(.3, 1.6, .5, 1);
  z-index: 8;
}
.toast small { display: block; font-size: 13px; font-weight: 500; opacity: .8; white-space: nowrap; }
.toast.on { opacity: 1; transform: translate(-50%, -50%) scale(1); }

#pet { overflow-y: auto; }
.best #petName:not(:empty) { font-weight: 700; color: var(--text); }
.best #petName:not(:empty)::after { content: ' · '; color: var(--hint); font-weight: 400; }
.field-label { align-self: stretch; max-width: 380px; width: 100%; margin: 0 auto; text-align: left; color: var(--hint); font-size: 13px; }
.name-row { display: flex; gap: 8px; width: 100%; max-width: 380px; }
.name-row input {
  flex: 1;
  min-width: 0;
  border: 1.5px solid #d8d8dc;
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  color: var(--text);
  background: #fff;
  user-select: text;
  -webkit-user-select: text;
}
.overlay .name-row button { margin: 0; padding: 0 18px; font-size: 16px; }
.slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: 100%; max-width: 380px; }
.slot { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 6px; text-align: center; border-radius: 16px; background: #f5f5f7; font-size: 14px; }
.slot small { color: var(--hint); font-size: 12px; }
.preview { width: 100%; aspect-ratio: 1; display: grid; place-items: center; }
.preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.overlay .slot .upload { margin: auto 0 0; border: 0; padding: 8px 12px; border-radius: 10px; background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 14px; }
.overlay .slot .reset { margin: 0; padding: 4px 6px; font-size: 12px; font-weight: 500; color: var(--hint); }
.slot.busy { opacity: .5; pointer-events: none; }

.dog.custom .head { left: 21%; top: -4%; }
.field.look .dog:not(.custom) .body {
  -webkit-mask-image: radial-gradient(ellipse 18% 10% at 90% 21%, transparent 80%, #000 100%);
  mask-image: radial-gradient(ellipse 18% 10% at 90% 21%, transparent 80%, #000 100%);
}

.dog .caught {
  position: absolute;
  left: 30%;
  top: -5%;
  width: 72%;
  opacity: 0;
  pointer-events: none;
  transform-origin: 67% 70%;
  z-index: 7;
}
.field.caught .dog { z-index: 7; }
.field.caught .dog .head { opacity: 0; transition: none; }
.field.caught .dog .caught { opacity: 1; animation: swallow 2.2s ease-in forwards; }
.dog.custom .caught { left: 16%; top: -4%; transform-origin: 50% 70%; }
@keyframes swallow {
  0% { transform: scale(1); opacity: 1; }
  60% { transform: scale(5); opacity: 1; }
  85% { transform: scale(5); opacity: 1; }
  100% { transform: scale(5); opacity: 0; }
}

.field.caught .alert { transform: translateX(-50%) scale(0); }
.hud { position: relative; z-index: 1; }
.field.caught { overflow: visible; z-index: 20; }
.field.caught .dog:not(.custom) .body { -webkit-mask-image: none; mask-image: none; }
.credits { color: var(--hint); font-size: 11px; max-width: 360px; }
.credits a { color: inherit; }

.tabs { display: flex; gap: 6px; padding: 4px; border-radius: 14px; background: #f5f5f7; width: 100%; max-width: 380px; }
.overlay .tabs .tab { flex: 1; margin: 0; padding: 9px 6px; font-size: 14px; border-radius: 10px; background: transparent; color: var(--text); font-weight: 600; }
.overlay .tabs .tab.active { background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.shop .power.on { color: #2f9e44; }

.decor { position: absolute; inset: 0; pointer-events: none; }
.decor.back { z-index: 1; }
.dog { z-index: 2; }
.decor.front { z-index: 3; }
.decor img { position: absolute; display: block; height: auto; filter: drop-shadow(0 4px 4px rgba(0, 0, 0, .18)); }
.it-bowl { left: 1%; bottom: 1%; width: 24%; }
.it-bone { left: 24%; bottom: 0; width: 20%; transform: rotate(-8deg); }
.it-ball { left: 45%; bottom: 1%; width: 11%; }
.it-owl { right: 22%; bottom: 1%; width: 17%; }
.it-teddy { right: 0; bottom: 1%; width: 22%; }
.dog .it-bowtie { position: absolute; left: 47%; top: 30%; width: 34%; transform: rotate(-14deg); pointer-events: none; filter: none; }
.dog.custom .it-bowtie { left: 33%; top: 42%; transform: none; }
.slot .local { font-size: 11px; color: var(--hint); text-decoration: underline; }

.rescue {
  position: absolute;
  left: 50%;
  bottom: 14%;
  transform: translateX(-50%);
  z-index: 25;
  border: 0;
  border-radius: 16px;
  padding: 14px 22px;
  font-size: 17px;
  font-weight: 800;
  color: #3b2a00;
  background: linear-gradient(135deg, #ffe27a, #f5b700);
  box-shadow: 0 6px 18px rgba(245, 183, 0, .45);
  white-space: nowrap;
  animation: rescue-pulse 1s ease-in-out infinite;
}
.rescue small { display: block; font-size: 12px; font-weight: 600; opacity: .75; }
@keyframes rescue-pulse { 50% { transform: translateX(-50%) scale(1.05); } }
.overlay .shop .stars-btn { background: linear-gradient(135deg, #ffe27a, #f5b700); color: #3b2a00; }
.shop .emoji-pic { width: 72px; height: 56px; display: grid; place-items: center; font-size: 34px; }
.shop .badge { display: inline-block; margin-left: 4px; padding: 1px 6px; border-radius: 6px; background: #ffe9a8; color: #7a5600; font-size: 11px; font-weight: 700; }

.heart, .spark { position: absolute; line-height: 1; pointer-events: none; z-index: 4; }
.heart { filter: drop-shadow(0 0 6px rgba(255, 60, 120, .55)); }
.heart.big { z-index: 6; filter: drop-shadow(0 0 14px rgba(255, 60, 120, .8)); }
.heart-ring { position: absolute; width: 40px; height: 40px; border-radius: 50%; border: 4px solid rgba(255, 90, 150, .8); pointer-events: none; z-index: 5; }
.spark { z-index: 6; filter: drop-shadow(0 0 4px rgba(255, 210, 60, .9)); }
