* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #05070d;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: #e8ecf4;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#scene {
  display: block;
  width: 100vw;
  height: 100vh;
  touch-action: none; /* let us handle all gestures */
}

#loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  letter-spacing: 0.08em;
  color: #6b7688;
  pointer-events: none;
  transition: opacity 0.5s;
}
#loading.hidden { opacity: 0; }

/* ---- prev/next location controls ---- */
#nav {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 20;
}
#nav button {
  width: 52px;
  height: 44px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  background: rgba(14,18,27,0.9);
  color: #cfe8ff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}
#nav button:active { background: rgba(60,80,110,0.7); }

/* ---- recency sidebar (all friends by last contact) ---- */
#side-toggle {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 16px;
  width: 52px;
  height: 44px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  background: rgba(14,18,27,0.9);
  color: #cfe8ff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  z-index: 36;
}
#side-toggle:active { background: rgba(60,80,110,0.7); }

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(272px, 80vw);
  z-index: 32;
  display: flex;
  flex-direction: column;
  padding: max(14px, env(safe-area-inset-top)) 14px 74px; /* bottom clears ☰ */
  background: #0b0f16; /* fully opaque: the panel sits behind and ghosts through alpha */
  border-right: 1px solid rgba(255,255,255,0.08);
  transition: transform 0.3s cubic-bezier(.2,.8,.2,1), opacity 0.24s;
}
#sidebar.hidden { transform: translateX(-105%); opacity: 0; pointer-events: none; }

#side-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 2px 4px 10px;
}
#side-title { font-size: 16px; font-weight: 600; }
#side-sorts { display: flex; align-items: baseline; gap: 2px; }
#side-sort, #side-dir, #side-all {
  border: none;
  background: none;
  color: #9aa6bd;
  font-size: 12px;
  cursor: pointer;
  padding: 4px 2px;
}
#side-dir, #side-all { font-size: 10px; }
#side-all.hidden { display: none; }
#side-sort:hover, #side-dir:hover, #side-all:hover { color: #cfe8ff; }

#side-search {
  width: 100%;
  margin-bottom: 8px;
  padding: 7px 10px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 9px;
  background: rgba(255,255,255,0.05);
  color: #e8ecf4;
  font-size: 13px;
}
#side-search:focus {
  outline: none;
  border-color: rgba(120,170,255,0.55);
}
#side-search::placeholder { color: #6b7688; }

#side-list {
  flex: 1;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

#side-backup {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 8px 4px 0;
  border-top: 1px solid rgba(255,255,255,0.08);
  font-size: 11px;
  color: #6b7688;
}
#side-backup button {
  border: none;
  background: none;
  padding: 2px 0;
  color: #6b7688;
  font-size: 11px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
#side-backup button:hover { color: #9aa6bd; }
#side-backup a {
  color: #6b7688;
  text-decoration: underline;
  text-underline-offset: 2px;
}
#side-backup a:hover { color: #9aa6bd; }
#side-backup-status {
  margin-left: auto;
  text-align: right;
  color: #9aa6bd;
}
#side-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 8px 4px;
  border-top: 1px solid rgba(255,255,255,0.06);
  font-size: 14px;
  cursor: pointer;
}
#side-list li:hover { background: rgba(255,255,255,0.04); }
/* fold/collapse section headers (name/city sorts) */
#side-list li.sec {
  font-size: 12px;
  font-weight: 600;
  color: #9aa6bd;
  letter-spacing: 0.04em;
  padding: 10px 4px 6px;
  border-top: none;
}
#side-list li.sec .chev { font-size: 10px; color: #6b7688; }
#side-list li .who { display: flex; flex-direction: column; }
#side-list li .sub { font-size: 11px; color: #6b7688; }
#side-list li .days {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* dev HUD (?hud) */
#hud {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: 10px;
  z-index: 40;
  font: 11px/1.4 ui-monospace, monospace;
  color: #7dffb0;
  background: rgba(0,0,0,0.5);
  padding: 3px 7px;
  border-radius: 6px;
  pointer-events: none;
}

