/* =============================================
 * GHOLAMI - Print Styles
 * Optimisé pour A4 Portrait avec pagination
 * ============================================= */

@media print {
    /* ===== PAGE SETUP ===== */
    @page {
        size: A4 portrait;
        margin: 12mm 10mm 12mm 10mm;
    }
    
    /* ===== COLOR PRESERVATION ===== */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
    
    body {
        background: white !important;
        color: #333 !important;
        font-size: 10px !important;
    }
    
    /* ===== HIDE NON-PRINT ELEMENTS ===== */
    .sidebar, .burger-btn, .sidebar-overlay,
    .page-header, .page-actions, .stats, .charts,
    #app, .nav-item, .user-section, .stock-box,
    #appFooter, #scrollTopBtn, #toastContainer,
    .no-print, .doc-footer, .smart-filter,
    .chat-input-area, .btn { 
        display: none !important; 
    }
    
    /* ===== SHOW ACTIVE MODAL ===== */
    .modal.active {
        display: flex !important;
        position: fixed !important;
        top: 0; left: 0; right: 0; bottom: 0;
        background: white !important;
        z-index: 99999 !important;
    }
    
    .modal-box {
        max-width: 100% !important;
        max-height: 100% !important;
        width: 100% !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
        background: white !important;
        box-shadow: none !important;
        border-radius: 0 !important;
        overflow: visible !important;
    }
    
    .modal-header { display: none !important; }
    
    /* ===== DOCUMENT VIEW ===== */
    .doc-view {
        box-shadow: none !important;
        background: white !important;
        padding: 0 !important;
        margin: 0 auto !important;
        max-width: 100% !important;
        width: 100% !important;
        page-break-after: auto;
        position: relative !important;
    }
    
    .doc-header {
        page-break-after: avoid;
        page-break-inside: avoid;
        margin: 0 !important;
        padding: 10px 15px !important;
    }
    
    .doc-header-info h3 {
        font-size: 13px !important;
        margin-bottom: 3px !important;
    }
    
    .doc-header-info p {
        font-size: 9px !important;
        margin: 2px 0 !important;
        line-height: 1.3 !important;
    }
    
    .doc-number {
        font-size: 11px !important;
        padding: 4px 8px !important;
    }
    
    .doc-body {
        page-break-after: auto;
        padding: 0 15px !important;
        margin: 0 !important;
    }
    
    .doc-meta {
        page-break-after: avoid;
        page-break-inside: avoid;
        margin-bottom: 10px !important;
    }
    
    .doc-meta-item { padding: 4px !important; }
    .doc-meta-item label { font-size: 9px !important; }
    .doc-meta-item span { font-size: 10px !important; }
    
    /* ===== TABLES ===== */
    .doc-table {
        page-break-inside: auto !important;
    }
    
    table {
        border: 1px solid #ccc !important;
        page-break-inside: auto !important;
        border-collapse: collapse !important;
        width: 100% !important;
    }
    
    thead { display: table-header-group !important; }
    tbody { display: table-row-group !important; }
    
    tr {
        page-break-inside: avoid !important;
        page-break-after: auto !important;
    }
    
    .doc-table th {
        padding: 5px 6px !important;
        font-size: 9px !important;
        font-weight: 600 !important;
    }
    
    th, td {
        border: 1px solid #ccc !important;
        color: #333 !important;
        padding: 5px 8px !important;
        font-size: 9px !important;
    }
    
    /* Large tables auto-reduce */
    .doc-table table tbody tr { line-height: 1.3 !important; }
    
    .doc-table td {
        padding: 4px 6px !important;
        font-size: 8.5px !important;
    }
    
    /* ===== TOTALS ===== */
    .doc-total {
        page-break-inside: avoid;
        margin-top: 10px !important;
        padding-top: 8px !important;
    }
    
    .doc-total-row {
        font-size: 10px !important;
        padding: 4px 0 !important;
    }
    
    .doc-total-row.grand {
        font-size: 12px !important;
        padding: 8px 0 !important;
    }
    
    /* ===== SIGNATURES ===== */
    .doc-signatures {
        page-break-before: auto;
        page-break-inside: avoid !important;
        margin-top: 15px !important;
        padding-top: 10px !important;
    }
    
    .doc-signature { font-size: 9px !important; }
    
    .doc-custom-footer {
        page-break-inside: avoid !important;
    }
    
    /* ===== BADGES IN PRINT ===== */
    .badge {
        padding: 2px 6px !important;
        font-size: 8px !important;
    }
    
    /* ===== LARGE TABLE OPTIMIZATION ===== */
    /* Tables with >15 rows get smaller font */
    tbody:has(tr:nth-child(15)) tr {
        font-size: 8.5px !important;
        line-height: 1.2 !important;
    }
    
    tbody:has(tr:nth-child(15)) ~ * th,
    tbody:has(tr:nth-child(15)) ~ * td {
        font-size: 8.5px !important;
        padding: 3px 5px !important;
    }
}

/* ===== LANDSCAPE PRINT ===== */
@media print and (orientation: landscape) {
    @page { size: A4 landscape; }
    
    th, td { font-size: 8px !important; padding: 4px 5px !important; }
    .doc-header-info h3 { font-size: 12px !important; }
}
