/* LSAS Field: mobile first (360-420 px), high-contrast light theme for
   sunlight, dark theme; no horizontal scrolling. */

:root {
  color-scheme: light;
  --bg: #ffffff;
  --surface: #ffffff;
  --surface2: #eef0f2;
  --ink: #0b0b0b;
  --ink2: #2e2e2b;
  --muted: #52514e;
  --line: #b9bab5;
  --grid: #dcdcd7;
  --primary: #0b3d91;
  --primary-ink: #ffffff;
  --focus: #ff8c00;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --good-ink: #075e07;
  --bad-ink: #a01f1f;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --s-other: #8a8a85;
  --radius: 10px;
  --tabbar-h: 64px;
  --header-h: 56px;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111111; --surface: #1a1a19; --surface2: #262624; --ink: #f0efec; --ink2: #d6d5cc; --muted: #b0afa5;
    --line: #45453f; --grid: #34342f; --primary: #3987e5; --primary-ink: #0b0b0b; --good-ink: #5fd35f; --bad-ink: #ff8a8a;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111111; --surface: #1a1a19; --surface2: #262624; --ink: #f0efec; --ink2: #d6d5cc; --muted: #b0afa5;
  --line: #45453f; --grid: #34342f; --primary: #3987e5; --primary-ink: #0b0b0b; --good-ink: #5fd35f; --bad-ink: #ff8a8a;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4; overflow-x: hidden; -webkit-text-size-adjust: 100%;
}
.boot { padding: 24px 16px; }
h1 { font-size: 1.35rem; margin: 4px 0 10px; }
h2 { font-size: 1.05rem; margin: 0 0 10px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-variant-numeric: tabular-nums; }
.small { font-size: .85rem; }
.muted { color: var(--muted); }
.good { color: var(--good-ink); font-weight: 600; }
.bad, .error { color: var(--bad-ink); font-weight: 600; }
.empty { color: var(--muted); text-align: center; padding: 16px 0; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------- shell ---- */
.topbar {
  position: sticky; top: 0; z-index: 1100; display: flex; gap: 8px; align-items: center;
  min-height: var(--header-h); padding: 6px 12px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 2px solid var(--line);
}
.conn { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); }
.conn-dot { flex: none; width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--ink); background: var(--critical); }
.conn-connected .conn-dot { background: var(--good); }
.conn-connecting .conn-dot { background: var(--warning); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .35; } }
.conn-text { display: flex; flex-direction: column; min-width: 0; }
.conn-text strong { font-size: 1.05rem; letter-spacing: .02em; }
.conn-text small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink2); }
.syncpill {
  flex: none; padding: 6px 10px; border-radius: 999px; font-size: .85rem; font-weight: 600; text-decoration: none;
  border: 2px solid var(--line); color: var(--ink); background: var(--surface2); white-space: nowrap;
}
.syncpill.good { border-color: var(--good); }
.syncpill.bad { border-color: var(--critical); }
.syncpill.warn { border-color: var(--warning); }

#main { padding: 12px 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); max-width: 720px; margin: 0 auto; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1100; display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 2px solid var(--line);
}
.tab {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--ink2); text-decoration: none; font-size: .78rem; font-weight: 600;
}
.tab .ico { font-size: 1.35rem; line-height: 1; }
.tab.active { color: var(--primary); box-shadow: inset 0 4px 0 var(--primary); }

/* ---------------------------------------------------------- cards ---- */
.card { background: var(--surface); border: 2px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 0 0 12px; }
.card.error { border-color: var(--critical); }
.kv { display: grid; grid-template-columns: minmax(0, 38%) minmax(0, 1fr); gap: 4px 10px; margin: 0; }
.kv dt { color: var(--muted); font-size: .9rem; }
.kv dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row > input { flex: 1 1 140px; min-width: 0; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }
.list { display: flex; flex-direction: column; gap: 8px; }

