/* ===== Base Print Styles ===== */
@media print {
    /* Reset everything for printing */
    body, html {
        margin: 0 !important;
        padding: 0 !important;
        height: auto !important;
        width: 100% !important;
        background: white !important;
        font-family: Arial, sans-serif !important;
    }

        /* Hide everything except print area */
        body * {
            visibility: hidden;
        }

    .print-area, .print-area *,
    #printable-form, #printable-form * {
        visibility: visible;
    }

    /* Force print area to take full width */
    .print-area,
    #printable-form {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        margin: 0 !important;
        page-break-before: avoid !important;
        page-break-after: avoid !important;
    }

    .print-area {
        top: -40px !important;
    }

    .print-area.invoice-preview-container,
    #printable-form {
        top: 0 !important;
    }

    /* Hide app chrome when printing invoices/vouchers */
    .sidebar, .header, #sidebar, .invoice-toolbar, .navbar,
    .sidebar-contact, .sidebar-themesettings, .sidebar-themeoverlay,
    .modal, .toast, .offcanvas, #blazor-error-ui {
        display: none !important;
    }

    .page-wrapper {
        margin: 0 !important;
        padding: 0 !important;
        margin-left: 0 !important;
        padding-top: 0 !important;
    }

    /* Invoice / voucher — preserve screen layout (do not shrink all images) */
    #printable-form img,
    .print-area img,
    .invoice-paper img {
        max-width: none !important;
        height: auto !important;
    }

    #printable-form .invox-logo,
    .invoice-paper .invox-logo {
        width: 140px !important;
        max-width: 140px !important;
        max-height: 82px !important;
        object-fit: contain !important;
    }

    /* Print color adjustments */
    * {
        -webkit-print-color-adjust: exact !important;
        color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    /* Remove all decorations */
    .card, .shadow-sm, .border-0, .rounded {
        box-shadow: none !important;
        border: none !important;
        border-radius: 0 !important;
        background: transparent !important;
    }

    /* Hide non-essential elements */
    .card-header, .no-print {
        display: none !important;
    }
}

/* ===== Paper Size Definitions ===== */
/* A4 (210 × 297 mm) with 10mm margins */
.paper-a4 {
    width: 190mm; /* 210 - 20mm */
    min-height: 277mm; /* 297 - 20mm */
    margin: 10mm auto;
    padding: 0;
    background: white;
}

/* A5 (148 × 210 mm) with 5mm margins */
.paper-a5 {
    width: 138mm; /* 148 - 10mm */
    min-height: 200mm; /* 210 - 10mm */
    margin: 5mm auto;
    padding: 0;
    background: white;
}

/* Thermal 2 inch (58mm) */
.paper-2inch {
    width: 56mm; /* 58 - 2mm */
    min-height: auto;
    margin: 1mm auto;
    padding: 0;
    background: white;
}

/* Thermal 3 inch (80mm) */
.paper-3inch {
    width: 78mm; /* 80 - 2mm */
    min-height: auto;
    margin: 1mm auto;
    padding: 0;
    background: white;
}

/* Thermal 4 inch (110mm) */
.paper-4inch {
    width: 108mm; /* 110 - 2mm */
    min-height: auto;
    margin: 1mm auto;
    padding: 0;
    background: white;
}

/* ===== Print-specific Paper Sizes ===== */
@media print {
    @page {
        size: auto;
        margin: 0mm;
        marks: none;
    }

    .paper-a4 {
        width: 190mm !important;
        min-height: 277mm !important;
        margin: 10mm auto !important;
        padding: 0 !important;
    }

    .paper-a5 {
        width: 138mm !important;
        min-height: 200mm !important;
        margin: 5mm auto !important;
        padding: 0 !important;
    }

    .paper-2inch {
        width: 56mm !important;
        min-height: auto !important;
        margin: 1mm auto !important;
        padding: 0 !important;
    }

    .paper-3inch {
        width: 78mm !important;
        min-height: auto !important;
        margin: 1mm auto !important;
        padding: 0 !important;
    }

    .paper-4inch {
        width: 108mm !important;
        min-height: auto !important;
        margin: 1mm auto !important;
        padding: 0 !important;
    }
}

