#!/usr/bin/env python
"""Render a markdown report to a self-contained, on-brand (asahfikir) HTML file.

Usage: python md2html.py INPUT.md OUTPUT.html "Page title" "Eyebrow"
"""
import sys
import markdown

BRAND_CSS = """
:root{
  --bg:#FAF7F1; --surface:#F3EDE4; --fg:#1C140E; --strong:#16110B;
  --muted:#6E6759; --accent:#F65E2F; --accent-deep:#E0491F; --on-accent:#FAF7F1;
  --dark:#16110B; --on-dark:#FAF7F1; --border:#E4DCD1; --radius:16px; --radius-sm:12px;
  --font-display:"Instrument Serif",Georgia,serif;
  --font-body:"Space Grotesk",system-ui,sans-serif;
  --font-mono:"JetBrains Mono",ui-monospace,monospace;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--bg);color:var(--fg);font-family:var(--font-body);
     font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:960px;margin-inline:auto;padding-inline:clamp(16px,4vw,32px)}
header.hero{background:var(--accent);color:var(--on-accent);
            padding-block:clamp(32px,6vw,56px) clamp(26px,5vw,44px)}
.eyebrow{font-size:13px;letter-spacing:.14em;text-transform:uppercase;font-weight:500;
         opacity:.92;margin:0 0 12px}
h1{font-family:var(--font-display);font-style:italic;font-weight:400;letter-spacing:-.01em;
   font-size:clamp(2rem,4.6vw,3rem);line-height:1.08;margin:0}
.lede{font-size:clamp(1rem,1.6vw,1.08rem);margin:14px 0 0;max-width:62ch;opacity:.95}
main{padding-block:clamp(28px,4.5vw,48px)}
h2{font-family:var(--font-display);font-style:italic;font-weight:400;color:var(--strong);
   font-size:clamp(1.45rem,2.8vw,1.9rem);line-height:1.18;
   margin:44px 0 6px;padding-bottom:8px;border-bottom:1px solid var(--border)}
h3{font-size:1.12rem;font-weight:700;color:var(--strong);margin:30px 0 8px}
h4{font-size:1rem;font-weight:500;color:var(--strong);margin:22px 0 6px}
p{margin:0 0 14px;max-width:74ch}
a{color:var(--accent);text-underline-offset:3px}
strong{color:var(--strong);font-weight:700}
code{font-family:var(--font-mono);font-size:.85em;background:var(--surface);
     border:1px solid var(--border);border-radius:6px;padding:1px 6px;word-break:break-word}
pre{background:var(--dark);color:var(--on-dark);padding:16px 18px;border-radius:var(--radius-sm);
    overflow-x:auto;font-size:.85rem;line-height:1.5}
pre code{background:none;border:0;color:inherit;padding:0}
blockquote{margin:0 0 16px;border-left:4px solid var(--accent);background:var(--surface);
           border-radius:0 var(--radius-sm) var(--radius-sm) 0;padding:14px 18px;max-width:76ch}
blockquote p:last-child{margin-bottom:0}
ul,ol{margin:0 0 16px;padding-left:1.3em;max-width:76ch}
li{margin-bottom:7px}
li::marker{color:var(--accent);font-weight:700}
hr{border:0;height:1px;background:var(--border);margin:32px 0}
.tablewrap{overflow-x:auto;-webkit-overflow-scrolling:touch;margin:0 0 22px;
           border:1px solid var(--border);border-radius:var(--radius-sm);background:#fff}
table{border-collapse:collapse;width:100%;min-width:540px;font-size:14.5px}
th,td{text-align:left;vertical-align:top;padding:11px 14px;border-bottom:1px solid var(--border)}
thead th{background:var(--surface);color:var(--strong);font-weight:700;font-size:13px;
         letter-spacing:.02em;position:sticky;top:0}
tbody tr:last-child td{border-bottom:0}
footer{border-top:1px solid var(--border);padding-block:26px 44px;color:var(--muted);font-size:13.5px}
@media print{
  header.hero{background:#fff;color:var(--strong);border-bottom:3px solid var(--accent)}
  .tablewrap,table,blockquote{break-inside:avoid}
  body{font-size:11pt}
}
"""

TPL = """<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title}</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=Space+Grotesk:wght@400;500;700&family=JetBrains+Mono:wght@400&display=swap" rel="stylesheet">
<style>{css}</style>
</head>
<body>
<header class="hero">
  <div class="wrap">
    <p class="eyebrow">{eyebrow}</p>
    <h1>{title}</h1>
  </div>
</header>
<main class="wrap">
{body}
</main>
<footer class="wrap">{footer}</footer>
</body>
</html>
"""


def convert(md_path, html_path, title, eyebrow, footer):
    md = open(md_path, encoding="utf-8").read()
    body = markdown.markdown(
        md, extensions=["tables", "fenced_code", "sane_lists", "toc", "attr_list"]
    )
    # wrap wide tables so they scroll instead of breaking the layout
    body = body.replace("<table>", '<div class="tablewrap"><table>').replace("</table>", "</table></div>")
    html = TPL.format(title=title, eyebrow=eyebrow, css=BRAND_CSS, body=body, footer=footer)
    open(html_path, "w", encoding="utf-8").write(html)
    return len(html)


if __name__ == "__main__":
    md_path, html_path, title, eyebrow = sys.argv[1:5]
    footer = (sys.argv[5] if len(sys.argv) > 5
              else "Disiapkan oleh Paarthurnax · asahfikir · 2026")
    n = convert(md_path, html_path, title, eyebrow, footer)
    print(f"wrote {html_path} ({n} bytes)")
