/* ===== Tema de chat ========================================================
   Paleta neutra (navy), claro por padrão, escuro por prefers-color-scheme
   ou [data-theme="dark"]. Tipografia Inter. */

:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --card: #f6f7f9;
  --bg-hover: #eef0f4;
  --text: #1f2937;
  --text-dim: #6b7280;
  --muted: #6b7280;
  --text-faint: #8b94a3;
  --border: #e5e7eb;
  --border-strong: #d5d9e0;
  --accent: #1f3a5f;
  --accent-soft: #eaf0f7;
  --danger: #c0392b;
  --user-bubble: #eaf0f7;
  --sidebar: 232px;
  --font: 'Inter', 'Public Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'Geist Mono', ui-monospace, SFMono-Regular, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151b; --panel: #141920; --card: #1b222c;
    --bg-hover: #222b37;
    --text: #e7ebf0; --text-dim: #9aa5b4; --muted: #9aa5b4; --text-faint: #7f8a99;
    --border: #2a3340; --border-strong: #364150;
    --accent: #8fb4ea; --accent-soft: #1b2a3d;
    --danger: #f87171; --user-bubble: #1b2a3d;
  }
}
:root[data-theme="dark"] {
  --bg: #11151b; --panel: #141920; --card: #1b222c;
  --bg-hover: #222b37;
  --text: #e7ebf0; --text-dim: #9aa5b4; --muted: #9aa5b4; --text-faint: #7f8a99;
  --border: #2a3340; --border-strong: #364150;
  --accent: #8fb4ea; --accent-soft: #1b2a3d;
  --danger: #f87171; --user-bubble: #1b2a3d;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== Layout ============================================================ */
.app { display: flex; height: 100vh; width: 100%; background: var(--panel); }

.sidebar {
  width: var(--sidebar); min-width: var(--sidebar); height: 100%;
  display: flex; flex-direction: column;
  background: var(--card); border-right: 1px solid var(--border);
  padding: 14px 10px;
}
.sidebar-top { display: flex; flex-direction: column; gap: 6px; min-height: 0; flex: 1; }
.sidebar-bottom { display: flex; flex-direction: column; gap: 8px; padding-top: 10px; }

.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 10px; }
.brand-badge {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 14px; flex-shrink: 0;
}
.brand-badge.small { width: 24px; height: 24px; font-size: 12px; border-radius: 7px; }
.brand-text { min-width: 0; }
.brand-name { display: block; font-weight: 650; font-size: 14px; }

