:root {
  color-scheme: dark;
  --bg: #0c0d10;
  --surface: #141519;
  --surface-2: #181a1f;
  --surface-3: #1e2027;
  --line: rgba(255, 255, 255, 0.07);
  --line-strong: rgba(255, 255, 255, 0.13);
  --text: #e8e9ec;
  --muted: #9a9fa8;
  --muted-soft: #6c7079;
  --accent: #6366f1;
  --accent-strong: #4f46e5;
  --accent-soft: rgba(99, 102, 241, 0.16);
  --accent-hi: #8b8ef6;
  --online: #3ba55d;
  --danger: #ed5a5a;
  --danger-hover: #d94a4a;
  /* Semantic fills/overlays — overridden per theme so they don't assume a dark bg. */
  --hover: rgba(255, 255, 255, 0.04);
  --track: rgba(255, 255, 255, 0.08);
  --track-strong: rgba(255, 255, 255, 0.12);
  --thumb: #ffffff;
  --on-accent: #ffffff;
  --scrim: rgba(0, 0, 0, 0.6);
  --shadow-modal: 0 24px 60px -20px rgba(0, 0, 0, 0.8);
  --radius: 14px;
  --radius-sm: 10px;
}

/* ---------- Light theme ---------- */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef0f4;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --surface-3: #eceef2;
  --line: rgba(15, 18, 26, 0.1);
  --line-strong: rgba(15, 18, 26, 0.18);
  --text: #1b1d22;
  --muted: #585e68;
  --muted-soft: #8a909b;
  --accent: #5457e6;
  --accent-strong: #4f46e5;
  --accent-soft: rgba(99, 102, 241, 0.14);
  --accent-hi: #6e72f0;
  --online: #2e9150;
  --danger: #d8443f;
  --danger-hover: #c43a36;
  --hover: rgba(15, 18, 26, 0.05);
  --track: rgba(15, 18, 26, 0.1);
  --track-strong: rgba(15, 18, 26, 0.16);
  --thumb: var(--accent-strong);
  --on-accent: #ffffff;
  --scrim: rgba(15, 18, 26, 0.42);
  --shadow-modal: 0 24px 60px -20px rgba(15, 18, 26, 0.35);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button,
input {
  font: inherit;
  color: inherit;
}

h1,
h2,
h3,
p {
  margin: 0;
}

/* ---------- App shell ---------- */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 60px;
  padding: 0 22px;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  flex: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
}

.logo {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  color: var(--on-accent);
  background: linear-gradient(150deg, var(--accent), var(--accent-strong));
}

.logo svg {
  width: 19px;
  height: 19px;
}

.brand-name {
  font-weight: 650;
  letter-spacing: -0.01em;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ---------- Theme toggle ---------- */

.theme-toggle {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.theme-toggle:hover {
  color: var(--text);
  border-color: var(--line-strong);
}

.theme-toggle svg {
  width: 18px;
  height: 18px;
}

/* Show the sun in dark mode (tap → go light), the moon in light mode. */
.theme-toggle .icon-sun {
  display: block;
}
.theme-toggle .icon-moon {
  display: none;
}
:root[data-theme="light"] .theme-toggle .icon-sun {
  display: none;
}
:root[data-theme="light"] .theme-toggle .icon-moon {
  display: block;
}

/* ---------- Status pill ---------- */

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 13px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--muted-soft);
  transition: background 0.2s ease;
}

.status-pill.connected {
  color: var(--text);
}

.status-pill.connected .status-dot {
  background: var(--online);
  box-shadow: 0 0 0 3px rgba(59, 165, 93, 0.18);
}

.status-pill.error {
  color: var(--text);
}

.status-pill.error .status-dot {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(237, 90, 90, 0.18);
}

/* ---------- View switching ---------- */

.call-view,
.controlbar {
  display: none;
}

.app[data-connected="true"] .connect-view {
  display: none;
}

.app[data-connected="true"] .call-view {
  display: grid;
}

.app[data-connected="true"] .controlbar {
  display: flex;
}

/* ---------- Connect view ---------- */

.connect-view {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}

.connect-card {
  width: min(420px, 100%);
  display: grid;
  gap: 12px;
}

.connect-card h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.connect-card .sub {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 6px;
}

label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
}

