/* ========================================
   FleetOS — CMS Dashboard Styles
   Clean / Light / Simple
   ======================================== */

:root {
  --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-display: 'Outfit', var(--font-sans);
  --bg: #F8F9FA;
  --surface: #FFFFFF;
  --surface2: #F1F3F5;
  --border: #E5E7EB;
  --accent: #3B82F6;
  --accent2: #3B82F6;
  --warn: #F59E0B;
  --danger: #EF4444;
  --text: #1F2937;
  --muted: #6B7280;
  --muted2: #D1D5DB;
  --online: #10B981;
  --offline: #EF4444;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.04);
  --radius: 8px;
}

/* ---- RESET ---- */
* { margin: 0; padding: 0; box-sizing: border-box; }

html {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  min-height: 100dvh;
  display: flex;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

/* ---- SCROLLBAR ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--muted2); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

@supports (scrollbar-width: thin) {
  * { scrollbar-width: thin; scrollbar-color: var(--muted2) transparent; }
}

/* ---- SIDEBAR ---- */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  box-shadow: 2px 0 8px rgba(0,0,0,0.04);
}

.sidebar-backdrop {
  display: none;
}

.mobile-menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
}

.mobile-menu-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
}

.logo {
  padding: 26px 22px 22px;
  border-bottom: 1px solid var(--border);
}

.logo-mark {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 20px;
  color: var(--text);
}
.logo-mark span { color: var(--accent); }

.logo-sub {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.3px;
  margin-top: 2px;
}

/* ---- CLIENT SWITCHER ---- */
.client-switcher {
  padding: 14px 22px 18px;
  border-bottom: 1px solid var(--border);
}

.client-switcher-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
  color: var(--muted);
  margin-bottom: 8px;
  text-transform: uppercase;
}

.client-group-select {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.client-group-select:hover {
  border-color: var(--muted2);
}

.client-group-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.12);
}

.nav {
  padding: 12px 0;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

.nav-section {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.4px;
  color: var(--muted);
  padding: 14px 22px 8px;
  text-transform: uppercase;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  margin: 2px 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  transition: all 0.15s;
  border-radius: 8px;
  border-left: 3px solid transparent;
}
.nav-item:hover {
  color: var(--text);
  background: var(--surface2);
}
.nav-item.active {
  color: var(--accent);
  background: rgba(59, 130, 246, 0.1);
  border-left-color: var(--accent);
}
.nav-item svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 10px;
  font-weight: 600;
}

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

.user-info { display: flex; align-items: center; gap: 10px; }

.user-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3B82F6, #6366F1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-family: var(--font-display);
  font-weight: 700;
  color: #FFFFFF;
  flex-shrink: 0;
}

.user-name { font-size: 13px; font-weight: 500; color: var(--text); }
.user-role { font-size: 11px; color: var(--muted); }

.logout-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  transition: color 0.15s;
}
.logout-btn:hover { color: var(--danger); }

/* ---- MAIN LAYOUT ---- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.topbar {
  height: 56px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  flex-shrink: 0;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.page-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
  flex: 1;
}

.server-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--online);
  animation: pulse 2s infinite;
}

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

.topbar-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  font-weight: 500;
}
.topbar-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(59, 130, 246, 0.06);
}
.topbar-btn:active {
  background: var(--surface2);
}

.content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
}

/* ---- LOGIN SCREEN ---- */
#login-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(145deg, #EFF1F4 0%, var(--bg) 50%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#login-screen.hidden { display: none; }

.login-box {
  width: 420px;
  background: var(--surface);
  border-radius: 16px;
  padding: 44px;
  box-shadow: 0 20px 40px -12px rgba(0, 0, 0, 0.08), 0 0 0 1px rgba(0, 0, 0, 0.02);
}

.login-logo {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 28px;
  margin-bottom: 4px;
}
.login-logo span { color: var(--accent); }

.login-sub {
  font-size: 13px;
  color: var(--muted);
  letter-spacing: 0.3px;
  margin-bottom: 36px;
}

