:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --surface-sunk: #efeeea;
  --border: #e2e1dc;
  --border-strong: #c9c8c1;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --accent-hover: #256abf;
  --accent-soft: #e6effb;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --grid: #e8e7e3;
  --good: #0ca30c;
  --good-ink: #006300;
  --good-soft: #e5f4e5;
  --warning: #fab219;
  --warning-ink: #7a5200;
  --warning-soft: #fdf3dc;
  --critical: #d03b3b;
  --critical-ink: #a32626;
  --critical-soft: #fbeaea;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 1px 1px rgba(0, 0, 0, .03);
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface-1: #1a1a19;
    --surface-2: #20201f;
    --surface-sunk: #161615;
    --border: #2f2f2d;
    --border-strong: #45443f;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #8e8d86;
    --accent: #3987e5;
    --accent-hover: #5598e7;
    --accent-soft: #17283d;
    --accent-ink: #ffffff;
    --series-1: #3987e5;
    --grid: #2a2a28;
    --good-ink: #5fd35f;
    --good-soft: #142914;
    --warning-ink: #fab219;
    --warning-soft: #2e2510;
    --critical-ink: #f08a8a;
    --critical-soft: #331818;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface-1: #1a1a19;
  --surface-2: #20201f;
  --surface-sunk: #161615;
  --border: #2f2f2d;
  --border-strong: #45443f;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8e8d86;
  --accent: #3987e5;
  --accent-hover: #5598e7;
  --accent-soft: #17283d;
  --accent-ink: #ffffff;
  --series-1: #3987e5;
  --grid: #2a2a28;
  --good-ink: #5fd35f;
  --good-soft: #142914;
  --warning-ink: #fab219;
  --warning-soft: #2e2510;
  --critical-ink: #f08a8a;
  --critical-soft: #331818;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text-primary);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--text-secondary); font-weight: 600; }
code { font-family: var(--mono); font-size: 12.5px; }