/* ---- detail panel ---- */
/* NOTE: no backdrop-filter here — blurring an animating WebGL canvas forces a
   recomposite every frame and visibly janks mid-range phones. Solid bg instead. */
#panel {
  position: fixed;
  z-index: 30;
  background: rgba(14, 18, 27, 0.96);
  border: 1px solid rgba(255,255,255,0.08);
  transition: transform 0.32s cubic-bezier(.2,.8,.2,1), opacity 0.24s;
}

/* landscape (desktop & phones held sideways): side panel on the right.
   Orientation-based (not width-based) so no phone falls through the cracks. */
@media (orientation: landscape) {
  #panel {
    top: 50%;
    right: 24px;
    transform: translateY(-50%);
    width: 300px;
    padding: 24px;
    border-radius: 16px;
  }
  #panel.hidden { opacity: 0; transform: translateY(-50%) translateX(20px); pointer-events: none; }
}

/* portrait: card in the top zone — the globe sits low, so the sky above the
   focused pin is reserved for info (not a bottom sheet) */
@media (orientation: portrait) {
  #panel {
    left: 12px;
    right: 12px;
    top: calc(12px + env(safe-area-inset-top));
    padding: 16px 18px;
    border-radius: 16px;
  }
  #panel.hidden { transform: translateY(-140%); opacity: 0; pointer-events: none; }
}

#panel-close {
  position: absolute;
  top: 12px;
  right: 14px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  color: #e8ecf4;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
#panel-back {
  position: absolute;
  top: 12px;
  right: 54px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  color: #e8ecf4;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
#panel-back.hidden { display: none; }

#panel-name { font-size: 22px; font-weight: 600; margin-bottom: 4px; padding-right: 76px; }
#panel-city { font-size: 15px; color: #9aa6bd; }
#panel-days { font-size: 14px; font-weight: 500; margin-top: 14px; }
#panel-days.hidden { display: none; }

/* ---- detail actions: handle deep-links + mark-as-contacted ---- */
#panel-handles {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}
#panel-handles.hidden { display: none; }
#panel-handles .handle {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.10);
  color: #e8ecf4;
  text-decoration: none;
  cursor: pointer;
}
#panel-handles .handle svg { width: 18px; height: 18px; display: block; }
#panel-handles .handle:hover { background: rgba(255,255,255,0.14); }

/* ---- notes: folded behind a small toggle, expands to a scrollable block ---- */
#panel-notes-btn {
  display: block;
  margin-top: 10px;
  padding: 4px 0;
  border: none;
  background: none;
  color: #9aa6bd;
  font-size: 13px;
  cursor: pointer;
}
#panel-notes-btn:hover { color: #cfe0ff; }
#panel-notes-btn.hidden { display: none; }
#panel-notes {
  margin-top: 6px;
  padding: 10px 12px;
  max-height: 32vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: 10px;
  background: rgba(255,255,255,0.05);
  color: #c6cfdd;
  font-size: 13.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  -webkit-user-select: text;
  user-select: text;
}
#panel-notes.hidden { display: none; }

/* ---- contact history: same fold pattern as notes, deletable rows ---- */
#panel-history-btn {
  display: block;
  margin-top: 10px;
  padding: 4px 0;
  border: none;
  background: none;
  color: #9aa6bd;
  font-size: 13px;
  cursor: pointer;
}
#panel-history-btn:hover { color: #cfe0ff; }
#panel-history-btn.hidden { display: none; }
#panel-history {
  list-style: none;
  margin-top: 6px;
  max-height: 26vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: 10px;
  background: rgba(255,255,255,0.05);
  padding: 4px 12px;
}
#panel-history.hidden { display: none; }
#panel-history li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
  border-top: 1px solid rgba(255,255,255,0.06);
  font-size: 13px;
  color: #c6cfdd;
}
#panel-history li:first-child { border-top: none; }
#panel-history li button {
  border: none;
  background: none;
  padding: 2px 6px;
  border-radius: 6px;
  color: #6b7688;
  font-size: 14px;
  cursor: pointer;
}
#panel-history li button:hover {
  background: rgba(255,90,90,0.12);
  color: #ffb3b3;
}
#panel-contacted {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 10px 14px;
  border: 1px solid rgba(90, 220, 130, 0.4);
  border-radius: 10px;
  background: rgba(90, 220, 130, 0.14);
  color: #b8f0c8;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
