---
// src/pages/ansible/docs.astro - Converted from static ansible-docs.html
import BaseLayout from '../../layouts/BaseLayout.astro';
import Header from '../../components/Header.astro';
import Footer from '../../components/Footer.astro';

const title = "Ansible Sandbox Documentation | Argobox";
const description = "Comprehensive documentation for the Ansible Sandbox playbooks. Learn about infrastructure automation, deployment patterns, and best practices.";

// Define sidebar structure (can be generated dynamically if needed)
const sidebarNav = [
  {
    title: "Getting Started",
    links: [
      { href: "#introduction", text: "Introduction" },
      { href: "#sandbox-overview", text: "Sandbox Overview" },
      { href: "#infrastructure", text: "Infrastructure Design" },
    ]
  },
  {
    title: "Playbooks",
    links: [
      { href: "#web-server", text: "Web Server Deployment" },
      { href: "#docker-compose", text: "Docker Compose Stack" },
      { href: "#k3s-cluster", text: "K3s Kubernetes Cluster" },
      { href: "#lamp-stack", text: "LAMP Stack" },
      { href: "#security-hardening", text: "Security Hardening" },
    ]
  },
  {
    title: "Advanced Topics",
    links: [
      { href: "#custom-roles", text: "Custom Roles" },
      { href: "#variables-inventory", text: "Variables & Inventory" },
      { href: "#best-practices", text: "Best Practices" },
    ]
  },
   {
    title: "Reference",
    links: [
      { href: "#cli-commands", text: "CLI Commands" },
      { href: "#troubleshooting", text: "Troubleshooting" },
    ]
  }
];
---

