:root {
  --bg-0: #1e1f22;
  --bg-1: #2b2d31;
  --bg-2: #313338;
  --bg-3: #383a40;
  --bg-hover: #35373c;
  --text: #dbdee1;
  --text-muted: #949ba4;
  --accent: #5865f2;
  --green: #23a55a;
  --red: #da373c;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg-2);
  color: var(--text);
  font: 15px/1.4 "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
ul { list-style: none; margin: 0; padding: 0; }
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: linear-gradient(135deg, #404eed, #2b2d31);
  padding: 16px;
}
.login-box {
  background: var(--bg-2); padding: 32px; border-radius: 12px;
  width: 100%; max-width: 400px; display: grid; gap: 14px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .4);
}
.login-box h1 { margin: 0; text-align: center; }
.login-box p { margin: 0 0 8px; text-align: center; color: var(--text-muted); }
.login-box label { display: grid; gap: 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); }
.login-box small { text-transform: none; font-weight: 400; }
.login-box input {
  background: var(--bg-0); border: 0; border-radius: 4px; padding: 10px;
  color: var(--text); font-size: 16px; outline: none;
}
.login-box button[type=submit] {
  background: var(--accent); border: 0; border-radius: 4px; padding: 12px;
  font-weight: 600; margin-top: 8px;
}
.login-box button[type=submit]:hover { filter: brightness(1.1); }
.login-error { color: #fa777c; text-align: center; min-height: 1em; font-size: 14px; }
.login-box label.check {
  display: flex; align-items: center; gap: 8px; text-transform: none;
  font-size: 14px; font-weight: 400; color: var(--text);
}
.login-box label.check input { accent-color: var(--accent); width: 16px; height: 16px; padding: 0; }
.install-btn {
  background: transparent; border: 1px solid var(--accent); color: var(--text);
  border-radius: 4px; padding: 10px; font-weight: 600;
}
.install-btn:hover { background: var(--accent); }

/* Status da ponte (o PC que segura o servidor) */
.bridge-status {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  font-size: 13px; padding: 8px 10px; border-radius: 6px; background: var(--bg-0); color: var(--text-muted);
}
.bridge-status .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--text-muted); flex: none; }
.bridge-status.on .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.bridge-status.on { color: var(--text); }
.bridge-status.off .dot { background: var(--red); }
.bridge-status.checking .dot { animation: d2-pulse 1s infinite alternate; }
@keyframes d2-pulse { from { opacity: .3; } to { opacity: 1; } }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 240px 1fr 220px; height: 100%; height: 100dvh; }

.sidebar { background: var(--bg-1); display: flex; flex-direction: column; min-height: 0; }
.server-name {
  padding: 14px 16px; font-weight: 700; border-bottom: 1px solid var(--bg-0);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
}
.channels { flex: 1; overflow-y: auto; padding: 8px; }
.section-title {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  color: var(--text-muted); padding: 16px 8px 6px;
}
.channel {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px;
  border-radius: 4px; color: var(--text-muted); cursor: pointer; user-select: none;
}
.channel:hover { background: var(--bg-hover); color: var(--text); }
.channel.active { background: var(--bg-3); color: #fff; }
.channel .icon { width: 20px; text-align: center; opacity: .8; }
.channel.unread { color: #fff; font-weight: 600; }
.channel.unread::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #fff; margin-left: auto; }

.voice-users { padding-left: 28px; }
.voice-user {
  display: flex; align-items: center; gap: 8px; padding: 3px 6px;
  font-size: 14px; color: var(--text-muted);
}
.voice-user .flags { margin-left: auto; display: flex; gap: 4px; font-size: 12px; }
.live {
  background: var(--red); color: #fff; font-size: 10px; font-weight: 700;
  padding: 1px 5px; border-radius: 4px;
}

.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; color: #fff; flex-shrink: 0; transition: box-shadow .1s;
}
.avatar.small { width: 24px; height: 24px; font-size: 12px; }
.avatar.speaking, .voice-user.speaking .avatar { box-shadow: 0 0 0 2px var(--bg-1), 0 0 0 4px var(--green); }

.voice-panel {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-top: 1px solid var(--bg-0); background: #232428;
}
.voice-status { display: flex; align-items: center; gap: 8px; }
.voice-status strong { color: var(--green); font-size: 14px; display: block; }
.voice-status small { color: var(--text-muted); }
.voice-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }

