:root {
  --bg: #f3f4f6; --card: #ffffff; --text: #111827; --muted: #6b7280; --line: #e5e7eb; --line-2: #d1d5db;
  --primary: #0f5a3f; --primary-2: #0b4530; --primary-soft: #e6f2ec; --gold: #c9a24a;
  --danger: #b91c1c; --danger-soft: #fee2e2; --warn: #b45309; --warn-soft: #fef3c7;
  --ok: #15803d; --ok-soft: #dcfce7; --info: #1d4ed8; --info-soft: #dbeafe;
  --radius: 4px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: 'Alexandria', 'Segoe UI', Tahoma, Arial, sans-serif; font-size: 14px; line-height: 1.5; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h2 { font-size: 20px; margin: 0; font-weight: 600; }
#app { display: grid; grid-template-columns: 230px minmax(0, 1fr); grid-template-rows: auto 1fr; min-height: 100vh; }

/* الشريط الجانبي */
.sidebar { grid-row: 1 / span 2; background: linear-gradient(180deg, #021b14 0%, #082a1f 100%); color: #cfe0d7; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { background: #021b14; padding: 14px 18px 10px; border-bottom: 1px solid rgba(255,255,255,.08); text-align: center; }
.brand-logo { width: 100%; max-width: 170px; height: auto; display: block; margin: 0 auto; }
nav { flex: 1; padding: 10px 12px; overflow-y: auto; }
nav a { display: flex; align-items: center; gap: 10px; padding: 8px 12px; margin: 1px 0; border-radius: 6px; color: #cfe0d7; font-size: 13.5px; position: relative; }
nav a .ico { width: 18px; height: 18px; flex-shrink: 0; opacity: .8; }
nav a:hover { background: rgba(255,255,255,.06); color: #fff; text-decoration: none; }
nav a.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
nav a.active .ico { opacity: 1; }
nav a.active::before { content: ''; position: absolute; inset-inline-start: -12px; top: 8px; bottom: 8px; width: 3px; border-radius: 0 3px 3px 0; background: var(--gold); }
nav .nav-sep { font-size: 10.5px; color: #86a695; padding: 16px 12px 6px; font-weight: 600; letter-spacing: .04em; }
nav .nav-sep:first-child { padding-top: 4px; }
.sidebar-footer { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-top: 1px solid rgba(255,255,255,.08); }
.user-box { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--gold); color: #082a1f; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.user-meta { min-width: 0; }
.user-meta b { display: block; color: #fff; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta span { color: #9dbcab; font-size: 11.5px; }
.icon-btn { background: transparent; border: 1px solid transparent; color: inherit; width: 34px; height: 34px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; }
.icon-btn svg { width: 18px; height: 18px; }
.sidebar .icon-btn { color: #9dbcab; }
.sidebar .icon-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.topbar .icon-btn { color: var(--text); border-color: var(--line-2); }
.backdrop { display: none; position: fixed; inset: 0; background: rgba(17,24,39,.45); z-index: 30; }
.backdrop.show { display: block; }

.topbar { background: var(--card); border-bottom: 1px solid var(--line); padding: 10px 24px; display: flex; align-items: center; gap: 12px; font-weight: 600; font-size: 15px; }
#menu-btn { display: none; }
main { padding: 22px 24px; max-width: 1400px; width: 100%; min-width: 0; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }

/* بطاقات ولوحات */
.card, .panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.card h3 { margin: 0 0 12px; font-size: 15px; font-weight: 600; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.panel { padding: 0; }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 14px; font-weight: 600; }
.panel-head a { font-size: 13px; }
.panel .table-wrap { padding: 0; }
.panel .empty { padding: 20px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid > .card, .grid > .panel { margin-bottom: 0; }
.grid.grid-2 { margin-bottom: 16px; }

/* مؤشرات رئيسية */
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 16px; }
.kpi-row.kpi-3 { grid-template-columns: repeat(3, 1fr); }
.kpi { padding: 14px 18px; border-inline-end: 1px solid var(--line); }
.kpi:last-child { border-inline-end: 0; }
.kpi-label { color: var(--muted); font-size: 12px; }
.kpi-value { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; direction: ltr; text-align: right; margin-top: 2px; }
.kpi-sub { color: var(--muted); font-size: 12px; }

/* بطاقات إحصائية (بقية الصفحات) */
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 20px; font-weight: 600; margin-top: 2px; }
.stat .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stat.danger .value { color: var(--danger); } .stat.warn .value { color: var(--warn); }

/* الجداول */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: right; vertical-align: middle; }
th { background: #f9fafb; color: var(--muted); font-weight: 600; font-size: 12px; white-space: nowrap; }
tbody tr:hover td { background: #fafafa; }
tfoot td { font-weight: 600; background: #f9fafb; border-top: 1px solid var(--line-2); }
td.num, th.num { text-align: left; font-variant-numeric: tabular-nums; direction: ltr; white-space: nowrap; }
.table-wrap { overflow-x: auto; }
table.plain tbody tr:last-child td { border-bottom: 0; }
table.plain tfoot td { border-bottom: 0; }
.kv-table td { padding: 8px 16px; }
.kv-table td:first-child { color: var(--muted); }
.kv-table tr:last-child td { border-bottom: 0; }
.sheet th, .sheet td { padding: 6px 8px; white-space: nowrap; }
.sheet .m0 { color: #c4c8cf; }
.sheet .sticky { position: sticky; right: 0; background: #fff; z-index: 1; }
.sheet td.cell-pay { cursor: pointer; }
.sheet td.cell-pay:hover { background: var(--primary-soft); }

/* الأزرار */
.btn { border: 1px solid var(--primary); background: var(--primary); color: #fff; padding: 7px 14px; border-radius: var(--radius); cursor: pointer; font-family: inherit; font-size: 13.5px; font-weight: 500; line-height: 1.4; }
.btn:hover { background: var(--primary-2); text-decoration: none; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--line-2); }
.btn-ghost:hover { background: #f3f4f6; }
.btn-danger { background: var(--danger); border-color: var(--danger); } .btn-danger:hover { background: #991b1b; }
.btn-outline { background: #fff; color: var(--primary); border-color: var(--primary); } .btn-outline:hover { background: var(--primary-soft); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* النماذج */
label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
input, select, textarea { width: 100%; padding: 7px 10px; border: 1px solid var(--line-2); border-radius: var(--radius); font-family: inherit; font-size: 13.5px; background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
input[type=number] { direction: ltr; text-align: right; }
.field { margin-bottom: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }
.hint { font-size: 12px; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.check input { width: auto; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.filters .field { margin: 0; min-width: 160px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 3px; font-size: 12px; font-weight: 500; }
.badge-ok { background: var(--ok-soft); color: var(--ok); } .badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); } .badge-info { background: var(--info-soft); color: var(--info); }
.badge-muted { background: #f3f4f6; color: var(--muted); }
.money { font-variant-numeric: tabular-nums; direction: ltr; display: inline-block; }
.pos { color: var(--ok); } .neg { color: var(--danger); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.empty { text-align: center; color: var(--muted); padding: 28px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 13.5px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; font-weight: 500; }
.tree ul { list-style: none; padding-inline-start: 22px; margin: 4px 0; border-inline-start: 1px solid var(--line-2); }
.tree li { padding: 4px 0; }
.tree .node { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tree .node .btn { padding: 2px 8px; font-size: 12px; }
.voided td { text-decoration: line-through; color: var(--muted); background: #fff7f7; }
.alert { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; font-size: 13.5px; border: 1px solid transparent; }
.alert-danger { background: var(--danger-soft); color: #991b1b; border-color: #fecaca; } .alert-warn { background: var(--warn-soft); color: #92400e; border-color: #fde68a; } .alert-ok { background: var(--ok-soft); color: #166534; border-color: #bbf7d0; }

/* النوافذ المنبثقة */
.modal-bg { position: fixed; inset: 0; background: rgba(17,24,39,.5); display: flex; align-items: flex-start; justify-content: center; padding: 40px 12px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: 6px; width: 100%; max-width: 640px; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.2); }
.modal.wide { max-width: 900px; }
.modal h3 { margin: 0 0 14px; font-size: 16px; font-weight: 600; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.modal-err { color: var(--danger); font-size: 13px; margin-top: 8px; min-height: 18px; }
#toast-root { position: fixed; bottom: 20px; left: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #1f2937; color: #fff; padding: 10px 16px; border-radius: var(--radius); animation: fade .2s; font-size: 13.5px; }
.toast.err { background: var(--danger); } .toast.ok { background: var(--ok); }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

/* تسجيل الدخول */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.login-card { background: #fff; padding: 32px; border: 1px solid var(--line); border-radius: 6px; width: 100%; max-width: 380px; }
.login-logo { display: block; width: 190px; margin: 0 auto 4px; }
.scope-bar { background: var(--primary-soft); border: 1px solid #cfe3d8; color: var(--primary); padding: 8px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 13px; }
.login-card p { text-align: center; color: var(--muted); margin: 4px 0 20px; font-size: 13px; }

@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; top: 0; bottom: 0; right: 0; left: auto; width: 260px; height: 100vh; transform: translateX(100%); transition: transform .2s; z-index: 40; box-shadow: -4px 0 20px rgba(0,0,0,.2); }
  .sidebar.open { transform: none; }
  #menu-btn { display: inline-block; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; }
  .kpi-row, .kpi-row.kpi-3 { grid-template-columns: 1fr 1fr; }
  .kpi:nth-child(2n) { border-inline-end: 0; } .kpi:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .form-grid { grid-template-columns: 1fr; }
  main { padding: 14px; }
}
@media (max-width: 560px) { .grid-2, .grid-3, .grid-4, .kpi-row, .kpi-row.kpi-3 { grid-template-columns: 1fr; } .kpi { border-inline-end: 0; border-bottom: 1px solid var(--line); } .kpi:last-child { border-bottom: 0; } }
@media print { .sidebar, .topbar, .btn, .filters { display: none !important; } #app { grid-template-columns: 1fr; } main { padding: 0; } .card, .panel { border: 0; } }

/* المستندات القابلة للطباعة (إيصالات وكشوفات) */
.doc { position: relative; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 22px 26px; margin: 0 auto 16px; }
.doc-a5 { max-width: 560px; }
.doc-a4 { max-width: 1000px; }
.doc-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; border-bottom: 2px solid var(--gold); padding-bottom: 12px; margin-bottom: 12px; }
.doc-logo { width: 76px; height: auto; }
.doc-org b { font-size: 16px; color: var(--primary); }
.doc-no { text-align: center; font-size: 11.5px; color: var(--muted); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 14px; }
.doc-no b { display: block; font-size: 15px; color: var(--text); font-variant-numeric: tabular-nums; direction: ltr; }
.doc-title { text-align: center; font-size: 17px; font-weight: 700; color: var(--primary); margin: 4px 0 12px; }
.doc-kv { width: 100%; }
.doc-kv td { padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.doc-kv tr:last-child td { border-bottom: 0; }
.doc-loanbox { background: #f9fafb; border: 1px solid var(--line); border-radius: var(--radius); margin-top: 10px; }
.doc-amount { display: flex; justify-content: space-between; align-items: center; gap: 14px; background: var(--primary-soft); border: 1px solid #cfe3d8; border-radius: var(--radius); padding: 10px 16px; margin: 12px 0 4px; }
.doc-amount .val { font-size: 24px; font-weight: 700; color: var(--primary); }
.doc-words { font-size: 13px; color: var(--primary); text-align: start; }
.doc-sign { display: flex; justify-content: space-between; gap: 30px; margin-top: 40px; }
.doc-sign div { text-align: center; width: 42%; border-top: 1px dashed var(--line-2); padding-top: 6px; color: var(--muted); font-size: 13px; }
.doc-foot { margin-top: 16px; font-size: 11px; color: var(--muted); text-align: center; border-top: 1px solid var(--line); padding-top: 8px; }
.void-stamp { display: inline-block; color: var(--danger); border: 2px solid var(--danger); border-radius: 4px; padding: 0 12px; font-size: 14px; transform: rotate(-6deg); margin-inline-start: 10px; }
@media print {
  body { background: #fff; }
  .no-print, #toast-root, .backdrop { display: none !important; }
  .doc { border: 0; border-radius: 0; padding: 0; margin: 0; max-width: none; }
  .doc-head { break-inside: avoid; }
  .doc tr { break-inside: avoid; }
}