<BaseLayout {title} {description}>
  {/* Add Font Awesome if not loaded globally */}
  {/* <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" /> */}
  <Header slot="header" />

  <div class="docs-container">
    <aside class="sidebar" id="docs-sidebar">
      <div class="sidebar-header">
        <h1 class="sidebar-title">Ansible Docs</h1>
        <a href="/ansible/sandbox" class="back-link"> {/* Updated Link */}
          <i class="fas fa-arrow-left"></i>
          <span>Back to Sandbox</span>
        </a>
      </div>

      {sidebarNav.map(section => (
        <div class="sidebar-section">
          <h2 class="sidebar-section-title">{section.title}</h2>
          <ul class="sidebar-nav">
            {section.links.map(link => (
              <li class="sidebar-nav-item">
                <a href={link.href} class="sidebar-nav-link">{link.text}</a>
              </li>
            ))}
          </ul>
        </div>
      ))}
       <div class="sidebar-section contact-info">
            <h2 class="sidebar-section-title">Need Help?</h2>
            <p>If you encounter issues or have questions, feel free to reach out:</p>
            <a href="mailto:daniel.laforce@argobox.com" class="contact-email">daniel.laforce@argobox.com</a>
        </div>
    </aside>

    <main class="main-content" id="main-docs-content">
      <header class="docs-header">
        <h1 class="docs-title">Ansible Sandbox Documentation</h1>
        <p class="docs-subtitle">Explore the playbooks, understand the infrastructure, and learn best practices for using the interactive Ansible Sandbox.</p>
      </header>

      <!-- Introduction Section -->
      <section id="introduction" class="docs-section">
        <h2 class="docs-section-title">Introduction</h2>
        <p class="docs-text">
          Welcome to the documentation for the ArgoBox Ansible Sandbox. This interactive environment allows you to safely experiment with Ansible playbooks designed to manage various aspects of a modern infrastructure setup, including web servers, containerized applications, and Kubernetes clusters.
        </p>
        <p class="docs-text">
          This documentation provides details on the available playbooks, the underlying infrastructure design of the sandbox environment, and best practices for writing and testing your own automation scripts.
        </p>
         <div class="docs-note">
            <h3 class="docs-note-title"><i class="fas fa-info-circle"></i> Purpose</h3>
            <p>The primary goal of the sandbox is to provide a hands-on learning experience for Ansible in a pre-configured, safe environment that mirrors real-world scenarios.</p>
        </div>
      </section>

      <!-- Sandbox Overview Section -->
      <section id="sandbox-overview" class="docs-section">
        <h2 class="docs-section-title">Sandbox Overview</h2>
        <p class="docs-text">
          The Ansible Sandbox provides you with temporary access to a set of virtual machines (VMs) where you can execute pre-defined Ansible playbooks or even upload and run your own. The environment is reset periodically to ensure a clean state for each session.
        </p>
        <h3 class="docs-subsection-title">Key Features</h3>
        <ul class="docs-list">
          <li>Isolated environment with multiple target VMs.</li>
          <li>Pre-loaded collection of common Ansible roles and playbooks.</li>
          <li>Ability to select and run specific playbooks via a web interface.</li>
          <li>Real-time output streaming of playbook execution.</li>
          <li>Network access between sandbox VMs for testing multi-tier applications.</li>
          <li>Environment reset functionality.</li>
        </ul>
         <a href="/ansible/sandbox" class="docs-button"> {/* Updated Link */}
            <i class="fab fa-ansible"></i> Launch Ansible Sandbox
        </a>
      </section>

      <!-- Infrastructure Design Section -->
      <section id="infrastructure" class="docs-section">
        <h2 class="docs-section-title">Infrastructure Design</h2>
        <p class="docs-text">
          The sandbox environment consists of several virtual machines managed by the main ArgoBox infrastructure, typically running on Proxmox VE. These VMs are provisioned specifically for sandbox use and are isolated from the core production services.
        </p>
        <h3 class="docs-subsection-title">Components</h3>
        <table class="docs-table">
          <thead>
            <tr>
              <th>Component</th>
              <th>Description</th>
              <th>OS</th>
              <th>Purpose</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Control Node</td>
              <td>The VM where Ansible commands are executed from.</td>
              <td>Debian 12</td>
              <td>Runs Ansible engine, hosts playbooks.</td>
            </tr>
            <tr>
              <td>Web Server Node</td>
              <td>Target VM for web server playbooks (Nginx/Apache).</td>
              <td>Ubuntu 22.04</td>
              <td>Simulates a typical web server.</td>
            </tr>
            <tr>
              <td>Database Node</td>
              <td>Target VM for database playbooks (PostgreSQL/MySQL).</td>
              <td>Debian 12</td>
              <td>Simulates a database server.</td>
            </tr>
             <tr>
              <td>Docker Node</td>
              <td>Target VM with Docker installed for container playbooks.</td>
              <td>Ubuntu 22.04</td>
              <td>Runs Docker containers via Compose.</td>
            </tr>
          </tbody>
        </table>
         <div class="docs-note">
            <h3 class="docs-note-title"><i class="fas fa-network-wired"></i> Networking</h3>
            <p>All sandbox VMs are placed on a dedicated, isolated VLAN with controlled access to simulate a realistic network environment.</p>
        </div>
      </section>

      <!-- Playbooks Section -->
      <section id="playbooks" class="docs-section">
         <h2 class="docs-section-title">Available Playbooks</h2>
         <p class="docs-text">The following playbooks are available for execution within the sandbox environment. Each playbook demonstrates different Ansible concepts and common infrastructure tasks.</p>

         {/* Web Server Playbook */}
         <div id="web-server" class="docs-subsection">
            <h3 class="docs-subsection-title">Web Server Deployment (Nginx)</h3>
            <p class="docs-text">Installs and configures the Nginx web server on the 'Web Server Node'. Includes setting up a basic virtual host and ensuring the service is running.</p>
            <div class="docs-code">
               <pre><code>---
