/* AVA Chatbot Widget — Smart Weblications GmbH
 * Ported from wiessner.it. All CSS variables are scoped to #chatbot-widget so they
 * never leak into the host page's :root / Tailwind theme. Minimal resets are scoped
 * to the widget subtree so the host's Tailwind preflight can't distort it.
 */

#chatbot-widget {
  /* brand palette — override these 6 per host (template footer) to re-theme the whole widget */
  --brand: #15803d;        /* primary */
  --brand-2: #22c55e;      /* bright accent / gradient end */
  --brand-light: #4ade80;
  --brand-dark: #14532d;   /* header gradient end (darker shade of primary) */
  --brand-rgb: 21,128,61;  /* primary as r,g,b for rgba() tints */
  --brand-glow: 34,197,94; /* accent as r,g,b for rgba() glows/borders */

  /* widget-local design tokens (do NOT leak to :root) */
  --purple: var(--brand);
  --purple-light: var(--brand-light);
  --cyan: var(--brand-2);
  --cyan-light: #86efac;
  --text: #f0fdf4;
  --text-muted: #86a386;
  --card-bg: #0f1a0f;
  --border: rgba(var(--brand-glow),0.15);
  --font-head: inherit;

  position: fixed;
  bottom: 2rem;
  right: 2rem;
  z-index: 1000;
}

/* scoped resets — independent of host reset/preflight */
#chatbot-widget *, #chatbot-widget *::before, #chatbot-widget *::after { box-sizing: border-box; }
#chatbot-widget button {
  margin: 0; font: inherit; color: inherit; cursor: pointer;
  background: none; border: 0; line-height: 1.2;
}
#chatbot-widget input { font: inherit; }
/* Honor the hidden attribute even if the host template's CSS styles all inputs
   (old smart-kvm/smart-ebics CSS forced the file input visible). */
#chatbot-widget [hidden] { display: none !important; }

#chat-toggle {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--purple), var(--cyan));
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 28px rgba(var(--brand-rgb),0.5);
  transition: transform 0.2s, box-shadow 0.2s;
  animation: chatPulse 3s ease-in-out infinite;
  padding: 3px;
  overflow: hidden;
}

#chat-toggle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: block;
}

#chat-toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 0 50px rgba(var(--brand-rgb),0.7);
}

@keyframes chatPulse {
  0%, 100% { box-shadow: 0 0 28px rgba(var(--brand-rgb),0.5); }
  50% { box-shadow: 0 0 48px rgba(var(--brand-glow),0.6), 0 0 80px rgba(var(--brand-rgb),0.25); }
}

#chat-window {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 360px;
  height: 520px;
  min-height: 380px;
  max-height: calc(100dvh - 160px);
  min-width: 280px;
  max-width: min(640px, calc(100vw - 2rem));
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  transform-origin: bottom right;
  transition: opacity 0.25s, transform 0.25s;
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  text-align: left;
}

#chat-window.hidden {
  opacity: 0;
  transform: scale(0.93) translateY(8px);
  pointer-events: none;
}

.chat-header {
  padding: 1rem 1.2rem;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--purple) 0%, var(--brand-dark) 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  font-size: 0.95rem;
}

.chat-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--brand-2);
  box-shadow: 0 0 6px var(--brand-2);
}

#voice-mode-btn, #close-chat {
  color: rgba(255,255,255,0.6); transition: color 0.2s;
  padding: 0.2rem 0.3rem; border-radius: 4px;
}
#voice-mode-btn:hover, #close-chat:hover { color: white; }
#close-chat { font-size: 1rem; }
#voice-mode-btn.active { color: var(--cyan); }

.msg-tts-btn {
  display: inline-flex; align-items: center; gap: 0.25rem;
  margin-top: 0.35rem; padding: 0.15rem 0.5rem;
  background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 4px; color: var(--text-muted); font-size: 0.7rem;
  cursor: pointer; transition: all 0.15s;
}
.msg-tts-btn:hover { background: rgba(var(--brand-rgb),0.15); border-color: var(--purple); color: var(--text); }
.msg-tts-btn.playing { color: var(--cyan); border-color: var(--cyan); }

