---
import Layout from '../layouts/Layout.astro';
---

<Layout title="TikSave - Download TikTok Videos Without Watermark | Free & Fast">
  <!-- Background Effects -->
  <div class="fixed inset-0 -z-10 overflow-hidden">
    <!-- Gradient orbs -->
    <div class="absolute top-0 -left-40 w-80 h-80 bg-tok-500/20 rounded-full blur-[100px] animate-float"></div>
    <div class="absolute top-1/4 -right-40 w-96 h-96 bg-cyber-500/20 rounded-full blur-[120px] animate-float" style="animation-delay: -3s;"></div>
    <div class="absolute bottom-0 left-1/3 w-72 h-72 bg-tok-400/15 rounded-full blur-[80px] animate-float" style="animation-delay: -5s;"></div>
    
    <!-- Grid pattern -->
    <div class="absolute inset-0 bg-[linear-gradient(rgba(0,0,0,0.02)_1px,transparent_1px),linear-gradient(90deg,rgba(0,0,0,0.02)_1px,transparent_1px)] dark:bg-[linear-gradient(rgba(255,255,255,0.02)_1px,transparent_1px),linear-gradient(90deg,rgba(255,255,255,0.02)_1px,transparent_1px)] bg-[size:50px_50px]"></div>
  </div>

  <!-- Header -->
  <header class="fixed top-0 left-0 right-0 z-50 px-4 py-4">
    <nav class="max-w-6xl mx-auto flex items-center justify-between">
      <a href="/" class="flex items-center gap-2 group">
        <div class="relative w-10 h-10 rounded-xl bg-gradient-to-br from-tok-500 to-cyber-500 flex items-center justify-center shadow-lg shadow-tok-500/25 group-hover:shadow-tok-500/40 transition-shadow">
          <svg class="w-6 h-6 text-white" fill="currentColor" viewBox="0 0 24 24">
            <path d="M19.59 6.69a4.83 4.83 0 01-3.77-4.25V2h-3.45v13.67a2.89 2.89 0 01-5.2 1.74 2.89 2.89 0 012.31-4.64 2.93 2.93 0 01.88.13V9.4a6.84 6.84 0 00-1-.05A6.33 6.33 0 005 20.1a6.34 6.34 0 0010.86-4.43v-7a8.16 8.16 0 004.77 1.52v-3.4a4.85 4.85 0 01-1-.1z"/>
          </svg>
        </div>
        <span class="text-xl font-bold tracking-tight">
          <span class="text-tok-500">Tik</span><span class="text-cyber-500">Save</span>
        </span>
      </a>
      
      <div class="flex items-center gap-3">
        <!-- Dark mode toggle -->
        <button 
          id="theme-toggle" 
          class="p-2.5 rounded-xl bg-white/80 dark:bg-midnight-900/80 backdrop-blur-sm border border-slate-200 dark:border-midnight-700 hover:border-tok-300 dark:hover:border-tok-500/50 transition-all group"
          aria-label="Toggle dark mode"
        >
          <svg class="w-5 h-5 text-amber-500 dark:hidden group-hover:rotate-45 transition-transform" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd" d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" clip-rule="evenodd"/>
          </svg>
          <svg class="w-5 h-5 text-cyber-400 hidden dark:block group-hover:rotate-12 transition-transform" fill="currentColor" viewBox="0 0 20 20">
            <path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"/>
          </svg>
        </button>
      </div>
    </nav>
  </header>

  <main class="relative">
    <!-- Hero Section -->
    <section class="min-h-[60vh] flex flex-col items-center justify-center px-4 pt-20 pb-12">
      <div class="max-w-4xl mx-auto text-center">
        <!-- Heading -->
        <h1 class="text-4xl sm:text-5xl md:text-6xl lg:text-7xl font-extrabold tracking-tight mb-6 animate-slide-up">
          Download <span class="gradient-text">TikTok</span><br class="hidden sm:block" />
          Videos <span class="text-tok-500">Without</span> Watermark
        </h1>
        
        <p class="text-lg sm:text-xl text-slate-600 dark:text-slate-400 max-w-2xl mx-auto mb-10 animate-slide-up" style="animation-delay: 0.1s;">
          Save your favorite TikTok videos in HD quality. No watermark, no limits. 
          Extract MP3 audio instantly.
        </p>
        
        <!-- Download Form -->
        <div class="w-full max-w-2xl mx-auto animate-slide-up" style="animation-delay: 0.2s;">
          <form id="download-form" class="relative">
            <div class="relative group">
              <!-- Glow effect -->
              <div class="absolute -inset-1 bg-gradient-to-r from-tok-500 to-cyber-500 rounded-2xl blur-lg opacity-25 group-hover:opacity-50 transition-opacity"></div>
              
              <div class="relative flex flex-col sm:flex-row gap-3 p-2 bg-white dark:bg-midnight-900 rounded-2xl shadow-xl border border-slate-200 dark:border-midnight-700">
                <div class="flex-1 flex items-center gap-3 px-4">
                  <svg class="w-6 h-6 text-slate-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1"/>
                  </svg>
                  <input 
                    type="url" 
                    id="tiktok-url"
                    placeholder="Paste TikTok video URL here..."
                    class="flex-1 py-3 bg-transparent outline-none text-slate-900 dark:text-white placeholder-slate-400 text-lg"
                    required
                  />
                </div>
                <button 
                  type="submit"
                  id="download-btn"
                  class="relative px-8 py-4 bg-gradient-to-r from-tok-500 to-tok-600 hover:from-tok-600 hover:to-tok-700 text-white font-semibold rounded-xl shadow-lg shadow-tok-500/30 hover:shadow-tok-500/50 transition-all duration-300 flex items-center justify-center gap-2 min-w-[160px] group overflow-hidden"
                >
                  <span class="relative z-10 flex items-center gap-2" id="btn-content">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                    </svg>
                    Download
                  </span>
                  <span class="hidden items-center gap-2" id="btn-loading">
                    <svg class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24">
                      <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                      <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Processing...
                  </span>
                  <!-- Shine effect -->
                  <div class="absolute inset-0 -translate-x-full group-hover:translate-x-full transition-transform duration-1000 bg-gradient-to-r from-transparent via-white/20 to-transparent"></div>
                </button>
              </div>
            </div>
            
            <!-- Error message -->
            <div id="error-message" class="hidden mt-4 p-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800/50 rounded-xl text-red-600 dark:text-red-400 text-sm animate-bounce-in">
              <div class="flex items-center gap-2">
                <svg class="w-5 h-5 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                  <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                </svg>
                <span id="error-text"></span>
              </div>
            </div>
          </form>
          
          <!-- Sample URL hint -->
          <p class="mt-4 text-sm text-slate-500 dark:text-slate-500">
            Example: https://vt.tiktok.com/1234567890/
          </p>
        </div>
        
        <!-- Trust indicators -->
        <div class="flex flex-wrap items-center justify-center gap-6 mt-12 animate-slide-up" style="animation-delay: 0.3s;">
          <div class="flex items-center gap-2 text-slate-500 dark:text-slate-400">
            <svg class="w-5 h-5 text-emerald-500" fill="currentColor" viewBox="0 0 20 20">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            <span class="text-sm font-medium">No Watermark</span>
          </div>
          <div class="flex items-center gap-2 text-slate-500 dark:text-slate-400">
            <svg class="w-5 h-5 text-emerald-500" fill="currentColor" viewBox="0 0 20 20">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            <span class="text-sm font-medium">HD Quality</span>
          </div>
          <div class="flex items-center gap-2 text-slate-500 dark:text-slate-400">
            <svg class="w-5 h-5 text-emerald-500" fill="currentColor" viewBox="0 0 20 20">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            <span class="text-sm font-medium">Unlimited Downloads</span>
          </div>
          <div class="flex items-center gap-2 text-slate-500 dark:text-slate-400">
            <svg class="w-5 h-5 text-emerald-500" fill="currentColor" viewBox="0 0 20 20">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            <span class="text-sm font-medium">MP3 Extraction</span>
          </div>
        </div>
      </div>
    </section>

    <!-- Results Section (Hidden by default) -->
    <section id="results-section" class="hidden pb-20 px-4">
      <div class="max-w-4xl mx-auto">
        <div id="video-preview" class="bg-white dark:bg-midnight-900 rounded-3xl shadow-2xl border border-slate-200 dark:border-midnight-700 overflow-hidden animate-bounce-in">
          <!-- Video thumbnail and info -->
          <div class="flex flex-col md:flex-row">
            <!-- Thumbnail -->
            <div class="relative md:w-72 lg:w-80 flex-shrink-0">
              <div class="aspect-[9/16] md:aspect-auto md:h-full bg-slate-100 dark:bg-midnight-800">
                <img 
                  id="video-thumbnail" 
                  src="" 
                  alt="Video thumbnail"
                  class="w-full h-full object-cover"
                />
              </div>
              <!-- Play overlay -->
              <div class="absolute inset-0 flex items-center justify-center bg-black/20">
                <div class="w-16 h-16 rounded-full bg-white/90 flex items-center justify-center shadow-lg">
                  <svg class="w-8 h-8 text-tok-500 ml-1" fill="currentColor" viewBox="0 0 24 24">
                    <path d="M8 5v14l11-7z"/>
                  </svg>
                </div>
              </div>
              <!-- Duration badge -->
              <div class="absolute bottom-3 right-3 px-2 py-1 bg-black/70 rounded-md text-white text-sm font-mono">
                <span id="video-duration">0:00</span>
              </div>
            </div>
            
            <!-- Video Info -->
            <div class="flex-1 p-6 flex flex-col">
              <div class="flex items-start justify-between mb-4">
                <div class="flex items-center gap-3">
                  <img 
                    id="author-avatar"
                    src=""
                    alt="Author"
                    class="w-12 h-12 rounded-full object-cover border-2 border-tok-500"
                  />
                  <div>
                    <h3 id="author-name" class="font-semibold text-slate-900 dark:text-white">@username</h3>
                    <p id="author-nickname" class="text-sm text-slate-500 dark:text-slate-400">Nickname</p>
                  </div>
                </div>
                <button 
                  id="close-results"
                  class="p-2 rounded-lg hover:bg-slate-100 dark:hover:bg-midnight-800 transition-colors"
                >
                  <svg class="w-5 h-5 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                  </svg>
                </button>
              </div>
              
              <p id="video-description" class="text-slate-600 dark:text-slate-300 text-sm mb-4 line-clamp-3">
                Video description goes here...
              </p>
              
              <!-- Stats -->
              <div class="flex flex-wrap gap-4 mb-6 text-sm text-slate-500 dark:text-slate-400">
                <div class="flex items-center gap-1">
                  <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
                    <path d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z"/>
                  </svg>
                  <span id="video-likes">0</span>
                </div>
                <div class="flex items-center gap-1">
                  <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M18 10c0 3.866-3.582 7-8 7a8.841 8.841 0 01-4.083-.98L2 17l1.338-3.123C2.493 12.767 2 11.434 2 10c0-3.866 3.582-7 8-7s8 3.134 8 7zM7 9H5v2h2V9zm8 0h-2v2h2V9zM9 9h2v2H9V9z" clip-rule="evenodd"/>
                  </svg>
                  <span id="video-comments">0</span>
                </div>
                <div class="flex items-center gap-1">
                  <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
                    <path d="M15 8a3 3 0 10-2.977-2.63l-4.94 2.47a3 3 0 100 4.319l4.94 2.47a3 3 0 10.895-1.789l-4.94-2.47a3.027 3.027 0 000-.74l4.94-2.47C13.456 7.68 14.19 8 15 8z"/>
                  </svg>
                  <span id="video-shares">0</span>
                </div>
                <div class="flex items-center gap-1">
                  <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
                    <path d="M10 12a2 2 0 100-4 2 2 0 000 4z"/>
                    <path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd"/>
                  </svg>
                  <span id="video-views">0</span>
                </div>
              </div>
              
              <!-- Download Options -->
              <div class="mt-auto space-y-3">
                <h4 class="font-semibold text-slate-900 dark:text-white flex items-center gap-2">
                  <svg class="w-5 h-5 text-tok-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                  </svg>
                  Download Options
                </h4>
                
                <div class="grid gap-3" id="download-options">
                  <!-- Download buttons will be inserted here -->
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- Add to history button -->
        <div class="text-center mt-6">
          <button 
            id="new-download-btn"
            class="inline-flex items-center gap-2 px-6 py-3 text-tok-500 hover:text-tok-600 font-medium transition-colors"
          >
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
            </svg>
            Download Another Video
          </button>
        </div>
      </div>
    </section>

    <!-- Features Section -->
    <section class="py-20 px-4 bg-white/50 dark:bg-midnight-900/50">
      <div class="max-w-6xl mx-auto">
        <div class="text-center mb-16">
          <h2 class="text-3xl sm:text-4xl font-bold text-slate-900 dark:text-white mb-4">
            Why Choose <span class="gradient-text">TikSave</span>?
          </h2>
          <p class="text-slate-600 dark:text-slate-400 max-w-2xl mx-auto">
            The fastest and most reliable TikTok video downloader. No registration, no limits, no hassle.
          </p>
        </div>
        
        <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
          <!-- Feature 1 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-tok-300 dark:hover:border-tok-500/50 transition-all hover:shadow-xl hover:shadow-tok-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-tok-500 to-tok-600 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">No Watermark</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              Download TikTok videos without the annoying watermark. Get clean, professional-looking videos.
            </p>
          </div>
          
          <!-- Feature 2 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-cyber-300 dark:hover:border-cyber-500/50 transition-all hover:shadow-xl hover:shadow-cyber-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-cyber-500 to-cyber-600 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Lightning Fast</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              Our servers process your videos instantly. Download in seconds, not minutes.
            </p>
          </div>
          
          <!-- Feature 3 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-tok-300 dark:hover:border-tok-500/50 transition-all hover:shadow-xl hover:shadow-tok-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-tok-500 to-cyber-500 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">MP3 Extraction</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              Extract audio from any TikTok video. Perfect for saving your favorite sounds and music.
            </p>
          </div>
          
          <!-- Feature 4 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-cyber-300 dark:hover:border-cyber-500/50 transition-all hover:shadow-xl hover:shadow-cyber-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-cyber-600 to-cyber-500 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Mobile Friendly</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              Works perfectly on any device. Download TikTok videos directly to your phone or tablet.
            </p>
          </div>
          
          <!-- Feature 5 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-tok-300 dark:hover:border-tok-500/50 transition-all hover:shadow-xl hover:shadow-tok-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-tok-600 to-tok-500 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Unlimited Downloads</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              No daily limits, no restrictions. Download as many videos as you want, forever free.
            </p>
          </div>
          
          <!-- Feature 6 -->
          <div class="group p-6 bg-white dark:bg-midnight-800 rounded-2xl border border-slate-200 dark:border-midnight-700 hover:border-cyber-300 dark:hover:border-cyber-500/50 transition-all hover:shadow-xl hover:shadow-cyber-500/5">
            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-cyber-500 to-tok-500 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform">
              <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/>
              </svg>
            </div>
            <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Safe & Secure</h3>
            <p class="text-slate-600 dark:text-slate-400 text-sm">
              No registration required. We don't store your data or videos. Your privacy is our priority.
            </p>
          </div>
        </div>
      </div>
    </section>

    <!-- How it Works Section -->
    <section class="py-20 px-4">
      <div class="max-w-4xl mx-auto">
        <div class="text-center mb-16">
          <h2 class="text-3xl sm:text-4xl font-bold text-slate-900 dark:text-white mb-4">
            How to <span class="gradient-text">Download</span>
          </h2>
          <p class="text-slate-600 dark:text-slate-400">
            Just 3 simple steps to save any TikTok video
          </p>
        </div>
        
        <div class="relative">
          <!-- Connection line -->
          <div class="hidden md:block absolute top-1/2 left-0 right-0 h-0.5 bg-gradient-to-r from-tok-500 via-cyber-500 to-tok-500 -translate-y-1/2"></div>
          
          <div class="grid md:grid-cols-3 gap-8 relative">
            <!-- Step 1 -->
            <div class="relative text-center">
              <div class="relative inline-flex items-center justify-center w-16 h-16 rounded-full bg-gradient-to-br from-tok-500 to-tok-600 text-white text-2xl font-bold mb-6 shadow-lg shadow-tok-500/30">
                1
              </div>
              <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Copy the Link</h3>
              <p class="text-slate-600 dark:text-slate-400 text-sm">
                Open TikTok and copy the video link from the share menu
              </p>
            </div>
            
            <!-- Step 2 -->
            <div class="relative text-center">
              <div class="relative inline-flex items-center justify-center w-16 h-16 rounded-full bg-gradient-to-br from-cyber-500 to-cyber-600 text-white text-2xl font-bold mb-6 shadow-lg shadow-cyber-500/30">
                2
              </div>
              <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Paste & Download</h3>
              <p class="text-slate-600 dark:text-slate-400 text-sm">
                Paste the link above and click the download button
              </p>
            </div>
            
            <!-- Step 3 -->
            <div class="relative text-center">
              <div class="relative inline-flex items-center justify-center w-16 h-16 rounded-full bg-gradient-to-br from-tok-500 to-cyber-500 text-white text-2xl font-bold mb-6 shadow-lg shadow-tok-500/30">
                3
              </div>
              <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Save Your Video</h3>
              <p class="text-slate-600 dark:text-slate-400 text-sm">
                Choose your format and quality, then save to your device
              </p>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- Download History Section -->
    <section id="history-section" class="hidden py-20 px-4 bg-white/50 dark:bg-midnight-900/50">
      <div class="max-w-4xl mx-auto">
        <div class="flex items-center justify-between mb-8">
          <h2 class="text-2xl font-bold text-slate-900 dark:text-white flex items-center gap-3">
            <svg class="w-6 h-6 text-tok-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
            Recent Downloads
          </h2>
          <button 
            id="clear-history"
            class="text-sm text-slate-500 hover:text-tok-500 transition-colors"
          >
            Clear History
          </button>
        </div>
        
        <div id="history-list" class="space-y-4">
          <!-- History items will be inserted here -->
        </div>
      </div>
    </section>

    <!-- FAQ Section -->
    <section class="py-20 px-4">
      <div class="max-w-3xl mx-auto">
        <div class="text-center mb-12">
          <h2 class="text-3xl sm:text-4xl font-bold text-slate-900 dark:text-white mb-4">
            Frequently Asked <span class="gradient-text">Questions</span>
          </h2>
        </div>
        
        <div class="space-y-4" id="faq-list">
          <details class="group p-6 bg-white dark:bg-midnight-900 rounded-2xl border border-slate-200 dark:border-midnight-700">
            <summary class="flex items-center justify-between cursor-pointer list-none">
              <span class="font-semibold text-slate-900 dark:text-white">Is TikSave free to use?</span>
              <svg class="w-5 h-5 text-slate-400 group-open:rotate-180 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
              </svg>
            </summary>
            <p class="mt-4 text-slate-600 dark:text-slate-400">
              Yes! TikSave is completely free to use. There are no hidden fees, subscriptions, or limits. Download as many videos as you want without paying anything.
            </p>
          </details>
          
          <details class="group p-6 bg-white dark:bg-midnight-900 rounded-2xl border border-slate-200 dark:border-midnight-700">
            <summary class="flex items-center justify-between cursor-pointer list-none">
              <span class="font-semibold text-slate-900 dark:text-white">Can I download private TikTok videos?</span>
              <svg class="w-5 h-5 text-slate-400 group-open:rotate-180 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
              </svg>
            </summary>
            <p class="mt-4 text-slate-600 dark:text-slate-400">
              No, TikSave can only download public videos. Private videos are protected by TikTok's privacy settings and cannot be accessed by third-party services.
            </p>
          </details>
          
          <details class="group p-6 bg-white dark:bg-midnight-900 rounded-2xl border border-slate-200 dark:border-midnight-700">
            <summary class="flex items-center justify-between cursor-pointer list-none">
              <span class="font-semibold text-slate-900 dark:text-white">What video quality can I download?</span>
              <svg class="w-5 h-5 text-slate-400 group-open:rotate-180 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
              </svg>
            </summary>
            <p class="mt-4 text-slate-600 dark:text-slate-400">
              TikSave provides the highest quality available for each video, typically HD (720p or 1080p). The exact resolution depends on the original video quality uploaded by the creator.
            </p>
          </details>
          
          <details class="group p-6 bg-white dark:bg-midnight-900 rounded-2xl border border-slate-200 dark:border-midnight-700">
            <summary class="flex items-center justify-between cursor-pointer list-none">
              <span class="font-semibold text-slate-900 dark:text-white">Is it legal to download TikTok videos?</span>
              <svg class="w-5 h-5 text-slate-400 group-open:rotate-180 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
              </svg>
            </summary>
            <p class="mt-4 text-slate-600 dark:text-slate-400">
              Downloading videos for personal use is generally acceptable. However, you should always respect copyright laws and not redistribute or use downloaded content commercially without permission from the original creator.
            </p>
          </details>
          
          <details class="group p-6 bg-white dark:bg-midnight-900 rounded-2xl border border-slate-200 dark:border-midnight-700">
            <summary class="flex items-center justify-between cursor-pointer list-none">
              <span class="font-semibold text-slate-900 dark:text-white">Do you store my downloaded videos?</span>
              <svg class="w-5 h-5 text-slate-400 group-open:rotate-180 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
              </svg>
            </summary>
            <p class="mt-4 text-slate-600 dark:text-slate-400">
              No, we don't store any videos or personal data. Videos are processed in real-time and downloaded directly to your device. Your privacy and security are our top priorities.
            </p>
          </details>
        </div>
      </div>
    </section>
  </main>

  <!-- Footer -->
  <footer class="py-12 px-4 border-t border-slate-200 dark:border-midnight-800">
    <div class="max-w-6xl mx-auto">
      <div class="flex flex-col md:flex-row items-center justify-between gap-6">
        <div class="flex items-center gap-2">
          <div class="w-8 h-8 rounded-lg bg-gradient-to-br from-tok-500 to-cyber-500 flex items-center justify-center">
            <svg class="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 24 24">
              <path d="M19.59 6.69a4.83 4.83 0 01-3.77-4.25V2h-3.45v13.67a2.89 2.89 0 01-5.2 1.74 2.89 2.89 0 012.31-4.64 2.93 2.93 0 01.88.13V9.4a6.84 6.84 0 00-1-.05A6.33 6.33 0 005 20.1a6.34 6.34 0 0010.86-4.43v-7a8.16 8.16 0 004.77 1.52v-3.4a4.85 4.85 0 01-1-.1z"/>
            </svg>
          </div>
          <span class="font-semibold">TikSave</span>
        </div>
        
        <p class="text-sm text-slate-500 dark:text-slate-400 text-center">
          © 2024 TikSave. Not affiliated with TikTok or ByteDance.
        </p>
        
        <div class="flex items-center gap-6 text-sm text-slate-500 dark:text-slate-400">
          <a href="#" class="hover:text-tok-500 transition-colors">Privacy</a>
          <a href="#" class="hover:text-tok-500 transition-colors">Terms</a>
          <a href="#" class="hover:text-tok-500 transition-colors">Contact</a>
        </div>
      </div>
    </div>
  </footer>
