:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface-1: #ffffff;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --grid: #e8e7e3;
  --axis: #c9c8c2;
  --accent: #2a78d6;
  --accent-soft: #e6f0fc;
  --series-1: #2a78d6;
  --series-1-soft: #86b6ef;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 1px 8px rgba(0,0,0,.04);
  --radius: 16px;
  --nav-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121212;
    --surface-1: #1a1a19;
    --surface-2: #242423;
    --border: #2e2e2c;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #96958c;
    --grid: #2b2b29;
    --axis: #4a4a46;
    --accent: #6da7ec;
    --accent-soft: #1c2a3b;
    --series-1: #3987e5;
    --series-1-soft: #256abf;
    --good-text: #3fbf3f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121212; --surface-1: #1a1a19; --surface-2: #242423; --border: #2e2e2c;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #96958c;
  --grid: #2b2b29; --axis: #4a4a46; --accent: #6da7ec; --accent-soft: #1c2a3b;
  --series-1: #3987e5; --series-1-soft: #256abf; --good-text: #3fbf3f; --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text-primary);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
button, input { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--text-muted); }
.num { font-variant-numeric: tabular-nums; }

/* ---- layout ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
}
.topbar h1 { font-size: 20px; }
.sync {
  border: 0; background: var(--surface-2); color: var(--text-secondary);
  border-radius: 999px; padding: 6px 12px; font-size: 13px; display: inline-flex; align-items: center; gap: 6px;
}
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.sync.offline .dot { background: var(--text-muted); }
.sync.loading .dot { background: var(--accent); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.banner {
  margin: 0 16px 8px; padding: 10px 14px; border-radius: 12px;
  background: var(--surface-2); color: var(--text-secondary); font-size: 14px;
}
main { padding: 0 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); max-width: 760px; margin: 0 auto; outline: none; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface-1); border-top: 1px solid var(--border);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-muted); text-decoration: none; font-size: 12px; font-weight: 500;
}
.tabs svg { width: 24px; height: 24px; fill: currentColor; padding: 2px 16px; box-sizing: content-box; border-radius: 999px; }
.tabs a.active { color: var(--text-primary); }
.tabs a.active svg { background: var(--accent-soft); color: var(--accent); }

/* ---- cards ---- */
.card {
  background: var(--surface-1); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 12px; border: 1px solid var(--border);
}
.card h3 { font-size: 15px; margin-bottom: 2px; }
.card .sub { color: var(--text-muted); font-size: 13px; margin-bottom: 12px; }
.section-title { font-size: 13px; font-weight: 600; color: var(--text-secondary); margin: 18px 4px 8px; text-transform: uppercase; letter-spacing: .04em; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

/* hero */
.hero { display: grid; grid-template-columns: 112px 1fr; gap: 16px; align-items: center; }
.ring { position: relative; width: 112px; height: 112px; }
.ring svg { width: 112px; height: 112px; transform: rotate(-90deg); }
.ring .track { stroke: var(--surface-2); }
.ring .val { stroke: var(--series-1); stroke-linecap: round; transition: stroke-dashoffset .6s; }
.ring .lbl { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .lbl b { font-size: 28px; line-height: 1; }
.ring .lbl small { font-size: 12px; color: var(--text-muted); }
.hero .big { font-size: 34px; font-weight: 650; line-height: 1.1; }
.hero .big small { font-size: 16px; font-weight: 500; color: var(--text-secondary); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-secondary); font-size: 13px;
}
.chip i { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); display: inline-block; }
.chip.good i { background: var(--good); }
.chip.warn i { background: var(--warning); }
.chip.crit i { background: var(--critical); }
.chip.info i { background: var(--accent); }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 12px; }
.tile .k { font-size: 12.5px; color: var(--text-secondary); }
.tile .v { font-size: 24px; font-weight: 650; line-height: 1.2; }
.tile .v small { font-size: 13px; font-weight: 500; color: var(--text-secondary); }
.tile .d { font-size: 12px; color: var(--text-muted); }

/* segmented / period chips */
.seg { display: flex; gap: 6px; overflow-x: auto; margin: 4px 0 12px; scrollbar-width: none; }
.seg button {
  flex: none; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary);
  border-radius: 999px; padding: 7px 14px; font-size: 14px;
}
.seg button[aria-pressed="true"] { background: var(--text-primary); border-color: var(--text-primary); color: var(--bg); }

