/* SW Analysis dashboard styling — dark palette inspired by SR. */

:root {
  --bg: #0e0f12;
  --surface: #16181d;
  --surface-2: #1d2026;
  --border: #2a2e36;
  --text: #e6e8eb;
  --text-dim: #8a8f99;
  --text-faint: #5a5f68;
  --accent: #4a9eff;
  --accent-dim: #2d6cb8;
  --success: #2ecc71;
  --warn: #e67e22;
  --error: #e74c3c;
  --radius: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 18px;
  line-height: 1.5;
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.subtitle {
  margin-top: 4px;
  color: var(--text-dim);
  font-size: 0.8rem;
}

main {
  flex: 1;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ── Date + asset controls ── */
.controls {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 220px;
}
/* Asset selector fields need extra room — long labels like
   "BRK-B — Berkshire Hathaway (Class B) (Stock)" need ~380px at 18px font.
   Same width applies to the +Add benchmark field. */
.field:has(#baseAsset),
.field:has(#quoteAsset),
.field:has(#addBenchmark) {
  min-width: 380px;
}

.field label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  font-weight: 500;
}

.field input {
  padding: 8px 10px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
}

.field input:focus {
  outline: none;
  border-color: var(--accent);
}

.field input::placeholder { color: var(--text-faint); }

/* Toggle switch (used for chart-mode "Index to 1000") */
.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.switch input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.btn {
  padding: 8px 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
}

.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent-dim); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent); }
.btn-ghost { background: transparent; }

/* ── Stats panel ── */
.panels {
  display: block;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  min-height: 180px;
  position: relative;
}

/* Single stats table — labels on the left, one column per asset
   (base, quote, repriced, benchmarks). Header row shows asset name +
   sub-info; body rows show stat values. */
.stats-table {
  /* inline-grid sizes the container to its content width — no full-width
     stretch — so the panel's empty space sits to the right of the grid
     rather than inside it. */
  display: inline-grid;
  /* grid-template-columns set dynamically by JS. Value columns auto-
     size to widest content; column-gap sets a tight uniform buffer. */
  column-gap: 10px;
  row-gap: 4px;
  align-items: center;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.stats-header-blank { /* top-left corner empty */ }
.stats-header-asset {
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
  margin-bottom: 6px;
  /* Right-aligned to match the right-aligned values below — column
     name sits at the same x-coordinate as the values. */
  text-align: right;
}
.stats-header-name {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text);
}
.stats-header-sub {
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-top: 2px;
}
.stats-row-label {
  color: var(--text-dim);
  white-space: nowrap;
  text-align: left;
  padding: 2px 6px;
  border-radius: 3px;
  transition: background 0.12s ease, color 0.12s ease;
}
.stats-row-cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* All values right-aligned — decimal points line up vertically across
     rows (e.g. "$3.85T" / "$1.62T" / "$254.30" stack neatly). */
  text-align: right;
  padding: 2px 6px;
  border-radius: 3px;
  transition: background 0.12s ease, color 0.12s ease;
}

/* Row wrapper uses display:contents so cells participate directly in
   the parent grid (no extra layout tier), but the wrapper still
   exists in the DOM so :hover propagates from any cell to all cells
   in the same row. Lets us highlight the whole stat-row on hover. */
.stats-row { display: contents; }
.stats-row:hover > .stats-row-label,
.stats-row:hover > .stats-row-cell {
  background: rgba(74, 158, 255, 0.10);   /* subtle blue tint */
  color: var(--text);                      /* brighten dim labels */
}
.panel-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 8px;
  font-weight: 600;
}
.panel-asset-name {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 2px;
}
.panel-asset-sub {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 12px;
}
.panel-empty {
  color: var(--text-faint);
  font-style: italic;
  font-size: 0.85rem;
  padding: 30px 0;
  text-align: center;
}

.stat-grid {
  display: grid;
  /* Two natural-width columns, both pinned to the left of the panel.
     Empty space on the right is fine — keeps eye-travel short between
     label and value. */
  grid-template-columns: auto auto;
  justify-content: start;
  column-gap: 14px;
  /* Fixed row height so stat rows align horizontally across Base /
     Quote / Repriced panels (i.e. row 5 in Base lines up with row 5
     in Quote and Repriced). */
  grid-auto-rows: 1.7rem;
  align-items: center;
  font-size: 0.85rem;
  margin: 0;
}
.stat-grid dt {
  color: var(--text-dim);
  white-space: nowrap;
}
.stat-grid dd {
  margin: 0;
  text-align: left;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  white-space: nowrap;
}
.stat-pos { color: var(--success); }
.stat-neg { color: var(--error); }
.stat-neutral { color: var(--text); }
.stat-faint { color: var(--text-faint); }

