/* Painel Jabuti — 2 campos + Roles + Memory + health 3 estados */

:root {
  --bg: #0f1419;
  --surface: #1a1f2e;
  --surface-2: #252b3d;
  --border: #2d3548;
  --text: #e4e7eb;
  --text-muted: #8b94a6;
  --primary: #4a9eff;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --purple: #a855f7;
  --cyan: #06b6d4;
  --gold: #fbbf24;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --mono: 'SF Mono', Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  /* Nunca cortar a tela: se o conteudo passar da altura, o body rola. */
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

/*
 * Container raiz do painel. Antes era height:100vh + body overflow:hidden:
 * quando o log/chat/banner de aprovacao crescia, a caixa de dialogo (chat-form)
 * ficava fora da viewport e NAO havia como rolar ate ela.
 * Agora: min-height garante tela cheia, mas o container cresce e o body rola.
 */
body > [x-data] {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  /* Sem overflow aqui: quem rola e o body. Deixar overflow-y:auto num
     container sem altura fixa criava uma barra interna inutil. */
  overflow: visible;
}

code { background: var(--surface-2); padding: 2px 6px; border-radius: 4px; font-family: var(--mono); font-size: 0.9em; }

.header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand .emoji { font-size: 28px; line-height: 1; }
.brand h1 { margin: 0; font-size: 1.15rem; }
.brand small { color: var(--text-muted); font-size: 0.75rem; }

.status {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 16px;
  font-weight: 500; font-size: 0.8rem;
  margin-left: auto;
}
.status.online { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.status.offline { background: rgba(239, 68, 68, 0.15); color: var(--error); }
.status.degraded { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.status .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: currentColor; box-shadow: 0 0 6px currentColor;
  animation: pulse 2s infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
.status .details { opacity: 0.75; font-weight: 400; font-size: 0.75rem; }

.login-status {
  padding: 5px 12px; border-radius: 16px;
  font-size: 0.8rem; font-weight: 500;
}
.login-status.ok { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.login-status.no { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.login-status.unknown { background: rgba(139, 148, 166, 0.15); color: var(--text-muted); }

.btn-icon {
  background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius); padding: 6px 10px;
  font-size: 1rem; cursor: pointer; color: var(--text);
}
.btn-icon:hover { background: var(--surface-2); }

/* Banners */
.offline-banner, .degraded-banner {
  margin: 10px 20px;
  padding: 14px 18px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.5;
}
.offline-banner {
  background: rgba(239, 68, 68, 0.12);
  border-left: 4px solid var(--error);
  color: var(--error);
}
.offline-banner small { color: var(--text-muted); display: block; margin-top: 4px; }

.degraded-banner {
  background: rgba(245, 158, 11, 0.12);
  border-left: 4px solid var(--warning);
  color: var(--warning);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.degraded-banner strong { display: block; }

.btn-inline {
  margin-left: auto;
  padding: 6px 12px;
  background: var(--warning);
  color: #0f1419;
  border: none;
  border-radius: 4px;
  font-weight: 600;
  font-size: 0.8rem;
  cursor: pointer;
  font-family: var(--font);
}
.btn-inline:hover:not(:disabled) { opacity: 0.9; }
.btn-inline:disabled { opacity: 0.5; cursor: not-allowed; }

.actions-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.btn-login, .btn-secondary, .btn-primary, .btn-mini {
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid var(--border);
  font-family: var(--font);
  font-weight: 500;
  transition: all 0.15s;
}

.btn-login {
  padding: 7px 14px;
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  font-size: 0.85rem;
}
.btn-login:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-login:not(:disabled):hover { background: #3a8be6; }

.btn-secondary {
  padding: 7px 14px;
  background: transparent;
  color: var(--text);
  font-size: 0.85rem;
  position: relative;
}
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); }
.btn-secondary:disabled { opacity: 0.4; cursor: not-allowed; }

.roles-badge {
  margin-left: 6px;
  padding: 1px 6px;
  background: var(--gold);
  color: #0f1419;
  border-radius: 8px;
  font-size: 0.7rem;
  font-weight: 700;
}

.btn-primary {
  padding: 10px 18px;
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.btn-primary:hover:not(:disabled) { background: #3a8be6; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-mini {
  padding: 4px 10px;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
}
.btn-mini:hover { background: var(--surface-2); color: var(--text); }
.btn-mini.active { color: var(--primary); border-color: var(--primary); }

.error-inline {
  margin-left: auto;
  padding: 5px 10px;
  background: rgba(239, 68, 68, 0.15);
  color: var(--error);
  border-radius: var(--radius);
  font-size: 0.8rem;
  max-width: 40%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brain-panel, .chat-panel {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 6px 14px;
  overflow: hidden;
  flex: 1;
  min-height: 0;
}

/* Log compacto: ocupa menos altura para sobrar area para o chat. */
.brain-panel { flex: 0 0 auto; min-height: 140px; max-height: 34vh; }
/* Log recolhido: so o cabecalho fica visivel, o log nao empurra mais o chat. */
.brain-panel.collapsed { min-height: 0; max-height: none; }
.brain-panel.collapsed .log { display: none; }

/*
 * Corpo do Brain: log (70%) + terminal (30%) NA MESMA faixa.
 * Antes o terminal era uma section de largura total logo abaixo do Brain,
 * gastando ~26vh de altura. Embutido a 20% de largura ele continua visível
 * durante a execução e devolve altura para o chat.
 */
.brain-body { display: flex; flex: 1 1 auto; min-height: 0; }
.brain-body .log { flex: 1 1 auto; min-width: 0; border-right: 1px solid var(--border); }
.brain-body .term-panel {
  flex: 0 0 30%;
  max-width: 30%;
  min-width: 170px;
  min-height: 0;
  max-height: none;
  margin: 0;
  border: 0;
  border-radius: 0;
}
.brain-body .term-panel .term-header { padding: 6px 8px; }
.brain-body .term-panel .term-header h2 small { display: none; }
.brain-body .term-panel .event-count { display: none; }
.brain-body .term-panel .panel-actions { gap: 4px; }
.brain-body .term-panel .term-body { padding: 6px 8px; font-size: 0.68rem; }
.brain-body .term-panel .term-now { padding: 3px 8px; font-size: 0.64rem; }
.brain-body .term-panel .term-now-cmd { max-width: 55%; }
/* Tela estreita: empilha (log em cima, terminal embaixo) em vez de espremer. */
@media (max-width: 900px) {
  .brain-body { flex-direction: column; }
  .brain-body .log { border-right: 0; }
  .brain-body .term-panel {
    flex: 0 0 auto;
    max-width: none;
    width: 100%;
    max-height: 22vh;
    border-top: 1px solid var(--border);
  }
}

/* ─── Terminal (prompt cmd/PowerShell) ───────────────────────────────────
 * Tela propria, separada do log: o log e historico linear de eventos; o
 * terminal mostra comando + saida com quebras de linha preservadas, como um
 * console de verdade. Fica no canto inferior direito do painel do Brain.
 */
.term-panel {
  flex: 0 0 auto;
  min-height: 110px;
  max-height: 26vh;
  display: flex;
  flex-direction: column;
  background: #05080b;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 6px 14px;
  overflow: hidden;
}
.term-panel.collapsed { min-height: 0; max-height: none; }
.term-panel.collapsed .term-body { display: none; }
.term-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: #0a0f14;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.term-header h2 { margin: 0; font-size: 0.82rem; font-weight: 600; }
.term-header h2 small { color: var(--text-muted); font-weight: 400; font-size: 0.72rem; }
.term-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 0.74rem;
  line-height: 1.45;
  scrollbar-gutter: stable;
}
.term-line { white-space: pre-wrap; word-break: break-word; }
.term-prompt { color: #22c55e; }
.term-cmd { color: #e5e7eb; }
.term-out { color: #9ca3af; }
.term-err { color: #f87171; }
.term-ok { color: #4ade80; }
.term-empty { color: var(--text-muted); font-style: italic; }
/* Cursor piscando na ultima linha enquanto executa */
.term-cursor {
  display: inline-block;
  width: 7px;
  height: 0.95em;
  background: #22c55e;
  vertical-align: text-bottom;
  animation: term-blink 1s step-end infinite;
}
@keyframes term-blink { 50% { opacity: 0; } }
/* Rodape fixo com o comando atual, alinhado a direita (mini mostrador) */
.term-now {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  padding: 3px 12px;
  background: #0a0f14;
  border-top: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
}
.term-now-cmd {
  color: var(--warning);
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60%;
}
.term-now-step { color: var(--text-muted); flex-shrink: 0; }

/*
 * Altura do chat: antes o painel crescia sem limite conforme as mensagens
 * chegavam (min-height:340px e nenhum teto), empurrando a caixa de dialogo
 * para fora da tela. Agora o painel tem teto e quem rola e o .chat-history.
 */
.chat-panel  { flex: 1.9; min-height: 340px; max-height: 62vh; }
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-header h2 { margin: 0; font-size: 0.9rem; font-weight: 600; }
.panel-header h2 small { color: var(--text-muted); font-weight: 400; font-size: 0.75rem; }

.panel-actions { display: flex; align-items: center; gap: 6px; }
.event-count { color: var(--text-muted); font-size: 0.75rem; margin-right: 6px; }

.log {
  flex: 1;
  overflow-y: auto;
  overflow-anchor: auto;
  padding: 6px 0;
  font-family: var(--mono);
  font-size: 0.74rem;
  line-height: 1.35;
  background: #0a0f14;
  scrollbar-gutter: stable;
}

.log-empty, .chat-empty {
  padding: 30px 20px;
  text-align: center;
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.85rem;
}

.log-line {
  display: flex;
  gap: 8px;
  padding: 1px 14px;
  border-left: 3px solid transparent;
}
.log-line:hover { background: rgba(255, 255, 255, 0.02); }

.log-ts { color: var(--text-muted); opacity: 0.6; flex-shrink: 0; font-size: 0.72rem; }
.log-tag { flex-shrink: 0; font-weight: 600; font-size: 0.7rem; min-width: 90px; }
.log-msg { flex: 1; word-break: break-word; color: var(--text); }

.log-run    .log-tag { color: var(--cyan); }
.log-run    { border-left-color: var(--cyan); }
.log-iter   .log-tag { color: var(--text-muted); }
.log-iter   { border-left-color: var(--text-muted); }
.log-llm    .log-tag { color: var(--purple); }
.log-llm    { border-left-color: var(--purple); }
.log-tool   .log-tag { color: var(--warning); }
.log-tool   { border-left-color: var(--warning); }
.log-final  .log-tag { color: var(--success); }
.log-final  { border-left-color: var(--success); background: rgba(34, 197, 94, 0.04); }
.log-error  .log-tag { color: var(--error); }
.log-error  { border-left-color: var(--error); background: rgba(239, 68, 68, 0.06); }
.log-browser .log-tag { color: #6b7280; }
.log-browser { border-left-color: #6b7280; }
.log-login  .log-tag { color: var(--primary); }
.log-login  { border-left-color: var(--primary); }
.log-other  .log-tag { color: var(--text-muted); }
.log-other  { border-left-color: transparent; }

.chat-history {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border-radius: var(--radius);
  max-width: 85%;
}
.chat-msg.chat-user { align-self: flex-end; background: var(--primary); color: white; }
.chat-msg.chat-assistant {
  align-self: flex-start;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.chat-role { font-size: 0.7rem; font-weight: 600; opacity: 0.85; }
.chat-content {
  margin: 0;
  font-family: var(--font);
  font-size: 0.88rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.chat-ts { font-size: 0.65rem; opacity: 0.5; align-self: flex-end; }

.chat-typing {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  color: var(--text-muted);
  font-size: 1rem;
  font-style: italic;
  align-self: flex-start;
  min-height: 44px;
}

.chat-typing .turtle-anim {
  font-size: 1.5rem;
  line-height: 1;
  display: inline-block;
}

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--text-muted);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.chat-form {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  align-items: flex-start;
  /* A caixa de dialogo nunca encolhe nem sai da area visivel do painel. */
  position: sticky;
  bottom: 0;
  z-index: 5;
}

/* Atalho flutuante para voltar a caixa de dialogo */
.jump-to-chat {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 60;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-lg, 0 8px 24px rgba(0, 0, 0, 0.35));
  opacity: 0.92;
}
.jump-to-chat:hover { opacity: 1; border-color: var(--primary); color: var(--primary); }

.role-select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 8px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.8rem;
  min-height: 44px;
  max-width: 200px;
  flex-shrink: 0;
}
.role-select:focus { outline: none; border-color: var(--primary); }
.role-select:disabled { opacity: 0.6; cursor: not-allowed; }

.chat-form textarea {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.92rem;
  line-height: 1.5;
  resize: vertical;
  /* 20% mais baixo que 76px/240px para caber mais tela sem rolar. */
  min-height: calc(76px * 0.8);
  max-height: calc(240px * 0.8);
  /* O rows="2" do HTML (~68px) brigava com o min-height (60.8px) e o campo
     ficava mais alto que o pedido; height:auto deixa o min-height mandar. */
  height: auto;
}
.chat-form textarea:focus { outline: none; border-color: var(--primary); }
.chat-form textarea:disabled { opacity: 0.6; cursor: not-allowed; }
/* ─── chat-input-wrap: overlay do "pensando" sobre o textarea ─── */
.chat-input-wrap {
  position: relative;
  flex: 1;
  display: flex;
  min-width: 0;
}
.chat-input-wrap textarea {
  width: 100%;
}
.chat-input-wrap.sending textarea {
  color: transparent !important;
  caret-color: transparent;
}
.chat-input-wrap.sending textarea::placeholder {
  color: transparent !important;
}
.chat-input-wrap .chat-typing {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  padding: 0 14px;
  margin: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  pointer-events: none;
  min-height: 0;
  font-size: 0.95rem;
  font-style: italic;
  color: var(--text-muted);
  gap: 10px;
  overflow: hidden;
}
.chat-input-wrap .chat-typing .turtle-anim {
  font-size: 1.5rem;
  line-height: 1;
  flex-shrink: 0;
  display: inline-block;
  /* O indicador é um overlay com overflow:hidden; o emoji (1.5rem) era cortado
     embaixo. Sobe um pouco e desenha fora da caixa: fica na mesma linha do
     cérebro (ambos com a mesma baseline) mas sem ser cortado. */
  position: relative;
  top: -2px;
  overflow: visible;
  transform-origin: center center;
}
/*
 * O indicador fica sobre o textarea (pointer-events: none), então os botões
 * Aprovar/Negar do modo "approving" precisam voltar a receber clique —
 * sem isto eles apareciam mas não respondiam.
 */
.chat-input-wrap .chat-typing .approval-inline { pointer-events: auto; }
/* Placeholder do chat textarea: estilo do "Jabuti pensando" */
.chat-form textarea::placeholder {
  font-style: italic;
  color: var(--text-muted);
}
.chat-form textarea:disabled::placeholder {
  color: #9ca3af;
  opacity: 1;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 480px;
  width: 100%;
}
.modal h2 { margin: 0 0 12px 0; font-size: 1.1rem; }
.modal p { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 16px 0; }
.modal input[type="password"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-family: var(--font);
  margin-bottom: 16px;
}
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ── Roles modal ────────────────────────────────────────────────────── */
.roles-modal {
  max-width: 860px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.roles-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.roles-header h2 { margin: 0; font-size: 1.05rem; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.roles-header h2 small { color: var(--text-muted); font-weight: 400; }
.roles-header > div { display: flex; gap: 8px; align-items: center; }

.edit-indicator {
  color: var(--purple);
  font-weight: 600;
  font-size: 0.85rem;
}

.roles-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.roles-loading {
  text-align: center;
  color: var(--text-muted);
  padding: 20px;
  font-style: italic;
}

.roles-section { display: flex; flex-direction: column; gap: 10px; }

.roles-section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}
.roles-section-title small { color: var(--text-muted); font-weight: 400; }

.toggle-disabled {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.toggle-disabled input[type="checkbox"] {
  width: 14px;
  height: 14px;
  cursor: pointer;
}

.role-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  transition: opacity 0.15s;
}
.role-card.custom   { border-left: 3px solid var(--purple); }
.role-card.builtin  { border-left: 3px solid var(--primary); }
.role-card.favorite { background: rgba(251, 191, 36, 0.04); }
.role-card.favorite.custom  { border-left-color: var(--gold); }
.role-card.favorite.builtin { border-left-color: var(--gold); }
.role-card.disabled { opacity: 0.55; }

/* Agentes de sistema (criador / manu): card maior, arredondado e destacado */
.role-card.system-agent {
  border-radius: 16px;
  padding: 16px 18px;
  border-width: 2px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
}
.role-card.system-agent header strong { font-size: 1.05rem; letter-spacing: 0.02em; }
.role-card.system-agent .system-agent-icon { font-size: 1.25rem; line-height: 1; }
.role-card.system-agent .system-agent-role {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.role-card.agent-criador {
  border-color: var(--purple);
  background: linear-gradient(180deg, rgba(168, 85, 247, 0.12), rgba(168, 85, 247, 0.03));
}
.role-card.agent-criador .system-agent-role { background: rgba(168, 85, 247, 0.18); color: var(--purple); }
.role-card.agent-manu {
  border-color: var(--primary);
  background: linear-gradient(180deg, rgba(74, 158, 255, 0.12), rgba(74, 158, 255, 0.03));
}
.role-card.agent-manu .system-agent-role { background: rgba(74, 158, 255, 0.18); color: var(--primary); }

.role-card header {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.role-card header strong { font-family: var(--mono); font-size: 0.9rem; color: var(--text); }

.btn-star {
  background: transparent;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0 4px;
  color: var(--text-muted);
  transition: transform 0.15s, color 0.15s;
  line-height: 1;
}
.btn-star:hover { transform: scale(1.2); color: var(--gold); }
.btn-star.active { color: var(--gold); }

.role-badge {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
}
.role-card.builtin .role-badge { background: rgba(74, 158, 255, 0.15); color: var(--primary); }
.role-card.custom  .role-badge { background: rgba(168, 85, 247, 0.15); color: var(--purple); }

.disabled-badge {
  background: rgba(245, 158, 11, 0.15) !important;
  color: var(--warning) !important;
}

.role-label {
  color: var(--text-muted);
  font-size: 0.82rem;
  flex: 1;
}

.btn-toggle-disable {
  background: transparent;
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font);
}
.btn-toggle-disable.deactivate {
  border: 1px solid var(--warning);
  color: var(--warning);
}
.btn-toggle-disable.deactivate:hover { background: rgba(245, 158, 11, 0.15); }
.btn-toggle-disable.activate {
  border: 1px solid var(--success);
  color: var(--success);
}
.btn-toggle-disable.activate:hover { background: rgba(34, 197, 94, 0.15); }

.btn-edit-role {
  background: transparent;
  border: 1px solid var(--primary);
  color: var(--primary);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-edit-role:hover { background: rgba(74, 158, 255, 0.15); }

.btn-cancel-role {
  background: transparent;
  border: 1px solid var(--error);
  color: var(--error);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.75rem;
  cursor: pointer;
}
.btn-cancel-role:hover { background: rgba(239, 68, 68, 0.15); }

.role-prefix {
  margin: 6px 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.4;
  font-style: italic;
}

/* ── Trabalho em andamento no card do agente ─────────────────────── */
.agent-work {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  background: rgba(34, 197, 94, 0.10);
  border-left: 3px solid #22c55e;
  font-size: 0.76rem;
  line-height: 1.3;
}
.agent-work:hover { background: rgba(34, 197, 94, 0.18); }

.agent-work-idle {
  background: rgba(148, 163, 184, 0.10);
  border-left-color: #94a3b8;
}
.agent-work-idle:hover { background: rgba(148, 163, 184, 0.18); }

.agent-work-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #22c55e; flex: 0 0 auto;
  animation: agent-pulse 1.4s ease-in-out infinite;
}
@keyframes agent-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.35; transform: scale(0.8); }
}

.agent-work-text {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.agent-work-time {
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

/* Última execução terminada ("terminou há 12s") */
.agent-work-done {
  background: rgba(59, 130, 246, 0.10);
  border-left-color: #3b82f6;
}
.agent-work-done:hover { background: rgba(59, 130, 246, 0.18); }

/* ── Aviso flutuante de fim de tarefa ────────────────────────────── */
.agent-toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 90;
  max-width: 420px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.84rem;
  line-height: 1.35;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  border-left: 4px solid #22c55e;
  background: #0f172a;
  color: #e2e8f0;
}
.agent-toast-ok  { border-left-color: #22c55e; }
.agent-toast-err { border-left-color: #ef4444; }

/* ── Modal de andamento das tarefas ──────────────────────────────── */
.agent-tasks-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
  margin: 10px 0;
}
.agent-task-row {
  border: 1px solid var(--border, rgba(148,163,184,0.25));
  border-radius: 8px;
  padding: 8px 10px;
}
.agent-task-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.84rem;
}
.agent-task-head strong { flex: 1 1 auto; }
.agent-task-time {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  font-size: 0.78rem;
}
.agent-task-status {
  font-size: 0.72rem;
  padding: 1px 7px;
  border-radius: 999px;
  white-space: nowrap;
}
.st-running { background: rgba(34,197,94,0.18);  color: #16a34a; }
.st-queued  { background: rgba(234,179,8,0.18);  color: #ca8a04; }
.st-done    { background: rgba(59,130,246,0.18); color: #2563eb; }
.st-failed  { background: rgba(239,68,68,0.18);  color: #dc2626; }

.agent-task-meta {
  margin: 4px 0 0;
  font-size: 0.74rem;
  color: var(--text-muted);
}
.agent-task-result,
.agent-task-error {
  margin: 4px 0 0;
  font-size: 0.76rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.agent-task-error { color: #dc2626; }

.role-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  font-size: 0.72rem;
  margin-top: 6px;
}
.role-tools strong { color: var(--text-muted); margin-right: 4px; }
.role-tool {
  background: var(--surface-2);
  padding: 2px 8px;
  border-radius: 4px;
  font-family: var(--mono);
  color: var(--text);
}

.roles-form {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bg);
}

.roles-form-help {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0 0 8px 0;
}
.roles-form-help code {
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 3px;
  font-family: var(--mono);
}

.roles-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 500;
}

.roles-form input[type="text"],
.roles-form textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.88rem;
}
.roles-form input[type="text"]:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.roles-form textarea {
  font-family: var(--mono);
  font-size: 0.82rem;
  resize: vertical;
  min-height: 60px;
}
.roles-form input[type="text"]:focus,
.roles-form textarea:focus {
  outline: none;
  border-color: var(--primary);
}

/* ── Memory modal ───────────────────────────────────────────────────── */
.memory-modal {
  max-width: 900px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.memory-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  flex: 1;
}

.memory-info {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.memory-size {
  margin-left: auto;
  font-family: var(--mono);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.memory-warn { color: var(--warning); font-weight: 500; }
.memory-dirty { color: var(--warning); font-weight: 600; }

.memory-textarea {
  width: 100%;
  min-height: 400px;
  max-height: 60vh;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.5;
  resize: vertical;
}
.memory-textarea:focus { outline: none; border-color: var(--primary); }
.memory-textarea:disabled { opacity: 0.6; cursor: not-allowed; }

.footer {
  display: flex;
  justify-content: space-between;
  padding: 8px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.7rem;
  flex-shrink: 0;
}
.footer-error { color: var(--error); }

@media (max-width: 700px) {
  .header { flex-wrap: wrap; padding: 10px 12px; gap: 8px; }
  .status, .login-status { font-size: 0.7rem; padding: 4px 10px; }
  .brain-panel, .chat-panel { margin: 8px 12px; }
  .actions-bar { padding: 8px 12px; }
  .panel-header h2 { font-size: 0.85rem; }
  .log { font-size: 0.72rem; }
  .role-select { max-width: 140px; font-size: 0.72rem; }
  .roles-modal { max-height: 95vh; }
  .roles-header h2 { font-size: 0.92rem; }
  .memory-modal { max-height: 95vh; }
  .memory-textarea { min-height: 300px; max-height: 50vh; }
}
/* ─── Botão "✅ Logado" (loggedIn === true) ──────────────────────── */
.btn-logged,
.btn-logged:disabled,
.btn-logged:hover {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
  color: #fff !important;
  border-color: #047857 !important;
  opacity: 1 !important;
  cursor: default !important;
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(16, 185, 129, 0.25);
}

/* ─── Agents (Fase 1b) ──────────────────────────────────────────── */
.agents-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--primary-soft, rgba(74, 158, 255, 0.14));
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
  border: 1px solid rgba(74, 158, 255, 0.28);
}

.agents-modal {
  max-width: 860px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.agents-modal select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.agents-modal .role-card.favorite {
  border-color: var(--gold);
  background: var(--warning-soft, rgba(245, 158, 11, 0.14));
}

.agents-modal .role-card header {
  flex-wrap: wrap;
  gap: 6px;
}

.agents-modal .btn-mini {
  padding: 2px 8px;
  font-size: 12px;
}

/* ─── Modal Rules (Fase 2) ──────────────────────────── */
.rules-modal {
  max-width: 640px;
  width: 92vw;
}
.rules-modal .role-card .role-label {
  max-height: 3.6em;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* \u2500\u2500\u2500 Stars modal \u2500\u2500\u2500 */
.stars-modal { max-width: 720px; width: 92vw; }
.stars-body { max-height: 60vh; overflow-y: auto; }
.stars-table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.stars-table th { text-align: left; padding: 6px 8px; background: var(--surface-2); font-size: 11px; text-transform: uppercase; color: var(--text-muted); }
.stars-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--text); }
.stars-row.color-below td { background: var(--error-soft, rgba(239, 68, 68, 0.14)); }
.stars-row.color-peak td { background: var(--success-soft, rgba(34, 197, 94, 0.14)); }
.stars-level { display: inline-block; padding: 1px 6px; border-radius: 4px; background: var(--primary-soft, rgba(74, 158, 255, 0.14)); color: var(--primary); font-weight: 600; font-size: 11px; }
.stars-total { font-weight: 700; color: var(--gold); }
.trend-strong-decline { color: var(--error); font-weight: 600; }
.trend-declining { color: var(--warning); }
.trend-stable { color: var(--text-muted); }
.trend-growing { color: var(--success); }
.trend-strong-growing { color: var(--success); font-weight: 600; }

/* Telas pequenas: o composer volta ao tamanho maior para não espremer o campo. */
@media (max-width: 600px) {
  .chat-form textarea { min-height: 76px; max-height: 240px; padding: 12px 14px; }
  .chat-form { padding: 12px 16px; }
}
