/* ============================================
   AI Chat — Painel flutuante
   ============================================ */

/* --------------------------------------------------
   Painel principal
   -------------------------------------------------- */
.ai-chat-panel {
  position: fixed;
  top: 50%;
  right: 80px; /* respeita a sidebar-right de 60px */
  width: 360px;
  max-height: 560px;
  height: min(560px, calc(100vh - 48px));
  display: flex;
  flex-direction: column;
  background: #ffffff;
  border-radius: 14px;
  box-shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 20px 40px -8px rgba(0, 0, 0, 0.18);
  border: 1px solid #e5e7eb;
  z-index: 500;
  overflow: hidden;
  opacity: 1;
  pointer-events: all;
  transform: translateY(-50%);
  transition: width 0.18s ease, height 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

.ai-chat-panel.open {
  transform: translateY(-50%);
}

.ai-chat-panel.collapsed {
  width: 56px;
  height: 56px;
  max-height: 56px;
  border-radius: 16px;
  box-shadow:
    0 4px 8px rgba(0, 0, 0, 0.08),
    0 14px 26px rgba(79, 70, 229, 0.18);
}

.ai-chat-panel.collapsed .ai-chat-messages,
.ai-chat-panel.collapsed .ai-chat-footer {
  display: none;
}

.ai-chat-panel.collapsed .ai-chat-header {
  padding: 0;
  width: 56px;
  height: 56px;
  justify-content: center;
  background: linear-gradient(180deg, #4338ca, #4f46e5);
  cursor: pointer;
}

.ai-chat-panel.collapsed .ai-chat-header-title {
  justify-content: center;
}

.ai-chat-panel.collapsed .ai-chat-header-title span,
.ai-chat-panel.collapsed .ai-chat-close {
  display: none;
}

.ai-chat-panel.collapsed .ai-chat-header-title i {
  font-size: 20px;
  color: #ffffff;
}

/* --------------------------------------------------
   Header
   -------------------------------------------------- */
.ai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: #111827;
  color: #f9fafb;
  flex-shrink: 0;
}

.ai-chat-header-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.ai-chat-header-title i {
  font-size: 14px;
  color: #a5b4fc;
}

.ai-chat-close {
  background: none;
  border: none;
  color: #9ca3af;
  cursor: pointer;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: background 0.12s, color 0.12s;
  padding: 0;
}

.ai-chat-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #f9fafb;
}

.ai-chat-panel.open .ai-chat-header {
  cursor: default;
}

/* --------------------------------------------------
   Mensagens
   -------------------------------------------------- */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.ai-chat-messages::-webkit-scrollbar {
  width: 4px;
}
.ai-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
.ai-chat-messages::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 2px;
}

/* Tela de boas-vindas */
.ai-chat-welcome {
  text-align: center;
  color: #6b7280;
  padding: 24px 8px;
}

.ai-chat-welcome-icon {
  font-size: 28px;
  color: #a5b4fc;
  margin-bottom: 10px;
  display: block;
}

.ai-chat-welcome p {
  margin: 0 0 6px;
  font-size: 13px;
  line-height: 1.5;
}

.ai-chat-welcome-hint {
  font-size: 12px !important;
  color: #9ca3af !important;
}

.ai-chat-welcome-hint em {
  color: #6366f1;
  font-style: normal;
}

/* Bolhas */
.ai-chat-message {
  display: flex;
  flex-direction: column;
  max-width: 88%;
}

.ai-chat-message-user {
  align-self: flex-end;
}

.ai-chat-message-assistant {
  align-self: flex-start;
}

.ai-chat-bubble-text {
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.55;
  word-break: break-word;
}

.ai-chat-message-user .ai-chat-bubble-text {
  background: #6366f1;
  color: #ffffff;
  border-bottom-right-radius: 3px;
}

.ai-chat-message-assistant .ai-chat-bubble-text {
  background: #f3f4f6;
  color: #111827;
  border-bottom-left-radius: 3px;
}

.ai-chat-message-error .ai-chat-bubble-text {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* Badge "Diagrama aplicado" */
.ai-chat-applied-badge {
  margin-top: 6px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: #059669;
  font-weight: 500;
}

.ai-chat-applied-badge i {
  font-size: 12px;
}

/* Thinking dots animation */
.ai-chat-thinking {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 2px 0;
}

.ai-chat-thinking span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #9ca3af;
  animation: ai-chat-pulse 1.2s ease-in-out infinite;
}

.ai-chat-thinking span:nth-child(2) {
  animation-delay: 0.2s;
}

