/* ===== فاتورة المبيعات — معاينة وطباعة ===== */
.invoice-preview-modal {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 24px 16px;
    overflow-y: auto;
    background: #e8eaed;
}

.invoice-preview-modal[hidden] {
    display: none !important;
}

.invoice-preview-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
}

.invoice-preview-dialog {
    position: relative;
    z-index: 1;
    width: min(210mm, 100%);
    margin: 0 auto;
}

.invoice-preview-toolbar {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 16px;
    position: sticky;
    top: 12px;
    z-index: 2;
}

.invoice-preview-toolbar button {
    border: none;
    border-radius: 12px;
    padding: 12px 24px;
    font-family: 'Cairo', sans-serif;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.invoice-preview-print {
    background: linear-gradient(135deg, #ff8c00, #ff7700);
    color: #fff;
}

.invoice-preview-close {
    background: #fff;
    color: #444;
}

/* ورقة الفاتورة — A4 */
.invoice-paper {
    background: #fff;
    color: #1f2937;
    border: 1px solid #cbd5e1;
    border-radius: 0;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
    padding: 14mm 16mm;
    box-sizing: border-box;
    font-family: 'Cairo', sans-serif;
    line-height: 1.55;
}

.invoice-nums-en,
.invoice-nums-en .num-cell,
.invoice-nums-en .value,
.invoice-nums-en .invoice-meta-row span:last-child,
.invoice-nums-en .invoice-total-line .value {
    font-variant-numeric: lining-nums;
    font-feature-settings: 'lnum' 1;
}

.invoice-paper-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 24px;
    padding-bottom: 20px;
    border-bottom: 2px solid #ff8c00;
    margin-bottom: 24px;
}

.invoice-paper-brand {
    display: flex;
    align-items: center;
    gap: 20px;
    flex: 1;
}

.invoice-paper-brand img {
    width: 200px;
    max-height: 150px;
    height: auto;
    object-fit: contain;
    flex-shrink: 0;
}

.invoice-paper-brand h1 {
    margin: 0 0 6px;
    font-size: 1.25rem;
    font-weight: 800;
    color: #1e293b;
    line-height: 1.35;
}

.invoice-paper-brand p,
.invoice-paper-brand .invoice-company-name-en {
    margin: 0;
    font-size: 0.88rem;
    color: #64748b;
    font-weight: 600;
    line-height: 1.4;
}

.invoice-paper-meta {
    text-align: left;
    min-width: 240px;
    flex-shrink: 0;
}

.invoice-paper-title {
    display: inline-block;
    background: #ff8c00;
    color: #fff;
    padding: 8px 20px;
    border-radius: 4px;
    font-size: 0.92rem;
    font-weight: 800;
    margin-bottom: 12px;
    letter-spacing: 0.01em;
    line-height: 1.45;
    text-align: center;
}

.invoice-meta-row {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    font-size: 0.84rem;
    padding: 5px 0;
    border-bottom: 1px solid #e5e7eb;
}

.invoice-meta-row:last-child {
    border-bottom: none;
}

.invoice-meta-row span:first-child {
    color: #64748b;
    font-weight: 600;
}

.invoice-meta-row span:last-child {
    font-weight: 700;
    direction: ltr;
    text-align: left;
}

.invoice-parties {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    gap: 20px;
    margin-bottom: 24px;
}

.invoice-party-box {
    flex: 1 1 0;
    min-width: 0;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    padding: 16px 18px;
    text-align: left;
    direction: ltr;
}

.invoice-party-box h3 {
    margin: 0 0 12px;
    font-size: 0.95rem;
    font-weight: 800;
    color: #ff8c00;
    padding-bottom: 8px;
    border-bottom: 2px solid rgba(255, 140, 0, 0.25);
    text-align: left;
}

.invoice-party-line {
    font-size: 0.82rem;
    margin: 5px 0;
    color: #334155;
    text-align: left;
}

.invoice-party-line strong {
    color: #64748b;
    font-weight: 700;
    margin-right: 6px;
    margin-left: 0;
}

.invoice-bi-label {
    display: inline;
    white-space: normal;
}

.invoice-label-sep,
.invoice-title-sep {
    opacity: 0.65;
    font-weight: 600;
}

.invoice-items-table th .invoice-bi-label {
    display: block;
    line-height: 1.35;
    font-size: 0.78rem;
}

.invoice-items-table th .invoice-label-ar {
    display: block;
}

.invoice-items-table th .invoice-label-sep {
    display: none;
}

.invoice-items-table th .invoice-label-en {
    display: block;
    font-size: 0.72rem;
    font-weight: 600;
    opacity: 0.92;
}

.invoice-items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 12px;
    font-size: 0.82rem;
}

.invoice-items-table thead tr {
    background: #ff8c00;
    color: #fff;
}