/* ---- shell ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 52px;
  background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; display: flex; align-items: center; gap: 8px; color: var(--text-primary); }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  display: grid; place-items: center; color: var(--accent-ink); font-size: 12px; font-weight: 700;
}
.nav { display: flex; gap: 4px; }
.nav a {
  color: var(--text-secondary); padding: 6px 10px; border-radius: 6px; font-weight: 500;
}
.nav a:hover { background: var(--surface-sunk); text-decoration: none; color: var(--text-primary); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.topbar .spacer { flex: 1; }
.mode { color: var(--text-muted); font-size: 12px; }
.theme-toggle {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-secondary);
  border-radius: 6px; padding: 4px 8px; cursor: pointer; font-size: 12px;
}

main { max-width: 1320px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--text-secondary); }

.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
}
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.small { font-size: 12px; }

/* ---- controls ---- */
button, .btn {
  font: inherit; font-weight: 550; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text-primary);
  border-radius: 7px; padding: 6px 12px; display: inline-flex; align-items: center; gap: 6px;
}
button:hover { background: var(--surface-sunk); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { background: var(--accent-hover); }
button.ghost { border-color: transparent; background: transparent; color: var(--text-secondary); padding: 4px 8px; }
button.ghost:hover { background: var(--surface-sunk); color: var(--text-primary); }
button.danger { color: var(--critical-ink); }
button.good { background: var(--good-soft); border-color: var(--good); color: var(--good-ink); font-weight: 600; }
button.good:hover { background: var(--good); color: #fff; }
input, select, textarea {
  font: inherit; color: var(--text-primary); background: var(--surface-2);
  border: 1px solid var(--border-strong); border-radius: 6px; padding: 5px 8px;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { width: 100%; resize: vertical; min-height: 44px; line-height: 1.45; }
label.inline { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); font-size: 12.5px; }

/* ---- chips & status (status colour always paired with icon + label) ---- */
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 550; background: var(--surface-sunk); color: var(--text-secondary);
  border: 1px solid var(--border); white-space: nowrap;
}
.chip.good { background: var(--good-soft); color: var(--good-ink); border-color: transparent; }
.chip.warn { background: var(--warning-soft); color: var(--warning-ink); border-color: transparent; }
.chip.bad { background: var(--critical-soft); color: var(--critical-ink); border-color: transparent; }
.chip.info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* a group heading as printed ("Q.III Write the meanings of the following words."), over its questions */
.group-head {
  display: flex; align-items: center; gap: 10px; margin: 18px 0 8px; padding: 10px 14px;
  border-left: 4px solid var(--accent); background: var(--accent-soft); border-radius: 8px;
  font-weight: 600; color: var(--text-primary); overflow-wrap: anywhere;
}

/* ---- tables ---- */
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th {
  text-align: left; font-weight: 600; color: var(--text-secondary); font-size: 12px;
  padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: var(--surface-sunk); }
.table-wrap { overflow-x: auto; }

/* ---- upload ---- */
.dropzone {
  display: block;
  border: 1.5px dashed var(--border-strong); border-radius: var(--radius); padding: 28px;
  text-align: center; background: var(--surface-1); cursor: pointer; transition: background .15s;
}
.dropzone.over { background: var(--accent-soft); border-color: var(--accent); }
.dropzone strong { display: block; font-size: 15px; margin-bottom: 4px; }

/* ---- stat tiles ---- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { color: var(--text-secondary); font-size: 12px; font-weight: 550; }
.tile .value { font-size: 26px; font-weight: 650; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { color: var(--text-muted); font-size: 12px; margin-top: 2px; }

/* ---- bar chart (one series, one hue) ---- */
.bars { position: relative; --cols: 110px 1fr 56px; }
.bar-row { display: grid; grid-template-columns: var(--cols); align-items: center; gap: 10px; height: 30px; }
.bar-row .name { color: var(--text-secondary); font-size: 13px; }
.bar-row .val { font-variant-numeric: tabular-nums; font-size: 13px; text-align: right; color: var(--text-primary); }
.track { position: relative; height: 100%; display: flex; align-items: center; }
.track::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--grid); }
.bar { position: relative; height: 14px; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.bar-row:hover .bar { filter: brightness(1.08); }
.target-line {
  position: absolute; top: 0; bottom: 18px; border-left: 1.5px dashed var(--text-muted); pointer-events: none;
}
.axis { display: grid; grid-template-columns: var(--cols); gap: 10px; font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.axis .ticks { display: flex; justify-content: space-between; }
.tooltip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--surface-2); color: var(--text-primary);
  border: 1px solid var(--border-strong); border-radius: 8px; padding: 8px 10px; font-size: 12.5px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .12); max-width: 280px; display: none;
}

