/* ============================================================
   Conversational Analytics — page-scoped styles (.ca-*)
   Uses design tokens only. Both dark (default) and light themes
   work automatically via the :root / [data-theme="light"] tokens
   defined in vercel/index.html.

   Layout overview:
     .ca-page         — full-height flex row (rail + main)
     .ca-rail         — fixed-width left sidebar (conversation list)
     .ca-main         — flex column (transcript + cost + input bar)
     .ca-transcript   — scrollable message area
     .ca-input-bar    — pinned bottom, textarea + send button
   ============================================================ */

/* ── Page shell ── */
.ca-page {
  display: flex;
  height: calc(100vh - 56px); /* subtract filter-bar height */
  overflow: hidden;
  background: var(--bg-base);
}

/* ── Left rail ── */
.ca-rail {
  width: 240px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-inset);
  border-right: 1px solid var(--border-default);
  overflow: hidden;
}

.ca-new-btn {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: var(--space-4);
  padding: var(--space-2) var(--space-3);
  background: var(--color-action);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: var(--font-body);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
  border: none;
}

.ca-new-btn:hover {
  background: var(--color-action-hover);
}

.ca-rail__list {
  flex: 1;
  overflow-y: auto;
  padding: 0 var(--space-2) var(--space-4);
}

.ca-rail__empty {
  font-size: var(--font-small);
  color: var(--text-tertiary);
  padding: var(--space-4);
  text-align: center;
}

.ca-rail__empty--err {
  color: #EF4444;
}

.ca-convo-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--font-small);
  font-family: var(--font-sans);
  line-height: 1.4;
  transition: background 0.12s ease, color 0.12s ease;
  margin-bottom: 2px;
}

.ca-convo-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.ca-convo-item--active {
  background: var(--color-action-ghost);
  color: var(--color-action);
  font-weight: 600;
}

.ca-convo-item__title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Main column ── */
.ca-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── Transcript ── */
.ca-transcript {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  scroll-behavior: smooth;
}

/* ── Empty state ── */
.ca-empty-state {
  margin: auto;
  text-align: center;
  max-width: 480px;
  padding: var(--space-12) var(--space-6);
}

.ca-empty-state__icon {
  color: var(--text-tertiary);
  margin-bottom: var(--space-4);
}

.ca-empty-state__heading {
  font-size: var(--font-h2);
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.ca-empty-state__sub {
  font-size: var(--font-body);
  color: var(--text-secondary);
  line-height: 1.6;
}

/* ── State messages ── */
.ca-state-msg {
  font-size: var(--font-body);
  color: var(--text-secondary);
  padding: var(--space-4) 0;
}

.ca-state-msg--err {
  color: #EF4444;
}

/* ── Bubbles ── */
.ca-bubble {
  display: flex;
}

.ca-bubble--user {
  justify-content: flex-end;
}

.ca-bubble--assistant {
  justify-content: flex-start;
}

.ca-bubble__content {
  max-width: 84%;
  border-radius: var(--radius-md);
  padding: var(--space-4);
  font-size: var(--font-body);
  line-height: 1.6;
}

.ca-bubble__content--user {
  background: var(--color-action);
  color: #fff;
  border-bottom-right-radius: var(--radius-xs);
  white-space: pre-wrap;
  word-break: break-word;
}

.ca-bubble__content--assistant {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  color: var(--text-primary);
  border-bottom-left-radius: var(--radius-xs);
  box-shadow: var(--shadow-sm);
  width: 100%;
  max-width: 100%;
}

/* ── Typing indicator ── */
.ca-typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  height: 20px;
}

.ca-typing span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-tertiary);
  animation: ca-bounce 1.2s ease-in-out infinite;
}

.ca-typing span:nth-child(2) { animation-delay: 0.2s; }
.ca-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes ca-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%            { transform: translateY(-5px); }
}

