/* ==========================================================================
   app.css — واجهة البرنامج (مش الفاتورة نفسها)
   ========================================================================== */

:root {
    --bg: #f2f0eb;
    --surface: #fbfaf7;
    --surface-2: #f6f4ef;
    --ink: #1b1b18;
    --ink-2: #3d3c37;
    --muted: #77756c;
    --line: #e1ddd3;
    --line-strong: #cfcabd;
    --accent: #1d5c4b;
    --danger: #a3372f;
    --warn: #a3621b;
    --ok: #2f6b45;
    --focus: color-mix(in srgb, var(--accent) 45%, transparent);

    --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
    --mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

    --r: 8px;
    --panel-w: 440px;
    --topbar-h: 56px;

    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121211;
        --surface: #1a1a18;
        --surface-2: #20201d;
        --ink: #ecebe5;
        --ink-2: #c9c7bf;
        --muted: #8f8d84;
        --line: #2c2b28;
        --line-strong: #3b3a36;
        --danger: #e07a70;
        --warn: #d9a05a;
        --ok: #7cbf8e;
        color-scheme: dark;
    }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

.mono { font-family: var(--mono); font-feature-settings: "zero" 1; }
.muted { color: var(--muted); }
.late { color: var(--warn); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

a { color: inherit; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- buttons ---------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 36px; padding: 0 14px;
    border: 1px solid var(--line-strong); border-radius: var(--r);
    background: var(--surface); color: var(--ink);
    font: inherit; font-weight: 500; font-size: 13.5px;
    text-decoration: none; cursor: pointer; white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: var(--surface-2); border-color: var(--muted); }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }

.btn-primary { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn-primary:hover { background: var(--ink-2); border-color: var(--ink-2); }

.btn-ghost { background: transparent; border-style: dashed; color: var(--ink-2); }
.btn-ghost:hover { background: var(--surface); }

.btn-danger-text { background: transparent; border-color: transparent; color: var(--danger); }
.btn-danger-text:hover { background: color-mix(in srgb, var(--danger) 8%, transparent); border-color: transparent; }

.link {
    background: none; border: 0; padding: 0;
    font: inherit; color: var(--ink-2); cursor: pointer;
    text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px;
}
.link:hover { color: var(--ink); text-decoration-color: currentColor; }

.icon-btn {
    display: inline-grid; place-items: center;
    width: 30px; height: 30px; padding: 0;
    border: 0; border-radius: 6px; background: transparent;
    color: var(--muted); cursor: pointer; font-size: 14px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.danger:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); }
.icon-btn:disabled { opacity: .25; pointer-events: none; }
.icon-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- inputs ---------- */

.input {
    width: 100%; min-width: 0;
    height: 36px; padding: 0 10px;
    border: 1px solid var(--line-strong); border-radius: 6px;
    background: var(--surface); color: var(--ink);
    font: inherit; font-size: 14px;
    transition: border-color .15s, box-shadow .15s;
}
textarea.input { height: auto; padding: 8px 10px; resize: vertical; line-height: 1.5; unicode-bidi: plaintext; }
.input::placeholder { color: color-mix(in srgb, var(--muted) 75%, transparent); }
.input:hover { border-color: var(--muted); }
.input:focus { outline: none; border-color: var(--ink-2); box-shadow: 0 0 0 3px var(--focus); }
.input.mono { font-size: 13.5px; }
select.input { padding-inline-end: 6px; cursor: pointer; }
input[type="date"].input { font-family: var(--mono); font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.field > span em { font-style: normal; font-weight: 400; color: var(--muted); }

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

.seg {
    display: grid; grid-template-columns: 1fr 1fr;
    height: 36px; padding: 3px;
    border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface-2);
}
.seg label { position: relative; display: grid; place-items: center; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { width: 100%; text-align: center; padding: 3px 0; border-radius: 4px; font-size: 13px; color: var(--muted); transition: background .15s, color .15s; }
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--line-strong); font-weight: 500; }
.seg input:focus-visible + span { outline: 2px solid var(--focus); }

.hint { margin: 0; font-size: 12.5px; color: var(--muted); }

