$name: 'tag-picker';

%item {
  align-items: center;
  border: 1px solid;
  border-color: inherit;
  display: flex;
  gap: 0.25em;
  padding: 0 0.25em;
}

%items {
  border-color: inherit;
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  padding: 1px;
}

.#{$name} {
  &,
  & * {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    box-sizing: border-box;
    color: inherit;
    font: inherit;
    outline: 0;
  }
}

.#{$name} {
  border: 1px solid;
  cursor: text;
  display: inline-flex;
  flex-wrap: wrap;
  position: relative;
  vertical-align: middle;
  &[aria-disabled='true'] {
    color: rgba(128, 128, 128);
    cursor: not-allowed;
    * {
      pointer-events: none;
    }
    .#{$name}__tag[aria-pressed='true'] {
      background: rgba(0, 0, 255, 0.25);
    }
  }
  &[aria-invalid='true'] {
    color: rgb(255, 0, 0);
  }
  &__flex {
    @extend %items;
    flex: 1;
    overflow: hidden;
  }
  &__self {
    background: 0 0;
    border-radius: 0;
    border: 0;
    bottom: 0;
    box-shadow: none;
    color: transparent;
    font: 0/0 a;
    height: 1px;
    left: 0;
    margin: 0;
    opacity: 0;
    outline: 0;
    overflow: hidden;
    padding: 0;
    position: absolute;
    text-shadow: none;
    width: 1px;
  }
  &__tag,
  &__text {
    @extend %item;
    overflow: hidden;
    position: relative;
  }
  &__tag {
    cursor: pointer;
    &[aria-disabled='true'] {
      &[aria-pressed='true'] {
        background: rgba(0, 0, 255, 0.25);
      }
    }
    &[aria-pressed='true'] {
      background: rgba(0, 0, 255, 0.5);
    }
    &:focus {
      outline: 1px solid;
      outline-offset: -2px;
    }
    // Keep selection (so that context menu can show the “Copy”, “Cut”, and “Paste” command), but hide it visually!
    ::selection {
      background-color: transparent;
      color: inherit;
    }
    + .#{$name}__text {
      span {
        + span {
          // Hide the placeholder if we have at least one tag
          display: none;
        }
      }
    }
  }
  &__text {
    border-color: transparent;
    flex: 1;
    span {
      flex: 1;
      position: relative;
      z-index: 1;
      // Ensure height even when it is empty
      &::after {
        content: '\200c';
      }
      * {
        display: inline; // Remove (hide) line-break in tag editor
      }
      br {
        display: none;
      }
      + span {
        align-items: center;
        bottom: 0;
        display: flex;
        left: 0;
        opacity: 0.5;
        padding: inherit;
        position: absolute;
        right: 0;
        top: 0;
        white-space: nowrap;
        z-index: 0;
      }
    }
  }
  &__v {
    flex: 1;
  }
  &__x {
    height: 1em;
    line-height: 0;
    margin-right: -0.125em;
    position: relative;
    user-select: none;
    width: 1em;
    &::after,
    &::before {
      border: 1px solid;
      bottom: 0;
      content: "";
      left: 50%;
      margin-left: -1px;
      position: absolute;
      top: 0;
    }
    &::after {
      transform: rotate(45deg);
    }
    &::before {
      transform: rotate(135deg);
    }
  }
}