/* APR Report Tables v1 (PR #252)
   Single-owner CSS for new-format report tables (.apr-table).
   Old reports use .table-wrapper — selectors never overlap, zero conflict.
   ─────────────────────────────────────────────────────────────────────── */

.report-detail {
  --apr-table-font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --apr-table-header-bg: #2c3e50;
  --apr-table-header-text: #ffffff;
  --apr-table-border: #cbd5e1;
  --apr-table-row-alt: #f8fafc;
  --apr-table-label-bg: #f8fafc;
  --apr-table-text: #0f172a;
  --apr-table-header-size: 0.82rem;
  --apr-table-body-size: 0.875rem;
  --apr-table-line-height: 1.45;
  --apr-table-header-weight: 700;
  --apr-table-body-weight: 400;
  --apr-table-label-weight: 600;
  --apr-table-pad-y: 7px;
  --apr-table-pad-x: 12px;
}

.apr-table {
  margin: 16px 0 24px;
}

.apr-table__scroll {
  overflow-x: auto;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  border: 1px solid var(--apr-table-border);
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
}

.apr-table__scroll table {
  width: 100%;
  min-width: 100%;
  border-collapse: collapse;
  font-family: var(--apr-table-font-family);
  color: var(--apr-table-text);
  font-variant-numeric: tabular-nums;
  background: #fff;
}

.apr-table__th,
.apr-table__td {
  border: 1px solid var(--apr-table-border);
  padding: var(--apr-table-pad-y) var(--apr-table-pad-x);
  vertical-align: top;
}

.apr-table__th {
  background: var(--apr-table-header-bg);
  color: var(--apr-table-header-text);
  font-size: var(--apr-table-header-size);
  font-weight: var(--apr-table-header-weight);
  line-height: 1.35;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
}

