/* ============ الثيمات ============
   كل ثيم مستوحى من طبيعة المملكة. ألوان الحالة ثابتة في كل الثيمات:
   أخضر = شغالة، أحمر = مفصولة، برتقالي = فيها مشكلة. */
:root {
  --font: 'Readex Pro', 'Segoe UI', Tahoma, system-ui, sans-serif;
  --font-scale: 1;
  --radius: 10px;
  --gap: 6px;
  --ok: #3fb97a; --bad: #e5534b; --warn: #e8a33d;
  --sidebar-w: 216px;
  --pad: 16px;
  --row-h: 40px;
}
[data-density="compact"] { --pad: 10px; --row-h: 34px; }
[data-density="roomy"] { --pad: 22px; --row-h: 46px; }

/* ليل نجد: أزرق ليلي هادئ، مريح لغرف المراقبة */
[data-theme="najd"] {
  --bg: #141a22; --bg-2: #10151c; --surface: #1b232e; --surface-2: #222c39; --line: #2c3746;
  --text: #e4e8ee; --muted: #8e9aab; --accent: #5fb3a8; --on-accent: #0d1a18; --tile: #0b0f14; color-scheme: dark;
}
/* غيم: فاتح بارد للمكاتب */
[data-theme="ghaim"] {
  --bg: #eef1f4; --bg-2: #e4e8ed; --surface: #ffffff; --surface-2: #f5f7f9; --line: #dbe1e8;
  --text: #1e2630; --muted: #657285; --accent: #3b6fb6; --on-accent: #ffffff; --tile: #1a2028; color-scheme: light;
}
/* نخيل: أخضر عميق مع ذهبي التمر */
[data-theme="nakheel"] {
  --bg: #12201b; --bg-2: #0e1915; --surface: #182b24; --surface-2: #1f362d; --line: #2a443a;
  --text: #e3ebe6; --muted: #8fa89c; --accent: #c9a85c; --on-accent: #1c1608; --tile: #09110e; color-scheme: dark;
}
/* شفق: بنفسجي الغروب */
[data-theme="shafaq"] {
  --bg: #1a1724; --bg-2: #15121e; --surface: #221f30; --surface-2: #2b273c; --line: #383250;
  --text: #ebe8f3; --muted: #9b95b2; --accent: #a594f0; --on-accent: #17122e; --tile: #0f0d16; color-scheme: dark;
}
/* فحم: رمادي محايد بأقل إضاءة، للشاشات اللي تشتغل 24 ساعة */
[data-theme="faham"] {
  --bg: #161718; --bg-2: #121314; --surface: #1d1f21; --surface-2: #25282a; --line: #313437;
  --text: #dcdedf; --muted: #8b9094; --accent: #7aa7c7; --on-accent: #0e1820; --tile: #08090a; color-scheme: dark;
}
/* صفاء: فاتح نقي بلمسة فيروزية */
[data-theme="safa"] {
  --bg: #f6f6f3; --bg-2: #ecece7; --surface: #ffffff; --surface-2: #f9f9f6; --line: #e1e1da;
  --text: #22262a; --muted: #6c7177; --accent: #0f766e; --on-accent: #ffffff; --tile: #1b1f22; color-scheme: light;
}

/* ============ الأساس ============ */
* { box-sizing: border-box; }
html { font-size: calc(15px * var(--font-scale)); }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font); background: var(--bg); color: var(--text);
  line-height: 1.55; -webkit-font-smoothing: antialiased; font-variant-numeric: tabular-nums;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { margin: 0; font-weight: 500; line-height: 1.3; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.12rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }
