/* Autocomplete Component
 * A custom autocomplete with clear separation between search and selections
 */

.autocomplete {
  position: relative;
  width: 100%;
}

/* Collapsible Trigger Button — matches Forms::InputComponent :sm */
.autocomplete__trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  height: 2.75rem; /* h-11 mobile */
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
  text-align: left;
  background-color: #fff;
  border: 1px solid rgb(229 229 229); /* neutral-200 */
  border-radius: 0.5rem;
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

@media (min-width: 640px) {
  .autocomplete__trigger {
    height: 2.5rem; /* sm:h-10 */
  }
}

.autocomplete__trigger:hover {
  border-color: rgb(163 163 163); /* neutral-400 */
}

.autocomplete__trigger:focus {
  outline: none;
  border-color: rgb(82 82 82); /* neutral-600 */
  box-shadow: 0 0 0 2px rgb(82 82 82 / 0.2); /* ring-neutral-600/20 */
}

/* Trigger with inline selections */
.autocomplete__trigger-selections {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.25rem;
  overflow: hidden;
  flex: 1;
  align-items: center;
}

.autocomplete__trigger-placeholder {
  color: rgb(163 163 163); /* neutral-400, matches placeholder:text-on-surface-var */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete__trigger-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.125rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 500;
  background-color: rgb(23 23 23); /* neutral-900 */
  color: #fff;
  border-radius: 9999px;
  white-space: nowrap;
  max-width: 120px;
  flex-shrink: 0;
}

.autocomplete__trigger-badge-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete__trigger-badge-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  font-size: var(--text-xs);
  font-weight: 600;
  border-radius: 9999px;
  background-color: rgb(229 229 229); /* neutral-200 */
  color: rgb(23 23 23); /* neutral-900 */
  flex-shrink: 0;
}

.autocomplete__trigger-more {
  font-size: 0.75rem;
  color: rgb(115 115 115); /* neutral-500 */
  white-space: nowrap;
  flex-shrink: 0;
}

/* Rich trigger item display (image + title + subtitle) */
.autocomplete__trigger-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  flex: 1;
}

.autocomplete__trigger-item-image {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 9999px;
  flex-shrink: 0;
  object-fit: cover;
}

.autocomplete__trigger-item-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  font-size: var(--text-xs);
  font-weight: 600;
  border-radius: 9999px;
  background-color: rgb(23 23 23); /* neutral-900 */
  color: #fff;
  flex-shrink: 0;
}

.autocomplete__trigger-item-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.2;
}

.autocomplete__trigger-item-title {
  font-weight: 600;
  font-size: 0.875rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: rgb(23 23 23); /* neutral-900 */
}

.autocomplete__trigger-item-subtitle {
  font-size: 0.75rem;
  color: rgb(115 115 115); /* neutral-500 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Legacy trigger text (for backwards compatibility) */
.autocomplete__trigger-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: rgb(23 23 23); /* neutral-900 */
}

.autocomplete__trigger-icon {
  flex-shrink: 0;
  margin-left: 0.5rem;
  color: rgb(163 163 163); /* neutral-400 */
  transition: transform 0.15s ease;
}

/* Hide trigger when expanded */
.autocomplete[data-expanded] .autocomplete__trigger {
  display: none;
}

/* Hide input wrapper and selections when collapsed (only for collapsible mode) */
.autocomplete:not([data-expanded]) .autocomplete__input-wrapper {
  /* Only hide if we have a trigger (collapsible mode) */
}

.autocomplete:has(.autocomplete__trigger):not([data-expanded]) .autocomplete__input-wrapper {
  display: none;
}

.autocomplete:has(.autocomplete__trigger):not([data-expanded]) .autocomplete__selections {
  display: none;
}

/* Swap mode — one record shown read-only with Change; Cancel keeps it. Sized like Forms::InputComponent :sm. */
.autocomplete__current {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  padding: 0.25rem 0.25rem 0.25rem 0.75rem;
  background-color: #fff;
  border: 1px solid rgb(229 229 229); /* neutral-200 */
  border-radius: 0.5rem;
}