/* ---- FORMS ---- */
.form-group { margin-bottom: 16px; }

.form-label {
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
  color: var(--muted);
  margin-bottom: 6px;
  display: block;
}

.form-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 14px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.12);
}

.login-btn {
  width: 100%;
  background: var(--accent);
  color: #FFFFFF;
  border: none;
  border-radius: 6px;
  padding: 12px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.3px;
  cursor: pointer;
  margin-top: 8px;
  transition: all 0.15s;
  touch-action: manipulation;
  min-height: 44px;
}
.login-btn:hover { background: #2563EB; transform: translateY(-1px); }
.login-btn:active { transform: translateY(0); }
.login-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.login-error {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--danger);
  margin-top: 12px;
  display: none;
}

/* ---- STAT CARDS ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px 22px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  border: 1px solid var(--border);
}
.stat-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
}
.stat-card.green::after { background: var(--online); }
.stat-card.green { background: linear-gradient(180deg, rgba(16, 185, 129, 0.04) 0%, var(--surface) 24px); }
.stat-card.blue::after { background: var(--accent2); }
.stat-card.blue { background: linear-gradient(180deg, rgba(59, 130, 246, 0.05) 0%, var(--surface) 24px); }
.stat-card.orange::after { background: var(--warn); }
.stat-card.orange { background: linear-gradient(180deg, rgba(245, 158, 11, 0.05) 0%, var(--surface) 24px); }
.stat-card.red::after { background: var(--danger); }
.stat-card.red { background: linear-gradient(180deg, rgba(239, 68, 68, 0.04) 0%, var(--surface) 24px); }

.stat-label {
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
  color: var(--muted);
  margin-bottom: 8px;
}

.stat-value {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 32px;
  line-height: 1;
}
.stat-sub { font-size: 11px; color: var(--muted); margin-top: 4px; }
.stat-value.green { color: var(--online); }
.stat-value.blue { color: var(--accent2); }
.stat-value.orange { color: var(--warn); }
.stat-value.red { color: var(--danger); }

/* ---- SECTION HEADER ---- */
.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
}

.section-count {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface2);
  padding: 2px 10px;
  border-radius: 12px;
  font-weight: 500;
}

/* ---- FILTER BAR ---- */
.filter-bar { display: flex; gap: 8px; margin-bottom: 12px; }

.search-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.filter-select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  cursor: pointer;
  transition: border-color 0.15s;
}
.filter-select:focus { border-color: var(--accent); }

/* ---- TABLE ---- */
.table-wrap {
  background: var(--surface);
  border-radius: 12px;
  overflow-x: auto;
  overflow-y: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  border: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

thead tr {
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}

th {
  text-align: left;
  padding: 12px 18px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
  color: var(--muted);
  text-transform: uppercase;
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
  cursor: pointer;
}
tbody tr:last-child { border-bottom: none; }
tbody tr:nth-child(even) { background: rgba(0,0,0,0.01); }
tbody tr:hover { background: rgba(59, 130, 246, 0.04); }

td { padding: 13px 18px; font-size: 13px; }

.device-id { color: var(--muted); font-size: 11px; }
.plate { font-family: var(--font-display); font-weight: 600; font-size: 14px; }

/* ---- BADGES ---- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 11px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.2px;
}
.badge-online {
  background: rgba(16, 185, 129, 0.12);
  color: var(--online);
  border: 1px solid rgba(16, 185, 129, 0.25);
}
.badge-offline {
  background: rgba(239, 68, 68, 0.1);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
}
.badge-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
}

.net-type { color: var(--muted); font-size: 12px; }

/* ---- ACTION BUTTONS ---- */
.action-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 11px;
  padding: 5px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
  margin-right: 6px;
  font-weight: 500;
  min-height: 32px;
  touch-action: manipulation;
}
.action-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(59, 130, 246, 0.06);
}
.action-btn:active { opacity: 0.9; }