/* ── Section labels within assistant bubble ── */
.ca-section-label {
  font-size: var(--font-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  margin-bottom: var(--space-2);
}

.ca-section-label--mt {
  margin-top: var(--space-4);
}

/* ── Clarify text ── */
.ca-clarify-text {
  color: var(--text-primary);
  line-height: 1.6;
}

/* ── Error banner (inside assistant bubble) ── */
.ca-error-banner {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: rgba(239, 68, 68, 0.10);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: var(--radius-sm);
  color: #EF4444;
  font-size: var(--font-body);
}

/* ── Pivot table ── */
.ca-pivot-wrap {
  overflow-x: auto;
  margin-bottom: var(--space-1);
}

.ca-pivot {
  border-collapse: collapse;
  font-size: var(--font-small);
  width: 100%;
  font-variant-numeric: tabular-nums;
}

.ca-pivot th,
.ca-pivot td {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-subtle);
  white-space: nowrap;
}

.ca-pivot thead tr {
  background: var(--bg-elevated);
}

.ca-pivot__row-header,
.ca-pivot__col-header {
  font-weight: 600;
  color: var(--text-secondary);
  text-align: left;
}

.ca-pivot__col-header {
  text-align: right;
}

.ca-pivot__row-key {
  color: var(--text-primary);
  font-weight: 500;
}

.ca-pivot__cell {
  text-align: right;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--font-xs);
}

/* ── Insight card ── */
.ca-insight {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: var(--space-4);
  font-size: var(--font-body);
  color: var(--text-primary);
  line-height: 1.7;
  max-width: 72ch;
}

.ca-insight p + p { margin-top: var(--space-2); }
.ca-insight ul,
.ca-insight ol    { padding-left: var(--space-5); margin-top: var(--space-2); }
.ca-insight li    { margin-bottom: var(--space-1); }
.ca-insight strong { font-weight: 700; }

/* ── Freshness line ── */
.ca-freshness {
  margin-top: var(--space-3);
  font-size: var(--font-xs);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
}

/* ── Send error (above cost footer) ── */
.ca-send-error {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-6);
  font-size: var(--font-small);
  color: #EF4444;
  background: rgba(239, 68, 68, 0.06);
  border-top: 1px solid rgba(239, 68, 68, 0.12);
}

/* ── Cost footer ── */
.ca-cost-footer {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-6);
  font-size: var(--font-xs);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-inset);
}

.ca-cost-sep {
  color: var(--border-strong);
}

/* ── Scope bar (B&S only) ──
   Compact CD + Group Brand dropdowns above the input. Sits between the
   transcript and the input bar; narrower padding than the bar below so the
   visual weight stays on the chat input. */
.ca-scope-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-6);
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  font-size: var(--font-small);
}

.ca-scope-bar__label {
  color: var(--text-tertiary);
  font-size: var(--font-xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-right: var(--space-1);
  white-space: nowrap;
}

.ca-scope-bar .searchable-select { min-width: 140px; }
.ca-scope-bar .searchable-select--wide { min-width: 200px; }

/* The scope bar sits at the bottom of the page just above the chat input.
   The default popover opens DOWN (top: calc(100% + 6px)) which gets clipped
   by the input bar / viewport edge. Flip it to open UPWARD inside this
   container so the full option list is always visible. */
.ca-scope-bar .searchable-select__popover {
  top: auto;
  bottom: calc(100% + 6px);
  max-height: 320px;
  overflow-y: auto;
}

.ca-scope-bar__clear {
  margin-left: auto;
  padding: 4px 10px;
  font-size: var(--font-xs);
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.ca-scope-bar__clear:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* ── Input bar ── */
.ca-input-bar {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6);
  border-top: 1px solid var(--border-default);
  background: var(--bg-surface);
}

/* Hide the global floating feedback FAB on Conversational Analytics pages —
   the chat box is the conversation surface; a floating FAB overlaps the
   send button and adds visual noise. The same `Send feedback` affordance
   lives at the bottom of the left rail (.ca-rail__feedback-btn) so users
   still have one-click access to the feedback modal. */
body[data-page="bs-conversational"] .feedback-fab,
body[data-page="conversational-analytics"] .feedback-fab {
  display: none !important;
}

/* Bottom-pinned feedback button inside the left rail. Sits under the
   conversation list. Same visual weight as `.ca-new-btn` but ghost style
   to keep "New conversation" as the primary action. */
.ca-rail__feedback-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin: var(--space-3);
  margin-top: auto;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-default);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--font-small);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.ca-rail__feedback-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.ca-rail__feedback-btn svg {
  flex-shrink: 0;
}

