/* ═════════════════════════════════════════════════════
   Elsa — site assistant widget
   Loaded only when elsa_enabled = 1.
   Positions avoid the existing .scroll-top button
   (bottom: 5.5rem / right: 1.5rem — main.css §27).
   ═════════════════════════════════════════════════════ */

.elsa-root {
  --elsa-accent: #1a5276;
  --elsa-accent-dark: #123a54;
  --elsa-bg: #ffffff;
  --elsa-ink: #16202b;
  --elsa-muted: #64748b;
  --elsa-line: #e4e9f0;
  --elsa-radius: 18px;

  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  z-index: 1000;
  font-family: inherit;
  color: var(--elsa-ink);
}

/* ── Launcher ── */
.elsa-launcher {
  width: 58px;
  height: 58px;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--elsa-accent), var(--elsa-accent-dark));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(10, 30, 60, 0.28);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  position: relative;
}

.elsa-launcher:hover { transform: translateY(-3px); box-shadow: 0 14px 34px rgba(10, 30, 60, 0.34); }
.elsa-launcher:focus-visible { outline: 3px solid #4c9ffe; outline-offset: 3px; }
.elsa-launcher svg { width: 26px; height: 26px; display: block; }
.elsa-launcher .elsa-launcher__close { display: none; }
.elsa-root[data-open="true"] .elsa-launcher .elsa-launcher__chat { display: none; }
.elsa-root[data-open="true"] .elsa-launcher .elsa-launcher__close { display: block; }

/* Subtle attention pulse, once, when the greeting auto-opens */
.elsa-launcher::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--elsa-accent);
  opacity: 0;
  pointer-events: none;
}
.elsa-root[data-nudge="true"] .elsa-launcher::after { animation: elsa-nudge 1.6s ease-out 2; }
@keyframes elsa-nudge {
  0% { opacity: 0.7; transform: scale(0.9); }
  100% { opacity: 0; transform: scale(1.35); }
}

/* ── Greeting bubble (typewriter) ── */
.elsa-bubble {
  position: absolute;
  right: 0;
  bottom: calc(100% + 14px);
  max-width: min(280px, calc(100vw - 3rem));
  background: #ffffff;
  color: var(--elsa-ink);
  border: 1px solid var(--elsa-line);
  border-radius: 14px;
  padding: 0.6rem 0.85rem;
  font-size: 0.85rem;
  line-height: 1.45;
  box-shadow: 0 10px 30px rgba(10, 30, 60, 0.16);
  pointer-events: none;
  opacity: 0;
  transform: translateY(8px);
  animation: elsa-bubble-in 0.35s ease forwards;
  z-index: 1001;
}
@keyframes elsa-bubble-in {
  to { opacity: 1; transform: none; }
}
.elsa-bubble::before,
.elsa-bubble::after {
  content: "";
  position: absolute;
  top: 100%;
  right: 20px;
  width: 0;
  height: 0;
  border: 9px solid transparent;
  border-top-color: var(--elsa-line);
  border-bottom: 0;
}
.elsa-bubble::after {
  right: 21px;
  border-width: 8px;
  border-top-color: #ffffff;
}

.elsa-bubble--hidden {
  animation: none;
  transition: opacity 0.18s ease, transform 0.18s ease, visibility 0.18s;
  opacity: 0;
  transform: translateY(8px);
  visibility: hidden;
}

.elsa-bubble__caret {
  display: inline-block;
  width: 2px;
  height: 0.95em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--elsa-accent);
  border-radius: 1px;
  animation: elsa-caret 1.05s steps(1) infinite;
}
@keyframes elsa-caret {
  50% { opacity: 0; }
}

/* ── Panel ── */
.elsa-panel {
  position: absolute;
  right: 0;
  bottom: calc(100% + 0.85rem);
  width: min(384px, calc(100vw - 3rem));
  height: min(560px, calc(100dvh - 11rem));
  background: var(--elsa-bg);
  border-radius: var(--elsa-radius);
  box-shadow: 0 24px 60px rgba(8, 22, 40, 0.28);
  border: 1px solid var(--elsa-line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(12px) scale(0.98);
  transform-origin: bottom right;
  visibility: hidden;
  transition: opacity 0.22s ease, transform 0.22s ease, visibility 0.22s;
}

.elsa-root[data-open="true"] .elsa-panel {
  opacity: 1;
  transform: none;
  visibility: visible;
}

/* ── Header ── */
.elsa-header {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.9rem 1rem;
  background: linear-gradient(135deg, var(--elsa-accent), var(--elsa-accent-dark));
  color: #fff;
}

.elsa-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
}

.elsa-header__text { flex: 1; min-width: 0; line-height: 1.25; }
.elsa-header__text strong { display: block; font-size: 0.98rem; }
.elsa-header__text span {
  font-size: 0.75rem;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.elsa-header__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #43e07a;
  display: inline-block;
}

