:root {
  --bg: #f4f2ec;
  --surface: #ffffff;
  --text: #1d2420;
  --muted: #66706a;
  --border: #dfdcd2;
  --accent: #1f3a2e;
  --online: #2e9e5b;
  --stale: #d99a1e;
  --offline: #c4423a;
  --unknown: #8a918d;
  --shadow: 0 6px 24px rgba(0, 0, 0, .12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131714;
    --surface: #1c221e;
    --text: #e8ece9;
    --muted: #9aa49e;
    --border: #2d3530;
    --accent: #9fd3b4;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
  z-index: 1000;
}
.brand { display: flex; gap: 10px; align-items: center; }
.logo { font-size: 26px; }
h1 { font-size: 17px; margin: 0; color: var(--accent); }
h2 { font-size: 15px; margin: 0; }
.sub, .muted { color: var(--muted); font-size: 12.5px; margin: 0; }
.icon-btn {
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  width: 38px; height: 38px; border-radius: 10px; font-size: 20px; cursor: pointer;
}
.actions { display: flex; gap: 8px; }
.text-btn {
  border: 1px solid var(--accent); background: transparent; color: var(--accent);
  height: 38px; padding: 0 14px; border-radius: 10px; font-weight: 600; cursor: pointer;
  white-space: nowrap; flex-shrink: 0;
}
.text-btn:disabled { opacity: .5; cursor: progress; }
.span2 { grid-column: 1 / -1; }
.icon-btn.spin { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.layout { flex: 1; display: flex; min-height: 0; position: relative; }
#map { flex: 1; min-height: 0; }
.panel {
  width: 360px; background: var(--surface); border-left: 1px solid var(--border);
  overflow-y: auto; padding: 14px 16px;
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.sheet-toggle { display: none; }

.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.card {
  border: 1px solid var(--border); border-radius: 12px; padding: 12px;
  background: var(--bg); cursor: pointer; transition: border-color .15s;
}
.card:hover, .card.active { border-color: var(--accent); }
.card-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card-name { font-weight: 600; display: flex; gap: 6px; align-items: center; }
.badge {
  font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
  color: #fff; white-space: nowrap;
}
.badge.online { background: var(--online); }
.badge.offline { background: var(--offline); }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin-top: 10px; font-size: 13px; }
.metrics dt { color: var(--muted); font-size: 11.5px; }
.metrics dd { margin: 0; font-variant-numeric: tabular-nums; }
.battery { display: flex; align-items: center; gap: 6px; }
.bar { flex: 1; height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; max-width: 70px; }
.bar > span { display: block; height: 100%; }
.warn { margin-top: 8px; font-size: 12px; color: var(--stale); }

.legend { display: flex; gap: 14px; margin: 14px 0 4px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.map-legend { display: none; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }
.dot.online { background: var(--online); }
.dot.offline { background: var(--offline); }

/* Map markers */
.node-marker {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  font-size: 19px; background: #fff; border: 4px solid var(--unknown); box-shadow: var(--shadow);
}
.node-marker.online { border-color: var(--online); }
.node-marker.offline { border-color: var(--offline); }
.leaflet-popup-content { font: 13px/1.4 system-ui, sans-serif; }
.leaflet-popup-content b { font-size: 14px; }

/* Mobile: map on top, bottom sheet */
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .panel {
    width: 100%; border-left: 0; border-top: 1px solid var(--border);
    max-height: 48vh; border-radius: 16px 16px 0 0; margin-top: -16px; z-index: 900;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    box-shadow: var(--shadow); transition: max-height .25s;
  }
  .panel { padding-top: 18px; }
  /* Legend floats on the map (bottom right, Leaflet control) instead of in the sheet */
  .panel .legend { display: none; }
  .map-legend {
    display: flex; gap: 10px; margin: 0 10px 6px 0 !important; padding: 6px 12px;
    background: var(--surface); color: var(--text); border-radius: 999px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .2); font-size: 12px;
  }
  .topbar { gap: 10px; }
  h1 { font-size: 15px; line-height: 1.2; }
  .logo { font-size: 22px; }
  /* The sheet overlaps the map by 16px: lift Leaflet's scale/attribution clear of it */
  .leaflet-bottom { bottom: 28px; }
  .panel.collapsed { max-height: 80px; overflow: hidden; }
  .panel-head { display: none; }
  .sheet-toggle {
    display: flex; align-items: center; gap: 10px;
    width: 100%; height: 48px; margin-bottom: 12px; padding: 0 6px 0 16px;
    border: 0; border-radius: 999px;
    background: linear-gradient(135deg, #2b5442, #1f3a2e);
    color: #fff; font: 600 15px system-ui, sans-serif; cursor: pointer;
    box-shadow: 0 4px 14px rgba(31, 58, 46, .35), inset 0 1px 0 rgba(255, 255, 255, .12);
    -webkit-tap-highlight-color: transparent;
    transition: transform .12s, box-shadow .2s, background .2s;
  }
  .sheet-toggle:active { transform: scale(.97); }
  .st-summary { display: flex; gap: 10px; font-size: 13px; font-weight: 500; }
  .st-summary span { display: inline-flex; align-items: center; gap: 5px; }
  .st-summary i {
    width: 9px; height: 9px; border-radius: 50%;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, .25);
  }
  .st-summary i.online { background: #4cd486; }
  .st-summary i.offline { background: #f0645b; }
  .st-label { margin-left: auto; opacity: .85; font-weight: 500; font-size: 14px; }
  .st-chevron {
    width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
    background: rgba(255, 255, 255, .16);
    transition: transform .3s cubic-bezier(.4, 0, .2, 1);
  }
  .panel:not(.collapsed) .st-chevron { transform: rotate(180deg); }
  .panel:not(.collapsed) .sheet-toggle {
    background: var(--bg); color: var(--text);
    box-shadow: inset 0 0 0 1px var(--border);
  }
  .panel:not(.collapsed) .st-chevron { background: var(--border); }
  .panel:not(.collapsed) .st-summary i { box-shadow: none; }
}