.ca-input {
  flex: 1;
  min-height: 44px;
  max-height: 160px;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-default);
  background: var(--bg-inset);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--font-body);
  line-height: 1.5;
  resize: none;
  overflow-y: auto;
  transition: border-color 0.15s ease;
  field-sizing: content; /* progressive enhancement: auto-grow */
}

.ca-input:focus {
  outline: none;
  border-color: var(--color-action);
}

.ca-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.ca-input::placeholder {
  color: var(--text-tertiary);
}

.ca-send-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  background: var(--color-action);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.ca-send-btn:hover:not(:disabled) {
  background: var(--color-action-hover);
}

.ca-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── SVG Chart components ── */
.ca-chart-wrap {
  position: relative;
  width: 100%;
  margin: var(--space-1) 0;
}

.ca-chart-wrap--donut {
  display: flex;
  align-items: flex-start;
  gap: var(--space-5);
  flex-wrap: wrap;
}

.ca-chart-grid-line {
  stroke: var(--border-subtle);
  stroke-width: 1;
}

.ca-chart-baseline {
  stroke: var(--border-default);
  stroke-width: 1;
}

.ca-chart-axis-label {
  font-family: var(--font-mono);
  font-size: 10px;
  fill: var(--text-tertiary);
}

.ca-chart-bar {
  cursor: pointer;
  transition: opacity 0.12s ease;
}

.ca-chart-bar:hover {
  opacity: 0.8;
}

.ca-chart-dot {
  cursor: pointer;
  transition: r 0.1s ease;
}

.ca-chart-dot:hover {
  r: 6;
}

.ca-chart-slice {
  cursor: pointer;
  transition: opacity 0.12s ease;
}

.ca-chart-slice:hover {
  opacity: 0.8;
}

.ca-chart-donut-total {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 700;
  fill: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.ca-chart-donut-label {
  font-family: var(--font-sans);
  font-size: 11px;
  fill: var(--text-tertiary);
}

/* Chart legend */
.ca-chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  margin-top: var(--space-2);
}

.ca-chart-legend--vert {
  flex-direction: column;
  justify-content: center;
}

.ca-chart-legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--font-small);
  color: var(--text-secondary);
}

.ca-chart-legend-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* Chart tooltip (position: fixed, JS-placed) */
.ca-chart-tip {
  position: fixed;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-small);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  box-shadow: var(--shadow-md);
  pointer-events: none;
  z-index: 700;
  white-space: nowrap;
  max-width: 280px;
}

/* ── Numeric cells (mirrors fin-table .num pattern) ── */
.ca-pivot__cell,
.ca-chart-tip {
  font-variant-numeric: tabular-nums;
}

/* ── Responsive ── */
@media (max-width: 1024px) {
  .ca-page {
    height: auto;
    flex-direction: column;
  }

  .ca-rail {
    width: 100%;
    height: auto;
    max-height: 180px;
    border-right: none;
    border-bottom: 1px solid var(--border-default);
  }

  .ca-rail__list {
    display: flex;
    flex-direction: row;
    gap: var(--space-1);
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0 var(--space-2) var(--space-2);
  }

  .ca-convo-item {
    white-space: nowrap;
    flex-shrink: 0;
    width: auto;
    max-width: 180px;
  }

  .ca-main {
    height: calc(100vh - 240px);
  }

  .ca-transcript {
    padding: var(--space-4);
  }

  .ca-input-bar {
    padding: var(--space-3) var(--space-4);
  }

  .ca-cost-footer {
    padding: var(--space-2) var(--space-4);
  }

  .ca-bubble__content {
    max-width: 96%;
  }
}
