/* ===== Design tokens ===== */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --border: #e6e9ef;
  --text: #11181c;
  --text-soft: #5b6573;
  --text-faint: #8b95a3;
  --primary: #ce2a22;
  --primary-strong: #a91f18;
  --primary-soft: #fdecea;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --warn: #7a5805;
  --warn-soft: #fdf3d3;
  --warn-border: #eec45a;
  --warn-accent: #d99a0b;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow-md: 0 6px 24px rgba(16, 24, 40, .08);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
/* Garante que o atributo [hidden] vença regras de display (flex) — senão
   login e chat ficam ambos visíveis empilhados. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}

/* ===== Brand mark ===== */
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 56px; height: 56px;
  border-radius: 16px;
  background: linear-gradient(160deg, #e23a30, #b5241c);
  color: #fff;
  font-weight: 700;
  letter-spacing: .5px;
  font-size: 1.1rem;
  box-shadow: var(--shadow-md);
}
.brand-mark.sm { width: 38px; height: 38px; border-radius: 11px; font-size: .8rem; box-shadow: none; }

/* Logo ASI em SVG (substitui o quadrado com texto "ASI"). */
.logo-asi { display: block; width: 44px; height: 44px; flex: 0 0 auto; }
.logo-asi.sm { width: 34px; height: 34px; }

/* ===== Login ===== */
.login {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-md);
  padding: 36px 32px;
}
.brand { text-align: center; margin-bottom: 28px; }
.brand h1 { font-size: 1.35rem; font-weight: 700; margin-top: 16px; }
.brand-sub { color: var(--text-soft); font-size: .9rem; margin-top: 6px; }

#login-form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: .8rem; font-weight: 600; color: var(--text-soft); }
input {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: .95rem;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: var(--text-faint); }
input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

button {
  font-family: inherit;
  font-size: .95rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s, opacity .15s, transform .05s;
}
button:active { transform: translateY(1px); }
#login-btn {
  padding: 13px;
  background: var(--primary);
  color: #fff;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-top: 4px;
}
#login-btn:hover { background: var(--primary-strong); }
#login-btn:disabled { opacity: .6; cursor: wait; }

.erro { color: var(--danger); font-size: .82rem; min-height: 1.1em; text-align: center; }
.login-foot { color: var(--text-faint); font-size: .78rem; }

/* ===== Spinner ===== */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255, 255, 255, .4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Layout com sidebar ===== */
.layout {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 264px 1fr;
}

