@import "../variables/variables";
@import "../mixins/public/utility-mixins";

/* these exclude the top/bottom padding */
$density-compact-cell-height: 64px;
$density-default-cell-height: 80px;
$density-relaxed-cell-height: 112px;
/* these exclude the 16px padding for top/bottom */
$density-compact-padding: $spacing-150;
$density-default-padding: $spacing-150;
$density-relaxed-padding: $spacing-200;
/* these are for image sizes in cells */
$density-compact-img-size: 40px;
$density-default-img-size: 56px;
$density-relaxed-img-size: 72px;
/* these are for cell widths */
$cell-max-width: 400px;
$cell-min-width: 124px;
$frozen-column-max-width: 124px;
/* these are for image radii in cells */
$density-compact-img-radius: 4px;
$density-default-img-radius: 8px;
$density-relaxed-img-radius: 8px;

.table {
    overflow-x: auto;
    position: relative;
}

@include scrollbars-permanent(".table");

.table table {
    border-collapse: separate;
    border-spacing: 0;
    width: 100%;
}

.table th,
.table td {
    // explicit background is needed for sticky columns
    background-color: var(--color-background-primary);
    border-bottom: 1px solid var(--color-stroke-subtle);
    box-sizing: border-box;
    height: $density-default-cell-height;
    max-height: $density-default-cell-height;
    max-width: $cell-max-width;
    min-width: $cell-min-width;

    // minimal padding to prevent text from touching the border
    padding: $density-default-padding;
}

.table th {
    white-space: nowrap;
}

.table--density-compact th,
.table--density-compact td {
    height: $density-compact-cell-height;
    max-height: $density-compact-cell-height;
    padding: $density-compact-padding;
}

.table--density-relaxed th,
.table--density-relaxed td {
    height: $density-relaxed-cell-height;
    max-height: $density-relaxed-cell-height;
    padding: $density-relaxed-padding;
}

.table--frozen-header thead {
    position: sticky;
    top: 0;
    z-index: 3;
}

.table--freeze-column-1 tr th:nth-child(-n + 1),
.table--freeze-column-1 tr td:nth-child(-n + 1) {
    left: 0;
    max-width: $frozen-column-max-width;
    position: sticky;
}

.table--freeze-column-2 tr th:nth-child(-n + 2),
.table--freeze-column-2 tr td:nth-child(-n + 2) {
    left: 0;
    max-width: $frozen-column-max-width;
    position: sticky;
}

.table--freeze-column-3 tr th:nth-child(-n + 3),
.table--freeze-column-3 tr td:nth-child(-n + 3) {
    left: 0;
    max-width: $frozen-column-max-width;
    position: sticky;
}

.table--full-height {
    max-height: unset;
}

.table--constrained-height {
    max-height: 90vh;
}

.table th button,
.table th a {
    align-items: center;
    display: flex;
    font-weight: bold;
    text-decoration: none;
}

// last row shouldn't have a bottom border
.table tbody tr:last-child th,
.table tbody tr:last-child td {
    border-bottom: none;
}

.table-cell {
    text-align: left;
}

.table-cell--numeric {
    text-align: right;
}

.table__cell--numeric {
    text-align: right;
}

.table-cell--icon-action {
    text-align: center;
    width: 64px;
}

.table [data-type="numeric"] {
    text-align: right;
}

.table-cell img {
    max-height: $density-default-img-size;
    max-width: $density-default-img-size;
}

.table--density-compact .table-cell img {
    max-height: $density-compact-img-size;
    max-width: $density-compact-img-size;
}

.table--density-relaxed .table-cell img {
    max-height: $density-relaxed-img-size;
    max-width: $density-relaxed-img-size;
}

.table th[scope="row"] {
    text-align: left;
}

.table th:last-child,
.table td:last-child {
    padding-inline-end: 0;
}

.table--loading-state {
    overflow: hidden;
}

.table--loading-state table {
    opacity: 0.15;
    user-select: none;
}

.table--loading-state img,
.table--loading-state .signal {
    filter: var(--color-media-disabled-filter);
}

.table--loading-state a {
    cursor: default;
    pointer-events: none;
    text-decoration: none;
}

.table--loading-state .progress-bar-expressive {
    bottom: calc(50%);
    left: 0;
    position: absolute;
    right: 0;
}

.table--mode-selection th:first-child,
.table--mode-selection td:first-child {
    min-width: 48px;
    text-align: center;
    width: 48px;
}
/* Not sure of downstream dev consumers' ability to utilize this.
   Convert to long-hand if problematic. */
.table--mode-selection tr:has(input:checked) :where(td, th) {
    background-color: var(--color-background-secondary);
}

.table tbody th {
    font-weight: normal;
}

.table thead button {
    background-color: initial;
    border: 1px solid transparent;
    border-radius: 10px;
    box-sizing: border-box;
    color: inherit;
    display: inline-block;
    font-family: inherit;
    font-size: var(--font-size-default);
    margin: 0;
    min-height: 40px;
    min-width: auto;
    padding: 3px;
    text-align: center;
    text-decoration: none;
    /* kick in GPU rendering to avoid trailing artifacts */
    transform: translate3d(0, 0, 0);
    vertical-align: initial;
    white-space: nowrap;
}

.table-cell__data {
    line-height: var(--spacing-250);
    max-width: 400px;
    min-width: 124px;
}

.table-cell__data--secondary {
    color: var(--color-foreground-secondary);
    font-size: var(--font-size-12);
    line-height: var(--spacing-200);
}

.table-cell__thumbnail {
    align-items: center;
    background-color: var(--color-background-tertiary);
    border-radius: $density-default-img-radius;
    display: flex;
    flex-wrap: nowrap;
    height: $density-default-img-size;
    justify-content: center;
    overflow: hidden; // to round square images
    text-align: center;
    width: $density-default-img-size;
}

.table--density-compact .table-cell__thumbnail {
    border-radius: $density-compact-img-radius;
    height: $density-compact-img-size;
    width: $density-compact-img-size;
}

.table--density-relaxed .table-cell__thumbnail {
    border-radius: $density-relaxed-img-radius;
    height: $density-relaxed-img-size;
    width: $density-relaxed-img-size;
}

.table-cell__layout {
    align-items: center;
    display: flex;
    gap: var(--spacing-200);
    width: max-content;
}

.table-cell__layout > * {
    flex-shrink: 0;
}

.table-cell__multiline {
    max-height: $density-default-cell-height;
    max-width: $cell-max-width - $density-default-cell-height - $spacing-200;
}

.table--density-compact .table-cell__multiline {
    max-height: $density-compact-cell-height;
    max-width: $cell-max-width - $density-compact-cell-height - $spacing-200;
}

.table--density-relaxed .table-cell__multiline {
    max-height: $density-relaxed-cell-height;
    max-width: $cell-max-width - $density-relaxed-cell-height - $spacing-200;
}

.table-cell__layout .table-cell__multiline .table-cell__data {
    margin-block-end: var(--spacing-50);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.table-cell .textbox,
.table-cell .textbox__control {
    width: 100%;
}

.table-cell--numeric .textbox__control {
    text-align: right;
}

.table-cell .signal {
    white-space: nowrap;
}

[dir="rtl"] {
    .table th[scope="row"] {
        text-align: right;
    }

    .table-cell {
        text-align: right;
    }

    .table-cell--numeric {
        text-align: left;
    }
}
