<fieldset
  class="js product-form__input product-form__input-{{ option_name | handleize }}"
  data-option="option{{ option_position }}"
  style="width:100%;max-width:100%;">
  <legend class="form__label" style="width:100%;">
    <span>
      {{ option_name }}:
      <span>{{ option_selected }}</span>
    </span>
    {% if option_position == 1 and product.selected_or_first_available_variant.sku != blank %}
      <span
        id="skubar-{{ section.id }}"
        class="skubar"
        style="float:right;">SKU: {{ product.selected_or_first_available_variant.sku }}</span>
    {% endif %}
  </legend>

  {% if option_name == 'Loft' %}
    {% assign option_values = option_values | sort: 'name' %}
  {% elsif option_name == 'Size' and settings.size_sort_enabled %}
    {% comment %} Custom numeric sort for sizes (shoe sizes, clothing sizes, etc.) {% endcomment %}
    {% comment %} Build a sorted list of size names in numeric order {% endcomment %}
    {% assign sorted_size_names = "" %}
    {% comment %} Comprehensive size order covering common shoe sizes (whole, half, wide, narrow) {% endcomment %}
    {% assign size_order = settings.size_sort_values | split: "," %}
    {% for size_value in size_order %}
      {% for value in option_values %}
        {% assign size_value_downcase = size_value | downcase | strip %}
        {% assign value_name_downcase = value.name | downcase | strip %}
        {% if value_name_downcase == size_value_downcase %}
          {% if sorted_size_names != "" %}
            {% assign sorted_size_names = sorted_size_names | append: "|||" %}
          {% endif %}
          {% assign sorted_size_names = sorted_size_names | append: value.name %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endfor %}
    {% comment %} Handle any sizes not in the predefined list (fallback to alphabetical) {% endcomment %}
    {% assign remaining_sizes = "" %}
    {% for value in option_values %}
      {% assign found_in_order = false %}
      {% for size_value in size_order %}
        {% assign size_value_downcase = size_value | downcase | strip %}
        {% assign value_name_downcase = value.name | downcase | strip %}
        {% if value_name_downcase == size_value_downcase %}
          {% assign found_in_order = true %}
          {% break %}
        {% endif %}
      {% endfor %}
      {% unless found_in_order %}
        {% if remaining_sizes != "" %}
          {% assign remaining_sizes = remaining_sizes | append: "|||" %}
        {% endif %}
        {% assign remaining_sizes = remaining_sizes | append: value.name %}
      {% endunless %}
    {% endfor %}
    {% if remaining_sizes != "" %}
      {% assign remaining_sizes_array = remaining_sizes | split: "|||" | sort %}
      {% for remaining_size in remaining_sizes_array %}
        {% if sorted_size_names != "" %}
          {% assign sorted_size_names = sorted_size_names | append: "|||" %}
        {% endif %}
        {% assign sorted_size_names = sorted_size_names | append: remaining_size %}
      {% endfor %}
    {% endif %}
    {% comment %} Store sorted names for use in the loop below {% endcomment %}
    {% if sorted_size_names != "" %}
      {% assign size_sorted_names = sorted_size_names | split: "|||" %}
    {% else %}
      {% comment %} If no sizes matched the order, fall back to alphabetical sort {% endcomment %}
      {% assign size_sorted_names = blank %}
    {% endif %}
  {% elsif option_name == 'Bounce' and settings.bounce_sort_enabled %}
    {% comment %} Custom numeric sort for bounce degrees {% endcomment %}
    {% assign sorted_bounce_string = "" %}
    {% assign bounce_order = settings.bounce_sort_values | split: "," %}
    {% for bounce_value in bounce_order %}
      {% if option_values contains bounce_value %}
        {% if sorted_bounce_string != "" %}
          {% assign sorted_bounce_string = sorted_bounce_string | append: "|||" %}
        {% endif %}
        {% assign sorted_bounce_string = sorted_bounce_string | append: bounce_value %}
      {% endif %}
    {% endfor %}
    {% assign option_values = sorted_bounce_string | split: "|||" %}
  {% endif %}

  {%- if option_name == 'Size' and settings.size_sort_enabled and sorted_size_names != "" and size_sorted_names.size > 0 -%}
    {%- comment -%} Loop through sizes in sorted order {%- endcomment -%}
    {%- assign size_index = 0 -%}
    {%- for sorted_size_name in size_sorted_names -%}
      {%- assign size_index = size_index | plus: 1 -%}
      {%- for value in option_values -%}
        {% assign sorted_size_name_downcase = sorted_size_name | downcase | strip %}
        {% assign value_name_downcase = value.name | downcase | strip %}
        {%- if value_name_downcase == sorted_size_name_downcase -%}
          {%- liquid
            if missing_children and missing_children contains value
              continue
            endif
            assign color_image = blank
            assign option_downcase = option_name | downcase
            assign option_number = 'option' | append: option_position
            if button_type == 'image' and option_downcase == 'color'
              assign variant_with_color = product.variants | where: option_number, value | first
              if variant_with_color.featured_media != blank
                capture color_image
                  render 'product-button_fieldset__image_swatch', media: variant_with_color.featured_media
                endcapture
              else
                # Strip all separators from color name - keep only letters and numbers
                assign color_clean = value | downcase | replace: ' ', '' | replace: '-', '' | replace: '_', '' | replace: '/', ''

                # Check for child product specific to this color value
                assign color_childhandle = ""
                for option in product.options_with_values
                  if option.name == option_name
                    for option_value in option.values
                      if option_value.product_url
                        # Strip all separators from URL - keep only letters and numbers
                        assign url_clean = option_value.product_url | downcase | replace: ' ', '' | replace: '-', '' | replace: '_', '' | replace: '/', ''

                        if url_clean contains color_clean
                          assign color_childhandle = option_value.product_url | remove: "/products/"
                          break
                        endif
                      endif
                    endfor
                    break
                  endif
                endfor

                if color_childhandle != ""
                  assign color_child_product = all_products[color_childhandle]
                  if color_child_product != blank
                    if color_child_product.selected_or_first_available_variant.image != blank
                      echo 'selected_or_first_available_variant'
                      capture color_image
                        render 'product-button_fieldset__image_swatch', media: color_child_product.selected_or_first_available_variant.image
                      endcapture
                    elsif color_child_product.featured_image != blank
                      echo 'featured_image'
                      capture color_image
                        render 'product-button_fieldset__image_swatch', media: color_child_product.featured_image
                      endcapture
                    endif
                  endif
                endif
              endif
            elsif button_type == 'pattern'
              assign swatch_metafield = shop.metafields.common.swatches.value[value]
            endif
          -%}

          <input
            type="radio"
            id="{{ section_id }}-{{ product.id }}-{{ option_position }}-{{ size_index }}"
            name="{{ option_name }}"
            value="{{ value | escape }}"
            form="{{ product_form_id }}"
            {% if option_selected == value %}
            checked{% endif %}
            {% if multiple_options %}
            {% unless unavailable_check contains value or option_position == 1 %}
            class="disabled"
            {% endunless %}
            {% endif %}>
          <label
            for="{{ section_id }}-{{ product.id }}-{{ option_position }}-{{ size_index }}"
            class="variant_label variant_label--{{ button_type }}{% if color_image != blank %} variant_label--color__image{% elsif swatch_metafield %} variant_label--color__pattern{% endif %}"
            {%- render 'shared-color_swatch_inline', swatch_metafield: swatch_metafield -%}>
            {{ color_image }}
            <span>
              {{ value }}
            </span>
          </label>
          {%- break -%}
        {%- endif -%}
      {%- endfor -%}
    {%- endfor -%}
  {%- else -%}
    {%- comment -%} Standard loop for non-size options {%- endcomment -%}
    {%- for value in option_values -%}
    {%- liquid
      if missing_children and missing_children contains value
        continue
      endif
      assign color_image = blank
      assign option_downcase = option_name | downcase
      assign option_number = 'option' | append: option_position
      if button_type == 'image' and option_downcase == 'color'
        assign variant_with_color = product.variants | where: option_number, value | first
        if variant_with_color.featured_media != blank
          capture color_image
            render 'product-button_fieldset__image_swatch', media: variant_with_color.featured_media
          endcapture
        else
          # Strip all separators from color name - keep only letters and numbers
          assign color_clean = value | downcase | replace: ' ', '' | replace: '-', '' | replace: '_', '' | replace: '/', ''

          # Check for child product specific to this color value
          assign color_childhandle = ""
          for option in product.options_with_values
            if option.name == option_name
              for option_value in option.values
                if option_value.product_url
                  # Strip all separators from URL - keep only letters and numbers
                  assign url_clean = option_value.product_url | downcase | replace: ' ', '' | replace: '-', '' | replace: '_', '' | replace: '/', ''

                  if url_clean contains color_clean
                    assign color_childhandle = option_value.product_url | remove: "/products/"
                    break
                  endif
                endif
              endfor
              break
            endif
          endfor

          if color_childhandle != ""
            assign color_child_product = all_products[color_childhandle]
            if color_child_product != blank
              if color_child_product.selected_or_first_available_variant.image != blank
                echo 'selected_or_first_available_variant'
                capture color_image
                  render 'product-button_fieldset__image_swatch', media: color_child_product.selected_or_first_available_variant.image
                endcapture
              elsif color_child_product.featured_image != blank
                echo 'featured_image'
                capture color_image
                  render 'product-button_fieldset__image_swatch', media: color_child_product.featured_image
                endcapture
              endif
            endif
          endif
        endif
      elsif button_type == 'pattern'
        assign swatch_metafield = shop.metafields.common.swatches.value[value]
      endif
    -%}

    <input
      type="radio"
      id="{{ section_id }}-{{ product.id }}-{{ option_position }}-{{ forloop.index }}"
      name="{{ option_name }}"
      value="{{ value | escape }}"
      form="{{ product_form_id }}"
      {% if option_selected == value %}
      checked{% endif %}
      {% if multiple_options %}
      {% unless unavailable_check contains value or option_position == 1 %}
      class="disabled"
      {% endunless %}
      {% endif %}>
    <label
      for="{{ section_id }}-{{ product.id }}-{{ option_position }}-{{ forloop.index }}"
      class="variant_label variant_label--{{ button_type }}{% if color_image != blank %} variant_label--color__image{% elsif swatch_metafield %} variant_label--color__pattern{% endif %}"
      {%- render 'shared-color_swatch_inline', swatch_metafield: swatch_metafield -%}>
      {{ color_image }}
      <span>
        {{ value }}
      </span>
    </label>
  {%- endfor -%}
  {%- endif -%}
</fieldset>