---
import BaseLayout from '../layouts/BaseLayout.astro';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { marked } from 'marked';

// Read PRIVACY.md at the repo root at build time.  Keeping the
// canonical copy at the repo root (not duplicated into the site)
// means CONTRIBUTING + the GitHub UI both see the same document
// lawyers and auditors read.
const mdPath = resolve(process.cwd(), '../../PRIVACY.md');
const markdown = readFileSync(mdPath, 'utf-8');

function slugify(text: string): string {
  return text
    .toLowerCase()
    .replace(/[^\w\s-]/g, '')
    .replace(/\s+/g, '-')
    .replace(/-+/g, '-')
    .trim();
}

const renderer = new marked.Renderer();
renderer.heading = function ({ text, depth }: { text: string; depth: number }) {
  const id = slugify(text);
  return `<h${depth} id="${id}">${text}</h${depth}>`;
};

marked.setOptions({ renderer, gfm: true, breaks: false });

const htmlContent = await marked.parse(markdown);

const schema = {
  '@type': 'WebPage',
  name: 'Privacy Policy — Kiln',
  description:
    'How Kiln handles data, what we collect, and what we never collect.',
  url: 'https://kiln3d.com/privacy',
};
---

<BaseLayout
  title="Privacy Policy — Kiln"
  description="How Kiln handles your data. Locally-installed software; your data stays on your machine unless you explicitly use cloud features."
  current="privacy"
  keywords={['kiln privacy policy', '3d printing privacy', 'mcp data handling']}
  schema={schema}
>
  <main class="legal-page">
    <div class="page-header">
      <h1>Privacy Policy</h1>
      <p class="subtitle">How Kiln handles your data.</p>
    </div>
    <article class="prose" set:html={htmlContent} />
  </main>
</BaseLayout>

<style>
  .legal-page {
    max-width: 760px;
    margin: 0 auto;
    padding: 48px 24px 96px;
  }

  .page-header {
    margin-bottom: 40px;
    padding-bottom: 28px;
    border-bottom: 1px solid var(--border);
  }

  .page-header h1 {
    font-size: 36px;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
  }

  .page-header .subtitle {
    color: var(--text-muted);
    font-size: 16px;
    line-height: 1.6;
    max-width: 560px;
  }

  .prose {
    font-size: 16px;
    line-height: 1.7;
    color: var(--text);
  }

  .prose :global(h1) {
    /* The first H1 comes from PRIVACY.md ('# Kiln Privacy Policy')
       and duplicates the page-header above, so we hide it. */
    display: none;
  }

  .prose :global(h2) {
    font-size: 22px;
    font-weight: 700;
    margin-top: 48px;
    margin-bottom: 14px;
    padding-top: 28px;
    border-top: 1px solid var(--border);
    letter-spacing: -0.01em;
  }

  .prose :global(h2:first-of-type) {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
  }

  .prose :global(h3) {
    font-size: 17px;
    font-weight: 600;
    margin-top: 32px;
    margin-bottom: 10px;
    padding-left: 12px;
    border-left: 3px solid var(--accent);
  }

  .prose :global(p) { margin-bottom: 14px; }
  .prose :global(strong) { font-weight: 600; color: var(--text); }
  .prose :global(a) {
    color: var(--accent);
    text-decoration: none;
  }
  .prose :global(a:hover) { text-decoration: underline; }

  .prose :global(ul),
  .prose :global(ol) {
    margin-bottom: 14px;
    padding-left: 22px;
  }
  .prose :global(li) { margin-bottom: 4px; }

  .prose :global(hr) {
    border: none;
    border-top: 1px solid var(--border);
    margin: 40px 0;
  }

  .prose :global(code) {
    font-family: var(--mono);
    font-size: 0.875em;
    background: var(--bg-code);
    border: 1px solid var(--border);
    padding: 2px 6px;
    border-radius: 4px;
  }

  .prose :global(.table-wrap),
  .prose :global(table) {
    display: block;
    overflow-x: auto;
    margin: 20px 0;
  }
  .prose :global(table) {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
  }
  .prose :global(th) {
    text-align: left;
    padding: 10px 14px;
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
  }
  .prose :global(td) {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
  }
  .prose :global(tr:last-child td) { border-bottom: none; }

  .prose :global(blockquote) {
    border-left: 3px solid var(--accent);
    padding: 10px 18px;
    margin: 16px 0;
    background: rgba(255, 107, 43, 0.05);
    border-radius: 0 6px 6px 0;
    color: var(--text-muted);
  }
  .prose :global(blockquote p) { margin-bottom: 0; }

  .prose :global(em) {
    color: var(--text-muted);
    font-style: italic;
  }

  @media (max-width: 768px) {
    .legal-page { padding: 32px 16px 80px; }
    .page-header h1 { font-size: 28px; }
    .prose :global(h2) { font-size: 18px; margin-top: 32px; padding-top: 20px; }
    .prose :global(h3) { font-size: 15px; margin-top: 24px; }
  }
</style>
