@import (once) "../../include/vars";
@import (once) "../../include/mixins";

:root {
    --table-inspector-border-radius: 10px;
    --table-head-font-size: 14px;
    --table-caption-font-size: 16px;
    --table-footer-font-size: 14px;
    --table-body-font-size: 16px;
    --table-border-radius: 6px;

    --table-border-color: #e8e8e8;
    --table-color: #191919;
    --table-background: transparent;
    --table-header-background: #fafafa;
    --table-header-color: #191919;
    --table-sortable-background: #e8e8e8;
    --table-sortable-color: #000000;
    --table-selected-background: #d4e2ff;
    --table-selected-color: #000000;
    --table-striped-background: #f8f8f8;
    --table-hover-background: #eaeaea;
    --table-hover-color: #191919;
    --table-inspector-background: #ffffff;
    --table-inspector-border-color: #e8e8e8;
    --table-inspector-color: #191919;
}

.dark-side {
    --table-border-color: #4a4d51;
    --table-color: #dbdfe7;
    --table-background: transparent;
    --table-header-background: #1b1b1e;
    --table-header-color: #efefef;
    --table-sortable-background: #26282e;
    --table-sortable-color: #ffffff;
    --table-selected-background: #2e436e;
    --table-selected-color: #ffffff;
    --table-striped-background: #26282e;
    --table-hover-background: #323437;
    --table-hover-color: #ffffff;
    --table-inspector-background: #26282e;
    --table-inspector-border-color: #4a4d51;
    --table-inspector-color: #dbdfe7;
}

.table-container {
    display: block;
    position: relative;
}

.table {display: table;}
.thead {display: table-header-group}
.tfoot {display: table-footer-group}
.tbody {display: table-row-group}
.tr {display: table-row}
.th, .td {display: table-cell}

