/* Zinacaftan CCTV — IDPS dark theme, flat, no gradients. */
:root {
  --bg: #202020;
  --panel: #2c2c2c;
  --panel2: #323232;
  --line: #3a3a3a;
  --text: #ffffff;
  --muted: #a3a3a3;
  --accent: #4cc2ff;
  --on-accent: #000000;
  --danger: #ff7a7a;
  --ok: #6ccb5f;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; overflow-x: clip; }
@supports not (overflow: clip) {
  html, body { overflow-x: hidden; }
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  min-height: 100vh;
}

/* ---- Boot screen (also styled inline-independent: shown before CSS loads is fine) ---- */
#zc-boot {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
}
#zc-boot-logo { max-width: min(78vw, 340px); height: auto; }
#zc-boot-name { font-size: 30px; font-weight: 800; letter-spacing: 1px; }
#zc-boot-text { color: var(--muted); font-size: 17px; }
#zc-boot-repair {
  margin-top: 8px;
  background: var(--accent);
  color: var(--on-accent);
  border: none;
  border-radius: 10px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

/* ---- Layout ---- */
#app { min-height: 100vh; }
.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px 40px; }

header.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
header.top img.logo { height: 44px; width: auto; }
header.top .title { font-size: 22px; font-weight: 800; flex: 1; }
header.top .subtitle { color: var(--muted); font-size: 13px; }
/* The action buttons live in .header-actions so the mobile layout can put
   them on their own row. On desktop the wrapper disappears from layout. */
.header-actions { display: contents; }
.role-badge {
  font-size: 13px;
  font-weight: 700;
  border-radius: 999px;
  padding: 4px 12px;
  white-space: nowrap;
  border: 1px solid var(--accent);
  color: var(--accent);
}
.role-badge.viewer { border-color: var(--line); color: var(--muted); }

