:root {
  --paper: #f5f1e6;
  --grid: rgba(29, 42, 68, 0.06);
  --ink: #1d2a44;
  --ink-soft: #5b6680;
  --card: #fffdf7;
  --line: #e3dccb;
  --hl: #ffd84d;          /* 招牌螢光筆黃 */
  --hl-soft: #fff3c2;
  --ok: #23935f;
  --ok-soft: #e3f5ea;
  --bad: #d2453f;
  --bad-soft: #fbe7e5;
  --radius: 14px;
  --sans: 'Noto Sans HK', system-ui, -apple-system, 'PingFang HK', sans-serif;
  --math: 'STIX Two Text', 'Times New Roman', serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
  min-height: 100dvh;
}

button { font-family: inherit; color: inherit; cursor: pointer; border: none; background: none; }

.screen {
  display: none;
  max-width: 520px;
  margin: 0 auto;
  /* Extra bottom space so the fixed “Powered by Netlify” badge doesn’t cover 提交 / 下一題 */
  padding: max(16px, env(safe-area-inset-top)) 16px max(96px, calc(72px + env(safe-area-inset-bottom)));
  min-height: 100dvh;
  flex-direction: column;
  gap: 16px;
}
.screen.active { display: flex; animation: fadeIn 0.25s ease; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- 主頁 ---------- */
.brand { display: flex; align-items: center; gap: 14px; padding: 12px 0 4px; }
.logo {
  width: 56px; height: 56px; border-radius: 16px;
  background: var(--ink); color: var(--hl);
  display: grid; place-items: center;
  font-family: var(--math); font-style: italic; font-size: 30px;
  position: relative; transform: rotate(-4deg);
  box-shadow: 4px 4px 0 var(--hl);
}
.logo sup { position: absolute; right: 8px; top: 8px; font-size: 14px; font-style: normal; }
.brand h1 { margin: 0; font-size: 24px; font-weight: 900; letter-spacing: 0.02em; }
.brand .sub { margin: 2px 0 0; font-size: 13px; color: var(--ink-soft); letter-spacing: 0.04em; }

.block h2 { font-size: 17px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }

.lang-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.lang-card {
  background: var(--card); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 18px 12px; text-align: left; display: flex; flex-direction: column; gap: 6px;
  transition: transform 0.12s, border-color 0.15s, box-shadow 0.15s;
}
.lang-card:active { transform: scale(0.97); }
.lang-card.selected { border-color: var(--ink); box-shadow: 4px 4px 0 var(--ink); background: var(--hl-soft); }
.lang-big { font-size: 22px; font-weight: 900; }
.foot { font-size: 13px; color: var(--ink-soft); margin: 8px 0 0; text-align: center; }

.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.mode-card {
  background: var(--card); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 16px 12px; text-align: left; display: flex; flex-direction: column; gap: 6px;
  transition: transform 0.12s, border-color 0.15s, box-shadow 0.15s;
}
.mode-card:active { transform: scale(0.97); }
.mode-card.selected { border-color: var(--ink); box-shadow: 4px 4px 0 var(--ink); background: var(--hl-soft); }
.mode-big { font-size: 18px; font-weight: 900; }
.mode-eg { font-size: 12px; color: var(--ink-soft); line-height: 1.4; }

.sheet {
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
}
.sheet summary {
  list-style: none;
  cursor: pointer;
  font-weight: 700;
  font-size: 15px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.sheet summary::-webkit-details-marker { display: none; }
.sheet summary::after {
  content: '▾';
  font-size: 14px;
  color: var(--ink-soft);
  transition: transform 0.15s;
}
.sheet[open] summary::after { transform: rotate(-180deg); }
.sheet-body { padding: 0 14px 20px; display: flex; flex-direction: column; gap: 18px; }
.sheet-group h3 {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  border-bottom: 1.5px solid var(--line);
  padding-bottom: 4px;
}
.sheet-cluster {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 6px 10px;
  align-items: center;
}
.sheet-cluster + .sheet-cluster { margin-top: 10px; }
/* 扇形圖左邊空白裁掉後，縮窄預留位，把闊度俾周界公式一行顯示 */
.sheet-cluster[data-fig="sector"] {
  grid-template-columns: 64px 1fr;
  gap: 6px 8px;
}
.sheet-cluster[data-fig="sector"] .sheet-fig { justify-content: flex-start; }
.sheet-cluster[data-fig="sector"] .fig-svg { max-width: 64px; }
.sheet-cluster[data-fig="sector"] .sheet-row { flex-wrap: nowrap; }
.sheet-cluster[data-fig="sector"] .sheet-name { flex: 0 0 auto; }
.sheet-cluster[data-fig="sector"] .sheet-formula { flex: 0 1 auto; white-space: nowrap; }
.sheet-fig { display: flex; align-items: center; justify-content: center; }
.sheet-fig .fig-svg { max-width: 96px; max-height: 84px; width: 100%; height: auto; }
.sheet-rows { min-width: 0; }
.sheet-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 5px 0;
  flex-wrap: wrap;
}
.sheet-name { font-size: 14px; font-weight: 500; flex: 0 1 auto; }
.sheet-formula { font-family: var(--math); font-size: 22px; display: flex; align-items: center; flex: 1 1 auto; }

.site-credit {
  margin-top: auto;
  padding-top: 16px;
  text-align: center;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-soft);
  letter-spacing: 0.01em;
}
.site-credit a {
  color: var(--ink);
  font-weight: 700;
  text-decoration: none;
}
.site-credit a:hover,
.site-credit a:active { text-decoration: underline; text-underline-offset: 3px; }

