/*
 * Clonable Search Widget Styles
 *
 * This file contains the default CSS styles for the Clonable search widget.
 * These styles can be disabled via the - Disable default search widget styling -
 * option in the Clonable settings.
 */

/* --- NORMALISATION ----------------------------------------------------------
   Resets common theme/plugin overrides so our own rules start from a
   predictable baseline.
   --------------------------------------------------------------------------- */

.clonable-search-widget-wrapper *,
.clonable-search-widget-wrapper *::before,
.clonable-search-widget-wrapper *::after {
  box-sizing: border-box;
}

.clonable-search-widget-wrapper input[data-clonable-search] {
  -webkit-appearance: none;
  appearance: none;
  box-shadow: none;
  margin: 0;
}

.clonable-search-widget-wrapper p {
  margin: 0;
  padding: 0;
}

.clonable-search-widget-wrapper a {
  color: inherit;
  text-decoration: none;
}

/* --- THEME TOKENS -----------------------------------------------------------
   Reads WordPress block-theme preset variables where available and derives
   all widget colours from them. Override any --csw-* token on
   .clonable-search-widget-wrapper to customise without touching the rules.
   --------------------------------------------------------------------------- */

.clonable-search-widget-wrapper {
  /* Source tokens - map to WP presets, fall back to neutral defaults */
  --csw-text:   var(--wp--preset--color--foreground, #1f2328);
  --csw-bg:     var(--wp--preset--color--background, #ffffff);
  --csw-accent: var(--wp--preset--color--primary,    #2563eb);

  /* Static fallbacks - active in browsers without color-mix() support */
  --csw-border:     rgba(31, 35, 40, 0.20);
  --csw-muted:      rgba(31, 35, 40, 0.55);
  --csw-score:      rgba(31, 35, 40, 0.35);
  --csw-divider:    rgba(31, 35, 40, 0.10);
  --csw-focus-ring: rgba(37, 99, 235, 0.15);
}

@supports (color: color-mix(in srgb, black, white)) {
  .clonable-search-widget-wrapper {
    --csw-border:     color-mix(in srgb, var(--csw-text) 20%, transparent);
    --csw-muted:      color-mix(in srgb, var(--csw-text) 55%, var(--csw-bg));
    --csw-score:      color-mix(in srgb, var(--csw-text) 35%, var(--csw-bg));
    --csw-divider:    color-mix(in srgb, var(--csw-text) 10%, transparent);
    --csw-focus-ring: color-mix(in srgb, var(--csw-accent) 15%, transparent);
  }
}

/* --- WIDGET --------------------------------------------------------------- */

.clonable-search-widget-wrapper {
  width: 100%;
  max-width: 500px;
}

/* --- SEARCH INPUT --------------------------------------------------------- */

.clonable-search-widget-wrapper .clonable-search-input-wrapper {
  position: relative;
  display: block;
}

.clonable-search-widget-wrapper input[data-clonable-search] {
  width: 100%;
  padding: 0.75rem 1rem 0.75rem 2.5rem;
  border: 1px solid var(--csw-border);
  border-radius: 6px;
  background-color: var(--csw-bg);
  color: var(--csw-text);
  font-size: 1rem;
  line-height: 1.5;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.clonable-search-widget-wrapper input[data-clonable-search]::placeholder {
  color: var(--csw-muted);
}

/* focus-visible: ring only on keyboard navigation, not mouse clicks */
.clonable-search-widget-wrapper input[data-clonable-search]:focus-visible {
  border-color: var(--csw-accent);
  outline: none;
  box-shadow: 0 0 0 3px var(--csw-focus-ring);
}

/* --- RESULTS CONTAINER ---------------------------------------------------- */

.clonable-search-widget-wrapper [data-clonable-search-results] {
  border: 1px solid var(--csw-border);
  border-radius: 6px;
  margin-top: 0.5rem;
  max-height: 420px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--csw-border) transparent;
}

.clonable-search-widget-wrapper [data-clonable-search-results]:empty {
  display: none;
}

/* --- RESULT ROWS ---------------------------------------------------------- */

.clonable-search-widget-wrapper .clonable-search-result-row {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.625rem 0.875rem;
  border-bottom: 1px solid var(--csw-divider);
}

.clonable-search-widget-wrapper .clonable-search-result-row:last-child {
  border-bottom: none;
}

/* Text column - grows to fill remaining space; min-width:0 prevents
   long titles/descriptions from overflowing the flex container */
.clonable-search-widget-wrapper .clonable-search-result-row .clonable-search-result-text {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--csw-text);
  text-decoration: none;
  transition: color 0.2s ease-in-out;
}

.clonable-search-widget-wrapper .clonable-search-result-text:is(:hover, :focus-visible) {
  color: var(--csw-accent);
  text-decoration: underline;
  outline: none;
}

.clonable-search-widget-wrapper .clonable-search-result-title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Image column - fixed-size square thumbnail */
.clonable-search-widget-wrapper .clonable-search-result-image {
  flex: 0 0 72px;
  align-self: flex-start;
}

/* When JS removes the <img> (no thumbnail), the <a> becomes childless and
   collapses so it doesn't reserve space or create a dead link. */
.clonable-search-widget-wrapper .clonable-search-result-image:empty {
  display: none;
}

.clonable-search-widget-wrapper .clonable-search-result-image img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 4px;
  display: block;
}

/* --- DESCRIPTION ---------------------------------------------------------- */

.clonable-search-widget-wrapper p[data-description_excerpt] {
  font-size: 0.85rem;
  font-weight: 400;
  line-height: 1.6;
  color: var(--csw-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* --- PRODUCT META (sku / price / stock) ----------------------------------- */

.clonable-search-widget-wrapper .clonable-search-result-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem 0.6rem;
  font-weight: 400;
  font-size: 0.8rem;
  color: var(--csw-muted);
}

/* Hide individual fields with no value */
.clonable-search-widget-wrapper .clonable-search-result-meta > span:empty {
  display: none;
}

/* Collapse the entire meta row when all child fields are empty
   (non-product results). Falls back to the per-span rule above
   in browsers without :has() support. */
.clonable-search-widget-wrapper
  .clonable-search-result-meta:not(:has(> span:not(:empty))) {
  display: none;
}

.clonable-search-widget-wrapper .clonable-search-result-price {
  font-weight: 600;
  color: var(--csw-text);
}

/* --- MISC ELEMENTS -------------------------------------------------------- */

/* Dev-only score badge */
.clonable-search-widget-wrapper [data-score] {
  font-size: 0.75rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  color: var(--csw-score);
  margin-top: 0.15rem;
}

/* Fuse.js highlight marks */
.clonable-search-widget-wrapper .clonable-search-result-row mark {
  background-color: rgba(255, 213, 0, 0.4);
  border-radius: 2px;
  padding: 0 1px;
}

/* --- NO RESULTS ----------------------------------------------------------- */

.clonable-search-widget-wrapper .clonable-search-no-results {
  padding: 1.5rem;
  text-align: center;
  color: var(--csw-muted);
}

.clonable-search-widget-wrapper .clonable-search-no-results.hint {
  font-style: italic;
}

/* --- RESPONSIVE ----------------------------------------------------------- */

/* Narrow mobile (≤ 440px): reduce thumbnail and container height */
@media (max-width: 440px) {
  .clonable-search-widget-wrapper [data-clonable-search-results] {
    max-height: 360px;
  }

  .clonable-search-widget-wrapper .clonable-search-result-image {
    flex-basis: 52px;
  }

  .clonable-search-widget-wrapper .clonable-search-result-image img {
    width: 52px;
    height: 52px;
  }

  .clonable-search-widget-wrapper .clonable-search-result-row {
    gap: 1rem;
    padding: 0.5rem 0.625rem;
  }
}

/* Very narrow (≤ 320px): stack image above text */
@media (max-width: 320px) {
  .clonable-search-widget-wrapper .clonable-search-result-row {
    flex-wrap: wrap;
  }

  .clonable-search-widget-wrapper .clonable-search-result-image {
    flex: 0 0 100%;
    order: -1;
  }

  .clonable-search-widget-wrapper .clonable-search-result-image img {
    width: 100%;
    height: auto;
    max-height: 140px;
    object-fit: contain;
  }
}