.sidebar {
  background: #0f172a;
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 12px;
  overflow: hidden;
}
.nova-conversa {
  background: rgba(255, 255, 255, .08);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, .14);
  padding: 11px;
  font-size: .88rem;
  text-align: left;
}
.nova-conversa:hover { background: rgba(255, 255, 255, .16); }
.lista-conversas { display: flex; flex-direction: column; gap: 3px; overflow-y: auto; }
.lista-conversas::-webkit-scrollbar { width: 6px; }
.lista-conversas::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 6px; }
.conv-item {
  text-align: left; background: transparent; color: #cbd5e1;
  border: none; padding: 9px 10px; border-radius: 8px;
  font-size: .84rem; font-weight: 500; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item:hover { background: rgba(255, 255, 255, .07); color: #fff; }
.conv-item.ativa { background: rgba(37, 83, 235, .35); color: #fff; }
.conv-item small { display: block; font-size: .7rem; color: #7c8aa0; font-weight: 400; }
.sidebar-vazia { color: #64748b; font-size: .8rem; padding: 8px 10px; }

.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40;
}

.icon-btn {
  display: none; background: transparent; color: var(--text-soft);
  border: none; font-size: 1.2rem; padding: 4px 8px; cursor: pointer;
}

/* ===== Chat (área principal) ===== */
.chat {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-brand { display: flex; align-items: center; gap: 12px; }
.topbar-titles { display: flex; flex-direction: column; }
.topbar-titles strong { font-size: .95rem; }
.topbar-titles small { color: var(--text-faint); font-size: .76rem; }
.ghost {
  background: transparent;
  color: var(--text-soft);
  padding: 8px 14px;
  border: 1px solid var(--border);
}
.ghost:hover { background: #f1f3f7; color: var(--text); }

/* ===== Mensagens ===== */
.mensagens {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 22px 18px;
  scroll-behavior: smooth;
}
.mensagens::-webkit-scrollbar { width: 8px; }
.mensagens::-webkit-scrollbar-thumb { background: #d3d9e2; border-radius: 8px; }

.msg {
  max-width: 86%;
  padding: 13px 16px;
  border-radius: var(--radius);
  font-size: .92rem;
  animation: rise .22s ease both;
}
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.msg.user {
  align-self: flex-end;
  /* Vermelho suavizado pro balão (menos saturado que o --primary cheio da
     marca, que fica reservado pra logo/botões/acentos). */
  background: #d96a64;
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  width: 86%;
  box-shadow: var(--shadow-sm);
}
.msg.bot.erro { background: var(--danger-soft); border-color: var(--danger-border); }

.msg.bot .summary { font-weight: 600; font-size: .96rem; color: var(--text); }
.msg.bot.erro .summary { color: #991b1b; }

.insights { list-style: none; margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.insights li {
  font-size: .85rem; color: var(--text-soft);
  padding-left: 18px; position: relative;
}
.insights li::before { content: "▸"; position: absolute; left: 0; color: var(--primary); }

/* Aviso de gráfico (ex: "falta um campo de tempo... quer ver em barras?").
   Callout amarelo discreto: separa a orientação do resto do texto sem alarmar
   como um erro vermelho. */
.aviso-grafico {
  margin-top: 12px;
  display: flex;
  gap: 9px;
  align-items: flex-start;
  background: var(--warn-soft);
  border: 1px solid var(--warn-border);
  border-left: 3px solid var(--warn-accent);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: .85rem;
  line-height: 1.4;
  color: var(--warn);
}
.aviso-grafico .icone { flex: 0 0 auto; font-size: .95rem; line-height: 1.4; }
.aviso-grafico .texto { flex: 1; }

.btn-visualizar {
  margin-top: 12px;
  align-self: flex-start;
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--primary);
  border-radius: 999px;
  padding: 5px 14px;
  font-size: .82rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background .15s ease, color .15s ease;
}
.btn-visualizar:hover { background: var(--primary); color: #fff; }
.btn-visualizar:disabled { opacity: .5; cursor: not-allowed; }

.chart-options {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chart-options button {
  background: #f8fafc;
  color: #334155;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: .8rem;
  font-weight: 600;
}
.chart-options button:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}
.chart-options button:disabled {
  opacity: .55;
  cursor: wait;
}

.follow-actions {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.follow-actions button {
  background: #fff;
  color: #475569;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: .78rem;
  font-weight: 600;
}
.follow-actions button:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}

.table-result {
  margin-top: 12px;
}
.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.table-toolbar span {
  color: var(--text-faint);
  font-size: .76rem;
  font-weight: 600;
}
.table-toolbar button {
  background: #fff;
  color: #475569;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: .75rem;
  font-weight: 600;
}
.table-toolbar button:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}
.table-viewport {
  width: 100%;
  overflow: auto;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
}
.table-result-scroll .table-viewport {
  max-height: min(52vh, 520px);
  border: 1px solid var(--border);
}
.chart-tabela {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
  background: #fff;
}
.chart-tabela thead {
  background: #f1f5f9;
  position: sticky;
  top: 0;
  z-index: 1;
}
.chart-tabela th {
  text-align: left;
  padding: 8px 10px;
  font-weight: 600;
  font-size: .78rem;
  color: #475569;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.chart-tabela td {
  padding: 7px 10px;
  border-top: 1px solid #e2e8f0;
  color: #1e293b;
}
.chart-tabela tbody tr:hover { background: #f8fafc; }
.table-compact .chart-tabela {
  font-size: .78rem;
}
.table-compact .chart-tabela th {
  padding: 5px 8px;
  font-size: .72rem;
}
.table-compact .chart-tabela td {
  padding: 4px 8px;
}

.problemas { list-style: none; margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.problemas li { font-size: .82rem; color: #991b1b; padding-left: 18px; position: relative; }
.problemas li::before { content: "⚠"; position: absolute; left: 0; }

.chart-wrap {
  margin-top: 14px;
  background: #fcfcfd;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.chart-wrap canvas {
  width: 100% !important;
  max-height: 360px;
}
.chart-wrap.chart-pie,
.chart-wrap.chart-doughnut,
.chart-wrap.chart-polarArea {
  width: min(100%, 780px);
  padding: 10px;
}
.chart-wrap.chart-pie canvas,
.chart-wrap.chart-doughnut canvas,
.chart-wrap.chart-polarArea canvas {
  max-height: 520px;
}

/* SQL colapsável */
details.sql-box { margin-top: 12px; }
details.sql-box summary {
  cursor: pointer; font-size: .76rem; color: var(--text-faint);
  user-select: none; list-style: none;
}
details.sql-box summary::-webkit-details-marker { display: none; }
details.sql-box summary::before { content: "▸ "; }
details.sql-box[open] summary::before { content: "▾ "; }
details.sql-box pre {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: .74rem; background: #f6f8fb; color: #334155;
  padding: 10px 12px; border-radius: 8px; margin-top: 8px;
  white-space: pre-wrap; overflow-x: auto; border: 1px solid var(--border);
}

/* Indicador "pensando" */
.typing { display: flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint);
  animation: blink 1.2s infinite both;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

/* Empty state */
.empty { margin: auto; text-align: center; color: var(--text-soft); max-width: 460px; }
.empty-icon { font-size: 2.4rem; }
.empty h2 { font-size: 1.25rem; color: var(--text); margin: 12px 0 6px; }
.empty p { font-size: .9rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-soft); font-size: .82rem; font-weight: 500;
  padding: 8px 14px; border-radius: 999px;
}
.chip:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }

/* Composer */
.composer {
  display: flex; gap: 10px; align-items: center;
  padding: 14px 18px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
#pergunta {
  flex: 1; padding: 13px 16px; border-radius: 999px;
  border: 1px solid var(--border); background: #fff;
}
#enviar {
  width: 46px; height: 46px; flex: none;
  display: grid; place-items: center;
  background: var(--primary); color: #fff; border-radius: 50%;
}
#enviar:hover { background: var(--primary-strong); }
#enviar:disabled { opacity: .5; cursor: wait; }

/* Disclaimer abaixo do composer */
.disclaimer {
  text-align: center;
  font-size: .72rem;
  color: var(--text-faint);
  padding: 6px 18px 12px;
  background: var(--surface);
}

/* Meta de tokens/custo na bolha do bot (estilo ChatGPT) */
.msg.bot .meta {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: .72rem;
  color: var(--text-faint);
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.msg.bot .meta .tok { font-variant-numeric: tabular-nums; }

/* Mobile */
@media (max-width: 700px) {
  .layout { grid-template-columns: 1fr; }
  .icon-btn { display: inline-block; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 264px; z-index: 50;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.aberta { transform: translateX(0); }
}
@media (min-width: 701px) {
  .sidebar-backdrop { display: none !important; }
}
@media (max-width: 600px) {
  .msg, .msg.bot { max-width: 95%; width: auto; }
  .topbar-titles strong { font-size: .88rem; }
  .chart-wrap canvas { max-height: 300px; }
  .chart-wrap.chart-pie,
  .chart-wrap.chart-doughnut,
  .chart-wrap.chart-polarArea { width: min(100%, 680px); }
  .chart-wrap.chart-pie canvas,
  .chart-wrap.chart-doughnut canvas,
  .chart-wrap.chart-polarArea canvas { max-height: 460px; }
}

/* ===== Modal de Export ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal[hidden] { display: none !important; }
.modal-card {
  background: #fff;
  border-radius: 12px;
  width: min(560px, 92vw);
  max-height: 88vh;
  overflow: auto;
  padding: 1.25rem 1.5rem;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: .5rem;
}
.modal-header h3 { margin: 0; font-size: 1.1rem; }
.modal-header .icon-btn {
  display: inline-block;
  color: #64748b;
  font-size: 1.4rem;
  line-height: 1;
}
.modal-header .icon-btn:hover { color: #0f172a; }
.modal-aviso {
  font-size: .85rem;
  color: #475569;
  background: #f1f5f9;
  padding: .55rem .8rem;
  border-radius: 8px;
  margin: .6rem 0;
}
.modal-aviso-warn {
  background: #fef3c7;
  color: #78350f;
}
.export-turnos {
  display: flex;
  flex-direction: column;
  gap: .4rem;
  margin: .75rem 0;
  max-height: 260px;
  overflow: auto;
}
.export-turno {
  display: flex;
  align-items: flex-start;
  gap: .6rem;
  padding: .55rem .8rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  transition: background .12s ease;
}
.export-turno:hover { background: #f8fafc; }
.export-turno input { margin-top: .2rem; flex-shrink: 0; }
.export-turno-info { display: flex; flex-direction: column; gap: .15rem; min-width: 0; }
.export-turno-pergunta {
  font-weight: 500;
  font-size: .92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.export-turno-meta {
  font-size: .78rem;
  color: #64748b;
}
.export-formato {
  border: none;
  padding: 0;
  margin: .6rem 0 0;
}
.export-formato legend { font-size: .9rem; font-weight: 500; padding-bottom: .25rem; }
.export-formato label {
  display: block;
  padding: .35rem 0;
  cursor: pointer;
  font-size: .92rem;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: .5rem;
  margin-top: 1rem;
}
button.primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: .55rem 1.1rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: .95rem;
}
button.primary:hover { background: var(--primary-strong); }
button.primary:disabled { background: #94a3b8; cursor: not-allowed; }

/* ===== Admin ===== */
.admin-layout { min-height: 100vh; display: flex; flex-direction: column; background: #f8fafc; }
.admin-content { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1rem 3rem; width: 100%; }
.admin-resumo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.admin-card {
  background: #fff;
  border-radius: 12px;
  padding: 1.1rem 1.25rem;
  display: flex; flex-direction: column; gap: .35rem;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
  border-left: 4px solid var(--primary);
}
.admin-card-ok { border-left-color: #16a34a; }
.admin-card-err { border-left-color: #dc2626; }
.admin-card-label { font-size: .78rem; color: #64748b; text-transform: uppercase; letter-spacing: .04em; }
.admin-card-value { font-size: 1.6rem; font-weight: 600; color: #0f172a; }

.admin-table {
  width: 100%;
  background: #fff;
  border-collapse: collapse;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.admin-table th, .admin-table td { padding: .7rem .9rem; text-align: left; font-size: .9rem; }
.admin-table thead { background: #f1f5f9; }
.admin-table th { font-weight: 600; color: #475569; font-size: .8rem; text-transform: uppercase; }
.admin-table tbody tr { border-top: 1px solid #e2e8f0; }
.admin-table tbody tr:hover { background: #f8fafc; }
.admin-table .pergunta-cel { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-badge {
  display: inline-block; padding: .15rem .55rem; border-radius: 12px;
  font-size: .75rem; font-weight: 500;
}
.admin-badge-ok { background: #dcfce7; color: #166534; }
.admin-badge-err { background: #fee2e2; color: #991b1b; }
.admin-acao { color: var(--primary); cursor: pointer; background: none; border: none; font-size: .85rem; font-weight: 500; }
.admin-acao:hover { text-decoration: underline; }
.admin-loading { text-align: center; color: #64748b; padding: 2rem; }

.admin-paginacao {
  display: flex; align-items: center; justify-content: center; gap: 1rem;
  margin-top: 1.25rem;
}

/* Modal de detalhe — herda .modal mas com card maior */
.admin-detalhe-card { width: min(820px, 94vw); max-height: 90vh; }
.admin-detalhe-card .admin-kv {
  display: grid; grid-template-columns: 160px 1fr;
  gap: .45rem 1rem; margin: .5rem 0;
}
.admin-detalhe-card .admin-kv dt { color: #64748b; font-size: .85rem; font-weight: 500; }
.admin-detalhe-card .admin-kv dd { margin: 0; font-size: .9rem; word-break: break-word; }
.admin-detalhe-card pre {
  background: #0f172a; color: #e2e8f0;
  padding: .75rem; border-radius: 8px;
  overflow: auto; font-size: .8rem;
  max-height: 280px;
}
.admin-detalhe-card h4 { margin: 1rem 0 .4rem; font-size: .95rem; color: #334155; }
