/*
 * Data table: financial schedules and comparison grids.
 *   <div class="lv-table-wrap" role="region" aria-label="Operating forecast" tabindex="0">     ← the scroll box
 *     <table class="lv-table">
 *       <thead><tr>
 *         <th scope="col">Line item</th>
 *         <th scope="col" class="lv-table__num">FY2025</th>
 *         <th scope="col" class="lv-table__num lv-table__forecast lv-table__boundary">FY2026E</th>   ← first forecast column
 *         <th scope="col" class="lv-table__num lv-table__forecast" aria-sort="descending"><button class="lv-table__sort">IRR</button></th>
 *       </tr></thead>
 *       <tbody class="lv-table__group">                              ← data-collapsed hides every row but the group head
 *         <tr class="lv-table__group-head"><th scope="rowgroup" colspan="4"><button class="lv-table__group-toggle" aria-expanded="true">Revenue build</button></th></tr>
 *         <tr class="lv-table__row--input"><th scope="row">Saunas sold (k)</th><td class="lv-table__num">…</td>…</tr>
 *         <tr class="lv-table__row--derived">…</tr>
 *         <tr class="lv-table__row--total">…</tr>
 *       </tbody>
 *     </table>
 *   </div>
 * The header row sticks inside the scroll box (it is capped at --lv-table-max-height, 70vh by default)
 * and the first column sticks while scrolling sideways. Every cell in a forecast column carries
 * lv-table__forecast (the tinted band); the first forecast column also carries lv-table__boundary
 * (the vertical rule where reported years end). Row styles: input (a driver we chose: ink, marked
 * with a bar), derived (arithmetic from inputs: quieter), total (semibold over a rule).
 * Current row: aria-selected="true" on the <tr>. Empty: one <tr class="lv-table__empty"><td colspan="…">.
 * Loading: aria-busy="true" on lv-table-wrap, with lv-skeleton bars in the cells.
 */
