/* Song Plus Telemetry Hub — mobile-first, dark by default, no inline styles (strict CSP). */

:root {
  --bg: #0e1116;
  --surface: #161b22;
  --surface-2: #1d242d;
  --border: #2a323d;
  --text: #e6edf3;
  --muted: #8b97a6;
  --accent: #3fb68b;
  --blue: #58a6ff;
  --orange: #f0883e;
  --purple: #bc8cff;
  --yellow: #d8b24a;
  --warn: #d29922;
  --danger: #f85149;
  --band-offline: rgba(139, 151, 166, 0.14);
  --band-charge: rgba(63, 182, 139, 0.16);
  --grid: rgba(139, 151, 166, 0.16);
  --radius: 14px;
  --tabbar-h: 62px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] {
    --bg: #f4f6f9; --surface: #ffffff; --surface-2: #eef1f5; --border: #d7dde5;
    --text: #1b2129; --muted: #5b6776; --accent: #16895f; --blue: #1f6feb;
    --orange: #c4611a; --purple: #8250df; --yellow: #9a7400; --warn: #9a6700; --danger: #cf222e;
    --band-offline: rgba(91, 103, 118, 0.12); --band-charge: rgba(22, 137, 95, 0.14);
    --grid: rgba(91, 103, 118, 0.18);
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f4f6f9; --surface: #ffffff; --surface-2: #eef1f5; --border: #d7dde5;
  --text: #1b2129; --muted: #5b6776; --accent: #16895f; --blue: #1f6feb;
  --orange: #c4611a; --purple: #8250df; --yellow: #9a7400; --warn: #9a6700; --danger: #cf222e;
  --band-offline: rgba(91, 103, 118, 0.12); --band-charge: rgba(22, 137, 95, 0.14);
  --grid: rgba(91, 103, 118, 0.18);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--blue); text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; }
.noscript { padding: 24px; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 600; min-width: 0; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
  padding: 4px 10px; border-radius: 999px; background: var(--surface-2);
  border: 1px solid var(--border); font-size: 13px; color: var(--muted);
}
.chip:empty { display: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.dot.live { background: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.dot.charging { background: var(--accent); animation: pulse 1.6s ease-in-out infinite; }
.dot.driving { background: var(--blue); }
.dot.offline { background: var(--muted); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .dot.charging { animation: none; } }

.banner {
  position: sticky; top: 56px; z-index: 999;
  margin: 8px 16px 0; padding: 10px 14px; border-radius: 10px;
  background: color-mix(in srgb, var(--warn) 18%, var(--surface)); border: 1px solid var(--warn);
  display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 14px;
}
.banner button { flex-shrink: 0; }

main {
  max-width: 1040px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  outline: none;
}
body.no-tabs main { padding-bottom: 32px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: flex; justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}
body.no-tabs .tabbar { display: none; }
.tabbar a {
  flex: 1; max-width: 140px; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 11.5px;
}
.tabbar svg { width: 22px; height: 22px; fill: currentColor; }
.tabbar a.active { color: var(--accent); }

/* ---------- building blocks ---------- */
.stack { display: grid; gap: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
}
.card-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }
.card-head .card-title { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.grid-2 { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 760px) { .grid-2 { grid-template-columns: 1fr 1fr; } }

.tiles { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
.tile {
  background: var(--surface-2); border-radius: 12px; padding: 10px 12px; min-height: 74px;
  display: flex; flex-direction: column; justify-content: space-between;
}
.tile .label { font-size: 12.5px; color: var(--muted); }
.tile .value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile .unit { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 3px; }
.tile .note { font-size: 11.5px; color: var(--muted); }
.tile.stale .value { color: var(--muted); }
.tile.missing .value { color: var(--muted); font-weight: 400; }
.pos { color: var(--orange); }
.neg { color: var(--accent); }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap;
}
.badge.ok { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 50%, transparent); }
.badge.info { color: var(--blue); border-color: color-mix(in srgb, var(--blue) 50%, transparent); }
.badge.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 50%, transparent); }
.badge.dc { color: var(--orange); border-color: color-mix(in srgb, var(--orange) 50%, transparent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: 10px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn[disabled] { opacity: .5; cursor: default; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: none; padding: 6px 11px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--border); }

input[type="password"], input[type="text"], input[type="number"], select {
  width: 100%; min-height: 42px; padding: 8px 12px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border);
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .seg button:focus-visible, a:focus-visible {
  outline: 2px solid var(--blue); outline-offset: 2px;
}
label.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }

