// Chatbot widget — launcher + dialog panel. Brand colours come from
// `libs/shared/tokens` via the global stylesheet; touch-target
// minimums are 44 × 44 px per ADR-0016. Citation side-panel styling
// lives in chatbot-citation-panel.scss to keep this file under the
// `anyComponentStyle` budget from ADR-0017.

$radius: 0.75rem;
$z-launcher: 40;
$z-panel: 50;

// Tokens that switch on the .dark theme. Declared as CSS custom
// properties so the dark-mode override is a single block at the
// bottom of the file instead of repeated `:host-context(.dark)`
// declarations on every selector.
:where(chatbot-host) {
  --chatbot-surface: #ffffff;
  --chatbot-surface-elevated: #ffffff;
  --chatbot-border: #e5e7eb;
  --chatbot-text: #1f2937;
  --chatbot-text-muted: #6b7280;
  --chatbot-assistant-bg: #f3f4f6;
  --chatbot-assistant-text: #1f2937;
  --chatbot-suggestion-bg: var(--color-brand-primary-50);
  --chatbot-suggestion-border: var(--color-brand-primary-200);
  --chatbot-suggestion-text: var(--color-brand-primary-700);
  --chatbot-suggestion-hover: var(--color-brand-primary-100);
  --chatbot-input-bg: #ffffff;
  --chatbot-input-border: #d1d5db;
  --chatbot-error-bg: rgba(220, 38, 38, 0.1);
  --chatbot-error-border: rgba(220, 38, 38, 0.35);
  --chatbot-error-text: #991b1b;
}

:host-context(.dark) chatbot-host {
  --chatbot-surface: #0f172a;
  --chatbot-surface-elevated: #0b1220;
  --chatbot-border: #1f2937;
  --chatbot-text: #e5e7eb;
  --chatbot-text-muted: #9ca3af;
  --chatbot-assistant-bg: #1f2937;
  --chatbot-assistant-text: #e5e7eb;
  --chatbot-suggestion-bg: rgba(31, 127, 150, 0.12);
  --chatbot-suggestion-border: rgba(31, 127, 150, 0.32);
  --chatbot-suggestion-text: var(--color-brand-primary-200);
  --chatbot-suggestion-hover: rgba(31, 127, 150, 0.2);
  --chatbot-input-bg: #0b1220;
  --chatbot-input-border: #374151;
  --chatbot-error-bg: rgba(220, 38, 38, 0.18);
  --chatbot-error-border: rgba(220, 38, 38, 0.5);
  --chatbot-error-text: #fecaca;
}

// ---------- Launcher ----------

.chatbot-launcher {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: $z-launcher;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 3.5rem; // 56 × 56 px, above 44 × 44 baseline
  height: 3.5rem;
  border-radius: 9999px;
  border: 0;
  cursor: pointer;
  background-color: var(--color-brand-primary-500);
  color: #ffffff;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.12),
    0 6px 16px rgba(15, 71, 92, 0.22);
  transition:
    background-color 0.15s ease-out,
    transform 0.15s ease-out;

  &:hover {
    background-color: var(--color-brand-primary-600);
  }
  &:active {
    transform: translateY(1px);
  }
  &:focus-visible {
    outline: 2px solid var(--color-brand-primary-500);
    outline-offset: 4px;
  }

  &--hidden {
    visibility: hidden;
    pointer-events: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chatbot-launcher {
    transition: none;
  }
}

// ---------- Panel ----------

.chatbot-panel {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: $z-panel;
  display: flex;
  flex-direction: column;
  width: min(22rem, calc(100vw - 2rem));
  height: min(32rem, calc(100vh - 2rem));
  background-color: var(--chatbot-surface);
  border: 1px solid var(--chatbot-border);
  border-radius: $radius;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.08),
    0 12px 28px rgba(15, 71, 92, 0.18);
  overflow: hidden;
  color: var(--chatbot-text);
}

.chatbot-panel--fullscreen {
  inset: 1rem;
  width: auto;
  height: auto;
}

@media (max-width: 30rem) {
  .chatbot-panel:not(.chatbot-panel--fullscreen) {
    width: calc(100vw - 1rem);
    right: 0.5rem;
    bottom: 0.5rem;
  }
}

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

.chatbot-panel__header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0.75rem;
  background-color: var(--color-brand-primary-500);
  color: #ffffff;
  border-top-left-radius: $radius;
  border-top-right-radius: $radius;
}

.chatbot-panel__title {
  margin: 0;
  flex: 1 1 auto;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.chatbot-panel__header-actions {
  display: flex;
  gap: 0.25rem;
}

.chatbot-icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem; // 44 × 44 px touch target (ADR-0016)
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  border-radius: 0.375rem;
  transition: background-color 0.12s ease-out;

  &:hover {
    background-color: rgba(255, 255, 255, 0.12);
  }
  &:focus-visible {
    outline: 2px solid #ffffff;
    outline-offset: 2px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chatbot-icon-button {
    transition: none;
  }
}

// ---------- Body + empty state ----------

.chatbot-panel__body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0.875rem 1rem;
  font-size: 0.9rem;
  line-height: 1.45;
  background-color: var(--chatbot-surface);
  color: var(--chatbot-text);
}

