/* chops-search — default styles.
 *
 * Plain CSS on purpose. Zola compiles Sass from `sass/`, but
 * `chops-search build` writes into `static/`, which Zola copies verbatim,
 * so a .scss shipped here would never be compiled. Custom properties are
 * the theming interface instead.
 *
 * Deliberately unopinionated: no colours of its own, no fonts. Everything
 * derives from `currentColor` and the surrounding text, so it inherits a
 * light or dark theme with no media query and no second ruleset. The one
 * exception is the overlay panel's background, which has to be opaque.
 *
 *   .chops {
 *     --chops-accent: var(--my-link-color);
 *     --chops-panel-bg: #14161a;
 *   }
 */

.chops {
  --chops-accent: currentColor;
  --chops-muted: 0.7;
  --chops-gap: 0.75rem;
  --chops-radius: 6px;
  /* The panel wants a softer corner than the elements inside it: a large
     surface with the same radius as a 2rem-tall input reads as sharp. */
  --chops-panel-radius: 1rem;
  --chops-mark: 18%;
  --chops-hover: 8%;
  --chops-panel-bg: Canvas;
}

/* ---- search bar ----------------------------------------------------- */

/* The bar carries the border, not the input. Bordering the input would
 * put the icon and clear button outside the visual field they belong to. */
.chops-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.15rem 0.5rem;
  border: 1px solid color-mix(in srgb, currentColor 30%, transparent);
  border-radius: calc(var(--chops-radius) * 2);
}

.chops-bar:focus-within {
  border-color: var(--chops-accent);
  outline: 1px solid var(--chops-accent);
}

.chops-bar input[type='search'] {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0;
  font: inherit;
  color: inherit;
  background: transparent;
  border: 0;
  outline: none;
  /* WebKit adds its own ✕ inside type=search, which would sit beside
   * ours and clear without telling the script. */
  appearance: none;
}

.chops-bar input[type='search']::-webkit-search-cancel-button {
  display: none;
}

.chops-icon,
.chops-clear {
  display: grid;
  place-items: center;
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  color: inherit;
}

.chops-icon {
  opacity: var(--chops-muted);
  pointer-events: none;
}

.chops-icon svg,
.chops-clear svg {
  width: 100%;
  height: 100%;
}

.chops-clear {
  padding: 0;
  background: none;
  border: 0;
  cursor: pointer;
  opacity: var(--chops-muted);
}

.chops-clear:hover,
.chops-clear:focus-visible {
  opacity: 1;
}

.chops-clear[hidden] {
  display: none;
}

/* ---- status line ---------------------------------------------------- */

/* Announces result counts and which engine answered. Visible rather than
 * screen-reader-only: keyword-vs-hybrid is genuinely useful information,
 * and hiding it wastes the one honest signal this search box has. */
.chops-mode {
  display: inline-block;
  margin-top: 0.35rem;
  font-size: 0.8rem;
  opacity: var(--chops-muted);
}

.chops-mode:empty {
  display: none;
}

/* ---- results -------------------------------------------------------- */

.chops-results {
  margin: var(--chops-gap) 0 0;
  padding: 0;
  list-style: none;
}

.chops-results:empty {
  display: none;
}

.chops-results > li {
  padding: 0.4rem 0.5rem;
  border-radius: var(--chops-radius);
}

/* The combobox cursor. The browser gives no free styling for
 * aria-selected, and without a visible cue the arrow keys move an
 * invisible focus. */
.chops-results > li[aria-selected='true'] {
  background: color-mix(in srgb, currentColor var(--chops-hover), transparent);
}

.chops-results a {
  color: var(--chops-accent);
  text-decoration: none;
}

.chops-results a:hover,
.chops-results a:focus-visible {
  text-decoration: underline;
}

/* ---- snippets ------------------------------------------------------- */

.chops-snippet {
  margin: 0.15rem 0 0;
  font-size: 0.85rem;
  line-height: 1.4;
  opacity: var(--chops-muted);
}

/* Snippets arrive a moment after the results, so this is briefly empty on
 * every query. Collapsing rather than reserving space costs a small
 * reflow; reserving it would leave a permanent gap on sites whose host
 * ignores range requests and never delivers snippets at all. */
.chops-snippet:empty {
  display: none;
}

.chops-snippet mark {
  color: inherit;
  background: color-mix(in srgb, currentColor var(--chops-mark), transparent);
  border-radius: 2px;
}

/* ---- overlay -------------------------------------------------------- */

/* Used when a page has no #chops-input and the script mounts its own
 * dialog. `hidden` does the showing and hiding, so there is no state to
 * keep in a class. */
.chops-overlay[hidden] {
  display: none;
}

.chops-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  justify-content: center;
  /* Not centred: a dialog that grows downward as results arrive would
   * jump around if it were vertically centred. */
  align-items: flex-start;
  padding: 10vh 1rem 1rem;
  background: color-mix(in srgb, Canvas 60%, transparent);
  backdrop-filter: blur(2px);
}

.chops-panel {
  width: min(38rem, 100%);
  max-height: 70vh;
  overflow-y: auto;
  padding: 1rem;
  border-radius: calc(var(--chops-radius) * 2);
  /* Opaque: results must stay readable over whatever the page had here. */
  background: var(--chops-panel-bg);
  border: 1px solid color-mix(in srgb, currentColor 15%, transparent);
  box-shadow: 0 1rem 3rem color-mix(in srgb, black 25%, transparent);
  border-radius: var(--chops-panel-radius);
}

/* Stops the page scrolling behind an open dialog. */
.chops-open,
.chops-open body {
  overflow: hidden;
}

@media (max-width: 34rem) {
  .chops-overlay {
    padding-top: 2vh;
  }
  .chops-panel {
    max-height: 90vh;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chops-overlay {
    backdrop-filter: none;
  }
  .chops * {
    transition: none !important;
  }
}

/* ---- class contract -------------------------------------------------
 *
 * What chops-search.js creates and expects. Restyle freely; renaming
 * means forking the page script.
 *
 *   .chops                       wrapper (also on the overlay panel)
 *   .chops-overlay               the dialog backdrop, when self-mounted
 *   .chops-panel                 the dialog itself
 *   .chops-open                  set on <html> while the dialog is open
 *   .chops-bar                   the search field row
 *   .chops-icon                  the magnifier
 *   .chops-clear                 clear when there is text, close when empty
 *   #chops-input                 the search input (role=combobox)
 *   #chops-mode / .chops-mode    status line (aria-live)
 *   #chops-results               result list (role=listbox)
 *   .chops-results > li          one result (role=option)
 *   .chops-snippet               matched text under a result
 *   .chops-snippet mark          highlighted query terms
 *
 * [data-chops-open]  opens the overlay on click, Enter, or Space.
 * [data-chops-clear] in INLINE mode, wires a host-supplied clear button.
 */