/* ===== Thermal Printer Styles ===== */
.thermal-invoice {
    font-family: monospace !important;
    font-size: 10px !important;
    line-height: 1.2;
    width: 100%;
}

    .thermal-invoice table {
        width: 100% !important;
        border-collapse: collapse !important;
        font-size: 9px !important;
    }

    .thermal-invoice th,
    .thermal-invoice td {
        padding: 2px !important;
        border: none !important;
        border-bottom: 1px dashed #ccc !important;
    }

    .thermal-invoice .border-bottom {
        border-bottom: 1px dashed #000 !important;
    }

/* ===== Regular Invoice Styles ===== */
.regular-invoice {
    font-family: Arial, sans-serif !important;
}

    /* Ensure tables maintain their layout */
    .regular-invoice table {
        width: 100% !important;
        border-collapse: collapse !important;
    }

    .regular-invoice th,
    .regular-invoice td {
        padding: 5px !important;
    }

    /* Maintain borders in print */
    .regular-invoice .border-top {
        border-top: 1px solid #dee2e6 !important;
    }

    .regular-invoice .border-bottom {
        border-bottom: 1px solid #dee2e6 !important;
    }

/* ===== Print Utility Classes ===== */
.print-only {
    display: none;
}

@media print {
    .d-flex {
        display: none !important;
    }

    .col-lg-4 {
        display: none !important;
    }

    .print-only {
        display: block !important;
    }

    .no-print {
        display: none !important;
    }

    /* Ensure images print properly (reports/small icons only — not invoice logos) */
    img:not(#printable-form img):not(.print-area img):not(.invoice-paper img) {
        max-width: 60px !important;
        height: 60px !important;
    }

    /* Invoice / voucher document logos (POS thermal excluded below) */
    #printable-form .default-logo img,
    #printable-form .zoho-logo img,
    #printable-form .tally-logo img,
    #printable-form .tax-logo-section img,
    #printable-form .thermal-logo img,
    .print-area .default-logo img,
    .print-area .zoho-logo img,
    .print-area .tally-logo img,
    .print-area .tax-logo-section img,
    .print-area .thermal-logo img,
    .invoice-paper .default-logo img,
    .invoice-paper .zoho-logo img,
    .invoice-paper .tally-logo img,
    .invoice-paper .tax-logo-section img,
    .invoice-paper .thermal-logo img,
    .jv-print-wrapper .print-logo,
    .uae-logo {
        width: 120px !important;
        height: 120px !important;
        max-width: 120px !important;
        max-height: 120px !important;
        object-fit: contain !important;
    }

    body.pos-print-match-preview .print-area img,
    body.pos-thermal-print-popup img,
    body.pos-thermal-printing .print-area img,
    body.pos-thermal-printing img,
    .pos-receipt img {
        max-width: 100% !important;
        height: auto !important;
    }

    /* Force background colors to print */
    .bg-white, .bg-light {
        background-color: white !important;
        -webkit-print-color-adjust: exact;
    }

    /* Maintain padding in print */
    .p-4 {
        padding: 1.5rem !important;
    }

    /* Maintain margins in print */
    .my-1 {
        margin-top: 0.25rem !important;
        margin-bottom: 0.25rem !important;
    }

    /* Fix for Java print service */
    br {
        line-height: 1 !important;
        height: auto !important;
    }

    /* Ensure proper alignment */
    .text-right {
        text-align: right !important;
    }

    .text-center {
        text-align: center !important;
    }

    /* Maintain font weights */
    .font-weight-bold {
        font-weight: bold !important;
    }
}

/* ===== Force visibility of all elements in print area ===== */
@media print {
    .print-area * {
        visibility: visible !important;
    }

    .print-area table * {
        visibility: visible !important;
    }

    .print-area tr,
    .print-area td,
    .print-area th {
        visibility: visible !important;
    }
}

/* ===== Ensure proper spacing in thermal print ===== */
@media print {
    .thermal-invoice tr {
        page-break-inside: avoid !important;
    }

    .thermal-invoice td {
        padding: 1px 0 !important;
    }
}

/* ===== Fix for dashed borders in print ===== */
@media print {
    .thermal-invoice tr {
        -webkit-print-color-adjust: exact;
    }

    .thermal-invoice td {
        -webkit-print-color-adjust: exact;
    }
}

