/* Chat — centered conversation panel, ChatGPT-style.
   Sits below the global top header (h-12) and to the right of the
   sidebar (w-72 on lg). Width is constrained so the page does not
   feel edge-to-edge.
*/
.chat-page {
  min-height: calc(100dvh - var(--header-h));
  height: calc(100dvh - var(--header-h));
  padding: 0;
  background: var(--surface);
  color: var(--fg);
}

.chat-shell {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1152px;
  margin: 0 auto;
  padding: 0 16px;
  min-height: 0;
}

/* Header — back link on the left, portfolio context picker on the right */
.chat-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0 4px;
}
.chat-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 4px 4px;
  border-radius: var(--r-md);
  background: transparent;
  border: none;
  color: var(--fg-2);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.chat-back:hover {
  background: var(--surface-hover);
  color: var(--fg);
}

/* Portfolio context picker */
.chat-portfolio {
  position: relative;
}
.chat-portfolio-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px 7px 11px;
  border-radius: var(--r-md);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--fg);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.chat-portfolio-trigger:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}
.chat-portfolio-trigger:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--fg);
}
.chat-portfolio-trigger svg { color: var(--fg-3); }
.chat-portfolio-eyebrow {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--fg-3);
}
.chat-portfolio-name {
  font-weight: 500;
  color: var(--fg);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-portfolio-caret {
  transition: transform 0.15s ease;
}
.chat-portfolio-caret.is-open {
  transform: rotate(180deg);
}

.chat-portfolio-menu {
  position: absolute;
  z-index: 30;
  top: calc(100% + 6px);
  right: 0;
  min-width: 260px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06), 0 2px 6px rgba(0, 0, 0, 0.04);
}
.chat-portfolio-menu form {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-portfolio-menu-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--fg-3);
  padding: 6px 10px 4px;
}

.chat-portfolio-search {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  margin-bottom: 4px;
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid transparent;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.chat-portfolio-search:focus-within {
  background: var(--surface);
  border-color: var(--border-strong);
}
.chat-portfolio-search > svg {
  flex-shrink: 0;
  color: var(--fg-3);
}
.chat-portfolio-search-input {
  flex: 1 1 auto;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  font-size: 13px;
  color: var(--fg);
  font-family: inherit;
  padding: 0;
}
.chat-portfolio-search-input::placeholder { color: var(--fg-3); }
.chat-portfolio-search-clear {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: var(--r-full);
  background: transparent;
  color: var(--fg-3);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.chat-portfolio-search-clear:hover {
  background: var(--surface-hover);
  color: var(--fg);
}

.chat-portfolio-empty {
  padding: 10px 10px 8px;
  font-size: 12px;
  color: var(--fg-3);
  text-align: center;
}

.chat-portfolio-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--r-md);
  border: none;
  background: transparent;
  text-align: left;
  cursor: pointer;
  color: var(--fg);
  transition: background 0.12s ease;
}
.chat-portfolio-option:hover {
  background: var(--surface-hover);
}
.chat-portfolio-option[aria-selected="true"] {
  background: var(--surface-2);
}
.chat-portfolio-option-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 1px;
}
.chat-portfolio-option-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--fg);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-portfolio-option-sub {
  font-size: 11px;
  color: var(--fg-3);
  line-height: 1.3;
}
.chat-portfolio-check {
  flex-shrink: 0;
  color: #10b981;
}

/* Message list */
.chat-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 24px 0 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: transparent;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: var(--r-full);
}

/* Empty state — centered hero */
.chat-empty {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  padding: 32px 16px 48px;
}
.chat-empty-spark {
  font-size: 32px;
  line-height: 1;
  color: #10b981;
  margin-bottom: 4px;
}
.chat-empty-title {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
  color: var(--fg);
  text-wrap: balance;
  max-width: 420px;
}
.chat-empty-sub {
  font-size: 14px;
  color: var(--fg-2);
  max-width: 360px;
  line-height: 1.5;
}

.chat-separator {
  align-self: center;
  color: var(--fg-3);
  font-size: 11px;
  margin: 8px 0;
  letter-spacing: 0.02em;
}

