/* ===== Chat Widget — Frontier Labs Aesthetic ===== */

/* D76 / D95: layout and behaviour are the Coffee chat's (D79); colours, font and rounding come from the project's accepted brand tokens
   (the theme.css custom properties the site already uses). The literal fallbacks are the Coffee values, so a page without the tokens looks as before.
   Contrast stays safe by construction: text/background use the site's own text/surface pair, and every filled control uses --cta, which the
   theme derives to 4.5:1 against white. */
.cgl-chat-trigger,
.cgl-chat-teaser,
.cgl-chat-panel {
  --chat-bg: var(--brand-900, #0f0f1a);
  --chat-surface: var(--brand-800, #1a1a2e);
  --chat-surface-hover: var(--brand-700, #252545);
  --chat-text: var(--brand-50, #eeeef5);
  --chat-text-muted: var(--brand-200, #a0a0be);
  --chat-accent: var(--cta, #10b981);
  --chat-accent-hover: var(--cta-hover, #059669);
  --chat-on-accent: #ffffff;
  --chat-user-bg: var(--cta, #0f3021);
  --chat-border: rgba(255, 255, 255, 0.1);
  --chat-radius: var(--site-radius, 16px);
  --chat-radius-sm: var(--site-radius-sm, 10px);
  --chat-radius-pill: var(--site-radius-pill, 20px);
}
@supports (color: color-mix(in srgb, red, blue)) {
  .cgl-chat-trigger, .cgl-chat-teaser, .cgl-chat-panel {
    --chat-border: color-mix(in srgb, var(--brand-50, #ffffff) 16%, transparent);
  }
}

/* --- Trigger Button --- */
.cgl-chat-trigger {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--chat-accent);
  color: var(--chat-on-accent);
  box-shadow:
    0 4px 24px rgba(0, 0, 0, 0.3),
    0 0 0 1px rgba(255, 255, 255, 0.06);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  animation: cgl-chat-pop 0.3s ease backwards;
  animation-delay: 1s;
}

.cgl-chat-trigger:hover {
  transform: scale(1.08);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.1);
}

.cgl-chat-trigger:active {
  transform: scale(0.96);
}

.cgl-chat-trigger--open {
  background: var(--chat-surface-hover);
  color: var(--chat-text);
}

@keyframes cgl-chat-pop {
  from {
    opacity: 0;
    transform: scale(0.6);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* --- Teaser Bubble --- */
.cgl-chat-teaser {
  position: fixed;
  bottom: 36px;
  right: 90px;
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-radius: var(--chat-radius-sm);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.35),
    0 0 0 1px rgba(255, 255, 255, 0.06);
  animation: cgl-chat-teaser-in 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  white-space: nowrap;
  font-family: var(--font-body, Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif);
}

.cgl-chat-teaser::after {
  content: '';
  position: absolute;
  right: -6px;
  bottom: 18px;
  width: 12px;
  height: 12px;
  background: var(--chat-surface);
  border-right: 1px solid var(--chat-border);
  border-bottom: 1px solid var(--chat-border);
  transform: rotate(-45deg);
}

.cgl-chat-teaser-text {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--chat-text);
}

.cgl-chat-teaser-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--chat-text-muted);
  padding: 2px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.cgl-chat-teaser-close:hover {
  color: var(--chat-text);
  background: rgba(255, 255, 255, 0.08);
}

@keyframes cgl-chat-teaser-in {
  from {
    opacity: 0;
    transform: translateX(10px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

/* --- Panel --- */
.cgl-chat-panel {

  position: fixed;
  bottom: 92px;
  right: 24px;
  z-index: 9998;
  width: 400px;
  height: 560px;
  max-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  border: 1px solid var(--chat-border);
  border-radius: var(--chat-radius);
  box-shadow:
    0 24px 80px rgba(0, 0, 0, 0.5),
    0 0 0 1px rgba(255, 255, 255, 0.08);
  overflow: hidden;
  animation: cgl-chat-slide-up 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  font-family: var(--font-body, Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif);
  font-size: 14px;
  line-height: 1.5;
  color: var(--chat-text);
}

/* Isolate from site global styles — override inherited body color */
.cgl-chat-panel,
.cgl-chat-panel * {
  color: inherit;
}
.cgl-chat-panel {
  color: var(--chat-text);
}

@keyframes cgl-chat-slide-up {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* --- Header --- */
.cgl-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--chat-border);
  background: var(--chat-surface);
  flex-shrink: 0;
}

.cgl-chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cgl-chat-header-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chat-accent);
  box-shadow: none;
}

.cgl-chat-header-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--chat-text);
}

.cgl-chat-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--chat-text-muted);
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
}

.cgl-chat-close:hover {
  color: var(--chat-text);
  background: rgba(255, 255, 255, 0.06);
}

/* --- Messages area --- */
.cgl-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.1) transparent;
}

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

.cgl-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.cgl-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
}