.user-panel {
  display: flex; align-items: center; gap: 8px; padding: 10px 8px; background: #232428;
}
.me-name { flex: 1; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  background: transparent; border: 0; border-radius: 4px; width: 32px; height: 32px;
  display: grid; place-items: center; font-size: 16px; position: relative;
}
.icon-btn:hover { background: var(--bg-hover); }
.icon-btn.off::after, .ctl.off::after {
  content: ""; position: absolute; width: 2px; height: 70%; background: var(--red);
  transform: rotate(45deg); border-radius: 2px;
}
.icon-btn.danger { color: var(--red); }

/* ---------- Principal ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.main-header {
  display: flex; align-items: center; gap: 6px; padding: 0 16px; height: 48px;
  border-bottom: 1px solid var(--bg-0); box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
  font-weight: 600; flex-shrink: 0;
}
.main-header .hash { color: var(--text-muted); font-size: 16px; }
.menu-btn { display: none; }

/* ---------- Fora da chamada ---------- */
.lobby { flex: 1; display: grid; place-items: center; padding: 16px; }
.lobby-box { text-align: center; max-width: 420px; }
.lobby-box h2 { margin: 0 0 6px; font-size: 20px; }
.lobby-box p { margin: 0 0 18px; color: var(--text-muted); }
.lobby-rooms { display: grid; gap: 8px; }
.lobby-rooms button {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px; border: 0;
  border-radius: var(--radius); background: var(--bg-1); text-align: left;
}
.lobby-rooms button:hover { background: var(--bg-3); }
.lobby-rooms .count { margin-left: auto; color: var(--text-muted); font-size: 13px; }

