$tableResponsiveBreakpointMedium: 800px;
$tableResponsiveBreakpointSmall: 500px;

.vuiTableWrapper {
  container-type: inline-size;
  width: 100%;
  position: relative;
}

// Enable nesting tables inside other tables as expandable content.
.vuiTableRowExpandedContent__cell > .vuiTableWrapper {
  container-type: normal;
}

.vuiTableBlock {
  background-color: rgba(var(--vui-color-empty-shade-rgb), 0.4);
  height: 100%;
  left: 0;
  padding: $sizeM;
  position: absolute;
  top: 0;
  width: 100%;
}

.vuiTable {
  width: 100%;
  table-layout: fixed;

  & > thead {
    border-bottom: 1px solid var(--vui-color-border-medium);
  }

  & > tbody > tr {
    border-bottom: 1px solid var(--vui-color-border-light);

    &.vuiTableRow-isBeingActedUpon,
    &:not(.vuiTableRow--inert):hover {
      background-color: rgba(var(--vui-color-light-shade-rgb), 0.25);
      box-shadow: $shadowSmallEnd;
      border-bottom-color: transparent;
    }

    &:last-child {
      border-bottom: 1px solid var(--vui-color-border-medium);
    }

    .vuiTableCell__label {
      display: none;
      max-inline-size: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .vuiTableCell {
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: flex-start;
    }
  }

  & > thead > tr > th {
    font-size: $fontSizeStandard;
    font-weight: $fontWeightBold;
    padding: $sizeXxs;
  }

  & > tbody > tr > td {
    font-size: $fontSizeStandard;
    padding: $sizeXxs;
    vertical-align: middle;
    word-break: break-word;
  }
}

.vuiTableHeader__responsiveSelectAllCheckbox {
  display: none;
}

.vuiTableCell__label {
  font-size: $fontSizeSmall;
  font-weight: $labelFontWeight;
  color: var(--vui-color-label);
}

@container (width < #{$tableResponsiveBreakpointMedium}) {
  .vuiTableHeader__responsiveSelectAllCheckbox {
    display: flex;
  }

  // Direct descendent selectors enable non-responsive tables to
  // be nested inside of responsive tables.
  .vuiTable--responsive {
    & > thead {
      display: none;
    }

    & > tbody > .vuiTableRow--isSelectable {
      padding-left: $sizeXxl;
    }

    & > tbody > .vuiTableRow--hasActions {
      padding-right: $sizeXxl;
    }

    // This enables truncated VuiText children to actually be truncated.
    & > tbody > tr > .vuiTableCell--truncate {
      min-width: 0;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    & > tbody > tr:not(.vuiTableRowExpandedContent) {
      position: relative;
      column-gap: $sizeM;
      row-gap: $sizeM;
      display: grid;
      grid-template-columns: 1fr 1fr;
      padding-top: $sizeM;
      padding-bottom: $sizeM;
      border: none;
      border-radius: $sizeXs;
      box-shadow: $shadowLargeStart;
      margin-bottom: $sizeS;

      &.vuiTableRow--expanded {
        margin-bottom: 0;
        border-radius: $sizeXs $sizeXs 0 0;
      }

      & > td {
        padding: 0;
      }

      & > td > .vuiTableCellWrapper {
        display: flex;
        flex-direction: column;
        gap: $sizeXxs;

        & > .vuiTableCell__label {
          display: block;
        }
      }

      & > .vuiTableRowSelect {
        position: absolute;
        top: $sizeM;
        left: $sizeXs;
      }

      & > .vuiTableRowActions {
        position: absolute;
        top: $sizeXs * 1.25;
        right: $sizeXs;
      }
    }

    &.vuiTable--notSelectable {
      tbody tr:not(.vuiTableRowExpandedContent) {
        padding-left: $sizeM;
      }
    }

    &.vuiTable--noActions {
      & > tbody tr:not(.vuiTableRowExpandedContent) {
        padding-right: $sizeM;
      }
    }

    // Expanded content in responsive mode
    & > tbody > tr.vuiTableRowExpandedContent {
      display: block;
      margin-bottom: $sizeS;
      padding: $sizeL;
      border-radius: 0 0 $sizeXs $sizeXs;
      box-shadow: $shadowLargeStart;

      .vuiTableRowExpandedContent__cell {
        padding: 0;
      }
    }
  }
}

@container (width < #{$tableResponsiveBreakpointSmall}) {
  .vuiTable--responsive {
    & > tbody > tr:not(.vuiTableRowExpandedContent) {
      grid-template-columns: 1fr;
    }
  }
}

.vuiTable--verticalAlignTop {
  & > tbody > tr > td {
    vertical-align: top;
  }
}

.vuiTable--verticalAlignMiddle {
  & > tbody > tr > td {
    vertical-align: middle;
  }
}

.vuiTable--verticalAlignBottom {
  & > tbody > tr > td {
    vertical-align: bottom;
  }
}

.vuiTable--fluid {
  table-layout: auto;
}

.vuiTableActions {
  display: flex;
  justify-content: flex-end;
}

.vuiTableHeaderSelect {
  width: 32px;

  .vuiTableCellWrapper {
    text-align: left; // Align checkbox with checkboxes in table rows.
  }
}

.vuiTableHeaderActions {
  width: 42px;
}
.vuiTableHeaderActions--extended {
  width: 76px;
}

.vuiTableContent {
  height: 80px;
}

.vuiTableStickyHeader {
  position: sticky;
  top: 0;
  background-color: var(--vui-color-empty-shade);
  padding: $sizeS $sizeS 0;
  margin: 0 -$sizeS; // Preserve same table position as when sticky header is not used
  margin-bottom: $sizeS;
  z-index: 1; // Ensure it is above the table content

  &::after {
    content: "";
    position: absolute;
    bottom: -$sizeS;
    left: 0;
    width: 100%;
    height: $sizeS;
    background: linear-gradient(rgba(255, 255, 255, 1), rgba(255, 255, 255, 0));
    pointer-events: none;
  }
}

.vuiTableHeaderCell--sortable {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: $sizeXs;
  font-size: inherit;
  font-weight: inherit;
  padding: $sizeXxs;

  &:hover {
    color: var(--vui-color-primary-shade);
    text-decoration: underline;
  }
}

.vuiTableHeaderCell__icon {
  transition: 150ms ease-out;
}

.vuiTableHeaderCell__icon--desc {
  transform: rotate(180deg);
}

.vuiTableHeaderCell__icon--none {
  opacity: 0.75;
}

// Expandable rows
.vuiTableRowExpandToggle {
  display: flex;
  align-items: center;
  justify-content: center;
}

.vuiTableRow--expanded {
  border-bottom-color: transparent;
}

.vuiTableRowExpandedContent {
  background-color: rgba(var(--vui-color-light-shade-rgb), 0.25);

  &:not(:last-child) {
    border-bottom: 1px solid var(--vui-color-border-light);
  }

  &:last-child {
    border-bottom: 1px solid var(--vui-color-border-medium);
  }
}

.vuiTable .vuiTableRowExpandedContent__cell {
  padding: $sizeS $sizeM;
}