.elsa-close {
  background: transparent;
  border: 0;
  color: #fff;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 8px;
}
.elsa-close:hover { background: rgba(255, 255, 255, 0.16); }
.elsa-close:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

/* ── Messages ── */
.elsa-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.9rem 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  scroll-behavior: smooth;
  background: #f7f9fc;
}

.elsa-msg {
  max-width: 86%;
  padding: 0.6rem 0.8rem;
  border-radius: 14px;
  font-size: 0.92rem;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: normal;
}

.elsa-msg--user {
  align-self: flex-end;
  background: var(--elsa-accent);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.elsa-msg--bot {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--elsa-line);
  border-bottom-left-radius: 5px;
}

.elsa-msg p { margin: 0 0 0.5rem; }
.elsa-msg p:last-child { margin-bottom: 0; }
.elsa-msg ul { margin: 0.3rem 0 0.5rem; padding-left: 1.1rem; }
.elsa-msg li { margin: 0.15rem 0; }
.elsa-msg a { color: var(--elsa-accent); font-weight: 600; text-decoration: underline; }
.elsa-msg--user a { color: #fff; }

/* Sources under a reply */
.elsa-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.5rem;
}
.elsa-source {
  font-size: 0.72rem;
  background: #eef3f9;
  border: 1px solid var(--elsa-line);
  color: var(--elsa-accent-dark);
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  text-decoration: none;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.elsa-source:hover { background: #e2eaf3; }

/* Typing indicator */
.elsa-typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 0.7rem 0.85rem;
  background: #fff;
  border: 1px solid var(--elsa-line);
  border-radius: 14px;
  border-bottom-left-radius: 5px;
}
.elsa-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #94a3b8;
  animation: elsa-bounce 1.2s infinite ease-in-out;
}
.elsa-typing span:nth-child(2) { animation-delay: 0.15s; }
.elsa-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes elsa-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ── Quick-reply chips ── */
.elsa-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.55rem 0.9rem;
  background: #f7f9fc;
  border-top: 1px solid var(--elsa-line);
}
.elsa-chips:empty { display: none; }

.elsa-chip {
  border: 1px solid var(--elsa-line);
  background: #fff;
  color: var(--elsa-accent-dark);
  font-size: 0.78rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.elsa-chip:hover { background: #eef3f9; border-color: var(--elsa-accent); }
.elsa-chip:focus-visible { outline: 2px solid var(--elsa-accent); outline-offset: 1px; }

/* ── Composer ── */
.elsa-composer {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  padding: 0.6rem 0.7rem;
  border-top: 1px solid var(--elsa-line);
  background: #fff;
}

.elsa-input {
  flex: 1;
  border: 1px solid var(--elsa-line);
  background: #f7f9fc;
  border-radius: 12px;
  padding: 0.6rem 0.75rem;
  font: inherit;
  font-size: 0.92rem;
  line-height: 1.4;
  resize: none;
  max-height: 110px;
  color: var(--elsa-ink);
}
.elsa-input:focus { outline: none; border-color: var(--elsa-accent); background: #fff; }
.elsa-input::placeholder { color: #94a3b8; }

.elsa-send {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: var(--elsa-accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease;
}
.elsa-send:hover { background: var(--elsa-accent-dark); }
.elsa-send:disabled { opacity: 0.45; cursor: not-allowed; }
.elsa-send:focus-visible { outline: 3px solid #4c9ffe; outline-offset: 2px; }
.elsa-send svg { width: 18px; height: 18px; }

/* ── Escalation footer ── */
.elsa-footer {
  display: flex;
  justify-content: center;
  gap: 1rem;
  padding: 0.45rem 0.7rem 0.6rem;
  background: #fff;
  border-top: 1px solid var(--elsa-line);
  font-size: 0.73rem;
}
.elsa-footer a {
  color: var(--elsa-muted);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
.elsa-footer a:hover { color: var(--elsa-accent); text-decoration: underline; }

/* ── Mobile: bottom sheet ── */
@media (max-width: 640px) {
  .elsa-root {
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    align-items: flex-end;
  }

  .elsa-panel {
    position: fixed;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    height: min(78dvh, calc(100dvh - 4rem));
    border-radius: 20px 20px 0 0;
    transform-origin: bottom center;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .elsa-root[data-open="false"] .elsa-panel { pointer-events: none; }

  .elsa-messages { padding-bottom: 0.6rem; }
  .elsa-bubble { right: 0.75rem; }
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  .elsa-panel,
  .elsa-launcher { transition: none; }
  .elsa-typing span,
  .elsa-root[data-nudge="true"] .elsa-launcher::after,
  .elsa-bubble__caret { animation: none; }
  .elsa-bubble { animation: none; opacity: 1; transform: none; }
  .elsa-bubble--hidden { opacity: 0; transform: translateY(8px); visibility: hidden; }
}
