:root {
  --bg: #f4f3f0;
  --card: #ffffff;
  --ink: #1d1c1a;
  --muted: #6c6a65;
  --line: #e2e0db;
  --accent: #3c5a8a;
  --accent-ink: #ffffff;
  --ok: #2f7a4a;
  --warn-bg: #fff4d6;
  --warn-line: #e8c770;
  --bad: #a33a2f;
  --attention: #c9772a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615;
    --card: #1f1f1d;
    --ink: #ecebe7;
    --muted: #9c9a94;
    --line: #34332f;
    --accent: #7d9dd6;
    --accent-ink: #10131a;
    --ok: #6cc28c;
    --warn-bg: #3a3018;
    --warn-line: #7a6326;
    --bad: #e2786c;
    --attention: #e3a35e;
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) .logo { filter: invert(1); }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
code { font: 0.9em ui-monospace, "JetBrains Mono", monospace; }
.top {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--card);
}
.top h1 { font-size: 18px; margin: 0; }
.logo { width: 28px; height: 28px; }
.account { color: var(--muted); }
.top button { margin-left: auto; }
main { max-width: 1200px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; min-width: 0; }
.card.attention { border-color: var(--attention); box-shadow: 0 0 0 1px var(--attention) inset; }
.card h2 { font-size: 15px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.banner { border: 1px solid var(--warn-line); background: var(--warn-bg); border-radius: 10px; padding: 10px 14px; }
.banner.notice { display: flex; gap: 12px; align-items: flex-start; }
.banner.notice div { flex: 1; }
#notices { display: flex; flex-direction: column; gap: 8px; }
#notices:empty { display: none; }

.pill { border-radius: 999px; padding: 2px 10px; font-size: 13px; border: 1px solid var(--line); }
.pill.online { color: var(--ok); border-color: var(--ok); }
.pill.offline, .pill.down { color: var(--bad); border-color: var(--bad); }
.pill.connecting, .pill.logged_out { color: var(--muted); }

.rows { list-style: none; margin: 0; padding: 0; }
.rows > li { border-top: 1px solid var(--line); padding: 9px 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.rows > li:first-child { border-top: 0; }
.rows .main { flex: 1 1 260px; min-width: 0; }
.rows .name { font-weight: 600; overflow-wrap: anywhere; }
.rows .sub { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.rows .sub.conn { display: block; }
.rows .sub.conn:empty { display: none; }
.rows .sub.conn.warn { color: var(--attention); }
.rows .actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.rows.nested > li { padding: 5px 0 5px 14px; border-top: 0; }
.tag { font-size: 12px; border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; color: var(--muted); }
.tag.active { color: var(--accent); border-color: var(--accent); }
.tag.me { color: var(--ok); border-color: var(--ok); }

button {
  font: inherit; font-size: 13px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  border-radius: 7px; padding: 4px 10px;
}
button.ghost { background: transparent; color: var(--accent); }
button.danger { background: transparent; border-color: var(--bad); color: var(--bad); }
button:disabled { opacity: 0.5; cursor: progress; }
input, select {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 7px; padding: 5px 8px; min-width: 0; width: 100%;
}
select { width: auto; }
label { display: flex; flex-direction: column; gap: 3px; font-size: 13px; color: var(--muted); }
.inline { flex-direction: row; align-items: center; gap: 5px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; align-items: end; margin-top: 8px; }
.grid .wide { grid-column: 1 / -1; }
details { margin-top: 10px; }
summary { cursor: pointer; color: var(--accent); font-size: 14px; }

.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink); flex-direction: row; }
.switch input { width: auto; accent-color: var(--accent); }

.voice-me { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 6px 0 4px; }
.talk { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--line); flex: none; }
.talk.talking { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 30%, transparent); }
.volume { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); flex-direction: row; }
.volume input[type=range] { width: 140px; padding: 0; accent-color: var(--accent); }
.volume output { min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }
.processing { margin-top: 10px; }
.tag.bad { color: var(--bad); border-color: var(--bad); }

