/* =====================================================================
   Swiss AI Agency — Chat-Widget (chatbot.css)
   ---------------------------------------------------------------------
   Selbsttragendes, front-end-only Support-Chat-Widget.
   - Alle Klassen mit Prefix `saig-chat` (Kollisionsschutz).
   - Design-Tokens als CSS-Variablen auf `.saig-chat` (vererben an Kinder).
   - Dark Mode ueber `html.dark` (Tailwind darkMode:"class").
   - Respektiert `prefers-reduced-motion`.
   - Kein horizontaler Overflow, hoher z-index ueber Header/Cookie-Banner.
   ===================================================================== */

/* ---- Wurzel: nur Variablen-Halter, erzeugt selbst keine Box ---------- */
.saig-chat {
  display: contents;

  /* Marken- und Flaechenfarben (Light) */
  --saig-red: #d91e18;
  --saig-red-hover: #bd1912;
  --saig-red-active: #a5150f;
  --saig-ink: #1e293b;
  --saig-surface: #ffffff;   /* Panel-Grundflaeche */
  --saig-page: #f7f9fb;      /* Nachrichtenliste */
  --saig-muted: #5d6673;
  --saig-border: #e2e8f0;
  --saig-bot-bubble: #eef1f5;
  --saig-bot-ink: #1e293b;
  --saig-user-ink: #ffffff;
  --saig-chip-bg: #ffffff;
  --saig-chip-border: #e2e8f0;

  /* Struktur */
  --saig-radius: 18px;
  --saig-radius-bubble: 16px;
  --saig-shadow: 0 12px 40px rgba(7, 17, 28, .18), 0 2px 8px rgba(7, 17, 28, .08);
  --saig-shadow-launcher: 0 8px 22px rgba(217, 30, 24, .34), 0 4px 12px rgba(7, 17, 28, .18);
  --saig-shadow-launcher-hover: 0 14px 32px rgba(217, 30, 24, .42), 0 6px 16px rgba(7, 17, 28, .22);
  --saig-z: 2147483000;
  --saig-font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --saig-anim: .22s cubic-bezier(.22, .61, .36, 1);
}

/* Dark-Mode-Tokens: Seite schaltet `dark` auf <html> */
html.dark .saig-chat {
  --saig-ink: #eff1f3;
  --saig-surface: #0b1622;
  --saig-page: #06111d;
  --saig-muted: #aab4c0;
  --saig-border: rgba(255, 255, 255, .12);
  --saig-bot-bubble: #12202f;
  --saig-bot-ink: #eff1f3;
  --saig-chip-bg: #12202f;
  --saig-chip-border: rgba(255, 255, 255, .14);
  --saig-shadow: 0 16px 46px rgba(0, 0, 0, .55), 0 2px 10px rgba(0, 0, 0, .4);
}

/* Alles im Widget teilt Box-Model + Font unabhaengig von Seiten-CSS */
.saig-chat *,
.saig-chat *::before,
.saig-chat *::after {
  box-sizing: border-box;
}

/* =====================================================================
   1) Launcher (geschlossener Zustand)
   ===================================================================== */
.saig-chat-launcher {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 60px;
  height: 60px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--saig-red);
  color: #ffffff;
  cursor: pointer;
  display: grid;
  place-items: center;
  z-index: var(--saig-z);
  box-shadow: var(--saig-shadow-launcher);
  transition: transform var(--saig-anim), box-shadow var(--saig-anim), opacity var(--saig-anim);
  -webkit-tap-highlight-color: transparent;
}

.saig-chat-launcher:hover {
  background: var(--saig-red-hover);
  transform: translateY(-3px);
  box-shadow: var(--saig-shadow-launcher-hover);
}

.saig-chat-launcher:active {
  background: var(--saig-red-active);
  transform: translateY(-1px);
}

.saig-chat-launcher:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 3px;
  box-shadow: var(--saig-shadow-launcher-hover), 0 0 0 6px rgba(217, 30, 24, .35);
}

.saig-chat-launcher svg {
  width: 28px;
  height: 28px;
  display: block;
  pointer-events: none;
}

