:root {
  --bg: #0a0e1a;
  --bg-2: #0f1420;
  --surface: #151c2e;
  --surface-2: #1c2540;
  --surface-3: #243056;
  --border: rgba(255,255,255,0.08);
  --border-strong: rgba(255,255,255,0.16);
  --text: #f4f6fb;
  --text-2: #c2cae0;
  --muted: #8593b3;
  --accent: #4a8fff;
  --accent-2: #2f74ff;
  --accent-soft: rgba(74,143,255,0.14);
  --danger: #ff5c7a;
  --danger-soft: rgba(255,92,122,0.12);
  --success: #4ed8a3;
  --warning: #ffb547;
  --radius: 14px;
  --radius-lg: 18px;
  --radius-sm: 10px;
  --shadow-sm: 0 2px 8px rgba(0,0,0,.25);
  --shadow: 0 8px 28px rgba(0,0,0,.4);
  --shadow-lg: 0 24px 48px rgba(0,0,0,.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); overscroll-behavior-y: none; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 800px at 80% -10%, rgba(74,143,255,0.12), transparent 60%),
    radial-gradient(900px 600px at -10% 100%, rgba(46,108,200,0.08), transparent 60%),
    var(--bg);
}

/* ---------- atoms ---------- */
button {
  font: inherit; color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: background .15s, border-color .15s, transform .1s;
  font-weight: 500;
  white-space: nowrap;
}
button:hover { background: var(--surface-3); border-color: var(--border-strong); }
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }
button.primary { background: var(--accent); border-color: var(--accent); color: white; }
button.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.ghost { background: transparent; border-color: var(--border); }
button.ghost:hover { background: var(--surface-2); }
button.danger { background: transparent; border-color: rgba(255,92,122,0.3); color: var(--danger); }
button.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
button.icon { padding: 8px; width: 38px; height: 38px; border-radius: 50%; }

input, select, textarea {
  font: inherit; color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
  transition: border-color .15s, background .15s;
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); background: var(--surface); }

a { color: var(--accent); text-decoration: none; }
hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }

/* ---------- shell ---------- */
.app {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding-top: var(--safe-top);
}

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: rgba(10,14,26,0.78);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; letter-spacing: 0.2px;
}
.brand .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 4px rgba(78,216,163,0.18); }
.brand .dot.off { background: var(--muted); box-shadow: none; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  background: transparent; border: 0; padding: 8px 14px; border-radius: 999px;
  color: var(--muted); font-weight: 500;
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--accent-soft); color: var(--accent); }
.spacer { flex: 1; }
.user-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 4px 4px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
}
.user-pill .avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #6f5cff);
  display: grid; place-items: center; color: white; font-weight: 700; font-size: 12px;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border);
  display: grid; place-items: center; cursor: pointer;
  color: var(--text-2);
  transition: all .15s;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.active { background: var(--accent-soft); color: var(--accent); border-color: rgba(74,143,255,0.35); }

.main {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 16px 24px;
}

/* desktop: hide mobile nav, show topbar tabs */
.nav-mobile { display: none; }
.tabs.desktop { display: flex; }

/* ---------- camera tiles ---------- */
.section-head {
  display: flex; align-items: end; justify-content: space-between;
  margin: 4px 0 14px;
}
.section-head h2 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.section-head .sub { color: var(--muted); font-size: 13px; }

