/* ==================== A4 EXAM SHEET ==================== */
.exam-sheet {
  position: relative;
  width: 210mm;
  min-height: 297mm;
  background: #ffffff;
  padding: var(--page-padding);
  box-shadow: 0 12px 36px rgba(15, 23, 42, 0.22);
  font-family: var(--font-exam);
  font-size: var(--font-size-exam);
  line-height: var(--line-height-exam);
  color: #000000;
  overflow: hidden;
}

/* ==================== WATERMARK & PATTERNS ==================== */
.watermark-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.watermark-text {
  font-family: system-ui, sans-serif;
  font-size: 54pt;
  font-weight: 800;
  color: var(--t-accent);
  opacity: var(--wm-opacity);
  transform: rotate(-32deg);
  white-space: nowrap;
  letter-spacing: 4px;
  text-transform: uppercase;
  user-select: none;
}

.watermark-layer.pattern-grid {
  background-image: radial-gradient(var(--t-accent) 0.9px, transparent 0.9px);
  background-size: 16px 16px;
  opacity: calc(var(--wm-opacity) * 1.4);
}

.watermark-layer.pattern-math {
  background-image: url("data:image/svg+xml,%3Csvg width='120' height='120' viewBox='0 0 120 120' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' stroke='%23334155' stroke-width='0.8'%3E%3Ccircle cx='25' cy='25' r='12'/%3E%3Cpath d='M75 15 L95 45 L55 45 Z'/%3E%3Cpath d='M15 95 Q35 65 55 95 T95 95'/%3E%3Cline x1='85' y1='75' x2='105' y2='75'/%3E%3Cline x1='95' y1='65' x2='95' y2='85'/%3E%3C/g%3E%3C/svg%3E");
  opacity: calc(var(--wm-opacity) * 1.1);
}

/* Kẻ hàng ngang như tập viết bài học sinh */
.watermark-layer.pattern-ruled {
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent 7.6mm,
    var(--t-accent) 7.6mm,
    var(--t-accent) 7.95mm
  );
  background-position: 0 10mm;
  opacity: calc(var(--wm-opacity) * 2.2);
}

/* Kẻ ô ly tập vở học sinh */
.watermark-layer.pattern-oli {
  background-image:
    linear-gradient(to right, var(--t-accent) 0.6px, transparent 0.6px),
    linear-gradient(to bottom, var(--t-accent) 0.6px, transparent 0.6px);
  background-size: 8mm 8mm;
  opacity: calc(var(--wm-opacity) * 2.0);
}

/* Page border frame (none / single / double) */
.page-frame {
  display: none;
  position: absolute;
  inset: 7mm;
  border: 2px solid var(--t-accent);
  pointer-events: none;
  z-index: 0;
}

.page-frame::after {
  content: "";
  position: absolute;
  inset: 3px;
  border: 0.75px solid var(--t-accent);
}

.exam-sheet[data-frame="none"] .page-frame {
  display: none !important;
}

.exam-sheet[data-frame="single"] .page-frame {
  display: block !important;
  border-width: 1.5px;
}

.exam-sheet[data-frame="single"] .page-frame::after {
  display: none;
}

.exam-sheet[data-frame="double"] .page-frame {
  display: block !important;
  border-width: 2px;
}

.exam-sheet[data-frame="double"] .page-frame::after {
  display: block;
}

/* Part heading background fill override */
.exam-sheet[data-fill-part="true"] .part-heading {
  background: var(--t-accent-light);
  border-left: 3.5px solid var(--t-accent);
  padding: 4px 8px;
}

.exam-sheet[data-fill-part="false"] .part-heading {
  background: transparent;
  border-left-color: transparent;
  padding-left: 0;
}

/* Question highlight / fill override */
.exam-sheet[data-fill-q="none"] .q-num {
  background: transparent;
  color: var(--t-accent);
  padding: 0;
  margin-right: 0;
}

.exam-sheet[data-fill-q="badge"] .q-num {
  background: var(--t-accent);
  color: #ffffff;
  padding: 1px 6px;
  margin-right: 4px;
  border-radius: 3px;
}

.exam-sheet[data-fill-q="card"] .question-item {
  background: var(--t-accent-light);
  border-left: 2.5px solid var(--t-accent);
  padding: 7px 10px;
  border-radius: 0 5px 5px 0;
}

.exam-sheet[data-fill-q="card"] .q-num {
  background: transparent;
  color: var(--t-accent);
  padding: 0;
}

/* Exam content wrapper above watermark layer */
.print-wrapper {
  width: 100%;
  position: relative;
  z-index: 1;
}

.running-header {
  display: flex;
  justify-content: space-between;
  font-size: 9.5pt;
  color: #475569;
  border-bottom: 0.75px solid #cbd5e1;
  padding-bottom: 4px;
  font-style: italic;
}

.running-footer {
  display: flex;
  justify-content: space-between;
  font-size: 9.5pt;
  color: #475569;
  border-top: 0.75px solid #cbd5e1;
  padding-top: 5px;
}

/* ==================== EXAM HEADER & STUDENT INFO ==================== */
.exam-header {
  display: grid;
  grid-template-columns: 44% 56%;
  gap: 10px;
  margin-bottom: 10px;
  text-align: center;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--t-header-bg);
  color: var(--t-header-color);
}

