:root {
  --bg-top: #FFF9EB;
  --bg-bottom: #E3F4FF;
  --ink: #2D3561;
  --ink-soft: #5E678E;
  --accent: #FF8C42;
  --accent-dark: #D96820;
  --mint: #38C98F;
  --mint-dark: #229B6B;
  --sky: #4DA8FF;
  --sky-dark: #257BD6;
  --yellow: #FFD15C;
  --card-back-1: #5AA9FF;
  --card-back-2: #3A7BFF;
  --card-radius: 20px;
  --shadow-soft: 0 8px 20px rgba(45, 53, 97, 0.12);
  --font-kid: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Hiragino Sans", system-ui, -apple-system, sans-serif;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--font-kid);
  color: var(--ink);
  background: linear-gradient(165deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  touch-action: manipulation;
}

/* 柔和背景圓球 */
.bg-decor {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}

.bubble {
  position: absolute;
  border-radius: 50%;
  opacity: 0.35;
}
.b1 { width: 220px; height: 220px; background: #FFE082; top: -60px; left: -60px; }
.b2 { width: 180px; height: 180px; background: #FFAB91; bottom: -50px; right: -40px; }
.b3 { width: 140px; height: 140px; background: #A5D6A7; top: 38%; right: -50px; }
.b4 { width: 120px; height: 120px; background: #90CAF9; bottom: 18%; left: -40px; }

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 50;
}

#app {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.screen {
  display: none;
  width: 100%;
  height: 100%;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ========== 首頁 ========== */
#home-screen {
  justify-content: space-between;
  gap: 10px;
  overflow-y: auto;
  padding-bottom: 8px;
}

.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.header-left-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.back-hub-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  background: #FFFFFF;
  color: var(--ink);
  font-size: 1.05rem;
  font-weight: 900;
  padding: 10px 16px;
  border-radius: 999px;
  border: 3px solid #E2E8F0;
  box-shadow: 0 4px 0 #CBD5E1;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.back-hub-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #CBD5E1;
}

.star-bank {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #FFFFFF;
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 1.35rem;
  font-weight: 900;
  color: #D97706;
  box-shadow: 0 4px 0 #FDE68A, var(--shadow-soft);
  border: 2px solid #FDE68A;
}

.hero {
  text-align: center;
  flex-shrink: 0;
}

.hero-badge {
  font-size: clamp(2.1rem, 5vw, 3rem);
  line-height: 1.1;
}

.game-title {
  margin: 4px 0 2px;
  font-size: clamp(1.9rem, 5.5vw, 2.9rem);
  font-weight: 900;
  letter-spacing: 0.05em;
  color: var(--ink);
  text-shadow: 0 3px 0 #FFFFFF;
}

.game-subtitle {
  margin: 0;
  font-size: clamp(0.95rem, 2.5vw, 1.2rem);
  color: var(--ink-soft);
  font-weight: 700;
}

.picker-block {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
}

.section-label {
  margin: 0 0 8px 4px;
  font-size: clamp(0.95rem, 2.3vw, 1.15rem);
  color: var(--ink-soft);
  font-weight: 800;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.theme-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 8px;
  background: #FFFFFF;
  border: 3px solid #E2E8F0;
  border-radius: 22px;
  box-shadow: 0 5px 0 #CBD5E1;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.14s ease, box-shadow 0.14s ease, background 0.14s ease;
  font-family: inherit;
}

.theme-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #CBD5E1;
}

.theme-btn.selected {
  background: #FFF6E5;
  border-color: var(--accent);
  box-shadow: 0 5px 0 var(--accent-dark);
  transform: translateY(-2px);
}

.theme-emoji {
  font-size: clamp(2rem, 5vw, 2.8rem);
  line-height: 1;
}

.theme-name {
  font-size: clamp(0.95rem, 2.4vw, 1.15rem);
  font-weight: 900;
  color: var(--ink);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.level-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 12px 8px;
  background: #FFFFFF;
  border: 3px solid #E2E8F0;
  border-radius: 20px;
  box-shadow: 0 5px 0 #CBD5E1;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.14s ease, box-shadow 0.14s ease;
  font-family: inherit;
}

.level-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #CBD5E1;
}

.level-btn.selected {
  background: #E8FBF3;
  border-color: var(--mint);
  box-shadow: 0 5px 0 var(--mint-dark);
  transform: translateY(-2px);
}

.level-stars {
  font-size: clamp(1rem, 2.5vw, 1.25rem);
  line-height: 1;
}

