:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181d;
  --muted: #5f6774;
  --border: #dde1e7;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --live: #1a9d55;
  --stale: #b7791f;
  --danger: #c53030;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(20, 24, 31, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #eceff4;
    --muted: #9aa3b2;
    --border: #2c313b;
    --accent: #5b8ff9;
    --accent-text: #0b0d10;
    --live: #3ccf7c;
    --stale: #e2a43b;
    --danger: #f16d6d;
    --shadow: 0 4px 18px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

h1, h2, h3 { line-height: 1.2; margin: 0 0 0.5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 0.75rem; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
.hidden { display: none !important; }

/* ---------- layout ---------- */

.page {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px calc(88px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0 16px;
}

.brand { font-weight: 700; font-size: 1.15rem; letter-spacing: -0.01em; }
.brand span { color: var(--accent); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.stack > * + * { margin-top: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row > .grow { flex: 1; min-width: 0; }

/* ---------- controls ---------- */

input[type="text"] {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
}
input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input.code-input { text-transform: uppercase; letter-spacing: 0.2em; font-family: ui-monospace, monospace; }

button, .btn {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 10px;
  padding: 11px 16px;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
}
button:disabled { opacity: 0.55; cursor: default; }
.btn-block { width: 100%; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-small { padding: 7px 11px; font-size: 0.875rem; }
.link-btn { background: none; color: var(--accent); padding: 4px 0; font-weight: 500; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 12px;
  border-radius: 999px;
  font-weight: 500;
  font-size: 0.9rem;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch { position: relative; width: 52px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: background 0.2s;
}
.switch span::after {
  content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; transition: transform 0.2s; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.switch input:checked + span { background: var(--live); }
.switch input:checked + span::after { transform: translateX(22px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- status ---------- */

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.8rem; font-weight: 600;
  padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.live { background: var(--live); box-shadow: 0 0 0 0 var(--live); animation: pulse 2s infinite; }
.dot.stale { background: var(--stale); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--live) 60%, transparent); }
  70% { box-shadow: 0 0 0 8px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.steps { counter-reset: step; list-style: none; padding: 0; margin: 0; }
.steps > li { counter-increment: step; position: relative; padding-left: 40px; margin-bottom: 18px; }
.steps > li::before {
  content: counter(step);
  position: absolute; left: 0; top: 0;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  font-weight: 700; font-size: 0.9rem;
  display: grid; place-items: center;
}

.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 12px 0; border-top: 1px solid var(--border); }
.list > li:first-child { border-top: 0; padding-top: 0; }
.list > li:last-child { padding-bottom: 0; }

.empty { text-align: center; color: var(--muted); padding: 8px 0; }

.group-code {
  font-family: ui-monospace, monospace; font-size: 1.6rem; font-weight: 700; letter-spacing: 0.25em;
}

details summary { cursor: pointer; color: var(--muted); font-size: 0.9rem; }
details[open] summary { margin-bottom: 8px; }

dl.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; font-size: 0.875rem; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }

/* ---------- bottom nav ---------- */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: flex; justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-inner { display: flex; width: 100%; max-width: 560px; }
.tabbar a {
  flex: 1; text-align: center; text-decoration: none;
  color: var(--muted); font-size: 0.75rem; font-weight: 600;
  padding: 10px 4px 12px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tabbar a svg { width: 22px; height: 22px; }
.tabbar a[aria-current="page"] { color: var(--accent); }

/* ---------- maps ---------- */

.map { height: 320px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); margin-bottom: 14px; }

.member-marker {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  border: 3px solid #fff; box-shadow: var(--shadow);
  display: grid; place-items: center;
  font: 700 12px/1 system-ui, sans-serif;
}
.member-marker.me { background: var(--live); }
/* Leaflet sets width: auto on marker images with a stronger selector. */
.leaflet-container .leaflet-marker-pane .member-marker img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex: none;
  background: var(--surface-2); color: var(--text);
  display: grid; place-items: center; font-weight: 700; font-size: 0.8rem;
  object-fit: cover;
}
.avatar-sm { width: 26px; height: 26px; font-size: 0.65rem; }
.avatar-lg { width: 72px; height: 72px; font-size: 1.4rem; }

/* ---------- toast ---------- */

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 2000;
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 0.9rem; font-weight: 500;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

/* ---------- public viewer ---------- */

body.viewer { overflow: hidden; }
#viewer-map { position: fixed; inset: 0; }
.viewer-card {
  position: fixed; z-index: 1000; left: 16px; right: 16px; bottom: calc(28px + env(safe-area-inset-bottom));
  max-width: 420px; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px;
}
.viewer-card .viewer-head { justify-content: space-between; }
.viewer-name { font-weight: 700; font-size: 1.1rem; }
.viewer-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 8px; font-size: 0.875rem; color: var(--muted); }
.viewer-ended { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; text-align: center; z-index: 1500; background: var(--bg); }

.leaflet-container { font: inherit; }

/* Wrappers whose content is re-rendered with avatarHtml(). */
#avatar, #avatar-preview { display: flex; flex: none; }