.apr-table__td {
  background: #fff;
  color: var(--apr-table-text);
  font-size: var(--apr-table-body-size);
  font-weight: var(--apr-table-body-weight);
  line-height: var(--apr-table-line-height);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.apr-table tbody tr:nth-child(even) .apr-table__td {
  background: var(--apr-table-row-alt);
}

/* ── Cell kind variants ─────────────────────────────────────────────── */

/* label: first column — bold, light bg, left-align */
.apr-table__th--label,
.apr-table__td--label { text-align: left; }
.apr-table__td--label {
  background: var(--apr-table-label-bg);
  font-weight: var(--apr-table-label-weight);
}

/* num: financial values — right-align, tabular nums */
.apr-table__th--num,
.apr-table__td--num { text-align: right; }

/* text: narrative — left-align, normal weight */
.apr-table__th--text,
.apr-table__td--text { text-align: left; }

/* chip: status badges (Impact/Priority HIGH/MED/LOW) — center-align */
.apr-table__th--chip,
.apr-table__td--chip { text-align: center; }

.apr-table[data-table-kind="analyst-ratings"] .apr-table__th,
.apr-table[data-table-kind="peer-readthrough"] .apr-table__th {
  white-space: normal;
  overflow-wrap: normal;
  word-break: keep-all;
  hyphens: none;
}

.apr-table[data-apr-layout-source="contract"] .apr-table__th,
.apr-table[data-apr-layout-source="contract"] .apr-table__td {
  white-space: var(--apr-col-white-space, normal);
  overflow-wrap: var(--apr-col-overflow-wrap, anywhere);
  word-break: var(--apr-col-word-break, break-word);
}

.apr-table[data-table-kind="company-positioning-equal"],
.apr-table[data-table-kind="monetization-vector"] {
  --apr-company-first-col-width: 24ch;
}

.apr-table[data-table-kind="company-positioning-equal"] th:nth-child(1),
.apr-table[data-table-kind="company-positioning-equal"] td:nth-child(1),
.apr-table[data-table-kind="monetization-vector"] th:nth-child(1),
.apr-table[data-table-kind="monetization-vector"] td:nth-child(1) {
  white-space: normal;
  overflow-wrap: normal;
  word-break: keep-all;
  hyphens: none;
}

.apr-table[data-table-kind="company-positioning-equal"] td:nth-child(1) .apr-company-name,
.apr-table[data-table-kind="monetization-vector"] td:nth-child(1) .apr-company-name {
  display: block;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: normal;
  hyphens: auto;
}

.apr-table[data-table-kind="company-positioning-equal"] td:nth-child(1) .apr-company-ticker,
.apr-table[data-table-kind="monetization-vector"] td:nth-child(1) .apr-company-ticker {
  display: block;
}

/* ── Desktop column proportions by table kind ──────────────────────────
   Uses data-table-kind attribute — no brittle nth-of-type positional selectors.
   Applies only at min-width: 641px. Mobile gets auto layout + horizontal scroll.
─────────────────────────────────────────────────────────────────────────────── */
@media (min-width: 641px) {

  .apr-table[data-apr-layout-source="contract"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-apr-layout-source="contract"] .apr-table__th,
  .apr-table[data-apr-layout-source="contract"] .apr-table__td { width: var(--apr-col-width-desktop, auto); }

  /* Key Takeaways: Item(22%) | Impact(14%) | Why It Mattered(rem) */
  .apr-table[data-table-kind="key-takeaways"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="key-takeaways"] th:nth-child(1),
  .apr-table[data-table-kind="key-takeaways"] td:nth-child(1) { width: 22%; }
  .apr-table[data-table-kind="key-takeaways"] th:nth-child(2),
  .apr-table[data-table-kind="key-takeaways"] td:nth-child(2) { width: 14%; }

  /* Results vs Expectations: Metric(18%) | Reported(12%) | Consensus(18%) | Guide(18%) | Read(rem) */
  .apr-table[data-table-kind="results-vs-expectations"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="results-vs-expectations"] th:nth-child(1),
  .apr-table[data-table-kind="results-vs-expectations"] td:nth-child(1) { width: 18%; }
  .apr-table[data-table-kind="results-vs-expectations"] th:nth-child(2),
  .apr-table[data-table-kind="results-vs-expectations"] td:nth-child(2) { width: 12%; }
  .apr-table[data-table-kind="results-vs-expectations"] th:nth-child(3),
  .apr-table[data-table-kind="results-vs-expectations"] td:nth-child(3) { width: 18%; }
  .apr-table[data-table-kind="results-vs-expectations"] th:nth-child(4),
  .apr-table[data-table-kind="results-vs-expectations"] td:nth-child(4) { width: 18%; }

  /* Sequential Comparison: Metric(22%) | Prior(18%) | Current(18%) | SeqChange(11%) | YoY(rem) */
  .apr-table[data-table-kind="sequential-comparison"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="sequential-comparison"] th:nth-child(1),
  .apr-table[data-table-kind="sequential-comparison"] td:nth-child(1) { width: 22%; }
  .apr-table[data-table-kind="sequential-comparison"] th:nth-child(2),
  .apr-table[data-table-kind="sequential-comparison"] td:nth-child(2) { width: 18%; }
  .apr-table[data-table-kind="sequential-comparison"] th:nth-child(3),
  .apr-table[data-table-kind="sequential-comparison"] td:nth-child(3) { width: 18%; }
  .apr-table[data-table-kind="sequential-comparison"] th:nth-child(4),
  .apr-table[data-table-kind="sequential-comparison"] td:nth-child(4) { width: 11%; }

  /* Forward Guidance: Metric(18%) | Current(15%) | MgmtDir(25%) | Implied(12%) | ReadThru(rem) */
  .apr-table[data-table-kind="forward-guidance"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="forward-guidance"] th:nth-child(1),
  .apr-table[data-table-kind="forward-guidance"] td:nth-child(1) { width: 18%; }
  .apr-table[data-table-kind="forward-guidance"] th:nth-child(2),
  .apr-table[data-table-kind="forward-guidance"] td:nth-child(2) { width: 15%; }
  .apr-table[data-table-kind="forward-guidance"] th:nth-child(3),
  .apr-table[data-table-kind="forward-guidance"] td:nth-child(3) { width: 25%; }
  .apr-table[data-table-kind="forward-guidance"] th:nth-child(4),
  .apr-table[data-table-kind="forward-guidance"] td:nth-child(4) { width: 12%; }

  /* Mgmt Commentary: Topic(18%) | PriorQuote(34%) | CurrentQuote(34%) | Signal(14%) */
  .apr-table[data-table-kind="mgmt-commentary"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="mgmt-commentary"] th:nth-child(1),
  .apr-table[data-table-kind="mgmt-commentary"] td:nth-child(1) { width: 18%; }
  .apr-table[data-table-kind="mgmt-commentary"] th:nth-child(2),
  .apr-table[data-table-kind="mgmt-commentary"] td:nth-child(2) { width: 34%; }
  .apr-table[data-table-kind="mgmt-commentary"] th:nth-child(3),
  .apr-table[data-table-kind="mgmt-commentary"] td:nth-child(3) { width: 34%; }
  .apr-table[data-table-kind="mgmt-commentary"] th:nth-child(4),
  .apr-table[data-table-kind="mgmt-commentary"] td:nth-child(4) { width: 14%; }

  /* Positioning: Metric(30%) | Value(20%) | Assessment(rem) */
  .apr-table[data-table-kind="positioning"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="positioning"] th:nth-child(1),
  .apr-table[data-table-kind="positioning"] td:nth-child(1) { width: 30%; }
  .apr-table[data-table-kind="positioning"] th:nth-child(2),
  .apr-table[data-table-kind="positioning"] td:nth-child(2) { width: 20%; }

  /* Price Performance: Period(10%) | Stock(15%) | Sector(18%) | Market(18%) | Context(rem) */
  .apr-table[data-table-kind="price-performance"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="price-performance"] th:nth-child(1),
  .apr-table[data-table-kind="price-performance"] td:nth-child(1) { width: 10%; }
  .apr-table[data-table-kind="price-performance"] th:nth-child(2),
  .apr-table[data-table-kind="price-performance"] td:nth-child(2) { width: 15%; }
  .apr-table[data-table-kind="price-performance"] th:nth-child(3),
  .apr-table[data-table-kind="price-performance"] td:nth-child(3) { width: 18%; }
  .apr-table[data-table-kind="price-performance"] th:nth-child(4),
  .apr-table[data-table-kind="price-performance"] td:nth-child(4) { width: 18%; }

  /* Analyst Ratings: Firm(16%) | Action(15%) | NewPT(10%) | Rating(10%) | Takeaway(rem) */
  .apr-table[data-table-kind="analyst-ratings"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="analyst-ratings"] th:nth-child(1),
  .apr-table[data-table-kind="analyst-ratings"] td:nth-child(1) { width: 16%; }
  .apr-table[data-table-kind="analyst-ratings"] th:nth-child(2),
  .apr-table[data-table-kind="analyst-ratings"] td:nth-child(2) { width: 15%; }
  .apr-table[data-table-kind="analyst-ratings"] th:nth-child(3),
  .apr-table[data-table-kind="analyst-ratings"] td:nth-child(3) { width: 10%; }
  .apr-table[data-table-kind="analyst-ratings"] th:nth-child(4),
  .apr-table[data-table-kind="analyst-ratings"] td:nth-child(4) { width: 10%; }

  /* Peer Read-Through: Peer(10%) | MarketCap(14%) | Valuation(18%) | ReadThrough(rem) */
  .apr-table[data-table-kind="peer-readthrough"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="peer-readthrough"] th:nth-child(1),
  .apr-table[data-table-kind="peer-readthrough"] td:nth-child(1) { width: 10%; }
  .apr-table[data-table-kind="peer-readthrough"] th:nth-child(2),
  .apr-table[data-table-kind="peer-readthrough"] td:nth-child(2) { width: 14%; }
  .apr-table[data-table-kind="peer-readthrough"] th:nth-child(3),
  .apr-table[data-table-kind="peer-readthrough"] td:nth-child(3) { width: 18%; }

  /* Peer Comparison: Peer(18%) | Price(12%) | MktCap(14%) | FwdPE(18%) | ReadThru(rem) */
  .apr-table[data-table-kind="peer-comparison"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="peer-comparison"] th:nth-child(1),
  .apr-table[data-table-kind="peer-comparison"] td:nth-child(1) { width: 18%; }
  .apr-table[data-table-kind="peer-comparison"] th:nth-child(2),
  .apr-table[data-table-kind="peer-comparison"] td:nth-child(2) { width: 12%; }
  .apr-table[data-table-kind="peer-comparison"] th:nth-child(3),
  .apr-table[data-table-kind="peer-comparison"] td:nth-child(3) { width: 14%; }
  .apr-table[data-table-kind="peer-comparison"] th:nth-child(4),
  .apr-table[data-table-kind="peer-comparison"] td:nth-child(4) { width: 18%; }

  /* Bull/Bear: equal split 50%/50% */
  .apr-table[data-table-kind="bull-bear"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="bull-bear"] th:nth-child(1),
  .apr-table[data-table-kind="bull-bear"] td:nth-child(1) { width: 50%; }

  /* Catalyst Watch: Catalyst(28%) | Priority(12%) | Date(18%) | Monitor(rem) */
  .apr-table[data-table-kind="catalyst-watch"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="catalyst-watch"] th:nth-child(1),
  .apr-table[data-table-kind="catalyst-watch"] td:nth-child(1) { width: 28%; }
  .apr-table[data-table-kind="catalyst-watch"] th:nth-child(2),
  .apr-table[data-table-kind="catalyst-watch"] td:nth-child(2) { width: 12%; }
  .apr-table[data-table-kind="catalyst-watch"] th:nth-child(3),
  .apr-table[data-table-kind="catalyst-watch"] td:nth-child(3) { width: 18%; }

  /* Q&A Coverage: Analyst(22%) | Firm(20%) | Topics(rem) */
  .apr-table[data-table-kind="qa-coverage"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="qa-coverage"] th:nth-child(1),
  .apr-table[data-table-kind="qa-coverage"] td:nth-child(1) { width: 22%; }
  .apr-table[data-table-kind="qa-coverage"] th:nth-child(2),
  .apr-table[data-table-kind="qa-coverage"] td:nth-child(2) { width: 20%; }

  /* Guidance vs Consensus: Metric(22%) | Guidance(20%) | Consensus(20%) | Notes(rem) */
  .apr-table[data-table-kind="guidance-vs-consensus"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="guidance-vs-consensus"] th:nth-child(1),
  .apr-table[data-table-kind="guidance-vs-consensus"] td:nth-child(1) { width: 22%; }
  .apr-table[data-table-kind="guidance-vs-consensus"] th:nth-child(2),
  .apr-table[data-table-kind="guidance-vs-consensus"] td:nth-child(2) { width: 20%; }
  .apr-table[data-table-kind="guidance-vs-consensus"] th:nth-child(3),
  .apr-table[data-table-kind="guidance-vs-consensus"] td:nth-child(3) { width: 20%; }

  /* Preview Sequential: Metric(30%) | Consensus(20%) | Prior(20%) | Change(rem) */
  .apr-table[data-table-kind="preview-sequential"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="preview-sequential"] th:nth-child(1),
  .apr-table[data-table-kind="preview-sequential"] td:nth-child(1) { width: 30%; }
  .apr-table[data-table-kind="preview-sequential"] th:nth-child(2),
  .apr-table[data-table-kind="preview-sequential"] td:nth-child(2) { width: 20%; }
  .apr-table[data-table-kind="preview-sequential"] th:nth-child(3),
  .apr-table[data-table-kind="preview-sequential"] td:nth-child(3) { width: 20%; }

  /* Estimate Revision History: Metric(28%) | Q1(22%) | Q2(22%) | Dir(rem) */
  .apr-table[data-table-kind="estimate-revision-history"] .apr-table__scroll table { table-layout: fixed; }
  .apr-table[data-table-kind="estimate-revision-history"] th:nth-child(1),
  .apr-table[data-table-kind="estimate-revision-history"] td:nth-child(1) { width: 28%; }
  .apr-table[data-table-kind="estimate-revision-history"] th:nth-child(2),
  .apr-table[data-table-kind="estimate-revision-history"] td:nth-child(2) { width: 22%; }
  .apr-table[data-table-kind="estimate-revision-history"] th:nth-child(3),
  .apr-table[data-table-kind="estimate-revision-history"] td:nth-child(3) { width: 22%; }

  /* generic, segment-scorecard, kpi-tracker,
     rating-distribution, summary-metrics, historical-financials,
     consensus-revision → auto layout (content-driven widths) */
}

/* ── Mobile overrides ───────────────────────────────────────────────── */
@media (max-width: 640px) {
  .report-detail {
    --apr-table-header-size: 0.75rem;
    --apr-table-body-size: 0.8125rem;
    --apr-table-line-height: 1.35;
    --apr-table-label-weight: 600;
    --apr-table-pad-y: 6px;
    --apr-table-pad-x: 8px;
  }

  /* Let mobile APR tables shrink-wrap before falling back to horizontal scroll. */
  .apr-table__scroll table {
    width: fit-content !important;
    min-width: 100%;
    max-width: none;
    table-layout: auto !important;
  }

  .apr-table__th {
    min-width: 72px;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .apr-table__scroll[data-sticky-first-col="true"] thead th:first-child {
    position: sticky;
    left: 0;
    z-index: 3;
    background: var(--apr-table-header-bg);
    box-shadow: 1px 0 0 var(--apr-table-border);
  }

  .apr-table__scroll[data-sticky-first-col="true"] tbody td:first-child,
  .apr-table__scroll[data-sticky-first-col="true"] tbody th:first-child,
  .apr-table__scroll[data-sticky-first-col="true"] tfoot td:first-child,
  .apr-table__scroll[data-sticky-first-col="true"] tfoot th:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--apr-table-label-bg);
    box-shadow: 1px 0 0 var(--apr-table-border);
  }

  .apr-table__scroll[data-sticky-first-col="true"] tbody tr:nth-child(even) td:first-child,
  .apr-table__scroll[data-sticky-first-col="true"] tbody tr:nth-child(even) th:first-child {
    background: var(--apr-table-row-alt);
  }

  .apr-table[data-apr-layout-source="contract"] .apr-table__scroll table {
    width: max(100%, var(--apr-contract-mobile-table-width, 0px)) !important;
    min-width: max(100%, var(--apr-contract-mobile-table-width, 0px)) !important;
    table-layout: fixed !important;
  }

  .apr-table[data-apr-layout-source="contract"] .apr-table__th,
  .apr-table[data-apr-layout-source="contract"] .apr-table__td {
    width: var(--apr-col-width-mobile, auto) !important;
    min-width: var(--apr-col-min-width-mobile, var(--apr-col-width-mobile, auto)) !important;
    max-width: var(--apr-col-width-mobile, none) !important;
    white-space: var(--apr-col-white-space, normal);
    overflow-wrap: var(--apr-col-overflow-wrap, anywhere);
    word-break: var(--apr-col-word-break, break-word);
  }

  .apr-table[data-table-kind="company-positioning-equal"] {
    --apr-company-table-min-width: 60rem;
  }

  .apr-table[data-table-kind="monetization-vector"] {
    --apr-company-table-min-width: 72rem;
  }

  .apr-table[data-table-kind="company-positioning-equal"] .apr-table__scroll table,
  .apr-table[data-table-kind="monetization-vector"] .apr-table__scroll table {
    width: var(--apr-company-table-min-width) !important;
    min-width: var(--apr-company-table-min-width) !important;
    table-layout: fixed !important;
  }

  .apr-table[data-table-kind="company-positioning-equal"] th:nth-child(1),
  .apr-table[data-table-kind="company-positioning-equal"] td:nth-child(1),
  .apr-table[data-table-kind="monetization-vector"] th:nth-child(1),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(1) {
    width: var(--apr-company-first-col-width) !important;
    min-width: var(--apr-company-first-col-width) !important;
    max-width: var(--apr-company-first-col-width) !important;
  }

  .apr-table[data-table-kind="company-positioning-equal"] th:nth-child(2),
  .apr-table[data-table-kind="company-positioning-equal"] td:nth-child(2),
  .apr-table[data-table-kind="company-positioning-equal"] th:nth-child(3),
  .apr-table[data-table-kind="company-positioning-equal"] td:nth-child(3),
  .apr-table[data-table-kind="company-positioning-equal"] th:nth-child(4),
  .apr-table[data-table-kind="company-positioning-equal"] td:nth-child(4) {
    width: calc((var(--apr-company-table-min-width) - var(--apr-company-first-col-width)) / 3) !important;
    min-width: calc((var(--apr-company-table-min-width) - var(--apr-company-first-col-width)) / 3) !important;
  }

  .apr-table[data-table-kind="monetization-vector"] th:nth-child(2),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(2),
  .apr-table[data-table-kind="monetization-vector"] th:nth-child(3),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(3),
  .apr-table[data-table-kind="monetization-vector"] th:nth-child(4),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(4),
  .apr-table[data-table-kind="monetization-vector"] th:nth-child(5),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(5) {
    width: 8rem !important;
    min-width: 8rem !important;
  }

  .apr-table[data-table-kind="monetization-vector"] th:nth-child(6),
  .apr-table[data-table-kind="monetization-vector"] td:nth-child(6) {
    width: calc(var(--apr-company-table-min-width) - var(--apr-company-first-col-width) - 32rem) !important;
    min-width: calc(var(--apr-company-table-min-width) - var(--apr-company-first-col-width) - 32rem) !important;
  }
}
