:root {
  --bg: #ffffff;
  --fg: #16202a;
  --muted: #5b6875;
  --line: #dde3e9;
  --accent: #0b4f7a;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --warn: #8a5300;
  --warn-bg: #fff4e0;
  --ok: #1a7f4b;
  --radius: 14px;
  --shadow: 0 4px 18px rgb(0 0 0 / 0.16);
  color-scheme: light dark;
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b232b;
    --fg: #e8edf2;
    --muted: #9aa7b4;
    --line: #33404c;
    --accent: #6cb6ea;
    --danger: #ff8a80;
    --danger-bg: #4a1f1c;
    --warn: #ffc56b;
    --warn-bg: #43330f;
    --ok: #6fd39e;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
[hidden] { display: none !important; }

button, select, input { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
}
button:active { transform: scale(0.97); }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(360px, 100%);
  display: grid;
  gap: 14px;
  justify-items: stretch;
  text-align: center;
}
.login-card img { justify-self: center; }
.login-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.login-card label { display: grid; gap: 4px; text-align: left; color: var(--muted); font-size: 0.9rem; }
.login-card input {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 1rem;
}
.login-card button[type="submit"] {
  padding: 12px;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.error { color: var(--danger); margin: 0; }

/* ---------- App layout ---------- */
.app { position: fixed; inset: 0; }
.map { position: absolute; inset: 0; z-index: 0; }

.topbar {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 10px;
  right: 10px;
  z-index: 1000;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.topbar-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.topbar button, .trail-range, .conn { box-shadow: var(--shadow); }
.trail-range {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 6px 4px 12px;
  display: flex;
  gap: 4px;
  align-items: center;
  color: var(--muted);
}
.trail-range select { border: 0; background: transparent; color: var(--fg); }

.conn {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px 6px 26px;
  position: relative;
  font-size: 0.85rem;
  white-space: nowrap;
}
.conn::before {
  content: "";
  position: absolute;
  left: 11px;
  top: 50%;
  width: 9px;
  height: 9px;
  margin-top: -4.5px;
  border-radius: 50%;
  background: var(--muted);
}
.conn[data-state="live"]::before { background: var(--ok); }
.conn[data-state="offline"]::before { background: var(--danger); }

.banner {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 58px);
  left: 10px;
  right: 10px;
  z-index: 1000;
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid currentColor;
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow);
  font-weight: 600;
}
.banner p { margin: 0; }
.banner p + p { margin-top: 4px; }
.banner small { display: block; font-weight: 400; }