.cam-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
}
.cam-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: transform .15s, box-shadow .15s, border-color .15s;
  position: relative;
}
.cam-tile:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }
.cam-thumb {
  aspect-ratio: 16/9; background: #000; position: relative; overflow: hidden;
}
.cam-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-thumb .overlay-top {
  position: absolute; inset: 12px 12px auto 12px;
  display: flex; align-items: center; justify-content: space-between;
  pointer-events: none;
}
.cam-thumb .overlay-bottom {
  position: absolute; inset: auto 12px 12px 12px;
  display: flex; align-items: center; justify-content: space-between;
  pointer-events: none;
}
.live-pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(8px);
  padding: 5px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 700; letter-spacing: 0.08em;
  color: white; text-transform: uppercase;
}
.live-pill .pulse {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--danger);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.6); }
}
.battery-pill {
  display: inline-flex; align-items: center; gap: 5px;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(8px);
  padding: 5px 9px; border-radius: 999px;
  font-size: 12px; color: white; font-weight: 600;
}
.battery-pill.low { color: var(--warning); }
.cam-info {
  padding: 12px 14px 14px;
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.cam-name { font-weight: 600; font-size: 16px; }
.cam-last { color: var(--muted); font-size: 12px; margin-top: 2px; }
.cam-actions { display: flex; gap: 6px; }
.cam-actions .icon-btn { background: var(--surface-2); }

/* ---------- events feed ---------- */
.filter-bar {
  display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px;
  align-items: center;
}
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px; color: var(--text-2);
  cursor: pointer;
  transition: all .15s;
  user-select: none;
}
.chip:hover { background: var(--surface-3); }
.chip.active { background: var(--accent-soft); color: var(--accent); border-color: rgba(74,143,255,0.4); }
.chip-select { position: relative; }
.chip-select select {
  appearance: none; -webkit-appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 7px 32px 7px 12px;
  font-size: 13px; color: var(--text-2);
  cursor: pointer;
}
.chip-select::after {
  content: ''; position: absolute; right: 12px; top: 50%; pointer-events: none;
  width: 6px; height: 6px; border-right: 1.5px solid var(--text-2); border-bottom: 1.5px solid var(--text-2);
  transform: translateY(-65%) rotate(45deg);
}

.day-group { margin-bottom: 22px; }
.day-head {
  position: sticky; top: 60px; z-index: 5;
  background: rgba(10,14,26,0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 10px 0 8px;
  margin-bottom: 10px;
  font-size: 13px; font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.04em; text-transform: uppercase;
  border-bottom: 1px solid var(--border);
}
.event-list { display: grid; gap: 10px; }
.event-row {
  display: grid;
  grid-template-columns: 132px 1fr auto;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  align-items: center;
  cursor: pointer;
  transition: transform .12s, border-color .15s, background .15s;
}
.event-row:hover { border-color: var(--border-strong); background: var(--surface-2); }
.event-row:active { transform: scale(0.99); }
.event-thumb {
  aspect-ratio: 16/9; background: #000; border-radius: var(--radius-sm);
  overflow: hidden; position: relative;
}
.event-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.event-thumb .play {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: linear-gradient(180deg, transparent 30%, rgba(0,0,0,0.45));
  opacity: 0; transition: opacity .15s;
}
.event-row:hover .event-thumb .play { opacity: 1; }
.event-thumb .play .play-circle {
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.92);
  display: grid; place-items: center;
  color: var(--bg);
}
.event-thumb .duration {
  position: absolute; bottom: 6px; right: 6px;
  background: rgba(0,0,0,0.7);
  padding: 2px 6px; border-radius: 5px;
  font-size: 11px; font-weight: 600; color: white;
}
.event-meta { min-width: 0; }
.event-title {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  font-weight: 600; font-size: 15px;
  margin-bottom: 4px;
}
.event-title .star {
  color: var(--warning); font-size: 13px;
}
.event-time { color: var(--muted); font-size: 13px; }
.event-meta .row-2 {
  display: flex; gap: 8px; margin-top: 4px; align-items: center;
  flex-wrap: wrap;
}
.kind-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
  text-transform: capitalize;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.kind-chip.motion { color: var(--warning); border-color: rgba(255,181,71,0.3); background: rgba(255,181,71,0.08); }
