:root {
  --pink: #ff4d8d;
  --pink-deep: #c2185b;
  --magenta: #7b1fa2;
  --bg: #f54e8a;
  --cream: #fff6fb;
  --ink: #4a154b;
  --gold: #ffd54a;
  --board: #5b2c6f;
  --cell: 44px;
  --gap: 3px;
  --frame-pad: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "Nunito", "Fredoka", sans-serif;
  background: radial-gradient(circle at 20% 10%, #ff8ab8 0%, var(--bg) 42%, #c43b8c 100%);
  color: var(--cream);
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  padding: 0;
}

body[data-screen="play"] {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body[data-screen="map"] {
  height: 100%;
  overflow: hidden;
}

button {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.bg-blobs span {
  position: fixed;
  border-radius: 50%;
  filter: blur(2px);
  opacity: 0.28;
  pointer-events: none;
  z-index: 0;
  animation: drift 16s ease-in-out infinite;
}
.bg-blobs span:nth-child(1) {
  width: 220px;
  height: 220px;
  background: #ffd54a;
  top: -40px;
  left: -30px;
}
.bg-blobs span:nth-child(2) {
  width: 180px;
  height: 180px;
  background: #7c4dff;
  right: -40px;
  top: 20%;
  animation-delay: -4s;
}
.bg-blobs span:nth-child(3) {
  width: 260px;
  height: 260px;
  background: #69f0ae;
  bottom: -80px;
  left: 20%;
  animation-delay: -8s;
}
.bg-blobs span:nth-child(4) {
  width: 140px;
  height: 140px;
  background: #40c4ff;
  bottom: 10%;
  right: 8%;
  animation-delay: -2s;
}

@keyframes drift {
  50% {
    transform: translateY(-18px);
  }
}

.screen {
  display: none;
  position: relative;
  z-index: 1;
  width: min(100%, 520px);
  margin: 0 auto;
  padding: calc(20px + var(--safe-top)) max(16px, var(--safe-right)) calc(24px + var(--safe-bottom))
    max(16px, var(--safe-left));
}
.screen.active {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
}

.brand {
  text-align: center;
  margin-top: 24px;
}
.logo-candy {
  width: 78px;
  height: 78px;
  margin: 0 auto 10px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 28%, #fff, #ff8aae 38%, #ff2e7a);
  box-shadow: 0 10px 0 #c2185b, 0 18px 30px rgba(80, 0, 40, 0.35);
}
h1 {
  font-family: "Fredoka", sans-serif;
  font-size: 48px;
  margin: 0;
  letter-spacing: 0.5px;
  text-shadow: 0 6px 0 #9c1458;
}
.tagline {
  margin: 8px 0 0;
  font-size: 18px;
  opacity: 0.95;
}

.btn-sound {
  position: fixed;
  top: calc(10px + var(--safe-top));
  right: calc(12px + var(--safe-right));
  left: auto;
  z-index: 12;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.28);
  color: #fff;
  font-size: 20px;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.25);
}
body[data-screen="play"] .btn-sound {
  display: none;
}
.btn-sound-play {
  flex: 0 0 48px;
  width: 48px;
  min-height: 48px;
  padding: 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  font-size: 22px;
  font-weight: 800;
}

.hero-candies {
  position: relative;
  height: 96px;
  margin: 12px auto 4px;
  width: min(100%, 360px);
  overflow: hidden;
}
.float-candy {
  position: absolute;
  width: 42px;
  height: 42px;
  animation: bob 3.2s ease-in-out infinite;
}
.float-candy.c0 {
  left: 4%;
  top: 28px;
  background: radial-gradient(circle at 30% 30%, #fff, #ff5d73);
  border-radius: 50%;
}
.float-candy.c1 {
  left: 21%;
  top: 8px;
  background: radial-gradient(circle at 30% 30%, #fff, #ff9f1c);
  border-radius: 28%;
  animation-delay: -0.4s;
}
.float-candy.c2 {
  left: 38%;
  top: 34px;
  background: radial-gradient(circle at 30% 30%, #fff, #ffd000);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  animation-delay: -0.8s;
}
.float-candy.c3 {
  left: 55%;
  top: 6px;
  background: radial-gradient(circle at 30% 30%, #fff, #2ecc71);
  border-radius: 18%;
  animation-delay: -1.2s;
}
.float-candy.c4 {
  left: 71%;
  top: 30px;
  background: radial-gradient(circle at 30% 30%, #fff, #3498db);
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  animation-delay: -1.6s;
}
.float-candy.c5 {
  left: 86%;
  top: 10px;
  width: 40px;
  height: 40px;
  background: radial-gradient(circle at 30% 30%, #fff, #9b59b6);
  border-radius: 40% 60% 50% 50%;
  animation-delay: -2s;
}

@keyframes bob {
  50% {
    transform: translateY(-10px) rotate(-6deg);
  }
}

.btn-play,
.btn-primary,
.btn-secondary,
.btn-ghost {
  font-family: inherit;
  cursor: pointer;
  border: none;
}
.btn-play,
.btn-primary {
  display: block;
  width: min(320px, 86%);
  margin: 12px auto 28px;
  padding: 16px 20px;
  min-height: 52px;
  border-radius: 999px;
  background: linear-gradient(#ffe36b, #ffb300);
  color: #6a3a00;
  font-weight: 900;
  font-size: 22px;
  box-shadow: 0 8px 0 #d68900, 0 14px 24px rgba(0, 0, 0, 0.22);
}
.btn-play:active,
.btn-primary:active {
  transform: translateY(4px);
  box-shadow: 0 4px 0 #d68900;
}

.features {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 12px;
}
.features li {
  background: rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(8px);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.18);
}
.features strong {
  display: block;
  font-size: 16px;
}
.features span {
  font-size: 14px;
  opacity: 0.9;
}

.topbar,
.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.topbar h2 {
  flex: 1;
  margin: 0;
  text-align: center;
  font-family: "Fredoka", sans-serif;
}
.btn-ghost {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border-radius: 999px;
  padding: 10px 14px;
  min-height: 44px;
  min-width: 44px;
  font-weight: 800;
}
.stars-total {
  font-weight: 900;
  color: var(--gold);
}

#home.screen.active {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}
#map.screen.active {
  height: 100vh;
  height: 100dvh;
  height: -webkit-fill-available;
  max-height: 100dvh;
  min-height: 0;
  overflow: hidden;
}
#map .topbar {
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 2;
}
.path {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 12px 8px calc(36px + var(--safe-bottom));
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
}
.path .level-btn {
  touch-action: pan-y;
}
.level-node {
  display: flex;
  align-items: center;
  gap: 12px;
}
.level-node:nth-child(odd) {
  padding-left: 12%;
}
.level-node:nth-child(even) {
  padding-right: 12%;
  flex-direction: row-reverse;
}
.level-btn {
  width: 64px;
  height: 64px;
  min-width: 64px;
  border-radius: 50%;
  border: 4px solid #fff;
  font-family: "Fredoka", sans-serif;
  font-size: 22px;
  font-weight: 700;
  color: white;
  background: linear-gradient(#7c4dff, #5e35b1);
  box-shadow: 0 6px 0 #4527a0;
  cursor: pointer;
}
.level-btn.locked {
  background: #9e9e9e;
  box-shadow: 0 6px 0 #616161;
  cursor: not-allowed;
  opacity: 0.8;
}
.level-btn.done {
  background: linear-gradient(#69f0ae, #00c853);
  box-shadow: 0 6px 0 #1b5e20;
}
.level-meta {
  font-size: 14px;
}
.level-stars {
  color: var(--gold);
  letter-spacing: 2px;
}

#play.screen.active {
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  height: -webkit-fill-available;
}
.hud {
  flex-shrink: 0;
}
.hud-card {
  flex: 1;
  min-width: 0;
  background: rgba(75, 20, 90, 0.45);
  border-radius: 16px;
  padding: 8px 6px;
  text-align: center;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.15);
}
.hud-label {
  display: block;
  font-size: 11px;
  letter-spacing: 1px;
  opacity: 0.8;
}
.hud-card strong {
  font-size: 20px;
}
.score-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  min-height: 28px;
  flex-shrink: 0;
}
.score strong {
  font-size: 22px;
}
.combo {
  font-family: "Fredoka", sans-serif;
  font-size: 22px;
  color: var(--gold);
  text-shadow: 0 2px 0 #9c1458;
  animation: pop-in 0.35s ease;
}
.score.pulse strong {
  animation: score-pop 0.35s ease;
}
@keyframes pop-in {
  from {
    transform: scale(0.4);
    opacity: 0;
  }
}
@keyframes score-pop {
  50% {
    transform: scale(1.28);
    color: #fff6a5;
  }
}

.board-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1 1 auto;
  min-height: 0;
}
.board-frame {
  position: relative;
  padding: var(--frame-pad);
  background: linear-gradient(180deg, #8e44ad, #5b2c6f);
  border-radius: 24px;
  box-shadow: 0 12px 0 #3d1452, 0 20px 40px rgba(0, 0, 0, 0.28);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.board-frame.shake {
  animation: board-shake 0.38s cubic-bezier(.36,.07,.19,.97);
}
.board-frame.flash::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 24px;
  background: radial-gradient(circle at center, rgba(255, 255, 255, 0.55), transparent 68%);
  pointer-events: none;
  animation: board-flash 0.32s ease-out;
  z-index: 5;
}
.board-frame.boom::after {
  background: radial-gradient(circle at center, rgba(255, 214, 74, 0.65), rgba(255, 77, 141, 0.2) 46%, transparent 70%);
}
@keyframes board-shake {
  10%,
  90% {
    transform: translateX(-3px) rotate(-0.4deg);
  }
  20%,
  80% {
    transform: translateX(4px);
  }
  30%,
  70% {
    transform: translateX(-5px) rotate(0.5deg);
  }
  40%,
  60% {
    transform: translateX(5px);
  }
}
@keyframes board-flash {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
.board {
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  grid-template-rows: repeat(8, var(--cell));
  gap: var(--gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.fx,
.float-layer {
  position: absolute;
  inset: var(--frame-pad);
  width: calc(100% - 2 * var(--frame-pad));
  height: calc(100% - 2 * var(--frame-pad));
  pointer-events: none;
  z-index: 4;
}
.float-layer {
  z-index: 6;
  overflow: visible;
}
.cell {
  position: relative;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.16);
}
.candy {
  position: absolute;
  inset: 4px;
  display: grid;
  place-items: center;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.2));
  will-change: transform;
}
.candy.selected {
  transform: scale(1.14);
  z-index: 2;
  filter: drop-shadow(0 0 10px rgba(255, 255, 180, 0.95)) drop-shadow(0 4px 0 rgba(0, 0, 0, 0.2));
}
.candy.hint {
  animation: hint-pulse 0.9s ease infinite;
}
.candy.pop {
  animation: crush 0.32s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
  z-index: 3;
}
.candy.spawn {
  animation: spawn 0.34s cubic-bezier(0.2, 1.4, 0.3, 1) both;
}
.candy.born-special {
  animation: special-birth 0.45s ease;
}

@keyframes crush {
  0% {
    transform: scale(1) rotate(0deg);
  }
  35% {
    transform: scale(1.28) rotate(8deg);
    filter: brightness(1.85) saturate(1.4);
  }
  100% {
    transform: scale(0) rotate(-28deg);
    opacity: 0;
    filter: brightness(2);
  }
}
@keyframes spawn {
  from {
    transform: scale(0.15) translateY(-28px);
    opacity: 0;
  }
}
@keyframes hint-pulse {
  50% {
    transform: scale(1.18);
    filter: drop-shadow(0 0 8px #fff8);
  }
}
@keyframes special-birth {
  0% {
    transform: scale(0.2);
  }
  55% {
    transform: scale(1.25);
    filter: drop-shadow(0 0 16px #fff);
  }
  100% {
    transform: scale(1);
  }
}

.piece {
  position: relative;
  width: 78%;
  height: 78%;
}
.piece::before {
  content: "";
  position: absolute;
  width: 38%;
  height: 18%;
  left: 18%;
  top: 14%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
  pointer-events: none;
}
.piece.t0 {
  border-radius: 50%;
  background: radial-gradient(circle at 30% 28%, #fff3f6, #ff4d6d 55%, #c9184a);
}
.piece.t1 {
  border-radius: 28%;
  background: radial-gradient(circle at 30% 28%, #fff6e8, #ff9f1c 55%, #e65100);
}
.piece.t2 {
  background: radial-gradient(circle at 30% 28%, #fffde7, #ffd000 55%, #f9a825);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.piece.t3 {
  border-radius: 18%;
  background: radial-gradient(circle at 30% 28%, #e8fff2, #2ecc71 55%, #1b8a48);
}
.piece.t4 {
  background: radial-gradient(circle at 30% 28%, #e3f2fd, #3498db 55%, #1565c0);
  clip-path: polygon(50% 4%, 96% 50%, 50% 96%, 4% 50%);
}
.piece.t5 {
  border-radius: 42% 58% 50% 50%;
  background: radial-gradient(circle at 30% 28%, #f3e5f5, #9b59b6 55%, #6a1b9a);
}

.sp-stripes,
.sp-wrap,
.sp-bomb {
  position: absolute;
  inset: 8%;
  pointer-events: none;
  border-radius: inherit;
}
.sp-stripes.h {
  background: repeating-linear-gradient(
    90deg,
    transparent 0 28%,
    rgba(255, 255, 255, 0.72) 28% 42%
  );
  animation: stripe-slide 0.8s linear infinite;
  mix-blend-mode: overlay;
  border-radius: 20%;
}
.sp-stripes.v {
  background: repeating-linear-gradient(
    180deg,
    transparent 0 28%,
    rgba(255, 255, 255, 0.72) 28% 42%
  );
  animation: stripe-slide-v 0.8s linear infinite;
  mix-blend-mode: overlay;
  border-radius: 20%;
}
.sp-wrap {
  border: 3px solid rgba(255, 255, 255, 0.9);
  border-radius: 30%;
  box-shadow: 0 0 10px #fff, inset 0 0 8px rgba(255, 214, 74, 0.8);
  animation: wrap-pulse 0.9s ease-in-out infinite;
}
.sp-bomb {
  border-radius: 50%;
  background: conic-gradient(#ff4d6d, #ffd000, #2ecc71, #3498db, #9b59b6, #ff4d6d);
  opacity: 0.55;
  mix-blend-mode: screen;
  animation: bomb-spin 1.4s linear infinite;
  filter: blur(0.2px);
}
@keyframes stripe-slide {
  to {
    background-position: 18px 0;
  }
}
@keyframes stripe-slide-v {
  to {
    background-position: 0 18px;
  }
}
@keyframes wrap-pulse {
  50% {
    transform: scale(1.12);
    opacity: 0.75;
  }
}
@keyframes bomb-spin {
  to {
    transform: rotate(360deg);
  }
}

.floater {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, -50%);
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 2px 0 #9c1458, 0 0 12px rgba(255, 213, 74, 0.9);
  animation: float-up 0.85s ease-out forwards;
  white-space: nowrap;
  pointer-events: none;
}
.floater.score-fx {
  font-size: 18px;
  color: #fff6a5;
}
.floater.combo-fx {
  font-size: 28px;
  color: #ffe36b;
  z-index: 8;
}
.floater.special-fx {
  font-size: 20px;
  color: #fff;
}
@keyframes float-up {
  0% {
    opacity: 0;
    transform: translate(-50%, -30%) scale(0.6);
  }
  18% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.12);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -130%) scale(1);
  }
}

.play-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  flex-shrink: 0;
}
.btn-secondary {
  flex: 1;
  padding: 12px;
  min-height: 48px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  font-weight: 800;
  font-size: 16px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(40, 0, 40, 0.55);
  display: grid;
  place-items: center;
  padding: max(20px, var(--safe-top)) max(20px, var(--safe-right)) max(20px, var(--safe-bottom))
    max(20px, var(--safe-left));
}
.modal[hidden] {
  display: none;
}
.modal-card {
  width: min(360px, 100%);
  background: linear-gradient(180deg, #fff7fb, #ffd6e8);
  color: var(--ink);
  border-radius: 24px;
  padding: 24px 20px 18px;
  text-align: center;
  box-shadow: 0 16px 0 #c2185b;
}
.modal-card h3 {
  margin: 0 0 8px;
  font-family: "Fredoka", sans-serif;
  font-size: 28px;
}
.modal-card p {
  margin: 0 0 12px;
}
.modal-stars {
  font-size: 28px;
  color: #f4b400;
  min-height: 36px;
  letter-spacing: 6px;
}
.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.modal-actions .btn-primary {
  width: 100%;
  margin: 0;
  font-size: 18px;
}
.modal-actions .btn-ghost {
  background: #f8bbd0;
  color: var(--ink);
}

@media (max-width: 480px) {
  h1 {
    font-size: clamp(32px, 10vw, 40px);
  }
  .tagline {
    font-size: 16px;
    padding: 0 8px;
  }
  .hero-candies {
    height: 92px;
  }
  .brand {
    margin-top: 8px;
  }
  .hud-label {
    font-size: 10px;
  }
  .hud-card strong {
    font-size: 18px;
    word-break: break-word;
  }
  .level-btn {
    width: 60px;
    height: 60px;
    min-width: 60px;
  }
}

@media (max-height: 740px) {
  .hero-candies {
    height: 80px;
  }
  .features {
    gap: 8px;
  }
  .features li {
    padding: 10px 12px;
  }
  .btn-play {
    margin-bottom: 16px;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  #play.screen.active {
    padding-top: max(8px, var(--safe-top));
    padding-bottom: max(8px, var(--safe-bottom));
  }
  .hud,
  .score-row {
    margin-bottom: 4px;
  }
  .play-actions {
    margin-top: 8px;
  }
  .btn-secondary {
    min-height: 40px;
    padding: 8px;
  }
}
