/**
 * Chatbot: launcher, popover shell, and skin for the ai_assistant_ui component.
 *
 * Colours, radii and spacing are ported from the previous deep-chat build
 * (fsj_theme/css/chatbot.css plus the messageStyles/auxiliaryStyle JSON that
 * used to be inlined in ai-deepchat--loom-bot.html.twig) so the widget looks
 * unchanged to users. deep-chat was a web component with a shadow DOM, which
 * is why that styling had to be injected as style strings; ai_assistant_ui
 * renders plain light DOM, so ordinary selectors work.
 */

.chatbot {
  --chatbot-brand: #195365;
  --chatbot-text: #355464;
  --chatbot-surface: #ffffff;
  --chatbot-bubble-ai: #f4f9fb;
  --chatbot-bubble-user: #e6f3ae;
  --chatbot-input-border: #6896a8;
  --chatbot-rule: rgba(104, 150, 168, 0.2);

  font-family: 'Inter', sans-serif;
}

/* ---------------------------------------------------------------- launcher */

.chatbot__launcher {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 1257;
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 11px;
  border: 0;
  border-radius: 50% 50% 0 50%;
  background-color: var(--chatbot-brand);
  box-shadow: rgba(0, 0, 0, 0.5) 0 0 6px 1px;
  color: var(--chatbot-surface);
  cursor: pointer;
}

.chatbot__launcher-text {
  font-weight: 700;
  line-height: 20px;
  color: var(--chatbot-surface);
}

/* Hide the launcher while the panel is open so they never overlap. */
.chatbot:has(.chatbot__panel:popover-open) .chatbot__launcher {
  display: none;
}

/* ------------------------------------------------------------------- panel */

.chatbot__panel {
  /*
   * display:flex is load-bearing, not cosmetic: the messages pane scrolls by
   * being a flex child with min-height:0. Without a flex context here the
   * component grows to its full content height and overflow:hidden silently
   * clips the answer instead of scrolling it.
   */
  display: flex;
  flex-direction: column;

  /* Reset UA popover defaults (inset:0; margin:auto; border; padding). */
  position: fixed;
  inset: auto 0 0 auto;
  width: 100%;
  max-width: 100%;
  height: min(600px, 90dvh);
  max-height: 90dvh;
  margin: 0;
  padding: 0;
  border: 0;
  background: var(--chatbot-surface);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2), 0 6px 20px rgba(0, 0, 0, 0.19);
  color: var(--chatbot-text);
  overflow: hidden;
  z-index: 1258;
}

.chatbot__panel:not(:popover-open) {
  display: none;
}

@media (min-width: 768px) {
  .chatbot__panel {
    right: 20px;
    /*bottom: 20px;*/
    width: 50%;
    height: min(720px, 85dvh);
    max-height: 85dvh;
  }
}

@media (min-width: 1200px) {
  .chatbot__panel {
    width: 550px;
  }
}

.chatbot__panel-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  background-color: var(--chatbot-surface);
  border-bottom: 1px solid var(--chatbot-rule);
}

.chatbot__panel-title {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 1.26px;
  color: var(--chatbot-brand);
}

.chatbot__close {
  display: flex;
  padding: 3px 5px;
  border: 0;
  background: none;
  color: var(--chatbot-text);
  cursor: pointer;
}

.chatbot__panel .ai-chatbot__disclaimer {
  margin: 0;
  padding: 0 0 16px;
  background-color: var(--chatbot-surface);
  font-size: 11px;
  line-height: 18px;
  color: #000;
}

/* ------------------------------------------------ ai_assistant_ui component */

.chatbot__panel .ai-chatbot {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  height: auto;
}

/* The panel head above already shows the bot name; avoid a second bar. */
.chatbot__panel .ai-chatbot__header {
  display: none;
}

.chatbot__panel .ai-chatbot__messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 30px;
}

/* ---------------------------------------------------------------- messages */

/*
 * Longhand, not the margin shorthand: the component right-aligns user
 * messages with margin-inline-start:auto, and a shorthand here would win on
 * specificity and reset it, flattening both roles to the left.
 */
