:root {
  --accent: #38bdf8;
  --accent-2: #7dd3fc;
  --panel: rgba(10, 40, 65, .55);
  --font-display: 'Baloo 2', system-ui, sans-serif;
  --font-body: 'Nunito', system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-body);
  background: radial-gradient(circle at 50% 0%, #dff3ff 0%, #bfe6ff 45%, #8fd0f5 100%);
}

#app { position: relative; width: 100%; height: 100%; }

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  overflow: auto;
}
.screen.active { display: flex; }

button { font-family: var(--font-body); cursor: pointer; border: none; }
ul { margin: 0; padding: 0; }

/* ===================== MENU ===================== */

.menu-inner {
  width: 100%;
  max-width: 900px;
  padding: 5vh 20px 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.title-wrap { margin-bottom: 28px; }

.game-title {
  font-family: var(--font-display);
  font-size: clamp(3rem, 11vw, 5.2rem);
  margin: 0;
  line-height: 1;
  letter-spacing: 2px;
}
.game-title .l {
  display: inline-block;
  margin: 0 1px;
  text-shadow: 0 3px 0 rgba(255,255,255,.75), 0 6px 14px rgba(20, 60, 90, .28);
}
.game-title .l:nth-child(1) { color: #ff8fa3; transform: rotate(-6deg) translateY(2px); }
.game-title .l:nth-child(2) { color: #fbbf24; transform: rotate(3deg)  translateY(-3px); }
.game-title .l:nth-child(3) { color: #34d399; transform: rotate(-3deg) translateY(3px); }
.game-title .l:nth-child(4) { color: #38bdf8; transform: rotate(4deg)  translateY(-2px); }
.game-title .l:nth-child(5) { color: #2563eb; transform: rotate(-5deg) translateY(2px); }

.subtitle {
  margin: 14px 0 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: #33526b;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 22px;
  width: 100%;
  margin-top: 8px;
}

.theme-card {
  position: relative;
  border-radius: 26px;
  overflow: hidden;
  border: 5px solid #fff;
  box-shadow: 0 10px 24px rgba(20, 60, 90, .25);
  cursor: pointer;
  background-size: cover;
  background-position: center;
  aspect-ratio: 3/4;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  transition: transform .18s ease, box-shadow .18s ease;
}
.theme-card:hover, .theme-card:focus-visible { transform: translateY(-6px) scale(1.02); box-shadow: 0 16px 30px rgba(20, 60, 90, .35); }
.theme-card:active { transform: translateY(-2px) scale(.99); }

.theme-card .scrim {
  padding: 16px 12px 14px;
  background: linear-gradient(to top, rgba(0,0,0,.72), rgba(0,0,0,0) 90%);
  color: #fff;
  text-align: left;
}
.theme-card h3 { margin: 0 0 4px; font-family: var(--font-display); font-size: 1.3rem; }
.theme-card .card-best { font-size: .8rem; opacity: .9; font-weight: 700; }
.theme-card .card-emoji {
  position: absolute; top: 10px; right: 10px;
  font-size: 1.6rem;
  background: rgba(255,255,255,.85);
  border-radius: 50%;
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,.2);
}

.ghost-btn {
  margin-top: 30px;
  background: rgba(255,255,255,.7);
  color: #1d4a68;
  font-weight: 800;
  font-family: var(--font-display);
  padding: 10px 22px;
  border-radius: 999px;
  font-size: .95rem;
  box-shadow: 0 4px 10px rgba(20,60,90,.15);
}
.ghost-btn:hover { background: #fff; }

.primary-btn {
  background: linear-gradient(180deg, #4fd1ff, #2196e0);
  color: #fff;
  font-weight: 800;
  font-family: var(--font-display);
  padding: 12px 28px;
  border-radius: 999px;
  font-size: 1.05rem;
  box-shadow: 0 6px 14px rgba(20,90,150,.35);
  letter-spacing: .3px;
}
.primary-btn:hover { filter: brightness(1.06); }
.primary-btn:active { transform: translateY(1px); }

.howto-panel {
  position: fixed; inset: 0;
  background: rgba(10,30,45,.55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  z-index: 50;
}
.howto-panel.hidden { display: none; }
.howto-card {
  background: #fff;
  border-radius: 24px;
  max-width: 460px;
  width: 100%;
  padding: 26px 26px 22px;
  text-align: left;
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.howto-card h2 { font-family: var(--font-display); margin: 0 0 14px; color: #1d4a68; }
.howto-card ul { list-style: none; display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
.howto-card li { font-size: .95rem; color: #33526b; font-weight: 700; line-height: 1.4; }
.howto-card .primary-btn { width: 100%; }

/* ===================== GAME ===================== */

#screen-game.active { background: #0d2436; }

#game-wrap {
  width: 100%;
  max-width: 560px;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 10px 12px 6px;
  margin: 0 auto;
}

#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 2px 8px;
}

.icon-btn {
  background: rgba(255,255,255,.9);
  border-radius: 14px;
  width: 42px; height: 42px;
  font-size: 1.2rem;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,.2);
  flex-shrink: 0;
}
.icon-btn.muted { opacity: .45; }

.hud-scores { display: flex; gap: 8px; }
.score-box {
  background: rgba(255,255,255,.92);
  border-radius: 14px;
  padding: 5px 14px;
  text-align: center;
  min-width: 66px;
  box-shadow: 0 3px 8px rgba(0,0,0,.18);
}
.score-box.best { background: rgba(255, 224, 130, .95); }
.score-label { display: block; font-size: .62rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: #5b7a91; }
.score-box.best .score-label { color: #8a6a10; }
.score-value { font-family: var(--font-display); font-size: 1.2rem; color: #1d4a68; font-weight: 700; }

.hud-controls { display: flex; gap: 6px; }

#next-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 6px;
}
.next-label { font-size: .78rem; font-weight: 800; color: #dceeff; text-shadow: 0 1px 3px rgba(0,0,0,.4); }
.next-thumb {
  width: 46px; height: 46px;
  background: rgba(255,255,255,.9);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,.25);
  overflow: hidden;
}
.next-thumb img { width: 88%; height: 88%; object-fit: contain; }

#canvas-container {
  --accent: #38bdf8;
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 12px 28px rgba(0,0,0,.4), inset 0 0 0 4px var(--accent);
  touch-action: none;
}

#bg-layer {
  position: absolute; inset: 0;
  background-size: cover;
  background-position: center;
  filter: saturate(1.05);
}
#bg-layer::after {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(to bottom, rgba(255,255,255,.08), rgba(0,0,0,.12));
}

#world {
  position: absolute;
  top: 0; left: 50%;
  transform: translateX(-50%);
  height: 100%;
  width: auto;
  max-width: 100%;
  display: block;
}

#danger-line {
  position: absolute;
  left: 6%;
  right: 6%;
  height: 0;
  border-top: 3px dashed rgba(255, 70, 70, .85);
  filter: drop-shadow(0 0 4px rgba(255,0,0,.5));
  pointer-events: none;
  transition: opacity .2s;
}
#danger-line.warn {
  animation: dangerPulse .5s infinite alternate;
}
@keyframes dangerPulse {
  from { border-color: rgba(255,60,60,.55); }
  to   { border-color: rgba(255,60,60,1); }
}

#particles-layer, #floaters-layer {
  position: absolute; inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.particle {
  position: absolute;
  border-radius: 50%;
  will-change: transform, opacity;
  animation: burstOut .6s ease-out forwards;
}
@keyframes burstOut {
  0% { transform: translate(-50%,-50%) scale(.4); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.1); opacity: 0; }
}

.floater {
  position: absolute;
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 2px 6px rgba(0,0,0,.5);
  white-space: nowrap;
  animation: floatUp 1s ease-out forwards;
  pointer-events: none;
}
@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%,-40%) scale(.7); }
  15% { opacity: 1; transform: translate(-50%,-70%) scale(1.08); }
  100% { opacity: 0; transform: translate(-50%,-160%) scale(1); }
}

.hint-line {
  text-align: center;
  color: #cfe9ff;
  font-size: .72rem;
  font-weight: 700;
  margin: 6px 0 2px;
  opacity: .85;
}

.overlay {
  position: absolute; inset: 0;
  background: rgba(6, 20, 32, .6);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  z-index: 20;
}
.overlay.hidden { display: none; }
.overlay-card {
  background: #fff;
  border-radius: 24px;
  padding: 28px 30px;
  text-align: center;
  width: 100%;
  max-width: 320px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.overlay-card h2 { font-family: var(--font-display); margin: 0 0 4px; color: #1d4a68; }
.record-badge {
  margin: 0;
  background: linear-gradient(90deg,#ffe066,#ffb347);
  color: #6b3e00;
  font-weight: 800;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: .85rem;
}
.record-badge.hidden { display: none; }
.final-scores {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 6px 0 10px;
}
.final-scores div { display: flex; flex-direction: column; align-items: center; }
.final-scores span { font-size: .7rem; font-weight: 800; color: #7c95a8; text-transform: uppercase; }
.final-scores strong { font-family: var(--font-display); font-size: 1.6rem; color: #1d4a68; }

/* responsive tweaks */
@media (max-height: 680px) {
  #next-panel { margin-bottom: 4px; }
  .hint-line { display: none; }
}
@media (max-width: 380px) {
  .icon-btn { width: 36px; height: 36px; font-size: 1rem; }
  .score-box { padding: 4px 10px; min-width: 54px; }
  .score-value { font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .particle, .floater, #danger-line.warn { animation: none !important; }
}

/* Phones: stack the three world cards so all of them are visible and tappable */
@media (max-width: 640px) {
  .menu-inner { padding-top: 4vh; }
  .title-wrap { margin-bottom: 18px; }
  .theme-grid {
    grid-template-columns: 1fr;
    gap: 14px;
    max-width: 420px;
    margin-left: auto;
    margin-right: auto;
  }
  .theme-card { aspect-ratio: 16 / 7; border-radius: 22px; }
}