- name: Deploy Nginx Web Server
  hosts: web_server_node # Corresponds to inventory group
  become: yes # Execute tasks with sudo
  tasks:
    - name: Update apt cache
      ansible.builtin.apt:
        update_cache: yes
      tags: [install]

    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present
      tags: [install]

    - name: Ensure Nginx service is started and enabled
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: yes
      tags: [configure]

    - name: Deploy basic index page (template example)
      ansible.builtin.template:
        src: templates/index.html.j2 # Example template path
        dest: /var/www/html/index.nginx-debian.html # Default Nginx path
        owner: root
        group: root
        mode: '0644'
      tags: [configure]</code></pre>
         </div>

         {/* Docker Compose Playbook */}
         <div id="docker-compose" class="docs-subsection">
            <h3 class="docs-subsection-title">Docker Compose Stack (Example: Portainer)</h3>
            <p class="docs-text">Deploys a simple Docker Compose application (e.g., Portainer) on the 'Docker Node'. Requires Docker and Docker Compose to be pre-installed on the target.</p>
             <div class="docs-code">
               <pre><code>---
- name: Deploy Docker Compose Application
  hosts: docker_node
  become: yes
  vars:
    app_name: portainer
    app_dir: "/opt/{{ app_name }}"
    compose_file_url: "https://downloads.portainer.io/ce2-19/portainer-agent-stack.yml" # Example URL
  tasks:
    - name: Ensure application directory exists
      ansible.builtin.file:
        path: "{{ app_dir }}"
        state: directory
        mode: '0755'

    - name: Download Docker Compose file
      ansible.builtin.get_url:
        url: "{{ compose_file_url }}"
        dest: "{{ app_dir }}/docker-compose.yml"
        mode: '0644'
      register: compose_download

    - name: Deploy Docker Compose stack
      community.docker.docker_compose:
        project_src: "{{ app_dir }}"
        state: present # Ensures stack is up
      when: compose_download.changed # Only run if file was downloaded/updated
</code></pre>
            </div>
         </div>

         {/* K3s Playbook */}
         <div id="k3s-cluster" class="docs-subsection">
            <h3 class="docs-subsection-title">K3s Kubernetes Cluster (Basic Setup)</h3>
            <p class="docs-text">Installs a single-node K3s cluster on the 'Control Node'. Note: This is a simplified setup for demonstration.</p>
             <div class="docs-code">
               <pre><code>---
- name: Install K3s Single Node Cluster
  hosts: control_node # Install on the control node itself for demo
  become: yes
  tasks:
    - name: Download K3s installation script
      ansible.builtin.get_url:
        url: https://get.k3s.io
        dest: /tmp/k3s-install.sh
        mode: '0755'

    - name: Execute K3s installation script
      ansible.builtin.command:
        cmd: /tmp/k3s-install.sh
        creates: /usr/local/bin/k3s # Avoid re-running if k3s exists
      register: k3s_install_result
      changed_when: k3s_install_result.rc == 0

    - name: Ensure K3s service is started
      ansible.builtin.service:
        name: k3s
        state: started
        enabled: yes

    - name: Wait for Kubeconfig to be available
      ansible.builtin.wait_for:
        path: /etc/rancher/k3s/k3s.yaml
        timeout: 60

    - name: Read Kubeconfig file
      ansible.builtin.slurp:
        src: /etc/rancher/k3s/k3s.yaml
      register: k3s_kubeconfig

    - name: Display Kubeconfig hint (for manual use)
      ansible.builtin.debug:
        msg: "K3s installed. Use 'sudo k3s kubectl get nodes' or copy Kubeconfig."
