/* ── BASE ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f2ed;
  --surface: #ffffff;
  --surface2: #faf8f4;
  --border: #ded6c9;
  --border-strong: #b9aa94;
  --text: #2b1d1f;
  --text-muted: #77686a;
  --accent: #641218;
  --accent-light: #fff7e4;
  --accent-hover: #4f0d12;
  --gold: #d3b562;
  --gold-light: #f8edc7;
  --green: #15803d;
  --green-light: #f0fdf4;
  --red: #b91c1c;
  --yellow: #b45309;
  --yellow-light: #fef9c3;
  --purple: #7e22ce;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 4px rgba(0,0,0,.07);
  --shadow-md: 0 4px 16px rgba(0,0,0,.10);
  --font: 'Inter', Arial, sans-serif;
}

body {
  font-family: var(--font);
  font-size: 12pt;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.local-copy-blocker { display: none; }
.local-copy .local-copy-blocker {
  position: fixed; inset: 0; z-index: 10000; display: flex;
  align-items: center; justify-content: center; padding: 24px;
  background: #f5f2ed; color: #2b1d1f; text-align: center;
}
.local-copy .local-copy-card {
  width: min(520px, 100%); padding: 30px; background: #fff;
  border: 1px solid #d3b562; border-radius: 14px;
  box-shadow: 0 16px 46px rgba(60,8,12,.16);
}
.local-copy .local-copy-card h1 { color: #641218; font-size: 20pt; margin-bottom: 10px; }
.local-copy .local-copy-card p { color: #77686a; line-height: 1.5; }
.local-copy body > :not(.local-copy-blocker) { display: none !important; }

/* ── TOP BAR ── */
#topbar {
  position: sticky; top: 0; z-index: 200;
  background: linear-gradient(90deg, #571015 0%, #711820 52%, #571015 100%);
  border-bottom: 3px solid var(--gold);
  padding: 0 18px;
  min-height: 72px;
  display: flex; align-items: center; gap: 10px;
  box-shadow: 0 3px 14px rgba(60,8,12,.18);
}
.brand-logo {
  width: 154px; height: 44px; flex: 0 0 154px;
  background: var(--gold);
  -webkit-mask: url('school-logo.png') center / contain no-repeat;
  mask: url('school-logo.png') center / contain no-repeat;
}
.tb-sep { width: 1px; height: 34px; background: rgba(255,255,255,.35); flex-shrink: 0; }
.tb-group { display: flex; align-items: center; gap: 6px; min-width: 0; }
.tb-label { font-size: 11pt; color: #f8edc7; white-space: nowrap; font-family: var(--font); font-weight: 700; }
.spacer { flex: 1; }

select, input[type=text] {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px 10px; font-size: 11pt; color: var(--text);
  background: var(--surface2); outline: none; font-family: var(--font);
  transition: border-color .15s;
}
select:focus, input[type=text]:focus { border-color: var(--accent); background: var(--surface); }
#subjectSelect { display: none; }
#sectionType { width: 286px; }
#subjectSelect { width: 220px; }
#variantSelect { width: 118px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 16px; border-radius: var(--radius-sm);
  font-size: 11pt; font-weight: 600; cursor: pointer;
  border: 1px solid transparent; transition: all .15s;
  white-space: nowrap; font-family: var(--font);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-outline { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-outline:hover { background: var(--surface2); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface2); color: var(--text); }
.btn-green { background: var(--green); color: #fff; }
.btn-green:hover { background: #166534; }
#topbar select { background: #fff; border-color: rgba(211,181,98,.75); }
#topbar .btn-outline { background: #fff; color: var(--accent); border-color: var(--gold); }
#topbar .btn-outline:hover { background: var(--gold-light); }
#topbar .btn-green { background: var(--gold); color: #4b0b11; border-color: #e7cc7d; }
#topbar .btn-green:hover { background: #e0c574; }
#topbar > .btn, #topbar > label.btn { padding-left: 12px; padding-right: 12px; font-size: 10.5pt; }

/* ── MAIN ── */
#workspaceLayout {
  width: min(1580px, 100%); margin: 0 auto; padding: 10px 12px 32px;
  display: grid; grid-template-columns: 116px minmax(0, 1fr) 238px;
  gap: 16px; align-items: start;
}
#editorPage { min-width: 0; margin: 0; padding: 0; grid-column: 2; grid-row: 1; }

/* ── AUTHOR CARD ── */
#authorCard {
  position: sticky; z-index: 118; top: 88px; grid-column: 3; grid-row: 1;
  width: 100%; padding: 14px; border: 1px solid rgba(211,181,98,.72);
  border-radius: 14px; background: rgba(255,253,248,.97);
  box-shadow: 0 8px 28px rgba(60,8,12,.13);
  text-align: center;
}
.author-photo {
  width: 84px; height: 84px; display: block; margin: 0 auto 11px;
  border: 3px solid #fff; border-radius: 50%; object-fit: cover;
  box-shadow: 0 0 0 2px var(--gold), 0 5px 14px rgba(60,8,12,.14);
}
.author-content { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.author-kicker {
  color: var(--accent); font-size: 8.5pt; font-weight: 700;
  letter-spacing: .07em; text-transform: uppercase;
}
.author-name { color: var(--text); font-size: 12pt; line-height: 1.25; }
.author-role { color: var(--text-muted); font-size: 9pt; line-height: 1.3; }
.author-instagram {
  margin-top: 8px; padding: 7px 11px; border: 1px solid var(--gold);
  border-radius: 999px; background: var(--gold-light); color: var(--accent);
  font-size: 9pt; font-weight: 700; text-decoration: none;
  transition: transform .15s, background .15s;
}
.author-instagram:hover { background: #f1dda0; transform: translateY(-1px); }

/* ── QUESTION NAVIGATION ── */
#questionNav {
  position: sticky; z-index: 120; top: 88px; grid-column: 1; grid-row: 1;
  width: 116px;
  max-height: calc(100vh - 96px); overflow-y: auto;
  padding: 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: rgba(255,255,255,.96); box-shadow: var(--shadow-md);
}
.qnav-title { font-size: 10pt; font-weight: 700; color: var(--text-muted); margin-bottom: 7px; }
.qnav-title .action-btn { display: none; }
.qnav-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.qnav-btn {
  min-width: 0; height: 25px; border: 1px solid var(--border); border-radius: 5px;
  background: var(--surface2); color: var(--text); cursor: pointer;
  font: 700 9pt var(--font); transition: all .12s;
}
.qnav-btn.status-empty { background: #eef0f4; color: #667085; border-color: #d0d5dd; }
.qnav-btn.status-partial { background: #fee2e2; color: #b91c1c; border-color: #f87171; }
.qnav-btn.status-complete { background: #dcfce7; color: #166534; border-color: #4ade80; }
.qnav-btn:hover { filter: brightness(.94); transform: translateY(-1px); }
.qnav-btn.active { outline: 2px solid var(--accent); outline-offset: 1px; }
.mobile-nav-toggle { display: none; }

/* ── PAGE TITLE AREA ── */
.page-header {
  background: var(--surface); border-radius: var(--radius);
  border: 1px solid var(--border); padding: 20px 24px;
  margin-bottom: 20px; box-shadow: var(--shadow);
}
.page-header h1 {
  font-size: 20pt; font-weight: 700; color: var(--accent);
  margin-bottom: 14px; font-family: var(--font);
}
.controls-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.ctrl-group { display: flex; flex-direction: column; gap: 4px; }
.ctrl-label { font-size: 10pt; color: var(--text-muted); font-family: var(--font); }
.controls-row select { padding: 7px 12px; font-size: 11pt; min-width: 200px; }
.controls-actions { display: flex; gap: 8px; margin-left: auto; align-items: flex-end; }

/* ── PROGRESS BAR ── */
.progress-wrap {
  background: var(--surface2); border-radius: var(--radius-sm);
  border: 1px solid var(--border); padding: 10px 16px;
  margin-bottom: 16px; display: flex; align-items: center; gap: 12px;
}
.progress-label { font-size: 11pt; color: var(--text-muted); }
.progress-bar { flex: 1; height: 6px; background: var(--border); border-radius: 3px; }
.progress-fill { height: 6px; background: var(--accent); border-radius: 3px; transition: width .3s; }
.progress-count { font-size: 12pt; font-weight: 700; color: var(--accent); }

/* ── SECTION BLOCK ── */
.section-block { margin-bottom: 24px; }
.section-header {
  background: var(--accent); color: #fff;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 10px 18px; display: flex; align-items: center; gap: 10px;
}
.section-header h2 { font-size: 12pt; font-weight: 700; font-family: var(--font); }
.section-instruction {
  background: var(--accent-light); border: 1px solid #c7d9f8;
  border-top: none; padding: 9px 18px;
  font-size: 11pt; font-style: italic; color: #1a3a7a; font-family: var(--font);
}

/* ── CONTEXT BLOCK ── */
.ctx-block {
  border: 2px solid var(--accent); border-top: none;
  background: #f8faff; padding: 14px 18px;
}
.ctx-block-title {
  font-size: 11pt; font-weight: 700; color: var(--accent);
  margin-bottom: 8px; font-family: var(--font);
}
.ctx-textarea {
  width: 100%; min-height: 80px; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-family: var(--font); font-size: 12pt; resize: vertical;
  outline: none; background: var(--surface);
  transition: border-color .15s;
}
.ctx-textarea:focus { border-color: var(--accent); }
.ctx-img-row { margin-top: 8px; display: flex; align-items: flex-start; gap: 10px; flex-wrap: wrap; }

/* ── QUESTION CARDS WRAPPER ── */
.questions-wrapper {
  border: 1px solid var(--border); border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--surface); overflow: hidden;
}

/* ── Q CARD ── */
.q-card {
  border-bottom: 1px solid var(--border);
  padding: 16px 18px;
  transition: background .1s;
}
.q-card:last-child { border-bottom: none; }
.q-card:focus-within { background: #fafbff; }

.q-card-head {
  display: flex; align-items: center; gap: 10px; margin-bottom: 10px;
}
.q-num-badge {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 11pt; font-weight: 700; flex-shrink: 0; font-family: var(--font);
}
.q-type-tag {
  font-size: 10pt; padding: 2px 10px; border-radius: 20px; font-weight: 600; font-family: var(--font);
}
.tag-single { background: var(--accent-light); color: var(--accent); }
.tag-multi  { background: var(--yellow-light); color: var(--yellow); }
.tag-match  { background: #f5f3ff; color: var(--purple); }

.q-card-actions { margin-left: auto; display: flex; gap: 6px; }
.action-btn {
  padding: 4px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 10pt; cursor: pointer; background: var(--surface2);
  color: var(--text-muted); font-family: var(--font); transition: all .15s;
  display: inline-flex; align-items: center; gap: 4px;
}
.action-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }

/* ── MATH TOOLBAR ── */
.math-toolbar {
  display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px;
}
.math-btn {
  padding: 3px 9px; font-size: 11pt;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface2); cursor: pointer;
  color: var(--text); font-family: var(--font); transition: all .12s;
}
.math-btn:hover { background: var(--accent-light); border-color: var(--accent); color: var(--accent); }
.formula-btn { color: var(--purple); font-weight: 700; border-style: solid; }

/* ── MATHLIVE DIALOG ── */
.math-modal {
  position: fixed; inset: 0; z-index: 1000; display: none;
  align-items: center; justify-content: center; padding: 20px;
  background: rgba(18,24,40,.55); backdrop-filter: blur(3px);
}
.math-modal.active { display: flex; }
.math-dialog {
  width: min(720px, 100%); background: #fff; border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25); padding: 20px;
}
.math-dialog-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.math-dialog-head h3 { font-size: 15pt; }
.math-dialog-head button { margin-left: auto; }
#mathField {
  display: block; width: 100%; min-height: 92px; padding: 14px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  font-size: 22px; background: #fff;
}
.math-dialog-help { margin: 10px 0 14px; color: var(--text-muted); font-size: 10pt; }
.math-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ── IMAGE / FORMULA DIALOG ── */
.media-dialog { width: min(900px, 100%); }
.media-paste-zone {
  min-height: 140px; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 8px; padding: 20px; text-align: center;
  border: 2px dashed var(--border-strong); border-radius: 10px;
  background: var(--surface2); outline: none;
}
.media-paste-zone:focus, .media-paste-zone.dragging { border-color: var(--accent); background: var(--accent-light); }
.media-paste-zone strong { font-size: 13pt; }
.media-paste-zone small { color: var(--text-muted); font-size: 10pt; }
.media-upload-link { color: var(--accent); text-decoration: underline; cursor: pointer; }
.media-preview-grid { display: none; grid-template-columns: 1fr 1fr; gap: 14px; }
.media-preview-grid.active { display: grid; }
.media-preview-pane { min-width: 0; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.media-preview-title { padding: 8px 11px; background: var(--surface2); border-bottom: 1px solid var(--border); font-weight: 700; font-size: 10pt; }
.media-image-wrap { min-height: 200px; display: flex; align-items: center; justify-content: center; padding: 12px; }
.media-image-wrap img { max-width: 100%; max-height: 320px; display: block; }
.media-result-wrap { padding: 12px; }
.media-empty-result { min-height: 132px; display: flex; align-items: center; justify-content: center; text-align: center; color: var(--text-muted); }
#ocrMathField { display: none; width: 100%; min-height: 132px; padding: 12px; border: 1px solid var(--border-strong); border-radius: 8px; font-size: 22px; }
#ocrMathField.active { display: block; }
.media-loading { display: none; align-items: center; justify-content: center; gap: 8px; min-height: 132px; color: var(--accent); }
.media-loading.active { display: flex; }
.media-spinner { width: 18px; height: 18px; border: 2px solid #c9d8f5; border-top-color: var(--accent); border-radius: 50%; animation: media-spin .8s linear infinite; }
@keyframes media-spin { to { transform: rotate(360deg); } }
.media-error { display: none; margin-top: 8px; padding: 8px 10px; border-radius: 6px; background: #fee2e2; color: #991b1b; font-size: 10pt; }
.media-error.active { display: block; }
.media-dialog-hint { margin-top: 8px; color: var(--text-muted); font-size: 9.5pt; }
.media-dialog-actions { display: none; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.media-dialog-actions.active { display: flex; }
.media-dialog-actions .media-replace-btn { margin-right: auto; }

/* ── QUESTION TEXT ── */
.q-text-label { font-size: 10pt; color: var(--text-muted); margin-bottom: 4px; font-family: var(--font); }
.q-text-input {
  width: 100%; min-height: 54px; padding: 8px 11px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 12pt; resize: vertical; outline: none;
  font-family: var(--font); color: var(--text); background: var(--surface);
  transition: border-color .15s; margin-bottom: 10px;
}
.q-text-input:focus { border-color: var(--accent); }
.q-img-preview { display: block; }

/* ── ANSWERS ── */
.answers-wrap { display: flex; flex-direction: column; gap: 6px; }
.answer-item { display: flex; flex-direction: column; gap: 6px; }
.answer-row { display: flex; align-items: center; gap: 8px; }
.answer-tools { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.answer-tool-btn {
  width: 32px; height: 32px; padding: 0; justify-content: center;
  font-size: 14px; overflow: hidden;
}
.answer-media-panel {
  margin: 0 26px 6px 38px; padding: 8px;
  border: 1px dashed var(--border); border-radius: var(--radius-sm);
  background: var(--surface2);
}
.answer-media-panel .tbl-gen { margin-top: 8px; }
.ans-prefix {
  width: 30px; height: 30px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 11pt; font-weight: 700; flex-shrink: 0; font-family: var(--font);
}
.pA { background: #dbeafe; color: #1d4ed8; }
.pB { background: #fce7f3; color: #be185d; }
.pC { background: #dcfce7; color: #15803d; }
.pD { background: #fef9c3; color: #a16207; }
.pE { background: #ede9fe; color: #6d28d9; }
.pF { background: #ffedd5; color: #c2410c; }

.ans-input {
  flex: 1; padding: 6px 9px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 12pt; outline: none; font-family: var(--font);
  transition: border-color .15s;
}
.ans-input:focus { border-color: var(--accent); }
.ans-check {
  width: 18px; height: 18px; flex-shrink: 0; cursor: pointer;
  accent-color: var(--green);
}
.ans-radio {
  width: 18px; height: 18px; flex-shrink: 0; cursor: pointer;
  accent-color: var(--green);
}

/* ── MATCH ── */
.match-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.match-col-title { font-size: 10pt; font-weight: 700; color: var(--text-muted); margin-bottom: 6px; font-family: var(--font); }
.match-row { display: flex; align-items: center; gap: 7px; margin-bottom: 6px; }
.match-lbl {
  width: 28px; height: 28px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 11pt; font-weight: 700; background: var(--surface2);
  border: 1px solid var(--border); flex-shrink: 0; font-family: var(--font);
}
.match-inp {
  flex: 1; padding: 5px 8px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 11pt; outline: none; font-family: var(--font);
}
.match-inp:focus { border-color: var(--accent); }
.match-ans-sel {
  width: 54px; padding: 5px 4px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 11pt; outline: none;
  font-family: var(--font); background: var(--surface2); font-weight: 700; color: var(--accent);
}

/* ── TABLE GEN ── */
.tbl-gen { margin-top: 8px; }
.tbl-ctrl { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.tbl-ctrl span { font-size: 10pt; color: var(--text-muted); font-family: var(--font); }
.tbl-ctrl input[type=number] {
  width: 50px; padding: 4px 6px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 11pt; text-align: center; outline: none;
  font-family: var(--font);
}
.editable-tbl { border-collapse: collapse; }
.editable-tbl td { border: 1px solid var(--border-strong); padding: 0; white-space: nowrap; }
.editable-tbl td input {
  border: none; padding: 5px 8px; font-size: 11pt; outline: none;
  background: transparent; width: 85px; font-family: var(--font);
}
.editable-tbl td .formula-btn { width: 26px; height: 26px; margin-right: 2px; }

/* ── IMG UPLOAD BTN ── */
.img-upload-label {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border: 1px dashed var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 10pt; color: var(--text-muted); background: var(--surface2);
  font-family: var(--font); transition: all .15s;
}
.img-upload-label:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }

/* ── RESIZABLE IMAGES ── */
.resizable-image {
  position: relative; display: inline-block; max-width: 100%;
  line-height: 0; margin-bottom: 10px; vertical-align: top;
}
.resizable-image img {
  width: 100%; height: auto; max-width: 100%; display: block;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.resizable-image.free-sized img { height: 100%; max-width: none; }
.resize-handle {
  position: absolute; right: -5px; bottom: -5px;
  width: 16px; height: 16px; border-radius: 4px;
  background: var(--accent); border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.25); cursor: nwse-resize;
  touch-action: none;
}
.resize-hint { font-size: 9pt; color: var(--text-muted); margin: -5px 0 8px; line-height: 1.2; }

/* ══════════════════════════════════
   PRINT / PREVIEW
══════════════════════════════════ */
#previewPage { display: none; }
#previewPage.active { display: block; }

.preview-topbar {
  position: sticky; top: 0; z-index: 200;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 10px 24px; display: flex; align-items: center; gap: 10px;
  box-shadow: var(--shadow);
}
.preview-topbar h2 { font-size: 13pt; font-weight: 700; font-family: var(--font); }
.print-note { font-size: 10pt; color: var(--text-muted); font-family: var(--font); }
.page-warning {
  display: none; padding: 6px 10px; border: 1px solid #ef4444; border-radius: 6px;
  background: #fef2f2; color: #b91c1c; font: 700 10pt var(--font);
}
.page-warning.show { display: inline-flex; }

/* A4 sheet preview */
.a4-container {
  padding: 24px 16px; background: var(--bg);
}
.a4-sheet {
  width: 794px; min-height: 1123px;
  margin: 0 auto; background: #fff;
  padding: 10mm;
  box-shadow: 0 4px 24px rgba(0,0,0,.15);
  font-family: 'Times New Roman', Times, serif; font-size: 11pt;
  position: relative;
}
.print-school-watermark {
  position: absolute; left: 50%; top: 50%; z-index: 0;
  width: 70%; max-height: 42%;
  transform: translate(-50%, -50%);
  object-fit: contain; opacity: .04;
  pointer-events: none; user-select: none;
}
.a4-sheet > :not(.print-school-watermark) {
  position: relative; z-index: 1;
}

/* PRINT HEADER */
.ph { text-align: center; margin-bottom: 16px; padding-bottom: 10px; border-bottom: 2px solid #222; }
.ph-name { font-size: 16pt; font-weight: 700; letter-spacing: 1.5px; text-transform: uppercase; }
.ph-variant { font-size: 12pt; color: #555; margin-top: 3px; }

/* SECTION in print */
.ps-title {
  font-size: 11pt; font-weight: 700; text-transform: uppercase;
  letter-spacing: .4px; margin: 14px 0 4px;
  padding-bottom: 3px; border-bottom: 1px solid #bbb;
  break-after: avoid;
}
.ps-inst {
  font-size: 10pt; font-style: italic; color: #444;
  margin-bottom: 8px; break-after: avoid;
}
.ps-ctx {
  border: 1px solid #aaa; border-radius: 4px;
  padding: 10px 12px; background: #f9f9f9;
  font-size: 11pt; margin-bottom: 10px;
}
.ps-ctx img { max-width: 100%; display: block; margin-top: 6px; }

/* TWO-COLUMN print */
.print-cols {
  column-count: 2;
  column-gap: 20px;
  column-fill: balance;
}
.pq {
  break-inside: avoid;
  break-inside: avoid-column;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  orphans: 3;
  widows: 3;
  margin-bottom: 10px;
}
.pq-head { font-weight: 700; font-size: 11pt; }
.pq-text { font-size: 11pt; margin-bottom: 4px; }
.pq img { max-width: 100%; display: block; margin: 4px 0; }
.pq-ans { padding-left: 6px; }
.pq-ans-item { display: block; font-size: 11pt; margin-bottom: 4px; break-inside: avoid; }
.pq-ans-text { display: block; }
.pq-ans-media { margin: 2px 0 5px 18px; }
.pq-ans-media img { margin: 2px 0; }
.pq-match { font-size: 11pt; }
.pq-match table { border-collapse: collapse; font-size: 11pt; margin-top: 4px; }
.pq-match td { padding: 3px 8px; border: 1px solid #bbb; }

/* ANSWER KEY */
.ak-wrap {
  margin-top: 20px; border: 1px solid #bbb; border-radius: 5px; overflow: hidden;
  page-break-inside: avoid;
}
.ak-title {
  background: #f0f0f0; padding: 8px 14px;
  font-weight: 700; font-size: 11pt; border-bottom: 1px solid #bbb;
}
.ak-grid { display: flex; flex-wrap: wrap; }
.ak-cell {
  min-width: 52px; padding: 6px 6px; text-align: center;
  border-right: 1px solid #ddd; border-bottom: 1px solid #ddd; font-size: 11pt;
}
.ak-cell .ak-n { display: block; font-size: 9pt; color: #777; font-weight: 600; }
.ak-cell .ak-a { display: block; font-weight: 700; color: #111; font-size: 11pt; }

/* PRINT MEDIA */
@page {
  size: A4;
  margin: 10mm;
  @top-left { content: ""; }
  @top-center { content: ""; }
  @top-right { content: ""; }
  @bottom-left { content: ""; }
  @bottom-center { content: ""; }
  @bottom-right {
    content: counter(page);
    font-family: 'Times New Roman', Times, serif;
    font-size: 11pt;
  }
}

@media print {
  #topbar, #workspaceLayout, .preview-topbar, .math-modal, .page-warning, .mobile-nav-toggle { display: none !important; }
  #previewPage { display: block !important; }
  .a4-container { padding: 0; background: transparent; }
  .a4-sheet { box-shadow: none; margin: 0; padding: 0; width: 100%; min-height: 0; }
  #a4Sheet, #a4Sheet * { font-size: 11pt !important; }
  #a4Sheet .print-school-watermark {
    position: fixed; left: 50%; top: 50%;
    width: 140mm; max-height: 90mm;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  body { background: white; }
  .resize-handle, .resize-hint { display: none !important; }
}

@media (max-width: 720px) {
  body { font-size: 11pt; }
  #topbar {
    min-height: 0; padding: 8px 10px 10px; gap: 8px;
    display: grid; grid-template-columns: repeat(2, minmax(0,1fr));
  }
  .brand-logo { grid-column: 1 / -1; width: 132px; height: 40px; flex-basis: auto; }
  .tb-sep, #topbar .spacer { display: none; }
  #topbar .tb-group:first-of-type {
    grid-column: 1 / -1; grid-row: 2;
    display: grid; grid-template-columns: auto minmax(0,1fr);
  }
  #topbar .tb-group:first-of-type #subjectSelect { grid-column: 2; }
  #topbar .tb-group:nth-of-type(2) { grid-column: 1 / -1; grid-row: 3; }
  #topbar .tb-group:nth-of-type(2) select { flex: 1; }
  #topbar select { min-width: 0; width: 100%; padding: 8px; }
  #topbar > .btn, #topbar > label.btn {
    min-height: 38px; padding: 7px 10px; justify-content: center;
  }
  #topbar > .btn-outline:nth-last-child(4) { grid-column: 1; grid-row: 4; }
  #topbar > label.btn-outline { grid-column: 2; grid-row: 4; }
  #topbar > .btn-green { grid-column: 1; grid-row: 5; }
  #topbar > .btn-outline:last-child { grid-column: 2; grid-row: 5; }
  #workspaceLayout { display: block; width: auto; padding: 12px 10px 80px; }
  #authorCard {
    position: static; width: auto; margin: 0 0 12px; padding: 12px;
    display: flex; align-items: center; gap: 12px; text-align: left;
  }
  .author-photo { width: 58px; height: 58px; flex: 0 0 58px; margin: 0; }
  .author-content { align-items: flex-start; gap: 2px; min-width: 0; }
  .author-kicker { font-size: 7.5pt; }
  .author-name { font-size: 11pt; }
  .author-role { font-size: 8.5pt; }
  .author-instagram { margin-top: 5px; padding: 4px 8px; font-size: 8.5pt; }
  #editorPage { padding: 0; }
  .progress-wrap { padding: 9px 11px; }
  .section-header { padding: 10px 12px; }
  .section-instruction, .ctx-block, .q-card { padding-left: 12px; padding-right: 12px; }
  .q-card-head { flex-wrap: wrap; }
  .q-card-actions { width: 100%; margin-left: 0; overflow-x: auto; padding-bottom: 2px; }
  .answer-row { align-items: flex-start; flex-wrap: wrap; }
  .ans-input { min-width: 0; width: calc(100% - 40px); }
  .answer-tools { margin-left: 38px; width: calc(100% - 38px); }
  .match-grid { grid-template-columns: 1fr; gap: 10px; }
  .math-dialog { padding: 14px; max-height: calc(100vh - 20px); overflow-y: auto; }
  .math-dialog-actions, .media-dialog-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .math-dialog-actions .btn, .media-dialog-actions .btn { justify-content: center; }
  .a4-container { overflow-x: auto; }
  #questionNav {
    position: fixed; z-index: 410; left: 10px; right: 10px; bottom: 68px; top: auto;
    width: auto; max-height: 58vh; padding: 14px;
    border-radius: 14px; transform: translateY(calc(100% + 90px));
    opacity: 0; pointer-events: none; transition: transform .22s ease, opacity .22s ease;
  }
  #questionNav.mobile-open { transform: translateY(0); opacity: 1; pointer-events: auto; }
  .qnav-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
  .qnav-title .action-btn { display: inline-flex; }
  .qnav-grid { grid-template-columns: repeat(8, 1fr); }
  .qnav-btn { min-width: 0; height: 34px; }
  .mobile-nav-toggle {
    display: inline-flex; position: fixed; z-index: 420; right: 14px; bottom: 14px;
    align-items: center; justify-content: center; gap: 7px; min-height: 44px;
    padding: 0 16px; border: 1px solid var(--gold); border-radius: 22px;
    background: var(--accent); color: #fff; box-shadow: 0 6px 20px rgba(60,8,12,.3);
    font: 700 11pt var(--font); cursor: pointer;
  }
  .media-preview-grid { grid-template-columns: 1fr; }
  .media-dialog-actions .media-replace-btn { width: 100%; margin-right: 0; }
  .preview-topbar { padding: 8px; flex-wrap: wrap; }
  .preview-topbar h2 { order: -1; width: 100%; }
  .print-note { display: none; }
}

@media (min-width: 721px) and (max-width: 1280px) {
  #workspaceLayout {
    grid-template-columns: 116px minmax(0, 1fr);
    grid-template-rows: auto auto;
  }
  #questionNav { grid-column: 1; grid-row: 1 / span 2; top: 146px; }
  #authorCard {
    position: static; grid-column: 2; grid-row: 1;
    width: min(620px, 100%); margin: 0; padding: 11px 16px;
    display: flex; align-items: center; gap: 14px; text-align: left;
  }
  #editorPage { grid-column: 2; grid-row: 2; }
  .author-photo { width: 58px; height: 58px; flex: 0 0 58px; margin: 0; }
  .author-content { display: grid; grid-template-columns: auto auto; column-gap: 12px; align-items: center; justify-content: start; }
  .author-kicker { grid-column: 1 / -1; }
  .author-name { grid-column: 1; }
  .author-role { grid-column: 1; }
  .author-instagram { grid-column: 2; grid-row: 2 / span 2; margin: 0; }
}

@media (min-width: 721px) and (max-width: 1650px) {
  #topbar { padding: 8px 16px; flex-wrap: wrap; }
  .brand-logo { width: 150px; flex-basis: 150px; }
  #topbar .spacer { display: none; }
  #questionNav, #authorCard { top: 146px; }
}

/* ── TOAST ── */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1c2033; color: #fff; padding: 9px 20px;
  border-radius: var(--radius-sm); font-size: 11pt; font-family: var(--font);
  opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 999;
}
#toast.show { opacity: 1; }

/* scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
