/* Asistente IA — chat consultas naturales (Fase 1) */

.ai-agent-wrap {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 960px;
  margin: 0 auto;
  min-height: 420px;
}

.ai-agent-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-muted, #64748b);
}

.ai-agent-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-warning, #f59e0b);
}

.ai-agent-status-dot.online {
  background: var(--status-success, #22c55e);
}

.ai-agent-status-dot.offline {
  background: var(--status-error, #ef4444);
}

.ai-agent-suggestions {
  display: none !important;
}

.ai-agent-suggestion {
  border: 1px solid var(--border-color, #e2e8f0);
  background: var(--surface-elevated, #f8fafc);
  color: var(--text-primary, #0f172a);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  font-size: 0.8125rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.ai-agent-suggestion:hover {
  background: rgba(99, 102, 241, 0.08);
  border-color: rgba(99, 102, 241, 0.35);
}

.ai-agent-chat {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-color, #e2e8f0);
  border-radius: 12px;
  background: var(--card-bg, #fff);
  overflow: hidden;
  min-height: 360px;
}

.ai-agent-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 52vh;
}

.ai-agent-msg {
  max-width: 88%;
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  font-size: 0.9375rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.ai-agent-msg-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
}

.ai-agent-msg-body {
  flex: 1;
  min-width: 0;
}

.ai-agent-copy {
  flex-shrink: 0;
  border: 1px solid rgba(99, 102, 241, 0.35);
  background: rgba(99, 102, 241, 0.08);
  color: var(--accent, #6366f1);
  border-radius: 6px;
  padding: 0.15rem 0.45rem;
  font-size: 0.6875rem;
  line-height: 1.2;
  cursor: pointer;
  opacity: 0.85;
}

.ai-agent-copy:hover {
  opacity: 1;
  background: rgba(99, 102, 241, 0.14);
}

.ai-agent-copy--done {
  color: var(--status-success, #16a34a);
  border-color: rgba(34, 197, 94, 0.35);
  background: rgba(34, 197, 94, 0.1);
}

.ai-agent-msg.user {
  align-self: flex-end;
  background: rgba(99, 102, 241, 0.12);
  border: 1px solid rgba(99, 102, 241, 0.2);
}

.ai-agent-msg.assistant {
  align-self: flex-start;
  background: var(--surface-elevated, #f1f5f9);
  border: 1px solid var(--border-color, #e2e8f0);
}

.ai-agent-msg.error {
  align-self: stretch;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  color: var(--status-error, #b91c1c);
}

.ai-agent-msg-meta {
  font-size: 0.75rem;
  color: var(--text-muted, #64748b);
  margin-top: 0.35rem;
}

.ai-agent-refs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.5rem;
}

.ai-agent-ref {
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: rgba(15, 23, 42, 0.06);
  border: none;
  cursor: pointer;
  color: var(--accent, #6366f1);
}

.ai-agent-ref:hover {
  text-decoration: underline;
}

.ai-agent-loading {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  color: var(--text-muted, #64748b);
}

.ai-agent-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(99, 102, 241, 0.25);
  border-top-color: #6366f1;
  border-radius: 50%;
  animation: ai-agent-spin 0.7s linear infinite;
}

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

.ai-agent-input-row {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  border-top: 1px solid var(--border-color, #e2e8f0);
  background: var(--surface-elevated, #f8fafc);
}

.ai-agent-input {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  resize: vertical;
  border: 1px solid var(--border-color, #cbd5e1);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font: inherit;
}

.ai-agent-actions {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.ai-agent-table-wrap {
  overflow-x: auto;
  margin-top: 0.5rem;
}

.ai-agent-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.ai-agent-table th,
.ai-agent-table td {
  border: 1px solid var(--border-color, #e2e8f0);
  padding: 0.35rem 0.5rem;
  text-align: left;
}

.ai-agent-empty {
  text-align: center;
  color: var(--text-muted, #64748b);
  padding: 2rem 1rem;
  font-size: 0.9375rem;
}

/* --- AVI — botón flotante (anclado abajo, panel hacia arriba-izquierda) --- */
.avi-widget {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 12050;
  display: flex;
  flex-direction: column-reverse;
  align-items: flex-end;
  justify-content: flex-start;
  gap: 0.65rem;
  pointer-events: none;
  max-height: calc(100vh - 44px);
}

.avi-widget > * {
  pointer-events: auto;
}

.avi-launcher {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  cursor: grab;
  color: #fff;
  background: linear-gradient(145deg, #6366f1 0%, #4f46e5 55%, #4338ca 100%);
  box-shadow: 0 8px 24px rgba(79, 70, 229, 0.45), 0 2px 8px rgba(15, 23, 42, 0.18);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  touch-action: none;
  user-select: none;
}

.avi-launcher:hover {
  transform: scale(1.04);
  box-shadow: 0 10px 28px rgba(79, 70, 229, 0.5), 0 4px 12px rgba(15, 23, 42, 0.2);
}

.avi-widget--dragging .avi-launcher {
  cursor: grabbing;
  transform: scale(1.02);
}

.avi-launcher-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.avi-robot-icon {
  width: 28px;
  height: 28px;
}

.avi-launcher-label {
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  line-height: 1;
}

.avi-panel {
  width: min(420px, calc(100vw - 24px));
  max-height: min(620px, calc(100vh - 120px));
  display: flex;
  flex-direction: column;
  flex-shrink: 1;
  min-height: 0;
  border-radius: 16px;
  overflow: hidden;
  background: var(--card-bg, #fff);
  border: 1px solid rgba(99, 102, 241, 0.25);
  box-shadow: 0 16px 48px rgba(15, 23, 42, 0.22);
  transform-origin: bottom right;
  animation: avi-panel-in 0.22s ease-out;
}

.avi-panel[hidden] {
  display: none !important;
}

@keyframes avi-panel-in {
  from {
    opacity: 0;
    transform: translate(8px, 16px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translate(0, 0) scale(1);
  }
}

.avi-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem;
  background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
  color: #fff;
}

.avi-panel-title {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.avi-widget--dragging .avi-panel-title {
  cursor: grabbing;
}

.avi-panel-title .avi-robot-icon {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

.avi-panel-title strong {
  display: block;
  font-size: 1rem;
  line-height: 1.2;
}

.avi-panel-sub {
  display: block;
  font-size: 0.6875rem;
  opacity: 0.9;
  font-weight: 400;
}

.avi-panel-actions {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

.avi-panel-btn {
  border: none;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  touch-action: manipulation;
  user-select: none;
}

.avi-panel-btn:hover {
  background: rgba(255, 255, 255, 0.28);
}

.avi-panel-body {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 360px;
}

.ai-agent-floating-root .ai-agent-wrap {
  max-width: none;
  margin: 0;
  min-height: 340px;
  padding: 0.65rem;
  gap: 0.65rem;
}

.ai-agent-floating-root .ai-agent-chat {
  min-height: 280px;
}

.ai-agent-floating-root .ai-agent-messages {
  max-height: 42vh;
}

@media (max-width: 480px) {
  .avi-widget {
    right: 14px;
    bottom: 14px;
  }

  .avi-launcher {
    width: 58px;
    height: 58px;
  }

  .avi-panel {
    width: calc(100vw - 16px);
  }
}
