/* ── Explain Button ───────────────────────────────────────────────────────── */

.explain-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  white-space: nowrap;
  /* Override pricer-btn-sm colors with accent outline style */
  color: var(--color-accent-text) !important;
  background: transparent !important;
  border-color: var(--color-accent) !important;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.explain-btn:hover {
  background: var(--color-accent);
  color: #fff;
}

/* Portfolio sidebar context: stretch the Explain button to match the
   sibling Reset P&L / Clear Portfolio buttons below it. */
.pf-actions #pf-explain-cell {
  display: block;
  margin-bottom: 6px;
}
.pf-actions .explain-btn {
  width: 100%;
}

/* Tighten on mobile so the button stops forcing the pricer's first column
   wider than necessary. */
@media (max-width: 768px) {
  .explain-btn {
    min-width: 0;
    padding: 0 10px;
    font-size: 0.78rem;
  }
}

/* ── Popover ─────────────────────────────────────────────────────────────── */

/* Container — peer-of-formula-panel surface tier (--bg-surface) with a
   trimmed --r-md radius. The audit V7 decision was: keep the frame
   (a draggable popover does need one), but step the radius down from
   --r-lg (12px) to --r-md (8px) so the panel reads as institutional
   reference, matching the formula-panel restraint. */
.explain-popover {
  position: fixed;
  z-index: var(--z-max);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.30), 0 1px 4px rgba(0, 0, 0, 0.15);
  max-height: 600px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: explain-fade-in 0.15s ease;
}
[data-theme="light"] .explain-popover {
  box-shadow: 0 6px 24px rgba(27, 58, 92, 0.12), 0 1px 4px rgba(27, 58, 92, 0.06);
}

@keyframes explain-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Header ──────────────────────────────────────────────────────────────── */

/* Header chrome — hosts the contextual title (Q1 option A), an optional
   module tag, the action buttons (copy, future re-explain), and the
   close button. Sits on the same surface as body — no background tint
   (V6 audit removed the invisible 2% rgba). The border-bottom alone
   separates the header from the prose. */
.explain-popover-header {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px 8px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  cursor: grab;
  user-select: none;
}
.explain-popover-header:active { cursor: grabbing; }

.explain-popover-header-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* AI transparency disclosure (EU AI Act Article 50(1)). Sits beneath the
   title row in muted-text register — present at first exposure, clearly
   legible, but visually subordinate to the title. */
.explain-popover-disclosure {
  font-family: var(--font);
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1.2;
  letter-spacing: 0.02em;
}

/* Q1 option A — contextual title (Inter sans, 0.95rem / 600, mixed case,
   --text colour). Matches formula-panel-title for cross-panel coherence
   (components.css ~5869). Drops the previous T09 stack (uppercase +
   tracked + muted). */
.explain-popover-title {
  font-family: var(--font);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
  /* Allow the title to truncate with an ellipsis if a multi-leg
     pricer produces a very long structure label — keeps the header
     row from wrapping. */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}

/* Action buttons (copy, future re-explain) — icon-only, ghost, top-right
   of the header before the close button. */
.explain-popover-actions {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.explain-popover-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  transition: color 0.15s, background 0.15s;
}
.explain-popover-action:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--text) 6%, transparent);
}
/* Brief check-mark state on copy success — no toast, no tooltip. */
.explain-popover-action.is-confirmed {
  color: var(--positive);
}
/* Copy is disabled while a stream is in flight (can't copy a half-response). */
.explain-popover-action:disabled {
  opacity: 0.4;
  cursor: default;
  pointer-events: none;
}
.explain-popover-action svg { display: block; }

.explain-popover-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}
.explain-popover-close:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--text) 6%, transparent);
}

/* ── Body ────────────────────────────────────────────────────────────────── */

/* Body padding uses --space-* tokens (V13 audit) — vertical-rhythm
   coherence with handbook prose. */
.explain-popover-body {
  padding: var(--space-block) var(--space-subsection)
           var(--space-subsection) var(--space-subsection);
  overflow-y: auto;
  flex: 1;
}

/* Q2 — body type lifted to Newsreader serif, 1rem (16px) / 1.65, --text
   colour. Pairs with the 560px popover width (set in JS at
   _positionPopover) to land at ~62 ch — inside Bringhurst's 60-75 ch
   comfort band. Drops the previous 12px Inter chrome-metadata register
   that read as a UI annotation rather than a textbook walkthrough. */
.explain-text {
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 1rem;
  line-height: 1.65;
  color: var(--text);
  font-feature-settings: 'kern' 1;
  margin: 0;
}
.explain-text p,
.explain-text li { color: inherit; }
.explain-text strong { font-weight: 600; color: inherit; }
.explain-text em { font-style: italic; color: inherit; }

/* Streaming cursor — a blinking caret at the leading edge while the model
   writes. The universal AI-streaming signal (a typographic cue, not an AI
   badge); class is added while the reader is open and removed on done/error. */