.level-title {
  font-size: clamp(1rem, 2.5vw, 1.2rem);
  font-weight: 900;
  color: var(--ink);
}

.level-desc {
  font-size: clamp(0.8rem, 2vw, 0.95rem);
  font-weight: 700;
  color: var(--ink-soft);
}

.start-area {
  display: flex;
  justify-content: center;
  padding: 4px 0 6px;
  flex-shrink: 0;
}

/* ========== 共用按鈕 ========== */
.primary-btn {
  border: none;
  background: linear-gradient(180deg, #FFA94D 0%, var(--accent) 100%);
  color: #FFFFFF;
  font-family: inherit;
  font-size: clamp(1.3rem, 3.5vw, 1.7rem);
  font-weight: 900;
  letter-spacing: 0.06em;
  padding: 16px 46px;
  border-radius: 999px;
  box-shadow: 0 7px 0 var(--accent-dark), 0 12px 24px rgba(217, 104, 32, 0.3);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.primary-btn:active {
  transform: translateY(4px);
  box-shadow: 0 3px 0 var(--accent-dark), 0 6px 12px rgba(217, 104, 32, 0.25);
}

.secondary-btn {
  border: 3px solid #CBD5E1;
  background: #FFFFFF;
  color: var(--ink);
  font-family: inherit;
  font-size: clamp(1.1rem, 3vw, 1.35rem);
  font-weight: 900;
  padding: 13px 30px;
  border-radius: 999px;
  box-shadow: 0 5px 0 #CBD5E1;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.secondary-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #CBD5E1;
}

.icon-btn {
  width: 52px;
  height: 52px;
  border-radius: 18px;
  border: 3px solid #E2E8F0;
  background: #FFFFFF;
  font-size: 1.5rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 0 #CBD5E1;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  padding: 0;
}

.icon-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #CBD5E1;
}

.pill-btn {
  height: 52px;
  padding: 0 18px;
  border-radius: 18px;
  border: 3px solid #FDE68A;
  background: #FFFBEB;
  color: #B45309;
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 900;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 0 #FCD34D;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.pill-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #FCD34D;
}

.pill-btn.nudge {
  animation: wiggle 1.2s ease-in-out infinite;
}

.pulse-gentle {
  animation: pulseSoft 2.2s ease-in-out infinite;
}

@keyframes pulseSoft {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

@keyframes wiggle {
  0%, 100% { transform: rotate(0deg) scale(1); }
  25% { transform: rotate(-5deg) scale(1.06); }
  75% { transform: rotate(5deg) scale(1.06); }
}

/* ========== 遊戲畫面 ========== */
#game-screen {
  gap: 10px;
}

.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.progress-pill {
  height: 52px;
  padding: 0 18px;
  border-radius: 18px;
  background: #FFFFFF;
  border: 3px solid #BAE6FD;
  box-shadow: 0 4px 0 #7DD3FC;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 1.25rem;
  font-weight: 900;
  color: #0369A1;
}

.cheer-banner {
  text-align: center;
  background: rgba(255, 255, 255, 0.88);
  border-radius: 999px;
  padding: 8px 20px;
  margin: 0 auto;
  font-size: clamp(1rem, 2.8vw, 1.3rem);
  font-weight: 900;
  color: var(--ink);
  box-shadow: 0 4px 12px rgba(45, 53, 97, 0.08);
  flex-shrink: 0;
  transition: transform 0.2s ease, background-color 0.2s ease;
}

.cheer-banner.happy {
  background: #DCFCE7;
  color: #166534;
  transform: scale(1.05);
}

.board-wrapper {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
}

.card-board {
  width: 100%;
  height: 100%;
  max-width: 860px;
  max-height: 680px;
  display: grid;
  gap: clamp(10px, 2vw, 18px);
  place-items: stretch;
  touch-action: none;
}

/* 網格排版（依照卡片數與螢幕方向自動調整） */
.card-board.grid-6 {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  max-width: 720px;
}

.card-board.grid-12 {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  max-width: 820px;
}

.card-board.grid-16 {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  max-width: 740px;
}

/* ========== 卡片與 3D 翻轉 ========== */
.card {
  position: relative;
  width: 100%;
  height: 100%;
  perspective: 900px;
  cursor: pointer;
  border: none;
  background: transparent;
  padding: 0;
  font-family: inherit;
  outline: none;
}

.card:focus-visible .card-inner {
  box-shadow: 0 0 0 5px #FF8C42;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--card-radius);
  transform-style: preserve-3d;
  transition: transform 0.42s cubic-bezier(0.22, 1, 0.36, 1);
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--card-radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px;
}

/* 卡背：可愛星星點點 */
.card-back {
  background:
    radial-gradient(circle at 22% 24%, rgba(255, 255, 255, 0.25) 0 10%, transparent 11%),
    radial-gradient(circle at 78% 76%, rgba(255, 255, 255, 0.25) 0 10%, transparent 11%),
    linear-gradient(145deg, var(--card-back-1) 0%, var(--card-back-2) 100%);
  border: 4px solid #FFFFFF;
  box-shadow: 0 7px 0 #2659C8, var(--shadow-soft);
}

.card-back-emblem {
  width: clamp(44px, 9vmin, 72px);
  height: clamp(44px, 9vmin, 72px);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.6rem, 4.5vmin, 2.6rem);
  color: #FFFFFF;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 卡面：大圖示 + 清楚中文名稱 */
.card-front {
  transform: rotateY(180deg);
  background: var(--card-tint, #FFFFFF);
  border: 4px solid #FFFFFF;
  box-shadow: 0 7px 0 #CBD5E1, var(--shadow-soft);
  gap: 4px;
}

.card-emoji {
  font-size: clamp(2.3rem, 9vmin, 4.6rem);
  line-height: 1.05;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.08));
}

