:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-hover: #334155;
  --border: #334155;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #38bdf8;
  --accent-dim: #0ea5e9;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --compass-size: min(72vw, 320px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 16px calc(12px + var(--safe-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.header {
  text-align: center;
  margin-bottom: 8px;
}

.header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.group-switcher-wrap {
  margin: 6px auto 0;
  text-align: center;
}

.group-switcher {
  padding: 5px 28px 5px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 20px;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2338bdf8' stroke-width='3'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
  font-family: inherit;
  max-width: 260px;
}

.group-switcher:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-group-info {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  margin-left: 6px;
  border-radius: 50%;
  border: 1px solid rgba(56, 189, 248, 0.25);
  background: rgba(56, 189, 248, 0.08);
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  vertical-align: middle;
}

.btn-group-info:hover {
  background: rgba(56, 189, 248, 0.18);
}

.group-info-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0.75rem 0;
}

.group-info-card {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.group-info-card.active {
  border-color: var(--accent);
  background: rgba(56, 189, 248, 0.06);
}

.group-info-card h4 {
  margin: 0 0 4px;
  font-size: 0.9rem;
  color: var(--text);
}

.group-info-card .pin-label {
  font-size: 0.8rem;
  color: var(--text-dim, #94a3b8);
}

.group-info-card .pin-value {
  font-family: monospace;
  font-weight: 600;
  color: var(--accent);
  user-select: all;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Compass */
.compass-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.compass-wrap {
  position: relative;
  width: var(--compass-size);
  height: var(--compass-size);
  overflow: visible;
}

/* Fixed — shows where you're facing (top of phone) */
.forward-marker {
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-top: 16px solid var(--danger);
  filter: drop-shadow(0 2px 6px rgba(239, 68, 68, 0.8));
  z-index: 6;
  pointer-events: none;
}

.compass-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 45%, #1e293b 0%, #0f172a 70%);
  border: 3px solid var(--border);
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.05),
    inset 0 2px 20px rgba(0, 0, 0, 0.4),
    0 8px 32px rgba(0, 0, 0, 0.3);
  transform-origin: center center;
  transition: transform 0.1s linear;
  will-change: transform;
}

.cardinal {
  position: absolute;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

.cardinal.n {
  top: 8%;
  left: 50%;
  transform: translateX(-50%);
  color: var(--danger);
  font-size: 0.9rem;
}

.cardinal.e { right: 8%; top: 50%; transform: translateY(-50%); }
.cardinal.s { bottom: 8%; left: 50%; transform: translateX(-50%); }
.cardinal.w { left: 8%; top: 50%; transform: translateY(-50%); }

.compass-ticks {
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  background: repeating-conic-gradient(
    from 0deg,
    transparent 0deg 8deg,
    rgba(148, 163, 184, 0.35) 8deg 9deg
  );
  z-index: 1;
}

/* Blue arrow overlay — rotates toward selected target */
.target-layer {
  position: absolute;
  inset: 10%;
  transform-origin: center center;
  transition: transform 0.2s ease-out;
  will-change: transform;
  pointer-events: none;
  z-index: 5;
}

.target-layer.inactive .target-arrow-path {
  fill: #475569;
  opacity: 0.5;
}

.target-arrow-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 0 8px rgba(56, 189, 248, 0.9));
}

.target-arrow-path {
  fill: #38bdf8;
  stroke: #e0f2fe;
  stroke-width: 2;
  stroke-linejoin: round;
}

.compass-hub {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text);
  border: 3px solid var(--surface);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  z-index: 7;
  pointer-events: none;
}

.compass-target {
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: center;
  margin: 0;
}

.compass-target strong {
  color: var(--accent);
  font-weight: 600;
}

.compass-info {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
  max-width: var(--compass-size);
}

.info-row {
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
  border: 1px solid var(--border);
}

.info-row .label {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 2px;
}

.info-row .value {
  font-size: 0.95rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* People list */
.people-section {
  flex: 1;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-header h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
}

.section-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.modal-hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.qr-wrap {
  display: flex;
  justify-content: center;
  padding: 8px;
  background: #f8fafc;
  border-radius: 12px;
}

.qr-wrap canvas {
  display: block;
  border-radius: 8px;
}

.share-coords {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.scan-video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  max-height: 280px;
  background: var(--bg);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.scan-video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-video-wrap.hidden {
  display: none;
}

.import-preview {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.5;
}

.import-preview strong {
  color: var(--accent);
}

.people-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.person-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  -webkit-user-select: none;
  user-select: none;
}

.person-item:active {
  transform: scale(0.98);
}

.person-item.selected {
  border-color: var(--accent);
  background: rgba(56, 189, 248, 0.08);
}

.person-avatar {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface-hover);
  border: 2px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--accent);
  flex-shrink: 0;
}

.status-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--surface);
}

