:root {
  --bg-top: #6ed6ff;
  --bg-bottom: #6d89ff;
  --panel: rgba(255,255,255,0.84);
  --panel-strong: rgba(255,255,255,0.96);
  --ink: #27406d;
  --ink-soft: #5f6f95;
  --yellow: #ffd54f;
  --orange: #ff9f43;
  --green: #51d88a;
  --pink: #ff7eb6;
  --purple: #8d6bff;
  --red: #ff6f61;
  --shadow: 0 14px 34px rgba(54, 78, 161, 0.22);
  --radius-xl: 28px;
  --radius-lg: 22px;
  --radius-md: 16px;
  --safe-bottom: max(14px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom)); font-family: 'Noto Sans SC', sans-serif; color: var(--ink); overflow: hidden; overscroll-behavior: none; }
body::before,
body::after {
  content: '';
  position: fixed;
  inset: auto;
  pointer-events: none;
  z-index: 0;
  border-radius: 50%;
  filter: blur(14px);
  opacity: .35;
}
body::before { width: 240px; height: 240px; left: -50px; top: 70px; background: rgba(255,255,255,.7); }
body::after { width: 280px; height: 280px; right: -60px; bottom: 120px; background: rgba(255, 216, 102, .45); }

button, input { font: inherit; }
button { border: 0; cursor: pointer; }

#app {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: calc(var(--vh, 1vh) * 100);
  width: 100%;
  padding: 14px 14px calc(14px + var(--safe-bottom));
}

.game-shell {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
}

