:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #1d4ed8;
  --primary-soft: #dbeafe;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --warn-soft: #fef9c3;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --card: #151b2e;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --line: #263049;
    --primary: #60a5fa;
    --primary-soft: #1e3a6e;
    --ok: #4ade80;
    --ok-soft: #12361f;
    --bad: #f87171;
    --bad-soft: #3b1518;
    --warn-soft: #3a3512;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.55 -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
#app { max-width: 640px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.3; }

/* layout */
.page { padding: 20px 16px 96px; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.topbar .title { flex: 1; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }
.iconbtn {
  width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent;
  font-size: 22px; display: grid; place-items: center;
}
.iconbtn:active { background: var(--line); }
.iconbtn.on { color: #f59e0b; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stack > * + * { margin-top: 12px; }
.muted { color: var(--muted); font-size: 14px; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { justify-content: space-between; }

/* buttons */
.btn {
  display: block; width: 100%; padding: 14px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); text-align: left; font-weight: 600;
}
.btn small { display: block; font-weight: 400; color: var(--muted); margin-top: 2px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; text-align: center; }
.btn.primary small { color: rgba(255,255,255,.8); }
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { transform: scale(.99); }
.menu .btn { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.badge { padding: 2px 10px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); font-size: 13px; font-weight: 700; white-space: nowrap; }

.seg { display: flex; background: var(--line); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; border: 0; font-size: 14px; white-space: nowrap; background: transparent; padding: 9px; border-radius: 10px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stats b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--primary); }

/* question */
.qhead { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.qno { font-weight: 800; color: var(--primary); }
.qtext { font-size: 17px; font-weight: 600; margin: 0 0 12px; word-break: keep-all; }
.qimg { display: block; width: 100%; max-height: 52vh; object-fit: contain; border-radius: 10px; background: var(--line); margin: 0 0 12px; cursor: zoom-in; }
.notes { margin: 0 0 12px; padding: 10px 12px 10px 28px; background: var(--warn-soft); border-radius: 10px; font-size: 14px; }
.choices { display: grid; gap: 8px; }
.choice {
  display: flex; gap: 10px; align-items: flex-start; text-align: left; width: 100%;
  padding: 12px 14px; border: 2px solid var(--line); border-radius: 12px; background: var(--card);
  word-break: keep-all;
}
.choice .n { flex: none; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 14px; font-weight: 700; background: var(--line); }
.choice.sel { border-color: var(--primary); background: var(--primary-soft); }
.choice.sel .n { background: var(--primary); color: #fff; }
.choice.correct { border-color: var(--ok); background: var(--ok-soft); }
.choice.correct .n { background: var(--ok); color: #fff; }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); }
.choice.wrong .n { background: var(--bad); color: #fff; }
.choice[disabled] { cursor: default; }
.hint { font-size: 13px; color: var(--primary); font-weight: 700; }
.result-line { margin: 14px 0 0; font-weight: 800; font-size: 18px; }
.result-line.ok { color: var(--ok); }
.result-line.bad { color: var(--bad); }
.explain { margin-top: 10px; padding: 12px 14px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); font-size: 15px; white-space: pre-wrap; word-break: keep-all; }
.explain h4 { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
.explain .qimg { max-height: 40vh; }

.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  background: var(--bg); border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}
.bottombar .inner { max-width: 640px; margin: 0 auto; display: flex; gap: 8px; }
.bottombar .btn { text-align: center; }
.bottombar .btn.sm { flex: 0 0 30%; }

/* palette */
.palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.palette button { aspect-ratio: 1; border: 1px solid var(--line); border-radius: 8px; background: var(--card); font-weight: 600; font-size: 14px; }
.palette button.done { background: var(--primary-soft); border-color: var(--primary); }
.palette button.cur { outline: 2px solid var(--primary); }
.palette button.ok { background: var(--ok-soft); border-color: var(--ok); }
.palette button.no { background: var(--bad-soft); border-color: var(--bad); }

/* result */
.score { text-align: center; padding: 24px 16px; }
.score .num { font-size: 56px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.score .tag { display: inline-block; margin-top: 8px; padding: 4px 16px; border-radius: 999px; font-weight: 800; }
.score .tag.pass { background: var(--ok-soft); color: var(--ok); }
.score .tag.fail { background: var(--bad-soft); color: var(--bad); }

.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.5); display: flex; align-items: flex-end; justify-content: center; }
.sheet > div { width: 100%; max-width: 640px; background: var(--bg); border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); max-height: 80vh; overflow: auto; }

#lightbox { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.92); display: grid; place-items: center; padding: 8px; cursor: zoom-out; }
#lightbox[hidden], #toast[hidden] { display: none; }
#lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 40; background: #111827; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px; }

/* 언어 선택 */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button { padding: 9px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--card); font-weight: 600; }
.chips button.on { background: var(--primary); border-color: var(--primary); color: #fff; }
/* 한국어 어절 단위 줄바꿈(keep-all)은 다른 언어에선 맞지 않는다 */
[lang]:not([lang="ko"]) { word-break: normal; overflow-wrap: anywhere; }
.ko-sub { display: block; margin-top: 4px; font-size: 13px; font-weight: 400; color: var(--muted); word-break: keep-all; }
.choice.sel .ko-sub, .choice.correct .ko-sub, .choice.wrong .ko-sub { color: inherit; opacity: .75; }
.qtext .ko-sub { font-size: 14px; margin-top: 6px; }

.footer { margin-top: 24px; font-size: 12px; color: var(--muted); text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 48px 0; }
