/**
 * Gallant ERP — production browser print engine (A4 transactional documents).
 * Quotation, Sales/Purchase Invoice, Delivery Note, Credit/Debit Note, Payment/Receipt vouchers.
 */

@media print {
    /*
     * Continuation pages start with the items table flush against the paper edge,
     * so pages 2+ get a deeper top margin. Page 1 keeps 12mm for the letterhead.
     *
     * The repeating page footer prints at the bottom of the content area, not in the margin, so
     * the bottom margin only has to clear the printer's dead zone — the space the footer needs
     * is reserved inside the flow instead (mountFooterReserve in erpPrintEngine.ts).
     */
    @page erp-a4 {
        size: A4 portrait;
        margin: 20mm 12mm 14mm 12mm;
    }

    @page {
        size: A4 portrait;
        margin: 20mm 12mm 14mm 12mm;
    }

    @page voucher-a4 {
        size: A4 portrait;
        margin: 20mm 12mm 14mm 12mm;
    }

    @page :first {
        margin-top: 12mm;
    }

    @page erp-a4:first {
        margin-top: 12mm;
    }

    @page voucher-a4:first {
        margin-top: 12mm;
    }

    /* `html` prefix keeps this ahead of the POS layout's receipt page rule. */
    html.erp-print-active,
    html body.erp-print-active,
    html body.invoice-details-print,
    html body.voucher-print-mode {
        page: voucher-a4;
    }

    /* Root printable document */
    .erp-print-root,
    .erp-print-root.inv-design-print,
    .erp-print-root.gd1-root,
    .erp-print-root.gd2-root,
    .erp-print-root.gd3-root,
    .erp-print-root.voucher-print-root {
        display: block !important;
        width: 100% !important;
        max-width: none !important;
        min-height: 0 !important;
        height: auto !important;
        overflow: visible !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .erp-print-root .inv-sheet,
    .erp-print-root .gd1-sheet,
    .erp-print-root .gd2-sheet,
    .erp-print-root .gd3-sheet,
    .erp-print-root .voucher-print-sheet {
        display: block !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        box-sizing: border-box !important;
    }

    /* First page: full header block */
    .erp-print-first-page-header {
        break-after: avoid-page;
        page-break-after: avoid;
    }

    /* Compact header — page 2+ only (never on short / single-page invoices) */
    .erp-print-compact-header-row {
        display: none !important;
        visibility: collapse !important;
        height: 0 !important;
        line-height: 0 !important;
    }

    .erp-print-root.erp-print-multi-page .erp-print-compact-header-row:not(.erp-hide-compact-header) {
        display: table-row !important;
        visibility: visible !important;
        height: auto !important;
        line-height: normal !important;
    }

    .erp-print-compact-header-row.erp-hide-compact-header {
        display: none !important;
        visibility: collapse !important;
        height: 0 !important;
        line-height: 0 !important;
    }

    .erp-compact-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        padding: 6mm 0 8px;
        border-bottom: 1px solid #cbd5e1;
        font-size: 8.5pt;
        color: #334155;
        background: #fff;
    }

    /* Continuation pages: thead repeats — add top gap on page 2+ */
    .erp-print-root.erp-print-multi-page .inv-table thead th,
    .erp-print-root.erp-print-multi-page .gd1-table thead th,
    .erp-print-root.erp-print-multi-page .gd2-table thead th,
    .erp-print-root.erp-print-multi-page .gd3-table thead th,
    .erp-print-root.erp-print-multi-page .erp-print-items-table thead th {
        padding-top: 4mm !important;
    }

    .erp-compact-header__left {
        font-weight: 700;
        color: #0f172a;
        max-width: 55%;
    }

    .erp-compact-header__center {
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--inv-accent, #1d4ed8);
    }

    .erp-compact-header__right {
        text-align: right;
        font-size: 8pt;
        color: #64748b;
    }

    /* Item tables — repeat column header every page */
    .erp-print-items-table,
    .erp-print-root .inv-table,
    .erp-print-root .gd1-table,
    .erp-print-root .gd2-table,
    .erp-print-root .gd3-table {
        width: 100% !important;
        border-collapse: collapse;
        page-break-inside: auto;
        break-inside: auto;
    }

    .erp-print-items-table thead,
    .erp-print-root .inv-table thead,
    .erp-print-root .gd1-table thead,
    .erp-print-root .gd2-table thead,
    .erp-print-root .gd3-table thead {
        display: table-header-group;
    }

    .erp-print-items-table tbody,
    .erp-print-root .inv-table tbody,
    .erp-print-root .gd1-table tbody,
    .erp-print-root .gd2-table tbody,
    .erp-print-root .gd3-table tbody {
        display: table-row-group;
    }

    .erp-print-items-table tr,
    .erp-print-root .inv-table tbody tr,
    .erp-print-root .gd1-table tbody tr,
    .erp-print-root .gd2-table tbody tr,
    .erp-print-root .gd3-table tbody tr {
        break-inside: auto;
        page-break-inside: auto;
    }

    .erp-print-items-table td,
    .erp-print-items-table th,
    .erp-print-root .inv-table td,
    .erp-print-root .inv-table th {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* Totals, words, terms, signatures — keep together on last page flow */
    .erp-print-last-page-only {
        break-inside: avoid-page;
        page-break-inside: avoid;
    }

    .erp-print-last-page-only .inv-totals,
    .erp-print-last-page-only .inv-totals-box,
    .erp-print-last-page-only .inv-summary-wrap,
    .erp-print-last-page-only .gd1-totals-wrap,
    .erp-print-last-page-only .gd1-words-box,
    .erp-print-last-page-only .gd1-terms,
    .erp-print-last-page-only .gallant-print-footer,
    .erp-print-last-page-only .voucher-signatures {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* Prevent flex layouts from creating empty bands between pages */
    .erp-print-root .inv-top-grid,
    .erp-print-root .inv-mid,
    .erp-print-root .inv-parties-block {
        display: block !important;
    }

    /* Page number footer — --inv-page-label carries the template's page number format */
    .erp-print-page-label::before {
        content: var(--inv-page-label, 'Page ' counter(page) ' of ' counter(pages));
    }

    .inv-page-footer__pageno .erp-print-page-label {
        font-size: 8pt;
        color: #64748b;
    }

    .inv-page-footer__pageno .erp-print-page-label:empty::before {
        content: var(--inv-page-label, 'Page ' counter(page) ' of ' counter(pages));
    }

    .erp-print-page-label--page-only:empty::before {
        content: counter(page);
    }

    /* Fixed document footer on every printed page (inside #printable-form) */
    .erp-print-footer-inline-hidden {
        display: none !important;
        visibility: hidden !important;
    }

    #erp-print-fixed-footer-host:not(.erp-print-footer-inline-hidden),
    .erp-print-fixed-footer-host:not(.erp-print-footer-inline-hidden) {
        display: block !important;
        visibility: visible !important;
    }

    #erp-print-fixed-footer-host,
    .erp-print-fixed-footer-host {
        position: static !important;
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        z-index: 2147483647 !important;
        box-sizing: border-box !important;
        visibility: visible !important;
        display: block !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    #erp-print-fixed-footer-host > .erp-print-footer-clone,
    #erp-print-fixed-footer-host > .inv-page-footer,
    #erp-print-fixed-footer-host > .erp-print-document-footer,
    .erp-print-fixed-footer-host > .erp-print-footer-clone,
    .erp-print-fixed-footer-host > .inv-page-footer,
    .erp-print-fixed-footer-host > .erp-print-document-footer {
        position: fixed !important;
        bottom: 12mm !important;
        left: 12mm !important;
        right: 12mm !important;
        width: auto !important;
        margin: 0 !important;
        padding: 6px 0 4mm !important;
        border-top: 1px solid #cbd5e1 !important;
        background: #fff !important;
        z-index: 2147483647 !important;
        box-sizing: border-box !important;
        visibility: visible !important;
        display: block !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    #erp-print-fixed-footer-host *,
    .erp-print-fixed-footer-host * {
        visibility: visible !important;
    }

    #erp-print-fixed-footer-host .erp-print-page-label-screen,
    .erp-print-fixed-footer-host .erp-print-page-label-screen {
        display: none !important;
    }

    #erp-print-fixed-footer-host .erp-print-page-label,
    .erp-print-fixed-footer-host .erp-print-page-label {
        display: inline-block !important;
        visibility: visible !important;
    }

    #printable-form > .erp-print-document-footer.inv-page-footer--fixed-print,
    .erp-print-root > .erp-print-document-footer.inv-page-footer--fixed-print {
        visibility: visible !important;
        display: block !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    body.erp-print-active .inv-page-footer--fixed-print,
    body.erp-print-active .erp-print-document-footer.inv-page-footer--fixed-print,
    body.invoice-details-print .inv-page-footer--fixed-print,
    body.invoice-details-print .erp-print-document-footer.inv-page-footer--fixed-print,
    body.voucher-print-mode .inv-page-footer--fixed-print,
    body.voucher-print-mode .erp-print-document-footer.inv-page-footer--fixed-print,
    .erp-print-root > .erp-print-document-footer.inv-page-footer--fixed-print,
    #printable-form > .erp-print-document-footer.inv-page-footer--fixed-print,
    .gd1-sheet > .inv-page-footer--fixed-print,
    .gd2-sheet > .inv-page-footer--fixed-print,
    .gd3-sheet > .inv-page-footer--fixed-print,
    .inv-sheet > .inv-page-footer--fixed-print {
        position: fixed !important;
        bottom: 12mm !important;
        left: 12mm !important;
        right: 12mm !important;
        width: auto !important;
        margin: 0 !important;
        padding: 6px 0 4mm !important;
        border-top: 1px solid #cbd5e1 !important;
        background: #fff !important;
        z-index: 9999 !important;
        box-sizing: border-box !important;
    }

    body.erp-print-active .gd1-sheet,
    body.erp-print-active .gd2-sheet,
    body.erp-print-active .gd3-sheet,
    body.invoice-details-print .inv-sheet,
    body.invoice-details-print .gd1-sheet,
    body.invoice-details-print .gd2-sheet,
    body.invoice-details-print .gd3-sheet {
        padding-bottom: var(--inv-footer-reserve, 36px) !important;
    }

    /* Voucher shell */
    .voucher-print-root .voucher-print-body {
        break-inside: avoid;
        page-break-inside: avoid;
    }
}

/* Screen preview should mirror print layout width */
@media screen {
    .erp-print-root .inv-sheet,
    .erp-print-root .gd1-sheet,
    .erp-print-root .gd2-sheet,
    .erp-print-root .gd3-sheet {
        max-width: 210mm;
        margin: 0 auto;
    }

    .erp-print-compact-header-row {
        display: none;
    }
}
