/* ==========================================================================
   Loupe Factory — Docs Support Chatbot
   Restrained, brand-consistent, dark + light parity via [data-md-color-scheme].
   No cards in the marketing sense; inline hierarchy only.
   ========================================================================== */

:root {
  --lf-chat-panel-bg: #ffffff;
  --lf-chat-panel-fg: #141824;
  --lf-chat-muted: rgba(20, 24, 36, 0.62);
  --lf-chat-subtle: rgba(20, 24, 36, 0.08);
  --lf-chat-subtle-strong: rgba(20, 24, 36, 0.14);
  --lf-chat-accent: #3874ff;
  --lf-chat-accent-strong: #003cc7;
  --lf-chat-accent-contrast: #ffffff;
  --lf-chat-user-bubble: linear-gradient(135deg, #3874ff 0%, #2056cc 100%);
  --lf-chat-user-bubble-fg: #ffffff;
  --lf-chat-assistant-fg: #141824;
  --lf-chat-panel-shadow: 0 24px 60px rgba(9, 21, 49, 0.18);
  --lf-chat-fab-bg: linear-gradient(135deg, #11275a 0%, #2a63d4 100%);
  --lf-chat-fab-shadow: 0 12px 32px rgba(9, 21, 49, 0.28);
  --lf-chat-source-border: rgba(43, 96, 207, 0.24);
  --lf-chat-error: #c7372b;
  --lf-chat-thumbs-active: #25b003;
}

[data-md-color-scheme="slate"] {
  --lf-chat-panel-bg: #0c1322;
  --lf-chat-panel-fg: #f3f6fd;
  --lf-chat-muted: rgba(243, 246, 253, 0.7);
  --lf-chat-subtle: rgba(243, 246, 253, 0.08);
  --lf-chat-subtle-strong: rgba(243, 246, 253, 0.16);
  --lf-chat-accent: #85a9ff;
  --lf-chat-accent-strong: #c9d7ff;
  --lf-chat-accent-contrast: #071029;
  --lf-chat-user-bubble: linear-gradient(135deg, #3b6df0 0%, #1c3fa3 100%);
  --lf-chat-user-bubble-fg: #f1f5ff;
  --lf-chat-assistant-fg: #edf2fb;
  --lf-chat-panel-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  --lf-chat-fab-bg: linear-gradient(135deg, #0b1e49 0%, #2457bd 100%);
  --lf-chat-fab-shadow: 0 16px 38px rgba(0, 0, 0, 0.52);
  --lf-chat-source-border: rgba(134, 170, 255, 0.28);
  --lf-chat-error: #ff8a7a;
  --lf-chat-thumbs-active: #80d96a;
}

/* Container ------------------------------------------------------------- */

lf-support-chat {
  position: fixed;
  z-index: 2147483000;
  bottom: 1.25rem;
  right: 1.25rem;
  pointer-events: none;
  font-family: var(--md-text-font, "Nunito Sans"), system-ui, -apple-system, sans-serif;
  font-size: 0.875rem;
  color: var(--lf-chat-panel-fg);
  line-height: 1.5;
}

lf-support-chat > * {
  pointer-events: auto;
}

lf-support-chat[hidden] {
  display: none !important;
}

/* FAB ------------------------------------------------------------------- */

.lf-chat-fab {
  appearance: none;
  border: 0;
  background: var(--lf-chat-fab-bg);
  color: #ffffff;
  width: 3.5rem;
  height: 3.5rem;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--lf-chat-fab-shadow);
  transition: transform 140ms ease-out, box-shadow 140ms ease-out;
}

.lf-chat-fab:hover { transform: translateY(-2px); box-shadow: 0 18px 42px rgba(9, 21, 49, 0.34); }
.lf-chat-fab:focus-visible { outline: 2px solid var(--lf-chat-accent); outline-offset: 3px; }
.lf-chat-fab svg { width: 1.5rem; height: 1.5rem; }

.lf-chat-fab[data-open="true"] { display: none; }

/* Panel ----------------------------------------------------------------- */

.lf-chat-panel {
  position: absolute;
  bottom: 0;
  right: 0;
  display: none;
  width: min(24rem, calc(100vw - 2.5rem));
  height: min(36rem, calc(100vh - 3rem));
  background: var(--lf-chat-panel-bg);
  color: var(--lf-chat-panel-fg);
  border-radius: 1rem;
  border: 1px solid var(--lf-chat-subtle-strong);
  box-shadow: var(--lf-chat-panel-shadow);
  overflow: hidden;
  flex-direction: column;
  transform-origin: 100% 100%;
  animation: lf-chat-pop 180ms ease-out;
}

.lf-chat-panel[data-open="true"] { display: flex; }

@keyframes lf-chat-pop {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)   scale(1); }
}

@media (max-width: 480px) {
  lf-support-chat { bottom: 0; right: 0; left: 0; }
  .lf-chat-panel {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    border: 0;
  }
  .lf-chat-fab { margin: 0 1rem 1rem auto; display: flex; }
}

/* Panel header ---------------------------------------------------------- */

.lf-chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.8rem 1rem;
  border-bottom: 1px solid var(--lf-chat-subtle);
  flex-shrink: 0;
}

