:root {
  --bg: #0B1220;
  --surface: #131C2E;
  --surface-raised: #1A2438;
  --border: #26324A;
  --border-soft: #1E2942;
  --text: #E8ECF4;
  --text-muted: #8592AC;
  --text-faint: #5A6685;

  --live: #2DD4A7;
  --live-dim: rgba(45, 212, 167, 0.16);
  --alert: #FF4757;
  --alert-dim: rgba(255, 71, 87, 0.18);
  --trail: #F2B84B;
  --you: #3B82F6;
  --you-dim: rgba(59, 130, 246, 0.18);
  --route: #6EA8FF;
  --route-fallback: #5A6685;

  --font-sans: 'Manrope', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, monospace;

  --sheet-w: 320px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  overflow: hidden;
}

#app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: 1fr var(--sheet-w);
  grid-template-areas:
    "topbar topbar"
    "map    sheet";
}

#app[hidden],
#landing[hidden] {
  display: none;
}

/* ---------- Top bar ---------- */

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--live);
  box-shadow: 0 0 0 3px var(--live-dim);
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.device-name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.01em;
}

.device-id {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--live-dim);
  border: 1px solid rgba(45, 212, 167, 0.35);
  font-size: 12px;
  font-weight: 600;
  color: var(--live);
  transition: background 0.25s, border-color 0.25s, color 0.25s;
}

.status-pill[data-state="sos"] {
  background: var(--alert-dim);
  border-color: rgba(255, 71, 87, 0.5);
  color: var(--alert);
  animation: pillPulse 1.6s ease-in-out infinite;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

@keyframes pillPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 71, 87, 0.35); }
  50% { box-shadow: 0 0 0 6px rgba(255, 71, 87, 0); }
}

/* ---------- Map ---------- */

.map-wrap {
  grid-area: map;
  position: relative;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
  background: var(--bg);
}

/* Dark basemap tuning */
.leaflet-container {
  background: var(--bg);
  font-family: var(--font-sans);
}

.leaflet-control-attribution {
  background: rgba(11, 18, 32, 0.75) !important;
  color: var(--text-faint) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--text-muted) !important; }

.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--surface-raised) !important; }

/* Device marker */
.device-marker {
  position: relative;
  width: 22px;
  height: 22px;
}

.device-marker .ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--live);
  opacity: 0.35;
  animation: ringExpand 2.2s ease-out infinite;
}

.device-marker .ring.ring2 { animation-delay: 1.1s; }

.device-marker .core {
  position: absolute;
  top: 50%; left: 50%;
  width: 14px; height: 14px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--live);
  border: 2px solid #0B1220;
  z-index: 2;
}

.device-marker[data-sos="true"] .ring,
.device-marker[data-sos="true"] .core {
  background: var(--alert);
}

@keyframes ringExpand {
  0% { transform: scale(0.4); opacity: 0.5; }
  100% { transform: scale(2.6); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .device-marker .ring { animation: none; opacity: 0.25; }
  .status-pill[data-state="sos"] { animation: none; }
}

/* "You are here" marker — viewer's own browser location */
.my-location-marker {
  position: relative;
  width: 18px;
  height: 18px;
}

.my-location-marker .pulse {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--you);
  opacity: 0.35;
  animation: ringExpand 2.2s ease-out infinite;
}

.my-location-marker .dot {
  position: absolute;
  top: 50%; left: 50%;
  width: 12px; height: 12px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--you);
  border: 2px solid #0B1220;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.35);
  z-index: 2;
}

/* Location permission prompt */
.location-prompt {
  position: absolute;
  left: 14px;
  bottom: 20px;
  z-index: 15;
  max-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

.location-prompt .btn {
  padding: 8px 12px;
  font-size: 12px;
}

/* Recenter button */
.recenter-btn {
  position: absolute;
  right: 14px;
  bottom: 20px;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transform: translateY(6px);
  transition: opacity 0.2s, transform 0.2s;
}

.recenter-btn.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.recenter-btn:hover { background: var(--surface-raised); }

.connection-toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
}

/* ---------- Status sheet ---------- */

.sheet {
  grid-area: sheet;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  z-index: 20;
}

.sheet-handle {
  display: none;
}

.sheet-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.metric-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.metric {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 14px;
  background: var(--surface-raised);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
}

.metric-wide { grid-column: 1 / -1; }

.metric-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  font-weight: 600;
}

.metric-value-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.metric-value {
  font-size: 15px;
  font-weight: 500;
}

.mono { font-family: var(--font-mono); }

#lastUpdate, #coordsValue { font-size: 13px; color: var(--text); }

/* Battery icon */
.battery-icon {
  position: relative;
  width: 22px;
  height: 11px;
  border: 1.5px solid var(--text-muted);
  border-radius: 2px;
  padding: 1.5px;
  display: inline-block;
}
.battery-icon::after {
  content: "";
  position: absolute;
  right: -3px;
  top: 3px;
  width: 2px;
  height: 5px;
  background: var(--text-muted);
  border-radius: 0 1px 1px 0;
}
.battery-fill {
  display: block;
  height: 100%;
  width: 70%;
  background: var(--live);
  border-radius: 1px;
  transition: width 0.3s, background 0.3s;
}

/* Signal bars */
.bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 12px;
}
.bars .bar {
  width: 3px;
  background: var(--border);
  border-radius: 1px;
}
.bars .bar:nth-child(1) { height: 25%; }
.bars .bar:nth-child(2) { height: 50%; }
.bars .bar:nth-child(3) { height: 75%; }
.bars .bar:nth-child(4) { height: 100%; }
.bars .bar.on { background: var(--live); }

/* Actions */
.sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 16px;
  border-radius: 10px;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  border: 1px solid transparent;
  transition: filter 0.15s;
}

.btn:hover { filter: brightness(1.1); }

.btn-primary {
  background: var(--live);
  color: #06251C;
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}

.demo-banner {
  margin: 0;
  padding: 10px 12px;
  background: rgba(242, 184, 75, 0.1);
  border: 1px solid rgba(242, 184, 75, 0.3);
  border-radius: 8px;
  font-size: 11.5px;
  line-height: 1.5;
  color: #E8C989;
}
.demo-banner code {
  font-family: var(--font-mono);
  background: rgba(0,0,0,0.2);
  padding: 1px 4px;
  border-radius: 4px;
}

/* State banner: invalid/expired link or connection errors, shown in
   place of the old demo banner. */
.state-banner {
  margin: 0;
  padding: 10px 12px;
  background: var(--live-dim);
  border: 1px solid rgba(45, 212, 167, 0.3);
  border-radius: 8px;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--live);
}
.state-banner-error {
  background: var(--alert-dim);
  border-color: rgba(255, 71, 87, 0.4);
  color: var(--alert);
}

/* ---------- Landing page ("/") ---------- */

#landing {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.landing-card {
  max-width: 420px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.landing-card .brand-mark {
  width: 14px;
  height: 14px;
}

.landing-card h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
}

.landing-card p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13.5px;
  line-height: 1.6;
}

/* ---------- Responsive: bottom sheet on mobile ---------- */

@media (max-width: 720px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "topbar"
      "map"
      "sheet";
  }

  .sheet {
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: 40dvh;
    display: flex;
    flex-direction: column;
  }

  .sheet.collapsed { max-height: 56px; }
  .sheet.collapsed .sheet-body { display: none; }

  .sheet-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 10px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-family: var(--font-sans);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
  }

  .sheet-grip {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--border);
  }

  .sheet-body {
    overflow-y: auto;
    padding: 4px 16px 20px;
  }
}
