:root {
  --brand: #0d6efd;
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1f2734;
  --muted: #6b7686;
  --line: #e3e8f0;
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20,30,50,.08), 0 6px 24px rgba(20,30,50,.06);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px;
}
button { font-family: inherit; }
.hidden { display: none !important; }

/* ---------- Auth screens ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; background:
  radial-gradient(1200px 600px at 50% -10%, #e8f0ff, var(--bg)); }
.auth-card { width: 380px; background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); padding: 30px; }
.auth-card h1 { font-size: 20px; margin: 0 0 4px; }
.auth-card .sub { color: var(--muted); margin: 0 0 20px; font-size: 13px; }
.brand-badge { width: 52px; height: 52px; border-radius: 14px; background: var(--brand);
  color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 22px; margin-bottom: 16px; }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: 244px 1fr; height: 100vh; transition: grid-template-columns .18s ease; }
.shell.nav-collapsed { grid-template-columns: 64px 1fr; }
.sidebar { background: #101725; color: #cdd6e4; display: flex; flex-direction: column; overflow: hidden; }
.sidebar .logo { padding: 16px 14px 12px; display: flex; align-items: center; gap: 10px; }
.sidebar .logo b { color: #fff; font-size: 15px; }
.sidebar .logo small { color: #7c8aa5; display: block; font-size: 11px; }
.logo-text { flex: 1; min-width: 0; overflow: hidden; }
.collapse-btn { background: #1b2536; color: #9fb0cc; border: none; width: 26px; height: 26px; border-radius: 7px;
  cursor: pointer; font-size: 14px; line-height: 1; flex: none; }
.collapse-btn:hover { background: #26344a; color: #fff; }
.nav { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 8px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px;
  color: #cdd6e4; text-decoration: none; cursor: pointer; font-size: 14px; white-space: nowrap; }
.nav a:hover { background: #1b2536; }
.nav a.active { background: var(--brand); color: #fff; }
.nav .ico { width: 20px; text-align: center; flex: none; font-size: 15px; }
.nav-label { overflow: hidden; text-overflow: ellipsis; }

/* Collapsible module groups */
.nav-group-head { position: relative; }
.nav-chevron { margin-inline-start: auto; font-size: 11px; transition: transform .15s ease; color: #7c8aa5; }
.nav-group.open > .nav-group-head .nav-chevron { transform: rotate(180deg); }
.nav-group-body { max-height: 0; overflow: hidden; transition: max-height .2s ease; }
.nav-group.open > .nav-group-body { max-height: 520px; }
.nav a.nav-sub { padding-inline-start: 42px; font-size: 13px; color: #a9b6cc; }
.nav a.nav-sub.active { color: #fff; }

/* Collapsed rail: icons only, groups flattened */
.nav-collapsed .logo-text, .nav-collapsed .nav-label, .nav-collapsed .nav-chevron { display: none; }
.nav-collapsed .sidebar .logo { justify-content: center; padding: 16px 6px 12px; }
.nav-collapsed .collapse-btn { position: absolute; }
.nav-collapsed .nav a { justify-content: center; padding: 10px 0; }
.nav-collapsed .nav a.nav-sub { display: none; }
.nav-collapsed .nav-group-body { max-height: 0 !important; }

.sidebar .foot { padding: 12px; border-top: 1px solid #1b2536; }
.app-ver { color: #5b6b86; font-size: 11px; text-align: center; padding-top: 8px; }
.nav-collapsed .app-ver { display: none; }
.main { overflow-y: auto; }
.topbar { display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar h2 { margin: 0; font-size: 17px; }
.topbar .who { color: var(--muted); font-size: 13px; }
.content { padding: 24px; }
.content .panel { overflow-x: auto; }   /* wide tables scroll within the panel, never break layout */
.topbar { flex-wrap: wrap; gap: 8px; }

/* ---------- Cards / KPI ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.kpi { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .val { font-size: 26px; font-weight: 700; margin-top: 6px; }
.kpi.accent .val { color: var(--brand); }
.kpi.warn .val { color: var(--warn); }

.panel { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 4px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; }
.panel-head h3 { margin: 0; font-size: 15px; }

/* ---------- Tables ---------- */
table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { padding: 11px 14px; text-align: start; border-bottom: 1px solid var(--line); }
table.grid th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .02em; }
table.grid tbody tr:hover { background: #f8fafd; }
table.grid td.num, table.grid th.num { text-align: end; font-variant-numeric: tabular-nums; }
.grp-row td { background: #f0f4fa; font-weight: 700; }

/* ---------- Forms / buttons ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 9px 16px; border-radius: 9px; cursor: pointer; font-size: 14px; }
.btn:hover { background: #f2f5fa; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 6px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); border-color: #f2c4c4; }
.inp, select.inp { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px;
  font-size: 14px; background: #fff; color: var(--ink); }
.inp:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(13,110,253,.12); }
.field { display: block; margin-bottom: 12px; }
.field-label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 5px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.gap { flex-wrap: wrap; }
.row.gap > * { flex: 1; min-width: 180px; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.badge { padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge.cust { background: #e6f0ff; color: #1d4ed8; }
.badge.vend { background: #fef3c7; color: #92400e; }

/* ---------- Modal / toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(15,23,37,.45); display: grid; place-items: center; z-index: 50; }
.modal { width: 520px; max-width: 94vw; max-height: 90vh; overflow: auto; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); }
.modal.modal-wide { width: 820px; }
/* Line-item tables inside forms scroll within their own box, never break the modal width. */
.modal .grid-scroll { overflow-x: auto; }
.modal table.grid { min-width: 100%; }
.modal-head { padding: 16px 20px; font-size: 16px; font-weight: 700; border-bottom: 1px solid var(--line); }
.modal-body { padding: 20px; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 10px; }
.toast { position: fixed; bottom: 24px; inset-inline-start: 50%; transform: translateX(-50%) translateY(20px);
  background: #101725; color: #fff; padding: 12px 20px; border-radius: 10px; opacity: 0; transition: all .3s; z-index: 60; }
[dir="rtl"] .toast { transform: translateX(50%) translateY(20px); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
[dir="rtl"] .toast.show { transform: translateX(50%) translateY(0); }
.toast.ok { border-inline-start: 4px solid var(--ok); }
.toast.err { border-inline-start: 4px solid var(--danger); }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 48px 20px; }

/* ---------- Kanban (CRM pipeline) ---------- */
.kanban { display: flex; gap: 14px; align-items: flex-start; overflow-x: auto; padding-bottom: 8px; }
.kanban-col { flex: 1 0 220px; min-width: 220px; background: #eef2f8; border-radius: var(--radius); padding: 10px; }
.kanban-head { display: flex; align-items: center; justify-content: space-between; font-weight: 700; font-size: 13px; padding: 2px 4px; }
.kanban-count { background: #d6deea; color: #43506a; border-radius: 20px; padding: 1px 9px; font-size: 12px; }
.kanban-sum { color: var(--muted); font-size: 12px; padding: 0 4px 8px; }
.kanban-card { background: var(--panel); border-radius: 10px; box-shadow: var(--shadow); padding: 11px; margin-bottom: 10px; }
.kanban-title { font-weight: 600; font-size: 14px; }
.kanban-stage { font-size: 12px; padding: 5px 8px; }

/* ---------- Full-screen invoice editor ---------- */
.inv-bottom { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 14px; align-items: start; }
@media (max-width: 980px) { .inv-bottom { grid-template-columns: 1fr; } }
.inv-totals { font-size: 14px; }
.inv-trow { display: flex; justify-content: space-between; padding: 7px 4px; border-bottom: 1px solid var(--line); }
.inv-trow.grand { border-top: 2px solid var(--brand); border-bottom: none; font-size: 18px; padding-top: 10px; background: #f3f6fb; }
.inv-trow.due b { color: var(--danger); }

/* ---------- Brand + cockpit ---------- */
.brand-logo { display: inline-flex; line-height: 0; }
.auth-card .brand-logo { margin-bottom: 16px; }
.cockpit-row { display: grid; gap: 16px; margin-top: 16px; }
@media (max-width: 980px) { .cockpit-row { grid-template-columns: 1fr !important; } }
.chart-legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.chart-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 5px; vertical-align: middle; }
.cockpit-row .panel table.grid td, .cockpit-row .panel table.grid th { padding: 8px 12px; }
