{% extends "base.html" %}

{% block head %}
<script>
  (function() {
    var theme = localStorage.getItem('theme');
    if (theme !== 'light') {
      document.documentElement.classList.add('dark');
    }
  })();
</script>
{% endblock %}

{% block content %}
{%- set base = site.config.base_url | default(value="") -%}
{%- set avatar = site.config.extra.avatar | default(value="") -%}
{%- set tagline = site.config.extra.tagline | default(value="") -%}
{%- set linkedin_url = site.config.extra.linkedin | default(value="") -%}
{%- set github_url = site.config.extra.github | default(value="") -%}
{%- set articles_url = site.config.extra.articles_url | default(value="") -%}
{%- set sponsor_url = site.config.extra.sponsor_url | default(value="") -%}
{%- set source_url = site.config.extra.source_url | default(value="") -%}
{%- set book_url = site.config.extra.book_url | default(value="") -%}
{%- set hero_pdf = site.config.extra.hero_pdf | default(value="") -%}
{%- set hero_pdf_label = site.config.extra.hero_pdf_label | default(value="View PDF") -%}
{%- set has_highlights = site.data.highlights -%}
{%- set has_experience = site.data.experience -%}
{%- set has_crates = site.data.crates -%}
{%- set has_projects = site.data.projects -%}
{%- set has_education = site.data.education -%}
{%- if avatar and not (avatar is starting_with("http")) and not (avatar is starting_with("//")) -%}
  {%- set avatar = base ~ avatar -%}
{%- endif -%}
{%- set hero_pdf_url = hero_pdf -%}
{%- if hero_pdf_url and not (hero_pdf_url is starting_with("http")) and not (hero_pdf_url is starting_with("//")) -%}
  {%- set hero_pdf_url = base ~ hero_pdf_url -%}
{%- endif -%}
<div class="w-screen relative left-1/2 -ml-[50vw] -mt-[calc(var(--header-height)+3rem)] -mb-12">
  <!-- Navigation -->
  <nav class="fixed top-0 left-0 right-0 bg-white dark:bg-gray-900 shadow-md dark:shadow-none border-b border-gray-200 dark:border-gray-800 z-50 h-16">
    <div class="px-6 h-full flex justify-between items-center">
      <a href="#hero" class="text-xl font-bold text-gray-900 dark:text-white hover:text-blue-500 dark:hover:text-blue-400 transition-colors">
        {{ site.config.title }}
      </a>
      <div class="flex items-center gap-4">
        <div class="hidden md:flex space-x-8 items-center">
          <a href="#about" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">About</a>
          {% if has_highlights %}<a href="#highlights" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Highlights</a>{% endif %}
          {% if has_experience %}<a href="#experience" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Experience</a>{% endif %}
          {% if has_crates %}<a href="#crates" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Crates</a>{% endif %}
          {% if has_projects %}<a href="#projects" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Projects</a>{% endif %}
          {% if has_education %}<a href="#education" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Education</a>{% endif %}
          {% if articles_url %}
          <a href="{{ articles_url | safe }}" target="_blank" rel="noopener noreferrer" class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
            <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
              <path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"/>
              <path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"/>
            </svg>
            Articles
          </a>
          {% endif %}
          {% if sponsor_url %}
          <a href="{{ sponsor_url | safe }}" target="_blank" rel="noopener noreferrer" class="px-4 py-2 bg-pink-500 text-white rounded-lg hover:bg-pink-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
            <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
              <path fill-rule="evenodd" 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" clip-rule="evenodd"/>
            </svg>
            Sponsor
          </a>
          {% endif %}
          {% if source_url %}
          <a href="{{ source_url | safe }}" target="_blank" rel="noopener noreferrer" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors p-2" aria-label="View source on GitHub" title="View source on GitHub">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
            </svg>
          </a>
          {% endif %}
        </div>
        <button
          id="portfolio-theme-toggle"
          class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors p-2"
          aria-label="Toggle theme"
        >
          <svg id="moon-icon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 hidden dark:block" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
          </svg>
          <svg id="sun-icon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 block dark:hidden" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
          </svg>
        </button>
        <input type="checkbox" id="nav-toggle" class="hidden peer" />
        <label for="nav-toggle" class="md:hidden flex items-center cursor-pointer text-gray-900 dark:text-white">
          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
          </svg>
        </label>
      </div>
      <div class="hidden peer-checked:flex peer-checked:flex-col peer-checked:absolute peer-checked:top-16 peer-checked:left-0 peer-checked:right-0 peer-checked:bg-gray-100 dark:peer-checked:bg-gray-800 peer-checked:border-b peer-checked:border-gray-300 dark:peer-checked:border-gray-700 peer-checked:p-4 peer-checked:space-y-4 md:hidden">
        <a href="#about" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">About</a>
        {% if has_highlights %}<a href="#highlights" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Highlights</a>{% endif %}
        {% if has_experience %}<a href="#experience" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Experience</a>{% endif %}
        {% if has_crates %}<a href="#crates" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Crates</a>{% endif %}
        {% if has_projects %}<a href="#projects" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Projects</a>{% endif %}
        {% if has_education %}<a href="#education" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">Education</a>{% endif %}
        {% if articles_url %}<a href="{{ articles_url | safe }}" target="_blank" rel="noopener noreferrer" class="text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 transition-colors">Articles</a>{% endif %}
        {% if sponsor_url %}<a href="{{ sponsor_url | safe }}" target="_blank" rel="noopener noreferrer" class="text-pink-500 dark:text-pink-400 hover:text-pink-600 transition-colors">Sponsor</a>{% endif %}
        {% if source_url %}<a href="{{ source_url | safe }}" target="_blank" rel="noopener noreferrer" class="text-gray-700 dark:text-gray-300 hover:text-blue-500 dark:hover:text-blue-400 transition-colors">View source on GitHub</a>{% endif %}
      </div>
    </div>
  </nav>

  <!-- Hero Section -->
  <section id="hero" class="min-h-screen flex items-center justify-center bg-gradient-to-br from-gray-100 to-gray-200 dark:from-gray-900 dark:to-gray-800 pt-16">
    <div class="text-center px-4">
      {% if avatar %}
      <img
        src="{{ avatar | safe }}"
        alt="{{ site.config.title }}"
        class="rounded-lg mx-auto mb-6 border-2 border-blue-500 dark:border-blue-400 object-cover"
        style="width: 180px; height: 180px;"
      />
      {% endif %}
      <h1 class="text-5xl md:text-7xl font-bold text-gray-900 dark:text-white mb-4">
        Hi, I'm <span class="text-blue-500 dark:text-blue-400">{{ site.config.title }}</span>
      </h1>
      {% if tagline %}
      <p class="text-xl md:text-2xl text-gray-600 dark:text-gray-300 mb-8">{{ tagline }}</p>
      {% endif %}

      <div class="flex gap-4 justify-center flex-wrap">
        {% if hero_pdf %}
        <button type="button" id="hero-pdf-open" class="px-6 py-3 bg-emerald-500 text-white rounded-lg hover:bg-emerald-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4z" clip-rule="evenodd"/>
          </svg>
          {{ hero_pdf_label }}
        </button>
        {% endif %}
        {% if has_experience %}
        <a href="#experience" class="px-6 py-3 bg-purple-500 text-white rounded-lg hover:bg-purple-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"/>
          </svg>
          Experience
        </a>
        {% endif %}
        {% if linkedin_url %}
        <a href="{{ linkedin_url | safe }}" target="_blank" rel="noopener noreferrer" class="px-6 py-3 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 24 24" fill="currentColor">
            <path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/>
          </svg>
          LinkedIn
        </a>
        {% endif %}
        {% if github_url %}
        <a href="{{ github_url | safe }}" target="_blank" rel="noopener noreferrer" class="px-6 py-3 bg-gray-700 text-white rounded-lg hover:bg-gray-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
          </svg>
          GitHub
        </a>
        {% endif %}
        {% if book_url %}
        <a href="{{ book_url | safe }}" target="_blank" rel="noopener noreferrer" class="px-6 py-3 bg-indigo-500 text-white rounded-lg hover:bg-indigo-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 24 24" fill="currentColor">
            <path d="M19 2H6c-1.206 0-3 .799-3 3v14c0 2.201 1.794 3 3 3h15v-2H6.012C5.55 19.988 5 19.806 5 19c0-.101.009-.191.024-.273.112-.576.584-.717.988-.727H21V4c0-1.103-.897-2-2-2zm0 14H5V5c0-.806.55-.988 1-1h13v12z"/>
          </svg>
          Book
        </a>
        {% endif %}
        {% if has_projects %}
        <a href="#projects" class="px-6 py-3 bg-teal-500 text-white rounded-lg hover:bg-teal-600 transition-colors inline-flex items-center gap-2 whitespace-nowrap">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M3 5a2 2 0 012-2h10a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V5zm11 1H6v8l4-2 4 2V6z" clip-rule="evenodd"/>
          </svg>
          Projects
        </a>
        {% endif %}
      </div>
    </div>
  </section>

  <!-- About Section -->
  <section id="about" class="py-20 bg-white dark:bg-gray-900">
    <div class="max-w-6xl mx-auto px-4">
      <h2 class="text-4xl font-bold text-center mb-12 text-gray-900 dark:text-white">About Me</h2>
      {% if home %}
      <div class="max-w-3xl mx-auto space-y-8">
        <div class="text-gray-600 dark:text-gray-300 leading-relaxed text-lg text-center [&>p]:mb-8 [&>p:last-child]:mb-0">
          {{ home.content | safe }}
        </div>
      </div>
      {% endif %}
    </div>
  </section>

  <!-- Highlights Section -->
  {% if has_highlights %}
  <section id="highlights" class="py-20 bg-gradient-to-br from-gray-100 to-gray-200 dark:from-gray-900 dark:to-gray-800">
    <div class="max-w-6xl mx-auto px-4">
      <div class="text-center mb-12">
        <h2 class="text-4xl font-bold text-gray-900 dark:text-white">Highlights</h2>
      </div>
      <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
        {% for item in site.data.highlights.items %}
        {%- set highlight_image = item.image | default(value="") -%}
        {%- if highlight_image and not (highlight_image is starting_with("http")) and not (highlight_image is starting_with("//")) -%}
          {%- set highlight_image = base ~ highlight_image -%}
        {%- endif -%}
        <div class="bg-white dark:bg-gray-900 rounded-lg shadow-lg overflow-hidden border border-gray-200 dark:border-gray-700 hover:shadow-xl hover:scale-105 transition-all flex flex-col">
          <a href="{{ item.link | safe }}" target="_blank" rel="noopener noreferrer" class="block flex-1">
            {% if highlight_image %}
            <img src="{{ highlight_image | safe }}" alt="{{ item.title }}" class="w-full h-48 object-cover" />
            {% endif %}
            <div class="p-6">
              <h3 class="text-xl font-bold mb-3 text-gray-900 dark:text-white">{{ item.title }}</h3>
              <p class="text-gray-600 dark:text-gray-300 leading-relaxed">{{ item.description }}</p>
            </div>
          </a>
          {% if item.demo_link %}
          <div class="p-6 pt-0">
            <a href="{{ item.demo_link | safe }}" target="_blank" rel="noopener noreferrer" class="block px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors text-center font-medium">
              {{ item.demo_label | default(value="View Live Demo") }}
            </a>
          </div>
          {% endif %}
        </div>
        {% endfor %}
      </div>
    </div>
  </section>
  {% endif %}

  <!-- Experience Section -->
  {% if has_experience %}
  <section id="experience" class="py-20 bg-white dark:bg-gray-900" data-timeline-section>
    <div class="max-w-4xl mx-auto px-4">
      <div class="flex justify-between items-center mb-12">
        <h2 class="text-4xl font-bold text-gray-900 dark:text-white">Experience</h2>
        <button type="button" data-timeline-toggle class="px-4 py-2 bg-gray-200 dark:bg-gray-700 text-gray-900 dark:text-white rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">Show Timeline</button>
      </div>
      <div class="flex justify-between items-center mb-8 hidden" data-timeline-controls>
        <button type="button" data-timeline-older class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">&larr; Older</button>
        <span class="text-gray-700 dark:text-gray-300" data-timeline-indicator>{{ site.data.experience.jobs | length }} of {{ site.data.experience.jobs | length }}</span>
        <button type="button" data-timeline-newer class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">Newer &rarr;</button>
      </div>
      <div class="space-y-8" data-timeline-items>
        {% for job in site.data.experience.jobs %}
        <div class="border-l-4 border-blue-500 pl-6" data-job-index="{{ loop.index0 }}">
          <h3 class="text-2xl font-bold text-gray-900 dark:text-white">{{ job.title }}</h3>
          <p class="text-lg text-gray-600 dark:text-gray-300 mb-2">{{ job.company }}</p>
          <p class="text-sm text-gray-500 dark:text-gray-400 mb-4">{{ job.period }}</p>
          <ul class="space-y-2">
            {% for achievement in job.achievements %}
            <li class="flex items-start">
              <span class="text-blue-500 dark:text-blue-400 mr-2">&bull;</span>
              <span class="text-gray-600 dark:text-gray-300">{{ achievement }}</span>
            </li>
            {% endfor %}
          </ul>
        </div>
        {% endfor %}
      </div>
    </div>
  </section>
  {% endif %}

  <!-- Crates Section -->
  {% if has_crates %}
  <section id="crates" class="py-20 bg-gray-100 dark:bg-gray-800" data-sort-section="crates">
    <div class="max-w-6xl mx-auto px-4">
      <div class="flex justify-between items-center mb-12">
        <h2 class="text-4xl font-bold text-gray-900 dark:text-white">{{ site.data.crates.title | default(value="Published Crates") }}</h2>
        <div class="flex gap-2">
          <button type="button" data-sort-target="crates-grid" data-sort-direction="asc" class="bamboo-sort-button" data-sort-active>A-Z</button>
          <button type="button" data-sort-target="crates-grid" data-sort-direction="desc" class="bamboo-sort-button">Z-A</button>
        </div>
      </div>
      <div id="crates-grid" class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
        {% for item in site.data.crates.items %}
        <a href="{{ item.link | safe }}" target="_blank" rel="noopener noreferrer" data-sort-key="{{ item.title }}" class="block bg-white dark:bg-gray-900 rounded-lg shadow-lg overflow-hidden hover:shadow-xl transition-all border border-gray-200 dark:border-gray-700 hover:scale-105 cursor-pointer">
          <div class="p-6">
            <h3 class="text-xl font-bold mb-3 text-gray-900 dark:text-white">{{ item.title }}</h3>
            <p class="text-gray-600 dark:text-gray-300 mb-4 leading-relaxed">{{ item.description }}</p>
            {% if item.technologies %}
            <div class="flex flex-wrap gap-2">
              {% for tech in item.technologies %}
              <span class="px-3 py-1 bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-300 rounded text-sm border border-gray-200 dark:border-gray-700">{{ tech }}</span>
              {% endfor %}
            </div>
            {% endif %}
          </div>
        </a>
        {% endfor %}
      </div>
    </div>
  </section>
  {% endif %}

  <!-- Projects Section -->
  {% if has_projects %}
  <section id="projects" class="py-20 bg-gray-100 dark:bg-gray-800" data-sort-section="projects">
    <div class="max-w-6xl mx-auto px-4">
      <div class="flex justify-between items-center mb-12">
        <h2 class="text-4xl font-bold text-gray-900 dark:text-white">{{ site.data.projects.title | default(value="Projects") }}</h2>
        <div class="flex gap-2">
          <button type="button" data-sort-target="projects-grid" data-sort-direction="asc" class="bamboo-sort-button" data-sort-active>A-Z</button>
          <button type="button" data-sort-target="projects-grid" data-sort-direction="desc" class="bamboo-sort-button">Z-A</button>
        </div>
      </div>
      <div id="projects-grid" class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
        {% for item in site.data.projects.items %}
        <a href="{{ item.link | safe }}" target="_blank" rel="noopener noreferrer" data-sort-key="{{ item.title }}" class="block bg-white dark:bg-gray-900 rounded-lg shadow-lg overflow-hidden hover:shadow-xl transition-all border border-gray-200 dark:border-gray-700 hover:scale-105 cursor-pointer">
          <div class="p-6">
            <h3 class="text-xl font-bold mb-3 text-gray-900 dark:text-white">{{ item.title }}</h3>
            <p class="text-gray-600 dark:text-gray-300 mb-4 leading-relaxed">{{ item.description }}</p>
            {% if item.technologies %}
            <div class="flex flex-wrap gap-2">
              {% for tech in item.technologies %}
              <span class="px-3 py-1 bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-300 rounded text-sm border border-gray-200 dark:border-gray-700">{{ tech }}</span>
              {% endfor %}
            </div>
            {% endif %}
          </div>
        </a>
        {% endfor %}
      </div>
    </div>
  </section>
  {% endif %}

  <!-- Education Section -->
  {% if has_education %}
  <section id="education" class="py-20 bg-gray-100 dark:bg-gray-800">
    <div class="max-w-4xl mx-auto px-4">
      <h2 class="text-4xl font-bold text-center mb-12 text-gray-900 dark:text-white">Education</h2>
      <div class="space-y-8">
        {% for degree in site.data.education.degrees %}
        <div class="bg-white dark:bg-gray-900 p-6 rounded-lg shadow-md border border-gray-200 dark:border-gray-700">
          <h3 class="text-2xl font-bold text-gray-900 dark:text-white mb-2">{{ degree.degree }}</h3>
          <p class="text-lg text-blue-500 dark:text-blue-400 mb-1">{{ degree.institution }}</p>
          <p class="text-sm text-gray-500 dark:text-gray-400 mb-3">{{ degree.period }}</p>
          <p class="text-gray-600 dark:text-gray-300">{{ degree.description }}</p>
        </div>
        {% endfor %}
      </div>
    </div>
  </section>
  {% endif %}

  {% if hero_pdf %}
  <!-- Hero PDF Modal -->
  <div id="hero-pdf-modal" class="fixed inset-0 z-100 hidden items-center justify-center p-4" style="background-color: rgba(0, 0, 0, 0.75);" role="dialog" aria-modal="true" aria-labelledby="hero-pdf-title">
    <div class="bg-white dark:bg-gray-800 rounded-lg w-full max-w-6xl flex flex-col" style="height: 90vh;">
      <div class="flex justify-between items-center p-4 border-b border-gray-300 dark:border-gray-700">
        <a href="{{ hero_pdf_url | safe }}" download class="px-4 py-2 bg-emerald-500 text-white rounded-lg hover:bg-emerald-600 transition-colors inline-flex items-center gap-2">
          <svg xmlns="http://www.w3.org/2000/svg" class="flex-shrink-0" style="width: 14px; height: 14px;" viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"/>
          </svg>
          Download
        </a>
        <span id="hero-pdf-title" class="sr-only">{{ hero_pdf_label }}</span>
        <button type="button" id="hero-pdf-close" class="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-200 text-2xl font-bold" aria-label="Close">&times;</button>
      </div>
      <div class="bg-gray-200 dark:bg-gray-700 p-4" style="flex: 1; width: 100%; overflow: auto;">
        <iframe src="{{ hero_pdf_url | safe }}#toolbar=1&view=FitH" style="width: 100%; height: 100%; border: 0; background: #ffffff;" title="{{ hero_pdf_label }}"></iframe>
      </div>
    </div>
  </div>
  {% endif %}
