/* Subsprint 8.10 — sökbara dropdowns.
 * 8.11 (2026-05-09): panel-uppgraderingen — bredare/högre listbox,
 * z-index ovanför sysparter (list-region), multi-select-stöd
 * (chip-input + checkbox-rader) och tydligare optgroup-separatorer.
 *
 * Plain CSS (not compiled by Tailwind) for the same reason as
 * motion.css / celebrations.css: this stylesheet ships static
 * primitives that the page-level Tailwind utilities (field-input,
 * arende-filter-bar__select, etc.) decorate but don't replace.
 * Loaded after motion.css so .surface-card glass-styling is
 * available when JS appends the listbox.
 */

.combobox {
  position: relative;
  display: block;
}

/* Make sure the synthetic input fills the field row's width. The
 * select-mode replacement input inherits the native select's classes,
 * but those classes assume the element behaves like a select so we
 * normalise here. */
.combobox--select .combobox__input {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
}

/* Chevron + room for the toggle button. The button is absolute-
 * positioned over the input's right edge; padding-right keeps the
 * caret/text from colliding with the chevron. */
.combobox__input {
  padding-right: 2rem;
}

.combobox__toggle {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  font-size: 0.85em;
  line-height: 1;
  border-radius: 0.25rem;
}

.combobox__toggle:hover {
  color: var(--text);
}

.combobox__toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.combobox.is-open .combobox__toggle {
  color: var(--accent);
}

/* Listbox panel — already carries .surface-card from JS so the glass
 * tokens (--glass-bg, --glass-border, backdrop-filter) defined in
 * motion.css apply. We layer typography + spacing on top.
 *
 * 8.11: panelen var tidigare 16rem max-höjd och bara så bred som
 * fältet — det gjorde långa listor (43 stadier) påtagligt jobbiga.
 * Nu: ungefär halva viewporten, alltid minst 22rem bred, och z-index
 * högt nog för att inte hamna under list-region-kortet. */
.combobox__listbox {
  position: absolute;
  z-index: 60;
  top: calc(100% + 4px);
  left: 0;
  /* Don't constrain to right:0 — let min-width win when the input
   * is narrow (filter-bar fält är ~14rem). */
  min-width: max(22rem, 100%);
  max-width: min(36rem, 92vw);
  margin: 0;
  padding: 0.35rem;
  list-style: none;
  max-height: min(60vh, 28rem);
  overflow-y: auto;
  font-size: 0.9rem;
  /* Match the input's text colour so disabled/empty hint text doesn't
   * inherit a contrasting tone. */
  color: var(--text);
  /* Sharper rounded corners than the default surface-card so the
   * popup feels distinct from inline cards. */
  border-radius: 0.6rem;
  box-shadow: 0 12px 32px -8px color-mix(in srgb, black 35%, transparent);
}

.combobox__option {
  padding: 0.5rem 0.7rem;
  border-radius: 0.4rem;
  cursor: pointer;
  user-select: none;
  /* Slightly bigger touch target — was 0.4rem padding. */
  line-height: 1.35;
}

.combobox__option:hover,
.combobox__option.is-active {
  background: color-mix(in oklch, var(--accent), transparent 80%);
  color: var(--text-strong);
}

.combobox__option[aria-selected="true"] {
  font-weight: 600;
}

/* Single-select check mark. Multi-mode uses a real checkbox in
 * the row instead, so disable the ::after marker there. */
.combobox:not(.combobox--multi) .combobox__option[aria-selected="true"]::after {
  content: " ✓";
  color: var(--accent);
}

/* Group / "Senaste"-separator label. role=presentation so it doesn't
 * show up as an option in the AT tree. */
.combobox__sep {
  padding: 0.55rem 0.7rem 0.25rem;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
  pointer-events: none;
  position: sticky;
  top: -0.35rem;
  /* Sticky group headers in long lists — användaren ser alltid
   * vilket spår de scrollar i. Bakgrund matchar listbox-glaset. */
  background: var(--glass-bg, var(--surface));
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 1;
}

.combobox__sep:not(:first-child) {
  border-top: 1px solid var(--border-soft);
  margin-top: 0.35rem;
  padding-top: 0.5rem;
}

.combobox__empty {
  padding: 0.6rem 0.7rem;
  color: var(--muted);
  font-style: italic;
  pointer-events: none;
}

/* --- Multi-select (8.11) -------------------------------------------------
 *
 * Multi-mode mirror — the native <select multiple> stays in DOM (form posts
 * it), JS hides it and builds:
 *
 *   <button data-combobox-input>     // shows summary text + chevron
 *   <ul role="listbox" aria-multiselectable=true>
 *     <li role="option">              // each row has a synthetic checkbox
 *
 * Each option-row is a label-style row with a checkbox-square on the left
 * and the option text. Click anywhere on the row toggles. */

.combobox--multi .combobox__option {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.combobox--multi .combobox__check {
  flex: 0 0 1rem;
  width: 1rem;
  height: 1rem;
  border-radius: 0.25rem;
  border: 1.5px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  color: transparent;
  font-size: 0.8rem;
  line-height: 1;
  transition: background var(--motion-fast) var(--motion-ease),
              border-color var(--motion-fast) var(--motion-ease),
              color var(--motion-fast) var(--motion-ease);
}

.combobox--multi .combobox__option[aria-selected="true"] .combobox__check {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast, white);
}

.combobox--multi .combobox__option[aria-selected="true"] .combobox__check::before {
  content: "✓";
}

/* Multi-mode summary input is rendered as a button-like text field with
 * chip-row inside it. The input itself is read-only typography. */
.combobox--multi .combobox__input {
  text-align: left;
  cursor: pointer;
  background: var(--surface);
  /* Prevent text-cursor — this is button territory. */
  caret-color: transparent;
}

.combobox--multi .combobox__input::placeholder {
  color: var(--muted);
}

/* Sticky action-bar in multi-mode listbox — "Rensa" + "Stäng" knappar. */
.combobox__actions {
  position: sticky;
  bottom: -0.35rem;
  background: var(--glass-bg, var(--surface));
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-top: 1px solid var(--border-soft);
  margin-top: 0.5rem;
  padding: 0.4rem 0.55rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  z-index: 2;
}

.combobox__actions-count {
  color: var(--muted);
}

.combobox__actions-clear {
  background: none;
  border: 0;
  color: var(--muted);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--muted) 35%, transparent);
  text-underline-offset: 3px;
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0.2rem 0.3rem;
  border-radius: 0.25rem;
}

.combobox__actions-clear:hover {
  color: var(--text);
}

.combobox__actions-clear:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Hide the native control once the combobox has taken over. We can't
 * use ``display:none`` because some browsers refuse to submit values
 * from non-visible selects in older quirks-mode contexts; the
 * sr-only-style absolute positioning is universally accepted. */
.combobox__native--hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
  opacity: 0 !important;
  pointer-events: none !important;
}
