:root {
    --dl-table-header-cell-vertical-align: top;
    --dl-table-cell-vertical-align: top;
    --dl-table-footer-cell-vertical-align: top;
    --dl-table-sort-icon-space: 18px;
}

@mixin stickyCellBg() {
    &:before {
        content: '';
        position: absolute;
        top: 0;
        width: calc(100% + 1.99px);
        left: var(--dl-table-cell-border-offset, -1px);
        height: 100%;
        pointer-events: none;
    }
}

@mixin tableSize() {
    &_size {
        &_l {
            --dl-table-row-height: 30px;
            --dl-table-font-size: 13px;
            --dl-table-cell-vertical-padding: 5px;
            --dl-table-cell-horizontal-padding: 10px;
            --dl-table-cell-bar-height: 19px;
            --chartkit-bg-bar-offset-width: 10px;
            line-height: 16px;
        }

        &_m {
            --dl-table-row-height: 24px;
            --dl-table-font-size: 12px;
            --dl-table-cell-vertical-padding: 4px;
            --dl-table-cell-horizontal-padding: 6px;
            --chartkit-bg-bar-offset-width: 6px;
            --dl-table-cell-bar-height: 17px;
            line-height: 15px;
        }

        &_s {
            --dl-table-row-height: 20px;
            --dl-table-font-size: 11px;
            --dl-table-cell-vertical-padding: 3px;
            --dl-table-cell-horizontal-padding: 4px;
            --chartkit-bg-bar-offset-width: 4px;
            --dl-table-cell-bar-height: 15px;
            line-height: 13px;
        }
    }
}

.dl-table {
    border-collapse: collapse;
    min-width: 100%;
    width: max-content;

    &__table-wrapper {
        width: 100%;

        &_highlight-rows {
            tbody tr:not(.dl-table__tr_no-data):hover td:not(.dl-table__td_pinned),
            tbody tr:not(.dl-table__tr_no-data):hover td:before {
                background: var(--g-color-base-simple-hover);
            }
        }

        @include tableSize();
    }

    &__title {
        font-weight: 700;
        padding: 5px 40px 5px 0;
        text-align: right;
    }

    &__th {
        text-align: left;
    }

    &__th,
    &__td {
        border: 1px solid var(--dl-table-border-color);
        border-top: 0;
        border-left: 0;
        font-size: var(--dl-table-font-size);

        &_clickable {
            cursor: pointer;
        }

        &_align_left {
            text-align: left;
            justify-content: flex-start;
        }

        &_align_center {
            text-align: center;
            justify-content: center;
        }

        &_type_number {
            text-align: right;
            justify-content: flex-end;
        }
    }

    &__th-content {
        display: inline-block;

        &_sortable {
            position: relative;
            padding-right: var(--dl-table-sort-icon-space);
            height: fit-content;
        }
    }

    &__cell-content {
        display: flex;
        min-width: 100%;
        height: 100%;
        justify-content: inherit;
        align-items: inherit;

        &_type_null {
            font-style: italic;
            opacity: 0.5;
        }
    }

    &__header {
        border-left: 1px solid var(--dl-table-border-color);

        /* stylelint-disable */
        @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
            top: 0.5px;
        }
        /* stylelint-enable */

        &_sticky {
            top: 0;
            z-index: 1;
            position: sticky;
            background: var(--dl-table-header-bg-color);

            th {
                @include stickyCellBg();
            }

            th:before {
                border-bottom: 1px solid var(--dl-table-border-color);
            }

            tr:first-child th:before {
                border-top: 1px solid var(--dl-table-border-color);
            }
        }
    }

    &__header &__th {
        position: relative;
        font-weight: 500;
        padding: var(--dl-table-cell-vertical-padding) var(--dl-table-cell-horizontal-padding);
        vertical-align: var(--dl-table-header-cell-vertical-align);
        box-sizing: border-box;
        height: 100%;
        min-height: var(--dl-table-row-height);
        border-bottom: 0;
    }

    &__header &__sort-icon {
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
    }

    &__body {
        border-left: 1px solid var(--dl-table-border-color);
    }

    &__body &__tr {
        height: var(--dl-table-row-height);
        td {
            min-height: var(--dl-table-row-height);
        }

        &_no-data {
            background: var(--dl-table-no-data-bg-color);
        }
    }

    &__body &__td {
        padding: var(--dl-table-cell-vertical-padding) var(--dl-table-cell-horizontal-padding);
        text-overflow: ellipsis;
        vertical-align: var(--dl-table-cell-vertical-align);
    }

    &__footer {
        border-left: 1px solid var(--dl-table-border-color);
        position: relative;
        z-index: 0;
    }

    &__footer &__td {
        min-height: var(--dl-table-row-height);
        padding: 0;
    }

    &__footer-cell-content {
        display: flex;
        justify-content: inherit;
        width: 100%;
        height: 100%;
        padding: var(--dl-table-cell-vertical-padding) var(--dl-table-cell-horizontal-padding);
        text-overflow: ellipsis;
        vertical-align: var(--dl-table-footer-cell-vertical-align);
        font-weight: 500;
    }

    &__footer .chartkit-table-bar {
        height: fit-content;
    }

    &__header &__th_pinned,
    &__body &__td_pinned,
    &__footer &__td_pinned {
        left: 0;
        position: sticky;
        z-index: 1;

        @include stickyCellBg();
        &:before {
            border-left: 1px solid var(--dl-table-border-color);
            z-index: -1;
        }
    }

    &__shadow {
        position: absolute;
        top: 0px;
        pointer-events: none;
        left: 100%;
        border-left: 1px solid var(--g-color-text-hint);
        width: 6px;
        height: calc(100% + 1px);
        background: linear-gradient(90deg, var(--g-color-sfx-shadow-light), transparent);
    }

    &__header &__shadow {
        height: 100%;
    }

    &__no-data {
        padding: var(--dl-table-cell-vertical-padding) var(--dl-table-cell-horizontal-padding);
        background: var(--dl-table-no-data-bg-color);
        border: 1px solid var(--dl-table-border-color);
        min-height: var(--dl-table-row-height);
        align-items: flex-start;
        display: flex;
        // for compatibility with the old table (delete after switching to a new plugin)
        margin-top: -1px;
    }

    colgroup {
        display: none;
    }

    &__background-table {
        position: absolute;
        top: -100000px;
        left: -100000px;
        z-index: -1;
        overflow: auto;
        font-size: var(--g-text-body-1-font-size);
        user-select: none;

        @include tableSize();

        table {
            min-width: auto;
        }

        tbody td {
            white-space: nowrap;
        }

        &_preWrap tbody td {
            white-space: pre-wrap;
        }

        .dl-table__th-content {
            width: min-content;
        }
    }

    &__diff-cell {
        display: flex;
        gap: 4px;
    }

    &__diff-cell-icon {
        font-size: 11px;

        &_inc {
            color: #008000;

            &:before {
                content: '▲';
            }
        }

        &_dec {
            color: #ff0000;

            &:before {
                content: '▼';
            }
        }
    }
}

table.dl-table_prepared {
    thead {
        display: grid;
        position: sticky;
        top: 0;
        z-index: 1;

        tr {
            display: contents;
        }
    }

    tbody {
        display: grid;
        position: relative;

        tr {
            display: contents;
        }
    }

    tfoot {
        display: grid;

        tr {
            display: contents;
        }
    }

    td,
    th {
        display: flex;
    }
}
