/*
 * BuildX chat UI inside the /embed iframe.
 * Brand colours come from /embed/theme.css (--bx-primary, --bx-accent, from config/widget.json);
 * the fallbacks below only apply when that file fails to load.
 */

:root {
  --bx-c-primary: var(--bx-primary, #070A0F);
  --bx-c-accent: var(--bx-accent, #F5AE1B);
  --bx-c-text: #1B1F27;
  --bx-c-muted: #5B6472;
  --bx-c-border: #E1E4EA;
  --bx-c-surface: #FFFFFF;
  --bx-c-canvas: #F4F5F7;
  --bx-c-error-bg: #FDEDEC;
  --bx-c-error-border: #F2C4BF;
  --bx-c-error-text: #8C1D18;
  --bx-c-invalid: #B3261E;
  --bx-c-field-border: #C9CED6;
  --bx-c-success-bg: #EDF7F0;
  --bx-c-success-border: #BFE3CB;
  --bx-c-success-text: #1E6B3A;
  --bx-font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --bx-font-mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: var(--bx-font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--bx-c-text);
  background: var(--bx-c-surface);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 3px solid var(--bx-c-primary);
  outline-offset: 2px;
}

.bx-visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.bx-icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.bx-icon--small {
  width: 16px;
  height: 16px;
}

.bx-icon--filled {
  fill: currentColor;
  stroke: none;
}

/* ---- Layout ------------------------------------------------------------------------ */

.bx-app {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.bx-main {
  position: relative; /* the lead form covers the conversation */
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
}

/* ---- Header ------------------------------------------------------------------------ */

.bx-header {
  flex: none;
  padding: 12px 10px 12px 14px;
  color: #FFFFFF;
  background: var(--bx-c-primary);
}

.bx-header :focus-visible {
  outline-color: var(--bx-c-accent);
}

.bx-header__top {
  display: flex;
  align-items: center;
  gap: 10px;
}

.bx-brand {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.bx-brand__mark {
  display: grid;
  flex: none;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  color: var(--bx-c-primary);
  background: var(--bx-c-accent);
}

.bx-brand__mark .bx-icon {
  width: 22px;
  height: 22px;
}

.bx-brand__text {
  min-width: 0;
}

.bx-brand__title,
.bx-brand__subtitle {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bx-brand__title {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.25;
}

.bx-brand__subtitle {
  font-size: 13px;
  line-height: 1.35;
  color: rgba(255, 255, 255, 0.78);
}

.bx-icon-button {
  display: grid;
  flex: none;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  color: #FFFFFF;
  background: transparent;
}

.bx-icon-button:hover {
  background: rgba(255, 255, 255, 0.12);
}

.bx-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.bx-chip-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 5px 12px;
  border: 1px solid rgba(255, 255, 255, 0.32);
  border-radius: 999px;
  font-size: 13px;
  line-height: 1.2;
  color: #FFFFFF;
  background: transparent;
}

.bx-chip-button:hover {
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.1);
}

/* "Để lại thông tin" while its form is open. */
.bx-chip-button[aria-expanded="true"] {
  color: var(--bx-c-primary);
  background: var(--bx-c-accent);
  border-color: var(--bx-c-accent);
}

/* ---- Privacy notice ------------------------------------------------------------------ */

.bx-privacy {
  flex: none;
  margin: 0;
  padding: 8px 14px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--bx-c-muted);
  background: var(--bx-c-surface);
  border-bottom: 1px solid var(--bx-c-border);
}

.bx-privacy a {
  color: var(--bx-c-primary);
  text-decoration-color: var(--bx-c-accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
}

/* ---- Messages ---------------------------------------------------------------------- */

.bx-messages {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 16px 12px 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bx-c-canvas);
  scrollbar-width: thin;
  scrollbar-color: #C4C9D2 transparent;
}

/* Focusable so keyboard users can scroll it; the ring sits inside the scroll box. */
.bx-messages:focus-visible {
  outline-offset: -3px;
}

.bx-msg {
  display: flex;
  flex-direction: column;
  max-width: 88%;
}

.bx-msg--user {
  align-self: flex-end;
  align-items: flex-end;
}

.bx-msg--bot,
.bx-msg--error,
.bx-msg--limit,
.bx-msg--notice {
  align-self: flex-start;
  align-items: flex-start;
}

.bx-msg__body {
  max-width: 100%;
  padding: 9px 13px;
  border-radius: 14px;
  overflow-wrap: anywhere;
}

.bx-msg--user .bx-msg__body {
  color: #FFFFFF;
  background: var(--bx-c-primary);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.bx-msg--bot .bx-msg__body {
  background: var(--bx-c-surface);
  border: 1px solid var(--bx-c-border);
  border-bottom-left-radius: 4px;
}

.bx-msg--error .bx-msg__body {
  color: var(--bx-c-error-text);
  background: var(--bx-c-error-bg);
  border: 1px solid var(--bx-c-error-border);
}

/* Expected refusals (rate limit, budget, bot switched off, conversation too long): a notice. */
.bx-msg--limit .bx-msg__body {
  color: var(--bx-c-text);
  background: #FDF3DC;
  background: color-mix(in srgb, var(--bx-c-accent) 14%, #FFFFFF);
  border: 1px solid #F0D089;
  border: 1px solid color-mix(in srgb, var(--bx-c-accent) 55%, #FFFFFF);
  border-bottom-left-radius: 4px;
}

/* The way forward under a refusal: "Cuộc trò chuyện mới" or "Để lại thông tin". */
.bx-msg__action {
  min-height: 36px;
  margin-top: 6px;
  padding: 6px 14px;
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--bx-c-primary);
  background: var(--bx-c-surface);
  border: 1px solid var(--bx-c-primary);
  border-radius: 10px;
}

.bx-msg__action:hover {
  background: var(--bx-c-canvas);
}

/* Confirmation of a sent lead (server message and ma_lead). */
.bx-msg--notice .bx-msg__body {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  color: var(--bx-c-text);
  background: var(--bx-c-success-bg);
  border: 1px solid var(--bx-c-success-border);
  border-bottom-left-radius: 4px;
}

.bx-notice__icon {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  color: var(--bx-c-success-text);
  stroke-width: 2.5;
}

.bx-notice {
  min-width: 0;
}

.bx-notice__text,
.bx-notice__code {
  margin: 0;
}

.bx-notice__code {
  margin-top: 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--bx-c-success-text);
  font-variant-numeric: tabular-nums;
}

.bx-msg__body.bx-plain {
  white-space: pre-wrap;
}

.bx-msg__note {
  margin: 4px 2px 0;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--bx-c-muted);
}

.bx-msg__note--error {
  color: var(--bx-c-error-text);
}

/* Markdown answers (sanitised HTML from marked + DOMPurify). */

.bx-md > :first-child {
  margin-top: 0;
}

.bx-md > :last-child {
  margin-bottom: 0;
}

.bx-md p,
.bx-md ul,
.bx-md ol,
.bx-md pre,
.bx-md blockquote,
.bx-md table {
  margin: 0 0 8px;
}

.bx-md ul,
.bx-md ol {
  padding-left: 20px;
}

.bx-md li + li {
  margin-top: 3px;
}

.bx-md li > p {
  margin: 0;
}

.bx-md h1,
.bx-md h2,
.bx-md h3,
.bx-md h4,
.bx-md h5,
.bx-md h6 {
  margin: 12px 0 6px;
  font-size: 15px;
  line-height: 1.35;
}

.bx-md h1 {
  font-size: 17px;
}

.bx-md h2 {
  font-size: 16px;
}

.bx-md a {
  color: var(--bx-c-primary);
  text-decoration: underline;
  text-decoration-color: var(--bx-c-accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
}

.bx-md code {
  padding: 1px 4px;
  font-family: var(--bx-font-mono);
  font-size: 13px;
  background: var(--bx-c-canvas);
  border-radius: 4px;
}

.bx-md pre {
  padding: 8px 10px;
  overflow-x: auto;
  background: var(--bx-c-canvas);
  border-radius: 8px;
}

.bx-md pre code {
  padding: 0;
  background: none;
}

.bx-md blockquote {
  padding-left: 10px;
  color: var(--bx-c-muted);
  border-left: 3px solid var(--bx-c-accent);
}

.bx-md table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  font-size: 13.5px;
  border-collapse: collapse;
}

.bx-md th,
.bx-md td {
  padding: 4px 8px;
  text-align: left;
  vertical-align: top;
  border: 1px solid var(--bx-c-border);
}

.bx-md th {
  background: var(--bx-c-canvas);
}

.bx-md hr {
  margin: 10px 0;
  border: 0;
  border-top: 1px solid var(--bx-c-border);
}

/* Typing indicator while waiting for the first words. */

.bx-typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 22px;
}

.bx-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--bx-c-muted);
  opacity: 0.35;
  animation: bx-typing 1.2s infinite ease-in-out;
}

