:root {
  --bg: #0b0e14;
  --panel: #131824;
  --panel-2: #1b2130;
  --panel-3: #232a3b;
  --accent: #6c8cff;
  --accent-2: #8b5cf6;
  --text: #e6e9f0;
  --muted: #9aa3b5;
  --danger: #ef4444;
  --ok: #22c55e;
  --speaking: #22c55e;
  --border: rgba(255, 255, 255, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
}

.hidden { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
:focus:not(:focus-visible) { outline: none; }

/* ---------------- Auth (login / convite / troca senha) ---------------- */
.auth-view {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 50% -10%, rgba(108, 140, 255, 0.18), transparent 70%),
    var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.brand h1 { font-size: 22px; margin: 0; font-weight: 800; letter-spacing: -0.5px; }
.brand h1 span { color: var(--accent); }
.brand-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 12px var(--accent);
  flex: none;
}

.subtitle { color: var(--muted); font-size: 14px; margin: 0 0 24px; }

.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 12px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel-2);
  color: var(--text); font-size: 15px; outline: none;
}
.field input:focus { border-color: var(--accent); }

.btn-primary {
  width: 100%; padding: 13px; border: none; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-size: 15px; font-weight: 700; cursor: pointer; transition: opacity 0.15s;
}
.btn-primary:hover { opacity: 0.92; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); cursor: pointer; font-size: 14px;
}
.btn-secondary:hover { background: var(--panel-3); }

.error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 12px 0 0; }
.auth-footer { margin-top: 24px; color: var(--muted); font-size: 12px; }

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

/* Sidebar */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  display: flex; align-items: center; gap: 8px;
  padding: 16px; border-bottom: 1px solid var(--border); font-size: 15px;
}
.channels-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 6px; color: var(--muted); font-size: 11px; letter-spacing: 0.5px;
}
.channel-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; min-height: 60px; }
.channel-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; color: var(--muted);
  font-size: 14px; margin-bottom: 2px;
}
.channel-item:hover { background: var(--panel-2); color: var(--text); }
.channel-item.active { background: var(--panel-3); color: var(--text); }
.channel-item .hash { opacity: 0.6; }
.channel-item .chan-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-item .chan-count { font-size: 11px; color: var(--muted); }
.channel-item .del-chan {
  opacity: 0; border: none; background: transparent; color: var(--muted);
  cursor: pointer; font-size: 14px; padding: 0 4px;
}
.channel-item:hover .del-chan { opacity: 1; }
.channel-item .del-chan:hover { color: var(--danger); }

/* membros dentro de um canal (mini lista) */
.channel-members { list-style: none; margin: 2px 0 6px; padding: 0 0 0 26px; }
.channel-member {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted); padding: 3px 0;
}
.channel-member.speaking { color: var(--speaking); }
.channel-member .mini-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none;
  box-shadow: none; transition: background 0.1s, box-shadow 0.1s;
}
.channel-member.speaking .mini-dot { background: var(--speaking); box-shadow: 0 0 8px var(--speaking); }
.channel-member .m-icon { font-size: 11px; }