/* chips = checkboxes (طرق الدفع) */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { position: relative; cursor: pointer; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
    display: inline-flex; align-items: center; gap: 7px;
    height: 32px; padding: 0 13px;
    border: 1px solid var(--line-strong); border-radius: 999px;
    background: var(--surface); color: var(--ink-2); font-size: 13px;
    transition: background .15s, border-color .15s, color .15s;
}
.chip span::before {
    content: ''; width: 12px; height: 12px; border-radius: 3px;
    box-shadow: inset 0 0 0 1.5px var(--line-strong);
}
.chip:hover span { border-color: var(--muted); }
.chip input:checked + span { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.chip input:checked + span::before {
    box-shadow: none; background: var(--bg);
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.2l2.3 2.3 4.7-5' fill='none' stroke='%23000' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
            mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.2l2.3 2.3 4.7-5' fill='none' stroke='%23000' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
}
.chip input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }

.pay-block, .pay-fields { display: flex; flex-direction: column; gap: 12px; }
.pay-block .chips { margin-bottom: 2px; }

/* معاينة خط الفاتورة في الإعدادات — ورق أبيض زي الفاتورة */
.font-sample {
    display: flex; flex-direction: column; gap: 4px;
    padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--r);
    background: #fff; color: #17171a;
    font-family: var(--ar-font, var(--font));
}
.font-sample .fs-bold { font-size: 19px; font-weight: var(--ar-w-bold, 600); line-height: 1.35; }
.font-sample .fs-medium { font-size: 14.5px; font-weight: var(--ar-w-medium, 500); }
.font-sample .fs-regular { font-size: 12.5px; font-weight: var(--ar-w-regular, 400); color: #8a877e; }

/* ---------- top bar ---------- */

.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; justify-content: space-between;
    height: var(--topbar-h); padding: 0 24px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
}
.brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; text-decoration: none; }
.brand-mark {
    width: 14px; height: 18px; border-radius: 2px;
    background: linear-gradient(to bottom, var(--accent) 0 3px, transparent 3px), var(--surface);
    box-shadow: inset 0 0 0 1.5px var(--ink);
}
.topnav { display: flex; align-items: center; gap: 20px; }
.topnav > a:not(.btn) { text-decoration: none; color: var(--muted); font-size: 13.5px; }
.topnav > a:not(.btn):hover, .topnav > a.active { color: var(--ink); }
.topnav .link { text-decoration: none; color: var(--muted); font-size: 13.5px; }
.topnav .link:hover { color: var(--ink); }

/* ==========================================================================
   List view
   ========================================================================== */

.list-view { max-width: 1040px; margin: 0 auto; padding: 40px 24px 64px; }

.list-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.list-head h1 { margin: 0; font-size: 28px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.2; }
.list-head p { margin: 4px 0 0; }
.list-tools { display: flex; gap: 8px; }
.list-tools #search { width: 280px; }
.list-tools select { width: 140px; }

.summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.sum-card { padding: 16px 18px; border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); }
.sum-label { font-size: 12.5px; color: var(--ink-2); margin-bottom: 6px; }
.sum-amount { font-size: 22px; font-weight: 500; line-height: 1.35; }
.sum-amount .money { font-family: var(--mono); letter-spacing: -0.02em; }
.sum-amount .cur { font-size: 13px; color: var(--muted); }
.sum-note { margin-top: 6px; font-size: 12.5px; }

.backup-note {
    display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
    margin-bottom: 16px; padding: 10px 14px;
    border: 1px solid color-mix(in srgb, var(--warn) 30%, var(--line));
    border-radius: var(--r);
    background: color-mix(in srgb, var(--warn) 6%, var(--surface));
    font-size: 13px; color: var(--ink-2);
}

.table-card { border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); overflow: hidden; }