.bx-typing__dot:nth-child(2) {
  animation-delay: 0.15s;
}

.bx-typing__dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes bx-typing {
  0%,
  80%,
  100% {
    opacity: 0.35;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* Suggested questions (until the first visitor message). */

.bx-suggestions {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  max-width: 92%;
}

.bx-suggestion {
  max-width: 100%;
  padding: 7px 12px;
  font-size: 14px;
  line-height: 1.4;
  text-align: left;
  color: var(--bx-c-text);
  background: var(--bx-c-surface);
  border: 1px solid var(--bx-c-border);
  border-radius: 12px;
}

.bx-suggestion:hover {
  border-color: var(--bx-c-accent);
  background: var(--bx-c-canvas);
  background: color-mix(in srgb, var(--bx-c-accent) 10%, #FFFFFF);
}

/* Rating buttons under each answer. */

.bx-rating {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: 4px;
}

.bx-rating__button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  color: var(--bx-c-muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
}

.bx-rating__button:hover {
  color: var(--bx-c-text);
  background: rgba(7, 10, 15, 0.06);
}

.bx-rating__button[aria-pressed="true"] {
  color: var(--bx-c-primary);
  background: var(--bx-c-canvas);
  background: color-mix(in srgb, var(--bx-c-accent) 24%, #FFFFFF);
}

.bx-rating__button[aria-pressed="true"] .bx-icon {
  fill: var(--bx-c-accent);
}

.bx-rating__button .bx-icon {
  width: 17px;
  height: 17px;
}

.bx-rating__error {
  margin-left: 6px;
  font-size: 12.5px;
  color: var(--bx-c-error-text);
}

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

.bx-composer {
  flex: none;
  padding: 10px 12px;
  background: var(--bx-c-surface);
  border-top: 1px solid var(--bx-c-border);
}

.bx-composer__row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.bx-composer__input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  max-height: 140px;
  padding: 10px 12px;
  /* 16px keeps iOS Safari from zooming the page when the box gets focus. */
  font-size: 16px;
  line-height: 1.4;
  color: var(--bx-c-text);
  background: var(--bx-c-surface);
  border: 1px solid #C9CED6;
  border-radius: 12px;
  resize: none;
  overflow-y: auto;
}

.bx-composer__input::placeholder {
  color: #7A828F;
}

/* The transparent outline becomes visible in forced-colors mode, where box-shadow is dropped. */
.bx-composer__input:focus-visible {
  outline: 3px solid transparent;
  outline-offset: 0;
  border-color: var(--bx-c-primary);
  box-shadow: 0 0 0 3px var(--bx-c-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--bx-c-accent) 55%, transparent);
}

.bx-composer__input[aria-invalid="true"] {
  border-color: #B3261E;
}

.bx-send {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 6px;
  height: 44px;
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--bx-c-primary);
  background: var(--bx-c-accent);
  border: 0;
  border-radius: 12px;
}

.bx-send:hover {
  filter: brightness(0.95);
}

.bx-send:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  filter: none;
}

