/* ===== CUSTOM PROPERTIES ===== */
:root {
  --primary: #0EA5A0;
  --primary-dark: #0A7A76;
  --primary-light: #ECFDF8;
  --primary-faint: rgba(14, 165, 160, 0.12);
  --text: #111827;
  --text-muted: #6B7280;
  --text-faint: #9CA3AF;
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --border: #E5E7EB;
  --danger: #EF4444;
  --danger-light: #FEF2F2;
  --online: #10B981;
  --nav-height: 60px;
  --topbar-height: 56px;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.07), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, p { margin: 0; }

button, input, textarea, a {
  font: inherit;
  color: inherit;
}

button { cursor: pointer; border: none; background: none; padding: 0; }
a { text-decoration: none; color: inherit; }

[hidden], .hidden { display: none !important; }

/* ===== UTILITIES ===== */
.primary { color: var(--primary); }

.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 52px;
  border-radius: 14px;
  padding: 0 24px;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  transition: background 0.15s, transform 0.1s;
}
.btn-primary:active { background: var(--primary-dark); transform: scale(0.98); }

.btn-secondary {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 52px;
  border-radius: 14px;
  padding: 0 24px;
  background: var(--border);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.btn-secondary:active { opacity: 0.75; }

.btn-full { width: 100%; }
.btn-arrow { width: 18px; height: 18px; flex-shrink: 0; }

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  color: var(--text);
  flex-shrink: 0;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:active { background: var(--border); }
.icon-btn.is-active { color: var(--primary); background: var(--primary-faint); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.error-text {
  color: var(--danger);
  font-size: 13px;
  margin-top: 4px;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--online);
  flex-shrink: 0;
}
.status-dot-offline { background: var(--danger); }
.status-dot-unknown { background: var(--text-faint); }

/* ===== LOGIN OVERLAY ===== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  padding: 0;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(4px);
}

.login-card {
  width: 100%;
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 24px 24px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: var(--shadow-lg);
}

.login-brand {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
}
.login-icon { width: 60px; height: 72px; }

.login-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-align: center;
}
.login-sub {
  font-size: 14px;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.field label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
.field input {
  height: 48px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0 14px;
  font-size: 16px;
  background: var(--bg);
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus { border-color: var(--primary); }

#login-form .btn-primary { margin-top: 8px; }

/* ===== APP SHELL ===== */
.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
}

/* ===== TOPBAR ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-height);
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 8px;
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.topbar h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-solo {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.topbar-live {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.live-topbar-center {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}
.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.live-dot-on { background: var(--danger); animation: livePulse 1.4s ease-in-out infinite; }
.live-dot-off { background: var(--text-faint); }

@keyframes livePulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ===== TAB PANELS ===== */
.tab-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.scroll-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ===== HOME TAB ===== */
.camera-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
}
.camera-card:active { opacity: 0.8; }

.cam-thumb {
  width: 46px;
  height: 46px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-faint);
}
.cam-thumb svg { width: 22px; height: 22px; }

.cam-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.cam-name {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cam-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text-muted);
}

.chevron { width: 18px; height: 18px; color: var(--text-faint); flex-shrink: 0; }

.stats-card {
  display: flex;
  align-items: stretch;
  padding: 16px;
}
.stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
}
.stat-icon {
  width: 22px;
  height: 22px;
  margin-bottom: 2px;
}
.stat-icon-motion { color: var(--primary); }
.stat-icon-time { color: var(--text-muted); }
.stat-icon-shield { color: var(--primary); }
.stat-value {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.stat-value-ok { color: var(--primary); }
.stat-value-warn { color: #F59E0B; }
.stat-label {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.3;
}
.stat-label.primary { color: var(--primary); }
.stat-sep {
  width: 1px;
  background: var(--border);
  margin: 4px 0;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}
.section-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}
.link-btn {
  font-size: 14px;
  color: var(--primary);
  font-weight: 500;
}
.link-btn:active { opacity: 0.6; }

.event-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.event-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 10px 12px;
  cursor: pointer;
}
.event-item:active { opacity: 0.75; }

.event-thumb {
  width: 70px;
  height: 52px;
  border-radius: 8px;
  background: #1A2535;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.event-thumb-icon {
  color: rgba(255,255,255,0.5);
  width: 22px;
  height: 22px;
}
.event-thumb-badge {
  position: absolute;
  bottom: 4px;
  left: 4px;
  background: rgba(0,0,0,0.55);
  border-radius: 4px;
  padding: 1px 4px;
  font-size: 10px;
  font-weight: 600;
  color: #fff;
}
.event-thumb-play {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255,255,255,0.85);
}
.event-thumb-play svg { width: 28px; height: 28px; }

.event-info { flex: 1; min-width: 0; }
.event-time {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.event-type {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.event-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  color: var(--text-faint);
}
.event-actions svg { width: 18px; height: 18px; }

.event-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 32px 16px;
}
.event-empty-icon {
  display: block;
  margin: 0 auto 12px;
  width: 40px;
  height: 40px;
  color: var(--text-faint);
}

/* ===== CLIPS TAB ===== */
.clip-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.clip-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 10px 12px;
  cursor: pointer;
}
.clip-item:active { opacity: 0.75; }

.clip-thumb {
  width: 88px;
  height: 62px;
  border-radius: 8px;
  background: #1A2535;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}
.clip-thumb video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #1A2535;
  display: block;
}
.clip-thumb-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.2);
}
.clip-thumb-overlay svg {
  width: 28px;
  height: 28px;
  color: rgba(255,255,255,0.9);
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.4));
}
.clip-thumb-badge {
  position: absolute;
  bottom: 4px;
  left: 4px;
  background: rgba(0,0,0,0.6);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 10px;
  font-weight: 600;
  color: #fff;
}

