:root {
    --navy: #01205f;
    --navy-2: #0c2a64;
    --red: #b73332;
    --bg: #eef3f9;
    --card: #ffffff;
    --line: #d7e1ef;
    --text: #132844;
    --muted: #64748b;
    --success: #0f766e;
    --warning: #a16207;
    --danger: #b91c1c;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: Arial, Tahoma, "Noto Naskh Arabic", "Segoe UI", sans-serif;
}
a { color: inherit; }
.app-header {
    background: var(--navy);
    color: #fff;
    padding: 18px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 18px;
    box-shadow: 0 8px 30px rgba(1, 32, 95, .22);
}
.app-brand {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}
.app-logo {
    width: 58px;
    height: 58px;
    object-fit: contain;
    background: #fff;
    border-radius: 12px;
    padding: 4px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, .18);
    flex: 0 0 auto;
}
.brand-title { margin: 0; font-size: 22px; line-height: 1.2; font-weight: 900; }
.brand-subtitle { margin: 4px 0 0; font-size: 13px; opacity: .85; }
.container { max-width: 1240px; margin: 24px auto 42px; padding: 0 18px; }
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 12px 30px rgba(1, 32, 95, .08);
    overflow: hidden;
}
.card-header {
    padding: 18px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--line);
}
.card-header h2 { margin: 0; font-size: 18px; color: var(--navy); }
.card-body { padding: 20px; }
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.btn {
    border: 0;
    display: inline-flex;
    min-height: 38px;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border-radius: 10px;
    padding: 9px 14px;
    font-size: 14px;
    line-height: 1;
    font-weight: 800;
    text-decoration: none;
    cursor: pointer;
    white-space: nowrap;
}
.btn-primary { background: var(--navy); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-soft { background: #e9f0fb; color: var(--navy); }
.btn-outline { background: #fff; color: var(--navy); border: 1px solid var(--navy); }
.btn-small { min-height: 32px; padding: 7px 10px; font-size: 12px; border-radius: 8px; }
.filters {
    display: grid;
    grid-template-columns: minmax(220px, 2fr) repeat(4, minmax(140px, 1fr)) auto;
    gap: 12px;
    align-items: end;
    padding: 16px 20px;
    border-bottom: 1px solid var(--line);
    background: #f8fbff;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.label-line { display: flex; justify-content: space-between; gap: 10px; color: var(--navy); font-size: 12px; font-weight: 900; }
.label-line [lang="ar"] { color: var(--red); }
input, select, textarea {
    width: 100%;
    border: 1px solid #b8c7dd;
    background: #fff;
    color: var(--text);
    border-radius: 10px;
    padding: 10px 12px;
    min-height: 40px;
    font: inherit;
    font-size: 14px;
    outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--navy); box-shadow: 0 0 0 3px rgba(1,32,95,.10); }
textarea { resize: vertical; min-height: 86px; }
.table-wrap { width: 100%; overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; min-width: 1040px; }
.data-table th, .data-table td { padding: 12px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.data-table th { background: #f4f8fd; color: var(--navy); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.data-table th .ar { display: block; text-transform: none; letter-spacing: 0; color: var(--red); margin-top: 3px; font-weight: 800; }
.data-table td { font-size: 14px; }
.amount { font-weight: 900; color: var(--navy); white-space: nowrap; }
.money-with-symbol { display: inline-flex; align-items: center; justify-content: flex-start; gap: 5px; direction: ltr; unicode-bidi: isolate; white-space: nowrap; }
.currency-icon { width: .82em; height: .95em; display: inline-block; flex: 0 0 auto; color: currentColor; vertical-align: -0.12em; }
.currency-label { font-size: .9em; font-weight: 900; opacity: .95; margin-left: 4px; white-space: nowrap; font-family: Arial, Tahoma, 'DejaVu Sans', sans-serif; }
.muted { color: var(--muted); font-size: 12px; }
.status-badge { display: inline-flex; gap: 5px; align-items: center; border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 900; white-space: nowrap; }
.status-paid { background: #dcfce7; color: #166534; }
.status-partial { background: #fef3c7; color: #92400e; }
.status-unpaid { background: #fee2e2; color: #991b1b; }
.status-draft { background: #e0f2fe; color: #075985; }
.status-cancelled { background: #e5e7eb; color: #374151; }
.pagination {
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}
.page-links { display: flex; gap: 6px; flex-wrap: wrap; }
.page-link { min-width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 9px; border: 1px solid var(--line); background: #fff; color: var(--navy); text-decoration: none; font-weight: 800; }
.page-link.active { background: var(--navy); color: #fff; border-color: var(--navy); }
.flash { margin-bottom: 16px; border-radius: 12px; padding: 12px 14px; font-weight: 800; }
.flash-success { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
.flash-error { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-4 { grid-column: span 4; }
.section-title { margin: 24px 0 12px; font-size: 16px; color: var(--navy); font-weight: 900; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.items-editor { border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.item-row {
    display: grid;
    grid-template-columns: 1.2fr 1.2fr .65fr .45fr .55fr .55fr .65fr .65fr .6fr auto;
    gap: 8px;
    padding: 12px;
    border-bottom: 1px solid var(--line);
    align-items: end;
    background: #fff;
}
.item-row:nth-child(odd) { background: #fbfdff; }
.item-row:last-child { border-bottom: 0; }
.item-row .field input { min-height: 36px; padding: 8px 9px; font-size: 13px; }
.item-total { font-weight: 900; color: var(--navy); padding: 9px 0; white-space: nowrap; }
.summary-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-top: 14px; }
.summary-box { background: #f8fbff; border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.summary-box span { display: block; color: var(--muted); font-size: 12px; margin-bottom: 5px; font-weight: 700; }
.summary-box strong { color: var(--navy); font-size: 16px; }
.error-list { margin: 0 0 16px; padding: 14px 18px 14px 34px; background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; border-radius: 12px; font-weight: 700; }
.empty-state { padding: 32px 20px; text-align: center; color: var(--muted); }
.preview-toolbar { position: sticky; top: 0; z-index: 20; background: #fff; border-bottom: 1px solid var(--line); padding: 14px 18px; display: flex; justify-content: space-between; align-items: center; gap: 12px; box-shadow: 0 8px 28px rgba(1,32,95,.12); }
.preview-toolbar h1 { margin: 0; color: var(--navy); font-size: 18px; }
.preview-toolbar p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.preview-shell { width: max-content; max-width: calc(100vw - 32px); margin: 24px auto 40px; overflow: auto; background: #fff; box-shadow: 0 18px 55px rgba(1,32,95,.20); }
.preview-shell .invoice-page { box-shadow: none; }
@media (max-width: 1100px) {
    .filters { grid-template-columns: repeat(2, 1fr); }
    .filters .actions { grid-column: span 2; }
    .form-grid { grid-template-columns: repeat(2, 1fr); }
    .form-grid .span-2, .form-grid .span-4 { grid-column: span 2; }
    .summary-grid { grid-template-columns: repeat(2, 1fr); }
    .item-row { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 720px) {
    .app-header, .card-header, .preview-toolbar { align-items: flex-start; flex-direction: column; }
    .app-brand { align-items: center; }
    .app-logo { width: 50px; height: 50px; border-radius: 10px; }
    .filters { grid-template-columns: 1fr; }
    .filters .actions { grid-column: auto; }
    .form-grid, .summary-grid, .item-row { grid-template-columns: 1fr; }
    .form-grid .span-2, .form-grid .span-4 { grid-column: auto; }
}
@media print {
    .preview-toolbar { display: none; }
    body { background: #fff; }
    .preview-shell { margin: 0; max-width: none; box-shadow: none; overflow: visible; }
}


/* Mobile CRUD responsiveness */
@media (max-width: 900px) {
    body { background: #f5f7fb; }
    .app-header { padding: 15px 14px; }
    .container { margin: 14px auto 28px; padding: 0 10px; }
    .card { border-radius: 12px; }
    .card-header { padding: 15px 14px; }
    .card-body { padding: 14px; }
    .filters { padding: 14px; gap: 10px; }
    .btn { width: 100%; white-space: normal; text-align: center; line-height: 1.2; }
    .actions { width: 100%; }
    .actions .btn, .actions form { flex: 1 1 150px; }
    .actions form .btn { width: 100%; }
    .data-table { min-width: 0; border-collapse: separate; border-spacing: 0 12px; }
    .data-table thead { display: none; }
    .data-table tbody, .data-table tr, .data-table td { display: block; width: 100%; }
    .data-table tr {
        background: #fff;
        border: 1px solid var(--line);
        border-radius: 14px;
        box-shadow: 0 8px 18px rgba(1, 32, 95, .08);
        overflow: hidden;
    }
    .data-table td {
        min-height: 44px;
        padding: 11px 12px;
        border-bottom: 1px solid #e8edf6;
        display: grid;
        grid-template-columns: minmax(110px, 38%) 1fr;
        gap: 10px;
        align-items: center;
        text-align: right;
    }
    .data-table td:last-child { border-bottom: 0; }
    .data-table td::before {
        content: attr(data-label);
        color: var(--navy);
        font-weight: 900;
        font-size: 12px;
        text-align: left;
    }
    .data-table td[data-label^="Actions"] {
        display: block;
        text-align: left;
    }
    .data-table td[data-label^="Actions"]::before {
        display: block;
        margin-bottom: 8px;
    }
    .table-wrap { overflow: visible; }
    .pagination { padding: 14px; align-items: stretch; }
    .page-links, .pagination .muted { width: 100%; justify-content: center; text-align: center; }
    .item-row { padding: 12px; border-radius: 0; }
    .summary-box { padding: 10px; }
    .preview-shell { transform-origin: top left; }
}

@media (max-width: 520px) {
    .brand-title { font-size: 18px; }
    .brand-subtitle { font-size: 12px; }
    .label-line { font-size: 11px; }
    input, select, textarea { font-size: 13px; padding: 9px 10px; }
    .section-title { flex-direction: column; align-items: stretch; }
    .summary-grid { grid-template-columns: 1fr; }
    .data-table td { grid-template-columns: 1fr; text-align: left; }
    .data-table td::before { margin-bottom: -4px; }
    .preview-toolbar .actions { display: grid; grid-template-columns: 1fr; }
    .preview-shell { max-width: calc(100vw - 12px); margin: 12px 6px 28px; }
}

/* WhatsApp PDF file sharing */
.btn-whatsapp-share {
    background: #22c55e;
    color: #fff;
    border: 1px solid #16a34a;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}
.btn-whatsapp-share:hover {
    background: #16a34a;
    color: #fff;
}
.btn-whatsapp-share:disabled {
    cursor: wait;
    opacity: .75;
}
.whatsapp-icon {
    width: 16px;
    height: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 16px;
}
.whatsapp-icon svg {
    width: 16px;
    height: 16px;
    display: block;
}
.share-status[hidden] { display: none; }
.share-status {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 1000;
    width: min(420px, calc(100vw - 36px));
    padding: 12px 14px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--navy);
    font-weight: 800;
    font-size: 13px;
    line-height: 1.45;
    box-shadow: 0 18px 45px rgba(1, 32, 95, .18);
}
.share-status-success {
    background: #ecfdf5;
    border-color: #86efac;
    color: #166534;
}
.share-status-warning {
    background: #fffbeb;
    border-color: #fcd34d;
    color: #92400e;
}
.share-status-error {
    background: #fef2f2;
    border-color: #fca5a5;
    color: #991b1b;
}
@media (max-width: 760px) {
    .whatsapp-icon, .whatsapp-icon svg { width: 15px; height: 15px; }
    .share-status {
        left: 10px;
        right: 10px;
        bottom: 10px;
        width: auto;
    }
}