.hidden { display: none !important; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ============ الهيكل ============ */
.shell { display: grid; grid-template-columns: var(--sidebar-w) 1fr; height: 100vh; overflow: hidden; }
.shell.collapsed { --sidebar-w: 64px; }
.sidebar {
  background: var(--bg-2); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 14px 10px; gap: 4px; min-width: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; min-height: 44px; }
.brand-mark { width: 30px; height: 30px; border-radius: 9px; background: var(--surface-2); display: grid; place-items: center; flex: none; }
.brand-mark span { width: 14px; height: 14px; border-radius: 50%; border: 3px solid var(--accent); position: relative; }
.brand-mark span::after { content: ''; position: absolute; inset: 2px; border-radius: 50%; background: var(--accent); }
.brand-name { font-weight: 600; font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-item {
  display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: calc(var(--radius) - 2px);
  color: var(--muted); cursor: pointer; border: 0; background: none; text-align: start; width: 100%; white-space: nowrap;
}
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.active { background: var(--surface-2); color: var(--text); box-shadow: inset -3px 0 0 var(--accent); }
.nav-item svg { flex: none; }
.nav-item .count { margin-inline-start: auto; background: var(--warn); color: #241600; border-radius: 99px; font-size: .72rem; padding: 0 7px; font-weight: 600; }
.shell.collapsed .nav-label, .shell.collapsed .brand-name, .shell.collapsed .count, .shell.collapsed .side-foot .who { display: none; }
.side-spacer { flex: 1; }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.who { padding: 4px 10px 8px; font-size: .85rem; }
.who b { display: block; font-weight: 500; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px var(--pad); border-bottom: 1px solid var(--line);
  min-height: 56px; background: var(--bg);
}
.topbar h1 { font-size: 1.1rem; }
.topbar .grow { flex: 1; }
.clock { font-size: .95rem; color: var(--muted); direction: ltr; }
.page { flex: 1; overflow: auto; padding: var(--pad); min-height: 0; }
.page.flush { padding: 0; overflow: hidden; display: flex; }

/* ============ عناصر عامة ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; height: 36px; padding: 0 14px;
  border-radius: calc(var(--radius) - 2px); border: 1px solid var(--line); background: var(--surface); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; font-weight: 500; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: none; border-color: transparent; }
.btn.ghost:hover { background: var(--surface); }
.btn.icon { width: 36px; padding: 0; }
.btn.sm { height: 30px; padding: 0 10px; font-size: .88rem; }
.btn.sm.icon { width: 30px; padding: 0; }
.btn svg { flex: none; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.on { background: var(--surface-2); border-color: var(--accent); }

.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: calc(var(--radius) - 2px); padding: 2px; gap: 2px; }
.seg button { border: 0; background: none; height: 30px; min-width: 34px; padding: 0 8px; border-radius: calc(var(--radius) - 4px); cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--surface-2); color: var(--text); box-shadow: 0 0 0 1px var(--line); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > span { font-size: .88rem; color: var(--muted); }
.field small { color: var(--muted); font-size: .8rem; }
.field > .seg, .field > .swatches { align-self: flex-start; }
.input, select.input, textarea.input {
  height: 38px; padding: 0 11px; border-radius: calc(var(--radius) - 3px); border: 1px solid var(--line);
  background: var(--bg); width: 100%; min-width: 0;
}
textarea.input { height: auto; padding: 9px 11px; min-height: 70px; resize: vertical; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.input.ltr-in { direction: ltr; text-align: left; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }
@media (max-width: 640px) { .grid2, .grid3 { grid-template-columns: 1fr; } }

.switch { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; user-select: none; }
.switch input { appearance: none; width: 36px; height: 20px; border-radius: 99px; background: var(--line); position: relative; cursor: pointer; transition: background .15s; margin: 0; flex: none; }
.switch input::after { content: ''; position: absolute; top: 3px; inset-inline-start: 3px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: inset-inline-start .15s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { inset-inline-start: 19px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 12px var(--pad); border-bottom: 1px solid var(--line); }
.panel-head .grow { flex: 1; }
.panel-body { padding: var(--pad); }
.stack { display: flex; flex-direction: column; gap: var(--pad); }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: var(--pad); flex-wrap: wrap; }
.page-head .grow { flex: 1; }

table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: start; font-weight: 400; color: var(--muted); font-size: .85rem; padding: 8px var(--pad); border-bottom: 1px solid var(--line); }
.tbl td { padding: 0 var(--pad); height: var(--row-h); border-bottom: 1px solid var(--line); }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.click { cursor: pointer; }
.tbl tr.click:hover td { background: var(--surface-2); }
.tbl .actions { text-align: end; white-space: nowrap; }
.table-wrap { overflow-x: auto; }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; background: var(--muted); }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.dot.offline { background: var(--bad); box-shadow: 0 0 0 3px color-mix(in srgb, var(--bad) 22%, transparent); }
.dot.warning { background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 22%, transparent); }
.tag { display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: .78rem; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.tag.accent { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.tag.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }

.meter { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; min-width: 70px; }
.meter i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.meter.full i { background: var(--warn); }
.usage { display: flex; flex-direction: column; gap: 3px; font-size: .8rem; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--pad); margin-bottom: var(--pad); }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px var(--pad); }
.stat b { font-size: 1.6rem; font-weight: 500; display: block; }
.stat span { color: var(--muted); font-size: .88rem; }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty .btn { margin-top: 14px; }

/* ============ نوافذ وتنبيهات ============ */
.modal-back { position: fixed; inset: 0; background: rgb(5 8 12 / .55); display: grid; place-items: center; z-index: 50; padding: 16px; animation: fade .12s; }
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: calc(var(--radius) + 4px); width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 24px 60px rgb(0 0 0 / .35); }
.modal.wide { width: min(780px, 100%); }
.modal-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px; overflow: auto; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-start; padding: 12px 18px; border-top: 1px solid var(--line); }
.modal-foot .grow { flex: 1; }
@keyframes fade { from { opacity: 0; } }
#toasts { position: fixed; bottom: 18px; inset-inline-start: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--surface-2); border: 1px solid var(--line); border-inline-start: 3px solid var(--accent); padding: 10px 14px; border-radius: var(--radius); max-width: 380px; box-shadow: 0 10px 30px rgb(0 0 0 / .25); animation: fade .15s; }
.toast.err { border-inline-start-color: var(--bad); }
.note { background: color-mix(in srgb, var(--accent) 10%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--line)); border-radius: calc(var(--radius) - 2px); padding: 10px 12px; font-size: .88rem; margin-bottom: 14px; }
.note.warn { background: color-mix(in srgb, var(--warn) 10%, var(--surface)); border-color: color-mix(in srgb, var(--warn) 35%, var(--line)); }
.code { direction: ltr; text-align: left; font-family: ui-monospace, Consolas, monospace; font-size: .82rem; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; word-break: break-all; }

