:root {
  color-scheme: light dark;
  --header-height: 52px;
  --bg: #f3f6f7;
  --surface: #fff;
  --passage: #edf6f5;
  --text: #203139;
  --muted: #566a70;
  --border: #cbd8d9;
  --accent: #176e68;
  --accent-soft: #e3f2ef;
  --correct: #167247;
  --correct-bg: #e6f5eb;
  --wrong: #ad343b;
  --wrong-bg: #fff0f0;
  --shadow: 0 3px 12px rgb(25 55 58 / 12%);
}

* { box-sizing: border-box; }
html { font-family: THSarabunNewDoc, sans-serif; font-size: 21px; line-height: 1.55; }
body { min-height: 100dvh; margin: 0; background: var(--bg); color: var(--text); overflow-wrap: anywhere; }
button, input { font: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.qz-top {
  position: sticky; top: 0; z-index: 20; height: var(--header-height);
  display: flex; align-items: center; gap: 10px; padding: 4px 16px;
  background: var(--surface); box-shadow: var(--shadow);
}
.qz-back { color: var(--accent); flex: none; }
.qz-top h1 { min-width: 0; margin: 0; font-size: 1.1rem; line-height: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.qz-progress { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: .85rem; white-space: nowrap; color: var(--muted); }
.qz-bar { width: clamp(30px, 10vw, 100px); height: 6px; overflow: hidden; border-radius: 99px; background: var(--border); }
.qz-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s ease; }
.qz-layout { min-width: 0; }
.qz-passage {
  display: flex; flex-direction: column;
  background: var(--passage); box-shadow: var(--shadow);
}
.qz-passage-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 16px; flex: none; }
.qz-passage-label { font-weight: bold; min-width: 0; }
#qz-passage-toggle { min-height: 40px; padding: 2px 12px; border: 1px solid var(--border); border-radius: 8px; color: var(--accent); background: var(--surface); flex: none; }
.qz-passage-body { padding: 0 16px 12px; min-height: 0; }
.qz-passage-body p { margin: 0 0 .6em; text-indent: 2em; }
.qz-passage.is-collapsed .qz-passage-body { display: none; }
.qz-feed { width: 100%; max-width: 712px; margin: 0 auto; padding: 16px; min-width: 0; }
.qz-card, .qz-summary { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); min-width: 0; }
.qz-meta { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; color: var(--muted); font-size: .9rem; }
.qz-no { font-weight: bold; color: var(--accent); }
.qz-ref { color: var(--accent); font-size: .9rem; }
.qz-stem { margin: .5em 0; font-weight: bold; }
.qz-choices { display: grid; gap: 8px; }
.qz-choice { width: 100%; min-height: 48px; display: flex; align-items: center; gap: 12px; text-align: left; padding: 7px 12px; border: 1px solid var(--border); border-radius: 10px; color: var(--text); background: var(--surface); line-height: 1.4; }
.qz-choice:not(:disabled):hover { border-color: var(--accent); background: var(--accent-soft); }
.qz-choice:disabled { cursor: default; }
.qz-letter { flex: 0 0 32px; width: 32px; height: 32px; display: grid; place-items: center; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: bold; line-height: 1; }
.qz-text { min-width: 0; }
.qz-choice.is-correct { border-color: var(--correct); background: var(--correct-bg); }
.qz-choice.is-correct .qz-letter { color: white; background: var(--correct); }
.qz-choice.is-wrong { border-color: var(--wrong); background: var(--wrong-bg); }
.qz-choice.is-wrong .qz-letter { color: white; background: var(--wrong); }
.qz-choice.is-dim { opacity: .6; }
.qz-explain { margin-top: 12px; padding: 12px 14px; border-left: 4px solid var(--correct); border-radius: 8px; background: var(--correct-bg); scroll-margin-top: calc(var(--header-height) + 12px); }
.qz-next { display: block; width: 100%; min-height: 48px; margin-top: 12px; padding: 6px 16px; border: 1px solid var(--accent); border-radius: 9px; background: var(--accent); color: white; font-weight: bold; }
.qz-explain:has(.qz-bad) { border-color: var(--wrong); background: var(--wrong-bg); }
.qz-verdict { font-weight: bold; }
.qz-ok { color: var(--correct); }
.qz-bad { color: var(--wrong); }
.qz-rationale { margin: .3em 0; }
.qz-why details { margin-top: .4em; }
.qz-why summary { cursor: pointer; font-weight: bold; }
.qz-table, .qz-sum-table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; margin: 12px 0; }
.qz-table th, .qz-table td, .qz-sum-table th, .qz-sum-table td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.qz-table th, .qz-sum-table th { background: var(--accent-soft); }
.qz-summary button { min-height: 48px; padding: 6px 16px; margin: 4px 6px 4px 0; border: 1px solid var(--accent); border-radius: 9px; background: var(--accent); color: white; }
.qz-summary .qz-link-btn, .qz-summary .qz-next-set { display: inline-flex; align-items: center; min-height: 48px; padding: 6px 16px; margin: 4px 6px 4px 0; border: 1px solid var(--accent); border-radius: 9px; background: var(--accent); color: var(--surface); text-decoration: none; }
.qz-new { display: inline-block; margin-left: .45em; padding: 0 .5em; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-size: .72em; line-height: 1.5; vertical-align: .08em; white-space: nowrap; }

