{%- style -%}
  #key-ingredients-{{ block.id }} {
    position: relative;
    font-size: 14px;
    line-height: 21px;
    margin-top: {{ block.settings.margin_top | divided_by: 10.0 }}rem;
    margin-bottom: {{ block.settings.margin_bottom | divided_by: 10.0 }}rem;
  }
  @media screen and (min-width: 750px) {
    #key-ingredients-{{ block.id }} {
      font-size: 16px;
      line-height: 24px;
    }
  }

  /* ── Default (inline) style ── */
  #key-ingredients-{{ block.id }} .ki-list {
    max-width: calc(100% - 38px);
    margin: 0;
    color: {{ block.settings.text_color }};
  }
  #key-ingredients-{{ block.id }} .ki-title {
    display: block;
    font-weight: 600;
    padding-bottom: 9px;
    color: {{ block.settings.title_color }};
    font-size: {{ block.settings.label_font_size_mobile }}px;
  }
  @media screen and (min-width: 750px) {
    #key-ingredients-{{ block.id }} .ki-title {
      font-size: {{ block.settings.label_font_size_desktop }}px;
    }
  }
  #key-ingredients-{{ block.id }} .ki-plus-btn {
    border: 0;
    background: none;
    padding: 0;
    cursor: pointer;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1.5px solid {{ block.settings.icon_border_color }};
    background-color: {{ block.settings.icon_bg_color }};
    transition: opacity 0.2s ease;
  }
  #key-ingredients-{{ block.id }} .ki-plus-btn:hover {
    opacity: 0.75;
  }
  #key-ingredients-{{ block.id }} .ki-plus-btn svg {
    display: block;
  }

  /* ── Hyperlink trigger ── */
  #key-ingredients-{{ block.id }} .ki-link {
    display: inline-block;
    margin-top: 10px;
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font-size: inherit;
    font-family: inherit;
    color: {{ block.settings.link_color }};
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: {{ block.settings.link_underline_color }};
    transition: opacity 0.2s ease;
  }
  #key-ingredients-{{ block.id }} .ki-link:hover {
    opacity: 0.7;
  }

  /* ── Accordion-style variant ── */
  #key-ingredients-{{ block.id }} .ki-accordion-btn {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    background: {{ block.settings.question_bg_color }};
    color: {{ block.settings.question_text_color }};
    border: none;
    text-align: left;
    padding: 16px 20px;
    font-size: 16px;
    font-weight: 600;
    transition: background 0.3s ease, color 0.3s ease;
  }
  #key-ingredients-{{ block.id }} .ki-accordion-btn:hover {
    background: {{ block.settings.question_hover_bg_color }};
  }
  #key-ingredients-{{ block.id }} .ki-accordion-chevron {
    flex-shrink: 0;
    margin-left: 1rem;
    display: flex;
    align-items: center;
    transition: transform 0.3s ease;
  }
  @media screen and (max-width: 749px) {
    #key-ingredients-{{ block.id }} .ki-accordion-btn {
      font-size: 14px;
      padding: 12px 16px;
    }
  }

  {%- if block.settings.show_accordion_border -%}
  #key-ingredients-{{ block.id }}.ki--accordion-style {
    border: 1px solid {{ block.settings.accordion_border_color }};
    border-radius: {{ block.settings.accordion_border_radius }}px;
    overflow: hidden;
  }
  {%- endif -%}

  /*
    The wrapper is teleported to <body> via JS (same DOM level as
    cart-drawer), so z-index: 1000 correctly beats the header group
    stacking context (z-index: 5), covering the announcement bar
    exactly like the cart drawer does.
  */
  #ki-wrapper-{{ block.id }} {
    position: fixed;
    z-index: 1000;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100%;
    display: flex;
    justify-content: flex-end;
    visibility: hidden;
    background-color: transparent;
    transition:
      visibility 0.35s ease,
      background-color 0.35s ease;
  }
  #ki-wrapper-{{ block.id }}.is-open {
    visibility: visible;
    background-color: rgba(0, 0, 0, 0.5);
  }

  /* Slide-out panel */
  #ki-drawer-{{ block.id }} {
    position: relative;
    width: min(480px, 100vw);
    height: 100%;
    background: {{ block.settings.drawer_bg_color }};
    overflow-y: auto;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.12);
    transform: translateX(100%);
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  }
  #ki-wrapper-{{ block.id }}.is-open #ki-drawer-{{ block.id }} {
    transform: translateX(0);
  }
  #ki-drawer-{{ block.id }} .ki-drawer__inner {
    padding: 48px 40px 40px;
  }
  #ki-drawer-{{ block.id }} .ki-drawer__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 24px;
    padding-bottom: 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  }
  #ki-drawer-{{ block.id }} .ki-drawer__title {
    font-size: 20px;
    font-weight: 600;
    margin: 0;
    color: {{ block.settings.drawer_title_color }};
  }
  #ki-drawer-{{ block.id }} .ki-drawer__close {
    background: none;
    border: none;
    padding: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: {{ block.settings.drawer_title_color }};
    flex-shrink: 0;
    margin-left: 16px;
    opacity: 0.7;
    transition: opacity 0.2s;
  }
  #ki-drawer-{{ block.id }} .ki-drawer__close:hover {
    opacity: 1;
  }
  #ki-drawer-{{ block.id }} .ki-drawer__body {
    font-size: 15px;
    line-height: 1.65;
    color: {{ block.settings.drawer_text_color }};
  }
  #ki-drawer-{{ block.id }} .ki-drawer__body p {
    margin: 0 0 16px;
  }
  #ki-drawer-{{ block.id }} .ki-drawer__body p:last-child {
    margin-bottom: 0;
  }
{%- endstyle -%}