.chat-list { flex: 1; overflow-y: auto; padding: 2px; }
.chat-item {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px; border: 0; border-radius: 9px; font-size: 13px;
  color: var(--muted); cursor: pointer; background: transparent;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item:hover, .chat-item.active { background: var(--accent-soft); color: var(--text); }

.status-row { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); padding: 0 6px; }
.dot { width: 8px; height: 8px; border-radius: 999px; background: var(--border-strong); flex-shrink: 0; }
.dot.online { background: #34d399; }
.dot.offline { background: #9ca3af; }
.dot.planilha { background: #60a5fa; }

.clear-chat {
  margin: 0 6px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: transparent; color: var(--text-dim); font-size: 12.5px; text-align: left;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.clear-chat:hover { background: var(--bg-hover); color: var(--text); border-color: var(--border-strong); }

/* ===== Main ============================================================== */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 18px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--panel); z-index: 5;
}
.icon-btn {
  border: 0; background: transparent; color: var(--text);
  padding: 6px; border-radius: 8px; display: none;
}
.icon-btn:hover { background: var(--bg-hover); }
.topbar-title { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; min-width: 0; }
.topbar-status { font-size: 12px; color: var(--muted); white-space: nowrap; }

.planilha-chip {
  display: none; align-items: center; gap: 7px; max-width: 260px;
  padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--card); font-size: 12px; color: var(--text-dim);
}
.planilha-chip.show { display: inline-flex; }
.planilha-chip #planilhaNome {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.scroll { flex: 1; overflow-y: auto; padding: 22px 18px 40px; }
.chat { max-width: 760px; margin: 0 auto; }

.welcome { max-width: 760px; margin: 8vh auto 0; text-align: center; }
.welcome-logo { color: var(--accent); display: grid; place-items: center; margin-bottom: 10px; }
.welcome h1 { font-size: 20px; font-weight: 650; margin: 0 0 6px; }
.welcome p { color: var(--muted); font-size: 13.5px; margin: 0 0 18px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggestion {
  border: 1px solid var(--border); border-radius: 999px; background: var(--card);
  color: var(--text-dim); padding: 8px 13px; font-size: 12.5px;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}
.suggestion:hover { border-color: var(--accent); color: var(--text); background: var(--accent-soft); }

/* ===== Mensagens ========================================================= */
.msg { display: flex; gap: 10px; margin: 0 0 22px; }
.msg.user { justify-content: flex-end; }
.msg .bubble { min-width: 0; max-width: 100%; font-size: 14px; line-height: 1.55; }
.msg.user .bubble {
  max-width: 78%; background: var(--user-bubble); color: var(--text);
  padding: 10px 13px; border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .bubble { flex: 1; }
.msg.error .bubble {
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  color: var(--danger); padding: 10px 13px; border-radius: 12px;
  font-size: 13.5px;
}
.msg.interrupted .bubble {
  background: color-mix(in srgb, var(--text-dim) 8%, transparent);
  border: 1px solid var(--border);
  color: var(--text-dim); padding: 10px 13px; border-radius: 12px;
  font-size: 13.5px;
}

/* ===== Loading =========================================================== */
.loading { display: inline-flex; align-items: center; gap: 5px; padding: 4px 0; }
.loading span {
  width: 7px; height: 7px; border-radius: 999px; background: var(--text-faint);
  animation: pulsa 1.2s ease-in-out infinite;
}
.loading span:nth-child(2) { animation-delay: 0.15s; }
.loading span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulsa {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ===== Respostas do assistente (markdown) ================================= */
.msg.assistant { flex-direction: column; line-height: 1.6; overflow-wrap: anywhere; }
.msg.assistant > .bubble > :first-child { margin-top: 0; }
.msg.assistant > .bubble > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 .75em; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 .75em; padding-left: 1.4em; }
.msg.assistant li { margin: .25em 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 {
  margin: 1em 0 .5em; line-height: 1.3; font-weight: 650;
}
.msg.assistant h1 { font-size: 1.3em; }
.msg.assistant h2 { font-size: 1.15em; }
.msg.assistant h3 { font-size: 1.05em; }
.msg.assistant a { color: var(--accent); }
.msg.assistant strong { font-weight: 650; }
.msg.assistant hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.msg.assistant blockquote {
  margin: 0 0 .75em; padding-left: 1em;
  border-left: 3px solid var(--border-strong); color: var(--muted);
}

/* Tabelas */
.msg.assistant .table-wrap {
  max-width: 100%; max-height: 420px; overflow: auto;
  margin: .6em 0 1em;
  border: 1px solid var(--border); border-radius: 10px;
}
.msg.assistant table {
  width: 100%; border-collapse: separate; border-spacing: 0;
  font-size: .88em;
}
.msg.assistant th, .msg.assistant td {
  padding: .55em .9em; text-align: left; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.msg.assistant th {
  background: var(--card); color: var(--muted);
  font-weight: 600; font-size: .85em;
  text-transform: uppercase; letter-spacing: .03em;
  position: sticky; top: 0; z-index: 1;
}
.msg.assistant tbody tr:nth-child(even) { background: color-mix(in srgb, var(--card) 55%, transparent); }
.msg.assistant tbody tr:hover { background: var(--accent-soft); }
.msg.assistant tbody tr:last-child td { border-bottom: 0; }

/* Código */
.msg.assistant code {
  font-family: var(--font-mono);
  background: var(--card); border: 1px solid var(--border);
  padding: .15em .4em; border-radius: 6px; font-size: .88em;
}
.msg.assistant pre {
  background: var(--card); border: 1px solid var(--border);
  padding: 1em; border-radius: 10px; overflow-x: auto; margin: .5em 0 1em;
}
.msg.assistant pre code { background: none; border: 0; padding: 0; }

/* Cursor piscando enquanto a resposta ainda está chegando */
.msg.assistant .bubble.streaming::after {
  content: "";
  display: inline-block;
  width: .5em; height: 1.05em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: currentColor;
  opacity: .6;
  border-radius: 1px;
  animation: cursorPisca 1s steps(2, start) infinite;
}
@keyframes cursorPisca {
  0%, 50% { opacity: .6; }
  50.01%, 100% { opacity: 0; }
}

/* Raciocínio do modelo (bloco separado e dobável) */
.msg.assistant .thinking {
  margin: 0 0 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  overflow: hidden;
}
.msg.assistant .thinking > summary {
  cursor: pointer;
  list-style: none;
  padding: 7px 12px;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 600;
  user-select: none;
}
.msg.assistant .thinking > summary::-webkit-details-marker { display: none; }
.msg.assistant .thinking > summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 7px;
  transition: transform .12s ease;
}
.msg.assistant .thinking[open] > summary::before { transform: rotate(90deg); }
.msg.assistant .thinking[open] > summary { border-bottom: 1px solid var(--border); }
.msg.assistant .thinking-body {
  padding: 10px 13px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.55;
  max-height: 340px;
  overflow: auto;
}
.msg.assistant .thinking-body :first-child { margin-top: 0; }
.msg.assistant .thinking-body :last-child { margin-bottom: 0; }
.msg.assistant .thinking-body p { margin: 0 0 .6em; }

/* ===== Composer ========================================================== */
.composer-wrap {
  position: sticky; bottom: 0; padding: 12px 18px 14px;
  border-top: 1px solid var(--border); background: var(--panel);
}
.composer {
  max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end;
  border: 1px solid var(--border); border-radius: 14px; padding: 6px 6px 6px 8px;
  background: var(--panel);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 14px; line-height: 1.5; outline: none; resize: none;
  padding: 8px 6px; max-height: 180px;
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer textarea:disabled { opacity: 0.6; }
.stop {
  border: 1px solid var(--border); background: var(--card); color: var(--text-dim);
  border-radius: 10px; padding: 9px 12px; display: grid; place-items: center;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease, transform 0.1s ease;
}
.stop:hover { background: var(--bg-hover); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.stop:active { transform: scale(0.96); }
.stop[hidden] { display: none; }
.send {
  border: 0; background: var(--accent); color: #fff; border-radius: 10px;
  padding: 9px 14px; font-size: 13px; font-weight: 650; display: grid; place-items: center;
  transition: opacity 0.12s ease, transform 0.1s ease;
}
.send:active:not(:disabled) { transform: scale(0.96); }
.send:disabled { opacity: 0.45; cursor: default; }
.send .spinner { display: none; }
.send.sending svg { display: none; }
.send.sending .spinner {
  display: block; width: 15px; height: 15px;
  border: 2px solid rgba(255, 255, 255, 0.35); border-top-color: #fff;
  border-radius: 999px; animation: girar 0.7s linear infinite;
}
@keyframes girar { to { transform: rotate(360deg); } }
.composer-note { max-width: 760px; margin: 7px auto 0; text-align: center; font-size: 11.5px; color: var(--text-faint); }

/* ===== Scrollbars ======================================================== */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 9px; height: 9px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; }
*::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ===== Backdrop (mobile) ================================================= */
.backdrop {
  display: none; position: fixed; inset: 0; z-index: 25;
  background: rgba(0, 0, 0, 0.45);
  animation: fade 0.15s ease;
}
.backdrop.show { display: block; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ===== Responsivo ======================================================== */
@media (max-width: 720px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(84%, 280px); z-index: 30;
    transform: translateX(-102%); transition: transform 0.18s ease;
    box-shadow: 8px 0 30px rgba(0, 0, 0, 0.18);
  }
  .sidebar.open { transform: translateX(0); }
  .icon-btn, .sidebar-toggle { display: inline-grid; }
  .stop { padding: 9px 10px; }
  .msg.user .bubble { max-width: 92%; }
  .planilha-chip { max-width: 150px; }
  .topbar-status { display: none; }
}