/* ---- MODAL ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 500;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 16px;
  width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.04);
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.modal-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
}

.modal-close {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}
.modal-close:hover { color: var(--text); }

.modal-body { padding: 24px; }

.modal-footer {
  padding: 18px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.info-key {
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
  color: var(--muted);
  margin-bottom: 3px;
}
.info-val { font-size: 13px; color: var(--text); }

/* ---- BUTTONS ---- */
.btn {
  padding: 9px 18px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid;
  touch-action: manipulation;
  min-height: 38px;
}
.btn-primary {
  background: var(--accent);
  color: #FFFFFF;
  border-color: var(--accent);
  font-family: var(--font-display);
  font-weight: 700;
}
.btn-primary:hover { background: #2563EB; border-color: #2563EB; }
.btn-primary:active { opacity: 0.95; }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}
.btn-ghost:hover {
  color: var(--text);
  border-color: var(--muted2);
  background: var(--surface2);
}
.btn-ghost:active { background: var(--surface2); }
.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.35);
}
.btn-danger:hover { background: rgba(239, 68, 68, 0.08); }
.btn-danger:active { background: rgba(239, 68, 68, 0.12); }

/* ---- LOADING / EMPTY ---- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: var(--muted);
  font-size: 13px;
  gap: 10px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 64px 24px;
  color: var(--muted);
  font-size: 13px;
  gap: 12px;
  text-align: center;
}
.empty-state svg { width: 44px; height: 44px; opacity: 0.35; flex-shrink: 0; }

/* ---- MAP PLACEHOLDER ---- */
.map-placeholder {
  background: var(--surface);
  border-radius: var(--radius);
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
  flex-direction: column;
  gap: 8px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.map-grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 40px 40px;
  opacity: 0.5;
}
.map-content {
  position: relative;
  z-index: 1;
  text-align: center;
}
.map-container {
  position: relative;
  z-index: 0;
}
.map-container .leaflet-pane { z-index: 1; }

/* ---- LIVE GRID ---- */
.live-grid-container {
  display: grid;
  gap: 8px;
  background: var(--surface2);
  padding: 12px;
  border-radius: var(--radius);
}
.live-grid-container.grid-4 { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr); }
.live-grid-container.grid-6 { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(3, 1fr); }
.live-grid-container.grid-8 { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(4, 1fr); }
.live-grid-cell {
  position: relative;
  background: #111;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 16/10;
}
.live-grid-cell video { width: 100%; height: 100%; object-fit: contain; }
.live-grid-cell .channel-label {
  position: absolute;
  top: 4px;
  left: 4px;
  font-size: 11px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
}

/* ---- REALTIME BADGE ---- */
.realtime-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 500;
  color: var(--accent);
  background: rgba(59, 130, 246, 0.06);
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid rgba(59, 130, 246, 0.12);
}
.realtime-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.5s infinite;
}

/* ---- ALARM TYPE ---- */
.alarm-type {
  font-size: 11px;
  font-weight: 500;
  padding: 3px 10px;
  border-radius: 12px;
  background: rgba(245, 158, 11, 0.08);
  color: var(--warn);
  border: 1px solid rgba(245, 158, 11, 0.15);
}

/* ---- PAGES ---- */
.page-view { display: none; }
.page-view.active { display: block; }

/* ---- SNAPSHOTS ---- */
.snapshot-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.snapshot-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  aspect-ratio: 16/9;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: border-color 0.15s;
  font-size: 12px;
  color: var(--muted);
  position: relative;
  overflow: hidden;
}
.snapshot-item:hover { border-color: var(--accent); }
.snapshot-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  inset: 0;
}
.snapshot-label {
  position: relative;
  z-index: 1;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
}

/* ---- QUERY PANELS ---- */
.query-panel {
  background: var(--surface);
  border-radius: 12px;
  padding: 22px;
  margin-bottom: 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  border: 1px solid var(--border);
}