@layer components {
  .lv-table-wrap {
    position: relative; width: 100%; max-height: var(--lv-table-max-height, 70vh); overflow: auto;
    overscroll-behavior-x: contain; scrollbar-width: thin;
  }
  .lv-table-wrap:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
  .lv-table-wrap[aria-busy="true"] { pointer-events: none; }
  /* No box around a table, so its first and last columns line up with the text above it. */
  .lv-table {
    --lv-table-edge: 0px;
    width: 100%; border-collapse: separate; border-spacing: 0; text-align: left;
    font-size: var(--type-table-size); line-height: var(--type-table-leading); font-variant-numeric: tabular-nums; color: var(--text);
  }
  /* separate borders (not collapsed) so sticky cells keep their rules while scrolling. */
  .lv-table :is(th, td) {
    padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border-soft);
    white-space: nowrap; vertical-align: baseline; background-color: var(--surface);
    border-bottom-color: color-mix(in srgb, var(--border-soft) 70%, transparent);
  }
  .lv-table :is(th, td):first-child { padding-left: var(--lv-table-edge); }
  .lv-table :is(th, td):last-child { padding-right: var(--lv-table-edge); }
  .lv-table tbody th { font-weight: var(--weight-regular); }

  /* Sticky header and first column. The corner cell sits above both. */
  .lv-table thead th {
    position: sticky; top: 0; z-index: 2; background-color: var(--surface);
    border-bottom: 1px solid var(--border); font-size: var(--text-s); font-weight: var(--weight-medium); color: var(--text-muted);
  }
  .lv-table :is(tbody, tfoot) > tr > :first-child { position: sticky; left: 0; z-index: 1; }
  .lv-table thead th:first-child { left: 0; z-index: 3; }

  .lv-table__num { text-align: right; font-variant-numeric: tabular-nums; }
  .lv-table td:empty::before { content: '—'; color: var(--text-faint); }

  /* Forecast band and the reported/forecast boundary. */
  .lv-table .lv-table__forecast { background-color: var(--forecast-band); }
  .lv-table thead .lv-table__forecast { background-color: var(--forecast-band); }
  .lv-table .lv-table__boundary { box-shadow: inset 1px 0 0 var(--border); }

  /* Hover and the current row are drawn as an overlay, so the forecast band shows through. */
  @media (hover: hover) { .lv-table tbody tr:not(.lv-table__group-head, .lv-table__empty):hover > * { background-image: linear-gradient(var(--lv-row-tint), var(--lv-row-tint)); } }
  .lv-table { --lv-row-tint: color-mix(in srgb, var(--text) 4%, transparent); }
  .lv-table tbody tr[aria-selected="true"] > * { background-image: linear-gradient(var(--selection-soft), var(--selection-soft)); }
  .lv-table tbody tr[aria-selected="true"] > :first-child { font-weight: var(--weight-semibold); }

  /* Row styles. The extra .lv-table in these selectors outranks the sticky-column rule above. */
  .lv-table__row--input > * { color: var(--text); }
  .lv-table__row--derived > * { color: var(--text-secondary); }
  .lv-table .lv-table__row--derived > th:first-child { padding-left: calc(var(--lv-table-edge) + var(--space-3)); }
  .lv-table .lv-table__row--total > * { font-weight: var(--weight-semibold); color: var(--text); border-top: 1px solid var(--border-strong); }

  /* Row groups that collapse. The toggle sticks to the left edge while the table scrolls sideways. */
  .lv-table__group-head > th { padding-top: var(--space-6); border-bottom: 0; padding-bottom: var(--space-1-5); background-color: var(--surface); }
  .lv-table .lv-table__group-head > th:first-child { position: static; box-shadow: none; }
  .lv-table__group-toggle {
    position: sticky; left: var(--lv-table-edge); display: inline-flex; align-items: center; gap: var(--space-2);
    font-size: var(--text-s); font-weight: var(--weight-semibold); color: var(--text); border-radius: var(--radius-tag);
  }
  .lv-table__group-toggle::before {
    content: ''; width: 5px; height: 5px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
    transform: rotate(45deg) translateY(-2px); transition: transform var(--duration-fast) var(--ease-out);
  }
  .lv-table__group-toggle[aria-expanded="false"]::before { transform: rotate(-45deg); }
  .lv-table__group-toggle:hover { color: var(--text-secondary); }
  .lv-table__group-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
  .lv-table__group[data-collapsed] > tr:not(.lv-table__group-head) { display: none; }

  /* Sortable headers: the whole header is the button; the arrow shows the current order. */
  .lv-table__sort {
    display: inline-flex; align-items: center; gap: var(--space-1); padding: 0; font: inherit; color: inherit;
    border-radius: var(--radius-tag);
  }
  .lv-table__num .lv-table__sort { flex-direction: row-reverse; }
  .lv-table__sort::after { content: '↕'; content: '↕' / ''; font-size: .9em; color: var(--text-faint); opacity: 0; transition: opacity var(--duration-fast) var(--ease-out); }
  .lv-table__sort:hover { color: var(--text); }
  .lv-table__sort:hover::after, .lv-table__sort:focus-visible::after { opacity: 1; }
  .lv-table__sort:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
  th[aria-sort="ascending"] > .lv-table__sort, th[aria-sort="descending"] > .lv-table__sort { color: var(--text); }
  th[aria-sort="ascending"] > .lv-table__sort::after { content: '↑'; content: '↑' / ''; opacity: 1; color: var(--text); }
  th[aria-sort="descending"] > .lv-table__sort::after { content: '↓'; content: '↓' / ''; opacity: 1; color: var(--text); }
  .lv-table__sort:disabled { cursor: default; }
  .lv-table__sort:disabled::after { content: none; }

  .lv-table__empty > td { padding: var(--space-8) var(--lv-table-edge); text-align: center; color: var(--text-muted); white-space: normal; }
  .lv-table .lv-table__empty > td:first-child { position: static; box-shadow: none; }
  .lv-table tbody:last-child > tr:last-child > * { border-bottom: 0; }

  /* Phones: cap sticky first-column width so wide row labels wrap instead of hiding the numbers. */
  @media (max-width: 640px) {
    .lv-table-wrap { -webkit-overflow-scrolling: touch; }
    .lv-table :is(th, td) { padding: var(--space-2) var(--space-2-5); }
    .lv-table:not(.overview-table, .prose-table) :is(tbody, tfoot) > tr > :first-child { max-width: 44vw; white-space: normal; }
  }
}
