"""Local/prod-like server: clean URLs (/about -> dist/about/index.html), real 404 status, no redirects."""
import os, sys, mimetypes
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "dist")
class H(BaseHTTPRequestHandler):
    def do_GET(self):
        p = self.path.split("?")[0].split("#")[0].rstrip("/") or "/"
        cand = [os.path.join(ROOT, p.lstrip("/"), "index.html")] if "." not in os.path.basename(p) else [os.path.join(ROOT, p.lstrip("/"))]
        f = next((c for c in cand if os.path.abspath(c).startswith(ROOT) and os.path.isfile(c)), None)
        if p.endswith(".html"): f = None  # extensions are not public URLs
        code = 200
        if not f: f, code = os.path.join(ROOT, "404.html"), 404
        data = open(f, "rb").read()
        self.send_response(code); self.send_header("Content-Type", mimetypes.guess_type(f)[0] or "text/html")
        self.send_header("Content-Length", str(len(data))); self.send_header("X-Content-Type-Options", "nosniff"); self.end_headers(); self.wfile.write(data)
    def log_message(self, *a): pass
if __name__ == "__main__":
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
    print(f"http://localhost:{port}"); ThreadingHTTPServer(("", port), H).serve_forever()