{%- if block.settings.accordion_style -%}

  {%- comment -%} ── Accordion-style layout ── {%- endcomment -%}
  <div
    id="key-ingredients-{{ block.id }}"
    class="key-ingredients-block ki--accordion-style {{ block.settings.visibility }}"
    {{ block.shopify_attributes }}
  >
    <button
      type="button"
      class="ki-accordion-btn"
      aria-label="View full ingredients"
      data-ki-open="ki-wrapper-{{ block.id }}"
    >
      <span class="ki-accordion-label">{{ block.settings.label | default: 'Key ingredients' }}</span>
      <span class="ki-accordion-chevron">
        <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M5 7.5L10 12.5L15 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </span>
    </button>
  </div>

{%- else -%}

  {%- comment -%} ── Default (inline) layout ── {%- endcomment -%}
  <div
    id="key-ingredients-{{ block.id }}"
    class="key-ingredients-block {{ block.settings.visibility }}"
    {{ block.shopify_attributes }}
  >
    {%- if block.settings.label != blank -%}
      <span class="ki-title">{{ block.settings.label }}</span>
    {%- endif -%}

    {%- if block.settings.ingredients_text != blank -%}
      <p class="ki-list">{{ block.settings.ingredients_text }}</p>
    {%- endif -%}

    {%- if block.settings.show_link and block.settings.link_text != blank -%}
      <button
        type="button"
        class="ki-link"
        aria-label="{{ block.settings.link_text | escape }}"
        data-ki-open="ki-wrapper-{{ block.id }}"
      >
        {{- block.settings.link_text -}}
      </button>
    {%- endif -%}

    <button
      type="button"
      class="ki-plus-btn"
      aria-label="View full ingredients"
      data-ki-open="ki-wrapper-{{ block.id }}"
    >
      <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M5.72069 11.033V5.96898H0.656637V5.38515H5.72069V0.321103H6.30451V5.38515H11.3686V5.96898H6.30451V11.033H5.72069Z" fill="{{ block.settings.icon_color }}"/>
      </svg>
    </button>
  </div>

{%- endif -%}

