/* =========================================
   INVOICE MODAL & SCREEN LAYOUT
   ========================================= */

/* Modal Wrapper - Centers the invoice on screen */
.invoice-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: flex-start; /* Start from top to allow scrolling */
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    opacity: 1;
    transition: opacity 0.3s ease;
    padding: 20px 10px; /* Padding for mobile edges */
    box-sizing: border-box;
    overflow: hidden; /* Prevent body scroll interactions */
}

/* Hidden State */
.invoice-modal.hidden {
    display: none;
    opacity: 0;
    pointer-events: none;
}

/* Invoice Content Scroll Wrapper */
.invoice-content {
    position: relative;
    z-index: 2002;
    width: 100%;
    max-width: 210mm; /* A4 Width Limit */
    max-height: 95vh; /* Keep within viewport height */
    overflow-y: auto; /* Enable Vertical Scrolling */
    overflow-x: hidden;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    border-radius: 4px;

    /* Scrollbar Styling */
    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 transparent;
}

.invoice-content::-webkit-scrollbar {
    width: 8px;
}
.invoice-content::-webkit-scrollbar-thumb {
    background-color: #cbd5e1;
    border-radius: 4px;
}

/* Action Bar (Buttons) */
.invoice-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 0 5px 10px 5px;
    position: sticky;
    top: 0;
    z-index: 10;
    /* Optional: Add a slight gradient/bg behind buttons if content scrolls under */
}

/* Dark Backdrop (Visual layer) */
.invoice-backdrop {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    z-index: -1;
}

/* =========================================
   THE INVOICE PAPER (Visual Design)
   ========================================= */

.invoice-paper {
    background: white;
    width: 100%;
    min-height: 297mm; /* A4 Height minimum */
    padding: 15mm 20mm; /* Standard Print Margins */
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    border-radius: 2px;
    font-family: 'Vazirmatn', 'Tahoma', 'Segoe UI', sans-serif;
    color: #333;
    direction: rtl;
    text-align: right;
    box-sizing: border-box;
}

/* --- Header Section --- */
.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 25px;
    flex-wrap: wrap; /* Allow wrapping on small screens */
    gap: 20px;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 15px;
    flex: 1;
    min-width: 200px;
}

.invoice-logo {
    height: 65px;
    width: auto;
    object-fit: contain;
}

.brand-info h1 {
    font-size: 1.6rem;
    margin: 0 0 5px 0;
    color: #1e3a8a; /* Brand Blue */
    font-weight: 800;
    line-height: 1.2;
}

.brand-info p {
    font-size: 0.85rem;
    color: #666;
    margin: 0;
    line-height: 1.4;
}

.header-meta {
    text-align: left;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 5px;
    min-width: 150px;
}

.meta-item {
    font-size: 0.9rem;
    color: #444;
    white-space: nowrap;
}

.meta-label {
    color: #888;
    margin-left: 8px;
    font-size: 0.85rem;
}

.meta-value {
    font-weight: bold;
    font-family: 'Courier New', monospace;
    letter-spacing: 1px;
    color: #000;
}

