<div class="back-to-top-demo">
  <div class="demo-content">
    <h3>Scroll Down to See the Button</h3>
    <p>This is a demonstration of the back to top button. Scroll down to see it appear.</p>
    <div class="spacer"></div>
    <p>Keep scrolling...</p>
    <div class="spacer"></div>
    <p>Almost there...</p>
    <div class="spacer"></div>
    <p>Now scroll back up to see the button in action!</p>
  </div>
</div>
<button class="back-to-top" id="backToTopBtn">
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
    <path d="M12 19V5M5 12l7-7 7 7"/>
  </svg>
</button>
     .back-to-top-demo {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 15px;
  padding: 30px;
  color: white;
  text-align: center;
}
.demo-content h3 {
  margin: 0 0 20px 0;
  font-size: 1.8rem;
  font-weight: 600;
}
.demo-content p {
  margin: 20px 0;
  font-size: 1.1rem;
  opacity: 0.9;
}
.spacer {
  height: 200px;
}
.back-to-top {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px);
  z-index: 1000;
}
.back-to-top.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}
.back-to-top:hover {
  transform: translateY(-3px) scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.back-to-top:active {
  transform: translateY(-1px) scale(1.05);
}
.back-to-top svg {
  width: 24px;
  height: 24px;
}
/* Pulse animation */
.back-to-top.pulse {
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% {
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  }
  50% {
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
  }
  100% {
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  }
}
/* Scroll progress indicator */
.back-to-top::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border-radius: 50%;
  background: conic-gradient(transparent, #667eea, transparent 30%);
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: -1;
}
.back-to-top.progress::before {
  opacity: 1;
  animation: rotate 1.5s linear infinite;
}
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
/* Responsive */
@media (max-width: 768px) {
  .back-to-top {
    bottom: 20px;
    right: 20px;
    width: 45px;
    height: 45px;
  }
  
  .back-to-top svg {
    width: 20px;
    height: 20px;
  }
}
@media (max-width: 480px) {
  .back-to-top {
    bottom: 15px;
    right: 15px;
    width: 40px;
    height: 40px;
  }
}
     document.addEventListener('DOMContentLoaded', function() {
  // Get DOM elements
  const backToTopBtn = document.getElementById('backToTopBtn');
  
  // Show/hide button based on scroll position
  window.addEventListener('scroll', function() {
    if (window.pageYOffset > 300) {
      backToTopBtn.classList.add('visible');
    } else {
      backToTopBtn.classList.remove('visible');
    }
    
    // Add progress indicator based on scroll position
    updateScrollProgress();
  });
  
  // Scroll to top when button is clicked
  backToTopBtn.addEventListener('click', function() {
    // Add click animation
    this.classList.add('pulse');
    
    // Scroll to top with smooth behavior
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    });
    
    // Remove animation class after delay
    setTimeout(() => {
      this.classList.remove('pulse');
    }, 2000);
  });
  
  // Update scroll progress indicator
  function updateScrollProgress() {
    const scrollTop = window.pageYOffset;
    const docHeight = document.body.scrollHeight - window.innerHeight;
    const scrollPercent = (scrollTop / docHeight) * 100;
    
    // Add progress class when scrolled
    if (scrollPercent > 0) {
      backToTopBtn.classList.add('progress');
    } else {
      backToTopBtn.classList.remove('progress');
    }
  }
  
  // Handle smooth scrolling for anchor links
  document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
      e.preventDefault();
      
      const target = document.querySelector(this.getAttribute('href'));
      if (target) {
        target.scrollIntoView({
          behavior: 'smooth',
          block: 'start'
        });
      }
    });
  });
  
  // Add keyboard accessibility
  backToTopBtn.addEventListener('keydown', function(e) {
    if (e.key === 'Enter' || e.key === ' ') {
      e.preventDefault();
      this.click();
    }
  });
  
  // Add touch feedback for mobile
  backToTopBtn.addEventListener('touchstart', function() {
    this.style.transform = 'translateY(-1px) scale(1.05)';
  });
  
  backToTopBtn.addEventListener('touchend', function() {
    this.style.transform = '';
  });
});