/* trips list */
.day { font-size: 13px; font-weight: 600; color: var(--text-secondary); margin: 16px 4px 6px; }
.day span { font-weight: 400; color: var(--text-muted); }
.trip {
  display: grid; grid-template-columns: 1fr auto auto; gap: 4px 14px; align-items: center;
  width: 100%; text-align: left; border: 1px solid var(--border); background: var(--surface-1);
  border-radius: 14px; padding: 12px 14px; margin-bottom: 8px; box-shadow: var(--shadow);
}
.trip .t { font-weight: 600; }
.trip .m { grid-column: 1; font-size: 13px; color: var(--text-muted); }
.trip .km { grid-row: 1 / span 2; grid-column: 2; text-align: right; font-size: 18px; font-weight: 650; }
.trip .km small { display: block; font-size: 12px; font-weight: 400; color: var(--text-muted); }
.trip .cons { grid-row: 1 / span 2; grid-column: 3; text-align: right; min-width: 64px; }
.trip .cons b { font-size: 16px; }
.trip .cons small { display: block; font-size: 11px; color: var(--text-muted); }
.trip.live { border-color: var(--accent); }
.empty { text-align: center; color: var(--text-muted); padding: 32px 16px; }

/* detail */
.back { border: 0; background: none; color: var(--accent); padding: 8px 0; font-weight: 500; display: inline-flex; gap: 4px; align-items: center; }
.map { height: 260px; border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; border: 1px solid var(--border); background: var(--surface-2); }
.map.small { height: 180px; }
.leaflet-container { font: inherit; background: var(--surface-2); }
@media (prefers-color-scheme: dark) { .leaflet-tile-pane { filter: brightness(.75) contrast(1.1); } }
.kv { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 8px; }
.kv div { min-width: 0; }
.kv .k { font-size: 12px; color: var(--text-muted); }
.kv .v { font-size: 17px; font-weight: 600; }
.kv .v small { font-size: 12px; font-weight: 500; color: var(--text-secondary); }
.list .item { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.list .item:first-child { border-top: 0; padding-top: 0; }
.list .item span:first-child { color: var(--text-secondary); }
.list .item span:last-child { font-weight: 500; text-align: right; }
.note { font-size: 12.5px; color: var(--text-muted); margin-top: 10px; }

/* tyres */
.tyres { display: grid; grid-template-columns: 1fr 64px 1fr; gap: 8px; align-items: center; text-align: center; }
.tyres .car { grid-row: 1 / span 2; grid-column: 2; height: 120px; border: 2px solid var(--axis); border-radius: 26px 26px 18px 18px; }
.tyres .p b { font-size: 20px; }
.tyres .p small { display: block; color: var(--text-muted); font-size: 12px; }

/* charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gl { stroke: var(--grid); stroke-width: 1; }
.chart .ax { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--text-muted); font-size: 11px; font-family: inherit; }
.chart .bar { fill: var(--series-1); }
.chart .bar.dim { fill: var(--series-1-soft); }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--series-1); opacity: .10; }
.chart .dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .cross { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .sel { fill: var(--accent-soft); }
.chart-actions { display: flex; justify-content: flex-end; margin-top: 6px; }
.linkbtn { border: 0; background: none; color: var(--accent); font-size: 13px; padding: 4px 0; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; margin-top: 8px; }
table.data th, table.data td { padding: 6px 4px; border-bottom: 1px solid var(--border); text-align: right; font-variant-numeric: tabular-nums; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--text-secondary); font-weight: 500; }
.tip {
  position: fixed; z-index: 50; pointer-events: none; max-width: 220px;
  background: var(--text-primary); color: var(--bg); border-radius: 10px; padding: 8px 10px; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
.tip b { display: block; font-size: 15px; }

/* settings */
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.field input { width: 96px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); text-align: right; }
.danger { border: 1px solid var(--border); background: var(--surface-1); color: var(--critical); border-radius: 12px; padding: 10px 14px; width: 100%; margin-top: 8px; font-weight: 500; }

/* login */
.login { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: grid; place-items: center; padding: 24px; }
.login form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login img { margin: 0 auto; border-radius: 18px; }
.login label { text-align: left; font-size: 13px; color: var(--text-secondary); display: flex; flex-direction: column; gap: 4px; }
.login input { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-1); font-size: 16px; }
.primary { border: 0; background: var(--accent); color: #fff; border-radius: 12px; padding: 12px; font-weight: 600; font-size: 16px; }
:root[data-theme="dark"] .primary, .primary { color: #fff; }
.error { color: var(--critical); min-height: 1.2em; margin: 0; font-size: 14px; }
.skeleton { height: 120px; border-radius: var(--radius); background: var(--surface-2); margin-bottom: 12px; animation: pulse 1.2s infinite; }

@media (min-width: 640px) {
  .tiles { grid-template-columns: repeat(4, 1fr); }
  .map { height: 340px; }
}