/* --- Welcome screen --- */
.cgl-chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 32px 16px 16px;
  gap: 8px;
}

.cgl-chat-welcome-icon {
  color: var(--chat-accent);
  margin-bottom: 4px;
  opacity: 0.8;
}

.cgl-chat-welcome-text {
  font-size: 16px;
  font-weight: 600;
  color: var(--chat-text);
  margin: 0;
}

.cgl-chat-welcome-sub {
  font-size: 13px;
  color: var(--chat-text-muted);
  margin: 0 0 12px;
  max-width: 280px;
}

.cgl-chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.cgl-chat-suggestion {
  padding: 8px 14px;
  border-radius: var(--chat-radius-pill);
  border: 1px solid var(--chat-border);
  background: var(--chat-surface-hover);
  color: var(--chat-text-muted);
  font-size: 12.5px;
  cursor: pointer;
  transition: all 0.15s ease;
  font-family: inherit;
  line-height: 1.3;
}

.cgl-chat-suggestion:hover {
  border-color: var(--chat-accent);
  color: var(--chat-text);
  background: var(--chat-surface-hover);
}

/* --- Message bubbles --- */
.cgl-chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 88%;
  animation: cgl-chat-msg-in 0.2s ease;
}

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

.cgl-chat-msg--user {
  align-self: flex-end;
}

.cgl-chat-msg--assistant {
  align-self: flex-start;
}

.cgl-chat-msg-bubble {
  padding: 10px 14px;
  border-radius: var(--chat-radius);
  word-break: break-word;
}

.cgl-chat-msg-bubble p {
  margin: 0;
}

.cgl-chat-msg-bubble p + p {
  margin-top: 8px;
}

.cgl-chat-msg-bubble ul {
  margin: 6px 0;
  padding-left: 18px;
}

.cgl-chat-msg-bubble li {
  margin-bottom: 2px;
}

.cgl-chat-msg-bubble a {
  color: var(--chat-text);
  text-decoration: underline;
  text-decoration-color: var(--chat-text-muted);
  text-underline-offset: 2px;
}

.cgl-chat-msg-bubble a:hover {
  text-decoration-color: var(--chat-text);
}

.cgl-chat-msg-bubble strong {
  font-weight: 600;
  color: var(--chat-text);
}

.cgl-chat-msg--user .cgl-chat-msg-bubble {
  background: var(--chat-user-bg);
  border: 1px solid var(--chat-accent);
  color: var(--chat-on-accent);
  border-bottom-right-radius: 4px;
}

.cgl-chat-msg--assistant .cgl-chat-msg-bubble {
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-bottom-left-radius: 4px;
  color: var(--chat-text);
}

/* --- Streaming cursor --- */
.cgl-chat-cursor {
  display: inline-block;
  width: 2px;
  height: 14px;
  background: var(--chat-accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cgl-chat-blink 0.8s step-end infinite;
}

@keyframes cgl-chat-blink {
  50% { opacity: 0; }
}

/* --- Typing indicator --- */
.cgl-chat-typing {
  display: flex;
  gap: 5px;
  padding: 14px 18px;
}

.cgl-chat-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--chat-text-muted);
  animation: cgl-chat-bounce 1.2s ease-in-out infinite;
}

.cgl-chat-typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.cgl-chat-typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes cgl-chat-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* --- Source cards --- */
.cgl-chat-sources {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}

