:root {
  --bg1: #fff3d6; --bg2: #ffe0e6; --bg3: #d9f3e4;
  --ink: #3b2a20; --ink-soft: #7a6558;
  --card: #ffffff; --line: #f0d9b8;
  --primary: #ff7a3d; --primary-d: #e65f22; --accent: #3fae5a;
  --good: #2e9e55; --bad: #e0453a; --gold: #ffb400;
  --radius: 18px; --shadow: 0 6px 0 rgba(120, 80, 40, .14), 0 10px 24px rgba(120, 80, 40, .12);
  --font: "Microsoft JhengHei", "微軟正黑體", "PingFang TC", "Noto Sans TC", "Noto Sans CJK TC", system-ui, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font); color: var(--ink); font-size: 16px; line-height: 1.5;
  background: linear-gradient(135deg, var(--bg1), var(--bg2) 55%, var(--bg3)) fixed;
  -webkit-tap-highlight-color: transparent;
}
body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none; opacity: .32; z-index: 0;
  background: url("../assets/pattern.svg") repeat; background-size: 220px;
}
.app { position: relative; z-index: 1; max-width: 960px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; }
.screen { min-height: 100vh; min-height: 100dvh; padding: 16px 16px 28px; display: flex; flex-direction: column; gap: 14px; animation: screenIn .35s ease both; }
@keyframes screenIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

