/* =====================================================
   Manna AI Chatbot Widget v2
   ===================================================== */

/* ── CSS Variables ── */
#mai-chat-widget {
  --mai-primary:      #2563eb;
  --mai-primary-dark: #1d4ed8;
  --mai-bg:           #ffffff;
  --mai-border:       #e2e8f0;
  --mai-text:         #1e293b;
  --mai-muted:        #94a3b8;
  --mai-user-bg:      #2563eb;
  --mai-bot-bg:       #f1f5f9;

  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 99999;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ── Toggle Button ── */
#mai-chat-widget #mai-chat-toggle {
  position: relative;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--mai-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.2s, box-shadow 0.2s;
}

#mai-chat-widget #mai-chat-toggle:hover {
  background: var(--mai-primary-dark);
  transform: scale(1.06);
  box-shadow: 0 6px 22px rgba(37, 99, 235, 0.55);
}

#mai-chat-widget #mai-chat-toggle:active {
  transform: scale(0.95);
}

/* Ambient pulse ring */
#mai-chat-widget #mai-chat-toggle::before {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid rgba(37, 99, 235, 0.3);
  animation: maiPulseRing 2.8s ease-out infinite;
  pointer-events: none;
}

@keyframes maiPulseRing {
  0%   { transform: scale(0.9);  opacity: 1; }
  70%  { transform: scale(1.28); opacity: 0; }
  100% { transform: scale(1.28); opacity: 0; }
}

/* ── Unread Badge ── */
#mai-chat-widget #mai-unread-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 18px;
  height: 18px;
  background: #ef4444;
  border: 2px solid #fff;
  border-radius: 9px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  pointer-events: none;
  animation: maiBadgePop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#mai-chat-widget #mai-unread-badge[hidden] {
  display: none;
}

@keyframes maiBadgePop {
  from { transform: scale(0); }
  to   { transform: scale(1); }
}

/* ── Chat Box ── */
#mai-chat-widget #mai-chat-box {
  position: absolute;
  bottom: 68px;
  right: 0;
  width: 360px;
  max-height: 540px;
  background: var(--mai-bg);
  border-radius: 18px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, .18), 0 2px 8px rgba(0, 0, 0, .07);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: maiChatOpen 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  transform-origin: bottom right;
}

#mai-chat-widget #mai-chat-box[hidden] {
  display: none;
}

@keyframes maiChatOpen {
  from { opacity: 0; transform: scale(0.88) translateY(12px); }
  to   { opacity: 1; transform: scale(1)    translateY(0); }
}

/* ── Header ── */
#mai-chat-widget #mai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--mai-primary) 0%, #4f46e5 100%);
  color: #fff;
  font-weight: 600;
  font-size: 15px;
  flex-shrink: 0;
}

/* Online dot before title */
#mai-chat-widget #mai-chat-title::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  background: #4ade80;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
  position: relative;
  top: -1px;
  box-shadow: 0 0 0 2px rgba(74, 222, 128, .3);
  animation: maiOnlinePulse 2.5s ease infinite;
}

@keyframes maiOnlinePulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.55; }
}

#mai-chat-widget #mai-chat-close {
  background: none;
  border: none;
  color: rgba(255, 255, 255, .75);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 5px;
  transition: color 0.15s, background 0.15s;
  display: flex;
  align-items: center;
}

#mai-chat-widget #mai-chat-close:hover {
  color: #fff;
  background: rgba(255, 255, 255, .15);
}

/* ── Messages Area ── */
#mai-chat-widget #mai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 380px;
  scroll-behavior: smooth;
}

/* Slim custom scrollbar */
#mai-chat-widget #mai-chat-messages::-webkit-scrollbar {
  width: 4px;
}
#mai-chat-widget #mai-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
#mai-chat-widget #mai-chat-messages::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 2px;
}
#mai-chat-widget #mai-chat-messages::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* ── Message Bubbles ── */
#mai-chat-widget .mai-msg {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  animation: maiMsgIn 0.22s ease;
}

@keyframes maiMsgIn {
  from { opacity: 0; transform: translateY(7px); }
  to   { opacity: 1; transform: translateY(0); }
}

#mai-chat-widget .mai-msg.user {
  background: var(--mai-user-bg);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 8px rgba(37, 99, 235, .22);
}

#mai-chat-widget .mai-msg.assistant {
  background: var(--mai-bot-bg);
  color: var(--mai-text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

/* ── Error Bubble ── */
#mai-chat-widget .mai-msg.mai-msg-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  font-size: 13px;
  max-width: 92%;
}

#mai-chat-widget .mai-msg.mai-msg-error .mai-err-icon {
  margin-right: 4px;
  font-style: normal;
}

#mai-chat-widget .mai-msg.mai-msg-error details {
  margin-top: 8px;
}