@media (max-width: 899px) {
  .qz-passage-label { font-size: .92em; }
  .qz-passage-body { padding-bottom: 6px; }
  .qz-passage-body p { margin-bottom: .3em; }
  .qz-feed { padding: 8px 12px; }
  .qz-card, .qz-summary { padding: 10px; margin-bottom: 10px; }
  .qz-meta { gap: 2px 8px; font-size: .82rem; }
  .qz-ref { font-size: .82rem; }
  .qz-stem { margin: .3em 0; }
  .qz-choices { gap: 4px; }
  .qz-choice { min-height: 44px; padding: 4px 8px; }
}

@media (max-width: 599px) {
  .qz-figure { margin: 6px -4px; }
  :root { --header-height: 48px; }
  .qz-top { gap: 6px; padding: 2px 10px; }
  .qz-passage-head { gap: 8px; padding: 2px 12px; }
  .qz-passage-label { font-size: 19px; line-height: 1.2; }
  #qz-passage-toggle { min-height: 32px; padding: 0 8px; }
  .qz-passage-body { padding: 0 12px 4px; font-size: 18px; line-height: 1.5; }
  .qz-passage-body p { margin: 0 0 .1em; }
  .qz-feed { padding: 4px 8px; }
  .qz-card, .qz-summary { padding: 6px 8px; margin-bottom: 8px; }
  .qz-meta, .qz-ref { line-height: 1.15; }
  .qz-stem { margin: .2em 0; line-height: 1.25; }
  .qz-choices { gap: 3px; }
  .qz-choice { font-size: 19px; line-height: 1.2; }
}

@media (min-width: 600px) and (max-width: 899px) {
  .qz-passage-body { font-size: 20px; }
}