/* ---- editor ---- */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) 460px; gap: 20px; align-items: start; }
.viewer { position: sticky; top: 68px; }
.viewer .page-wrap { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: #fff; }
.viewer img { display: block; width: 100%; }
.hl { position: absolute; left: 0; right: 0; background: rgba(42, 120, 214, .12); border-top: 2px solid var(--accent); pointer-events: none; transition: top .15s, height .15s; }
.figbox { position: absolute; border: 1.5px dashed var(--accent); pointer-events: none; }
.review-strip { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.qcard { border: 1px solid var(--border); background: var(--surface-1); border-radius: var(--radius); padding: 12px 14px; scroll-margin-top: 70px; }
.qcard + .qcard { margin-top: 10px; }
.qcard.low { border-color: var(--critical); box-shadow: inset 3px 0 0 var(--critical); }
.qcard.reviewed { border-color: var(--good); box-shadow: inset 3px 0 0 var(--good); }
.qcard.reviewed .flags { background: var(--surface-sunk); color: var(--text-muted); text-decoration: line-through; }
.qcard.reviewed .why { opacity: .55; }
.qcard.selected { outline: 2px solid var(--accent); outline-offset: -1px; }
.qcard .qhead { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.qcard .qnum { width: 110px; font-weight: 650; }
.qcard .marks { width: 70px; }
.qcard .why { display: flex; flex-wrap: wrap; gap: 4px 10px; margin: -2px 0 8px; font-size: 12px; color: var(--text-muted); }
.qcard .why span.ok { color: var(--good-ink); }
.qcard .why span.less { color: var(--warning-ink); }
.qcard .why span.bad { color: var(--critical-ink); }
.qcard.passage { background: var(--surface-sunk); }
.qcard .flags { margin: 0 0 8px; padding: 8px 10px; border-radius: 6px; background: var(--critical-soft); color: var(--critical-ink); font-size: 12.5px; list-style: none; }
.qcard .flags li + li { margin-top: 2px; }
.qcard .flags.quiet { background: var(--surface-sunk); color: var(--text-muted); }
.qcard .sub-block { margin-top: 10px; }
.opt-row { display: grid; grid-template-columns: 44px 1fr auto; gap: 6px; margin-top: 4px; }
.figs { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-start; }
.fig { border: 1px solid var(--border); border-radius: 6px; padding: 4px; background: #fff; position: relative; max-width: 100%; }
.fig img { display: block; height: auto; max-width: 100%; }
.fig .fig-id { font-size: 11px; color: #52514e; padding: 2px 2px 0; }
.grid-table { border-collapse: collapse; }
.grid-table td { padding: 0; border: 1px solid var(--border-strong); }
.grid-table input { border: 0; border-radius: 0; width: 130px; background: var(--surface-2); }
.grid-table td.merged input { background: var(--accent-soft); }
.savebar {
  position: sticky; bottom: 0; z-index: 10; margin-top: 16px; padding: 12px 16px;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; align-items: center; gap: 12px; box-shadow: 0 -2px 12px rgba(0, 0, 0, .06);
}
.log-entry { display: grid; grid-template-columns: 22px 1fr; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.log-entry:last-child { border-bottom: 0; }
details > summary { cursor: pointer; font-weight: 600; }

/* ---- fixes ---- */
.rule { font-family: var(--mono); font-size: 12.5px; line-height: 1.7; }
.rule b { color: var(--accent); font-weight: 700; }
.fix-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 12px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--text-primary); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-weight: 550; box-shadow: 0 6px 24px rgba(0, 0, 0, .2); display: none; max-width: 90vw;
}
.empty { text-align: center; padding: 28px; color: var(--text-muted); }

@media (max-width: 1000px) {
  .editor-grid { grid-template-columns: 1fr; }
  .viewer { position: static; }
}
@media (max-width: 640px) {
  main { padding: 16px; }
  .topbar { padding: 0 16px; gap: 12px; }
  .mode { display: none; }
  .bars { --cols: 80px 1fr 48px; }
  .fix-grid { grid-template-columns: 1fr; }
}

/* crop a picture on the page, and the size it shows at */
.fig-size { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #52514e; padding: 2px; margin: 0; }
.fig-size input { width: 96px; min-width: 0; padding: 0; }
.crop-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 8px; }
.page-wrap.cropping .figbox, .page-wrap.cropping .hl { display: none; }
.cropbox { position: absolute; box-sizing: border-box; border: 2px solid var(--accent); box-shadow: 0 0 0 9999px rgba(0, 0, 0, .35); cursor: move; touch-action: none; }
.crop-handle { position: absolute; box-sizing: border-box; width: 13px; height: 13px; background: #fff; border: 2px solid var(--accent); border-radius: 2px; }
.crop-handle.nw { left: -7px; top: -7px; cursor: nwse-resize; }
.crop-handle.n { left: calc(50% - 6px); top: -7px; cursor: ns-resize; }
.crop-handle.ne { right: -7px; top: -7px; cursor: nesw-resize; }
.crop-handle.e { right: -7px; top: calc(50% - 6px); cursor: ew-resize; }
.crop-handle.se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.crop-handle.s { left: calc(50% - 6px); bottom: -7px; cursor: ns-resize; }
.crop-handle.sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.crop-handle.w { left: -7px; top: calc(50% - 6px); cursor: ew-resize; }

/* The teacher's fix, offered for the others with the same problem: stays in view while they check the questions. */
.fix-offer { position: sticky; top: 8px; z-index: 20; }
.fix-offer .card { border-color: var(--accent); box-shadow: 0 2px 10px rgba(0, 0, 0, .12); max-height: 40vh; overflow: auto; }
.fix-offer:empty { display: none; }
.fix-list { margin: 8px 0 0; padding: 0; list-style: none; }
.fix-list li { padding: 3px 0; }
.issue-list { gap: 6px; }

/* ---- question wording with formatting (rich-text.js) ---- */
.rt { border: 1px solid var(--border-strong); border-radius: 6px; background: var(--surface-2); position: relative; }
.rt-bar { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px; border-bottom: 1px solid var(--border); background: var(--surface-sunk); border-radius: 6px 6px 0 0; }
.rt-group { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.rt-btn { border: 0; border-radius: 0; padding: 4px 9px; min-width: 32px; justify-content: center; font-size: 13px; background: transparent; }
.rt-btn + .rt-btn { border-left: 1px solid var(--border); }
.rt-btn:hover { background: var(--accent-soft); }
.rt-area { min-height: 64px; padding: 8px 10px; line-height: 1.5; outline: none; overflow-wrap: anywhere; }
.rt-area:focus { box-shadow: inset 0 0 0 2px var(--accent-soft); }
.rt-area table { border-collapse: collapse; margin: 4px 0; }
.rt-area td, .rt-area th { border: 1px solid var(--border-strong); padding: 3px 8px; min-width: 40px; }
.rt-area img { max-width: 100%; vertical-align: middle; }
.rt-source { border: 0; border-radius: 0 0 6px 6px; min-height: 90px; font-family: var(--mono); font-size: 12.5px; }
.rt-pop { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.rt-pop[hidden] { display: none; }
.rt-symbols { display: flex; flex-wrap: wrap; gap: 2px; }
.rt-symbols button { min-width: 30px; justify-content: center; }
.opt-row.with-answer { grid-template-columns: 22px 44px 1fr auto; align-items: center; }
/* an option's words, or which of the question's pictures it is ("Picture 2"), with that picture beside them */
.opt-text { display: flex; align-items: center; gap: 8px; min-width: 0; }
.opt-text > :first-child { flex: 1; min-width: 0; }
.opt-pic:empty { display: none; }
.opt-pic img { display: block; max-width: 140px; max-height: 100px; border: 1px solid var(--border, #ddd); border-radius: 4px; background: #fff; }
.rt-menus { overflow: visible; }
/* equations: kept as LaTeX, shown as maths; a click opens the equation editor */
.rt-math { display: inline-block; padding: 0 3px; margin: 0 1px; border-radius: 4px; cursor: pointer; vertical-align: middle; }
.rt-area .rt-math:hover { background: var(--accent-soft); outline: 1px solid var(--accent); }
.eq-box { width: 100%; gap: 8px; }
.eq-tools { display: flex; flex-wrap: wrap; gap: 3px; }
.eq-tool { min-width: 34px; padding: 3px 7px; justify-content: center; font-size: 13px; }
.eq-field { display: block; width: 100%; min-height: 44px; font-size: 20px; padding: 6px 8px; border: 1px solid var(--border-strong);
  border-radius: 6px; background: var(--surface-2); color: var(--text-primary); }
/* an option: words and maths in one field, like the question's text */
.mt-field { flex: 1; min-width: 0; min-height: 34px; padding: 6px 10px; border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface-2); color: var(--text-primary); line-height: 1.6; outline: none; overflow-x: auto; white-space: pre-wrap; }
.mt-field:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.mt-field .rt-math:hover { background: var(--accent-soft); outline: 1px solid var(--accent); }
.mt-add { min-width: 34px; justify-content: center; }
.mt-pop { padding: 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); }
.mt-pop[hidden] { display: none; }
.eq-preview { padding: 4px 8px; border-left: 3px solid var(--accent-soft); color: var(--text-primary); font-size: 15px; overflow-x: auto; }
.eq-preview[hidden] { display: none; }
.rt-menu { border: 0; border-radius: 0; padding: 4px 6px; font-size: 13px; background: transparent; max-width: 150px; }
.rt-menu + .rt-menu { border-left: 1px solid var(--border); }
.rt-area h3, .rt-area h4, .rt-area h5 { margin: 4px 0; }
.rt-area pre { font-family: var(--mono); margin: 4px 0; white-space: pre-wrap; }
