/**
 * @file table.css
 * @component Table (Data)
 * @description Owns the native table, its caption and its overflow wrapper.
 * Sizes are fixed pixels on a 16px type base, so the table renders at the same
 * size whatever root font size the theme sets. Publishes the density, striping
 * and fill state that Table Row and Table Cell read.
 *
 * @section Public custom properties
 * None.
 *
 * @section Private custom properties
 * --_length-maximumWidth : Selected wrapper maximum; defaults to 100%.
 * --_number-columnWidth  : Validated percentage on each owned col; root reset zero.
 */
:where([data-component-id="marsal_sdc:table"]) {
  --_length-maximumWidth: 100%;
  --_number-columnWidth: 0;

  min-inline-size: 0;
  max-inline-size: min(100%, var(--_length-maximumWidth));
  overflow: auto;
  overscroll-behavior-inline: contain;

  /* Browsers focus the wrapper only when the table actually overflows. */
  &:focus-visible {
    outline: 2px solid var(--marsal-color-focusRing);
    outline-offset: 2px;
    box-shadow: 0 0 0 2px var(--marsal-color-focusRingContrast);
  }

  /* The native table sets the 16px type base that the caption and Table Cells size from. */
  & > .marsal-table__table {
    inline-size: 100%;
    margin-block: 0;
    table-layout: fixed;
    border-collapse: collapse;
    background-color: var(--marsal-color-surfacePrimary);
    font-size: 16px;
    line-height: 1.5;

    & > .marsal-table__caption {
      background-color: var(--marsal-color-surfaceBrand);
      color: var(--marsal-color-textOnBrand);
      font-family: var(--marsal-fontFamily-label-standard);
      font-size: 1em;
      font-weight: 700;
      line-height: 1.5;
      letter-spacing: 0;
      text-align: start;
      caption-side: top;
      overflow-wrap: anywhere;
      padding-block: 12px;
      padding-inline: 16px;

      /* Formatted text, such as a text field's paragraph, sits flush with the caption padding. */
      & > :first-child {
        margin-block-start: 0;
      }

      & > :last-child {
        margin-block-end: 0;
      }
    }

    & > .marsal-table__columns > .marsal-table__column {
      inline-size: calc(var(--_number-columnWidth) * 1%);
    }
  }
}

:where([data-component-id="marsal_sdc:table"][data-maximum-width="narrow"]) {
  --_length-maximumWidth: 384px;
}

:where([data-component-id="marsal_sdc:table"][data-maximum-width="standard"]) {
  --_length-maximumWidth: 512px;
}

:where([data-component-id="marsal_sdc:table"][data-maximum-width="wide"]) {
  --_length-maximumWidth: 768px;
}

:where([data-component-id="marsal_sdc:table"][data-elevated="true"]) {
  box-shadow: 0 0 20px rgb(0 0 0 / 15%);

  &:focus-visible {
    box-shadow: 0 0 0 2px var(--marsal-color-focusRingContrast), 0 0 20px rgb(0 0 0 / 15%);
  }
}

/*
 * The native table paints its own Primary surface, so it restores the neutral
 * roles its cells read, even inside a dark Background surface. The wrapper
 * keeps the inherited roles for its focus ring, which sits outside the table.
 */
[data-component-id="marsal_sdc:table"] > .marsal-table__table {
  --marsal-color-textPrimary: var(--marsal-color-textPrimaryTheme);
  --marsal-color-textStrong: var(--marsal-color-textStrongTheme);
  --marsal-color-textMuted: var(--marsal-color-textMutedTheme);
  --marsal-color-textBrand: var(--marsal-color-textBrandTheme);
  --marsal-color-link: var(--marsal-color-linkTheme);
  --marsal-color-borderSubtle: var(--marsal-color-borderSubtleTheme);
  --marsal-color-borderDefault: var(--marsal-color-borderDefaultTheme);
  --marsal-color-borderControl: var(--marsal-color-borderControlTheme);
  --marsal-color-borderInteractive: var(--marsal-color-borderInteractiveTheme);
  --marsal-color-borderAccent: var(--marsal-color-borderAccentTheme);
  --marsal-color-dividerAccent: var(--marsal-color-dividerAccentTheme);
  --marsal-color-focusRing: var(--marsal-color-focusRingTheme);
  --marsal-color-focusRingContrast: var(--marsal-color-focusRingContrastTheme);
}

:where([data-component-id="marsal_sdc:table"][data-density="compact"]) > .marsal-table__table > .marsal-table__caption {
  padding-block: 6px;
  padding-inline: 8px;
}

:where([data-component-id="marsal_sdc:table"][data-density="spacious"]) > .marsal-table__table > .marsal-table__caption {
  padding-block: 16px;
  padding-inline: 24px;
}

@media screen and (max-width: 28rem) {
  :where([data-component-id="marsal_sdc:table"]:not([data-density="compact"])) > .marsal-table__table > .marsal-table__caption {
    padding-inline: 9px;
  }
}

/* Fill engages only where Flex column_to_row places tables side by side. */
@media screen and (min-width: 55.25rem) {
  :where([data-component-id="marsal_sdc:table"][data-fill-height="true"]) {
    display: flex;
    min-block-size: 100%;
  }
}

@media print {
  :where([data-component-id="marsal_sdc:table"][data-elevated="true"]) {
    box-shadow: none;
  }

  :where([data-component-id="marsal_sdc:table"][data-fill-height="true"]) {
    display: flex;
    min-block-size: 100%;
  }

  :where([data-component-id="marsal_sdc:table"]) > .marsal-table__table {
    break-inside: avoid;
  }
}
