@layer components {
  .btn {
    --transition: 150ms ease;
    --btn-border-radius: 2em;

    align-items: center;
    background-color: var(--btn-background, var(--color-canvas));
    border-radius: var(--btn-border-radius);
    border: var(--btn-border-size, 1px) solid var(--btn-border-color, var(--color-ink-light));
    color: var(--btn-color, var(--color-ink));
    cursor: pointer;
    display: inline-flex;
    font-size: 1em;
    font-weight: var(--btn-font-weight, 600);
    gap: var(--btn-gap, 0.5em);
    justify-content: center;
    padding: var(--btn-padding, 0.5em 1.1em);
    pointer-events: auto;
    transition:
      background-color var( --transition),
      border var( --transition),
      color var( --transition),
      filter var( --transition),
      opacity var( --transition),
      scale var( --transition);

    /* Default icon styles */
    &:where(:has(.icon)) {
      text-align: start;

      .icon {
        --icon-size: var(--btn-icon-size, 1.3em);
      }
    }

    &:where(.avatar) {
      --btn-border-radius: var(--avatar-border-radius);
      --btn-padding: 0;
      --btn-border-size: 0;
      --hover-size: 0;

      aspect-ratio: 1;
      block-size: var(--btn-size);
      display: grid;
      inline-size: var(--btn-size);
      justify-content: unset;
      place-items: center;

      > * {
        grid-area: 1/1;
      }

      img {
        --icon-size: 100%;
        block-size: 100%;
        inline-size: 100%;
        max-inline-size: unset;
      }

      @media (any-hover: hover) {
        &:where(:not(:active):hover) {
          filter: brightness(0.7);
        }
      }
    }

    /* Circle buttons */
    &.btn--circle,
    &[aria-label]:where(:has(.icon)),
    &:where(:has(.for-screen-reader):has(.icon)) {
      --btn-border-radius: 50%;
      --btn-padding: 0;

      aspect-ratio: 1;
      block-size: var(--btn-size);
      display: grid;
      inline-size: var(--btn-size);
      place-items: center;

      > * {
        grid-area: 1/1;
      }
    }

    /* With radios and checkboxes */
    &:has(input[type=radio]),
    &:has(input[type=checkbox]) {
      position: relative;

      :is(input[type=radio]),
      :is(input[type=checkbox]) {
        --hover-size: 0;

        appearance: none;
        border-radius: var(--btn-border-radius);
        cursor: pointer;
        display: flex;
        inset: 0;
        margin: 0;
        padding: 0;
        position: absolute;

        &:focus-visible {
          outline: none;
        }
      }

      .checked {
        display: none;
      }
    }

    &:has(input:checked)  {
      --btn-background: var(--color-ink);
      --btn-color: var(--color-ink-inverted);
      --outline-color: var(--color-ink);

      .checked {
        display: block;
      }
    }

    &[disabled],
    &:has([disabled]),
    [disabled] &[type=submit],
    &[type=submit]:disabled {
      cursor: not-allowed;
      opacity: 0.3;
      pointer-events: none;
    }

    @media print {
      display: none;
    }
  }

  /* Variants */
  .btn--link {
    --btn-background: var(--color-link);
    --btn-color: var(--color-ink-inverted);
    --outline-color: var(--color-link);
  }

  .btn--negative {
    --btn-background: var(--color-negative);
    --btn-color: var(--color-ink-inverted);
    --outline-color: var(--color-negative);
  }

  .btn--placeholder {
    pointer-events: none;
    visibility: hidden;
  }

  .btn--plain {
    --btn-background: transparent;
    --btn-border-radius: 0;
    --btn-border-size: 0;
    --btn-color: inherit;
    --btn-icon-size: 100%;
    --btn-padding: 0;
    --hover-size: 0;
    --outline-size: 0;
  }

  .btn--positive {
    --btn-background: var(--color-positive);
    --btn-color: var(--color-ink-inverted);
    --outline-color: var(--color-positive);
  }

  .btn--remove {
    --btn-icon-size: 0.7em;
  }

  .btn--reversed {
    --btn-background: var(--color-ink);
    --btn-color: var(--color-canvas);
    --outline-color: var(--color-ink);
  }

  :is(.btn--link, .btn--negative, .btn--positive, .btn--reversed) {
    --btn-border-color: var(--color-canvas);
  }

  .btn--success {
    --success-timing-function: cubic-bezier(0.25, 1.25, 0.5, 1);
    animation: success 1s var(--success-timing-function);

    .icon {
      animation: zoom-fade 500ms var(--success-timing-function);
    }
  }

  .btn__group {
    .btn {
      --btn-border-radius: 0;
      --radius: 0.3em;

      inline-size: 100%;
      justify-content: center;
    }

    form {
      flex: 1 1 0%;
    }

    :first-of-type .btn {
      border-end-start-radius: var(--radius);
      border-inline-end: 0;
      border-start-start-radius: var(--radius);
      flex: 1 0 50%;
      padding-inline-end: 0.8em;
    }

    :last-of-type .btn {
      border-end-end-radius: var(--radius);
      border-inline-start: 0;
      border-start-end-radius: var(--radius);
      flex: 1 0 50%;
      padding-inline-start: 0.8em;
    }

    span {
      inline-size: 100%;
    }
  }
}

