/* ═══════════════════════════════════════════════════════════ */
/* A4 TIMETABLE PRINT STYLESHEET (Portrait & Landscape Support) */
/* ═══════════════════════════════════════════════════════════ */

@media print {
    /* 1. DYNAMIC PAGE ORIENTATION: Landscape for Class-wise, Portrait for Teacher-wise */
    @page {
        size: landscape;
        margin: 0.35in 0.4in;
    }

    @page portrait-page {
        size: portrait;
        margin: 0.35in 0.35in;
    }

    @page landscape-page {
        size: landscape;
        margin: 0.35in 0.4in;
    }

    body.print-orientation-portrait,
    .print-portrait-mode {
        page: portrait-page;
    }

    body.print-orientation-landscape,
    .print-landscape-mode {
        page: landscape-page;
    }

    /* 2. COLOR FIDELITY & EXACT RENDERING */
    *, *::before, *::after {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        color-adjust: exact !important;
        box-sizing: border-box !important;
    }

    html, body {
        background: #ffffff !important;
        color: #0f172a !important;
        padding: 0 !important;
        margin: 0 !important;
        width: 100% !important;
        font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
        font-size: 9pt !important;
        line-height: 1.3 !important;
    }

    /* 3. HIDE ALL NON-PRINT UI ELEMENTS & ISOLATE PREVIEW SHEET */
    body > :not(#stylishPrintPreviewModal):not(script):not(style),
    main,
    .no-print,
    header,
    footer,
    nav,
    aside,
    #toastNotification,
    #solvingLoadingOverlay,
    #cellEditorModal,
    #tableBottomExportButtons,
    #emptyTimetableState,
    .no-print-element,
    .print-preview-toolbar-box,
    button:not(.allow-print) {
        display: none !important;
    }

    /* 4. MODAL PRINT CONTAINER: MAKE VISIBLE TOP-LEVEL DOCUMENT */
    #stylishPrintPreviewModal.hidden {
        display: none !important;
    }

    #stylishPrintPreviewModal:not(.hidden) {
        position: static !important;
        background: #ffffff !important;
        padding: 0 !important;
        margin: 0 !important;
        display: block !important;
        backdrop-filter: none !important;
        overflow: visible !important;
        z-index: 99999 !important;
        inset: auto !important;
        width: 100% !important;
        max-width: 100% !important;
        box-shadow: none !important;
    }

    #printPreviewZoomWrapper {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        transform: none !important;
    }

    .a4-page-sheet {
        page-break-after: always !important;
        break-after: page !important;
        page-break-before: auto !important;
        break-before: auto !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
        border: none !important;
        border-radius: 0 !important;
        display: block !important;
        background: transparent !important;
    }

    .a4-page-sheet:last-child {
        page-break-after: auto !important;
        break-after: auto !important;
    }

    .a4-page-sheet > div {
        display: block !important;
        height: auto !important;
        width: 100% !important;
    }

    .letterhead-header {
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
    }

    /* 5. MAIN TABLE CONTAINER: 100% WIDTH */
    #previewTableContainer {
        width: 100% !important;
        max-width: 100% !important;
        overflow: visible !important;
        margin: 0 !important;
        padding: 0 !important;
        display: block !important;
        zoom: var(--print-scale-zoom, 1) !important;
    }

    /* Force columns to fit within a single page width */
    table, 
    #printDocRenderedTable,
    .printDocRenderedTable,
    .print-table {
        width: 100% !important;
        max-width: 100% !important;
        table-layout: fixed !important;
        border-collapse: collapse !important;
        margin: 0 !important;
        page-break-after: auto !important;
        height: auto !important;
    }

    thead {
        display: table-header-group !important; /* Repeats table header across pages */
    }

    thead tr {
        background-color: #0f172a !important;
        color: #ffffff !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        page-break-after: avoid !important;
        break-after: avoid !important;
    }

    tbody {
        display: table-row-group !important;
        height: auto !important;
    }

    .page-break-after-page {
        page-break-after: always !important;
        break-after: page !important;
    }

    .portrait-sheet #printDocRenderedTable tbody tr,
    .portrait-sheet .printDocRenderedTable tbody tr,
    .print-orientation-portrait #printDocRenderedTable tbody tr,
    .print-orientation-portrait .printDocRenderedTable tbody tr,
    table.teacher-wise-table tbody tr {
        height: calc(240mm / var(--rows-per-page, 15)) !important;
        min-height: calc(240mm / var(--rows-per-page, 15)) !important;
        max-height: calc(240mm / var(--rows-per-page, 15)) !important;
    }

    .landscape-sheet #printDocRenderedTable tbody tr,
    .landscape-sheet .printDocRenderedTable tbody tr,
    .print-orientation-landscape #printDocRenderedTable tbody tr,
    .print-orientation-landscape .printDocRenderedTable tbody tr,
    table.class-wise-table tbody tr {
        height: calc(160mm / var(--rows-per-page, 15)) !important;
        min-height: calc(160mm / var(--rows-per-page, 15)) !important;
        max-height: calc(160mm / var(--rows-per-page, 15)) !important;
    }

    tfoot {
        display: table-footer-group !important;
    }

    /* STRICT ROW BREAK RULE: avoid cutting across rows & dynamic full-page height distribution */
    tr {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        min-height: var(--row-min-height, 36px) !important;
    }

    tbody tr:last-child {
        page-break-after: auto !important;
        break-after: auto !important;
    }

    /* Teacher-wise portrait print mode optimization to fill the full A4 page height */
    .portrait-sheet #printDocRenderedTable td,
    .portrait-sheet #printDocRenderedTable th,
    .print-orientation-portrait #printDocRenderedTable td,
    .print-orientation-portrait #printDocRenderedTable th,
    .teacher-wise-table td,
    .teacher-wise-table th {
        padding: 6px 4px !important;
    }
    .portrait-sheet #printDocRenderedTable td.teacher-column,
    .print-orientation-portrait #printDocRenderedTable td.teacher-column,
    .teacher-wise-table td.teacher-column {
        padding: 6px 8px !important;
    }
    .portrait-sheet #printDocRenderedTable .slot-card,
    .print-orientation-portrait #printDocRenderedTable .slot-card,
    .teacher-wise-table .slot-card {
        padding: 4px 3px !important;
    }

    th, td {
        vertical-align: middle !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }

    /* Stricter column width constraint specifically for 'Class' / 'Grade' column (minimized in class mode to maximize period columns width) */
    .grade-column,
    .class-column {
        width: 3% !important;
        max-width: 34px !important;
        min-width: 20px !important;
        padding: 2px 1px !important;
        text-align: center !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    /* Teacher / Faculty column in Teacher Wise mode - comfortable width and enhanced font size for Portrait */
    .teacher-column {
        width: 18% !important;
        max-width: 150px !important;
        min-width: 90px !important;
        padding: 3px 5px !important;
        text-align: left !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    .teacher-column span:first-child {
        font-size: 11pt !important;
        font-weight: 900 !important;
        color: #020617 !important;
        line-height: 1.25 !important;
    }

    .teacher-column span:last-child {
        font-size: 7.5pt !important;
        font-weight: 700 !important;
        color: #64748b !important;
    }

    #printDocRenderedTable th {
        border: 1px solid #334155 !important;
        padding: 4px 2px !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }

    #printDocRenderedTable td {
        border: 1px solid #cbd5e1 !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        vertical-align: middle !important;
        padding: 2px !important;
        min-height: var(--row-min-height, 36px) !important;
        height: inherit !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }

    /* 6. PRESERVE BEAUTIFUL CARD BADGES & FORMATTING EXACTLY AS IN PREVIEW MODAL */
    .slot-card {
        border-radius: 6px !important;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05) !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        text-align: center !important;
        width: 100% !important;
        height: 100% !important;
        min-height: var(--card-min-height, 28px) !important;
        box-sizing: border-box !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }

    .slot-card * {
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
        max-width: 100% !important;
    }

    .letterhead-header {
        border-bottom: 1.5px solid #0f172a !important;
        padding-bottom: 4px !important;
        margin-bottom: 6px !important;
        text-align: center !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }

    .letterhead-header h1 {
        font-size: 15pt !important;
        font-weight: 900 !important;
        color: #0f172a !important;
        margin: 0 !important;
        padding: 0 !important;
        text-transform: uppercase !important;
    }

    /* EXPLICITLY REMOVE PRINCIPAL SIGNATURE LINES & REDUNDANT FOOTERS */
    .letterhead-footer,
    .principal-signature,
    .signature-line,
    .signature-section,
    .signature-box,
    .extra-print-header,
    .extra-print-footer {
        display: none !important;
    }
}
