/**
 * Countivoo — 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,
    .erp-print-root .template-erp-a4,
    .erp-print-root .template-invox,
    .erp-print-root .invoice-paper {
        display: block !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        box-sizing: border-box !important;
        padding-bottom: 0 !important;
    }

    /* First page: full header block — anchor to page 1, never push header to next page */
    .erp-print-first-page-header {
        break-before: avoid !important;
        page-break-before: avoid !important;
        break-inside: avoid !important;
        page-break-inside: avoid !important;
        break-after: auto !important;
        page-break-after: auto !important;
    }

    /* 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 th,
    .erp-print-root .inv-table th,
    .erp-print-root .gd1-table th,
    .erp-print-root .gd2-table th,
    .erp-print-root .gd3-table th,
    .erp-print-root .erp-a4-items-table th,
    .erp-print-root .invox-table th {
        break-inside: avoid !important;
        page-break-inside: avoid !important;
    }

    .erp-print-items-table tbody td,
    .erp-print-root .inv-table tbody td,
    .erp-print-root .gd1-table tbody td,
    .erp-print-root .gd2-table tbody td,
    .erp-print-root .gd3-table tbody td,
    .erp-print-root .erp-a4-items-table tbody td,
    .erp-print-root .invox-table tbody td {
        break-inside: auto !important;
        page-break-inside: auto !important;
        vertical-align: top !important;
    }

    /* Long / multiline product descriptions must paginate cleanly across pages without jumping */
    .erp-print-items-table td .inv-pre,
    .erp-print-items-table td .gd1-item-desc,
    .erp-print-items-table td .gd2-prod-desc,
    .erp-print-items-table td .gd3-item-desc,
    .erp-print-items-table td .erp-a4-item-sub,
    .erp-print-items-table td .zoho-item-sub,
    .erp-print-items-table td .print-imei-line,
    .inv-table td .inv-pre,
    .inv-table td .inv-item-desc {
        break-inside: auto !important;
        page-break-inside: auto !important;
        white-space: pre-wrap !important;
        overflow-wrap: anywhere !important;
        word-break: break-word !important;
    }

    /* 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 .countivoo-print-footer,
    .erp-print-last-page-only .voucher-signatures {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* Allow terms & conditions and notes to flow naturally without premature page breaks */
    .gd3-terms-section,
    .gd3-notes-section,
    .ir-terms-section,
    .ir-notes-section,
    .gd1-terms,
    .gd2-terms,
    .inv-rich-content {
        break-inside: auto !important;
        page-break-inside: auto !important;
    }

    /* 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;
    }

    /* HTML Editor Output print fidelity */
    .inv-rich-content,
    .inv-rich-content * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
    .inv-rich-content ul {
        list-style-type: disc !important;
        margin: 2px 0 4px 18px !important;
        padding-left: 0 !important;
    }
    .inv-rich-content ol {
        list-style-type: decimal !important;
        margin: 2px 0 4px 18px !important;
        padding-left: 0 !important;
    }
    .inv-rich-content li {
        display: list-item !important;
        margin-bottom: 2px !important;
    }
    .inv-rich-content table {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }
    .inv-pre.inv-rich-content {
        white-space: normal !important;
    }
}

/* 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;
    }
}

/* HTML Editor / Rich text output styling for notes and terms & conditions across all templates */
.inv-rich-content {
    font-family: inherit;
    line-height: 1.45;
    color: inherit;
    word-break: normal;
    overflow-wrap: break-word;
}

.inv-rich-content p {
    margin: 0 0 4px 0;
}

.inv-rich-content p:last-child {
    margin-bottom: 0;
}

.inv-rich-content ul,
.inv-rich-content ol {
    margin: 2px 0 6px 18px;
    padding-left: 0;
}

.inv-rich-content ul {
    list-style-type: disc;
}

.inv-rich-content ol {
    list-style-type: decimal;
}

.inv-rich-content li {
    display: list-item;
    margin-bottom: 2px;
    line-height: 1.4;
}

.inv-rich-content strong,
.inv-rich-content b {
    font-weight: 700;
}

.inv-rich-content em,
.inv-rich-content i {
    font-style: italic;
}

.inv-rich-content u {
    text-decoration: underline;
}

.inv-rich-content s,
.inv-rich-content strike {
    text-decoration: line-through;
}

.inv-rich-content h1,
.inv-rich-content h2,
.inv-rich-content h3,
.inv-rich-content h4,
.inv-rich-content h5,
.inv-rich-content h6 {
    margin: 4px 0 3px;
    font-weight: 700;
    line-height: 1.25;
}

.inv-rich-content h1 { font-size: 1.2em; }
.inv-rich-content h2 { font-size: 1.1em; }
.inv-rich-content h3 { font-size: 1.0em; }

.inv-rich-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 4px 0 6px;
    font-size: 0.95em;
}

.inv-rich-content th,
.inv-rich-content td {
    border: 1px solid #cbd5e1;
    padding: 3px 6px;
    text-align: left;
}

.inv-rich-content th {
    background-color: #f1f5f9;
    font-weight: 600;
}

.inv-rich-content blockquote {
    margin: 4px 0;
    padding-left: 8px;
    border-left: 3px solid #cbd5e1;
    color: #475569;
}

.inv-rich-content font[size="1"], font[size="1"] { font-size: 10px !important; }
.inv-rich-content font[size="2"], font[size="2"] { font-size: 12px !important; }
.inv-rich-content font[size="3"], font[size="3"] { font-size: 14px !important; }
.inv-rich-content font[size="4"], font[size="4"] { font-size: 16px !important; }
.inv-rich-content font[size="5"], font[size="5"] { font-size: 20px !important; }
.inv-rich-content font[size="6"], font[size="6"] { font-size: 26px !important; }
.inv-rich-content font[size="7"], font[size="7"] { font-size: 32px !important; }

/* Enhanced font sizing for Sales Invoice view and browser print */
.invoice-page-container .erp-print-root,
.invoice-page-container .invoice-paper,
body.invoice-details-print .erp-print-root,
body.invoice-details-print .invoice-paper,
body.voucher-print-mode .invoice-paper {
    font-size: 13.5px;
}

.invoice-page-container .erp-print-items-table th,
.invoice-page-container .erp-print-items-table td,
body.invoice-details-print .erp-print-items-table th,
body.invoice-details-print .erp-print-items-table td,
body.voucher-print-mode .erp-print-items-table th,
body.voucher-print-mode .erp-print-items-table td {
    font-size: 9.5pt !important;
}

.invoice-page-container .erp-compact-header,
body.invoice-details-print .erp-compact-header,
body.voucher-print-mode .erp-compact-header {
    font-size: 9.5pt !important;
}