.bx-send.is-streaming {
  color: #FFFFFF;
  background: var(--bx-c-primary);
}

.bx-send__icon {
  width: 18px;
  height: 18px;
}

.bx-send__icon--stop,
.bx-send.is-streaming .bx-send__icon--send {
  display: none;
}

.bx-send.is-streaming .bx-send__icon--stop {
  display: block;
}

.bx-composer__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.3;
  color: var(--bx-c-muted);
}

.bx-counter {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

.bx-counter.is-over {
  font-weight: 600;
  color: #B3261E;
}

/* ---- Lead form ("Để lại thông tin"), over the conversation ------------------------------ */

.bx-lead {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin: 0;
  background: var(--bx-c-surface);
}

/* Only the fields move in: the form's own background hides the chat from the first frame. */
@keyframes bx-lead-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

.bx-lead__body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 16px 14px 4px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: #C4C9D2 transparent;
  animation: bx-lead-in 160ms ease-out;
}

.bx-lead__title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.3;
}

/* Focused by script on touch screens only (tabindex="-1"): no ring for a pointer tap. */
.bx-lead__title:focus:not(:focus-visible) {
  outline: none;
}

.bx-lead__intro {
  margin: 4px 0 14px;
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--bx-c-muted);
}

/* role="alert": stays in the page while empty so every new message is announced. */
.bx-lead__alert:not(:empty) {
  margin: 0 0 14px;
  padding: 8px 12px;
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--bx-c-error-text);
  background: var(--bx-c-error-bg);
  border: 1px solid var(--bx-c-error-border);
  border-radius: 10px;
}

.bx-field {
  margin: 0 0 14px;
}

.bx-field__label {
  display: block;
  margin-bottom: 5px;
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.35;
}

.bx-field__required {
  color: var(--bx-c-invalid);
}

.bx-field__input {
  display: block;
  width: 100%;
  min-height: 44px;
  margin: 0;
  padding: 10px 12px;
  /* 16px keeps iOS Safari from zooming the page when a field gets focus. */
  font-size: 16px;
  line-height: 1.4;
  color: var(--bx-c-text);
  background: var(--bx-c-surface);
  border: 1px solid var(--bx-c-field-border);
  border-radius: 10px;
}

