:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --text: #1c1d1f;
  --muted: #6b6f76;
  --line: #e6e3dc;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fdf0e8;
  --ok: #15803d;
  --warn: #b45309;
  --off: #9ca3af;
  --shadow: 0 1px 2px rgba(20, 20, 20, .06), 0 4px 16px rgba(20, 20, 20, .06);
  --radius: 12px;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111315;
    --surface: #1a1d20;
    --surface-2: #212529;
    --text: #e8e6e1;
    --muted: #9aa0a6;
    --line: #2c3035;
    --accent: #f97316;
    --accent-ink: #1a0e05;
    --accent-soft: #33200f;
    --ok: #4ade80;
    --warn: #fbbf24;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 14px; -webkit-font-smoothing: antialiased; }
h2 {
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); margin: 0 0 10px;
}

button {
  font: inherit; cursor: pointer; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px;
  transition: background .15s, border-color .15s, color .15s;
}
button:hover { border-color: var(--accent); }
button:focus-visible, input:focus-visible, select:focus-visible, .trip:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.ghost { background: none; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: var(--surface-2); }
button.small { padding: 3px 8px; font-size: 12px; }
input, select {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; min-width: 0;
}

/* ---------- top bar ---------- */
.topbar {
  height: 56px; display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: relative; z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand-mark {
  width: 30px; height: 30px; padding: 4px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink);
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.topbar select { min-width: 130px; }
.pill {
  display: inline-flex; align-items: center; gap: 8px; padding: 5px 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line); font-size: 13px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); flex: none; }