.chatbot__panel .ai-chatbot__message {
  margin-bottom: 5px;
}

/*
 * The previous build showed bubbles only, with no sender labels. Kept in the
 * accessibility tree rather than removed, since the <dl> needs its <dt>.
 */
.chatbot__panel .ai-chatbot__message-sender {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.chatbot__panel .ai-chatbot__message-content {
  margin: 0 20px 0 0;
  padding: 12px;
  border-radius: 10px 10px 10px 2px;
  background: var(--chatbot-bubble-ai);
  font-size: 13px;
  line-height: 17px;
  color: var(--chatbot-text);
}

.chatbot__panel .ai-chatbot__message-content--user {
  margin: 0 0 0 20px;
  border-radius: 10px 10px 2px 10px;
  background: var(--chatbot-bubble-user);
  color: #000;
}

/* Links keep the wipe-in underline the old bubbles used. */
.chatbot__panel .ai-chatbot__message-content a,
.chatbot__panel .ai-chatbot__message-content a:link,
.chatbot__panel .ai-chatbot__message-content a:visited {
  color: var(--chatbot-text);
  text-decoration: underline;
  background-image: linear-gradient(var(--chatbot-bubble-user), var(--chatbot-bubble-user));
  background-repeat: no-repeat;
  background-size: 0 5px;
  background-position: 0 calc(100% - 6px);
  transition: background-size 0.3s ease-in-out;
}

.chatbot__panel .ai-chatbot__message-content a:hover {
  color: var(--chatbot-text);
  background-size: 100% 5px;
}

/* Partial Markdown can become a heading mid-stream; never use page-title sizes. */
.chatbot__panel .ai-chatbot__message-content :is(h1, h2, h3, h4, h5, h6) {
  margin: 1em 0 0.5em;
  font: inherit;
  font-weight: 600;
  color: inherit;
}

.chatbot__panel .ai-chatbot__message-content :is(p, h1, h2, h3, h4, h5, h6):first-child {
  margin-top: 0;
}

.chatbot__panel .ai-chatbot__message-content :is(p, h1, h2, h3, h4, h5, h6):last-child {
  margin-bottom: 0;
}

/* Override the component's button reset to keep copying a secondary action. */
.chatbot__panel .ai-chatbot__copy {
  padding: 3px 8px;
  font-size: 12px;
  line-height: 16px;
}

.chatbot__panel .ai-chatbot__copy svg {
  width: 1em;
  height: 1em;
}

.chatbot__panel .ai-chatbot__thinking {
  margin: 0 30px;
}

.chatbot__panel .ai-chatbot__message-content:has(.ai-chatbot__thinking:not([hidden])) {
  margin: 10px 0;
}

/* -------------------------------------------------------- intro + prompts */

.chatbot__panel .ai-chatbot__intro-text h2 {
  font-size: 22px;
  font-weight: 600;
  color: var(--chatbot-text);
}

.chatbot__panel .ai-chatbot__intro {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/*
 * Two-up grid, as before. The buttons are siblings of the intro text rather
 * than wrapped in their own container, so the grid is declared on a
 * :has()-scoped rule instead of a wrapper element.
 */
@media (min-width: 480px) {
  .chatbot__panel .ai-chatbot__intro {
    display: grid;
    gap: 16px;
    grid-template-columns: repeat(2, 1fr);
  }

  .chatbot__panel .ai-chatbot__intro-text {
    grid-column: 1 / -1;
  }
}

.chatbot__panel .ai-chatbot__suggestion-btn {
  margin: 0;
  padding: 12px;
  border: 0;
  border-radius: 0;
  background-color: var(--chatbot-bubble-user);
  color: #000;
  font-family: inherit;
  font-size: 16px;
  line-height: 24px;
  text-align: left;
  cursor: pointer;
}

.chatbot__panel .ai-chatbot__suggestion-btn:hover,
.chatbot__panel .ai-chatbot__suggestion-btn:focus-visible {
  filter: brightness(0.96);
}

/* -------------------------------------------------------------- composer */

.chatbot__panel .ai-chatbot__footer {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 12px;
  padding: 0 16px;
  background-color: var(--chatbot-surface);
}

.chatbot__panel .ai-chatbot__input-wrapper {
  order: 0;
  flex: 0 0 100%;
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 16px 0 12px;
  padding: 13px 16px;
  border: 2px solid var(--chatbot-input-border);
  border-radius: 2px;
  background: var(--chatbot-surface);
  box-sizing: border-box;
}

.chatbot__panel .ai-chatbot__input-wrapper:focus-within {
  border-color: #000;
}

.chatbot__panel .ai-chatbot__input {
  flex: 1 1 auto;
  padding: 0;
  border: 0;
  background: transparent;
  font-family: inherit;
  font-size: 14px;
  line-height: normal;
  color: var(--chatbot-text);
  resize: none;
}

.chatbot__panel .ai-chatbot__input:focus {
  outline: none;
}

/*
 * Icon-only submit, as before. font-size:0 hides the label visually while
 * leaving it as the button's accessible name.
 */
.chatbot__panel .ai-chatbot__send {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  padding: 0;
  border: 0;
  background: none;
  font-size: 0;
  color: var(--chatbot-brand);
  cursor: pointer;
}

.chatbot__panel .ai-chatbot__send svg {
  width: 16px;
  height: 16px;
  stroke: var(--chatbot-brand);
}

.chatbot__panel .ai-chatbot__send[disabled] {
  opacity: 0.4;
  cursor: default;
}

.chatbot__panel .ai-chatbot__clear {
  margin: 0;
  padding: 0 0 1px;
  white-space: nowrap;
  border: 0;
  border-bottom: 1px solid var(--chatbot-text);
  background: none;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--chatbot-text);
  cursor: pointer;
  text-decoration: none;
  border-radius: 0;
}

.chatbot__panel .ai-chatbot__clear:hover {
  border-bottom-color: transparent;
}

/* ------------------------------------------------------------ external links */

.chatbot__panel .ai-chatbot__message-content a[target="_blank"]::after {
  content: " (" attr(data-host) ")";
  font-size: 0.85em;
  color: var(--chatbot-input-border);
  background: none;
}

/* ------------------------------------------------------------- vote buttons */

/*
 * Replaces ai_evaluations' deep-chat button injection. The old rule hid every
 * button and un-hid only `.outer-message-container:last-child`, which silently
 * failed whenever anything trailed the messages. Here the buttons exist only
 * on the message the JS attaches them to, so no :last-child gamble.
 *
 * Scoped to .chatbot__panel deliberately: the component resets its buttons
 * with `.ai-chatbot button { background: transparent; border: 1px solid }`,
 * and that (0,1,1) selector beats a lone class, blanking the icon.
 */
.chatbot__panel .chatbot__vote {
  display: flex;
  gap: 5px;
  margin-top: 6px;
  /* @todo - remove this when there's some decision about the ai-evaluations module */
  display: none;
}

.chatbot__panel .chatbot__vote-btn {
  width: 25px;
  height: 25px;
  padding: 0;
  border: 0;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  cursor: pointer;
}

/*
 * The --hover assets are used as the resting state: the plain ones fill the
 * icon with #aec5cc, which all but disappears against the bubble. Same files
 * as before, just the darker pair (#56868a). Hover darkens further.
 */
.chatbot__panel .chatbot__vote-btn--up {
  background-image: url("../assets/bot/like-button-icon--hover.svg");
}

.chatbot__panel .chatbot__vote-btn--down {
  background-image: url("../assets/bot/dislike-button-icon--hover.svg");
}

.chatbot__panel .chatbot__vote-btn:hover,
.chatbot__panel .chatbot__vote-btn[aria-pressed="true"] {
  filter: brightness(0.82);
}

.chatbot__panel .chatbot__vote[data-voted="true"] .chatbot__vote-btn:not([aria-pressed="true"]) {
  opacity: 0.35;
  pointer-events: none;
}

/* ------------------------------------------------------------------- print */

@media print {
  .chatbot {
    display: none;
  }
}