/* Benachrichtigungs-Punkt */
.saig-chat-launcher__dot {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #ffffff;
  border: 2px solid var(--saig-red);
}

.saig-chat-launcher__dot::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .85);
  animation: saig-chat-pulse 2s ease-out infinite;
}

@keyframes saig-chat-pulse {
  0%   { transform: scale(.6); opacity: .8; }
  70%  { transform: scale(1.6); opacity: 0; }
  100% { transform: scale(1.6); opacity: 0; }
}

/* Panel offen -> Launcher ausblenden */
.saig-chat.is-open .saig-chat-launcher {
  transform: scale(.6);
  opacity: 0;
  pointer-events: none;
}

/* =====================================================================
   2) Panel (offener Zustand)
   ===================================================================== */
.saig-chat-panel {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: min(380px, calc(var(--saig-vw) - 40px));
  height: min(560px, calc(100vh - 120px));
  max-height: calc(100vh - 40px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--saig-surface);
  color: var(--saig-ink);
  border: 1px solid var(--saig-border);
  border-radius: var(--saig-radius);
  box-shadow: var(--saig-shadow);
  font-family: var(--saig-font);
  font-size: 14px;
  line-height: 1.5;
  z-index: var(--saig-z);

  /* Start: geschlossen / unsichtbar */
  opacity: 0;
  visibility: hidden;
  transform: translateY(14px) scale(.98);
  transform-origin: bottom right;
  pointer-events: none;
  transition: opacity var(--saig-anim), transform var(--saig-anim), visibility var(--saig-anim);
}

.saig-chat.is-open .saig-chat-panel {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ---- Header --------------------------------------------------------- */
.saig-chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--saig-red);
  color: #ffffff;
  flex: 0 0 auto;
}

.saig-chat-header__avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .16);
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.saig-chat-header__avatar svg {
  width: 22px;
  height: 22px;
  display: block;
}

.saig-chat-header__meta {
  min-width: 0;
  flex: 1 1 auto;
}

.saig-chat-header__title {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saig-chat-header__subtitle {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 12px;
  line-height: 1.3;
  color: rgba(255, 255, 255, .88);
}

.saig-chat-header__status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #3ddc84;
  box-shadow: 0 0 0 3px rgba(61, 220, 132, .3);
  flex: 0 0 auto;
}

.saig-chat-header__close {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, .0);
  color: #ffffff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background var(--saig-anim);
}

.saig-chat-header__close:hover {
  background: rgba(255, 255, 255, .18);
}

.saig-chat-header__close:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

.saig-chat-header__close svg {
  width: 20px;
  height: 20px;
  display: block;
}

/* ---- Nachrichtenliste ---------------------------------------------- */
.saig-chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 14px 8px;
  background: var(--saig-page);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--saig-border) transparent;
}

.saig-chat-log::-webkit-scrollbar {
  width: 8px;
}

.saig-chat-log::-webkit-scrollbar-thumb {
  background: var(--saig-border);
  border-radius: 8px;
}

/* ---- Nachrichten-Bubbles ------------------------------------------- */
.saig-chat-msg {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: var(--saig-radius-bubble);
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  animation: saig-chat-msg-in var(--saig-anim);
}

@keyframes saig-chat-msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.saig-chat-msg--bot {
  align-self: flex-start;
  background: var(--saig-bot-bubble);
  color: var(--saig-bot-ink);
  border-bottom-left-radius: 5px;
}

.saig-chat-msg--user {
  align-self: flex-end;
  background: var(--saig-red);
  color: var(--saig-user-ink);
  border-bottom-right-radius: 5px;
}

/* Eingebettete CTA (nur intern gesetzt, feste href) */
.saig-chat-msg__cta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--saig-red);
  color: #ffffff !important;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  transition: background var(--saig-anim);
}

.saig-chat-msg__cta:hover {
  background: var(--saig-red-hover);
}

.saig-chat-msg__cta:focus-visible {
  outline: 2px solid var(--saig-red);
  outline-offset: 2px;
}

/* ---- Quick-Reply-Chips --------------------------------------------- */
.saig-chat-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-self: flex-start;
  max-width: 100%;
  margin: 2px 0 4px;
}

