/* ============================================================
   Event Flow Analyzer - Dashboard Stylesheet
   Dark theme for real-time crowd monitoring at doujin exhibitions.
   Designed for tablet (iPad 768px+) and desktop (1200px+).
   ============================================================ */

/* ----- CSS Custom Properties ----- */
:root {
  /* Background & surface */
  --bg-primary: #1a1a2e;
  --bg-surface: #16213e;
  --bg-surface-alt: #1b2a4a;
  --bg-surface-hover: #1e3050;
  --bg-elevated: #0f3460;

  /* Borders */
  --border-default: #0f3460;
  --border-subtle: #1e3a5f;
  --border-focus: #4a6fa5;

  /* Text */
  --text-primary: #e8e8f0;
  --text-secondary: #a0a0c0;
  --text-muted: #6a6a8a;
  --text-heading: #c8c8e0;

  /* Accent */
  --accent-primary: #4a6fa5;
  --accent-bright: #5c8fd6;

  /* Zone status */
  --zone-normal: #00c853;
  --zone-warning: #ffd600;
  --zone-danger: #ff1744;

  /* Alert levels */
  --alert-info: #2979ff;
  --alert-warning: #ffd600;
  --alert-danger: #ff1744;

  /* Camera status */
  --camera-online: #00c853;
  --camera-offline: #6a6a8a;
  --camera-error: #ff1744;

  /* Connection */
  --conn-connected: #00c853;
  --conn-connecting: #ffd600;
  --conn-disconnected: #ff1744;

  /* Sizes */
  --header-height: 56px;
  --border-radius: 8px;
  --border-radius-sm: 4px;
  --border-radius-lg: 12px;
  --border-radius-pill: 9999px;

  /* Spacing */
  --gap: 12px;
  --padding-panel: 16px;

  /* Shadows */
  --shadow-card: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-elevated: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-glow-danger: 0 0 12px rgba(255, 23, 68, 0.3);

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --transition-slow: 400ms ease;

  /* Font */
  --font-stack: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP",
    "Yu Gothic UI", "Meiryo", -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Source Code Pro", "SF Mono",
    Consolas, "Courier New", monospace;
}

/* ----- Reset & Base ----- */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-stack);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  overflow: hidden;
}

a {
  color: var(--accent-bright);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

ul,
ol {
  list-style: none;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

img,
canvas {
  display: block;
  max-width: 100%;
}

/* ----- Typography ----- */
h1 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.02em;
}

h2 {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
}

h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-heading);
  margin-bottom: 8px;
}

.text-mono {
  font-family: var(--font-mono);
}

.text-muted {
  color: var(--text-muted);
}

.text-small {
  font-size: 0.8rem;
}

/* ----- Scrollbar ----- */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-focus);
}

/* Firefox scrollbar */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-default) transparent;
}

/* ----- Header ----- */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: var(--header-height);
  padding: 0 20px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-default);
  position: relative;
  z-index: 100;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-logo {
  width: 28px;
  height: 28px;
  border-radius: var(--border-radius-sm);
}

