:root {
  --orange: #e8551c;
  --orange-dark: #c24313;
  --orange-soft: #fdece3;
  /* Paleta clara neutra (cinza-branco), alinhada ao padrao de referencia -
     antes tinha um viés bege/amarronzado (#f3f1ec, #eae6dd etc). A sidebar
     continua neutra como já era, so o fundo/cards/bordas mudaram de tom. */
  --gray: #94969c;
  --gray-dark: #585b62;
  --bg: #f4f5f7;
  --bg-accent: #e9eaed;
  --card-bg: #ffffff;
  --card-bg-soft: #f7f8fa;
  --text: #1f2328;
  --text-soft: #6b7280;
  --border: #e3e5e9;
  --shadow: 0 10px 30px -12px rgba(20, 22, 26, 0.12);
  --shadow-sm: 0 4px 14px -6px rgba(20, 22, 26, 0.10);
  --sidebar-from: #4a4741;
  --sidebar-to: #2c2b28;
  --sidebar-text: #e8e6e1;
  --sidebar-text-soft: #cfcdc7;
  --focus-ring: rgba(232, 85, 28, 0.28);
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 10px;
  /* Cor de destaque (nav ativo, botao primario, links, cabecalhos de mensagem).
     No tema claro segue igual ao laranja da marca; o escuro sobrescreve abaixo. */
  --accent: var(--orange);
  --accent-dark: var(--orange-dark);
  --accent-soft: var(--orange-soft);
}

:root[data-theme="dark"] {
  /* Cinza-azulado neutro (nao acinzentado com viés laranja/marrom), no mesmo
     tom frio da sidebar. */
  --bg: #1b1c1f;
  --bg-accent: #212226;
  --card-bg: #24252a;
  --card-bg-soft: #1e1f23;
  --text: #e9eaec;
  --text-soft: #a3a6ac;
  --border: #34363c;
  --orange: #ef6a35;
  --orange-dark: #ff8955;
  --orange-soft: #3a2418;
  --gray: #9498a0;
  --gray-dark: #c1c4c9;
  --shadow: 0 14px 36px -14px rgba(0, 0, 0, 0.55);
  --shadow-sm: 0 6px 16px -8px rgba(0, 0, 0, 0.5);
  /* Chumbo azulado neutro, sem tingimento laranja - pedido explicito de nao
     usar laranja na sidebar. */
  --sidebar-from: #171a20;
  --sidebar-to: #0c0e12;
  --sidebar-text: #e7e9ec;
  --sidebar-text-soft: #9aa0aa;
  --focus-ring: rgba(239, 106, 53, 0.35);
  /* Destaque segue o laranja da marca tambem no escuro - so em tom mais claro
     (--orange* acima), para contrastar com o fundo escuro. O que NAO leva
     laranja e o fundo da sidebar/cards, que ficam no cinza neutro frio. */
  --accent: var(--orange);
  --accent-dark: var(--orange-dark);
  --accent-soft: var(--orange-soft);
}

* { box-sizing: border-box; }

html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }
html, body { overflow-x: hidden; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s ease, color 0.2s ease;
}

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

/* ---------- Layout ---------- */

.layout {
  display: flex;
  min-height: 100vh;
  gap: 20px;
  padding: 20px;
}

.nav-toggle {
  display: none;
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 40;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--sidebar-from);
  color: #fff;
  font-size: 18px;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: linear-gradient(165deg, var(--sidebar-from), var(--sidebar-to));
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 26px 18px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  height: calc(100vh - 40px);
  /* Fixa mesmo, nao sticky: sticky ainda rola junto se algum ancestral tiver
     overflow, e some da tela em paginas mais longas. */
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 30;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 26px;
  padding: 0 6px;
}

.sidebar .brand img { width: 36px; height: 36px; border-radius: 10px; }
.sidebar .brand span { font-weight: 700; font-size: 17px; letter-spacing: 0.3px; }