</code></pre>
            </div>
             <div class="docs-warning">
                <h3 class="docs-warning-title"><i class="fas fa-exclamation-triangle"></i> Simplified Setup</h3>
                <p>This playbook installs a basic single-node K3s cluster. Production setups require more complex configuration, multiple nodes, and security considerations.</p>
            </div>
         </div>

         {/* LAMP Stack Playbook */}
         <div id="lamp-stack" class="docs-subsection">
            <h3 class="docs-subsection-title">LAMP Stack Installation</h3>
            <p class="docs-text">Installs Apache, MySQL (MariaDB), and PHP on the 'Web Server Node'.</p>
            {/* Add LAMP playbook code example here */}
             <p class="docs-text"><i>Code example for LAMP stack coming soon...</i></p>
         </div>

         {/* Security Hardening Playbook */}
         <div id="security-hardening" class="docs-subsection">
            <h3 class="docs-subsection-title">Basic Security Hardening</h3>
            <p class="docs-text">Applies basic security measures like installing fail2ban and configuring UFW firewall rules on a target node.</p>
            {/* Add Security playbook code example here */}
             <p class="docs-text"><i>Code example for security hardening coming soon...</i></p>
         </div>
      </section>

      {/* Advanced Topics Section */}
      <section id="advanced-topics" class="docs-section">
         <h2 class="docs-section-title">Advanced Topics</h2>

         <div id="custom-roles" class="docs-subsection">
            <h3 class="docs-subsection-title">Using Custom Roles</h3>
            <p class="docs-text">Learn how to structure your automation using Ansible Roles for better reusability and organization. Examples of common role structures will be provided.</p>
             <p class="docs-text"><i>Details on custom roles coming soon...</i></p>
         </div>

         <div id="variables-inventory" class="docs-subsection">
            <h3 class="docs-subsection-title">Variables and Inventory Management</h3>
            <p class="docs-text">Understand how to manage variables for different environments (e.g., development, production) and how to define your infrastructure using Ansible inventory files (static and dynamic).</p>
             <p class="docs-text"><i>Details on variables and inventory coming soon...</i></p>
         </div>

         <div id="best-practices" class="docs-subsection">
            <h3 class="docs-subsection-title">Ansible Best Practices</h3>
            <p class="docs-text">Explore recommended practices for writing effective, maintainable, and idempotent Ansible playbooks, including task naming, using handlers, and managing secrets.</p>
             <p class="docs-text"><i>Best practices details coming soon...</i></p>
         </div>
      </section>

       <!-- Reference Section -->
      <section id="reference" class="docs-section">
         <h2 class="docs-section-title">Reference</h2>

         <div id="cli-commands" class="docs-subsection">
            <h3 class="docs-subsection-title">Common CLI Commands</h3>
            <p class="docs-text">A quick reference for frequently used Ansible CLI commands.</p>
            <table class="docs-table">
               <thead>
                  <tr><th>Command</th><th>Description</th></tr>
               </thead>
               <tbody>
                  <tr><td><code>ansible --version</code></td><td>Check Ansible version.</td></tr>
                  <tr><td><code>ansible-inventory --list -i inventory.yml</code></td><td>List inventory hosts and groups.</td></tr>
                  <tr><td><code>ansible all -m ping -i inventory.yml</code></td><td>Ping all hosts in inventory.</td></tr>
                  <tr><td><code>ansible-playbook playbook.yml -i inventory.yml</code></td><td>Run a playbook.</td></tr>
                  <tr><td><code>ansible-playbook playbook.yml --check</code></td><td>Perform a dry run of a playbook.</td></tr>
                  <tr><td><code>ansible-playbook playbook.yml --limit web_servers</code></td><td>Run playbook only on specific hosts/groups.</td></tr>
                  <tr><td><code>ansible-vault create secrets.yml</code></td><td>Create an encrypted vault file.</td></tr>
                  <tr><td><code>ansible-playbook playbook.yml --ask-vault-pass</code></td><td>Run playbook asking for vault password.</td></tr>
               </tbody>
            </table>
         </div>

         <div id="troubleshooting" class="docs-subsection">
            <h3 class="docs-subsection-title">Troubleshooting Tips</h3>
            <p class="docs-text">Common issues and how to resolve them when working with the sandbox or Ansible in general.</p>
            <ul class="docs-list">
               <li><strong>Connection Errors:</strong> Ensure SSH keys are correctly configured and target VMs are reachable. Check firewall rules.</li>
               <li><strong>Permission Denied:</strong> Use `become: yes` for tasks requiring root privileges. Verify sudo configuration on target nodes.</li>
               <li><strong>Module Not Found:</strong> Ensure required Ansible collections or Python libraries are installed on the control node.</li>
               <li><strong>Idempotency Issues:</strong> Review tasks to ensure they only make changes when necessary (e.g., use `creates` argument for command/shell modules).</li>
               <li><strong>Variable Undefined:</strong> Check variable definitions, scope (host_vars, group_vars), and precedence. Use `-v` or `-vvv` for verbose output.</li>
            </ul>
         </div>
      </section>

      <footer class="docs-footer">
        Last updated: April 27, 2025 | Need more help? Visit the <a href="https://docs.ansible.com/" target="_blank" rel="noopener noreferrer">Official Ansible Documentation</a>.
      </footer>
    </main>
  </div>

  <button class="mobile-menu-toggle" id="mobile-menu-btn" aria-label="Toggle Documentation Menu">
    <i class="fas fa-bars"></i>
  </button>

  <Footer /> {/* Assuming Footer is global */}
