:root {
  --bg: #f6f7f9; --panel: #fff; --ink: #1c2230; --muted: #5b616e;
  --line: #e4e7ec; --accent: #2563eb; --ok: #047857; --bad: #b91c1c;
  --new: #475569; --easy: #047857; --med: #b45309; --hard: #b91c1c;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg); color: var(--ink); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar { display: flex; align-items: center; gap: 20px; padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar .brand { font-weight: 700; font-size: 16px; }
.topbar nav a { margin-right: 14px; color: var(--muted); cursor: pointer; }
.topbar nav .navbtn { background: none; border: 0; font: inherit; color: var(--muted);
  cursor: pointer; padding: 0; margin-right: 14px; }
.topbar nav .navbtn:hover { color: var(--ink); text-decoration: underline; }
main { max-width: 1000px; margin: 18px auto; padding: 0 16px; }

.controls { background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px; margin-bottom: 16px; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.row + .row { margin-top: 10px; }
label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 4px; }
select, input[type=text], input[type=search], input[type=number] {
  font-size: 14px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; background: #fff; color: var(--ink); }
input[type=number] { width: 84px; }
label:has(input[type=checkbox]) { flex-direction: row; align-items: center; color: var(--ink); }

.btn { display: inline-block; font-size: 13px; padding: 7px 12px; border: 1px solid var(--line);
  border-radius: 7px; background: #fff; color: var(--ink); cursor: pointer; }
.btn:hover { border-color: #c5cad3; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.ghost { color: var(--muted); }
.exportbox { margin-top: 12px; }
.exportbox summary { cursor: pointer; font-weight: 600; }

.list-head { margin: 6px 2px 12px; }
.muted { color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); padding: 20px; text-align: center; }

.qcard { background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  margin-bottom: 10px; overflow: hidden; }
.qsum { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 12px; }
.qid { font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--muted); }
.qsum .open { margin-left: auto; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; background: #eef1f5; color: var(--ink); white-space: nowrap; }
.badge.skill { background: #eef2ff; color: #3730a3; max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.badge.dom { background: #f1f5f9; }
.badge.ibn { background: #fef3c7; color: #92400e; }
.badge.diff-E { background: #dcfce7; color: #065f46; }
.badge.diff-M { background: #fef9c3; color: #92400e; }
.badge.diff-H { background: #fee2e2; color: var(--hard); }
.badge.status.new { background: #e2e8f0; color: var(--new); }
.badge.status.correct { background: #dcfce7; color: var(--ok); }
.badge.status.incorrect { background: #fee2e2; color: var(--bad); }

.qbody { padding: 0 14px; }
.qcard.open .qbody { padding: 4px 14px 16px; border-top: 1px solid var(--line); }
.qstem { margin: 12px 0; }
.qstem svg, .opt svg, .rationale svg, figure { max-width: 100%; height: auto; }
/* CB marks a fill-in-the-blank as <span aria-hidden>______</span><span class="sr-only">blank</span>;
   hide the screen-reader label visually so it doesn't render as "______blank". */
.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; }
/* CB formatting classes (e.g. italic math variables) that rely on CB's stylesheet. */
.qstem .italic, .opt .italic, .rationale .italic { font-style: italic; }
.qstem .underline, .opt .underline, .rationale .underline { text-decoration: underline; }
/* CB statement lists (I/II/III) carry the marker style only as a class. */
.qstem ol.list_upper_roman, .opt ol.list_upper_roman, .rationale ol.list_upper_roman { list-style-type: upper-roman; }
.choices { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.choice:hover { border-color: #c5cad3; }
.choice .lbl { font-weight: 700; }
.spr input { width: 220px; }
.actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.loadmore { display: block; margin: 14px auto; }

.answer-slot:not(:empty) { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.result { font-weight: 700; margin-bottom: 8px; }
.result.ok { color: var(--ok); } .result.bad { color: var(--bad); }
.result .muted { font-weight: 400; margin-left: 8px; }
.correct { margin-bottom: 8px; }
.rationale { background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }

/* Stats modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 20; overflow: auto; }
.modal[hidden] { display: none; }
.modal .sheet { background: #fff; border-radius: 12px; max-width: 900px; width: 100%; padding: 20px; }
.modal h2 { margin-top: 0; }
.modal .close { float: right; }
table.stats { border-collapse: collapse; width: 100%; font-size: 14px; }
table.stats th, table.stats td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
table.stats th { background: #f1f5f9; }

.sitefoot { max-width: 1000px; margin: 30px auto; padding: 14px 16px; color: var(--muted);
  font-size: 12px; border-top: 1px solid var(--line); }

/* ---- Accessibility & motion (all viewports) ---- */
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.choice:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 2px; }
.choice input:focus-visible { outline: none; }  /* the .choice box already shows the ring */
.choice, .btn, .qcard, .topbar a, .topbar .navbtn, summary {
  -webkit-tap-highlight-color: rgba(37, 99, 235, .15); }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important; scroll-behavior: auto !important; }
}

/* ---- Phone / touch (<=600px). screen-only so the print rules below stay full-width ---- */
@media screen and (max-width: 600px) {
  html, body { max-width: 100%; overflow-x: hidden; }
  body { font-size: 16px; }

  .topbar { position: static; padding: 9px 14px; gap: 10px 14px; flex-wrap: wrap; }
  .topbar nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .topbar nav a, .topbar nav .navbtn { margin-right: 0; padding: 10px 8px; min-height: 44px;
    display: inline-flex; align-items: center; }
  main { margin: 12px auto; padding: 0 12px; }
  .controls { padding: 12px; }

  /* Stack every filter / export control full-width and make it tappable */
  .row { gap: 10px; }
  .row > label, .row > .btn, .row > a.btn, .row > button, .row > form { flex: 1 1 100%; min-width: 0; }
  .row > form > .btn { width: 100%; }
  .row select, .row input[type=text], .row input[type=search] { width: 100%; }
  select, input[type=text], input[type=search], input[type=number], textarea {
    font-size: 16px; min-height: 44px; padding: 10px 12px; }
  input[type=number] { width: 100%; }
  label:has(input[type=checkbox]) { min-height: 44px; align-items: center; gap: 10px; }
  label:has(input[type=checkbox]) input { width: 22px; height: 22px; }
  .btn { min-height: 44px; padding: 11px 16px; font-size: 15px;
    display: inline-flex; align-items: center; justify-content: center; }

  /* Question cards: bigger reading text + comfortable choice targets */
  .qstem, .opt, .choice, .rationale { font-size: 16px; line-height: 1.6; }
  .qsum { gap: 10px; }
  .badge { font-size: 12px; padding: 3px 9px; }
  .badge.skill { max-width: 100%; }
  .qsum .open { flex: 1 1 100%; margin-left: 0; order: 99; margin-top: 2px; }
  .choices { gap: 12px; }
  .choice { min-height: 48px; padding: 12px 14px; align-items: center; }
  .choice input[type=radio] { width: 22px; height: 22px; flex: none; }
  .choice .opt { min-width: 0; }
  .actions { gap: 12px; flex-wrap: wrap; }
  .actions .btn { flex: 1 1 auto; }
  .spr { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
  .spr input { width: 100%; max-width: 360px; }

  /* Keep wide content (long equations, graphs, tables) inside its own scroll box */
  .qstem, .opt, .rationale { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .qstem mjx-container, .opt mjx-container, .rationale mjx-container { max-width: 100%; }
  .qstem table, .opt table, .rationale table {
    display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Stats modal: shrink padding, full-width close, self-scrolling 8-col table */
  .modal { padding: 16px 8px; }
  .modal .sheet { padding: 14px; }
  .modal .close { float: none; display: block; width: 100%; min-height: 44px; margin: 0 0 10px; }
  table.stats { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch;
    white-space: nowrap; font-size: 13px; }
  table.stats th, table.stats td { padding: 6px 8px; }
}

/* Print: only the generated print area shows */
#print-area { display: none; }
@media print {
  body > header, body > main, body > footer { display: none !important; }
  #print-area { display: block; font: 14px/1.5 Georgia, 'Times New Roman', serif; color: #000; }
  #print-area h1 { font-size: 20px; }
  #print-area .meta { color: #555; font-size: 12px; margin-bottom: 16px; }
  #print-area .q-export { page-break-inside: avoid; border-bottom: 1px solid #ccc; padding: 12px 0; }
  #print-area .q-head { font-size: 12px; color: #555; margin-bottom: 6px; }
  #print-area .choice { border: 0; padding: 2px 0; }
  #print-area .answer-key { page-break-before: always; }
  #print-area svg, #print-area figure { max-width: 100%; height: auto; }
}
