:root {
  --accent: #0EA5A4;
  --accent-dark: #0b8988;
  --accent-tint: #e6f7f6;
  --bg: #f4f6f9;
  --sidebar: #101c25;
  --sidebar-2: #18242f;
  --sidebar-hover: #1b2b37;
  --card: #ffffff;
  --line: #e6eaef;
  --line-2: #d6dde5;
  --ink: #17222e;
  --muted: #667585;
  --muted-2: #97a5b3;
  --green: #16a34a; --green-t: #e7f6ec;
  --gray: #94a3b8;
  --amber: #d97706; --amber-t: #fdf1e3;
  --red: #dc2626; --red-t: #fdeaea;
  --purple: #7c3aed;
  --radius: 12px;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --font: 'Inter', system-ui, sans-serif;
  --shadow: 0 1px 2px rgba(16,28,37,.04), 0 4px 16px -8px rgba(16,28,37,.12);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); color: var(--ink); background: var(--bg); font-size: 14px; -webkit-font-smoothing: antialiased; }
h1, h2, h3 { margin: 0; font-weight: 600; }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
.sm { font-size: 12px; }
.pad { padding: 20px; }
button, input, select { font-family: inherit; font-size: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }

/* ── buttons ─────────────────────────────────────────────────────────────── */
.btn { border: 1px solid var(--line-2); background: #fff; color: var(--ink); padding: 8px 15px; border-radius: 9px; cursor: pointer; font-weight: 500; transition: .14s; }
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: #fff; }
.btn-danger { background: #fff; color: var(--red); border-color: #f0c4c4; }
.btn-danger:hover { background: var(--red-t); border-color: var(--red); }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { border: 0; background: transparent; color: var(--muted); font-size: 16px; cursor: pointer; width: 30px; height: 30px; border-radius: 8px; }
.icon-btn:hover { background: var(--line); color: var(--ink); }
.link-btn { border: 0; background: transparent; color: var(--accent); cursor: pointer; font-weight: 500; padding: 0; }

/* ── app shell ───────────────────────────────────────────────────────────── */
.app { display: grid; grid-template-columns: 240px 1fr; height: 100vh; }
.sidebar { background: var(--sidebar); color: #cdd8e0; display: flex; flex-direction: column; padding: 18px 14px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: #fff; padding: 6px 8px 18px; }
.nav { display: flex; flex-direction: column; gap: 3px; }
.nav-item { display: flex; align-items: center; gap: 11px; padding: 10px 12px; border-radius: 9px; color: #a9b7c4; font-weight: 500; cursor: pointer; border-left: 3px solid transparent; }
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-hover); color: #fff; border-left-color: var(--accent); }
.nav-ic { display: inline-flex; opacity: .9; }
.sidebar-foot { margin-top: auto; padding: 10px 12px; font-size: 11px; color: #5b6b78; letter-spacing: .04em; }

.content { display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0 24px; height: 60px; background: #fff; border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 18px; }
.topbar-right { display: flex; align-items: center; gap: 18px; }
.live { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--gray); }
.live-dot.on { background: var(--green); }
.live-dot.pulse { box-shadow: 0 0 0 4px var(--green-t); transition: box-shadow .1s; }

.userchip { position: relative; }
.u-btn { display: flex; align-items: center; gap: 9px; background: transparent; border: 0; cursor: pointer; padding: 5px; border-radius: 9px; }
.u-btn:hover { background: var(--bg); }
.u-avatar { display: none; }
.u-text { text-align: right; line-height: 1.15; }
.u-name { display: block; font-weight: 600; }
.u-role { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.u-role.is-admin { color: var(--accent); }
.u-menu { position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 5px; min-width: 170px; display: none; z-index: 20; }
.u-menu.open { display: block; }
.u-menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 9px 11px; border-radius: 7px; cursor: pointer; }
.u-menu button:hover { background: var(--bg); }

.page { padding: 22px 24px; overflow-y: auto; }

/* ── cards / tables ──────────────────────────────────────────────────────── */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-head { padding: 14px 18px; border-bottom: 1px solid var(--line); }
.card-head h3 { font-size: 14px; }
.page-actions { display: flex; justify-content: flex-end; margin-bottom: 16px; }

.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; padding: 11px 18px; border-bottom: 1px solid var(--line); background: #fafbfc; }
.table td { padding: 13px 18px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover { background: var(--accent-tint); }
.table tr.row-alert { background: var(--red-t); }
.table tr.row-alert:hover { background: #fbe0e0; }
.text-alert { color: var(--red); font-weight: 500; }
.cellnode { display: flex; align-items: center; gap: 10px; }
.cellnode .ic { color: var(--accent); display: inline-flex; }
.chips-cell { max-width: 340px; }

/* ── pills ───────────────────────────────────────────────────────────────── */
.pill { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 3px 9px; border-radius: 20px; white-space: nowrap; }
.pill-online { background: var(--green-t); color: var(--green); }
.pill-offline { background: #eef1f4; color: var(--gray); }
.pill-pending { background: var(--amber-t); color: var(--amber); }
.pill-muted { background: #eef1f4; color: var(--muted); }
.pill-alert { background: var(--red-t); color: var(--red); }
.pill-admin { background: #f1e9fd; color: var(--purple); }
.pill-plain { background: #eef1f4; color: var(--muted); }

/* ── dashboard ───────────────────────────────────────────────────────────── */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat-value { font-size: 28px; font-weight: 700; letter-spacing: -.02em; }
.stat-value .stat-sub { font-size: 16px; color: var(--muted-2); font-weight: 500; margin-left: 3px; }
.stat-label { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.stat-alert .stat-value { color: var(--red); }
.stat-warn .stat-value { color: var(--amber); }
.stat-ok .stat-value { color: var(--green); }
.stat-link { display: block; text-decoration: none; color: inherit; transition: .14s; }
.stat-link:hover { border-color: var(--accent); box-shadow: 0 4px 18px -8px rgba(14,165,164,.5); }
.stat-go { opacity: 0; color: var(--accent); transition: opacity .14s; }
.stat-link:hover .stat-go { opacity: 1; }

/* sortable table headers */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--ink); }
th.sortable.active { color: var(--accent); }
th .sar { font-size: 10px; margin-left: 2px; }

/* pager */
.pager { display: flex; align-items: center; justify-content: space-between; padding: 12px 18px; border-top: 1px solid var(--line); gap: 12px; }
.pager-btns { display: flex; align-items: center; gap: 10px; }
.pager .btn { padding: 6px 12px; }
.pager .btn:disabled { opacity: .4; }

/* battery cell */
.batt { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; white-space: nowrap; }
.batt-bar { width: 34px; height: 8px; border-radius: 4px; background: #e7ebf0; overflow: hidden; display: inline-block; }
.batt-bar i { display: block; height: 100%; border-radius: 4px; background: var(--green); }
.batt-ok .batt-bar i { background: var(--green); }
.batt-low .batt-bar i { background: var(--amber); }
.batt-bad .batt-bar i { background: var(--red); }
.batt-bad { color: var(--red); }

input[type="date"] { padding: 7px 10px; border: 1px solid var(--line-2); border-radius: 9px; background: #fff; color: var(--ink); }

/* node-type editor */
.hint { color: var(--muted); font-size: 12.5px; margin: 14px 4px 0; line-height: 1.5; }
.hero-note { background: var(--accent-tint); color: var(--accent-dark); font-size: 12px; padding: 9px 11px; border-radius: 9px; margin: 4px 0 6px; line-height: 1.5; }
.ntch { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 10px; position: relative; background: #fbfcfd; }
.ntch-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ntch-grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 10px; }
.ntch .hide { display: none; }
.ntch-del { position: absolute; top: 8px; right: 8px; }
@media (max-width: 640px) { .ntch-grid, .ntch-grid3 { grid-template-columns: 1fr; } }

.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.dash-grid .span-2 { grid-column: 1 / -1; }
.list-row { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; border: 0; background: transparent; padding: 12px 18px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list-row:last-child { border-bottom: 0; }
button.list-row:hover { background: var(--accent-tint); }
.list-row .ic { color: var(--accent); display: inline-flex; }
.list-row .ic.alert { color: var(--red); }
.lr-main { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.lr-main .muted { font-size: 12px; }

/* ── filters ─────────────────────────────────────────────────────────────── */
.filters-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.filters-bar .search { flex: 1; min-width: 200px; }
.filters-bar input, .filters-bar select { padding: 8px 11px; border: 1px solid var(--line-2); border-radius: 9px; background: #fff; }
.filters-bar .count { color: var(--muted); font-size: 12.5px; margin-left: auto; }
.check { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.inline-label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.inline-label select { padding: 8px 11px; border: 1px solid var(--line-2); border-radius: 9px; background: #fff; color: var(--ink); }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-tint); }

/* ── map ─────────────────────────────────────────────────────────────────── */
.map-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 16px; flex-wrap: wrap; }
.map-canvas { height: calc(100vh - 60px - 44px - 44px); min-height: 440px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.map-legend { display: flex; gap: 16px; color: var(--muted); font-size: 12.5px; }
.map-legend .dotmk { width: 11px; height: 11px; border-radius: 50%; display: inline-block; margin-right: 5px; vertical-align: -1px; }
.dotmk.teal { background: var(--accent); } .dotmk.gray { background: var(--gray); }
.dotmk.amber { background: var(--amber); } .dotmk.gw { background: #3b82f6; }
.mk { display: flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.25); border: 2px solid var(--accent); color: var(--accent); }
.mk-teal { border-color: var(--accent); color: var(--accent); }
.mk-gray { border-color: var(--gray); color: var(--gray); }
.mk-amber { border-color: var(--amber); color: var(--amber); animation: mkpulse 1.8s infinite; }
.mk-gw { border-color: #3b82f6; color: #3b82f6; }
.mk-gwoff { border-color: var(--gray); color: var(--gray); }
@keyframes mkpulse { 0% { box-shadow: 0 0 0 0 rgba(217,119,6,.4); } 70% { box-shadow: 0 0 0 9px rgba(217,119,6,0); } 100% { box-shadow: 0 0 0 0 rgba(217,119,6,0); } }
.leaflet-popup-content-wrapper { border-radius: 12px; box-shadow: 0 12px 32px -12px rgba(16,28,37,.45); }
.af-pop .leaflet-popup-content { margin: 13px 15px; width: auto !important; }
.mappop { font-family: var(--font); color: var(--ink); }
.pop-head { display: flex; align-items: center; gap: 9px; margin-bottom: 9px; }
.pop-ic { display: inline-flex; width: 32px; height: 32px; align-items: center; justify-content: center; border-radius: 9px; background: var(--accent-tint); color: var(--accent); flex: none; }
.pop-title { font-weight: 600; font-size: 14px; line-height: 1.2; }
.pop-sub { font-size: 11px; color: var(--muted); }
.pop-pills { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 9px; }
.pop-reading { margin-bottom: 6px; }
.pop-section { font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 10px 0 6px; }
.pop-chart { min-height: 30px; margin-bottom: 8px; }
.pop-kv { display: grid; gap: 5px; margin-bottom: 10px; }
.pop-kv > div { display: grid; grid-template-columns: 82px 1fr; gap: 8px; font-size: 12.5px; }
.pop-kv span { color: var(--muted); }
.pop-link { display: inline-block; color: var(--accent); font-weight: 600; font-size: 13px; }
.pop-link:hover { text-decoration: underline; }

/* trend chart (node details + map popup) */
.trend { display: grid; gap: 13px; }
.pad-sm { padding: 8px 0; }
.trow-h { display: flex; justify-content: space-between; align-items: baseline; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.trow-h b { color: var(--ink); font-weight: 600; font-family: var(--mono); font-size: 12.5px; }
.spark { display: block; background: #fbfcfd; border: 1px solid var(--line); border-radius: 8px; }
.trow-f { display: flex; justify-content: space-between; margin-top: 2px; }
.bstrip { display: flex; gap: 2px; height: 18px; }
.bcell { flex: 1 1 0; border-radius: 2px; background: #e7ebf0; min-width: 2px; }
.bcell.b-on { background: var(--accent); }
.bcell.b-alert { background: var(--red); }

/* ── modal ───────────────────────────────────────────────────────────────── */
#modal-root { position: fixed; inset: 0; pointer-events: none; z-index: 10000; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(16,28,37,.42); display: flex; align-items: center; justify-content: center; padding: 20px; opacity: 0; transition: opacity .18s; pointer-events: auto; }
.modal-backdrop.show { opacity: 1; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 560px; max-height: 88vh; overflow: auto; box-shadow: 0 24px 60px -20px rgba(16,28,37,.4); transform: translateY(8px); transition: transform .18s; }
.modal-backdrop.show .modal { transform: none; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; padding: 18px 20px 12px; }
.modal-head h3 { font-size: 17px; }
.modal-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; font-family: var(--mono); }
.modal-body { padding: 4px 20px 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: #fff; }

.modal-hero { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.hero-icon { display: inline-flex; width: 46px; height: 46px; align-items: center; justify-content: center; border-radius: 12px; background: var(--accent-tint); color: var(--accent); }
.hero-meta { display: flex; flex-wrap: wrap; gap: 6px; }
.section-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 16px 0 8px; }
.row-between { display: flex; align-items: center; justify-content: space-between; }
.edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 8px 0; }
.field { display: block; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input, .field select { width: 100%; padding: 9px 11px; border: 1px solid var(--line-2); border-radius: 9px; background: #fff; }
.field input:disabled { background: var(--bg); color: var(--muted); }

/* metric + badge reading views */
.metric-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.metric { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; background: #fbfcfd; }
.metric-alert { border-color: #f0c4c4; background: var(--red-t); }
.metric-v { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.metric-v span { font-size: 13px; color: var(--muted-2); font-weight: 500; margin-left: 2px; }
.metric-alert .metric-v { color: var(--red); }
.metric-l { color: var(--muted); font-size: 12px; margin-top: 2px; }
.badge-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.badge { border: 1px solid var(--line); border-radius: 10px; padding: 14px; text-align: center; }
.badge-state { font-size: 17px; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; }
.badge-label { color: var(--muted); font-size: 12px; margin-top: 3px; }
.badge-off { background: #fbfcfd; } .badge-off .badge-state { color: var(--muted); }
.badge-on { background: var(--green-t); border-color: #bfe6cb; } .badge-on .badge-state { color: var(--green); }
.badge-alert { background: var(--red-t); border-color: #f0c4c4; } .badge-alert .badge-state { color: var(--red); }

/* chips */
.chip { display: inline-block; font-family: var(--mono); font-size: 11.5px; padding: 2px 8px; border-radius: 20px; background: #eef1f4; color: var(--muted); margin: 2px 4px 2px 0; }
.chip b { color: var(--ink); font-weight: 600; }
.chip-on { background: var(--green-t); color: var(--green); }
.chip-alert { background: var(--red-t); color: var(--red); }
.chip-alert b { color: var(--red); }

/* mini table (recent readings in node modal) */
.mini-table { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.mini-row { display: grid; grid-template-columns: 1fr 1.2fr auto; gap: 10px; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 12.5px; }
.mini-row:last-child { border-bottom: 0; }
.mini-row.is-alert { background: var(--red-t); }

/* key-value block (reading modal) */
.kv { display: grid; gap: 8px; margin-top: 14px; }
.kv > div { display: grid; grid-template-columns: 90px 1fr; gap: 10px; font-size: 13px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.kv span { color: var(--muted); }

/* ── toast ───────────────────────────────────────────────────────────────── */
#toast-root { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 10001; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 9px; font-size: 13px; opacity: 0; transform: translateY(8px); transition: .2s; box-shadow: var(--shadow); }
.toast.toast-bad { background: var(--red); }
.toast.show { opacity: 1; transform: none; }

.empty-state { padding: 60px 20px; text-align: center; color: var(--muted); }

/* ── login ───────────────────────────────────────────────────────────────── */
.login-body { display: grid; place-items: center; height: 100vh; overflow: hidden; background: linear-gradient(160deg, #eef2f6, #f7f9fb); }
#bg { position: fixed; inset: 0; z-index: 0; pointer-events: none; }
.login-card { position: relative; z-index: 1; width: min(92vw, 360px); background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: 0 20px 50px -24px rgba(16,28,37,.4); }
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.login-title { font-weight: 700; font-size: 19px; }
.login-sub { color: var(--muted); font-size: 12px; letter-spacing: .1em; text-transform: uppercase; }
.login-card .field { margin-bottom: 14px; }
.login-card .btn { margin-top: 6px; }
.login-err { color: var(--red); font-size: 13px; margin: 12px 0 0; }

/* ── responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; align-items: center; padding: 8px 12px; overflow-x: auto; }
  .brand span { display: none; }
  .nav { flex-direction: row; gap: 2px; }
  .nav-item { border-left: 0; border-bottom: 3px solid transparent; padding: 8px 10px; white-space: nowrap; }
  .nav-item.active { border-left: 0; border-bottom-color: var(--accent); }
  .nav-item span:not(.nav-ic) { display: none; }
  .sidebar-foot { display: none; }
  .dash-grid { grid-template-columns: 1fr; }
  .dash-grid .span-2 { grid-column: auto; }
  .edit-grid { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