/* overflow-x explicito: com apenas `overflow-y: auto`, o CSS promove o
   overflow-x de `visible` para `auto` - e qualquer deslocamento horizontal no
   hover fazia surgir uma barra de rolagem no rodape da sidebar. */
.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar nav a {
  color: var(--sidebar-text);
  padding: 10px 14px;
  border-radius: 999px;
  font-size: 14px;
  text-decoration: none;
  /* O leve avanco do hover vem do padding, nao de translateX: transform
     transborda a caixa (gerando a rolagem) e a caixa aqui ja ocupa a largura
     toda do nav. */
  transition: background 0.15s ease, padding-left 0.15s ease;
}

.sidebar nav a:hover { background: rgba(255,255,255,0.09); padding-left: 18px; }
.sidebar nav a.active { background: var(--accent); color: #fff; font-weight: 600; box-shadow: 0 4px 14px -4px var(--accent-shadow, rgba(232,85,28,0.6)); }
:root[data-theme="dark"] .sidebar nav a.active { box-shadow: 0 4px 14px -4px rgba(239,106,53,0.5); }

.theme-toggle {
  margin-left: auto;
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--sidebar-text-soft);
  opacity: 0.55;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
  transition: opacity 0.15s ease, background 0.15s ease;
}
.theme-toggle:hover { opacity: 1; background: rgba(255,255,255,0.08); }
.theme-icon { font-size: 13px; line-height: 1; }

