/* Material Design 3 checkboxes, adapted to native Materialize markup. */

@at-root {
  :root {
    --checkbox-size: 20px;
    --checkbox-shape: 2px;
    --checkbox-state-layer-size: 44px;
  }
}

input[type="checkbox"] {
  box-sizing: border-box;
  position: relative;
  z-index: 0;
  flex: 0 0 var(--checkbox-size);
  width: var(--checkbox-size);
  height: var(--checkbox-size);
  margin: 0;
  padding: 0;
  vertical-align: middle;
  appearance: none;
  -webkit-appearance: none;
  color: var(--checkbox-selected-icon-color, var(--md-sys-color-on-primary));
  background: transparent;
  border: 2px solid var(--checkbox-outline-color, var(--md-sys-color-on-surface-variant));
  border-radius: var(--checkbox-shape);
  outline: none;
  cursor: pointer;
  transition: background-color 150ms linear, border-color 150ms linear;
  -webkit-tap-highlight-color: transparent;

  // The visual box stays compact while interaction feedback occupies 44px.
  &::before {
    content: "";
    position: absolute;
    z-index: -1;
    top: 50%;
    left: 50%;
    width: var(--checkbox-state-layer-size);
    height: var(--checkbox-state-layer-size);
    background: transparent;
    border-radius: 50%;
    transform: translate(-50%, -50%) scale(0.8);
    transition: background-color 150ms linear, transform 150ms ease;
    pointer-events: none;
  }

  // Check mark; the indeterminate state replaces it with a short bar.
  &::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 6px;
    box-sizing: border-box;
    width: 5px;
    height: 10px;
    border: solid currentColor;
    border-width: 0 2px 2px 0;
    opacity: 0;
    transform: rotate(45deg) scale(0.6);
    transform-origin: center;
    transition: opacity 50ms linear, transform 350ms cubic-bezier(0.2, 0, 0, 1);
    pointer-events: none;
  }

  &:checked,
  &:indeterminate {
    background: var(--checkbox-selected-container-color, var(--md-sys-color-primary));
    border-color: var(--checkbox-selected-container-color, var(--md-sys-color-primary));
  }

  &:checked::after {
    opacity: 1;
    transform: rotate(45deg) scale(1);
  }

  &:indeterminate::after {
    top: 7px;
    left: 3px;
    width: 10px;
    height: 2px;
    background: currentColor;
    border: 0;
    opacity: 1;
    transform: scale(1);
  }

  &:not(:disabled):hover::before {
    background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
    transform: translate(-50%, -50%) scale(1);
  }

  &:not(:disabled):is(:checked, :indeterminate):hover::before {
    background: color-mix(in srgb, var(--md-sys-color-primary) 8%, transparent);
  }

  &:not(:disabled):active::before {
    background: color-mix(in srgb, var(--md-sys-color-on-surface) 12%, transparent);
    transform: translate(-50%, -50%) scale(1);
  }

  &:not(:disabled):is(:checked, :indeterminate):active::before {
    background: color-mix(in srgb, var(--md-sys-color-primary) 12%, transparent);
  }

  &:focus-visible::before,
  &.tabbed:focus::before {
    background: color-mix(in srgb, var(--md-sys-color-on-surface) 12%, transparent);
    transform: translate(-50%, -50%) scale(1);
  }

  &:is(:checked, :indeterminate):focus-visible::before,
  &:is(:checked, :indeterminate).tabbed:focus::before {
    background: color-mix(in srgb, var(--md-sys-color-primary) 12%, transparent);
  }

  &:disabled {
    cursor: default;
    border-color: color-mix(in srgb, var(--md-sys-color-on-surface) 38%, transparent);
  }

  &:disabled:is(:checked, :indeterminate) {
    color: var(--md-sys-color-surface);
    background: color-mix(in srgb, var(--md-sys-color-on-surface) 38%, transparent);
    border-color: transparent;
  }
}

// Material has one checkbox type; keep this legacy class API visually unified.
input[type="checkbox"].filled-in {
  border-radius: var(--checkbox-shape);
}

// Match the 20px box with a 48px accessible row target. The :not(.lever)
// sibling keeps these layout rules away from Materialize switches.
label:has(> input[type="checkbox"]):has(> span:not(.lever)) {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  color: var(--md-sys-color-on-surface);
  font-size: 16px;
  cursor: pointer;
  user-select: none;

  > span:not(.lever) {
    padding: 0;
    line-height: 24px;
  }

  > .checkbox-icon {
    flex: 0 0 auto;
    color: var(--md-sys-color-on-surface-variant);
    font-size: 20px;
    line-height: 24px;
  }

  > input[type="checkbox"]:disabled ~ span:not(.lever),
  > input[type="checkbox"]:disabled ~ .checkbox-icon {
    color: color-mix(in srgb, var(--md-sys-color-on-surface) 38%, transparent);
  }

  &:has(> input[type="checkbox"]:disabled) {
    cursor: default;
  }
}

// The 48px label already provides Material's touch target. Remove the default
// paragraph margins when a paragraph is used only as a checkbox row.
p:has(> label:has(> input[type="checkbox"]):has(> span:not(.lever))) {
  margin-block: 0;
}
