/* FleetTrack - responsive UI, light + dark, no framework. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --line: #e2e5ea;
  --text: #14181f;
  --text-dim: #66707e;
  --accent: #3563e9;
  --accent-soft: #e8eeff;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --charge: #0891b2;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 6px 20px rgba(16, 24, 40, .06);
  --topbar-h: 108px;
}

/* Dark values live in a reusable block so the theme can be forced either way:
   system preference by default, overridden by data-theme on <html>. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1d232c;
    --line: #262d38;
    --text: #e6e9ef;
    --text-dim: #8d97a5;
    --accent: #6b8afd;
    --accent-soft: #1b2440;
    --good: #34d399;
    --warn: #fbbf24;
    --bad: #f87171;
    --charge: #22d3ee;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1d232c;
    --line: #262d38;
    --text: #e6e9ef;
    --text-dim: #8d97a5;
    --accent: #6b8afd;
    --accent-soft: #1b2440;
    --good: #34d399;
    --warn: #fbbf24;
    --bad: #f87171;
    --charge: #22d3ee;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

/* Author `display` rules below would otherwise beat the UA's [hidden] rule. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3 { margin: 0; font-weight: 620; letter-spacing: -.01em; }
h1 { font-size: 17px; }
h2 { font-size: 14px; }
button, input, select { font: inherit; color: inherit; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }
.muted { color: var(--text-dim); }
.note { font-size: 12.5px; margin: 10px 0 0; }

/* ---------------------------------------------------------------- topbar */
.topbar {
  position: sticky; top: 0; z-index: 40;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
  padding: 10px 16px 0;
  padding-top: max(10px, env(safe-area-inset-top));
}
.topbar-main { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; flex: none;
  background: linear-gradient(140deg, var(--accent), #8b5cf6);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.18);
}
.brand-sub { margin: 1px 0 0; font-size: 12px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 6px; flex: none; }

.icon-btn, .unit-toggle, .ghost-btn {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 9px; cursor: pointer; padding: 6px 10px;
  transition: background .15s, border-color .15s, transform .1s;
}
.icon-btn { width: 34px; height: 32px; padding: 0; font-size: 15px; }
.theme-toggle { min-width: 34px; height: 32px; font-size: 14px; }
.unit-toggle { min-width: 44px; height: 32px; font-size: 12.5px; font-weight: 600; }
.icon-btn:hover, .unit-toggle:hover, .ghost-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(.94); }
.ghost-btn { font-size: 12.5px; padding: 5px 11px; }

.livestrip { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 9px; scrollbar-width: none; }
.livestrip::-webkit-scrollbar { display: none; }
.live-pill {
  display: inline-flex; align-items: baseline; gap: 6px; flex: none;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 4px 11px; font-size: 12.5px; white-space: nowrap;
}
.live-pill b { font-weight: 620; font-variant-numeric: tabular-nums; }
.live-pill.state-driving { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.live-pill.state-charging { background: color-mix(in srgb, var(--charge) 16%, transparent);
  border-color: transparent; color: var(--charge); }
.live-pill.state-asleep { opacity: .72; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; align-self: center; }
.dot.pulse { animation: pulse 1.8s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .25 } }

/* ------------------------------------------------------------------ tabs */
.tabs {
  position: sticky; top: var(--topbar-h); z-index: 30;
  display: flex; gap: 2px; padding: 0 12px;
  background: var(--bg); border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; cursor: pointer; padding: 11px 12px;
  font-size: 13.5px; color: var(--text-dim); border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 14px 12px 48px; max-width: 1180px; margin: 0 auto; }
.view { display: none; }
.view.is-active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(3px) } }

/* --------------------------------------------------------------- filters */
.filters { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.range-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.range-chips::-webkit-scrollbar { display: none; }
.range-chips button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 5px 14px; font-size: 13px; cursor: pointer; flex: none;
}
.range-chips button.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }

.filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filter-row input[type=search] { grid-column: 1 / -1; }
.filter-row input[type=search], .filter-row select, .place-form input, .place-form button {
  width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 11px; outline: none;
}
.filter-row input[type=search]:focus, .filter-row select:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.minkm { display: flex; align-items: center; gap: 10px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px; font-size: 12.5px;
  color: var(--text-dim); }
.minkm span { white-space: nowrap; font-variant-numeric: tabular-nums; }
.minkm input { flex: 1; accent-color: var(--accent); min-width: 60px; }

/* ------------------------------------------------------------------ kpis */
.kpi-strip { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 14px; }
.kpi-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 14px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 11px 13px;
}
.kpi .kpi-label { font-size: 11.5px; color: var(--text-dim); text-transform: uppercase;
  letter-spacing: .04em; }
