@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=Noto+Serif:wght@400;700&display=swap');

/* --- BASE STYLES --- */
body { 
    font-family: '-apple-system', 'BlinkMacSystemFont', 'San Francisco', 'Plus Jakarta Sans', sans-serif; 
    background-color: #F8F9FD; 
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
}

/* Global helper classes */
.hidden { display: none !important; }
.modal-overlay { background-color: rgba(0, 0, 0, 0.5); backdrop-filter: blur(2px); }
.tabular-nums { font-variant-numeric: tabular-nums; }

.nav-item.active { background-color: #EFF6FF; color: #1e3a8a; }
input:focus, select:focus, textarea:focus { outline: 2px solid #2563EB; outline-offset: 1px; }

.bill-input-screen:hover { background-color: #f3f4f6; border-radius: 4px; }
.bill-input-screen:focus { background-color: white; border-radius: 4px; }

input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active {
    -webkit-box-shadow: 0 0 0 30px white inset !important;
    transition: background-color 5000s ease-in-out 0s;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

/* Table Counters for Desktop */
table tbody { counter-reset: rowNumber; }
.sn-col { position: relative; text-align: center; vertical-align: middle; color: #6b7280; font-size: 0.75rem; font-weight: 600; }
.sn-col::before { counter-increment: rowNumber; content: counter(rowNumber); }

/* --- ANIMATIONS --- */
@keyframes modalSlideIn {
    from { opacity: 0; transform: translateY(20px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

#invoice-preview-modal:not(.hidden) #invoice-paper {
    animation: modalSlideIn 0.3s ease-out forwards;
}

/* --- MOBILE RESPONSIVE TABLES (Card View) --- */
@media screen and (max-width: 768px) {
    table, thead, tbody, tfoot, th, td, tr { display: block; }
    thead { display: none; }
    
    tbody tr {
        background: #ffffff;
        border-radius: 16px;
        padding: 16px;
        margin-bottom: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        border: 1px solid #f0f0f0;
        position: relative;
    }
    
    tbody td {
        padding: 4px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border: none !important;
        font-size: 13px;
        text-align: right;
    }
    
    tbody td::before {
        content: attr(data-label);
        font-weight: 700;
        color: #86868b;
        text-transform: uppercase;
        font-size: 10px;
        text-align: left;
        margin-right: 12px;
    }

    /* Ledger Footer Mobile Style */
    tfoot { display: block !important; margin-top: 16px; margin-bottom: 24px; }
    tfoot tr { display: block; background: #F9FAFB; border: 2px dashed #E5E7EB; border-radius: 16px; padding: 16px; }
    tfoot td { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; width: 100%; border: none !important; font-size: 14px; }
    
    /* Ledger Footer Specifics */
    tfoot td:nth-child(1) { font-weight: 800; text-transform: uppercase; color: #374151; border-bottom: 1px solid #E5E7EB !important; margin-bottom: 8px; padding-bottom: 8px; display: block; text-align: left; }
    tfoot td:nth-child(2)::before { content: "Total Debit"; color: #6B7280; font-weight: 600; font-size: 12px; }
    tfoot td:nth-child(3)::before { content: "Total Credit"; color: #6B7280; font-weight: 600; font-size: 12px; }
    tfoot td:nth-child(4)::before { content: "Closing Balance"; color: #111; font-weight: 800; font-size: 13px; }
    tfoot td:nth-child(4) { border-top: 1px solid #E5E7EB !important; margin-top: 8px; padding-top: 8px; font-size: 16px; font-weight: 900; color: #1d1d1f; }

    /* Billing Table Mobile Fixes */
    #billing-items-container { min-height: auto !important; margin-bottom: 20px !important; }
    #bill-items tr { display: grid; grid-template-columns: 30px 1.5fr 1fr 1fr; gap: 8px; }
    #bill-items td:nth-child(1) { grid-column: 1; grid-row: 1; justify-content: center; font-weight: bold; color: #888; }
    #bill-items td:nth-child(1)::before { display: none; }
    #bill-items td:nth-child(2) { grid-column: 2 / -1; width: 100%; border-bottom: 1px dashed #eee !important; padding-bottom: 8px; margin-bottom: 4px; justify-content: start; }
    #bill-items td:nth-child(2)::before { display: none; }
    #bill-items td:nth-child(3), #bill-items td:nth-child(4), #bill-items td:nth-child(5) { display: flex; flex-direction: column; align-items: flex-start; }
    #bill-items td:nth-child(3)::after { content: 'Qty'; font-size: 9px; color: #aaa; font-weight: bold; }
    #bill-items td:nth-child(4)::after { content: 'Rate'; font-size: 9px; color: #aaa; font-weight: bold; }
    #bill-items td:nth-child(5)::after { content: 'Amt'; font-size: 9px; color: #aaa; font-weight: bold; }
    #bill-items td::before { display: none !important; }
    #bill-items td:last-child { position: absolute; top: 12px; right: 12px; }

    /* Quote Table Mobile Fixes */
    #quote-rows tr { display: grid; grid-template-columns: 30px 1fr 1fr 1fr; gap: 8px; }
    #quote-rows td:nth-child(1) { grid-column: 1; font-weight: bold; justify-content: center; } 
    #quote-rows td:nth-child(2) { grid-column: 2 / -1; width: 100%; border-bottom: 1px dashed #eee !important; }
    #quote-rows td:last-child { position: absolute; top: 12px; right: 12px; }
}

/* --- PRINT STYLES --- */
@media print {
    @page { margin: 10mm; size: auto; }
    @page :first { margin: 0mm; } 

    html, body { 
        width: 100%; 
        height: auto !important; 
        min-height: 0 !important;
        overflow: visible !important; 
        background: white !important; 
        color: #000; 
        margin: 0 !important; 
        padding: 0 !important;
        font-family: 'Noto Serif', serif; 
        font-size: 10pt; 
        line-height: 1.3;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    /* Reset Container Widths for Mobile Print */
    #app-container, main, .app-section {
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
        display: block !important;
        height: auto !important;
        overflow: visible !important;
        position: static !important;
    }

    /* Hide UI elements */
    .no-print, aside, nav, .mobile-bottom-nav, #mobile-menu-drawer, #auth-container, #logout-modal, .modal-overlay { 
        display: none !important; 
    }

    /* --- SCENARIO 1: Printing Invoice History (Modal) --- */
    body.printing-invoice #app-container { display: none !important; }
    
    body.printing-invoice #invoice-preview-modal {
        display: block !important;
        visibility: visible !important;
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: auto !important;
        overflow: visible !important;
        background: white !important;
        z-index: 9999;
        margin: 0 !important;
        padding: 0 !important;
    }

    body.printing-invoice #invoice-paper {
        min-height: 0 !important;
        padding: 0.5cm !important;
        margin: 0 !important;
        box-shadow: none !important;
        border: none !important;
        width: 100% !important;
    }

    /* --- SCENARIO 2: Printing Bill / Quote / Ledger (Main App) --- */
    /* Hide all siblings of the active section */
    body.printing-bill #dashboard, body.printing-bill #history, body.printing-bill #customers, body.printing-bill #reports, body.printing-bill #settings, body.printing-bill #ledger, body.printing-bill #inventory, body.printing-bill #quotations { display: none !important; }
    
    /* Ensure the active print view is visible and full width */
    #billing-print-view, #quotation-paper, #ledger-print-view {
        display: block !important;
        width: 100% !important;
        max-width: none !important;
        min-width: 0 !important; /* Fix for mobile content overflow */
        box-shadow: none !important;
        border: none !important;
        padding: 0.5cm !important;
        margin: 0 !important;
        animation: none !important;
        transform: none !important;
    }

    /* Grid Adjustments for Print */
    .bill-info-grid { display: grid !important; grid-template-columns: 1fr 1fr !important; gap: 1cm !important; margin-bottom: 15px !important; }
    
    /* Ledger Summary Grid Fix */
    #ledger-print-view .grid-cols-4 { display: grid !important; grid-template-columns: repeat(4, 1fr) !important; gap: 5px !important; }
    #ledger-print-view .grid-cols-4 > div { padding: 5px !important; border: 1px solid #eee !important; text-align: center !important; }
    #ledger-print-view .grid-cols-4 p { font-size: 12px !important; }
    #ledger-print-view .grid-cols-4 .text-3xl { font-size: 20pt !important; } /* Amounts bigger */
    #ledger-print-view .grid-cols-4 .text-4xl { font-size: 20pt !important; } /* Total bigger */

    /* Table Fixes */
    table { width: 100% !important; border-collapse: collapse !important; display: table !important; }
    thead { display: table-header-group !important; }
    tfoot { display: table-row-group !important; } /* FIXED: Prevents repeating footer on every page */
    tbody { display: table-row-group !important; }
    tr { display: table-row !important; page-break-inside: avoid; border-bottom: 1px solid #eee !important; }
    th { display: table-cell !important; }
    
    /* Force black text for table cells generally */
    
    /* Mobile pseudo-elements hide */
    tbody td::before { display: none !important; }

    /* S.N. Visibility Logic */
    .sn-col::before { display: block !important; content: counter(rowNumber) !important; font-weight: bold; color: #000 !important; } 

    /* Specific Input Fixes */
    #bill-chassis { width: 140px !important; }
    #bill-date-input { width: 145px !important; text-align: right; padding-right: 5px !important; }
    #bill-mileage { display: none !important; }
    #bill-mileage-print { display: block !important; }
    
    /* Discount Row Visibility */
    div.hidden.print\:flex { display: flex !important; }

    input, select, textarea { border: none !important; background: transparent !important; padding: 0 !important; margin: 0 !important; font-family: inherit; font-size: inherit; font-weight: inherit; color: inherit; width: auto; resize: none; appearance: none; box-shadow: none !important; }
    
    ::placeholder { color: transparent; }
    .print-hide-empty:placeholder-shown { display: none !important; }

    #quotations #quote-date-input { text-align: right !important; width: 90px !important; }

   
    .break-inside-avoid { break-inside: avoid !important; page-break-inside: avoid !important; }
}