:root {
  --bg: #fff4e0;
  --bg2: #ffe0b5;
  --ink: #4a2e12;
  --muted: #8a6238;
  --accent: #c4782a;
  --accent2: #e8a04a;
  --good: #3d9a5f;
  --soft: #ff8fab;
  --card: #fffaf2;
  --shadow: rgba(74, 46, 18, 0.18);
  --tile: #fff8ee;
  --radius: 18px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; min-height: 100%;
  font-family: var(--font); color: var(--ink);
  background: radial-gradient(120% 80% at 50% 0%, #ffe9c4 0%, var(--bg) 55%, #ffd9a8 100%);
  overscroll-behavior: none;
  touch-action: manipulation;
}
body { display: flex; justify-content: center; }

#fx-layer {
  position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden;
}

#app {
  width: 100%;
  max-width: 430px;
  min-height: 100dvh;
  padding: 12px 12px calc(16px + var(--safe-b));
  position: relative;
}

.screen { display: none; animation: fadeIn 0.35s ease; }
.screen.active { display: block; }
#screen-game.screen.active {
  display: flex;
  flex-direction: column;
  min-height: calc(100dvh - 12px);
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.hero { text-align: center; padding: 12px 8px 8px; }
.hero-dog {
  filter: drop-shadow(0 8px 14px var(--shadow));
  border-radius: 16px;
}
h1 {
  margin: 8px 0 4px;
  font-size: 2.1rem;
  letter-spacing: -0.02em;
  background: linear-gradient(120deg, #a85a18, #e08a30 50%, #c4782a);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tagline { margin: 0; font-weight: 700; font-size: 1.05rem; }
.tagline-sub { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; }
.hint { text-align: center; color: var(--muted); font-size: 0.82rem; margin-top: 14px; }

.level-list {
  display: grid; gap: 10px; margin-top: 14px;
  max-height: 55dvh; overflow: auto; padding: 2px;
}
.level-btn {
  display: flex; gap: 12px; align-items: center;
  width: 100%; text-align: left; border: none; cursor: pointer;
  background: var(--card); border-radius: 16px; padding: 12px 14px;
  box-shadow: 0 4px 0 #e8c89a, 0 8px 18px var(--shadow);
  transition: transform 0.12s ease;
}
.level-btn:active { transform: translateY(2px) scale(0.99); }
.level-num {
  flex: 0 0 42px; height: 42px; border-radius: 12px;
  display: grid; place-items: center; font-weight: 800; color: #fff;
  background: linear-gradient(145deg, var(--accent2), var(--accent));
  box-shadow: inset 0 -2px 0 rgba(0,0,0,0.12);
}
.level-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.level-meta strong { font-size: 1rem; }
.level-meta span { color: var(--muted); font-size: 0.78rem; line-height: 1.25; }

.hud {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
}
.btn-icon {
  border: none; background: var(--card); width: 40px; height: 40px;
  border-radius: 12px; font-size: 1.2rem; cursor: pointer;
  box-shadow: 0 3px 0 #e8c89a;
}
.hud-stats {
  flex: 1; display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
  background: var(--card); border-radius: 14px; padding: 8px 6px;
  box-shadow: 0 3px 0 #e8c89a;
}
.stat { text-align: center; }
.stat-label { display: block; font-size: 0.65rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-weight: 800; font-size: 0.95rem; }

.progress-wrap {
  height: 10px; background: #f0d7b0; border-radius: 999px; overflow: hidden; margin-bottom: 8px;
}
.progress-bar {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #7bc47f, #f7c948, #ff8fab);
  transition: width 0.35s ease;
  border-radius: 999px;
}

.special-bar {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
  background: #fff0f5; border: 2px dashed #ff8fab; border-radius: 14px; padding: 8px;
}
.special-bar.hidden { display: none; }
.special-hint { font-size: 0.75rem; color: var(--muted); }
.btn.special {
  border: none; background: linear-gradient(145deg, #ffb3c6, #ff8fab);
  color: #5a2035; font-weight: 800; border-radius: 12px; padding: 8px 12px; cursor: pointer;
}
.btn.special:disabled { opacity: 0.45; cursor: default; }
.btn.special.armed { animation: pulsePaw 0.9s ease infinite; }
@keyframes pulsePaw {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255,143,171,0.5); }
  50% { transform: scale(1.04); box-shadow: 0 0 0 8px rgba(255,143,171,0); }
}

.board-shell {
  position: relative;
  background: linear-gradient(180deg, #f6d7a8, #efc48a);
  border-radius: 20px;
  padding: 8px;
  box-shadow: inset 0 2px 8px rgba(255,255,255,0.45), 0 10px 24px var(--shadow);
  width: 100%;
}
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 6), 1fr);
  gap: 4px;
  width: 100%;
  aspect-ratio: 1;
  background: #d9a86a;
  border-radius: 14px;
  padding: 8px;
  touch-action: none;
}