/* -------------------------------------------------------- controls ---- */
button, input, select { font: inherit; color: inherit; }
.btn {
  min-height: 48px; padding: 10px 16px; border-radius: var(--radius); border: 2px solid var(--ink2);
  background: var(--surface2); color: var(--ink); font-weight: 700; cursor: pointer; touch-action: manipulation;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { border-color: var(--critical); color: var(--bad-ink); background: transparent; }
.btn.small { min-height: 40px; padding: 6px 12px; font-size: .9rem; }
.btn.small.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.wide { width: 100%; margin-top: 8px; }
.btn.huge { width: 100%; min-height: 64px; font-size: 1.2rem; margin: 6px 0; }
.linkbtn { background: none; border: none; color: var(--primary); text-decoration: underline; font-weight: 600; min-height: 44px; padding: 0 4px; cursor: pointer; }
input[type=text], input[type=password], input[type=url], input[type=number], select {
  min-height: 48px; padding: 8px 10px; border: 2px solid var(--ink2); border-radius: var(--radius);
  background: var(--bg); color: var(--ink); max-width: 100%;
}
label { display: block; margin: 0 0 10px; font-weight: 600; font-size: .92rem; }
label > input:not([type=checkbox]), label > select { display: block; width: 100%; margin-top: 4px; font-weight: 400; }
.card > input, .card > select { width: 100%; margin: 6px 0; }
label.check { display: flex; align-items: center; gap: 10px; min-height: 48px; font-weight: 600; }
input[type=checkbox] { width: 26px; height: 26px; accent-color: var(--primary); flex: none; }
.seg { display: flex; gap: 8px; margin: 4px 0 8px; }
.seg-btn {
  flex: 1; min-height: 56px; border-radius: var(--radius); border: 2px solid var(--ink2); background: var(--surface2);
  font-weight: 800; font-size: 1.05rem; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.seg-btn small { font-weight: 500; font-size: .78rem; color: var(--muted); }
.seg-btn.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.seg-btn.on small { color: inherit; }
.listbtn {
  text-align: left; min-height: 52px; padding: 8px 12px; border-radius: var(--radius); border: 2px solid var(--line);
  background: var(--surface2); cursor: pointer; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: baseline;
}
.listbtn small { width: 100%; color: var(--muted); }
.listbtn.on { border-color: var(--primary); }
details.settings summary { font-weight: 800; font-size: 1.05rem; min-height: 40px; cursor: pointer; }
details.settings[open] summary { margin-bottom: 10px; }

/* ---------------------------------------------------------- status ---- */
.big-state { font-size: 1.35rem; font-weight: 800; padding: 10px 12px; border-radius: var(--radius); margin-bottom: 10px; border: 3px solid; }
.big-state.small { font-size: 1rem; margin: 0 0 8px; }
.big-state.good { border-color: var(--good); color: var(--good-ink); }
.big-state.bad { border-color: var(--critical); color: var(--bad-ink); }
.big-state.warn { border-color: var(--warning); }
.devname { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; margin-bottom: 8px; }
.devname strong { font-size: 1.5rem; }
.devname .mono { font-size: 1.1rem; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .8rem; font-weight: 700; white-space: nowrap; background: var(--surface2); color: var(--ink); border: 1px solid var(--line); }
.st-good { background: var(--good); color: #0b0b0b; border-color: var(--good); }
.st-warning { background: var(--warning); color: #0b0b0b; border-color: var(--warning); }
.st-critical { background: var(--critical); color: #ffffff; border-color: var(--critical); }
.st-pending { background: var(--surface2); color: var(--ink); border: 1px dashed var(--ink2); }
.st-neutral { background: var(--surface2); color: var(--ink); }

/* ----------------------------------------------------------- tiles ---- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.tile { border: 2px solid var(--line); border-radius: var(--radius); padding: 8px; display: flex; flex-direction: column; min-width: 0; }
.tile-l { font-size: .78rem; color: var(--muted); font-weight: 600; }
.tile-v { font-size: 1.3rem; font-weight: 800; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.tile-s { font-size: .75rem; color: var(--muted); overflow-wrap: anywhere; }
.msg-summary { margin: 0 0 8px; }

/* ----------------------------------------------------------- nodes ---- */
.node-card { border: 2px solid var(--line); border-radius: var(--radius); padding: 10px 12px; background: var(--surface); }
.node-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.node-head strong { font-size: 1.15rem; }
.node-head .badge { margin-left: auto; }
.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 4px; flex: none; }
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 8px; }
.stat { display: flex; flex-direction: column; min-width: 0; }
.stat-l { font-size: .75rem; color: var(--muted); font-weight: 600; }
.stat-v { font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-s { font-size: .72rem; color: var(--muted); overflow-wrap: anywhere; }
.nb-row, .heard-row, .loss-row { border-bottom: 1px solid var(--grid); padding: 6px 0; overflow-wrap: anywhere; }
.heard-row.dim { opacity: .7; }

/* -------------------------------------------------------- messages ---- */
.msg { border: 2px solid var(--line); border-radius: var(--radius); padding: 8px 10px; background: var(--surface); }
.msg.in { border-style: dashed; }
.msg-head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.msg-head .badge { margin-left: auto; }
.msg-body { font-size: 1.05rem; margin: 4px 0; overflow-wrap: anywhere; }
.receivers { list-style: none; padding: 0; margin: 4px 0 0; }
.receivers li { display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; padding: 2px 0; border-top: 1px solid var(--grid); }
.receivers li span:first-child { font-weight: 700; }

/* ------------------------------------------------------------- log ---- */
.loglist { display: flex; flex-direction: column; }
.logrow { display: grid; grid-template-columns: auto auto minmax(0, 1fr); gap: 6px; padding: 5px 0; border-bottom: 1px solid var(--grid); font-size: .88rem; }
.logrow .kind { font-weight: 700; color: var(--muted); }
.logrow .sum { overflow-wrap: anywhere; }
.logrow.k-note .sum { font-weight: 700; }

/* ---------------------------------------------------------- charts ---- */
.chart-wrap { width: 100%; }
svg.chart { width: 100%; height: auto; display: block; overflow: visible; }
svg.chart .grid { stroke: var(--grid); stroke-width: 1; }
svg.chart .axis { stroke: var(--ink2); stroke-width: 1; }
svg.chart .tick { fill: var(--muted); font-size: 10px; }
svg.chart .lbl { fill: var(--muted); font-size: 10px; font-weight: 700; }
svg.chart .val { fill: var(--ink); font-size: 10px; font-weight: 700; }
svg.chart .empty { fill: var(--muted); font-size: 12px; }
svg.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
svg.chart .dot.ring { stroke: var(--surface); stroke-width: 2; }
svg.chart .hit { fill: transparent; stroke: none; cursor: pointer; }
svg.chart .bar { cursor: pointer; }
svg.chart .lane { fill: var(--surface2); }
svg.chart .loss { fill: var(--critical); cursor: pointer; }
.tip { min-height: 1.4em; margin: 6px 0 0; color: var(--ink2); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.leg { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.hbars { display: flex; flex-direction: column; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(0, 28%) minmax(0, 1fr) auto; gap: 8px; align-items: center; }
.hbar-l { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { height: 16px; background: var(--surface2); border-radius: 4px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; border-radius: 0 4px 4px 0; }
.hbar-v { font-weight: 800; font-variant-numeric: tabular-nums; }
.hbar-v small { font-weight: 500; color: var(--muted); }

/* ------------------------------------------------------------- map ---- */
#main:has(.map-screen:not([hidden])) { padding: 0; max-width: none; }
.map-screen { position: relative; height: calc(100dvh - var(--header-h) - var(--tabbar-h) - env(safe-area-inset-bottom)); min-height: 300px; }
.mapbox { position: absolute; inset: 0; }
.map-tools { position: absolute; top: 10px; right: 10px; z-index: 1000; display: flex; gap: 6px; }
.map-legend {
  position: absolute; left: 10px; bottom: 24px; z-index: 1000; display: flex; gap: 10px; padding: 6px 10px;
  background: var(--surface); border: 2px solid var(--line); border-radius: var(--radius); font-size: .8rem; font-weight: 600;
}
.map-legend span { display: inline-flex; align-items: center; gap: 4px; }
.map-legend .ring { width: 14px; height: 14px; border-radius: 50%; border: 4px solid; display: inline-block; background: transparent !important; }
.ring.st-good { border-color: var(--good); }
.ring.st-warning { border-color: var(--warning); }
.ring.st-critical { border-color: var(--critical); }
.map-msg { position: absolute; top: 60px; left: 50%; transform: translateX(-50%); z-index: 1000; background: var(--surface); padding: 6px 12px; border-radius: var(--radius); border: 2px solid var(--line); font-weight: 600; }
.leaflet-tooltip.map-label { font-weight: 700; font-size: 12px; padding: 1px 5px; background: #ffffff; color: #0b0b0b; border: 1px solid #0b0b0b; box-shadow: none; }
.gcs-icon span {
  display: flex; align-items: center; justify-content: center; width: 44px; height: 26px; border-radius: 6px;
  background: #0b3d91; color: #ffffff; font-weight: 800; font-size: 12px; border: 2px solid #ffffff; box-shadow: 0 0 0 2px #0b3d91;
}
.leaflet-container { font: inherit; }

/* ----------------------------------------------------- toast/dialog ---- */
#toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom)); z-index: 2000;
  padding: 12px 14px; border-radius: var(--radius); background: var(--ink); color: var(--bg); font-weight: 700;
  opacity: 0; transform: translateY(10px); transition: opacity .2s, transform .2s; pointer-events: none; max-width: 600px; margin: 0 auto;
}
#toast.show { opacity: 1; transform: none; }
#toast.good { border-left: 8px solid var(--good); }
#toast.bad { border-left: 8px solid var(--critical); }
#toast.warn { border-left: 8px solid var(--warning); }
dialog.dlg { border: 3px solid var(--ink); border-radius: var(--radius); padding: 16px; max-width: min(92vw, 460px); background: var(--surface); color: var(--ink); }
dialog.dlg::backdrop { background: rgba(0, 0, 0, .55); }
dialog.dlg .btn { min-width: 110px; }

@media (min-width: 560px) {
  .stats-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