.empty { color: var(--muted); text-align: center; padding: 18px 8px; margin: 0; }
.error { border-color: var(--danger); }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 24px 4px; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.hint strong { color: var(--text); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Now ---------- */
.hero { display: grid; grid-template-columns: 136px 1fr; gap: 16px; align-items: center; }
.ring { width: 136px; height: 136px; }
.ring circle { fill: none; stroke-width: 10; }
.ring .track { stroke: var(--surface-2); }
.ring .level { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset .6s ease; }
.ring .level.low { stroke: var(--warn); }
.ring .level.critical { stroke: var(--danger); }
.ring text { fill: var(--text); font-weight: 700; }
.ring .soc { font-size: 30px; }
.ring .sub { font-size: 11px; fill: var(--muted); font-weight: 500; }
.hero-state { font-size: 22px; font-weight: 700; }
.hero-meta { color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.hero-figures { display: flex; gap: 18px; margin-top: 10px; flex-wrap: wrap; }
.hero-figures div { display: grid; }
.hero-figures b { font-size: 18px; font-variant-numeric: tabular-nums; }
.hero-figures span { font-size: 12px; color: var(--muted); }
@media (max-width: 380px) { .hero { grid-template-columns: 1fr; justify-items: center; text-align: center; } .hero-figures { justify-content: center; } }

.tires { display: grid; grid-template-columns: 1fr 70px 1fr; grid-template-rows: 1fr 1fr; gap: 10px 14px; align-items: center; }
.tires .car { grid-column: 2; grid-row: 1 / span 2; justify-self: center; width: 56px; height: 120px; border-radius: 22px 22px 16px 16px; border: 2px solid var(--border); background: var(--surface-2); position: relative; }
.tires .car::before { content: ""; position: absolute; left: 8px; right: 8px; top: 20px; height: 26px; border-radius: 8px 8px 4px 4px; background: var(--border); }
.tire { background: var(--surface-2); border-radius: 12px; padding: 8px 10px; text-align: center; border: 1px solid transparent; }
.tire .value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tire .label { font-size: 11.5px; color: var(--muted); }
.tire .note { font-size: 10.5px; color: var(--muted); }
.tire.stale .value { color: var(--muted); }
.tire.low { border-color: var(--warn); }
.tire.low .value { color: var(--warn); }
.tire.high { border-color: var(--orange); }
.tire.missing .value { color: var(--muted); font-weight: 400; }

.map { height: 260px; border-radius: 12px; overflow: hidden; background: var(--surface-2); }
.map.tall { height: 340px; }
.car-marker { width: 22px; height: 22px; }
.car-marker .arrow { width: 22px; height: 22px; border-radius: 50%; background: var(--blue); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); position: relative; }
.car-marker .arrow.heading::after { content: ""; position: absolute; left: 50%; top: -9px; margin-left: -5px; border: 5px solid transparent; border-bottom: 8px solid var(--blue); }
.leaflet-container { font: inherit; background: var(--surface-2); }
.leaflet-control-attribution { font-size: 10.5px; }

/* ---------- charts ---------- */
.chart { width: 100%; }
.chart-title { font-size: 13px; color: var(--muted); margin: 4px 0 2px; display: flex; justify-content: space-between; }
.uplot { font-family: inherit; }
.uplot .u-legend { font-size: 12px; color: var(--muted); text-align: left; padding: 0 4px; }
.uplot .u-legend .u-marker { border-radius: 2px; }
.u-select { background: color-mix(in srgb, var(--blue) 18%, transparent); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; justify-content: space-between; }
.legend-note { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend-note i { display: inline-block; width: 12px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.legend-note .offline { background: var(--band-offline); border: 1px solid var(--border); }
.legend-note .charge { background: var(--band-charge); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); }

/* ---------- lists ---------- */
.day-head { font-size: 13px; color: var(--muted); margin: 14px 2px 6px; display: flex; justify-content: space-between; }
.list { display: grid; gap: 8px; }
.item {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 14px; color: var(--text);
}
.item:hover { border-color: var(--muted); }
.item .title { font-weight: 600; font-variant-numeric: tabular-nums; }
.item .sub { font-size: 13px; color: var(--muted); grid-column: 1 / -1; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.item .big { font-size: 17px; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.stats { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.back { display: inline-flex; align-items: center; gap: 4px; margin-bottom: 10px; font-size: 14px; }
.flags { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); }

/* ---------- login ---------- */
.login { max-width: 420px; margin: 8vh auto 0; }
.login h1 { font-size: 22px; margin-bottom: 6px; }
.login form { display: grid; gap: 12px; margin-top: 16px; }
.login .error-text { color: var(--danger); font-size: 14px; min-height: 1.2em; }