/* ---------- Stage (vídeos) ---------- */
.stage {
  background: #000; flex: 1; display: flex; flex-direction: column;
  min-height: 0; position: relative;
}
.tiles {
  flex: 1; display: grid; gap: 8px; padding: 8px; min-height: 0;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  grid-auto-rows: minmax(0, 1fr); overflow: auto;
}
.tile {
  position: relative; background: var(--bg-1); border-radius: var(--radius);
  overflow: hidden; display: grid; place-items: center; min-height: 140px;
  outline: 2px solid transparent; transition: outline-color .1s;
}
.tile.speaking { outline-color: var(--green); }
.tile video { width: 100%; height: 100%; object-fit: contain; display: none; background: #000; }
.tile.has-video video { display: block; }
.tile.has-video .avatar { display: none; }
.tile.mirror video { transform: scaleX(-1); }
.tile .avatar { width: 80px; height: 80px; font-size: 32px; }
/* Câmera/avatar de quem apresenta, sobreposto à tela transmitida */
/* --x/--y vão de 0 a 1 (canto esquerdo/topo até direito/baixo), sempre dentro da tela */
.pip {
  position: absolute; z-index: 2; width: 22%; min-width: 96px; max-width: 260px;
  left: calc(10px + (100% - 20px) * var(--x, 1)); top: calc(10px + (100% - 20px) * var(--y, 1));
  transform: translate(calc(var(--x, 1) * -100%), calc(var(--y, 1) * -100%));
  aspect-ratio: 16 / 9; border-radius: var(--radius); overflow: hidden; background: var(--bg-1);
  display: grid; place-items: center; box-shadow: 0 4px 16px rgba(0, 0, 0, .5);
  outline: 2px solid transparent; transition: outline-color .1s;
  cursor: grab; touch-action: none; user-select: none;
}
.pip.dragging { cursor: grabbing; opacity: .85; }
.pip.speaking { outline-color: var(--green); }
.tile .pip video { display: none; object-fit: cover; pointer-events: none; }
.tile .pip.has-video video { display: block; }
.tile .pip .avatar { display: grid; width: 44px; height: 44px; font-size: 18px; }
.tile .pip.has-video .avatar { display: none; }
.pip.mirror video { transform: scaleX(-1); }
/* Sem câmera: só uma bolinha com o avatar */
.pip:not(.has-video) {
  width: 56px; min-width: 0; aspect-ratio: 1; border-radius: 50%;
  background: transparent; box-shadow: none; overflow: visible;
}
.pip-close {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; padding: 0;
  border: 0; border-radius: 50%; background: rgba(0, 0, 0, .7); font-size: 11px;
  display: grid; place-items: center; opacity: 0; transition: opacity .15s;
}
.pip:not(.has-video) .pip-close { top: -6px; right: -6px; }
.pip:hover .pip-close { opacity: 1; }

.tile-label {
  position: absolute; left: 8px; bottom: 8px; background: rgba(0, 0, 0, .6);
  padding: 2px 8px; border-radius: 4px; font-size: 13px;
}
.tile-stats {
  position: absolute; left: 8px; top: 8px; z-index: 3; background: rgba(0, 0, 0, .7);
  padding: 4px 8px; border-radius: 4px; font: 12px/1.4 ui-monospace, Consolas, monospace;
  white-space: pre; pointer-events: none;
}
.popover .preset {
  border: 0; border-radius: 4px; padding: 8px; font-weight: 700; cursor: pointer;
  background: var(--accent); color: #fff;
}
.tile-tools {
  position: absolute; right: 8px; top: 8px; display: flex; gap: 6px;
  opacity: 0; transition: opacity .15s;
}
.tile:hover .tile-tools { opacity: 1; }
.tile-tools button {
  background: rgba(0, 0, 0, .6); border: 0; border-radius: 4px; padding: 4px 8px;
}
.tile-tools input[type=range] { width: 90px; accent-color: var(--accent); }
.tile-tools .vol { display: flex; align-items: center; gap: 4px; background: rgba(0, 0, 0, .6); border-radius: 4px; padding: 0 6px; }

/* Foco: um tile ocupa tudo, os outros ficam numa faixa */
.tiles.has-focus { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.tiles.has-focus .tile:not(.focused) { display: none; }

/* Alguém transmitindo: a tela ocupa o espaço, as câmeras ficam numa faixa embaixo */
.tiles.has-screen { display: flex; flex-wrap: wrap; align-content: stretch; }
.tiles.has-screen .tile.screen { flex: 1 1 0; min-width: min(100%, 320px); height: 100%; }
.tiles.has-screen .tile:not(.screen) { flex: 0 0 180px; height: 110px; min-height: 0; }
.tiles.has-screen:has(.tile:not(.screen)) .tile.screen { height: calc(100% - 118px); }
.tiles.has-screen .tile:not(.screen) .avatar { width: 48px; height: 48px; font-size: 20px; }

.controls {
  display: flex; justify-content: center; gap: 12px; padding: 10px; flex-shrink: 0;
}
.ctl {
  width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--bg-3);
  font-size: 20px; display: grid; place-items: center; position: relative;
}
.ctl:hover { filter: brightness(1.2); }
.ctl.on { background: #fff; }
.ctl.danger { background: var(--red); }
.ctl.mini { width: 32px; height: 32px; font-size: 15px; align-self: center; margin-left: -8px; }

.popover {
  position: absolute; bottom: 72px; left: 50%; transform: translateX(-50%); z-index: 30;
  width: min(300px, calc(100% - 32px)); background: var(--bg-1); border-radius: var(--radius);
  padding: 14px 16px; display: grid; gap: 10px; box-shadow: 0 8px 30px rgba(0, 0, 0, .5);
  max-height: calc(100vh - 140px); overflow-y: auto;
}
.popover h3 { margin: 0; font-size: 15px; }
.popover label { display: grid; gap: 4px; font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); }
.popover select {
  background: var(--bg-0); color: var(--text); border: 0; border-radius: 4px;
  padding: 8px; font: inherit; text-transform: none; font-weight: 400;
}
.popover label.check { display: flex; align-items: center; gap: 8px; text-transform: none; font-size: 14px; color: var(--text); font-weight: 400; }
.popover label.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.popover small { color: var(--text-muted); font-size: 12px; }

/* ---------- Configurações de voz e vídeo ---------- */
.modal-bg {
  position: fixed; inset: 0; z-index: 45; background: rgba(0, 0, 0, .7);
  display: grid; place-items: center; padding: 16px;
}
.modal {
  position: relative; width: min(680px, 100%); max-height: calc(100dvh - 32px); overflow-y: auto;
  background: var(--bg-2); border-radius: 12px; padding: 24px 28px; display: grid; gap: 14px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5);
}
.modal h2 { margin: 4px 0 0; font-size: 20px; }
.modal h3 { margin: 10px 0 0; font-size: 15px; }
.modal-close {
  position: absolute; top: 14px; right: 14px; width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid var(--text-muted); background: transparent; color: var(--text-muted); font-size: 14px;
}
.modal-close:hover { color: var(--text); border-color: var(--text); }
.modal label { display: grid; gap: 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); }
.modal label .val { float: right; color: var(--text); font-weight: 400; }
.modal select {
  background: var(--bg-0); color: var(--text); border: 0; border-radius: 4px;
  padding: 10px; font: inherit; text-transform: none; font-weight: 400; min-width: 0;
}
.modal input[type=range] { accent-color: var(--accent); width: 100%; }
.modal small { color: var(--text-muted); font-size: 12px; text-transform: none; font-weight: 400; }
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 20px; }
.btn-accent {
  background: var(--accent); border: 0; border-radius: 4px; padding: 10px 18px; font-weight: 600; flex: none;
}
.btn-accent:hover { filter: brightness(1.1); }
.btn-accent.on { background: var(--red); }
.mic-test { display: flex; align-items: center; gap: 14px; }
.meter { flex: 1; height: 12px; border-radius: 6px; background: var(--bg-0); overflow: hidden; }
#set-meter-bar { height: 100%; width: 0; background: var(--green); transition: width .08s; }
.profiles { display: grid; gap: 6px; }
.modal label.radio, .modal label.check {
  display: flex; align-items: flex-start; gap: 10px; text-transform: none; font-size: 14px;
  font-weight: 400; color: var(--text); cursor: pointer;
}
.modal label.radio { padding: 8px 10px; border-radius: 6px; background: var(--bg-1); }
.modal label.radio:has(input:checked) { outline: 1px solid var(--accent); }
.modal label.radio span { display: grid; gap: 2px; }
.modal label.radio input, .modal label.check input { accent-color: var(--accent); width: 18px; height: 18px; margin: 2px 0 0; flex: none; }
.custom-opts { display: grid; gap: 8px; padding-left: 4px; }
.cam-test { display: grid; gap: 10px; justify-items: start; }
#set-cam-preview { width: min(320px, 100%); border-radius: 8px; background: #000; transform: scaleX(-1); }