.header-title {
  font-size: 1.1rem;
  font-weight: 600;
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-clock {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* ----- Navigation ----- */
.header-nav {
  display: flex;
  gap: 8px;
  align-items: center;
}

.nav-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  color: var(--text-muted);
  transition: color 0.2s, background 0.2s;
}

.nav-link:hover {
  color: var(--text-primary);
  background: var(--bg-surface-alt);
  text-decoration: none;
}

.nav-link svg {
  width: 18px;
  height: 18px;
}

/* ----- Connection Status ----- */
.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: var(--border-radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background var(--transition-normal), color var(--transition-normal);
}

.status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status.connected {
  background: rgba(0, 200, 83, 0.15);
  color: var(--conn-connected);
}

.status.connected::before {
  background: var(--conn-connected);
  box-shadow: 0 0 6px var(--conn-connected);
}

.status.connecting {
  background: rgba(255, 214, 0, 0.15);
  color: var(--conn-connecting);
}

.status.connecting::before {
  background: var(--conn-connecting);
  animation: pulse-dot 1.5s ease-in-out infinite;
}

.status.disconnected {
  background: rgba(255, 23, 68, 0.15);
  color: var(--conn-disconnected);
}

.status.disconnected::before {
  background: var(--conn-disconnected);
}

.status.error {
  background: rgba(213, 0, 0, 0.15);
  color: #ff5252;
}

.status.error::before {
  background: #ff5252;
}

/* ----- Main Layout ----- */
main {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: 1fr auto;
  gap: var(--gap);
  padding: var(--gap);
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

/* ----- Panel Base ----- */
section,
.panel {
  background: var(--bg-surface);
  border-radius: var(--border-radius);
  border: 1px solid var(--border-subtle);
  padding: var(--padding-panel);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.panel-header h2 {
  margin-bottom: 0;
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.panel-footer {
  flex-shrink: 0;
  padding-top: 8px;
  border-top: 1px solid var(--border-subtle);
  margin-top: 8px;
}

/* ----- Map View ----- */
#map-view {
  grid-row: 1 / 3;
  position: relative;
  padding: 0;
  overflow: hidden;
}

#map-view h2 {
  position: absolute;
  top: 12px;
  left: 16px;
  z-index: 10;
  margin-bottom: 0;
  background: rgba(22, 33, 62, 0.85);
  padding: 4px 10px;
  border-radius: var(--border-radius-sm);
  backdrop-filter: blur(4px);
}

.map-canvas-container {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.map-canvas-container canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.map-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.map-overlay svg {
  width: 100%;
  height: 100%;
}

.map-controls {
  position: absolute;
  bottom: 12px;
  right: 12px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.map-controls button {
  width: 36px;
  height: 36px;
  background: rgba(22, 33, 62, 0.9);
  border: 1px solid var(--border-default);
  border-radius: var(--border-radius-sm);
  color: var(--text-secondary);
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  transition: background var(--transition-fast), color var(--transition-fast);
}

.map-controls button:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
}

.map-legend {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 10;
  display: flex;
  gap: 12px;
  background: rgba(22, 33, 62, 0.9);
  padding: 6px 12px;
  border-radius: var(--border-radius-sm);
  backdrop-filter: blur(4px);
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.map-legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.map-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.map-legend-dot.normal {
  background: var(--zone-normal);
}

.map-legend-dot.warning {
  background: var(--zone-warning);
}

.map-legend-dot.danger {
  background: var(--zone-danger);
}

/* ----- Zone Polygons (SVG / Canvas overlay) ----- */
.zone-polygon {
  fill-opacity: 0.2;
  stroke-width: 2;
  stroke-opacity: 0.8;
  transition: fill-opacity var(--transition-normal),
    stroke-opacity var(--transition-normal);
  cursor: pointer;
}

.zone-polygon:hover {
  fill-opacity: 0.35;
  stroke-opacity: 1;
}

.zone-polygon.normal {
  fill: var(--zone-normal);
  stroke: var(--zone-normal);
}

.zone-polygon.warning {
  fill: var(--zone-warning);
  stroke: var(--zone-warning);
}

.zone-polygon.danger {
  fill: var(--zone-danger);
  stroke: var(--zone-danger);
  animation: zone-danger-pulse 2s ease-in-out infinite;
}

.zone-polygon.inactive {
  fill: var(--text-muted);
  stroke: var(--text-muted);
  fill-opacity: 0.08;
  stroke-opacity: 0.3;
}

.zone-label {
  font-size: 12px;
  font-weight: 600;
  fill: var(--text-primary);
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

.zone-count-label {
  font-size: 18px;
  font-weight: 700;
  fill: var(--text-primary);
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

/* Zone tooltip on hover */
.zone-tooltip {
  position: absolute;
  z-index: 20;
  background: rgba(15, 52, 96, 0.95);
  border: 1px solid var(--border-focus);
  border-radius: var(--border-radius-sm);
  padding: 8px 12px;
  font-size: 0.8rem;
  pointer-events: none;
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow-elevated);
  white-space: nowrap;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity var(--transition-fast), transform var(--transition-fast);
}

.zone-tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}

.zone-tooltip .zone-tooltip-name {
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.zone-tooltip .zone-tooltip-count {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 700;
}

/* ----- Right Sidebar ----- */
.sidebar-right {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  overflow: hidden;
}

/* ----- Statistics Panel ----- */
#stats-panel {
  flex-shrink: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.stat-card {
  background: var(--bg-surface-alt);
  border: 1px solid var(--border-subtle);
  border-radius: var(--border-radius);
  padding: 12px;
  text-align: center;
  transition: border-color var(--transition-fast),
    background var(--transition-fast);
}

.stat-card:hover {
  border-color: var(--border-focus);
  background: var(--bg-surface-hover);
}

.stat-card.highlight {
  border-color: var(--accent-primary);
  background: rgba(74, 111, 165, 0.1);
}

.stat-card-label {
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.stat-card-value {
  font-family: var(--font-mono);
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
}

.stat-card-value.large {
  font-size: 2rem;
}

.stat-card-unit {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--text-muted);
  margin-left: 2px;
}

.stat-card-delta {
  font-size: 0.7rem;
  font-weight: 600;
  margin-top: 2px;
}

.stat-card-delta.up {
  color: var(--zone-danger);
}

.stat-card-delta.down {
  color: var(--zone-normal);
}

.stat-card-delta.neutral {
  color: var(--text-muted);
}

/* Colored stat card variants */
.stat-card.normal .stat-card-value {
  color: var(--zone-normal);
}

.stat-card.warning .stat-card-value {
  color: var(--zone-warning);
}

.stat-card.danger .stat-card-value {
  color: var(--zone-danger);
}

/* Zone stats list */
.zone-stats-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}

.zone-stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  background: var(--bg-surface-alt);
  border-radius: var(--border-radius-sm);
  border-left: 3px solid var(--zone-normal);
  transition: background var(--transition-fast),
    border-color var(--transition-fast);
}

.zone-stat-row:hover {
  background: var(--bg-surface-hover);
}

.zone-stat-row.normal {
  border-left-color: var(--zone-normal);
}

.zone-stat-row.warning {
  border-left-color: var(--zone-warning);
}

.zone-stat-row.danger {
  border-left-color: var(--zone-danger);
}

.zone-stat-name {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-primary);
}

.zone-stat-count {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* ----- Charts Area ----- */
#charts-panel {
  flex: 1;
  min-height: 0;
}

.chart-container {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 150px;
}

.chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

.chart-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.chart-tab {
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--border-radius-sm);
  transition: background var(--transition-fast), color var(--transition-fast),
    border-color var(--transition-fast);
}

.chart-tab:hover {
  color: var(--text-secondary);
  background: var(--bg-surface-alt);
}

.chart-tab.active {
  color: var(--text-primary);
  background: var(--bg-surface-alt);
  border-color: var(--border-focus);
}

/* ----- Camera List Panel ----- */
#cameras-panel {
  flex-shrink: 0;
  max-height: 200px;
}