input[type="text"],
input:not([type]),
input[type="password"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  padding: 12px 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:not([type="range"]):focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.adv {
  margin: 2px 0 4px;
}

.adv summary {
  list-style: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 0;
  user-select: none;
}

.adv summary::-webkit-details-marker {
  display: none;
}

.adv summary::before {
  content: "▸ ";
  color: var(--muted-soft);
}

.adv[open] summary::before {
  content: "▾ ";
}

.adv label {
  display: block;
  margin: 10px 0 6px;
}

/* ---------- Buttons ---------- */

button {
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.05s ease, background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.primary {
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  min-height: 46px;
  padding: 0 18px;
}

.primary:not(:disabled):hover {
  background: var(--accent-strong);
}

.block {
  width: 100%;
}

.ghost {
  background: var(--surface-3);
  border-color: var(--line);
  min-height: 42px;
  padding: 0 16px;
}

.ghost:not(:disabled):hover {
  border-color: var(--line-strong);
}

.link-button {
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
}

.link-button:hover {
  color: var(--text);
}

/* ---------- Call view ---------- */

.call-view {
  flex: 1;
  grid-template-columns: 256px 1fr;
  min-height: 0;
}

.channels {
  border-right: 1px solid var(--line);
  padding: 16px 12px;
  overflow: auto;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.count {
  text-transform: none;
  letter-spacing: 0;
  color: var(--muted-soft);
  font-weight: 500;
}

.stage {
  padding: 20px 24px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.stage-head h2 {
  font-size: 18px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

/* ---------- Channel tree ---------- */

.channel-tree {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 2px;
}

.channel-empty {
  padding: 18px 10px;
  color: var(--muted-soft);
  font-size: 13px;
}

.channel-item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  cursor: pointer;
  font-size: 14px;
}

.channel-item:hover {
  background: var(--hover);
  color: var(--text);
}

.channel-item.current,
.channel-item:disabled {
  opacity: 1;
  cursor: default;
}

.channel-item.current {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
}

.channel-item .channel-icon {
  color: var(--muted-soft);
  font-size: 13px;
  flex: none;
}

.channel-item.current .channel-icon {
  color: var(--accent);
}

.channel-item .channel-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.channel-item .channel-meta {
  flex: none;
  color: var(--muted-soft);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.channel-item.child {
  margin-left: 16px;
}

/* ---------- Roster ---------- */

.roster {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 6px;
}

.roster li {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface);
}

.avatar {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 999px;
  background: linear-gradient(150deg, var(--accent), var(--accent-strong));
  color: var(--on-accent);
  font-weight: 650;
  font-size: 14px;
  flex: none;
}

.roster-text {
  display: grid;
  line-height: 1.3;
}

.roster strong {
  font-size: 14px;
  font-weight: 600;
}

.roster small {
  color: var(--muted);
  font-size: 12px;
}

.roster-text {
  flex: 1;
  min-width: 0;
  gap: 4px;
}

.avatar.self {
  background: linear-gradient(150deg, var(--muted), var(--muted-soft));
}

.roster-empty {
  justify-content: center;
  color: var(--muted-soft);
  font-size: 13px;
}

/* Per-person volume control inside a roster row. */
.vol-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.roster .client-vol {
  width: auto;
  flex: 1;
  min-width: 90px;
}

.vol-pct {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-width: 38px;
  text-align: right;
}

/* ---------- Log (collapsed) ---------- */

.log-wrap {
  margin-top: auto;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.log-wrap summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  user-select: none;
}

.log-wrap summary::-webkit-details-marker {
  display: none;
}

.log {
  list-style: none;
  padding: 10px 0 0;
  margin: 0;
  display: grid;
  gap: 6px;
  max-height: 180px;
  overflow: auto;
  font-family: "Cascadia Mono", "Consolas", ui-monospace, monospace;
  font-size: 12px;
}

.log li {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.log time {
  color: var(--accent);
  margin-right: 4px;
}

/* ---------- Control bar ---------- */

.controlbar {
  flex: none;
  align-items: center;
  gap: 12px;
  height: 68px;
  padding: 0 18px;
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid var(--line);
  background: var(--surface-2);
}

.tool {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 42px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
}

.tool-icon {
  width: 19px;
  height: 19px;
}

.tool:hover {
  color: var(--text);
}

.tool.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.level {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.meter {
  flex: 1;
  height: 6px;
  min-width: 40px;
  border-radius: 999px;
  background: var(--track);
  overflow: hidden;
}

.meter-fill {
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--accent-hi));
  transition: width 80ms linear;
}

.level-value {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-width: 34px;
  text-align: right;
}

.vol {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted-soft);
}

.vol svg {
  width: 18px;
  height: 18px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 110px;
  height: 5px;
  border-radius: 999px;
  background: var(--track-strong);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 999px;
  background: var(--thumb);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: none;
  border-radius: 999px;
  background: var(--thumb);
  cursor: pointer;
}

.hangup {
  height: 42px;
  padding: 0 18px;
  border-radius: 999px;
  background: var(--danger);
  color: var(--on-accent);
  font-weight: 600;
}

.hangup:not(:disabled):hover {
  background: var(--danger-hover);
}

/* ---------- Password modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--scrim);
  backdrop-filter: blur(4px);
  z-index: 50;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  width: min(380px, 100%);
  display: grid;
  gap: 12px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  box-shadow: var(--shadow-modal);
}

.modal h3 {
  font-size: 17px;
  font-weight: 650;
}

.modal-hint {
  color: var(--muted);
  font-size: 13px;
}

.modal-hint.error {
  color: var(--danger);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 4px;
}

/* ---------- Scrollbars ---------- */

.channels::-webkit-scrollbar,
.stage::-webkit-scrollbar,
.log::-webkit-scrollbar {
  width: 8px;
}

.channels::-webkit-scrollbar-thumb,
.stage::-webkit-scrollbar-thumb,
.log::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: var(--track-strong);
}

/* ---------- Mobile ---------- */

@media (max-width: 720px) {
  .topbar {
    padding: 0 16px;
    padding-top: env(safe-area-inset-top);
  }

  .call-view {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .channels {
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: 38vh;
  }

  .controlbar {
    flex-wrap: wrap;
    height: auto;
    gap: 10px;
    padding: 12px 14px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .tool {
    flex: 1;
  }

  .tool .tool-label {
    display: none;
  }

  .level {
    order: 3;
    flex-basis: 100%;
  }

  .vol {
    flex: 1;
  }

  .vol input[type="range"] {
    flex: 1;
    width: auto;
  }

  .hangup {
    flex: 1;
  }
}