.saig-chat-chip {
  padding: 7px 13px;
  border: 1px solid var(--saig-red);
  border-radius: 999px;
  background: var(--saig-chip-bg);
  color: var(--saig-red);
  font-family: var(--saig-font);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
  transition: background var(--saig-anim), color var(--saig-anim), transform var(--saig-anim);
}

.saig-chat-chip:hover {
  background: var(--saig-red);
  color: #ffffff;
  transform: translateY(-1px);
}

.saig-chat-chip:focus-visible {
  outline: 2px solid var(--saig-red);
  outline-offset: 2px;
}

/* ---- Typing-Indicator ---------------------------------------------- */
.saig-chat-typing {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 12px 14px;
  border-radius: var(--saig-radius-bubble);
  border-bottom-left-radius: 5px;
  background: var(--saig-bot-bubble);
}

.saig-chat-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--saig-muted);
  opacity: .5;
  animation: saig-chat-blink 1.3s infinite ease-in-out both;
}

.saig-chat-typing__dot:nth-child(2) { animation-delay: .18s; }
.saig-chat-typing__dot:nth-child(3) { animation-delay: .36s; }

@keyframes saig-chat-blink {
  0%, 80%, 100% { transform: scale(.7); opacity: .4; }
  40%           { transform: scale(1);  opacity: 1; }
}

/* ---- Eingabezeile --------------------------------------------------- */
.saig-chat-input {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--saig-border);
  background: var(--saig-surface);
}

.saig-chat-input__field {
  flex: 1 1 auto;
  min-width: 0;
  max-height: 120px;
  min-height: 44px;
  padding: 11px 14px;
  border: 1px solid var(--saig-border);
  border-radius: 14px;
  background: var(--saig-page);
  color: var(--saig-ink);
  font-family: var(--saig-font);
  font-size: 14px;
  line-height: 1.4;
  resize: none;
  overflow-y: auto;
  transition: border-color var(--saig-anim), box-shadow var(--saig-anim);
}

.saig-chat-input__field::placeholder {
  color: var(--saig-muted);
}

.saig-chat-input__field:focus {
  outline: none;
  border-color: var(--saig-red);
  box-shadow: 0 0 0 3px rgba(217, 30, 24, .16);
}

.saig-chat-input__send {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--saig-red);
  color: #ffffff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background var(--saig-anim), transform var(--saig-anim), opacity var(--saig-anim);
}

.saig-chat-input__send:hover {
  background: var(--saig-red-hover);
  transform: translateY(-1px);
}

.saig-chat-input__send:focus-visible {
  outline: 2px solid var(--saig-red);
  outline-offset: 2px;
}

.saig-chat-input__send:disabled {
  opacity: .45;
  cursor: default;
  transform: none;
}

.saig-chat-input__send svg {
  width: 20px;
  height: 20px;
  display: block;
}

/* ---- Footer --------------------------------------------------------- */
.saig-chat-footer {
  flex: 0 0 auto;
  padding: 6px 14px 10px;
  text-align: center;
  font-size: 11px;
  line-height: 1.3;
  color: var(--saig-muted);
  background: var(--saig-surface);
}

/* =====================================================================
   3) Responsive: Mobil (<480px) nahezu Vollbild
   ===================================================================== */
@media (max-width: 480px) {
  .saig-chat-panel {
    right: 10px;
    left: 10px;
    bottom: 10px;
    top: 10px;
    width: auto;
    height: auto;
    max-height: none;
    border-radius: 16px;
  }

  .saig-chat-launcher {
    right: 16px;
    bottom: 16px;
  }
}

/* =====================================================================
   4) Reduced Motion
   ===================================================================== */
@media (prefers-reduced-motion: reduce) {
  .saig-chat-launcher,
  .saig-chat-panel,
  .saig-chat-msg,
  .saig-chat-chip,
  .saig-chat-input__field,
  .saig-chat-input__send,
  .saig-chat-header__close,
  .saig-chat-msg__cta {
    transition: none !important;
    animation: none !important;
  }

  .saig-chat-launcher__dot::after,
  .saig-chat-typing__dot {
    animation: none !important;
  }

  .saig-chat.is-open .saig-chat-launcher {
    transform: none;
  }
}