.ai-chat-thinking span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes ai-chat-pulse {
  0%, 80%, 100% {
    opacity: 0.3;
    transform: scale(0.85);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

/* --------------------------------------------------
   Footer / Input
   -------------------------------------------------- */
.ai-chat-footer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px 12px;
  border-top: 1px solid #f3f4f6;
  flex-shrink: 0;
}

.ai-chat-input {
  flex: 1;
  resize: none;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  line-height: 1.5;
  color: #111827;
  background: #f9fafb;
  outline: none;
  min-height: 38px;
  max-height: 120px;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.ai-chat-input:focus {
  border-color: #6366f1;
  background: #ffffff;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.ai-chat-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ai-chat-send {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 8px;
  border: none;
  background: #6366f1;
  color: #ffffff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: background 0.12s, transform 0.1s;
  flex-shrink: 0;
}

.ai-chat-send:hover:not(:disabled) {
  background: #4f46e5;
}

.ai-chat-send:active:not(:disabled) {
  transform: scale(0.93);
}

.ai-chat-send:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

/* --------------------------------------------------
   Botão na sidebar
   -------------------------------------------------- */
#btn-ai-chat {
  position: relative;
  overflow: visible;
  color: #4f46e5;
  background: #eef2ff;
}

#btn-ai-chat:hover {
  background: #e0e7ff;
  color: #4338ca;
}

#btn-ai-chat i {
  font-size: 16px;
}

#btn-ai-chat.active i {
  color: #6366f1;
}

#btn-ai-chat .sidebar-btn-badge {
  position: absolute;
  right: -2px;
  bottom: -1px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: #c7d2fe;
  color: #3730a3;
  font-size: 9px;
  font-weight: 700;
  line-height: 16px;
  letter-spacing: 0.03em;
  box-sizing: border-box;
}

#btn-ai-chat.active {
  background: #e0e7ff;
}

#btn-ai-chat.active .sidebar-btn-badge {
  background: #6366f1;
  color: #ffffff;
}

/* --------------------------------------------------
   Tool cards — seção de chamadas de ferramenta
   -------------------------------------------------- */
.ai-chat-tools-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 6px;
}

.ai-chat-tool-card {
  border-radius: 8px;
  border: 1px solid #e5e7eb;
  overflow: hidden;
  font-size: 12px;
}

/* Header colorido por tipo */
.ai-chat-tool-card-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  cursor: pointer;
  user-select: none;
  transition: filter 0.1s;
}

.ai-chat-tool-card-header:hover {
  filter: brightness(0.96);
}

/* applyDiagram — roxo */
.ai-chat-tool-card[data-tool="applyDiagram"] .ai-chat-tool-card-header {
  background: #ede9fe;
  color: #5b21b6;
}

/* previewDiagram — âmbar */
.ai-chat-tool-card[data-tool="previewDiagram"] .ai-chat-tool-card-header {
  background: #fef3c7;
  color: #92400e;
}

/* fallback para outras tools */
.ai-chat-tool-card:not([data-tool="applyDiagram"]):not([data-tool="previewDiagram"]) .ai-chat-tool-card-header {
  background: #f0f9ff;
  color: #0c4a6e;
}

.ai-chat-tool-card-name {
  font-weight: 600;
  font-size: 11.5px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-chat-tool-card-meta {
  font-size: 11px;
  opacity: 0.7;
  white-space: nowrap;
}

.ai-chat-tool-card-status {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 500;
}

/* spinner de carregamento */
.ai-chat-tool-card-status .spinner {
  width: 11px;
  height: 11px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: ai-chat-spin 0.7s linear infinite;
  opacity: 0.7;
}

@keyframes ai-chat-spin {
  to { transform: rotate(360deg); }
}

.ai-chat-tool-card-chevron {
  font-size: 10px;
  opacity: 0.6;
  transition: transform 0.15s ease;
  flex-shrink: 0;
}

.ai-chat-tool-card.expanded .ai-chat-tool-card-chevron {
  transform: rotate(180deg);
}

.ai-chat-tool-card-ok {
  color: #059669;
  font-size: 12px;
}

/* Body expansível */
.ai-chat-tool-card-body {
  display: none;
  padding: 8px 10px;
  background: #fafafa;
  border-top: 1px solid #e5e7eb;
  flex-direction: column;
  gap: 6px;
}

.ai-chat-tool-card.expanded .ai-chat-tool-card-body {
  display: flex;
}

.ai-chat-tool-card-label {
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b7280;
  margin-bottom: 2px;
}

.ai-chat-tool-card-json {
  background: #f3f4f6;
  border-radius: 5px;
  padding: 6px 8px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 10.5px;
  color: #374151;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 140px;
  overflow-y: auto;
  line-height: 1.45;
}

.ai-chat-tool-card-json::-webkit-scrollbar {
  width: 3px;
}
.ai-chat-tool-card-json::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 2px;
}

/* Imagem do preview */
.ai-chat-tool-preview-img {
  width: 100%;
  border-radius: 5px;
  border: 1px solid #e5e7eb;
  display: block;
  margin-top: 2px;
}
