/* TABLES AND DASHBOARDS */

table {
    width: max-content;
    min-width: 100%;
    table-layout: fixed;
    border-spacing: 0;
    border-collapse: separate;

    &[data-resizable-table] {
        table-layout: auto;

        th.table-column-capped > section,
        td.table-column-capped > div {
            max-width: max(0px, calc(var(--table-column-max-width) - 2 * var(--space-sm) - 1px));
            overflow: hidden;
        }

        th.table-column-fixed,
        td.table-column-fixed {
            width: var(--table-column-width);
            min-width: var(--table-column-width);
            max-width: var(--table-column-width);

            > section,
            > div {
                width: max(0px, calc(var(--table-column-width) - 2 * var(--space-sm) - 1px));
                overflow: hidden;
            }
        }
    }

    th, td {
        padding: var(--space-xs) var(--space-sm);
        vertical-align: middle;
        text-align: left;
        background-color: var(--white);
        border-right: 1px solid color-mix(in srgb, var(--light) 80%, var(--white));
        border-bottom: 1px solid color-mix(in srgb, var(--light) 80%, var(--white));

        &[data-sticky-column='left'] {
            position: sticky;
            left: 0;
            z-index: 1;
            background-color: var(--white);
        }

        &[data-sticky-column='right'] {
            position: sticky;
            right: 0;
            z-index: 1;
            background-color: var(--white);
            border-left: 1px solid color-mix(in srgb, var(--light) 80%, var(--white));
        }

        &:last-child { border-right: none; }

        &[data-text-side='center'] {
            text-align: center;
        }
    }

    td {
        &[data-text-side='right'] {
            font-family: 'Roboto Mono', ui-monospace, SFMono-Regular, Consolas, monospace;
            font-variant-numeric: tabular-nums;
            text-align: right;
        }
    }

    .table-empty-row td,
    .table-load-row td {
        color: var(--dark);
        text-align: center;
        border-bottom: none;
    }

    thead th {
        position: sticky;
        top: 0;
        z-index: 4;
        white-space: nowrap;
        background-color: color-mix(in srgb, var(--secondary) 10%, var(--white));
        border-right: 1px solid color-mix(in srgb, var(--dark) 40%, var(--white));

        &[data-sticky-column='left'],
        &[data-sticky-column='right'] {
            z-index: 5;
            background-color: color-mix(in srgb, var(--secondary) 10%, var(--white));
        }

        main {
            > button {
                cursor: pointer;
                &.active { color: var(--secondary); }
                > span {
                    display: block;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }
        }

        &[data-text-side='center'] > div,
        &[data-text-side='right'] > div {
            text-align: inherit;
        }

        aside {
            > button {
                cursor: pointer;
                &.active { color: var(--secondary); }
                > span {
                    display: inline-flex;
                    align-items: center;
                    justify-content: center;
                    min-width: 1.2rem;
                    min-height: 1.2rem;
                    padding: 0 0.2rem;
                    font-size: var(--font-size-xs);
                    background-color: color-mix(in srgb, var(--secondary) 20%, var(--white));
                    border-radius: var(--radius-pill);
                }
            }
            > section {
                right: anchor(right);
                left: unset;
                min-width: 320px;
                position-try-fallbacks: --bottom-right, --bottom-left, --top-right, --top-left;

                label {
                    display: flex;
                    gap: var(--space-xs);
                    align-items: center;
                    padding: var(--space-xs);
                    cursor: pointer;
                    border-radius: var(--radius-xs);

                    &:hover {
                        background-color: color-mix(in srgb, var(--secondary) 20%, var(--white));
                    }
                }
            }
        }

        [data-column-resizer] {
            position: absolute;
            top: 0;
            right: 0;
            width: var(--space-sm);
            height: 100%;
            touch-action: none;
            cursor: col-resize;
            border-right: 2px solid color-mix(in srgb, var(--secondary) 40%, var(--white));
        }
    }

    tbody tr {
        &:nth-child(even) td { background-color: color-mix(in srgb, var(--light) 20%, var(--white)); }
    }
}

.static-table--import-results {
    th:first-child,
    td:first-child {
        min-width: 5rem;
    }

    th:last-child,
    td:last-child {
        min-width: 18rem;
        max-width: 32rem;
        white-space: normal;
        overflow-wrap: anywhere;
    }
}

.static-table-scroll {
    max-height: min(60vh, 36rem);
}

.table {
    width: 100%;
    min-height: 0;

    &.htmx-request [data-table-summary],
    &.htmx-request [data-table-active-filters] {
        display: none;
    }

    &.htmx-request .table-request-indicator {
        display: inline-flex;
    }

    &:not(:has([data-resizable-table])) .table-request-indicator {
        display: none;
    }

    &.height-full { height: 100%; }

    &:fullscreen,
    &.is-maximized {
        display: flex;
        flex-direction: column;
        height: 100%;
        background-color: var(--white);

        > form { flex: 1 1 auto; }
    }

    &.is-maximized {
        position: fixed;
        inset: var(--space-sm);
        z-index: var(--z-index-modal);
        width: calc(100vw - 2 * var(--space-sm));
        height: calc(100dvh - 2 * var(--space-sm));
        margin: 0;
        box-shadow: var(--shadow-lg);
    }

    * { scrollbar-width: thin; }

    &.table-resizing,
    &.table-resizing * {
        cursor: col-resize !important;
        user-select: none !important;
    }

    > form {
        display: flex;
        flex-direction: column;
        height: 100%;
        min-height: 0;
        overflow: hidden;
        background-color: var(--white);
        border-radius: var(--radius-md);

        > main {
            flex: 1 1 auto;
            min-height: 0;

            > section {
                height: 100%;
                max-height: none;
                overflow: auto;
                overscroll-behavior: contain;
            }
        }
    }

}

.dashboard {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: var(--space-xs) 0;
    background-color: var(--background);

    [data-dashboard-toolbar] {
        > [data-dashboard-actions] {
            flex-shrink: 0;
        }
    }

    [data-dashboard-filters] {
        flex: 1;
    }

    &.headless {
        display: none;
    }
}
