/* ═══════════════════════════════════════════
   LAYOUT PRINCIPAL — GRELHA DE 3 COLUNAS
═══════════════════════════════════════════ */
:root {
  --sidebar-w: 230px;
  --users-w:   210px;
  --topbar-h:   48px;
  --input-h:    68px;
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--users-w);
  grid-template-rows: var(--topbar-h) 1fr var(--input-h);
  grid-template-areas:
    "sidebar  topbar   userlist"
    "sidebar  msgs     userlist"
    "sidebar  input    userlist";
  height: 100vh;
  overflow: hidden;
}

/* ═══════════════════════════════════════════
   SIDEBAR — LISTA DE CANAIS
═══════════════════════════════════════════ */
#sidebar {
  grid-area: sidebar;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.server-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 0.92rem;
  flex-shrink: 0;
}

.server-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--error);
  flex-shrink: 0;
  transition: background 0.3s, box-shadow 0.3s;
}
.server-dot.conectado {
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
}
.server-dot.a-ligar {
  background: var(--warning);
  animation: pulsar 0.8s infinite;
}
@keyframes pulsar {
  50% { opacity: 0.3; }
}

.channels-section {
  flex: 1;
  overflow-y: auto;
  padding: 0.6rem 0;
}

.section-label {
  padding: 0.5rem 1rem 0.3rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-add {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  transition: color 0.15s;
  line-height: 1;
}
.section-add:hover {
  color: var(--text);
}

.channel-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.38rem 0.75rem 0.38rem 1rem;
  cursor: pointer;
  border-radius: 4px;
  margin: 0 0.4rem 1px;
  font-size: 0.9rem;
  color: var(--text2);
  transition: background 0.1s, color 0.1s;
}
.channel-item:hover {
  background: var(--surface2);
  color: var(--text);
}
.channel-item.ativo {
  background: var(--surface3);
  color: var(--text);
  font-weight: 600;
}

.ch-hash {
  color: var(--muted);
  font-size: 1rem;
  flex-shrink: 0;
}
.channel-item.ativo .ch-hash {
  color: var(--text2);
}

.ch-nome {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ch-fechar {
  opacity: 0;
  color: var(--muted);
  font-size: 0.72rem;
  transition: opacity 0.1s;
  cursor: pointer;
  padding: 0 2px;
}
.channel-item:hover .ch-fechar {
  opacity: 1;
}
.ch-fechar:hover {
  color: var(--error);
}

.ch-nao-lido {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  display: none;
}
.channel-item.nao-lido .ch-nao-lido {
  display: block;
}
.channel-item.nao-lido {
  color: var(--text);
}

/* Painel do utilizador no fundo da sidebar */
.user-panel {
  border-top: 1px solid var(--border);
  padding: 0.6rem 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-shrink: 0;
  min-height: 56px;
}

.user-panel-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface2);
  border: 2px solid var(--border2);
  flex-shrink: 0;
}

.user-panel-info {
  flex: 1;
  min-width: 0;
}

.user-panel-nome {
  font-size: 0.85rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-panel-sub {
  font-size: 0.7rem;
  color: var(--muted);
  font-family: var(--mono);
}

.panel-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.15s;
}
.panel-btn:hover {
  background: var(--surface2);
  color: var(--text);
}

/* ═══════════════════════════════════════════
   TOPBAR
═══════════════════════════════════════════ */
#topbar {
  grid-area: topbar;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 1rem;
  z-index: 10;
}

.tb-hash {
  color: var(--muted);
  font-size: 1.3rem;
  font-weight: 300;
}