@media (min-width: 640px) {
  .autocomplete__current {
    min-height: 2.5rem;
  }
}

.autocomplete__current-record {
  display: flex;
  flex: 1;
  min-width: 0;
}

.autocomplete__search {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.autocomplete__search .autocomplete__input-wrapper {
  flex: 1;
}

.autocomplete__current[hidden],
.autocomplete__search[hidden],
.autocomplete__swap-button[hidden] {
  display: none;
}

.autocomplete__swap-button {
  flex-shrink: 0;
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: rgb(64 64 64); /* neutral-700 */
  border-radius: 0.375rem;
  transition: background-color 0.15s ease;
}

.autocomplete__swap-button:hover {
  background-color: rgb(245 245 245); /* neutral-100 */
}

/* Rotate chevron when expanded */
.autocomplete[data-expanded] .autocomplete__trigger-icon {
  transform: rotate(180deg);
}

/* Selections Container */
.autocomplete__selections {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.autocomplete__selections:empty,
.autocomplete__selections[hidden] {
  display: none;
}

/* Selection Chips */
.autocomplete__chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  /* Extra left padding so the text isn't cramped against the edge: it visually
     balances the right side, where the centered glyph in the 1.25rem remove button
     adds whitespace beyond the 0.5rem padding. */
  padding: 0.25rem 0.5rem 0.25rem 0.75rem;
  font-size: 0.875rem;
  background-color: rgb(245 245 245); /* neutral-100 */
  border: 1px solid rgb(229 229 229); /* neutral-200 */
  border-radius: 9999px;
  color: rgb(23 23 23); /* neutral-900 */
}

.autocomplete__chip-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  font-size: var(--text-xs);
  font-weight: 600;
  border-radius: 9999px;
  background-color: rgb(23 23 23); /* neutral-900 */
  color: #fff;
}

