/* ================================
   ERP AI Widget Stylesheet
   File: erp-ai-widget.css
   ================================ */

/* --- FAB (Floating Action Button) --- */
#ai-fab {
  position: fixed!important;
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 12px);
  z-index: 9000;

  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;

  background: #1f2937;   /* grigio scuro */
  color: #fff;
  cursor: pointer;

  box-shadow: 0 8px 24px rgba(0,0,0,.18);
  opacity: 0.95;
  transition: transform .15s ease, box-shadow .2s ease, opacity .2s ease;

  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
#ai-fab:hover { transform: translateY(-2px); box-shadow: 0 10px 28px rgba(0,0,0,.22); }
#ai-fab:active { transform: translateY(0); }

/* --- Chat Widget Container --- */
#erp-ai-widget {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 72px); /* sopra il FAB */
  z-index: 9000;

  width: 360px;
  max-width: calc(100vw - 32px);
  height: 520px;
  max-height: calc(100vh - 32px - env(safe-area-inset-bottom));

  background: #fff;
  border-radius: 16px;
  box-shadow: 0 12px 36px rgba(0,0,0,.22);
  overflow: hidden;

  display: none;
  transform: translateY(12px) scale(.98);
  opacity: 0;
  transition: transform .18s ease, opacity .18s ease;
}

/* Stato visibile */
#erp-ai-widget.open {
  display: block;
  transform: translateY(0) scale(1);
  opacity: 1;
}

/* --- Header --- */
#erp-ai-widget .ai-header {
  position: sticky;
  top: 0;
  z-index: 1;

  background: #111827;
  color: #fff;
  padding: 10px 12px;

  display: flex;
  align-items: center;
  justify-content: space-between;
}
#erp-ai-widget .ai-title { font-size: 14px; font-weight: 600; }
#erp-ai-widget .ai-close {
  background: transparent;
  border: 0;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
}

/* --- Corpo (log messaggi) --- */
#erp-ai-widget .ai-log {
  height: calc(100% - 112px);
  overflow-y: auto;
  padding: 12px;
}

/* --- Input bar --- */
#erp-ai-widget .ai-inputbar {
  position: sticky;
  bottom: 0;
  background: #fff;
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  border-top: 1px solid #e5e7eb;
}
#ai-input {
  flex: 1 1 auto;
  min-height: 42px;
  max-height: 120px;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  padding: 8px 10px;
  resize: none;
}
#ai-send {
  background: #2563eb;
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 0 14px;
  cursor: pointer;
  min-width: 72px;
  height: 42px;
}

/* --- Bubbles --- */
#erp-ai-widget .me,
#erp-ai-widget .bot {
  margin: 8px 0;
  display: flex;
}
#erp-ai-widget .me { justify-content: flex-end; }
#erp-ai-widget .bot { justify-content: flex-start; }

#erp-ai-widget .bubble {
  max-width: 82%;
  padding: 8px 10px;
  border-radius: 12px;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  color: #111827;
}
#erp-ai-widget .me .bubble {
  background: #dbeafe;
  border-color: #bfdbfe;
}

#erp-ai-widget .bubble ul {
    white-space: normal !important;
    line-height: 1.5em !important;
}


/* --- Responsive --- */
@media (max-width: 640px) {
  #ai-fab {
    width: 54px; height: 54px;
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 4px);
  }
  #erp-ai-widget {
    width: min(94vw, 420px);
    height: min(70vh, 80vh);
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 58px);
    border-radius: 14px;
  }
  #erp-ai-widget .ai-log { height: calc(100% - 110px); }
}

@media (min-width: 1280px) {
  #erp-ai-widget { width: 400px; height: 560px; }
}

/* --- Disabilita widget quando Fancybox attivo (opzionale) --- */
html.fancybox-enabled #ai-fab,
html.fancybox-enabled #erp-ai-widget,
body.fancybox-enabled #ai-fab,
body.fancybox-enabled #erp-ai-widget,
body.fancybox-active #ai-fab,
body.fancybox-active #erp-ai-widget {
  display: none !important;
}

/* --- Badge modalità --- */
.ai-mode-badge {
  margin-left: 8px;
  background: #374151;
  color: #e5e7eb;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  vertical-align: middle;
}

/* --- Select modalità --- */
.ai-mode-select {
  margin-left: auto;
  background: #111827;
  color: #e5e7eb;
  border: 1px solid #374151;
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
}

/* --- Typing indicator --- */
.ai-typing {
  display: none;
  position: absolute;
  right: 16px;
  bottom: 62px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 6px 10px;
  box-shadow: 0 6px 18px rgba(0,0,0,.08);
}
.ai-typing .dot {
  display: inline-block;
  width: 6px; height: 6px;
  margin: 0 2px;
  border-radius: 50%;
  background: #9ca3af;
  animation: ai-typing 1.2s infinite ease-in-out;
}
.ai-typing .dot:nth-child(2){ animation-delay: .15s; }
.ai-typing .dot:nth-child(3){ animation-delay: .3s; }
@keyframes ai-typing {
  0%, 80%, 100% { opacity: .3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* --- HTML “ricco” dentro .bubble del bot --- */
#erp-ai-widget .bot .bubble h1,
#erp-ai-widget .bot .bubble h2,
#erp-ai-widget .bot .bubble h3 {
  margin: 6px 0 4px;
  font-weight: 600;
  line-height: 1.25;
}
#erp-ai-widget .bot .bubble h1 { font-size: 15px; }
#erp-ai-widget .bot .bubble h2 { font-size: 14px; }
#erp-ai-widget .bot .bubble h3 { font-size: 13px; }

#erp-ai-widget .bot .bubble p { margin: 6px 0; }

#erp-ai-widget .bot .bubble strong { font-weight: 700; }
#erp-ai-widget .bot .bubble em { font-style: italic; }

#erp-ai-widget .bot .bubble ul,
#erp-ai-widget .bot .bubble ol {
  margin: 6px 0 6px 18px;
  padding-left: 0;
}
#erp-ai-widget .bot .bubble li { margin: 2px 0; }

#erp-ai-widget .bot .bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
  padding: 1px 4px;
  border-radius: 6px;
}
#erp-ai-widget .bot .bubble pre {
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  padding: 8px;
  border-radius: 10px;
  overflow: auto;
  white-space: pre;
}

/* tabelle nelle risposte */
#erp-ai-widget .bot .bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 6px 0;
  font-size: 12px;
}
#erp-ai-widget .bot .bubble th,
#erp-ai-widget .bot .bubble td {
  border: 1px solid #e5e7eb;
  padding: 6px 8px;
  text-align: left;
}
#erp-ai-widget .bot .bubble th {
  background: #f3f4f6;
  font-weight: 600;
}

/* piccoli fix di layout */
#erp-ai-widget { position: fixed; }
#erp-ai-widget .ai-log { position: relative; }