#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; box-shadow: 0 4px 16px #0003; overflow-wrap: anywhere; }
.toast.error { border-color: var(--bad); }
.toast input { margin-top: 6px; }

@media (max-width: 520px) {
  .cols { grid-template-columns: 1fr; }
  .top button { margin-left: 0; }
}
.bad { color: var(--bad); }

/* --- avatars: round picture, initials underneath as the fallback, online-devices badge */
.avatar {
  --size: 32px;
  position: relative; display: inline-grid; place-items: center; flex: none;
  width: var(--size); height: var(--size); border-radius: 50%;
  background: oklch(0.62 0.09 var(--hue, 250)); color: #fff;
  font-size: calc(var(--size) * 0.4); font-weight: 600; line-height: 1; vertical-align: middle;
}
.avatar img, .avatar .initials { grid-area: 1 / 1; }
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.avatar.offline img, .avatar.offline .initials { opacity: 0.45; filter: grayscale(0.7); }
.avatar .badge {
  --b: clamp(12px, calc(var(--size) * 0.38), 18px);
  position: absolute; right: -3px; bottom: -3px; min-width: var(--b); height: var(--b); padding: 0 3px;
  border-radius: calc(var(--b) / 2); background: var(--ok); color: var(--card); border: 1.5px solid var(--card);
  font-size: calc(var(--b) * 0.62); line-height: calc(var(--b) - 3px); text-align: center; box-sizing: border-box;
}
.who { display: flex; align-items: center; gap: 8px; }
.stack { display: inline-flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }

