:root {
  --bg: #0e1116;
  --panel: #151a21;
  --panel-2: #1b222b;
  --line: #262e39;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #3b82f6;
  --online: #22c55e;
  --stale: #f59e0b;
  --dormant: #64748b;
  --danger: #ef4444;
  --radius: 10px;
  --sidebar-w: 340px;
}

* { box-sizing: border-box; }

/* The `hidden` attribute is how this app shows and hides panels, but the UA rule
 * that implements it loses to any author `display` declaration -- author styles
 * beat the user-agent stylesheet regardless of specificity. Without this, a
 * `.login-screen { display: grid }` overlay stays painted over a perfectly
 * working app. Applies to .app, .route-summary and .trip-list too. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- login ---------- */

.login-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b2735, var(--bg));
  z-index: 50;
}

.login-card {
  width: min(360px, 90vw);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
}

.login-card h1 { margin: 0; font-size: 26px; letter-spacing: -.02em; }
.login-sub { margin: 0 0 18px; color: var(--muted); }
.login-card label { font-size: 12px; color: var(--muted); margin-top: 8px; }

input, select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  width: 100%;
}

input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

button:hover { filter: brightness(1.08); }
button:disabled { opacity: .55; cursor: default; }
button.secondary { background: var(--panel-2); color: var(--text); border-color: var(--line); }

#login-btn { margin-top: 18px; }

.login-error {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(239, 68, 68, .12);
  border: 1px solid rgba(239, 68, 68, .35);
  color: #fca5a5;
  font-size: 13px;
}

/* ---------- layout ---------- */

.app { display: flex; height: 100%; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.brand { font-weight: 700; font-size: 17px; letter-spacing: -.01em; }

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  padding: 4px;
  font-weight: 500;
  font-size: 13px;
}
.link-btn:hover { color: var(--text); }

.controls { padding: 14px 14px 10px; display: flex; flex-direction: column; gap: 10px; }

.filters { display: flex; gap: 6px; }

.chip {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 7px 4px;
  border-radius: 7px;
}
.chip span { opacity: .7; margin-left: 2px; }
.chip.is-active { background: var(--accent); color: #fff; border-color: transparent; }

/* ---------- fleet list ---------- */

.fleet {
  list-style: none;
  margin: 0;
  padding: 0 8px 8px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.fleet li {
  padding: 11px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid transparent;
}

.fleet li:hover { background: var(--panel-2); }
.fleet li.is-selected { background: var(--panel-2); border-color: var(--accent); }

.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.online { background: var(--online); box-shadow: 0 0 0 3px rgba(34, 197, 94, .16); }
.dot.stale { background: var(--stale); }
.dot.dormant, .dot.no_data { background: var(--dormant); }

.veh-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.veh-sub { font-size: 12px; color: var(--muted); }
.veh-right { text-align: right; font-size: 12px; }
.veh-speed { font-weight: 700; }
.veh-volt { color: var(--muted); }
.veh-volt.low { color: var(--danger); font-weight: 600; }

.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.sidebar-foot {
  border-top: 1px solid var(--line);
  padding: 10px 16px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

.conn::before {
  content: "";
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.conn-on::before { background: var(--online); }
.conn-off::before { background: var(--dormant); }

/* ---------- map ---------- */

.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; }

.maplibregl-ctrl-attrib { font-size: 10px; }

/* ---------- detail panel ---------- */

.detail {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 320px;
  max-height: calc(100% - 24px);
  overflow-y: auto;
  background: rgba(21, 26, 33, .96);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .5);
}

.detail h2 { margin: 0 4px 4px 0; font-size: 18px; letter-spacing: -.01em; }

.detail-close {
  float: right;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  padding: 0 2px;
}
.detail-close:hover { color: var(--text); }

.detail-state { font-size: 12px; color: var(--muted); margin-bottom: 14px; }

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.stat {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 9px 11px;
}
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 16px; font-weight: 650; margin-top: 2px; }
.stat-value.low { color: var(--danger); }
.stat.wide { grid-column: 1 / -1; }
.stat.wide .stat-value { font-size: 13px; font-weight: 500; line-height: 1.35; }

.detail-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.detail-actions label { grid-column: 1 / -1; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.detail-actions button { padding: 9px 12px; font-size: 13px; }

.route-summary {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.raw { margin-top: 16px; }
.raw summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.raw table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 12px; }
.raw td { padding: 4px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.raw td:first-child { color: var(--muted); width: 45%; }
.raw td:last-child { text-align: right; word-break: break-word; }

@media (max-width: 820px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; flex: 0 0 46vh; border-right: none; border-bottom: 1px solid var(--line); }
  .detail { position: fixed; inset: auto 8px 8px 8px; width: auto; max-height: 55vh; }
}

/* ---------- trips & day summaries ---------- */

.detail-actions.secondary-row {
  grid-template-columns: 1fr 1fr auto;
  border-top: none;
  padding-top: 8px;
}

.trip-list { margin-top: 12px; }

.trip-list h3 {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin: 0 0 8px;
}

.trip {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 10px;
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--panel-2);
  margin-bottom: 6px;
  cursor: pointer;
  border: 1px solid transparent;
}
.trip:hover { border-color: var(--line); }
.trip.is-active { border-color: var(--accent); }

.trip-when { font-weight: 600; font-size: 13px; }
.trip-meta { font-size: 11px; color: var(--muted); }
.trip-dist { font-weight: 700; font-size: 13px; text-align: right; }
.trip-speed { font-size: 11px; color: var(--muted); text-align: right; }

.day-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  padding: 7px 11px;
  font-size: 12px;
  border-bottom: 1px solid var(--line);
}
.day-row:last-child { border-bottom: none; }
.day-row .d { color: var(--muted); }
.day-row .km { font-weight: 650; text-align: right; }
.day-row .mv { color: var(--muted); text-align: right; }

.trip-empty { color: var(--muted); font-size: 12px; padding: 10px 2px; }

/* ---------- replay ---------- */

.replay {
  position: absolute;
  left: 12px;
  right: 356px;
  bottom: 12px;
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  background: rgba(21, 26, 33, .96);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, .5);
}

.replay-play {
  width: 40px; height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 15px;
  line-height: 1;
  display: grid;
  place-items: center;
}

.replay-body { min-width: 0; }

.replay-scrub {
  width: 100%;
  padding: 0;
  background: none;
  border: none;
  accent-color: var(--online);
  cursor: pointer;
}

.replay-meta {
  display: flex;
  gap: 14px;
  align-items: baseline;
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}
.replay-kmh { font-weight: 700; color: var(--text); }
.replay-progress { margin-left: auto; }

.replay-rate { width: auto; padding: 8px 10px; font-size: 13px; }

.replay-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  padding: 0 4px;
}
.replay-close:hover { color: var(--text); }

@media (max-width: 820px) {
  .replay { right: 12px; bottom: 12px; }
}
