/* ==========================================================================
   Niwa Chatbot – Message Bubbles
   ---------------------------------------------------------------------------
   Bot messages: left-aligned with avatar.
   User messages: right-aligned, primary color.
   Grouped messages share spacing; only first in group shows avatar.

   BEM CLASSES:
     .niwa-messages              — scrollable message list container
     .niwa-messages__date-sep    — date separator ("Today")
     .niwa-message               — single message row
     .niwa-message--bot          — bot variant (left)
     .niwa-message--user         — user variant (right)
     .niwa-message--continuation — grouped, no avatar
     .niwa-message__avatar       — avatar circle
     .niwa-message__bubble       — the text container
     .niwa-message__text         — paragraph inside bubble
     .niwa-message__time         — timestamp label
     .niwa-message__link         — inline link styling

   TOKENS: --niwa-color-bubble-*, --niwa-radius-xl, --niwa-space-*
   ========================================================================== */

/* ── Welcome Section (logo + greeting inside body) ─────────────────────── */
.niwa-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--niwa-space-xl) var(--niwa-space-lg) var(--niwa-space-lg);
}

.niwa-welcome__icon {
  width: 64px;
  height: 64px;
  margin-bottom: var(--niwa-space-md);
}

.niwa-welcome__icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.niwa-welcome__text {
  font-size: var(--niwa-font-size-xl);
  font-weight: var(--niwa-font-weight-semibold);
  color: var(--niwa-color-text-primary);
  text-align: center;
}

/* ── Message List Container ─────────────────────────────────────────────── */
.niwa-messages {
  display: flex;
  flex-direction: column;
  padding: var(--niwa-space-lg);
  gap: var(--niwa-space-xs);
}

/* ── Date Separator ─────────────────────────────────────────────────────── */
.niwa-widget .niwa-messages__date-sep,
.niwa-messages__date-sep {
  display: flex;
  align-items: center;
  gap: var(--niwa-space-md);
  margin: var(--niwa-space-lg) 0;
  padding: 0 var(--niwa-space-lg);
  color: var(--niwa-color-text-tertiary);
  font-size: var(--niwa-font-size-xs);
  font-weight: var(--niwa-font-weight-medium);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.niwa-messages__date-sep::before,
.niwa-messages__date-sep::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--niwa-color-border-subtle);
}

/* ── Single Message Row ─────────────────────────────────────────────────── */
/* .niwa-widget prefix for specificity — the scoped reset (.niwa-widget *)
   zeroes margin/padding, so we need at least 0-2-0 to win. */
.niwa-widget .niwa-message,
.niwa-message {
  display: flex;
  align-items: flex-end;
  gap: var(--niwa-space-sm);
  max-width: var(--niwa-message-row-max-width, 85%);
  /* Horizontal margin — messages are direct children of the body container
     which only has vertical padding (no .niwa-messages wrapper in WP). */
  margin-left: var(--niwa-space-lg);
  margin-right: var(--niwa-space-lg);
}

/* ── Bot Message (left) ─────────────────────────────────────────────────── */
.niwa-widget .niwa-message--bot,
.niwa-message--bot {
  align-self: flex-start;
  max-width: var(--niwa-message-bot-max-width, var(--niwa-message-row-max-width, 85%));
}

/* ── User Message (right) ───────────────────────────────────────────────── */
.niwa-widget .niwa-message--user,
.niwa-message--user {
  align-self: flex-end;
  flex-direction: row-reverse;
  max-width: var(--niwa-message-user-max-width, var(--niwa-message-row-max-width, 85%));
}

/* ── Continuation (grouped, tighter spacing) ────────────────────────────── */
.niwa-widget .niwa-message--continuation,
.niwa-message--continuation {
  margin-top: calc(var(--niwa-space-xs) * -1 + 2px);
}

