:root {
  --bg: #0f1216;
  --panel: #171b21;
  --panel-2: #1e242c;
  --border: #2a313b;
  --text: #e8ebf0;
  --muted: #8d97a6;
  --accent: #3d8bfd;
  --accent-ink: #ffffff;
  --rec: #2fb36d;
  --danger: #ef5b5b;
  --bar-h: 52px;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Segoe UI", Tahoma, system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: #3a4350; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button:disabled { opacity: .5; cursor: default; }
select, input[type="date"], input[type="time"], input[type="datetime-local"], input[type="text"], input[type="password"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  min-width: 0;
  color-scheme: dark;
}
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* ---------- الدخول ---------- */
.auth {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.auth form {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  display: grid;
  gap: 12px;
}
.auth h1 { margin: 0 0 4px; font-size: 20px; }
.auth p { margin: 0; color: var(--muted); }
.auth label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
.auth input { font-size: 16px; color: var(--text); }
.auth .error { color: var(--danger); min-height: 1.5em; }

/* ---------- الهيكل ---------- */
.shell { height: 100dvh; display: flex; flex-direction: column; }
.shell > .view { flex: 1; }
.banner.offline { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 8px 12px 0; }
.banner.offline span { flex: 1; min-width: 200px; }
.topbar {
  flex: none;
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 600; margin-inline-end: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { display: flex; gap: 4px; }
.tabs button { border-color: transparent; background: transparent; }
.tabs button.on { background: var(--panel-2); border-color: var(--border); color: var(--text); }
.topbar .spacer { flex: 1; }
.topbar .user { color: var(--muted); font-size: 13px; }
.view { min-height: 0; overflow: auto; }

/* ---------- البث المباشر ---------- */
.live { height: 100%; display: grid; grid-template-rows: auto 1fr; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.toolbar .group { display: flex; gap: 4px; align-items: center; }
.toolbar .label { color: var(--muted); font-size: 13px; }
.grid {
  min-height: 0;
  display: grid;
  gap: 3px;
  padding: 3px;
  grid-template-columns: repeat(var(--cols, 4), 1fr);
  grid-template-rows: repeat(var(--rows, 4), 1fr);
}
.tile {
  position: relative;
  overflow: hidden;
  background: #000;
  border-radius: 4px;
  display: grid;
  place-items: center;
  container-type: size;
  cursor: pointer;
  min-height: 0;
  min-width: 0;
}
.tile video {
  aspect-ratio: var(--ar, 1.777);
  width: min(100cqw, calc(100cqh * var(--ar, 1.777)));
  object-fit: fill;
  display: block;
  background: #000;
}
.tile .name {
  position: absolute;
  inset-block-end: 6px;
  inset-inline-start: 6px;
  background: rgba(0, 0, 0, .6);
  padding: 1px 8px;
  border-radius: 4px;
  font-size: 12px;
  pointer-events: none;
  max-width: calc(100% - 12px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile .status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 8px;
  pointer-events: none;
  background: rgba(0, 0, 0, .35);
}
.tile .status:empty { display: none; }

.focus {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: #000;
  display: grid;
  grid-template-rows: auto 1fr;
}
.focus .head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.focus .head .title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.focus .tile { border-radius: 0; cursor: default; }

/* ---------- التسجيلات ---------- */
.playback { max-width: 1280px; margin: 0 auto; padding: 12px; display: grid; gap: 10px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.row label > * { color: var(--text); font-size: 15px; }
.stage {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  height: min(62dvh, 720px);
  display: grid;
  place-items: center;
}
.stage { container-type: size; }
/* الفيديو يُعرض بنسبة أبعاد الكاميرا الحقيقية (--ar) كاملاً بلا قصّ */
.stage video {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(100cqw, calc(100cqh * var(--ar, 1.7778)));
  aspect-ratio: var(--ar, 1.7778);
  height: auto;
  object-fit: fill;
  background: #000;
}
.stage video.hidden { visibility: hidden; }
.stage .msg {
  position: relative;
  z-index: 2;
  background: rgba(0, 0, 0, .65);
  padding: 8px 14px;
  border-radius: var(--radius);
  color: var(--text);
  text-align: center;
  max-width: 90%;
}
.stage .msg:empty { display: none; }
.transport { justify-content: center; }
.transport .clock { font-size: 18px; font-weight: 600; min-width: 9ch; text-align: center; }
.transport button.play { min-width: 84px; }

.timeline { direction: ltr; user-select: none; touch-action: none; }
.timeline .caption { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; direction: rtl; margin-bottom: 2px; }
.track {
  position: relative;
  height: 34px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
}
.track .rec { position: absolute; top: 0; bottom: 0; background: var(--rec); opacity: .75; min-width: 1px; }
.track .tick { position: absolute; top: 0; bottom: 0; width: 1px; background: rgba(255, 255, 255, .12); }
.track .cursor { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); pointer-events: none; }
.track .hover {
  position: absolute; top: 0; bottom: 0; width: 1px; background: var(--accent); pointer-events: none; display: none;
}
.track .hover span {
  position: absolute; top: 2px; left: 4px; background: var(--accent); color: var(--accent-ink);
  font-size: 11px; padding: 0 5px; border-radius: 3px; white-space: nowrap;
}
.track .hover.flip span { left: auto; right: 4px; }
.scale { position: relative; height: 16px; color: var(--muted); font-size: 11px; }
.scale span { position: absolute; transform: translateX(-50%); }
.scale span:first-child { transform: none; }
.scale span:last-child { transform: translateX(-100%); }
.legend { color: var(--muted); font-size: 12px; direction: rtl; }
.legend i { display: inline-block; width: 12px; height: 10px; background: var(--rec); opacity: .75; border-radius: 2px; margin-inline-end: 4px; }

/* ---------- البحث بالوجه ---------- */
.faces { max-width: 1280px; margin: 0 auto; padding: 12px; display: grid; gap: 12px; }
.faces h2 { margin: 0; font-size: 17px; }
.card, .panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; }
.panel > summary { cursor: pointer; font-weight: 600; }
.panel[open] > summary { margin-bottom: 4px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.label { color: var(--muted); font-size: 13px; }
.row .spacer { flex: 1; }
.query-faces { display: flex; flex-wrap: wrap; gap: 8px; }
.query-faces:empty { display: none; }
.qface { padding: 0; border: 2px solid var(--border); border-radius: var(--radius); overflow: hidden; background: #000; line-height: 0; }
.qface.on { border-color: var(--accent); }
.qface img { width: 96px; height: 96px; object-fit: cover; display: block; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 13px; color: var(--muted); }
.chip.on { color: var(--accent-ink); }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 4px 12px; }
.checks label, .panel .row label { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
.index-status { display: grid; gap: 2px; font-size: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
.index-status .bad { color: var(--danger); }
/* خطوات البحث المرقّمة */
.steps { gap: 0; padding: 0; }
.step { padding: 14px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.step:last-child { border-bottom: 0; }
.step-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; }
.step-n {
  width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  display: inline-grid; place-items: center; font-size: 14px; flex: none;
}
.step.go { background: var(--panel-2); border-radius: 0 0 var(--radius) var(--radius); }
button.big { font-size: 17px; padding: 10px 34px; }
.note { color: var(--muted); font-size: 13px; }
.note:empty { display: none; }
.note.ok { color: #7be0a6; }
.note.bad { color: #ff8f8f; font-size: 14px; }

/* بطاقة حالة البحث: لونها يدل على الحالة */
.job { border-radius: var(--radius); padding: 14px; display: grid; gap: 8px; border: 1px solid var(--border); border-inline-start-width: 5px; background: var(--panel); }
.job:empty { display: none; }
.job h2 { font-size: 18px; }
.job.run { border-inline-start-color: var(--accent); }
.job.ok { border-inline-start-color: var(--rec); }
.job.none, .job.stop { border-inline-start-color: var(--muted); }
.job.bad { border-inline-start-color: var(--danger); }
.job.bad h2 { color: #ff8f8f; }
.job.ok h2 { color: #7be0a6; }
.bar { height: 14px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; direction: ltr; }
.bar > div { height: 100%; background: var(--accent); min-width: 2px; transition: width .6s ease; }
.job.stop .bar > div { background: var(--muted); }
@media (prefers-reduced-motion: reduce) { .bar > div { transition: none; } }
.results-head { min-height: 36px; }
.summary { color: var(--text); }
.hits { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.hit { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; display: grid; grid-template-columns: 96px 1fr; gap: 10px; align-items: start; }
.hit img { width: 96px; height: 96px; object-fit: cover; border-radius: 6px; background: #000; }
.hit .meta { display: grid; gap: 2px; font-size: 14px; min-width: 0; }
.hit .meta .num { text-align: start; }
.hit .actions { grid-column: 1 / -1; display: flex; gap: 6px; }
.hit .actions button { flex: 1; }
.badge { display: inline-block; justify-self: start; padding: 0 8px; border-radius: 999px; font-size: 12px; margin-top: 2px; }
.badge.strong { background: #173d29; color: #7be0a6; }
.badge.likely { background: #3a3212; color: #ecd277; }
.badge.weak { background: #2a2f38; color: var(--muted); }

dialog.sheet {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  width: min(440px, calc(100vw - 32px));
}
dialog.sheet.wide { width: min(640px, calc(100vw - 32px)); display: grid; gap: 10px; }
dialog.sheet.wide:not([open]) { display: none; }
dialog.sheet::backdrop { background: rgba(0, 0, 0, .6); }
dialog.sheet h2 { margin: 0 0 8px; font-size: 18px; }
dialog.sheet p { margin: 0 0 10px; }
dialog.sheet ul { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }
dialog.sheet li { background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px; }
dialog.sheet .addr { font-size: 17px; font-weight: 600; text-align: start; user-select: all; }
dialog.sheet .hint { color: var(--muted); font-size: 13px; }

.banner { background: #3a2a12; border: 1px solid #6b4d1c; color: #f4d9a6; padding: 8px 12px; border-radius: var(--radius); }
.error-page { padding: 24px; text-align: center; color: var(--muted); }

/* ---------- الصوت ---------- */
.sound { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sound label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.focus .head { flex-wrap: wrap; }

/* ---------- عدّاد التنبيهات في التبويب ---------- */
.tabs .count {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-inline-start: 6px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.tabs .count[hidden] { display: none; }

/* ---------- نوافذ التنبيه المنبثقة ---------- */
.toasts { position: fixed; z-index: 50; inset-block-end: 12px; inset-inline-end: 12px; display: grid; gap: 8px; width: min(420px, calc(100vw - 24px)); }
.toast {
  display: grid; grid-template-columns: 120px 1fr; gap: 10px; align-items: start;
  background: var(--panel); border: 1px solid var(--border); border-inline-start: 5px solid var(--danger);
  border-radius: var(--radius); padding: 10px; box-shadow: 0 8px 30px rgba(0, 0, 0, .55);
  animation: toast-in .2s ease-out;
}
.toast.dwell { border-inline-start-color: #e0a63a; }
.toast img { width: 120px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 6px; background: #000; }
.toast .body { display: grid; gap: 4px; min-width: 0; }
.toast .row button { padding: 4px 10px; font-size: 13px; }
.toast:not(:has(img)) { grid-template-columns: 1fr; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

/* ---------- صفحات التنبيهات والزونات والمستخدمين ---------- */
.alerts-page, .users-page { max-width: 1280px; margin: 0 auto; padding: 12px; display: grid; gap: 12px; }
.alerts-body { display: grid; gap: 12px; }
.alerts-page h2, .users-page h2 { margin: 0; font-size: 17px; }
.seg { display: flex; gap: 4px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px; justify-self: start; }
.seg[hidden] { display: none; }
.seg button { border-color: transparent; background: transparent; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.card.empty { text-align: center; justify-items: center; }
.card.empty[hidden] { display: none; }
.alert-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.alert-card { background: var(--panel); border: 1px solid var(--border); border-inline-start: 4px solid var(--danger); border-radius: var(--radius); padding: 10px; display: grid; gap: 8px; }
.alert-card.dwell { border-inline-start-color: #e0a63a; }
.alert-card img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 6px; background: #000; cursor: zoom-in; }
.alert-card .meta { display: grid; gap: 2px; }
.alert-card .meta .num { text-align: start; }
.kind { justify-self: start; font-size: 12px; padding: 0 8px; border-radius: 999px; background: #3d1d1d; color: #ff9b9b; }
.kind.dwell { background: #3a3212; color: #ecd277; }
.kind.admin { background: #172d4d; color: #8fbaff; }
.kind.user { background: #2a2f38; color: var(--muted); }
img.full { width: 100%; border-radius: 6px; background: #000; display: block; }
button.danger, button.primary.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.danger-text { color: #ff8f8f; }
.notify { gap: 6px; }

.zone-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; }
.zone-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; display: grid; gap: 8px; }
.zone-card.off { opacity: .7; }
.zone-card .meta { display: grid; gap: 4px; }
.zone-card .row button { padding: 5px 10px; font-size: 14px; }
.rules { margin: 0; padding-inline-start: 18px; font-size: 14px; }

.zone-fig { position: relative; width: 100%; aspect-ratio: var(--ar, 1.7778); background: #000; border-radius: 6px; overflow: hidden; }
.zone-fig img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; user-select: none; }
.zone-fig svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.zone-fig .zone { fill: rgba(255, 196, 0, .22); stroke: #ffc400; stroke-width: 2.5; }
.zone-fig .zone.editing { fill: rgba(61, 139, 253, .25); stroke: #5aa0ff; }
.zone-fig .other { fill: rgba(255, 255, 255, .08); stroke: rgba(255, 255, 255, .5); stroke-width: 1.5; stroke-dasharray: 4 3; }
.zone-fig polyline { fill: none; }
.zone-fig.drawing { cursor: crosshair; touch-action: none; }
.zone-fig .dots { position: absolute; inset: 0; }
.zone-fig .dot {
  position: absolute; width: 18px; height: 18px; margin: -9px 0 0 -9px; border-radius: 50%;
  background: #5aa0ff; border: 2px solid #fff; cursor: grab; touch-action: none;
}
.zone-editor { max-height: calc(100dvh - 32px); overflow: auto; }
.zone-editor .choice { display: grid; gap: 6px; background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.zone-editor .choice label, .user-editor label { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.zone-editor .choice label.sub { padding-inline-start: 26px; color: var(--muted); font-size: 13px; }
label.field { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
label.field input { font-size: 16px; color: var(--text); }
dialog .step-title { margin-top: 6px; }

.user-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 10px; }
.user-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.user-card .meta { display: grid; gap: 3px; }
.user-editor { max-height: calc(100dvh - 32px); overflow: auto; }
.perm-section { display: grid; gap: 8px; }
.perm-section[hidden] { display: none; }
.chips.dim { opacity: .5; }

@media (max-width: 640px) {
  :root { --bar-h: 48px; }
  .topbar .user, .topbar .brand { display: none; }
  /* ثلاثة تبويبات وزرّان في عرض الهاتف: نضيّق الحشو، ونسمح بالتمرير احتياطاً */
  .topbar { gap: 4px; padding: 0 6px; overflow-x: auto; }
  .topbar button { padding: 6px 8px; font-size: 14px; }
  .tabs { gap: 2px; }
  .hits { grid-template-columns: 1fr; }
  .step { padding: 12px 10px; }
  button.big { width: 100%; }
  .playback { padding: 8px; }
  .stage { height: 46dvh; }
  .transport button { padding: 7px 9px; }
  .toolbar { padding: 6px 8px; }
  /* خمسة تبويبات لا تتسع مع الأزرار: التبويبات في سطر خاص بها تحت الأزرار */
  .topbar { height: auto; flex-wrap: wrap; padding: 4px 6px; row-gap: 2px; }
  .topbar .tabs { order: 5; width: 100%; overflow-x: auto; }
  .alerts-page, .users-page { padding: 8px; }
  .zone-list, .user-list, .alert-grid { grid-template-columns: 1fr; }
  .toast { grid-template-columns: 90px 1fr; }
  .toast img { width: 90px; }
}
.perm-section .checks { grid-template-columns: 1fr; }
.push-box { display: grid; gap: 6px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 8px 0; }
.push-box .addr { font-size: 16px; font-weight: 600; margin: 4px 0; text-align: start; }
.push-box .addr a { color: #8fbaff; }
.howto { margin: 0; padding-inline-start: 20px; display: grid; gap: 4px; font-size: 14px; }