</BaseLayout>

<style is:global>
  /* Import base styles if needed, or rely on BaseLayout */
  /* @import url('../styles/theme.css'); */

  /* Styles specific to the docs layout */
  .docs-container {
    display: flex;
    /* Adjust top/bottom margin if header/footer height changes */
    /* margin-top: 4.5rem; */ /* Assuming header height */
    /* min-height: calc(100vh - 4.5rem - 3rem); */ /* Full height minus header/footer */
  }

  .sidebar {
    width: 280px;
    background-color: var(--sidebar-bg, var(--bg-secondary)); /* Use theme var with fallback */
    border-right: 1px solid var(--border);
    position: sticky; /* Make sidebar sticky */
    top: 4.5rem; /* Stick below header */
    height: calc(100vh - 4.5rem); /* Full height minus header */
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
    padding: 2rem 0;
    flex-shrink: 0; /* Prevent shrinking */
  }
  .sidebar::-webkit-scrollbar { width: 6px; }
  .sidebar::-webkit-scrollbar-track { background: transparent; }
  .sidebar::-webkit-scrollbar-thumb { background-color: var(--border); border-radius: 3px; }

  .sidebar-header { padding: 0 1.5rem; margin-bottom: 2rem; }
  .sidebar-title { font-size: 1.25rem; font-weight: 600; margin-bottom: 0.5rem; color: var(--text-primary); }
  .back-link { display: flex; align-items: center; gap: 0.5rem; color: var(--accent); font-weight: 500; font-size: 0.9rem; padding: 0.5rem 0; transition: all var(--transition-normal); text-decoration: none; }
  .back-link:hover { color: var(--accent-darker); transform: translateX(-3px); }

  .sidebar-section { margin-bottom: 1.5rem; }
  .sidebar-section-title { font-weight: 600; font-size: 0.85rem; text-transform: uppercase; color: var(--text-secondary); letter-spacing: 0.05em; padding: 0 1.5rem; margin-bottom: 0.75rem; }
  .sidebar-nav { list-style: none; padding: 0; margin: 0; }
  .sidebar-nav-item { /* No extra styles needed */ }
  .sidebar-nav-link { display: block; padding: 0.5rem 1.5rem; color: var(--text-secondary); text-decoration: none; transition: all var(--transition-normal); position: relative; font-size: 0.95rem; border-left: 3px solid transparent; /* Add space for active indicator */ }
  .sidebar-nav-link:hover { color: var(--text-primary); background-color: rgba(255, 255, 255, 0.05); }
  .sidebar-nav-link.active { color: var(--accent); background-color: rgba(59, 130, 246, 0.1); border-left-color: var(--accent); font-weight: 500; }
  /* .sidebar-nav-link.active::before { content: ''; position: absolute; top: 0; left: 0; height: 100%; width: 3px; background: var(--accent-gradient); } */ /* Alternative active style */

   .contact-info { padding: 0 1.5rem; margin-top: 2rem; }
   .contact-info p { margin-bottom: 0.5rem; font-size: 0.9rem; color: var(--text-secondary); }
   .contact-email { color: var(--accent); font-weight: 600; text-decoration: none; }
   .contact-email:hover { text-decoration: underline; }


  .main-content {
    flex: 1;
    padding: 3rem;
    max-width: 1000px; /* Adjust max width as needed */
    margin-left: 280px; /* Ensure space for fixed sidebar */
  }

  .docs-header { margin-bottom: 3rem; border-bottom: 1px solid var(--border); padding-bottom: 1.5rem; }
  .docs-title { font-size: 2.5rem; font-weight: 700; margin-bottom: 0.5rem; color: var(--text-primary); }
  .docs-subtitle { color: var(--text-secondary); font-size: 1.2rem; }

  .docs-section { margin-bottom: 3rem; scroll-margin-top: 6rem; /* Offset for sticky header */ }
  .docs-section-title { font-size: 1.75rem; font-weight: 600; margin-bottom: 1.5rem; color: var(--text-primary); position: relative; padding-bottom: 0.75rem; border-bottom: 1px solid var(--border); }
  .docs-subsection { margin-bottom: 2rem; }
  .docs-subsection-title { font-size: 1.25rem; font-weight: 600; margin-bottom: 1rem; color: var(--accent); }

  .docs-text { color: var(--text-secondary); margin-bottom: 1.5rem; line-height: 1.7; }
  .docs-text a { color: var(--accent); text-decoration: underline; }
  .docs-text a:hover { color: var(--accent-darker); }

  .docs-list { list-style-type: disc; margin-left: 1.5rem; margin-bottom: 1.5rem; color: var(--text-secondary); padding-left: 1rem; }
  .docs-list li { margin-bottom: 0.5rem; }

  .docs-code {
    font-family: var(--font-mono); /* Use theme mono font */
    background-color: var(--bg-code); /* Use theme code bg */
    padding: 1.5rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin-bottom: 1.5rem;
    border: 1px solid var(--border);
    font-size: 0.9rem;
    line-height: 1.6;
    color: var(--text-code); /* Use theme code text color */
  }
  .docs-code pre { margin: 0; }
  /* Add syntax highlighting styles if using Prism/Shiki integrated with Astro */

  .docs-table { width: 100%; border-collapse: collapse; margin-bottom: 1.5rem; font-size: 0.9rem; }
  .docs-table th { text-align: left; padding: 0.75rem 1rem; background-color: var(--bg-secondary); color: var(--text-primary); font-weight: 600; border-bottom: 2px solid var(--border); }
  .docs-table td { padding: 0.75rem 1rem; border-bottom: 1px solid var(--border); color: var(--text-secondary); }
  .docs-table tr:last-child td { border-bottom: none; }
  .docs-table code { font-family: var(--font-mono); background-color: rgba(255, 255, 255, 0.05); padding: 0.1em 0.3em; border-radius: 3px; font-size: 0.85em; }

  .docs-note, .docs-warning {
    padding: 1rem 1.5rem;
    border-radius: 0 0.5rem 0.5rem 0;
    margin: 1.5rem 0;
    font-size: 0.95rem;
  }
  .docs-note { background-color: rgba(59, 130, 246, 0.1); border-left: 4px solid var(--accent); }
  .docs-warning { background-color: rgba(245, 158, 11, 0.1); border-left: 4px solid var(--warning); }
  .docs-note-title, .docs-warning-title { font-weight: 600; display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; }
  .docs-note-title { color: var(--accent); }
  .docs-warning-title { color: var(--warning); }
  .docs-note p, .docs-warning p { margin: 0; color: var(--text-secondary); }

  .docs-button { display: inline-flex; align-items: center; gap: 0.5rem; background-color: var(--accent); color: white; padding: 0.75rem 1.25rem; border-radius: 0.5rem; font-weight: 500; text-decoration: none; transition: all var(--transition-normal); margin-bottom: 1.5rem; border: none; cursor: pointer; }
  .docs-button:hover { background-color: var(--accent-darker); transform: translateY(-2px); box-shadow: var(--card-shadow); }

  .docs-footer { border-top: 1px solid var(--border); margin-top: 3rem; padding-top: 2rem; color: var(--text-secondary); font-size: 0.9rem; }
  .docs-footer a { color: var(--accent); }
  .docs-footer a:hover { color: var(--accent-darker); }

  .mobile-menu-toggle {
    display: none; /* Hidden by default */
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    background: var(--accent-gradient);
    color: white;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    font-size: 1.5rem;
    align-items: center;
    justify-content: center;
    z-index: 1010; /* Above sidebar */
    box-shadow: var(--card-shadow);
  }

  @media (max-width: 992px) {
    .sidebar {
      position: fixed; /* Change to fixed for mobile overlay */
      top: 0; /* Align to top */
      left: 0;
      height: 100vh; /* Full viewport height */
      z-index: 1000; /* Ensure it's above content */
      transform: translateX(-100%);
      transition: transform var(--transition-normal);
      background-color: var(--sidebar-bg, var(--bg-secondary)); /* Ensure background */
      padding-top: 5.5rem; /* Space for header */
    }
    .sidebar.active { transform: translateX(0); }
    .main-content { margin-left: 0; padding: 2rem 1.5rem; margin-top: 4.5rem; /* Space for header */ }
    .mobile-menu-toggle { display: flex; }
    .docs-container { margin-top: 0; margin-bottom: 0; min-height: 100vh; } /* Adjust container */
  }

