:host {
  display: block;
}

.biel-message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
}

.biel-message--user {
  align-items: flex-end;
}

.biel-message__label {
  font-size: var(--biel-message-label-font-size);
  color: var(--biel-text-color);
  margin-bottom: 6px;
  font-weight: 500;
}

.biel-message__bubble {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--biel-message-border-radius);
  font-size: var(--biel-message-font-size);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.biel-message--user .biel-message__bubble {
  background-color: var(--biel-message-user-bg-color);
  color: var(--biel-message-user-text-color);
}

.biel-message--ai .biel-message__bubble {
  background-color: var(--biel-message-biel-bg-color);
  color: var(--biel-message-biel-text-color);
}

.biel-message--no-avatar {
  max-width: 100%;
}

.biel-message--no-avatar .biel-message__content {
  max-width: 100%;
}

.biel-message__content * {
  margin: 0;
}

/* Fade only the arriving text; words already on screen keep their opacity. */
.biel-stream-fragment {
  animation: biel-text-reveal 200ms ease-out both;
}

@keyframes biel-text-reveal {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .biel-stream-fragment {
    animation: none;
  }
}

.biel-message__content hr {
  display: none;
}

.biel-message__content ul,
.biel-message__content ol {
  padding-inline-start: 0;
  margin-left: 20px;
}

.biel-message__content > div > ul,
.biel-message__content > div > ol {
  padding: 0;
}

.biel-message__content > div:not(:first-child),
.biel-message__content li,
.biel-message__content p:not(:first-child),
.biel-message__content h1,
.biel-message__content h2,
.biel-message__content h3,
.biel-message__content h4,
.biel-message__content h5,
.biel-message__content h6 {
  margin-top: 10px;
}
.biel-message__content h1 {
  font-size: var(--biel-message-h1-font-size);
}

.biel-message__content h2 {
  font-size: var(--biel-message-h2-font-size);
}

.biel-message__content h3 {
  font-size: var(--biel-message-h3-font-size);
}

.biel-message__content h4 {
  font-size: var(--biel-message-h4-font-size);
}

.biel-message__content h5 {
  font-size: var(--biel-message-h5-font-size);
}

.biel-message__content h6 {
  font-size: var(--biel-message-h6-font-size);
}

.biel-message__content pre {
  margin: 10px 0;
}

.biel-message__content a {
  color: var(--biel-message-link-color);
  text-decoration: none;
}

@media (hover: hover) {
  .biel-message__content a:hover {
    text-decoration: underline;
  }
}

/* Inline action chip: an <action name="x"/> tag the LLM placed in the answer,
   resolved to a button-styled link rendered in place. */
.biel-message__content a.biel-inline-action {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: var(--biel-radius-sm);
  background: var(--biel-action-bg-color, var(--biel-message-source-bg-color));
  color: var(--biel-action-text-color, var(--biel-message-source-text-color));
  text-decoration: none;
  font-size: var(--biel-action-font-size, var(--biel-message-source-font-size));
  line-height: normal;
  vertical-align: middle;
}

.biel-message__content a.biel-inline-action::after {
  content: '→';
  font-size: 0.9em;
}

@media (hover: hover) {
  .biel-message__content a.biel-inline-action:hover {
    background: var(--biel-action-bg-hover-color, var(--biel-message-source-bg-hover-color));
    color: var(--biel-action-text-hover-color, var(--biel-message-source-text-color));
    text-decoration: none;
  }
}

/* Inline citation chip: a numeric-text markdown link like [1](url) the backend
   streamed as a citation, upgraded to a compact scholarly superscript chip.
   Themed via --biel-citation-* props that fall back to the source palette, so
   it tracks both light and dark widget themes out of the box (mirroring the
   .biel-inline-action fallback pattern). */
.biel-message__content a.biel-inline-citation {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25em;
  height: 1.25em;
  padding: 0.2em;
  margin: 0 0.1em;
  border-radius: 4px;
  background: var(--biel-citation-bg-color, var(--biel-message-source-bg-color));
  color: var(--biel-citation-text-color, var(--biel-message-source-text-color));
  font-size: var(--biel-citation-font-size, 0.75em);
  font-weight: 600;
  text-decoration: none;
  vertical-align: baseline;
  position: relative;
  top: -0.1em;
}

.biel-message__content a.biel-inline-citation sup {
  vertical-align: baseline;
  font-size: inherit;
  line-height: inherit;
  top: 0;
}

@media (hover: hover) {
  .biel-message__content a.biel-inline-citation:hover {
    background: var(--biel-citation-bg-hover-color, var(--biel-message-source-bg-hover-color));
    color: var(--biel-citation-text-hover-color, var(--biel-message-source-text-color));
    text-decoration: none;
  }
}

/* `.biel-message__content *` above resets margins on every descendant. A rule
   from this document targeting the host outranks the component's own :host
   rule, so anything that needs block spacing has to ask for it back here. */
.biel-message__content biel-math[display='true'] {
  display: block;
  margin: 10px 0;
}

.biel-message__content biel-diagram {
  display: block;
  margin: 10px 0;
}

.biel-message__avatar {
  display: none;
}

.biel-message__content {
  width: 100%;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
}

.biel-table-container {
  margin: 10px 0;
  overflow-x: auto;
  background-color: var(--biel-table-bg-color);
  border-radius: var(--biel-radius-sm);
}

.biel-table-container table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  color: var(--biel-table-content-text-color);
}

.biel-table-container th,
.biel-table-container td {
  padding: 8px;
  border: 1px solid #303030;
}

.biel-table-container th {
  background-color: var(--biel-table-th-bg-color);
}