.kpi .kpi-value { font-size: 21px; font-weight: 640; margin-top: 3px;
  font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi .kpi-value small { font-size: 12px; font-weight: 500; color: var(--text-dim); margin-left: 2px; }
.kpi .kpi-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

/* ----------------------------------------------------------------- trips */
.trip-list { display: flex; flex-direction: column; gap: 8px; }
.trip {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer; box-shadow: var(--shadow);
  transition: border-color .15s, transform .1s;
}
.trip:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.trip:active { transform: scale(.995); }
.trip-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.trip-date { font-size: 12.5px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.time-end { opacity: .75; white-space: nowrap; }
.trip-dist { font-size: 15px; font-weight: 640; font-variant-numeric: tabular-nums; }
.route { display: grid; grid-template-columns: 14px 1fr; gap: 0 9px; margin: 9px 0 10px; }
.route-rail { position: relative; }
.route-rail::before {
  content: ""; position: absolute; left: 5px; top: 8px; bottom: 8px;
  width: 2px; background: var(--line); border-radius: 2px;
}
.route-node { display: flex; align-items: center; height: 20px; }
.route-node i {
  width: 8px; height: 8px; border-radius: 50%; margin-left: 2px; z-index: 1;
  background: var(--surface); box-shadow: 0 0 0 2px var(--text-dim);
}
.route-node.end i { background: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.route-text { display: flex; flex-direction: column; min-width: 0; }
.route-text span {
  display: block; height: 20px; line-height: 20px; font-size: 13.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}
.route-text span:first-child { color: var(--text-dim); }
.trip-meta { display: flex; flex-wrap: wrap; gap: 5px 6px; }
.chip {
  font-size: 11.5px; background: var(--surface-2); border-radius: 7px; padding: 2px 8px;
  color: var(--text-dim); font-variant-numeric: tabular-nums;
}
.chip.soc { color: var(--good); background: color-mix(in srgb, var(--good) 12%, transparent); }
.chip.energy { color: var(--warn); background: color-mix(in srgb, var(--warn) 13%, transparent); }
.badge {
  display: inline-block; margin-left: 6px; font-size: 9.5px; letter-spacing: .02em;
  text-transform: uppercase; border-radius: 5px; padding: 1px 5px; vertical-align: 1px;
}
.badge.approx { color: var(--text-dim); border: 1px dashed var(--line); }
.badge.live { color: var(--accent); background: var(--accent-soft); }

.list-footer { display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 18px 0 6px; font-size: 12.5px; }
.empty { text-align: center; color: var(--text-dim); padding: 46px 16px; }
.empty b { display: block; color: var(--text); margin-bottom: 4px; }

/* ---------------------------------------------------------------- banner */
.banner {
  --tone: var(--warn);
  display: flex; align-items: center; gap: 12px; margin-bottom: 12px;
  padding: 11px 12px 11px 14px; border-radius: var(--radius);
  background: color-mix(in srgb, var(--tone) 11%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--tone) 35%, var(--line));
  border-left: 4px solid var(--tone);
}
.banner.is-overdue { --tone: var(--bad); }
.banner-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.banner-text b { font-size: 14px; font-weight: 620; }
.banner-text span { font-size: 12.5px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.banner .ghost-btn { flex: none; }

/* ----------------------------------------------------------------- cards */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px;
  margin-bottom: 12px; }
.card-head select { background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 4px 8px; font-size: 12.5px; }
.two-up { display: grid; grid-template-columns: 1fr; gap: 12px; }
.chart-wrap { width: 100%; overflow-x: auto; }
.chart-wrap svg { display: block; width: 100%; height: auto; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 13px; }
.kv dt { color: var(--text-dim); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.meter { height: 7px; border-radius: 999px; background: var(--surface-2); overflow: hidden;
  margin: 6px 0 2px; }
.meter i { display: block; height: 100%; background: var(--accent); border-radius: 999px;
  transition: width .3s; }
.meter i.warn { background: var(--warn); }
.meter i.bad { background: var(--bad); }
.budget-row { margin-bottom: 14px; }
.budget-row .budget-head { display: flex; justify-content: space-between; font-size: 13px; }
.budget-row .budget-sub { font-size: 12px; color: var(--text-dim); }

.place-list { display: flex; flex-direction: column; gap: 6px; }
.place-row { display: flex; justify-content: space-between; align-items: center; gap: 10px;
  background: var(--surface-2); border-radius: 10px; padding: 8px 11px; font-size: 13px; }
.place-row .place-meta { color: var(--text-dim); font-size: 12px; }
.place-row button { border: 0; background: none; color: var(--bad); cursor: pointer; font-size: 13px; }
.place-form { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 12px; }
.place-form button { background: var(--accent); color: #fff; border-color: transparent;
  cursor: pointer; grid-column: 1 / -1; }

.log { display: flex; flex-direction: column; gap: 3px; max-height: 320px; overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; }
.log-line { display: flex; gap: 8px; }
.log-line time { color: var(--text-dim); flex: none; }
.log-line.error { color: var(--bad); }
.log-line.warn { color: var(--warn); }

/* ----------------------------------------------------------------- sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(8, 10, 14, .45);
  backdrop-filter: blur(2px); z-index: 50; }
.sheet {
  position: fixed; z-index: 60; background: var(--surface); display: flex; flex-direction: column;
  left: 0; right: 0; bottom: 0; top: 8vh; border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, .3); animation: slideUp .22s cubic-bezier(.2, .8, .3, 1);
}
@keyframes slideUp { from { transform: translateY(14px); opacity: .6 } }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: 14px 16px;
  border-bottom: 1px solid var(--line); flex: none; }
.sheet-body { overflow-y: auto; padding: 14px 16px 40px; -webkit-overflow-scrolling: touch; }
#map { height: 240px; border-radius: 12px; overflow: hidden; background: var(--surface-2);
  margin-bottom: 14px; }
.leaflet-container { background: var(--surface-2); font: inherit; }
/* Attribution is required by Mapbox's terms; make it legible in both themes. */
.leaflet-control-attribution {
  background: color-mix(in srgb, var(--surface) 82%, transparent) !important;
  color: var(--text-dim) !important; font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--text-dim) !important; }
.detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 14px; }
.detail-grid .kpi { padding: 9px 11px; }
.detail-grid .kpi-value { font-size: 15.5px; }
.detail-grid .kpi-label { font-size: 10.5px; }
.note-field { width: 100%; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 11px; resize: vertical; min-height: 62px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 22px; z-index: 90;
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 999px;
  font-size: 13px; box-shadow: var(--shadow); }

.skeleton { background: linear-gradient(90deg, var(--surface-2) 25%,
  color-mix(in srgb, var(--surface-2) 60%, var(--line)) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite; border-radius: var(--radius);
  height: 92px; }
@keyframes shimmer { to { background-position: -200% 0 } }

/* ----------------------------------------------------------- breakpoints */
@media (min-width: 700px) {
  :root { --topbar-h: 104px; }
  main { padding: 20px 20px 60px; }
  .filter-row { grid-template-columns: 1fr auto auto; }
  .filter-row input[type=search] { grid-column: auto; }
  .filter-row select { width: auto; }
  .minkm { width: 220px; }
  .kpi-strip { grid-template-columns: repeat(4, 1fr); }
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  .two-up { grid-template-columns: 1fr 1fr; }
  .place-form { grid-template-columns: 1.4fr 1fr 1fr .8fr auto; }
  .place-form button { grid-column: auto; }
  /* Desktop: one scannable row per trip, origin and destination inline. */
  .trip { display: grid; grid-template-columns: 252px minmax(0, 1fr) minmax(0, 300px) 86px;
    align-items: center; gap: 16px; padding: 11px 16px; }
  .trip-top { display: contents; }
  /* All four cells share row 1; without this, sparse auto-placement pushes
     the route and chips onto a second row. */
  .trip-date, .trip-dist, .route, .trip-meta { grid-row: 1; }
  .trip-date { grid-column: 1; }
  .trip-dist { grid-column: 4; text-align: right; }
  .route { grid-column: 2; margin: 0; grid-template-columns: 1fr; }
  .route-rail { display: none; }
  .route-text { flex-direction: row; align-items: center; gap: 8px; }
  .route-text span:first-child { flex: 0 1 auto; }
  .route-text span + span { flex: 1 1 auto; }
  .route-text span + span::before { content: "→ "; color: var(--text-dim); }
  .trip-meta { grid-column: 3; margin-top: 0; justify-content: flex-end; }
  /* Charge rows need a wider trailing cell for "41% → 84%". */
  .trip.charge-row { grid-template-columns: 252px minmax(0, 1fr) minmax(0, 270px) 118px; }
  .charge-row .route-text span:first-child { color: var(--text); }
  .sheet { top: 0; left: auto; width: min(560px, 100%); border-radius: 0;
    animation: slideIn .22s cubic-bezier(.2, .8, .3, 1); }
  @keyframes slideIn { from { transform: translateX(20px); opacity: .5 } }
  #map { height: 300px; }
  .detail-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1000px) {
  .trip { grid-template-columns: 268px minmax(0, 1fr) minmax(0, 380px) 92px; }
  .trip.charge-row { grid-template-columns: 168px minmax(0, 1fr) minmax(0, 390px) 122px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
