:root {
  --bg: #f3f5f9;
  --panel: #ffffff;
  --text: #1c2434;
  --muted: #6b7486;
  --line: #e3e7ef;
  --primary: #1456b8;
  --primary-2: #0d3f8a;
  --primary-soft: #e8f0fc;
  --green: #138a52;
  --green-soft: #e3f5ec;
  --red: #c8332f;
  --red-soft: #fbe9e8;
  --amber: #b86e00;
  --amber-soft: #fdf2df;
  --side: #0f2747;
  --side-text: #c9d6ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 30, 60, .06), 0 2px 8px rgba(16, 30, 60, .05);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Tajawal', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; direction: rtl;
}
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ── الدخول ── */
#login {
  min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(135deg, #0f2747, #1456b8);
}
.login-card { background: var(--panel); border-radius: 14px; padding: 28px; width: 100%; max-width: 380px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }

/* ── الهيكل ── */
#app { display: flex; min-height: 100%; }
#side {
  width: 230px; flex-shrink: 0; background: var(--side); color: var(--side-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
#side .brand { padding: 18px 16px; font-weight: 800; font-size: 17px; color: #fff; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-logo { display: block; width: 100%; max-height: 110px; object-fit: contain; background: #fff; border-radius: 10px; padding: 6px; margin-bottom: 10px; }
.login-logo { display: block; max-width: 220px; max-height: 150px; margin: 0 auto 14px; object-fit: contain; }
.logo-preview { max-width: 260px; max-height: 160px; border: 1px solid var(--line); border-radius: 8px; padding: 6px; background: #fff; display: block; margin-bottom: 10px; }
#side .brand small { display: block; font-weight: 400; font-size: 12px; color: var(--side-text); opacity: .8; }
#side nav { padding: 8px; flex: 1; }
#side nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; color: var(--side-text); margin-bottom: 2px;
}
#side nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
#side nav a.active { background: var(--primary); color: #fff; }
#side nav a .ico { width: 20px; text-align: center; }
#side nav a .badge { margin-inline-start: auto; }
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#top {
  display: flex; align-items: center; gap: 12px; padding: 12px 20px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 50;
}
#top h2 { margin: 0; font-size: 18px; flex: 1; }
#menuBtn { display: none; }
#page { padding: 20px; flex: 1; }
.badge { background: var(--red); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 7px; min-width: 20px; text-align: center; }
.bell { position: relative; }
.bell .badge { position: absolute; top: -4px; left: -4px; }