.camera-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.camera-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bg-surface-alt);
  border: 1px solid var(--border-subtle);
  border-radius: var(--border-radius);
  transition: background var(--transition-fast),
    border-color var(--transition-fast);
}

.camera-card:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-focus);
}

.camera-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background var(--transition-normal),
    box-shadow var(--transition-normal);
}

.camera-status-dot.online {
  background: var(--camera-online);
  box-shadow: 0 0 6px rgba(0, 200, 83, 0.4);
}

.camera-status-dot.offline {
  background: var(--camera-offline);
}

.camera-status-dot.error {
  background: var(--camera-error);
  box-shadow: 0 0 6px rgba(255, 23, 68, 0.4);
  animation: pulse-dot 2s ease-in-out infinite;
}

.camera-info {
  flex: 1;
  min-width: 0;
}

.camera-name {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.camera-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
}

.camera-fps {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.camera-thumbnail {
  width: 48px;
  height: 36px;
  border-radius: var(--border-radius-sm);
  background: var(--bg-primary);
  border: 1px solid var(--border-subtle);
  object-fit: cover;
  flex-shrink: 0;
}

/* ----- Alert Panel (Full Width Bottom) ----- */
#alerts-panel {
  grid-column: 1 / -1;
  max-height: 200px;
  transition: max-height var(--transition-slow);
}

#alerts-panel.collapsed {
  max-height: 44px;
  overflow: hidden;
}

.alert-panel-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  border-radius: var(--border-radius-sm);
  transition: color var(--transition-fast), background var(--transition-fast);
}

.alert-panel-toggle:hover {
  color: var(--text-secondary);
  background: var(--bg-surface-alt);
}

.alert-panel-toggle .toggle-icon {
  transition: transform var(--transition-normal);
}

#alerts-panel.collapsed .alert-panel-toggle .toggle-icon {
  transform: rotate(-90deg);
}

#alert-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.alert-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg-surface-alt);
  border-radius: var(--border-radius-sm);
  border-left: 3px solid var(--alert-info);
  transition: background var(--transition-fast);
  animation: alert-fade-in 0.4s ease-out;
}

.alert-item:hover {
  background: var(--bg-surface-hover);
}