#panel-contacted:hover:not(:disabled) { background: rgba(90, 220, 130, 0.24); }
#panel-contacted:disabled { opacity: 0.55; cursor: default; }
#panel-contacted.hidden { display: none; }
#panel-edit {
  display: block;
  margin-top: 8px;
  padding: 4px 0;
  border: none;
  background: none;
  color: #6b7688;
  font-size: 12.5px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}
#panel-edit:hover { color: #9aa6bd; }
#panel-edit.hidden { display: none; }
/* after tapping a handle: "did you catch up? log it" */
#panel-contacted.nudge { animation: nudge-pulse 1.6s ease-in-out infinite; }
@keyframes nudge-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(90, 220, 130, 0); }
  50%      { box-shadow: 0 0 0 5px rgba(90, 220, 130, 0.25); background: rgba(90, 220, 130, 0.28); }
}

#panel-list {
  list-style: none;
  margin-top: 10px;
  max-height: 38vh;
  overflow-y: auto;
  overscroll-behavior: contain;
}
#panel-list.hidden { display: none; }
#panel-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 9px 4px;
  border-top: 1px solid rgba(255,255,255,0.06);
  font-size: 15px;
  cursor: pointer;
}
#panel-list li:hover { background: rgba(255,255,255,0.04); }
#panel-list li .days {
  color: #9aa6bd;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
#panel-list li .who { display: flex; flex-direction: column; }
#panel-list li .sub { font-size: 12px; color: #6b7688; }

/* Portrait: compact two-column grid so a 9-friend metro doesn't swallow the
   globe. Tighter type, no row rules, half the height. */
@media (orientation: portrait) {
  #panel { padding: 14px 16px; }
  #panel-name { font-size: 18px; margin-bottom: 2px; }
  #panel-city { font-size: 13px; }
  #panel-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px 14px;
    max-height: 24vh;
    margin-top: 8px;
  }
  #panel-list li {
    border-top: none;
    padding: 5px 2px;
    font-size: 13px;
  }
  #panel-list li .sub { font-size: 11px; }
  #panel-list li .days { font-size: 11px; }
  #panel-handles { margin-top: 10px; gap: 6px; }
  #panel-handles .handle { width: 36px; height: 36px; }
  #panel-handles .handle svg { width: 16px; height: 16px; }
  #panel-contacted { margin-top: 10px; padding: 8px 12px; font-size: 13px; }
}

/* ---- add-friend button (bottom-right, same family as nav) ---- */
#add-btn {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 20;
  width: 52px;
  height: 44px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  background: rgba(14,18,27,0.9);
  color: #cfe8ff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}
#add-btn:active { background: rgba(60,80,110,0.7); }

/* ---- contact form modal ----
   Solid card, no backdrop-filter (see the panel note: blurring an animating
   WebGL canvas janks phones). */
#form-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(4, 6, 10, 0.55);
  display: grid;
  place-items: center;
  padding: 16px;
}
#form-card {
  position: relative;  /* anchors the prefill toggle + popover */
  width: min(420px, 100%);
  max-height: min(88vh, 100%);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: #10141d;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 22px;
}
#form-title { font-size: 19px; font-weight: 600; margin-bottom: 14px; }
.f-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #6b7688;
  margin: 14px 0 6px;
}
#form-card input, #form-card select, #form-card textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 9px;
  background: rgba(255,255,255,0.05);
  color: #e8ecf4;
  font-size: 15px;
}
#form-card textarea {
  font-family: inherit;
  resize: vertical;
  min-height: 72px;
}
#form-card input:focus, #form-card select:focus, #form-card textarea:focus {
  outline: none;
  border-color: rgba(120,170,255,0.55);
}
#f-status {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(90, 220, 130, 0.12);
  border: 1px solid rgba(90, 220, 130, 0.35);
  color: #b8f0c8;
  font-size: 13px;
}
#f-status.hidden { display: none; }
#toast {
  position: fixed;
  bottom: 92px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  padding: 9px 16px;
  border-radius: 10px;
  background: #171c28;
  border: 1px solid rgba(255,255,255,0.14);
  color: #e8f0fa;
  font-size: 13px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
