:root {
  color-scheme: light dark;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #eef1f6;
  --text: #101828;
  --text-muted: #667085;
  --border: #e4e7ec;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --in-color: #16a34a;
  --out-color: #dc2626;
  --warn-color: #d97706;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #131c2e;
  --surface-alt: #1b2740;
  --text: #e6e9f0;
  --text-muted: #93a0b4;
  --border: #24314d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select {
  font-family: inherit;
  font-size: 15px;
}

/* ---------- Login ---------- */

.view-login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at top, #1e3a8a 0%, #0b1220 65%);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 24px;
}

.brand-title {
  font-size: 22px;
  margin: 0 0 4px;
  text-align: center;
}

.brand-sub {
  color: var(--text-muted);
  text-align: center;
  margin: 0 0 24px;
  font-size: 14px;
}

#login-form label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text-muted);
}

#login-form input {
  width: 100%;
  margin-top: 6px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  min-height: 44px;
}

#login-form .checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
}

#login-form .checkbox-label input {
  width: auto;
  margin-top: 0;
  min-height: auto;
}

.btn-primary {
  width: 100%;
  min-height: 46px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
  cursor: pointer;
  margin-top: 6px;
}

.btn-secondary {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  padding: 0 14px;
}

.full-width { width: 100%; margin-top: 8px; }

.error-text {
  color: var(--out-color);
  font-size: 13px;
  margin-top: 10px;
  text-align: center;
}

.lang-switch { margin-top: 18px; text-align: center; }
.lang-switch select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
}

/* ---------- App shell ---------- */

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--out-color);
  display: inline-block;
}
.dot.online { background: var(--in-color); }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  border: none;
  background: var(--surface-alt);
  color: var(--text);
  border-radius: 10px;
  min-width: 40px;
  min-height: 40px;
  cursor: pointer;
  font-size: 14px;
}

.content {
  flex: 1;
  padding: 16px 16px calc(90px + var(--safe-bottom));
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.view[hidden] { display: none; }

/* KPI cards */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  text-align: center;
}

.kpi-value { font-size: 28px; font-weight: 700; }
.kpi-label { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.kpi-in .kpi-value { color: var(--in-color); }
.kpi-out .kpi-value { color: var(--out-color); }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 0 10px;
}
.section-header h2 { font-size: 16px; margin: 0; }

.select-compact, #filter-direction, #filter-camera {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.live-frame-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  aspect-ratio: 16 / 9;
  box-shadow: var(--shadow);
}
.live-frame-wrap img { width: 100%; height: 100%; object-fit: contain; display: block; }
.live-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.live-badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}
.live-badge.state-connecting { color: var(--warn-color); }
.live-badge.state-online { color: var(--in-color); }
.live-badge.state-offline { color: var(--out-color); }

.live-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.live-spinner::after {
  content: "";
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.25);
  border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
.live-spinner[hidden] { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }

/* Event cards */
.event-list { display: flex; flex-direction: column; gap: 10px; }

.event-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 12px;
  cursor: pointer;
}

.event-thumb {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  background: var(--surface-alt);
  object-fit: cover;
  flex-shrink: 0;
}

.event-info { flex: 1; min-width: 0; }
.event-info .row1 { display: flex; align-items: center; gap: 8px; }
.event-camera { font-weight: 600; font-size: 14px; }
.event-time { color: var(--text-muted); font-size: 12px; margin-top: 2px; }

.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  color: #fff;
}
.badge-in { background: var(--in-color); }
.badge-out { background: var(--out-color); }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
  font-size: 14px;
}

.loading-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
  font-size: 14px;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }

.media-loading {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  padding: 24px 0;
}

/* Filter bar */
.filter-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.segmented {
  display: flex;
  width: 100%;
  background: var(--surface-alt);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}
.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
  font-size: 12px;
  padding: 9px 0;
  border-radius: 8px;
  cursor: pointer;
}
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg-btn.seg-in.active { color: var(--in-color); }
.seg-btn.seg-out.active { color: var(--out-color); }

.custom-range-panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 14px;
}
.custom-range-panel[hidden] { display: none; }
.custom-range-row {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}
.custom-range-row label {
  flex: 1;
  font-size: 12px;
  color: var(--text-muted);
}
.custom-range-row input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  min-height: 42px;
  font-size: 14px;
}

.disclaimer {
  font-size: 12px;
  color: var(--warn-color);
  background: color-mix(in srgb, var(--warn-color) 12%, transparent);
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 14px;
}

/* Face gallery */
.face-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
}
.face-grid img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 10px;
  background: var(--surface-alt);
  cursor: pointer;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 10;
}
.nav-btn {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-muted);
  padding: 10px 4px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  cursor: pointer;
}
.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 20px; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal[hidden] {
  display: none;
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 20px;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
}
.modal-card video, .modal-card img.detail-img {
  width: 100%;
  border-radius: 10px;
  background: #000;
  margin-bottom: 12px;
}
.modal-card .detail-row { font-size: 13px; margin-bottom: 6px; color: var(--text-muted); }

@media (min-width: 768px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  .content { padding-left: 24px; padding-right: 24px; }
}

/* ---------- Enterprise branding footer ---------- */

.app-footer {
  margin-top: 28px;
  padding: 16px 12px calc(14px + var(--safe-bottom));
  text-align: center;
  border-top: 1px solid var(--border);
}

.login-footer {
  margin-top: 24px;
  padding: 16px 4px 0;
}

.footer-badge {
  display: inline-block;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: 999px;
  padding: 3px 10px;
  margin-bottom: 8px;
}

.footer-brand {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.footer-contact {
  margin-top: 4px;
  font-size: 11px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  color: var(--text-muted);
}

.footer-contact a {
  color: var(--text-muted);
  text-decoration: none;
}
.footer-contact a:hover { color: var(--accent); }

.footer-sep { margin: 0 6px; opacity: 0.6; }

