/**
 * Option swatches on the product page.
 *
 * Markup: SelectVariationAttributeWidget::expandSwatches().
 * One file for every storefront theme and every product layout. Sizes and
 * shape are variables, so a theme or a shop's custom CSS can change them:
 *   --im-swatch-size   the circle (default 36px)
 *   --im-swatch-hit    the tap area around it (default 44px)
 *   --im-swatch-radius 50% is a circle, 6px is a soft square
 */

/* Hidden until the script takes over: with JavaScript off the shopper keeps
   the select. Drupal puts "js" on <html> when scripts run. */
.im-swatches,
.im-swatches__picked {
  display: none;
}

html.js .im-swatches {
  --im-swatch-size: 36px;
  --im-swatch-hit: 44px;
  --im-swatch-radius: 50%;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin: 0 0 8px -4px;
}

html.js .im-swatches__picked {
  display: block;
  min-height: 1.4em;
  margin: 0 0 2px;
  font-size: 0.95em;
}

/* The button is the tap area. The chip inside it is what the shopper sees. */
.im-swatch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  min-width: var(--im-swatch-hit);
  height: var(--im-swatch-hit);
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  line-height: 1;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

/* The tap area never has paint of its own. Themes and shop color schemes
   style every <button> (fill, shadow, hover fill), so this must win in every
   state. */
.im-swatches .im-swatch,
.im-swatches .im-swatch:hover,
.im-swatches .im-swatch:focus,
.im-swatches .im-swatch:active {
  background: none !important;
  box-shadow: none !important;
  border: 0 !important;
  /* Themes also turn button text white on hover. */
  color: inherit !important;
  border-radius: 0;
  text-transform: none;
  transition: none;
}

.im-swatch__chip {
  position: relative;
  display: block;
  box-sizing: border-box;
  width: var(--im-swatch-size);
  height: var(--im-swatch-size);
  overflow: hidden;
  border-radius: var(--im-swatch-radius);
  /* An edge on every swatch: white on a white page and black on a dark page
     both stay visible. */
  border: 1px solid rgba(128, 128, 128, 0.7);
  border: 1px solid color-mix(in srgb, currentColor 45%, transparent);
  background-color: #ccc;
  /* Keep the color in Windows high contrast mode, where backgrounds are
     normally replaced. */
  forced-color-adjust: none;
}

.im-swatch__chip img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: none;
  margin: 0;
  object-fit: cover;
}

/* An option with no color and no picture: its name on a pill. */
.im-swatch--text {
  padding: 0 4px;
}

.im-swatch__text {
  display: block;
  box-sizing: border-box;
  padding: 9px 14px;
  border-radius: 999px;
  border: 1px solid rgba(128, 128, 128, 0.7);
  border: 1px solid color-mix(in srgb, currentColor 45%, transparent);
  white-space: nowrap;
}

/* "Show options as: Buttons": a row of pills with the option name, the usual
   look for sizes. Picked is filled with the shop's Secondary color and its text
   is On Secondary (Design settings). The corners come from "Option Button
   Shape" in the product display settings (--im-button-radius). */
