:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1d2433;
  --muted: #6b7385;
  --line: #e4e7ec;
  --primary: #1f5fae;
  --primary-soft: #e8f0fb;
  --side: #14233c;
  --side-text: #c6d0e0;
  --pos: #1a7f4b;
  --neg: #c0392b;
  --rev: #2e7dd7;
  --exp: #e08a3c;
  --radius: 10px;
}
* { box-sizing: border-box; }
body {
  margin: 0; display: flex; min-height: 100vh; background: var(--bg); color: var(--text);
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", "Geeza Pro", Arial, sans-serif; font-size: 15px;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code { background: #eef0f4; padding: 1px 6px; border-radius: 4px; direction: ltr; display: inline-block; }

/* الاتجاه: الأرقام تُحاذى لنهاية السطر (يسار في العربي، يمين في الإنجليزي) */
[dir=rtl] .num, [dir=rtl] .pct { text-align: left !important; }
[dir=ltr] .num, [dir=ltr] .pct { text-align: right !important; }
[dir=rtl] .kpi-value { text-align: right; }
[dir=ltr] .kpi-value { text-align: left; }
[dir=ltr] body { font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif; }

/* القائمة الجانبية */
.sidebar { width: 230px; background: var(--side); color: var(--side-text); padding: 18px 12px; flex-shrink: 0; }
.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 18px; }
.logo { width: 38px; height: 38px; border-radius: 9px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 20px; }
.brand-name { color: #fff; font-weight: 700; font-size: 17px; }
.brand-sub { font-size: 12px; opacity: .75; }
.nav-label { font-size: 11px; text-transform: uppercase; opacity: .55; padding: 14px 10px 6px; }
.sidebar nav a { display: block; color: var(--side-text); padding: 8px 10px; border-radius: 7px; margin-bottom: 2px; }
.sidebar nav a:hover { background: rgba(255,255,255,.07); text-decoration: none; }
.sidebar nav a.active { background: var(--primary); color: #fff; }
.lang-switch { margin-top: 18px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,.1); }
.lang-switch a { display: block; color: #fff; padding: 8px 10px; border-radius: 7px; background: rgba(255,255,255,.08); text-align: center; }
.lang-switch a:hover { background: rgba(255,255,255,.15); text-decoration: none; }

.main { flex: 1; padding: 24px 28px; max-width: 1200px; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 16px 0 6px; color: var(--primary); }
.muted { color: var(--muted); font-weight: normal; }
.small { font-size: 13px; }
.actions { display: flex; gap: 8px; align-items: center; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card.hint { background: var(--primary-soft); border-color: #c9daf3; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-2 > .card { margin-bottom: 0; }

/* الأزرار */
.btn { display: inline-block; background: var(--primary); color: #fff; border: 1px solid var(--primary); padding: 8px 16px; border-radius: 8px; cursor: pointer; font: inherit; }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn-ghost { background: #fff; color: var(--text); border-color: var(--line); }
.btn-danger { background: #fff; color: var(--neg); border-color: #f0c4bf; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 20px; cursor: pointer; line-height: 1; }
.icon-btn:hover { color: var(--neg); }

/* التنبيهات */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; }
.flash-success { background: #e6f5ec; color: var(--pos); border: 1px solid #bfe3cd; }
.flash-error { background: #fdecea; color: var(--neg); border: 1px solid #f5c6c0; }
.status { font-weight: 600; margin: 8px 0 0; }
.status.ok { color: var(--pos); }
.status.bad { color: var(--neg); }

/* الجداول */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: start; font-weight: 600; color: var(--muted); font-size: 13px; border-bottom: 2px solid var(--line); padding: 8px 10px; }
.table td { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.table.hover tbody tr { cursor: pointer; }
.table.hover tbody tr:hover { background: #f8fafc; }
.num { direction: ltr; font-variant-numeric: tabular-nums; white-space: nowrap; }
.center { text-align: center !important; }
.pct { color: var(--muted); font-size: 13px; width: 70px; direction: ltr; }
.code { color: var(--muted); font-variant-numeric: tabular-nums; width: 80px; white-space: nowrap; }
.group-row td { background: #f8f9fb; }
.inactive td { opacity: .5; }
.total-row td, .subtotal td { font-weight: 700; border-top: 2px solid var(--line); }
.grand td { font-weight: 700; font-size: 16px; background: var(--primary-soft); border-bottom: 0; }
.grand.pos td { color: var(--pos); }
.grand.neg td { color: var(--neg); }
.row-actions { width: 60px; text-align: end; font-size: 13px; }
.tag { font-size: 11px; background: #eee; padding: 1px 6px; border-radius: 4px; margin-inline-start: 6px; }
.type { font-size: 12px; padding: 2px 8px; border-radius: 20px; }
.type-asset { background: #e8f0fb; color: #1f5fae; }
.type-liability { background: #fdeee4; color: #b4581b; }
.type-equity { background: #efe8fb; color: #6a3fb5; }
.type-revenue { background: #e6f5ec; color: #1a7f4b; }
.type-expense { background: #fdecea; color: #c0392b; }

/* النماذج */
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-bottom: 14px; }
.span-2 { grid-column: span 2; }
input, select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: #fff; color: var(--text); width: 100%; }
input:focus, select:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); margin-bottom: 8px; }
.form label.check input { width: auto; }
.form-actions { display: flex; gap: 8px; margin-top: 16px; }
.lines td { padding: 5px 4px; }
.filters { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.filters label { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }
.filters input, .filters select { width: auto; }
.filters input[type=search] { min-width: 260px; }
.meta { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }

/* لوحة التحكم */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.kpi-label { color: var(--muted); font-size: 13px; }
.kpi-value { font-size: 24px; font-weight: 700; margin-top: 6px; direction: ltr; font-variant-numeric: tabular-nums; }
.kpi-value.pos { color: var(--pos); } .kpi-value.neg { color: var(--neg); }
.kpi-unit { color: var(--muted); font-size: 12px; }
.legend { font-size: 12px; color: var(--muted); margin-bottom: 8px; display: flex; gap: 6px; align-items: center; }
.dot { width: 10px; height: 10px; border-radius: 2px; display: inline-block; margin-inline-start: 8px; }
.dot-rev, .bar-rev { background: var(--rev); }
.dot-exp, .bar-exp { background: var(--exp); }
.bars { display: flex; gap: 6px; height: 200px; align-items: stretch; }
.bar-group { flex: 1; display: flex; flex-direction: column; }
.bar-pair { flex: 1; display: flex; gap: 2px; align-items: flex-end; border-bottom: 1px solid var(--line); }
.bar { flex: 1; border-radius: 3px 3px 0 0; min-height: 1px; }
.bar-label { text-align: center; font-size: 11px; color: var(--muted); padding-top: 4px; }

@media (max-width: 900px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; }
  .sidebar nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .nav-label { display: none; }
  .kpis, .grid-2 { grid-template-columns: 1fr 1fr; }
}
@media print {
  .sidebar, .no-print, .flash { display: none !important; }
  .main { padding: 0; max-width: none; }
  .card { border: 0; padding: 0; }
  body { background: #fff; }
}

/* ===================================================== v0.2: المبيعات والمشتريات */
.sidebar { overflow-y: auto; height: 100vh; position: sticky; top: 0; }
.sidebar nav a { padding: 6px 10px; font-size: 14px; }
.nav-label { padding: 12px 10px 4px; }
.neg { color: var(--neg) !important; }
.pos { color: var(--pos) !important; }
.ltr-cell { direction: ltr; unicode-bidi: isolate; }
.small-value { font-size: 17px !important; direction: inherit !important; text-align: start !important; }
.kpi-link { color: inherit; display: block; }
.kpi-link:hover { border-color: var(--primary); text-decoration: none; }
.kpis + .kpis { margin-top: -4px; }

.badge { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 20px; font-weight: 600; margin-inline-start: 6px; vertical-align: middle; }
.badge-paid { background: #e6f5ec; color: var(--pos); }
.badge-partial { background: #fff4e0; color: #b4741b; }
.badge-unpaid { background: #eef0f4; color: var(--muted); }
.badge-overdue { background: #fdecea; color: var(--neg); }

.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 16px; }
.grid-3 > .card { margin-bottom: 0; }
.highlight .hl-name { font-size: 18px; font-weight: 700; margin: 6px 0 2px; }
.hbar { height: 6px; background: #eef0f4; border-radius: 3px; overflow: hidden; margin-bottom: 2px; }
.hbar-fill { height: 100%; background: var(--rev); border-radius: 3px; }

.section-row td { font-weight: 700; color: var(--primary); background: #fafbfc; padding-top: 14px; }
.ratios { display: flex; gap: 14px; margin-top: 16px; flex-wrap: wrap; }
.ratios > div { flex: 1; min-width: 160px; background: var(--primary-soft); border-radius: 8px; padding: 10px 14px; display: flex; flex-direction: column; gap: 4px; }
.ratios strong { font-size: 20px; }

.warn-row td { background: #fff8f7; }
.warn-card { border-color: #f5c6c0; }
.flash-info { background: var(--primary-soft); color: var(--primary); border: 1px solid #c9daf3; }
.stock-hint { margin-top: 2px; min-height: 14px; font-size: 12px; }

/* الفاتورة والسند (قابلة للطباعة) */
.invoice-doc .inv-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--primary); padding-bottom: 12px; margin-bottom: 12px; }
.inv-company { font-size: 20px; font-weight: 700; }
.inv-title { font-size: 15px; color: var(--primary); margin-top: 2px; }
.inv-meta { text-align: end; line-height: 1.8; }
.inv-party { padding: 8px 0 14px; }
.voucher td:first-child { width: 180px; }
.big-num { font-size: 20px; }
.signatures { display: flex; justify-content: space-around; margin-top: 50px; color: var(--muted); }
.signatures div { border-top: 1px solid var(--line); padding-top: 6px; min-width: 160px; text-align: center; }
@media (max-width: 900px) { .grid-3 { grid-template-columns: 1fr; } }
@media print { .invoice-doc { padding: 0; } .sidebar { display: none !important; } }

/* ===================================================== v0.3: الموارد البشرية */
.table-scroll { overflow-x: auto; }
.payroll-table td:first-child { white-space: nowrap; }
.payroll-table input { min-width: 80px; }
.rules p { margin: 3px 0; }
.payslip .grid-2 { gap: 12px; }
.payslip .grid-2 .table { align-self: start; }

/* ===================================================== v0.4: تسجيل الدخول + الجوال */
.auth-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; background: var(--side); }
.auth-box { background: var(--card); border-radius: 14px; padding: 28px; width: 100%; max-width: 400px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.auth-brand { padding: 0 0 18px; }
.auth-brand .brand-name { color: var(--text); }
.auth-brand .brand-sub { color: var(--muted); opacity: 1; }
.auth-box .form label { margin-bottom: 12px; }
.auth-box h2 { margin-bottom: 14px; }
.auth-lang { text-align: center; margin-top: 16px; font-size: 14px; }
.btn-block { width: 100%; padding: 11px; font-size: 16px; margin-top: 6px; }
.user-box { margin-top: 14px; padding: 10px; border-radius: 8px; background: rgba(255,255,255,.06); font-size: 13px; }
.user-name { color: #fff; margin-bottom: 4px; }
.user-links a { color: var(--side-text); }
.mobile-bar, .menu-overlay { display: none; }

@media (max-width: 900px) {
  body { display: block; }
  .mobile-bar { display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 30;
                background: var(--side); color: #fff; padding: 10px 14px; }
  .menu-btn { background: none; border: 0; color: #fff; font-size: 24px; cursor: pointer; line-height: 1; }
  .mobile-title { font-weight: 700; font-size: 17px; }
  .sidebar { position: fixed; top: 0; bottom: 0; inset-inline-start: 0; width: 270px; height: 100vh; z-index: 50;
             transition: transform .25s ease; }
  [dir=rtl] .sidebar { transform: translateX(100%); }
  [dir=ltr] .sidebar { transform: translateX(-100%); }
  body.menu-open .sidebar { transform: none; }
  body.menu-open .menu-overlay { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; }
  .sidebar nav { display: block; }
  .nav-label { display: block; }
  .main { padding: 14px; max-width: 100%; }
  .card { overflow-x: auto; padding: 14px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi-value { font-size: 19px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .page-head h1 { font-size: 19px; }
  .filters input, .filters select { max-width: 100%; }
  .filters input[type=search] { min-width: 0; width: 100%; }
  .span-2 { grid-column: auto; }
  .table td, .table th { padding: 7px 6px; }
}
@media print { .mobile-bar, .menu-overlay, .user-box { display: none !important; } }

/* ===================================================== v0.5: الصلاحيات */
.user-card-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 4px 16px; margin: 6px 0 10px; }
.perm-form .check, .perm-grid .check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.perm-form .check input, .perm-grid .check input { width: auto; }
.user-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.user-actions form { display: flex; gap: 6px; }
.user-actions input { width: 170px; }
.table-wrap { overflow-x: auto; }
.nowrap { white-space: nowrap; }
.badge { white-space: nowrap; }

/* v0.7: الرسوم البيانية والموازنة */
.chart-box { direction: ltr; }
.chart-box .legend { flex-wrap: wrap; gap: 14px; direction: rtl; }
[dir=ltr] .chart-box .legend { direction: ltr; }
.chart-box .legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-box .legend i { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }
.chart-box .legend i.line { height: 0; border-top: 3px solid; border-radius: 0; width: 18px; }
.chart-box .legend i.line.dash { border-top-style: dashed; }
.hs-chart { width: 100%; height: auto; display: block; }
.hs-chart .grid { stroke: #eef0f4; } .hs-chart .axis0 { stroke: #b9c0cc; }
.hs-chart .tick { font-size: 11px; fill: var(--muted); font-family: inherit; }
.budget-grid input { width: 82px; padding: 4px 6px; font-size: 13px; text-align: end; direction: ltr; }
.budget-grid td, .budget-grid th { padding: 4px 6px; font-size: 13px; }
.budget-grid .acc-name { white-space: nowrap; min-width: 170px; position: sticky; inset-inline-start: 0; background: var(--card); z-index: 1; }
.budget-grid tr.section td { background: var(--primary-soft); font-weight: 700; }
.budget-grid tr.sum td { font-weight: 700; border-top: 2px solid var(--line); }
.budget-grid tr.profit td { background: #f1f8f4; font-weight: 700; }
.var-good { color: var(--pos); } .var-bad { color: var(--neg); }
.progress { height: 8px; background: #eef0f4; border-radius: 4px; overflow: hidden; min-width: 70px; }
.progress > div { height: 100%; background: var(--rev); }
.progress > div.over { background: var(--neg); }
.progress > div.good { background: var(--pos); }
@media print { .budget-grid input { border: none; width: 70px; } }
.insights { margin: 0; padding: 0; list-style: none; }
.insights li { padding: 8px 12px; border-radius: 8px; margin-bottom: 6px; font-size: 15px; border-inline-start: 4px solid; }
.ins-good { background: #eef8f2; border-color: var(--pos) !important; }
.ins-bad { background: #fdf0ee; border-color: var(--neg) !important; }
.ins-warn { background: #fff7e8; border-color: #e0a030 !important; }
.ins-info { background: var(--primary-soft); border-color: var(--primary) !important; }
.summary-table td, .summary-table th { font-size: 15px; }
.hb-row { display: grid; grid-template-columns: 150px 1fr 110px; gap: 10px; align-items: center; padding: 5px 0; border-bottom: 1px solid #f0f2f5; }
.hb-label { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hb-bars .hb { height: 8px; border-radius: 4px; margin: 2px 0; min-width: 1px; }
.hb.plan { background: #c9d3e0; } .hb.act { background: var(--rev); } .hb.act.over { background: var(--neg); }
.hb-val { text-align: end; direction: ltr; white-space: nowrap; }
.legend-inline { margin-bottom: 6px; } .legend-inline .sw { display: inline-block; width: 12px; height: 8px; border-radius: 3px; margin-inline-start: 10px; }
.sw-plan { background: #c9d3e0; } .sw-act { background: var(--rev); } .sw-over { background: var(--neg); }
details.card > summary { cursor: pointer; list-style-position: inside; margin-bottom: 10px; }
@media print { details.card { display: block; } details.card > summary { list-style: none; } }
/* v0.8 */
.logo-img { object-fit: contain; background: #fff; padding: 2px; }
.doc-logo { max-height: 54px; max-width: 160px; display: block; margin-bottom: 6px; }
.logo-row { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.logo-preview { max-height: 70px; max-width: 200px; border: 1px solid var(--line); border-radius: 8px; padding: 4px; background: #fff; }
.step { display: flex; gap: 14px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.step:last-child { border-bottom: none; }
.step-n { width: 30px; height: 30px; border-radius: 50%; background: var(--primary); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex: none; }
.step-n.done { background: var(--pos); }
.step-body { flex: 1; } .step-body h3 { margin: 2px 0 6px; }
.count-grid input { width: 100px; text-align: end; direction: ltr; }
.diff-pos { color: var(--pos); font-weight: 600; } .diff-neg { color: var(--neg); font-weight: 600; }
.inline-form { margin: 0; gap: 6px; flex-wrap: nowrap; } .inline-form input, .inline-form select { padding: 4px 6px; font-size: 13px; }
.input-error { border-color: var(--neg) !important; background: #fdecea !important; }
.theme-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.theme-swatch { display: flex; flex-direction: column; align-items: center; gap: 6px; background: var(--card); border: 2px solid var(--line); border-radius: 10px; padding: 8px 10px; cursor: pointer; font: inherit; font-size: 13px; color: var(--text); }
.theme-swatch.current { border-color: var(--primary); }
.theme-swatch .sw-side { width: 64px; height: 40px; border-radius: 6px; display: flex; align-items: flex-end; padding: 6px; }
.theme-swatch .sw-pri { width: 30px; height: 10px; border-radius: 3px; }
input[type=color] { width: 46px; height: 32px; padding: 2px; vertical-align: middle; }
