:root {
  --ink: #0f1e19;
  --ink-2: #16302a;
  --ink-3: #1f3d35;
  --line: #2b4a41;
  --paper: #eef0ea;
  --text: #e8ece6;
  --muted: #9db1a9;
  --lvl-0: #3fa36b;
  --lvl-1: #d2b33a;
  --lvl-2: #e3822f;
  --lvl-3: #d9443a;
  --fire: #ff5a1f;
  --off: #8a948f;
  --accent: #7fd1ae;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-columns: 400px minmax(0, 1fr);
  font: 14px/1.45 var(--sans);
  color: var(--text);
  background: var(--ink);
}
h1, h2 { margin: 0; font-weight: 650; }
h2 {
  margin: 22px 0 8px;
  font-size: 11px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--muted);
}
.muted { color: var(--muted); margin: 0 0 10px; font-size: 13px; }
code { font-family: var(--mono); font-size: 12px; }
button, input { font: inherit; color: inherit; }

/* ---- sidebar ---- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100vh;
  background: var(--ink);
  border-right: 1px solid var(--line);
}
.brand { display: flex; gap: 12px; align-items: center; padding: 18px 20px 14px; }
.brand h1 { font-size: 19px; letter-spacing: -.01em; }
.brand-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.brand-mark {
  position: relative; width: 26px; height: 32px; flex: none;
  border: 2px solid var(--text); border-radius: 3px 3px 6px 6px; overflow: hidden;
}
.brand-mark::before { content: ""; position: absolute; left: -4px; right: -4px; top: -2px; height: 3px; background: var(--text); }
.brand-mark span { position: absolute; left: 0; right: 0; bottom: 0; height: 62%; background: linear-gradient(var(--lvl-2), var(--lvl-3)); }
.conn::before { content: "●"; margin-right: 4px; color: var(--off); }
.conn.live::before { color: var(--lvl-0); }
.conn.down::before { color: var(--lvl-3); }

.user { display: flex; gap: 8px; align-items: center; padding: 0 20px 10px; font-size: 12px; color: var(--muted); }
.user[hidden] { display: none; }
#user-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user .role { padding: 0 6px; border: 1px solid var(--line); border-radius: 999px; }
.user button { margin-left: auto; padding: 2px 10px; border-radius: 6px; border: 1px solid var(--line); background: none; cursor: pointer; }
.user button:hover { color: var(--text); }
body:is([data-role="viewer"], [data-role="none"]) .operator-only { display: none; }
.tabs { display: flex; gap: 2px; padding: 0 12px; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; padding: 10px 6px 9px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--muted); cursor: pointer; font-weight: 600;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 3px; border-radius: 9px;
  background: var(--lvl-3); color: #fff; font-size: 11px; line-height: 18px; font-variant-numeric: tabular-nums;
}
.badge.fire { background: var(--fire); animation: throb 1.2s ease-in-out infinite; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--line); border-bottom: 1px solid var(--line); }
.kpi { display: flex; flex-direction: column; padding: 12px 12px 10px; background: var(--ink-2); min-width: 0; }
.kpi-value { font: 600 20px/1.1 var(--mono); font-variant-numeric: tabular-nums; }
.kpi-label { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-fire.hot { background: #4a1a0e; }
.kpi-fire.hot .kpi-value { color: var(--fire); }

.panels { flex: 1; min-height: 0; overflow-y: auto; padding: 0 20px 24px; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; }
.panel-head h2 { margin-bottom: 8px; }

.legend { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 13px; }
.legend li { display: flex; gap: 8px; align-items: center; }
.swatch { width: 10px; height: 14px; border-radius: 2px; display: inline-block; }
.swatch.lvl-0 { background: var(--lvl-0); } .swatch.lvl-1 { background: var(--lvl-1); }
.swatch.lvl-2 { background: var(--lvl-2); } .swatch.lvl-3 { background: var(--lvl-3); }
.swatch.lvl-fire { background: var(--fire); box-shadow: 0 0 6px var(--fire); }
.swatch.lvl-off { background: transparent; border: 1.5px dashed var(--off); }

.rank, .stops { list-style: none; margin: 0; padding: 0; counter-reset: item; }
.rank li, .stops li {
  display: grid; grid-template-columns: 26px minmax(0, 1fr) auto; gap: 8px; align-items: center;
  padding: 7px 0; border-bottom: 1px solid var(--ink-3); cursor: pointer;
}
.rank li::before, .stops li::before {
  counter-increment: item; content: counter(item);
  font: 600 11px var(--mono); color: var(--muted); text-align: right;
}
.rank li:hover, .stops li:hover { background: var(--ink-2); }
.addr { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.addr small { display: block; color: var(--muted); font: 11px var(--mono); }
.pct { font: 600 13px var(--mono); font-variant-numeric: tabular-nums; }
.pct.lvl-0 { color: var(--lvl-0); } .pct.lvl-1 { color: var(--lvl-1); } .pct.lvl-2 { color: var(--lvl-2); } .pct.lvl-3 { color: var(--lvl-3); }
.stops li.priority::before { content: "🔥"; font-size: 13px; }
.stops li.trip-break { cursor: default; display: block; padding: 10px 0 4px; color: var(--muted); font-size: 11px; letter-spacing: .1em; text-transform: uppercase; }
.stops li.trip-break::before { content: none; }

/* bin detail */
.bin-detail { margin-top: 18px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--ink-2); }
.bin-detail header { display: flex; justify-content: space-between; gap: 10px; }
.bin-detail h3 { margin: 0; font-size: 16px; }
.bin-detail .close { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; }
.bin-detail dl { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 3px 12px; margin: 12px 0 0; font-size: 13px; }
.bin-detail dt { color: var(--muted); }
.bin-detail dd { margin: 0; font-family: var(--mono); font-size: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; background: var(--ink-3); }
.chip.fire { background: var(--fire); color: #fff; }
.chip.overflow { background: var(--lvl-3); color: #fff; }
.chip.offline { background: var(--off); color: #111; }
.chip.battery_low, .chip.sensor_fault { background: var(--lvl-1); color: #111; }
.sensors { display: flex; gap: 6px; align-items: flex-end; height: 64px; margin: 12px 0 2px; }
.sensor { flex: 1; position: relative; height: 100%; border: 1px solid var(--line); border-top: 0; border-radius: 0 0 4px 4px; }
.sensor i { position: absolute; left: 0; right: 0; bottom: 0; background: var(--ink-3); }
.sensor.used i { background: var(--accent); opacity: .75; }
.sensor.bad { border-style: dashed; }
.sensor span { position: absolute; top: 2px; left: 0; right: 0; text-align: center; font: 10px var(--mono); color: var(--text); text-shadow: 0 0 3px var(--ink), 0 0 2px var(--ink); }
.caption { font-size: 11px; color: var(--muted); }

/* routes */
.controls { display: grid; gap: 12px; margin: 10px 0 4px; }
.controls label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.controls output { float: right; color: var(--text); font-family: var(--mono); }
.controls input[type="number"] { padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--ink-2); }
input[type="range"] { accent-color: var(--accent); }
.check { display: flex !important; grid-template-columns: none; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 14px 0 6px; }
.summary div { padding: 10px; border-radius: 8px; background: var(--ink-2); }
.summary b { display: block; font: 600 18px var(--mono); }
.summary span { font-size: 11px; color: var(--muted); }

/* stats */
.chart { position: relative; padding: 8px; border-radius: var(--radius); background: var(--ink-2); }
.zones { display: grid; gap: 8px; }
.zone { padding: 10px 12px; border-radius: 8px; background: var(--ink-2); border-left: 4px solid var(--lvl-0); cursor: pointer; }
.zone.strained { border-left-color: var(--lvl-2); }
.zone.overloaded { border-left-color: var(--lvl-3); }
.zone header { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
.zone header span { font: 600 12px var(--mono); }
.zone p { margin: 6px 0 0; font-size: 13px; }
.zone small { color: var(--muted); }
.areas { width: 100%; border-collapse: collapse; font-size: 13px; }
.areas th { text-align: left; font-weight: 500; font-size: 11px; color: var(--muted); padding: 4px 4px 6px 0; }
.areas td { padding: 5px 4px 5px 0; border-top: 1px solid var(--ink-3); font-family: var(--mono); font-size: 12px; }
.areas td:first-child { font-family: var(--sans); }

/* alerts */
.alerts { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.alert { display: grid; grid-template-columns: 8px minmax(0, 1fr) auto; gap: 10px; padding: 9px 10px; border-radius: 8px; background: var(--ink-2); cursor: pointer; }
.alert::before { content: ""; border-radius: 4px; background: var(--off); }
.alert.critical::before { background: var(--fire); }
.alert.warning::before { background: var(--lvl-2); }
.alert.info::before { background: var(--lvl-1); }
.alert.resolved { opacity: .55; }
.alert b { font-size: 13px; }
.alert p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.alert time { font: 11px var(--mono); color: var(--muted); white-space: nowrap; }
.alert button { margin-top: 4px; padding: 2px 8px; border-radius: 6px; border: 1px solid var(--line); background: none; cursor: pointer; font-size: 11px; }
.empty { color: var(--muted); font-size: 13px; padding: 8px 0; }

/* ---- map ---- */
.map-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--paper); }
.tile-warning {
  position: absolute; z-index: 800; left: 50%; top: 12px; transform: translateX(-50%); max-width: 80%;
  padding: 8px 12px; border-radius: 8px; background: rgba(15, 30, 25, .92); font-size: 12px;
}
.leaflet-container { font: 13px/1.4 var(--sans); }
.leaflet-tooltip { font-size: 12px; }

/* Bin marker: a tiny bin whose bar fills up. */
.bin-icon { background: none; border: 0; }
.bin-glyph {
  position: relative; width: 14px; height: 18px; margin: 1px 0 0 3px;
  border: 2px solid #1b1f1d; border-radius: 2px 2px 4px 4px; background: #fff; overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
}
.bin-glyph::before { content: ""; position: absolute; left: -1px; right: -1px; top: 0; height: 2px; background: #1b1f1d; z-index: 1; }
.bin-glyph i { position: absolute; left: 0; right: 0; bottom: 0; background: var(--lvl-0); transition: height .6s ease; }
.bin-glyph.lvl-1 i { background: var(--lvl-1); }
.bin-glyph.lvl-2 i { background: var(--lvl-2); }
.bin-glyph.lvl-3 i { background: var(--lvl-3); }
.bin-glyph.offline { border-style: dashed; border-color: #555; opacity: .6; }
.bin-glyph.offline i { background: var(--off); }
.bin-glyph.selected { outline: 3px solid var(--accent); outline-offset: 2px; }
.bin-glyph.fire { border-color: #7a1400; background: #ffe2d4; }
.bin-glyph.fire i { background: var(--fire); }
.fire-ring {
  position: absolute; left: -13px; top: -12px; width: 48px; height: 48px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 90, 31, .75) 20%, rgba(255, 90, 31, 0) 70%);
  animation: throb 1s ease-in-out infinite; pointer-events: none;
}
@keyframes throb { 0%, 100% { transform: scale(.85); opacity: .8; } 50% { transform: scale(1.25); opacity: 1; } }

.stop-icon, .depot-icon, .placement-icon { background: none; border: 0; }
.stop-num {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: #10231d; color: #fff; font: 700 11px var(--mono); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .4);
}
.stop-num.priority { background: var(--fire); }
.depot {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 6px;
  background: #10231d; color: var(--accent); font: 800 13px var(--mono); border: 2px solid var(--accent);
}
.placement {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  border: 2px dashed #10231d; background: rgba(127, 209, 174, .85); color: #10231d; font: 800 15px/1 var(--mono);
}

/* toasts */
.toasts { position: fixed; z-index: 1000; right: 16px; bottom: 16px; display: grid; gap: 8px; width: min(360px, calc(100vw - 32px)); }
.toast {
  padding: 12px 14px; border-radius: 10px; background: var(--ink-2); border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .35); animation: slide .25s ease-out; cursor: pointer;
}
.toast.critical { background: #5a1a08; border-color: var(--fire); }
.toast b { display: block; }
.toast p { margin: 2px 0 0; font-size: 12px; color: #f3d6c9; }
@keyframes slide { from { transform: translateY(12px); opacity: 0; } }

@media (max-width: 820px) {
  body { grid-template-columns: 1fr; grid-template-rows: 55vh auto; }
  .map-wrap { grid-row: 1; min-height: 55vh; }
  .sidebar { grid-row: 2; height: auto; border-right: 0; }
  .panels { overflow: visible; }
}