.inv-table { width: 100%; border-collapse: collapse; }
.inv-table th {
    padding: 10px 16px; text-align: start;
    font-size: 12px; font-weight: 500; color: var(--muted);
    border-bottom: 1px solid var(--line); background: var(--surface-2);
}
.inv-table td { padding: 12px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.inv-table tbody tr:last-child td { border-bottom: 0; }
.inv-table tbody tr { cursor: pointer; transition: background .12s; }
.inv-table tbody tr:hover { background: var(--surface-2); }
.inv-table tr.is-void td:not(:nth-child(5)):not(.actions-col) { opacity: .5; }
.inv-table tr.is-void .money { text-decoration: line-through; }
.inv-table td.mono { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.inv-table .num { text-align: end; white-space: nowrap; }
.inv-table td.num .money { font-family: var(--mono); font-size: 13.5px; }
.inv-table td.num .cur { font-size: 12px; color: var(--muted); }
.cell-main { font-weight: 500; }
.cell-sub { font-size: 12.5px; color: var(--muted); max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.actions-col { width: 80px; text-align: end; white-space: nowrap; }
.actions-col .icon-btn { opacity: 0; transition: opacity .12s; }
.inv-table tr:hover .icon-btn, .inv-table .icon-btn:focus-visible { opacity: 1; }

/* status pill (a styled select) */
.pill {
    position: relative; display: inline-flex; align-items: center;
    height: 26px; padding-inline: 10px 22px; border-radius: 999px;
    font-size: 12.5px; font-weight: 500; cursor: pointer;
    background: var(--c-bg); color: var(--c-fg);
}
.pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; margin-inline-end: 7px; }
.pill::after {
    content: ''; position: absolute; inset-inline-end: 9px; top: 50%;
    width: 5px; height: 5px; margin-top: -4px;
    border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
    transform: rotate(45deg); opacity: .6;
}
.pill select {
    appearance: none; border: 0; background: transparent; color: inherit;
    font: inherit; cursor: pointer; padding: 0; outline: none;
}
.pill select option { color: var(--ink); background: var(--surface); }
.pill:focus-within { box-shadow: 0 0 0 3px var(--focus); }
.pill-unpaid  { --c-bg: color-mix(in srgb, var(--ink) 7%, transparent); --c-fg: var(--ink-2); }
.pill-overdue { --c-bg: color-mix(in srgb, var(--warn) 14%, transparent); --c-fg: var(--warn); }
.pill-paid    { --c-bg: color-mix(in srgb, var(--ok) 13%, transparent); --c-fg: var(--ok); }
.pill-canceled{ --c-bg: transparent; --c-fg: var(--muted); box-shadow: inset 0 0 0 1px var(--line-strong); }

.empty { padding: 56px 24px; text-align: center; }
.empty-title { margin: 0 0 6px; font-size: 17px; font-weight: 600; }
.empty p { margin: 0 auto 20px; max-width: 380px; }

.list-foot { display: flex; justify-content: center; gap: 10px; margin-top: 24px; font-size: 13px; color: var(--muted); }

/* ==========================================================================
   Editor
   ========================================================================== */

.editor {
    display: grid;
    grid-template-columns: var(--panel-w) minmax(0, 1fr);
    min-height: calc(100vh - var(--topbar-h));
}

.panel {
    position: sticky; top: var(--topbar-h);
    height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
    padding: 20px 24px 0;
    border-inline-end: 1px solid var(--line);
    background: var(--surface);
    display: flex; flex-direction: column; gap: 0;
}

.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.back { font-size: 13px; color: var(--muted); text-decoration: none; }
.back:hover { color: var(--ink); }
.save-state { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.save-state::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--line-strong); }
.save-state[data-state="saved"]::before { background: var(--ok); }
.save-state[data-state="saving"]::before { background: var(--warn); }

.group { display: flex; flex-direction: column; gap: 12px; padding: 20px 0; border-bottom: 1px solid var(--line); }
.group h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--ink); }
.group-head { display: flex; align-items: baseline; justify-content: space-between; }