.top-bar, .bottom-bar, .card, .modal-panel, .toast {
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.top-bar {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 10px;
}

.brand-panel, .profile-panel, .bottom-bar, .card, .world-chip, .reward-item, .small-panel {
  background: var(--panel);
  border: 2px solid rgba(255,255,255,0.55);
  box-shadow: var(--shadow);
}

.brand-panel, .profile-panel {
  border-radius: var(--radius-xl);
  padding: 14px 16px;
}

.brand-title {
  font-family: 'Baloo 2', cursive;
  font-weight: 800;
  font-size: 1.55rem;
  line-height: 1;
  margin: 0;
}

.brand-subtitle, .muted, .knowledge-tag, .footer-note, .parent-metric span:last-child {
  color: var(--ink-soft);
}

.brand-subtitle { margin-top: 6px; font-size: .86rem; }

.profile-row, .stat-row, .nav-row, .level-row, .collection-grid, .meta-grid, .parent-grid, .music-row, .math-grid, .word-grid, .letter-grid { display: grid; gap: 10px; }
.profile-row { grid-template-columns: 56px 1fr; align-items: center; }
.avatar {
  width: 56px; height: 56px; border-radius: 18px;
  background: linear-gradient(145deg, #ffef7d, #ffa5d9);
  display: grid; place-items: center; font-size: 2rem; box-shadow: inset 0 4px 12px rgba(255,255,255,.55);
}
.player-name { font-weight: 800; font-size: 1rem; }
.level-badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(109, 137, 255, .14); color: var(--purple);
  padding: 4px 10px; border-radius: 999px; font-size: .82rem; font-weight: 700;
}

.progress-track {
  width: 100%; height: 12px; border-radius: 999px; background: rgba(90,108,164,.14); overflow: hidden;
}
.progress-fill {
  height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--green), #7cf3ba); transition: width .35s ease;
}
.progress-fill.xp { background: linear-gradient(90deg, var(--orange), #ffd76b); }

.main-view {
  min-height: 0;
  overflow: auto;
  padding-bottom: 2px;
  scroll-behavior: smooth;
}
.main-view::-webkit-scrollbar { width: 0; height: 0; }

.screen { display: none; animation: popIn .3s ease; }
.screen.active { display: block; }

.card {
  border-radius: var(--radius-xl);
  padding: 16px;
  margin-bottom: 12px;
}

.hero-card {
  background:
    radial-gradient(circle at top right, rgba(255,255,255,.82), transparent 34%),
    linear-gradient(145deg, rgba(255, 244, 154, .88), rgba(255, 177, 125, .94));
}
.hero-title { margin: 0; font-family: 'Baloo 2', cursive; font-size: 1.7rem; line-height: 1.05; }
.hero-actions, .quest-actions, .result-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }

.primary-btn, .secondary-btn, .ghost-btn, .option-btn, .nav-btn, .pill-btn {
  border-radius: 18px;
  padding: 12px 16px;
  min-height: 48px;
  font-weight: 800;
  transition: transform .16s ease, box-shadow .16s ease, background .16s ease, opacity .16s ease;
  will-change: transform;
}
.primary-btn:active, .secondary-btn:active, .ghost-btn:active, .option-btn:active, .nav-btn:active, .pill-btn:active { transform: scale(.97); }
.primary-btn {
  color: white; background: linear-gradient(135deg, var(--pink), var(--red)); box-shadow: 0 12px 22px rgba(255,111,133,.28);
}
.secondary-btn {
  color: white; background: linear-gradient(135deg, var(--purple), #5fa8ff); box-shadow: 0 12px 22px rgba(95,168,255,.24);
}
.ghost-btn { background: rgba(255,255,255,.74); color: var(--ink); }
.pill-btn {
  background: rgba(109, 137, 255, .12); color: var(--purple); padding: 10px 14px; min-height: 40px; font-size: .9rem;
}

.meta-grid { grid-template-columns: repeat(3, 1fr); }
.small-panel { border-radius: 20px; padding: 12px; text-align: center; }
.small-panel strong { display: block; font-size: 1.15rem; font-family: 'Baloo 2', cursive; }
.small-panel span { font-size: .8rem; color: var(--ink-soft); }

.section-title {
  margin: 0 0 10px;
  font-size: 1.02rem;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.section-title strong { font-family: 'Baloo 2', cursive; font-size: 1.2rem; }

.world-grid, .level-list, .achievement-list, .quest-list, .leaderboard-list, .parent-grid { display: grid; gap: 10px; }
.world-chip {
  border-radius: 24px;
  padding: 14px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
}
.world-icon { width: 52px; height: 52px; border-radius: 18px; display: grid; place-items: center; font-size: 1.7rem; color: white; }
.world-info h3, .quest-title, .achievement-title, .parent-metric strong, .leader-name { margin: 0; }
.world-info p { margin: 3px 0 0; color: var(--ink-soft); font-size: .82rem; }
.world-lock { font-size: .78rem; font-weight: 800; color: var(--ink-soft); }
.world-progress { min-width: 74px; text-align: right; font-weight: 800; }

.level-card {
  border-radius: 24px;
  padding: 14px;
  background: linear-gradient(145deg, rgba(255,255,255,.95), rgba(243,246,255,.9));
  border: 2px solid rgba(255,255,255,.75);
  box-shadow: var(--shadow);
}
.level-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.level-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.knowledge-tag {
  display: inline-flex; align-items: center; padding: 5px 9px; border-radius: 999px; background: rgba(109, 137, 255, .1); font-size: .72rem; font-weight: 700;
}
.level-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; }
.stars { color: #ffb703; font-size: 1rem; letter-spacing: 1px; }

.quiz-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.quiz-title { margin: 0; font-size: 1.15rem; font-family: 'Baloo 2', cursive; }
.subject-pill {
  background: rgba(255, 210, 82, .24);
  color: #9a6700;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: .78rem;
  font-weight: 800;
}
.quiz-question { font-size: 1.06rem; line-height: 1.6; margin: 14px 0; }
.option-grid { display: grid; gap: 10px; }
.option-btn {
  text-align: left; background: rgba(255,255,255,.82); color: var(--ink); border: 2px solid rgba(137,151,204,.18);
  min-height: 56px;
}
.option-btn.correct, .tile.correct { background: rgba(81,216,138,.2); border-color: rgba(81,216,138,.7); }
.option-btn.wrong, .tile.wrong { background: rgba(255,111,97,.18); border-color: rgba(255,111,97,.72); }
.option-btn:disabled { cursor: default; opacity: 1; }

.word-grid, .letter-grid, .math-grid { grid-template-columns: repeat(2, 1fr); }
.tile {
  min-height: 74px;
  border-radius: 20px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,.88);
  border: 2px dashed rgba(95,111,149,.28);
  font-weight: 800;
  font-size: 1.1rem;
}
.tile.small { min-height: 62px; font-size: 1rem; }
.music-pad {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 12px;
}
.match-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.match-col { display: grid; gap: 10px; }
.match-board { position: relative; }
.match-lines {
  position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible;
}
.match-line {
  fill: none; stroke: rgba(81,216,138,.9); stroke-width: 2.8; stroke-linecap: round;
  stroke-dasharray: 120; stroke-dashoffset: 120; animation: drawLine .42s ease forwards;
  filter: drop-shadow(0 2px 6px rgba(81,216,138,.25));
}
.match-source { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.match-card {
  min-height: 56px; border-radius: 18px; border: 2px solid rgba(137,151,204,.2);
  background: rgba(255,255,255,.94); font-weight: 800; color: var(--ink);
  box-shadow: var(--shadow);
}
.match-card.selected { outline: 3px solid rgba(141,107,255,.28); }
.match-card.used { opacity: .5; transform: scale(.98); }
.match-card.dragging { opacity: .35; }
.match-card.snap { animation: snapCard .34s ease; }
.match-slot {
  min-height: 78px; border-radius: 20px; border: 2px dashed rgba(137,151,204,.35);
  background: rgba(255,255,255,.7); padding: 10px; display: grid; gap: 8px;
}
.match-slot.hover { border-color: rgba(81,216,138,.85); background: rgba(81,216,138,.12); }
.match-slot.locked { opacity: .9; }
.match-slot.snap { animation: snapSlot .34s ease; }
.match-slot-target { font-weight: 800; color: var(--ink-soft); }
.match-slot-fill {
  min-height: 38px; border-radius: 14px; background: rgba(109,137,255,.1);
  display: grid; place-items: center; padding: 6px 8px; font-weight: 700; color: var(--ink-soft);
}
.match-slot-fill.filled { background: rgba(81,216,138,.18); color: var(--ink); }
.music-key {
  border-radius: 20px; min-height: 72px; color: white; font-weight: 900; font-size: 1.1rem;
}
.music-key:nth-child(1) { background: #ff7a8b; }
.music-key:nth-child(2) { background: #ffb347; }
.music-key:nth-child(3) { background: #6fd08c; }
.music-key:nth-child(4) { background: #6ea8ff; }
.music-key.active { transform: translateY(-4px) scale(1.02); box-shadow: 0 12px 20px rgba(61,77,126,.25); }
.rhythm-status { display: flex; gap: 8px; flex-wrap: wrap; }
.rhythm-chip {
  padding: 6px 10px; border-radius: 999px; background: rgba(109,137,255,.1);
  font-size: .82rem; font-weight: 700; color: var(--ink-soft);
}

.bottom-bar {
  border-radius: 24px;
  padding: 10px;
  position: sticky;
  bottom: 0;
}
.nav-row { grid-template-columns: repeat(5, 1fr); }
.nav-btn {
  background: transparent; color: var(--ink-soft);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 58px; padding: 6px;
}
.nav-btn.active { background: rgba(109,137,255,.12); color: var(--purple); }
.nav-btn .icon { font-size: 1.24rem; }
.nav-btn .label { font-size: .72rem; font-weight: 800; }

.reward-item, .achievement-item, .quest-item, .leader-item, .parent-metric {
  border-radius: 22px; padding: 12px 14px;
}
.collection-grid { grid-template-columns: repeat(3, 1fr); }
.reward-item { text-align: center; }
.reward-item .emoji { font-size: 1.8rem; }
.reward-item.locked { opacity: .55; filter: grayscale(.2); }

.achievement-item, .quest-item, .leader-item {
  background: rgba(255,255,255,.88);
  border: 2px solid rgba(255,255,255,.6);
  box-shadow: var(--shadow);
}
.achievement-item.done { background: rgba(255,239,174,.9); }
.achievement-head, .leader-item, .quest-item, .parent-metric { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.achievement-mark, .quest-mark {
  width: 46px; height: 46px; border-radius: 16px; display: grid; place-items: center; font-size: 1.4rem; background: rgba(109,137,255,.14);
}
.achievement-item.done .achievement-mark { background: rgba(255,183,3,.24); }
.achievement-desc, .quest-desc, .leader-score { font-size: .82rem; color: var(--ink-soft); }

.parent-grid { grid-template-columns: 1fr 1fr; }
.parent-metric {
  background: rgba(255,255,255,.88);
  align-items: flex-start;
  flex-direction: column;
  min-height: 106px;
}
.parent-metric strong { font-size: 1.2rem; font-family: 'Baloo 2', cursive; }
.parent-wide { grid-column: 1 / -1; }
.bar-group { display: grid; gap: 8px; width: 100%; }
.subject-row { display: grid; grid-template-columns: 62px 1fr 38px; align-items: center; gap: 8px; font-size: .82rem; }
.subject-bar { height: 10px; background: rgba(94,115,168,.12); border-radius: 999px; overflow: hidden; }
.subject-bar > span { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--purple), var(--pink)); }

.footer-note { text-align: center; font-size: .78rem; margin-top: 4px; }

.modal {
  position: fixed; inset: 0; display: none; align-items: flex-end; justify-content: center; padding: 16px; z-index: 10;
  background: rgba(39,64,109,.28);
}
.modal.show { display: flex; }
.modal-panel {
  width: min(100%, 520px); max-height: 88vh; max-height: calc(var(--vh, 1vh) * 88); overflow: auto;
  border-radius: 28px; background: var(--panel-strong); padding: 18px; padding-bottom: calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 24px 44px rgba(26,45,97,.28);
  animation: slideUp .25s ease;
}
.result-mascot { font-size: 3rem; text-align: center; }
.result-score { text-align: center; font-size: 2rem; font-family: 'Baloo 2', cursive; margin: 4px 0; }
.result-summary { text-align: center; color: var(--ink-soft); }

.toast {
  position: fixed; left: 50%; bottom: calc(92px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px);
  opacity: 0; pointer-events: none;
  background: rgba(39,64,109,.9); color: white; padding: 11px 14px; border-radius: 999px; font-size: .84rem; font-weight: 700;
  transition: .25s ease; z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.celebrate { animation: wiggle .4s ease; }
.hidden { display: none !important; }

@keyframes popIn { from { opacity: 0; transform: translateY(8px) scale(.985); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes slideUp { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: translateY(0); } }
@keyframes wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-2deg); } 75% { transform: rotate(2deg); } }
@keyframes snapSlot { 0% { transform: scale(.98); } 50% { transform: scale(1.03); background: rgba(81,216,138,.18); } 100% { transform: scale(1); } }
@keyframes snapCard { 0% { transform: scale(1); } 50% { transform: scale(1.06); } 100% { transform: scale(.98); } }
@keyframes drawLine { to { stroke-dashoffset: 0; } }

@media (min-width: 720px) {
  #app { max-width: 520px; margin: 0 auto; }
}

@media (max-width: 380px) {
  .meta-grid, .word-grid, .letter-grid, .math-grid, .parent-grid, .match-grid, .match-source { grid-template-columns: 1fr; }
  .top-bar { grid-template-columns: 1fr; }
  .collection-grid { grid-template-columns: repeat(2, 1fr); }
  .hero-title { font-size: 1.45rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