/* ============ الدخول ============ */
.login { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.login-art { background: var(--tile); position: relative; overflow: hidden; }
.login-art canvas { position: absolute; inset: 0; width: 100%; height: 100%; opacity: .9; }
.login-art .cap { position: absolute; bottom: 28px; inset-inline-start: 32px; color: #e7ebef; max-width: 360px; }
.login-art .cap h2 { font-size: 1.6rem; font-weight: 400; margin-bottom: 6px; }
.login-art .cap p { margin: 0; color: #a8b3bf; }
.login-form { display: grid; place-items: center; padding: 32px; }
.login-form form { width: min(360px, 100%); }
.login-form .brand { padding: 0 0 28px; }
.login-form h1 { margin-bottom: 4px; }
.login-form p.muted { margin: 0 0 24px; }
.err-text { color: var(--bad); font-size: .88rem; min-height: 1.4em; margin-bottom: 8px; }
@media (max-width: 860px) { .login { grid-template-columns: 1fr; } .login-art { display: none; } }

/* ============ شاشة البث ============ */
.live { display: flex; flex: 1; min-width: 0; min-height: 0; }
.cams { width: 270px; flex: none; border-inline-end: 1px solid var(--line); background: var(--bg-2); display: flex; flex-direction: column; min-height: 0; }
.cams.hidden-panel { display: none; }
.cams-head { padding: 10px 12px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.cams-list { overflow: auto; flex: 1; padding: 6px 6px 16px; }
.branch-node > summary { list-style: none; display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 7px; cursor: pointer; font-weight: 500; }
.branch-node > summary::-webkit-details-marker { display: none; }
.branch-node > summary:hover { background: var(--surface); }
.branch-node > summary .chev { transition: transform .15s; color: var(--muted); }
.branch-node[open] > summary .chev { transform: rotate(-90deg); }
.branch-node > summary .n { margin-inline-start: auto; color: var(--muted); font-weight: 400; font-size: .8rem; }
.dev-label { padding: 6px 10px 2px 8px; font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.cam-item { display: flex; align-items: center; gap: 9px; padding: 6px 10px; padding-inline-start: 22px; border-radius: 7px; cursor: grab; font-size: .92rem; user-select: none; }
.cam-item:hover { background: var(--surface); }
.cam-item.in-grid { color: var(--accent); }
.cam-item svg { color: var(--muted); flex: none; }

.wall-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.wall-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.wall-bar .grow { flex: 1; }
.wall-bar select.input { height: 34px; width: auto; min-width: 150px; }
.wall { flex: 1; display: grid; gap: var(--gap); padding: var(--gap); background: var(--bg-2); min-height: 0; }
.wall.tiles-names-off .tile-name { display: none; }

.tile { position: relative; background: var(--tile); border-radius: max(0px, calc(var(--radius) - 4px)); overflow: hidden; min-width: 0; min-height: 0; outline: 1px solid transparent; }
.tile.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.tile.drop { outline: 2px dashed var(--accent); outline-offset: -2px; }
.tile video, .tile canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: var(--tile); }
.wall.fill .tile video, .wall.fill .tile canvas { object-fit: cover; }
.tile-state { position: absolute; top: 0; inset-inline: 0; height: 2px; background: transparent; z-index: 3; }
.tile[data-state="live"] .tile-state { background: var(--ok); opacity: .7; }
.tile[data-state="connecting"] .tile-state, .tile[data-state="reconnecting"] .tile-state { background: var(--warn); }
.tile[data-state="failed"] .tile-state { background: var(--bad); }
.tile-name {
  position: absolute; bottom: 0; inset-inline: 0; padding: 18px 10px 6px; z-index: 2;
  background: linear-gradient(to top, rgb(0 0 0 / .62), transparent); color: #eef2f5; font-size: .82rem;
  display: flex; align-items: center; gap: 6px; pointer-events: none;
}
.tile-name .b { color: #aab4be; }
.tile-name .q { margin-inline-start: auto; font-size: .7rem; border: 1px solid rgb(255 255 255 / .35); border-radius: 4px; padding: 0 4px; direction: ltr; }
.tile-tools { position: absolute; top: 6px; inset-inline-end: 6px; display: flex; gap: 4px; z-index: 4; opacity: 0; transition: opacity .12s; }
.tile:hover .tile-tools, .tile:focus-within .tile-tools { opacity: 1; }
.tile-tools button { width: 28px; height: 28px; border-radius: 7px; border: 0; background: rgb(10 14 18 / .7); color: #e8edf1; cursor: pointer; display: grid; place-items: center; }
.tile-tools button:hover { background: rgb(10 14 18 / .92); }
.tile-msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: #b7c0c9; font-size: .85rem; z-index: 2; padding: 10px; }
.tile-msg .btn { margin-top: 8px; background: rgb(255 255 255 / .08); border-color: rgb(255 255 255 / .18); color: #eef2f5; }
.tile-empty { position: absolute; inset: 0; display: grid; place-items: center; color: color-mix(in srgb, var(--muted) 70%, transparent); font-size: .85rem; border: 1px dashed color-mix(in srgb, var(--muted) 30%, transparent); border-radius: inherit; }
.tile-empty div { text-align: center; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2px solid rgb(255 255 255 / .2); border-top-color: #fff; animation: spin .8s linear infinite; margin: 0 auto 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
.tile.maxed { position: fixed; inset: 0; z-index: 40; border-radius: 0; }
.wall-wrap:fullscreen { background: var(--bg-2); }
.wall-wrap:fullscreen .wall-bar { display: none; }

/* ============ الإعدادات ============ */
.themes { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.theme-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 0; cursor: pointer; background: none; text-align: start; overflow: hidden; }
.theme-card.on { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent); }
.theme-prev { height: 78px; display: grid; grid-template-columns: 28% 1fr; gap: 4px; padding: 6px; }
.theme-prev i { border-radius: 4px; display: block; }
.theme-prev .tp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; }
.theme-card .tn { padding: 8px 10px; font-size: .9rem; display: block; }
.theme-card .tn small { display: block; color: var(--muted); font-size: .76rem; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatch.on { border-color: var(--text); }
.swatch-custom { width: 32px; height: 32px; border: 1px solid var(--line); border-radius: 50%; padding: 0; background: none; cursor: pointer; }
.range-row { display: flex; align-items: center; gap: 12px; }
.range-row input[type=range] { flex: 1; accent-color: var(--accent); }
.range-row output { min-width: 44px; text-align: center; color: var(--muted); }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: var(--pad); align-items: start; }

.check-list { display: flex; flex-direction: column; gap: 8px; max-height: 200px; overflow: auto; padding: 8px 10px; border: 1px solid var(--line); border-radius: calc(var(--radius) - 3px); background: var(--bg); }
.chan-table input.input { height: 32px; }
.dev-row td:first-child { width: 28px; }
.branch-block { margin-bottom: var(--pad); }

@media (max-width: 900px) {
  .shell { grid-template-columns: 64px 1fr; }
  .shell .nav-label, .shell .brand-name, .shell .count, .shell .side-foot .who { display: none; }
  .cams { position: absolute; inset-inline-start: 64px; top: 56px; bottom: 0; z-index: 30; box-shadow: 0 10px 40px rgb(0 0 0 / .4); }
}
@media (max-width: 700px) {
  .wall-bar { flex-wrap: nowrap; overflow-x: auto; }
  .wall-bar .grow, .wall-bar .muted { display: none; }
  .wall { overflow: auto; align-content: start; grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
          grid-template-rows: none !important; grid-auto-rows: calc((100vw - 18px) / 2 * 9 / 16); }
  .wall[data-grid="1"] { grid-template-columns: 1fr !important; grid-auto-rows: calc((100vw - 12px) * 9 / 16); }
  .topbar .clock { display: none; }
  .page-head, .topbar { flex-wrap: wrap; }
}
@media (max-width: 560px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { order: 2; flex-direction: row; padding: 6px; border-inline-end: 0; border-top: 1px solid var(--line); overflow-x: auto; }
  .sidebar .brand, .side-spacer, .side-foot .who { display: none; }
  .side-foot { border: 0; padding: 0; flex-direction: row; }
  .nav-item { width: auto; }
  .cams { inset-inline-start: 0; }
}

/* ============ خريطة الموقع ============ */
.map-page { display: flex; flex: 1; min-width: 0; min-height: 0; }
.map-side { width: 280px; flex: none; border-inline-end: 1px solid var(--line); background: var(--bg-2); display: flex; flex-direction: column; overflow: auto; padding-bottom: 12px; }
.map-side-head { padding: 12px 14px 6px; }
.map-side-foot { display: flex; gap: 6px; padding: 10px 12px; margin-top: auto; border-top: 1px solid var(--line); }
.map-tree { display: flex; flex-direction: column; padding: 0 6px; }
.map-node { display: flex; align-items: center; gap: 9px; padding: 7px 10px; border-radius: 7px; border: 0; background: none; cursor: pointer; text-align: start; color: var(--text); }
.map-node:hover { background: var(--surface); }
.map-node.on { background: var(--surface-2); box-shadow: inset -3px 0 0 var(--accent); }
.map-node .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-sec { padding: 4px 12px; border-bottom: 1px solid var(--line); }
.map-sec > summary { cursor: pointer; padding: 10px 0; font-weight: 500; }
.map-sec p { margin: 0 0 8px; }
.map-cams { display: flex; flex-direction: column; gap: 2px; padding-bottom: 10px; }
.map-cams-b { font-size: .78rem; color: var(--muted); padding: 8px 4px 2px; }
.map-cam { display: flex; align-items: center; gap: 9px; padding: 5px 6px; border-radius: 7px; cursor: grab; font-size: .9rem; }
.map-cam:hover { background: var(--surface); }
.map-cam > span:nth-child(2) { flex: 1; display: flex; flex-direction: column; line-height: 1.3; }
.map-cam.placed { color: var(--muted); }
.map-cam.placed svg:last-child { color: var(--ok); }
.map-thumb { width: 46px; height: 28px; border-radius: 4px; background: var(--tile); display: grid; place-items: center; color: #8e9aab; overflow: hidden; flex: none; }
.map-thumb img { width: 100%; height: 100%; object-fit: cover; }
.map-lib { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.map-card { border: 1px solid var(--line); border-radius: 7px; overflow: hidden; cursor: grab; background: var(--surface); font-size: .8rem; }
.map-card img, .map-card-ph { width: 100%; height: 62px; object-fit: cover; display: grid; place-items: center; background: var(--tile); color: var(--muted); }
.map-card span { display: block; padding: 4px 7px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.map-props { padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--surface); }
.map-props > b { display: block; margin-bottom: 8px; }
.map-props input[type=range] { accent-color: var(--accent); width: 100%; }

.map-main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.map-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.map-bar .grow { flex: 1; }
.map-tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.bar-sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.map-crumbs { display: flex; align-items: center; gap: 2px; flex-wrap: wrap; }
.crumb { border: 0; background: none; padding: 5px 8px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.crumb:hover { background: var(--surface); color: var(--text); }
.crumb.on { color: var(--text); font-weight: 500; }
.map-crumbs .sep { color: var(--muted); display: inline-flex; }
.map-canvas { flex: 1; position: relative; min-height: 0; }
.map-viewport { position: absolute; inset: 0; overflow: hidden; background:
  radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--muted) 18%, transparent) 1px, transparent 0) 0 0 / 22px 22px, var(--bg-2);
  cursor: grab; touch-action: none; outline: none; }
.map-viewport:active { cursor: grabbing; }
.map-viewport.editing[data-tool="zone"] { cursor: crosshair; }
.map-viewport.drop { box-shadow: inset 0 0 0 2px var(--accent); }
.map-stage { position: absolute; top: 0; left: 0; transform-origin: 0 0; direction: ltr; --inv: 1; }
.map-stage.anim { transition: transform .22s ease; }
.map-stage img { position: absolute; inset: 0; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; box-shadow: 0 10px 40px rgb(0 0 0 / .3); border-radius: 4px; }
.map-stage.no-image { background: var(--surface); border: 2px dashed var(--line); border-radius: 8px; }
.map-layer { position: absolute; inset: 0; }
.map-empty { position: absolute; inset: 0; display: grid; place-content: center; }

.zone { position: absolute; border: calc(2px * var(--inv)) solid color-mix(in srgb, var(--accent) 70%, transparent);
  background: color-mix(in srgb, var(--accent) 10%, transparent); border-radius: calc(6px * var(--inv)); transition: background .12s; }
.zone.linked { cursor: pointer; }
.zone:hover { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.zone.st-online { border-color: color-mix(in srgb, var(--ok) 75%, transparent); background: color-mix(in srgb, var(--ok) 9%, transparent); }
.zone.st-warning { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.zone.st-offline { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 16%, transparent); animation: zonePulse 2s ease-in-out infinite; }
@keyframes zonePulse { 50% { background: color-mix(in srgb, var(--bad) 30%, transparent); } }
.zone.sel { outline: calc(2px * var(--inv)) dashed var(--text); outline-offset: calc(3px * var(--inv)); }
.zone-label { position: absolute; top: 0; right: 0; transform: scale(var(--inv)); transform-origin: top right; direction: rtl;
  display: inline-flex; align-items: center; gap: 6px; background: rgb(12 16 22 / .82); color: #eef2f5; font-size: 13px; padding: 4px 9px;
  border-radius: 0 6px 0 8px; white-space: nowrap; pointer-events: none; }
.zone-go { display: inline-flex; opacity: .7; }

.pin { position: absolute; width: 0; height: 0; z-index: 2; cursor: pointer; }
.pin > * { position: absolute; }
.pin .cone { width: 120px; height: 120px; left: -60px; top: -60px; transform-origin: 50% 50%; pointer-events: none; }
.pin .cone { scale: var(--inv); }
.pin .cone path { fill: color-mix(in srgb, var(--accent) 22%, transparent); stroke: color-mix(in srgb, var(--accent) 55%, transparent); stroke-width: 1; }
.pin-dot { width: 26px; height: 26px; left: -13px; top: -13px; border-radius: 50%; background: #1b232e; border: 3px solid var(--muted);
  display: grid; place-items: center; color: #eef2f5; transform: scale(var(--inv)); box-shadow: 0 2px 8px rgb(0 0 0 / .45); }
.pin.st-online .pin-dot { border-color: var(--ok); }
.pin.st-offline .pin-dot { border-color: var(--bad); background: #3a1716; }
.pin.st-warning .pin-dot { border-color: var(--warn); }
.pin.st-offline .cone path { fill: color-mix(in srgb, var(--bad) 18%, transparent); stroke: color-mix(in srgb, var(--bad) 50%, transparent); }
.pin.sel .pin-dot { outline: 2px solid var(--text); outline-offset: 2px; }
.pin:hover .pin-dot, .pin:focus-visible .pin-dot { filter: brightness(1.3); }
.pin-label { top: 0; left: 0; transform: translate(-50%, 18px) scale(var(--inv)); transform-origin: 50% -18px; white-space: nowrap; text-align: center;
  background: rgb(12 16 22 / .8); color: #eef2f5; border-radius: 6px; padding: 2px 8px; font-size: 12px; line-height: 1.4; pointer-events: none; direction: rtl; }
.pin-label b { font-weight: 500; display: block; }
.pin-label small { color: #a9b4c0; font-size: 10.5px; display: block; }
.map-viewport:not(.editing) .pin-label { opacity: 0; transition: opacity .12s; }
.map-viewport:not(.editing) .pin:hover .pin-label, .map-viewport:not(.editing) .pin.sel .pin-label { opacity: 1; }
.handle { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 2px solid #fff; transform: translate(-50%, -50%) scale(var(--inv)); z-index: 3; cursor: grab; }
.handle.rs { position: absolute; right: 0; bottom: 0; left: auto; top: auto; transform: translate(50%, 50%) scale(var(--inv)); cursor: nwse-resize; border-radius: 3px; }

.map-pops { position: absolute; bottom: 14px; left: 14px; z-index: 10; }
.map-pop { width: 420px; max-width: calc(100vw - 40px); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: 0 18px 50px rgb(0 0 0 / .45); animation: fade .15s; }
.map-pop.big { width: min(1100px, calc(100vw - 120px)); }
.pop-head { display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 6px; }
.pop-head .grow { flex: 1; }
.pop-video { position: relative; aspect-ratio: 16 / 9; border-radius: 0; }

.map-banner { display: flex; align-items: center; gap: 16px; padding: 10px 14px; background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border-bottom: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); flex-wrap: wrap; }
.wz-steps { display: flex; gap: 6px; }
.wz-dot { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: .8rem; border: 1px solid var(--line); color: var(--muted); background: var(--surface); }
.wz-dot.on { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.wz-dot.done { border-color: var(--accent); color: var(--accent); }
.wz-text { flex: 1; display: flex; flex-direction: column; min-width: 220px; }
.wz-list { display: flex; flex-direction: column; gap: 4px; max-height: 120px; overflow: auto; }
.wz-item { display: flex; align-items: center; gap: 8px; font-size: .9rem; }
@media (max-width: 900px) { .map-side { display: none; } .map-pop.big { width: calc(100vw - 30px); } }

/* ضبط الصورة والتحدث */
.tile-tools button.talk.on { background: var(--bad); }
.adj-panel { position: absolute; top: 40px; inset-inline-end: 8px; z-index: 6; width: 240px; background: rgb(14 18 24 / .94); color: #e8edf1;
  border: 1px solid rgb(255 255 255 / .12); border-radius: 10px; padding: 10px 12px; font-size: .85rem; }
.adj-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.adj-head .btn { color: #e8edf1; }
.adj-row { display: grid; grid-template-columns: 52px 1fr 42px; align-items: center; gap: 6px; margin-bottom: 6px; }
.adj-row input { accent-color: var(--accent); width: 100%; }
.adj-row output { text-align: center; color: #aab4be; font-size: .78rem; }
.adj-panel .btn { background: rgb(255 255 255 / .08); border-color: rgb(255 255 255 / .18); color: #eef2f5; }
.tile.maxed video, .tile.maxed canvas { transition: transform .05s; }
.pb-video { aspect-ratio: 16 / 9; border-radius: var(--radius); }

/* ============ الإنذارات ============ */
.rules { display: flex; flex-direction: column; gap: 10px; }
.rule { display: flex; align-items: center; gap: 14px; padding: 14px var(--pad); }
.rule.off { opacity: .6; }
.rule-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.rule-name { font-weight: 500; display: flex; gap: 8px; align-items: center; }
.rule-flow { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: .92rem; }
.rule-if { background: var(--surface-2); border: 1px solid var(--line); padding: 3px 10px; border-radius: 99px; }
.rule-then { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--text); border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); padding: 3px 10px; border-radius: 99px; }
.rule-arrow { color: var(--muted); display: inline-flex; }
.rule-actions { display: flex; align-items: center; gap: 6px; }
.rule-sec { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px 2px; margin-bottom: 14px; }
.rule-sec h3 { margin-bottom: 10px; color: var(--accent); }
.act { margin-bottom: 12px; }
.act-extra { margin: 10px 0 0; padding-inline-start: 44px; }
.days { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }

.tile.alert { outline: 3px solid var(--bad); outline-offset: -3px; animation: tileAlert 1s ease-in-out 6; }
.tile.alert::after { content: attr(data-alert); position: absolute; top: 8px; inset-inline-start: 8px; z-index: 5; background: var(--bad); color: #fff; font-size: .75rem; padding: 2px 8px; border-radius: 99px; }
@keyframes tileAlert { 50% { outline-color: transparent; } }
.pin.alert .pin-dot { animation: pinAlert .7s ease-in-out 12; border-color: var(--bad); }
@keyframes pinAlert { 50% { box-shadow: 0 0 0 calc(14px * var(--inv)) color-mix(in srgb, var(--bad) 35%, transparent); } }
.zone.alert { animation: zonePulse .8s ease-in-out 10; border-color: var(--bad); }

.alarm-pops { position: fixed; bottom: 16px; inset-inline-end: 16px; z-index: 45; display: flex; flex-direction: column; gap: 10px; }
.alarm-pop { width: 380px; max-width: calc(100vw - 32px); background: var(--surface); border: 1px solid var(--bad); border-radius: var(--radius); overflow: hidden; box-shadow: 0 18px 50px rgb(0 0 0 / .45); }
.alarm-pop.flash { animation: popFlash .6s ease-in-out 3; }
@keyframes popFlash { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--bad) 40%, transparent), 0 18px 50px rgb(0 0 0 / .45); } }
.alarm-badge { background: var(--bad); color: #fff; font-size: .75rem; padding: 2px 9px; border-radius: 99px; white-space: nowrap; }

/* ============ المساعد الذكي ============ */
#toasts { bottom: 84px; }
.ai-fab { position: fixed; bottom: 18px; inset-inline-start: 18px; z-index: 44; width: 52px; height: 52px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--on-accent); box-shadow: 0 10px 30px rgb(0 0 0 / .35); display: grid; place-items: center; }
.ai-fab:hover { filter: brightness(1.08); }
.ai-panel { position: fixed; bottom: 82px; inset-inline-start: 18px; z-index: 46; width: 380px; max-width: calc(100vw - 36px); height: min(540px, calc(100vh - 120px));
  background: var(--surface); border: 1px solid var(--line); border-radius: calc(var(--radius) + 4px); box-shadow: 0 24px 60px rgb(0 0 0 / .4); display: flex; flex-direction: column; overflow: hidden; }
.ai-panel .pop-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.ai-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.ai-msg { max-width: 85%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; font-size: .92rem; line-height: 1.6; }
.ai-msg.assistant { background: var(--surface-2); align-self: flex-start; border-start-start-radius: 4px; }
.ai-msg.user { background: color-mix(in srgb, var(--accent) 22%, var(--surface)); align-self: flex-end; border-start-end-radius: 4px; }
.ai-msg.err { color: var(--bad); }
.ai-done { margin-top: 6px; font-size: .8rem; color: var(--ok); }
.ai-input { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); align-items: flex-end; }
.ai-input textarea { min-height: 38px; height: 38px; resize: none; }
.ai-input .btn.on { background: var(--bad); color: #fff; border-color: transparent; }
.ai-fab, .ai-panel { inset-inline-start: 236px; }
@media (max-width: 900px) { .ai-fab, .ai-panel { inset-inline-start: 80px; } }
@media (max-width: 560px) { .ai-fab { inset-inline-start: 14px; bottom: 74px; } .ai-panel { inset-inline-start: 10px; bottom: 136px; } }