.pill.moving .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.pill.parked .dot { background: var(--accent); }
.pill.stale .dot { background: var(--warn); }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 360px 1fr; height: calc(100% - 56px); }
.panel { overflow-y: auto; padding: 14px; display: grid; gap: 12px; align-content: start; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.card-head h2 { margin: 0; }

.seg { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { border: none; background: none; padding: 6px 4px; border-radius: 7px; color: var(--muted); font-weight: 500; }
.seg button:hover { color: var(--text); }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.seg.units { grid-template-columns: 1fr 1fr; padding: 2px; gap: 2px; border-radius: 8px; }
.seg.units button { padding: 4px 10px; font-size: 13px; border-radius: 6px; }
.custom { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; margin-top: 12px; }
.custom label { display: grid; gap: 4px; }
.custom label span { font-size: 12px; color: var(--muted); }
.custom.active input { border-color: var(--accent); }

.switch { display: flex; align-items: center; gap: 10px; margin-top: 12px; color: var(--muted); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track { width: 34px; height: 20px; border-radius: 999px; background: var(--line); position: relative; transition: background .15s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s; }
.switch input:checked + .track { background: var(--ok); }
.switch input:checked + .track::after { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; }
.tile .label { font-size: 12px; color: var(--muted); }
.tile .value { font-size: 22px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.tile .value small { font-size: 13px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.tile .sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.kv dd.warn { color: var(--warn); font-weight: 600; }
.bars { display: inline-flex; gap: 2px; align-items: end; height: 12px; margin-right: 6px; vertical-align: -1px; }
.bars i { width: 3px; background: var(--line); border-radius: 1px; }
.bars i.on { background: var(--text); }

.trips { display: grid; gap: 2px; }
.day { font-size: 12px; font-weight: 600; color: var(--muted); margin: 10px 0 4px; }
.day:first-child { margin-top: 0; }
.trip {
  display: grid; grid-template-columns: 4px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 8px 8px 0; border-radius: 8px; cursor: pointer;
}
.trip:hover { background: var(--surface-2); }
.trip.selected { background: var(--accent-soft); }
.trip .bar { align-self: stretch; border-radius: 2px; background: var(--line); }
.trip.selected .bar { background: var(--accent); }
.trip .t-time { font-weight: 600; font-variant-numeric: tabular-nums; }
.trip .t-meta { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.trip .t-dist { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.trip .t-dist small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; }
.none { color: var(--muted); padding: 6px 0; }

/* ---------- map ---------- */
.map-wrap { position: relative; min-height: 320px; }
.map { position: absolute; inset: 0; background: var(--surface-2); }
.leaflet-container { font: inherit; }
.leaflet-bar a, .leaflet-control-layers { background: var(--surface) !important; color: var(--text) !important; border-color: var(--line) !important; }
.leaflet-control-layers-expanded { padding: 8px 12px; border-radius: 10px; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); }
.leaflet-popup-content { font-size: 13px; line-height: 1.5; margin: 10px 14px; }
.leaflet-popup-content .muted { color: var(--muted); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--muted) !important; }

.legend {
  background: var(--surface); color: var(--text); padding: 8px 12px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 12px; line-height: 1.8; border: 1px solid var(--line);
}
.legend i { display: inline-block; width: 18px; height: 5px; border-radius: 3px; margin-right: 8px; vertical-align: middle; }
.legend .p { width: 14px; height: 14px; border-radius: 4px; background: #334155; color: #fff; font-size: 9px; font-weight: 700; line-height: 14px; text-align: center; font-style: normal; }
.fit-btn { font-size: 18px; line-height: 30px; width: 34px; height: 34px; text-align: center; display: block; }

.van-marker {
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  border: 3px solid #fff; display: grid; place-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.35);
  animation: pulse 2.2s infinite;
}
.van-marker svg { width: 18px; height: 18px; }
@keyframes pulse {
  0% { box-shadow: 0 2px 8px rgba(0,0,0,.35), 0 0 0 0 rgba(234, 88, 12, .5); }
  70% { box-shadow: 0 2px 8px rgba(0,0,0,.35), 0 0 0 16px rgba(234, 88, 12, 0); }
  100% { box-shadow: 0 2px 8px rgba(0,0,0,.35), 0 0 0 0 rgba(234, 88, 12, 0); }
}
.stop-marker {
  width: 22px; height: 22px; border-radius: 6px; background: #334155; color: #fff; border: 2px solid #fff;
  font: 700 11px/18px system-ui, sans-serif; text-align: center; box-shadow: 0 1px 4px rgba(0,0,0,.35);
}
@media (prefers-reduced-motion: reduce) { .van-marker { animation: none; } }

.banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 1000;
  max-width: min(520px, calc(100% - 24px)); background: var(--surface); color: var(--text);
  border: 1px solid var(--warn); border-left-width: 4px; border-radius: 10px; padding: 10px 14px;
  box-shadow: var(--shadow);
}
.empty-state {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 900;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 22px; box-shadow: var(--shadow); display: grid; gap: 4px; text-align: center;
}
.empty-state span { color: var(--muted); }

/* ---------- login ---------- */
.login-body { display: grid; place-items: center; padding: 16px; }
.login {
  width: min(360px, 100%); display: grid; gap: 14px; background: var(--surface);
  padding: 28px; border-radius: 16px; border: 1px solid var(--line); box-shadow: var(--shadow);
}
.login .brand { margin-bottom: 6px; font-size: 18px; }
.login label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login input { padding: 9px 10px; }
.login button { padding: 10px; }
.error { color: var(--warn); margin: 0; }

/* ---------- small screens ---------- */
@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 58vh auto; height: auto; }
  .map-wrap { order: -1; height: 58vh; }
  .panel { padding: 12px 16px 24px; }
  .topbar { gap: 10px; padding: 0 12px; }
  .brand span { display: none; }
  .pill { font-size: 12px; padding: 4px 10px; }
  .topbar-right { gap: 4px; }
  .topbar select { min-width: 0; max-width: 96px; }
  .topbar .ghost { padding: 6px 6px; white-space: nowrap; }
  .legend { font-size: 11px; line-height: 1.5; padding: 6px 8px; }
  .legend i { width: 12px; margin-right: 5px; }
}
