:root {
  --bg: #0d0d0d;
  --surface: #1a1a2e;
  --surface2: #16213e;
  --accent: #e94560;
  --accent2: #0f3460;
  --text: #eaeaea;
  --text-muted: #888;
  --border: #2a2a4a;
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#app { height: 100%; display: flex; flex-direction: column; position: relative; }

/* ── Views ── */
.view { display: none; flex: 1; flex-direction: column; overflow: hidden; position: relative; }
.view.active { display: flex; }

/* ── Auth ── */
.auth-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 16px;
}

.auth-logo { font-size: 2.4rem; font-weight: 800; color: var(--accent); letter-spacing: -1px; }
.auth-logo span { color: var(--text); }
.auth-sub { color: var(--text-muted); font-size: 0.9rem; text-align: center; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card h2 { font-size: 1.1rem; font-weight: 700; }

label { font-size: 0.82rem; color: var(--text-muted); display: block; margin-bottom: 4px; }

input[type=text], input[type=email], input[type=tel], input[type=password], input[type=number], textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1rem;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.2s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
input:focus, textarea:focus { border-color: var(--accent); }

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  text-decoration: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  position: relative;
  z-index: 1;
}
.btn:active { opacity: 0.75; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); font-size: 0.85rem; }
.btn:disabled { opacity: 0.5; pointer-events: none; }
.btn-full { width: 100%; }

.link-btn { background: none; border: none; color: var(--accent); font-size: 0.85rem; cursor: pointer; padding: 8px 4px; touch-action: manipulation; -webkit-tap-highlight-color: rgba(0,0,0,0); position: relative; z-index: 1; }

.error-msg { color: var(--accent); font-size: 0.85rem; min-height: 1.2em; }
.success-msg { color: #4caf50; font-size: 0.85rem; }

/* ── Bottom Nav ── */
.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 4px;
  background: none;
  border: none;
  color: var(--text);
  font-size: 0.65rem;
  gap: 3px;
  cursor: pointer;
  transition: color 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--accent); }

/* ── AR View ── */
#view-ar { position: relative; background: #000; }
#ar-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
#ar-overlay { position: absolute; inset: 0; pointer-events: none; z-index: 5; }

#ar-start-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  z-index: 30;
}
#ar-start-overlay[hidden] { display: none; }
#ar-start-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 16px 32px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.ar-marker {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  pointer-events: auto;
  cursor: pointer;
}
.ar-marker-icon {
  border-radius: 50%;
  border: 2px solid #fff;
  background: var(--surface);
  object-fit: cover;
  box-shadow: 0 2px 12px rgba(0,0,0,0.6);
}
.ar-marker-label {
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
  border-radius: 20px;
  padding: 3px 8px;
  font-size: 0.72rem;
  color: #fff;
  white-space: nowrap;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
.ar-edge-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,0.55);
  border-radius: 50%;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-size: 1.1rem;
}

.ar-controls {
  position: absolute;
  bottom: 20px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 20;
  pointer-events: auto;
}
.ar-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  border: 1px solid rgba(255,255,255,0.2);
  color: #fff;
  font-size: 1.2rem;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: rgba(255,255,255,0.2);
}

.ar-status {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.78rem;
  color: #fff;
  z-index: 20;
  pointer-events: none;
  white-space: nowrap;
}

/* ── Radar View ── */
#radar-canvas { display: block; margin: auto; }

/* ── Friends List ── */
.list-view { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 16px; display: flex; flex-direction: column; gap: 10px; -webkit-overflow-scrolling: touch; }
.list-header { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 8px; }
.list-header h2 { font-size: 1.1rem; font-weight: 700; }

.friend-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: background 0.15s;
}
.friend-card:active { background: var(--surface2); }

.friend-avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface2);
  flex-shrink: 0;
}
.friend-info { flex: 1; min-width: 0; }
.friend-name { font-weight: 600; font-size: 0.95rem; }
.friend-status { font-size: 0.78rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.friend-dist { font-size: 0.78rem; color: var(--accent); font-weight: 600; white-space: nowrap; }

.badge-invisible { font-size: 0.7rem; background: var(--surface2); border-radius: 10px; padding: 2px 7px; color: var(--text-muted); }

/* ── Chat ── */
#view-chat { background: var(--bg); }
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.chat-header-back { background: none; border: none; color: var(--text); font-size: 1.3rem; cursor: pointer; padding: 4px; }
.chat-header-name { font-weight: 700; font-size: 1rem; flex: 1; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.msg-bubble {
  max-width: 75%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.4;
  word-break: break-word;
}
.msg-bubble.mine { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg-bubble.theirs { align-self: flex-start; background: var(--surface); border-bottom-left-radius: 4px; }
.msg-bubble img { max-width: 200px; border-radius: 8px; display: block; margin-top: 4px; }
.msg-time { font-size: 0.65rem; opacity: 0.6; margin-top: 3px; }

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.chat-input-bar textarea {
  flex: 1;
  resize: none;
  max-height: 100px;
  border-radius: 20px;
  padding: 9px 14px;
  font-size: 0.9rem;
  line-height: 1.4;
}
.chat-icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.1rem;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
.chat-send-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: 1.1rem;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}

/* ── Profile / Settings ── */
.settings-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 16px; padding-bottom: 32px; display: flex; flex-direction: column; gap: 16px; -webkit-overflow-scrolling: touch; }
.settings-section { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.settings-section-title { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; padding: 14px 16px 8px; }
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-top: 1px solid var(--border);
  gap: 12px;
  touch-action: manipulation;
  min-height: 56px;
}
.settings-row:first-of-type { border-top: none; }
.settings-row-label { font-size: 0.9rem; }
.settings-row-value { font-size: 0.85rem; color: var(--text-muted); }

/* Toggle switch */
.toggle {
  position: relative;
  width: 52px;
  height: 32px;
  flex-shrink: 0;
  touch-action: manipulation;
}
.toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  margin: 0;
  cursor: pointer;
  z-index: 2;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 16px;
  transition: background 0.2s;
  pointer-events: none;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 24px; height: 24px;
  left: 4px; top: 4px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  pointer-events: none;
}
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider::before { transform: translateX(20px); }

/* Avatar grid */
.avatar-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; padding: 12px; }
.avatar-option {
  aspect-ratio: 1;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.8rem;
  transition: border-color 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
.avatar-option.selected { border-color: var(--accent); }

/* ── Modal backdrop ── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal-backdrop:not([hidden]) { display: flex; }

/* ── Action Sheet ── */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  display: none;
  align-items: flex-end;
}
.sheet-backdrop.open { display: flex; }
.action-sheet {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sheet-title { font-size: 0.8rem; color: var(--text-muted); text-align: center; padding-bottom: 8px; }
.sheet-btn {
  padding: 14px;
  border-radius: var(--radius-sm);
  background: var(--surface2);
  border: none;
  color: var(--text);
  font-size: 0.95rem;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  width: 100%;
}
.sheet-btn.danger { color: var(--accent); }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 0.85rem;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  z-index: 200;
  pointer-events: none;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Invite landing ── */
.invite-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 20px;
  text-align: center;
}
.invite-icon { font-size: 4rem; }
.invite-title { font-size: 1.3rem; font-weight: 700; }
.invite-sub { color: var(--text-muted); font-size: 0.9rem; }

/* ── Spinner ── */
.spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Misc ── */
.empty-state { text-align: center; color: var(--text-muted); padding: 40px 20px; font-size: 0.9rem; }
.divider { height: 1px; background: var(--border); }