.alert-item.info {
  border-left-color: var(--alert-info);
}

.alert-item.warning {
  border-left-color: var(--alert-warning);
}

.alert-item.danger {
  border-left-color: var(--alert-danger);
  animation: alert-fade-in 0.4s ease-out, alert-danger-pulse 3s ease-in-out 0.4s infinite;
}

.alert-item.acknowledged {
  opacity: 0.5;
}

.alert-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  margin-top: 1px;
}

.alert-item.info .alert-icon {
  color: var(--alert-info);
}

.alert-item.warning .alert-icon {
  color: var(--alert-warning);
}

.alert-item.danger .alert-icon {
  color: var(--alert-danger);
}

.alert-content {
  flex: 1;
  min-width: 0;
}

.alert-message {
  font-size: 0.85rem;
  color: var(--text-primary);
  line-height: 1.4;
}

.alert-zone {
  font-weight: 600;
}

.alert-timestamp {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
}

.alert-actions {
  flex-shrink: 0;
  display: flex;
  gap: 4px;
}

.alert-action-btn {
  padding: 4px 8px;
  font-size: 0.7rem;
  color: var(--text-muted);
  border: 1px solid var(--border-subtle);
  border-radius: var(--border-radius-sm);
  transition: color var(--transition-fast), background var(--transition-fast),
    border-color var(--transition-fast);
}

.alert-action-btn:hover {
  color: var(--text-primary);
  background: var(--bg-surface-hover);
  border-color: var(--border-focus);
}

.alert-empty {
  text-align: center;
  padding: 20px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.alert-count-badge {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: var(--border-radius-pill);
  background: var(--alert-danger);
  color: #fff;
  min-width: 20px;
  text-align: center;
  line-height: 1.3;
}

.alert-count-badge.warning {
  background: var(--alert-warning);
  color: #000;
}

.alert-count-badge.info {
  background: var(--alert-info);
  color: #fff;
}

.alert-count-badge.zero {
  background: var(--bg-surface-alt);
  color: var(--text-muted);
}

/* ----- Badge ----- */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 8px;
  border-radius: var(--border-radius-pill);
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

.badge-count {
  font-family: var(--font-mono);
  min-width: 22px;
  text-align: center;
}

.badge-green {
  background: rgba(0, 200, 83, 0.15);
  color: var(--zone-normal);
}

.badge-amber {
  background: rgba(255, 214, 0, 0.15);
  color: var(--zone-warning);
}

.badge-red {
  background: rgba(255, 23, 68, 0.15);
  color: var(--zone-danger);
}

.badge-blue {
  background: rgba(41, 121, 255, 0.15);
  color: var(--alert-info);
}

.badge-grey {
  background: rgba(106, 106, 138, 0.15);
  color: var(--text-muted);
}

.badge-solid-green {
  background: var(--zone-normal);
  color: #000;
}

.badge-solid-amber {
  background: var(--zone-warning);
  color: #000;
}

.badge-solid-red {
  background: var(--zone-danger);
  color: #fff;
}

.badge-solid-blue {
  background: var(--alert-info);
  color: #fff;
}

/* Status-named badge aliases (used by app.js) */
.badge-normal  { background: rgba(0, 200, 83, 0.15); color: var(--zone-normal); }
.badge-warning { background: rgba(255, 214, 0, 0.15); color: var(--zone-warning); }
.badge-danger  { background: rgba(255, 23, 68, 0.15); color: var(--zone-danger); }
.badge-info    { background: rgba(41, 121, 255, 0.15); color: var(--alert-info); }

/* Alert level aliases (level-* used by app.js) */
.alert-item.level-info    { border-left: 3px solid var(--alert-info); }
.alert-item.level-warning { border-left: 3px solid var(--zone-warning); }
.alert-item.level-danger  { border-left: 3px solid var(--zone-danger); }

/* Zone table row status */
.zone-row { transition: background var(--transition-fast); }
.zone-row:hover { background: var(--bg-surface-alt); }
.zone-row.status-normal  td:first-child { border-left: 3px solid var(--zone-normal); }
.zone-row.status-warning td:first-child { border-left: 3px solid var(--zone-warning); }
.zone-row.status-danger  td:first-child { border-left: 3px solid var(--zone-danger); }
.zone-name { font-weight: 500; }
.zone-count { font-family: var(--font-mono); text-align: right; }
.zone-score { width: 100px; }

/* Score bar (crowd score visualization in zone table) */
.score-bar {
  height: 8px;
  background: var(--bg-surface-alt);
  border-radius: 4px;
  overflow: hidden;
}
.score-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--zone-normal);
  transition: width 0.5s ease, background 0.5s ease;
}
.status-warning .score-fill { background: var(--zone-warning); }
.status-danger  .score-fill { background: var(--zone-danger); }