.lf-chat-header-brand {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.lf-chat-header-mark {
  width: 2.1rem;
  height: 2.1rem;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 0.55rem;
  background: color-mix(in srgb, var(--lf-chat-accent) 10%, var(--lf-chat-panel-bg));
  color: var(--lf-chat-accent);
  border: 1px solid var(--lf-chat-source-border);
}

.lf-chat-header-mark svg {
  width: 1.15rem;
  height: 1.15rem;
}

.lf-chat-header-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.08rem;
}

.lf-chat-title {
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.15;
  letter-spacing: -0.015em;
}

.lf-chat-sub {
  font-size: 0.74rem;
  color: var(--lf-chat-muted);
  font-weight: 600;
  line-height: 1.25;
}

.lf-chat-header-actions {
  margin-left: auto;
  display: inline-flex;
  gap: 0.25rem;
}

.lf-chat-icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--lf-chat-muted);
  width: 2rem;
  height: 2rem;
  border-radius: 0.5rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms ease, color 120ms ease;
}
.lf-chat-icon-btn:hover { background: var(--lf-chat-subtle); color: var(--lf-chat-panel-fg); }
.lf-chat-icon-btn:focus-visible { outline: 2px solid var(--lf-chat-accent); outline-offset: 2px; }
.lf-chat-icon-btn svg { width: 1.1rem; height: 1.1rem; }

/* Message area ---------------------------------------------------------- */

.lf-chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scroll-behavior: smooth;
}

.lf-chat-log::-webkit-scrollbar { width: 8px; }
.lf-chat-log::-webkit-scrollbar-thumb {
  background: var(--lf-chat-subtle-strong);
  border-radius: 4px;
}

.lf-chat-empty {
  color: var(--lf-chat-muted);
  font-size: 0.875rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 0;
}

.lf-chat-empty strong {
  color: var(--lf-chat-panel-fg);
  font-weight: 700;
  letter-spacing: -0.01em;
  font-size: 1rem;
}

.lf-chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  margin-top: 0.25rem;
}

.lf-chat-suggestion {
  appearance: none;
  text-align: left;
  border: 1px solid var(--lf-chat-subtle-strong);
  background: transparent;
  color: var(--lf-chat-panel-fg);
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  font-size: 0.82rem;
  cursor: pointer;
  font-family: inherit;
  transition: background 120ms ease, border-color 120ms ease;
}
.lf-chat-suggestion:hover {
  background: var(--lf-chat-subtle);
  border-color: var(--lf-chat-accent);
}

/* Turns + bubbles ------------------------------------------------------- */

.lf-chat-turn { display: flex; flex-direction: column; gap: 0.375rem; }
.lf-chat-turn--user { align-items: flex-end; }
.lf-chat-turn--assistant { align-items: flex-start; }

.lf-chat-bubble {
  max-width: 85%;
  padding: 0.625rem 0.875rem;
  border-radius: 0.875rem;
  font-size: 0.875rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.lf-chat-turn--user .lf-chat-bubble {
  background: var(--lf-chat-user-bubble);
  color: var(--lf-chat-user-bubble-fg);
  border-bottom-right-radius: 0.25rem;
}

.lf-chat-turn--assistant .lf-chat-bubble {
  background: var(--lf-chat-subtle);
  color: var(--lf-chat-assistant-fg);
  border-bottom-left-radius: 0.25rem;
  max-width: 100%;
}

/* Markdown rendering inside assistant bubbles */
.lf-chat-bubble p { margin: 0 0 0.5rem; }
.lf-chat-bubble p:last-child { margin-bottom: 0; }
.lf-chat-bubble ol,
.lf-chat-bubble ul {
  margin: 0.25rem 0 0.5rem;
  padding-left: 1.25rem;
}
.lf-chat-bubble li { margin: 0.2rem 0; }
.lf-chat-bubble code {
  font-family: var(--md-code-font, "JetBrains Mono"), ui-monospace, monospace;
  font-size: 0.82em;
  background: var(--lf-chat-subtle-strong);
  padding: 0.1em 0.35em;
  border-radius: 0.25rem;
}
.lf-chat-bubble pre {
  background: var(--lf-chat-subtle-strong);
  padding: 0.6rem 0.75rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 0.4rem 0;
}
.lf-chat-bubble pre code {
  background: transparent;
  padding: 0;
  font-size: 0.78em;
}
.lf-chat-bubble a {
  color: var(--lf-chat-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Streaming caret */
.lf-chat-caret {
  display: inline-block;
  width: 0.5em;
  height: 1em;
  background: currentColor;
  vertical-align: -0.15em;
  margin-left: 2px;
  animation: lf-chat-blink 1s steps(2) infinite;
  opacity: 0.6;
}
@keyframes lf-chat-blink { to { opacity: 0; } }

/* Under-answer blocks --------------------------------------------------- */

.lf-chat-sources {
  border-top: 1px dashed var(--lf-chat-source-border);
  padding-top: 0.5rem;
  margin-top: 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  width: 100%;
  max-width: 100%;
}

.lf-chat-sources-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--lf-chat-muted);
}

.lf-chat-source {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  text-decoration: none;
  color: inherit;
  padding: 0.4rem 0.55rem;
  border-radius: 0.4rem;
  transition: background 120ms ease;
}
.lf-chat-source:hover { background: var(--lf-chat-subtle); }

.lf-chat-source-title {
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--lf-chat-accent);
}
.lf-chat-source-section {
  font-size: 0.7rem;
  color: var(--lf-chat-muted);
  letter-spacing: 0.02em;
}