.explain-text--streaming::after {
  content: '\258C';  /* left half block */
  display: inline-block;
  margin-left: 1px;
  color: var(--text-muted);
  animation: explain-cursor-blink 1s steps(2) infinite;
}
@keyframes explain-cursor-blink {
  0%, 50% { opacity: 1; }
  50.01%, 100% { opacity: 0; }
}

/* Q3 — heading hierarchy. Quiet textbook ladder: weight + size + spacing
   only. No colour, no transform, no tracking, no border (except optional
   hairline above h3 for top-level break). The single .explain-heading
   class still ships from _formatExplanation; we scope by tag for the
   per-level treatment. */
.explain-text h3.explain-heading {
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.35;
  color: var(--text);
  margin: var(--space-subsection) 0 var(--space-block);
  padding-top: var(--space-block);
  border-top: 1px solid var(--border);
  /* drop: text-transform, letter-spacing, --explain-heading colour */
}
.explain-text h4.explain-heading {
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
  margin: var(--space-block) 0 4px;
  padding-top: 0;
  border-top: 0;
}
.explain-text h5.explain-heading {
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
  margin: 12px 0 4px;
  padding-top: 0;
  border-top: 0;
}
/* First heading in the body needs no top margin / rule (no preceding
   content to break from). */
.explain-text > h3.explain-heading:first-child,
.explain-text > h4.explain-heading:first-child,
.explain-text > h5.explain-heading:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

/* Opening "summary" paragraph: lead-in italic + hairline rule. Together
   they read as a deliberate "thesis line set apart from the analysis."
   The italic carries the typographic signal; the rule reinforces it. */
.explain-para { margin: 0 0 var(--space-block); }

.explain-text .explain-para:first-child {
  font-style: italic;
  line-height: 1.55;
  padding-bottom: var(--space-block);
  margin-bottom: var(--space-block);
  border-bottom: 1px solid var(--border);
}

/* Bullet lists — V11 audit. Hanging indent, mid-grey middle-dot marker
   sitting in the gutter. Matches handbook list rendering; bullet doesn't
   compete with body weight. */
.explain-list {
  list-style: none;
  margin: var(--space-block) 0;
  padding-left: 1.25rem;
}
.explain-list li {
  position: relative;
  margin-bottom: 6px;
  line-height: inherit;
}
.explain-list li::before {
  content: '\2022';
  position: absolute;
  left: -0.85rem;
  top: 0;
  color: var(--text-muted);
  font-weight: 400;
  line-height: inherit;
}

/* ── Stale-explanation banner (S3) ─────────────────────────────────── */

/* Hidden by default; JS adds .is-visible when the cached gatherFn
   payload diverges from the current one. Class-toggle (not the HTML
   `hidden` attribute) so the banner's display rule beats UA defaults. */
.explain-stale-banner {
  display: none;
  align-items: center;
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 0.92rem;
  color: var(--text-secondary);
  background: color-mix(in srgb, var(--warning) 10%, transparent);
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  line-height: 1.4;
}
.explain-stale-banner.is-visible { display: flex; }
.explain-stale-retry {
  background: none;
  border: 0;
  padding: 0;
  margin-left: 6px;
  font: inherit;
  color: var(--color-accent-text);
  text-decoration: underline;
  cursor: pointer;
}

/* ── Error state — quiet by default, emphatic only on hard failures ── */

.explain-error {
  font-family: 'Newsreader', 'Inter', system-ui, serif;
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text-secondary);
  margin: 0;
}
.explain-error--hard {
  color: var(--negative);
}
.explain-error-retry {
  display: inline;
  margin-left: 6px;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--color-accent-text);
  text-decoration: underline;
  cursor: pointer;
}
.explain-error-retry:hover { color: var(--text); }

/* ── Skeleton loading — V12 retuned for new body type ─────────────────── */

/* Six bars (was 3) at 16px height matching the new Newsreader 1rem body;
   slightly slower 1.4s pulse for less attention-grabbing waiting. */
.explain-skeleton {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.explain-skeleton span {
  display: block;
  height: 16px;
  border-radius: 4px;
  background: var(--bg-elevated);
  animation: explain-pulse 1.4s ease-in-out infinite;
}
.explain-skeleton span:nth-child(1) { width: 92%; }
.explain-skeleton span:nth-child(2) { width: 76%; }
.explain-skeleton span:nth-child(3) { width: 88%; }
.explain-skeleton span:nth-child(4) { width: 64%; }
.explain-skeleton span:nth-child(5) { width: 80%; }
.explain-skeleton span:nth-child(6) { width: 50%; }

@keyframes explain-pulse {
  0%, 100% { opacity: 0.4; }
  50%      { opacity: 0.8; }
}

/* ── Mobile: full-width popover ── */
@media (max-width: 768px) {
  .explain-popover {
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-height: 70vh;
  }
}
