.search-bar {
  position: relative;
  flex: 1;
  min-width: 260px;
}

.search {
  display: flex;
  gap: var(--space-2);
  align-items: stretch;
}

/* One box holding the validated ingredients and the field for the next one. */
.search__control {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--card-bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-2);
  box-shadow: var(--shadow-1);
  transition: box-shadow var(--normal) var(--ease), border-color var(--normal) var(--ease);
}

.search__control:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 14%, transparent);
  outline: none;
}

.search__icon {
  display: inline-flex;
  color: var(--muted);
}

.search__tags {
  display: contents;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Without JS the field already holds the comma separated list: showing tags too would repeat it. */
@media (scripting: none) {
  .search__tags {
    display: none;
  }
}

.search__tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 3px var(--space-2) 3px var(--space-3);
  background: var(--accent-bg);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: var(--radius-pill);
  color: var(--accent);
  font-size: var(--text-sm);
  font-weight: 600;
  white-space: nowrap;
}

/* Set by the search controller the moment the tag is clicked, before the server answers. */
.search__tag[data-removing] {
  display: none;
}

.search__tag-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  color: inherit;
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease);
}

.search__tag-remove:hover {
  background: var(--accent);
  color: var(--card-bg);
}

.search__input {
  flex: 1;
  min-width: 140px;
  padding: var(--space-2) 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: var(--text-md);
}

.search__input:focus {
  outline: none;
}

.search__submit {
  padding: var(--space-2) var(--space-5);
  border: 1px solid var(--accent);
  border-radius: var(--radius-2);
  background: var(--accent);
  color: var(--card-bg);
  font-family: inherit;
  font-size: var(--text-md);
  font-weight: 600;
  white-space: nowrap;
  transition: background var(--fast) var(--ease), transform var(--fast) var(--ease);
}

.search__submit:hover {
  background: color-mix(in srgb, var(--accent) 88%, black);
}

.search__submit:active {
  transform: translateY(1px);
}

/* The suggestions sit under the box, as a vertical list walked with the arrow keys. */
.suggestions {
  display: none;
  position: absolute;
  top: calc(100% + var(--space-2));
  left: 0;
  right: 0;
  z-index: 20;
  max-height: 340px;
  overflow-y: auto;
  padding: var(--space-1);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-3);
  box-shadow: var(--shadow-3);
}

.search-bar--open .suggestions {
  display: block;
  animation: suggestions-in var(--normal) var(--ease);
}

@keyframes suggestions-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

.suggestion {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-2);
  color: var(--text);
  font-size: var(--text-md);
  text-decoration: none;
}

.suggestion__add {
  display: inline-flex;
  opacity: 0;
  color: var(--accent);
  transition: opacity var(--fast) var(--ease);
}

.suggestion:hover,
.suggestion--active {
  background: var(--accent-bg);
  color: var(--accent);
}

.suggestion:hover .suggestion__add,
.suggestion--active .suggestion__add {
  opacity: 1;
}

@media (max-width: 767px) {
  /* Too narrow to share a row: field, button and each tag get a line of their own. */
  .search {
    flex-direction: column;
  }

  .search__submit {
    width: 100%;
    padding: var(--space-3) var(--space-5);
  }

  .search__tag {
    flex: 0 0 100%;
    justify-content: space-between;
  }

  /* After the tags, so the magnifier stays next to the field. */
  .search__icon {
    order: 1;
  }

  .search__input {
    order: 2;
    width: 100%;
  }
}

/* On a phone the suggestions cover the screen until an ingredient is validated. */
@media (max-width: 767px) {
  .site-header:has(.search-bar--open) {
    position: fixed;
    inset: 0;
    z-index: 100;
    flex-direction: column;
    align-items: stretch;
    background: var(--bg);
    backdrop-filter: none;
    border-bottom: 0;
  }

  .site-header:has(.search-bar--open) .search-bar {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .site-header:has(.search-bar--open) .suggestions {
    position: static;
    flex: 1;
    max-height: none;
    margin-top: var(--space-3);
    padding: var(--space-2) 0 0;
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 0;
    box-shadow: none;
    animation: none;
  }

  body.is-searching {
    overflow: hidden;
  }
}