.header-left,
.header-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.org-upper { font-size: 10.5pt; text-transform: uppercase; }
.org-main  { font-size: 11.5pt; font-weight: bold; text-transform: uppercase; }
.exam-title { font-size: 12pt; font-weight: bold; text-transform: uppercase; }
.exam-sub   { font-size: 11pt; font-weight: bold; }
.exam-time  { font-size: 10.5pt; font-style: italic; }

.short-hr {
  width: 45%;
  border: none;
  border-top: 1px solid currentColor;
  margin: 4px auto;
  opacity: 0.7;
}

.student-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--t-border-color);
  border-bottom: 1.5px double var(--t-border-color);
  padding: 6px 4px;
  margin-bottom: 14px;
  font-size: 11.5pt;
  gap: 10px;
}

.exam-code-box {
  border: 1.5px solid var(--t-accent);
  color: var(--t-accent);
  background: var(--t-accent-light);
  padding: 2px 10px;
  font-weight: bold;
  border-radius: 3px;
  white-space: nowrap;
}

/* ==================== PARTS & QUESTIONS ==================== */
.part-heading {
  font-weight: bold;
  margin: 14px 0 8px 0;
  padding: 4px 8px;
  background: var(--t-part-bg);
  border-left: 3.5px solid var(--t-part-border);
  color: var(--t-accent);
  border-radius: 3px;
  page-break-after: avoid;
  break-after: avoid;
}

.part-note {
  font-style: italic;
  font-weight: normal;
  color: #1e293b;
}

.question-item {
  margin-bottom: 11px;
  break-inside: avoid;
  page-break-inside: avoid;
}

.question-stem {
  text-align: justify;
  margin-bottom: 5px;
}

.q-num {
  font-weight: bold;
  color: var(--t-qnum-color);
  background: var(--t-qnum-bg);
  padding: 0 3px;
  border-radius: 3px;
}

.question-stem img,
.option-item img,
.explanation-box img,
.question-stem svg:not(.katex svg),
.option-item svg:not(.katex svg),
.explanation-box svg:not(.katex svg) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 6px auto;
}

/* Bảo vệ SVG nội bộ của KaTeX (\sqrt, \overrightarrow, \widehat, ngoặc kéo giãn...) */
.exam-sheet .katex svg {
  max-width: none;
  height: inherit;
  margin: 0;
}

/* Bố cục kiểu immini (LaTeX) cho câu MCQ có hình vẽ: nội dung + đáp án bên trái, hình bên phải */
.mcq-immini {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(36mm, 50mm);
  gap: 8px 12px;
  align-items: start;
}

.mcq-immini-content {
  min-width: 0;
}

.mcq-immini-media {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 4px;
}

.mcq-immini-media svg,
.mcq-immini-media img {
  max-width: 100%;
  max-height: 46mm;
  width: auto;
  height: auto;
  display: block;
  margin: 0 auto;
}

.options-grid {
  display: grid;
  gap: 4px 14px;
  margin-top: 4px;
}

.options-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.options-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.options-grid.cols-1 { grid-template-columns: 1fr; }

.opt-label {
  font-weight: bold;
  color: var(--t-accent);
}

.opt-correct-mark {
  text-decoration: underline;
  color: #b91c1c !important;
}

.msq-options {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-left: 14px;
}

.sa-line {
  margin-top: 4px;
  font-style: italic;
  font-size: 11pt;
}

.explanation-box {
  margin-top: 6px;
  padding: 6px 10px;
  border-left: 3px solid var(--t-accent);
  background: var(--t-accent-light);
  font-size: 11pt;
  border-radius: 0 4px 4px 0;
}

.explanation-title {
  font-weight: bold;
  color: var(--t-accent);
}

.exam-end {
  text-align: center;
  font-weight: bold;
  margin-top: 18px;
  color: var(--t-accent);
  letter-spacing: 0.8px;
}

/* ==================== ANSWER KEY SECTION ==================== */
.answer-key-section {
  margin-top: 20px;
  page-break-before: always;
  break-before: page;
}

.answer-key-title {
  text-align: center;
  font-weight: bold;
  font-size: 13pt;
  color: var(--t-accent);
  text-transform: uppercase;
  margin-bottom: 10px;
}

table.ans-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
  font-size: 11pt;
  text-align: center;
}

table.ans-table th,
table.ans-table td {
  border: 1px solid #334155;
  padding: 4px 6px;
}

table.ans-table th {
  background: var(--t-accent-light);
  color: var(--t-accent);
  font-weight: bold;
}

/* ==================== OFF-SCREEN BATCH PDF EXPORT STAGE ==================== */
.batch-export-stage {
  position: fixed;
  left: -12000px;
  top: 0;
  width: 210mm;
  pointer-events: none;
  z-index: -1;
}

.exam-sheet.export-measure-sheet {
  width: 210mm;
  min-height: 0;
  height: auto;
  box-shadow: none;
  overflow: visible;
}

.exam-sheet.export-a4-page {
  width: 210mm;
  height: 297mm;
  min-height: 297mm;
  max-height: 297mm;
  box-shadow: none;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.export-page-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.export-run-header {
  margin-bottom: 6px;
  flex-shrink: 0;
}

.export-page-content {
  flex: 1;
  min-height: 0;
}

.export-run-footer {
  margin-top: auto;
  padding-top: 5px;
  flex-shrink: 0;
}
