:root {
  --bg: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --fg: #e6edf3;
  --fg-muted: #8b949e;
  --border: #30363d;
  --accent: #1566A9;
  --accent-light: #58a6ff;
  --online: #3fb950;
  --offline: #f85149;
  --new-node: #93E929;
  --gateway: #d2a8ff;
  --link-good: #04C714;
  --link-bad: #F02311;
  --sidebar-width: 380px;
  --header-height: 48px;
  --radius: 8px;
  --transition: 0.2s ease;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
  height: 100vh;
}

a { color: var(--accent-light); }
a:visited { color: var(--accent-light); }

#app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--header-height) 1fr;
  grid-template-areas: "header header" "sidebar map";
  height: 100vh;
}

/* --- Header --- */
header {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
}

.header-brand {
  font-weight: 700;
  font-size: 18px;
  color: var(--accent-light);
  white-space: nowrap;
}

.header-stats {
  font-size: 13px;
  color: var(--fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.header-links {
  display: flex;
  gap: 12px;
}
.header-links a {
  color: var(--fg-muted);
  text-decoration: none;
  font-size: 13px;
  transition: color var(--transition);
}
.header-links a:hover { color: var(--accent-light); }

.header-sse {
  display: flex;
  align-items: center;
  gap: 6px;
}
.sse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--fg-muted);
  transition: background var(--transition);
}
.sse-dot.connected { background: var(--online); animation: pulse 2s infinite; }
.sse-dot.error { background: var(--offline); }

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

/* --- Sidebar --- */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 900;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab {
  flex: 1;
  padding: 10px 8px;
  background: none;
  border: none;
  color: var(--fg-muted);
  font-size: 13px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all var(--transition);
}
.tab:hover { color: var(--fg); background: var(--bg-tertiary); }
.tab.active { color: var(--accent-light); border-bottom-color: var(--accent-light); }

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.tab-pane { padding: 12px; }
.tab-pane.hidden { display: none; }

/* --- Search --- */
#search-box { position: relative; margin-bottom: 12px; }
#search-input, #list-search {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
}
#search-input:focus, #list-search:focus {
  border-color: var(--accent-light);
}
.search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 300px;
  overflow-y: auto;
  z-index: 100;
}
.search-results.hidden { display: none; }
.search-item {
  padding: 8px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  transition: background var(--transition);
}
.search-item:hover { background: var(--bg); }

/* --- Node detail --- */
#node-detail {
  background: var(--bg-secondary);
}