.query-grid-4 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 12px;
  align-items: end;
}

.query-grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 12px;
  align-items: end;
}

/* ---- MANAGEMENT TABS ---- */
.mgmt-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

.mgmt-tab {
  padding: 10px 20px;
  font-size: 13px;
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
  background: none;
  border-top: none;
  border-left: none;
  border-right: none;
}
.mgmt-tab:hover { color: var(--text); }
.mgmt-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.mgmt-panel { display: none; }
.mgmt-panel.active { display: block; }

.mgmt-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  max-width: 600px;
}
.mgmt-form .form-group.full { grid-column: 1 / -1; }

/* ---- RESULT MESSAGE ---- */
.result-msg {
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
  margin-top: 16px;
  display: none;
}
.result-msg.visible { display: block; }
.result-msg.success {
  background: rgba(16, 185, 129, 0.08);
  border: 1px solid rgba(16, 185, 129, 0.2);
  color: var(--online);
}
.result-msg.error {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.2);
  color: var(--danger);
}

/* ---- ALARM TOAST ---- */
.alarm-toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
  pointer-events: none;
}
.alarm-toast {
  pointer-events: auto;
  background: var(--surface);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-left: 4px solid var(--danger);
  border-radius: var(--radius);
  padding: 12px 16px;
  box-shadow: var(--shadow-lg);
  font-size: 13px;
  animation: alarmToastIn 0.25s ease;
}
@keyframes alarmToastIn {
  from { opacity: 0; transform: translateX(100%); }
  to { opacity: 1; transform: translateX(0); }
}
.alarm-toast-title { font-weight: 600; color: var(--danger); margin-bottom: 4px; }
.alarm-toast-body { color: var(--muted); }
.alarm-toast a { color: var(--accent); cursor: pointer; }

/* ---- BUTTON GROUP ---- */
.btn-group {
  display: flex;
  gap: 8px;
  align-items: center;
}

.toolbar-row {
  width: 100%;
}

/* ---- TOUCH & MOBILE HELPERS ---- */
a, button, .nav-item, .action-btn, .btn, .topbar-btn, .mgmt-tab, .login-btn,
input[type="checkbox"], select {
  -webkit-tap-highlight-color: transparent;
}

.form-input,
.filter-select,
.client-group-select {
  -webkit-appearance: none;
  appearance: none;
}

select.filter-select,
select.client-group-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236B7280' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

@media (hover: none) and (pointer: coarse) {
  .btn:active {
    opacity: 0.85;
    transform: scale(0.98);
  }

  .action-btn:active {
    opacity: 0.85;
  }

  .nav-item:active {
    background: var(--surface2);
  }

  tbody tr:active {
    background: rgba(59, 130, 246, 0.08);
  }
}