</div>

<style>
  html { scroll-behavior: smooth; }
  #hero-pdf-modal.is-open { display: flex; }
  .bamboo-sort-button {
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    transition: background-color 150ms cubic-bezier(0.4, 0, 0.2, 1), color 150ms cubic-bezier(0.4, 0, 0.2, 1);
    background-color: #e5e7eb;
    color: #111827;
    cursor: pointer;
  }
  .bamboo-sort-button:hover { background-color: #f3f4f6; }
  .dark .bamboo-sort-button { background-color: #374151; color: #d1d5db; }
  .dark .bamboo-sort-button:hover { background-color: #1f2937; }
  .bamboo-sort-button[data-sort-active] { background-color: #3b82f6; color: #ffffff; }
  .bamboo-sort-button[data-sort-active]:hover { background-color: #2563eb; }
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
</style>

<script>
  document.getElementById('portfolio-theme-toggle').addEventListener('click', function() {
    var html = document.documentElement;
    if (html.classList.contains('dark')) {
      html.classList.remove('dark');
      localStorage.setItem('theme', 'light');
    } else {
      html.classList.add('dark');
      localStorage.setItem('theme', 'dark');
    }
  });

  (function() {
    var modal = document.getElementById('hero-pdf-modal');
    if (!modal) return;
    var openButton = document.getElementById('hero-pdf-open');
    var closeButton = document.getElementById('hero-pdf-close');
    var lastFocused = null;

    function focusables() {
      return Array.from(modal.querySelectorAll(
        'a[href], button:not([disabled]), iframe, [tabindex]:not([tabindex="-1"])'
      ));
    }

    function openModal() {
      lastFocused = document.activeElement;
      modal.classList.add('is-open');
      var first = focusables()[0];
      if (first) first.focus();
    }

    function closeModal() {
      modal.classList.remove('is-open');
      if (lastFocused && typeof lastFocused.focus === 'function') lastFocused.focus();
    }

    if (openButton) openButton.addEventListener('click', openModal);
    if (closeButton) closeButton.addEventListener('click', closeModal);
    modal.addEventListener('click', function(event) {
      if (event.target === modal) closeModal();
    });
    document.addEventListener('keydown', function(event) {
      if (!modal.classList.contains('is-open')) return;
      if (event.key === 'Escape') {
        closeModal();
        return;
      }
      if (event.key !== 'Tab') return;
      var nodes = focusables();
      if (nodes.length === 0) return;
      var first = nodes[0];
      var last = nodes[nodes.length - 1];
      if (event.shiftKey && document.activeElement === first) {
        event.preventDefault();
        last.focus();
      } else if (!event.shiftKey && document.activeElement === last) {
        event.preventDefault();
        first.focus();
      }
    });
  })();

  (function() {
    function applySort(target, direction) {
      var items = Array.from(target.children);
      items.sort(function(a, b) {
        var keyA = (a.dataset.sortKey || '').toLowerCase();
        var keyB = (b.dataset.sortKey || '').toLowerCase();
        if (keyA < keyB) return -direction;
        if (keyA > keyB) return direction;
        return 0;
      });
      items.forEach(function(item) { target.appendChild(item); });
    }

    document.querySelectorAll('[data-sort-section]').forEach(function(section) {
      var buttons = Array.from(section.querySelectorAll('[data-sort-target]'));
      buttons.forEach(function(button) {
        button.addEventListener('click', function() {
          var target = document.getElementById(button.dataset.sortTarget);
          if (!target) return;
          var direction = button.dataset.sortDirection === 'desc' ? -1 : 1;
          applySort(target, direction);
          buttons.forEach(function(other) { other.removeAttribute('data-sort-active'); });
          button.setAttribute('data-sort-active', '');
        });
      });
      var active = section.querySelector('[data-sort-active]');
      if (active) {
        var target = document.getElementById(active.dataset.sortTarget);
        var direction = active.dataset.sortDirection === 'desc' ? -1 : 1;
        if (target) applySort(target, direction);
      }
    });
  })();

  (function() {
    var section = document.querySelector('[data-timeline-section]');
    if (!section) return;
    var toggle = section.querySelector('[data-timeline-toggle]');
    var controls = section.querySelector('[data-timeline-controls]');
    var items = Array.from(section.querySelectorAll('[data-job-index]'));
    var older = section.querySelector('[data-timeline-older]');
    var newer = section.querySelector('[data-timeline-newer]');
    var indicator = section.querySelector('[data-timeline-indicator]');
    if (!toggle || !controls || items.length === 0) return;
    var showAll = true;
    var currentIndex = 0;
    var total = items.length;

    function render() {
      if (showAll) {
        items.forEach(function(item) { item.style.display = ''; });
        controls.classList.add('hidden');
        toggle.textContent = 'Show Timeline';
      } else {
        items.forEach(function(item, i) {
          item.style.display = i === currentIndex ? '' : 'none';
        });
        controls.classList.remove('hidden');
        if (indicator) indicator.textContent = (total - currentIndex) + ' of ' + total;
        if (older) older.disabled = currentIndex === total - 1;
        if (newer) newer.disabled = currentIndex === 0;
        toggle.textContent = 'Show All';
      }
    }

    toggle.addEventListener('click', function() { showAll = !showAll; render(); });
    if (older) older.addEventListener('click', function() { if (currentIndex < total - 1) { currentIndex++; render(); } });
    if (newer) newer.addEventListener('click', function() { if (currentIndex > 0) { currentIndex--; render(); } });
    render();
  })();
</script>
{% endblock %}