.tb-canal {
  font-weight: 700;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.tb-divisor {
  width: 1px;
  height: 20px;
  background: var(--border2);
  flex-shrink: 0;
}

.tb-topico {
  font-size: 0.82rem;
  color: var(--text2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.tb-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 4px;
  transition: color 0.15s;
  font-size: 0.9rem;
}
.tb-btn:hover {
  color: var(--text);
}

/* ═══════════════════════════════════════════
   ÁREA DE MENSAGENS
═══════════════════════════════════════════ */
#msgs {
  grid-area: msgs;
  overflow-y: auto;
  padding: 0.5rem 0;
  display: flex;
  flex-direction: column;
}

.history-divisor {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.04em;
}
.history-divisor::before,
.history-divisor::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* Linha de mensagem */
.msg {
  display: grid;
  grid-template-columns: 72px 1fr;
  padding: 0.12rem 1rem;
  transition: background 0.08s;
}
.msg:hover {
  background: rgba(255, 255, 255, 0.025);
}

.msg.destaque {
  background: rgba(250, 166, 26, 0.08);
  border-left: 2px solid var(--warning);
  padding-left: calc(1rem - 2px);
}
.msg.propria {
  background: rgba(0, 180, 216, 0.04);
}
.msg.historico {
  opacity: 0.78;
}

/* Coluna do avatar */
.msg-esquerda {
  display: flex;
  justify-content: flex-end;
  padding-right: 12px;
  padding-top: 2px;
  flex-shrink: 0;
}

.msg-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.15s;
  background: var(--surface2);
  flex-shrink: 0;
}
.msg-avatar:hover {
  transform: scale(1.06);
}

.msg-avatar-ph {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

/* Mensagens consecutivas — esconder avatar */
.msg.consecutiva .msg-avatar,
.msg.consecutiva .msg-avatar-ph {
  display: none;
}

.msg-hora-sm {
  display: none;
  font-family: var(--mono);
  font-size: 0.6rem;
  color: var(--muted);
  align-items: center;
  justify-content: flex-end;
  padding-top: 3px;
  width: 40px;
}
.msg:hover.consecutiva .msg-hora-sm {
  display: flex;
}

/* Conteúdo da mensagem */
.msg-corpo {
  min-width: 0;
}

.msg-cabecalho {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 1px;
}

.msg-nick {
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.msg-nick:hover {
  text-decoration: underline;
}
.msg-nick.e-op    { color: var(--warning) !important; }
.msg-nick.e-voice { color: var(--success) !important; }
.msg-nick.e-reg   { border-bottom: 1px dotted currentColor; }

.msg-hora {
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--muted);
}

.hist-tag {
  font-size: 0.6rem;
  font-family: var(--mono);
  color: var(--muted);
  background: var(--surface2);
  padding: 0 4px;
  border-radius: 2px;
  border: 1px solid var(--border2);
}

.msg-texto {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text);
  word-break: break-word;
}
.msg-texto a {
  color: var(--accent);
}
.msg-texto a:hover {
  text-decoration: underline;
}

/* Tipos especiais de mensagem */
.msg.sistema .msg-texto  { color: var(--muted); font-size: 0.82rem; font-style: italic; }
.msg.acao .msg-texto     { color: var(--text2); font-style: italic; }
.msg.aviso .msg-texto    { color: var(--warning); }
.msg.erro-linha .msg-texto { color: var(--error); }

.msg.sistema,
.msg.aviso,
.msg.erro-linha {
  grid-template-columns: 16px 1fr;
}

/* Estado vazio */
.estado-vazio {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.9rem;
  gap: 0.5rem;
  text-align: center;
  padding: 2rem;
}
.estado-vazio .icone {
  font-size: 2.5rem;
  opacity: 0.35;
  margin-bottom: 0.25rem;
}

/* ═══════════════════════════════════════════
   ÁREA DE INPUT
═══════════════════════════════════════════ */
#input-area {
  grid-area: input;
  background: var(--surface2);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
}

.input-caixa {
  flex: 1;
  display: flex;
  align-items: center;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border2);
  padding: 0 0.9rem;
  gap: 0.6rem;
  transition: border-color 0.15s;
}
.input-caixa:focus-within {
  border-color: var(--accent);
}

.input-canal {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--accent);
  flex-shrink: 0;
  opacity: 0.6;
}

#msgInput {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: var(--sans);
  font-size: 0.92rem;
  outline: none;
  padding: 0.75rem 0;
}
#msgInput::placeholder {
  color: var(--muted);
}
#msgInput:disabled {
  opacity: 0.3;
}

/* ═══════════════════════════════════════════
   LISTA DE UTILIZADORES
═══════════════════════════════════════════ */
#userlist {
  grid-area: userlist;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.ul-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
  flex-shrink: 0;
  gap: 0.4rem;
}