.cgl-chat-source-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 10px;
  background: var(--chat-surface-hover);
  border: 1px solid var(--chat-border);
  color: var(--chat-text-muted);
  text-decoration: none;
  font-size: 12px;
  transition: all 0.15s ease;
  overflow: hidden;
}

.cgl-chat-source-pill:hover {
  border-color: var(--chat-border);
  color: var(--chat-text);
  background: var(--chat-surface-hover);
}

.cgl-chat-source-cat {
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--chat-surface-hover);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
  color: var(--chat-accent);
}

.cgl-chat-source-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cgl-chat-source-pill svg {
  flex-shrink: 0;
  opacity: 0.5;
}

/* --- Input bar --- */
.cgl-chat-input-wrap {
  padding: 12px 16px;
  border-top: 1px solid var(--chat-border);
  background: var(--chat-surface);
  flex-shrink: 0;
}

.cgl-chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--chat-bg);
  border: 1px solid var(--chat-border);
  border-radius: 12px;
  padding: 4px 4px 4px 14px;
  transition: border-color 0.15s;
}

.cgl-chat-input-row:focus-within {
  border-color: var(--chat-accent);
}

.cgl-chat-textarea {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  resize: none;
  color: var(--chat-text);
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.5;
  padding: 8px 0;
  max-height: 120px;
}

.cgl-chat-textarea::placeholder {
  color: var(--chat-text-muted);
}

.cgl-chat-textarea:disabled {
  opacity: 0.5;
}

.cgl-chat-send {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.04);
  color: var(--chat-text-muted);
  transition: all 0.15s ease;
}

.cgl-chat-send--active {
  background: var(--chat-accent);
  color: white;
}

.cgl-chat-send--active:hover {
  background: var(--chat-accent-hover);
}

.cgl-chat-send:disabled {
  cursor: not-allowed;
  opacity: 0.3;
}

.cgl-chat-char-count {
  display: block;
  text-align: right;
  font-size: 11px;
  color: var(--chat-text-muted);
  margin-top: 4px;
  padding-right: 4px;
}

/* --- Mobile --- */
@media (max-width: 480px) {
  .cgl-chat-panel {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 85vh;
    max-height: 85vh;
    border-radius: var(--chat-radius) var(--chat-radius) 0 0;
    animation-name: cgl-chat-slide-up-mobile;
  }

  @keyframes cgl-chat-slide-up-mobile {
    from {
      opacity: 0;
      transform: translateY(100%);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .cgl-chat-trigger {
    bottom: 16px;
    right: 16px;
  }

  .cgl-chat-trigger--open {
    display: none;
  }

  .cgl-chat-teaser {
    bottom: 28px;
    right: 82px;
  }
}

/* --- Pipelines additions (D76/D77): states the original widget did not have. Same tokens, no new decoration. --- */
.cgl-chat-trigger:focus-visible,
.cgl-chat-close:focus-visible,
.cgl-chat-suggestion:focus-visible,
.cgl-chat-send:focus-visible,
.cgl-chat-source-pill:focus-visible,
.cgl-chat-teaser-close:focus-visible,
.cgl-chat-textarea:focus-visible {
  outline: 2px solid var(--chat-text);
  outline-offset: 2px;
}
.cgl-chat-notice {
  margin: 8px 0 0;
  font-size: 11.5px;
  color: var(--chat-text-muted);
  max-width: 300px;
}
.cgl-chat-source-snippet {
  display: block;
  font-size: 11.5px;
  color: var(--chat-text-muted);
  margin-top: 2px;
}
.cgl-chat-msg--note .cgl-chat-msg-bubble {
  background: transparent;
  border: 1px dashed var(--chat-border);
  color: var(--chat-text-muted);
  font-size: 13px;
}
.cgl-chat-cite {
  font-size: 11px;
  vertical-align: super;
  text-decoration: none;
  margin: 0 1px;
}
@media (prefers-reduced-motion: reduce) {
  .cgl-chat-trigger, .cgl-chat-teaser, .cgl-chat-panel, .cgl-chat-msg, .cgl-chat-cursor, .cgl-chat-typing span { animation: none; }
}
.cgl-chat-footnote { margin: 0; padding: 6px 14px 10px; max-width: none; border-top: 1px solid var(--chat-border); }
.cgl-chat-footnote:empty { display: none; }
