<!-- Invisible YouTube Player -->
<div id="youtube" style="display:none;"></div>

<!-- Floating Music Control -->
<div id="music-control">
  <div id="main-btn">🎵</div>
  <div id="sub-btns" class="hidden">
    <button id="pause-btn">Pause</button>
    <button id="play-btn">Play</button>
  </div>
</div>

<style>
  /* Floating music control styling */
  #music-control {
    position: fixed;
    bottom: 30px;
    right: 30px;
    z-index: 9999;
  }

  #main-btn {
    background-color: black;
    color: #ad03fc;
    font-size: 28px;
    border-radius: 50%;
    width: 65px;
    height: 65px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 0 15px rgba(173, 3, 252, 0.3);
    transition: all 0.2s ease;
    user-select: none;
  }

  #main-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 0 20px rgba(173, 3, 252, 0.5);
  }

  #sub-btns {
    position: absolute;
    bottom: 80px;
    right: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: opacity 0.2s ease, transform 0.2s ease;
  }

  #sub-btns.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
  }

  #sub-btns button {
    background-color: black;
    color: white;
    border: 2px solid #ad03fc;
    border-radius: 25px; /* smooth rounded look */
    width: 85px;
    height: 38px;
    cursor: pointer;
    font-weight: 600;
    font-family: 'Arial', sans-serif;

    /* Center text vertically and horizontally */
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #sub-btns button:hover {
    background-color: #ad03fc;
    color: black;
  }
</style>

<script>
let player;
let isPlaying = false;
let userInteracted = false;
let menuOpen = false;

// Load YouTube IFrame API
let tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
let firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Always start fresh (no resume)
localStorage.removeItem("musicTime");