/* --- Badges --- */
.invoice-badge {
    display: inline-block;
    padding: 6px 18px;
    border-radius: 20px;
    color: white;
    font-weight: bold;
    font-size: 0.9rem;
    margin-top: 8px;
    text-align: center;
    min-width: 80px;
}
.badge-electricity { background-color: #f59e0b; } /* Amber */
.badge-water       { background-color: #3b82f6; } /* Blue */
.badge-gas         { background-color: #ef4444; } /* Red */
.badge-heat        { background-color: #ec4899; } /* Pink */

/* --- Divider --- */
.invoice-divider {
    border: 0;
    border-top: 2px solid #f1f5f9;
    margin: 20px 0 25px 0;
}

/* --- Subscriber Info Grid --- */
.subscriber-info {
    display: grid;
    grid-template-columns: repeat(3, 1fr); /* 3 Columns on Desktop */
    gap: 20px;
    background: #f8fafc;
    padding: 20px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    margin-bottom: 30px;
}

.info-column h4 {
    margin: 0 0 12px 0;
    font-size: 0.95rem;
    color: #64748b;
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 6px;
    display: inline-block;
}

.info-column p {
    margin: 6px 0;
    font-size: 0.92rem;
    color: #334155;
    line-height: 1.6;
}

.info-column .highlight {
    color: #1e3a8a;
    font-weight: bold;
    font-size: 1.1rem;
    margin-top: 10px;
    display: block;
}

/* --- Responsive Grids (Breakdown Boxes) --- */
.breakdown-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    width: 100%;
}

.metric-box {
    background: #f8fafc;
    padding: 12px;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
    text-align: center;
}

/* --- Tables --- */
.invoice-section { margin-bottom: 30px; }
.invoice-section h4 {
    font-size: 1.1rem;
    color: #1e293b;
    margin-bottom: 12px;
    border-right: 4px solid #1e3a8a;
    padding-right: 10px;
}

.invoice-table-wrapper {
    overflow-x: auto; /* Allow horizontal scroll for tables on mobile */
    -webkit-overflow-scrolling: touch;
}

.invoice-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    background: white;
    min-width: 300px; /* Prevent squishing on very small screens */
}

.invoice-table th {
    background: #f1f5f9;
    color: #475569;
    padding: 12px 15px;
    text-align: right;
    border-bottom: 2px solid #cbd5e1;
    font-weight: 700;
}

.invoice-table td {
    padding: 12px 15px;
    border-bottom: 1px solid #e2e8f0;
    color: #334155;
}

.invoice-table tr:last-child td { border-bottom: none; }

/* Financial Table Specifics */
.financial-table th { background: #1e3a8a; color: white; border: none; }
.financial-table tbody tr:nth-child(even) { background-color: #f8fafc; }

.financial-table tfoot .total-row {
    background-color: #1e3a8a;
    color: white;
    font-weight: bold;
    font-size: 1.2rem;
}
.financial-table tfoot td { border: none; padding: 15px; }
.financial-table tfoot .text-end { color: #fbbf24; } /* Gold Color */

/* Alignment Utilities */
.text-center { text-align: center; }
.text-end { text-align: left; direction: ltr; } /* LTR for currency/numbers */

/* Indicators */
.dot-indicator {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-left: 8px;
}
.dot-indicator.peak   { background-color: #ef4444; }
.dot-indicator.normal { background-color: #f59e0b; }
.dot-indicator.low    { background-color: #10b981; }

/* Status Bar */
.status-bar {
    margin-top: 15px;
    background: #fff;
    border-radius: 8px;
    padding: 12px 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}

/* --- Footer --- */
.invoice-footer {
    margin-top: 50px;
    padding-top: 30px;
    border-top: 1px dashed #cbd5e1;
    text-align: center;
}
.footer-note { font-size: 0.85rem; color: #64748b; margin-bottom: 15px; line-height: 1.6; }
.footer-branding { font-size: 0.8rem; color: #94a3b8; text-transform: uppercase; letter-spacing: 1px; }

/* =========================================
   RESPONSIVE DESIGN (Mobile/Tablet)
   ========================================= */

@media screen and (max-width: 768px) {
    .invoice-modal {
        padding: 0;
        align-items: flex-start;
    }

    .invoice-content {
        max-height: 100vh;
        margin: 0;
        border-radius: 0;
    }

    .invoice-paper {
        padding: 20px 15px;
        min-height: auto;
        box-shadow: none;
    }

    /* Stack Header */
    .invoice-header {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: 15px;
    }

    .header-brand {
        flex-direction: column;
        width: 100%;
        text-align: center;
    }

    .header-meta {
        align-items: center;
        width: 100%;
        text-align: center;
        margin-top: 10px;
        border-top: 1px solid #f1f5f9;
        padding-top: 15px;
    }

    /* Stack Grid Columns */
    .subscriber-info {
        grid-template-columns: 1fr; /* Stack into 1 column */
        gap: 15px;
        padding: 15px;
    }

    .breakdown-grid {
        grid-template-columns: 1fr !important; /* Stack metrics */
    }

    /* Adjust Status Bar */
    .status-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    .status-bar > div:last-child {
        align-self: flex-end;
    }

    /* Condensed Table Fonts */
    .invoice-table th, .invoice-table td {
        padding: 8px 10px;
        font-size: 0.85rem;
    }

    .financial-table tfoot .total-row {
        font-size: 1rem;
    }
}

/* =========================================
   PRINT CONFIGURATION (Strict A4)
   ========================================= */

@media print {
    /* Hide the entire app UI */
    body > * { display: none !important; }

    /* Reveal only the Invoice Modal components */
    .invoice-modal {
        display: block !important;
        position: static !important;
        background: white !important;
        width: 100% !important;
        height: auto !important;
        padding: 0 !important;
        margin: 0 !important;
        z-index: 9999 !important;
        overflow: visible !important;
    }

    .invoice-content, #printableInvoice {
        display: block !important;
        visibility: visible !important;
        width: 100% !important;
        max-width: none !important;
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
        overflow: visible !important;
        height: auto !important;
    }

    /* Force A4 Width for the Paper */
    .invoice-paper {
        padding: 0 !important;
        margin: 0 !important;
        width: 210mm !important;
        min-height: 297mm !important;
    }

    /* Restore Grids layout for Print (don't use mobile stack) */
    .subscriber-info { grid-template-columns: repeat(3, 1fr) !important; }
    .invoice-header { flex-direction: row !important; text-align: right !important; }
    .header-brand { flex-direction: row !important; text-align: right !important; }
    .header-meta { align-items: flex-end !important; text-align: left !important; }
    .breakdown-grid { grid-template-columns: 1fr 1fr !important; }
    .status-bar { flex-direction: row !important; }

    /* Hide Action Buttons & Backdrop */
    .invoice-backdrop, .invoice-actions { display: none !important; }

    /* Page Margins & Colors */
    @page { size: A4; margin: 15mm; }
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
}