/* ── عناصر ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; line-height: 1.2;
}
.btn:hover { border-color: #c5cddc; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.danger { color: var(--red); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.icon { padding: 6px 10px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.grid { display: grid; gap: 12px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card h3 { margin: 0 0 12px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.toolbar { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .field { margin: 0; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; cursor: pointer; margin: 0; }
.check input { width: 18px; height: 18px; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.kpi .t { color: var(--muted); font-size: 13px; }
.kpi .v { font-size: 22px; font-weight: 800; margin-top: 4px; }
.kpi.green .v { color: var(--green); }
.kpi.red .v { color: var(--red); }
.kpi.blue .v { color: var(--primary); }

/* جداول */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 13px; color: var(--muted); font-weight: 600; background: #f8f9fc; position: sticky; top: 0; }
tr:hover td { background: #fafbfd; }
tr.void td { color: var(--muted); text-decoration: line-through; }
td.wrap { white-space: normal; }
tfoot td { font-weight: 800; background: #f8f9fc; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; background: #eef1f6; color: var(--muted); }
.pill.green { background: var(--green-soft); color: var(--green); }
.pill.red { background: var(--red-soft); color: var(--red); }
.pill.amber { background: var(--amber-soft); color: var(--amber); }
.pill.blue { background: var(--primary-soft); color: var(--primary); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #b8c0cc; margin-inline-end: 6px; }
.dot.on { background: #20b26b; box-shadow: 0 0 0 3px rgba(32,178,107,.18); }

/* المودال */
.modal-bg {
  position: fixed; inset: 0; background: rgba(10, 20, 40, .45); z-index: 1000; display: flex; align-items: flex-start;
  justify-content: center; padding: 30px 12px; overflow-y: auto;
}
.modal { background: var(--panel); border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.wide { max-width: 900px; }
.modal header { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal header h3 { margin: 0; font-size: 17px; flex: 1; }
.modal .body { padding: 18px; }
.modal footer { display: flex; gap: 8px; justify-content: flex-start; padding: 12px 18px; border-top: 1px solid var(--line); }
.close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }

/* إشعارات منبثقة */
#toasts { position: fixed; bottom: 16px; left: 16px; z-index: 2000; display: flex; flex-direction: column; gap: 8px; max-width: 360px; }
.toast {
  background: #1c2434; color: #fff; padding: 12px 14px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25);
  animation: slide .25s ease-out; cursor: pointer;
}
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
.toast b { display: block; margin-bottom: 2px; }
@keyframes slide { from { transform: translateY(20px); opacity: 0; } }

/* خلاصة الأحداث */
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.feed li:last-child { border-bottom: none; }
.feed li.unread { background: linear-gradient(90deg, transparent, var(--primary-soft)); }
.feed .ic { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex-shrink: 0; font-size: 16px; background: #eef1f6; }
.feed .ic.sale { background: var(--green-soft); }
.feed .ic.return { background: var(--amber-soft); }
.feed .ic.receipt { background: var(--primary-soft); }
.feed .ic.payment { background: var(--red-soft); }
.feed .ic.alert { background: var(--red-soft); }
.feed .tt { font-weight: 700; }
.feed .tm { color: var(--muted); font-size: 12px; white-space: nowrap; }

/* الرسم البياني */
.chart svg { width: 100%; height: 220px; display: block; }
.chart .bar { fill: var(--primary); }
.chart .bar:hover { fill: var(--primary-2); }
.chart text { font-size: 11px; fill: var(--muted); font-family: inherit; }

/* الخريطة */
.map-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; height: calc(100vh - 110px); }
.map-side { overflow-y: auto; margin: 0; }
#map { height: 100%; border-radius: var(--radius); box-shadow: var(--shadow); z-index: 1; }
.rep-list .rep { padding: 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.rep-list .rep:hover { background: #f6f8fb; }
.rep-list .rep.sel { border-color: var(--primary); background: var(--primary-soft); }
.rep-marker {
  position: absolute; left: 0; top: 0; transform: translate(-50%, -130%);
  background: var(--primary); color: #fff; border-radius: 16px; padding: 3px 9px; font-size: 12px; font-weight: 700;
  white-space: nowrap; border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.3); font-family: 'Tajawal', sans-serif;
}
.rep-marker.off { background: #7d8798; }
.rep-marker::after {
  content: ''; position: absolute; left: 50%; bottom: -8px; transform: translateX(-50%);
  border: 6px solid transparent; border-top-color: #fff;
}
.doc-marker { width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; display: grid; place-items: center; color: #fff; font-size: 11px; box-shadow: 0 1px 4px rgba(0,0,0,.35); }

/* سطور الأصناف */
.lines-table td { padding: 5px; }
.lines-table input, .lines-table select { padding: 6px 8px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs button { background: none; border: none; padding: 10px 14px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 700; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px 20px; }
.banner { background: var(--amber-soft); color: var(--amber); padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; }
.banner .btn { margin-inline-start: 8px; }
tr.pos-row td { background: var(--green-soft); }
tr.neg-row td { background: var(--red-soft); text-decoration-color: var(--red); }

@media (max-width: 900px) {
  #side { position: fixed; right: 0; z-index: 900; transform: translateX(100%); transition: transform .2s; }
  #side.open { transform: none; box-shadow: -10px 0 40px rgba(0,0,0,.3); }
  #menuBtn { display: inline-flex; }
  #page { padding: 12px; }
  #top { padding: 10px 12px; }
  .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .map-layout { grid-template-columns: 1fr; height: auto; }
  #map { height: 60vh; }
  .map-side { max-height: 40vh; }
}
@media (max-width: 520px) {
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .toolbar input, .toolbar select { min-width: 0; width: 100%; }
  .toolbar .field { flex: 1 1 140px; }
}
@media print {
  #side, #top, .toolbar, .no-print { display: none !important; }
  #page { padding: 0; }
  .card { box-shadow: none; }
}
