/* Layout: left rail (New chat on top, History anchored at the bottom) + wider chat area
   (scrollable list + composer at the bottom). */

.chat-layout {
  display: flex;
  width: 100%;
  /* Full-bleed: rail hugs the left edge, chat extends to the right edge. */
  /* Fill the viewport below the header so messages scroll internally and the
     composer stays pinned; the footer sits below and is reached by scrolling. */
  height: calc(100dvh - var(--header-h));
  min-height: 0;
}

.rail {
  flex: 0 0 236px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  /* No left padding: history sits flush against the left edge. */
  padding: 16px 10px 14px 0;
  border-right: 1px solid var(--border);
}

.rail__new {
  width: calc(100% - 10px);
  margin-left: 10px;
}

.rail__history {
  display: flex;
  flex-direction: column;
  gap: 8px;
  /* Ocupa o resto da coluna: a lista rola dentro dela, sem deixar um vão no meio. */
  flex: 1 1 auto;
  min-height: 0;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.rail__history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 10px;
}

.rail__history-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.conversations__list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  /* Do NOT use flex here: within a bounded height the items would shrink (flex-shrink) and
     overlap. Block flow keeps each item's natural height. */
  display: block;
}

.conversations__list:empty::after {
  content: "Nenhuma conversa ainda.";
  display: block;
  padding: 6px 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.conversation {
  /* flush left; right inset only, to avoid touching the scrollbar. */
  padding: 5px 4px 5px 10px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 4px;
}

.conversation__main {
  flex: 1 1 auto;
  min-width: 0; /* enables ellipsis inside a flex row */
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.conversation__delete {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  padding: 2px 7px;
  border-radius: var(--radius-sm);
  opacity: 0; /* revealed on hover/focus to keep the list clean */
  transition:
    opacity 0.12s ease,
    background 0.12s ease,
    color 0.12s ease;
}

.conversation:hover .conversation__delete,
.conversation__delete:focus-visible {
  opacity: 1;
}

.conversation__delete:hover {
  background: rgba(211, 47, 47, 0.12);
  color: #d32f2f;
}

/* Touch devices have no hover: keep the action discoverable. */
@media (hover: none) {
  .conversation__delete {
    opacity: 0.55;
  }
}

.conversation__title {
  font-size: 0.88rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation__date {
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1.2;
}

.conversation.is-active .conversation__date {
  color: var(--primary);
}

.conversation:hover {
  background: var(--surface-2);
}

.conversation.is-active {
  background: var(--surface-2);
  font-weight: 600;
}

.rail__debug {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--text-muted);
  padding: 8px 4px 0 10px;
  border-top: 1px solid var(--border);
  cursor: pointer;
}

.chat {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0 clamp(8px, 3vw, 28px);
  /* Origem do botão "ir para o fim", que flutua logo acima do composer. */
  position: relative;
}

/* Como a rolagem não persegue mais o texto, este é o caminho de volta — e só existe quando
   há conteúdo abaixo da dobra. */
.to-end {
  position: absolute;
  bottom: 92px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  font-size: 1.05rem;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition:
    color 0.15s ease,
    border-color 0.15s ease;
}

.to-end:hover {
  color: var(--text);
  border-color: var(--primary-300);
}

.to-end[hidden] {
  display: none;
}

.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px 4px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  /* Referência para offsetTop das mensagens: o JS ancora o turno em curso no topo da área de
     leitura, e essa medida precisa estar na mesma origem do scrollTop. */
  position: relative;
}

/* Empty state takes over the whole message area and centres itself. */
.messages--empty {
  justify-content: center;
  align-items: center;
}

.empty {
  max-width: 560px;
  padding: 0 8px 40px;
  text-align: center;
}

.empty__title {
  font-size: var(--fs-title);
  font-weight: 600;
  letter-spacing: -0.005em;
  margin: 0 0 10px;
}

.empty__lead {
  margin: 0 auto 22px;
  max-width: 460px;
  color: var(--text-muted);
  font-size: 0.94rem;
}

.empty__suggestions {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.message {
  display: flex;
}

.message--user {
  justify-content: flex-end;
}

/* Vertical column: bubble + citations + meta stacked (not side by side). */
.message__col {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 86%;
  min-width: 0;
}

/* Assistant answers get more reading width (tables/lists). */
.message--assistant .message__col {
  max-width: 92%;
}

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

.message__bubble {
  max-width: 100%;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message--user .message__bubble {
  background: var(--primary);
  color: var(--on-primary);
  border-bottom-right-radius: 4px;
}

.message--assistant .message__bubble {
  border-bottom-left-radius: 4px;
  white-space: normal;
  line-height: 1.5;
}

/* Markdown rendered inside the assistant bubble. One element per block, so the rhythm comes
   from these margins alone — never from stacked <br>. */
.message__bubble strong {
  font-weight: 700;
}
.message__bubble em {
  font-style: italic;
}
.message__bubble a {
  color: var(--primary);
  text-decoration: underline;
}
.message__bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.message__bubble p {
  margin: 0 0 10px;
}
.message__bubble ul,
.message__bubble ol {
  margin: 0 0 10px;
  padding-left: 20px;
}
.message__bubble li {
  margin: 2px 0;
}
/* Sublista continua o item pai: sem margem extra, só o recuo. */
.message__bubble li > ul,
.message__bubble li > ol {
  margin: 2px 0 0;
}
.message__bubble del {
  opacity: 0.7;
}
.message__bubble h4.md-h {
  font-size: 1.02em;
  font-weight: 700;
  margin: 16px 0 6px;
}

.message__bubble blockquote {
  margin: 0 0 10px;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.message__bubble .md-code {
  margin: 0 0 10px;
  padding: 10px 12px;
  overflow-x: auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.86em;
  line-height: 1.5;
}

/* Dentro do bloco, o <code> não repete a moldura do código inline. */
.message__bubble .md-code code {
  padding: 0;
  background: none;
  border: 0;
  font-size: inherit;
}
.message__bubble > *:first-child {
  margin-top: 0;
}
.message__bubble > *:last-child {
  margin-bottom: 0;
}

/* Markdown (GFM) tables: horizontal scroll on narrow screens + zebra/header. */
.md-table-wrap {
  margin: 0 0 10px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.message__bubble .md-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.88em;
  line-height: 1.4;
}
.message__bubble .md-table th,
.message__bubble .md-table td {
  padding: 7px 11px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.message__bubble .md-table th {
  background: var(--surface-2);
  font-weight: 700;
  white-space: nowrap;
}
.message__bubble .md-table tbody tr:last-child td {
  border-bottom: none;
}
.message__bubble .md-table tbody tr:nth-child(even) {
  background: var(--surface-2);
}

.citation-badge {
  display: inline;
  font-size: 0.82em;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--primary);
  white-space: nowrap;
}

/* O rótulo citado no texto vira link quando o turno trouxe URL para aquela fonte. */
a.citation-badge {
  text-decoration: none;
  transition:
    background 0.15s ease,
    border-color 0.15s ease;
}

a.citation-badge:hover {
  background: var(--surface);
  border-color: var(--primary-300);
}

.message__citations {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.message__citations-label {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-right: 2px;
}

.thinking {
  color: var(--text-muted);
  font-style: italic;
}

.citation {
  font-size: 0.76rem;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  text-decoration: none;
}

.citation:hover {
  border-color: var(--primary-300);
  color: var(--primary);
}

.message__meta {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  font-size: 0.74rem;
  color: var(--text-muted);
}

.confidence {
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.confidence--alta {
  color: #1a7f37;
}
.confidence--media {
  color: var(--accent);
}
.confidence--baixa {
  color: #cf222e;
}

/* Ícone traçado: dimensionado em em, então acompanha a fonte do contexto. */
.icon {
  width: 1em;
  height: 1em;
  display: block;
}

.feedback {
  display: inline-flex;
  gap: 2px;
}

.feedback__panel {
  flex-basis: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.feedback__row,
.feedback__reasons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.feedback__panel[hidden],
.feedback__reasons[hidden],
.feedback__editor[hidden],
.feedback__send[hidden],
.feedback__saved[hidden] {
  display: none;
}

.feedback__label {
  color: var(--text-muted);
}

.feedback__chip {
  font-size: 0.78rem;
  padding: 4px 11px;
}

.feedback__chip[aria-pressed="true"],
.feedback__chip.is-filled {
  color: var(--primary);
  border-color: var(--primary);
  background: var(--surface-2);
}

.feedback__toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-left: 6px;
}

.feedback__chevron {
  display: flex;
  font-size: 0.8em;
  transition: transform 0.15s ease;
}

.feedback__toggle[aria-expanded="true"] .feedback__chevron {
  transform: rotate(180deg);
}

.feedback__editor {
  max-width: 460px;
}

.feedback__text {
  display: block;
  width: 100%;
  padding: 5px 0;
  font: inherit;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  resize: none;
  overflow-y: hidden;
}

.feedback__text:focus {
  border-bottom-color: var(--primary);
  outline: none;
}

.feedback__actions {
  display: flex;
  justify-content: flex-end;
  min-height: 24px;
  margin-top: 4px;
}

.feedback__send {
  font: inherit;
  font-size: 0.78rem;
  color: var(--primary);
  background: none;
  border: none;
  padding: 2px 0;
  cursor: pointer;
}

.feedback__send:hover:not(:disabled) {
  color: var(--primary-600);
}

.feedback__send:disabled {
  color: var(--text-muted);
  cursor: default;
}

.feedback__saved {
  color: var(--text-muted);
}

/* Ação só-ícone da barra de meta: copiar e feedback. */
.icon-btn {
  display: inline-flex;
  padding: 5px;
  font-size: 0.95rem;
  color: var(--text-muted);
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition:
    color 0.15s ease,
    background 0.15s ease;
}

.icon-btn:hover {
  color: var(--text);
  background: var(--surface-2);
}

.icon-btn[aria-pressed="true"] {
  color: var(--primary);
}

/* Preenchimento leve marca a escolha sem depender só da cor. */
.icon-btn[aria-pressed="true"] .icon {
  fill: currentColor;
  fill-opacity: 0.16;
}

.message__note {
  margin-top: 6px;
  font-size: 0.78rem;
  font-style: italic;
  color: var(--text-muted);
}

.typing::after {
  content: "▋";
  animation: blink 1s steps(2) infinite;
  color: var(--primary-300);
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  padding: 14px 0 20px;
  align-items: flex-end;
}

.composer__input {
  flex: 1 1 auto;
  font: inherit;
  resize: none;
  max-height: 160px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.composer__input:focus {
  outline: 2px solid var(--primary-300);
  outline-offset: 1px;
}

/* Top bar of the chat column: mobile drawer toggle (left) + server status (right). */
.chat-topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0 2px;
}

/* Live LLM (GPU) server status; mirrors the RAG indicator. */
.chat-status {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.status-dot {
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9aa3ad;
}

.status-dot.connected {
  background: #28a745;
}

.status-dot.loading {
  background: #f0ad4e;
  animation: status-pulse 1s infinite;
}

.status-dot.warning {
  background: #f0ad4e;
}

.status-dot.error {
  background: #dc3545;
}

@keyframes status-pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
}

/* Mobile drawer toggle + backdrop: hidden on desktop where the rail is inline. */
.rail-toggle {
  display: none;
}

.rail-backdrop {
  display: none;
}

@media (max-width: 760px) {
  /* Rail becomes a slide-in drawer instead of vanishing. */
  .rail {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 50;
    width: 268px;
    max-width: 82vw;
    padding: 16px 12px 14px 12px;
    background: var(--surface);
    box-shadow: var(--shadow);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
  }

  .rail.is-open {
    transform: translateX(0);
  }

  .rail-backdrop.is-open {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(0, 0, 0, 0.42);
  }

  .rail-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
  }
}

@media (max-width: 600px) {
  .message__bubble {
    max-width: 88%;
  }
}