.table {
    width: 100%;
    
    caption {
        font-size: var(--table-caption-font-size);
        font-weight: bold;
        padding: 10px 20px;
        border: 1px solid var(--table-border-color);
    }
    
    thead :is(th, td) {
        font-size: var(--table-head-font-size);
    }
    
    tfoot :is(th, td) {
        font-size: var(--table-head-font-size);
    }
    
    tbody td {
        font-size: var(--table-body-font-size);
    }
    
    th, .th, td, .td {
        padding: 0.625rem;
    }

    &.fixed-layout {
        table-layout: fixed;

        th, .th, td, .td {
            white-space: nowrap;
            overflow: hidden;
        }
    }

    thead, tfoot, .thead, .tfoot {
        border-bottom: 4px solid var(--table-border-color);
        background: var(--table-header-background);
        color: var(--table-header-color);

        th, td, .th, .td {
            cursor: default;
            color: var(--table-color);
            border-color: transparent;
            text-align: left;
            font-weight: bold;
            line-height: 1.2;
        }
    }

    tfoot, .tfoot {
        border-top: 4px solid var(--table-border-color);
    }

    tbody, .tbody {
        td, .td {
            padding: 0.625rem;
            vertical-align: middle;
        }
    }

    .sortable-column, .sorting {
        position: relative;
        cursor: pointer;
        padding-right: 30px;
        &:after {
            position: absolute;
            content: "\21C5";
            width: 1rem;
            height: 1rem;
            left: 100%;
            .px2rem(margin-left, -20px);
            top: 50%;
            margin-top: -.3rem;
            color: inherit;
            font-size: .8em;
            font-weight: 100;
            line-height: 1;
            opacity: .7;
        }

        &.sort-asc, &.sort-desc, &.sorting_asc, &.sorting_desc {
            background-color: var(--table-sortable-background);
            color: var(--table-sortable-color);

            &:after {
                //color: var(--table-sortable-color);
                opacity: 1;
                font-weight: 700;
                font-size: 1em;
                margin-top: -.5rem;
            }
        }

        &.sort-asc, &.sorting_asc {
            &:after {
                content: "\2191";
            }
        }

        &.sort-desc, &.sorting_desc {
            &:after {
                content: "\2193";
            }
        }
    }

    &.sortable-markers-on-left {
        .sortable-column, .sorting {
            padding-left: 30px;
            padding-right: 10px;

            &:before, &:after {
                left: 0;
                .px2rem(margin-left, 10px);
            }
        }
    }

    tr.selected, .tr.selected {
        td, .td {
            background-color: var(--table-selected-background);
            color: var(--table-selected-color);
        }
    }

    td.selected, .td.selected {
        background-color: var(--table-selected-background);
        color: var(--table-selected-color);
    }

    &.striped {
        tbody tr:nth-child(odd),
        .tbody .tr:nth-child(odd) {
            background: var(--table-striped-background);
        }
    }

    &.inverse-striped {
        tbody tr:nth-child(even),
        .tbody .tr:nth-child(even) {
            background: var(--table-striped-background);
        }
    }

    &.row-hover {
        tbody, .tbody {
            tr, .tr {
                &:hover {
                    background-color: var(--table-hover-background);
                    color: var(--table-hover-color);
                }
            }
        }
    }

    &.cell-hover {
        tbody, .tbody {
            td, .td {
                &:hover {
                    background-color: var(--table-hover-background);
                    color: var(--table-hover-color);
                }
            }
        }
    }

    &.table-border {
        border: 1px var(--border-color) solid;
    }

    &.cell-border {
        th, td, .th, .td {
            border: 1px var(--border-color) solid;
        }

        thead, .thead {
            tr:first-child, .tr:first-child {
                th, td, .th, .td {
                    border-top: none;

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

        tbody, .tbody {
            tr, .tr {
                &:first-child {
                    td, .td {
                        border-top: none;
                    }
                }

                td, .td {
                    &:first-child {
                        border-left: none;
                    }
                    &:last-child {
                        border-right: none;
                    }
                }

                &:last-child {
                    td, .td {
                        border-bottom: none;
                    }
                }
            }
        }
    }

    &.row-border {
        tr + tr, .tr + .tr  {
            border-top: 1px var(--table-border-color) solid;
        }
    }

    &.subcompact {
        th, td, .th, .td {
            padding: 3px 8px;
            font-size: 0.8em;
        }
    }

    &.compact {
        th, td, .th, .td {
            padding: 7px 8px;
            font-size: 0.9em;
        }
    }

    // default padding 10px
    &.small-padding {
        th, td, .th, .td {
            padding: 3px;
        }
    }
    
    &.medium-padding {
        th, td, .th, .td {
            padding: 6px;
        }
    }
    
    &.default-padding {
        th, td, .th, .td {
            padding: 10px;
        }
    }
    
    &.large-padding {
        th, td, .th, .td {
            padding: 16px;
        }
    }
    
    &.largest-padding {
        th, td, .th, .td {
            padding: 20px;
        }
    }
    
    .check-cell, .rownum-cell {
        width: 36px;
        text-align: center;
    }

    thead, tbody, .thead, .tbody {
        td, th, .th, .td {
            &.hidden {
                display: none!important;
            }
        }
    }

    .data-wrapper {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .checkbox, .radio, .switch {
        height: initial;
    }
}

.table-component {
    display: block;
    position: relative;
    container: table-component / inline-size;
    
    .inspector-button {
        font-family: "Segoe UI Symbol", serif;
    }
}

.table-top {
    display: flex;
    flex-flow: row wrap;
    gap: 6px;
}

.table-bottom {
    margin-left: 0;
    margin-right: 0;
}

.table-info, .table-pagination, .table-skip {
    flex-basis: auto;
}

.table-info {
    margin-bottom: 10px;
}

.table-progress {
    width: 64px;
    height: 64px;
    position: absolute;
    top: 200px;
    left: 50%;
    .translateX(-50%);
    display: none;
    z-index: 2;

    [class*=activity-] {
        margin: 0 auto;
    }
}

.table-inspector {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    //.translateX(-50%);
    background-color: var(--table-inspector-background);
    color: var(--table-inspector-color);
    z-index: @z-index-absolute;
    border: 1px double var(--table-inspector-border-color);
    border-radius: var(--table-inspector-border-radius);
    width: auto;
    padding: 10px;
    max-height: calc(100vh - 40px);
    .win-shadow();

    .table-inspector-header {
        display: block;
        position: relative;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding: 0 .5rem .5rem;
        border-bottom: 1px solid var(--table-border-color);
        font-size: 24px;
        font-weight: 100;
        margin-bottom: .5rem;
    }

    .table-wrap {
        display: block;
        height: calc(100% - 110px);
        overflow-x: hidden;
        overflow-y: auto;
    }

    .table-inspector-actions {
        display: block;
        position: relative;
        border-top: 1px solid var(--table-border-color);
        padding: .5rem .5rem 0;
        margin-top: .5rem;
    }

    table, .table {
        margin-bottom: 0;

        td, .td {
            vertical-align: middle;
        }

        tr.flash, .tr.flash {
            animation: flash-bg 1s ease-out;
            animation-iteration-count: 1;
        }
    }

    .spinner {
        width: 120px;
        input {
            font-size: 14px;
        }
    }

    &.open {
        display: block;
    }
}

.table-form {
    display: block;
    position: absolute;
    z-index: @z-index-absolute;
    height: auto;
    max-height: calc(100vh - 40px);
    width: auto;
    max-width: calc(100vw - 40px);
    top: 50%;
    left: 50%;
    .translate3d(-50%, -50%);
    border: 1px solid var(--border-color);
    .win-shadow();
}

@keyframes flash-bg {
    0% {
        background-color: var(--table-background);
    }
    30% {
        background-color: var(--table-hover-background);
    }
    100% {
        background-color: var(--table-background);
    }
}

each(@media-rules, {
    @container (min-width: @value) {
        .table.normal-@{key} {
            th, td, .th, .td {
                padding: 0.625rem;
                font-size: 1em;
            }
        }
        .table.compact-@{key} {
            th, td, .th, .td {
                padding: 7px 8px;
                font-size: 80%;
            }
        }
        .table.subcompact-@{key} {
            th, td, .th, .td {
                padding: 3px 8px;
                font-size: 80%;
            }
        }
    }
})

.table-load-data-activity {
    position: absolute!important;
    top: 0;
    left: 0;
    z-index: @z-index-absolute;
}

.table-skip {
    display: flex;
    justify-content: flex-end;
    position: relative;
    flex-flow: row nowrap;
    gap: 6px;

    .input {
        width: 200px;
    }
}


.table-component {
    .table-top, .table-rows-block, .table-search-block {
        display: flex;
        flex-flow: row wrap;
        justify-content: space-between;
        width: 100%;
    }

    .table-search-block {
        //flex-basis: 100%;
        flex-shrink: 1;
    }

    .table-rows-block {
        width: min(100%, 220px);
    }

    .table-top {
        //& > * {
        //    margin-bottom: 4px;
        //}
    }
}

@media screen and (min-width: @md){
    .table-component {
        .table-top {
            flex-flow: row nowrap;
        }
    }
}

.table-component {
    .table-container {
        border: 1px solid var(--border-color);
        margin: 10px 0;
        overflow: hidden;
        position: relative;
    }
}

.table-component {
    .table-container {
        &.horizontal-scroll {
            overflow-x: auto;
            .table {
                width: auto;
                min-width: 100%;
            }
        }
    }
}

.table-scrollable {
    overflow-x: auto;
    .table {
        width: auto;
        min-width: 100%;
    }
}

each(@media-rules-max, {
    @container table-component (max-width: @value) {
        .table.responsive-@{key} {
            --table-body-font-size: 12px;
            border: none;
            caption {
                margin-bottom: 6px;
            }
            thead {
                border: none;
                clip: rect(0 0 0 0);
                height: 1px;
                margin: -1px;
                overflow: hidden;
                padding: 0;
                position: absolute;
                width: 1px;
            }
            tbody {
                display: flex;
                flex-flow: column;
                gap: 6px;
            }
            tr {
                border: 3px solid var(--table-border-color);
                display: block;
                border-radius: var(--table-border-radius);
            }

            td {
                border-bottom: 1px solid var(--table-border-color);
                display: block;
                text-align: right;
                width: 100%;
                font-size: 14px;
                
                &::before {
                    content: attr(data-label);
                    float: left;
                    font-weight: bold;
                    text-transform: uppercase;
                    font-size: 12px;
                }
            }
        }
    }
})

.table-container {
    scroll-snap-type: y mandatory;
}

.table {
    &.fixed-header {
        tr {
            scroll-snap-align: start;
        }
        thead {
            position: sticky;
            top: 0;
        }
    }
}
