﻿/* The one data grid design, shared by every table in the app - master lists, transaction registers,
   report bodies, document line editors.

   Denser and more ruled than a typical web table on purpose: these screens replace spreadsheets
   read across a dozen columns, so gridlines and tabular figures do more work than whitespace. */

.data-table-wrapper {
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  overflow: auto;
  background-color: var(--color-canvas-default);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;

  & thead th {
    text-align: left;
    padding: 6px 9px;
    background-color: var(--color-table-header-bg);
    color: var(--color-table-header-fg);
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 11.5px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    border: 1px solid var(--color-border-default);
    border-top: 0;
    white-space: nowrap;
    /* The header survives scrolling inside .data-table-wrapper, which matters most on long
       registers where the column you are reading is thirty rows down.

       z-index has to clear whatever a row cell contains, not just the cell itself: a row's
       overflow menu is .btn-menu, which is position:relative, and a pressed .btn-group button
       carries z-index:1 - both would otherwise paint over the header they scroll beneath. */
    position: sticky;
    top: 0;
    z-index: 2;
  }

  & tbody td {
    padding: 5px 9px;
    border: 1px solid var(--color-table-grid);
    vertical-align: middle;
  }

  & tbody tr:nth-child(even) td {
    background-color: var(--color-table-zebra);
  }

  & tbody tr:hover td {
    background-color: var(--color-neutral-subtle);
  }

  & tbody tr[data-selected="true"] td {
    background-color: var(--color-table-row-selected);
  }

  /* A voided or cancelled row is still history and must stay readable - dimmed, not hidden. */
  & tbody tr[data-voided="true"] td {
    opacity: 0.55;
    text-decoration: line-through;
    text-decoration-color: var(--color-fg-subtle);
  }

  /* Totals row. */
  & tfoot td {
    padding: 6px 9px;
    background-color: var(--color-table-foot-bg);
    color: var(--color-fg-default);
    font-family: var(--font-mono);
    font-weight: 700;
    border: 1px solid var(--color-border-default);
    /* Same reasoning as the header above - and this had no z-index at all, so a positioned
       element in the last visible row painted straight over the totals. */
    position: sticky;
    bottom: 0;
    z-index: 2;
  }

  & tfoot td.lbl {
    font-family: var(--font-display);
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
  }
}

/* Rows that navigate somewhere on click. */
.data-table tbody tr[data-clickable="true"] {
  cursor: pointer;
}

/* ---- Cell roles ------------------------------------------------------------------------------ */

/* Any cell holding a figure. Mono and right-aligned so magnitudes line up down the column. */
.data-table td.num,
.data-table th.num,
.data-table tfoot td.num {
  font-family: var(--font-mono);
  text-align: right;
  white-space: nowrap;
}

/* The row's identity column - document number, account code, employee code. */
.data-table td.lbl {
  font-weight: 600;
}

/* A secondary descriptor under or beside a label - a customer name under an invoice number. */
.data-table td.sub,
.data-table .cell-sub {
  color: var(--color-fg-muted);
  font-size: 11.5px;
}

.data-table td.code {
  font-family: var(--font-mono);
  white-space: nowrap;
}

/* A column of dates never needs to wrap and always benefits from lining up. */
.data-table td.date,
.data-table th.date {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Indented hierarchy - the chart of accounts, a report's account groupings. */
.data-table td.indent-1 { padding-left: 26px; }
.data-table td.indent-2 { padding-left: 44px; }
.data-table td.indent-3 { padding-left: 62px; }

/* A subtotal or group heading inside the body rather than in the foot. */
.data-table tbody tr.row-group td {
  background-color: var(--color-canvas-inset);
  font-weight: 600;
  border-top: 1px solid var(--color-border-default);
}

.data-table tbody tr.row-subtotal td {
  font-family: var(--font-mono);
  font-weight: 700;
  border-top: 1px solid var(--color-border-strong);
  background-color: var(--color-canvas-subtle);
}

/* ---- Semantic shading -------------------------------------------------------------------------- */
/* Driven by the same domain tokens as badges and dashboard tiles, so a cell means the same thing
   wherever it appears. */

.data-table td.good {
  background-color: var(--color-success-subtle) !important;
  color: var(--color-success-fg);
  font-weight: 600;
}

.data-table td.warn {
  background-color: var(--color-attention-subtle) !important;
  color: var(--color-attention-fg);
  font-weight: 600;
}

.data-table td.bad {
  background-color: var(--color-danger-subtle) !important;
  color: var(--color-danger-fg);
  font-weight: 600;
}

/* Aging heat ramp, current through 90+ days. */
.data-table td[data-aging="0"] { background-color: var(--color-aging-0); }
.data-table td[data-aging="1"] { background-color: var(--color-aging-1) !important; }
.data-table td[data-aging="2"] { background-color: var(--color-aging-2) !important; }
.data-table td[data-aging="3"] { background-color: var(--color-aging-3) !important; }
.data-table td[data-aging="4"] { background-color: var(--color-aging-4) !important; font-weight: 600; }

.data-table-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  white-space: nowrap;
}

/* ---- Sortable headers --------------------------------------------------------------------------- */

.data-table th.sortable {
  cursor: pointer;
  user-select: none;

  &:hover {
    color: var(--color-fg-default);
  }

  & .sort-glyph {
    margin-left: 4px;
    font-size: 9px;
    opacity: 0.45;
  }

  &[aria-sort="ascending"] .sort-glyph,
  &[aria-sort="descending"] .sort-glyph {
    opacity: 1;
    color: var(--color-accent-fg);
  }
}

/* ---- Density variants ----------------------------------------------------------------------------- */

/* For tables in the context rail, a dialog, or a document's line list. */
.data-table[data-density="compact"] {
  font-size: 12px;

  & thead th { padding: 4px 7px; }
  & tbody td { padding: 3px 7px; }
  & tfoot td { padding: 4px 7px; }
}

/* For a report meant to be read rather than scanned - more air, fewer rules. */
.data-table[data-density="report"] {
  font-size: 13px;

  /* Opaque, not transparent, even though this density is meant to read as airy. These cells are
     still position:sticky from the base rule, and a transparent sticky cell does not hide what
     scrolls under it - the rows show straight through and the header's own text double-prints over
     them. Matching .data-table-wrapper's own canvas keeps the intended look exactly: against a
     stationary table it is indistinguishable from transparent, and it occludes once it scrolls. */
  & thead th {
    background-color: var(--color-canvas-default);
    border: 0;
    border-bottom: 2px solid var(--color-border-strong);
    color: var(--color-fg-muted);
  }

  & tbody td {
    border: 0;
    border-bottom: 1px solid var(--color-border-muted);
    padding: 6px 10px;
  }

  & tbody tr:nth-child(even) td {
    background-color: transparent;
  }

  /* Opaque for the same reason as the header - see above. */
  & tfoot td {
    background-color: var(--color-canvas-default);
    border: 0;
    border-top: 2px solid var(--color-border-strong);
  }
}

/* ---- Pager --------------------------------------------------------------------------------------- */

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--color-fg-muted);

  & .pager-controls {
    display: flex;
    align-items: center;
    gap: 4px;
  }

  & .pager-page {
    font-family: var(--font-mono);
    padding: 0 8px;
  }
}
