:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --accent: #4f46e5;
  --accent-contrast: #ffffff;
  --bubble-mine: #4f46e5;
  --bubble-mine-text: #ffffff;
  --bubble-other: #eef2ff;
  --bubble-other-text: #1e1b4b;
  --danger: #dc2626;
  --ok: #059669;
  --badge: #ef4444;
  --shadow: 0 1px 3px rgba(0, 0, 0, .1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #172033;
    --border: #334155;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --accent: #6366f1;
    --bubble-mine: #6366f1;
    --bubble-mine-text: #ffffff;
    --bubble-other: #334155;
    --bubble-other-text: #e2e8f0;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

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

a { color: var(--accent); text-decoration: none; }

/* ---------------------------------------------------------------- Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); }
.topnav { display: flex; align-items: center; gap: 16px; }
.topnav a { color: var(--text-muted); font-size: 14px; }
.topnav a:hover { color: var(--accent); }
.topuser { font-size: 14px; color: var(--text-muted); }
.btn-sair {
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px;
  color: var(--text) !important; font-size: 13px;
}

/* ---------------------------------------------------------------- Flashes */
.flashes { padding: 8px 16px; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 8px; font-size: 14px; }
.flash-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.flash-success { background: #ecfdf5; color: var(--ok); border: 1px solid #a7f3d0; }

/* ---------------------------------------------------------------- Login */
.tela-login {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border-radius: 16px; padding: 32px; box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.login-logo { text-align: center; margin-bottom: 24px; }
.login-logo h1 { margin: 12px 0 4px; font-size: 24px; }
.login-logo p { margin: 0; color: var(--text-muted); font-size: 14px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); }
.login-form input[type="email"], .login-form input[type="password"] {
  padding: 11px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font-size: 15px;
}
.login-form .lembrar { flex-direction: row; align-items: center; gap: 8px; }
.login-form button {
  margin-top: 6px; padding: 12px; border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-contrast); font-size: 15px;
  font-weight: 600; cursor: pointer;
}
.login-form button:hover { filter: brightness(1.05); }

/* ---------------------------------------------------------------- Layout chat */
.conteudo { height: calc(100vh - 52px); }
.chat {
  display: grid; grid-template-columns: 320px 1fr; height: 100%;
}
.sidebar {
  border-right: 1px solid var(--border); background: var(--surface);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { padding: 12px; border-bottom: 1px solid var(--border); }
.btn-nova {
  width: 100%; padding: 10px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: var(--accent-contrast); font-weight: 600; font-size: 14px;
}
.busca-conversas {
  width: 100%; margin-top: 10px; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface-2); color: var(--text); font-size: 14px;
}
.lista-conversas { flex: 1; overflow-y: auto; min-height: 0; }
.conv-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.ativa { background: var(--bubble-other); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 15px;
}
.avatar.grupo { background: #7c3aed; }
.conv-corpo { flex: 1; min-width: 0; }
.conv-titulo { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.conv-titulo strong { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-previa { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag-externo { font-size: 10px; background: #f59e0b; color: #fff; padding: 1px 6px; border-radius: 999px; margin-left: 4px; }
.badge {
  background: var(--badge); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center;
}

/* ---------------------------------------------------------------- Painel */
.painel { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.painel-vazio { display: flex; align-items: center; justify-content: center; height: 100%; color: var(--text-muted); }
.painel-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.painel-head .titulo { font-weight: 600; }
.painel-head .subtitulo { font-size: 12px; color: var(--text-muted); }
.painel-head .acoes { margin-left: auto; display: flex; gap: 8px; }
.painel-head .acoes button {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); cursor: pointer; font-size: 13px;
}
.mensagens { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 14px; font-size: 14px; line-height: 1.35; word-wrap: break-word; }
.msg .autor { font-size: 12px; font-weight: 600; margin-bottom: 2px; opacity: .85; }
.msg .hora { font-size: 10px; opacity: .7; margin-top: 3px; text-align: right; }
.msg.minha { align-self: flex-end; background: var(--bubble-mine); color: var(--bubble-mine-text); border-bottom-right-radius: 4px; }
.msg.outra { align-self: flex-start; background: var(--bubble-other); color: var(--bubble-other-text); border-bottom-left-radius: 4px; }
.msg.sistema { align-self: center; background: transparent; color: var(--text-muted); font-size: 12px; }
.digitando { padding: 0 16px 6px; height: 18px; font-size: 12px; color: var(--text-muted); font-style: italic; }
.composer {
  display: flex; gap: 8px; padding: 12px 16px; background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; height: 42px; max-height: 140px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2);
  color: var(--text); font-size: 14px; font-family: inherit;
}
.composer button {
  padding: 0 18px; border: 0; border-radius: 12px; background: var(--accent);
  color: #fff; font-weight: 600; cursor: pointer;
}
.ws-status { font-size: 11px; padding: 2px 8px; border-radius: 999px; }
.ws-on { color: var(--ok); }
.ws-off { color: var(--danger); }

/* ---------------------------------------------------------------- Modal */
.modal-bg {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .5); display: none;
  align-items: center; justify-content: center; padding: 20px; z-index: 50;
}
.modal-bg.aberto { display: flex; }
.modal {
  width: 100%; max-width: 440px; background: var(--surface); border-radius: 14px;
  padding: 20px; box-shadow: var(--shadow); max-height: 85vh; overflow-y: auto;
}
.modal h3 { margin: 0 0 14px; }
.modal .abas { display: flex; gap: 8px; margin-bottom: 14px; }
.modal .abas button {
  flex: 1; padding: 8px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); cursor: pointer;
}
.modal .abas button.ativa { background: var(--accent); color: #fff; border-color: var(--accent); }
.modal input, .modal textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font-size: 14px; margin-bottom: 10px; font-family: inherit;
}
.resultados-busca { display: flex; flex-direction: column; gap: 4px; }
.res-item {
  display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; cursor: pointer;
}
.res-item:hover { background: var(--surface-2); }
.res-nome { font-size: 14px; font-weight: 600; }
.res-email { font-size: 12px; color: var(--text-muted); }
.modal .rodape { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.modal .rodape button { padding: 9px 16px; border-radius: 10px; border: 0; cursor: pointer; }
.btn-primario { background: var(--accent); color: #fff; }
.btn-secundario { background: var(--surface-2); color: var(--text); border: 1px solid var(--border) !important; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { background: var(--bubble-other); color: var(--bubble-other-text); padding: 4px 10px; border-radius: 999px; font-size: 13px; }
.chip button { border: 0; background: none; cursor: pointer; color: inherit; margin-left: 4px; }

/* ---------------------------------------------------------------- Admin */
.admin { max-width: 900px; margin: 0 auto; padding: 24px 16px; }
.admin h2 { margin-top: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; margin-bottom: 18px; }
.tabela { width: 100%; border-collapse: collapse; }
.tabela th, .tabela td { text-align: left; padding: 10px; border-bottom: 1px solid var(--border); font-size: 14px; }
.tabela th { color: var(--text-muted); font-weight: 600; }
.form-inline { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.form-inline label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-muted); }
.form-inline input, .form-inline select {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface-2); color: var(--text); font-size: 14px;
}
.btn { padding: 9px 16px; border: 0; border-radius: 9px; background: var(--accent); color: #fff; cursor: pointer; font-size: 14px; }
.btn-mini { padding: 5px 10px; font-size: 12px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; }
.switch { display: inline-flex; align-items: center; gap: 8px; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; }
.pill-on { background: #ecfdf5; color: var(--ok); }
.pill-off { background: #fef2f2; color: var(--danger); }

/* ---------------------------------------------------------------- Responsivo */
@media (max-width: 720px) {
  .chat { grid-template-columns: 1fr; }
  .chat.ver-painel .sidebar { display: none; }
  .chat:not(.ver-painel) .painel { display: none; }
}
