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

:root {
  --shell: #cfe3f7;
  --bg-1: #bfe0ff; --bg-2: #eaf5ff; --bg-3: #f8fbff; --bg-4: #d7ecff;
  --bar-g1: #57a7ff; --bar-g2: #3d8ef0;
  --pill-bg: #ffffff; --pill-fg: #2f6fd0;
  --badge-bg: rgba(255,255,255,0.92); --badge-fg: #2f6fd0;
  --icon-btn-bg: rgba(255,255,255,0.22); --icon-btn-fg: #ffffff;
  --star-on: #ffd23e; --star-off: rgba(255,255,255,0.35); --star-off-card: #d7dfea;
  --heart-on: #ff5a6e; --heart-off: #b9c6d8;
  --accent: #ffd23e;
  --banner-g1: #58a9ff; --banner-g2: #2f7fe8;
  --banner-fg: #ffffff; --banner-sub: rgba(255,255,255,0.75);
  --card-bg: #ffffff; --card-title: #274b83; --card-text: #5a7089;
  --toast-fg: #2f6fd0; --toast-sh: rgba(255,255,255,0.8);
  --overlay: rgba(25,45,80,0.45);
  --shadow: rgba(30,90,180,0.35);
  --hand: #ffd996;
  --menu-bg1: rgba(191,224,255,0.94); --menu-bg2: rgba(234,245,255,0.94);
  --menu-title: #274b83; --menu-sub: #5a7089;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
  font-family: -apple-system, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  background: var(--shell);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: relative;
  margin: 0 auto;
  max-width: 480px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--bg-1) 0%, var(--bg-2) 34%, var(--bg-3) 60%, var(--bg-4) 100%);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  transition: background 0.4s;
}

/* ---------- icons ---------- */

.icon {
  width: 1em;
  height: 1em;
  display: inline-block;
  vertical-align: middle;
  fill: currentColor;
  flex-shrink: 0;
}

/* ---------- top bar ---------- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: calc(10px + env(safe-area-inset-top)) 14px 0;
  padding: 8px 12px;
  background: linear-gradient(180deg, var(--bar-g1), var(--bar-g2));
  border-radius: 999px;
  box-shadow: 0 4px 12px var(--shadow), inset 0 1px 0 rgba(255,255,255,0.35);
  transition: background 0.4s;
}

.bar-side { display: flex; gap: 8px; }

.icon-btn {
  border: none;
  background: var(--icon-btn-bg);
  border-radius: 50%;
  width: 38px;
  height: 38px;
  cursor: pointer;
  color: var(--icon-btn-fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn .icon { width: 20px; height: 20px; }
.icon-btn:active { transform: scale(0.92); }

#stars { display: flex; gap: 5px; color: var(--star-on); }
#stars .icon { width: 24px; height: 24px; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.3)); transition: color 0.3s, transform 0.3s, filter 0.3s; }
#stars .icon.off { color: var(--star-off); filter: none; transform: scale(0.85); }

#level-badge {
  background: var(--badge-bg);
  color: var(--badge-fg);
  font-weight: 800;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 15px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  white-space: nowrap;
}

/* ---------- sub bar ---------- */

#subbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
}

.pill {
  background: var(--pill-bg);
  color: var(--pill-fg);
  font-weight: 800;
  font-size: 15px;
  padding: 7px 14px;
  border-radius: 999px;
  box-shadow: 0 3px 8px var(--shadow);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.pill .icon { width: 15px; height: 15px; }

#hearts { display: flex; gap: 4px; }
#hearts .heart { color: var(--heart-on); transition: all 0.25s; display: inline-flex; }
#hearts .heart .icon { width: 19px; height: 19px; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.25)); }
#hearts .heart.lost { color: var(--heart-off); transform: scale(0.8); }
#hearts .heart.lost .icon { filter: none; }
#hearts .heart.pop { animation: heartpop 0.45s ease; }
@keyframes heartpop {
  0% { transform: scale(1); }
  40% { transform: scale(1.6) rotate(-12deg); }
  100% { transform: scale(0.8); }
}

/* ---------- stage ---------- */

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  touch-action: none;
  cursor: grab;
}
#stage canvas { position: absolute; inset: 0; display: block; }

/* ---------- bottom banner ---------- */

#bottombar {
  padding: 0 14px calc(12px + env(safe-area-inset-bottom));
}

#btn-banner {
  width: 100%;
  border: none;
  cursor: pointer;
  padding: 14px 16px 12px;
  border-radius: 22px;
  background: linear-gradient(180deg, var(--banner-g1) 0%, var(--banner-g2) 100%);
  box-shadow: 0 6px 16px var(--shadow), inset 0 1px 0 rgba(255,255,255,0.35);
  color: var(--banner-fg);
  text-align: center;
  transition: background 0.4s;
}
#btn-banner:active { transform: scale(0.98); }

#carousel-text {
  display: block;
  font-size: 26px;
  font-weight: 900;
  color: var(--banner-fg);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
  transition: opacity 0.35s, transform 0.35s;
}
#carousel-text.fade { opacity: 0; transform: translateY(8px); }
#carousel-text .accent { color: var(--accent); }

#btn-banner small {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  font-weight: 600;
  color: var(--banner-sub);
}

/* ---------- tutorial hand ---------- */