.lf-chat-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.lf-chat-cta {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--lf-chat-accent);
  color: var(--lf-chat-accent-contrast);
  padding: 0.45rem 0.75rem;
  border-radius: 0.5rem;
  text-decoration: none;
  font-weight: 600;
  font-size: 0.82rem;
  transition: background 120ms ease, transform 120ms ease;
}
.lf-chat-cta,
.lf-chat-cta:visited,
.lf-chat-bubble .lf-chat-cta,
.lf-chat-bubble .lf-chat-cta:visited {
  color: var(--lf-chat-accent-contrast);
  text-decoration: none;
}
.lf-chat-cta:hover { background: var(--lf-chat-accent-strong); transform: translateY(-1px); }
.lf-chat-cta:hover,
.lf-chat-cta:focus-visible,
.lf-chat-bubble .lf-chat-cta:hover,
.lf-chat-bubble .lf-chat-cta:focus-visible {
  color: var(--lf-chat-accent-contrast);
  text-decoration: none;
}
.lf-chat-cta svg { width: 0.9rem; height: 0.9rem; }

.lf-chat-thumbs {
  margin-left: auto;
  display: inline-flex;
  gap: 0.125rem;
}

.lf-chat-thumb {
  appearance: none;
  background: transparent;
  border: 1px solid transparent;
  color: var(--lf-chat-muted);
  width: 1.9rem;
  height: 1.9rem;
  border-radius: 0.4rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 120ms ease, background 120ms ease, border-color 120ms ease;
}
.lf-chat-thumb:hover { background: var(--lf-chat-subtle); color: var(--lf-chat-panel-fg); }
.lf-chat-thumb[data-active="true"] {
  color: var(--lf-chat-thumbs-active);
  border-color: var(--lf-chat-thumbs-active);
}
.lf-chat-thumb svg { width: 1rem; height: 1rem; }

/* Fallback + error states ---------------------------------------------- */

.lf-chat-fallback {
  padding: 0.75rem;
  border: 1px solid var(--lf-chat-source-border);
  border-radius: 0.5rem;
  background: var(--lf-chat-subtle);
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.lf-chat-fallback strong { color: var(--lf-chat-panel-fg); }

.lf-chat-error {
  color: var(--lf-chat-error);
  font-size: 0.82rem;
  padding: 0.375rem 0.5rem;
  background: color-mix(in srgb, var(--lf-chat-error) 12%, transparent);
  border-radius: 0.4rem;
}

/* Composer -------------------------------------------------------------- */

.lf-chat-composer {
  flex-shrink: 0;
  padding: 0.75rem;
  border-top: 1px solid var(--lf-chat-subtle);
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  background: var(--lf-chat-panel-bg);
}

.lf-chat-textarea {
  flex: 1 1 auto;
  resize: none;
  background: var(--lf-chat-subtle);
  color: var(--lf-chat-panel-fg);
  border: 1px solid transparent;
  border-radius: 0.75rem;
  padding: 0.6rem 0.75rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.5;
  min-height: 2.5rem;
  max-height: 8rem;
  overflow-y: auto;
  transition: border-color 120ms ease, background 120ms ease;
}
.lf-chat-textarea::placeholder { color: var(--lf-chat-muted); }
.lf-chat-textarea:focus {
  outline: none;
  border-color: var(--lf-chat-accent);
  background: var(--lf-chat-panel-bg);
}

.lf-chat-send {
  appearance: none;
  border: 0;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 0.75rem;
  background: var(--lf-chat-accent);
  color: var(--lf-chat-accent-contrast);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms ease, transform 120ms ease;
}
.lf-chat-send:hover:not(:disabled) { background: var(--lf-chat-accent-strong); transform: translateY(-1px); }
.lf-chat-send:disabled { opacity: 0.45; cursor: not-allowed; }
.lf-chat-send svg { width: 1.05rem; height: 1.05rem; }

.lf-chat-foot {
  font-size: 0.72rem;
  color: var(--lf-chat-muted);
  padding: 0 1rem 0.7rem;
  text-align: center;
  line-height: 1.45;
}

@media (prefers-reduced-motion: reduce) {
  .lf-chat-panel,
  .lf-chat-caret,
  .lf-chat-fab,
  .lf-chat-cta,
  .lf-chat-send {
    animation: none !important;
    transition: none !important;
  }
}