html.js .im-swatches--buttons {
  --im-button-on-bg: var(--im-secondary, #111);
  --im-button-on-fg: var(--im-on-secondary, #fff);
  gap: 8px;
  margin-left: 0;
}

.im-swatches--buttons .im-swatch--text {
  padding: 0;
}

.im-swatches--buttons .im-swatch__text {
  min-width: 52px;
  padding: 0 18px;
  border: 1px solid rgba(128, 128, 128, 0.45);
  border: 1px solid color-mix(in srgb, currentColor 22%, transparent);
  border-radius: var(--im-button-radius, 999px);
  font-size: 14px;
  font-weight: 500;
  line-height: 42px;
  letter-spacing: 0.01em;
  text-align: center;
  transition: border-color 0.12s ease, background-color 0.12s ease, color 0.12s ease;
}

/* Hover is made from Secondary, so one Design color drives every state: a
   light tint of it under the name, and a dark edge. The edge is the text
   color, so it still shows when Secondary is the same color as the page. */
.im-swatches--buttons .im-swatch:not(.is-on):hover .im-swatch__text {
  border-color: currentColor;
  background: rgba(128, 128, 128, 0.12);
  background: color-mix(in srgb, var(--im-button-on-bg) 12%, transparent);
  outline: none;
}

/* Hover on the picked button: Secondary moved a little toward its text color. */
.im-swatches--buttons .im-swatch.is-on:hover .im-swatch__text {
  background: color-mix(in srgb, var(--im-button-on-bg) 86%, var(--im-button-on-fg));
}

.im-swatches--buttons .im-swatch.is-on .im-swatch__text {
  /* The edge is the text color, two pixels: the picked button stays clear
     when Secondary is the same color as the page. */
  border-color: var(--im-button-on-fg);
  box-shadow: inset 0 0 0 1px var(--im-button-on-fg);
  background: var(--im-button-on-bg);
  color: var(--im-button-on-fg);
  font-weight: 600;
  outline: none;
}

/* The name is on the button, so the line of text above the row is for screen
   readers only here. */
.im-swatches__picked--buttons {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* Picked: a ring with a gap, so it shows on any swatch color. */
.im-swatch.is-on .im-swatch__chip,
.im-swatch.is-on .im-swatch__text {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

/* Keyboard focus: a different look from "picked". */
.im-swatch:focus {
  outline: none;
}

.im-swatch:focus-visible .im-swatch__chip,
.im-swatch:focus-visible .im-swatch__text {
  outline: 3px dashed currentColor;
  outline-offset: 3px;
}

/* Hover: a thin ring, lighter than "picked". */
.im-swatch:not(.is-on):hover .im-swatch__chip,
.im-swatch:not(.is-on):hover .im-swatch__text {
  border-color: currentColor;
  outline: 1px solid currentColor;
  outline-offset: 2px;
}

/* The option name on a small label above the swatch, for a mouse and for the
   keyboard. It replaces the browser tooltip. A touch screen has no hover: the
   picked line above the row gives the name there. */
.im-swatch {
  position: relative;
}

.im-swatch[data-tip]::before,
.im-swatch[data-tip]::after {
  position: absolute;
  left: 50%;
  z-index: 20;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.12s ease, transform 0.12s ease, visibility 0.12s;
}

.im-swatch[data-tip]::after {
  content: attr(data-tip);
  bottom: calc(100% + 6px);
  max-width: 220px;
  padding: 6px 10px;
  overflow: hidden;
  border-radius: 6px;
  background: #1c1c1e;
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: 0.01em;
  text-transform: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  transform: translate(-50%, 4px);
}

/* The small arrow under the label. */
.im-swatch[data-tip]::before {
  content: "";
  bottom: calc(100% + 1px);
  border: 5px solid transparent;
  border-bottom-width: 0;
  border-top-color: #1c1c1e;
  transform: translate(-50%, 4px);
}

.im-swatch[data-tip]:focus-visible::before,
.im-swatch[data-tip]:focus-visible::after {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
}

@media (hover: hover) {
  .im-swatch[data-tip]:hover::before,
  .im-swatch[data-tip]:hover::after {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .im-swatch[data-tip]::before,
  .im-swatch[data-tip]::after {
    transition: none;
  }
}

/* Sold out: still visible and still clickable. A circle gets a line through it. The
   words are in the button's name and in the picked line too. */
.im-swatch.is-soldout .im-swatch__chip::after {
  content: "";
  position: absolute;
  top: 50%;
  left: -20%;
  width: 140%;
  border-top: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.75);
  transform: rotate(-45deg);
}

.im-swatch.is-soldout .im-swatch__chip {
  opacity: 0.6;
}

/* A sold-out text button is dimmed only, with no line through the name. */
.im-swatch.is-soldout .im-swatch__text {
  opacity: 0.4;
}

/* The script adds .im-swatches-on. The select stays in the form (it is what
   is posted) but the buttons replace it on screen. */
.im-swatches-on select.im-swatch-select {
  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;
}

.im-swatches-on .field-prefix {
  display: block;
}

@media (forced-colors: active) {
  .im-swatch__chip,
  .im-swatch__text {
    border-color: ButtonText;
  }

  .im-swatch.is-on .im-swatch__chip,
  .im-swatch.is-on .im-swatch__text {
    outline-color: Highlight;
  }

  .im-swatches--buttons .im-swatch.is-on .im-swatch__text {
    border-color: Highlight;
    background: Highlight;
    color: HighlightText;
  }
}
