/* 小小遊戲樂園 — 共用樣式
 * 相容舊款 Safari：不用 flex gap、aspect-ratio、inset、dvh、backdrop-filter。
 */
:root {
  --bg1: #2b1d6e;
  --bg2: #120c33;
  --ink: #ffffff;
  --muted: rgba(255, 255, 255, 0.72);
  --card: #ffffff;
  --card-ink: #2d2a4a;
  --c1: #7c8cff;
  --c2: #3d2c8d;
  --font: "Arial Rounded MT Bold", -apple-system, "PingFang TC", "Heiti TC", "Microsoft JhengHei", sans-serif;
  --ink: #3a3850;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg2);
  -webkit-text-size-adjust: 100%;
}
button { font-family: inherit; }

/* ================= 首頁 ================= */
.home-page {
  min-height: 100%;
  background: #6cd3ff linear-gradient(180deg, #6cd3ff 0%, #a8ecff 55%, #d9f7ff 100%) fixed;
  padding: env(safe-area-inset-top) 16px calc(24px + env(safe-area-inset-bottom));
  overflow-x: hidden;
}
.sky { position: fixed; top: 0; left: 0; right: 0; bottom: 0; pointer-events: none; z-index: 0; overflow: hidden; }
.cloud { position: absolute; width: 180px; height: 56px; border-radius: 40px; background: rgba(255, 255, 255, 0.75); animation: drift 40s linear infinite; }
.cloud::before, .cloud::after { content: ""; position: absolute; background: inherit; border-radius: 50%; }
.cloud::before { width: 80px; height: 80px; left: 26px; top: -40px; }
.cloud::after { width: 64px; height: 64px; left: 88px; top: -28px; }
.k1 { top: 10%; left: -10%; }
.k2 { top: 38%; left: 60%; animation-duration: 55s; animation-delay: -20s; }
.k3 { top: 72%; left: 20%; animation-duration: 48s; animation-delay: -35s; }
@keyframes drift { from { margin-left: -200px; } to { margin-left: 110vw; } }

