:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --line: #e5e7eb;
  --text: #0f172a;
  --muted: #64748b;
  --brand: #16a34a;
  --brand-ink: #ffffff;
  --side: #0b1220;
  --side-text: #cbd5e1;
  --side-active: #1e293b;
  --danger: #dc2626;
  --warn: #d97706;
  --ok: #16a34a;
  --info: #2563eb;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .06);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f17; --panel: #121826; --panel-2: #0f1520; --line: #1f2937; --text: #e5e7eb; --muted: #94a3b8;
    --side: #070b12; --side-active: #182131; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0f17; --panel: #121826; --panel-2: #0f1520; --line: #1f2937; --text: #e5e7eb; --muted: #94a3b8;
  --side: #070b12; --side-active: #182131; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 Inter, system-ui, -apple-system, Segoe UI, sans-serif; background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--info); text-decoration: none; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 20px; } h2 { font-size: 16px; } h3 { font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ── shell ── */
.shell { display: grid; grid-template-columns: 220px 1fr; height: 100vh; height: 100dvh; }
.side { background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 16px 10px; gap: 2px; overflow-y: auto; }
.side .logo { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 16px; padding: 4px 10px 18px; }
.side .logo i { width: 28px; height: 28px; border-radius: 8px; background: var(--brand); display: grid; place-items: center; font-style: normal; font-size: 15px; }
.side a { display: flex; align-items: center; gap: 10px; color: var(--side-text); padding: 9px 12px; border-radius: 8px; font-weight: 500; }
.side a:hover { background: var(--side-active); color: #fff; }
.side a.active { background: var(--side-active); color: #fff; box-shadow: inset 3px 0 0 var(--brand); }
.side a .ic { width: 18px; text-align: center; opacity: .9; }
.side a .badge { margin-left: auto; }
.side .foot { margin-top: auto; padding: 10px 12px; font-size: 12px; color: #64748b; }
.side .foot button { background: none; border: 0; color: #94a3b8; cursor: pointer; padding: 0; }
.main { overflow: auto; display: flex; flex-direction: column; min-width: 0; }
.page { padding: 20px 24px 40px; display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-head h1 { margin-right: auto; }

/* ── building blocks ── */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.card > .hd { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.card > .hd h2 { margin-right: auto; }
.card > .bd { padding: 16px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; cursor: pointer; font-weight: 500; white-space: nowrap; min-height: 34px; }
.btn:hover { border-color: #cbd5e1; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.sm { padding: 4px 9px; min-height: 28px; font-size: 12px; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.input, select.input, textarea.input { width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 10px; min-height: 34px; }
.input:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
label.field > span { color: var(--muted); }
label.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: color-mix(in srgb, var(--c, #64748b) 14%, transparent); color: color-mix(in srgb, var(--c, #64748b) 85%, var(--text)); }
.chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--c, #64748b); }
.chip.plain::before { display: none; }
.badge { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--c, #94a3b8); flex: none; }

table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--panel-2); position: sticky; top: 0; z-index: 1; }
table.t td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: var(--panel-2); }
table.t td.num, table.t th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow: auto; max-width: 100%; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

/* KPI strip */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.kpi .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi .l { font-size: 12px; color: var(--muted); }
.kpi.bad .v { color: var(--danger); }
.kpi.good .v { color: var(--ok); }

/* ── live ── */
.live { display: grid; grid-template-columns: minmax(0, 1fr) 420px; gap: 16px; flex: 1; min-height: 560px; }
.live .mapcard { position: relative; overflow: hidden; min-height: 520px; }
.map { position: absolute; inset: 0; background: var(--panel-2); }
.map-tools { position: absolute; top: 10px; left: 10px; z-index: 500; display: flex; gap: 6px; flex-wrap: wrap; }
.map-tools .btn { box-shadow: var(--shadow); }
.map-tools .btn.on { background: var(--text); color: var(--panel); border-color: var(--text); }
.map-legend { position: absolute; bottom: 10px; left: 10px; z-index: 500; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12px; display: flex; gap: 12px; box-shadow: var(--shadow); flex-wrap: wrap; }
.side-panel { display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; gap: 2px; padding: 4px; background: var(--panel-2); border-bottom: 1px solid var(--line); border-radius: var(--radius) var(--radius) 0 0; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs button.on { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.panel-body { overflow: auto; flex: 1; min-height: 0; }
.qitem { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.qitem:hover { background: var(--panel-2); }
.qitem .t1 { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qitem .t2 { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qitem.late { box-shadow: inset 3px 0 0 var(--danger); }
.qitem.waiting { box-shadow: inset 3px 0 0 var(--warn); }

/* matrix */
.matrix td.v-short { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.matrix td.v-tight { background: color-mix(in srgb, var(--warn) 12%, transparent); }
.matrix td.v-ok { background: color-mix(in srgb, var(--ok) 10%, transparent); }
.verdict { font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
.verdict.short { color: var(--danger); } .verdict.tight { color: var(--warn); } .verdict.ok { color: var(--ok); }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; display: flex; }
.bar i { display: block; height: 100%; }

/* map markers */
.mk-driver svg { display: block; }
.mk-driver { position: relative; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 11px; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); background: var(--c); transform: translate(0, 0); }
.mk-driver.stale { box-shadow: 0 0 0 3px #dc2626, 0 2px 8px rgba(0,0,0,.45); }
.mk-alert { position: absolute; top: -6px; right: -6px; width: 16px; height: 16px; border-radius: 50%; background: #dc2626; color: #fff; font-size: 11px; display: grid; place-items: center; border: 2px solid #fff; }
.alerts { display: flex; flex-direction: column; gap: 6px; }
.alertrow { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 10px; background: color-mix(in srgb, var(--danger) 10%, var(--panel)); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); cursor: pointer; }
.alertrow.medium { background: color-mix(in srgb, var(--warn) 10%, var(--panel)); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.alertrow.low { background: var(--panel); border-color: var(--line); }
.mk-store { width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center; background: #111827; color: #fff; font-size: 13px; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.mk-store.waiting { background: var(--warn); animation: pulse 1.4s infinite; }
.mk-brand { position: relative; border-radius: 9px; display: grid; place-items: center; background: var(--c); color: #fff; font-weight: 800; letter-spacing: -.02em; border: 2px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.35); cursor: pointer; }
.mk-brand.waiting { box-shadow: 0 0 0 3px var(--warn), 0 1px 5px rgba(0,0,0,.35); animation: pulse 1.4s infinite; }
.mk-count { position: absolute; top: -7px; right: -7px; min-width: 18px; height: 18px; padding: 0 4px; border-radius: 9px; background: var(--warn); color: #fff; font-size: 11px; display: grid; place-items: center; border: 2px solid #fff; }
.brand-chip { display: inline-flex; align-items: center; gap: 6px; }
.heat td.h { text-align: center; font-variant-numeric: tabular-nums; font-size: 12px; padding: 6px 4px; min-width: 26px; }
.heat th.h { text-align: center; padding: 6px 4px; font-size: 10px; }
.heat td.now, .heat th.now { outline: 2px solid var(--info); outline-offset: -2px; }
.summary { margin: 0; padding-left: 18px; line-height: 1.6; }
.mk-drop { width: 14px; height: 14px; border-radius: 50%; background: var(--c, #f59e0b); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(217,119,6,.6); } 50% { box-shadow: 0 0 0 8px rgba(217,119,6,0); } }
.leaflet-div-icon { background: none; border: 0; }
.gm-label { background: var(--panel); color: var(--text); }

/* drawer + modal */
.drawer-bg, .modal-bg { position: fixed; inset: 0; background: rgba(15, 23, 42, .35); z-index: 1000; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 100vw); background: var(--bg); z-index: 1001; display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0,0,0,.15); animation: slidein .18s ease-out; }
@keyframes slidein { from { transform: translateX(30px); opacity: .6; } }
.drawer > .hd { display: flex; align-items: center; gap: 10px; padding: 14px 18px; background: var(--panel); border-bottom: 1px solid var(--line); }
.drawer > .hd h2 { margin-right: auto; }
.drawer > .bd { overflow: auto; padding: 16px 18px 40px; display: flex; flex-direction: column; gap: 14px; }
.modal { position: fixed; z-index: 1002; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 40px); overflow: auto; background: var(--panel); border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal > .hd { padding: 16px 18px 0; display: flex; align-items: center; }
.modal > .hd h2 { margin-right: auto; }
.modal > .bd { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.modal > .ft { padding: 12px 18px 16px; display: flex; gap: 8px; justify-content: flex-end; border-top: 1px solid var(--line); }
.x { border: 0; background: transparent; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 16px 1fr auto; gap: 10px; padding: 6px 0; position: relative; }
.timeline li::before { content: ""; position: absolute; left: 7px; top: 22px; bottom: -6px; width: 2px; background: var(--line); }
.timeline li:last-child::before { display: none; }
.timeline .dot { margin-top: 5px; width: 12px; height: 12px; border: 2px solid var(--panel); box-shadow: 0 0 0 1px var(--line); }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; }
.kv > div:nth-child(odd) { color: var(--muted); }
.minimap { height: 260px; border-radius: 10px; overflow: hidden; position: relative; }
.secret { display: flex; gap: 6px; align-items: center; background: var(--panel-2); border: 1px dashed var(--line); padding: 8px 10px; border-radius: 8px; word-break: break-all; }
pre.code { background: #0b1220; color: #d1e7ff; padding: 12px; border-radius: 8px; overflow: auto; font-size: 12px; max-height: 360px; margin: 0; }

#toasts { position: fixed; bottom: 16px; right: 16px; z-index: 2000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #0f172a; color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 360px; animation: slidein .15s; }
.toast.err { background: var(--danger); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 600px at 20% 0%, color-mix(in srgb, var(--brand) 15%, transparent), transparent), var(--bg); }
.login .card { width: min(380px, 100%); }
.login .bd { display: flex; flex-direction: column; gap: 12px; padding: 24px; }

.zones-layout { grid-template-columns: minmax(0, 1fr) 360px; }

/* phone */
.mobile-nav { display: none; }
@media (max-width: 1100px) {
  .live, .zones-layout { grid-template-columns: 1fr; }
  .live .mapcard { min-height: 420px; }
  .side-panel { max-height: 640px; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .side { display: none; }
  .mobile-nav { display: flex; background: var(--side); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); gap: 2px; overflow-x: auto; scrollbar-width: none; }
  .mobile-nav::-webkit-scrollbar { display: none; }
  .mobile-nav a { flex: 1 0 auto; color: var(--side-text); display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 10px; padding: 6px 8px; border-radius: 8px; }
  .mobile-nav a .ic { font-size: 17px; }
  .mobile-nav a.active { color: #fff; background: var(--side-active); }
  .page { padding: 14px 16px 24px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 110px 1fr; }
  .kpis { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .kpi { padding: 8px 10px; }
  .kpi .v { font-size: 18px; }
  .kpi .l { font-size: 11px; line-height: 1.25; }
  .mobile-nav a { flex: 0 0 auto; min-width: 64px; }
  .live .mapcard { min-height: 360px; }
  .drawer { width: 100vw; }
  .hide-sm { display: none !important; }
}

/* Managers view only: every control that changes something is marked .admin-only. */
.role-manager .admin-only { display: none !important; }

/* On/off switch for active / inactive. */
.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 12px; font-weight: 600; white-space: nowrap; }
.switch input { appearance: none; width: 36px; height: 20px; border-radius: 10px; background: #cbd5e1; position: relative; cursor: pointer; transition: background .15s; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: left .15s; }
.switch input:checked { background: var(--ok); }
.switch input:checked::after { left: 18px; }

.connbar { display: flex; align-items: center; gap: 6px; padding: 0 10px 14px; }
.connbar .chip { background: rgba(255,255,255,.06); color: #cbd5e1; font-size: 11px; }
.connbar .btn { background: transparent; color: #cbd5e1; border-color: #334155; min-height: 24px; padding: 2px 8px; font-size: 11px; }

/* Arabic (right-to-left) */
[dir="rtl"] .side a.active { box-shadow: inset -3px 0 0 var(--brand); }
[dir="rtl"] table.t th, [dir="rtl"] table.t td { text-align: right; }
[dir="rtl"] table.t td.num, [dir="rtl"] table.t th.num { text-align: left; }
[dir="rtl"] .drawer { right: auto; left: 0; }
[dir="rtl"] .map-tools { left: auto; right: 10px; }
[dir="rtl"] .map-legend { left: auto; right: 10px; }
[dir="rtl"] table.t td, [dir="rtl"] .kv > div, [dir="rtl"] .qitem .t2 { unicode-bidi: plaintext; }