#hand {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  transform: translate(-38%, -6%);
  animation: handtap 1.1s ease-in-out infinite;
}
#hand .hand-icon {
  width: 46px;
  height: 46px;
  color: var(--hand);
  filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.45));
}
#hand .ripple {
  position: absolute;
  left: 38%;
  top: 6%;
  width: 14px;
  height: 14px;
  border: 3px solid var(--hand);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  animation: ripple 1.1s ease-out infinite;
}
@keyframes ripple {
  0% { opacity: 0.9; scale: 0.5; }
  70% { opacity: 0; scale: 2.6; }
  100% { opacity: 0; scale: 2.6; }
}
@keyframes handtap {
  0%, 100% { margin-top: 0; }
  35% { margin-top: 14px; }
  55% { margin-top: 0; }
}

/* ---------- toast ---------- */

#toast {
  position: absolute;
  top: 34%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 44px;
  font-weight: 900;
  color: var(--toast-fg);
  text-shadow: 0 3px 0 var(--toast-sh);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s, transform 0.4s;
  z-index: 25;
}
#toast.show { opacity: 1; transform: scale(1.06); }
#toast { white-space: pre-line; }

/* ---------- build badge ---------- */

#build {
  position: absolute;
  right: 9px;
  bottom: calc(3px + env(safe-area-inset-bottom));
  z-index: 55;
  font-size: 10px;
  font-weight: 700;
  font-family: ui-monospace, Menlo, monospace;
  color: var(--menu-sub);
  opacity: 0.45;
  pointer-events: none;
}

/* ---------- modal ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--overlay);
  backdrop-filter: blur(4px);
  opacity: 1;
  transition: opacity 0.3s;
}
.overlay[hidden] { display: none; }

.card {
  background: var(--card-bg);
  border-radius: 26px;
  padding: 28px 30px 24px;
  width: min(320px, 84%);
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  animation: cardin 0.42s cubic-bezier(0.24, 1.4, 0.42, 1);
}
@keyframes cardin {
  from { transform: scale(0.7) translateY(30px); opacity: 0; }
  to { transform: scale(1) translateY(0); opacity: 1; }
}

.card h2 { font-size: 28px; font-weight: 900; color: var(--card-title); }

#modal-stars { margin-top: 12px; display: flex; justify-content: center; gap: 8px; }
#modal-stars[hidden] { display: none; }
#modal-stars .icon { width: 40px; height: 40px; color: var(--star-off-card); transition: color 0.3s; }
#modal-stars .icon.earned { color: var(--star-on); filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.3)); animation: starpop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1) both; }
#modal-stars .icon.earned:nth-child(2) { animation-delay: 0.15s; }
#modal-stars .icon.earned:nth-child(3) { animation-delay: 0.3s; }
@keyframes starpop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

.card p { margin-top: 12px; font-size: 16px; font-weight: 600; color: var(--card-text); }

button.primary {
  margin-top: 18px;
  width: 100%;
  border: none;
  cursor: pointer;
  padding: 14px;
  border-radius: 16px;
  font-size: 19px;
  font-weight: 800;
  color: var(--banner-fg);
  background: linear-gradient(180deg, var(--banner-g1), var(--banner-g2));
  box-shadow: 0 5px 14px var(--shadow), inset 0 1px 0 rgba(255,255,255,0.35);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
button.primary .icon { width: 20px; height: 20px; }
button.primary:active { transform: scale(0.97); }

/* ---------- dev toolbar ---------- */

#devbar {
  position: absolute;
  right: 10px;
  top: 168px;
  z-index: 26;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}
#devbar[hidden] { display: none; }

.dev-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  border-radius: 999px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  backdrop-filter: blur(3px);
}
.dev-btn .icon { width: 14px; height: 14px; }
.dev-btn:active { transform: scale(0.94); }

/* ---------- main menu ---------- */

#menu {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--menu-bg1), var(--menu-bg2));
  padding: calc(26px + env(safe-area-inset-top)) 22px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
}
#menu[hidden] { display: none; }

.game-title {
  margin-top: 4vh;
  text-align: center;
  font-size: 52px;
  font-weight: 900;
  letter-spacing: 1px;
  color: var(--menu-title);
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.08);
}
.game-subtitle {
  text-align: center;
  margin-top: 2px;
  font-size: 18px;
  font-weight: 800;
  color: var(--menu-sub);
}

#btn-play {
  margin: 26px auto 14px;
  width: 100%;
  max-width: 260px;
  font-size: 22px;
  padding: 16px;
  border-radius: 20px;
}

#mode-switch {
  display: flex;
  margin: 0 auto 22px;
  width: 100%;
  max-width: 260px;
  background: rgba(0, 0, 0, 0.12);
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}
#mode-switch button {
  flex: 1;
  border: none;
  border-radius: 999px;
  padding: 9px 0;
  font-size: 15px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  background: transparent;
  color: var(--menu-sub);
}
#mode-switch button.active {
  background: var(--pill-bg);
  color: var(--pill-fg);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}

#level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding-bottom: 10px;
}
.lvl {
  position: relative;
  aspect-ratio: 1;
  border: none;
  border-radius: 16px;
  background: var(--pill-bg);
  color: var(--pill-fg);
  font-weight: 900;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  box-shadow: 0 3px 8px var(--shadow);
  font-family: inherit;
}
.lvl .mini { display: flex; gap: 2px; }
.lvl .mini .icon { width: 11px; height: 11px; color: var(--star-off-card); }
.lvl .mini .icon.on { color: var(--star-on); }
.lvl.locked { opacity: 0.5; cursor: default; box-shadow: none; }
.lvl.locked .icon { width: 18px; height: 18px; }
.lvl.current { outline: 3px solid var(--accent); }
.lvl:active:not(.locked) { transform: scale(0.94); }