<!--
  Wrapper is rendered here but immediately teleported to <body> by JS.
  This ensures it lives at the same DOM level as cart-drawer, outside
  all ancestor stacking contexts, so z-index: 1000 works correctly.
-->
<div id="ki-wrapper-{{ block.id }}" aria-hidden="true">
  <div
    id="ki-drawer-{{ block.id }}"
    role="dialog"
    aria-modal="true"
    aria-labelledby="ki-drawer-title-{{ block.id }}"
  >
    <div class="ki-drawer__inner">
      <div class="ki-drawer__header">
        <h2 class="ki-drawer__title" id="ki-drawer-title-{{ block.id }}">
          {{ block.settings.drawer_title | default: 'Ingredients' }}
        </h2>
        <button
          type="button"
          class="ki-drawer__close"
          aria-label="Close ingredients panel"
        >
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M15 5L5 15M5 5l10 10" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
          </svg>
        </button>
      </div>
      <div class="ki-drawer__body">
        {%- if block.settings.drawer_content_type == 'page' or block.settings.drawer_content_type == 'both' -%}
          {%- if block.settings.drawer_page != blank -%}
            {{ pages[block.settings.drawer_page].content }}
          {%- endif -%}
        {%- endif -%}
        {%- if block.settings.drawer_content_type == 'custom' or block.settings.drawer_content_type == 'both' -%}
          {%- if block.settings.drawer_custom_content != blank -%}
            {{ block.settings.drawer_custom_content }}
          {%- endif -%}
        {%- endif -%}
      </div>
    </div>
  </div>
</div>

<script>
  (function () {
    var WRAPPER_ID = 'ki-wrapper-{{ block.id }}';
    var DRAWER_ID  = 'ki-drawer-{{ block.id }}';

    function initKIDrawer() {
      var btns     = document.querySelectorAll('[data-ki-open="' + WRAPPER_ID + '"]');
      var wrapper  = document.getElementById(WRAPPER_ID);
      var drawer   = document.getElementById(DRAWER_ID);
      var closeBtn = drawer ? drawer.querySelector('.ki-drawer__close') : null;

      if (!btns.length || !wrapper) return;

      /*
        Teleport wrapper to <body> so it shares the same stacking context
        as cart-drawer. Without this it's nested inside the product section
        and its z-index gets clipped by ancestor stacking contexts, causing
        it to render behind the sticky announcement bar.
      */
      if (wrapper.parentElement !== document.body) {
        document.body.appendChild(wrapper);
      }

      function openDrawer() {
        wrapper.classList.add('is-open');
        wrapper.removeAttribute('aria-hidden');
        document.body.style.overflow = 'hidden';
      }

      function closeDrawer() {
        wrapper.classList.remove('is-open');
        wrapper.setAttribute('aria-hidden', 'true');
        document.body.style.overflow = '';
      }

      btns.forEach(function (btn) {
        if (btn._kiInitialized) return;
        btn._kiInitialized = true;
        btn.addEventListener('click', function (e) {
          e.stopPropagation();
          openDrawer();
        });
      });

      if (closeBtn && !closeBtn._kiInitialized) {
        closeBtn._kiInitialized = true;
        closeBtn.addEventListener('click', closeDrawer);
      }

      /* Clicking the dim area (wrapper) outside the panel closes it */
      if (drawer && !drawer._kiInitialized) {
        drawer._kiInitialized = true;
        drawer.addEventListener('click', function (e) {
          e.stopPropagation();
        });
      }
      if (!wrapper._kiInitialized) {
        wrapper._kiInitialized = true;
        wrapper.addEventListener('click', closeDrawer);
      }

      /* Escape key */
      document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape' && wrapper.classList.contains('is-open')) closeDrawer();
      });
    }

    document.addEventListener('DOMContentLoaded',       initKIDrawer);
    document.addEventListener('shopify:section:load',   initKIDrawer);
    document.addEventListener('shopify:block:select',   initKIDrawer);
    document.addEventListener('shopify:block:deselect', initKIDrawer);
  })();
</script>