.hero { position: relative; z-index: 1; display: flex; align-items: center; justify-content: center; padding: 18px 0 4px; }
.logo { width: auto; height: 120px; margin-right: 14px; animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-8px) rotate(4deg); } }
.hero-text { text-align: left; }
.hero h1 {
  margin: 0 0 6px;
  font-size: 40px; font-weight: 900; letter-spacing: 2px;
  color: #fff;
  -webkit-text-stroke: 2px #3a3850;
  text-shadow: 0 4px 0 #3a3850;
}
.tag { margin: 0; display: inline-block; padding: 4px 12px; border-radius: 999px; background: rgba(255, 255, 255, 0.85); color: #3a3850; font-size: 15px; font-weight: 800; }

.cats {
  position: relative; z-index: 1;
  display: flex; flex-wrap: wrap; justify-content: center;
  margin: 14px auto 4px; max-width: 900px;
}
.cat {
  margin: 5px;
  border: 3px solid #3a3850; border-radius: 999px;
  padding: 9px 18px;
  font-size: 18px; font-weight: 900;
  color: #3a3850;
  background: #fff;
  box-shadow: 0 4px 0 rgba(58, 56, 80, 0.35);
  cursor: pointer;
}
.cat:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(58, 56, 80, 0.35); }
.cat.on { color: #fff; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25); }
.cat .dot { display: inline-block; width: 14px; height: 14px; margin-right: 7px; border-radius: 50%; vertical-align: -1px; border: 2px solid #3a3850; }
.cat.on .dot { border-color: #fff; }

/* Bento 方格：大小混排，大格佔 2x2（用 padding-top 撐出正方形，相容舊 Safari） */
.grid {
  position: relative; z-index: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  grid-auto-flow: dense;
  grid-gap: 14px; gap: 14px;
  max-width: 1100px;
  margin: 14px auto 0;
}
.tile {
  position: relative; display: block;
  border-radius: 24px;
  border: 4px solid #fff;
  background: #fff;
  box-shadow: 0 6px 0 rgba(58, 56, 80, 0.28), 0 10px 22px rgba(30, 80, 140, 0.18);
  overflow: hidden;
  text-decoration: none;
  animation: pop-in 0.35s ease-out both;
  transition: transform 0.12s;
  -webkit-transform: translateZ(0);
}
.tile::before { content: ""; display: block; padding-top: 100%; }
.tile.big { grid-column: span 2; grid-row: span 2; }
.tile.wide { grid-column: span 2; }
.tile.wide::before { padding-top: calc(50% - 7px); }
.tile:active { transform: scale(0.95); }
@media (hover: hover) { .tile:hover { transform: scale(1.04) rotate(-1deg); } }
@keyframes pop-in { from { opacity: 0; transform: scale(0.85); } to { opacity: 1; transform: scale(1); } }
.tile-img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; object-fit: cover; }
.tile-name {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 26px 8px 9px;
  background: linear-gradient(180deg, rgba(20, 10, 50, 0) 0%, rgba(20, 10, 50, 0.72) 100%);
  color: #fff; text-align: center;
  font-size: 19px; font-weight: 900;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.45);
}
.tile.big .tile-name { font-size: 28px; padding-bottom: 14px; }
.tile.wide .tile-name { font-size: 22px; }
.tile-desc { display: block; font-size: 14px; font-weight: 700; opacity: 0.92; margin-top: 2px; }
.tile:not(.big) .tile-desc { display: none; }
.badge {
  position: absolute; top: 8px; right: 8px; z-index: 2;
  font-size: 13px; font-weight: 900;
  padding: 3px 9px; border-radius: 999px;
  color: #fff; border: 2px solid #fff;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}
.badge.duo { background: #a06bff; }
.badge.done { background: #2fbf71; }
.foot { position: relative; z-index: 1; max-width: 700px; margin: 32px auto 0; padding: 12px 16px; text-align: center; color: #3a3850; font-size: 13px; line-height: 1.7; background: rgba(255, 255, 255, 0.6); border-radius: 18px; }
.foot p { margin: 4px 0; }

@media (max-width: 420px) {
  .grid { grid-template-columns: repeat(2, 1fr); grid-gap: 12px; gap: 12px; }
  .tile-name { font-size: 17px; }
  .hero h1 { font-size: 30px; }
  .logo { width: auto; height: 86px; }
}

/* ================= 遊戲頁 ================= */
.game-page {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  background: var(--bg2);
}
#app {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#bar {
  flex: 0 0 auto;
  display: flex; align-items: center;
  height: 58px; padding: 0 8px;
  background: linear-gradient(90deg, var(--c1), var(--c2));
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
  position: relative; z-index: 3;
  color: #fff;
}
.ib {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  width: 46px; height: 46px; margin: 0 3px;
  border: 0; border-radius: 14px;
  background: rgba(255, 255, 255, 0.22);
  font-size: 22px; color: #fff; text-decoration: none;
  cursor: pointer;
}
.ib:active { transform: scale(0.92); }
.ib svg { width: 26px; height: 26px; display: block; }
#title {
  flex: 1 1 auto; min-width: 0;
  margin: 0 8px;
  font-size: 20px; font-weight: 900;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}
#title img { width: 38px; height: 38px; vertical-align: middle; margin-right: 8px; position: relative; top: -2px; }
.chip {
  flex: 0 0 auto;
  margin: 0 4px; padding: 7px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.28);
  color: #fff;
  font-size: 17px; font-weight: 800;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
  white-space: nowrap;
}
.chip:empty { display: none; }
.chip-best { background: rgba(255, 255, 255, 0.25); }
@media (max-width: 480px) {
  #title { font-size: 0; margin: 0; }
  .chip { font-size: 15px; padding: 6px 9px; }
}

#stage { position: relative; flex: 1 1 auto; overflow: hidden; }
#cv { position: absolute; top: 0; left: 0; display: block; }

#overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  display: none;
  align-items: center; justify-content: center;
  padding: 16px;
  background: rgba(10, 6, 30, 0.62);
  z-index: 5;
  overflow-y: auto;
  touch-action: pan-y;
}
#overlay.show { display: flex; }
.card {
  width: 100%; max-width: 400px;
  margin: auto;
  padding: 22px 22px 18px;
  margin-top: 60px;
  border-radius: 28px;
  border: 4px solid var(--ink);
  background: var(--card);
  color: var(--card-ink);
  text-align: center;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.25);
  animation: card-in 0.28s cubic-bezier(.2, 1.4, .5, 1) both;
}
@keyframes card-in { from { opacity: 0; transform: scale(0.7); } to { opacity: 1; transform: scale(1); } }
.card-icon { display: block; width: 104px; height: 104px; margin: -62px auto 0; object-fit: contain; filter: drop-shadow(0 5px 0 rgba(0, 0, 0, 0.15)); }
.card-happy .card-icon { animation: bob 1.2s ease-in-out infinite; }
.card h2 { margin: 6px 0 8px; font-size: 28px; font-weight: 900; color: var(--c2); }
.card p { margin: 6px 0; font-size: 17px; line-height: 1.55; color: #5a5677; }
.card p.big { font-size: 44px; margin: 4px 0; font-weight: 900; color: var(--card-ink); }
.card p.record { font-size: 22px; font-weight: 900; color: #ff5c8a; animation: bob 1s ease-in-out infinite; }
.card p.muted { font-size: 15px; color: #8a86a8; }
.card-btns { margin-top: 14px; }
.btn {
  display: block; width: 100%;
  margin: 10px 0 0; padding: 14px 10px;
  border: 0; border-radius: 18px;
  font-size: 21px; font-weight: 900; color: #fff;
  background: var(--c2);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.2), inset 0 -4px 0 rgba(0, 0, 0, 0.12);
  border: 3px solid var(--ink);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
  cursor: pointer;
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); }
.btn-main { display: block; }
.btn-sub { display: block; font-size: 13px; font-weight: 700; opacity: 0.9; margin-top: 2px; }
.btn-sub:empty { display: none; }
.b-easy { background: #2fbf71; }
.b-mid { background: #ffa41b; }
.b-hard { background: #ff5c5c; }
.b-blue { background: #3d8bfd; }
.b-home { background: #e9e7f5; color: #5a5677; text-shadow: none; }

/* 開始畫面：左邊封面＋操作圖解，右邊難度按鈕（窄螢幕上下排） */
.card-start { max-width: 760px; margin: auto; padding: 18px; display: flex; align-items: stretch; text-align: left; }
.card-left { flex: 0 0 46%; margin-right: 18px; }
.card-right { flex: 1 1 auto; display: flex; flex-direction: column; justify-content: center; text-align: center; }
.card-cover { display: block; width: 100%; height: auto; border-radius: 20px; border: 4px solid var(--ink); box-shadow: 0 5px 0 rgba(0, 0, 0, 0.18); }
.card-how { display: flex; align-items: center; margin-top: 12px; }
.ctrl-pic { flex: 0 0 auto; width: 104px; height: 76px; margin-right: 10px; animation: wiggle 1.6s ease-in-out infinite; }
@keyframes wiggle { 0%, 100% { transform: rotate(-3deg) scale(1); } 50% { transform: rotate(3deg) scale(1.05); } }
.card-how p { margin: 0; font-size: 16px; line-height: 1.45; color: #5a5677; text-align: left; }
.card-start h2 { margin: 0 0 4px; font-size: 32px; }
.card-start .card-btns { margin-top: 4px; }
@media (max-width: 640px), (max-height: 520px) and (max-width: 900px) {
  .card-start { display: block; max-width: 420px; text-align: center; }
  .card-left { margin: 0 0 8px; }
  .card-cover { height: 150px; object-fit: cover; }
}
@media (max-height: 560px) {
  .card-start .btn { padding: 9px 10px; font-size: 19px; }
}

/* 結算星星與彩帶 */
.stars { margin: 4px 0 2px; }
.stars .star {
  display: inline-block; width: 58px; height: 58px; margin: 0 4px;
  -webkit-filter: grayscale(1); filter: grayscale(1); opacity: 0.3;
}
.stars .star.on { -webkit-filter: none; filter: none; opacity: 1; animation: star-pop 0.45s cubic-bezier(.2, 1.6, .5, 1) both; }
.stars .star:nth-child(2) { width: 70px; height: 70px; }
@keyframes star-pop { from { transform: scale(0) rotate(-90deg); } to { transform: scale(1) rotate(0); } }
.confetti {
  position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px;
  animation: confetti-fall 2s linear infinite; pointer-events: none;
}
@keyframes confetti-fall {
  from { transform: translateY(0) rotate(0); }
  to { transform: translateY(110vh) rotate(720deg); }
}
#stage canvas, #stage div { -webkit-backface-visibility: hidden; backface-visibility: hidden; }
