server {
    listen 80;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    # Reverse-proxy the API on the SAME origin as the web app. The browser calls
    # /api/... on whatever host:port it loaded the page from, and nginx forwards
    # to the backend container. This removes any dependency on "localhost" and
    # works identically on the Docker host or any remote machine.
    location /api/ {
        proxy_pass http://backend:8000/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 120s;
    }

    # Static asset caching.
    location /assets/ {
        expires 7d;
        add_header Cache-Control "public, max-age=604800, immutable";
    }

    # SPA fallback: route all unknown paths to index.html so React Router works.
    location / {
        try_files $uri $uri/ /index.html;
    }
}