#chat-messages {
  flex: 1;
  min-height: 0;
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

#chat-messages::-webkit-scrollbar {
  width: 4px;
}

#chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.chat-msg {
  max-width: 85%;
  padding: 0.7rem 1rem;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.55;
  word-break: break-word;
}

.chat-msg.user {
  background: var(--purple);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.chat-msg.bot {
  background: rgba(255,255,255,0.07);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.chat-msg.bot.typing::after {
  content: '▌';
  animation: blink 1s step-end infinite;
}

/* markdown inside bot messages */
.chat-msg p { margin: 0 0 0.4em; }
.chat-msg p:last-child { margin-bottom: 0; }
.chat-msg h2, .chat-msg h3, .chat-msg h4 {
  font-family: var(--font-head); font-weight: 700;
  margin: 0.6em 0 0.2em; line-height: 1.2;
}
.chat-msg h2 { font-size: 1rem; }
.chat-msg h3 { font-size: 0.93rem; }
.chat-msg h4 { font-size: 0.88rem; }
.chat-msg ul, .chat-msg ol {
  padding-left: 1.2em; margin: 0.3em 0;
}
.chat-msg li { margin: 0.15em 0; }
.chat-msg code {
  background: rgba(255,255,255,0.1); border-radius: 4px;
  padding: 0.1em 0.35em; font-size: 0.82em; font-family: monospace;
}
.chat-msg pre {
  background: rgba(0,0,0,0.35); border-radius: 8px;
  padding: 0.7em 0.9em; overflow-x: auto; margin: 0.4em 0;
}
.chat-msg pre code {
  background: none; padding: 0; font-size: 0.8em;
}
.chat-msg strong { font-weight: 700; color: var(--text); }
.chat-msg em { font-style: italic; opacity: 0.9; }

.chat-msg.florian {
  background: rgba(var(--brand-rgb),0.15);
  border: 1px solid rgba(var(--brand-rgb),0.35);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.chat-sender-label {
  display: block;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--purple-light);
  margin-bottom: 0.2rem;
}

.chat-status-line {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-muted);
  font-style: italic;
  padding: 0.1rem 0;
}

#chat-florian-bar {
  padding: 0.3rem 0.8rem 0;
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}

#request-florian {
  font-size: 0.75rem;
  color: var(--purple-light);
  border: 1px solid rgba(var(--brand-rgb),0.3);
  border-radius: 20px;
  padding: 0.25rem 0.9rem;
  transition: all 0.2s;
  background: transparent;
}

#request-florian:hover {
  background: rgba(var(--brand-rgb),0.12);
  border-color: var(--purple);
}

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

.chat-input-row {
  padding: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input-top {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

#chat-input {
  flex: 1;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.9rem;
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  outline: none;
  transition: border-color 0.2s;
}

#chat-input:focus {
  border-color: var(--purple);
}

#chat-send {
  width: 100%;
  background: linear-gradient(135deg, var(--purple), var(--cyan));
  color: white;
  border-radius: 10px;
  padding: 0.6rem 1.1rem;
  font-weight: 600;
  font-size: 0.88rem;
  transition: opacity 0.2s;
}

#chat-send:hover {
  opacity: 0.88;
}

#chat-resize-grip {
  height: 8px; flex-shrink: 0; cursor: n-resize;
  display: flex; align-items: center; justify-content: center;
  background: transparent; transition: background 0.2s;
}
#chat-resize-grip::after {
  content: ''; width: 36px; height: 3px;
  background: rgba(255,255,255,0.12); border-radius: 2px;
  transition: background 0.2s;
}
#chat-resize-grip:hover::after { background: rgba(var(--brand-glow),0.5); }
#chat-window.hidden #chat-resize-grip { display: none; }