/* ===== POS thermal receipt (80mm / 58mm) — browser print clarity ===== */
@media print {
    html:has(body.pos-thermal-printing),
    html:has(body.thermal-print-80),
    html:has(.thermal-print-80),
    html:has(.pos-rcpt-modal-overlay.thermal-print-80),
    html:has(.pos-modal-overlay.thermal-print-80) {
        width: 80mm !important;
        max-width: 80mm !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    html:has(body.pos-thermal-printing.thermal-print-58),
    html:has(body.thermal-print-58),
    html:has(.thermal-print-58),
    html:has(.pos-rcpt-modal-overlay.thermal-print-58),
    html:has(.pos-modal-overlay.thermal-print-58) {
        width: 58mm !important;
        max-width: 58mm !important;
    }

    body.pos-thermal-printing,
    body.pos-print-match-preview,
    body:has(.thermal-print-80),
    body:has(.pos-rcpt-modal-overlay.thermal-print-80),
    body:has(.pos-modal-overlay.thermal-print-80) {
        width: 80mm !important;
        max-width: 80mm !important;
        font-family: "Segoe UI", Arial, sans-serif !important;
    }

    body.pos-thermal-printing,
    body:has(.thermal-print-80),
    body:has(.pos-rcpt-modal-overlay.thermal-print-80),
    body:has(.pos-modal-overlay.thermal-print-80) {
        -webkit-font-smoothing: none !important;
        text-rendering: optimizeSpeed !important;
    }

    body.pos-print-match-preview {
        -webkit-font-smoothing: auto !important;
        text-rendering: auto !important;
    }

    body.pos-thermal-printing.thermal-print-58,
    body.pos-thermal-printing:has(.thermal-print-58),
    body.pos-print-match-preview.thermal-print-58,
    body:has(.thermal-print-58),
    body:has(.pos-rcpt-modal-overlay.thermal-print-58),
    body:has(.pos-modal-overlay.thermal-print-58) {
        width: 58mm !important;
        max-width: 58mm !important;
    }

    html body.pos-thermal-printing .print-area,
    html body:has(.thermal-print-80) .print-area,
    html body:has(.thermal-print-80) .print-area,
    html body:has(.thermal-print-58) .print-area,
    body.pos-thermal-printing .print-area,
    body:has(.thermal-print-80) .print-area,
    body:has(.thermal-print-58) .print-area,
    body:has(.pos-rcpt-modal-overlay.thermal-print-80) .print-area,
    body:has(.pos-rcpt-modal-overlay.thermal-print-58) .print-area,
    body:has(.pos-modal-overlay.thermal-print-80) .print-area,
    body:has(.pos-modal-overlay.thermal-print-58) .print-area {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        width: 78mm !important;
        max-width: 78mm !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    html body.pos-thermal-printing.thermal-print-58 .print-area,
    html body.pos-thermal-printing:has(.thermal-print-58) .print-area,
    html body:has(.thermal-print-58) .print-area,
    body.pos-thermal-printing:has(.thermal-print-58) .print-area,
    body:has(.thermal-print-58) .print-area,
    body:has(.pos-rcpt-modal-overlay.thermal-print-58) .print-area,
    body:has(.pos-modal-overlay.thermal-print-58) .print-area {
        width: 56mm !important;
        max-width: 56mm !important;
    }

    body.pos-thermal-printing .pos-receipt,
    body:has(.thermal-print-80) .pos-receipt,
    body:has(.thermal-print-58) .pos-receipt {
        font-family: "Segoe UI", Arial, sans-serif !important;
        color: #000 !important;
        -webkit-font-smoothing: none !important;
    }
}

@media print {
    body.pos-thermal-printing,
    body:has(.thermal-print-80),
    body:has(.pos-rcpt-modal-overlay.thermal-print-80),
    body:has(.pos-modal-overlay.thermal-print-80) {
        page: pos-thermal-80;
    }

    body.pos-thermal-printing:has(.thermal-print-58),
    body:has(.thermal-print-58),
    body:has(.pos-rcpt-modal-overlay.thermal-print-58),
    body:has(.pos-modal-overlay.thermal-print-58) {
        page: pos-thermal-58;
    }

    @page pos-thermal-80 {
        size: 80mm auto;
        margin: 0;
    }

    @page pos-thermal-58 {
        size: 58mm auto;
        margin: 0;
    }
}