/* ---------- 按鈕 ---------- */
.btn-primary, .btn-secondary, .btn-ghost {
  border-radius: var(--radius); font-weight: 700; font-size: 16px; padding: 14px 16px;
  transition: transform 0.1s;
}
.btn-primary { background: var(--ink); color: #fff; box-shadow: 0 4px 0 #0c1426; }
.btn-primary:active:not(:disabled), .btn-secondary:active { transform: translateY(3px); box-shadow: 0 1px 0 #0c1426; }
#btn-start { margin: 2px 0 4px; }
.btn-primary:disabled,
.btn-primary.is-muted {
  opacity: 0.42;
  box-shadow: none;
  cursor: not-allowed;
  transform: none;
}
.btn-secondary { background: var(--hl); color: var(--ink); box-shadow: 0 4px 0 #c9a520; }
.btn-ghost { color: var(--ink-soft); text-decoration: underline; text-underline-offset: 3px; font-weight: 500; }
.big { width: 100%; font-size: 18px; padding: 17px; }

/* ---------- 答題 ---------- */
#screen-quiz { gap: 12px; }
.quiz-top { display: flex; align-items: center; gap: 10px; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; background: var(--card); border: 1.5px solid var(--line); font-size: 15px; }
.progress { flex: 1; height: 10px; background: var(--line); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--ink); border-radius: 999px; transition: width 0.35s ease; }
.counter { font-size: 13px; font-weight: 700; min-width: 36px; text-align: right; }
.lang-toggle { font-size: 12px; font-weight: 700; padding: 7px 10px; border-radius: 999px; background: var(--hl); }

.question-card {
  background: var(--card); border-radius: var(--radius); padding: 16px 16px 14px;
  border: 2px solid var(--ink); box-shadow: 4px 4px 0 var(--ink);
}
.q-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.topic-tag, .mode-tag {
  display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 6px;
}
.topic-tag { background: var(--hl-soft); border: 1px solid var(--hl); }
.mode-tag { background: var(--card); border: 1px solid var(--line); color: var(--ink-soft); }
.q-guide { margin: 10px 0 4px; }
.q-figure { display: flex; justify-content: center; }
.fig-svg { width: 100%; max-width: 280px; height: auto; max-height: 160px; display: block; }
.fig-svg .fig-label {
  font-family: var(--math);
  font-style: italic;
  font-size: 16px;
  fill: #1d2a44;
  stroke: none;
}
.q-hint {
  margin: 8px 0 0;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.45;
  color: var(--ink);
  background: var(--hl-soft);
  border-radius: 8px;
  padding: 8px 10px;
}
.q-text { font-size: 20px; line-height: 1.45; margin: 10px 0 8px; font-weight: 700; }
mark.q-key {
  background: transparent;
  background-image: linear-gradient(transparent 58%, var(--hl) 58%, var(--hl) 94%, transparent 94%);
  color: inherit;
  font-weight: inherit;
  padding: 0 0.12em;
  border-radius: 0;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.q-legend { margin: 0; display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--ink-soft); }
.q-legend .m-var { color: var(--ink); font-size: 16px; }

.answer-zone {
  flex: 1; min-height: 120px; background: #fff; border-radius: var(--radius);
  border: 2px dashed var(--line); padding: 10px 14px; display: flex; flex-direction: column;
  transition: background 0.2s, border-color 0.2s;
}
.answer-zone.is-correct { border: 2px solid var(--ok); background: var(--ok-soft); }
.answer-zone.is-wrong { border: 2px solid var(--bad); background: var(--bad-soft); }
.answer-label { font-size: 12px; color: var(--ink-soft); font-weight: 700; }
.answer-display {
  flex: 1; display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  font-family: var(--math); font-size: 34px; padding: 8px 0; overflow-x: auto;
}
.answer-msg { margin: 0; min-height: 18px; font-size: 13px; color: var(--bad); text-align: center; font-weight: 500; }
.placeholder { font-family: var(--sans); font-size: 15px; color: #b3ad9c; }

/* ---------- 數學排版 ---------- */
.m-var { font-family: var(--math); font-style: italic; padding: 0 1px; }
.m-pi, .m-num { font-family: var(--math); font-style: normal; }
.m-op { margin: 0 0.22em; font-family: var(--math); }
.m-imp { display: inline-block; width: 0.06em; }
.m-sup { font-size: 0.6em; vertical-align: 0.85em; margin-left: 1px; line-height: 0; }
.m-base { display: inline-flex; align-items: center; }
.m-paren { font-family: var(--math); font-size: 1.1em; }
.m-ghost { opacity: 0.25; }
.m-raw { margin: 0 2px; }
.m-frac {
  display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle;
  margin: 0 0.15em; font-size: 0.82em;
}
.m-numer, .m-denom {
  display: flex; align-items: center; justify-content: center;
  padding: 0 0.18em; min-height: 1.1em; min-width: 0.75em;
  border-radius: 3px;
}
.m-numer { border-bottom: 2px solid currentColor; padding-bottom: 1px; }
.m-denom { padding-top: 1px; }
.answer-display .m-numer,
.answer-display .m-denom { cursor: pointer; }
.m-numer.is-active, .m-denom.is-active { background: rgba(232, 196, 80, 0.45); }
.m-sqrt { display: inline-flex; align-items: stretch; }
.m-rad { font-size: 1.1em; }
.m-radicand { border-top: 2px solid currentColor; padding: 2px 3px 0; display: inline-flex; align-items: center; margin-top: 0.18em; }
.m-hole {
  display: inline-block; width: 0.7em; height: 0.85em; border: 1.5px dashed #b7b09f; border-radius: 3px; margin: 0 2px;
}
.m-caret {
  display: inline-block; width: 2px; height: 1em; background: var(--ink); margin: 0 1px;
  animation: blink 1s steps(1) infinite; vertical-align: middle;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- 鍵盤：左字母、右數字，三組顏色（字母／數字／運算功能） ---------- */
.pad-area { display: flex; flex-direction: column; gap: 8px; }
.keypad {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}
.pad-letters, .pad-nums {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.pad-bottom {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 2fr 1fr 2fr;
  gap: 6px;
}
.key {
  height: 50px; border-radius: 10px; background: var(--card); font-size: 20px;
  border: 1.5px solid var(--line); box-shadow: 0 3px 0 var(--line);
  display: flex; align-items: center; justify-content: center; gap: 2px;
  transition: transform 0.06s, box-shadow 0.06s;
  user-select: none;
}
.key:active { transform: translateY(3px); box-shadow: 0 0 0 var(--line); }
.k-var { font-family: var(--math); font-style: italic; font-size: 24px; background: #fff; }
.k-pi { font-style: normal; }
.k-num { font-family: var(--math); font-size: 21px; background: var(--card); }
.k-op { font-family: var(--math); background: #eef0f5; border-color: #d8dce6; box-shadow: 0 3px 0 #c9cfdc; }
.k-op i { font-family: var(--math); }
.k-op sup { font-size: 12px; margin-top: -10px; }
.k-ctrl { font-size: 15px; font-weight: 700; background: #eef0f5; border-color: #d8dce6; box-shadow: 0 3px 0 #c9cfdc; }
.k-ratio { font-family: var(--math); font-size: 28px; font-weight: 700; }
.frac-chips {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.frac-chip {
  height: 52px;
  border-radius: 10px;
  background: var(--card);
  border: 1.5px solid var(--line);
  box-shadow: 0 3px 0 var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--math);
  font-size: 20px;
  user-select: none;
}
.frac-chip:active { transform: translateY(3px); box-shadow: 0 0 0 var(--line); }
.frac-chip .m-frac { margin: 0; font-size: 1em; }
.frac-chip .m-numer, .frac-chip .m-denom { cursor: default; min-width: 0.6em; }
.frac-chip .m-var { font-style: italic; }

.pad-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.pad-actions #btn-reveal { align-self: center; }
#btn-submit { width: 100%; font-size: 18px; padding: 17px; }

/* ---------- 回饋 ---------- */
.feedback {
  background: var(--card); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px;
  border: 2px solid var(--ink); animation: slideUp 0.25s ease;
}
.feedback.ok { border-color: var(--ok); }
.feedback.bad { border-color: var(--bad); }
@keyframes slideUp { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }
.fb-title { margin: 0; font-size: 18px; font-weight: 900; }
.feedback.ok .fb-title { color: var(--ok); }
.feedback.bad .fb-title { color: var(--bad); }
.fb-row { display: flex; align-items: center; gap: 12px; }
.fb-explain {
  margin: 0;
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--hl-soft);
  border-radius: 10px;
  padding: 10px 12px;
}
.fb-label { font-size: 12px; font-weight: 700; color: var(--ink-soft); min-width: 56px; }
.fb-formula { font-family: var(--math); font-size: 28px; display: flex; align-items: center; flex-wrap: wrap; }
.fb-formula.muted { color: var(--ink-soft); font-size: 22px; text-decoration: line-through; text-decoration-color: var(--bad); }
.feedback.ok .fb-formula { background: linear-gradient(transparent 60%, var(--hl) 60%); }

/* ---------- 總結 ---------- */
.score-card {
  background: var(--ink); color: #fff; border-radius: 20px; padding: 26px 20px; text-align: center;
  box-shadow: 6px 6px 0 var(--hl); margin-top: 12px;
}
.score-label { margin: 0; font-size: 14px; color: var(--hl); font-weight: 700; letter-spacing: 0.1em; }
.score { margin: 6px 0; font-size: 72px; font-weight: 900; font-family: var(--math); line-height: 1; }
.score-den { font-size: 32px; opacity: 0.6; }
.score-msg { margin: 0; font-size: 15px; line-height: 1.5; }
.all-correct { margin: 0; font-size: 15px; color: var(--ok); font-weight: 600; }
.review-list, .wrong-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.review-item, .wrong-list li { background: var(--card); border: 1.5px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.review-title, .wrong-list p { margin: 0 0 8px; font-size: 16px; font-weight: 700; }
.review-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
  flex-wrap: wrap;
}
.review-list .fb-formula, .wrong-list .fb-formula { font-size: 22px; }
.review-empty { font-family: var(--sans); font-size: 14px; color: var(--ink-soft); font-style: normal; }
.summary-actions { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }

.shake { animation: shake 0.4s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

@media (max-height: 700px) {
  .key { height: 44px; }
  .q-text { font-size: 18px; }
  .answer-zone { min-height: 88px; }
  .answer-display { font-size: 30px; }
  .fig-svg { max-height: 130px; }
}