</Layout>

<script>
  // Main application logic
  const form = document.getElementById('download-form') as HTMLFormElement;
  const urlInput = document.getElementById('tiktok-url') as HTMLInputElement;
  const downloadBtn = document.getElementById('download-btn') as HTMLButtonElement;
  const btnContent = document.getElementById('btn-content') as HTMLElement;
  const btnLoading = document.getElementById('btn-loading') as HTMLElement;
  const errorMessage = document.getElementById('error-message') as HTMLElement;
  const errorText = document.getElementById('error-text') as HTMLElement;
  const resultsSection = document.getElementById('results-section') as HTMLElement;
  const historySection = document.getElementById('history-section') as HTMLElement;
  const historyList = document.getElementById('history-list') as HTMLElement;
  const clearHistoryBtn = document.getElementById('clear-history') as HTMLButtonElement;
  const closeResultsBtn = document.getElementById('close-results') as HTMLButtonElement;
  const newDownloadBtn = document.getElementById('new-download-btn') as HTMLButtonElement;
  const downloadOptions = document.getElementById('download-options') as HTMLElement;

  // State
  let isLoading = false;

  // Helper functions
  function showError(message: string) {
    errorText.textContent = message;
    errorMessage.classList.remove('hidden');
    errorMessage.classList.add('animate-bounce-in');
    setTimeout(() => {
      errorMessage.classList.remove('animate-bounce-in');
    }, 600);
  }

  function hideError() {
    errorMessage.classList.add('hidden');
  }

  function setLoading(loading: boolean) {
    isLoading = loading;
    downloadBtn.disabled = loading;
    btnContent.classList.toggle('hidden', loading);
    btnContent.classList.toggle('flex', !loading);
    btnLoading.classList.toggle('hidden', !loading);
    btnLoading.classList.toggle('flex', loading);
  }

  function formatNumber(num: number): string {
    if (num >= 1000000) {
      return (num / 1000000).toFixed(1) + 'M';
    }
    if (num >= 1000) {
      return (num / 1000).toFixed(1) + 'K';
    }
    return num.toString();
  }

  function formatDuration(seconds: number): string {
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins}:${secs.toString().padStart(2, '0')}`;
  }

  function formatFileSize(bytes: number): string {
    if (bytes >= 1048576) {
      return (bytes / 1048576).toFixed(1) + ' MB';
    }
    if (bytes >= 1024) {
      return (bytes / 1024).toFixed(1) + ' KB';
    }
    return bytes + ' B';
  }

  // Validate TikTok URL
  function isValidTikTokUrl(url: string): boolean {
    const patterns = [
      /^https?:\/\/(www\.)?tiktok\.com\/@[\w.-]+\/video\/\d+/,
      /^https?:\/\/(www\.)?tiktok\.com\/t\/\w+/,
      /^https?:\/\/vm\.tiktok\.com\/\w+/,
      /^https?:\/\/vt\.tiktok\.com\/\w+/,
      /^https?:\/\/(www\.)?tiktok\.com\/v\/\d+/,
    ];
    return patterns.some(pattern => pattern.test(url));
  }

  // Display video results
  function displayResults(data: any) {
    // Update thumbnail
    const thumbnail = document.getElementById('video-thumbnail') as HTMLImageElement;
    thumbnail.src = data.thumbnail || data.cover || '';
    
    // Update author info
    const authorAvatar = document.getElementById('author-avatar') as HTMLImageElement;
    const authorName = document.getElementById('author-name') as HTMLElement;
    const authorNickname = document.getElementById('author-nickname') as HTMLElement;
    
    authorAvatar.src = data.author?.avatar || '';
    authorName.textContent = '@' + (data.author?.unique_id || data.author?.username || 'unknown');
    authorNickname.textContent = data.author?.nickname || '';
    
    // Update video info
    const description = document.getElementById('video-description') as HTMLElement;
    const duration = document.getElementById('video-duration') as HTMLElement;
    const likes = document.getElementById('video-likes') as HTMLElement;
    const comments = document.getElementById('video-comments') as HTMLElement;
    const shares = document.getElementById('video-shares') as HTMLElement;
    const views = document.getElementById('video-views') as HTMLElement;
    
    description.textContent = data.description || data.title || 'No description';
    duration.textContent = formatDuration(data.duration || 0);
    likes.textContent = formatNumber(data.statistics?.like_count || data.likes || 0);
    comments.textContent = formatNumber(data.statistics?.comment_count || data.comments || 0);
    shares.textContent = formatNumber(data.statistics?.share_count || data.shares || 0);
    views.textContent = formatNumber(data.statistics?.play_count || data.views || 0);
    
    // Generate download options
    downloadOptions.innerHTML = '';
    
    // No watermark option
    if (data.video?.no_watermark || data.download?.no_watermark) {
      const noWatermarkUrl = data.video?.no_watermark || data.download?.no_watermark;
      downloadOptions.innerHTML += createDownloadButton(
        noWatermarkUrl,
        'MP4 Without Watermark',
        'HD Quality',
        'tok',
        data.video?.size || null
      );
    }
    
    // With watermark option
    if (data.video?.watermark || data.download?.watermark) {
      const watermarkUrl = data.video?.watermark || data.download?.watermark;
      downloadOptions.innerHTML += createDownloadButton(
        watermarkUrl,
        'MP4 With Watermark',
        'Original',
        'slate',
        null
      );
    }
    
    // Audio option
    if (data.music?.url || data.audio) {
      const audioUrl = data.music?.url || data.audio;
      downloadOptions.innerHTML += createDownloadButton(
        audioUrl,
        'MP3 Audio Only',
        data.music?.title || 'Original Sound',
        'cyber',
        null
      );
    }
    
    // Show results section
    resultsSection.classList.remove('hidden');
    resultsSection.scrollIntoView({ behavior: 'smooth', block: 'center' });
    
    // Save to history
    saveToHistory(data);
  }

  function createDownloadButton(url: string, label: string, quality: string, color: string, size: number | null): string {
    const sizeText = size ? `• ${formatFileSize(size)}` : '';
    const colorClass = color === 'tok' ? 'from-tok-500 to-tok-600 shadow-tok-500/30 hover:shadow-tok-500/50' :
                       color === 'cyber' ? 'from-cyber-500 to-cyber-600 shadow-cyber-500/30 hover:shadow-cyber-500/50' :
                       'from-slate-500 to-slate-600 shadow-slate-500/30 hover:shadow-slate-500/50';
    
    return `
      <a 
        href="${url}" 
        target="_blank"
        rel="noopener noreferrer"
        class="flex items-center justify-between p-4 bg-gradient-to-r ${colorClass} text-white rounded-xl shadow-lg hover:-translate-y-0.5 transition-all group"
        download
      >
        <div class="flex items-center gap-3">
          <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
          </svg>
          <div>
            <div class="font-semibold">${label}</div>
            <div class="text-xs opacity-80">${quality} ${sizeText}</div>
          </div>
        </div>
        <svg class="w-5 h-5 group-hover:translate-x-1 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
        </svg>
      </a>
    `;
  }

  // History management
  function getHistory(): any[] {
    try {
      return JSON.parse(localStorage.getItem('tiksave_history') || '[]');
    } catch {
      return [];
    }
  }

  function saveToHistory(data: any) {
    const history = getHistory();
    const item = {
      id: Date.now(),
      thumbnail: data.thumbnail || data.cover,
      author: data.author?.unique_id || data.author?.username,
      description: data.description || data.title,
      url: urlInput.value,
      timestamp: new Date().toISOString()
    };
    
    // Avoid duplicates
    const existingIndex = history.findIndex((h: any) => h.url === item.url);
    if (existingIndex > -1) {
      history.splice(existingIndex, 1);
    }
    
    history.unshift(item);
    
    // Keep only last 20 items
    if (history.length > 20) {
      history.pop();
    }
    
    localStorage.setItem('tiksave_history', JSON.stringify(history));
    renderHistory();
  }

  function renderHistory() {
    const history = getHistory();
    
    if (history.length === 0) {
      historySection.classList.add('hidden');
      return;
    }
    
    historySection.classList.remove('hidden');
    
    historyList.innerHTML = history.map((item: any) => `
      <div class="flex items-center gap-4 p-4 bg-white dark:bg-midnight-800 rounded-xl border border-slate-200 dark:border-midnight-700">
        <img 
          src="${item.thumbnail}" 
          alt=""
          class="w-16 h-16 rounded-lg object-cover flex-shrink-0"
        />
        <div class="flex-1 min-w-0">
          <p class="font-medium text-slate-900 dark:text-white truncate">@${item.author}</p>
          <p class="text-sm text-slate-500 dark:text-slate-400 truncate">${item.description}</p>
          <p class="text-xs text-slate-400 dark:text-slate-500 mt-1">${new Date(item.timestamp).toLocaleDateString()}</p>
        </div>
        <button 
          class="history-download-btn p-2 text-tok-500 hover:bg-tok-50 dark:hover:bg-tok-900/20 rounded-lg transition-colors"
          data-url="${item.url}"
        >
          <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
          </svg>
        </button>
      </div>
    `).join('');
    
    // Add event listeners for history download buttons
    document.querySelectorAll('.history-download-btn').forEach(btn => {
      btn.addEventListener('click', (e) => {
        const url = (e.currentTarget as HTMLElement).dataset.url;
        if (url) {
          urlInput.value = url;
          form.dispatchEvent(new Event('submit'));
        }
      });
    });
  }

  // Event handlers
  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    
    if (isLoading) return;
    
    const url = urlInput.value.trim();
    
    if (!url) {
      showError('Please enter a TikTok video URL');
      return;
    }
    
    if (!isValidTikTokUrl(url)) {
      showError('Please enter a valid TikTok video URL');
      return;
    }
    
    hideError();
    setLoading(true);
    resultsSection.classList.add('hidden');
    
    try {
      const response = await fetch('/api/download', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ url })
      });
      
      const data = await response.json();
      
      if (!response.ok) {
        throw new Error(data.error || 'Failed to process video');
      }
      
      displayResults(data);
    } catch (error: any) {
      showError(error.message || 'Something went wrong. Please try again.');
    } finally {
      setLoading(false);
    }
  });

  closeResultsBtn?.addEventListener('click', () => {
    resultsSection.classList.add('hidden');
  });

  newDownloadBtn?.addEventListener('click', () => {
    resultsSection.classList.add('hidden');
    urlInput.value = '';
    urlInput.focus();
    window.scrollTo({ top: 0, behavior: 'smooth' });
  });

  clearHistoryBtn?.addEventListener('click', () => {
    localStorage.removeItem('tiksave_history');
    renderHistory();
  });

  // Initialize history on page load
  renderHistory();

  // Handle paste event for better UX
  urlInput.addEventListener('paste', () => {
    setTimeout(() => {
      if (urlInput.value && isValidTikTokUrl(urlInput.value)) {
        form.dispatchEvent(new Event('submit'));
      }
    }, 100);
  });
</script>


