:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --danger: #dc2626;
  --ring-bg: #e2e8f0;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .04);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --card: #131c2e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #1e293b;
    --accent: #60a5fa;
    --accent-soft: #1e3a5f;
    --ok: #4ade80;
    --ok-soft: #14532d;
    --warn: #fbbf24;
    --warn-soft: #422006;
    --danger: #f87171;
    --ring-bg: #1e293b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pad { padding: 0 16px; }
.error { color: var(--danger); margin: 0; }
.ok { color: var(--ok); }
code, kbd { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; background: var(--accent-soft); padding: 1px 5px; border-radius: 5px; word-break: break-all; }
.code { background: var(--accent-soft); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }

/* ---------- auth pages ---------- */
body.center-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 28px 24px; width: 100%; max-width: 380px; text-align: center; }
.auth-card.wide { max-width: 560px; text-align: left; }
.auth-card h1 { margin: 8px 0 16px; font-size: 22px; }
.auth-logo { border-radius: 18px; }
.steps { padding-left: 20px; }
.steps li { margin-bottom: 12px; }
.stack { display: flex; flex-direction: column; gap: 10px; text-align: left; }
label { font-size: 14px; color: var(--muted); }
input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  border-radius: 12px; padding: 12px 16px; width: 100%;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; }

/* ---------- app shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar > div { flex: 1; min-width: 0; }
.topbar .title { font-weight: 700; font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .subtitle { color: var(--muted); font-size: 13px; }
.topbar select { width: auto; padding: 8px 10px; }
.icon-btn { border: 1px solid var(--border); background: var(--card); color: var(--text); border-radius: 50%; width: 42px; height: 42px; display: grid; place-items: center; cursor: pointer; flex: none; }
.icon-btn.spinning svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.banner { margin: 0 16px 10px; padding: 10px 14px; border-radius: 12px; background: var(--warn-soft); color: var(--warn); font-size: 14px; }
.banner a { color: inherit; font-weight: 600; }

#views { max-width: 720px; margin: 0 auto; }
.view { display: none; padding: 4px 16px 16px; }
.view.active { display: block; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.label { color: var(--muted); font-size: 13px; font-weight: 500; margin-bottom: 4px; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.grid .card { margin: 0; }
.tile .value { font-size: 20px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } #view-stats .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

/* hero */
.hero { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 24px 16px; }
.ring-wrap { position: relative; width: 200px; height: 200px; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; stroke-linecap: round; }
.ring-bg { stroke: var(--ring-bg); }
.ring-fg { stroke: var(--accent); stroke-dasharray: 326.73; stroke-dashoffset: 326.73; transition: stroke-dashoffset .8s ease, stroke .3s; }
.ring-fg.charging { stroke: var(--ok); }
.ring-fg.low { stroke: var(--warn); }
.ring-label { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.soc { font-size: 52px; font-weight: 750; line-height: 1; letter-spacing: -1px; }
.soc small { font-size: 22px; font-weight: 600; margin-left: 2px; }
.range { color: var(--muted); font-size: 17px; margin-top: 6px; }
.pill { padding: 6px 14px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 14px; }
.pill.charging { background: var(--ok-soft); color: var(--ok); }

/* maps */
.mini-map { height: 180px; border-radius: 12px; overflow: hidden; margin: 6px 0; }
.big-map { height: calc(100vh - 200px - env(safe-area-inset-bottom) - env(safe-area-inset-top)); min-height: 320px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
.leaflet-container { background: var(--ring-bg); font: inherit; }
.car-marker { width: 22px; height: 22px; border-radius: 50%; background: var(--accent); border: 3px solid #fff; box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 30%, transparent); }

.segmented { display: flex; background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.segmented button { flex: 1; border: 0; background: none; color: var(--muted); font: inherit; font-size: 14px; padding: 8px 4px; border-radius: 9px; cursor: pointer; }
.segmented button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.chart-box { position: relative; height: 220px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.list li:first-child { border-top: 0; }
.list .main { font-weight: 600; }
.list .sub { color: var(--muted); font-size: 13px; }
.list .right { text-align: right; white-space: nowrap; }
.list .empty { color: var(--muted); justify-content: center; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; word-break: break-word; }

/* tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border: 0; background: none; color: var(--muted); font: inherit; font-size: 11px; cursor: pointer; }
.tabbar svg { width: 26px; height: 26px; }
.tabbar button.active { color: var(--accent); }
@media (prefers-color-scheme: dark) {
  .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.9) contrast(.9); }
}

/* ---------- sub pages ---------- */
.page { max-width: 640px; margin: 0 auto; padding: 4px 16px 24px; }
.page h2 { font-size: 18px; margin: 0; }
.page h3 { font-size: 15px; margin: 8px 0 0; }
.topbar a.icon-btn { text-decoration: none; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); vertical-align: middle; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
details > summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
details[open] > summary { margin-bottom: 8px; }
details.inline > summary { font-weight: 500; color: var(--accent); }
.flash { padding: 12px 14px; border-radius: 12px; margin-bottom: 12px; background: var(--ok-soft); color: var(--ok); }
.flash.error { background: var(--warn-soft); color: var(--danger); }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); }
.check input { width: auto; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.link.danger { color: var(--danger); }
.stack-sm { display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }

/* ---------- remote control ---------- */
.controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 8px; }
.ctrl { text-align: left; display: flex; align-items: center; gap: 8px; justify-content: flex-start; font-weight: 600; font-size: 15px; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); color: var(--text); cursor: pointer; font-family: inherit; }
.ctrl .ico { font-size: 18px; width: 22px; text-align: center; }
.ctrl.busy { opacity: .6; pointer-events: none; }
.ctrl.busy .ico { animation: spin 1s linear infinite; display: inline-block; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); z-index: 2000; max-width: calc(100% - 32px); padding: 12px 16px; border-radius: 12px; background: var(--text); color: var(--bg); font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- confirm dialog ---------- */
.dialog { border: 1px solid var(--border); border-radius: 18px; padding: 20px; width: min(420px, calc(100vw - 32px)); background: var(--card); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.dialog::backdrop { background: rgba(2, 6, 23, .55); backdrop-filter: blur(2px); }
.dialog h2 { margin: 0 0 8px; font-size: 19px; }
.dialog p { margin: 0 0 10px; }
.dialog-actions { display: flex; gap: 10px; margin-top: 16px; }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ---------- A/C running indicator ---------- */
.pill-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.pill.precond { display: inline-flex; align-items: center; gap: 6px; background: color-mix(in srgb, #06b6d4 18%, transparent); color: #0891b2; }
@media (prefers-color-scheme: dark) { .pill.precond { color: #22d3ee; } }
.fan { width: 18px; height: 18px; flex: none; animation: fan-spin 1.4s linear infinite; }
.value.precond-on { display: flex; align-items: center; gap: 8px; color: #0891b2; }
@media (prefers-color-scheme: dark) { .value.precond-on { color: #22d3ee; } }
.value.precond-on .fan { width: 22px; height: 22px; }
@keyframes fan-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .fan { animation: none; } }
details.sub-details { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 6px; }
details.sub-details > summary { font-weight: 500; color: var(--muted); }

/* ---------- vehicle image & names ---------- */
.car-thumb { flex: none; width: 68px; height: 42px; padding: 0; border: 0; background: none; cursor: zoom-in; border-radius: 8px; }
.car-thumb img { width: 100%; height: 100%; object-fit: contain; transition: transform .2s; }
.car-thumb:hover img, .car-thumb:focus-visible img { transform: scale(1.08); }

.gallery { border: 0; padding: 0; width: min(760px, 100vw); max-width: 100vw; max-height: 100dvh; background: var(--card); color: var(--text); border-radius: 20px; box-shadow: 0 20px 60px rgba(0,0,0,.4); overflow: hidden; }
@media (max-width: 640px) { .gallery { width: 100vw; border-radius: 20px 20px 0 0; margin: auto 0 0; } }
.gallery::backdrop { background: rgba(2, 6, 23, .7); backdrop-filter: blur(3px); }
.gallery[open] { animation: gallery-in .25s ease-out; }
@keyframes gallery-in { from { opacity: 0; transform: translateY(24px) scale(.98); } to { opacity: 1; transform: none; } }
.gallery-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 0; }
.gallery-title { font-weight: 700; font-size: 18px; }
.gallery-track { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.gallery-track::-webkit-scrollbar { display: none; }
.gallery-track figure { flex: 0 0 100%; margin: 0; scroll-snap-align: center; display: grid; place-items: center; padding: 8px 12px; }
.gallery-track img { width: 100%; height: auto; aspect-ratio: 5 / 3; object-fit: contain; }
.gallery-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.gallery-dots { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.gallery-dots button { width: 8px; height: 8px; padding: 0; border-radius: 50%; border: 0; background: var(--border); cursor: pointer; }
.gallery-dots button.active { background: var(--accent); width: 20px; border-radius: 4px; }
.vehicle-row { display: flex; flex-direction: column; gap: 6px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; }
.inline-form .btn { width: auto; flex: none; }

/* ---------- trip details ---------- */
.list li.clickable { cursor: pointer; border-radius: 10px; margin: 0 -8px; padding-left: 8px; padding-right: 8px; }
.list li.clickable:hover { background: var(--bg); }
.list li.clickable .chev { color: var(--muted); align-self: center; margin-left: -4px; }
.trip-dialog { max-height: 92dvh; overflow-y: auto; }
.trip-map { height: 260px; margin: 12px 16px 0; border-radius: 14px; overflow: hidden; border: 1px solid var(--border); }
.trip-body { padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); }
.trip-route { list-style: none; margin: 0 0 12px; padding: 0; position: relative; }
.trip-route li { display: flex; gap: 12px; align-items: flex-start; padding: 6px 0; }
.trip-route li:first-child::after { content: ""; position: absolute; left: 5px; top: 22px; height: calc(50% - 14px); border-left: 2px dashed var(--border); }
.trip-route .main { font-weight: 600; }
.trip-route .sub { color: var(--muted); font-size: 13px; }
.trip-route .dot { flex: none; width: 12px; height: 12px; border-radius: 50%; margin-top: 4px; border: 2px solid var(--card); box-shadow: 0 0 0 2px currentColor; }
.dot.start { background: var(--ok); color: var(--ok); }
.dot.end { background: var(--danger); color: var(--danger); }
.trip-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.trip-grid > div { background: var(--bg); border-radius: 12px; padding: 10px 12px; }
.trip-grid .label { margin: 0; }
.trip-grid .v { font-weight: 650; font-size: 17px; }
.map-pin { width: 16px; height: 16px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.map-pin.start { background: #16a34a; }
.map-pin.end { background: #dc2626; }
