/* ============================================================
   基礎樣式：色彩、字體、頁首頁尾、按鈕、卡片、對話框與動態偏好
   ============================================================ */
:root {
  --ink: #0b1220;
  --night: #111a2c;
  --night-2: #18243b;
  --night-3: #22314c;
  --paper: #f6ead2;
  --paper-2: #fdf7e8;
  --amber: #f2a33c;
  --amber-2: #e3b45c;
  --ember: #d9643a;
  --jade: #4f9c8f;
  --plum: #6f73a8;
  --mist: #aab3c9;
  --charcoal: #3a4050;
  --line: rgba(246, 234, 210, 0.2);
  --card: rgba(18, 28, 47, 0.9);
  --card-2: rgba(26, 39, 63, 0.86);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
  --radius: 22px;
  --header-h: 64px;
  --footer-h: 76px;
  --max: 1120px;
  --font-body: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", system-ui, sans-serif;
  --font-display: "Songti TC", "Noto Serif TC", "PingFang TC", serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: calc(var(--header-h) + 12px); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--paper);
  background: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.65;
  overflow-x: hidden;
}
body::selection, ::selection { background: var(--amber); color: var(--ink); }
h1, h2, h3, p, dl, dd, ul, ol, figure { margin: 0; }
ul, ol { padding-left: 1.3em; }
h1, h2, h3 { line-height: 1.25; font-family: var(--font-display); letter-spacing: 0.02em; }
h1 { font-size: clamp(2rem, 5vw, 3.6rem); }
h2 { font-size: clamp(1.35rem, 2.8vw, 2rem); }
h3 { font-size: 1.08rem; }
a { color: var(--amber-2); }
button, input, select { font: inherit; }
button { color: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
img { max-width: 100%; height: auto; object-fit: contain; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--amber); outline-offset: 3px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.skip-link {
  position: fixed; left: 12px; top: -80px; z-index: 100;
  padding: 10px 16px; border-radius: 999px; background: var(--amber); color: var(--ink); font-weight: 700;
}
.skip-link:focus { top: 12px; }

/* 背景：固定裝飾層，永遠在內容下方，不攔截滑鼠 */
.bg-art { position: fixed; inset: 0; z-index: -2; pointer-events: none; background: radial-gradient(circle at 80% 8%, #2b3554 0, #111a2c 38%, #080d17 100%); }
.bg-art__img {
  position: absolute; inset: 0; background-position: center; background-size: cover; background-repeat: no-repeat;
  opacity: 0.18; filter: saturate(0.78) contrast(1.05); transition: opacity 0.6s ease;
}
.bg-art__img.is-swap { animation: bg-swap 0.65s ease; }
.bg-art__grain {
  position: absolute; inset: 0; opacity: 0.22;
  background-image:
    repeating-linear-gradient(0deg, rgba(246,234,210,0.035) 0, rgba(246,234,210,0.035) 1px, transparent 1px, transparent 4px),
    radial-gradient(circle at 50% 100%, rgba(242,163,60,0.12), transparent 55%);
}
.game { min-height: 100vh; display: flex; flex-direction: column; }

/* 頁首 */
.app-header {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: var(--header-h);
  padding: calc(8px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-left)) 8px max(16px, env(safe-area-inset-right));
  background: rgba(10, 16, 29, 0.98);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(242, 163, 60, 0.22);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand__mark {
  flex: none; display: grid; place-items: center; width: 38px; height: 42px; border-radius: 9px;
  background: linear-gradient(180deg, #d9643a, #8e3b46); border: 1px solid rgba(246,234,210,0.45);
  box-shadow: inset 0 0 0 3px rgba(11,18,32,0.35);
}
.brand__lamp { display: block; width: 15px; height: 20px; border-radius: 50% 50% 44% 44%; background: var(--amber); box-shadow: 0 0 18px rgba(242,163,60,0.85); }
.brand__text { display: flex; flex-direction: column; min-width: 0; }
.brand__title { font-family: var(--font-display); font-weight: 700; font-size: clamp(1rem, 2.4vw, 1.3rem); letter-spacing: 0.05em; white-space: nowrap; }
.brand__sub { font-size: 0.68rem; color: var(--mist); letter-spacing: 0.12em; white-space: nowrap; }
.header-controls { display: flex; align-items: center; gap: 6px; flex: none; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border-radius: 12px; border: 1px solid rgba(246,234,210,0.24);
  background: rgba(246,234,210,0.06); color: var(--paper); cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:hover { background: rgba(242,163,60,0.18); border-color: var(--amber); }
.icon-btn:active { transform: scale(0.95); }
.icon-btn[aria-pressed="true"] { background: var(--amber); color: var(--ink); }

/* 主要內容與頁尾 */
main {
  flex: 1 0 auto;
  width: 100%;
  padding: 22px max(16px, env(safe-area-inset-left)) calc(var(--footer-h) + env(safe-area-inset-bottom) + 28px) max(16px, env(safe-area-inset-right));
}
.screen { width: 100%; max-width: var(--max); margin: 0 auto; }
.screen.is-enter { animation: screen-in 0.36s ease both; }
.app-footer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
  padding: 10px max(16px, env(safe-area-inset-left)) calc(10px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-right));
  background: rgba(8, 13, 23, 0.98);
  border-top: 1px solid rgba(242,163,60,0.25);
  backdrop-filter: blur(10px);
  text-align: center;
}
.app-footer p { margin: 0; font-size: 0.76rem; line-height: 1.5; color: var(--mist); overflow-wrap: anywhere; }
.app-footer a { color: var(--amber-2); text-decoration: underline; text-underline-offset: 2px; }

/* 共用元件 */
.card {
  background: var(--card);
  border: 1px solid rgba(242, 163, 60, 0.24);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(18px, 3vw, 30px);
  overflow-wrap: anywhere;
}
.kicker { color: var(--amber); font-size: 0.76rem; letter-spacing: 0.18em; font-weight: 700; text-transform: uppercase; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 4px 11px; border-radius: 999px;
  background: rgba(246,234,210,0.08); border: 1px solid rgba(246,234,210,0.2);
  color: var(--paper); font-size: 0.78rem; white-space: nowrap; font-variant-numeric: tabular-nums;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 20px; border-radius: 999px; border: 1px solid transparent;
  font-weight: 700; letter-spacing: 0.03em; cursor: pointer; text-decoration: none;
  transition: transform 0.16s ease, background 0.18s ease, border-color 0.18s ease;
  overflow-wrap: anywhere;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn--primary { background: linear-gradient(180deg, #ffc266, var(--amber)); color: #24160a; box-shadow: 0 6px 0 #9b5a18, 0 12px 24px rgba(0,0,0,0.28); }
.btn--primary:hover { background: linear-gradient(180deg, #ffd287, #f6ad4b); }
.btn--soft { background: rgba(246,234,210,0.08); border-color: rgba(246,234,210,0.26); color: var(--paper); }
.btn--soft:hover { background: rgba(246,234,210,0.14); }
.btn--ghost { background: transparent; border-color: transparent; color: var(--mist); }
.btn--ghost:hover { color: var(--paper); background: rgba(246,234,210,0.08); }
.btn--danger { background: #8e3b46; color: var(--paper); border-color: #c15b68; }
.btn--xl { min-height: 54px; padding: 12px 30px; font-size: 1.08rem; }
.section-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px 18px; margin-bottom: 16px; }
.section-head p { color: var(--mist); font-size: 0.9rem; }
.screen-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 14px; margin: 4px 0 16px; }
.screen-lead { color: var(--mist); max-width: 70ch; margin: 0 0 20px; }

/* 提示訊息（系統列，不是題目控制項） */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--footer-h) + env(safe-area-inset-bottom) + 14px); z-index: 50;
  transform: translate(-50%, 16px); max-width: min(92vw, 560px);
  padding: 11px 18px; border-radius: 14px; background: rgba(8,13,23,0.96); border: 1px solid var(--amber);
  color: var(--paper); font-size: 0.9rem; text-align: center; opacity: 0; pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease; overflow-wrap: anywhere;
}
.toast.is-show { opacity: 1; transform: translate(-50%, 0); }

/* 紙花 */
.confetti { position: fixed; inset: 0; z-index: 60; pointer-events: none; overflow: hidden; }
.confetti i {
  position: absolute; top: -14px; width: 8px; height: 13px; border-radius: 2px;
  animation: confetti-fall 1.3s ease-in forwards;
}

/* 密技提示（系統覆蓋層） */
.cheat-overlay {
  position: fixed; left: 50%; top: 50%; z-index: 70; transform: translate(-50%, -50%);
  padding: 22px 30px; border-radius: 20px; text-align: center; pointer-events: none;
  background: rgba(8,13,23,0.96); border: 1px solid var(--amber); box-shadow: 0 0 60px rgba(242,163,60,0.4);
  opacity: 0;
}
.cheat-overlay.is-show { animation: cheat-show 2.8s ease both; }
.cheat-overlay span { display: block; color: var(--amber); letter-spacing: 0.3em; font-size: 1.1rem; }
.cheat-overlay b { display: block; margin-top: 6px; font-size: 1.3rem; }

/* 對話框 */
.dialog {
  width: min(620px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid rgba(242,163,60,0.4); border-radius: 20px;
  background: #111a2c; color: var(--paper); box-shadow: 0 30px 90px rgba(0,0,0,0.6);
  overflow: hidden;
}
.dialog::backdrop { background: rgba(3, 7, 14, 0.72); backdrop-filter: blur(4px); }
.dialog[open] { display: flex; flex-direction: column; animation: dialog-in 0.22s ease both; }
.dialog__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px 12px; border-bottom: 1px solid var(--line); flex: none; }
.dialog__head h2 { font-size: 1.35rem; }
.dialog__close { width: 44px; height: 44px; font-size: 1.1rem; }
.dialog__body { padding: 18px 20px; overflow-y: auto; min-height: 0; }
.dialog__body p { margin: 0 0 10px; color: var(--mist); }
.dialog__foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 20px 18px; border-top: 1px solid var(--line); flex: none; }
.dialog__lead { color: var(--paper) !important; }
.dialog__note { font-size: 0.78rem; margin-top: 8px !important; }
.steps-list { margin: 8px 0 0; padding-left: 1.25em; color: var(--paper); }
.steps-list li { margin: 6px 0; }
.kbd { display: inline-block; padding: 1px 7px; border-radius: 5px; background: rgba(246,234,210,0.12); border: 1px solid var(--line); }

/* 設定列 */
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 11px 0; border-bottom: 1px solid rgba(246,234,210,0.1); }
.setting-row b { display: block; font-size: 0.98rem; }
.setting-row small { display: block; color: var(--mist); font-size: 0.78rem; }
.switch {
  position: relative; flex: none; width: 58px; height: 44px; padding: 0; border: 0;
  background: transparent; cursor: pointer;
}
.switch::before {
  content: ""; position: absolute; left: 3px; right: 3px; top: 8px; height: 28px;
  border-radius: 999px; border: 1px solid rgba(246,234,210,0.3); background: rgba(246,234,210,0.12);
  transition: background 0.2s ease, border-color 0.2s ease;
}
.switch span { position: absolute; left: 6px; top: 11px; width: 22px; height: 22px; border-radius: 50%; background: var(--mist); transition: transform 0.2s ease, background 0.2s ease; }
.switch[aria-checked="true"]::before { background: var(--jade); border-color: #8ad9ca; }
.switch[aria-checked="true"] span { transform: translateX(21px); background: var(--paper-2); }
.volume-row { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 10px; padding: 8px 0 12px; color: var(--mist); font-size: 0.86rem; }
.volume-row input[type="range"] {
  width: 100%; height: 44px; margin: 0; padding: 0; background: transparent;
  -webkit-appearance: none; appearance: none; cursor: pointer;
}
.volume-row input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: 999px; background: rgba(246,234,210,0.24); }
.volume-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 22px; height: 22px; margin-top: -8px;
  border-radius: 50%; border: 2px solid #ffd287; background: var(--amber);
}
.volume-row input[type="range"]::-moz-range-track { height: 6px; border-radius: 999px; background: rgba(246,234,210,0.24); }
.volume-row input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; border: 2px solid #ffd287; background: var(--amber); }
.setting-block { padding: 14px 0; border-bottom: 1px solid rgba(246,234,210,0.1); }
.setting-block p { font-size: 0.82rem; margin: 4px 0 10px; }
.setting-block--danger { border-bottom: 0; }
.offline-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.setting-state { font-size: 0.8rem; color: var(--amber-2); }

@media (max-width: 520px) {
  :root { --header-h: 64px; --footer-h: 82px; }
  .app-header { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .brand { gap: 8px; }
  .brand__mark { width: 32px; height: 36px; }
  .brand__lamp { width: 12px; height: 17px; }
  .brand__sub { font-size: 0.61rem; letter-spacing: 0.06em; }
  .icon-btn { width: 44px; height: 44px; border-radius: 11px; }
  .icon-btn svg { width: 21px; height: 21px; }
  .header-controls { gap: 4px; }
  main { padding-top: 16px; }
  .app-footer p { font-size: 0.7rem; }
  .setting-row { padding: 7px 0; }
  .volume-row { padding: 4px 0 8px; }
  .setting-block { padding: 12px 0; }
}
@media (max-height: 540px) {
  :root { --footer-h: 0px; }
  .app-footer { position: static; }
  main { padding-bottom: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}
html.reduce-motion *, html.reduce-motion *::before, html.reduce-motion *::after {
  animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important;
}

/* 矮螢幕（例如 360×640）改用正常流的頁尾，避免固定頁尾蓋住題目與作答區 */
@media (max-height: 700px) and (max-width: 820px) {
  :root { --footer-h: 0px; }
  .app-footer { position: static; }
  main { padding-bottom: 22px; }
}
