/* ==========================================================================
   Collection picker — a multi-select field with a searchable popover.
   Markup: app/views/shared/_collection_picker.html.erb
   Behavior: app/javascript/controllers/collection_picker_controller.js

   Propshaft loads this file BEFORE list-blocks, main and utilities (see the
   note in application.css), so every rule that has to beat an element style
   from one of those — the search input, the reset buttons — carries two
   classes rather than relying on source order.
   ========================================================================== */

.collection-picker .picker-label {
  display: block;
  margin-bottom: var(--space-1);
  font-weight: var(--fw-bold);
  color: var(--vn-heading);
}

/* The popover is positioned against this, not against the whole picker — the
   hidden inputs and the option template are siblings of the field. */
.collection-picker .picker-anchor {
  position: relative;
  max-width: var(--input-max-width);
}


/* --- The field (closed state) --------------------------------------------- */

.collection-picker .picker-field {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  min-height: 2.75rem;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-field-border);
  border-radius: 0.625rem;
  background: var(--nieve);
  cursor: pointer;
}

.collection-picker .picker-field:hover,
.collection-picker[data-open="true"] .picker-field {
  border-color: var(--pajaro-azul);
}

.collection-picker .picker-chips {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  gap: 0.4375rem;
}

.collection-picker .picker-placeholder {
  font-size: var(--fs-meta);
  line-height: 1.625rem;
  color: #aaa;
}

.collection-picker .picker-caret {
  flex: none;
  color: var(--color-caret);
  transition: transform 0.15s ease-out;
}

.collection-picker[data-open="true"] .picker-caret {
  transform: rotate(180deg);
}

/* The × on a chip. Has to climb out of the global BUTTON style in
   utilities.css, which loads after this file. */
.collection-picker .picker-chip-remove {
  display: inline-flex;
  padding: 0;
  border: none;
  border-radius: 0;
  background: none;
  box-shadow: none;
  line-height: 0;
  color: inherit;
  opacity: 0.75;
  cursor: pointer;
}

.collection-picker .picker-chip-remove:hover {
  background: none;
  opacity: 1;
}


/* --- Button presentation -------------------------------------------------- */

/* `display: :button` — a trigger and nothing else, for a picker whose current
   selection is already shown by the page around it. The anchor shrink-wraps
   the trigger instead of stretching to a field's width, and the popover hangs
   off the button's right edge rather than matching its (much narrower) width.

   The trigger is a real <button>, so utilities.css's BUTTON rules give it the
   secondary fill; nothing here has to restate them. */
.collection-picker.picker-as-button .picker-anchor {
  display: inline-block;
  max-width: none;
}

/* The label names one action; breaking it across two lines makes the trigger
   twice as tall as the heading beside it. */
.collection-picker.picker-as-button .picker-trigger {
  white-space: nowrap;
}

.collection-picker.picker-as-button .picker-popover {
  right: 0;
  left: auto;
  width: 22rem;
}

/* Below 24rem of content column a fixed-width popover would overflow the
   panel it hangs in. */
@media (max-width: 30rem) {
  .collection-picker.picker-as-button .picker-popover {
    width: min(22rem, 80vw);
  }
}


/* --- The popover (open state) --------------------------------------------- */

.collection-picker .picker-popover {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  left: 0;
  z-index: 9;
  overflow: hidden;
  border: 1px solid var(--color-field-border);
  border-radius: 0.875rem;
  background: var(--nieve);
  box-shadow: var(--shadow-popover);
}

.collection-picker[data-open="false"] .picker-popover {
  display: none;
}

.collection-picker .picker-search {
  padding: 0.625rem;
  border-bottom: 1px solid var(--color-popover-divider);
}

.collection-picker .picker-search input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-field-border);
  border-radius: var(--radius-md);
  background: var(--color-field-fill);
  font: inherit;
  font-size: var(--fs-meta);
  color: var(--color-text);
}

.collection-picker .picker-search input:focus {
  border-color: var(--pajaro-azul);
  background: var(--nieve);
}

.collection-picker .picker-list {
  max-height: 14.75rem;
  overflow-y: auto;
  padding: var(--space-1) 0;
}


/* --- Rows ----------------------------------------------------------------- */

.collection-picker .picker-option {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.4375rem 0.875rem;
  cursor: pointer;
}

/* .picker-option sets a display, so the hidden attribute's UA rule needs
   restating for filtered-out rows. */
.collection-picker .picker-option[hidden] {
  display: none;
}

.collection-picker .picker-option:hover,
.collection-picker .picker-option[data-active="true"] {
  background: var(--niebla);
}

.collection-picker .picker-checkbox {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 1.125rem;
  height: 1.125rem;
  border: 1.5px solid var(--color-checkbox-border);
  border-radius: 0.3125rem;
  background: var(--nieve);
  color: transparent;
}

.collection-picker .picker-option[data-selected="true"] .picker-checkbox {
  border: 1px solid var(--pajaro-azul);
  background: var(--pajaro-azul);
  color: var(--trigo);
}

.collection-picker .picker-option-name {
  font-size: var(--fs-meta);
  line-height: 1.375rem;
  color: var(--hollin);
}

/* Selection is never carried by color alone — the check mark says it too. */
.collection-picker .picker-option[data-selected="true"] .picker-option-name {
  font-weight: 500;
  color: var(--crepusculo);
}

.collection-picker .picker-empty {
  padding: 0.625rem 0.875rem;
  font-size: var(--fs-meta);
  color: var(--color-text-muted);
}


/* --- Footer --------------------------------------------------------------- */

.collection-picker .picker-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.625rem;
  padding: 0.5625rem 0.875rem;
  border-top: 1px solid var(--color-popover-divider);
  background: var(--trigo);
}

.collection-picker .picker-count {
  font-size: 0.8125rem;
  color: var(--chocolate);
}

/* Leaves the page for the record's own "new" form — the popover is no place
   to be filling in a second record. */
.collection-picker .picker-create {
  flex: none;
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.4;
  color: var(--color-link);
  text-decoration: none;
}

.collection-picker .picker-create:hover {
  color: var(--color-link-hover);
  text-decoration: underline;
}


/* --- Narrow screens ------------------------------------------------------- */

@media (max-width: 48rem) {
  .collection-picker .picker-list {
    max-height: 12.5rem;
  }
}