/* ── Chart ── */
.chart-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 460px;
  position: relative;
}
#chart { width: 100%; height: 460px; }
.chart-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 460px;
  color: var(--text-faint);
  font-style: italic;
}
/* Toolbar that sits just below the chart's x-axis area. Holds the
   "Index to 1000" toggle (and any future chart-scoped controls).
   Right-aligned so it lines up under the chart's right margin without
   clashing with the y-axis label on the left. */
.chart-toolbar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 16px;
  margin-top: 4px;
  padding-left: 90px; /* mirrors Plotly's left margin so it sits under the plot's origin */
  color: var(--text-dim);
  font-size: 0.85rem;
}
.chart-toolbar .switch { height: auto; font-size: inherit; color: var(--text-dim); }
.chart-toolbar .switch input { width: 14px; height: 14px; }

/* ── Status / loading ── */
.status {
  font-size: 0.78rem;
  color: var(--text-dim);
  min-height: 1.2em;
}
.status .err { color: var(--error); }
.status .warn { color: var(--warn); }
.status .ok { color: var(--success); }

.benchmarks {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 4px;
}
.benchmark-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  font-size: 0.78rem;
}
.benchmark-chip .swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.benchmark-chip button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0 0 0 4px;
}
.benchmark-chip button:hover { color: var(--error); }