.detail-close {
  position: sticky;
  top: 0;
  float: right;
  background: var(--bg-secondary);
  border: none;
  color: var(--fg-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 2px 8px;
  z-index: 10;
  border-radius: var(--radius);
  line-height: 1;
}
.detail-close:hover { color: var(--fg); background: var(--bg-tertiary); }

.node-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.node-header h2 {
  font-size: 18px;
  font-weight: 600;
  word-break: break-word;
}
.node-status {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.node-status.online { background: var(--online); box-shadow: 0 0 6px var(--online); }
.node-status.offline { background: var(--offline); box-shadow: 0 0 6px var(--offline); }

.device-image {
  width: 100%;
  max-height: 150px;
  object-fit: contain;
  margin: 8px 0;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 8px;
}

.device-warning {
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 12px;
  line-height: 1.4;
  margin: 8px 0;
}
.device-warning a { color: inherit; text-decoration: underline; }
.device-warning.eol {
  background: rgba(208, 62, 42, 0.15);
  border-left: 3px solid #D43E2A;
  color: #ff6b5b;
}
.device-warning.deprecated {
  background: rgba(255, 214, 0, 0.1);
  border-left: 3px solid #FFD600;
  color: #ffd600;
}

.detail-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  font-size: 13px;
  margin-bottom: 16px;
}
.detail-grid dt { color: var(--fg-muted); white-space: nowrap; }
.detail-grid dd { word-break: break-all; }

.progress-bar {
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
  margin: 2px 0 4px;
}
.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width var(--transition);
}
.bar-green { background: var(--online); }
.bar-yellow { background: #d29922; }
.bar-red { background: var(--offline); }

.neighbour-list {
  list-style: none;
}
.neighbour-item {
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color var(--transition);
}
.neighbour-item:hover { color: var(--accent-light); }

.grafana-link {
  display: inline-block;
  margin-top: 12px;
  padding: 8px 16px;
  background: var(--bg-tertiary);
  color: var(--accent-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  font-size: 13px;
  transition: all var(--transition);
}
.grafana-link:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.charts-container {
  margin-top: 16px;
}
.chart-wrapper {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}
.chart-wrapper h4 {
  font-size: 13px;
  color: var(--fg-muted);
  margin-bottom: 8px;
}

/* --- Node list --- */
.list-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.list-controls select {
  padding: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-size: 13px;
}

.node-list {
  max-height: calc(100vh - var(--header-height) - 120px);
  overflow-y: auto;
}
.node-list-item {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  transition: background var(--transition);
}
.node-list-item:hover { background: var(--bg-tertiary); }
.node-list-item .hostname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-list-item .clients { color: var(--fg-muted); font-size: 12px; }
.node-list-item .list-meta { color: var(--fg-muted); font-size: 11px; white-space: nowrap; flex-shrink: 0; }

/* --- Stats --- */
.stat-card {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.stat-card h3 {
  font-size: 14px;
  color: var(--fg-muted);
  margin-bottom: 8px;
}
.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent-light);
}
.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.stat-row:last-child { border: 0; }
.stat-row .label { color: var(--fg-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.stat-row .value { font-weight: 600; margin-left: 8px; }

/* --- Map --- */
#map {
  grid-area: map;
  z-index: 1;
}

/* Custom map markers */
.node-marker {
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.8);
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  transition: transform 0.15s ease;
}
.node-marker:hover { transform: scale(1.3); z-index: 10000 !important; }
.node-marker.online { background: var(--accent); }
.node-marker.online-uplink { background: var(--accent); border-color: var(--online); }
.node-marker.offline { background: var(--offline); opacity: 0.7; }
.node-marker.new-node { background: var(--new-node); border-color: var(--accent); }
.node-marker.gateway { background: var(--gateway); }
.node-marker.selected {
  transform: scale(1.5);
  box-shadow: 0 0 12px var(--accent-light);
  z-index: 10000 !important;
}

/* Leaflet overrides for dark theme */
.leaflet-popup-content-wrapper {
  background: var(--bg-secondary) !important;
  color: var(--fg) !important;
  border-radius: var(--radius) !important;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5) !important;
}
.leaflet-popup-tip { background: var(--bg-secondary) !important; }
.leaflet-popup-content { margin: 12px !important; font-size: 13px; }
.leaflet-popup-content a { color: var(--accent-light); }

.leaflet-control-zoom a {
  background: var(--bg-secondary) !important;
  color: var(--fg) !important;
  border-color: var(--border) !important;
}
.leaflet-control-layers {
  background: var(--bg-secondary) !important;
  color: var(--fg) !important;
  border-color: var(--border) !important;
  border-radius: var(--radius) !important;
}

/* Marker cluster overrides */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background-color: rgba(21, 102, 169, 0.4) !important;
}
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background-color: var(--accent) !important;
  color: #fff !important;
  font-weight: 600;
}

/* --- Responsive --- */
@media (max-width: 768px) {
  :root { --sidebar-width: 100vw; }
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: var(--header-height) 50vh 1fr;
    grid-template-areas: "header" "map" "sidebar";
  }
  .sidebar { border-right: none; border-top: 1px solid var(--border); }
  .header-links { display: none; }
}

/* --- Force Graph Canvas --- */
#graph-canvas {
  position: absolute;
  top: var(--header-height);
  right: 0;
  width: calc(100vw - var(--sidebar-width));
  height: calc(100vh - var(--header-height));
  background: var(--bg);
  z-index: 2;
}
#graph-canvas.hidden { display: none; }

/* --- Domain filter in list --- */
.list-controls { flex-wrap: wrap; }
.list-controls select { min-width: 100px; flex: 1; }

/* --- uPlot dark overrides --- */
.u-title { color: var(--fg-muted) !important; font-size: 12px !important; }
.u-legend { font-size: 12px !important; }
.u-legend th { color: var(--fg-muted) !important; }
.u-legend td { color: var(--fg) !important; }
.u-series > * { padding: 2px 4px !important; }

/* --- About code blocks --- */
#about-content code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
  color: var(--accent-light);
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg-muted); }