.biel-table-container::-webkit-scrollbar {
  height: 8px;
}

.biel-table-container::-webkit-scrollbar-thumb:horizontal {
  background-color: var(--biel-chat-scrollbar-color);
  border-radius: 10px;
  border: 3px solid transparent;
}

.biel-table-container::-webkit-scrollbar-thumb:hover {
  background-color: var(--biel-chat-scrollbar-hover-color);
}

.biel-message__content code {
  background-color: var(--biel-message-code-inline-bg-color);
  padding: 2px 4px;
  border-radius: var(--biel-message-code-inline-border-radius);
  font-size: var(--biel-message-code-font-size);
  color: var(--biel-message-code-inline-text-color);
}

.biel-message__content pre code {
  color: initial;
  background-color: var(--biel-message-code-bg-color);
}

/* --- Answer footer -------------------------------------------------------- */
/* One compact row at the end of a completed AI answer: sources toggle on the
   left, icon actions (copy + thumbs) on the right. Spacing alone separates it
   from the answer — no rule, so the answer and its footer read as one block. */
.biel-message__footer {
  margin-top: 12px;
}

.biel-message__footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: nowrap;
}

.biel-message__footer-left {
  min-width: 0;
  display: flex;
  align-items: center;
}

.biel-message__footer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.biel-message__sources-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 100%;
  background-color: var(--biel-source-toggle-bg);
  color: var(--biel-message-source-text-color);
  border: 1px solid var(--biel-source-toggle-border);
  padding: 4px 6px;
  border-radius: var(--biel-radius-sm);
  cursor: pointer;
  font-size: var(--biel-input-options-font-size, 12px);
  font-family: var(--biel-font-family);
  line-height: 1.5;
  white-space: nowrap;
  transition: all 0.2s ease;
}

/* The answer bubble already uses the input-option hover tint, so use the
   stronger menu hover shade to keep the Sources action visibly interactive. */
@media (hover: hover) {
  .biel-message__sources-toggle:hover {
    background-color: var(--biel-dropdown-selected-hover-bg-color);
    border-color: var(--biel-source-toggle-border-hover-color);
  }
}

.biel-message__sources-toggle[aria-expanded='true'] {
  border-color: var(--biel-source-toggle-border-hover-color);
}

.biel-message__sources-toggle:focus-visible {
  outline: none;
  border-color: var(--biel-input-options-button-focus-border-color);
}

.biel-message__chevron {
  flex-shrink: 0;
  transition: transform 0.2s ease;
}

.biel-message__chevron--up {
  transform: rotate(180deg);
}

/* Ghost icon button: transparent at rest, ~36px hit area around a small icon
   whose visual size is unchanged. */
.biel-message__action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: var(--biel-radius-sm);
  color: var(--biel-footer-action-color);
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    color 0.15s ease,
    opacity 0.15s ease,
    transform 0.15s ease;
}

@media (hover: hover) {
  .biel-message__action:hover {
    background-color: var(--biel-footer-action-hover-bg);
    color: var(--biel-message-source-text-color);
  }
}

/* Paired with :hover so the tint survives the pointer resting on the button
   after the click. */
.biel-message__action--copied,
.biel-message__action--copied:hover {
  color: var(--biel-footer-action-copied-color);
}

.biel-message__thumb--selected,
.biel-message__thumb--selected:hover {
  background-color: var(--biel-footer-thumb-selected-bg);
  color: var(--biel-footer-thumb-selected-color);
  animation: biel-thumb-pop 0.15s ease;
}

.biel-message__thumb--muted {
  opacity: 0.35;
}

@keyframes biel-thumb-pop {
  0% {
    transform: scale(1.15);
  }

  100% {
    transform: scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .biel-message__thumb--selected,
  .biel-message__thumb--selected:hover {
    animation: none;
  }

  .biel-message__chevron {
    transition: none;
  }
}

/* Expanded sources: document-icon cards. 2-up when the panel is wide enough,
   1-up when narrow (driven by the panel's own width, not the viewport). */
.biel-message__sources-panel {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.biel-message__source-card {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  background-color: var(--biel-source-card-bg);
  border: 1px solid var(--biel-source-card-border);
  border-radius: var(--biel-radius-sm);
  text-decoration: none;
  color: var(--biel-source-card-title-color);
  overflow: hidden;
  transition: all 0.2s ease;
}

/* Match search results: tint the surface and highlight the border on hover. */
@media (hover: hover) {
  .biel-message__source-card:hover {
    background-color: var(--biel-input-options-button-hover-bg-color);
    border-color: var(--biel-source-card-border-hover-color);
  }
}

.biel-message__source-card:focus-visible {
  outline: none;
  border-color: var(--biel-source-card-border-hover-color);
}

.biel-message__source-icon {
  flex-shrink: 0;
  margin-top: 2px;
  color: var(--biel-source-card-icon-color);
  opacity: 0.6;
}

.biel-message__source-badge {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  min-width: 1.75em;
  height: 1.75em;
  margin-top: 2px;
  padding: 0 0.25em;
  border-radius: 4px;
  background: var(--biel-citation-bg-color, var(--biel-message-source-bg-color));
  color: var(--biel-citation-text-color, var(--biel-message-source-text-color));
  font-size: var(--biel-text-sm-font-size);
  font-weight: 600;
  line-height: 1;
}

.biel-message__source-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.biel-message__source-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: var(--biel-text-font-size);
  line-height: 1.35;
}

.biel-message__source-host {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--biel-text-sm-font-size);
  color: var(--biel-source-card-host-color);
}
