<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ title }}</title>
    <link rel="alternate" type="text/markdown" href="/api/v1/documents/{{ slug }}" title="Full document (markdown)">
    <style>
/* twofold — paper theme
   Warm. Serif body text. Book-like. Light-only. Zero external requests. */

:root {
    --bg: #fefcf9;
    --bg-subtle: #f8f4ee;
    --fg: #333333;
    --fg-secondary: #555555;
    --fg-muted: #888888;
    --border: #e8e0d4;
    --border-strong: #d4c8b8;
    --accent: #8b4513;
    --accent-hover: #6b3410;
    --link: #2b5797;
    --link-visited: #654a80;
    --link-hover: #1a3a6b;
    --code-bg: #f5f0e8;
    --code-border: #e0d8cc;
    --code-fg: #444444;
    --mark-bg: #fff3cc;
    --table-stripe: #faf6f0;
    --shadow: rgba(0, 0, 0, 0.04);

    --font-body: Georgia, 'Times New Roman', 'Nimbus Roman No9 L', serif;
    --font-heading: Georgia, 'Times New Roman', serif;
    --font-mono: 'Courier New', Courier, monospace;
    --font-size: 1.125rem;
    --line-height: 1.85;
    --max-width: 640px;

    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.75rem;
    --space-xl: 2.5rem;
    --space-xxl: 4rem;
}

*, *::before, *::after { box-sizing: border-box; }

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font-body);
    font-size: var(--font-size);
    line-height: var(--line-height);
    overflow-wrap: break-word;
    hyphens: auto;
    text-rendering: optimizeLegibility;
}

main {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: var(--space-xxl) var(--space-lg);
}

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-heading);
    font-weight: 700;
    line-height: 1.2;
    color: var(--fg);
    margin-bottom: var(--space-sm);
}

h1 {
    font-size: 2rem;
    margin-top: 0;
    margin-bottom: var(--space-xl);
    font-style: italic;
    font-weight: 400;
    text-align: center;
    border-bottom: none;
}

h2 {
    font-size: 1.4rem;
    margin-top: var(--space-xxl);
    font-style: italic;
    font-weight: 400;
    color: var(--accent);
}

h3 {
    font-size: 1.15rem;
    margin-top: var(--space-xl);
    color: var(--fg-secondary);
    font-weight: 600;
}

h4, h5, h6 {
    font-size: 1rem;
    margin-top: var(--space-lg);
    color: var(--fg-secondary);
    font-variant: small-caps;
    letter-spacing: 0.05em;
}

p {
    margin: 0 0 var(--space-md);
    text-indent: 1.5em;
}

p:first-child, h1 + p, h2 + p, h3 + p, h4 + p, blockquote p, li p {
    text-indent: 0;
}

a {
    color: var(--link);
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, var(--link) 40%, transparent);
    text-underline-offset: 3px;
}
a:visited { color: var(--link-visited); }
a:hover { color: var(--link-hover); text-decoration-color: var(--link-hover); }

ul, ol { margin: 0 0 var(--space-md); padding-left: 1.5em; }
li { margin: 0.3em 0; }

blockquote {
    margin: var(--space-lg) var(--space-lg);
    padding: 0;
    border-left: none;
    font-style: italic;
    color: var(--fg-secondary);
    position: relative;
}
blockquote::before {
    content: "\201C";
    position: absolute;
    left: -0.6em;
    top: -0.2em;
    font-size: 3em;
    color: var(--border-strong);
    font-family: Georgia, serif;
}
blockquote p:last-child { margin-bottom: 0; }

code {
    font-family: var(--font-mono);
    font-size: 0.85em;
    background: var(--code-bg);
    color: var(--code-fg);
    border: 1px solid var(--code-border);
    border-radius: 2px;
    padding: 0.1em 0.3em;
}

pre {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 3px;
    padding: var(--space-md) var(--space-lg);
    overflow-x: auto;
    margin: var(--space-lg) 0;
}

pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 0.8rem;
    line-height: 1.6;
}

table {
    border-collapse: collapse;
    width: 100%;
    margin: var(--space-lg) 0;
    font-size: 0.9rem;
}
thead { border-bottom: 2px solid var(--accent); }
th {
    padding: var(--space-sm) var(--space-md);
    text-align: left;
    font-weight: 700;
    font-variant: small-caps;
    letter-spacing: 0.03em;
}
td {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border);
}
tbody tr:nth-child(even) { background: var(--table-stripe); }
table { display: block; overflow-x: auto; }
thead, tbody, tr, th, td { display: revert; }

/* Code inside cells stays on one line; table scrolls horizontally instead */
td code, th code {
    white-space: nowrap;
    word-break: keep-all;
}

hr {
    border: none;
    height: 0;
    margin: var(--space-xxl) auto;
    text-align: center;
}
hr::after {
    content: "\2022 \2022 \2022";
    color: var(--fg-muted);
    letter-spacing: 1em;
}

img { max-width: 100%; height: auto; }

footer {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: var(--space-xl) var(--space-lg);
    text-align: center;
}
footer::before {
    content: "";
    display: block;
    width: 4rem;
    height: 1px;
    background: var(--border-strong);
    margin: 0 auto var(--space-lg);
}
footer small {
    color: var(--fg-muted);
    font-size: 0.75rem;
    font-style: italic;
}

footer small a {
    color: var(--link);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    transition: color 0.15s ease;
}

footer small a:hover {
    color: var(--link-hover);
}

@media (max-width: 600px) {
    :root { --font-size: 1rem; }
    main { padding: var(--space-xl) var(--space-md); }
    h1 { font-size: 1.6rem; }
    blockquote { margin-left: var(--space-md); margin-right: 0; }
}