.status-dot.online {
  background: #22c55e;
}

.status-dot.offline {
  background: #6b7280;
}

.person-item.stale {
  opacity: 0.55;
}

.person-details {
  flex: 1;
  min-width: 0;
}

.person-name {
  font-weight: 600;
  font-size: 0.95rem;
  margin: 0;
}

.person-coords {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 2px 0 0;
  font-variant-numeric: tabular-nums;
}

.person-distance {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.btn-remove {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0.4;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
  flex-shrink: 0;
  padding: 0;
}

.btn-remove:hover,
.btn-remove:active {
  opacity: 1;
  color: var(--accent);
  background: rgba(56, 189, 248, 0.1);
}

.person-actions {
  display: flex;
  gap: 4px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  font-family: inherit;
}

.btn:active {
  opacity: 0.85;
}

.btn-primary {
  background: var(--accent);
  color: var(--bg);
}

.btn-secondary {
  background: var(--surface-hover);
  color: var(--text);
}

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
}

.btn-small {
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: none;
  color: var(--text-muted);
  border-radius: 8px;
}

.btn-small:hover,
.btn-small:active {
  color: var(--text);
  background: var(--surface-hover);
}

/* Status bar */
.status-bar {
  margin-top: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
}

.gps-banner {
  margin-top: 12px;
  padding: 14px;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid var(--warning);
  border-radius: 12px;
  text-align: center;
}

.gps-banner.hidden {
  display: none;
}

.gps-banner p {
  margin: 0 0 10px;
  font-size: 0.85rem;
  color: var(--warning);
}

.gps-banner .btn {
  width: 100%;
}

.status-bar.ok { color: var(--success); }
.status-bar.warn { color: var(--warning); }
.status-bar.error { color: var(--danger); }

.enable-compass-btn,
.permission-btn {
  margin-top: 10px;
  width: 100%;
}

.permission-btn + .permission-btn {
  margin-top: 8px;
}

.btn-ble.connected {
  border-color: var(--accent);
  color: var(--accent);
}

/* Modal */
.modal {
  border: none;
  border-radius: 16px;
  padding: 0;
  background: transparent;
  max-width: calc(100vw - 32px);
  width: 100%;
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-content h3 {
  margin: 0;
  font-size: 1.1rem;
}

.modal-content label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.modal-content input {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}

.modal-content input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.modal-actions .btn {
  flex: 1;
  min-width: 100px;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.peer-modal-info {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.btn-scan {
  width: 100%;
  gap: 8px;
}

.modal-error {
  margin: 0;
  padding: 10px 14px;
  font-size: 0.85rem;
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid var(--danger);
  border-radius: 8px;
}

.modal-error.hidden {
  display: none;
}

.modal-success {
  margin: 0;
  padding: 10px 14px;
  font-size: 0.85rem;
  color: var(--success);
  background: rgba(34, 197, 94, 0.1);
  border: 1px solid var(--success);
  border-radius: 8px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.empty-state {
  text-align: center;
  padding: 24px 16px;
  color: var(--text-muted);
  font-size: 0.9rem;
  border: 1px dashed var(--border);
  border-radius: 12px;
}

@media (max-height: 600px) {
  :root {
    --compass-size: min(55vw, 240px);
  }

  .header h1 {
    font-size: 1.15rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .compass-ring,
  .target-layer {
    transition: none;
  }
}

/* Settings modal */
.settings-section {
  margin: 1rem 0;
}

.settings-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.5rem;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.theme-swatch {
  padding: 10px 8px;
  border-radius: 8px;
  border: 2px solid var(--border);
  cursor: pointer;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  transition: border-color 0.15s, transform 0.1s;
}

.theme-swatch:hover {
  transform: scale(1.03);
}

.theme-swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.25);
}

.color-picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.color-picker-row input[type="color"] {
  width: 40px;
  height: 34px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: none;
  padding: 0;
}

.color-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 600;
}

.avatar-preview {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface-hover);
  border: 2px solid var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.75rem;
}

.btn-style-grid {
  display: flex;
  gap: 8px;
}

.btn-style-option {
  flex: 1;
  padding: 8px 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s;
}

.btn-style-option:hover {
  border-color: var(--accent);
}

.btn-style-option.active {
  border-color: var(--accent);
  background: rgba(56, 189, 248, 0.1);
}

/* Button style variants applied to body */
body.btn-style-square .btn,
body.btn-style-square .btn-primary,
body.btn-style-square .btn-secondary,
body.btn-style-square .btn-danger {
  border-radius: 6px;
}

body.btn-style-outline .btn-primary {
  background: transparent;
  border: 2px solid var(--accent);
  color: var(--accent);
}

body.btn-style-outline .btn-secondary {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text);
}

body.btn-style-outline .btn-danger {
  background: transparent;
  border: 2px solid var(--danger);
  color: var(--danger);
}
