/*
 * Email recipient chips (Tagify) for the invite modals.
 * Loaded after static/vendor/tagify/tagify.css and restyles it onto the
 * Bootstrap/brand tokens: a 4px-radius field like .form-control, chips in
 * the subtle "badge" family, invalid chips in the danger family plus an icon,
 * so the state does not rely on colour alone. All colour comes from runtime
 * --bs-* / --brand-* tokens (Seed Rule), never brand hexes.
 */

.lib-emails-input-container {
  display: block;
}

.lib-emails-input-container .tagify {
  --tags-border-color: var(--bs-border-color);
  --tags-hover-border-color: var(--bs-gray-500, #adb5bd);
  --tags-focus-border-color: var(--brand-primary-tint-50, var(--bs-primary));
  --placeholder-color: var(--bs-secondary-color);
  --placeholder-color-focus: var(--bs-tertiary-color, var(--bs-secondary-color));
  --input-color: var(--bs-body-color);
  --tag-pad: 0.125rem 0.125rem 0.125rem 0.5rem;
  --tag-hide-transition: 0.15s;

  width: 100%;
  min-height: 5rem;
  align-content: flex-start;
  padding: 0.3125rem 0.375rem;
  background-color: var(--bs-body-bg);
  border-radius: var(--bs-border-radius-sm, 0.25rem);
  font-size: 0.875rem;
  line-height: 1.5;
  cursor: text;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.lib-emails-input-container .tagify.tagify--focus {
  border-color: var(--tags-focus-border-color);
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.25);
}

/* Field-level problem: an invalid chip, or a server error on the hidden input. */
.lib-emails-input-container .tagify.email-tags--has-invalid,
.lib-emails-input-container:has(~ input.is-invalid) .tagify {
  --tags-border-color: var(--bs-form-invalid-border-color, var(--bs-danger));
  --tags-hover-border-color: var(--bs-form-invalid-border-color, var(--bs-danger));
  --tags-focus-border-color: var(--bs-form-invalid-border-color, var(--bs-danger));
}

.lib-emails-input-container .tagify.email-tags--has-invalid.tagify--focus,
.lib-emails-input-container:has(~ input.is-invalid) .tagify.tagify--focus {
  box-shadow: 0 0 0 0.25rem rgba(var(--bs-danger-rgb), 0.25);
}

/* Typing area */
.lib-emails-input-container .tagify__input {
  margin: 0.1875rem 0.25rem;
  padding: 0.125rem 0.25rem;
  line-height: 1.5;
  caret-color: var(--bs-primary);
}

.lib-emails-input-container .tagify__input::before {
  line-height: 1.5;
}

/* Chip */
.lib-emails-input-container .tagify__tag {
  margin: 0.1875rem 0.375rem 0.1875rem 0;
  color: var(--bs-primary-text-emphasis);
  background-color: var(--bs-primary-bg-subtle);
  border: 1px solid var(--bs-primary-border-subtle);
  border-radius: var(--bs-border-radius, 0.375rem);
  transition: background-color 0.15s ease-out, border-color 0.15s ease-out,
    box-shadow 0.15s ease-out;
}

.lib-emails-input-container .tagify__tag:last-child {
  margin-right: 0.375rem;
}

.lib-emails-input-container .tagify__tag > div {
  color: inherit;
  padding: var(--tag-pad);
  line-height: 1.5;
}

/* Tagify paints chips with an inset-shadow ::before; the border does it here. */
.lib-emails-input-container .tagify__tag > div::before {
  content: none;
}

.lib-emails-input-container .tagify__tag {
  max-width: 100%;
}

/* One line per chip; a very long address truncates (full value in the tooltip). */
.lib-emails-input-container .tagify__tag > div {
  min-width: 0;
}

.lib-emails-input-container .tagify__tag-text {
  max-width: 40ch;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-emails-input-container .tagify__tag-text::selection {
  background-color: rgba(var(--bs-primary-rgb), 0.2);
}

/* Remove button: a drawn x (Bootstrap Icons "x" path) rather than a glyph. */
.lib-emails-input-container .tagify__tag__removeBtn {
  width: 1.25rem;
  height: 1.25rem;
  margin-inline: 0.125rem 0.1875rem;
  color: inherit;
  background: transparent;
  border-radius: var(--bs-border-radius-sm, 0.25rem);
  opacity: 0.7;
  font: inherit;
  transition: background-color 0.15s ease-out, opacity 0.15s ease-out;
}

.lib-emails-input-container .tagify__tag__removeBtn::after {
  content: "";
  width: 0.875rem;
  height: 0.875rem;
  background-color: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708'/%3E%3C/svg%3E") center / contain no-repeat;
  transition: none;
}

.lib-emails-input-container .tagify__tag__removeBtn:hover {
  color: inherit;
  opacity: 1;
  background-color: rgba(var(--bs-primary-rgb), 0.12);
}

/* Tagify fades the chip text while the remove button is hovered; keep it legible. */
.lib-emails-input-container .tagify__tag__removeBtn:hover + div > span {
  opacity: 1;
}

.lib-emails-input-container .tagify__tag:has(.tagify__tag__removeBtn:hover) {
  border-color: var(--bs-primary);
}

/* Invalid chip: danger family + leading icon (Bootstrap Icons exclamation-circle-fill). */
.lib-emails-input-container .tagify__tag.tagify--notAllowed:not(.tagify__tag--editable) {
  color: var(--bs-danger-text-emphasis);
  background-color: var(--bs-danger-bg-subtle);
  border-color: var(--bs-danger-border-subtle);
  cursor: pointer;
}

.lib-emails-input-container .tagify__tag.tagify--notAllowed:not(.tagify__tag--editable) > div > span {
  opacity: 1;
}

.lib-emails-input-container .tagify__tag.tagify--notAllowed:not(.tagify__tag--editable) > div::before {
  content: "";
  position: static;
  flex: none;
  align-self: center;
  width: 0.875rem;
  height: 0.875rem;
  margin-right: 0.3125rem;
  box-shadow: none;
  animation: none;
  background-color: var(--bs-danger);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0M8 4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0 0 1.1 0l.35-3.507A.905.905 0 0 0 8 4m.002 6a1 1 0 1 0 0 2 1 1 0 0 0 0-2'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0M8 4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0 0 1.1 0l.35-3.507A.905.905 0 0 0 8 4m.002 6a1 1 0 1 0 0 2 1 1 0 0 0 0-2'/%3E%3C/svg%3E") center / contain no-repeat;
}

.lib-emails-input-container .tagify__tag.tagify--notAllowed .tagify__tag__removeBtn:hover {
  background-color: rgba(var(--bs-danger-rgb), 0.14);
}

.lib-emails-input-container .tagify__tag.tagify--notAllowed:has(.tagify__tag__removeBtn:hover) {
  border-color: var(--bs-danger);
}

/* Editing a chip (double-click): it becomes a small inline field. */
.lib-emails-input-container .tagify__tag.tagify__tag--editable {
  color: var(--bs-body-color);
  background-color: var(--bs-body-bg);
  border-color: var(--bs-primary);
  box-shadow: 0 0 0 0.125rem rgba(var(--bs-primary-rgb), 0.2);
}

.lib-emails-input-container .tagify__tag.tagify__tag--editable.tagify--invalid {
  border-color: var(--bs-danger);
  box-shadow: 0 0 0 0.125rem rgba(var(--bs-danger-rgb), 0.2);
}

.lib-emails-input-container .tagify__tag--editable > div::before {
  content: none;
}

.lib-emails-input-container .tagify__tag--editable .tagify__tag-text[contenteditable] {
  max-width: min(40ch, 100%);
  white-space: nowrap;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

/* The remove button has no job while a chip is being edited. */
.lib-emails-input-container .tagify__tag--editable > .tagify__tag__removeBtn {
  display: none;
}

/* A repeated address points back at the chip it repeats. */
.lib-emails-input-container .tagify__tag.email-tag--repeat {
  border-color: var(--bs-primary);
  animation: email-tag-repeat 0.9s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes email-tag-repeat {
  0% {
    box-shadow: 0 0 0 0 rgba(var(--bs-primary-rgb), 0.45);
  }
  100% {
    box-shadow: 0 0 0 0.375rem rgba(var(--bs-primary-rgb), 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .lib-emails-input-container .tagify,
  .lib-emails-input-container .tagify__tag,
  .lib-emails-input-container .tagify__tag__removeBtn {
    transition: none;
  }

  .lib-emails-input-container .tagify__tag.email-tag--repeat {
    animation: none;
    box-shadow: 0 0 0 0.125rem rgba(var(--bs-primary-rgb), 0.35);
  }
}

/* Hint line under the field: guidance on the left, recipient count on the right.
   The guidance swaps for the problem when a chip is invalid. */
.email-tags-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.375rem;
}

.email-tags-meta .form-text {
  margin-top: 0;
}

.email-tags-meta .email-tags-hint {
  color: var(--bs-secondary-color);
}

.email-tags-meta.has-invalid .email-tags-hint {
  color: var(--bs-danger-text-emphasis);
}

.email-tags-meta.is-notice .email-tags-hint {
  color: var(--bs-primary-text-emphasis);
}

.email-tags-meta .email-tags-count {
  flex: none;
  color: var(--bs-secondary-color);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.email-tags-meta .email-tags-count:empty {
  display: none;
}
