:root {
  --bg: #f2f2f7;
  --panel: rgba(255, 255, 255, 0.72);
  --panel-solid: #ffffff;
  --text: #1c1c1e;
  --text-dim: #6b6b70;
  --hair: rgba(0, 0, 0, 0.08);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.16);
  --accent: #0a84ff;
  --danger: #ff375f;
  --live: #30d158;
  --radius: 22px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: rgba(28, 28, 30, 0.72);
    --panel-solid: #1c1c1e;
    --text: #f5f5f7;
    --text-dim: #9a9aa0;
    --hair: rgba(255, 255, 255, 0.12);
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
}
body { position: fixed; inset: 0; overflow: hidden; }
#map { position: absolute; inset: 0; }

button { font: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: absolute; z-index: 5;
  top: calc(var(--safe-t) + 12px);
  left: calc(var(--safe-l) + 12px);
  right: calc(var(--safe-r) + 12px);
  display: flex; align-items: center; justify-content: space-between;
  pointer-events: none;
}
.brand, .top-actions { pointer-events: auto; }
.brand {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border: 1px solid var(--hair);
  border-radius: 18px; padding: 8px 14px 8px 12px;
  box-shadow: var(--shadow);
}
.brand-mark { width: 28px; height: 28px; border-radius: 8px; display: block; }
.brand-mark.big { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 6px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-size: 16px; letter-spacing: 0.2px; }
.brand-text small { font-size: 11px; color: var(--text-dim); }
.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center; color: var(--text);
  background: var(--panel);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border: 1px solid var(--hair);
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, background 0.2s ease;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn.spin svg { animation: spin 0.7s ease; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Live bar ---------- */
.livebar {
  position: absolute; z-index: 6;
  top: calc(var(--safe-t) + 66px);
  left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  background: var(--live);
  color: #032d10;
  padding: 8px 8px 8px 14px; border-radius: 999px;
  box-shadow: var(--shadow); font-size: 13px; font-weight: 600;
  animation: drop 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
@keyframes drop { from { opacity: 0; transform: translate(-50%, -14px); } }
.livedot { width: 9px; height: 9px; border-radius: 50%; background: #032d10; animation: pulse 1.4s infinite; }
.livebar-stop { background: rgba(0,0,0,0.18); color: #032d10; padding: 5px 12px; border-radius: 999px; font-weight: 700; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ---------- Bottom sheet ---------- */
.sheet {
  position: absolute; z-index: 5; left: 0; right: 0; bottom: 0;
  background: var(--panel);
  -webkit-backdrop-filter: saturate(160%) blur(16px);
  backdrop-filter: saturate(160%) blur(16px);
  border-top-left-radius: var(--radius); border-top-right-radius: var(--radius);
  border-top: 1px solid var(--hair);
  box-shadow: 0 -8px 40px rgba(0,0,0,0.18);
  padding: 0 16px calc(var(--safe-b) + 12px);
  transition: transform 0.42s cubic-bezier(0.22, 0.9, 0.3, 1);
  will-change: transform;
  max-height: 82vh; display: flex; flex-direction: column;
}
.sheet[data-state="peek"] { transform: translateY(calc(100% - 148px)); }
.sheet[data-state="open"] { transform: translateY(0); }
.sheet-grabber { padding: 10px 0 4px; display: grid; place-items: center; cursor: grab; touch-action: none; }
.sheet-grabber span { width: 38px; height: 5px; border-radius: 3px; background: var(--text-dim); opacity: 0.5; }
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 2px 4px 12px; }
.sheet-head h2 { margin: 0; font-size: 22px; letter-spacing: 0.3px; }
.pill { font-size: 13px; font-weight: 600; color: var(--text-dim); background: var(--hair); padding: 2px 10px; border-radius: 999px; }
.friend-list { overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding-bottom: 8px; }

.friend {
  display: flex; align-items: center; gap: 13px;
  padding: 11px 6px; border-radius: 16px;
  transition: background 0.15s ease, transform 0.15s ease;
  cursor: pointer;
}
.friend:active { transform: scale(0.985); background: var(--hair); }
.friend + .friend { border-top: 1px solid var(--hair); }
.avatar {
  width: 46px; height: 46px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-size: 22px;
  background: var(--panel-solid);
  background-size: cover; background-position: center;
  box-shadow: 0 0 0 3px var(--ring, var(--accent)), 0 3px 10px rgba(0,0,0,0.18);
}
.friend-info { flex: 1; min-width: 0; }
.friend-name { font-size: 16px; font-weight: 600; display: flex; align-items: center; gap: 7px; }
.friend-sub { font-size: 13px; color: var(--text-dim); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.friend-dist { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; flex: none; }
.badge-live { font-size: 11px; font-weight: 700; color: var(--live); border: 1px solid var(--live); border-radius: 999px; padding: 0 7px; letter-spacing: 0.4px; }
.empty { text-align: center; color: var(--text-dim); padding: 26px 10px 30px; font-size: 14px; line-height: 1.5; }

/* ---------- FAB ---------- */
.fab {
  position: absolute; z-index: 6;
  right: calc(var(--safe-r) + 16px);
  bottom: calc(var(--safe-b) + 168px);
  display: flex; align-items: center; gap: 9px;
  background: var(--accent); color: #fff;
  padding: 13px 18px; border-radius: 999px; font-weight: 600; font-size: 15px;
  box-shadow: 0 8px 26px rgba(10,132,255,0.45);
  transition: transform 0.16s ease, box-shadow 0.2s ease;
}
.fab:active { transform: scale(0.95); }

/* ---------- Modal ---------- */
.modal, .gate {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: flex-end; justify-content: center;
  /* Plain scrim (no backdrop-filter): blurring the live WebGL map behind is
     expensive on phones and was the main cause of the janky feel. */
  background: rgba(0,0,0,0.5);
  animation: fade 0.2s ease;
}
.gate { align-items: center; }
@keyframes fade { from { opacity: 0; } }
.modal-card {
  width: 100%; max-width: 520px;
  background: var(--panel-solid);
  border-top-left-radius: 26px; border-top-right-radius: 26px;
  padding: 8px 20px calc(var(--safe-b) + 22px);
  box-shadow: var(--shadow);
  animation: slideup 0.34s cubic-bezier(0.22, 0.9, 0.3, 1);
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
/* keep the drag handle from scrolling away */
.modal-grabber { position: sticky; top: 0; background: var(--panel-solid); z-index: 1; }
@keyframes slideup { from { transform: translateY(100%); } }
.modal-grabber { display: grid; place-items: center; padding: 8px 0 6px; }
.modal-grabber span { width: 38px; height: 5px; border-radius: 3px; background: var(--text-dim); opacity: 0.4; }
.modal-card h3 { margin: 6px 2px 16px; font-size: 20px; }
.avatar-edit { display: flex; align-items: center; gap: 16px; margin-bottom: 18px; }
.avatar-preview {
  width: 66px; height: 66px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-size: 32px;
  background: var(--bg); background-size: cover; background-position: center;
  box-shadow: 0 0 0 3px var(--accent);
}
.avatar-edit-actions { display: flex; flex-direction: column; gap: 8px; }
.btn.small { flex: none; padding: 9px 16px; font-size: 14px; border-radius: 11px; }

.field { display: block; margin-bottom: 15px; }
.field > span { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 7px; }
.field input {
  width: 100%; padding: 13px 14px; font-size: 16px;
  border-radius: 13px; border: 1px solid var(--hair);
  background: var(--bg); color: var(--text);
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.chips { display: flex; flex-wrap: wrap; gap: 9px; }
.chip {
  width: 44px; height: 44px; border-radius: 13px; font-size: 22px;
  display: grid; place-items: center; background: var(--bg);
  border: 2px solid transparent; transition: transform 0.12s ease;
}
.chip:active { transform: scale(0.9); }
.chip.is-active { border-color: var(--accent); }
.chips.colors .chip { font-size: 0; }
.color-dot { width: 22px; height: 22px; border-radius: 50%; }
.seg {
  display: flex; gap: 4px; background: var(--bg); border-radius: 13px; padding: 4px; margin: 4px 0 8px;
  border: 1px solid var(--hair);
}
.seg-btn { flex: 1; padding: 10px; border-radius: 10px; font-weight: 600; font-size: 15px; color: var(--text-dim); transition: all 0.18s ease; }
.seg-btn.is-active { background: var(--panel-solid); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,0.14); }
.hint { font-size: 13px; color: var(--text-dim); margin: 0 2px 16px; line-height: 1.45; }
.modal-actions { display: flex; gap: 10px; }
.btn { flex: 1; padding: 14px; border-radius: 14px; font-weight: 600; font-size: 16px; transition: transform 0.14s ease, opacity 0.2s; }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: var(--bg); color: var(--text); border: 1px solid var(--hair); }
.btn.block { width: 100%; margin-top: 6px; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }

/* ---------- Friend detail (Locket-style) ---------- */
.detail-card {
  width: 100%; max-width: 480px;
  background: var(--panel-solid);
  border-top-left-radius: 26px; border-top-right-radius: 26px;
  padding-bottom: calc(var(--safe-b) + 20px);
  box-shadow: var(--shadow);
  animation: slideup 0.34s cubic-bezier(0.22, 0.9, 0.3, 1);
  max-height: 94vh; max-height: 94dvh;
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.detail-card .modal-grabber {
  position: sticky; top: 0; z-index: 2;
  background: var(--panel-solid);
  border-top-left-radius: 26px; border-top-right-radius: 26px;
}
.detail-photo {
  width: 100%; aspect-ratio: 1 / 1;
  background-color: var(--bg);
  background-size: cover; background-position: center;
  display: grid; place-items: center;
  font-size: 96px;
}
.detail-photo.is-emoji { background: var(--pin, var(--accent)); }
.detail-body { padding: 16px 20px 4px; }
.detail-name { font-size: 22px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.detail-note { font-size: 16px; color: var(--text); margin-top: 8px; line-height: 1.45; }
.detail-meta { margin-top: 14px; display: flex; flex-direction: column; gap: 9px; }
.detail-row { display: flex; align-items: center; gap: 10px; color: var(--text-dim); font-size: 14px; }
.detail-row svg { flex: none; opacity: 0.8; }
.detail-actions { display: flex; gap: 10px; margin-top: 18px; }

/* ---------- Gate ---------- */
.gate-card {
  width: calc(100% - 40px); max-width: 380px; text-align: center;
  background: var(--panel-solid); border-radius: 26px; padding: 30px 24px;
  box-shadow: var(--shadow);
}
.gate-card h3 { margin: 8px 0 6px; font-size: 24px; }
.gate-card p { color: var(--text-dim); font-size: 14px; margin: 0 0 20px; line-height: 1.5; }
.gate-card input { width: 100%; padding: 14px; font-size: 16px; text-align: center; border-radius: 13px; border: 1px solid var(--hair); background: var(--bg); color: var(--text); margin-bottom: 12px; }
.gate-err { color: var(--danger); }

/* ---------- Toast ---------- */
.toast {
  position: absolute; z-index: 30; left: 50%;
  bottom: calc(var(--safe-b) + 200px); transform: translateX(-50%);
  background: rgba(0,0,0,0.86); color: #fff; padding: 11px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 500; box-shadow: var(--shadow);
  animation: toastin 0.25s ease;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ---------- Map markers ---------- */
.k-pin { position: relative; width: 46px; height: 56px; cursor: pointer; will-change: transform; }
.k-pin-halo {
  position: absolute; left: 50%; top: 20px; transform: translate(-50%, -50%);
  width: 46px; height: 46px; border-radius: 50%;
  background: var(--pin, #0a84ff); opacity: 0;
}
.k-pin.live .k-pin-halo { animation: halo 2s infinite; }
@keyframes halo { 0% { opacity: 0.45; transform: translate(-50%,-50%) scale(0.7); } 70%,100% { opacity: 0; transform: translate(-50%,-50%) scale(2.4); } }
.k-pin-bubble {
  position: absolute; left: 50%; top: 20px; transform: translate(-50%, -50%);
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center; font-size: 21px;
  background: #fff;
  background-size: cover; background-position: center;
  box-shadow: 0 0 0 3px var(--pin, #0a84ff), 0 4px 12px rgba(0,0,0,0.35);
  animation: popin 0.34s cubic-bezier(0.2, 0.9, 0.3, 1.4);
}
@keyframes popin { from { transform: translate(-50%,-50%) scale(0); } }
.k-pin-tip {
  position: absolute; left: 50%; top: 38px; transform: translateX(-50%);
  width: 0; height: 0; border-left: 6px solid transparent; border-right: 6px solid transparent;
  border-top: 9px solid var(--pin, #0a84ff);
}
.k-pin-label {
  position: absolute; left: 50%; top: 44px; transform: translateX(-50%);
  font-size: 11px; font-weight: 700; white-space: nowrap;
  background: var(--panel-solid); color: var(--text);
  padding: 2px 8px; border-radius: 999px; box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
/* my own location dot */
.me-dot { width: 20px; height: 20px; border-radius: 50%; background: #0a84ff; box-shadow: 0 0 0 4px rgba(10,132,255,0.3), 0 0 0 2px #fff inset; }
.me-dot::after { content: ""; position: absolute; inset: -8px; border-radius: 50%; background: rgba(10,132,255,0.25); animation: halo 2.2s infinite; }

.maplibregl-ctrl-attrib { font-size: 10px; }