#toast.show { opacity: 1; }
.f-suggest-wrap { position: relative; }
.f-suggest {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 5;
  list-style: none;
  background: #171c28;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 10px;
  max-height: 208px;
  overflow-y: auto;
  box-shadow: 0 10px 28px rgba(0,0,0,0.5);
}
.f-suggest.hidden { display: none; }
.f-suggest li {
  padding: 9px 12px;
  font-size: 14px;
  cursor: pointer;
}
.f-suggest li:hover { background: rgba(255,255,255,0.07); }
/* prefill sub-menu: person+ toggle in the card's top-right, anchored popover */
#f-prefill-toggle {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 50%;
  background: rgba(255,255,255,0.06);
  color: #9fb4d8;
  cursor: pointer;
}
#f-prefill-toggle svg { width: 17px; height: 17px; }
#f-prefill-toggle:hover { background: rgba(255,255,255,0.12); color: #cfe0ff; }
#f-prefill-toggle.hidden { display: none; }
#f-prefill-menu {
  position: absolute;
  top: 56px;
  right: 14px;
  z-index: 6;
  width: min(264px, calc(100% - 28px));
  padding: 12px;
  background: #171c28;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.5);
}
#f-prefill-menu.hidden { display: none; }
#f-imports-label { font-size: 12px; color: #9aa6bd; line-height: 1.45; margin-bottom: 10px; }
#f-imports-label .opt { color: #6b7688; }
#f-imports { display: flex; flex-wrap: wrap; gap: 6px; }
.f-import {
  padding: 5px 11px;
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 999px;
  background: rgba(255,255,255,0.06);
  color: #cfe0ff;
  font-size: 12.5px;
  cursor: pointer;
}
.f-import:hover:not(:disabled) { background: rgba(255,255,255,0.12); }
.f-import:disabled { opacity: 0.5; }
.f-handle-row {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
#form-card .f-handle-row select { width: 118px; flex: none; }
#form-card .f-handle-row input { flex: 1; min-width: 0; width: auto; }
.f-del {
  flex: none;
  width: 32px;
  height: 34px;
  border: none;
  border-radius: 8px;
  background: none;
  color: #6b7688;
  font-size: 16px;
  cursor: pointer;
}
.f-del:hover { background: rgba(255,255,255,0.07); }
#f-add-handle {
  margin-top: 2px;
  padding: 5px 10px;
  border: none;
  background: none;
  color: #7aa5ff;
  font-size: 13px;
  cursor: pointer;
}
#f-error {
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(255,90,90,0.12);
  border: 1px solid rgba(255,90,90,0.35);
  color: #ffb3b3;
  font-size: 13px;
}
#f-error.hidden { display: none; }
#f-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 18px;
}
.f-spacer { flex: 1; }
#f-actions button {
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(255,255,255,0.06);
  color: #e8ecf4;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
#f-save {
  background: rgba(120,170,255,0.2) !important;
  border-color: rgba(120,170,255,0.5) !important;
  color: #cfe0ff !important;
}
#f-delete {
  background: rgba(255,90,90,0.1) !important;
  border-color: rgba(255,90,90,0.35) !important;
  color: #ffb3b3 !important;
}

/* PWA install invitation (js/pwa.js) */
#install-card {
  position: fixed;
  bottom: 92px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  z-index: 45;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  width: max-content;
  max-width: min(92vw, 480px);
  padding: 12px 14px;
  border-radius: 12px;
  background: #171c28;
  border: 1px solid rgba(255,255,255,0.14);
  color: #e8f0fa;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
}
#install-card.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#install-card span { flex: 1 1 200px; }
#install-card button {
  flex-shrink: 0;
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.18);
  background: transparent;
  color: #e8f0fa;
  font-size: 13px;
  cursor: pointer;
}
#install-card #install-yes {
  background: rgba(255, 200, 98, 0.14);
  border-color: rgba(255, 200, 98, 0.4);
  color: #ffd98a;
}