/* ----- Buttons ----- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 500;
  border-radius: var(--border-radius-sm);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  background: var(--bg-surface-alt);
  transition: background var(--transition-fast), color var(--transition-fast),
    border-color var(--transition-fast);
}

.btn:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
  border-color: var(--border-focus);
}

.btn-primary {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-bright);
  border-color: var(--accent-bright);
  color: #fff;
}

.btn-sm {
  padding: 3px 10px;
  font-size: 0.7rem;
}

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: var(--border-radius-sm);
}

/* ----- Loading / Spinner ----- */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border-subtle);
  border-top-color: var(--accent-bright);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.spinner-sm {
  width: 16px;
  height: 16px;
  border-width: 2px;
}

.spinner-lg {
  width: 40px;
  height: 40px;
  border-width: 4px;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(26, 26, 46, 0.85);
  z-index: 50;
  backdrop-filter: blur(4px);
}

.loading-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ----- Skeleton Loading ----- */
.skeleton {
  background: linear-gradient(
    90deg,
    var(--bg-surface-alt) 25%,
    var(--bg-surface-hover) 50%,
    var(--bg-surface-alt) 75%
  );
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
  border-radius: var(--border-radius-sm);
}

.skeleton-line {
  height: 14px;
  margin-bottom: 8px;
}

.skeleton-line:last-child {
  margin-bottom: 0;
  width: 60%;
}

.skeleton-card {
  height: 80px;
}

.skeleton-circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.skeleton-stat {
  height: 60px;
}

/* ----- Toast Notifications ----- */
.toast-container {
  position: fixed;
  top: calc(var(--header-height) + 12px);
  right: 12px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: 360px;
  width: 100%;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-elevated);
  pointer-events: auto;
  animation: toast-slide-in 0.3s ease-out;
  backdrop-filter: blur(8px);
}

.toast.toast-exit {
  animation: toast-slide-out 0.25s ease-in forwards;
}

.toast-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  margin-top: 1px;
}

.toast.success .toast-icon {
  color: var(--zone-normal);
}

.toast.error .toast-icon {
  color: var(--zone-danger);
}

.toast.warning .toast-icon {
  color: var(--zone-warning);
}

.toast.info .toast-icon {
  color: var(--alert-info);
}

.toast-body {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.toast-message {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.toast-close {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--border-radius-sm);
  transition: color var(--transition-fast), background var(--transition-fast);
}

.toast-close:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.05);
}

/* Toast border accents */
.toast.success {
  border-left: 3px solid var(--zone-normal);
}

.toast.error {
  border-left: 3px solid var(--zone-danger);
}

.toast.warning {
  border-left: 3px solid var(--zone-warning);
}

.toast.info {
  border-left: 3px solid var(--alert-info);
}

/* ----- Tables ----- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.data-table th {
  text-align: left;
  padding: 8px 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.7rem;
  border-bottom: 1px solid var(--border-subtle);
}

.data-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
}

.data-table tbody tr {
  transition: background var(--transition-fast);
}

.data-table tbody tr:hover {
  background: var(--bg-surface-alt);
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.data-table .cell-number {
  font-family: var(--font-mono);
  font-weight: 600;
  text-align: right;
}

/* ----- Divider ----- */
.divider {
  border: none;
  border-top: 1px solid var(--border-subtle);
  margin: 12px 0;
}

/* ----- Empty State ----- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px 16px;
  color: var(--text-muted);
  text-align: center;
}

.empty-state-icon {
  font-size: 2rem;
  opacity: 0.4;
}

.empty-state-text {
  font-size: 0.85rem;
}

/* ----- Tripwire Lines ----- */
.tripwire-line {
  stroke-width: 2;
  stroke-dasharray: 6 3;
  fill: none;
  opacity: 0.8;
}

.tripwire-line.entry {
  stroke: var(--zone-normal);
}

.tripwire-line.exit {
  stroke: var(--zone-danger);
}

.tripwire-arrow {
  fill: var(--text-primary);
  opacity: 0.7;
}

/* ----- Trajectory Lines ----- */
.trajectory-path {
  fill: none;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.5;
}

