:root {
  --ink: #1b1f3b;
  --accent: #4a5cff;
  --accent-2: #17c98a;
  --bg: #f4f5fb;
  --card: #ffffff;
  --muted: #6b7089;
  --danger: #e5484d;
  --border: #e3e5f0;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
/* height:100% can fall short of the true visible screen on iOS Safari/WKWebView
   (it resolves against the layout viewport, not the actual visible one) —
   leaving a gap below the tab bar instead of it sitting flush at the bottom.
   100dvh tracks the real visible viewport; @supports guards older engines. */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior: none;
}
#app, .view { height: 100%; }
/* !important needed: #view-auth/#view-app set their own `display` by ID,
   which otherwise outranks this class+attribute selector and the "hidden"
   screen keeps rendering on top regardless of the hidden attribute. */
.view[hidden] { display: none !important; }

/* ---- Auth ---- */
#view-auth {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, var(--ink), #363c72);
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border-radius: 20px;
  padding: 28px 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.auth-card h1 { margin: 0 0 4px; font-size: 24px; }
.tagline { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.version-tag { margin: 16px 0 0; color: var(--muted); font-size: 11px; text-align: center; }
.copyright { margin: 2px 0 0; color: var(--muted); font-size: 11px; text-align: center; }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 15px;
}
.auth-form button, .inline-form button, #place-add-btn {
  padding: 12px 14px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.link-btn {
  display: block;
  width: 100%;
  margin-top: 14px;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  text-align: center;
}
.error {
  background: #fdeceb;
  color: var(--danger);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
}

/* ---- App shell ---- */
#view-app { display: flex; flex-direction: column; position: relative; }
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 12px 10px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.topbar select {
  flex: 1;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
  min-width: 0;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 36px; height: 36px;
  border-radius: 999px;
  border: none;
  background: var(--border);
  cursor: pointer;
  font-size: 16px;
}

/* #map-area is the flex space between the topbar and tab bar — whatever
   that actually is, since both bars size themselves from real content plus
   safe-area insets rather than a fixed guess. #map and every .panel fill it
   exactly, so neither has to hardcode a pixel height that would drift out of
   sync with the tab bar's true height (which varies by device thanks to
   env(safe-area-inset-bottom) below). */
#map-area { flex: 1; min-height: 0; position: relative; }

/* position+z-index (not auto) makes #map establish its own stacking context,
   so Leaflet's internal panes/controls (z-index up to ~1000) can't leak out
   and paint on top of the .panel overlays (Activity/Places/Circle), which
   otherwise only have z-index:20 to compete with. */
#map { position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 0; }

.tabbar {
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1;
  padding: 12px 6px;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  border-top: 2px solid transparent;
}
.tab.active { color: var(--accent); border-top-color: var(--accent); font-weight: 600; }

.panel {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  overflow-y: auto;
  padding: 16px;
  z-index: 20;
}
/* !important needed: #panel-chat sets its own `display` (flex, for its
   column layout), which otherwise outranks this class+attribute selector —
   same trap as the #view-auth/#view-app fix earlier. */
.panel[hidden] { display: none !important; }
.panel h2 { margin: 0 0 10px; }
.panel h3 { margin: 20px 0 8px; font-size: 15px; }

/* Places is a bottom sheet, not a full-screen panel: it needs the map
   visible above it so the "tap the map to add a place" flow is possible —
   a full-height opaque panel would hide the very thing you're meant to tap. */