.invoice-items-table th,
.invoice-items-table td {
    padding: 10px 9px;
    border: 1px solid #d1d5db;
    text-align: right;
}

.invoice-items-table th {
    font-weight: 700;
}

.invoice-items-table th:first-child,
.invoice-items-table td:first-child {
    text-align: center;
    width: 40px;
}

.invoice-items-table tbody tr:nth-child(even) {
    background: #f9fafb;
}

.invoice-items-table .num-cell {
    direction: ltr;
    text-align: left;
    white-space: nowrap;
}

.invoice-bottom {
    display: block;
    width: 100%;
    margin-top: 4px;
}

.invoice-totals-box {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: stretch;
    background: #fff;
    border: 2px solid #ff8c00;
    border-radius: 4px;
    padding: 0;
    overflow: hidden;
    box-sizing: border-box;
}

.invoice-total-line {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    padding: 10px 14px;
    font-size: 0.8rem;
    border-bottom: none;
    border-left: 1px solid #e5e7eb;
}

.invoice-total-line:first-child {
    border-left: none;
}

.invoice-total-line span:first-child {
    color: #64748b;
    font-weight: 700;
    line-height: 1.35;
}

.invoice-total-line.grand {
    margin-top: 0;
    padding-top: 10px;
    padding-bottom: 10px;
    border-top: none;
    border-left: 2px solid #ff8c00;
    background: #fff7ed;
    font-size: 0.88rem;
    font-weight: 800;
    color: #ff8c00;
}

.invoice-total-line.grand span:first-child {
    color: #c2410c;
}

.invoice-total-line .value {
    direction: ltr;
    font-weight: 800;
    font-size: 0.92rem;
    white-space: nowrap;
}

.invoice-total-line.grand .value {
    font-size: 1rem;
    color: #ea580c;
}

@media (max-width: 768px) {
    .invoice-paper {
        padding: 20px 16px;
    }

    .invoice-paper-header {
        flex-direction: column;
    }

    .invoice-paper-brand img {
        width: 160px;
        max-height: 120px;
    }

    .invoice-totals-box {
        flex-direction: column;
    }

    .invoice-total-line {
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        border-left: none !important;
        border-top: 1px solid #e5e7eb;
    }

    .invoice-total-line:first-child {
        border-top: none;
    }

    .invoice-paper-meta {
        text-align: right;
        width: 100%;
    }
}

@media print {
    @page {
        size: A4 portrait;
        margin: 0;
    }

    html,
    body {
        width: auto !important;
        height: auto !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    body.printing-invoice {
        padding-top: 0 !important;
    }

    body.printing-invoice > *:not(#invoicePreviewModal) {
        display: none !important;
    }

    body.printing-invoice #invoicePreviewModal {
        display: block !important;
        position: static !important;
        inset: auto !important;
        width: auto !important;
        height: auto !important;
        padding: 0 !important;
        margin: 0 !important;
        overflow: visible !important;
        background: #fff !important;
    }

    body.printing-invoice #invoicePreviewModal[hidden] {
        display: block !important;
    }

    .invoice-preview-backdrop,
    .invoice-preview-toolbar {
        display: none !important;
    }

    .invoice-preview-dialog {
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* ضغط المحتوى لصفحة A4 واحدة */
    .invoice-paper {
        box-shadow: none !important;
        border: none !important;
        border-radius: 0 !important;
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
        padding: 10mm 12mm !important;
    }

    .invoice-paper-header {
        margin-bottom: 16px !important;
        padding-bottom: 14px !important;
    }

    .invoice-parties {
        margin-bottom: 16px !important;
    }

    .invoice-party-box {
        padding: 12px 14px !important;
    }

    .invoice-items-table {
        margin-bottom: 10px !important;
        font-size: 0.76rem !important;
    }

    .invoice-items-table th,
    .invoice-items-table td {
        padding: 5px 6px !important;
    }

    .invoice-bottom {
        margin-top: 0 !important;
        page-break-inside: avoid;
    }

    .invoice-totals-box {
        width: 100% !important;
        max-width: none !important;
        flex-direction: row !important;
        page-break-inside: avoid;
    }

    .invoice-total-line {
        padding: 8px 10px !important;
        font-size: 0.74rem !important;
    }

    .invoice-total-line .value {
        font-size: 0.82rem !important;
    }

    .invoice-total-line.grand {
        font-size: 0.8rem !important;
    }

    .invoice-total-line.grand .value {
        font-size: 0.9rem !important;
    }

    .invoice-parties {
        display: flex !important;
        flex-direction: row !important;
        align-items: stretch !important;
        gap: 16px !important;
    }

    .invoice-party-box {
        flex: 1 1 50% !important;
        min-width: 0 !important;
    }

    .invoice-items-table thead tr {
        background: #ff8c00 !important;
        color: #fff !important;
    }
}