@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .query-grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 1024px) {
  body {
    overflow-x: hidden;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(300px, 82vw);
    min-width: 0;
    height: 100dvh;
    transform: translateX(-105%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 120;
    will-change: transform;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body.sidebar-open {
    overflow: hidden;
    touch-action: none;
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 110;
    background: rgba(0, 0, 0, 0.3);
    opacity: 0;
    pointer-events: none;
    border: 0;
    transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    -webkit-tap-highlight-color: transparent;
  }

  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-menu-btn {
    display: inline-flex;
    flex-shrink: 0;
    min-width: 44px;
    min-height: 44px;
  }

  .main {
    width: 100%;
    min-width: 0;
  }

  .topbar {
    padding: 0 14px;
    gap: 10px;
  }

  .content {
    padding: 16px;
  }

  .dash-row {
    grid-template-columns: 1fr !important;
    gap: 16px !important;
  }

  .query-grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .snapshot-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mgmt-form,
  .info-grid {
    grid-template-columns: 1fr;
    max-width: none;
  }

  .section-header,
  .filter-bar,
  .btn-group {
    flex-wrap: wrap;
  }

  .query-panel {
    padding: 18px;
  }

  .alarm-toast-container {
    left: 12px;
    right: 12px;
    bottom: 12px;
    max-width: none;
  }

  .modal {
    width: min(680px, calc(100vw - 24px));
  }

  .nav-item {
    padding: 12px 18px;
    min-height: 44px;
  }

  .centeredVideo {
    max-width: 100% !important;
    max-height: 280px !important;
  }
}

@media (max-width: 768px) {
  .topbar {
    height: auto;
    min-height: 56px;
    padding: 10px 12px;
    flex-wrap: wrap;
  }

  .page-title {
    font-size: 15px;
    min-width: 0;
  }

  .server-status {
    display: none;
  }

  .content {
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }

  .stats-grid,
  .query-grid-4,
  .query-grid-3,
  .snapshot-grid {
    grid-template-columns: 1fr;
  }

  .stat-card {
    padding: 16px 18px;
  }

  .stat-value {
    font-size: 28px;
  }

  .filter-bar {
    flex-direction: column;
  }

  .filter-bar > * {
    min-width: 0;
    width: 100%;
  }

  .toolbar-row {
    flex-wrap: wrap !important;
  }

  .live-grid-container.grid-4,
  .live-grid-container.grid-6,
  .live-grid-container.grid-8 {
    grid-template-columns: 1fr;
    grid-template-rows: none;
  }

  .modal-overlay.open {
    align-items: flex-end;
  }

  .modal {
    width: 100vw;
    max-width: 100vw;
    max-height: 92dvh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 14px 16px;
  }

  .modal-footer {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }

  .login-box {
    width: calc(100vw - 24px);
    max-width: 420px;
    padding: 30px 22px;
    border-radius: 12px;
  }

  #trackingMap,
  #historyMap {
    height: 260px !important;
  }

  .centeredVideo {
    max-height: 240px !important;
  }

  .form-input,
  .filter-select,
  .client-group-select {
    font-size: 16px;
    padding: 12px 14px;
  }

  .section-header {
    gap: 8px;
  }

  .section-title {
    font-size: 14px;
  }

  table {
    min-width: 600px;
  }

  td, th {
    padding: 10px 12px;
  }

  .action-btn {
    padding: 8px 12px;
    font-size: 12px;
    min-height: 36px;
  }

  .topbar-btn {
    padding: 8px 12px;
    font-size: 12px;
  }
}

@media (max-width: 560px) {
  .content {
    padding: 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .query-panel {
    padding: 12px;
    border-radius: 8px;
  }

  .filter-bar > * {
    width: 100%;
  }

  .modal-footer {
    flex-direction: column;
  }

  .modal-footer .btn {
    width: 100%;
    text-align: center;
  }

  .stats-grid {
    gap: 10px;
  }

  .stat-card {
    padding: 14px 16px;
  }

  .stat-value {
    font-size: 24px;
  }

  .query-grid-4,
  .query-grid-3 {
    gap: 10px;
  }

  .topbar {
    padding: 8px 10px;
    gap: 8px;
  }

  .page-title {
    font-size: 14px;
  }

  .section-header .btn {
    width: 100%;
  }

  .mgmt-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 0;
    flex-wrap: nowrap;
  }

  .mgmt-tab {
    white-space: nowrap;
    flex-shrink: 0;
    padding: 10px 14px;
  }

  .login-box {
    width: calc(100vw - 16px);
    padding: 24px 18px;
  }

  #login-screen {
    padding: 16px;
  }

  .alarm-toast-container {
    left: 8px;
    right: 8px;
    bottom: 8px;
  }

  .alarm-toast {
    padding: 10px 12px;
    font-size: 12px;
  }
}

@media (max-width: 380px) {
  .content {
    padding: 6px;
  }

  .topbar {
    padding: 6px 8px;
  }

  .stat-value {
    font-size: 22px;
  }

  .btn {
    padding: 8px 12px;
    font-size: 12px;
  }

  .form-input,
  .filter-select {
    padding: 10px 12px;
  }
}
