:root {
  --bg: #0b1020;
  --surface: #141b2e;
  --surface-2: #1c2540;
  --line: #2a3556;
  --text: #eff2fa;
  --dim: #9aa4bf;
  --primary: #4f7cff;
  --online: #2ecc71;
  --offline: #e5484d;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* الخاصية hidden يجب أن تغلب أي display في القواعد الأخرى */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 "Segoe UI", Tahoma, "Noto Sans Arabic", sans-serif;
}

button, input, select { font: inherit; color: inherit; }

button {
  border: 0;
  border-radius: 10px;
  padding: 9px 16px;
  cursor: pointer;
  min-height: 40px;
  transition: filter .15s, background .15s;
}
button:hover { filter: brightness(1.12); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); color: #fff; font-weight: 600; }
button.ghost { background: transparent; border: 1px solid var(--line); }
button.danger { background: transparent; border: 1px solid var(--offline); color: var(--offline); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

input, select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  min-height: 40px;
}

label { display: grid; gap: 6px; color: var(--dim); font-size: 14px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.error { color: var(--offline); min-height: 1.4em; margin: 4px 0; font-size: 14px; }
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--primary); }

/* الدخول */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); padding: 28px; display: grid; gap: 14px; }

/* الهيكل */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.tabs { display: flex; gap: 6px; margin-inline-start: auto; }
.tab { background: transparent; color: var(--dim); }
.tab.active { background: var(--surface-2); color: var(--text); }
.me { display: flex; align-items: center; gap: 10px; color: var(--dim); }

.layout {
  display: grid; grid-template-columns: 320px 1fr; gap: 16px;
  padding: 16px 20px; flex: 1; min-height: 0;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }

.sidebar { padding: 12px; display: flex; flex-direction: column; gap: 10px; max-height: calc(100vh - 100px); }
.sidebar-head { display: flex; gap: 8px; }
.sidebar-head input { flex: 1; }
.user-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; display: grid; gap: 6px; }
.user-list li button {
  width: 100%; text-align: start; background: var(--surface-2); padding: 10px 12px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.user-list li button.selected { outline: 2px solid var(--primary); }
.user-list small { color: var(--dim); display: block; }

.badge { font-size: 12px; padding: 2px 10px; border-radius: 99px; white-space: nowrap; }
.badge.admin { background: rgba(79,124,255,.2); color: #9db4ff; }
.badge.off { background: rgba(229,72,77,.18); color: var(--offline); }
.badge.on { background: rgba(46,204,113,.16); color: var(--online); }

.panel { padding: 18px; display: flex; flex-direction: column; gap: 16px; min-height: 300px; }
.empty { color: var(--dim); margin: auto; text-align: center; }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.panel-head h2 { margin: 0; font-size: 20px; flex: 1; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.perm-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.perm-tools input { flex: 1; min-width: 200px; }
.counter { color: var(--dim); font-size: 14px; }

.stations { display: grid; gap: 8px; overflow-y: auto; max-height: calc(100vh - 330px); padding-inline-end: 4px; }
.station {
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 12px;
  padding: 10px 14px; background: var(--surface-2); border-radius: 12px; border: 1px solid transparent;
}
.station.allowed { border-color: rgba(79,124,255,.5); }
.station .name { font-weight: 600; }
.station .meta { color: var(--dim); font-size: 13px; }
.station .cams { color: var(--primary); font-size: 13px; }

.switch { position: relative; width: 46px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch span {
  position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: background .15s;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; inset-inline-start: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; transition: transform .15s;
}
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(-20px); }
[dir="ltr"] .switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

.save-bar {
  position: sticky; bottom: 0; display: flex; gap: 10px; align-items: center; justify-content: flex-end;
  padding-top: 10px; border-top: 1px solid var(--line); background: var(--surface);
}
.dirty { color: #f5a524; font-size: 14px; margin-inline-end: auto; }

.modal {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 22px; width: min(560px, calc(100% - 24px));
}
.modal::backdrop { background: rgba(0,0,0,.6); }
.modal h3 { margin: 0 0 14px; }
#modal-body { display: grid; gap: 12px; max-height: 60vh; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.cam-list { display: grid; gap: 4px; }
.cam-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2);
  cursor: pointer;
}
.cam-row input { width: 20px; height: 20px; min-height: 0; flex: none; }
.cam-row b { min-width: 52px; }

.audit { margin: 16px 20px; padding: 8px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; text-align: start; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--dim); font-weight: 600; }

.toast {
  position: fixed; bottom: 20px; inset-inline-start: 50%; transform: translateX(50%);
  background: var(--surface-2); border: 1px solid var(--line); padding: 10px 18px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.toast.show { opacity: 1; }
.spinner { color: var(--dim); padding: 20px; text-align: center; }

/* إعدادات الحساب */
.settings { margin: 16px 20px; padding: 22px; max-width: 620px; }
.settings h2 { margin: 0 0 6px; font-size: 19px; }
.hint { color: var(--dim); font-size: 14px; margin: 0 0 14px; }
.settings-form { display: grid; gap: 14px; }
.ok { color: var(--online); }

/* مربعات الاختيار لا تأخذ عرض الحقول النصية */
.perm-tools input[type="checkbox"] { flex: none; width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--primary); }