</style>

<script>
  // Docs Page Specific JS

  // Mobile Sidebar Toggle
  const mobileMenuBtn = document.getElementById('mobile-menu-btn');
  const sidebar = document.getElementById('docs-sidebar');
  const mainContent = document.getElementById('main-docs-content'); // Target main content

  if (mobileMenuBtn && sidebar) {
    mobileMenuBtn.addEventListener('click', () => {
      sidebar.classList.toggle('active');
      // Optional: Add overlay or dim main content when sidebar is open
      if (sidebar.classList.contains('active')) {
         mobileMenuBtn.innerHTML = '<i class="fas fa-times"></i>'; // Change icon to X
         // Add overlay logic if desired
      } else {
         mobileMenuBtn.innerHTML = '<i class="fas fa-bars"></i>'; // Change icon back to bars
         // Remove overlay logic if desired
      }
    });

     // Close sidebar when clicking outside of it (on main content)
     mainContent?.addEventListener('click', () => {
        if (sidebar.classList.contains('active')) {
            sidebar.classList.remove('active');
            mobileMenuBtn.innerHTML = '<i class="fas fa-bars"></i>';
        }
     });

     // Close sidebar when a link is clicked
     sidebar.querySelectorAll('.sidebar-nav-link').forEach(link => {
        link.addEventListener('click', () => {
             if (sidebar.classList.contains('active')) {
                sidebar.classList.remove('active');
                mobileMenuBtn.innerHTML = '<i class="fas fa-bars"></i>';
            }
        });
     });
  }

  // Active Link Highlighting (Scroll Spy)
  const sections = document.querySelectorAll('.docs-section');
  const navLinks = document.querySelectorAll('.sidebar-nav-link');

  function activateLink() {
    let currentSectionId = '';
    const scrollPosition = window.scrollY;

    sections.forEach(section => {
      const sectionTop = section.offsetTop - 150; // Adjust offset as needed
      const sectionHeight = section.offsetHeight;
      const sectionId = section.getAttribute('id');

      if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
        currentSectionId = sectionId;
      }
    });

     // Handle reaching the bottom of the page
     if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50) {
         const lastSection = sections[sections.length - 1];
         if (lastSection) {
            currentSectionId = lastSection.getAttribute('id');
         }
     }

    navLinks.forEach(link => {
      link.classList.remove('active');
      if (link.getAttribute('href') === `#${currentSectionId}`) {
        link.classList.add('active');
      }
    });

     // Ensure first link is active if scrolled to top
     if (window.scrollY < 100 && navLinks.length > 0) {
         navLinks.forEach(link => link.classList.remove('active'));
         navLinks[0].classList.add('active');
     }
  }

  window.addEventListener('scroll', activateLink);
  // Initial check on load
  document.addEventListener('DOMContentLoaded', activateLink);

</script>