@media (min-width: 900px) {
  html { font-size: 22px; }
  body.has-passage .qz-layout { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(0, 712px); max-width: 1500px; margin: 0 auto; }
  .qz-passage { position: sticky; top: var(--header-height); align-self: start; }
  .qz-feed { padding: 24px 16px; }
  .qz-explain { scroll-margin-top: calc(var(--header-height) + 12px); }
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #101a1c; --surface: #1b292c; --passage: #203431; --text: #e8f1ef; --muted: #b3c5c2; --border: #49605e; --accent: #82d3c5; --accent-soft: #294844; --correct: #69d99a; --correct-bg: #193d2c; --wrong: #ff989b; --wrong-bg: #49282c; --shadow: 0 3px 12px rgb(0 0 0 / 30%); }
  .qz-choice.is-correct .qz-letter, .qz-choice.is-wrong .qz-letter, .qz-summary button, .qz-next { color: #10201d; }
}
.qz-figure { margin: 8px 0; }
.qz-figure img { display: block; width: 100%; max-width: 640px; height: auto; margin: 0 auto; background: #fff; border-radius: 8px; cursor: zoom-in; }
.qz-zoom { position: fixed; inset: 0; z-index: 100; background: rgb(0 0 0 / 85%); display: flex; align-items: center; justify-content: center; padding: 8px; }
.qz-zoom img { background: #fff; border-radius: 8px; max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; touch-action: pinch-zoom; }
.qz-zoom-close { position: fixed; top: 12px; right: 12px; width: 48px; height: 48px; border: 0; border-radius: 50%; background: rgb(255 255 255 / 90%); color: #111; font-size: 32px; line-height: 1; }


/* desktop: keep figure + 4 choices inside one screen */
@media (min-width: 900px) {
  .qz-figure img { width: auto; max-width: 100%; max-height: calc(100dvh - 460px); }
  .qz-feed:has(.qz-card.has-figure) { max-width: 1100px; }
  body.has-passage .qz-layout:has(.qz-card.has-figure) { grid-template-columns: minmax(300px, 1fr) minmax(0, 1100px); }
  .qz-card.has-figure { display: grid; grid-template-columns: minmax(0, 55fr) minmax(0, 45fr); column-gap: 20px; align-content: start; }
  .qz-card.has-figure > * { grid-column: 2; min-width: 0; }
  .qz-card.has-figure > .qz-figure { grid-column: 1; grid-row: 1 / span 10; align-self: start; }
  .qz-card.has-figure .qz-figure img { width: 100%; max-width: 100%; max-height: calc(100dvh - 140px); object-fit: contain; }
}

#qz-print-view { max-width: 800px; margin: 24px auto; padding: 24px; background: white; color: black; }
.qz-print-choices { list-style: none; padding: 0; }
.qz-print-figure img { display: block; width: 100%; max-width: 120mm; max-height: 110mm; object-fit: contain; margin: auto; background: white; }
.qz-print-key table { border-collapse: collapse; }
.qz-print-key td { border: 1px solid; padding: 2px 12px; }

@page { size: A4; margin: 15mm; }
@media print {
  html { font-size: 16pt; }
  body { background: white; color: black; }
  .qz-top, button { display: none !important; }
  #qz-print-view { max-width: none; margin: 0; padding: 0; }
  .qz-print-item { break-inside: avoid; page-break-inside: avoid; margin-bottom: 8mm; }
  .qz-print-figure img { width: 100%; max-width: 120mm; max-height: 110mm; margin: auto; background: white; }
  #qz-print-view table { display: table; width: 100%; border-collapse: collapse; }
  #qz-print-view th, #qz-print-view td { border: 1px solid black; }
  .qz-print-key { break-before: page; page-break-before: always; }
}


/* compact print layout (number beside stem, 2-column choices) */
.qz-print-item { display: grid; grid-template-columns: 2.2em 1fr; column-gap: 4px; align-items: start; }
.qz-print-item > * { grid-column: 2; }
.qz-print-item > div:first-child { grid-column: 1; grid-row: 1; font-weight: bold; }
.qz-print-item > p { margin: 0 0 2mm; }
.qz-print-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 1mm 6mm; margin: 1mm 0 0; }
.qz-print-choices li { margin: 0; }
@media print {
  html { font-size: 15pt; }
  .qz-print-item { margin-bottom: 5mm; }
  .qz-print-figure img { max-width: 110mm; max-height: 95mm; }
}

/* tablet portrait: keep figure + 4 choices inside one screen */
@media (min-width: 600px) and (max-width: 899px) {
  .qz-figure img { width: auto; max-width: 100%; max-height: calc(100dvh - 430px); margin: 0 auto; }
}

/* phones: no print button in the compact header (print from tablet/computer) */
@media (max-width: 599px) { .qz-print-btn { display: none !important; } }
.qz-print-btn { white-space: nowrap; }

/* reading gloss: small red Thai meaning above an English word */
:root { --gloss: #c62828; }
@media (prefers-color-scheme: dark) { :root { --gloss: #ff8a80; } }
ruby.gl { ruby-position: over; ruby-align: center; }
ruby.gl rt { color: var(--gloss); font-size: .74em; font-weight: normal; line-height: 1; white-space: nowrap; letter-spacing: 0; }
.qz-stem:has(ruby.gl), .qz-text:has(ruby.gl), .qz-passage-body p:has(ruby.gl) { line-height: 2.05 !important; }
body.no-gloss ruby.gl rt { display: none; }
.qz-gloss-btn { flex: none; min-height: 36px; padding: 2px 10px; border: 1px solid var(--border); border-radius: 8px; color: var(--gloss); background: var(--surface); white-space: nowrap; font-size: .8rem; }
@media print { ruby.gl rt { color: #c62828; } }ruby.gl rt { ruby-align: center; text-align: center; }
ruby.gl rt > span { display: inline-block; white-space: nowrap; }
