@media print {
  /* Issue #437: long reports (cash handover, fee analytics) clipped in
     the browser's print-preview dialog because the app layout sets
     html/body/main with viewport-bound overflow rules. Force the
     document to flow naturally across pages so the preview paginates
     and the user can scroll through all pages before printing. */
  html, body {
    height: auto !important;
    overflow: visible !important;
    max-height: none !important;
  }

  /* Hide non-printable elements */
  #sidebar,
  aside,
  .safe-area-shell-top,
  .mobile-topbar,
  .admin-topbar,
  nav,
  .sidebar,
  .topbar,
  #sidebar-toggle,
  #user-dropdown-container,
  .no-print,
  button:not(.print-include),
  a.btn,
  .pagination,
  .flash,
  [data-controller="flash"],
  [data-controller="sidebar"],
  [data-controller="dropdown"] {
    display: none !important;
  }

  /* Reset layout */
  body {
    background: white !important;
    color: black !important;
    font-size: 12pt !important;
    line-height: 1.4 !important;
  }

  #main-content,
  main {
    margin-left: 0 !important;
    padding: 0 !important;
    width: 100% !important;
  }

  /* Page setup */
  @page {
    size: A4;
    margin: 15mm 10mm;
  }

  /* Prevent page breaks inside elements */
  .card,
  table,
  tr,
  .stat-card,
  .report-card-section {
    page-break-inside: avoid;
  }

  /* Table styling for print */
  table {
    border-collapse: collapse;
    width: 100%;
  }

  th, td {
    border: 1px solid #333;
    padding: 6px 8px;
    text-align: left;
  }

  th {
    background-color: #f3f4f6 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* Fee analytics / dashboards — remove overflow clipping so all content prints.
     Issue #437: added .overflow-x-hidden / .overflow-y-hidden because the
     app layout's <main> uses overflow-x-hidden, which was clipping the
     cash handover report in the print-preview dialog. */
  .overflow-hidden,
  .overflow-x-hidden,
  .overflow-y-hidden,
  .overflow-x-auto,
  .overflow-y-auto,
  .overflow-auto,
  [style*="overflow"],
  [style*="max-height"] {
    overflow: visible !important;
    max-height: none !important;
    height: auto !important;
  }

  /* Charts should page-break after, not clip */
  canvas {
    max-width: 100% !important;
    height: auto !important;
    page-break-after: auto;
  }

  /* Dashboard stat cards — flow naturally */
  .grid {
    display: block !important;
  }

  .grid > * {
    page-break-inside: avoid;
    margin-bottom: 10px;
  }

  /* Fee receipt specific — fits both A4 and thermal 80mm */
  .fee-receipt {
    max-width: 210mm;
    margin: 0 auto;
    padding: 10mm;
    border: 2px solid #333;
  }

  .fee-receipt .school-header {
    text-align: center;
    border-bottom: 2px solid #333;
    padding-bottom: 10px;
    margin-bottom: 15px;
  }

  /* Thermal receipt (80mm POS printer) */
  .fee-receipt.thermal,
  .fee-receipt--thermal {
    max-width: 72mm;    /* 80mm paper minus ~4mm margin each side */
    padding: 2mm;
    border: none;
    font-size: 9pt;
    line-height: 1.2;
  }

  .fee-receipt.thermal .school-header,
  .fee-receipt--thermal .school-header {
    border-bottom: 1px dashed #333;
    padding-bottom: 4px;
    margin-bottom: 6px;
  }

  .fee-receipt.thermal table,
  .fee-receipt--thermal table {
    font-size: 8pt;
  }

  .fee-receipt.thermal th,
  .fee-receipt.thermal td,
  .fee-receipt--thermal th,
  .fee-receipt--thermal td {
    padding: 2px 3px;
    border: none;
    border-bottom: 1px dotted #999;
  }

  /* Report card specific — A4 portrait */
  @page report-card {
    size: A4 portrait;
    margin: 15mm;
  }

  @page single-report-card {
    size: A4 portrait;
    margin: 8mm;
  }

  .report-card {
    page: report-card;
    max-width: 210mm;
    margin: 0 auto;
    border: 2px solid #333;
    padding: 15mm;
    font-size: 11pt;
    line-height: 1.4;
    color: #000 !important;
    background: #fff !important;
  }

  .report-card * {
    color: #000 !important;
    background: transparent !important;
  }

  .report-card table {
    width: 100% !important;
    border-collapse: collapse;
  }

  .report-card th,
  .report-card td {
    border: 1px solid #333 !important;
    padding: 4px 8px;
    text-align: left;
    font-size: 10pt;
  }

  .report-card th {
    background: #f0f0f0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    font-weight: bold;
  }

  .report-card .school-header {
    text-align: center;
    margin-bottom: 20px;
  }

  .report-card .school-header h1,
  .report-card .school-header h2 {
    margin: 4px 0;
  }

  .report-card .student-info {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 15px;
  }

  .report-card .grade-summary {
    margin-top: 15px;
    page-break-inside: avoid;
  }

  .report-card .signatures {
    margin-top: 30px;
    display: flex;
    justify-content: space-between;
    page-break-inside: avoid;
  }

  /* Template report cards use one portrait design system for Single and
     Combined output. Semantic component hooks keep print proportions aligned
     without relying on responsive screen breakpoints or global font shrinking. */
  @page combined-report-card {
    size: A4 portrait;
    margin: 8mm;
  }

  .single-exam-report-card {
    page: single-report-card;
  }

  .combined-report-card {
    page: combined-report-card;
  }

  .single-exam-report-card,
  .combined-report-card {
    --report-card-body-size: 9pt;
    --report-card-meta-size: 8.25pt;
    --report-card-section-heading-size: 9.5pt;
    --report-card-title-size: 11.5pt;
    --report-card-school-name-size: 16pt;
    --report-card-section-gap: 1.2mm;
    --report-card-signature-space: 24mm;
    width: 100% !important;
    max-width: none !important;
    padding: 0 !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .single-exam-report-card .report-card-header,
  .combined-report-card .report-card-header {
    display: block !important;
    margin-bottom: 1.5mm !important;
    padding-bottom: 1mm !important;
  }

  .single-exam-report-card .report-card-header img,
  .combined-report-card .report-card-header img {
    width: 10mm !important;
    height: 10mm !important;
    margin-bottom: 0.75mm !important;
  }

  .single-exam-report-card .report-card-school-name,
  .combined-report-card .report-card-school-name {
    margin: 0 !important;
    font-size: var(--report-card-school-name-size) !important;
    line-height: 1.1 !important;
  }

  .single-exam-report-card .report-card-header p,
  .combined-report-card .report-card-header p {
    font-size: var(--report-card-meta-size) !important;
    line-height: 1.15 !important;
  }

  .single-exam-report-card .report-card-header .report-card-title,
  .combined-report-card .report-card-header .report-card-title {
    margin-top: 0.75mm !important;
    font-size: var(--report-card-title-size) !important;
    line-height: 1.1 !important;
  }

  .single-exam-report-card [data-testid="configurable-report-sections"],
  .combined-report-card [data-testid="configurable-report-sections"] {
    display: flex !important;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--report-card-section-gap) 1.5mm !important;
    break-inside: auto;
    page-break-inside: auto;
  }

  .single-exam-report-card [data-testid="configurable-report-sections"] section,
  .combined-report-card [data-testid="configurable-report-sections"] section {
    box-sizing: border-box;
    display: block !important;
    width: calc(50% - 0.75mm) !important;
    margin: 0 !important;
    min-width: 0;
    flex: 0 0 auto;
  }

  .single-exam-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-student"],
  .single-exam-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-academics"],
  .single-exam-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-summary"],
  .single-exam-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-signatures"],
  .combined-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-student"],
  .combined-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-academics"],
  .combined-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-summary"],
  .combined-report-card [data-testid="configurable-report-sections"] [data-testid="report-section-signatures"] {
    flex: 0 0 auto !important;
    width: 100% !important;
  }

  .single-exam-report-card .report-card-section-heading,
  .combined-report-card .report-card-section-heading {
    margin-bottom: 0.6mm !important;
    padding: 0.7mm 1.2mm !important;
    font-size: var(--report-card-section-heading-size) !important;
    line-height: 1.1 !important;
  }

  .single-exam-report-card .report-card-student-grid,
  .combined-report-card .report-card-student-grid {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
  }

  .single-exam-report-card .grid > *,
  .combined-report-card .grid > * {
    margin-bottom: 0 !important;
  }

  .single-exam-report-card [data-testid="report-section-attendance"] > .grid,
  .combined-report-card [data-testid="report-section-attendance"] > .grid {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .single-exam-report-card [data-testid="report-section-health"] > .grid,
  .combined-report-card [data-testid="report-section-health"] > .grid,
  .combined-report-card [data-testid="report-section-remarks"] > .grid,
  .single-exam-report-card [data-testid="report-section-promotion"] > .grid,
  .combined-report-card [data-testid="report-section-promotion"] > .grid {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .single-exam-report-card .report-card-student-field,
  .combined-report-card .report-card-student-field {
    display: grid !important;
    grid-template-columns: minmax(0, 42%) minmax(0, 58%) !important;
    align-items: start;
    column-gap: 1mm;
    padding: 0.75mm 1mm !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
  }

  .single-exam-report-card .report-card-student-field dt,
  .combined-report-card .report-card-student-field dt {
    width: auto !important;
  }

  .single-exam-report-card th,
  .single-exam-report-card td,
  .combined-report-card th,
  .combined-report-card td {
    padding: 0.55mm 0.8mm !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
  }

  .single-exam-report-card section,
  .combined-report-card section,
  .single-exam-report-card tr,
  .combined-report-card tr {
    break-inside: avoid-page;
    page-break-inside: avoid;
  }

  .single-exam-report-card [data-testid="report-section-student"],
  .combined-report-card [data-testid="report-section-student"] {
    break-after: avoid-page;
    page-break-after: avoid;
  }

  /* A short academic table must stay one unit. In Chromium paged flex
     layout, allowing this item to fragment can stretch its first fragment to
     the page bottom, then move the tfoot and all following sections to a new
     sheet even when the complete card easily fits on page one. */
  .single-exam-report-card [data-testid="report-section-academics"],
  .combined-report-card [data-testid="report-section-academics"] {
    break-inside: avoid-page;
    page-break-inside: avoid;
  }

  .single-exam-report-card table,
  .combined-report-card table {
    break-inside: auto;
    page-break-inside: auto;
  }

  .single-exam-report-card table,
  .combined-report-card table {
    table-layout: fixed !important;
    width: 100% !important;
  }

  .combined-report-card .report-card-responsive-table {
    max-width: 100% !important;
    overflow: visible !important;
  }

  .single-exam-report-card thead th,
  .combined-report-card thead th {
    width: 1% !important;
  }

  .single-exam-report-card th,
  .single-exam-report-card td,
  .combined-report-card th,
  .combined-report-card td {
    overflow-wrap: break-word;
    white-space: normal;
  }

  .single-exam-report-card thead,
  .combined-report-card thead {
    display: table-header-group;
  }

  /* Single Exam totals are ordinary content, not a repeating page footer.
     Keeping the tfoot in the table row flow avoids Chromium's special paged
     footer placement that produced the reporter's mostly-empty first page. */
  .single-exam-report-card .report-card-academic-table tfoot {
    display: table-row-group;
  }

  .single-exam-report-card section [class~="p-2"],
  .combined-report-card section [class~="p-2"],
  .single-exam-report-card section [class~="p-3"],
  .combined-report-card section [class~="p-3"] {
    padding: 0.8mm 1mm !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
  }

  .single-exam-report-card .report-card-summary-grid,
  .combined-report-card .report-card-summary-grid {
    display: grid !important;
    gap: 0.8mm !important;
  }

  .single-exam-report-card .report-card-summary-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
  }

  .combined-report-card .report-card-summary-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr)) !important;
  }

  .single-exam-report-card .report-card-summary-item,
  .combined-report-card .report-card-summary-item {
    min-width: 0;
    padding: 0.8mm 1mm !important;
  }

  .single-exam-report-card .report-card-summary-label,
  .combined-report-card .report-card-summary-label {
    font-size: 8pt !important;
    line-height: 1.05 !important;
  }

  .single-exam-report-card .report-card-summary-value,
  .combined-report-card .report-card-summary-value {
    margin-top: 0.4mm;
    font-size: 10.75pt !important;
    line-height: 1.05 !important;
  }

  .single-exam-report-card .report-card-signature-grid,
  .combined-report-card .report-card-signature-grid {
    display: grid !important;
    padding-top: var(--report-card-signature-space) !important;
    font-size: var(--report-card-body-size) !important;
    line-height: 1.15 !important;
  }

  [data-testid="professional-report-card"] {
    break-after: page;
    page-break-after: always;
  }

  [data-testid="professional-report-card"]:last-child {
    break-after: auto;
    page-break-after: auto;
  }

  /* Attendance register specific */
  @page attendance-register {
    size: A4 landscape;
    margin: 10mm 8mm;
  }

  .attendance-register {
    page: attendance-register;
    font-size: 9pt;
  }

  .attendance-register th,
  .attendance-register td {
    padding: 3px 4px;
    text-align: center;
  }

  .attendance-register-table {
    font-size: 8pt;
  }

  .attendance-register-table th,
  .attendance-register-table td {
    padding: 2px 3px;
    border: 1px solid #666;
  }

  .attendance-register .print\:block {
    display: block !important;
  }

  /* Certificate specific */
  .certificate {
    max-width: 297mm;
    min-height: 210mm;
    margin: 0 auto;
    padding: 20mm;
    border: 3px double #333;
    text-align: center;
  }

  .certificate .cert-border {
    border: 1px solid #666;
    padding: 15mm;
    min-height: calc(210mm - 40mm);
  }

  /* Remove background colors except where explicitly preserved */
  *:not(th):not(.print-bg) {
    background-color: transparent !important;
  }

  [data-testid="professional-report-card"] *,
  [data-testid="professional-report-card"] {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  [data-testid="professional-report-card"] .bg-gray-900 {
    color: #fff !important;
    background-color: #111827 !important;
  }

  [data-testid="professional-report-card"] .bg-gray-100 {
    background-color: #f3f4f6 !important;
  }

  [data-testid="professional-report-card"] .bg-gray-50 {
    background-color: #f9fafb !important;
  }

  [data-testid="professional-report-card"] .bg-green-50 {
    background-color: #f0fdf4 !important;
  }

  [data-testid="professional-report-card"] .bg-red-50 {
    background-color: #fef2f2 !important;
  }

  /* Issue #2511: the generic print cleanup above must not flatten the shared
     school-report palette back to black, white, and grey. Reassert the same
     semantic colors after that reset so screen, preview, PDF, and color paper
     preserve one visual hierarchy. */
  [data-testid="professional-report-card"] .report-card-document {
    background-color: #fff !important;
    border-color: var(--report-card-line, #a8c0c0) !important;
    color: var(--report-card-ink, #24353b) !important;
  }

  [data-testid="professional-report-card"] .report-card-header {
    background-color: #fff !important;
    border-bottom-color: var(--report-card-accent, #bf8b30) !important;
  }

  [data-testid="professional-report-card"] .report-card-section-heading {
    background-color: var(--report-card-primary, #174f55) !important;
    color: #fff !important;
  }

  [data-testid="professional-report-card"] .report-card-student-field:nth-child(4n + 1),
  [data-testid="professional-report-card"] .report-card-student-field:nth-child(4n + 2) {
    background-color: var(--report-card-surface-alt, #fafcfb) !important;
  }

  [data-testid="professional-report-card"] table thead th {
    background-color: var(--report-card-soft, #e7f0ef) !important;
    color: var(--report-card-primary-dark, #123d40) !important;
  }

  [data-testid="professional-report-card"] table tbody th {
    background-color: var(--report-card-surface, #f4f8f7) !important;
    color: var(--report-card-primary-dark, #123d40) !important;
  }

  [data-testid="professional-report-card"] .report-card-academic-table tbody tr:nth-child(even) > :not(.bg-green-50) {
    background-color: var(--report-card-surface-alt, #fafcfb) !important;
  }

  [data-testid="professional-report-card"] .report-card-academic-table tfoot td {
    background-color: var(--report-card-accent-soft, #faf3e5) !important;
    color: var(--report-card-primary-dark, #123d40) !important;
  }

  [data-testid="professional-report-card"] .report-card-summary-item {
    background-color: var(--report-card-surface, #f4f8f7) !important;
  }

  [data-testid="professional-report-card"] .report-card-result--pass {
    background-color: var(--report-card-pass-bg, #e7f5ec) !important;
  }

  [data-testid="professional-report-card"] .report-card-result--fail {
    background-color: var(--report-card-fail-bg, #fbecec) !important;
  }

  /* Preserve background for elements that need it */
  .print-bg,
  th {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* Bulk print: page break between report cards / certificates */
  .report-card + .report-card,
  .certificate + .certificate {
    page-break-before: always;
  }

  /* Transfer certificate specific */
  .transfer-certificate {
    max-width: 210mm;
    margin: 0 auto;
    padding: 15mm;
    border: 2px solid #333;
  }

  .transfer-certificate .school-header {
    text-align: center;
    border-bottom: 2px solid #333;
    padding-bottom: 10px;
    margin-bottom: 20px;
  }

  .transfer-certificate .tc-field {
    display: flex;
    gap: 8px;
    padding: 4px 0;
    border-bottom: 1px dotted #999;
  }

  .transfer-certificate .tc-label {
    font-weight: bold;
    min-width: 180px;
  }

  /* Links */
  a {
    color: black !important;
    text-decoration: none !important;
  }

  /* Images */
  img {
    max-width: 100%;
  }
}