.trajectory-head {
  r: 3;
  fill: var(--accent-bright);
  opacity: 0.8;
}

/* ----- Heatmap Overlay ----- */
.heatmap-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.6;
  mix-blend-mode: screen;
}

/* ----- Animations / Keyframes ----- */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse-dot {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.5;
    transform: scale(0.85);
  }
}

@keyframes zone-danger-pulse {
  0%,
  100% {
    fill-opacity: 0.2;
    stroke-opacity: 0.8;
  }
  50% {
    fill-opacity: 0.35;
    stroke-opacity: 1;
  }
}

@keyframes alert-fade-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes alert-danger-pulse {
  0%,
  100% {
    box-shadow: none;
  }
  50% {
    box-shadow: var(--shadow-glow-danger);
  }
}

@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

@keyframes toast-slide-in {
  from {
    opacity: 0;
    transform: translateX(40px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes toast-slide-out {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(40px);
  }
}

@keyframes number-pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.15);
  }
  100% {
    transform: scale(1);
  }
}

.number-updated {
  animation: number-pop 0.3s ease-out;
}

/* ----- Responsive: Tablet (768px - 1199px) ----- */
@media (max-width: 1199px) {
  :root {
    --gap: 10px;
    --padding-panel: 12px;
  }

  html {
    font-size: 13px;
  }

  header {
    padding: 0 16px;
  }

  main {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(300px, 1fr) auto auto;
    height: auto;
    min-height: calc(100vh - var(--header-height));
    overflow-y: auto;
  }

  body {
    overflow: auto;
  }

  #map-view {
    grid-row: auto;
    min-height: 350px;
    max-height: 50vh;
  }

  .sidebar-right {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--gap);
  }

  #charts-panel {
    grid-column: 1 / -1;
    min-height: 200px;
  }

  #cameras-panel {
    max-height: none;
  }

  #alerts-panel {
    grid-column: auto;
    max-height: 180px;
  }

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

/* ----- Responsive: Small Tablet / Large Phone ----- */
@media (max-width: 767px) {
  :root {
    --gap: 8px;
    --padding-panel: 10px;
    --header-height: 48px;
  }

  html {
    font-size: 12px;
  }

  header {
    padding: 0 12px;
  }

  .header-title {
    font-size: 0.95rem;
  }

  .header-clock {
    display: none;
  }

  main {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    padding: var(--gap);
  }

  #map-view {
    min-height: 280px;
    max-height: 40vh;
  }

  .sidebar-right {
    grid-template-columns: 1fr;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }

  .stat-card-value {
    font-size: 1.3rem;
  }

  .map-legend {
    flex-wrap: wrap;
    gap: 6px 10px;
  }

  .toast-container {
    right: 8px;
    left: 8px;
    max-width: none;
  }
}

/* ----- Responsive: Desktop Wide (1440px+) ----- */
@media (min-width: 1440px) {
  html {
    font-size: 15px;
  }

  :root {
    --gap: 14px;
    --padding-panel: 18px;
  }

  .stats-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ----- Utility Classes ----- */
.flex {
  display: flex;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.gap-sm {
  gap: 4px;
}

.gap-md {
  gap: 8px;
}

.gap-lg {
  gap: 16px;
}

.mt-sm {
  margin-top: 4px;
}

.mt-md {
  margin-top: 8px;
}

.mt-lg {
  margin-top: 16px;
}

.mb-sm {
  margin-bottom: 4px;
}

.mb-md {
  margin-bottom: 8px;
}

.mb-lg {
  margin-bottom: 16px;
}

.w-full {
  width: 100%;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.hidden {
  display: none !important;
}

.invisible {
  visibility: hidden;
}

.no-select {
  user-select: none;
}

.pointer {
  cursor: pointer;
}

/* ----- Print ----- */
@media print {
  body {
    background: #fff;
    color: #000;
    overflow: visible;
  }

  header {
    background: #fff;
    border-bottom: 2px solid #000;
    color: #000;
  }

  main {
    display: block;
    height: auto;
    overflow: visible;
  }

  section,
  .panel {
    background: #fff;
    border: 1px solid #ccc;
    break-inside: avoid;
    margin-bottom: 12px;
  }

  .toast-container,
  .map-controls,
  .loading-overlay {
    display: none;
  }

  h2 {
    color: #333;
  }

  .stat-card {
    background: #f5f5f5;
    border: 1px solid #ccc;
  }

  .stat-card-value {
    color: #000;
  }
}
