:root {
  --bg: #0a0e1a;
  --bg-elev: #0f1424;
  --panel: rgba(19, 24, 38, 0.6);
  --panel-strong: rgba(19, 24, 38, 0.85);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.15);
  --accent: #00d9ff;
  --accent-glow: rgba(0, 217, 255, 0.45);
  --alert: #ff3b6b;
  --alert-glow: rgba(255, 59, 107, 0.55);
  --ok: #38e89b;
  --warn: #ffb43a;
  --text: #e6edf7;
  --muted: #8a93a6;
  --shadow: 0 18px 50px -12px rgba(0, 0, 0, 0.65);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  background-image:
    radial-gradient(circle at 12% 8%, rgba(0, 217, 255, 0.12) 0%, transparent 38%),
    radial-gradient(circle at 88% 92%, rgba(168, 85, 247, 0.10) 0%, transparent 42%),
    radial-gradient(circle at 50% 50%, rgba(255, 59, 107, 0.05) 0%, transparent 60%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { display: flex; flex-direction: column; min-height: 100vh; overflow-x: hidden; }

/* Glass panels */
.glass {
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(20px) saturate(140%);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.glass-strong { background: var(--panel-strong); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.18s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: rgba(255, 255, 255, 0.09); border-color: rgba(255, 255, 255, 0.22); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-primary {
  background: linear-gradient(135deg, rgba(0, 217, 255, 0.85), rgba(56, 232, 155, 0.85));
  border-color: transparent;
  color: #06121a;
  font-weight: 600;
  box-shadow: 0 6px 20px -6px var(--accent-glow);
}
.btn-primary:hover { box-shadow: 0 10px 28px -6px var(--accent-glow); transform: translateY(-1px); }

.btn-danger {
  border-color: rgba(255, 59, 107, 0.4);
  color: #ffb6c5;
}
.btn-danger:hover { background: rgba(255, 59, 107, 0.15); border-color: var(--alert); }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: rgba(255, 255, 255, 0.06); }

.btn-sm { padding: 6px 12px; font-size: 12px; border-radius: 8px; }
.btn-icon { padding: 8px; width: 36px; height: 36px; border-radius: 10px; }

/* Inputs */
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; color: var(--muted); font-weight: 500; letter-spacing: 0.02em; }
.input, .select {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.18s, background 0.18s, box-shadow 0.18s;
}
.input:focus, .select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 217, 255, 0.15);
  background: rgba(0, 0, 0, 0.35);
}
.input::placeholder { color: var(--muted); }

/* Range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: grab;
  box-shadow: 0 0 0 4px rgba(0, 217, 255, 0.18), 0 0 12px var(--accent-glow);
  transition: transform 0.12s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.15); }

/* Switch */
.switch { position: relative; display: inline-block; width: 42px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-track {
  position: absolute; inset: 0;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  transition: background 0.22s;
}
.switch-track::before {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px;
  background: white; border-radius: 50%;
  transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
}
.switch input:checked + .switch-track { background: var(--accent); box-shadow: 0 0 12px var(--accent-glow); }
.switch input:checked + .switch-track::before { transform: translateX(18px); }

/* Header */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  background: rgba(10, 14, 26, 0.72);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #a855f7);
  display: grid; place-items: center;
  box-shadow: 0 0 24px -4px var(--accent-glow);
  font-weight: 700; color: #06121a; font-size: 16px; letter-spacing: -0.02em;
}
.brand-name { font-weight: 600; font-size: 16px; letter-spacing: -0.01em; }
.brand-sub { font-size: 11px; color: var(--muted); margin-top: 2px; letter-spacing: 0.06em; text-transform: uppercase; }

.header-right { display: flex; align-items: center; gap: 12px; }
.clock { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }

/* Layout */
.app-layout { display: flex; flex: 1; min-height: 0; }
.main { flex: 1; padding: 20px; min-width: 0; }

/* Camera grid */
.camera-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 18px;
}