.sidebar .footer-links { display: flex; flex-direction: column; gap: 4px; border-top: 1px solid rgba(255,255,255,0.08); padding-top: 14px; }
.sidebar .footer-links a { color: var(--sidebar-text-soft); font-size: 13px; padding: 8px 14px; border-radius: 999px; }
.sidebar .footer-links a:hover { background: rgba(255,255,255,0.08); text-decoration: none; }
.sidebar .footer-links button {
  background: none; border: none; color: var(--sidebar-text-soft); font-size: 13px;
  padding: 8px 14px; text-align: left; cursor: pointer; font-family: inherit; border-radius: 999px; width: 100%;
}
.sidebar .footer-links button:hover { color: #fff; background: rgba(255,255,255,0.08); }

.whoami { padding: 4px 14px 10px; display: flex; flex-direction: column; gap: 2px; }
.whoami-name { font-size: 13px; font-weight: 600; color: #fff; }
.whoami-role { font-size: 11px; color: var(--sidebar-text-soft); text-transform: uppercase; letter-spacing: 0.4px; }

.main {
  flex: 1;
  /* Compensa a sidebar fixa (232px + 20px de gap) que saiu do fluxo. */
  margin-left: 252px;
  padding: 8px 12px 32px;
  max-width: 1160px;
  min-width: 0;
}

h1 { font-size: 23px; margin: 0 0 20px; letter-spacing: -0.2px; }
h2 { font-size: 17px; margin: 0 0 12px; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.page-head h1 { margin: 0; }

/* ---------- Cards ---------- */

.cards { display: flex; gap: 16px; margin-bottom: 28px; flex-wrap: wrap; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 22px;
  min-width: 160px;
  flex: 1 1 160px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

.card .label { font-size: 13px; color: var(--text-soft); margin-bottom: 6px; }
.card .value { font-size: 28px; font-weight: 700; color: var(--accent-dark); }

/* Card com 3 sub-valores lado a lado (ex: novos contatos por dia/semana/mes),
   em vez de um numero unico ocupando o card inteiro. */
.card-multi { flex: 1 1 220px; }
.multi-stat { display: flex; gap: 18px; margin-top: 4px; }
.multi-stat > div { text-align: center; }
.multi-stat-value { display: block; font-size: 20px; font-weight: 700; color: var(--accent-dark); }
.multi-stat-label { display: block; font-size: 11px; color: var(--text-soft); text-transform: uppercase; letter-spacing: 0.3px; margin-top: 2px; }

/* ---------- Dashboard charts ---------- */

.chart-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px; }
@media (max-width: 900px) {
  .chart-row { grid-template-columns: 1fr; }
}

.chart-panel h2 { display: flex; align-items: baseline; gap: 8px; }
.chart-panel h2 .muted { font-size: 12px; font-weight: 400; }

.chart-legend { display: flex; gap: 14px; margin-bottom: 8px; flex-wrap: wrap; }
.chart-legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-soft); }
.chart-legend-item i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.chart-svg { display: block; overflow: visible; }
.chart-baseline { stroke: var(--border); stroke-width: 1; }
.chart-gridline { stroke: var(--border); stroke-width: 1; stroke-dasharray: 3 4; opacity: .7; }
.chart-axis-label { fill: var(--text-soft); font-size: 11px; }
.chart-value-label { fill: var(--text-soft); font-size: 11px; font-weight: 600; }

.chart-summary { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.chart-summary-series { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--text-soft); }
.chart-summary-name { display: inline-flex; align-items: center; gap: 6px; min-width: 88px; }
.chart-summary-name i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.chart-summary-metric b { color: var(--text); font-weight: 600; }

/* ---------- Contatos mais ativos ---------- */

.top-contacts { display: flex; flex-direction: column; gap: 10px; }
.top-contact-row { display: flex; align-items: center; gap: 12px; }
.top-contact-rank {
  width: 24px; height: 24px; border-radius: 50%; flex-shrink: 0;
  background: var(--bg-accent); color: var(--text-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
}
.top-contact-info { flex: 1; min-width: 0; }
.top-contact-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-contact-meta { font-size: 12px; color: var(--text-soft); }
.top-contact-bar-wrap { flex-shrink: 0; width: 90px; }
.top-contact-bar-track { background: var(--bg-accent); border-radius: 999px; height: 6px; overflow: hidden; }
.top-contact-bar-fill { background: var(--accent); height: 100%; border-radius: 999px; }
.top-contact-count { font-size: 13px; font-weight: 700; color: var(--accent-dark); min-width: 26px; text-align: right; }

/* ---------- Tables ---------- */

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

table { width: 100%; border-collapse: collapse; background: var(--card-bg); border-radius: var(--radius-md); overflow: hidden; }
th, td { text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 14px; }
th { background: var(--bg-accent); color: var(--text-soft); font-weight: 600; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--card-bg-soft); }

.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.row-actions form { display: inline; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--bg-accent); color: var(--text-soft); }
.badge.ok { background: #e5f4e6; color: #2c7a34; }
.badge.error { background: #fbe6e2; color: var(--orange-dark); }
:root[data-theme="dark"] .badge.ok { background: #1d3a22; color: #7fd48c; }
:root[data-theme="dark"] .badge.error { background: #3a231c; color: #ff9d78; }

/* "erro tecnico": falha que chegou ao cliente. Vermelho solido para separar do
   badge de status 'error', que marca qualquer falha (inclusive recuperada). */
.badge.tech-error { background: #c0392b; color: #fff; }
:root[data-theme="dark"] .badge.tech-error { background: #e0503c; color: #fff; }

/* ---------- Logs ---------- */

.logs-search {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.logs-search input[type=search] { flex: 1 1 280px; min-width: 0; }

/* Resumo em uma linha: o texto completo fica na tela de detalhes. */
.logs-table td.nowrap { white-space: nowrap; }
.logs-table .log-summary {
  max-width: 460px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-block {
  background: var(--card-bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin: 0;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 460px;
  overflow-y: auto;
}

.detail-grid {
  display: grid;
  grid-template-columns: minmax(120px, max-content) 1fr;
  gap: 8px 18px;
  margin: 0;
}
.detail-grid dt { font-size: 13px; font-weight: 600; color: var(--text-soft); }
.detail-grid dd { margin: 0; font-size: 14px; overflow-wrap: anywhere; }

@media (max-width: 620px) {
  .detail-grid { grid-template-columns: 1fr; gap: 2px 0; }
  .detail-grid dd { margin-bottom: 10px; }
  .logs-table .log-summary { max-width: 200px; }
}

/* ---------- Panels & forms ---------- */

.panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}

.form-panel { max-width: 760px; }

form .field { margin-bottom: 16px; display: flex; flex-direction: column; gap: 6px; }
label { font-size: 13px; font-weight: 600; color: var(--text-soft); }

/* Formulario em grade de 2 colunas: evita um cartao estreito no meio de uma
   tela larga. Campos marcados .full (ex: um select isolado) ocupam a linha
   inteira; o resto se pareia automaticamente (usuario+email, senha+confirmar). */
.form-panel form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 24px;
}
.form-panel form .field { margin-bottom: 0; }
.form-panel form .field.full { grid-column: 1 / -1; }
.form-panel form button { grid-column: 1 / -1; justify-self: start; margin-top: 8px; }

@media (max-width: 640px) {
  .form-panel form { grid-template-columns: 1fr; }
}

input[type=text], input[type=password], input[type=number], input[type=url], input[type=email], input[type=search], select, textarea {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--card-bg-soft);
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--focus-ring);
}
input:disabled { opacity: 0.6; }

button, .btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: inline-block;
  transition: background 0.15s ease, transform 0.1s ease;
}
button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; transform: translateY(-1px); }
button.secondary, .btn.secondary { background: var(--bg-accent); color: var(--text); }
button.secondary:hover, .btn.secondary:hover { background: var(--border); }
button.danger, .btn.danger { background: var(--orange-soft); color: var(--orange-dark); }
button.danger:hover, .btn.danger:hover { background: var(--orange); color: #fff; }

.alert { padding: 13px 18px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 14px; }
.alert.error { background: #fbe6e2; color: var(--orange-dark); }
.alert.success { background: #e5f4e6; color: #2c7a34; }
:root[data-theme="dark"] .alert.error { background: #3a231c; color: #ff9d78; }
:root[data-theme="dark"] .alert.success { background: #1d3a22; color: #7fd48c; }

/* ---------- Aviso fixo de WhatsApp desconectado (todas as telas) ----------
   Vermelho proprio, nao o laranja da marca: precisa ler como "algo quebrou",
   nao como um destaque comum do painel. Fica grudado no topo (sticky) enquanto
   o problema existir - some sozinho quando a conexao volta (js/connection.js). */
.global-alert {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  gap: 16px;
  align-items: flex-start;
  margin: 0 0 22px;
  padding: 18px 22px;
  border: 1px solid #f0b09e;
  border-left: 6px solid #d0331a;
  border-radius: var(--radius-md);
  background: #fdeae5;
  color: #7c2310;
  box-shadow: var(--shadow);
}
/* display:flex venceria o atributo hidden do HTML. */
.global-alert[hidden] { display: none; }

.global-alert-icon {
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #d0331a;
  color: #fff;
  font-size: 21px;
  font-weight: 700;
  line-height: 34px;
  text-align: center;
}

.global-alert-body { min-width: 0; }
.global-alert h2 { font-size: 17px; margin: 3px 0 8px; color: inherit; }
.global-alert p { margin: 0 0 8px; font-size: 14px; line-height: 1.5; }
.global-alert ol { margin: 0 0 12px; padding-left: 20px; font-size: 14px; line-height: 1.6; }
.global-alert-state { font-size: 13px; opacity: 0.85; }
.global-alert-actions { margin: 14px 0 0; }
.global-alert .btn { background: #d0331a; color: #fff; }
.global-alert .btn:hover { background: #a92814; }
:root[data-theme="dark"] .global-alert .btn { background: #e2553a; color: #24120c; }
:root[data-theme="dark"] .global-alert .btn:hover { background: #ff7355; }
.global-alert ol:last-child, .global-alert p:last-child { margin-bottom: 0; }

:root[data-theme="dark"] .global-alert {
  background: #3b1c14;
  border-color: #6d3324;
  border-left-color: #e2553a;
  color: #ffbba4;
}
:root[data-theme="dark"] .global-alert-icon { background: #e2553a; color: #24120c; }

@media (max-width: 720px) {
  .global-alert { padding: 15px 16px; gap: 12px; }
  .global-alert h2 { font-size: 15px; }
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-soft);
}
.empty-state h1 { color: var(--text); }

/* ---------- Login ---------- */

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--sidebar-from), var(--sidebar-to));
  padding: 20px;
}

.login-card {
  background: var(--card-bg);
  padding: 40px 34px;
  border-radius: 26px;
  width: 100%;
  max-width: 340px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
}

.login-card img { width: 60px; height: 60px; margin-bottom: 14px; border-radius: 14px; }
.login-card h1 { font-size: 19px; margin-bottom: 22px; }
.login-card form { text-align: left; }

/* ---------- Conversation ---------- */

.conv-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.conv-head-title { min-width: 0; }
.conv-head-title p:first-child { margin: 0 0 6px; }
.conv-head-title h1 { margin: 0 0 4px; }
.conv-head-title .muted { margin: 0; }

.conv-search { display: flex; align-items: center; gap: 10px; }
.conv-search input[type=search] { width: 260px; }
.conv-search .muted { min-width: 38px; text-align: center; font-size: 13px; }
.conv-search-nav { display: flex; gap: 8px; }

/* Botoes de navegacao da busca: circulo com borda propria, bem mais visivel
   que um .btn.secondary achatado - contraste claro entre habilitado/desabilitado. */
.search-nav-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--card-bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
}
.search-nav-btn:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); color: #fff; transform: translateY(-1px); }
.search-nav-btn:disabled { opacity: 0.35; box-shadow: none; cursor: default; }

@media (max-width: 640px) {
  .conv-head { flex-direction: column; align-items: stretch; }
  .conv-search input[type=search] { flex: 1; width: auto; }
}

mark.search-hit {
  background: rgba(255, 214, 0, 0.4);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
}
mark.search-hit.active { background: #ffd600; color: #1f2328; }

.msg-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 65vh;
  overflow-y: auto;
}
.msg-list::-webkit-scrollbar { width: 8px; }
.msg-list::-webkit-scrollbar-track { background: transparent; }
.msg-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
.msg-list::-webkit-scrollbar-thumb:hover { background: var(--text-soft); }
.msg-bubble {
  max-width: 70%;
  padding: 11px 15px;
  border-radius: 18px;
  font-size: 14px;
  background: var(--card-bg-soft);
  border: 1px solid var(--border);
}
.msg-bubble.from-me { align-self: flex-end; background: var(--accent-soft); border-color: transparent; border-bottom-right-radius: 6px; }
.msg-bubble:not(.from-me) { border-bottom-left-radius: 6px; }
.msg-meta { font-size: 11px; color: var(--text-soft); margin-top: 4px; }

.msg-sender { font-size: 12px; font-weight: 600; color: var(--accent-dark); margin-bottom: 3px; }

.msg-text { overflow-wrap: anywhere; }
.msg-text > *:first-child { margin-top: 0; }
.msg-text > *:last-child { margin-bottom: 0; }
.msg-text p { margin: 0 0 8px; line-height: 1.5; }
.msg-text h3, .msg-text h4, .msg-text h5, .msg-text h6 {
  margin: 12px 0 6px; line-height: 1.3; color: var(--accent-dark);
}
.msg-text h3 { font-size: 16px; }
.msg-text h4 { font-size: 15px; }
.msg-text h5, .msg-text h6 { font-size: 14px; }
.msg-text ul, .msg-text ol { margin: 0 0 8px; padding-left: 20px; }
.msg-text li { margin-bottom: 3px; line-height: 1.5; }
.msg-text hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.msg-text blockquote {
  margin: 0 0 8px; padding: 2px 0 2px 10px;
  border-left: 3px solid var(--border); color: var(--text-soft);
}
.msg-text code {
  background: rgba(0,0,0,0.08); padding: 1px 4px; border-radius: 4px;
  font-family: ui-monospace, Consolas, monospace; font-size: 13px;
}
:root[data-theme="dark"] .msg-text code { background: rgba(255,255,255,0.1); }
.msg-text pre {
  background: rgba(0,0,0,0.08); padding: 10px 12px; border-radius: var(--radius-sm);
  margin: 0 0 8px; overflow-x: auto;
}
:root[data-theme="dark"] .msg-text pre { background: rgba(255,255,255,0.08); }
.msg-text pre code { background: none; padding: 0; }
.msg-text .table-wrap { overflow-x: auto; margin: 0 0 8px; box-shadow: none; }
.msg-text table { background: transparent; border: 1px solid var(--border); font-size: 13px; }
.msg-text th, .msg-text td { padding: 6px 9px; font-size: 13px; }
.msg-text th { background: rgba(0,0,0,0.05); }
.msg-text tr:hover td { background: transparent; }

.msg-bubble:has(table) { max-width: 92%; }

.pill-nav { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.pill-nav a { padding: 7px 16px; border-radius: 999px; background: var(--bg-accent); font-size: 13px; color: var(--text); }
.pill-nav a.active { background: var(--accent); color: #fff; }

.muted { color: var(--text-soft); font-size: 13px; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .layout { flex-direction: column; padding: 0; gap: 0; }

  .nav-toggle { display: flex; align-items: center; justify-content: center; }

  .sidebar {
    top: 0;
    left: 0;
    height: 100vh;
    width: 250px;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 50;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 0 2000px rgba(0,0,0,0.4); }

  .main { margin-left: 0; padding: 66px 16px 32px; max-width: 100%; overflow-x: auto; }

  .cards { gap: 12px; }
  .card { min-width: 130px; padding: 14px 16px; }
  .card .value { font-size: 22px; }

  .panel { padding: 18px; border-radius: var(--radius-md); }
  .form-panel { max-width: 100%; }

  th, td { padding: 10px 12px; font-size: 13px; }

  .msg-bubble { max-width: 88%; }
}

@media (max-width: 520px) {
  .page-head { flex-direction: column; align-items: flex-start; }
  .row-actions { justify-content: flex-start; flex-wrap: wrap; }
}

/* ---------- Tela de pareamento (QR Code) ---------- */

.qr-area { display: flex; gap: 26px; flex-wrap: wrap; align-items: flex-start; }

.qr-frame {
  position: relative;
  flex: 0 0 auto;
  width: 288px;
  min-height: 288px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  /* Fundo branco fixo, nao var(--card-bg): no tema escuro um QR com fundo
     escuro nao e lido pela camera. */
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.qr-frame img { width: 260px; height: 260px; display: block; image-rendering: pixelated; }
.qr-frame .muted { color: #6b7280; text-align: center; }

.qr-expired {
  position: absolute;
  inset: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 12px;
  font-size: 14px;
  font-weight: 600;
  color: #7c2310;
  background: rgba(253, 234, 229, 0.93);
  border-radius: var(--radius-sm);
}
.qr-expired[hidden] { display: none; }

.qr-side { flex: 1 1 260px; min-width: 240px; }
.qr-side h3 { font-size: 14px; margin: 18px 0 4px; }
.qr-side .muted { font-size: 13px; line-height: 1.55; }

.pairing-code {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 4px;
  color: var(--accent);
}

@media (max-width: 560px) {
  .qr-frame { width: 100%; min-height: 0; }
  .qr-frame img { width: 100%; height: auto; }
}

/* ---------- Chat de teste (Configuracoes) ---------- */

.test-chat-list {
  height: 340px;
  margin: 14px 0;
  padding: 14px;
  background: var(--card-bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.test-chat-list .muted { margin: auto; text-align: center; }

.test-chat-form { display: flex; flex-direction: column; gap: 10px; }
.test-chat-form textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  background: var(--card-bg);
  color: var(--text);
}
.test-chat-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Tela de Configuracoes: grade de cards recolhiveis ----------

   A tela tinha uma coluna so de cartoes empilhados dentro do limite de 1160px
   do .main, o que deixava metade da largura vazia a direita em telas grandes e
   obrigava a rolar muito para achar uma configuracao. Aqui ela usa a largura
   toda e distribui os cartoes em colunas, cada um podendo ser recolhido. */

.main-wide { max-width: none; }

.settings-grid {
  display: grid;
  /* auto-fill + minmax: 1 coluna no celular, 2 no notebook, 3+ no monitor
     grande - sem media query por faixa de largura. */
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  /* Respiro maior entre linhas do que entre colunas: cartoes recolhidos viram
     barras baixas e, com o mesmo gap dos dois lados, a tela fica apertada. */
  gap: 28px 24px;
  /* Cartoes de alturas diferentes na mesma linha nao esticam junto com o mais
     alto: cada um ocupa so o que precisa. */
  align-items: start;
  margin-bottom: 32px;
}
.settings-grid > .panel { margin-bottom: 0; }

/* Cartoes com conteudo largo (prompt, provedores de IA). O span so entra
   quando ha largura para 2+ colunas - em 1 coluna, `span 2` criaria uma
   coluna implicita e estouraria a tela para o lado. */
@media (min-width: 1180px) {
  .settings-grid > .panel-wide { grid-column: span 2; }
}

/* Linha inteira (chat de teste): `1 / -1` e seguro em qualquer numero de
   colunas, inclusive uma so. */
.settings-grid > .panel-full { grid-column: 1 / -1; }

/* Cabecalho clicavel. E um <button> (teclado/leitor de tela funcionam de
   graca), entao precisa desfazer o estilo do botao laranja padrao. */
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  margin: 0;
  padding: 0;
  background: none;
  border: none;
  border-radius: 0;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.card-head:hover { background: none; transform: none; }
.card-head:hover h2 { color: var(--accent); }
.card-head h2 { margin: 0; }
.card-head:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }

.card-chevron {
  flex: none;
  color: var(--text-soft);
  transition: transform 0.18s ease;
}
.panel.collapsed .card-chevron { transform: rotate(-90deg); }
.panel.collapsed .card-body { display: none; }
.card-body { margin-top: 16px; }

/* Resumo curto no cabecalho (ex.: "3 de 5 chaves"), visivel tambem recolhido. */
.card-tag {
  flex: none;
  margin-left: auto;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  color: var(--text-soft);
  background: var(--bg-accent);
  border-radius: 999px;
  padding: 3px 9px;
}

.settings-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 4px 0 22px;
}
.settings-toolbar button { padding: 7px 14px; font-size: 13px; }

/* Barra de salvar: com os cartoes espalhados pela tela, um botao no fim da
   pagina ficaria longe demais de onde se esta editando. */
.settings-save {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 14px 20px;
  /* Espaco proprio: colada nos cartoes, a barra parecia parte do ultimo card. */
  margin: 8px 0 36px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
}
.settings-save .muted { margin: 0; }

/* Linha de uma chave do rodizio da Gabby. */
.key-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.key-row span { min-width: 62px; }
.key-row input { flex: 1; min-width: 0; }

@media (max-width: 900px) {
  .settings-grid { grid-template-columns: 1fr; }
}

/* ---------- Minha conta ----------

   Dois cartoes curtos, empilhados numa coluna de 760px, deixavam quase toda a
   tela vazia a direita. Lado a lado (e em coluna unica dentro de cada um, para
   nao sobrar meia-linha vazia com 3 campos) a tela fica cheia sem esticar
   campo de senha por 1000px. */

.account-grid {
  display: grid;
  /* Sem max-width: os cartoes ocupam a largura toda (a tela usa `wide`).
     minmax(320px) da 3 colunas no monitor e 1 no celular. */
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 24px;
  align-items: start;
}
.account-grid > .panel { margin-bottom: 0; }

/* Anula a grade de 2 colunas de .form-panel form: dentro de um cartao estreito
   ela criava celulas vazias ("Confirmar nova senha" sozinha na linha). */
.form-panel-stack { max-width: none; }
.form-panel-stack form { grid-template-columns: 1fr; gap: 14px; }
.form-panel-stack form button { margin-top: 4px; }