::selection {
    background: rgba(139, 69, 19, 0.15);
    color: var(--fg);
}

.doc-toolbar {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.4rem;
    margin-bottom: var(--space-lg);
    flex-wrap: wrap;
}

.doc-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.25em;
    padding: 0.2em 0.65em;
    font-family: var(--font-heading);
    font-size: 0.75rem;
    font-weight: 450;
    color: var(--fg-muted);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 999px;
    cursor: pointer;
    text-decoration: none;
    transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
    letter-spacing: 0.01em;
    white-space: nowrap;
    line-height: 1.6;
}

.doc-btn:hover {
    color: var(--fg-secondary);
    border-color: var(--border-strong);
    background: var(--bg-subtle);
    text-decoration: none;
}

.doc-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.doc-toast {
    position: fixed;
    bottom: var(--space-lg);
    right: var(--space-lg);
    padding: 0.4em 0.85em;
    background: var(--fg);
    color: var(--bg);
    font-family: var(--font-heading);
    font-size: 0.8rem;
    border-radius: 5px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(6px);
    transition: opacity 0.18s ease, transform 0.18s ease;
    z-index: 9999;
}

.doc-toast.doc-toast--visible {
    opacity: 1;
    transform: translateY(0);
}

/* Expiry countdown */
.doc-expiry {
    font-family: var(--font-heading);
    font-size: 0.72rem;
    color: var(--fg-muted);
    letter-spacing: 0.01em;
    white-space: nowrap;
    margin-right: auto;
    font-style: italic;
}

.doc-expiry.expiry-urgent {
    color: #c0392b;
}

@media print {
    @page { margin: 2cm; }

    body {
        background: white;
        color: #333333;
        font-size: 11pt;
    }

    main {
        max-width: none;
        padding: 0;
    }

    a {
        color: inherit;
        text-decoration: underline;
    }

    a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #888;
    }

    pre, code {
        background: #f5f0e8;
        color: #444444;
        border: 1px solid #ddd;
    }

    /* Decorative opening quote clips at page edges in print */
    blockquote::before {
        display: none;
    }

    pre, blockquote, table {
        page-break-inside: avoid;
    }

    h1, h2, h3, h4, h5, h6 {
        page-break-after: avoid;
    }

    .doc-toolbar, .doc-toast, .doc-expiry { display: none; }
}
    </style>
</head>
<body>
    <main>
        <article>
            <div class="doc-toolbar">
                {% if let Some(exp) = expires_at %}
                <div class="doc-expiry" data-expires="{{ exp }}">
                    <span class="expiry-label">Expires in </span><span class="expiry-countdown"></span>
                </div>
                {% endif %}
                <button class="doc-btn" id="btn-copy-url" title="Copy link to clipboard">
                    Copy link
                </button>
                <button class="doc-btn" id="btn-copy-md" title="Copy markdown source to clipboard">
                    Copy markdown
                </button>
                <button class="doc-btn" id="btn-pdf" title="Save as PDF"{% if body_empty %} hidden{% endif %}>PDF</button>
                <a class="doc-btn" id="btn-view-raw" href="#" title="View raw markdown source">
                    View raw
                </a>
            </div>
            {{ content|safe }}
        </article>
    </main>
    <footer>
        <small>shared via <a href="https://github.com/gabaum10/twofold">twofold</a></small>
    </footer>

    <div class="doc-toast" id="doc-toast" aria-live="polite"></div>

    <script>
    (function () {
        'use strict';

        var toast = document.getElementById('doc-toast');
        var toastTimer = null;

        function showToast(msg) {
            if (toastTimer) clearTimeout(toastTimer);
            toast.textContent = msg;
            toast.classList.add('doc-toast--visible');
            toastTimer = setTimeout(function () {
                toast.classList.remove('doc-toast--visible');
            }, 2000);
        }

        var pathParts = window.location.pathname.replace(/^\//, '').split('/');
        var slug = pathParts[0];
        var rawUrl = window.location.origin + '/' + slug + '?raw=1';

        document.getElementById('btn-view-raw').href = rawUrl;

        document.getElementById('btn-copy-url').addEventListener('click', function () {
            navigator.clipboard.writeText(window.location.href).then(function () {
                showToast('Copied!');
            }).catch(function () {
                showToast('Copy failed');
            });
        });

        document.getElementById('btn-copy-md').addEventListener('click', function () {
            fetch('/' + slug + '?raw=1')
                .then(function (r) { return r.text(); })
                .then(function (text) {
                    return navigator.clipboard.writeText(text);
                })
                .then(function () {
                    showToast('Copied markdown!');
                })
                .catch(function () {
                    showToast('Copy failed');
                });
        });

        var btnPdf = document.getElementById('btn-pdf');
        if (btnPdf) {
            btnPdf.addEventListener('click', function() {
                window.print();
            });
        }
    })();

    (function() {
        var el = document.querySelector('.doc-expiry');
        if (!el) return;
        var exp = new Date(el.dataset.expires);

        function update() {
            var now = new Date();
            var diff = exp - now;
            if (diff <= 0) {
                el.querySelector('.expiry-countdown').textContent = 'expired';
                el.classList.add('expired');
                return;
            }
            var h = Math.floor(diff / 3600000);
            var m = Math.floor((diff % 3600000) / 60000);
            var s = Math.floor((diff % 60000) / 1000);
            var text = '';
            if (h > 0) text += h + 'h ';
            if (h > 0 || m > 0) text += m + 'm ';
            text += s + 's';
            el.querySelector('.expiry-countdown').textContent = text;
            if (diff < 300000) el.classList.add('expiry-urgent');
            requestAnimationFrame(update);
        }
        update();
    })();
    </script>
</body>
</html>