.chatbot-greeting {
  margin: 0 0 1rem;
  color: var(--chatbot-text);
}

.chatbot-suggestions {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
}

.chatbot-suggestion {
  display: block;
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--chatbot-suggestion-border);
  background-color: var(--chatbot-suggestion-bg);
  color: var(--chatbot-suggestion-text);
  border-radius: 0.5rem;
  text-align: left;
  cursor: pointer;
  font-size: 0.875rem;
  transition:
    background-color 0.12s ease-out,
    color 0.12s ease-out;

  &:hover {
    background-color: var(--chatbot-suggestion-hover);
  }
  &:focus-visible {
    outline: 2px solid var(--color-brand-primary-500);
    outline-offset: 2px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chatbot-suggestion {
    transition: none;
  }
}

// ---------- Messages ----------

.chatbot-messages {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.chatbot-message {
  max-width: 85%;
  padding: 0.5rem 0.75rem;
  border-radius: 0.625rem;
  word-break: break-word;
  hyphens: auto;
}

.chatbot-message__content {
  margin: 0;
  white-space: pre-wrap;
}

.chatbot-message--user {
  align-self: flex-end;
  background-color: var(--color-brand-primary-500);
  color: #ffffff;
}

.chatbot-message--assistant {
  align-self: flex-start;
  background-color: var(--chatbot-assistant-bg);
  color: var(--chatbot-assistant-text);
}

.chatbot-message--error {
  align-self: flex-start;
}

.chatbot-message__error {
  padding: 0.5rem 0.75rem;
  background-color: var(--chatbot-error-bg);
  border: 1px solid var(--chatbot-error-border);
  border-radius: 0.5rem;
  color: var(--chatbot-error-text);
  font-size: 0.85rem;
}

// ---------- Typing dots (reduced-motion gated) ----------

.chatbot-typing {
  display: inline-flex;
  gap: 0.25rem;
  margin-top: 0.375rem;
}

.chatbot-typing__dot {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 9999px;
  background-color: var(--color-brand-primary-400);
  animation: chatbot-typing-bounce 1s infinite ease-in-out;

  &:nth-child(2) {
    animation-delay: 0.15s;
  }
  &:nth-child(3) {
    animation-delay: 0.3s;
  }
}

@keyframes chatbot-typing-bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.6;
  }
  30% {
    transform: translateY(-0.25rem);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chatbot-typing__dot {
    animation: none;
    opacity: 1;
  }
}

// ---------- Inline citation chips ----------

.chatbot-citations {
  list-style: none;
  margin: 0.5rem 0 0;
  padding: 0;
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.chatbot-citation {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem;
  min-height: 2.75rem; // 44 × 44 touch target
  padding: 0 0.5rem;
  border: 1px solid var(--chatbot-suggestion-border);
  background-color: var(--chatbot-suggestion-bg);
  color: var(--chatbot-suggestion-text);
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.12s ease-out;

  &:hover {
    background-color: var(--chatbot-suggestion-hover);
  }
  &:focus-visible {
    outline: 2px solid var(--color-brand-primary-500);
    outline-offset: 2px;
  }
}

// ---------- Input form ----------

.chatbot-panel__form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.625rem 0.75rem;
  border-top: 1px solid var(--chatbot-border);
  background-color: var(--chatbot-surface);
}

.chatbot-panel__input {
  flex: 1 1 auto;
  resize: none;
  min-height: 2.75rem;
  max-height: 8rem;
  padding: 0.5rem 0.625rem;
  border: 1px solid var(--chatbot-input-border);
  border-radius: 0.5rem;
  font-family: inherit;
  font-size: 0.9rem;
  line-height: 1.4;
  background-color: var(--chatbot-input-bg);
  color: var(--chatbot-text);

  &:focus-visible {
    outline: 2px solid var(--color-brand-primary-500);
    outline-offset: 1px;
    border-color: transparent;
  }
  &:disabled {
    background-color: var(--chatbot-assistant-bg);
    color: var(--chatbot-text-muted);
    cursor: not-allowed;
  }
}

.chatbot-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem; // 44 × 44 touch target
  border: 0;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: background-color 0.12s ease-out;

  &:disabled {
    cursor: not-allowed;
    opacity: 0.55;
  }
  &:focus-visible {
    outline: 2px solid var(--color-brand-primary-500);
    outline-offset: 2px;
  }
}

.chatbot-action--send {
  background-color: var(--color-brand-primary-500);
  color: #ffffff;
  &:hover:not(:disabled) {
    background-color: var(--color-brand-primary-600);
  }
}

.chatbot-action--stop {
  background-color: #b91c1c;
  color: #ffffff;
  &:hover {
    background-color: #991b1b;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chatbot-action {
    transition: none;
  }
}
