/* Exercise-book theme: squared paper, ink blue, red margin rule. */
:root {
  --paper: #fbfcfe;
  --grid: #e4ebf5;
  --ink: #1c2a5c;
  --ink-soft: #55618a;
  --margin: #cf3f37;
  --line: #cfd8e8;
  --ok: #2c7a4b;
  --field: #ffffff;
  --radius: 6px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #12172a; --grid: #1c2440; --ink: #dfe6fb; --ink-soft: #98a3c7;
    --margin: #e8665e; --line: #2d3758; --ok: #5cc68a; --field: #0d1222;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0; font-family: var(--font); color: var(--ink); line-height: 1.5;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
}
a { color: var(--ink); text-underline-offset: 3px; }
:focus-visible { outline: 3px solid var(--margin); outline-offset: 2px; }

.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px clamp(16px, 4vw, 40px); background: var(--paper); border-bottom: 2px solid var(--ink);
}
.brand { font-weight: 700; font-size: 1.15rem; text-decoration: none; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  background: var(--ink); color: var(--paper); font-size: 1.2rem;
}
.nav { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 18px; }
.nav a { text-decoration: none; padding: 2px 0; border-bottom: 2px solid transparent; }
.nav a[aria-current="page"] { border-bottom-color: var(--margin); }

.page { max-width: 1080px; margin: 0 auto; padding: 32px clamp(12px, 4vw, 40px) 64px; }
.sheet {
  position: relative; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px 28px 28px 56px; margin: 0 auto 24px; max-width: 720px;
}
/* The red margin rule of an exercise book */
.sheet::before { content: ""; position: absolute; top: 0; bottom: 0; left: 36px; width: 2px; background: var(--margin); opacity: .7; }
.sheet-narrow { max-width: 440px; }
.sheet-wide { max-width: none; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }

h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 6px; letter-spacing: -0.01em; }
h2 { font-size: 1.3rem; margin: 0 0 6px; }
.lede { color: var(--ink-soft); margin: 0 0 22px; }
.aside, .meta, .hint { color: var(--ink-soft); font-size: .92rem; }
.meta { margin: 20px 0 0; }
.crumb { margin: 0 0 4px; font-size: .92rem; }

.field { margin-bottom: 16px; flex: 1 1 200px; }
.field-row { display: flex; flex-wrap: wrap; gap: 0 16px; }
.field-check { display: flex; align-items: flex-end; padding-bottom: 10px; }
.field-check label { display: flex; gap: 8px; align-items: center; font-weight: 400; }
label { display: block; font-weight: 600; margin-bottom: 6px; }
.optional { font-weight: 400; color: var(--ink-soft); font-size: .88rem; }
input[type=text], input:not([type]), input[type=email], input[type=tel], input[type=password], input[type=search], select {
  width: 100%; font: inherit; color: var(--ink); background: var(--field);
  border: 1.5px solid var(--line); border-radius: var(--radius); padding: 9px 11px;
}
input:focus, select:focus { border-color: var(--ink); outline: none; box-shadow: 0 0 0 3px var(--grid); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--ink); }
select:disabled, input:disabled { opacity: .6; }
.field-error { color: var(--margin); margin: 6px 0 0; font-size: .92rem; }