/* ── Avatar ─────────────────────────────────────────────────────────────── */
.niwa-message__avatar {
  width: 28px;
  height: 28px;
  border-radius: var(--niwa-radius-full);
  background: var(--niwa-color-primary-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
}

.niwa-message__avatar svg,
.niwa-message__avatar img {
  width: 28px;
  height: 28px;
  border-radius: var(--niwa-radius-full);
}

/* Hide avatar on continuation messages — keep space for alignment */
.niwa-message--continuation .niwa-message__avatar {
  visibility: hidden;
}

/* No avatar on user messages */
.niwa-message--user .niwa-message__avatar {
  display: none;
}

.niwa-message__content {
  min-width: 0;
  max-width: 100%;
}

/* ── Bubble ─────────────────────────────────────────────────────────────── */
.niwa-message__bubble {
  padding: var(--niwa-space-md) var(--niwa-space-lg);
  border-radius: var(--niwa-radius-xl);
  line-height: var(--niwa-line-height-normal);
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Bot bubble */
.niwa-message--bot .niwa-message__bubble {
  background: var(--niwa-color-bubble-bot);
  color: var(--niwa-color-bubble-bot-text);
  border-bottom-left-radius: var(--niwa-radius-sm);
}

/* Light theme: subtle shadow so bot bubbles stand out on white backgrounds */
.niwa-widget[data-niwa-theme="light"] .niwa-message--bot .niwa-message__bubble {
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08), 0 0 1px rgba(0, 0, 0, 0.05);
}

/* User bubble */
.niwa-message--user .niwa-message__bubble {
  background: var(--niwa-color-bubble-user);
  color: var(--niwa-color-bubble-user-text);
  border-bottom-right-radius: var(--niwa-radius-sm);
}

/* Continuation: restore full radius on top */
.niwa-message--bot.niwa-message--continuation .niwa-message__bubble {
  border-top-left-radius: var(--niwa-radius-sm);
  border-bottom-left-radius: var(--niwa-radius-sm);
}

.niwa-message--user.niwa-message--continuation .niwa-message__bubble {
  border-top-right-radius: var(--niwa-radius-sm);
  border-bottom-right-radius: var(--niwa-radius-sm);
}

/* ── Message Text ───────────────────────────────────────────────────────── */
.niwa-widget .niwa-message__text {
  font-size: var(--niwa-font-size-md);
  /* Override WordPress theme p/div margins that leak into bubbles */
  margin: 0 !important;
  padding: 0 !important;
  line-height: var(--niwa-line-height-normal);
}

/* Ensure no extra space in bubbles from WordPress theme styles */
.niwa-widget .niwa-message__bubble {
  overflow: hidden;
}
.niwa-widget .niwa-message__bubble p,
.niwa-widget .niwa-message__bubble div,
.niwa-widget .niwa-message__bubble span {
  margin: 0 !important;
  padding: 0 !important;
}

.niwa-widget .niwa-message__bubble code,
.niwa-message__bubble code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.92em;
  color: inherit;
  background: rgba(17, 24, 39, 0.08);
  border: 1px solid rgba(17, 24, 39, 0.12);
  border-radius: 6px;
  padding: 0.1em 0.42em;
  white-space: break-spaces;
  overflow-wrap: anywhere;
  word-break: break-word;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.niwa-widget[data-niwa-theme="dark"] .niwa-message__bubble code,
[data-niwa-theme="dark"] .niwa-message__bubble code {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.12);
}

.niwa-widget .niwa-message__bubble pre,
.niwa-message__bubble pre {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  margin: var(--niwa-space-sm) 0 0 !important;
  padding: 12px 14px !important;
  border-radius: 12px;
  background: rgba(17, 24, 39, 0.06);
  border: 1px solid rgba(17, 24, 39, 0.1);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  box-sizing: border-box;
  tab-size: 2;
}

.niwa-widget[data-niwa-theme="dark"] .niwa-message__bubble pre,
[data-niwa-theme="dark"] .niwa-message__bubble pre {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.1);
}

.niwa-widget .niwa-message__bubble pre code,
.niwa-message__bubble pre code {
  display: block;
  padding: 0;
  margin: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  white-space: inherit;
  overflow-wrap: inherit;
  word-break: inherit;
}

/* ── Inline Link ────────────────────────────────────────────────────────── */
.niwa-widget .niwa-message--bot .niwa-message__bubble strong,
.niwa-widget .niwa-message--bot .niwa-message__bubble b {
  font-weight: var(--niwa-font-weight-semibold);
}

.niwa-widget[data-niwa-theme="light"] .niwa-message--bot .niwa-message__bubble strong,
.niwa-widget[data-niwa-theme="light"] .niwa-message--bot .niwa-message__bubble b {
  color: var(--niwa-color-bubble-bot-text);
}

.niwa-widget[data-niwa-theme="dark"] .niwa-message--bot .niwa-message__bubble strong,
.niwa-widget[data-niwa-theme="dark"] .niwa-message--bot .niwa-message__bubble b {
  color: #ffffff;
}

.niwa-message__link {
  color: var(--niwa-color-text-link);
  text-decoration: underline;
  text-underline-offset: 2px;
  transition: opacity var(--niwa-transition-fast);
}

.niwa-message__link:hover {
  opacity: 0.8;
}

.niwa-message--user .niwa-message__link {
  color: var(--niwa-color-bubble-user-text);
  opacity: 0.9;
}

/* ── Timestamp ──────────────────────────────────────────────────────────── */
.niwa-message__time {
  font-size: var(--niwa-font-size-xs);
  color: var(--niwa-color-text-tertiary);
  margin-top: var(--niwa-space-xs);
  padding: 0 var(--niwa-space-xs);
}

.niwa-message--user .niwa-message__time {
  text-align: right;
}
