:root {
  --bg: #f4f7fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #0e7490;
  --primary-strong: #155e75;
  --primary-soft: #e0f2f7;
  --ok: #15803d;      --ok-soft: #dcfce7;
  --warn: #b45309;    --warn-soft: #fef3c7;
  --crit: #b91c1c;    --crit-soft: #fee2e2;
  --info: #1d4ed8;    --info-soft: #dbeafe;
  --neutral-soft: #eef2f6;
  --chart-1: #0e7490;
  --chart-2: #7c3aed;
  --chart-3: #c2410c;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 1px 8px rgb(15 23 42 / 4%);
  --radius: 12px;
  --sidebar: 248px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220; --surface: #111a2b; --surface-2: #0f1727; --border: #22304a; --text: #e2e8f0; --muted: #94a3b8;
    --primary: #22b8cf; --primary-strong: #67d8e8; --primary-soft: #0e2c38;
    --ok: #4ade80; --ok-soft: #10301d; --warn: #fbbf24; --warn-soft: #3a2a0a; --crit: #f87171; --crit-soft: #3b1414;
    --info: #93c5fd; --info-soft: #172a4d; --neutral-soft: #1a2538;
    --chart-1: #22b8cf; --chart-2: #a78bfa; --chart-3: #fb923c;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---- layout ---- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 1.1rem; }
.brand svg { width: 28px; height: 28px; color: var(--primary); }
.brand small { display: block; font-weight: 500; font-size: 0.72rem; color: var(--muted); }
.nav-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); padding: 14px 12px 4px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: var(--text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--primary-soft); color: var(--primary-strong); }
.nav svg { width: 18px; height: 18px; flex: none; opacity: 0.85; }
.nav .count { margin-left: auto; }
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 500; }
.topbar .title { font-weight: 600; font-size: 1.05rem; margin-right: auto; }
.content { padding: 24px; max-width: 1400px; width: 100%; margin: 0 auto; }
.btn.menu-btn { display: none; }
.scrim { display: none; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; z-index: 1000; width: min(var(--sidebar), 85vw); transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .shell.nav-open .sidebar { transform: none; }
  .shell.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / 35%); z-index: 999; }
  .btn.menu-btn { display: inline-flex; }
  .topbar { padding: 10px 16px; }
  .content { padding: 16px; }
}