.autocomplete__chip-text {
  font-weight: 500;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete__chip-remove {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  margin-left: 0.125rem;
  padding: 0;
  border: none;
  background: transparent;
  color: rgb(163 163 163); /* neutral-400 */
  border-radius: 9999px;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.autocomplete__chip-remove:hover {
  background-color: rgb(229 229 229); /* neutral-200 */
  color: rgb(23 23 23); /* neutral-900 */
}

.autocomplete__chip-remove:focus {
  outline: 2px solid rgb(82 82 82); /* neutral-600 */
  outline-offset: 1px;
}

/* Rich selection chips — mirror the search-result row (avatar/image + title +
   description) with a remove button, instead of a compact pill. Opt-in per
   autocomplete via the component's rich_chips:. Reuses the .autocomplete__result-*
   atoms so a selected pill looks exactly like its result row. */
.autocomplete__selections--rich {
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  gap: 0.375rem;
}

.autocomplete__chip--rich {
  width: 100%;
  gap: 0.75rem;
  padding: 0.375rem 0.5rem 0.375rem 0.625rem;
  border-radius: 0.5rem; /* override the pill radius */
}

.autocomplete__chip--rich .autocomplete__result-content {
  flex: 1;
  min-width: 0;
}

.autocomplete__chip--rich .autocomplete__chip-remove {
  flex-shrink: 0;
}

/* Input Wrapper */
.autocomplete__input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.autocomplete__input {
  width: 100%;
  height: 2.75rem; /* h-11 mobile */
  padding: 0 2rem 0 0.75rem;
  font-size: 0.875rem;
  background-color: #fff;
  border: 1px solid rgb(229 229 229); /* neutral-200 */
  border-radius: 0.5rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

@media (min-width: 640px) {
  .autocomplete__input {
    height: 2.5rem; /* sm:h-10 */
  }
}

.autocomplete__input:hover {
  border-color: rgb(163 163 163); /* neutral-400 */
}

.autocomplete__input:focus {
  outline: none;
  border-color: rgb(82 82 82); /* neutral-600 */
  box-shadow: 0 0 0 2px rgb(82 82 82 / 0.2); /* ring-neutral-600/20 */
}

.autocomplete__input::placeholder {
  color: rgb(163 163 163); /* neutral-400 */
}

/* Clear Button */
.autocomplete__clear {
  position: absolute;
  right: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  border: none;
  background: transparent;
  color: rgb(163 163 163); /* neutral-400 */
  border-radius: 0.25rem;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.autocomplete__clear:hover {
  background-color: rgb(245 245 245); /* neutral-100 */
  color: rgb(38 38 38); /* neutral-800 */
}

.autocomplete__clear:focus {
  outline: 2px solid rgb(82 82 82); /* neutral-600 */
  outline-offset: 1px;
}

.autocomplete__clear[hidden] {
  display: none;
}

/* Spinner */
.autocomplete__spinner {
  position: absolute;
  right: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  color: var(--color-on-surface-var);
}

.autocomplete__spinner[hidden] {
  display: none;
}

/* Results Dropdown — sized to its content: never narrower than the input, never
   wider than 300px, so a long email widens the panel instead of truncating. */
.autocomplete__results {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 100%;
  width: max-content;
  max-width: 300px;
  z-index: 50;
  margin-top: 0.25rem;
  max-height: 16rem;
  overflow-y: auto;
  background-color: #fff;
  border: 1px solid rgb(229 229 229); /* neutral-200 */
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

.autocomplete__results[hidden] {
  display: none;
}

/* Result Items */
.autocomplete__result {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  transition: background-color 0.1s ease;
}

.autocomplete__result:hover,
.autocomplete__result--active {
  background-color: rgb(250 250 250); /* neutral-50 */
}

.autocomplete__result:focus {
  outline: none;
  background-color: rgb(245 245 245); /* neutral-100 */
}

.autocomplete__result:first-child {
  border-radius: 0.5rem 0.5rem 0 0;
}

.autocomplete__result:last-child {
  border-radius: 0 0 0.5rem 0.5rem;
}

.autocomplete__result:only-child {
  border-radius: 0.5rem;
}

/* Result Avatar */
.autocomplete__result-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  flex-shrink: 0;
  border-radius: 9999px;
  background-color: rgb(23 23 23); /* neutral-900 */
}

.autocomplete__result-avatar span {
  font-size: var(--text-xs);
  font-weight: 500;
  color: #fff;
}

/* Result Image */
.autocomplete__result-image {
  width: 2rem;
  height: 2rem;
  flex-shrink: 0;
  object-fit: cover;
  border-radius: 9999px;
}

/* Result Content */
.autocomplete__result-content {
  flex: 1;
  min-width: 0;
}

/* Option text follows the type scale: a menu item's name is text-sm, its detail text-xs. */
.autocomplete__result-title {
  font-size: var(--text-sm);
  font-weight: 500;
  color: rgb(23 23 23); /* neutral-900 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete__result-description {
  font-size: var(--text-xs);
  color: rgb(115 115 115); /* neutral-500 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* No Results */
.autocomplete__no-results {
  padding: 0.75rem;
  font-size: var(--text-sm);
  text-align: center;
  color: rgb(115 115 115); /* neutral-500 */
}

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* Mobile Responsiveness */
@media (max-width: 640px) {
  .autocomplete__chip-text {
    max-width: 100px;
  }

  .autocomplete__input {
    font-size: 1rem; /* Prevent zoom on iOS */
  }

  .autocomplete__trigger {
    font-size: 1rem; /* Prevent zoom on iOS */
  }

  .autocomplete__trigger-badge {
    max-width: 80px;
  }

  .autocomplete__trigger-item-title {
    font-size: 0.875rem;
  }

  .autocomplete__trigger-item-subtitle {
    font-size: var(--text-xs);
  }

  .autocomplete__results {
    max-height: 12rem;
  }
}
