/* ===== 雙位數進位加法大冒險 ===== */

:root {
  --bg: #FFF7EC;
  --bg-2: #FFEED6;
  --card: #FFFFFF;
  --ink: #10182B;
  --ink-2: #3B4A63;
  --muted: #6B7A91;
  --line: #E3E9F2;
  --brand: #1D6BF3;
  --brand-dark: #1149B4;
  --brand-soft: #E7F0FF;
  --accent: #FFB020;
  --accent-soft: #FFF3D6;
  --ok: #128A45;
  --ok-soft: #E4F8EC;
  --bad: #D62828;
  --bad-soft: #FDECEC;
  --shadow: 0 6px 0 rgba(16, 24, 43, .10), 0 12px 28px rgba(16, 24, 43, .10);
  --shadow-sm: 0 3px 0 rgba(16, 24, 43, .10);
  --radius: 20px;
  --font: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", "Hiragino Sans CNS", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* hidden 屬性優先，避免被 display 規則蓋過 */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 620px at 12% -12%, #FFF0D6 0%, rgba(255, 240, 214, 0) 62%),
    radial-gradient(900px 520px at 92% 0%, #E4F0FF 0%, rgba(228, 240, 255, 0) 60%),
    var(--bg);
  min-height: 100dvh;
  padding-bottom: max(18px, env(safe-area-inset-bottom));
  overflow-x: hidden;
}

/* ---------- 頂欄 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  background: rgba(255, 247, 236, .92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.pill-btn {
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  color: var(--ink);
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 999px;
  min-height: 44px;
  padding: 9px 13px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform .08s ease, background .15s ease;
  white-space: nowrap;
}
.pill-btn:active { transform: translateY(2px); box-shadow: none; }

.sound-btn[aria-pressed="false"] {
  background: var(--bad-soft);
  border-color: #F6C9C9;
  color: #8E1F1F;
}

.level-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: 0;
  padding: 2px 6px;
}
.chip-num {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--brand);
}
.chip-name {
  font-size: clamp(17px, 4.6vw, 22px);
  font-weight: 900;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* ---------- 進度 ---------- */
.progress-wrap { padding: 8px 16px 0; max-width: 760px; margin: 0 auto; width: 100%; }
.progress-track {
  height: 12px;
  border-radius: 999px;
  background: #F0E3CE;
  overflow: hidden;
  border: 1px solid #E7D7BD;
}
.progress-fill {
  height: 100%;
  width: 10%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), #FF8A00);
  transition: width .3s ease;
}
.progress-text {
  margin: 6px 0 0;
  font-size: 14px;
  font-weight: 800;
  color: var(--ink-2);
  text-align: center;
}