.bx-field__input--multiline {
  min-height: 88px;
  max-height: 200px;
  resize: vertical;
}

.bx-field__input::placeholder {
  color: #7A828F;
}

.bx-field__input:focus-visible {
  outline: 3px solid transparent;
  outline-offset: 0;
  border-color: var(--bx-c-primary);
  box-shadow: 0 0 0 3px var(--bx-c-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--bx-c-accent) 55%, transparent);
}

.bx-field__input[aria-invalid="true"] {
  border-color: var(--bx-c-invalid);
  box-shadow: inset 0 0 0 1px var(--bx-c-invalid);
}

.bx-field__input[aria-invalid="true"]:focus-visible {
  box-shadow: inset 0 0 0 1px var(--bx-c-invalid), 0 0 0 3px var(--bx-c-accent);
  box-shadow: inset 0 0 0 1px var(--bx-c-invalid), 0 0 0 3px color-mix(in srgb, var(--bx-c-accent) 55%, transparent);
}

/* Sending: the fields are read-only until the answer arrives (still focusable and readable). */
.bx-field__input[readonly] {
  color: var(--bx-c-muted);
  background: var(--bx-c-canvas);
  cursor: progress;
}

.bx-field__hint,
.bx-field__error {
  margin: 5px 0 0;
  font-size: 12.5px;
  line-height: 1.4;
}

.bx-field__hint {
  color: var(--bx-c-muted);
}

.bx-field__error {
  font-weight: 600;
  color: var(--bx-c-invalid);
}

.bx-field__counter {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  line-height: 1.3;
  text-align: right;
  color: var(--bx-c-muted);
  font-variant-numeric: tabular-nums;
}

.bx-lead__actions {
  position: relative; /* above the fields while they slide in */
  z-index: 1;
  display: flex;
  flex: none;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bx-c-surface);
  border-top: 1px solid var(--bx-c-border);
}

.bx-button {
  min-width: 88px;
  min-height: 44px;
  padding: 0 18px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.2;
  border: 1px solid transparent;
  border-radius: 12px;
}

.bx-button--primary {
  color: var(--bx-c-primary);
  background: var(--bx-c-accent);
}

.bx-button--primary:hover {
  filter: brightness(0.95);
}

.bx-button--secondary {
  color: var(--bx-c-text);
  background: var(--bx-c-surface);
  border-color: var(--bx-c-field-border);
}

.bx-button--secondary:hover {
  background: var(--bx-c-canvas);
}

/* Sending: still focusable (aria-disabled), clicks are ignored by the script. */
.bx-button[aria-disabled="true"] {
  cursor: progress;
  opacity: 0.6;
  filter: none;
}

.bx-button--secondary[aria-disabled="true"]:hover {
  background: var(--bx-c-surface);
}

/* ---- Input type, frame size and user preferences ---------------------------------------- */

@media (pointer: coarse) {
  .bx-composer__hint {
    display: none;
  }
}

/* Short frames (phones in landscape): one header row and tighter spacing leave room for messages. */
@media (max-height: 480px) {
  .bx-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 8px 6px 12px;
  }

  .bx-header__top {
    display: contents;
  }

  .bx-toolbar {
    justify-content: flex-end;
    order: 1;
    margin-top: 0;
  }

  .bx-chip-button {
    white-space: nowrap;
  }

  .bx-icon-button {
    order: 2;
  }

  .bx-brand__mark {
    width: 32px;
    height: 32px;
  }

  .bx-privacy {
    padding: 5px 12px;
  }

  .bx-messages {
    padding-top: 10px;
  }

  .bx-composer {
    padding: 6px 10px;
  }

  .bx-composer__hint {
    display: none;
  }

  .bx-composer__meta {
    margin-top: 2px;
  }

  .bx-lead__body {
    padding-top: 10px;
  }

  .bx-lead__intro,
  .bx-field {
    margin-bottom: 10px;
  }

  .bx-lead__actions {
    padding: 6px 10px;
  }
}

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

  .bx-lead__body {
    animation: none;
  }
}

@media (forced-colors: active) {
  .bx-icon-button,
  .bx-chip-button,
  .bx-rating__button,
  .bx-suggestion,
  .bx-send,
  .bx-button,
  .bx-msg__action {
    border: 1px solid ButtonText;
  }

  .bx-rating__button[aria-pressed="true"],
  .bx-chip-button[aria-expanded="true"] {
    outline: 2px solid Highlight;
  }

  .bx-field__input[aria-invalid="true"] {
    border-width: 2px;
  }
}