.tile {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 16px;
  overflow: hidden;
  background: #04060c;
  border: 1px solid var(--border);
  transition: border-color 0.25s, box-shadow 0.25s, transform 0.25s;
}
.tile canvas { width: 100%; height: 100%; display: block; }
.tile-overlay-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  background: linear-gradient(180deg, rgba(4,6,12,0.78), rgba(4,6,12,0));
  pointer-events: none;
}
.tile-overlay-top > * { pointer-events: auto; }

.tile-actions { display: flex; gap: 4px; flex-shrink: 0; margin-left: auto; }
.tile-action-btn {
  width: 30px; height: 30px;
  background: rgba(10,14,26,0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: grid; place-items: center;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.08s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}
.tile-action-btn:hover { background: rgba(0,217,255,0.18); border-color: var(--accent); color: var(--accent); }
.tile-action-btn:active { transform: scale(0.92); }
.tile-action-btn.active { background: rgba(0,217,255,0.28); border-color: var(--accent); color: var(--accent); }
.tile-action-btn[data-action="talk"].active {
  background: rgba(255, 59, 107, 0.32);
  border-color: var(--alert);
  color: var(--alert);
  animation: talk-pulse 1.2s ease-in-out infinite;
}
@keyframes talk-pulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--alert-glow); }
  50%      { box-shadow: 0 0 0 6px transparent; }
}
@media (hover: none) and (pointer: coarse) {
  .tile-action-btn { width: 36px; height: 36px; }
}

.tile-name { font-weight: 600; font-size: 14px; flex: 1; }
.status-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 8px currentColor;
  position: relative;
}
.status-dot.live { background: var(--ok); color: var(--ok); }
.status-dot.live::after {
  content: ''; position: absolute; inset: -3px; border-radius: 50%;
  border: 2px solid currentColor; opacity: 0.5;
  animation: pulse-ring 1.6s ease-out infinite;
}
.status-dot.connecting { background: var(--warn); color: var(--warn); }
.status-dot.error { background: var(--alert); color: var(--alert); }

@keyframes pulse-ring {
  0% { transform: scale(0.9); opacity: 0.6; }
  100% { transform: scale(2); opacity: 0; }
}

.tile-status-text { font-size: 12px; color: var(--muted); }

.tile.motion {
  border-color: var(--alert);
  box-shadow: 0 0 0 2px var(--alert-glow), 0 0 40px -4px var(--alert-glow);
  animation: motion-pulse 1.4s ease-out;
}
@keyframes motion-pulse {
  0%   { box-shadow: 0 0 0 2px var(--alert-glow), 0 0 0 6px rgba(255,59,107,0.35); }
  100% { box-shadow: 0 0 0 2px var(--alert-glow), 0 0 40px -4px var(--alert-glow); }
}

.tile.empty-state {
  border-style: dashed;
  background: var(--panel);
  display: grid; place-items: center;
  color: var(--muted);
  text-align: center;
  padding: 24px;
  aspect-ratio: 16 / 9;
}

.tile-error-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(4, 6, 12, 0.7);
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 16px;
}

/* Sidebar (events) */
.sidebar {
  width: 320px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: rgba(10, 14, 26, 0.55);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  display: flex; flex-direction: column;
  transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s;
}
.sidebar.collapsed { width: 0; opacity: 0; pointer-events: none; }
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.sidebar-title { font-weight: 600; font-size: 13px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.sidebar-list { flex: 1; overflow-y: auto; padding: 8px 12px; }
.sidebar-list::-webkit-scrollbar { width: 6px; }
.sidebar-list::-webkit-scrollbar-track { background: transparent; }
.sidebar-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 999px; }

.event-item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 4px;
  animation: slideIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.event-item:hover { background: rgba(255, 255, 255, 0.05); }
.event-bullet {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--alert);
  box-shadow: 0 0 10px var(--alert-glow);
  margin-top: 6px; flex-shrink: 0;
}
.event-meta { flex: 1; min-width: 0; }
.event-cam { font-weight: 500; font-size: 13px; }
.event-time { font-size: 11px; color: var(--muted); margin-top: 2px; font-variant-numeric: tabular-nums; }
.event-score { font-size: 11px; color: var(--muted); }
.event-empty { padding: 32px 16px; text-align: center; color: var(--muted); font-size: 13px; }