#mai-chat-widget .mai-msg.mai-msg-error details summary {
  cursor: pointer;
  font-size: 11px;
  color: #b91c1c;
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 4px;
}

#mai-chat-widget .mai-msg.mai-msg-error details summary::before {
  content: '▶';
  font-size: 9px;
  transition: transform 0.15s;
}

#mai-chat-widget .mai-msg.mai-msg-error details[open] summary::before {
  transform: rotate(90deg);
}

#mai-chat-widget .mai-msg.mai-msg-error details pre {
  margin: 6px 0 0;
  font-size: 10px;
  font-family: ui-monospace, 'Cascadia Code', monospace;
  white-space: pre-wrap;
  word-break: break-all;
  background: rgba(185, 28, 28, .06);
  padding: 7px 9px;
  border-radius: 5px;
  max-height: 140px;
  overflow-y: auto;
  color: #7f1d1d;
  line-height: 1.5;
}

/* ── Typing Indicator ── */
#mai-chat-widget .mai-msg.typing {
  background: var(--mai-bot-bg);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  padding: 13px 16px;
}

.mai-typing-dots {
  display: flex;
  gap: 5px;
  align-items: center;
}

.mai-typing-dots span {
  width: 7px;
  height: 7px;
  background: var(--mai-muted);
  border-radius: 50%;
  animation: maiDotBounce 1.3s ease-in-out infinite;
}

.mai-typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.mai-typing-dots span:nth-child(3) { animation-delay: 0.36s; }

@keyframes maiDotBounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: 0.45; }
  30%           { transform: translateY(-6px); opacity: 1;    }
}

/* ── Input Area ── */
#mai-chat-widget #mai-chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--mai-border);
  align-items: flex-end;
  background: #fafafa;
  flex-shrink: 0;
}

#mai-chat-widget #mai-chat-input {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--mai-border);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.5;
  max-height: 100px;
  overflow-y: auto;
  outline: none;
  font-family: inherit;
  background: #fff;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#mai-chat-widget #mai-chat-input:focus {
  border-color: var(--mai-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

#mai-chat-widget #mai-chat-send {
  width: 38px;
  height: 38px;
  background: var(--mai-primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.15s;
}

#mai-chat-widget #mai-chat-send:hover {
  background: var(--mai-primary-dark);
  transform: scale(1.06);
}

#mai-chat-widget #mai-chat-send:active {
  transform: scale(0.94);
}

#mai-chat-widget #mai-chat-send:disabled {
  background: #cbd5e1;
  cursor: not-allowed;
  transform: none;
}

/* ── Lead Form ── */
#mai-chat-widget #mai-lead-form-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
}

#mai-chat-widget .mai-lead-form-inner {
  text-align: center;
  width: 100%;
}

#mai-chat-widget .mai-lead-form-inner p {
  margin: 0 0 18px;
  font-size: 14px;
  line-height: 1.5;
  color: #475569;
}

#mai-chat-widget #mai-lead-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#mai-chat-widget #mai-lead-form input {
  padding: 10px 14px;
  border: 1.5px solid var(--mai-border);
  border-radius: 10px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
  width: 100%;
  box-sizing: border-box;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#mai-chat-widget #mai-lead-form input:focus {
  border-color: var(--mai-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

#mai-chat-widget #mai-lead-form button {
  background: var(--mai-primary);
  color: #fff;
  border: none;
  padding: 12px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
  width: 100%;
}

#mai-chat-widget #mai-lead-form button:hover {
  background: var(--mai-primary-dark);
}

#mai-chat-widget #mai-lead-form button:active {
  transform: scale(0.98);
}

/* ── Markdown in Assistant Messages ── */
#mai-chat-widget .mai-msg.assistant p {
  margin-top: 0;
  margin-bottom: 8px;
}
#mai-chat-widget .mai-msg.assistant p:last-child { margin-bottom: 0; }

#mai-chat-widget .mai-msg.assistant ul,
#mai-chat-widget .mai-msg.assistant ol {
  margin: 0 0 8px;
  padding-left: 18px;
}

#mai-chat-widget .mai-msg.assistant li { margin-bottom: 3px; }

#mai-chat-widget .mai-msg.assistant strong {
  font-weight: 600;
  color: #0f172a;
}

#mai-chat-widget .mai-msg.assistant a {
  color: var(--mai-primary);
  text-decoration: none;
  font-weight: 500;
}

#mai-chat-widget .mai-msg.assistant a:hover { text-decoration: underline; }

#mai-chat-widget .mai-msg.assistant code {
  background: rgba(0, 0, 0, .06);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
  font-family: ui-monospace, monospace;
}

/* ── Responsive ── */
@media (max-width: 420px) {
  #mai-chat-widget {
    bottom: 16px;
    right: 12px;
  }

  #mai-chat-widget #mai-chat-box {
    width: calc(100vw - 24px);
    right: 0;
  }
}