/* ---- components ---- */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-bottom: 18px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.page-head .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head .spacer { margin-left: auto; }
.card-head a { font-size: 0.85rem; text-decoration: none; }
.kpi .label { color: var(--muted); font-size: 0.8rem; font-weight: 500; }
.kpi .value { font-size: 1.7rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .value small { font-size: 0.9rem; font-weight: 500; color: var(--muted); margin-left: 3px; }
.kpi .sub { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.banner { display: flex; align-items: center; gap: 14px; padding: 14px 18px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid transparent; }
.banner .dot { width: 12px; height: 12px; border-radius: 50%; background: currentColor; flex: none; box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 20%, transparent); }
.banner strong { display: block; }
.banner.ok { background: var(--ok-soft); color: var(--ok); }
.banner.warning { background: var(--warn-soft); color: var(--warn); }
.banner.critical { background: var(--crit-soft); color: var(--crit); }
.banner.unknown { background: var(--neutral-soft); color: var(--muted); }
.banner span { color: var(--text); opacity: .85; }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; white-space: nowrap; background: var(--neutral-soft); color: var(--muted); }
.badge.ok, .badge.online, .badge.resolved, .badge.completed, .badge.Low { background: var(--ok-soft); color: var(--ok); }
.badge.warning, .badge.delayed, .badge.in_progress, .badge.ongoing, .badge.Medium, .badge.potential { background: var(--warn-soft); color: var(--warn); }
.badge.critical, .badge.offline, .badge.High, .badge.confirmed_leak, .badge.overdue { background: var(--crit-soft); color: var(--crit); }
.badge.info, .badge.under_review, .badge.upcoming, .badge.Sensor { background: var(--info-soft); color: var(--info); }
.badge.pending, .badge.cancelled, .badge.false_alarm { background: var(--neutral-soft); color: var(--muted); }
.count { background: var(--crit); color: #fff; border-radius: 999px; font-size: 0.7rem; font-weight: 700; padding: 1px 7px; min-width: 20px; text-align: center; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-strong); }
.btn.danger { color: var(--crit); }
.btn.small { padding: 5px 10px; font-size: 0.8rem; }
.btn.icon { padding: 7px; position: relative; }
.btn.icon svg { width: 20px; height: 20px; }
.btn.icon .count { position: absolute; top: -4px; right: -6px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.form { display: grid; gap: 14px; }
.form .row { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
label.field { display: grid; gap: 5px; font-weight: 500; font-size: 0.85rem; }
label.field small { color: var(--muted); font-weight: 400; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; width: 100%; }
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 45%, transparent); border-color: var(--primary); }
.filters { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.filters > * { width: auto; flex: 1 1 160px; max-width: 280px; }
.filters input[type=search] { flex: 2 1 220px; max-width: 360px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
.list { display: grid; gap: 2px; }
.list-item { display: flex; gap: 12px; align-items: flex-start; padding: 10px 4px; border-bottom: 1px solid var(--border); text-decoration: none; color: inherit; }
.list-item:last-child { border-bottom: 0; }
.list-item .meta { color: var(--muted); font-size: 0.8rem; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item.unread { background: color-mix(in srgb, var(--primary-soft) 55%, transparent); border-radius: 8px; }
.empty { color: var(--muted); text-align: center; padding: 28px 12px; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.pager { display: flex; align-items: center; gap: 10px; justify-content: flex-end; margin-top: 12px; color: var(--muted); }

.chart-box { position: relative; height: 280px; }
.chart-box.short { height: 200px; }
.map { position: relative; height: 300px; border-radius: 10px; border: 1px solid var(--border); z-index: 0; overflow: hidden; background: var(--surface-2); }
.map.tall { height: min(70vh, 640px); min-height: 360px; }
.map > .map-canvas { position: absolute; inset: 0; }
.map-empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; text-align: center; color: var(--muted); }

/* Device map pins: one combined pipeline status color */
:root { --pin-normal: #16a34a; --pin-warning: #f59e0b; --pin-critical: #dc2626; --pin-offline: #6b7280; }
.hp-pin-wrap { background: none; border: 0; }
.hp-pin { position: relative; width: 28px; height: 38px; cursor: pointer; color: var(--pin-offline); filter: drop-shadow(0 2px 3px rgb(0 0 0 / 35%)); }
.hp-pin svg { display: block; width: 100%; height: 100%; position: relative; }
.hp-pin.normal { color: var(--pin-normal); }
.hp-pin.warning { color: var(--pin-warning); }
.hp-pin.critical { color: var(--pin-critical); }
.hp-pin.critical::before { content: ""; position: absolute; left: 50%; bottom: -6px; width: 28px; height: 12px; margin-left: -14px; border-radius: 50%;
  background: var(--pin-critical); opacity: 0.5; animation: hp-pulse 1.6s ease-out infinite; }
@keyframes hp-pulse { from { transform: scale(0.5); opacity: 0.6; } to { transform: scale(2.2); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .hp-pin.critical::before { animation: none; opacity: 0.35; } }
.map-foot { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; justify-content: space-between; margin-top: 8px; }
.map-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 0.8rem; color: var(--muted); }
.map-legend span { display: inline-flex; align-items: center; gap: 6px; }
.map-legend .dot, .dev-list .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: var(--pin-offline); flex: none; }
.dot.normal { background: var(--pin-normal); } .dot.warning { background: var(--pin-warning); } .dot.critical { background: var(--pin-critical); }
.map-note { text-align: right; }
.map-pop { font: 13px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); min-width: 220px; }
.map-pop-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.map-pop ul { margin: 4px 0 8px; padding-left: 16px; }
.map-pop .kv { font-size: 0.8rem; gap: 2px 10px; margin-bottom: 6px; }
.mapboxgl-popup-content, .leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); }
.mapboxgl-popup-content { border-radius: 10px; padding: 12px 14px; box-shadow: var(--shadow); }
.mapboxgl-popup-close-button { color: var(--muted); font-size: 18px; padding: 0 6px; }
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip, .mapboxgl-popup-anchor-bottom-left .mapboxgl-popup-tip, .mapboxgl-popup-anchor-bottom-right .mapboxgl-popup-tip { border-top-color: var(--surface); }
.mapboxgl-popup-anchor-top .mapboxgl-popup-tip, .mapboxgl-popup-anchor-top-left .mapboxgl-popup-tip, .mapboxgl-popup-anchor-top-right .mapboxgl-popup-tip { border-bottom-color: var(--surface); }
.mapboxgl-popup-anchor-left .mapboxgl-popup-tip { border-right-color: var(--surface); }
.mapboxgl-popup-anchor-right .mapboxgl-popup-tip { border-left-color: var(--surface); }
.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .map-layout { grid-template-columns: 1fr; } }
.dev-list { display: flex; flex-direction: column; gap: 8px; }
.dev-item { display: block; width: 100%; text-align: left; font: inherit; color: inherit; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; cursor: pointer; }
.dev-item:hover, .dev-item:focus-visible { border-color: var(--primary); }
.dev-item .row1 { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.dev-item .row1 strong { display: inline-flex; align-items: center; gap: 8px; }
.dev-item ul { margin: 6px 0 4px; padding-left: 16px; font-size: 0.82rem; }
.dev-item .meta { color: var(--muted); font-size: 0.78rem; }
.reasons { margin: 8px 0 0; padding-left: 18px; }
.reasons li { margin: 4px 0; }
.reasons .pts { color: var(--muted); font-size: 0.78rem; margin-left: 4px; }
.gauge { display: flex; align-items: center; gap: 16px; }
.gauge .ring { --v: 0; --c: var(--ok); width: 96px; height: 96px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: conic-gradient(var(--c) calc(var(--v) * 1%), var(--neutral-soft) 0); }
.gauge .ring span { width: 74px; height: 74px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-weight: 700; font-size: 1.4rem; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { border-left: 2px solid var(--border); padding: 0 0 14px 16px; position: relative; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: 4px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.notice { padding: 12px 14px; border-radius: 10px; background: var(--info-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--info) 30%, transparent); }
.notice.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }
.img-thumb { max-width: 100%; max-height: 320px; border-radius: 10px; border: 1px solid var(--border); }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; font-size: 0.85em; word-break: break-all; }