#panel-places {
  top: auto;
  height: 42%;
  min-height: 220px;
  border-top: 1px solid var(--border);
  box-shadow: 0 -6px 20px rgba(0,0,0,.12);
}
.hint { color: var(--muted); font-size: 13px; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list li {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
}
.list li .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.list li .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.list li button {
  border: none; background: none; color: var(--danger); font-size: 13px; cursor: pointer;
}

.inline-form { display: flex; flex-wrap: wrap; gap: 8px; }
.inline-form input {
  flex: 1;
  min-width: 120px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
}

.place-picker {
  position: fixed;
  left: 12px; right: 12px; bottom: 70px;
  background: var(--card);
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.2);
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.place-picker[hidden] { display: none; }
.place-picker span { font-size: 12px; color: var(--muted); }
.place-picker input { padding: 10px; border-radius: 8px; border: 1px solid var(--border); }

/* ---- Member bottom sheet (Map tab) — Life360-style member list floating
   over the map, with a detail card that replaces it when drilled into someone. ---- */
.member-sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  max-height: 42%;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -6px 24px rgba(0,0,0,.18);
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.member-sheet[hidden] { display: none; }
.member-sheet-handle {
  width: 40px; height: 4px;
  background: var(--border);
  border-radius: 999px;
  margin: 10px auto 4px;
  flex-shrink: 0;
}
.member-sheet-list {
  overflow-y: auto;
  padding: 6px 14px calc(14px + env(safe-area-inset-bottom));
  margin: 0;
  gap: 2px;
}
.member-sheet-list li { padding: 0; border: none; background: none; }
.member-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 8px 6px;
  cursor: pointer;
  border-radius: 12px;
}
.member-row:active { background: var(--bg); }
.member-row-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.member-avatar-wrap { position: relative; flex-shrink: 0; }
.battery-badge {
  position: absolute;
  bottom: -3px; left: -6px;
  font-size: 9px;
  font-weight: 700;
  color: white;
  padding: 1px 5px;
  border-radius: 999px;
  line-height: 1.4;
  white-space: nowrap;
}
.battery-badge.batt-good { background: var(--accent-2); }
.battery-badge.batt-low { background: #f5a524; }
.battery-badge.batt-critical { background: var(--danger); }

/* ---- Member detail card (Map tab, drilled into one person) ---- */
.member-detail-back {
  position: absolute;
  top: 14px; left: 14px;
  z-index: 1200; /* above Leaflet's own controls (z-index: 1000) */
  width: 44px; height: 44px;
  font-size: 22px;
  background: var(--card);
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}
.member-detail-back[hidden] { display: none; }
.member-detail {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -6px 24px rgba(0,0,0,.18);
  z-index: 20;
  padding: 18px 20px calc(18px + env(safe-area-inset-bottom));
}
.member-detail[hidden] { display: none; }
.member-detail-name { margin: 0 0 6px; font-size: 26px; }
.member-detail-status { margin: 0; font-size: 17px; font-weight: 700; }
.member-detail-live {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  margin: 8px 0 0;
}
.live-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--accent-2); display: inline-block; }

/* ---- SOS button (Map tab only) ---- */
#sos-btn {
  position: absolute;
  right: 14px; bottom: 14px;
  z-index: 25;
  width: 56px; height: 56px;
  border-radius: 999px;
  border: 3px solid white;
  background: var(--danger);
  color: white;
  font-weight: 800;
  font-size: 13px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.35);
}
#sos-btn[hidden] { display: none; }

/* ---- Chat ---- */
#panel-chat { display: flex; flex-direction: column; }
#panel-chat h2 { flex-shrink: 0; }
#chat-list { flex: 1; overflow-y: auto; min-height: 0; }
.chat-form { flex-shrink: 0; margin-top: 10px; }
.list li.chat-msg .row { justify-content: flex-start; gap: 6px; }
.list li.chat-msg .sender { font-weight: 700; }

/* ---- Status badges (stale / paused) on member entries ---- */
.badge-stale { color: var(--danger); font-weight: 600; }
.badge-paused { color: var(--muted); font-style: italic; }

.list li.activity-sos {
  background: #fdeceb;
  border-color: var(--danger);
}

.member-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  font-weight: 700;
  font-size: 13px;
  border: 2px solid white;
  box-shadow: 0 2px 8px rgba(0,0,0,.3);
}
/* Sharing looks stopped (no ping in a while) — dim + amber ring instead of
   the normal accent color, so it reads differently from "up to date" at a glance. */