/* --- relay cards: a background picture under a scrim, who is here, the full list */
.rows > li.relay-card {
  position: relative; overflow: hidden; display: block; padding: 0;
  border: 1px solid var(--line); border-radius: 10px; margin-top: 10px; background: var(--card);
}
.rows > li.relay-card:first-child { margin-top: 0; border-top: 1px solid var(--line); }
.relay-card .card-body { position: relative; z-index: 1; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 12px; }
.relay-card .card-bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; z-index: 0; }
/* Readable over any picture: a dark scrim, light text with a shadow. */
.relay-card .card-scrim {
  position: absolute; inset: 0; z-index: 0;
  background: linear-gradient(90deg, rgb(0 0 0 / 0.78), rgb(0 0 0 / 0.55) 60%, rgb(0 0 0 / 0.45));
}
.relay-card.has-bg .card-body { color: #fff; text-shadow: 0 1px 2px rgb(0 0 0 / 0.8); min-height: 110px; }
.relay-card.has-bg .sub { color: rgb(255 255 255 / 0.86); }
.relay-card.has-bg .tag, .relay-card.has-bg .chip { color: #fff; border-color: rgb(255 255 255 / 0.6); background: rgb(0 0 0 / 0.25); }
.relay-card.has-bg .avatar { text-shadow: none; }
.relay-card.has-bg button.ghost { background: rgb(0 0 0 / 0.35); color: #fff; border-color: rgb(255 255 255 / 0.6); }
.relay-card.has-bg button.danger { background: rgb(0 0 0 / 0.45); color: #ffb8ad; border-color: #ffb8ad; }
.here {
  --here-size: 30px;
  display: flex; gap: 4px; align-items: center; flex-wrap: nowrap; overflow: hidden;
  height: calc(var(--here-size) + 8px); margin-top: 6px; min-width: 0;
}
.here .avatar { cursor: default; }
.here .avatar.host, .here-row .avatar.host { box-shadow: 0 0 0 2px var(--accent); }
.here-more {
  flex: none; height: var(--here-size); min-width: var(--here-size); padding: 0 8px; border-radius: 999px;
  font-size: 13px; font-weight: 600; text-shadow: none;
}
.here-dialog { width: min(440px, calc(100vw - 32px)); padding: 0; overflow: hidden; display: none; }
.here-dialog[open] { display: flex; flex-direction: column; }
.here-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 8px 16px; border-bottom: 1px solid var(--line); }
.here-head h2 { flex: 1; margin: 0; }
.here-list { position: relative; height: min(480px, calc(100dvh - 120px)); overflow-y: auto; overscroll-behavior: contain; }
.here-list:focus { outline: none; }
.here-spacer { position: relative; }
.here-row {
  position: absolute; left: 0; right: 0; height: 48px; display: flex; align-items: center; gap: 10px;
  padding: 0 16px; border-bottom: 1px solid var(--line);
}
.here-row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.here-empty { padding: 12px 16px; margin: 0; }
.tag.host { color: var(--accent); border-color: var(--accent); }
.here-tip {
  position: fixed; z-index: 60; padding: 3px 8px; border-radius: 6px; font-size: 13px; pointer-events: none;
  background: var(--ink); color: var(--card); white-space: nowrap;
}
.here-tip[hidden] { display: none; }
@media (max-width: 760px) {
  .here { --here-size: 36px; }
  .here-dialog { width: calc(100vw - 16px); max-width: none; }
}
.profile { display: flex; gap: 14px; align-items: center; }
.profile .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.voice-device { margin-top: 12px; }
#me-avatar:empty { display: none; }
label.button {
  display: inline-flex; flex-direction: row; font-size: 13px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  border-radius: 7px; padding: 4px 10px;
}

dialog {
  border: 1px solid var(--line); border-radius: 12px; background: var(--card); color: var(--ink);
  padding: 16px; width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px); overflow: auto; overscroll-behavior: contain;
}
dialog::backdrop { background: #0007; }
dialog h2 { font-size: 15px; margin: 0 0 6px; }
.crop-stage { position: relative; width: 100%; max-width: 320px; margin: 8px auto; aspect-ratio: 1; }
#avatar-canvas { width: 100%; height: 100%; border-radius: 8px; background: var(--bg); touch-action: none; cursor: grab; display: block; }
#avatar-canvas:active { cursor: grabbing; }
.crop-busy { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; color: var(--muted); }
.crop-busy[hidden] { display: none; }
.error-box {
  white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.4 ui-monospace, monospace; color: var(--bad);
  border: 1px solid var(--bad); border-radius: 8px; padding: 8px; margin: 8px 0 0; max-height: 200px; overflow: auto;
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
#voice-elsewhere { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
#voice-elsewhere[hidden] { display: none; }

/* --- shared audio: sinks + incoming sources */
.rows.sources > li { gap: 6px; align-items: center; }
.rows.sources .name { font-weight: 500; }
.tag.ok { color: var(--ok); border-color: var(--ok); }
code.node { overflow-wrap: anywhere; }
form.rename { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
form.rename input { width: auto; flex: 1 1 160px; }
#sinks .name .talk { vertical-align: middle; }

/* --- device types: kind icons, caps, windows grouped by app */
.kicon { display: inline-grid; place-items: center; width: 22px; height: 22px; flex: none; color: var(--muted); vertical-align: middle; }
.tag.caps { font-variant-numeric: tabular-nums; }
.appicon { width: 24px; height: 24px; flex: none; object-fit: contain; }
.rows > li.app { display: block; }
.apphead { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.rows.windows > li { padding-left: 34px; }
.titles-row { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin: 6px 0 4px; }
.titles-row p { margin: 4px 0 0; }
.titles-row[hidden] { display: none; }
.tag.own { color: var(--attention); border-color: var(--attention); }
.rows > li.app.idle .rows > li { padding-left: 0; }

/* --- meta.tags: #chips (data only, nothing is hidden by them) */
.chips { display: inline-flex; gap: 4px; flex-wrap: wrap; align-items: center; margin-left: 6px; vertical-align: middle; font-weight: 400; }
.chip { font-size: 12px; border-radius: 999px; padding: 0 7px; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); display: inline-flex; align-items: center; gap: 2px; }
.chip-x { border: 0; background: none; color: inherit; padding: 0 0 0 2px; font-size: 13px; line-height: 1; }
.chip-add { display: inline-flex; }
.chip-add input { width: 6.5em; font-size: 12px; padding: 0 6px; border-radius: 999px; background: transparent; }

/* --- local vs hosted (transport.js sets <html data-mode>) */
html[data-mode="hosted"] [data-only="local"],
html[data-mode="local"] [data-only="hosted"],
html:not([data-mode]) [data-only="hosted"] { display: none !important; }

/* --- the call bar: mute / deafen / join, always in reach */
.callbar {
  position: sticky; bottom: 0; z-index: 5;
  display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap;
  max-width: 1200px; margin: 0 auto; padding: 10px 16px;
  background: color-mix(in srgb, var(--card) 92%, transparent); border-top: 1px solid var(--line);
  backdrop-filter: blur(6px);
}
.callbar[hidden] { display: none; }
.callbar #voice-me-level { width: 14px; height: 14px; }

/* --- tabs: phones only */
.tabs { display: none; }
.tabs button[aria-current="page"] { background: var(--accent); color: var(--accent-ink); }

@media (max-width: 760px) {
  main { padding: 12px max(12px, env(safe-area-inset-left)) 12px max(12px, env(safe-area-inset-right)); gap: 12px; }
  .top { padding: 8px max(12px, env(safe-area-inset-left)); padding-top: max(8px, env(safe-area-inset-top)); gap: 8px; }
  .top h1 { font-size: 16px; }
  .account { font-size: 13px; flex: 1 1 100%; order: 5; overflow-wrap: anywhere; }
  .cols { grid-template-columns: 1fr; gap: 12px; }
  .card { padding: 12px; }

  /* One section at a time; the tab bar and the call bar sit at the bottom, over the safe area. */
  body[data-tab="relays"] .card[data-tab]:not([data-tab~="relays"]),
  body[data-tab="people"] .card[data-tab]:not([data-tab~="people"]),
  body[data-tab="voice"] .card[data-tab]:not([data-tab~="voice"]),
  body[data-tab="you"] .card[data-tab]:not([data-tab~="you"]) { display: none; }
  .tabs {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
    padding: 6px max(8px, env(safe-area-inset-left)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-right));
    background: var(--card); border-top: 1px solid var(--line);
  }
  .tabs button { background: transparent; color: var(--ink); border-color: transparent; font-size: 14px; }
  .callbar {
    position: fixed; left: 0; right: 0; bottom: calc(57px + env(safe-area-inset-bottom)); max-width: none;
    padding: 8px max(8px, env(safe-area-inset-left)); justify-content: space-around; flex-wrap: nowrap;
  }
  .callbar button { flex: 1 1 0; min-width: 0; padding: 4px 6px; }
  .callbar #voice-tone { display: none; }
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  body.has-callbar { padding-bottom: calc(128px + env(safe-area-inset-bottom)); }
  #toasts { left: 12px; right: 12px; bottom: calc(136px + env(safe-area-inset-bottom)); max-width: none; }
  .volume input[type=range] { width: auto; flex: 1 1 120px; }
  .volume { flex: 1 1 100%; }
  .rows .actions { flex: 1 1 100%; }
  .profile { flex-wrap: wrap; }
}

/* --- touch: every control at least 44 px (phones, and any coarse pointer) */
@media (max-width: 760px), (pointer: coarse) {
  button, label.button, select, input:not([type=checkbox]):not([type=radio]):not([type=range]) { min-height: 44px; }
  button, label.button { padding: 8px 14px; font-size: 15px; display: inline-flex; align-items: center; justify-content: center; }
  .switch { min-height: 44px; }
  .switch input[type=checkbox] { width: 22px; height: 22px; }
  input[type=range] { min-height: 44px; }
  summary { min-height: 44px; display: flex; align-items: center; }
  .chip { min-height: 32px; }
  .chip-x { min-width: 44px; min-height: 44px; margin: -6px -14px -6px -8px; }
  .chip-add input { min-height: 44px; }
}
body.has-callbar #toasts { bottom: 76px; }

/* Video viewer panels (video.js): bottom right on desktops, the whole screen on phones. */
#video-panels {
  position: fixed; right: 16px; bottom: 16px; z-index: 40;
  display: flex; flex-direction: column; gap: 12px; align-items: flex-end;
  max-height: calc(100vh - 32px); overflow-y: auto;
}
.video-panel {
  width: min(720px, calc(100vw - 32px)); background: var(--card); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px rgb(0 0 0 / 0.35);
  overflow: hidden;
}
.video-head { display: flex; align-items: center; gap: 8px; padding: 6px 10px; }
.video-head .video-status { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.video-head .video-settings { white-space: nowrap; }
.stream-pickers { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.video-sound { display: flex; align-items: center; gap: 8px; padding: 0 10px 6px; flex-wrap: wrap; }
.video-sound[hidden] { display: none; }
.video-sound input[type=range] { width: 110px; }
.video-sound .video-sound-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.video-panel canvas { display: block; width: 100%; height: auto; background: #000; }
.video-panel.ended canvas { opacity: 0.4; }
.video-head { cursor: move; touch-action: none; user-select: none; }
.video-head button, .video-head select { cursor: pointer; }
.video-panel { position: relative; }
.video-resize {
  position: absolute; right: 0; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize; touch-action: none;
  background: linear-gradient(135deg, transparent 50%, var(--line) 50%, var(--line) 60%, transparent 60%,
    transparent 70%, var(--line) 70%, var(--line) 80%, transparent 80%);
}
/* Moved or resized: placed by its inline left/top/width/height, the picture fits inside. */
.video-panel.floating { position: fixed; z-index: 41; display: flex; flex-direction: column; }
.video-panel.floating canvas { flex: 1; min-height: 0; height: 100%; object-fit: contain; }
/* Input (video.js): toggles in the head, a ring while your input is live, the release hint. */
.video-input { display: inline-flex; gap: 6px; }
.video-input button.on { background: var(--accent); color: var(--accent-ink); }
.video-panel canvas:focus { outline: none; }
.video-panel.input-control canvas, .video-panel.input-ghost canvas { touch-action: none; }
.video-panel.input-ghost canvas { cursor: crosshair; }
.video-panel.input-control canvas:focus { box-shadow: inset 0 0 0 3px var(--accent); }
.video-hint { padding: 4px 10px; }
.banner.controlling { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; }
.banner.controlling div { flex: 1; }
.tag.consumers { cursor: default; }
@media (max-width: 760px) {
  .video-head { cursor: default; }
  .video-resize { display: none; }
  .video-panel.floating { position: relative; left: auto !important; top: auto !important;
    width: 100vw !important; height: 100dvh !important; }
  #video-panels { inset: 0; right: 0; bottom: 0; gap: 0; max-height: none; z-index: 60; pointer-events: none; }
  #video-panels:empty { display: none; }
  .video-panel {
    pointer-events: auto; width: 100vw; height: 100vh; height: 100dvh; border-radius: 0; border: 0;
    display: flex; flex-direction: column; background: #000; color: #fff;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  }
  .video-head { background: rgb(0 0 0 / 0.6); min-height: 44px; }
  .video-head button { min-height: 44px; }
  .video-panel canvas { flex: 1; min-height: 0; object-fit: contain; height: 100%; }
  /* Phones already show the viewer fullscreen; a pop-up would only be another tab. */
  body:not(.video-popout) .video-head .popout { display: none; }
}
/* A viewer popped out into its own window: the panel fills it. */
body.video-popout { margin: 0; background: #000; }
body.video-popout .video-panel {
  width: 100vw; height: 100vh; display: flex; flex-direction: column;
  border: 0; border-radius: 0; box-shadow: none; background: #000; color: #fff;
}
body.video-popout .video-panel canvas { flex: 1; min-height: 0; height: 100%; object-fit: contain; }
body.video-popout .video-head { cursor: default; }
body.video-popout .video-resize { display: none; }