/* ---- modal ---- */
dialog { border: 1px solid var(--border); border-radius: var(--radius); padding: 0; width: min(640px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / 25%); }
dialog::backdrop { background: rgb(15 23 42 / 45%); }
dialog .modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
dialog .modal-head h2 { margin-right: auto; }
dialog .modal-body { padding: 20px; max-height: 70vh; overflow-y: auto; }
dialog .modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }

/* ---- toasts ---- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 2000; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--primary); padding: 12px 14px; border-radius: 10px; box-shadow: var(--shadow); }
.toast.error { border-left-color: var(--crit); }
.toast.warning { border-left-color: var(--warn); }

/* ---- auth ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth .brand { justify-content: center; padding-bottom: 8px; }
.auth p.lead { text-align: center; color: var(--muted); margin: 0 0 20px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; margin-bottom: 18px; border: 1px solid var(--border); }
.tabs button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 7px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.error-text { color: var(--crit); font-size: 0.85rem; min-height: 1.2em; }

/* ---- chat ---- */
.chat { display: flex; flex-direction: column; height: calc(100vh - 190px); min-height: 420px; padding: 0; overflow: hidden; }
.chat-log { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(760px, 88%); padding: 10px 14px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error { align-self: flex-start; background: var(--crit-soft); color: var(--crit); }
.msg.assistant.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.chat-input { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); background: var(--surface); }
.chat-input textarea { min-height: 44px; max-height: 160px; height: 44px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; }
.suggestions button { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 0.82rem; cursor: pointer; }
.suggestions button:hover { border-color: var(--primary); color: var(--primary); }
.fab { position: fixed; right: 20px; bottom: 20px; width: 54px; height: 54px; border-radius: 50%; background: var(--primary); color: #fff; display: grid; place-items: center; box-shadow: 0 6px 20px rgb(14 116 144 / 35%); z-index: 800; text-decoration: none; }
.fab svg { width: 26px; height: 26px; }