/* ---------- 版面 ---------- */
.stage {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 10px 12px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 場景插圖：獨立區塊，永不覆蓋算式 */
.scene {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 3px solid #fff;
  background: #EDE4D6;
}
.scene-img {
  display: block;
  width: 100%;
  height: clamp(76px, 11.5vh, 190px);
  object-fit: cover;
  object-position: center 38%;
  transition: height .22s ease;
}
.scene-tag {
  position: absolute;
  left: 10px;
  bottom: 10px;
  background: rgba(255, 255, 255, .94);
  color: var(--ink);
  font-size: 13px;
  font-weight: 900;
  padding: 5px 11px;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(16, 24, 43, .18);
}

.board {
  background: var(--card);
  border: 3px solid #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 12px 14px;
}

/* ---------- 直式算式 ---------- */
.vmath {
  display: grid;
  grid-template-columns: 1.15em repeat(3, minmax(0, 1fr));
  row-gap: .16em;
  align-items: center;
  justify-items: center;
  width: 100%;
  max-width: min(320px, 84vw);
  margin: 2px auto 0;
  font-size: clamp(28px, 8.6vw, 50px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.vrow { display: contents; }
.vcell {
  width: 100%;
  text-align: center;
  font-size: 1em;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.vop { color: var(--brand); font-weight: 900; }

.vline {
  grid-column: 1 / -1;
  height: 0;
  width: 100%;
  border-top: .11em solid var(--ink);
  margin: .1em 0 .12em;
  border-radius: 2px;
}

.ansbox {
  width: 100%;
  height: clamp(52px, 14vw, 84px);
  font-family: inherit;
  font-size: clamp(28px, 8vw, 46px);
  font-weight: 900;
  text-align: center;
  color: var(--brand-dark);
  background: #F7FAFF;
  border: 3px solid #BFD4F5;
  border-radius: 14px;
  padding: 0;
  outline: none;
  caret-color: transparent;
  cursor: pointer;
  transition: border-color .12s ease, box-shadow .12s ease, background .12s ease;
  -webkit-appearance: none;
  appearance: none;
}
.ansbox.active {
  background: #FFFFFF;
  border-color: var(--brand);
  box-shadow: 0 0 0 5px rgba(29, 107, 243, .22);
}
.ansbox.wrong {
  background: var(--bad-soft);
  border-color: var(--bad);
  animation: shake .42s cubic-bezier(.36, .07, .19, .97);
}
.ansbox.right {
  background: var(--ok-soft);
  border-color: var(--ok);
}
@keyframes shake {
  10%, 90% { transform: translateX(-3px); }
  20%, 80% { transform: translateX(5px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}

.start-tip {
  margin: 12px 0 0;
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  color: var(--muted);
}

/* ---------- 提示 ---------- */
.hint {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--accent-soft);
  border: 2px solid #FFD98A;
  border-radius: 16px;
  animation: pop .22s ease;
}
.hint-img { width: 46px; height: 56px; object-fit: contain; flex: 0 0 auto; }
.hint p {
  margin: 0;
  font-size: clamp(15px, 4.2vw, 18px);
  font-weight: 800;
  line-height: 1.45;
  color: #7A4A00;
}
@keyframes pop { from { transform: scale(.94); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* 顯示進位提示時收緊版面，確保提交按鈕仍在畫面內
   （只在單欄版面生效；平板橫向的雙欄版面垂直空間充足，毋須壓縮插圖） */
@media (max-width: 879px), (orientation: portrait) {
  body.hint-on .scene-img { height: 52px; }
  body.hint-on .board { padding: 10px 10px 12px; }
  body.hint-on .start-tip { display: none; }
  body.hint-on .hint { padding: 8px 10px; margin-top: 10px; }
  body.hint-on .hint-img { width: 34px; height: 40px; }
  body.hint-on .hint p { font-size: 14.5px; line-height: 1.4; }
  body.hint-on .keypad { gap: 6px; margin-top: 10px; }
  body.hint-on .key { min-height: 50px; font-size: clamp(22px, 6.6vw, 32px); }
  body.hint-on .key-fn { font-size: clamp(17px, 4.8vw, 23px); }
  body.hint-on .submit-btn { min-height: 56px; margin-top: 10px; }
}

/* ---------- 數字鍵盤 ---------- */
.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(7px, 2vw, 11px);
  margin-top: 12px;
}
.key {
  font-family: inherit;
  font-size: clamp(26px, 7.6vw, 36px);
  font-weight: 900;
  color: var(--ink);
  background: #FFFFFF;
  border: 2px solid var(--line);
  border-bottom-width: 5px;
  border-radius: 16px;
  min-height: clamp(48px, 7.6vh, 70px);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: transform .06s ease, background .12s ease;
  user-select: none;
}
.key:active { transform: translateY(3px); border-bottom-width: 2px; background: var(--brand-soft); }
.key-fn { font-size: clamp(20px, 5.6vw, 27px); background: #F4F7FC; color: var(--ink-2); }
.key-sub { font-size: 11px; font-weight: 800; color: var(--muted); margin-top: 3px; letter-spacing: .04em; }

.submit-btn {
  width: 100%;
  margin-top: 12px;
  font-family: inherit;
  font-size: clamp(21px, 5.6vw, 26px);
  font-weight: 900;
  letter-spacing: .06em;
  color: #fff;
  background: linear-gradient(180deg, #2E86FF, var(--brand-dark));
  border: none;
  border-bottom: 6px solid #0C3A90;
  border-radius: 18px;
  min-height: clamp(58px, 9vh, 76px);
  cursor: pointer;
  transition: transform .06s ease;
}
.submit-btn:active { transform: translateY(4px); border-bottom-width: 2px; }

/* ---------- 浮層 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(16, 24, 43, .55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fade .18s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.modal {
  position: relative;
  width: 100%;
  max-width: 430px;
  max-height: 92dvh;
  overflow-y: auto;
  background: var(--card);
  border-radius: 26px;
  padding: 20px 18px 22px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(16, 24, 43, .3);
  animation: rise .24s cubic-bezier(.2, .9, .3, 1.2);
}
@keyframes rise { from { transform: translateY(18px) scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }

.hero { display: block; width: min(46vw, 190px); margin: -6px auto 4px; }
.hero.sm { width: min(38vw, 150px); }
.modal.celebrate .hero { width: min(52vw, 220px); animation: bounce .7s ease infinite alternate; }
@keyframes bounce { from { transform: translateY(0); } to { transform: translateY(-10px); } }

.deco { position: absolute; pointer-events: none; opacity: .95; }
.deco-confetti { width: 130px; top: -8px; right: -14px; transform: rotate(14deg); }

.modal-title { margin: 4px 0 8px; font-size: clamp(23px, 6.2vw, 30px); font-weight: 900; }
.praise { margin: 4px 0 12px; font-size: clamp(21px, 5.6vw, 26px); font-weight: 900; color: var(--ok); }
.eq-note { margin: 0 0 14px; font-size: clamp(17px, 4.6vw, 20px); font-weight: 800; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.big-line { margin: 6px 0; font-size: clamp(16px, 4.4vw, 19px); font-weight: 800; color: var(--ink-2); }
.muted { color: var(--muted); font-size: 14px; font-weight: 700; margin: 6px 0 0; }
.tiny { font-size: 12px; }

.stars { display: flex; justify-content: center; gap: 6px; margin: 6px 0 10px; }
.star-svg { width: 38px; height: 38px; display: block; }
.star-svg.dim { opacity: .85; }
.star-svg.pop { animation: starPop .5s cubic-bezier(.2, .9, .3, 1.6) backwards; }
.star-svg:nth-child(2) { animation-delay: .14s; }
.star-svg:nth-child(3) { animation-delay: .28s; }
.star-svg.tiny-star { width: 13px; height: 13px; display: inline-block; vertical-align: -1px; }
@keyframes starPop { from { transform: scale(.2) rotate(-40deg); opacity: 0; } to { transform: none; opacity: 1; } }

.deco-stars {
  position: absolute;
  width: 108px;
  top: -10px;
  left: -18px;
  transform: rotate(-12deg);
  pointer-events: none;
}

.ach-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 14px; }
.ach-cell { background: var(--brand-soft); border-radius: 14px; padding: 10px 4px; }
.ach-cell b { display: block; font-size: clamp(22px, 6vw, 28px); font-weight: 900; color: var(--brand-dark); }
.ach-cell small { font-size: 11px; font-weight: 800; color: var(--ink-2); }

.btn-row { display: flex; flex-direction: column; gap: 9px; margin-top: 4px; }

.btn {
  font-family: inherit;
  font-weight: 900;
  border-radius: 16px;
  cursor: pointer;
  border: none;
  padding: 14px 18px;
  font-size: 18px;
  transition: transform .06s ease;
}
.btn:active { transform: translateY(3px); }
.btn.xl { font-size: clamp(20px, 5.4vw, 25px); padding: 16px 18px; width: 100%; }
.btn.primary {
  color: #fff;
  background: linear-gradient(180deg, #2E86FF, var(--brand-dark));
  border-bottom: 6px solid #0C3A90;
}
.btn.primary:active { border-bottom-width: 2px; }
.btn.ghost { background: #F1F5FB; color: var(--ink-2); border: 2px solid var(--line); }
.btn.danger { background: var(--bad); color: #fff; border-bottom: 6px solid #8E1F1F; }
.btn.danger-ghost { background: #fff; color: var(--bad); border: 2px solid #F6C9C9; font-size: 16px; padding: 11px 16px; }

/* ---------- 關卡選擇 ---------- */
.sheet-overlay { align-items: flex-end; padding: 0; }
.sheet {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 26px 26px 0 0;
  padding: 16px 14px calc(20px + env(safe-area-inset-bottom));
  animation: sheetUp .26s cubic-bezier(.2, .9, .3, 1.05);
}
@keyframes sheetUp { from { transform: translateY(60px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 900; }

.level-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 620px) { .level-grid { grid-template-columns: repeat(3, 1fr); } }

.lv-card {
  font-family: inherit;
  text-align: left;
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: 18px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform .08s ease;
  position: relative;
}
.lv-card:active { transform: translateY(2px); }
.lv-card img { display: block; width: 100%; height: 78px; object-fit: cover; }
.lv-card .lv-body { display: block; padding: 7px 9px 10px; }
.lv-card .lv-no { display: block; font-size: 11px; font-weight: 900; color: var(--brand); letter-spacing: .06em; }
.lv-card .lv-nm { display: block; font-size: 16px; font-weight: 900; line-height: 1.2; }
.lv-card .lv-st {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lv-card.locked { cursor: not-allowed; opacity: .62; }
.lv-card.locked img { filter: grayscale(.85); }
.lv-card.locked .lv-body::after {
  content: "🔒 未解鎖";
  position: absolute;
  inset: auto 0 0 0;
  background: rgba(255, 255, 255, .92);
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-align: center;
  padding: 4px 0;
}
.lv-card.current { border-color: var(--brand); box-shadow: 0 0 0 4px rgba(29, 107, 243, .18); }
.lv-card.done .lv-st { color: var(--ok); }
.lv-stars { display: inline-flex; gap: 1px; }
.lv-stars img { width: 13px; height: 13px; object-fit: contain; }
.lv-stars .dim { filter: grayscale(1) opacity(.3); }

.sheet-foot { margin-top: 16px; display: flex; flex-direction: column; align-items: center; gap: 6px; }

/* ---------- 矮屏幕（如 iPhone SE）收緊版面 ---------- */
@media (max-height: 700px) {
  .topbar { padding-top: 6px; padding-bottom: 6px; }
  .progress-wrap { padding-top: 6px; }
  .scene-img { height: clamp(56px, 8.6vh, 110px); }
  .vmath { font-size: clamp(26px, 8vw, 40px); }
  .ansbox { height: clamp(46px, 12.6vw, 70px); font-size: clamp(24px, 7.4vw, 40px); }
  .start-tip { margin-top: 8px; font-size: 12px; }
  .keypad { gap: 6px; margin-top: 10px; }
  .key { min-height: 46px; border-radius: 14px; }
  .submit-btn { min-height: 54px; margin-top: 10px; }
  .board { padding: 10px 10px 12px; }
}

@media (max-height: 700px) and (max-width: 879px) {
  body.hint-on .scene-img { height: 44px; }
  body.hint-on .key { min-height: 44px; }
}

/* ---------- 橫向／平板 ---------- */
@media (min-width: 880px) and (orientation: landscape) {
  .stage {
    max-width: 1080px;
    display: grid;
    grid-template-columns: 1.05fr 1fr;
    align-items: start;
    gap: 16px;
    padding-top: 14px;
  }
  .scene { position: sticky; top: 84px; }
  .scene-img { height: min(62dvh, 460px); }
  .board { padding: 18px 18px 20px; }
}

@media (min-width: 880px) {
  .keypad { grid-template-columns: repeat(3, 1fr); }
  .vmath { font-size: 46px; max-width: 380px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