button { font-family: inherit; color: inherit; cursor: pointer; }
button:focus-visible, .card:focus-visible { outline: 4px solid #2a6df4; outline-offset: 3px; }
.btn {
  border: 3px solid var(--ink); background: #fff; border-radius: 16px; padding: 10px 18px; font-size: 1.05rem; font-weight: 700;
  box-shadow: 0 4px 0 var(--ink); transition: transform .08s, box-shadow .08s, background .15s; min-height: 44px;
}
.btn:hover { background: #fff7e8; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.primary { background: var(--primary); color: #fff; text-shadow: 0 1px 0 rgba(0,0,0,.25); }
.btn.primary:hover { background: var(--primary-d); }
.btn.big { font-size: 1.2rem; padding: 14px 22px; width: 100%; }
.btn.small { padding: 6px 14px; font-size: .95rem; min-height: 38px; }
.btn.danger { background: #ffe8e5; border-color: var(--bad); color: var(--bad); box-shadow: 0 4px 0 var(--bad); }
.icon-btn { width: 46px; height: 46px; border-radius: 14px; border: 3px solid var(--ink); background: #fff; font-size: 1.3rem; box-shadow: 0 3px 0 var(--ink); }
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.topbar h2 { margin: 0; font-size: 1.5rem; }
.topbar > span:empty { min-width: 70px; }

/* 主選單 */
#screen-menu { align-items: center; text-align: center; justify-content: center; gap: 18px; }
#screen-menu .topbar { width: 100%; position: absolute; top: 12px; left: 0; padding: 0 16px; }
.menu-hero .logo { width: min(46vw, 190px); height: auto; filter: drop-shadow(0 8px 0 rgba(120, 80, 40, .15)); animation: bob 3.2s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-8px) rotate(2deg); } }
.menu-hero h1 { margin: 6px 0 0; font-size: clamp(2.4rem, 9vw, 3.6rem); letter-spacing: .08em; color: var(--primary-d); text-shadow: 3px 3px 0 #fff, 5px 5px 0 rgba(120, 80, 40, .2); }
.subtitle { margin: 0; font-size: 1.3rem; font-weight: 700; color: var(--accent); letter-spacing: .3em; }
.menu-actions { display: flex; flex-direction: column; gap: 12px; width: min(100%, 360px); }
.menu-daily { margin: 0; font-weight: 700; background: rgba(255,255,255,.7); padding: 6px 14px; border-radius: 999px; }
.foot-note { margin: 0; color: var(--ink-soft); font-size: .9rem; }

/* 模式選擇 */
.diff-bar { display: flex; align-items: center; gap: 8px; justify-content: center; flex-wrap: wrap; }
.diff-label { font-weight: 700; }
.seg { border: 3px solid var(--ink); background: #fff; border-radius: 999px; padding: 6px 20px; font-weight: 700; font-size: 1rem; min-height: 40px; }
.seg[aria-checked="true"] { background: var(--accent); color: #fff; box-shadow: 0 3px 0 #1f7a38; }
.mode-list { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); }
.mode-card {
  display: flex; flex-direction: column; gap: 6px; text-align: left; padding: 18px; border-radius: 22px; border: 3px solid var(--ink);
  background: #fff; box-shadow: var(--shadow); transition: transform .15s;
}
.mode-card:hover { transform: translateY(-4px) rotate(-.5deg); }
.mode-card:active { transform: translateY(1px); }
.mode-card[data-mode="memory"] { background: linear-gradient(160deg, #fff, #ffe9cf); }
.mode-card[data-mode="classify"] { background: linear-gradient(160deg, #fff, #dff5e5); }
.mode-card.daily { background: linear-gradient(160deg, #fff, #e6e0fb); }
.mode-art { font-size: 2.6rem; line-height: 1.1; }
.mode-name { font-size: 1.35rem; font-weight: 800; }
.mode-desc { color: var(--ink-soft); }
.mode-best { margin-top: auto; font-weight: 700; color: var(--primary-d); font-size: .95rem; }

/* 面板 */
.panel { background: rgba(255,255,255,.92); border: 3px solid var(--ink); border-radius: 22px; padding: 18px 20px; box-shadow: var(--shadow); }
.panel h3 { margin: 18px 0 6px; font-size: 1.15rem; }
.panel h3:first-child { margin-top: 0; }
.panel p, .panel li { margin: 4px 0; }
.panel ul { padding-left: 1.3rem; margin: 4px 0; }
.setting { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; font-weight: 700; }
.switch { width: 58px; height: 32px; border-radius: 999px; border: 3px solid var(--ink); background: #ddd; position: relative; padding: 0; transition: background .2s; }
.switch .knob { position: absolute; top: 2px; left: 2px; width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 2px solid var(--ink); transition: transform .2s; }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"] .knob { transform: translateX(26px); }
.warn { color: var(--bad); font-weight: 700; }
.table-wrap { overflow-x: auto; }
.table { border-collapse: collapse; width: 100%; font-size: .95rem; }
.table th, .table td { border: 2px solid var(--line); padding: 6px 8px; text-align: center; white-space: nowrap; }
.table th { background: #fff3d6; }
.list .row { padding: 6px 4px; border-bottom: 1px dashed var(--line); font-size: .95rem; }
.empty { color: var(--ink-soft); }
.danger-zone { margin-top: 18px; text-align: right; }

/* 遊戲 */
.game { padding: 10px 12px 18px; gap: 8px; position: relative; }
.hud { background: rgba(255,255,255,.92); border: 3px solid var(--ink); border-radius: 20px; padding: 8px 10px; box-shadow: var(--shadow); }
.hud-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hud-title { font-weight: 800; font-size: 1.1rem; text-align: center; flex: 1; }
.hud-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 6px 0; }
.stat { display: flex; flex-direction: column; align-items: center; background: #fff7e8; border-radius: 12px; padding: 2px 4px; min-width: 0; }
.stat-label { font-size: .72rem; color: var(--ink-soft); }
.stat-val { font-weight: 800; font-size: 1.15rem; white-space: nowrap; }
.stat-val.lives { font-size: .95rem; letter-spacing: -1px; }
#hud-combo.hot { color: var(--primary-d); animation: pulse .4s; }
@keyframes pulse { 50% { transform: scale(1.25); } }
.bars { display: grid; gap: 4px; }
.bar-row { display: grid; grid-template-columns: 38px 1fr 52px; align-items: center; gap: 6px; font-size: .8rem; }
.bar-val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.bar { height: 12px; background: #eadfcf; border-radius: 999px; overflow: hidden; border: 2px solid var(--ink); }
.bar-fill { height: 100%; width: 0; border-radius: 999px; }
.bar-fill.timer { background: linear-gradient(90deg, #4cc36f, #ffd23f); transition: width .06s linear; }
.bar-fill.progress { background: linear-gradient(90deg, #5aa9ff, #8b6fd0); transition: width .3s; }
#timer-wrap.low .bar-fill.timer { background: var(--bad); }
#timer-wrap.low .bar-val { color: var(--bad); animation: pulse .5s infinite; }

.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 10px; padding-top: 8px; min-height: 0; }
.prompt { margin: 4px 0 0; font-size: 1.35rem; font-weight: 800; text-align: center; min-height: 2em; background: rgba(255,255,255,.75); border-radius: 999px; padding: 2px 18px; }
.feedback { margin: 0; min-height: 3em; max-width: 640px; text-align: center; font-weight: 700; }
.feedback.good { color: var(--good); } .feedback.bad { color: var(--bad); }
.board { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--gap, 12px); max-width: 100%; }
.board-classify { flex-direction: column; align-items: center; flex-wrap: nowrap; width: min(100%, 560px); gap: 14px; }

/* 食材記憶卡 */
.card { width: var(--card-w, 90px); height: var(--card-h, 108px); padding: 0; border: 0; background: none; perspective: 700px; position: relative; will-change: transform; }
.card-inner { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .45s cubic-bezier(.4, .2, .2, 1.2); border-radius: 14px; }
.card.flipped .card-inner { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 14px; border: 3px solid var(--ink); backface-visibility: hidden; -webkit-backface-visibility: hidden; display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: 0 4px 0 rgba(0,0,0,.2); overflow: hidden; }
.face.back { background: url("../assets/cardback.svg") center / cover, #ff9d5c; }
body:not([data-cardback="back-classic"]) .face.back { background: var(--cb-img, url("../assets/cardback.svg")) center / cover, #ff9d5c; }
.face.front { background: #fff; transform: rotateY(180deg); gap: 2px; }
.pos { position: absolute; top: 4px; left: 6px; font-size: .8rem; font-weight: 800; color: #fff; background: rgba(60,30,10,.55); border-radius: 999px; min-width: 22px; text-align: center; padding: 0 5px; }
.c-emoji { font-size: calc(var(--card-w, 90px) * .46); line-height: 1.1; }
.c-name { font-size: calc(var(--card-w, 90px) * .17); font-weight: 800; white-space: nowrap; }
.board:not(.is-locked) .card:hover .face.back { filter: brightness(1.08); }
.board:not(.is-locked) .card { cursor: pointer; }
.card.correct .face { border-color: var(--good); box-shadow: 0 0 0 4px rgba(46,158,85,.45); }
.card.wrong .face { border-color: var(--bad); box-shadow: 0 0 0 4px rgba(224,69,58,.45); }
.card.wrong { animation: shake .4s; }
.card.reveal .face { border-color: var(--gold); box-shadow: 0 0 0 4px rgba(255,180,0,.55); }
.card.correct { animation: hop .45s; }
@keyframes shake { 20% { transform: translateX(-7px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(5px); } }
@keyframes hop { 40% { transform: translateY(-12px) scale(1.06); } }
#btn-ready { min-width: 200px; }

/* 分類快答 */
.q-card { width: min(78vw, 250px); aspect-ratio: 1 / 1.05; background: #fff; border: 4px solid var(--cat-color, var(--ink)); border-radius: 28px; display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: 0 8px 0 rgba(0,0,0,.15); position: relative; max-height: 34vh; }
.q-card.enter { animation: popIn .35s cubic-bezier(.3, 1.5, .5, 1); }
.q-card.good { animation: hop .45s; background: #e9fbef; }
.q-card.bad { animation: shake .4s; background: #fdeceb; }
@keyframes popIn { from { transform: scale(.6) rotate(-6deg); opacity: 0; } to { transform: none; opacity: 1; } }
.q-emoji { font-size: clamp(3.6rem, 16vh, 6.4rem); line-height: 1.1; }
.q-name { font-size: 1.7rem; font-weight: 800; }
.opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; width: 100%; }
.opt { position: relative; display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 58px; border: 3px solid var(--ink); border-radius: 16px; background: #fff; font-size: 1.15rem; font-weight: 800; box-shadow: 0 4px 0 var(--cat-color, var(--ink)); transition: transform .08s, background .15s; padding: 6px 10px 6px 30px; }
.opt .key { position: absolute; left: 6px; top: 6px; font-size: .7rem; width: 18px; height: 18px; line-height: 18px; text-align: center; border-radius: 6px; background: var(--ink); color: #fff; }
.opt-emoji { font-size: 1.5rem; }
.board:not(.is-locked) .opt:hover { background: #fff7e8; transform: translateY(-2px); }
.board:not(.is-locked) .opt:active { transform: translateY(2px); }
.board.is-locked .opt { cursor: default; }
.opt.correct { background: #c9f2d6; border-color: var(--good); animation: hop .45s; }
.opt.wrong { background: #ffd4d0; border-color: var(--bad); animation: shake .4s; }

/* 回饋 */
.pops { position: fixed; left: 0; right: 0; top: 30%; display: flex; justify-content: center; pointer-events: none; z-index: 20; }
.pop { position: absolute; font-size: 2.4rem; font-weight: 900; text-shadow: 0 3px 0 #fff, 0 0 12px #fff; animation: floatUp .9s ease-out forwards; }
.pop.good { color: var(--good); } .pop.bad { color: var(--bad); }
@keyframes floatUp { from { transform: translateY(0) scale(.7); opacity: 0; } 15% { opacity: 1; transform: translateY(-10px) scale(1.2); } to { transform: translateY(-80px) scale(1); opacity: 0; } }
.game.flash-good::after, .game.flash-bad::after { content: ""; position: fixed; inset: 0; pointer-events: none; z-index: 15; animation: flash .5s ease-out forwards; }
.game.flash-good::after { background: radial-gradient(circle, transparent 50%, rgba(46,158,85,.28)); }
.game.flash-bad::after { background: radial-gradient(circle, transparent 50%, rgba(224,69,58,.35)); }
@keyframes flash { from { opacity: 1; } to { opacity: 0; } }
.banner { position: fixed; left: 50%; top: 42%; transform: translate(-50%, -50%); z-index: 25; background: #fff; border: 4px solid var(--ink); border-radius: 24px; padding: 20px 34px; text-align: center; box-shadow: var(--shadow); pointer-events: none; }
.banner.show { animation: bannerIn .5s cubic-bezier(.3, 1.4, .5, 1); }
@keyframes bannerIn { from { transform: translate(-50%, -50%) scale(.5); opacity: 0; } to { transform: translate(-50%, -50%); opacity: 1; } }
.banner-title { font-size: 1.8rem; font-weight: 900; color: var(--primary-d); }
.banner-sub { font-weight: 700; color: var(--ink-soft); }

.overlay { position: fixed; inset: 0; background: rgba(59, 42, 32, .6); z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog { background: #fff; border: 4px solid var(--ink); border-radius: 24px; padding: 20px; width: min(100%, 360px); display: flex; flex-direction: column; gap: 12px; text-align: center; box-shadow: var(--shadow); }
.dialog h2 { margin: 0 0 4px; }

/* 結果 */
#screen-results { justify-content: center; align-items: center; }
.panel.result { width: min(100%, 560px); text-align: center; }
.res-badge { font-size: 4rem; animation: bob 2.5s ease-in-out infinite; }
.panel.result h2 { margin: 0; font-size: 2rem; }
.res-sub { color: var(--ink-soft); font-weight: 700; margin: 4px 0; }
.stars { font-size: 3rem; letter-spacing: 6px; margin: 4px 0; }
.star { color: #d9cdbd; display: inline-block; opacity: 0; animation: starIn .5s cubic-bezier(.3, 1.8, .5, 1) forwards; }
.star.on { color: var(--gold); text-shadow: 0 3px 0 #b87d00; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); opacity: 0; } to { transform: none; opacity: 1; } }
.res-score { font-size: 3.2rem; font-weight: 900; color: var(--primary-d); line-height: 1.1; }
.res-score small { font-size: 1.1rem; margin-left: 4px; color: var(--ink); }
.new-best { display: block; font-size: 1.1rem; color: var(--good); animation: pulse 1s infinite; }
.res-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 12px 0; }
.cell { background: #fff7e8; border-radius: 14px; padding: 6px; display: flex; flex-direction: column; }
.cell-label { font-size: .8rem; color: var(--ink-soft); } .cell-val { font-weight: 800; font-size: 1.15rem; }
.res-missed { text-align: left; margin-bottom: 10px; }
.miss-title { font-weight: 800; margin-bottom: 4px; }
.chip { display: inline-block; margin: 3px 4px 3px 0; padding: 3px 10px; border-radius: 999px; background: #ffe8e5; border: 2px solid var(--bad); font-weight: 700; font-size: .9rem; }
.daily-card { background: linear-gradient(160deg, #f0ebff, #fff); border: 3px dashed #8b6fd0; border-radius: 18px; padding: 10px; margin-bottom: 12px; }
.dc-title { font-weight: 800; } .daily-card pre { margin: 6px 0; font-family: inherit; white-space: pre-wrap; font-weight: 700; }
.res-actions { display: flex; flex-direction: column; gap: 10px; }
.fatal { padding: 2rem; font-size: 1.2rem; color: var(--bad); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 20px; border-radius: 999px; z-index: 60; font-weight: 700; }

@media (max-width: 560px) {
  .screen { padding: 12px 10px 22px; }
  .prompt { font-size: 1.1rem; }
  .q-card { width: min(60vw, 200px); }
  .opts { grid-template-columns: repeat(2, 1fr); }
  .opts .opt:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .stat-val { font-size: 1rem; }
  .hud-title { font-size: .95rem; }
  .btn.big { font-size: 1.1rem; }
}
@media (min-width: 900px) { .hud-stats { max-width: 560px; margin-left: auto; margin-right: auto; } .bars { max-width: 560px; margin: 0 auto; } }
@media (max-height: 520px) and (orientation: landscape) { .q-card { max-height: 30vh; } .q-emoji { font-size: 3rem; } }

.reduce-motion *, .reduce-motion *::before, .reduce-motion *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
@media (prefers-reduced-motion: reduce) {
  .menu-hero .logo, .res-badge, .new-best { animation: none; }
  .card-inner { transition-duration: .01ms; }
}

/* 經典玩法：改用原創 SVG（art.js），不使用 emoji */
.art-wrap { display: inline-flex; align-items: center; justify-content: center; line-height: 0; }
.art-wrap svg, .inline-art svg, .mode-art svg { display: block; width: 100%; height: 100%; }
.q-art { width: clamp(4.4rem, 18vh, 7.6rem); height: clamp(4.4rem, 18vh, 7.6rem); }
.c-art { width: calc(var(--card-w, 90px) * .62); height: calc(var(--card-w, 90px) * .62); }
.opt-art { width: 2rem; height: 2rem; flex: none; }
.inline-art { display: inline-block; width: 1.5em; height: 1.5em; vertical-align: -.4em; margin-right: 4px; }
.mode-art { display: flex; gap: 6px; height: 3.2rem; }
.mode-art svg { width: 3.2rem; height: 3.2rem; }
.mode-art svg.mode-ic { width: 2.6rem; height: 2.6rem; margin-top: .3rem; color: var(--ink); }
.prompt .ic svg, .feedback .ic svg, .stat-val .ic svg, .btn [data-icon] svg, .icon-btn svg, #btn-pause-sound svg { width: 1.1em; height: 1.1em; vertical-align: -.18em; margin-right: 4px; }
.icon-btn svg { margin: 0; width: 1.4rem; height: 1.4rem; }
.prompt .inline-art { width: 1.7em; height: 1.7em; }
.stat-val.lives { display: inline-flex; gap: 1px; color: #e0453a; }
.stat-val.lives svg { width: 1.05rem; height: 1.05rem; }
.stat-val.lives svg.off { color: #6b5a50; opacity: .6; }
.res-badge svg.badge-ic { width: 4.2rem; height: 4.2rem; color: var(--gold, #ffb400); }
.res-badge svg.detective-svg { width: 5.2rem; height: 5.6rem; }
.chip .inline-art { width: 1.4em; height: 1.4em; }