.clip-info { flex: 1; min-width: 0; }
.clip-time {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}
.clip-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.clip-size {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 1px;
}

.clip-chevron {
  width: 18px;
  height: 18px;
  color: var(--text-faint);
  flex-shrink: 0;
}

.clip-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 48px 24px;
}
.clip-empty svg {
  display: block;
  margin: 0 auto 12px;
  width: 44px;
  height: 44px;
  color: var(--text-faint);
}
.clip-empty strong {
  display: block;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

/* ===== LIVE TAB ===== */
.tab-live {
  background: #000;
}
.tab-live .topbar {
  background: rgba(255,255,255,0.96);
  backdrop-filter: blur(10px);
}

.live-video-wrap {
  position: relative;
  width: 100%;
  background: #000;
  flex-shrink: 0;
  /* 16:9 aspect ratio */
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.live-video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  display: block;
}

.live-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.live-pill {
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  border-radius: 5px;
  padding: 2px 7px;
}
.live-ts {
  font-size: 12px;
  font-weight: 500;
  color: rgba(255,255,255,0.85);
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.hd-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0,0,0,0.45);
  color: rgba(255,255,255,0.9);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-radius: 5px;
  padding: 2px 7px;
  border: 1px solid rgba(255,255,255,0.2);
}

.live-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: #0D1520;
  color: rgba(255,255,255,0.55);
}
.live-empty-icon {
  width: 48px;
  height: 48px;
  opacity: 0.4;
}
.live-empty p {
  font-size: 14px;
}

.live-controls {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 8px;
}
.live-ctrl {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 6px 0;
  border-radius: 10px;
}
.live-ctrl svg { width: 24px; height: 24px; color: var(--text); }
.live-ctrl span { font-size: 11px; color: var(--text-muted); font-weight: 500; }
.live-ctrl:active { background: var(--bg); }
.live-ctrl-disabled { opacity: 0.4; }
.live-ctrl-disabled:active { background: none; }
.live-ctrl-active svg { color: var(--primary); }

.live-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.secure-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: calc(12px + var(--safe-bottom)) 16px 12px;
  font-size: 12px;
  color: rgba(255,255,255,0.45);
  background: #000;
}
.secure-footer svg { width: 14px; height: 14px; }

/* ===== SETTINGS TAB ===== */
#tab-settings .topbar { background: var(--bg); border-bottom: none; }
#tab-settings { background: var(--bg); }

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.settings-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 4px;
}

.settings-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  padding: 0 16px;
  gap: 12px;
}
.setting-row-col {
  flex-direction: column;
  align-items: flex-start;
  padding: 0;
}
.setting-key {
  font-size: 15px;
  color: var(--text);
  flex-shrink: 0;
}
.setting-val-input {
  border: none;
  outline: none;
  text-align: right;
  font-size: 15px;
  color: var(--text-muted);
  background: transparent;
  min-width: 0;
  flex: 1;
  max-width: 160px;
}
.setting-val-input:focus { color: var(--text); }

.token-status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 6px;
  font-size: 14px;
  color: var(--text-muted);
}

.token-textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  color: var(--text);
  background: var(--bg);
  resize: none;
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.5;
}
.token-textarea:focus { border-color: var(--primary); }

.status-chip {
  font-size: 12px;
  font-weight: 600;
  border-radius: 6px;
  padding: 2px 8px;
  background: var(--primary-faint);
  color: var(--primary-dark);
}
.status-chip-off {
  background: var(--border);
  color: var(--text-muted);
}

.settings-version {
  text-align: center;
  font-size: 12px;
  color: var(--text-faint);
  padding: 8px 0 4px;
}

/* ===== BOTTOM NAV ===== */
.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
}

.nav-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: var(--nav-height);
  font-size: 10px;
  font-weight: 500;
  color: var(--text-faint);
  letter-spacing: 0.01em;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.nav-tab .nav-icon { width: 24px; height: 24px; }
.nav-tab.active { color: var(--primary); }
.nav-tab:active { opacity: 0.6; }

/* ===== CLIP PLAYER MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
}

.modal-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--topbar-height);
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-title-wrap {
  flex: 1;
  min-width: 0;
  text-align: center;
}
.modal-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.modal-date {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

.clip-video-wrap {
  background: #000;
  flex-shrink: 0;
  aspect-ratio: 16 / 9;
}
.clip-video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  display: block;
}

.clip-actions {
  display: flex;
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 8px;
}
.clip-act {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 6px 0;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
}
.clip-act svg { width: 22px; height: 22px; }
.clip-act:active { background: var(--bg); }
.clip-act.is-active { color: var(--primary); background: var(--primary-faint); }
.clip-act-danger { color: var(--danger); }

.event-summary {
  margin: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.summary-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}
.summary-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.summary-icon {
  width: 22px;
  height: 22px;
  color: var(--primary);
  flex-shrink: 0;
  margin-top: 1px;
}
.summary-main {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}
.summary-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  background: #1A1F26;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  border-radius: 100px;
  padding: 10px 20px;
  white-space: nowrap;
  box-shadow: var(--shadow-lg);
  max-width: calc(100vw - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== MISC ===== */
@media (prefers-color-scheme: dark) {
  /* Opt-out: keep light mode for the mobile-style app */
}