/* ---------- Membros ---------- */
.members { background: var(--bg-1); overflow-y: auto; padding: 8px; }
.member { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 4px; }
.member:hover { background: var(--bg-hover); }
.member-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-tools { display: none; }
.member:hover .member-tools { display: flex; }
.member-tools .icon-btn { width: 26px; height: 26px; font-size: 13px; }
.member .avatar { position: relative; }
.member .avatar::after {
  content: ""; position: absolute; right: -2px; bottom: -2px; width: 10px; height: 10px;
  background: var(--green); border-radius: 50%; border: 3px solid var(--bg-1);
}

/* ---------- Avisos ---------- */
.banner {
  position: fixed; top: 0; left: 0; right: 0; background: var(--red); color: #fff;
  text-align: center; padding: 6px; z-index: 50; font-weight: 600;
}
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 60; }
.toast {
  background: var(--bg-0); padding: 10px 16px; border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0, 0, 0, .4); animation: fade 3.5s forwards;
}
@keyframes fade { 0%, 85% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- Celular / tela pequena ---------- */
/* Touch não tem "passar o mouse": ferramentas sempre visíveis */
@media (hover: none) {
  .tile-tools, .pip-close { opacity: 1; }
  .member-tools { display: flex; }
}
@media (max-width: 1000px) {
  .app { grid-template-columns: 240px 1fr; }
  .members { display: none; }
}
@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40;
    transform: translateX(-100%); transition: transform .2s;
    box-shadow: 4px 0 20px rgba(0, 0, 0, .5);
  }
  .sidebar.open { transform: none; }
  .menu-btn { display: grid; }
  .main-header { padding: 0 8px; }
  .tiles { grid-template-columns: 1fr; padding: 6px; gap: 6px; }
  .tile { min-height: 110px; }
  .tile .avatar { width: 56px; height: 56px; font-size: 22px; }
  .tile-tools input[type=range] { width: 64px; }
  .tiles.has-screen .tile:not(.screen) { flex-basis: 110px; height: 76px; }
  .tiles.has-screen:has(.tile:not(.screen)) .tile.screen { height: calc(100% - 82px); }
  .pip { width: 32%; min-width: 80px; }
  .controls { gap: 8px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); flex-wrap: wrap; }
  .ctl { width: 44px; height: 44px; font-size: 18px; }
  .popover { bottom: 64px; }
  .modal { padding: 20px 16px; }
  .set-grid { grid-template-columns: 1fr; }
  .lobby-rooms button { padding: 14px 16px; }
}
