/* Sales Quotation + Sales Invoice browser print — load last to override zeroed margins/padding */
@media print {
    /*
     * Deeper top margin from page 2 on; page 1 stays at 12mm for the letterhead.
     * The repeating page footer prints at the bottom of the content area, so the bottom margin
     * only clears the printer dead zone; footer space is reserved inside the flow instead.
     */
    @page {
        size: A4 portrait;
        margin-top: 20mm;
        margin-right: 12mm;
        margin-bottom: 14mm;
        margin-left: 12mm;
    }

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

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

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

    /*
     * Other print CSS sets padding: 0 !important on paper/templates.
     * Restore a clear content inset so page 1 is not edge-flush.
     * Page 2+ relies on @page margins (padding does not repeat across pages).
     */
    body.invoice-details-print .template-default,
    body.voucher-print-mode .template-default {
        padding: 28px !important;
        box-sizing: border-box !important;
    }

    body.invoice-details-print .template-zoho,
    body.voucher-print-mode .template-zoho,
    body.invoice-details-print .template-tally,
    body.voucher-print-mode .template-tally,
    body.invoice-details-print .template-taxinvoice,
    body.voucher-print-mode .template-taxinvoice {
        padding: 20px !important;
        box-sizing: border-box !important;
    }

    body.invoice-details-print .template-erp-a4,
    body.voucher-print-mode .template-erp-a4,
    body.invoice-details-print .template-invox,
    body.voucher-print-mode .template-invox {
        padding: 0.45in 0.4in !important;
        box-sizing: border-box !important;
    }

    /* Invoice puts template class on #printable-form AND a child — pad only the inner template */
    body.invoice-details-print .invoice-paper.template-erp-a4,
    body.voucher-print-mode .invoice-paper.template-erp-a4,
    body.invoice-details-print .invoice-paper.template-invox,
    body.voucher-print-mode .invoice-paper.template-invox,
    body.invoice-details-print .invoice-paper.template-default,
    body.voucher-print-mode .invoice-paper.template-default,
    body.invoice-details-print .invoice-paper.template-zoho,
    body.voucher-print-mode .invoice-paper.template-zoho,
    body.invoice-details-print .invoice-paper.template-tally,
    body.voucher-print-mode .invoice-paper.template-tally,
    body.invoice-details-print .invoice-paper.template-taxinvoice,
    body.voucher-print-mode .invoice-paper.template-taxinvoice {
        padding: 0 !important;
    }

    /* Quotation: paper has no template class */
    body.invoice-details-print #printable-form.invoice-paper:not([class*="template-"]),
    body.voucher-print-mode #printable-form.invoice-paper:not([class*="template-"]) {
        padding: 0 !important;
    }

    /*
     * Kill screen A4 "full page" flex layout during print.
     * min-height: 1123px + flex + margin-top:auto on footer creates a huge
     * empty band between totals and signature when content spans pages.
     */
    body.invoice-details-print .invoice-paper,
    body.voucher-print-mode .invoice-paper,
    body.invoice-details-print .template-erp-a4,
    body.voucher-print-mode .template-erp-a4,
    body.invoice-details-print .template-invox,
    body.voucher-print-mode .template-invox,
    body.invoice-details-print .template-default,
    body.voucher-print-mode .template-default,
    body.invoice-details-print .template-zoho,
    body.voucher-print-mode .template-zoho,
    body.invoice-details-print .template-tally,
    body.voucher-print-mode .template-tally,
    body.invoice-details-print .template-taxinvoice,
    body.voucher-print-mode .template-taxinvoice {
        display: block !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
    }

    body.invoice-details-print .erp-a4-body,
    body.voucher-print-mode .erp-a4-body,
    body.invoice-details-print .invox-main,
    body.voucher-print-mode .invox-main,
    body.invoice-details-print .invox-body,
    body.voucher-print-mode .invox-body {
        display: block !important;
        flex: none !important;
        min-height: 0 !important;
        height: auto !important;
    }

    body.invoice-details-print .erp-a4-footer,
    body.voucher-print-mode .erp-a4-footer,
    body.invoice-details-print .invox-footer,
    body.voucher-print-mode .invox-footer,
    body.invoice-details-print .default-footer,
    body.voucher-print-mode .default-footer,
    body.invoice-details-print .tally-footer,
    body.voucher-print-mode .tally-footer {
        margin-top: 16px !important;
        flex: none !important;
    }

    /* Thermal: keep compact — no A4 content padding */
    body.invoice-details-print .thermal-invoice,
    body.voucher-print-mode .thermal-invoice {
        padding: 8px !important;
        margin: 0 auto !important;
        min-height: 0 !important;
        height: auto !important;
    }

    /*
     * Gallant prebuilt invoice designs (gallant-design-1/2/3)
     * Long item descriptions must paginate without hiding totals/footer.
     */
    body.invoice-details-print .gd1-root,
    body.invoice-details-print .gd2-root,
    body.invoice-details-print .gd3-root,
    body.invoice-details-print .gd1-sheet,
    body.invoice-details-print .gd2-sheet,
    body.invoice-details-print .gd3-sheet,
    body.voucher-print-mode .gd1-root,
    body.voucher-print-mode .gd2-root,
    body.voucher-print-mode .gd3-root,
    body.voucher-print-mode .gd1-sheet,
    body.voucher-print-mode .gd2-sheet,
    body.voucher-print-mode .gd3-sheet {
        display: block !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    body.invoice-details-print .gd1-table,
    body.invoice-details-print .gd2-table,
    body.invoice-details-print .gd3-table,
    body.voucher-print-mode .gd1-table,
    body.voucher-print-mode .gd2-table,
    body.voucher-print-mode .gd3-table {
        width: 100% !important;
        page-break-inside: auto;
    }

    body.invoice-details-print .gd1-table thead,
    body.invoice-details-print .gd2-table thead,
    body.invoice-details-print .gd3-table thead,
    body.voucher-print-mode .gd1-table thead,
    body.voucher-print-mode .gd2-table thead,
    body.voucher-print-mode .gd3-table thead {
        display: table-header-group;
    }

    body.invoice-details-print .gd1-table tbody tr,
    body.invoice-details-print .gd2-table tbody tr,
    body.invoice-details-print .gd3-table tbody tr,
    body.invoice-details-print .gd1-table tbody td,
    body.invoice-details-print .gd2-table tbody td,
    body.invoice-details-print .gd3-table tbody td,
    body.voucher-print-mode .gd1-table tbody tr,
    body.voucher-print-mode .gd2-table tbody tr,
    body.voucher-print-mode .gd3-table tbody tr,
    body.voucher-print-mode .gd1-table tbody td,
    body.voucher-print-mode .gd2-table tbody td,
    body.voucher-print-mode .gd3-table tbody td {
        page-break-inside: auto !important;
        break-inside: auto !important;
    }

    body.invoice-details-print .gd1-item-name,
    body.invoice-details-print .gd2-prod-name,
    body.invoice-details-print .gd3-item-name,
    body.voucher-print-mode .gd1-item-name,
    body.voucher-print-mode .gd2-prod-name,
    body.voucher-print-mode .gd3-item-name {
        page-break-after: avoid;
        break-after: avoid-page;
    }

    body.invoice-details-print .gd1-item-desc,
    body.invoice-details-print .gd2-prod-desc,
    body.invoice-details-print .gd3-item-desc,
    body.voucher-print-mode .gd1-item-desc,
    body.voucher-print-mode .gd2-prod-desc,
    body.voucher-print-mode .gd3-item-desc {
        display: block !important;
        margin-top: 4px !important;
        white-space: pre-wrap !important;
        overflow-wrap: anywhere !important;
        word-break: break-word !important;
    }

    body.invoice-details-print .gallant-print-footer,
    body.voucher-print-mode .gallant-print-footer {
        page-break-inside: avoid;
        break-inside: avoid-page;
    }

    body.invoice-details-print .gd1-table thead th,
    body.invoice-details-print .gd2-table thead th,
    body.invoice-details-print .gd3-table thead th,
    body.voucher-print-mode .gd1-table thead th,
    body.voucher-print-mode .gd2-table thead th,
    body.voucher-print-mode .gd3-table thead th,
    body.invoice-details-print .inv-table thead th,
    body.voucher-print-mode .inv-table thead th {
        padding-top: 4mm !important;
    }

    body.invoice-details-print .gd1-table thead tr,
    body.invoice-details-print .gd3-table thead tr,
    body.voucher-print-mode .gd1-table thead tr,
    body.voucher-print-mode .gd3-table thead tr {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
}
