/* Reset and Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: #f1f5f9;
  color: #1e293b;
  min-height: 100vh;
}

/* Header toolbar */
.app-header {
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  padding: 12px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  background: #f8fafc;
  color: #475569;
  border: 1px solid #cbd5e1;
  transition: all 0.2s ease;
}

.btn-back:hover {
  background: #0284c7;
  color: #ffffff;
  border-color: #0284c7;
}

.header-icon {
  width: 40px;
  height: 40px;
  background: #e0f2fe;
  color: #0284c7;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-left h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: #0f172a;
}

.subtitle {
  font-size: 0.82rem;
  color: #64748b;
  margin-top: 2px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #0284c7;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s;
  box-shadow: 0 2px 4px rgba(2, 132, 199, 0.25);
}

.btn-primary:hover {
  background: #0369a1;
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  color: #334155;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  background: #f1f5f9;
  border-color: #94a3b8;
  color: #0f172a;
}

.btn-secondary:active {
  transform: translateY(1px);
}

/* Control Config Card */
.controls-wrapper {
  max-width: 1200px;
  margin: 16px auto 0;
  padding: 0 16px;
}

.controls-card {
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid #e2e8f0;
  padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.control-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 12px;
}

.control-row:last-of-type {
  margin-bottom: 12px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-width: 180px;
}

.control-group.flex-2 {
  flex: 2;
  min-width: 260px;
}

.control-group label {
  font-size: 12.5px;
  font-weight: 600;
  color: #475569;
}

.form-select, .form-input {
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13.5px;
  color: #1e293b;
  outline: none;
  transition: border-color 0.2s;
  background: #ffffff;
}

.form-select:focus, .form-input:focus {
  border-color: #0284c7;
  box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15);
}

.tips-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12.5px;
}

/* Preview Workspace */
.preview-area {
  display: flex;
  justify-content: center;
  padding: 24px 16px 60px;
  overflow-x: auto;
}

.sheets-container {
  display: flex;
  flex-direction: column;
  gap: 32px;
  align-items: center;
}

/* A4 Page Simulation */
.a4-page {
  width: 210mm;
  min-height: 297mm;
  height: 297mm;
  background: #ffffff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  box-sizing: border-box;
  padding: 7mm 8mm;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  color: #000000;
  font-family: 'Times New Roman', Times, serif;
}

/* Multi-sheet in 1 page (layout 2) */
.a4-page.layout-split {
  padding: 5mm 6mm;
  gap: 4mm;
}

.cut-divider {
  border-top: 1.5px dashed #64748b;
  position: relative;
  margin: 1.5mm 0;
  width: 100%;
}

.cut-divider::before {
  content: "✂ CẮT TẠI ĐÂY";
  position: absolute;
  top: -9px;
  left: 50%;
  transform: translateX(-50%);
  background: #ffffff;
  padding: 0 8px;
  font-size: 10px;
  font-family: 'Inter', sans-serif;
  color: #64748b;
  font-weight: 600;
  letter-spacing: 0.5px;
}

/* Answer Sheet Box */
.sheet-unit {
  border: 2px solid #000;
  padding: 4mm 5mm;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  flex: 1;
  position: relative;
}

.a4-page.layout-split .sheet-unit {
  padding: 2.5mm 3.5mm;
  border: 1.5px solid #000;
}

/* Sheet Header */
.sheet-unit-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 2mm;
  border-bottom: 1px solid #000;
  padding-bottom: 1.5mm;
}

.header-meta {
  flex: 1;
}

.school-name {
  font-size: 11pt;
  font-weight: bold;
  text-transform: uppercase;
}

.exam-name {
  font-size: 10pt;
  font-weight: bold;
  margin-top: 1mm;
}

.exam-time {
  font-size: 9pt;
  font-style: italic;
  margin-top: 0.5mm;
}

.header-title-box {
  text-align: right;
  flex: 1;
}

.sheet-main-title {
  font-size: 12.5pt;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sheet-sub-title {
  font-size: 9pt;
  font-weight: normal;
  font-style: italic;
  margin-top: 0.5mm;
}

/* Top Section: Info & SBD & Mã Đề */
.sheet-top-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 3mm;
  margin-bottom: 2mm;
}

/* Student Info Area */
.student-info-box {
  border: 1px solid #000;
  padding: 2mm 3mm;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  font-size: 9.5pt;
  line-height: 1.55;
}

.info-line {
  display: flex;
  align-items: baseline;
}

.info-label {
  font-weight: bold;
  white-space: nowrap;
}

.dotted-line {
  flex: 1;
  border-bottom: 1px dotted #000;
  margin-left: 4px;
  height: 12px;
}

.instructions-box {
  margin-top: 1.5mm;
  padding: 1.5mm;
  border-top: 1px dashed #666;
  font-size: 8pt;
  color: #111;
  line-height: 1.35;
}

.instructions-box strong {
  text-decoration: underline;
}

.guide-bubbles {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 1mm;
  font-size: 7.5pt;
}

.sample-circle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid #000;
  font-size: 7pt;
  font-family: Arial, sans-serif;
  font-weight: bold;
}