.card-board.grid-16 .card-emoji {
  font-size: clamp(1.9rem, 7vmin, 3.6rem);
}

.card-label {
  font-size: clamp(0.85rem, 2.3vmin, 1.25rem);
  font-weight: 900;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  padding: 2px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* 配對成功特效 */
.card.matched .card-front {
  border-color: var(--mint);
  box-shadow: 0 6px 0 var(--mint-dark), 0 0 0 4px rgba(56, 201, 143, 0.28);
}

.card.just-matched {
  animation: matchPop 0.55s ease;
}

@keyframes matchPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.1); }
  70% { transform: scale(0.96); }
  100% { transform: scale(1); }
}

/* 配對失敗溫柔搖晃 */
.card.mismatch {
  animation: gentleShake 0.42s ease;
}

@keyframes gentleShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* 提示發光效果 */
.card.hint-glow .card-back {
  border-color: #FFD15C;
  box-shadow: 0 0 0 6px rgba(255, 209, 92, 0.75), 0 7px 0 #D97706;
  animation: pulseSoft 0.7s ease-in-out infinite;
}

/* ========== 過關慶祝視窗 ========== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(45, 53, 97, 0.48);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.hidden {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 460px;
  background: #FFFFFF;
  border-radius: 32px;
  border: 5px solid #FFE082;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 18px 40px rgba(45, 53, 97, 0.25);
  animation: modalIn 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.85) translateY(20px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.win-stars {
  font-size: 3.3rem;
  letter-spacing: 6px;
  margin-bottom: 6px;
}

.win-title {
  margin: 0 0 8px;
  font-size: 1.75rem;
  font-weight: 900;
  color: var(--ink);
}

.win-summary {
  margin: 0 0 16px;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink-soft);
}

.rest-reminder {
  background: #ECFDF5;
  border: 2px solid #A7F3D0;
  color: #065F46;
  font-size: 0.98rem;
  font-weight: 800;
  padding: 10px 14px;
  border-radius: 16px;
  margin-bottom: 18px;
}

.rest-reminder.hidden {
  display: none;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.modal-actions .primary-btn,
.modal-actions .secondary-btn {
  width: 100%;
}

/* ========== 直立螢幕（例如 iPhone 直拿）自適應 ========== */
@media (max-width: 640px) {
  .theme-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  .theme-btn {
    padding: 10px 6px;
    border-radius: 18px;
  }

  .level-grid {
    gap: 8px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
    font-size: 1.3rem;
    border-radius: 14px;
  }

  .pill-btn {
    height: 44px;
    padding: 0 12px;
    font-size: 0.98rem;
    border-radius: 14px;
  }

  .progress-pill {
    height: 44px;
    padding: 0 12px;
    font-size: 1.05rem;
    border-radius: 14px;
  }

  .card-back,
  .card-front {
    border-width: 3px;
  }
}

@media (orientation: portrait) {
  .card-board.grid-6 {
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(3, 1fr);
    max-width: 420px;
  }

  .card-board.grid-12 {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(4, 1fr);
    max-width: 480px;
  }

  .card-board.grid-16 {
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    max-width: 500px;
  }
}
