/**
 * WC Product Engine — Variant Selector & Accessible Swatches Styles
 *
 * Scoped design tokens (--wpe-sel-*) and defensive theme isolation (Stage 3, P026.3).
 * Preserves all P022.4 WCAG 2.2 AA contrast guarantees on light and dark themes.
 */

/* Scoped Storefront Design Tokens */
.wpe-variant-selector,
.wpe-product-instance,
.variations_form,
.wpe-swatches {
  --wpe-sel-surface: #ffffff;
  --wpe-sel-surface-hover: #f9f9f9;
  --wpe-sel-text: #222222;
  /* #8c8f94: 3.24:1 against white, WCAG 1.4.11 (P022.4). */
  --wpe-sel-border: #8c8f94;
  --wpe-sel-border-hover: #555555;
  --wpe-sel-accent: #0071a1;
  --wpe-sel-accent-surface: #f0f7fb;
  --wpe-sel-accent-text: #003750;
  --wpe-sel-disabled-border: #c3c4c7;
  --wpe-sel-disabled-surface: #f6f7f7;
  /* #646970 on #f6f7f7 is 5.1:1, WCAG 1.4.3 (P022.4). */
  --wpe-sel-disabled-text: #646970;
  /* Solid mark, 4.2:1 on unavailable background (P022.4). */
  --wpe-sel-unavailable-mark: #d63638;
  --wpe-sel-light-swatch-border: #787c82;
  --wpe-sel-notice-warn-bg: #fff8e5;
  --wpe-sel-notice-warn-border: #dba617;
  --wpe-sel-notice-warn-text: #614700;
  --wpe-sel-notice-error-bg: #fcf0f1;
  --wpe-sel-notice-error-border: #d63638;
  --wpe-sel-notice-error-text: #780004;
  --wpe-sel-radius: 4px;
  --wpe-sel-radius-image: 6px;
}

/* Container & Options */
.wpe-variant-selector {
  margin-bottom: 1.5rem;
  box-sizing: border-box;
}

.wpe-option-group {
  margin-bottom: 1.25rem;
  box-sizing: border-box;
}

.wpe-option-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  margin-bottom: 0.35rem;
  font-size: 0.95rem;
  line-height: 1.4;
  letter-spacing: normal;
  text-transform: none;
}

.wpe-selected-value-hint {
  font-weight: 400;
  /* Muted text follows the theme text colour, light or dark (P022.4). */
  color: inherit;
  color: color-mix(in srgb, currentColor 85%, transparent);
  margin-left: 0.5rem;
}

/* Help Text below Attribute Label (§4) */
.wpe-option-help-text {
  display: block;
  font-size: 0.8125rem;
  line-height: 1.4;
  font-weight: 400;
  letter-spacing: normal;
  text-transform: none;
  color: inherit;
  color: color-mix(in srgb, currentColor 85%, transparent);
  margin: 0.15rem 0 0.5rem 0;
}

.wpe-option-help-text:empty {
  display: none;
  margin: 0;
}

/* Single Price Display: Hide secondary variation price below options (§2).
 * Override required because WooCommerce variation JS toggles inline display styles. */
.wpe-single-price-active .woocommerce-variation-price,
.wpe-single-price-active .single_variation .price {
  display: none !important;
}

/* Swatches Group */
.wpe-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: stretch;
  max-width: 100%;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Base Swatch Button & Button Selector (§5) — isolated against hostile theme button rules */
.wpe-swatches .wpe-swatch.wpe-swatch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.5rem;
  min-height: 2.5rem;
  max-width: 100%;
  margin: 0;
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--wpe-sel-border);
  border-radius: var(--wpe-sel-radius);
  background-color: var(--wpe-sel-surface);
  background-image: none;
  color: var(--wpe-sel-text);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  font-style: normal;
  line-height: 1.25;
  letter-spacing: normal;
  text-transform: none;
  text-shadow: none;
  text-decoration: none;
  text-align: center;
  vertical-align: middle;
  word-break: break-word;
  box-shadow: none;
  cursor: pointer;
  transition: border-color 0.15s ease-in-out, background-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  user-select: none;
  box-sizing: border-box;
}

.wpe-swatches .wpe-swatch.wpe-swatch:hover:not([aria-disabled="true"]):not(.is-disabled):not(.is-selected):not([aria-checked="true"]) {
  border-color: var(--wpe-sel-border-hover);
  background-color: var(--wpe-sel-surface-hover);
  color: var(--wpe-sel-text);
}