.kind-chip.ding { color: var(--accent); border-color: rgba(74,143,255,0.35); background: var(--accent-soft); }
.kind-chip.manual { color: #b797ff; border-color: rgba(183,151,255,0.3); background: rgba(183,151,255,0.08); }
.event-meta .size { color: var(--muted); font-size: 12px; }
.event-actions { display: flex; gap: 4px; }

/* ---------- player ---------- */
.dialog-bg {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(2,4,12,0.78);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  animation: fade-in .2s;
}
@keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }
.dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 880px;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  max-height: calc(100dvh - 32px);
  overflow: hidden;
  animation: slide-up .25s ease-out;
}
@keyframes slide-up { from { transform: translateY(20px); opacity: 0 } to { transform: none; opacity: 1 } }
.dialog .head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.dialog .head .title { font-weight: 600; }
.dialog .head .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.dialog video {
  width: 100%; background: #000; display: block;
  aspect-ratio: 16/9;
  max-height: 65dvh;
  object-fit: contain;
}
.dialog .foot {
  display: flex; gap: 8px; padding: 12px 16px;
  background: var(--bg-2);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

/* ---------- login ---------- */
.login-screen {
  min-height: 100dvh; display: grid; place-items: center;
  padding: 24px;
  background:
    radial-gradient(800px 500px at 50% 0%, rgba(74,143,255,0.18), transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow);
}
.login-card .logo {
  width: 56px; height: 56px; border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), #6f5cff);
  display: grid; place-items: center;
  margin: 0 auto 18px;
  box-shadow: 0 6px 20px rgba(74,143,255,0.4);
}
.login-card h1 {
  margin: 0 0 4px; text-align: center; font-size: 22px; letter-spacing: -0.01em;
}
.login-card .lead {
  text-align: center; color: var(--muted); margin-bottom: 22px; font-size: 13px;
}
.login-card label {
  display: block; margin: 14px 0 6px;
  font-size: 12px; font-weight: 600; color: var(--text-2);
  text-transform: uppercase; letter-spacing: 0.06em;
}
.login-card .err {
  color: var(--danger); margin-top: 10px; font-size: 13px;
  min-height: 18px; text-align: center;
}
.login-card button { width: 100%; padding: 12px; margin-top: 18px; }

/* ---------- empty / loading ---------- */
.empty-state {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-state .ico {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--surface-2);
  display: grid; place-items: center;
  margin: 0 auto 14px; color: var(--text-2);
}
.empty-state h3 { color: var(--text); margin: 0 0 6px; font-size: 16px; }

.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { 0% { background-position: 200% 0 } 100% { background-position: -200% 0 } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + var(--safe-bottom));
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  padding: 10px 18px;
  box-shadow: var(--shadow-lg);
  z-index: 60;
  font-size: 14px;
  animation: toast-in .2s;
}
@keyframes toast-in { from { transform: translate(-50%, 10px); opacity: 0 } to { transform: translateX(-50%); opacity: 1 } }

/* ---------- mobile ---------- */
@media (max-width: 720px) {
  .tabs.desktop { display: none; }
  .nav-mobile {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    position: sticky; bottom: 0; z-index: 20;
    padding: 10px 16px calc(10px + var(--safe-bottom));
    background: rgba(10,14,26,0.85);
    backdrop-filter: saturate(160%) blur(14px);
    -webkit-backdrop-filter: saturate(160%) blur(14px);
    border-top: 1px solid var(--border);
  }
  .nav-mobile button {
    background: transparent; border: 0; padding: 10px;
    border-radius: 12px;
    color: var(--muted);
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 11px; font-weight: 600;
  }
  .nav-mobile button.active { background: var(--accent-soft); color: var(--accent); }
  .topbar { padding: 10px 12px; }
  .main { padding: 14px 12px 24px; }
  .user-pill .label { display: none; }
  .user-pill { padding: 4px; }
  .event-row { grid-template-columns: 110px 1fr; }
  .event-actions { grid-column: 1 / -1; justify-content: flex-end; }
  .day-head { top: 56px; }
  .dialog { max-height: 90dvh; align-self: end; border-radius: 18px 18px 0 0; }
  .dialog-bg { padding: 0; align-items: flex-end; }
}