#ulInner {
  flex: 1;
  overflow-y: auto;
  padding: 0.4rem 0;
}

.ul-seccao {
  padding: 1rem 0.8rem 0.3rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.ul-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  border-radius: 4px;
  margin: 0 0.4rem 1px;
  transition: background 0.1s;
}
.ul-item:hover {
  background: var(--surface2);
}

.ul-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface2);
  flex-shrink: 0;
}

.ul-avatar-ph {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 0.72rem;
}

.ul-nick {
  font-size: 0.85rem;
  color: var(--text2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.ul-nick.registado {
  color: var(--text);
  font-weight: 500;
}

.ul-modo {
  font-size: 0.72rem;
  flex-shrink: 0;
}
.ul-modo.op    { color: var(--warning); }
.ul-modo.voice { color: var(--success); }

/* ═══════════════════════════════════════════
   TOOLTIP DE PERFIL
═══════════════════════════════════════════ */
#profileTip {
  position: fixed;
  z-index: 500;
  background: var(--surface3);
  border: 1px solid var(--border2);
  border-radius: 8px;
  width: 248px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.6);
  display: none;
  overflow: hidden;
  animation: tipEntrar 0.12s ease;
}
@keyframes tipEntrar {
  from { opacity: 0; transform: scale(0.94); }
}

.tip-banner {
  height: 60px;
  background: linear-gradient(135deg, #0a2035 0%, #160d30 100%);
}

.tip-corpo {
  padding: 0 1rem 1rem;
  margin-top: -24px;
}

.tip-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid var(--surface3);
  background: var(--surface2);
}

.tip-nick {
  font-weight: 700;
  font-size: 1rem;
  margin-top: 0.35rem;
}

.tip-username {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.1rem;
}

.tip-linha {
  height: 1px;
  background: var(--border);
  margin: 0.75rem 0 0.6rem;
}

.tip-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.tip-bio {
  font-size: 0.82rem;
  color: var(--text2);
  line-height: 1.45;
}

.tip-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.7rem;
  font-family: var(--mono);
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  border: 1px solid rgba(0, 180, 216, 0.2);
  margin-top: 0.5rem;
}

/* ═══════════════════════════════════════════
   MODAIS
═══════════════════════════════════════════ */
.modal-fundo {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 300;
  backdrop-filter: blur(2px);
}
.modal-fundo.aberto {
  display: flex;
}

.modal-cartao {
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 1.5rem;
  width: 380px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: modalEntrar 0.15s ease;
}
@keyframes modalEntrar {
  from { opacity: 0; transform: scale(0.96) translateY(8px); }
}

.modal-titulo {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 0.35rem;
}

.modal-sub {
  font-size: 0.82rem;
  color: var(--text2);
  margin-bottom: 1.2rem;
}

.modal-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.88rem;
  padding: 0.65rem 0.9rem;
  border-radius: 4px;
  outline: none;
  margin-bottom: 1rem;
  transition: border-color 0.15s;
}
.modal-input:focus {
  border-color: var(--accent);
}

.modal-botoes {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

/* Modal de perfil */
.modal-perfil {
  width: 440px;
  max-height: 88vh;
  overflow-y: auto;
}

.perfil-banner {
  height: 80px;
  background: linear-gradient(135deg, #0a2035 0%, #160d30 100%);
  border-radius: 8px 8px 0 0;
  margin: -1.5rem -1.5rem 0;
}

.perfil-avatar-wrap {
  position: relative;
  display: inline-block;
  margin-top: -32px;
  margin-left: 1rem;
  cursor: pointer;
}

.perfil-avatar-wrap:hover::after {
  content: 'Alterar';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  color: white;
  font-weight: 700;
}

.perfil-grande-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid var(--surface2);
  background: var(--surface);
  display: block;
}

.perfil-feedback {
  font-size: 0.8rem;
  text-align: center;
  margin-top: 0.5rem;
  min-height: 1.2em;
  font-family: var(--mono);
}
.perfil-feedback.ok  { color: var(--success); }
.perfil-feedback.err { color: var(--error); }