/* Selected State (not communicated by color alone) */
.wpe-swatches .wpe-swatch.wpe-swatch[aria-checked="true"],
.wpe-swatches .wpe-swatch.wpe-swatch.is-selected {
  border: 2px solid var(--wpe-sel-accent);
  background-color: var(--wpe-sel-accent-surface);
  color: var(--wpe-sel-accent);
  font-weight: 600;
  box-shadow: 0 0 0 1px var(--wpe-sel-accent);
}

/* Focus Visible for Keyboard Accessibility */
.wpe-swatches .wpe-swatch.wpe-swatch:focus-visible {
  outline: 2px solid var(--wpe-sel-accent);
  outline-offset: 2px;
}

/* Disabled / Unavailable Value Base State (§8) */
.wpe-swatches .wpe-swatch.wpe-swatch[aria-disabled="true"],
.wpe-swatches .wpe-swatch.wpe-swatch.is-disabled,
.wpe-swatches .wpe-swatch.wpe-swatch.is-unavailable {
  /* No opacity: the name and thumbnail stay legible (docs/09-ui.md, mark_unavailable_options). */
  opacity: 1;
  cursor: not-allowed;
  border-color: var(--wpe-sel-disabled-border);
  background-color: var(--wpe-sel-disabled-surface);
  color: var(--wpe-sel-disabled-text);
}

/* Mark Unavailable Options enabled: diagonal cross-out while keeping text/thumbnail legible (§8) */
.wpe-swatches .wpe-swatch.is-marked-unavailable::after,
.wpe-swatches .wpe-swatch[aria-disabled="true"]:not(.is-disabled-standard)::after,
.wpe-swatches .wpe-swatch.is-disabled:not(.is-disabled-standard)::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 3px;
  right: 3px;
  height: 2px;
  background-color: var(--wpe-sel-unavailable-mark);
  transform: rotate(-28deg);
  pointer-events: none;
}

/* Mark Unavailable Options disabled: standard disabled state without extra decoration (§8) */
.wpe-swatches .wpe-swatch.wpe-swatch.is-disabled-standard::after {
  content: none;
  display: none;
}

/* Color Swatch (§6): pure circular swatch with no visible text label next to the circle */
.wpe-swatches .wpe-swatch.wpe-swatch-color {
  padding: 3px;
  width: 2.25rem;
  height: 2.25rem;
  min-width: 2.25rem;
  min-height: 2.25rem;
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  flex: 0 0 2.25rem;
}

.wpe-swatches .wpe-swatch.wpe-swatch-color:not(.wpe-swatch-color-fallback) .wpe-swatch-label {
  display: none;
}

.wpe-swatches .wpe-swatch.wpe-swatch-color[aria-checked="true"],
.wpe-swatches .wpe-swatch.wpe-swatch-color.is-selected {
  border-radius: 50%;
  background-color: var(--wpe-sel-surface);
  box-shadow: 0 0 0 2px var(--wpe-sel-surface), 0 0 0 4px var(--wpe-sel-accent);
}

.wpe-swatch-color-bubble {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
}

/* Contrasting border so white/light colors remain visible on light backgrounds (§6) */
.wpe-swatch-color-bubble.is-light-swatch {
  border: 1.5px solid var(--wpe-sel-light-swatch-border);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}

/* Color Swatch fallback to named button when hex color code is missing (§6) */
.wpe-swatches .wpe-swatch.wpe-swatch-color.wpe-swatch-color-fallback {
  width: auto;
  height: auto;
  min-width: 2.5rem;
  min-height: 2.5rem;
  aspect-ratio: auto;
  flex: 0 1 auto;
  padding: 0.35rem 0.75rem;
  border-radius: var(--wpe-sel-radius);
}

.wpe-swatches .wpe-swatch.wpe-swatch-color.wpe-swatch-color-fallback[aria-checked="true"],
.wpe-swatches .wpe-swatch.wpe-swatch-color.wpe-swatch-color-fallback.is-selected {
  border-radius: var(--wpe-sel-radius);
  background-color: var(--wpe-sel-accent-surface);
  box-shadow: 0 0 0 1px var(--wpe-sel-accent);
}