/* items */
.items { display: flex; flex-direction: column; gap: 10px; }
.item {
    display: flex; flex-direction: column; gap: 6px;
    padding: 10px; border: 1px solid var(--line); border-radius: var(--r);
    background: var(--surface-2);
}
.item-top { display: grid; grid-template-columns: minmax(0, 1fr) 30px; gap: 6px; align-items: center; }
.item .input { height: 34px; padding: 0 9px; }
.item textarea.input-sub {
    height: 30px; min-height: 30px; padding: 5px 9px; resize: none; overflow: hidden;
    font-size: 12.5px; color: var(--ink-2); border-style: dashed;
}
.item textarea.input-sub:focus { border-style: solid; }
.item-nums { display: grid; grid-template-columns: 72px minmax(0, 1fr) minmax(0, 1fr); gap: 6px; margin-top: 2px; }
.mini { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.mini > span { font-size: 11px; color: var(--muted); }
.item-amt {
    height: 34px; display: flex; align-items: center; justify-content: flex-end;
    font-size: 13px; font-weight: 500; color: var(--ink); white-space: nowrap; overflow: hidden;
}
.btn-add { align-self: flex-start; height: 32px; font-size: 13px; }
.adjust { margin-top: 4px; }

.from-group { gap: 0; }
.from-group summary {
    cursor: pointer; list-style: none; font-size: 13px; font-weight: 600;
    display: flex; align-items: center; justify-content: space-between;
}
.from-group summary::-webkit-details-marker { display: none; }
.from-group summary::after { content: '+'; font-family: var(--mono); color: var(--muted); font-weight: 400; }
.from-group[open] summary::after { content: '−'; }
.from-group[open] { gap: 12px; }

.panel-actions {
    position: sticky; bottom: 0;
    display: flex; gap: 8px; flex-wrap: wrap;
    margin: 0 -24px; padding: 14px 24px 18px;
    background: linear-gradient(to top, var(--surface) 75%, transparent);
    margin-top: auto;
}
.panel-actions .btn-primary { flex: 1; }
.panel-actions .btn-danger-text { margin-inline-start: auto; }

/* preview */
.preview {
    padding: 32px 32px 64px;
    display: flex; justify-content: center; align-items: flex-start;
    overflow-x: hidden;
}

/* ==========================================================================
   Settings dialog
   ========================================================================== */

.dialog {
    width: min(560px, calc(100vw - 32px));
    max-height: calc(100vh - 48px);
    padding: 0; border: 1px solid var(--line); border-radius: 12px;
    background: var(--surface); color: var(--ink);
    box-shadow: 0 24px 64px -12px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(20, 20, 18, .35); backdrop-filter: blur(2px); }
.dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 0 24px; }
.dialog-head {
    position: sticky; top: 0; z-index: 1;
    display: flex; align-items: center; justify-content: space-between;
    margin: 0 -24px; padding: 18px 24px 12px; background: var(--surface);
}
.dialog-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.dialog-actions {
    position: sticky; bottom: 0;
    display: flex; justify-content: flex-end; gap: 8px;
    margin: 4px -24px 0; padding: 14px 24px 18px;
    background: var(--surface); border-top: 1px solid var(--line);
}

.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
    width: 28px; height: 28px; padding: 0; border-radius: 50%;
    border: 2px solid var(--surface); background: var(--c);
    box-shadow: 0 0 0 1px var(--line-strong); cursor: pointer;
}
.swatch.on { box-shadow: 0 0 0 2px var(--ink); }
.swatch-custom { position: relative; display: grid; place-items: center; background: var(--surface-2); color: var(--muted); font-family: var(--mono); }
.swatch-custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.logo-row { display: flex; align-items: center; gap: 12px; }
.logo-preview {
    width: 96px; height: 48px; display: grid; place-items: center;
    border: 1px dashed var(--line-strong); border-radius: 6px; background: #fff; font-size: 12px;
}
.logo-preview img { max-width: 88px; max-height: 40px; object-fit: contain; }
.logo-row label.btn { cursor: pointer; }

/* toast */
.toast {
    position: fixed; bottom: 24px; left: 50%; z-index: 100;
    transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
    padding: 9px 16px; border-radius: 8px;
    background: var(--ink); color: var(--bg); font-size: 13px;
    transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 1080px) {
    :root { --panel-w: 400px; }
}

@media (max-width: 860px) {
    .editor { grid-template-columns: 1fr; }
    .panel { position: static; height: auto; border-inline-end: 0; border-bottom: 1px solid var(--line); }
    .preview { padding: 24px 16px 48px; }
    .list-tools { width: 100%; }
    .list-tools #search { flex: 1; width: auto; }
    .summary { grid-template-columns: 1fr; }
    .inv-table th:nth-child(3), .inv-table td:nth-child(3) { display: none; }
    .actions-col .icon-btn { opacity: 1; }
}

@media (max-width: 560px) {
    .topbar { padding: 0 16px; }
    .topnav { gap: 12px; }
    .topnav [data-nav] { display: none; }
    .list-view { padding: 24px 16px 48px; }
    .row-3 { grid-template-columns: 1fr 1fr; }
    .inv-table th, .inv-table td { padding-inline: 10px; }
    .inv-table th:nth-child(1), .inv-table td:nth-child(1) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}