/* Cards: a swipeable row along the bottom on phones, a sidebar on wide screens. */
.cards {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  gap: 10px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  padding: 10px 10px calc(env(safe-area-inset-bottom) + 12px);
}
.card {
  flex: 0 0 min(86vw, 340px);
  scroll-snap-align: center;
  background: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  border-left: 6px solid var(--child);
  /* Column layout with the chips pinned to the bottom, and a stable minimum height, so the
     cards don't jump as the "updated" text, speed or warnings change length. */
  display: flex;
  flex-direction: column;
  min-height: 150px;
}
.card .chips { margin-top: auto; padding-top: 8px; }
.card.not-reporting { outline: 2px solid var(--danger); }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-name { font-weight: 700; font-size: 1.05rem; cursor: pointer; background: none; border: 0; padding: 0; }
.card-platform { color: var(--muted); font-size: 0.85rem; }
.card-head .spacer { flex: 1; }
.card-meta { color: var(--muted); font-size: 0.9rem; margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.card-meta strong { color: var(--fg); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn);
}
.chip.danger { background: var(--danger-bg); color: var(--danger); }
.card-actions { display: flex; gap: 6px; }
.motion { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-weight: 600; }
.motion::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}
.motion.moving { color: var(--ok); }
.motion.moving::before { background: var(--ok); }
.motion.unknown { color: var(--muted); font-weight: 400; }
.trail-toggle[aria-pressed="true"] { background: var(--child); border-color: var(--child); color: #fff; }
.empty { background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }

/* History: a bottom sheet on phones, the sidebar on wide screens. */
.history {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  max-height: 55vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  padding: 12px 14px calc(env(safe-area-inset-bottom) + 14px);
  border-top: 6px solid var(--child);
}
.history-head { display: flex; align-items: center; gap: 10px; }
.history-head strong { font-size: 1.05rem; }
.history-day { display: flex; align-items: center; justify-content: space-between; margin: 10px 0 4px; }
.history-day span { font-weight: 600; }
.history-day button { width: 40px; }
.history-day button:disabled { opacity: 0.3; }
.history-summary { color: var(--muted); margin: 0 0 8px; font-size: 0.9rem; }
.history-list { list-style: none; margin: 0; padding: 0; }
.history-list li {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 2px 8px;
  padding: 10px 6px;
  border-top: 1px solid var(--line);
  cursor: pointer;
  border-radius: 8px;
}
.history-list li:first-child { border-top: 0; }
.history-list li[aria-selected="true"] { background: color-mix(in srgb, var(--child) 14%, transparent); }
.history-list .icon { grid-row: span 2; font-size: 1.2rem; line-height: 1.6; text-align: center; }
.history-list .title { font-weight: 600; }
.history-list .sub { color: var(--muted); font-size: 0.85rem; }
.history-list .sub .chip { margin-left: 6px; }
.history-empty { color: var(--muted); padding: 12px 0; }

.endpoint {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 3px solid var(--child);
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.4);
}
.endpoint.end { background: var(--child); border-color: #fff; }

@media (min-width: 900px) {
  .history {
    top: 70px;
    right: auto;
    width: 360px;
    max-height: none;
    border-radius: var(--radius);
    margin: 0 0 16px 10px;
    border-top: 0;
    border-left: 6px solid var(--child);
  }
  .cards {
    top: 70px;
    right: auto;
    width: 360px;
    flex-direction: column;
    overflow-x: visible;
    overflow-y: auto;
    padding-bottom: 16px;
  }
  .card { flex: none; }
  .banner { left: 380px; }
}

/* ---------- Alerts dialog ---------- */
.alerts-dialog {
  width: min(420px, calc(100vw - 24px));
  border: 0;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--bg);
  color: var(--fg);
  padding: 18px 20px 16px;
}
.alerts-dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.alerts-dialog h2 { margin: 0 0 10px; font-size: 1.2rem; }
.alerts-dialog h3 { margin: 0 0 4px; font-size: 1rem; }
.account { border-top: 1px solid var(--line); margin-top: 16px; padding-top: 14px; }
.alerts-close { position: absolute; top: 10px; right: 10px; }
.alerts-close button { border: 0; font-size: 1rem; }
.alerts-status { margin: 0 0 12px; color: var(--muted); }
.alerts-status.ok { color: var(--ok); font-weight: 600; }
.alerts-status.problem { color: var(--danger); }
.alerts-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.alerts-actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; padding: 9px 14px; }
.alerts-prefs { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px 10px; margin: 0; }
.alerts-prefs legend { color: var(--muted); font-size: 0.85rem; padding: 0 4px; }
.alerts-prefs label { display: flex; align-items: center; gap: 10px; padding: 7px 0; }
.alerts-prefs input { width: 18px; height: 18px; accent-color: var(--accent); }
.alerts-prefs small { color: var(--muted); }
.alerts-note { color: var(--muted); font-size: 0.8rem; margin: 10px 0 0; }

/* ---------- Map markers ---------- */
.pin {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--child);
  border: 3px solid #fff;
  box-shadow: 0 2px 6px rgb(0 0 0 / 0.4);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
  font-size: 15px;
}
.pin { position: relative; }
.pin.stale { filter: grayscale(0.8); opacity: 0.75; }
.speed-badge {
  position: absolute;
  top: 32px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  background: #16202a;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 999px;
}
.speed-badge:empty { display: none; }
.leaflet-container { font: inherit; }
.leaflet-bottom { bottom: var(--cards-h, 0px); }