#chat-resize-left {
  position: absolute;
  top: 8px; left: 0;
  width: 7px; height: calc(100% - 8px);
  cursor: ew-resize; z-index: 10;
}
#chat-resize-left::after {
  content: '';
  position: absolute;
  top: 50%; left: 2px;
  transform: translateY(-50%);
  width: 3px; height: 32px;
  background: rgba(255,255,255,.1); border-radius: 2px;
  transition: background .2s;
}
#chat-resize-left:hover::after { background: rgba(var(--brand-glow),.5); }
#chat-window.hidden #chat-resize-left { display: none; }

#mic-btn {
  flex-shrink: 0; width: 34px; height: 34px; border-radius: 50%;
  background: rgba(255,255,255,0.07); border: 1px solid var(--border);
  color: var(--text-muted); display: flex; align-items: center; justify-content: center;
  transition: all 0.2s; position: relative;
}
#mic-btn:hover { background: rgba(var(--brand-rgb),0.15); color: var(--text); border-color: var(--purple); }
#mic-btn.recording {
  background: rgba(239,68,68,0.2); border-color: #ef4444; color: #ef4444;
  box-shadow: 0 0 0 calc(var(--mic-level, 0) * 10px) rgba(239,68,68,0.25);
  transition: box-shadow 0.1s;
}
#mic-btn.processing { opacity: 0.5; pointer-events: none; }
#mic-btn.recording::after {
  content: 'Sprich jetzt…';
  position: absolute; bottom: calc(100% + 6px); left: 50%;
  transform: translateX(-50%);
  font-size: 0.7rem; white-space: nowrap;
  background: #1a1a1a; color: #ef4444;
  padding: 2px 7px; border-radius: 4px;
  pointer-events: none;
}

#img-upload-btn {
  flex-shrink: 0; width: 34px; height: 34px; border-radius: 50%;
  background: rgba(255,255,255,0.07); border: 1px solid var(--border);
  color: var(--text-muted); display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
}
#img-upload-btn:hover { background: rgba(var(--brand-rgb),0.15); color: var(--text); border-color: var(--purple); }

.chat-img-preview {
  max-width: 180px; max-height: 160px;
  border-radius: 8px; display: block;
  margin-bottom: 0.3rem; object-fit: cover;
  border: 1px solid var(--border);
}
.chat-img-desc {
  font-size: 0.78rem; opacity: 0.75; margin-top: 0.2rem; line-height: 1.4;
}

#chat-messages.drag-over {
  outline: 2px dashed var(--purple);
  outline-offset: -4px;
  background: rgba(var(--brand-rgb),0.04);
}

#screen-share-bar {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.8rem;
  background: rgba(var(--brand-glow),0.07);
  border-top: 1px solid rgba(var(--brand-glow),0.2);
  font-size: 0.8rem; flex-shrink: 0;
}
#screen-share-bar.hidden { display: none; }
#screen-share-txt { flex: 1; color: var(--cyan); }
#screen-share-accept {
  padding: 0.25rem 0.7rem; border-radius: 8px; cursor: pointer;
  background: rgba(var(--brand-glow),0.18); border: 1px solid var(--cyan);
  color: var(--cyan); font-size: 0.76rem;
}
#screen-share-decline {
  padding: 0.25rem 0.7rem; border-radius: 8px; cursor: pointer;
  background: rgba(255,255,255,0.05); border: 1px solid var(--border);
  color: var(--text-muted); font-size: 0.76rem;
}

/* RESPONSIVE */
@media (max-width: 960px) {
  #chat-window {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    width: 100%;
    height: 75dvh;
    max-height: 75dvh;
    border-radius: 20px 20px 0 0;
    transform-origin: bottom center;
  }
  #chat-window.hidden {
    transform: translateY(100%);
  }
  #chat-resize-left,
  #chat-resize-grip {
    display: none;
  }
}