.member-badge-stale {
  background: #9a9fb3;
  border-color: #f5a524;
}
/* Deliberately paused (not an error state) — neutral grey, no amber warning ring. */
.member-badge-paused {
  background: #9a9fb3;
}
.member-badge img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }

.my-avatar-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.my-avatar-section .member-badge { width: 48px; height: 48px; font-size: 16px; }
.leaflet-popup-content { font-size: 13px; }

/* ---- Photo crop modal ---- */
.crop-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 20px;
}
.crop-modal[hidden] { display: none; }
.crop-viewport {
  width: 240px; height: 240px;
  border-radius: 50%;
  overflow: hidden;
  position: relative;
  background: #111;
  touch-action: none;
  box-shadow: 0 0 0 4px rgba(255,255,255,.35);
}
.crop-viewport img {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  user-select: none;
}
#crop-zoom { width: 240px; }
.crop-actions { display: flex; gap: 14px; }
.crop-actions button { padding: 10px 22px; }
.crop-actions .link-btn { width: auto; margin: 0; color: white; }
.map-layer-toggle {
  padding: 8px 14px;
  border-radius: 999px;
  border: none;
  background: var(--card);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}

/* ---- Live location request (member detail card) ---- */
.member-detail-request {
  margin-top: 12px;
  width: 100%;
  padding: 11px 14px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.member-detail-request:disabled { opacity: .55; cursor: default; }
.member-detail-request-status { margin: 8px 0 0; }

/* ---- Toast (transient confirmations) ---- */
#toast-host {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 60;
  pointer-events: none;
}
.toast {
  max-width: 84%;
  background: rgba(24,26,42,.95);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .25s ease, transform .25s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* ---- 1.03: quick actions, digest, drives ---- */
.quick-actions { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
.qa-btn {
  padding: 10px 14px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}
.qa-btn:disabled { opacity: .55; cursor: default; }
.onmyway-row { display: flex; gap: 8px; }
.onmyway-row select { flex: 1; min-width: 0; }
.onmyway-row .qa-btn { white-space: nowrap; }
.precision-label { display: flex; align-items: center; gap: 8px; }
.precision-label select { flex: 1; }
.secondary-btn {
  width: 100%; margin: 10px 0 0; padding: 10px 14px; border: 1px solid var(--accent);
  border-radius: 10px; background: transparent; color: var(--accent); font-size: 14px;
  font-weight: 600; cursor: pointer;
}
.digest-box, .member-detail-drives { margin-top: 10px; }
.digest-box h3 { margin: 4px 0 8px; }
.digest-row, .drive-row {
  padding: 8px 10px; margin-bottom: 6px; background: var(--card);
  border-radius: 10px; font-size: 13px;
}
.digest-row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.drive-row .meta, .digest-row .meta { color: var(--muted); font-size: 12px; }

/* ---- 1.04: check-in (map), trip stats, who's-home, scrubber ---- */
.checkin-map-btn {
  position: absolute;
  right: 14px; bottom: 80px;   /* sits just above the round SOS button */
  z-index: 25;
  padding: 9px 14px;
  border-radius: 999px;
  border: 2px solid #fff;
  background: var(--accent-2);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.3);
}
.checkin-map-btn[hidden] { display: none; }
.member-detail-trip-stats { margin: 6px 0 0; font-weight: 600; color: var(--ink); }
.member-detail-actions { display: flex; gap: 16px; margin-top: 8px; }
.member-detail-actions .link-btn { width: auto; margin: 0; }
.place-here { margin-top: 2px; color: var(--accent-2); font-weight: 600; }
.member-detail-scrubber { margin-top: 10px; }
#scrubber-range { width: 100%; }
.scrubber-readout { text-align: center; font-size: 13px; font-weight: 600; color: var(--muted); margin-top: 2px; }
