.kselect {
    --menu-hover:lightgreen;
    --background:#262626;
    --item-color: var(--white);
    --item-hover: #262626;
    --item-hover-background:#3e3e3e;
    --item-selected-color: lightgreen;
    --item-selected-color: var(--white);
    --item-selected-background:rgba(74, 116, 220, .6);
    --item-selected-hover-background:rgba(74, 116, 220, .8);
    --scroll-background:rgba(79, 126, 230, .7);
}

.light .kselect {
    --menu-hover: #333;
    --background: white;
    --item-color: #333;
    --item-hover: #333;
    --item-hover-background:rgba(157, 208, 179, .2);
    --item-selected-color: #333;
    --item-selected-background:rgba(157, 208, 179, .6);
    --item-selected-hover-background:rgba(157, 208, 179, .8);
    --scroll-background:rgba(157, 208, 179, .4);
}


form [popover] {
    opacity: 0;
    transform: translateY(-4px);
    transition: 
        opacity 0.3s ease-out,
        transform 0.3s ease-out,
        display 0.3s ease-out allow-discrete,
        overlay 0.3s ease-out allow-discrete;
    margin: 0;
    inset: unset;
    padding: 0;
    background-color: var(--background);
    color: var(--white);
    border: 2px solid var(--border-focus);
    user-select: none;
    outline: none;
    overflow: visible;
    border-radius: 10px;
    box-shadow:
        0 4px 6px rgba(0,0,0,.4),
        0 12px 32px rgba(0,0,0,.5),
        inset 0 1px 0 rgba(255,255,255,.04);

    &::before {
        content: '';
        position: absolute;
        top: -8px;
        left: 14px;
        border-left: 7px solid transparent;
        border-right: 7px solid transparent;
        border-bottom: 8px solid var(--border-focus);
    }
    &::after {
        content: '';
        position: absolute;
        top: -6px;
        left: 16px;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-bottom: 6px solid var(--background);
    }
    &:popover-open {
        opacity: 1;
        transform: translateY(0);
        transition: 
            opacity 0.3s ease-out,
            transform 0.3s ease-out,
            display 0.3s ease-out allow-discrete,
            overlay 0.3s ease-out allow-discrete;
        @starting-style { & { opacity: 0; transform: translateY(0); } }
    }
}


@keyframes ksFadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes ksFadeOut {
    from { opacity: 1; transform: translateY(0); }
    to   { opacity: 0; transform: translateY(-4px); }
}

.kselect {

    position: relative;
    width: 100%;
    min-width: max-content;
    border-radius: 4px;
    box-sizing: border-box;
    background-color: var(--background);

    transition: border-color .2s ease-in-out;
    border: 2px solid var(--input-border);
    user-select: none;
    outline: none;
    &:hover, &:has(.selected-item:focus), &.open {
        border-color: var(--border-focus);
        border-radius: 5px;
    }

    &.open { 
        border-bottom-color: transparent;
        border-radius: 5px 5px 0 0;
        & i.arrow { transform: rotate(180deg); } 
    }
    &.default .selected-item { 
        color: var(--input-placeholder); 
        & :not(span):before { color:var(--input-placeholder); }
    }

    &.r1 { --max-height: 50px;  }
    &.r2 { --max-height: 90px;  }
    &.r3 { --max-height: 130px; }
    &.r4 { --max-height: 170px; }
    &.r5 { --max-height: 210px; }
    &.r6 { --max-height: 250px; }
    &.r7 { --max-height: 290px; }
    &.r8 { --max-height: 330px; }

    & input.ksearch-input:not([type="checkbox"]):not([type="radio"]):focus {
        border:0 !important;
    }

    & .ksearch-input {
        display: block;
        width: 100%;
        box-sizing: border-box;
        padding: 0 12px !important;
        border: none;
        color: var(--input-color);
        font-size: var(--input-size);
        outline: none;
        &:hover { border:0 !important; }
    }

    & .selected-item {
        cursor: pointer;
        color: var(--input-color);
        opacity: .87;
        padding: 0 12px;

        height: 36px;
        line-height: 36px;
        font-size: var(--input-size);

        background-color: var(--background);
        margin: 0;
        outline: none;
        display: flex;
            align-items: center;
            box-sizing: border-box;
        & span { flex: 1; }
        & :not(span) {
            width: 30px !important;
            color:var(--input-color);
            cursor: pointer;
            &:before { color:var(--input-color); }
            &:hover:before { color:lightgreen; }
        }
        & i.arrow {
            display: flex;
            align-items: center;
            justify-content: center;
            transition: transform 0.25s ease;
            &:before { content: "\f0d7"; top: 45%; }
        }
        & .kft.x { width:30px; height:100%; }
        & .kft.x:before { top: 46%; }
        & .kft.x:hover:before { color:lightgreen; }
    }

    & .item {
        font-size: var(--input-size);
        padding: 0 0 0 32px;
        height: 40px;
        line-height: 40px;
        min-height: 40px;
        border-radius: 5px;
        margin: 0 5px;
        cursor: pointer;
        color: var(--item-color);
        border-top: 0;
        &:hover { background-color: var(--item-hover-background); }
        &.active {
            color: var(--item-selected-color);
            background-color: var(--item-selected-background);
            &:hover { background-color:var(--item-selected-hover-background); }
            font-weight: normal;
            position: relative;
            &:before {
                font-family: "kui";
                content: "\e90b";
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                left: 10px;
                color: var(--item-selected-color);
                font-size: 14px;
                font-weight: normal;
            }
        }
    }

    & .popover {
        box-sizing: border-box;
        display: none;
        position: absolute;
        top: 34px;
        margin-top:-1px;
        left: -2px;
        right: -2px;
        max-height: var(--max-height, 170px);
        overflow-y: auto;
        z-index: 1000;
        background-color: var(--background);
        scrollbar-color: var(--scroll-background) transparent;
        border-radius: 0 0 5px 5px;
        box-shadow: 12px 12px 12px -10px rgba(0, 0, 0, .4);
        padding: 4px 0;
        border: 2px solid var(--border-focus);
        border-top: 0;
        &.open {
            display: block;
            animation: ksFadeIn 0.25s ease forwards;
        }
        &.closing {
            display: block;
            animation: ksFadeOut 0.25s ease forwards;
        }
    }
}

form.kui .field.error .kselect { border-color: var(--error-border); }
form.kui .field.error .popover { 
    border-color: var(--error-border);
    scrollbar-color: var(--error-border) transparent;
}