/* ── Analysis report section ─────────────────────────────────────── */
.analysis-section {
  margin-top: 24px;
  padding: 18px 20px 22px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.analysis-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
}
.analysis-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}
.analysis-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}
.analysis-asset-input {
  flex: 0 1 280px;
  min-width: 180px;
  padding: 6px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  font-size: 0.85rem;
}
.analysis-asset-input:focus {
  outline: none;
  border-color: var(--accent);
}
#btn-generate-analysis {
  padding: 7px 14px;
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
}
#btn-generate-analysis:hover { filter: brightness(1.1); }
#btn-generate-analysis:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  filter: none;
}
.analysis-status { font-size: 0.78rem; color: var(--text-dim); }
.analysis-progress { margin-bottom: 14px; }
.analysis-progress-bar {
  width: 100%;
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}
.analysis-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 250ms ease-out;
}
.analysis-progress-label {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 6px;
}
.analysis-empty {
  padding: 30px 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
}
.analysis-empty code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.8rem;
}
.analysis-body .report-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.analysis-body .report-title { font-size: 1.05rem; font-weight: 600; }
.analysis-body .report-meta { font-size: 0.78rem; color: var(--text-dim); }
.analysis-body .verdict-pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.78rem;
  font-weight: 600;
  margin-left: 8px;
}
.analysis-body .verdict-strong-buy { background: #1e6b3a; color: #fff; }
.analysis-body .verdict-buy        { background: #2d8a4e; color: #fff; }
.analysis-body .verdict-hold       { background: #847c2a; color: #fff; }
.analysis-body .verdict-sell       { background: #b25030; color: #fff; }
.analysis-body .verdict-avoid      { background: #8a2828; color: #fff; }
.analysis-body section.report-section { margin: 18px 0; }
/* Banner-style section headers — left accent bar, subtle gradient
   background, uppercase title, thin bottom rule. Visually demarcates
   each section without being heavy. */
.analysis-body section.report-section h3 {
  margin: 0 0 10px;
  padding: 8px 12px;
  background: linear-gradient(90deg, rgba(74, 158, 255, 0.14), rgba(74, 158, 255, 0));
  border-left: 4px solid var(--accent);
  border-bottom: 1px solid var(--border);
  border-radius: 3px 3px 0 0;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 1px;
  text-transform: uppercase;
}
.analysis-body section.report-section p {
  margin: 0 0 6px;
  padding: 0 4px;
  line-height: 1.55;
  color: var(--text);
  font-size: 0.9rem;
}
.analysis-body section.report-section ul {
  margin: 0;
  padding-left: 26px;
}
.analysis-body section.report-section li {
  margin: 4px 0;
  font-size: 0.88rem;
  line-height: 1.5;
}
.analysis-body .projection-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 8px;
}
@media (max-width: 720px) {
  .analysis-body .projection-grid { grid-template-columns: 1fr; }
}
.analysis-body .case-card {
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.analysis-body .case-card.bear  { border-color: #8a2828; }
.analysis-body .case-card.base  { border-color: #4a9eff; }
.analysis-body .case-card.bull  { border-color: #2d8a4e; }
.analysis-body .case-card .case-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.analysis-body .case-card .case-price {
  font-size: 1.2rem;
  font-weight: 600;
}
.analysis-body .case-card .case-return {
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.analysis-body .case-card .case-rationale {
  font-size: 0.83rem;
  line-height: 1.5;
  color: var(--text);
}
.analysis-body .data-gaps {
  margin-top: 10px;
  padding: 10px 12px;
  background: rgba(180, 130, 50, 0.08);
  border-left: 3px solid #b48232;
  border-radius: 3px;
  font-size: 0.82rem;
}
.analysis-body .data-gaps strong { color: #d6a455; }

/* ── Comparison panel ────────────────────────────────────────────── */
.analysis-compare {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.analysis-compare-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}
.analysis-compare-header h3 {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 600;
}
.analysis-compare-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.cmp-select {
  flex: 0 1 auto;
  min-width: 200px;
  padding: 6px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  font-size: 0.82rem;
}
.cmp-select:focus { outline: none; border-color: var(--accent); }
#btn-view-selected,
#btn-compare {
  padding: 7px 14px;
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.82rem;
}
/* View is the more common action ("recall a single past report"); style
   it slightly differently from Compare so the two adjacent primary
   buttons are visually distinct. View = filled accent, Compare = outline
   accent. */
#btn-compare {
  background: transparent;
  color: var(--accent);
}
#btn-view-selected:hover,
#btn-compare:hover { filter: brightness(1.15); background: var(--accent); color: #fff; }
#btn-view-selected:disabled,
#btn-compare:disabled { opacity: 0.55; cursor: not-allowed; }
#btn-clear-compare {
  padding: 7px 12px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.82rem;
}
#btn-clear-compare:hover { color: var(--text); border-color: var(--text-dim); }

/* Up to 3 columns wide, drops to 2 on medium, then stacks. The user
   asked for 3 if space permits, else 2. auto-fit + minmax(280px) gives
   exactly that responsive behaviour. */
.compare-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
}
.compare-card {
  padding: 14px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.compare-card .cmp-card-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.compare-card .cmp-ticker {
  font-size: 1rem;
  font-weight: 600;
}
.compare-card .cmp-company {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 2px;
}
.compare-card .cmp-meta {
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-top: 6px;
}
.compare-card .cmp-cases {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.compare-card .cmp-case {
  display: grid;
  grid-template-columns: 50px 1fr auto;
  gap: 8px;
  align-items: baseline;
  padding: 6px 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.02);
  font-size: 0.8rem;
}
.compare-card .cmp-case.bear { border-left: 3px solid #8a2828; }
.compare-card .cmp-case.base { border-left: 3px solid #4a9eff; }
.compare-card .cmp-case.bull { border-left: 3px solid #2d8a4e; }
.compare-card .cmp-case-label {
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}
.compare-card .cmp-case-price { font-weight: 600; }
.compare-card .cmp-case-return {
  font-size: 0.74rem;
  color: var(--text-dim);
  text-align: right;
}
.compare-card .cmp-verdict-summary {
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--text);
}

/* Report header action buttons (Download PDF / Regenerate) */
.analysis-body .report-header { align-items: flex-start; }
.report-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}
.report-action-btn {
  padding: 6px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  font-size: 0.78rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
.report-action-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Compare-card action links (PDF / Rerun) */
.cmp-card-header { align-items: flex-start; }
.cmp-card-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.cmp-action-link {
  padding: 3px 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
  font-size: 0.72rem;
  cursor: pointer;
  text-decoration: none;
}
.cmp-action-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Persistent in-body error panel — replaces the old tiny status-line
   error display so messages are readable and copyable. */
.analysis-error {
  margin: 12px 0;
  padding: 14px 16px;
  background: rgba(180, 60, 50, 0.08);
  border: 1px solid #8a3838;
  border-left: 4px solid #c0392b;
  border-radius: 4px;
}
.analysis-error-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.analysis-error-header strong { color: #e57373; font-size: 0.95rem; }
.analysis-error-text {
  margin: 0;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 480px;
  overflow: auto;
  -webkit-user-select: text;
  user-select: text;
}
.analysis-error-hint {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-dim);
}

/* ── Tabs (Asset analysis | Allocation) ───────────────────────────── */
.tabbar {
  display: flex;
  gap: 4px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.tabbar .tab {
  appearance: none;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 11px 16px 9px;
  cursor: pointer;
}
.tabbar .tab:hover { color: var(--text); }
.tabbar .tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}
.tabpanel { display: none; }
.tabpanel.active { display: block; }
/* Allocation dashboard is a self-contained page hosted in an iframe; fill the viewport below the tab bar. */
.alloc-frame {
  display: block;
  width: 100%;
  height: calc(100vh - 110px);
  min-height: 600px;
  border: none;
  background: var(--bg);
}