/* ---- Buttons / inputs ---- */
button {
  font-family: inherit;
  font-size: 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  padding: 10px 16px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 700; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button:disabled { opacity: 0.5; cursor: default; }

input, select {
  font-family: inherit;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
label { display: block; color: var(--muted); font-size: 14px; margin: 12px 0 4px; }

/* ---- Filter chips ---- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; padding: 18px 0 6px; }
.chip {
  border-radius: 999px;
  padding: 8px 18px;
  background: var(--panel);
}
.chip.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 700; }

/* ---- Camera grid ---- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  margin-top: 14px;
}
.cam {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  cursor: pointer;
  text-align: right;
  padding: 0;
}
.cam:hover { border-color: var(--accent); }
.cam-view {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #161616;
  overflow: hidden;
}
/* Live picture: the snapshot <img> sits on top of the placeholder; it only
   shows once a frame has actually loaded (.live), otherwise the tile keeps
   the honest «بانتظار الربط» state. */
.cam-view .cam-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}
.cam.live .cam-img { display: block; }
.cam.live .cam-ph { display: none; }
.cam-view .cam-ph {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
}
.cam-view .cam-ph svg { width: 56px; height: 56px; opacity: 0.7; }
.cam-view .state { font-size: 15px; }
.cam-meta { padding: 12px 14px; display: flex; align-items: center; gap: 8px; }
.cam-meta .names { flex: 1; min-width: 0; }
.cam-meta .cam-name { font-weight: 700; font-size: 17px; }
.cam-meta .shop-name { color: var(--muted); font-size: 13px; }
.badge {
  font-size: 12px;
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
  background: var(--panel2);
  border: 1px solid var(--line);
  color: var(--muted);
}
.badge.audio { color: var(--accent); border-color: var(--accent); }
.badge.pinned { color: #f5c542; border-color: #f5c542; }

.empty {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  text-align: center;
  color: var(--muted);
  margin-top: 14px;
}

/* ---- Lock screen ---- */
.lock {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
}
.lock img.logo { max-width: min(78vw, 320px); height: auto; }
.lock h1 { font-size: 26px; margin: 6px 0 0; }
.lock p { color: var(--muted); max-width: 420px; margin: 0 0 8px; }
.lock .pin-box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  width: min(92vw, 380px);
}
.lock input { text-align: center; font-size: 24px; letter-spacing: 10px; direction: ltr; }
.lock button { width: 100%; margin-top: 14px; }
.lock .error { color: var(--danger); min-height: 22px; font-size: 14px; }
.lock .hint { color: var(--muted); font-size: 13px; margin-top: 10px; }

/* ---- Enlarged camera overlay ---- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
  padding: 16px;
  overflow-y: auto;
}
.overlay .box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  max-width: 960px;
  width: 100%;
  margin: auto;
  overflow: hidden;
}
.overlay .big-view {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #161616;
  overflow: hidden;
}
/* Live MJPEG <img>: it paints over the placeholder as soon as frames
   arrive (an MJPEG <img> never fires "load", so the placeholder stays
   behind it until then). object-fit: contain — the whole frame, no crop. */
.overlay .big-view img#big-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  z-index: 1;
}
.overlay .big-view .big-ph {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
}
.overlay .big-view .big-ph svg { width: 84px; height: 84px; opacity: 0.7; }
.overlay .controls { padding: 16px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.overlay .controls .spacer { flex: 1; }
.overlay .info { padding: 0 16px 16px; color: var(--muted); font-size: 14px; }
.overlay .info b { color: var(--text); }

/* ---- Fullscreen (double-tap, Khalid 3 Dec) ----
   .fs always fills the viewport on its own: the native Fullscreen API is
   only a bonus where it works (desktop / Android Chrome). In the APK
   WebView and on iOS the class alone does the job. Controls + info hide
   while fullscreen; double-tap again (or the system back gesture) exits. */
.overlay.fs { padding: 0; background: #000; }
.overlay.fs .box { max-width: none; width: 100%; height: 100%; margin: 0; border: 0; border-radius: 0; display: flex; flex-direction: column; }
.overlay.fs .big-view { flex: 1; aspect-ratio: auto; min-height: 0; }
.overlay.fs .controls, .overlay.fs .info { display: none; }

/* ---- Settings ---- */
.settings h2 { font-size: 20px; margin: 26px 0 6px; }
.settings .card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  margin-top: 12px;
}
.settings .row { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.settings .row > div { flex: 1 1 200px; }
.settings .row button { flex: 0 0 auto; }
.settings .cam-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}
.settings .cam-row:first-of-type { border-top: none; }
.settings .cam-row .nm { flex: 1 1 180px; }
.settings .cam-row label { margin: 0 0 2px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; }
.check input { width: auto; }
.muted { color: var(--muted); font-size: 14px; }
.topbar { display: flex; gap: 10px; align-items: center; padding: 16px 0 0; }
.topbar h1 { flex: 1; font-size: 22px; margin: 0; }

/* ---- Toast ---- */
#zc-toast {
  position: fixed;
  bottom: 20px;
  right: 50%;
  transform: translateX(50%);
  z-index: 90;
  background: var(--panel2);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 18px;
  max-width: 92vw;
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
}

/* ---- TV / wide screens ---- */
@media (min-width: 1100px) {
  .grid { grid-template-columns: repeat(4, 1fr); }
}

/* ---- Small screens: the header holds logo + title + badge + 3 buttons,
   which together are ~585px wide and forced the whole page wider than a
   phone viewport (horizontal overflow — the shifted/cut layout Khalid saw
   on his phone, same in the APK and in Chrome). The header gets TWO clean
   rows: logo + title + badge on top, all three buttons on their own row
   below. (A plain flex-wrap made a mess: the title's min-width:0 let it
   collapse and its text paint over the badge and logo.) The header also
   sits under the phone's translucent status bar, so it gets safe-area
   top padding. ---- */
@media (max-width: 640px) {
  header.top { flex-wrap: wrap; row-gap: 8px; padding: max(10px, env(safe-area-inset-top)) 12px 10px; }
  header.top .subtitle { display: none; }
  header.top .title { font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  header.top img.logo { height: 36px; flex: none; }
  .role-badge { font-size: 12px; padding: 3px 10px; flex: none; }
  .header-actions { display: flex; flex-basis: 100%; gap: 8px; }
  .header-actions button { flex: 1; padding: 8px 6px; font-size: 14px; }
}

/* ---- TV distance viewing (Khalid's choice A: 8 cameras, 4x2 grid) ----
   On a TV the tiles are big but the text must stay readable from metres
   away, so tile text scales up on very wide screens. Phone layout untouched. */
@media (min-width: 1600px) {
  .cam-meta .cam-name { font-size: 24px; }
  .cam-meta .shop-name { font-size: 17px; }
  .cam-view .state { font-size: 20px; }
  .badge { font-size: 15px; }
  .cam-view svg { width: 72px; height: 72px; }
}
