/* <sg-data-table>. Without JavaScript this is the quiet look: a native table,
   ruled off, that reads in full in the order it was written. With it, the table
   scrolls inside its own wrapper at phone width, the sortable columns are real
   buttons, and td[data-money] is rewritten with the Indian grouping. */

sg-data-table { display: block; color: var(--sg-text); font: var(--sg-step-0) / var(--sg-leading-body) var(--sg-font-body); }
sg-data-table[hidden] { display: none !important; }

/* the scroll region: the table scrolls inside it, the page never does */
sg-data-table .sg-table-wrap {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
sg-data-table .sg-table-wrap:focus-visible {
  outline: var(--sg-focus-width) solid var(--sg-focus);
  outline-offset: var(--sg-focus-offset);
}
sg-data-table .sg-table-wrap { position: relative; }

/* the table itself */
sg-data-table table {
  min-width: max-content;
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  font-variant-numeric: tabular-nums lining-nums;
}
sg-data-table caption {
  text-align: start;
  font: var(--sg-weight-medium) var(--sg-step-1) / var(--sg-leading-snug) var(--sg-font-display);
  padding: 0 0 var(--sg-space-3);
  color: var(--sg-text);
}
sg-data-table th, sg-data-table td {
  text-align: start;
  padding: var(--sg-space-3) var(--sg-space-4);
  border-bottom: var(--sg-hairline) solid var(--sg-rule);
  vertical-align: baseline;
}
sg-data-table thead th {
  font: var(--sg-weight-medium) var(--sg-step--1) / 1.3 var(--sg-font-body);
  font-variant-caps: all-small-caps;
  letter-spacing: 0.08em;
  color: var(--sg-text-soft);
  border-bottom: var(--sg-stroke-bold) solid var(--sg-rule-strong);
  white-space: nowrap;
}
sg-data-table tbody tr:last-child :is(th, td) { border-bottom: 0; }
/* money and number columns sit on the right, on the tabular figures */
sg-data-table td[data-money], sg-data-table th[data-money-col], sg-data-table td[data-value] { text-align: end; }
sg-data-table td[data-money] { color: var(--sg-text); font-weight: var(--sg-weight-medium); }

/* the sort button: the platform's focus, Enter and Space */
sg-data-table .sg-table-sort {
  all: unset;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  font-variant-caps: inherit;
  min-height: 2rem;
  border-radius: 2px;
}
sg-data-table .sg-table-sort:hover { color: var(--sg-text); }
sg-data-table .sg-table-sort:focus-visible { outline: var(--sg-focus-width) solid var(--sg-focus); outline-offset: 2px; }
/* the arrow: a shape as well as aria-sort, never colour alone */
sg-data-table .sg-table-sort::after {
  content: "";
  width: 0.5em; height: 0.5em;
  border-inline-end: var(--sg-stroke-bold) solid currentColor;
  border-block-end: var(--sg-stroke-bold) solid currentColor;
  transform: rotate(45deg) translateY(-0.1em);
  opacity: 0;
}
sg-data-table th[aria-sort] .sg-table-sort::after { opacity: 1; }
sg-data-table th[aria-sort="ascending"] .sg-table-sort::after { transform: rotate(-135deg) translateY(-0.1em); }
sg-data-table th[aria-sort="descending"] .sg-table-sort::after { transform: rotate(45deg) translateY(-0.1em); }
sg-data-table th[aria-sort] { color: var(--sg-text); }

/* generous space, hairline rules, zero ornament: quiet is everything above */

/* warm: the hand-drawn rule under the header, drawn by the skin */
sg-data-table[data-skin="warm"] .sg-table-ink,
sg-data-table[data-skin="playful"] .sg-table-ink {
  position: absolute;
  z-index: 1;
  pointer-events: none;
  overflow: visible;
  fill: none;
  stroke: var(--sg-rule-strong);
  stroke-linecap: round;
}
sg-data-table[data-skin="warm"] caption,
sg-data-table[data-skin="playful"] caption { font-family: var(--sg-font-hand); font-weight: 400; }

/* playful: colour and delight, a tinted zebra and a coloured sort */
sg-data-table[data-skin="playful"] tbody tr:nth-child(even) { background: color-mix(in oklch, var(--sg-accent) 7%, transparent); }
sg-data-table[data-skin="playful"] th[aria-sort] .sg-table-sort { color: var(--sg-accent-text); }
sg-data-table[data-skin="playful"] th[aria-sort] .sg-table-sort::after { border-color: var(--sg-accent-text); }
/* money stays in ink in every register: red figures in a ledger read as owed or lost */
sg-data-table[data-skin="playful"] caption { color: var(--sg-accent-text); }

@media (prefers-reduced-motion: reduce) {
  sg-data-table .sg-table-sort { transition: none; }
}

@media (forced-colors: active) {
  sg-data-table th, sg-data-table td { border-color: CanvasText; }
  sg-data-table .sg-table-sort::after { border-color: CanvasText; }
}

/* ── arrivals: the tala stagger ── An item that has not been seen plays its
   arrival once, on the teental cycle: sixteen beats of --sg-tala-beat, nothing
   landing on the three khali beats (8 to 10). This is talaDelay() in tokens.js
   written in CSS (arrive.test.js keeps the two agreeing); where sibling-index()
   is missing, core/arrive.js hands each item the same delay. Only the first five
   items have a beat of their own (the ones whose beat is inside the 600 ms cap);
   every later item takes the fifth's (520 ms), so a long list cascades top to
   bottom, the tail lands with the fifth, and nothing waits longer than 520 ms. Only arrivals play: arrive.js marks an item
   data-seen when it has, so a sort or a re-append never does. Quiet gets none
   (data-arrive is absent), and neither does reduced motion. */
@keyframes sg-arrive { from { opacity: 0; translate: 0 var(--sg-arrive-rise, 0.5rem); scale: var(--sg-arrive-scale, 1); } }
@media (prefers-reduced-motion: no-preference) {
  sg-data-table[data-arrive] tbody tr:not([data-seen]) {
    animation: sg-arrive var(--sg-dur-calm) var(--sg-ease-out) backwards;
    animation-delay: var(--sg-arrive-delay, 0ms);
  }
  sg-data-table[data-arrive="full"] tbody tr:not([data-seen]) {
    --sg-arrive-rise: 0.9rem; --sg-arrive-scale: 0.96;
    animation-duration: var(--sg-dur-slow); animation-timing-function: var(--sg-ease-spring);
  }
  @supports (width: calc(sibling-index() * 1px)) {
    sg-data-table[data-arrive] tbody tr:not([data-seen]) {
      --sg-arrive-n: calc(sibling-index() - 1);
      --sg-arrive-k: min(var(--sg-arrive-n), 4);
      --sg-arrive-m: mod(var(--sg-arrive-k), 13);
      animation-delay: var(--sg-arrive-delay, calc(var(--sg-tala-beat) * (16 * round(down, var(--sg-arrive-k) / 13, 1) + var(--sg-arrive-m) + 3 * round(down, var(--sg-arrive-m) / 8, 1))));
    }
  }
}

/* ── the touch contract: a press answers within a frame ── No transition on the
   way in, so :active lands on the next frame; the way out eases back in under
   150 ms. Quiet settles 1 px, warm shades the colour toward the ink, playful
   gives a little and is let go on the spring. Reduced motion keeps the colour
   and drops the movement; a key held down does not move anything. */
sg-data-table :is(.sg-table-sort):not([aria-disabled="true"], :disabled) {
  transition: translate 120ms var(--sg-ease-out), scale 120ms var(--sg-ease-out), background-color 120ms var(--sg-ease-out);
}
sg-data-table[data-skin="playful"] :is(.sg-table-sort):not([aria-disabled="true"], :disabled) { transition-timing-function: var(--sg-ease-spring); }
sg-data-table :is(.sg-table-sort) { --press-base: transparent; }
sg-data-table :is(.sg-table-sort):not([aria-disabled="true"], :disabled):active {
  transition: none; translate: 0 1px;
  background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 10%);
}
sg-data-table[data-skin="warm"] :is(.sg-table-sort):not([aria-disabled="true"], :disabled):active { translate: none; background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 25%); }
sg-data-table[data-skin="playful"] :is(.sg-table-sort):not([aria-disabled="true"], :disabled):active {
  translate: none; scale: 0.97;
  background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 14%);
}
@media (prefers-reduced-motion: reduce) {
  sg-data-table :is(.sg-table-sort):active { translate: none !important; scale: none !important; transition: none; }
}
:root[data-sg-input="key"] sg-data-table :is(.sg-table-sort):active { translate: none !important; scale: none !important; }
