:root {
  color-scheme: dark;
  --ink: #f6f7ff;
  --muted: #9aa8cf;
  --panel: rgba(21, 32, 68, 0.88);
  --panel-strong: rgba(26, 39, 80, 0.97);
  --line: rgba(151, 172, 235, 0.18);
  --gold: #ffc96b;
  --cyan: #69e4ff;
  --pink: #ff7bb0;
  --board-size: min(88vw, 410px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 0%, rgba(91, 180, 255, 0.22), transparent 34%),
    radial-gradient(circle at 92% 20%, rgba(186, 91, 255, 0.18), transparent 30%),
    linear-gradient(160deg, #0b1330 0%, #111b3c 45%, #1a1940 100%);
}

button { font: inherit; }

.app-shell {
  width: min(100%, 580px);
  margin: 0 auto;
  padding: 24px 16px 44px;
}

.topbar, .chapter-card, .section-heading, .hero-heading, .enemy-area, .battle-footer,
.bar-row, .enemy-meta, .result-actions {
  display: flex;
  align-items: center;
}

.topbar { justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.topbar-actions { display: flex; align-items: center; gap: 7px; }
.topbar h1, h2, h3, p { margin: 0; }
.topbar h1 { font-size: clamp(25px, 7vw, 34px); letter-spacing: -0.06em; }
.eyebrow { color: var(--cyan); font-size: 10px; font-weight: 800; letter-spacing: 0.18em; text-transform: uppercase; }
.topbar-badge, .tag, .section-note { color: var(--gold); font-size: 10px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
.topbar-badge { padding: 8px 10px; border: 1px solid rgba(255, 201, 107, 0.34); border-radius: 999px; background: rgba(255, 201, 107, 0.09); white-space: nowrap; }
.audio-toggle { display: inline-flex; align-items: center; gap: 4px; padding: 8px 9px; border: 1px solid rgba(105, 228, 255, 0.26); border-radius: 999px; color: var(--cyan); background: rgba(105, 228, 255, 0.08); cursor: pointer; font-size: 10px; font-weight: 800; }
.audio-toggle:hover { border-color: rgba(105, 228, 255, 0.62); background: rgba(105, 228, 255, 0.15); }
.audio-toggle.is-muted { border-color: rgba(255,255,255,0.13); color: var(--muted); background: rgba(255,255,255,0.06); }
.audio-icon { font-size: 15px; line-height: 1; }

.panel {
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(145deg, rgba(33, 50, 100, 0.9), rgba(17, 26, 57, 0.9));
  box-shadow: 0 18px 50px rgba(2, 7, 25, 0.26), inset 0 1px 0 rgba(255,255,255,0.06);
}

.chapter-card { justify-content: space-between; gap: 14px; padding: 16px 18px; margin-bottom: 12px; }
.chapter-card h2 { margin-top: 5px; font-size: 18px; }
.story-line { max-width: 320px; margin-top: 7px; color: #c4ccef; font-size: 11px; line-height: 1.45; }
.level-progress { display: flex; align-items: center; gap: 8px; min-width: 105px; color: var(--muted); font-size: 11px; font-weight: 800; }
.level-progress > span:first-child { color: var(--ink); }
.progress-track { width: 58px; height: 6px; overflow: hidden; border-radius: 99px; background: rgba(255,255,255,0.12); }
.progress-track i { display: block; width: 16%; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--cyan), var(--pink)); transition: width .25s ease; }

.battle-card { position: relative; overflow: hidden; padding: 16px; }
.enemy-area { justify-content: space-between; gap: 15px; padding: 2px 2px 12px; }
.enemy-copy { min-width: 0; flex: 1; }
.enemy-copy h3 { margin: 3px 0 9px; font-size: 20px; }
.enemy-avatar, .hero-avatar { position: relative; display: grid; place-items: center; flex: 0 0 auto; overflow: hidden; border: 1px solid rgba(255,255,255,0.16); border-radius: 50%; box-shadow: inset 0 0 20px rgba(255,255,255,0.16), 0 8px 25px rgba(0,0,0,0.24); }
.enemy-avatar { width: 82px; height: 82px; font-size: 48px; background: radial-gradient(circle at 35% 25%, #afffbc, #27846d 52%, #123c55); }
.enemy-avatar img { width: 142%; height: 142%; object-fit: contain; transform: translateY(7%); filter: drop-shadow(0 8px 8px rgba(0,0,0,0.25)); }
.enemy-fallback { line-height: 1; }
.hero-avatar { width: 52px; height: 52px; font-size: 30px; background: radial-gradient(circle at 35% 25%, #ffe7fa, #9875e9 55%, #283a86); }
.hero-avatar img { width: 165%; height: 165%; object-fit: contain; transform: translateY(14%); filter: drop-shadow(0 5px 5px rgba(0,0,0,0.25)); }
.bar-row { gap: 7px; font-size: 10px; color: var(--muted); }
.bar-row > span { width: 18px; font-weight: 800; }
.bar-row b { min-width: 65px; color: var(--ink); text-align: right; font-size: 10px; }
.bar { height: 9px; flex: 1; overflow: hidden; border-radius: 999px; background: rgba(7, 12, 34, 0.72); box-shadow: inset 0 1px 3px rgba(0,0,0,0.35); }
.bar i { display: block; width: 100%; height: 100%; border-radius: inherit; transition: width .35s ease; }
.enemy-bar i { background: linear-gradient(90deg, #ff7796, #ffbe63); }
.hero-bar i { background: linear-gradient(90deg, #65e0b6, #c6ff8e); }
.mana-bar i { background: linear-gradient(90deg, #56b9ff, #b68bff); }
.enemy-meta { justify-content: space-between; gap: 12px; margin-top: 8px; color: var(--muted); font-size: 10px; }
.enemy-meta span:last-child { color: var(--gold); text-align: right; }
.battle-message { min-height: 27px; padding: 7px 10px; border-radius: 10px; color: #d5dcff; background: rgba(7, 13, 35, 0.42); font-size: 12px; text-align: center; }

.board-wrap { position: relative; width: var(--board-size); max-width: 100%; margin: 12px auto 10px; }
.board-glow { position: absolute; inset: 8%; border-radius: 35%; background: rgba(89, 190, 255, 0.16); filter: blur(22px); pointer-events: none; }
.board { position: relative; display: grid; grid-template-columns: repeat(8, 1fr); gap: clamp(4px, 1.35vw, 7px); padding: clamp(8px, 2vw, 12px); border: 1px solid rgba(255,255,255,0.16); border-radius: 19px; background: linear-gradient(140deg, rgba(5, 13, 42, 0.88), rgba(28, 34, 83, 0.88)); box-shadow: inset 0 0 0 1px rgba(255,255,255,0.04), 0 12px 32px rgba(3,8,30,0.34); touch-action: none; user-select: none; }
.gesture-hint { display: flex; align-items: center; justify-content: center; gap: 5px; margin-top: 7px; color: #a8b9e8; font-size: 10px; font-weight: 700; letter-spacing: .02em; }
.gesture-finger { color: var(--cyan); font-size: 15px; }
.gem { position: relative; display: grid; place-items: center; aspect-ratio: 1; padding: 0; border: 0; border-radius: 26%; cursor: pointer; color: white; font-size: clamp(18px, 5.7vw, 28px); font-weight: 900; line-height: 1; transform: translateZ(0); transition: transform .13s ease, filter .13s ease, box-shadow .13s ease; user-select: none; -webkit-tap-highlight-color: transparent; }
.gem::before { content: ""; position: absolute; inset: 13%; border-radius: 30%; background: linear-gradient(135deg, rgba(255,255,255,0.62), transparent 34%); opacity: .7; pointer-events: none; }
.gem:hover { transform: translateY(-2px) scale(1.04); filter: brightness(1.12); }
.gem:focus-visible { outline: 2px solid white; outline-offset: 2px; }
.gem.selected { transform: scale(1.12); outline: 3px solid var(--gold); outline-offset: 2px; z-index: 2; }
.gem.swap-target { transform: scale(1.1); outline: 3px solid #fff; outline-offset: 2px; z-index: 3; filter: brightness(1.14); }
.gem.matched { animation: pop-away .22s ease forwards; }
.gem.special { box-shadow: 0 0 0 2px rgba(255,255,255,0.76), 0 0 17px rgba(255,255,255,0.46); }
.gem.special .special-mark { position: absolute; right: 8%; bottom: 4%; display: grid; place-items: center; width: 42%; height: 42%; border: 1px solid rgba(255,255,255,0.72); border-radius: 50%; background: rgba(8, 15, 43, 0.66); color: white; font-size: 42%; }
.gem-fire, .legend-gem.fire { background: linear-gradient(145deg, #ff8b78, #d83266); box-shadow: inset 0 2px 6px rgba(255,255,255,.34), 0 5px 12px rgba(222, 48, 105, .3); }
.gem-water, .legend-gem.water { background: linear-gradient(145deg, #6ee2ff, #2875db); box-shadow: inset 0 2px 6px rgba(255,255,255,.34), 0 5px 12px rgba(48, 131, 230, .3); }
.gem-leaf, .legend-gem.leaf { background: linear-gradient(145deg, #9cf7a5, #329a83); box-shadow: inset 0 2px 6px rgba(255,255,255,.34), 0 5px 12px rgba(45, 170, 126, .3); }
.gem-light, .legend-gem.light { background: linear-gradient(145deg, #fff09b, #ed9c44); box-shadow: inset 0 2px 6px rgba(255,255,255,.34), 0 5px 12px rgba(246, 167, 73, .3); }
.gem-void, .legend-gem.void { background: linear-gradient(145deg, #d69cff, #7146bd); box-shadow: inset 0 2px 6px rgba(255,255,255,.34), 0 5px 12px rgba(126, 74, 204, .3); }
@keyframes pop-away { to { transform: scale(.42) rotate(12deg); opacity: .1; } }

.battle-footer { justify-content: center; gap: 8px; }
.match-pill, .turn-pill, .moves-pill { display: flex; align-items: baseline; gap: 6px; padding: 7px 10px; border: 1px solid rgba(255,255,255,0.09); border-radius: 999px; background: rgba(5, 12, 35, 0.44); color: var(--muted); font-size: 10px; }
.match-pill strong, .turn-pill strong, .moves-pill strong { color: var(--ink); font-size: 14px; }
.match-pill strong { color: var(--gold); }

.hero-card { margin-top: 12px; padding: 16px; }
.hero-heading { gap: 10px; }
.hero-heading h3 { margin-top: 2px; font-size: 18px; }
.hero-statuses { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 5px; margin-left: auto; color: var(--muted); font-size: 10px; text-align: right; }
.hero-bar-row { margin-top: 14px; }
.mana-row { margin-top: 6px; }
.skill-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin-top: 14px; }
.skill-button { display: flex; align-items: center; gap: 7px; min-width: 0; padding: 9px 7px; border: 1px solid rgba(255,255,255,0.1); border-radius: 13px; color: var(--ink); background: rgba(8, 15, 41, 0.55); cursor: pointer; text-align: left; transition: transform .15s ease, border-color .15s ease, background .15s ease; }
.skill-button:hover:not(:disabled) { transform: translateY(-2px); border-color: rgba(105, 228, 255, .55); background: rgba(35, 62, 119, .7); }
.skill-button:disabled { cursor: not-allowed; opacity: .38; }
.skill-icon { font-size: 20px; }
.skill-button b, .skill-button small { display: block; white-space: nowrap; }
.skill-button b { font-size: 11px; }
.skill-button small { margin-top: 2px; color: var(--muted); font-size: 8px; }

.levels-card { margin-top: 12px; padding: 16px; }
.levels-card > summary { list-style: none; cursor: pointer; }
.levels-card > summary.section-heading { margin-bottom: 0; }
.levels-card > summary::-webkit-details-marker { display: none; }
.levels-card > summary::after { content: "⌄"; margin-left: auto; color: var(--cyan); font-size: 16px; transition: transform .2s ease; }
.levels-card[open] > summary { margin-bottom: 12px; }
.levels-card[open] > summary::after { transform: rotate(180deg); }
.section-heading { justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.section-heading h3 { margin-top: 3px; font-size: 18px; }
.section-note { color: var(--muted); font-size: 9px; letter-spacing: 0.04em; text-align: right; text-transform: none; }
.level-list { display: grid; grid-template-columns: repeat(6, 1fr); gap: 7px; }
.level-button { position: relative; display: grid; place-items: center; min-height: 44px; padding: 4px; border: 1px solid rgba(255,255,255,0.11); border-radius: 12px; color: var(--ink); background: rgba(8, 15, 41, .55); cursor: pointer; font-size: 12px; font-weight: 800; transition: transform .15s ease, border-color .15s ease; }
.level-button:hover:not(:disabled), .level-button.active { transform: translateY(-2px); border-color: var(--gold); }
.level-button.active { background: linear-gradient(145deg, rgba(255, 201, 107, .2), rgba(96, 89, 180, .25)); }
.level-button:disabled { cursor: not-allowed; opacity: .34; }
.level-button small { display: block; margin-top: 2px; color: var(--muted); font-size: 8px; font-weight: 600; }
.level-button .lock { position: absolute; top: 3px; right: 4px; font-size: 9px; }

.legend { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; padding: 12px 14px; }
.legend-title { color: var(--muted); font-size: 10px; font-weight: 800; white-space: nowrap; }
.legend-items { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px 10px; color: var(--muted); font-size: 9px; }
.legend-items span { display: inline-flex; align-items: center; gap: 4px; }
.legend-gem { display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 5px; color: white; font-size: 11px; font-style: normal; }

.result-backdrop { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 20px; background: rgba(3, 7, 23, .72); backdrop-filter: blur(8px); }
.result-backdrop.is-hidden { display: none; }
.result-card { width: min(100%, 360px); padding: 28px 22px 22px; text-align: center; }
.result-icon { font-size: 54px; filter: drop-shadow(0 8px 16px rgba(255, 201, 107, .35)); }
.result-card h2 { margin-top: 8px; font-size: 27px; }
.result-card p:not(.eyebrow) { margin-top: 8px; color: var(--muted); font-size: 13px; line-height: 1.6; }
.result-actions { justify-content: center; gap: 8px; margin-top: 20px; }
.primary-button, .secondary-button { padding: 11px 16px; border-radius: 11px; cursor: pointer; font-size: 12px; font-weight: 800; }
.primary-button { border: 1px solid rgba(255, 223, 145, .7); color: #2a2140; background: linear-gradient(135deg, #ffe08f, #ff9f81); box-shadow: 0 8px 20px rgba(255, 159, 129, .2); }
.secondary-button { border: 1px solid rgba(255,255,255,.16); color: var(--ink); background: rgba(255,255,255,.07); }

@media (max-width: 460px) {
  :root { --board-size: min(92vw, 360px); }
  body { min-height: 100dvh; overflow-x: hidden; overscroll-behavior-y: none; }
  .app-shell { width: 100%; min-height: 100dvh; padding: max(10px, env(safe-area-inset-top)) 10px max(14px, env(safe-area-inset-bottom)); }
  .topbar { gap: 8px; margin-bottom: 8px; }
  .topbar h1 { font-size: 25px; }
  .topbar-badge { display: none; }
  .audio-toggle { padding: 7px 10px; }
  .chapter-card { align-items: flex-start; gap: 8px; padding: 11px 13px; margin-bottom: 8px; }
  .chapter-card h2 { margin-top: 3px; font-size: 16px; }
  .story-line { max-width: 235px; margin-top: 5px; font-size: 10px; }
  .level-progress { min-width: 80px; gap: 5px; font-size: 9px; }
  .progress-track { width: 42px; height: 5px; }
  .battle-card { padding: 10px; border-radius: 18px; }
  .enemy-area { gap: 9px; padding: 0 0 7px; }
  .enemy-copy h3 { margin: 2px 0 6px; font-size: 17px; }
  .enemy-avatar { width: 64px; height: 64px; font-size: 36px; }
  .enemy-meta { margin-top: 5px; font-size: 9px; }
  .battle-message { min-height: 24px; padding: 5px 7px; font-size: 10px; }
  .board-wrap { margin: 9px auto 7px; }
  .board { gap: 3px; padding: 7px; border-radius: 15px; }
  .gem { font-size: clamp(17px, 5.8vw, 24px); border-radius: 22%; }
  .gesture-hint { margin-top: 5px; font-size: 9px; }
  .gesture-finger { font-size: 13px; }
  .battle-footer { gap: 5px; }
  .match-pill, .turn-pill, .moves-pill { padding: 5px 8px; font-size: 9px; }
  .match-pill strong, .turn-pill strong, .moves-pill strong { font-size: 12px; }
  .hero-card { position: relative; margin-top: 8px; padding: 10px; border-radius: 18px; background: rgba(22, 34, 72, .96); }
  .hero-avatar { width: 39px; height: 39px; font-size: 21px; }
  .hero-heading { gap: 7px; }
  .hero-heading h3 { font-size: 15px; }
  .hero-statuses { font-size: 9px; }
  .hero-bar-row { margin-top: 8px; }
  .mana-row { margin-top: 4px; }
  .bar { height: 7px; }
  .bar-row b { min-width: 57px; font-size: 9px; }
  .skill-grid { gap: 5px; margin-top: 8px; }
  .skill-button { flex-direction: row; align-items: center; padding: 7px 5px; border-radius: 10px; text-align: left; }
  .skill-icon { font-size: 17px; }
  .skill-button b { font-size: 10px; }
  .skill-button small { font-size: 7px; }
  .levels-card { margin-top: 8px; padding: 11px 12px; border-radius: 16px; }
  .levels-card > summary.section-heading { margin-bottom: 0; }
  .levels-card[open] > summary.section-heading { margin-bottom: 9px; }
  .section-heading h3 { font-size: 15px; }
  .section-note { font-size: 8px; }
  .level-list { grid-template-columns: repeat(6, 1fr); gap: 4px; }
  .level-button { min-height: 37px; border-radius: 9px; font-size: 10px; }
  .level-button small { font-size: 7px; }
  .legend { align-items: flex-start; flex-direction: column; gap: 6px; margin-top: 8px; padding: 9px 11px; }
  .legend-items { justify-content: flex-start; gap: 5px 8px; }
}

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