# Force default index file
DirectoryIndex index.html index.php

# Security: Disable directory listing
Options -Indexes -MultiViews

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # Allow direct access to index.html
  RewriteRule ^index\.html$ - [L]

  # 1. Existing real file -> serve directly
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^ - [L]

  # 2. Prerendered sub-route with index.html (e.g. /auth/ -> /auth/index.html)
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteCond %{REQUEST_FILENAME}/index.html -f
  RewriteRule ^(.*)$ /index.html [L]

  # 3. Fallback all remaining routes to root index.html for SPA router
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^.*$ /index.html [L,QSA]
</IfModule>

# Fallback for 404 errors
ErrorDocument 404 /index.html

# MIME Types for modern web assets
<IfModule mod_mime.c>
  AddType application/javascript js mjs
  AddType text/css css
  AddType image/svg+xml svg svgz
  AddType image/x-icon ico
  AddType application/manifest+json webmanifest
  AddType application/xml xml
</IfModule>

<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
</IfModule>

# Browser Caching & Performance
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json image/svg+xml
</IfModule>
