// https://m3.material.io/components/cards/specs

$label_text_empty_size: 16px;
$label_text_populated_size: 12px;
$label_text_gap_size: 3.2px;

.sd-text_field {
    box-sizing: border-box;
    min-width: 200px; // follow webkit input default min-width
    display: inline-flex;
    align-items: center;
    background: var(--md-sys-color-surface-container-highest);
    cursor: text;
    border-top-left-radius: 0.25rem;
    border-top-right-radius: 0.25rem;
    padding: 0.75rem;
    transition: filter 200ms, border 200ms;

    &:hover {
        filter: brightness(96%);
    }

    &:active {
        filter: brightness(92%);
    }

    &-leading_icon,
    &-trailing_icon {
        flex-shrink: 0;
        transform: translateY(1px);
        font-size: 20px;
        svg {
            width: 20px;
        }
    }
    &-leading_icon {
        margin-right: 0.5rem;
    }
    &-trailing_icon {
        margin-left: 0.5rem;
    }

    position: relative;

    &-active_indicator {
        position: absolute;
        left: 0;
        bottom: 0;
        height: 2px;
        width: 100%;
        display: block;

        &::before,
        &::after {
            content: '';
            display: block;
            height: 1px;
        }
    }

    &:has(&-supporting_text) {
        margin-bottom: 18px;
    }

    &-supporting_text {
        font-size: 12px;
        position: absolute;
        color: var(--md-sys-color-on-surface-variant);
        font-size: small;
        left: 0;
        bottom: -18px;
        text-wrap: nowrap;
        padding: 0 8px;
        box-sizing: border-box;
        width: 100%;
    }

    input {
        all: unset;
        max-width: 100%;
    }

    // support for textarea
    textarea {
        all: unset;
        display: inline-block;
        resize: both;
        flex-grow: 1;
        border: none;
        outline: none;
        padding: 0;
        font-family: unset;
        background: transparent;
        box-sizing: border-box;
        // occupy whole area
        min-width: 100%;
        min-height: 100%;
        font-size: 15px;
        scrollbar-gutter: stable;
        // wrap
        word-break: break-all;
        text-wrap: wrap;
    }

    &:has(textarea) {
        align-items: flex-start;
        // this make the textarea to place at the right bottom of the container without padding
        // allowing the resizer to place at the correct place
    }
}

//! outlined (default filled)
.sd-text_field[data-sd='outlined'] {
    border: 2px solid var(--md-sys-color-outline);
    background: transparent;
    border-radius: 0.25rem;

    //? limit the leading_icon to has max width 1rem
    //? this make the component do not need javascript to calculate
    //? the leading_icon's width, so we can just place the label_text
    //? easily and correctly when the input value is empty
    .sd-text_field-leading_icon {
        margin-right: 1rem;
        width: 1rem;
    }
    &:has(.sd-text_field-leading_icon) {
        .sd-text_field-label_text {
            left: 2.5rem;
        }
    }
    .sd-text_field-label_text {
        position: absolute;
        transition: all 200ms;
        left: 8px;
        top: 16px;
        color: var(--md-sys-color-on-surface-variant);
    }

    &[data-sd-label_text='populated'] .sd-text_field-label_text {
        left: 8px;
        top: -8px;
        background: rgb(255 255 255);
        font-size: $label_text_populated_size;
        padding: 0 4px;
    }

    &[data-sd-focus='true'] {
        border: 2px solid var(--md-sys-color-primary);
        .sd-text_field-label_text {
            color: var(--md-sys-color-primary);
        }
    }

    // override default
    &:hover,
    &:active {
        filter: none;
    }

    input {
        width: 0; // this will auto grow as we specify flex-grow:1
        max-width: 100%;
        flex-grow: 1;
    }
}

//! filled
.sd-text_field[data-sd='filled'] {
    .sd-text_field-filled_helper {
        position: relative;
        width: 100%;
        height: $label_text_empty_size + $label_text_populated_size;

        .sd-text_field-label_text {
            display: block;
            width: fit-content;
            position: absolute;
            transition: all 200ms;
            text-wrap: nowrap;
            user-select: none;
            pointer-events: none;
        }

        input {
            position: absolute;
        }
    }
    textarea {
        margin-top: 0.75rem;
    }
}

//! label_text empty
.sd-text_field[data-sd-label_text='empty'] {
    .sd-text_field-filled_helper {
        .sd-text_field-label_text {
            top: 50%;
            transform: translateY(-50%);
            line-height: $label_text_empty_size;
            font-size: $label_text_empty_size;
            color: var(--md-sys-color-on-surface-variant);
        }
        input {
            // this make the input fill all the available container space
            left: -0.5rem;
            top: -0.75rem;
            width: calc(100% + 0.5rem + 0.5rem);
            height: calc(100% + 0.75rem + 0.75rem);
        }
    }

    .sd-text_field-active_indicator {
        &::before {
            background: var(--md-sys-color-on-surface-variant);
        }
        &::after {
            background: transparent;
        }
    }
}

//! label_text populated
.sd-text_field[data-sd-label_text='populated'] {
    .sd-text_field-filled_helper {
        .sd-text_field-label_text {
            top: -($label_text_gap_size);
            line-height: $label_text_populated_size;
            font-size: $label_text_populated_size;
            color: var(--md-sys-color-primary);
        }
        input {
            top: $label_text_populated_size;
        }
    }

    .sd-text_field-active_indicator {
        &::before,
        &::after {
            background: var(--md-sys-color-primary);
        }
    }
}

// disabled
.sd-text_field[data-sd-disabled='true'] {
    filter: grayscale(100%);
    opacity: 0.3;
    cursor: not-allowed;
}

// error
.sd-text_field[data-sd-error='true'] {
    .sd-text_field-filled_helper {
        .sd-text_field-label_text {
            color: var(--md-sys-color-error);
        }
    }

    .sd-text_field-active_indicator {
        &::before,
        &::after {
            background: var(--md-sys-color-error);
        }
    }

    .sd-text_field-supporting_text {
        color: var(--md-sys-color-error);
    }
}