/* Phone / PWA: board true edge-to-edge — kill frame margins */
@media (max-width: 900px) {
  #app {
    max-width: none;
    width: 100%;
    padding-left: 0;
    padding-right: 0;
    padding-top: 6px;
  }
  #screen-game .hud,
  #screen-game .progress-wrap,
  #screen-game .special-bar,
  #screen-game .goal-hint,
  #screen-game .fair-badge {
    margin-left: max(8px, env(safe-area-inset-left, 0px));
    margin-right: max(8px, env(safe-area-inset-right, 0px));
  }
  #screen-game .board-shell {
    width: 100vw;
    max-width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
    border-radius: 0;
    padding: 0;
    box-shadow: none;
    background: #d9a86a;
  }
  #screen-game .board {
    width: 100%;
    border-radius: 0;
    padding: 0;
    gap: 2px;
    background: #c8965a;
  }
  #screen-game .juice {
    border-radius: 0;
  }
  #screen-game .tile {
    border-radius: 6px;
  }
  #screen-game .tile-img {
    border-radius: 6px;
  }
}
.juice { position: absolute; inset: 0; pointer-events: none; overflow: hidden; border-radius: 20px; }

.tile {
  position: relative;
  border: none;
  border-radius: 12px;
  background: var(--tile);
  padding: 0;
  aspect-ratio: 1;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0,0,0,0.08);
  overflow: hidden;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.2s ease;
  will-change: transform;
}
.tile.empty { background: transparent; box-shadow: none; cursor: default; }
.tile.selected {
  outline: 3px solid #fff;
  box-shadow: 0 0 0 3px var(--accent), 0 6px 14px rgba(196,120,42,0.45);
  transform: scale(1.06);
  z-index: 2;
}
.tile.matched {
  animation: popMatch 0.32s ease forwards;
}
.tile.falling, .tile.spawning {
  transition: transform var(--fall-ms, 320ms) cubic-bezier(0.22, 1, 0.36, 1);
}
.tile.swapping {
  transition: transform var(--swap-ms, 220ms) cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 4;
  pointer-events: none;
}
/* Distinct tile backplates — easier to tell pup types apart */
/* Light blonde → dark gold coats */
.tile-type-0 { background: linear-gradient(160deg, #fff6dd, #f3e0a8); box-shadow: 0 2px 0 #e0c878; }
.tile-type-1 { background: linear-gradient(160deg, #ffe9b8, #efcf7a); box-shadow: 0 2px 0 #d4b45c; }
.tile-type-2 { background: linear-gradient(160deg, #f6d48a, #e0a84a); box-shadow: 0 2px 0 #c4842e; }
.tile-type-3 { background: linear-gradient(160deg, #e8b85a, #d4922e); box-shadow: 0 2px 0 #b07418; }
.tile-type-4 { background: linear-gradient(160deg, #d4923a, #b86a18); box-shadow: 0 2px 0 #8f5210; }
.tile-type-5 { background: linear-gradient(160deg, #b87428, #8a4e10); box-shadow: 0 2px 0 #6a3a0c; }
.tile-img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  margin: 0; pointer-events: none; border-radius: 12px;
}
.tile-type-0 .tile-img { filter: brightness(1.06) saturate(1.0); }
.tile-type-1 .tile-img { filter: brightness(1.03) saturate(1.02); }
.tile-type-2 .tile-img { filter: brightness(1.0) saturate(1.05); }
.tile-type-3 .tile-img { filter: brightness(0.97) saturate(1.08); }
.tile-type-4 .tile-img { filter: brightness(0.93) saturate(1.1); }
.tile-type-5 .tile-img { filter: brightness(0.88) saturate(1.12); }
.tile.iced .tile-img { filter: brightness(0.92) saturate(0.85); }
.ice-overlay {
  position: absolute; inset: 0; border-radius: 12px;
  background: linear-gradient(145deg, rgba(180,230,255,0.55), rgba(120,190,255,0.35));
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.55);
  pointer-events: none;
}
.tile.crack-flash .ice-overlay { animation: crackIce 0.35s ease forwards; }
@keyframes crackIce {
  to { opacity: 0; transform: scale(1.15); }
}
@keyframes popMatch {
  0% { transform: scale(1); }
  40% { transform: scale(1.18) rotate(-4deg); }
  100% { transform: scale(0); opacity: 0; }
}

.goal-hint {
  text-align: center; margin: 10px 0 4px; color: var(--ink); font-weight: 600; font-size: 0.9rem;
}
.fair-badge {
  text-align: center; margin: 0; color: var(--muted); font-size: 0.72rem;
}

.result-card {
  margin-top: 40px; background: var(--card); border-radius: 24px; padding: 24px 18px;
  text-align: center; box-shadow: 0 12px 30px var(--shadow); position: relative; overflow: hidden;
}
.result-card.win { border: 3px solid #7bc47f; }
.result-card.lose { border: 3px solid #e0b08a; }
.result-card.nearmiss { border: 3px solid #ff8fab; background: linear-gradient(180deg, #fff7fb, #fffaf2); }
.result-dog { filter: drop-shadow(0 6px 10px var(--shadow)); }
.result-msg { color: var(--muted); margin: 8px 0; }
.result-score { font-weight: 800; margin: 0 0 12px; }
.soft-note {
  background: #fff0f5; color: #8a3a55; border-radius: 12px; padding: 8px 10px;
  font-size: 0.82rem; font-weight: 600; margin: 0 0 14px;
}
.result-actions { display: grid; gap: 8px; }

.btn {
  border: none; border-radius: 14px; padding: 12px 14px; font-weight: 800;
  font-size: 1rem; cursor: pointer; transition: transform 0.1s ease;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: linear-gradient(145deg, var(--accent2), var(--accent)); color: #fff; box-shadow: 0 4px 0 #9a5a1c; }
.btn.secondary { background: #ffe0b8; color: var(--ink); box-shadow: 0 3px 0 #e0bd88; }
.btn.ghost { background: transparent; color: var(--muted); box-shadow: none; }

.bounce-soft { animation: bounceSoft 2.4s ease-in-out infinite; }
@keyframes bounceSoft {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}
.wiggle { animation: wiggle 0.7s ease infinite; }
@keyframes wiggle {
  0%, 100% { transform: rotate(-3deg); }
  50% { transform: rotate(3deg); }
}
.nuzzle { animation: nuzzle 1.4s ease-in-out infinite; }
@keyframes nuzzle {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(4px) scale(1.04); }
}
.floaty { animation: floatIn 0.45s cubic-bezier(0.2, 1.2, 0.3, 1); }
@keyframes floatIn {
  from { opacity: 0; transform: translateY(24px) scale(0.94); }
  to { opacity: 1; transform: none; }
}

.sparkle-burst::before, .sparkle-burst::after {
  content: "✨ 💛 🐾 ✨ 🦴 ✨";
  position: absolute; left: 0; right: 0; top: 8px;
  font-size: 1.1rem; opacity: 0.85; animation: sparkleDrift 2.2s linear infinite;
}
.sparkle-burst::after { top: auto; bottom: 8px; animation-direction: reverse; }
@keyframes sparkleDrift {
  from { transform: translateX(-10%); }
  to { transform: translateX(10%); }
}

.fx-heart, .fx-paw, .fx-star {
  position: absolute; font-size: 1.2rem; animation: floatUp 0.9s ease-out forwards;
  pointer-events: none;
}
@keyframes floatUp {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(-60px) scale(1.4); }
}

.cascade-banner {
  position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%);
  background: rgba(255,255,255,0.92); color: var(--accent);
  font-weight: 900; font-size: 1.2rem; padding: 8px 14px; border-radius: 999px;
  box-shadow: 0 8px 20px var(--shadow); animation: cascadePop 0.7s ease forwards;
  z-index: 5; pointer-events: none; white-space: nowrap;
}
@keyframes cascadePop {
  0% { opacity: 0; transform: translate(-50%, -40%) scale(0.6); }
  30% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}

.board.paw-target .tile:not(.empty) { outline: 2px dashed #ff8fab; }

/* —— Special tile badges (distinct from ice) —— */
.special-badge {
  position: absolute;
  right: 2px;
  bottom: 2px;
  z-index: 3;
  font-size: 0.72rem;
  line-height: 1;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  padding: 2px 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  pointer-events: none;
}
.tile.has-special {
  box-shadow: 0 0 0 2px #fff59d, 0 2px 0 rgba(0, 0, 0, 0.12);
}
.tile.special-row { outline: 2px solid rgba(255, 200, 60, 0.85); }
.tile.special-col { outline: 2px solid rgba(100, 180, 255, 0.85); }
.tile.special-wrap { outline: 2px solid rgba(255, 120, 80, 0.9); }
.tile.special-color { outline: 2px solid rgba(180, 100, 255, 0.95); }
/* Ice sits above badge visually but badge stays readable */
.tile.iced .special-badge { z-index: 4; }

/* HUD tick during leftover-move bonus */
.hud-tick {
  animation: hudTick 0.15s ease;
  color: var(--good);
}
@keyframes hudTick {
  0% { transform: scale(1); }
  50% { transform: scale(1.18); }
  100% { transform: scale(1); }
}
.stat-value { display: inline-block; }

/* —— Bigger level-clear celebration —— */
.win-subtitle {
  margin: -4px 0 8px;
  font-weight: 800;
  color: var(--good);
  letter-spacing: 0.02em;
}
.win-stars {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 8px 0 12px;
  min-height: 2.2rem;
}
.win-stars .star {
  font-size: 2rem;
  display: inline-block;
  opacity: 0;
  transform: scale(0.3) translateY(12px);
  animation: starPop 0.45s cubic-bezier(0.2, 1.4, 0.3, 1) forwards;
}
.win-stars .star.empty {
  opacity: 0.35;
  filter: grayscale(1);
}
.win-stars .star.filled {
  filter: drop-shadow(0 2px 6px rgba(255, 180, 40, 0.55));
}
@keyframes starPop {
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.win-bonus {
  margin: 0 0 6px;
  font-weight: 700;
  font-size: 0.92rem;
  color: #a85a18;
  background: #fff3d6;
  border-radius: 12px;
  padding: 8px 10px;
}

.win-confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  border-radius: 24px;
}
.confetti-bit {
  position: absolute;
  top: -10%;
  animation-name: confettiFall;
  animation-timing-function: ease-in;
  animation-fill-mode: forwards;
}
@keyframes confettiFall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(120%) rotate(320deg); opacity: 0; }
}

.result-card.win h2#win-title {
  background: linear-gradient(120deg, #3d9a5f, #c4782a 60%, #e8a04a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: 1.85rem;
  margin: 8px 0 0;
}


/* Bottom yard — fills leftover space under the board */
.bottom-yard {
  position: relative;
  flex: 1 1 auto;
  min-height: 120px;
  margin-top: 6px;
  overflow: hidden;
  border-radius: 16px 16px 0 0;
  background: linear-gradient(180deg, #ffe9c4 0%, #f7d9a0 45%, #e8c078 100%);
}
.yard-sky {
  position: absolute; inset: 0 0 38% 0;
  background: linear-gradient(180deg, #fff6e4, transparent);
}
.yard-ground {
  position: absolute; left: 0; right: 0; bottom: 0; height: 42%;
  background:
    radial-gradient(120% 80% at 50% 0%, #d7b06a 0%, transparent 60%),
    linear-gradient(180deg, #c9964e, #b8833c);
  border-top: 3px solid #e8c078;
}
.yard-cloud {
  position: absolute;
  width: 64px; height: 22px;
  background: rgba(255,255,255,0.75);
  border-radius: 999px;
  filter: blur(0.2px);
  animation: cloudDrift 14s linear infinite;
}
.yard-cloud:before, .yard-cloud:after {
  content: "";
  position: absolute;
  background: inherit;
  border-radius: 50%;
}
.yard-cloud:before { width: 28px; height: 28px; top: -14px; left: 10px; }
.yard-cloud:after { width: 36px; height: 36px; top: -18px; left: 28px; }
.yard-cloud.c1 { top: 12%; left: -20%; animation-duration: 18s; }
.yard-cloud.c2 { top: 22%; left: -30%; animation-duration: 22s; animation-delay: -8s; opacity: 0.7; }

.yard-pup {
  position: absolute;
  bottom: 8%;
  width: clamp(72px, 22vw, 110px);
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 6px 8px rgba(74,46,18,0.25));
  border-radius: 12px;
}
.yard-pup.p1 { left: 4%; animation: pupBounce 2.4s ease-in-out infinite; }
.yard-pup.p2 { left: 38%; animation: pupBounce 2.8s ease-in-out infinite 0.4s; }
.yard-pup.p3 { right: 4%; left: auto; animation: pupWag 3.2s ease-in-out infinite 0.2s; }

.yard-float {
  position: absolute;
  font-size: 1.25rem;
  animation: floatUp 5.5s ease-in-out infinite;
  opacity: 0.85;
}
.yard-float.f1 { left: 18%; bottom: 45%; animation-delay: 0s; }
.yard-float.f2 { left: 48%; bottom: 52%; animation-delay: -1.2s; }
.yard-float.f3 { left: 72%; bottom: 40%; animation-delay: -2.4s; }
.yard-float.f4 { left: 30%; bottom: 60%; animation-delay: -0.8s; font-size: 1rem; }
.yard-float.f5 { left: 60%; bottom: 58%; animation-delay: -3.1s; }

@keyframes cloudDrift {
  from { transform: translateX(0); }
  to { transform: translateX(140vw); }
}
@keyframes pupBounce {
  0%, 100% { transform: translateY(0) rotate(-2deg); }
  50% { transform: translateY(-10px) rotate(2deg); }
}
@keyframes pupWag {
  0%, 100% { transform: translateY(0) rotate(3deg) scaleX(-1); }
  50% { transform: translateY(-8px) rotate(-3deg) scaleX(-1); }
}
@keyframes floatUp {
  0%, 100% { transform: translateY(0) scale(1); opacity: 0.35; }
  40% { opacity: 0.95; }
  50% { transform: translateY(-18px) scale(1.12); opacity: 0.9; }
}

@media (max-width: 900px) {
  .bottom-yard {
    margin-top: 4px;
    border-radius: 0;
    min-height: 140px;
    width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
  }
}
