$border-width: 1px;
$border-radius: var(
  --wds-swatches-swatch-border-radius-medium,
  var(--wds-border-radius-300, 6px)
);
$tertiary-fill: var(--wds-color-fill-standard-tertiary, rgba(255, 255, 255, 1));

$chessboard: linear-gradient(45deg, lightgray 26%, transparent 26%),
  linear-gradient(135deg, lightgray 26%, transparent 26%),
  linear-gradient(45deg, transparent 75%, lightgray 75%),
  linear-gradient(135deg, transparent 75%, lightgray 75%);

.fill-preview {
  display: flex;
  min-width: 24px;
  min-height: 24px;
  width: 100%;
  height: 100%;

  /* Size */
  &--size-extra-tiny {
    min-height: var(--wds-swatches-swatch-size-extra-tiny, var(--wds-space-300, 18px));
    min-width: var(--wds-swatches-swatch-size-extra-tiny, var(--wds-space-300, 18px));
  }

  &--size-tiny {
    min-height: var(--wds-swatches-swatch-size-tiny, var(--wds-space-400, 24px));
    min-width: var(--wds-swatches-swatch-size-tiny, var(--wds-space-400, 24px));
  }

  &--size-small {
    min-height: var(--wds-swatches-swatch-size-small, var(--wds-space-500, 30px));
    min-width: var(--wds-swatches-swatch-size-small, var(--wds-space-500, 30px));
  }

  &--size-medium {
    min-height: var(--wds-swatches-swatch-size-medium, var(--wds-space-600, 36px));
    min-width: var(--wds-swatches-swatch-size-medium, var(--wds-space-600, 36px));
  }

  &--size-large {
    min-height: var(--wds-swatches-swatch-size-large, var(--wds-space-700, 40px));
    min-width: var(--wds-swatches-swatch-size-large, var(--wds-space-700, 40px));
  }

  &__button {
    /* stylelint-disable-next-line plugin/use-baseline -- color-mix has a literal fallback color */
    --fill-preview-border-color: var(
      --wds-color-border-transparent-primary,
      color-mix(in srgb, var(--wsr-color-B00, #084EBD) 24%, transparent)
    );
    --fill-preview-border-selected-color: var(
      --wds-swatches-fill-border-selected,
      var(--wds-color-border-standard-secondary-active, var(--wsr-color-B10, #116DFF))
    );

    border-radius: #{$border-radius};
    width: 100%;
    height: 100%;
    padding: 0;
    justify-content: center;
    align-items: center;
    border: $border-width solid;
    background-color: transparent;
    background-origin: border-box;
    border-color: var(--fill-preview-border-color);
    /* Disable native focus */
    /* stylelint-disable-next-line plugin/use-baseline */
    outline: none;
  }

  &__button#{&}__button {
    display: flex;
  }

  &__content::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: #{$border-radius};
    border: $border-width solid var(--fill-preview-border-color);
  }

  /* the shared focus-ring band lives on ::before; while selected, that pseudo
   * is the active-swatch halo — pin its geometry and drop the band there. The
   * triple `&__button` outranks FocusRing's `.focus-ring:focus-visible::before`
   * (equal specificity otherwise) regardless of bundle order. */
  &__button#{&}__button#{&}__button--selected::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    border-radius: var(--wds-swatches-swatch-active-border-radius-medium, 9px);
    box-shadow: 0 0 0 1px
      var(--fill-preview-border-selected-color);
    /* stylelint-disable-next-line plugin/use-baseline */
    outline: none;
  }

  &__button--empty {
    /* stylelint-disable-next-line plugin/use-baseline -- color-mix has a literal fallback color */
    --fill-preview-border-color: var(
      --wds-swatches-fill-border-no-value,
      var(
        --wds-color-border-standard-secondary,
        color-mix(in srgb, var(--wsr-color-B00, #084EBD) 24%, transparent)
      )
    );

    background: linear-gradient(
      to top left,
      #{$tertiary-fill} 0%,
      #{$tertiary-fill} calc(50% - 0.8px),
      var(
          --wds-swatches-fill-border-no-value-dash,
          var(--wds-color-fill-destructive-primary, rgba(214, 69, 61, 1))
        )
        50%,
      #{$tertiary-fill} calc(50% + 0.8px),
      #{$tertiary-fill} 100%
    );
  }

  @media (hover: hover) {
    &__button:hover {
      /* stylelint-disable-next-line plugin/use-baseline -- color-mix has a literal fallback color */
      --fill-preview-border-color: var(
        --wds-color-border-transparent-primary-hover,
        color-mix(in srgb, var(--wsr-color-B00, #084EBD) 60%, transparent)
      );
    }
  }

  /*
   * `ButtonCore` never sets the native `disabled` attribute — it renders
   * `aria-disabled` alongside its own state class — so `:disabled` would never
   * match. `[aria-disabled='true']` carries the same (0,2,0) weight the Stylable
   * state class had.
   */
  &__button[aria-disabled='true'] {
    border: none;
    --fill-preview-border-selected-color: var(
      --wds-swatches-fill-border-disabled,
      var(--wds-color-black-100-transparent-0, rgba(0, 6, 36, 0))
    );
    background-color: var(
      --wds-swatches-fill-disabled,
      var(--wds-color-fill-standard-secondary-disabled, var(--wsr-color-D10-10, rgba(0, 6, 36, 0.10)))
    );
  }

  /* Handles fill list — `__content` is `ButtonCore`'s content element */
  &__content {
    padding: 0;
    width: calc(100% + #{$border-width} * 2);
    height: calc(100% + #{$border-width} * 2);
    margin: calc(#{$border-width} * 2 * -1);
    overflow: hidden;
  }

  &__content--color-count-4 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
  }

  &__content--color-count-3 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    grid-template-areas: 'first second' 'first third';
  }

  &__content--color-count-3 > *:nth-child(1) {
    grid-area: first;
  }

  &__content--color-count-3 > *:nth-child(2) {
    grid-area: second;
  }

  &__content--color-count-3 > *:nth-child(3) {
    grid-area: third;
  }

  &__content--color-count-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr;
  }

  &__content--color-count-2 > *:nth-child(1) {
    grid-row: 1;
    grid-column: 1;
  }

  &__content--color-count-2 > *:nth-child(2) {
    grid-row: 1;
    grid-column: 2;
  }

  &__content--color-count-1 {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }

  &__content--color-count-1 > *:nth-child(1) {
    grid-row: 1;
    grid-column: 1;
  }

  /* Set background color for each child */
  &__content > *:nth-child(1) {
    background-color: var(
      --wds-internal-fill-preview-background-color-0,
      transparent
    );
  }

  &__content > *:nth-child(2) {
    background-color: var(
      --wds-internal-fill-preview-background-color-1,
      transparent
    );
  }

  &__content > *:nth-child(3) {
    background-color: var(
      --wds-internal-fill-preview-background-color-2,
      transparent
    );
  }

  &__content > *:nth-child(4) {
    background-color: var(
      --wds-internal-fill-preview-background-color-3,
      transparent
    );
  }

  /* chessboard css for color fill*/
  &__color-backdrop {
    border-radius: #{$border-radius};
    background-image: #{$chessboard};
    background-size: 8px 8px;
    background-position: 0 0, 4px 0, 4px -4px, 0px 4px;
  }

  /* chessboard css for background-image fill*/
  &__image-backdrop {
    position: relative;
    overflow: hidden;
    border-radius: #{$border-radius};
    background-image: #{$chessboard};
    background-size: 8px 8px;
    background-position: 0 0, 4px 0, 4px -4px, 0px 4px;
    background-repeat: repeat;

    &::before {
      content: '';
      position: absolute;
      inset: 0;
      background-image: var(--wds-internal-fill-preview-background-image, none);
      background-size: var(
        --wds-internal-fill-preview-background-size,
        100% 100%
      );
      background-position: center;
      background-repeat: no-repeat;
    }
  }
}