.btn {
  display: inline-block; font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  background: var(--ink); color: var(--paper); border: 2px solid var(--ink); border-radius: var(--radius);
  padding: 9px 18px;
}
.btn:hover { filter: brightness(1.15); }
.btn-quiet { background: transparent; color: var(--ink); }
.btn-quiet:hover { background: var(--grid); filter: none; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.link { font: inherit; background: none; border: 0; padding: 0; color: var(--ink); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.link-danger { color: var(--margin); }
form.inline { display: inline; margin: 0; }

.notice { border-left: 4px solid var(--ok); background: var(--field); padding: 10px 14px; margin: 0 auto 20px; max-width: 720px; border-radius: 0 var(--radius) var(--radius) 0; }
.notice-error { border-left-color: var(--margin); }
.sheet .notice { max-width: none; }

.filters { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; }
.filters input { flex: 1 1 240px; width: auto; }
.filters select { width: auto; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th { text-align: left; font-weight: 600; color: var(--ink-soft); border-bottom: 2px solid var(--ink); padding: 8px 10px; white-space: nowrap; }
td { border-bottom: 1px solid var(--line); padding: 10px; vertical-align: middle; }
tr.is-off td { color: var(--ink-soft); }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions > * + * { margin-left: 14px; }
.tag { display: inline-block; font-size: .8rem; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--line); }
.tag-admin { border-color: var(--ink); font-weight: 600; }

.facts { display: flex; flex-wrap: wrap; gap: 12px 40px; margin: 0 0 24px; }
.facts dt { color: var(--ink-soft); font-size: .9rem; }
.facts dd { margin: 0; font-weight: 600; font-size: 1.1rem; }
.placeholder { border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px; text-align: center; color: var(--ink-soft); margin-bottom: 16px; }
.placeholder-eq { font-size: 2.4rem; font-weight: 700; color: var(--ink); margin: 0 0 6px; font-variant-numeric: tabular-nums; }
.error-code { font-size: 4rem; font-weight: 800; color: var(--margin); margin: 0; line-height: 1; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (max-width: 560px) {
  .sheet { padding: 22px 18px 22px 40px; }
  .sheet::before { left: 24px; }
  h1 { font-size: 1.6rem; }
}

/* ---------- Quiz ---------- */
.muted { color: var(--ink-soft); }
code { font-size: .9em; background: var(--grid); padding: 1px 5px; border-radius: 4px; }
.link-plain { align-self: center; }
.field-grow { flex: 3 1 320px; }
textarea {
  width: 100%; font: inherit; color: var(--ink); background: var(--field); resize: vertical;
  border: 1.5px solid var(--line); border-radius: var(--radius); padding: 9px 11px; margin-bottom: 14px;
}
textarea:focus { border-color: var(--ink); outline: none; box-shadow: 0 0 0 3px var(--grid); }
.btn-danger { background: var(--margin); border-color: var(--margin); color: #fff; }
.danger { border-color: var(--margin); }
.tag-live { border-color: var(--ok); color: var(--ok); font-weight: 600; }

.grade-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 18px; }
.grade-tabs a { text-decoration: none; min-width: 40px; text-align: center; padding: 5px 12px; border: 1.5px solid var(--line); border-radius: 999px; }
.grade-tabs a[aria-current="page"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.tex-help { background: var(--field); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 6px 0 22px; }
.tex-help p { margin: 0 0 10px; font-size: .95rem; }
.tex-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.tex-tools button {
  font: inherit; min-width: 42px; padding: 4px 10px; cursor: pointer; color: var(--ink);
  background: var(--paper); border: 1.5px solid var(--line); border-radius: var(--radius);
}
.tex-tools button:hover { border-color: var(--ink); }

.qcard { border: 1.5px solid var(--line); border-radius: var(--radius); padding: 14px 16px 6px; margin: 0 0 18px; }
.qcard legend { font-weight: 700; padding: 0 6px; }
.qcard.has-error { border-color: var(--margin); }
.qerrors { margin: 0 0 12px; padding-left: 18px; }
.qgrid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; }
@media (max-width: 860px) { .qgrid { grid-template-columns: 1fr; } }
.choices-label { font-weight: 600; margin: 0 0 6px; }
.choices-edit { display: grid; gap: 8px; margin-bottom: 14px; }
.choice-edit { display: flex; align-items: center; gap: 8px; }
.choice-edit input[type=radio] { position: absolute; opacity: 0; width: 1px; height: 1px; }
.choice-letter {
  flex: none; margin: 0; width: 34px; height: 34px; display: grid; place-items: center; cursor: pointer;
  border: 2px solid var(--line); border-radius: 50%; font-weight: 700;
}
.choice-edit input[type=radio]:checked + .choice-letter { background: var(--ok); border-color: var(--ok); color: #fff; }
.choice-edit input[type=radio]:focus-visible + .choice-letter { outline: 3px solid var(--margin); outline-offset: 2px; }
.qpreview { background: var(--field); border: 1px dashed var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; min-width: 0; }
.preview-label { margin: 0 0 6px; font-size: .85rem; color: var(--ink-soft); }
.preview-body { overflow-x: auto; }

.submit-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 10px; }
.submit-sticky {
  position: sticky; bottom: 0; background: var(--paper); border-top: 2px solid var(--ink);
  padding: 12px 0; justify-content: space-between;
}
.progress { color: var(--ink-soft); font-variant-numeric: tabular-nums; }

/* Questions as students see them */
.qitem { border: 0; border-bottom: 1px solid var(--line); padding: 0 0 22px; margin: 0 0 22px; min-width: 0; }
.qitem:last-of-type { border-bottom: 0; }
.qitem legend { padding: 0; width: 100%; }
.qnum { display: block; color: var(--ink-soft); font-size: .9rem; font-weight: 600; margin: 0 0 6px; }
.qtext { display: block; font-size: 1.15rem; margin-bottom: 14px; overflow-x: auto; overflow-y: hidden; }
.options { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.option {
  position: relative; display: flex; align-items: center; gap: 12px; margin: 0; font-weight: 400;
  border: 1.5px solid var(--line); border-radius: var(--radius); padding: 10px 12px; background: var(--field); min-width: 0;
}
label.option { cursor: pointer; }
label.option:hover { border-color: var(--ink); }
.option input[type=radio] { position: absolute; opacity: 0; }
.opt-letter {
  flex: none; width: 30px; height: 30px; display: grid; place-items: center;
  border: 2px solid var(--line); border-radius: 50%; font-weight: 700;
}
.opt-text { flex: 1 1 auto; overflow-x: auto; overflow-y: hidden; min-width: 0; font-size: 1.05rem; }
.option:has(input:checked) { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.option:has(input:checked) .opt-letter { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.option:has(input:focus-visible) { outline: 3px solid var(--margin); outline-offset: 2px; }
.opt-note { flex: none; margin-left: auto; font-size: .82rem; font-weight: 600; }
.option { flex-wrap: wrap; }

/* Results */
.score-head { display: flex; align-items: center; gap: 24px; flex-wrap: wrap; margin-bottom: 8px; }
.score { margin: 0; font-size: 1.6rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; line-height: 1; }
.score span { font-size: 4.2rem; font-weight: 800; color: var(--ink); }
.result-tag { margin-left: 8px; padding: 1px 9px; border-radius: 999px; font-size: .8rem; border: 1.5px solid currentColor; }
.result-ok .result-tag { color: var(--ok); }
.result-bad .result-tag { color: var(--margin); }
.result-skip .result-tag { color: var(--ink-soft); }
.option.is-correct { border-color: var(--ok); box-shadow: inset 0 0 0 1px var(--ok); }
.option.is-correct .opt-letter { background: var(--ok); border-color: var(--ok); color: #fff; }
.option.is-correct .opt-note { color: var(--ok); }
.option.is-wrong { border-color: var(--margin); box-shadow: inset 0 0 0 1px var(--margin); }
.option.is-wrong .opt-letter { background: var(--margin); border-color: var(--margin); color: #fff; }
.option.is-wrong .opt-note { color: var(--margin); }
.explanation { margin-top: 12px; padding: 10px 14px; border-left: 3px solid var(--ink); background: var(--field); overflow-x: auto; }

/* Student home */
.set-list { list-style: none; padding: 0; margin: 0; }
.set-item { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.set-item:last-child { border-bottom: 0; }
.set-item h2 { margin: 0 0 4px; font-size: 1.2rem; }
.set-item h2 a { text-decoration: none; }
.set-item p { margin: 0; }
.set-item .meta { margin-top: 4px; }
.set-main { flex: 1 1 280px; }
.katex { font-size: 1.08em; }

/* Figures */
.mt-figure { display: block; margin: 10px 0; max-width: 100%; overflow-x: auto; }
.mt-figure-svg { display: block; max-width: 100%; height: auto; }
.opt-text .mt-figure { margin: 4px 0; }
.mt-error { color: var(--margin); font-size: .9rem; }
.import-box { border: 1.5px dashed var(--line); border-radius: var(--radius); padding: 10px 14px; margin: 6px 0 16px; }
.import-box summary { cursor: pointer; font-weight: 600; }
.import-box textarea { margin: 8px 0; font-family: Consolas, "Cascadia Mono", monospace; font-size: .85rem; }
.import-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.import-actions label { margin: 0; font-weight: 400; }
.import-actions select { width: auto; }
.import-status { margin: 8px 0 0; min-height: 1.2em; font-size: .92rem; }

/* Tables */
.mt-table-wrap { display: block; max-width: 100%; overflow-x: auto; margin: 8px 0; }
.mt-table { border-collapse: collapse; font-size: 1rem; width: auto; }
.mt-table td, .mt-table th { padding: 6px 14px; text-align: center; vertical-align: middle; border: 1.5px solid var(--ink-soft); min-width: 2.2em; }
.mt-table th { background: var(--grid); font-weight: 600; color: var(--ink); white-space: normal; }
.mt-border-none td, .mt-border-none th { border: 0; }
.mt-border-none tr:first-child th { border-bottom: 1.5px solid var(--ink-soft); }
.mt-border-none th:first-child { border-right: 1.5px solid var(--ink-soft); }
.opt-text .mt-table td, .opt-text .mt-table th { padding: 4px 9px; }

/* Pictures and charts */
.mt-image { display: block; margin: 8px 0; max-width: 100%; }
.mt-image img { display: block; max-width: 100%; height: auto; border-radius: 4px; }
.mt-caption { display: block; margin-top: 4px; font-size: .88rem; color: var(--ink-soft); }
.mt-chart { display: block; margin: 8px 0; max-width: 100%; overflow-x: auto; }
.mt-chart-svg { display: block; max-width: 100%; height: auto; }