.sidebar-foot {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-top: 1px solid var(--border); background: var(--panel-2);
}
.me { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.me-name { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.status-dot.online { background: var(--ok); box-shadow: 0 0 8px var(--ok); }

.icon-btn {
  border: none; background: transparent; color: var(--muted);
  cursor: pointer; font-size: 16px; padding: 6px; border-radius: 8px; line-height: 1;
}
.icon-btn:hover { background: var(--panel-3); color: var(--text); }

/* Main */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.main-head {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.main-title { font-size: 16px; font-weight: 600; }
.voice-mode { font-size: 13px; color: var(--muted); }
.switch-label { display: flex; align-items: center; gap: 8px; cursor: pointer; }

/* Palco: por padrao so o chat (grande). Com video, divide em fatias iguais. */
.stage-area { flex: 1; display: flex; min-height: 0; min-width: 0; overflow: hidden; }
.stage-grid {
  display: none; /* escondido ate haver video/tela */
  gap: 6px; padding: 6px; overflow: hidden;
}
/* Com transmissao ativa: o grid de video ocupa o espaco e o chat vira lateral. */
.app.has-video .stage-grid {
  display: grid;
  flex: 1;
  min-width: 0;
  min-height: 0;
  height: 100%;
}
.app.has-video .chat { width: 320px; flex: none; border-left: 1px solid var(--border); }

/* Divisao em fatias IGUAIS que preenchem toda a area, conforme a quantidade.
   Desktop (paisagem): divide em colunas. Cada tile preenche sua fatia. */
.stage-grid[data-count="1"] { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.stage-grid[data-count="2"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
.stage-grid[data-count="3"] { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr; }
.stage-grid[data-count="4"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.stage-grid[data-count="5"], .stage-grid[data-count="6"] { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }
.stage-grid[data-count="7"], .stage-grid[data-count="8"], .stage-grid[data-count="9"] { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; }
.stage-grid[data-count="many"] { grid-template-columns: repeat(4, 1fr); grid-auto-rows: 1fr; }

/* Tiles preenchem a fatia inteira (sem aspect-ratio fixo, sem sobra). */
.app.has-video .stage-grid .tile {
  aspect-ratio: auto !important;
  width: 100%; height: 100%;
  min-height: 0; min-width: 0;
  margin: 0;
}
.app.has-video .stage-grid .tile video {
  width: 100%; height: 100%; object-fit: cover;
}
/* Sem video: o chat ocupa todo o palco central */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }

.empty-hint { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }

.tile {
  position: relative; background: var(--panel-2); border: 2px solid var(--border);
  border-radius: 14px; aspect-ratio: 16 / 10; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  transition: border-color 0.1s, box-shadow 0.1s;
}
.tile.speaking { border-color: var(--speaking); box-shadow: 0 0 0 2px var(--speaking); }
.tile video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.tile .avatar {
  width: 84px; height: 84px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: flex; align-items: center; justify-content: center;
  font-size: 34px; font-weight: 700; color: #fff;
}
.tile .name-tag {
  position: absolute; left: 10px; bottom: 10px; background: rgba(0, 0, 0, 0.55);
  padding: 4px 10px; border-radius: 8px; font-size: 13px;
  display: flex; align-items: center; gap: 6px;
}
.tile .muted-icon { color: var(--danger); }

.status-bar { min-height: 20px; padding: 0 20px 6px; color: var(--muted); font-size: 13px; text-align: center; }

.controls {
  display: flex; gap: 14px; justify-content: center;
  padding: 16px; border-top: 1px solid var(--border); background: var(--panel);
}
.ctrl {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-width: 72px; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 12px; background: var(--panel-2); color: var(--text);
  cursor: pointer; transition: background 0.15s;
}
.ctrl:hover { background: var(--panel-3); }
.ctrl-icon { font-size: 22px; }
.ctrl-label { font-size: 12px; color: var(--muted); }
.ctrl.off { background: #2a2030; border-color: rgba(239, 68, 68, 0.5); }
.ctrl.off .ctrl-icon { filter: grayscale(1) brightness(0.7); }
.ctrl.active { background: rgba(108, 140, 255, 0.18); border-color: var(--accent); }
.ctrl-danger { background: rgba(239, 68, 68, 0.15); border-color: rgba(239, 68, 68, 0.4); }
.ctrl-danger:hover { background: rgba(239, 68, 68, 0.28); }

/* Users (agora na sidebar esquerda) */
.users-head { padding: 14px 16px 6px; color: var(--muted); font-size: 11px; letter-spacing: 0.5px; }
.user-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; max-height: 34%; }
.user-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; font-size: 14px; }
.user-row .u-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-row.offline { color: var(--muted); opacity: 0.7; }
.user-row .role-badge { font-size: 10px; color: var(--accent); border: 1px solid var(--accent); border-radius: 6px; padding: 0 4px; }
.user-row.speaking { color: var(--speaking); }
.user-row.speaking .status-dot { background: var(--speaking); box-shadow: 0 0 8px var(--speaking); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-card { width: 100%; max-width: 460px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 24px; }
.modal-card h2 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.invite-link-box { display: flex; gap: 8px; margin: 12px 0; }
.invite-link-box input { flex: 1; padding: 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font-size: 13px; }
.admin-list { list-style: none; margin: 0; padding: 0; max-height: 280px; overflow-y: auto; }
.admin-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); font-size: 14px; }
.admin-list .danger-link { color: var(--danger); background: transparent; border: none; cursor: pointer; font-size: 13px; }

/* Painel de audio (nivel + selecao mic + monitor) */
.audio-panel {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-top: 1px solid var(--border); background: var(--panel);
}
.mic-meter { display: flex; align-items: center; gap: 8px; }
.mic-meter-icon { font-size: 16px; }
.mic-meter-track {
  width: 140px; height: 8px; border-radius: 4px; background: var(--panel-3); overflow: hidden;
}
.mic-meter-bar {
  height: 100%; width: 0%; border-radius: 4px;
  background: linear-gradient(90deg, var(--ok), #a3e635); transition: width 0.06s linear;
}
.mic-select {
  max-width: 220px; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font-size: 13px;
}
.chip-btn {
  padding: 7px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); cursor: pointer; font-size: 13px;
}
.chip-btn:hover { background: var(--panel-3); }
.chip-btn[aria-pressed="true"] { background: rgba(108,140,255,0.18); border-color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.gain-control { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.gain-control input[type="range"] { width: 90px; accent-color: var(--accent); }
.gain-val { min-width: 38px; text-align: right; font-variant-numeric: tabular-nums; }

/* Chat */
.chat-head { padding: 14px 16px; border-bottom: 1px solid var(--border); font-size: 14px; font-weight: 600; background: var(--panel); }
.chat-messages { list-style: none; margin: 0; padding: 12px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 10px; }
.chat-msg { display: flex; flex-direction: column; gap: 2px; }
.chat-msg .cm-head { display: flex; align-items: baseline; gap: 8px; }
.chat-msg .cm-name { font-size: 13px; font-weight: 600; color: var(--accent); }
.chat-msg .cm-time { font-size: 10px; color: var(--muted); }
.chat-msg .cm-text { font-size: 14px; color: var(--text); word-wrap: break-word; white-space: pre-wrap; }
.chat-msg.mine .cm-name { color: var(--accent-2); }
.chat-empty { color: var(--muted); font-size: 13px; text-align: center; margin: auto; }
.chat-attach-preview {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; margin: 0 12px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
}
.chat-attach-preview img { max-height: 60px; max-width: 100px; border-radius: 6px; object-fit: cover; }
.chat-attach-preview .ap-info { flex: 1; font-size: 13px; overflow: hidden; }
.chat-attach-preview .ap-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-attach-preview .ap-size { color: var(--muted); font-size: 11px; }
.chat-attach-preview .ap-remove { border: none; background: transparent; color: var(--danger); cursor: pointer; font-size: 16px; }
.chat-upload-bar { height: 4px; background: var(--panel-3); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.chat-upload-bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.1s; }

.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); align-items: center; }
.chat-attach-btn { border: none; background: transparent; color: var(--muted); cursor: pointer; font-size: 20px; padding: 4px 6px; border-radius: 8px; }
.chat-attach-btn:hover { background: var(--panel-3); color: var(--text); }
.chat-attach-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Anexos dentro das mensagens */
.cm-attach { margin-top: 4px; }
.cm-attach img { max-width: 100%; max-height: 300px; border-radius: 8px; cursor: pointer; display: block; }
.cm-file {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--panel-2); border: 1px solid var(--border); max-width: 280px;
}
.cm-file .cf-icon { font-size: 22px; }
.cm-file .cf-info { flex: 1; overflow: hidden; }
.cm-file .cf-name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cm-file .cf-size { font-size: 11px; color: var(--muted); }
.cm-file .cf-dl { border: none; background: var(--accent); color: #fff; border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 13px; text-decoration: none; }
.cm-file.expired { opacity: 0.6; }
.cm-file.expired .cf-dl { display: none; }
.cm-file .cf-expired { font-size: 11px; color: var(--danger); }
.chat-drop { outline: 2px dashed var(--accent); outline-offset: -8px; }
.chat-form input {
  flex: 1; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px; outline: none;
}
.chat-form input:focus { border-color: var(--accent); }
.chat-form input:disabled { opacity: 0.5; }
.chat-send {
  border: none; border-radius: 10px; padding: 0 16px; cursor: pointer; font-size: 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff;
}
.chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* Navegacao inferior mobile (escondida no desktop) */
.mobile-nav { display: none; }

@media (min-width: 821px) {
  /* desktop: layout de video lado a lado com chat */
  .app.has-video .stage-area { flex-direction: row; }
}

/* ============ MOBILE (<= 820px): painarel navegavel estilo app ============ */
@media (max-width: 820px) {
  .app {
    display: block;
    position: relative;
    height: 100%;
    overflow: hidden;
  }

  /* Sidebar e main viram paineis full-screen sobrepostos */
  .sidebar, .main {
    position: absolute;
    inset: 0;
    bottom: 56px; /* espaco para a nav inferior */
    width: 100%;
    border: none;
    transition: transform 0.2s ease;
  }

  /* Por padrao mostra a sidebar (canais). Alterna via data-mobile-panel */
  .sidebar { transform: translateX(0); z-index: 2; }
  .main { transform: translateX(100%); z-index: 1; }
  .app[data-mobile-panel="main"] .sidebar { transform: translateX(-100%); }
  .app[data-mobile-panel="main"] .main { transform: translateX(0); z-index: 3; }

  /* Sidebar mobile: canais e usuarios com mais espaco */
  .sidebar { display: flex; flex-direction: column; }
  .channel-list { flex: none; max-height: 45%; }
  .user-list { max-height: none; flex: 1; }
  .channel-item { padding: 12px 12px; font-size: 15px; }
  .user-row { padding: 10px 8px; font-size: 15px; }

  /* Area principal mobile */
  .main-head { padding: 12px 16px; }
  .stage-area { flex-direction: column; }
  .app.has-video .stage-grid { max-height: 55vh; }
  .chat { width: auto !important; border: none; }

  /* Mobile: fatias LADO A LADO (colunas iguais), cada uma cheia na altura.
     2 pessoas = duas colunas dividindo a tela verticalmente ao meio. */
  .app.has-video .stage-grid { display: flex !important; flex-direction: row; }
  .app.has-video .stage-grid .tile { flex: 1 1 0; width: auto; height: 100%; min-width: 0; }
  .chat-messages { padding: 12px; }
  .chat-msg .cm-text { font-size: 15px; }

  /* Controles de voz: barra compacta */
  .controls { padding: 10px; gap: 8px; flex-wrap: wrap; }
  .ctrl { min-width: 60px; padding: 8px 10px; }
  .ctrl-icon { font-size: 20px; }

  /* Painel de audio: rola horizontalmente para caber */
  .audio-panel { flex-wrap: nowrap; overflow-x: auto; gap: 10px; padding: 8px 12px; -webkit-overflow-scrolling: touch; }
  .audio-panel > * { flex: none; }
  .mic-select { max-width: 150px; }

  /* Voice mode (PTT toggle) menor */
  .main-head .voice-mode { font-size: 12px; }

  /* Barra de navegacao inferior */
  .mobile-nav {
    display: flex;
    position: absolute;
    left: 0; right: 0; bottom: 0;
    height: 56px;
    background: var(--panel);
    border-top: 1px solid var(--border);
    z-index: 10;
  }
  .mnav-btn {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    border: none; background: transparent; color: var(--muted);
    font-size: 20px; cursor: pointer;
  }
  .mnav-btn .mnav-label { font-size: 11px; }
  .mnav-btn.active { color: var(--accent); }
  .mnav-btn .mnav-badge {
    position: absolute; top: 6px; margin-left: 20px;
    background: var(--danger); color: #fff; border-radius: 10px;
    font-size: 10px; padding: 0 5px; min-width: 16px; text-align: center;
  }

  /* Modais ocupam mais espaco no celular */
  .modal-card { max-width: 92vw; max-height: 85vh; overflow-y: auto; }

  /* Login/registro: cartao ocupa a largura */
  .auth-card { max-width: 92vw; padding: 24px; }
}