/* Suppress competing duplicate theme option buttons when WPE selector controls the form */
.variations_form:has(.wpe-swatches, .wpe-select, .wpe-selector-instance-config) .starterkit-live-options,
.wpe-product-instance .starterkit-live-options {
  display: none !important;
}

/* Image Swatch: always displays thumbnail + value label (§7) */
.wpe-swatches .wpe-swatch.wpe-swatch-image {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0.25rem;
  padding: 0.35rem;
  width: auto;
  min-width: 4.25rem;
  max-width: 6.75rem;
  height: auto;
  border-radius: var(--wpe-sel-radius-image);
  overflow: hidden;
}

.wpe-swatch-image-wrap {
  display: block;
  width: 3rem;
  height: 3rem;
  border-radius: var(--wpe-sel-radius);
  overflow: hidden;
  flex-shrink: 0;
  background-color: var(--wpe-sel-disabled-surface);
}

.wpe-swatches .wpe-swatch-image img,
.wpe-swatches .wpe-swatch-image .wpe-swatch-image-thumb {
  width: 100%;
  height: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
  border: 0;
  box-shadow: none;
  object-fit: cover;
  border-radius: 3px;
  display: block;
}

.wpe-swatch-image-label {
  display: block;
  font-size: 0.75rem;
  line-height: 1.2;
  text-align: center;
  word-break: break-word;
  max-width: 100%;
}

/* Screen-Reader Only text & Hidden Native Select (single control per attribute, §9) */
.wpe-sr-only,
.wpe-select-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;
}

/* Reset / Clear Button — isolated against aggressive theme button styles */
body button.wpe-reset-selection.wpe-reset-selection,
body .wpe-reset-selection.wpe-reset-selection {
  appearance: none;
  -webkit-appearance: none;
  display: inline-block;
  margin: 0.5rem 0 0 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  background-image: none;
  box-shadow: none;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 400;
  line-height: 1.4;
  letter-spacing: normal;
  text-transform: none;
  text-shadow: none;
  color: inherit;
  color: color-mix(in srgb, currentColor 85%, transparent);
  text-decoration: underline;
  cursor: pointer;
}

body button.wpe-reset-selection.wpe-reset-selection:hover,
body .wpe-reset-selection.wpe-reset-selection:hover {
  color: inherit;
  background: transparent;
}

body button.wpe-reset-selection.wpe-reset-selection:focus-visible,
body .wpe-reset-selection.wpe-reset-selection:focus-visible {
  outline: 2px solid var(--wpe-sel-accent);
  outline-offset: 2px;
}

/* Notice & Status Messages */
.wpe-selector-notice {
  margin: 0.75rem 0;
  padding: 0.5rem 0.75rem;
  border-radius: var(--wpe-sel-radius);
  font-size: 0.875rem;
  display: none;
  box-sizing: border-box;
}

.wpe-selector-notice.is-visible {
  display: block;
}

.wpe-selector-notice.notice-warning {
  background-color: var(--wpe-sel-notice-warn-bg);
  border: 1px solid var(--wpe-sel-notice-warn-border);
  color: var(--wpe-sel-notice-warn-text);
}

.wpe-selector-notice.notice-error {
  background-color: var(--wpe-sel-notice-error-bg);
  border: 1px solid var(--wpe-sel-notice-error-border);
  color: var(--wpe-sel-notice-error-text);
}

.wpe-selector-notice.notice-info {
  background-color: var(--wpe-sel-accent-surface);
  border: 1px solid var(--wpe-sel-accent);
  color: var(--wpe-sel-accent-text);
}

/* Loading / Resolving state */
.wpe-variant-selector[aria-busy="true"] .wpe-swatches {
  position: relative;
  pointer-events: none;
  opacity: 0.7;
}

@media (max-width: 600px) {
  .wpe-swatches {
    gap: 0.4rem;
  }
  .wpe-swatches .wpe-swatch.wpe-swatch {
    min-height: 2.35rem;
    padding: 0.3rem 0.6rem;
    font-size: 0.8125rem;
  }
  .wpe-swatches .wpe-swatch.wpe-swatch-image {
    min-width: 3.75rem;
    max-width: 5.5rem;
  }
}

/* Respect the reduced-motion preference (P022.4). */
@media (prefers-reduced-motion: reduce) {
  .wpe-swatches .wpe-swatch.wpe-swatch {
    transition: none;
  }
}