@keyframes slideIn {
  from { transform: translateX(20px); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

/* Settings page */
.settings-container { max-width: 960px; margin: 0 auto; padding: 28px 24px; }
.page-title { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; margin: 0 0 4px; }
.page-sub { color: var(--muted); margin: 0 0 24px; font-size: 13px; }

.cam-list { display: flex; flex-direction: column; gap: 12px; }
.cam-row {
  display: flex; align-items: center; gap: 16px;
  padding: 16px 18px;
}
.cam-thumb {
  width: 80px; height: 48px; border-radius: 8px; flex-shrink: 0;
  background: linear-gradient(135deg, rgba(0,217,255,0.2), rgba(168,85,247,0.2));
  display: grid; place-items: center;
  font-weight: 600; color: var(--accent); font-size: 18px;
}
.cam-info { flex: 1; min-width: 0; }
.cam-name { font-weight: 600; }
.cam-source { font-size: 12px; color: var(--muted); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 420px; }
.cam-tag {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 10px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  margin-right: 6px;
}
.cam-tag.rtsp { background: rgba(0,217,255,0.15); color: var(--accent); }
.cam-tag.usb { background: rgba(168,85,247,0.18); color: #c4a4ff; }
.cam-tag.motion { background: rgba(255,59,107,0.18); color: #ff8aaa; }
.cam-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(4, 6, 12, 0.7);
  backdrop-filter: blur(6px);
  display: grid; place-items: center;
  z-index: 60;
  animation: fade 0.2s ease;
}
.modal {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 24px;
  animation: pop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.modal h3 { margin: 0 0 4px; font-size: 18px; }
.modal-sub { color: var(--muted); font-size: 12px; margin: 0 0 18px; }
.modal-grid { display: grid; gap: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }

@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
@keyframes pop {
  from { transform: translateY(8px) scale(0.98); opacity: 0; }
  to   { transform: translateY(0) scale(1); opacity: 1; }
}

/* Login */
.login-shell {
  flex: 1;
  display: grid; place-items: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}
.login-shell::before, .login-shell::after {
  content: '';
  position: absolute;
  width: 480px; height: 480px;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.45;
  z-index: -1;
}
.login-shell::before { background: var(--accent); top: -120px; left: -120px; }
.login-shell::after { background: #a855f7; bottom: -160px; right: -120px; }

.login-card { width: min(420px, 100%); padding: 32px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.02em; }
.login-card .sub { color: var(--muted); font-size: 13px; margin: 0 0 24px; }
.login-error { color: var(--alert); font-size: 12px; min-height: 18px; }

/* Helpers */
.row { display: flex; align-items: center; gap: 12px; }
.row.between { justify-content: space-between; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  font-size: 11px; color: var(--muted);
  border: 1px solid var(--border);
}

/* PTZ overlay */
.ptz-pad {
  position: absolute; bottom: 12px; left: 12px;
  display: grid;
  grid-template-columns: repeat(3, 34px);
  grid-template-rows: repeat(3, 34px);
  gap: 3px;
  opacity: 0; transition: opacity 0.2s;
  z-index: 4;
}
.ptz-zoom-cluster {
  position: absolute; bottom: 12px; right: 12px;
  display: flex; flex-direction: column; gap: 6px;
  opacity: 0; transition: opacity 0.2s;
  z-index: 4;
}
.tile:hover .ptz-pad,
.tile:hover .ptz-zoom-cluster,
.ptz-controls[data-ptz-visible="true"] .ptz-pad,
.ptz-controls[data-ptz-visible="true"] .ptz-zoom-cluster { opacity: 1; pointer-events: auto; }
.ptz-pad, .ptz-zoom-cluster { pointer-events: none; }

.ptz-btn {
  background: rgba(10,14,26,0.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  font-size: 13px; line-height: 1;
  cursor: pointer;
  display: grid; place-items: center;
  user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}
.ptz-btn:hover { background: rgba(0,217,255,0.18); border-color: var(--accent); color: var(--accent); }
.ptz-btn:active { transform: scale(0.92); background: rgba(0,217,255,0.32); }

.ptz-btn[data-ptz="up"]    { grid-column: 2; grid-row: 1; }
.ptz-btn[data-ptz="left"]  { grid-column: 1; grid-row: 2; }
.ptz-btn[data-ptz="stop"]  { grid-column: 2; grid-row: 2; opacity: 0.55; font-size: 9px; }
.ptz-btn[data-ptz="right"] { grid-column: 3; grid-row: 2; }
.ptz-btn[data-ptz="down"]  { grid-column: 2; grid-row: 3; }

.ptz-zoom { width: 34px; height: 34px; font-size: 16px; font-weight: 600; }

@media (hover: none) and (pointer: coarse) {
  /* Touch: ignore tile:hover, only show PTZ when explicitly toggled */
  .tile:hover .ptz-pad,
  .tile:hover .ptz-zoom-cluster { opacity: 0; pointer-events: none; }
  .ptz-controls[data-ptz-visible="true"] .ptz-pad,
  .ptz-controls[data-ptz-visible="true"] .ptz-zoom-cluster { opacity: 1; pointer-events: auto; }
  .ptz-btn { width: 40px; height: 40px; font-size: 15px; }
  .ptz-pad { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); }
}

/* Sidebar backdrop (mobile) */
.sidebar-backdrop {
  position: fixed; inset: 0;
  background: rgba(2, 4, 10, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 35;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease;
}
.sidebar-backdrop.visible { opacity: 1; pointer-events: auto; }

/* Touch-device adaptations: bigger tap targets */
@media (hover: none) and (pointer: coarse) {
  .btn-icon { width: 40px; height: 40px; }
  .btn:hover, .btn-primary:hover { transform: none; }
}

/* Tablet portrait & smaller (existing breakpoint, expanded) */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; right: 0; top: 0;
    height: 100vh; height: 100dvh;
    width: min(340px, calc(100vw - 48px));
    z-index: 40;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sidebar.collapsed { transform: translateX(100%); width: min(340px, calc(100vw - 48px)); opacity: 1; }
  .camera-grid { grid-template-columns: 1fr; gap: 14px; }
  .main {
    padding: 14px;
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
  .app-header { padding: 12px 16px; }
  .clock { display: none; }
}

/* Phone-sized */
@media (max-width: 640px) {
  .app-header { padding: 10px 14px; gap: 8px; }
  .header-right { gap: 4px; }
  .brand { gap: 8px; }
  .brand-logo { width: 32px; height: 32px; font-size: 14px; }
  .brand-name { font-size: 14px; }
  .brand-sub { display: none; }
  #streamCount { display: none; }

  .camera-grid { gap: 10px; }
  .tile-overlay-top { padding: 10px 12px; }
  .tile-name { font-size: 13px; }
  .tile-status-text { display: none; }

  /* Settings page - row becomes 2-line layout */
  .settings-container { padding: 18px 14px;
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }
  .page-title { font-size: 18px; }
  .settings-container .row.between {
    flex-direction: column; align-items: stretch; gap: 12px;
  }
  .settings-container .row.between .btn-primary { width: 100%; }

  .cam-row {
    flex-wrap: wrap; gap: 12px; padding: 14px;
  }
  .cam-info { flex-basis: calc(100% - 80px - 12px); min-width: 0; }
  .cam-source { max-width: 100%; white-space: normal; word-break: break-all; }
  .cam-actions { width: 100%; gap: 8px; }
  .cam-actions .btn { flex: 1; }

  /* Modal becomes bottom sheet */
  .modal-backdrop { align-items: flex-end; }
  .modal {
    width: 100vw;
    max-height: 90vh;
    max-height: 90dvh;
    border-radius: 18px 18px 0 0;
    padding: 20px 18px;
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    animation: slideUp 0.25s cubic-bezier(0.32, 0.72, 0, 1);
  }

  .login-shell { padding: 16px; }
  .login-card { padding: 24px 20px; }
}

@keyframes slideUp {
  from { transform: translateY(100%); opacity: 0.8; }
  to   { transform: translateY(0); opacity: 1; }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