function onYouTubeIframeAPIReady() {
  player = new YT.Player('youtube', {
    height: '0',
    width: '0',
    playerVars: {
      listType: 'playlist',
      list: 'PLblHtqohnqAH-H8dttIwNnqteLMNcnosw',
      autoplay: 1,
      loop: 1,
      disablekb: 1,
      modestbranding: 1,
      controls: 0,
      showinfo: 0,
      rel: 0
    },
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}

function onPlayerReady() {
  let savedVolume = localStorage.getItem("musicVolume") ?? 15;
  player.setVolume(Number(savedVolume));

  // Enable shuffle (starts with random video)
  if (player.setShuffle) {
    player.setShuffle(true);
  }

  try { player.playVideo(); } catch (_) {}

  // One-time click to allow autoplay
  document.body.addEventListener("click", () => {
    if (!userInteracted) {
      userInteracted = true;
      tryPlay();
    }
  }, { once: true, passive: true });
}

function onPlayerStateChange(event) {
  if (event.data === YT.PlayerState.PLAYING) {
    isPlaying = true;
  } else if (event.data === YT.PlayerState.PAUSED) {
    isPlaying = false;
  }
}

function tryPlay() {
  if (player && !isPlaying) {
    try {
      player.playVideo();
      isPlaying = true;
    } catch (_) {}
  }
}

function togglePause() {
  if (player && isPlaying) {
    player.pauseVideo();
    isPlaying = false;
  }
}

function togglePlay() {
  if (player && !isPlaying) {
    tryPlay();
  }
}

// Toggle sub-menu open/close
document.getElementById("main-btn").addEventListener("click", () => {
  const menu = document.getElementById("sub-btns");
  menuOpen = !menuOpen;
  if (menuOpen) {
    menu.classList.remove("hidden");
  } else {
    menu.classList.add("hidden");
  }
});

// Sub-menu button actions
document.getElementById("pause-btn").addEventListener("click", togglePause);
document.getElementById("play-btn").addEventListener("click", togglePlay);
</script>
.notification-container {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 99999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.sa-feedback {
  pointer-events: auto;
  display: flex;
  gap: 10px;
  background: linear-gradient(135deg, rgba(26,0,0,0.95), rgba(139,0,0,0.98));
  color: #fff;
  padding: 10px 14px;
  border-radius: 12px;
  min-width: 280px;
  max-width: 360px;
  font-family: system-ui;
  font-size: 13px;
  box-shadow: 0 8px 22px rgba(139,0,0,.18);
  transform: translateX(120%);
  opacity: 0;
  transition: all .7s ease;
}

.sa-feedback.show {
  transform: translateX(0);
  opacity: 1;
}
<section class="before-after" id="before-after" data-component-id="{{ componentId }}">
  <div class="container">
    <div class="header-block">
      <div class="nexus-badge">
        {{ properties.badge_text | renderString }}
      </div>
      {% if properties.title %}
      <div class="section-title">
        <h2>{{ properties.title | renderString }}</h2>
      </div>
      {% endif %}

      {% if properties.subtitle %}
      <div class="section-subtitle">
        <p>{{ properties.subtitle | renderString }}</p>
      </div>
      {% endif %}
    </div>
    <div class="ba-box">
      <div class="ba-inner">
        <img src="{{ properties.before_image | imageUrl }}" class="ba-before">
        <img src="{{ properties.after_image | imageUrl }}" class="ba-after" id="ba-after">
        <span class="ba-label ba-label-before">{{ properties.label_before}}</span>
        <span class="ba-label ba-label-after">{{ properties.label_after}}</span>
        <div class="ba-line" id="ba-line"></div>
        <div class="ba-handle" id="ba-handle" tabindex="0">
          <span class="ba-arrow left"></span>
          <span class="ba-arrow right"></span>
        </div>
      </div>
    </div>
  </div>
</section>

<style>
.ba-box {
  width: 100%;
  border-radius: 18px;
  overflow: hidden;
  background: rgba(255,255,255,0.04);
  backdrop-filter: blur(18px);
  border: 3px solid rgba(85, 155, 247, 0.4);
  box-shadow: 0 20px 40px rgba(0,0,0,0.25);
}

.ba-inner {position: relative; padding-bottom: 56.25%;}
.ba-before, .ba-after {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  pointer-events: none;
  user-select: none;
}
.ba-after {clip-path: polygon(50% 0%,100% 0%,100% 100%,50% 100%);}

.ba-label {
  position: absolute;
  top: 12px;
  padding: 4px 10px;
  font-size: 0.9rem;
  font-weight: 700;
  color: white;
  background: rgba(0,0,0,0.5);
  border-radius: 8px;
  z-index: 9;
  pointer-events: none;
}
.ba-label-before {left: 12px;}
.ba-label-after {right: 12px;}

.ba-line {
  position: absolute;
  top: 0;
  left: 50%;
  width: 3px;
  height: 100%;
  background: rgba(255,255,255,0.9);
  transform: translateX(-50%);
  z-index: 8;
  pointer-events: none;
}

.ba-handle {
  position: absolute;
  top: 50%; left: 50%;
  width: 52px; height: 52px;
  transform: translate(-50%,-50%);
  background: rgba(255,255,255,0.15);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: ew-resize; z-index: 10;
  backdrop-filter: blur(10px);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.25),0 0 15px rgba(0,0,0,0.25);
}
.ba-arrow {
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
}
.ba-arrow.left {border-right: 12px solid white; margin-right: 6px;}
.ba-arrow.right {border-left: 12px solid white; margin-left: 6px;}
</style>

<script>
const handle = document.getElementById('ba-handle');
const after = document.getElementById('ba-after');
const line = document.getElementById('ba-line');
const wrapper = document.querySelector('.ba-inner');
let drag = false;

function move(x) {
  const r = wrapper.getBoundingClientRect();
  let p = ((x - r.left) / r.width) * 100;
  p = Math.max(0, Math.min(100, p));
  handle.style.left = p + '%';
  line.style.left = p + '%';
  after.style.clipPath = `polygon(${p}% 0%,100% 0%,100% 100%,${p}% 100%)`;
}

handle.addEventListener('mousedown', () => drag = true);
document.addEventListener('mousemove', e => drag && move(e.clientX));
document.addEventListener('mouseup', () => drag = false);

handle.addEventListener('touchstart', () => drag = true);
document.addEventListener('touchmove', e => drag && move(e.touches[0].clientX));
document.addEventListener('touchend', () => drag = false);
</script>
<section class="center-3d-hero component" data-component-id="{{ componentId }}">
<div class="hero-back-text">
  {% if properties.title %}
  <h2>{{ properties.title | renderString }}</h2>
  {% endif %}
  {% if properties.text %}
  <p>{{ properties.text | renderString }}</p>
  {% endif %}
</div>
<div class="model-viewer-container">
  <model-viewer
  src="{{ properties.model_src }}"
  auto-rotate
  auto-rotate-delay="0"
  rotation-per-second="38deg"
  interaction-prompt="none"
  disable-tap
  disable-zoom
  camera-controls="false"
  shadow-intensity="0"
  alt="3D Model"
></model-viewer>
</div>
<div class="hero-content">
  {% if properties.cta and properties.cta_text %}
  <a href="{{ properties.cta | shopUrl }}" class="hero-btn">
    {{ properties.cta_text }}
    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" 
         stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
      <path d="M5 12h14M12 5l7 7-7 7"/>
    </svg>
  </a>
  {% endif %}
</div>
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
</section>
<style>
  .center-3d-hero {
    position: relative;
    width: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .center-3d-hero::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
      to top,
      rgba(0, 0, 0, 0.95) 0%,
      rgba(0, 0, 0, 0.7) 25%,
      rgba(0, 0, 0, 0.3) 60%,
      rgba(0, 0, 0, 0) 100%
    );
    pointer-events: none;
    z-index: 2;
  }

  .hero-back-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    z-index: 1;
    pointer-events: none;
    width: 95%;
    max-width: 1400px;
  }

  .hero-back-text h2 {
    font-size: clamp(4rem, 10vw, 9rem);
    font-weight: 900;
    letter-spacing: 3px;
    text-transform: uppercase;
    margin-bottom: 15px;
    line-height: 1;
  }

  .hero-back-text p {
    font-size: clamp(1.3rem, 3vw, 2.2rem);
    max-width: 1000px;
    margin: 0 auto;
    line-height: 1.5;
    letter-spacing: 1px;
  }

  .model-viewer-container {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 80vh;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 30px 0;
    overflow: hidden;
  }

  model-viewer {
    width: 100vw;
    height: 110%;
    max-width: 1400px;
    background: transparent;
    pointer-events: auto;
  }

  .model-viewer-container::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60%;
    background: linear-gradient(
      to bottom,
      rgba(0, 0, 0, 0) 0%,
      rgba(0, 0, 0, 0.4) 35%,
      rgba(0, 0, 0, 0.8) 100%
    );
    pointer-events: none;
    z-index: 2;
  }

  .hero-content {
    position: relative;
    z-index: 4;
    text-align: center;
    margin-top: -100px;
    max-width: 900px;
    padding: 0 20px;
  }

  .hero-content h1 {
    font-size: clamp(2.2rem, 6vw, 5rem);
    font-weight: 900;
    color: white;
    margin-bottom: 25px;
    line-height: 1.1;
    letter-spacing: -0.5px;
  }

  .hero-content p {
    font-size: clamp(1.1rem, 2.5vw, 1.6rem);
    color: rgba(255, 255, 255, 0.85);
    margin-bottom: 45px;
    line-height: 1.8;
    max-width: 750px;
    margin-left: auto;
    margin-right: auto;
  }

  .hero-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 12px;
    padding: 18px 52px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.08));
    backdrop-filter: blur(15px);
    border: 1.5px solid rgba(255, 255, 255, 0.3);
    border-radius: 14px;
    color: white;
    text-decoration: none;
    font-size: clamp(1rem, 1.5vw, 1.2rem);
    font-weight: 700;
    letter-spacing: 0.5px;
    cursor: pointer;
    transition: all 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
    overflow: hidden;
  }

  .hero-btn::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.12));
    border-radius: 14px;
    opacity: 0;
    transition: opacity 0.35s ease;
    z-index: -1;
  }

  .hero-btn:hover {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.15));
    border-color: rgba(255, 255, 255, 0.5);
    transform: translateY(-3px);
  }

  .hero-btn:hover::before {
    opacity: 1;
  }

  @media (max-width: 1200px) {
    model-viewer {
      width: 95vw;
    }
  }

  @media (max-width: 768px) {
    .center-3d-hero {
      padding: 40px 0;
    }

    .model-viewer-container {
      height: 55vh;
      margin: 20px 0;
    }

    model-viewer {
      width: 100vw;
    }

    .hero-btn {
      padding: 16px 40px;
    }
  }

  @media (max-width: 480px) {
    .center-3d-hero {
      padding: 30px 0;
    }

    .model-viewer-container {
      height: 45vh;
    }

    .hero-content {
      margin-top: 30px;
    }

    .hero-btn {
      padding: 14px 32px;
      font-size: 1rem;
    }
  }
</style>
<script>
  document.addEventListener('DOMContentLoaded', () => {
    const modelViewer = document.querySelector('model-viewer');
    function updateCameraControls() {
      if (window.innerWidth <= 768) {
        modelViewer.removeAttribute('camera-controls');
      } else {
        modelViewer.setAttribute('camera-controls', 'false');
      }
    }
    updateCameraControls();
    window.addEventListener('resize', updateCameraControls);
  });
</script>