.sample-circle.filled {
  background: #000;
  color: #fff;
}

.sample-circle.wrong-x {
  position: relative;
}
.sample-circle.wrong-x::after {
  content: "✕";
  color: red;
  font-size: 10px;
  font-weight: bold;
}

/* Grid for SBD and Mã Đề */
.identification-boxes {
  display: flex;
  gap: 2mm;
}

.code-table-box {
  border: 1px solid #000;
  display: flex;
  flex-direction: column;
}

.code-title {
  text-align: center;
  font-size: 8pt;
  font-weight: bold;
  background: #f0f0f0;
  border-bottom: 1px solid #000;
  padding: 1.5px 2px;
  text-transform: uppercase;
}

.code-grid {
  border-collapse: collapse;
  margin: 0 auto;
}

.code-grid td, .code-grid th {
  border: 1px solid #333;
  width: 15px;
  height: 15px;
  text-align: center;
  padding: 0;
  vertical-align: middle;
}

.digit-header-cell {
  height: 18px !important;
  background: #fff;
}

.digit-header-cell span {
  display: block;
  width: 100%;
  height: 100%;
}

.bubble-cell {
  font-family: Arial, sans-serif;
  font-size: 7pt;
  font-weight: bold;
  cursor: default;
}

.bubble-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 12px;
  height: 12px;
  border: 1px solid #444;
  border-radius: 50%;
}

/* Answers Section */
.answers-section {
  border: 1px solid #000;
  padding: 2mm 3mm;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.answers-title {
  text-align: center;
  font-weight: bold;
  font-size: 9.5pt;
  text-transform: uppercase;
  margin-bottom: 1.5mm;
  letter-spacing: 0.5px;
  border-bottom: 1px solid #ddd;
  padding-bottom: 1mm;
}

.answers-columns-container {
  display: flex;
  justify-content: space-around;
  flex: 1;
  gap: 3mm;
}

.answer-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.answer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7mm 0;
  border-bottom: 0.5px dotted #ccc;
  font-family: Arial, sans-serif;
}

.a4-page.layout-split .answer-row {
  padding: 0.4mm 0;
}

.answer-row:last-child {
  border-bottom: none;
}

.question-num {
  font-weight: bold;
  font-size: 8pt;
  min-width: 18px;
  text-align: right;
  margin-right: 4px;
}

.bubbles-group {
  display: flex;
  gap: 3.5px;
}

.opt-bubble {
  width: 14px;
  height: 14px;
  border: 1px solid #222;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 7.5pt;
  font-weight: bold;
  color: #111;
  user-select: none;
}

/* Corner Alignment Markers for Scanner Compatibility */
.corner-marker {
  position: absolute;
  width: 7mm;
  height: 7mm;
}
.marker-tl { top: 1.5mm; left: 1.5mm; border-top: 2.5px solid #000; border-left: 2.5px solid #000; }
.marker-tr { top: 1.5mm; right: 1.5mm; border-top: 2.5px solid #000; border-right: 2.5px solid #000; }
.marker-bl { bottom: 1.5mm; left: 1.5mm; border-bottom: 2.5px solid #000; border-left: 2.5px solid #000; }
.marker-br { bottom: 1.5mm; right: 1.5mm; border-bottom: 2.5px solid #000; border-right: 2.5px solid #000; }

/* 2-split adjustments */
.a4-page.layout-split .sheet-unit-header {
  margin-bottom: 1.5mm;
  padding-bottom: 1mm;
}
.a4-page.layout-split .school-name {
  font-size: 9pt;
}
.a4-page.layout-split .sheet-main-title {
  font-size: 10.5pt;
}
.a4-page.layout-split .exam-name {
  font-size: 8.5pt;
}
.a4-page.layout-split .student-info-box {
  font-size: 8pt;
  line-height: 1.45;
  padding: 1.5mm 2mm;
}
.a4-page.layout-split .instructions-box {
  font-size: 7pt;
  padding: 1mm;
}
.a4-page.layout-split .code-grid td, .a4-page.layout-split .code-grid th {
  width: 13px;
  height: 13px;
}
.a4-page.layout-split .bubble-num {
  width: 10px;
  height: 10px;
  font-size: 6.5pt;
}
.a4-page.layout-split .opt-bubble {
  width: 12px;
  height: 12px;
  font-size: 6.5pt;
}
.a4-page.layout-split .question-num {
  font-size: 7.5pt;
  min-width: 16px;
}

/* ==========================================================
   PRINT MEDIA RULES
   ========================================================== */
@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  html, body {
    margin: 0;
    padding: 0;
    background: #ffffff !important;
    width: 210mm;
    height: 297mm;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .no-print {
    display: none !important;
  }

  .preview-area {
    padding: 0 !important;
    margin: 0 !important;
  }

  .sheets-container {
    gap: 0 !important;
  }

  .a4-page {
    box-shadow: none !important;
    border: none !important;
    margin: 0 !important;
    width: 210mm !important;
    height: 297mm !important;
    page-break-after: always;
    break-after: page;
  }
}