/* Bubbles */
.chat-bubble {
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.chat-bubble--assistant {
  align-self: stretch;
  background: transparent;
  color: var(--fg);
  padding: 0;
  max-width: 100%;
}
.chat-bubble--user {
  align-self: flex-end;
  max-width: 80%;
  color: var(--fg);
  background: var(--surface-2);
  padding: 10px 16px;
  border-radius: 18px;
  font-size: 14px;
}

/* Pending bubble */
.chat-bubble.chat-pending.chat-bubble--user { opacity: 0.85; }
.chat-bubble.chat-thinking {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 0;
  min-height: 24px;
}
.chat-thinking span {
  width: 6px;
  height: 6px;
  border-radius: var(--r-full);
  background: var(--fg-3);
  display: inline-block;
  animation: chat-thinking-bounce 1.2s infinite ease-in-out;
}
.chat-thinking span:nth-child(2) { animation-delay: 0.15s; }
.chat-thinking span:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-thinking-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.35; }
  30%           { transform: translateY(-4px); opacity: 1; }
}

/* Load-older sentinel — sits at the top of the message list and shows a small
   loader while the previous page is fetched on scroll-up. */
.chat-load-older {
  align-self: center;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 16px;
  padding: 2px 0;
}
.chat-load-older-dots span {
  width: 5px;
  height: 5px;
  border-radius: var(--r-full);
  background: var(--fg-3);
  display: inline-block;
  margin: 0 2px;
  animation: chat-thinking-bounce 1.2s infinite ease-in-out;
}
.chat-load-older-dots span:nth-child(2) { animation-delay: 0.15s; }
.chat-load-older-dots span:nth-child(3) { animation-delay: 0.3s; }

/* References strip */
.chat-refs-label {
  align-self: flex-start;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--fg-3);
  margin-top: 6px;
  margin-bottom: 2px;
}
.chat-refs {
  align-self: flex-start;
  flex-shrink: 0;
  max-width: 100%;
  display: flex;
  gap: 8px;
  padding: 4px 0 6px;
  margin-top: -2px;
  overflow-x: auto;
  scrollbar-width: thin;
  scroll-snap-type: x proximity;
}
.chat-refs::-webkit-scrollbar { height: 6px; }
.chat-refs::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--r-full); }

.chat-ref {
  flex: 0 0 168px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-decoration: none;
  color: var(--fg);
  scroll-snap-align: start;
  border-radius: var(--r-md);
  padding: 4px;
  transition: background 0.15s ease;
}
.chat-ref:hover { background: var(--surface-hover); }

.chat-ref__thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  flex-shrink: 0;
  border-radius: var(--r-md);
  overflow: hidden;
  background: var(--surface-2);
}
.chat-ref__thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.chat-ref__play {
  position: absolute;
  inset: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--fg-inverse);
  background: rgba(0, 0, 0, 0.15);
  opacity: 0;
  transition: opacity 0.15s ease;
}
.chat-ref:hover .chat-ref__play { opacity: 1; }
.chat-ref__play svg {
  background: rgba(0, 0, 0, 0.6);
  padding: 6px;
  width: 28px;
  height: 28px;
  border-radius: var(--r-full);
}
.chat-ref__title {
  font-size: 12px;
  line-height: 1.3;
  color: var(--fg-2);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ─── Composer region ─────────────────────────────────────── */
.chat-composer-region {
  flex-shrink: 0;
  padding: 8px 0 14px;
  background: var(--surface);
}

/* Suggestion pills */
.chat-suggestions {
  display: flex;
  gap: 8px;
  padding: 0 0 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chat-suggestions::-webkit-scrollbar { display: none; }
.chat-suggestions form { margin: 0; }
.chat-suggestion-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--r-full);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg-2);
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.chat-suggestion-pill:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
  color: var(--fg);
}
.chat-suggestion-spark {
  color: #10b981;
  font-size: 12px;
}

/* Composer card */
.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 26px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.chat-composer:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}
.chat-composer textarea {
  flex: 1 1 auto;
  resize: none;
  border: none;
  padding: 12px 0;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.45;
  max-height: 200px;
  min-height: 24px;
  outline: none;
  background: transparent;
  color: var(--fg);
}
.chat-composer textarea::placeholder { color: var(--fg-3); }

.chat-send {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: var(--r-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--primary-fg);
  background: var(--primary);
  border: none;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.08s ease, opacity 0.15s ease;
}
.chat-send:hover { background: var(--primary-hover); }
.chat-send:active { transform: scale(0.95); }
.chat-send:disabled { opacity: 0.35; cursor: not-allowed; }

.chat-footnote {
  text-align: center;
  font-size: 11px;
  color: var(--fg-3);
  margin-top: 8px;
  letter-spacing: 0.01em;
}
