@use '../../styles/abstract/base' as base;
@use '../../styles/abstract/variables' as v;
@use '../../styles/abstract/functions' as f;
@use '../../styles/abstract/mixins' as m;
@use '../../styles/abstract/theme' as t;

$component: '#{base.$prefix}-checkbox';

.#{$component} {
display: inline-flex;
align-items: center;
position: relative;
min-height: 48px;
min-width: 48px;
box-sizing: border-box;
padding: 4px 0;
user-select: none;

&:not(:has(> .#{$component}__label)) {
justify-content: center;
}

&__input {
font: inherit;
position: absolute;
opacity: 0;
width: 100%;
height: 100%;
margin: 0;
cursor: pointer;
z-index: 1;

&:disabled {
cursor: not-allowed;
}
}

&__icon {
position: relative;
flex-shrink: 0;
width: 18px;
height: 18px;
box-sizing: border-box;
border-radius: 2px;
background-color: transparent;
border: 2px solid t.color('on-surface-variant');
transition: m.spring(background-color, fast-effects), m.spring(border-color, fast-effects);

&::before {
content: '';
position: absolute;
inset: -13px;
border-radius: 50%;
background-color: transparent;
pointer-events: none;
}

svg {
position: absolute;
top: 50%;
left: 50%;
width: 18px;
height: 18px;
transform: translate(-50%, -50%) scale(0);
fill: currentColor;
color: t.color('on-primary');
}
}

&__label {
@include m.typography('body-large');
color: t.color('on-surface');
}

&--label-end .#{$component}__label {
margin-inline-start: 12px;
}

&--label-start .#{$component}__label {
margin-inline-end: 12px;
}

.#{$component}__input:checked ~ .#{$component}__icon,
&--indeterminate .#{$component}__icon {
background-color: t.color('primary');
border-color: t.color('primary');
}

.#{$component}__input:checked ~ .#{$component}__icon svg {
transform: translate(-50%, -50%) scale(1);
transition: m.spring(transform, default-spatial);
}

&--indeterminate {
.#{$component}__icon {
&::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 2px;
background-color: t.color('on-primary');
}

svg {
display: none;
}
}
}

&--error {
.#{$component}__icon {
border-color: t.color('error');
}

.#{$component}__input:checked ~ .#{$component}__icon,
&.#{$component}--indeterminate .#{$component}__icon {
background-color: t.color('error');
border-color: t.color('error');
}

.#{$component}__icon svg {
color: t.color('on-error');
}

&.#{$component}--indeterminate .#{$component}__icon::after {
background-color: t.color('on-error');
}
}

&--disabled {
.#{$component}__input {
cursor: not-allowed;
}

.#{$component}__icon {
background-color: transparent;
border-color: t.alpha('on-surface', 0.38);
}

.#{$component}__input:checked ~ .#{$component}__icon,
&.#{$component}--indeterminate .#{$component}__icon {
background-color: t.alpha('on-surface', 0.38);
border-color: transparent;
}

.#{$component}__icon svg {
color: t.color('surface');
}

&.#{$component}--indeterminate .#{$component}__icon::after {
background-color: t.color('surface');
}

.#{$component}__label {
color: t.alpha('on-surface', 0.38);
}
}

&:not(&--disabled) {
$input: '.#{$component}__input';
$layer: '.#{$component}__icon::before';

#{$input}:is(:hover, :focus-visible, :active):not(:checked) ~ .#{$component}__icon {
border-color: t.color('on-surface');
}

#{$input}:hover ~ #{$layer} { background-color: t.alpha('on-surface', v.state('hover-state-layer-opacity')); }
#{$input}:focus-visible ~ #{$layer} { background-color: t.alpha('on-surface', v.state('focus-state-layer-opacity')); }
#{$input}:active ~ #{$layer} { background-color: t.alpha('primary', v.state('pressed-state-layer-opacity')); }

#{$input}:checked:hover ~ #{$layer},
&.#{$component}--indeterminate #{$input}:hover ~ #{$layer} { background-color: t.alpha('primary', v.state('hover-state-layer-opacity')); }
#{$input}:checked:focus-visible ~ #{$layer},
&.#{$component}--indeterminate #{$input}:focus-visible ~ #{$layer} { background-color: t.alpha('primary', v.state('focus-state-layer-opacity')); }
#{$input}:checked:active ~ #{$layer},
&.#{$component}--indeterminate #{$input}:active ~ #{$layer} { background-color: t.alpha('on-surface', v.state('pressed-state-layer-opacity')); }

&.#{$component}--error {
#{$input}:not(:checked):is(:hover, :focus-visible, :active) ~ .#{$component}__icon { border-color: t.color('error'); }
#{$input}:hover ~ #{$layer} { background-color: t.alpha('error', v.state('hover-state-layer-opacity')); }
#{$input}:focus-visible ~ #{$layer} { background-color: t.alpha('error', v.state('focus-state-layer-opacity')); }
#{$input}:active ~ #{$layer} { background-color: t.alpha('error', v.state('pressed-state-layer-opacity')); }
}

#{$input}:focus-visible ~ #{$layer} {
outline: 3px solid t.color('secondary');
outline-offset: 2px;
}
}

@include m.reduced-motion {
.#{$component}__icon,
.#{$component}__icon svg {
transition: none;
}
}

@include m.high-contrast {
.#{$component}__icon {
border-width: 2px;
}
}

}

.#{$component}-group {
display: flex;
flex-direction: column;
gap: 8px;

&--horizontal {
flex-direction: row;
gap: 16px;
flex-wrap: wrap;
}
}