:root {
  --hv: #7b2cbf; --mv: #e8590c; --lv: #1c7ed6; --ok: #2f9e44; --off: #e03131; --dead: #adb5bd;
  --bg: #1b2433; --panel: #ffffff; --ink: #1f2937; --muted: #6b7280; --line: #e5e7eb; --accent: #1c7ed6;
  --top: 52px; --sheet-w: 400px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); }
button { font: inherit; cursor: pointer; }

/* ---------- top bar */
#topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top); z-index: 1000; background: var(--bg); color: #fff;
  display: flex; align-items: center; gap: 10px; padding: 0 10px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand img { width: 28px; height: 28px; }
.search { position: relative; flex: 1; max-width: 460px; }
#search { width: 100%; height: 36px; border-radius: 18px; border: 0; padding: 0 14px; font-size: 15px; background: #2d3a50; color: #fff; outline: none; }
#search::placeholder { color: #9aa5b8; }
#search-results { position: absolute; top: 40px; left: 0; right: 0; margin: 0; padding: 4px 0; list-style: none; background: #fff; color: var(--ink); border-radius: 10px; box-shadow: 0 8px 24px #0004; max-height: 60vh; overflow: auto; }
#search-results li { padding: 9px 14px; cursor: pointer; display: flex; gap: 8px; align-items: baseline; }
#search-results li:hover, #search-results li.hl { background: #f1f5f9; }
#search-results small { color: var(--muted); margin-left: auto; white-space: nowrap; }
.top-actions { display: flex; gap: 6px; margin-left: auto; }
.icon-btn { width: 38px; height: 38px; border-radius: 10px; border: 0; background: #2d3a50; color: #fff; font-size: 18px; }
.icon-btn:active { background: #3d4d68; }

#map { position: fixed; top: var(--top); left: 0; right: 0; bottom: 0; background: #e9ecef; }
@media (min-width: 900px) { #map { right: var(--sheet-w); } }

/* ---------- layers popover */
.popover { position: fixed; top: calc(var(--top) + 6px); right: 8px; z-index: 1200; background: #fff; border-radius: 12px; box-shadow: 0 10px 30px #0003; padding: 6px 14px 12px; width: 240px; }
.popover h4 { margin: 10px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.popover label { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.sw { display: inline-block; width: 18px; height: 4px; border-radius: 2px; }
.sw.dot { width: 10px; height: 10px; border-radius: 50%; }

/* ---------- banners, toast */
#outage-banner {
  position: fixed; top: calc(var(--top) + 8px); left: 50%; transform: translateX(-50%); z-index: 900;
  background: var(--off); color: #fff; padding: 7px 14px; border-radius: 20px; font-weight: 600; box-shadow: 0 4px 14px #0003;
  white-space: nowrap; cursor: pointer; max-width: calc(100vw - 90px); overflow: hidden; text-overflow: ellipsis;
}
#outage-banner.ok { background: var(--ok); }
@media (min-width: 900px) { #outage-banner { left: calc(50% - var(--sheet-w) / 2); } }
#draw-hint { position: fixed; top: calc(var(--top) + 48px); left: 50%; transform: translateX(-50%); z-index: 900; background: #1f2937e6; color: #fff; padding: 6px 12px; border-radius: 8px; font-size: 13px; max-width: 92vw; text-align: center; }
@media (min-width: 900px) { #draw-hint { left: calc(50% - var(--sheet-w) / 2); } }
#toast { position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); z-index: 2000; background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: 0 6px 20px #0004; max-width: 90vw; }
#toast.err { background: var(--off); }
@media (min-width: 900px) { #toast { bottom: 24px; left: calc(50% - var(--sheet-w) / 2); } }

/* ---------- sheet (bottom on mobile, sidebar on desktop) */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1100; background: var(--panel);
  border-radius: 16px 16px 0 0; box-shadow: 0 -6px 24px #0003; display: flex; flex-direction: column;
  transition: height .22s ease; padding-bottom: var(--safe-b);
}
#sheet[data-state="peek"] { height: calc(118px + var(--safe-b)); }
#sheet[data-state="half"] { height: 48vh; }
#sheet[data-state="full"] { height: calc(100% - var(--top) - 8px); }
#sheet-handle { height: 22px; display: flex; align-items: center; justify-content: center; touch-action: none; cursor: grab; flex: none; }
#sheet-handle span { width: 42px; height: 5px; border-radius: 3px; background: #cbd5e1; }
@media (min-width: 900px) {
  #sheet, #sheet[data-state] { top: var(--top); left: auto; width: var(--sheet-w); height: auto; border-radius: 0; box-shadow: -2px 0 12px #0002; }
  #sheet-handle { display: none; }
}
#modes { display: flex; gap: 4px; margin: 2px 12px 8px; background: #f1f5f9; border-radius: 10px; padding: 3px; flex: none; }
#modes button { flex: 1; border: 0; background: transparent; padding: 8px 4px; border-radius: 8px; font-weight: 600; color: var(--muted); }
#modes button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px #0002; }
#modes button[data-mode="fault"].active { color: var(--off); }
#panel { overflow-y: auto; padding: 0 14px 16px; flex: 1; -webkit-overflow-scrolling: touch; }

/* ---------- panel content */
.card { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 8px 0; }
.card.alert { border-color: #ffc9c9; background: #fff5f5; }
.card.good { border-color: #b2f2bb; background: #ebfbee; }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.stat { background: #f8fafc; border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 11px; color: var(--muted); }
.stat.bad b { color: var(--off); }
.btn { border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 8px 12px; font-weight: 600; color: var(--ink); min-height: 38px; }
.btn:active { background: #f1f5f9; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--off); }
.btn.warn { background: var(--off); border-color: var(--off); color: #fff; }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .45; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.list { list-style: none; margin: 4px 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 8px; padding: 7px 2px; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.list li .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li.link { cursor: pointer; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; background: #f1f5f9; }
.pill.on { background: #d3f9d8; color: #2b8a3e; }
.pill.off { background: #ffe3e3; color: #c92a2a; }
.pill.warn { background: #fff3bf; color: #e67700; }
table.props { width: 100%; border-collapse: collapse; font-size: 13px; }
table.props td { padding: 5px 2px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.props td:first-child { color: var(--muted); width: 40%; }
table.props input, table.props select, .field input, .field select { width: 100%; font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tool { border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 6px 2px; font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 3px; min-height: 58px; color: var(--ink); }
.tool.active { border-color: var(--accent); background: #e7f5ff; box-shadow: inset 0 0 0 1px var(--accent); }
.tool svg { width: 24px; height: 24px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: #fff; padding: 7px 6px; font-size: 13px; }
.seg button.active { background: var(--accent); color: #fff; }
.field { margin: 8px 0; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.toggle { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; font-size: 12px; }
.legend div { display: flex; align-items: center; gap: 6px; }
.legend svg { width: 18px; height: 18px; flex: none; }
.steps { margin: 6px 0; padding-left: 18px; font-size: 13px; }
.steps li { padding: 2px 0; }
hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

/* ---------- map symbols */
.nd { background: none; border: 0; }
.nd svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 1px 1.5px #0006); }
.nd.sel svg { filter: drop-shadow(0 0 3px #22d3ee) drop-shadow(0 0 3px #22d3ee); }
.nd.tripped svg { animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.35); } }
.fault-mk { background: none; border: 0; }
.fault-mk div { width: 30px; height: 30px; border-radius: 50%; background: var(--off); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 17px; box-shadow: 0 0 0 0 #e0313199; animation: ring 1.4s infinite; border: 2px solid #fff; }
@keyframes ring { 70% { box-shadow: 0 0 0 14px #e0313100; } 100% { box-shadow: 0 0 0 0 #e0313100; } }
.handle { background: #fff; border: 3px solid var(--accent); border-radius: 50%; box-shadow: 0 1px 4px #0005; }
.vtx { background: #fff; border: 2px solid #22d3ee; border-radius: 3px; }
.vtx.mid { opacity: .6; border-radius: 50%; border-style: dashed; }
.leaflet-tooltip.lbl { background: #ffffffd9; border: 0; box-shadow: none; padding: 0 4px; font-size: 11px; font-weight: 600; color: #1f2937; border-radius: 4px; }
.leaflet-tooltip.lbl::before { display: none; }
.leaflet-tooltip.lbl.big { font-size: 12px; color: var(--hv); }
.leaflet-container { font: inherit; }
.leaflet-control-attribution { font-size: 10px; }
@media (max-width: 899px) {
  .leaflet-bottom { bottom: calc(118px + var(--safe-b)); }
  .leaflet-control-zoom { display: none; }
}

@media (max-width: 520px) {
  .brand span { display: none; }
  #toast { bottom: auto; top: calc(var(--top) + 52px); }
}
