<!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 — default theme
   Classless CSS for semantic HTML produced by markdown renderers.
   Dark/light via prefers-color-scheme. Zero external requests.
   Designed for reading. Tufte-informed, hearth-warmed. */

/* ═══════════════════════════════════════════
   COLOR SYSTEM
   ═══════════════════════════════════════════ */

:root {
    /* Light mode — warm parchment, not clinical white */
    --bg: #faf8f5;
    --bg-subtle: #f3f0eb;
    --fg: #2c2825;
    --fg-secondary: #5c554e;
    --fg-muted: #8a827a;
    --border: #e2ddd7;
    --border-strong: #c9c2ba;
    --accent: #8b5e3c;
    --accent-hover: #6d4a2f;
    --link: #4a6fa5;
    --link-visited: #7a5e8a;
    --link-hover: #2d4a73;
    --code-bg: #f0ece7;
    --code-border: #e2ddd7;
    --code-fg: #4a4540;
    --mark-bg: #faecd0;
    --table-stripe: #f7f4f0;
    --shadow: rgba(44, 40, 37, 0.06);

    /* Typography */
    --font-body: Charter, 'Bitstream Charter', 'Sitka Text', Cambria, serif;
    --font-heading: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, 'SF Mono', Consolas, 'Liberation Mono', Menlo, monospace;
    --font-size: 1.0625rem;
    --line-height: 1.75;
    --max-width: 850px;

    /* Spacing scale — modular, based on line-height */
    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.75rem;
    --space-xl: 2.5rem;
    --space-xxl: 4rem;
}

@media (prefers-color-scheme: dark) {
    :root {
        /* Dark mode — deep warm charcoal, not blue-black */
        --bg: #1c1917;
        --bg-subtle: #252220;
        --fg: #ede9e4;
        --fg-secondary: #b8b0a8;
        --fg-muted: #87807a;
        --border: #3d3632;
        --border-strong: #5c534d;
        --accent: #d4a574;
        --accent-hover: #e2bb92;
        --link: #7ea8d4;
        --link-visited: #b09ac0;
        --link-hover: #a8cae8;
        --code-bg: #252220;
        --code-border: #3d3632;
        --code-fg: #d4cec8;
        --mark-bg: #4a3d25;
        --table-stripe: #222018;
        --shadow: rgba(0, 0, 0, 0.2);
    }
}

/* ═══════════════════════════════════════════
   RESET & BASE
   ═══════════════════════════════════════════ */

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

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    -moz-osx-font-smoothing: grayscale;
    -webkit-font-smoothing: antialiased;
    scroll-behavior: smooth;
}

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;
    word-wrap: break-word;
    hyphens: auto;
    text-rendering: optimizeLegibility;
}

/* ═══════════════════════════════════════════
   LAYOUT
   ═══════════════════════════════════════════ */

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

article {
    min-width: 0;
}

/* ═══════════════════════════════════════════
   TYPOGRAPHY — Heading Hierarchy
   ═══════════════════════════════════════════ */

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);
    letter-spacing: -0.01em;
}

h1 {
    font-size: 2.25rem;
    font-weight: 800;
    margin-top: 0;
    margin-bottom: var(--space-lg);
    letter-spacing: -0.025em;
    line-height: 1.1;
}

h2 {
    font-size: 1.5rem;
    margin-top: var(--space-xxl);
    padding-bottom: var(--space-xs);
    border-bottom: 1px solid var(--border);
}

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

h4 {
    font-size: 1.0625rem;
    margin-top: var(--space-lg);
    font-weight: 600;
    color: var(--fg-secondary);
    text-transform: uppercase;
    font-size: 0.8125rem;
    letter-spacing: 0.05em;
}

h5, h6 {
    font-size: var(--font-size);
    margin-top: var(--space-lg);
    font-weight: 600;
    color: var(--fg-muted);
}

/* ═══════════════════════════════════════════
   BODY TEXT
   ═══════════════════════════════════════════ */

p {
    margin: 0 0 var(--space-md);
    hanging-punctuation: first last;
}

/* First paragraph after a heading gets no extra top margin */
h1 + p, h2 + p, h3 + p, h4 + p {
    margin-top: 0;
}

strong, b {
    font-weight: 650;
}

em, i {
    font-style: italic;
}

small {
    font-size: 0.875rem;
    color: var(--fg-secondary);
}

mark {
    background: var(--mark-bg);
    padding: 0.1em 0.2em;
    border-radius: 2px;
}

abbr[title] {
    text-decoration: underline dotted;
    text-underline-offset: 3px;
    cursor: help;
}

/* ═══════════════════════════════════════════
   LINKS
   ═══════════════════════════════════════════ */

a {
    color: var(--link);
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, var(--link) 40%, transparent);
    text-underline-offset: 3px;
    text-decoration-thickness: 1px;
    transition: color 0.15s ease, text-decoration-color 0.15s ease;
}

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

a:hover {
    color: var(--link-hover);
    text-decoration-color: var(--link-hover);
}

a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 2px;
}

/* ═══════════════════════════════════════════
   LISTS
   ═══════════════════════════════════════════ */

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

li {
    margin: 0.3em 0;
}

li > ul, li > ol {
    margin-bottom: 0;
}

/* Task lists (GitHub-flavored markdown) */
li input[type="checkbox"] {
    margin-right: 0.5em;
    transform: translateY(1px);
}

dt {
    font-weight: 600;
    margin-top: var(--space-md);
}

dd {
    margin-left: 1.5em;
    margin-bottom: var(--space-sm);
    color: var(--fg-secondary);
}

/* ═══════════════════════════════════════════
   BLOCKQUOTE
   ═══════════════════════════════════════════ */

blockquote {
    margin: var(--space-lg) 0;
    padding: var(--space-sm) 0 var(--space-sm) var(--space-lg);
    border-left: 3px solid var(--accent);
    color: var(--fg-secondary);
    font-style: italic;
}

blockquote p:last-child {
    margin-bottom: 0;
}

blockquote cite {
    display: block;
    margin-top: var(--space-sm);
    font-size: 0.875rem;
    font-style: normal;
    color: var(--fg-muted);
}

blockquote cite::before {
    content: "\2014\00a0";
}

/* Nested blockquotes */
blockquote blockquote {
    margin-top: var(--space-sm);
    border-left-color: var(--border);
}

/* ═══════════════════════════════════════════
   CODE BLOCKS
   ═══════════════════════════════════════════ */

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: 4px;
    padding: 0.15em 0.4em;
    word-break: normal;
}

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

pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 0.8125rem;
    line-height: 1.6;
    word-break: normal;
    color: var(--code-fg);
}

/* Syntax highlighting — warm palette */
pre code .comment,
pre code .prolog { color: var(--fg-muted); font-style: italic; }

pre code .keyword,
pre code .tag { color: #a0616a; }

pre code .string,
pre code .attr-value { color: #6a8f5c; }

pre code .function,
pre code .class-name { color: #8b7e4f; }

pre code .number,
pre code .boolean { color: #9a6e3a; }

pre code .operator,
pre code .punctuation { color: var(--fg-secondary); }

pre code .property,
pre code .attr-name { color: #7a6f8a; }

@media (prefers-color-scheme: dark) {
    pre code .keyword,
    pre code .tag { color: #d4868f; }

    pre code .string,
    pre code .attr-value { color: #9abb8a; }

    pre code .function,
    pre code .class-name { color: #c4b77a; }

    pre code .number,
    pre code .boolean { color: #d4a06a; }

    pre code .property,
    pre code .attr-name { color: #b0a0c4; }
}

/* ═══════════════════════════════════════════
   TABLES
   ═══════════════════════════════════════════ */

table {
    border-collapse: collapse;
    width: 100%;
    margin: var(--space-lg) 0;
    font-size: 0.9rem;
    line-height: 1.5;
}

thead {
    border-bottom: 2px solid var(--border-strong);
}

th {
    padding: var(--space-sm) var(--space-md);
    text-align: left;
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: 0.8125rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--fg-secondary);
}

td {
    padding: var(--space-sm) var(--space-md);
    text-align: left;
    border-bottom: 1px solid var(--border);
}

/* Alternating rows — subtle */
tbody tr:nth-child(even) {
    background: var(--table-stripe);
}

/* Responsive table wrapper */
table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

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;
}

/* ═══════════════════════════════════════════
   HORIZONTAL RULE
   ═══════════════════════════════════════════ */

hr {
    border: none;
    height: 1px;
    background: linear-gradient(
        to right,
        transparent,
        var(--border-strong) 20%,
        var(--border-strong) 80%,
        transparent
    );
    margin: var(--space-xxl) 0;
}

/* ═══════════════════════════════════════════
   IMAGES & MEDIA
   ═══════════════════════════════════════════ */

img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    margin: var(--space-md) 0;
}

figure {
    margin: var(--space-lg) 0;
    padding: 0;
}

figure img {
    margin-bottom: var(--space-sm);
}

figcaption {
    font-size: 0.8125rem;
    color: var(--fg-muted);
    font-style: italic;
    text-align: center;
}

/* ═══════════════════════════════════════════
   DETAILS / SUMMARY
   ═══════════════════════════════════════════ */

details {
    margin: var(--space-md) 0;
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-subtle);
    border-radius: 6px;
    border: 1px solid var(--border);
}

summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--fg-secondary);
}

details[open] summary {
    margin-bottom: var(--space-sm);
}

/* ═══════════════════════════════════════════
   FOOTER
   ═══════════════════════════════════════════ */

footer {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: var(--space-lg) var(--space-lg) var(--space-xl);
    text-align: center;
}

footer::before {
    content: "";
    display: block;
    width: 3rem;
    height: 1px;
    background: var(--border);
    margin: 0 auto var(--space-lg);
}

footer small {
    color: var(--fg-muted);
    font-size: 0.75rem;
    font-family: var(--font-heading);
    letter-spacing: 0.05em;
    text-transform: lowercase;
}

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);
}

/* ═══════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════ */

@media (max-width: 600px) {
    :root {
        --font-size: 1rem;
    }

    main {
        padding: var(--space-xl) var(--space-md) var(--space-xl);
    }

    h1 {
        font-size: 1.75rem;
    }

    h2 {
        font-size: 1.3rem;
    }

    pre {
        padding: var(--space-sm) var(--space-md);
        border-radius: 4px;
        font-size: 0.8em;
    }

    table {
        font-size: 0.85rem;
    }

    th, td {
        padding: var(--space-xs) var(--space-sm);
    }

    blockquote {
        margin-left: 0;
        margin-right: 0;
        padding-left: var(--space-md);
    }
}

/* Fine-tuned for larger displays */
@media (min-width: 1200px) {
    main {
        padding-top: var(--space-xxl);
        padding-bottom: var(--space-xxl);
    }
}

/* ═══════════════════════════════════════════
   PRINT
   ═══════════════════════════════════════════ */

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

    body {
        background: white;
        color: black;
        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: #666;
    }

    pre, code {
        border: 1px solid #ddd;
    }

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

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

    footer::before {
        display: none;
    }
}

/* ═══════════════════════════════════════════
   SELECTION
   ═══════════════════════════════════════════ */

::selection {
    background: color-mix(in srgb, var(--accent) 25%, transparent);
    color: var(--fg);
}

/* ═══════════════════════════════════════════
   TOOLBAR
   ═══════════════════════════════════════════ */

.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;
}

@media (max-width: 400px) {
    .doc-toolbar {
        justify-content: flex-start;
    }
}

/* 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;
}

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

@media (prefers-color-scheme: dark) {
    .doc-expiry.expiry-urgent {
        color: #e57373;
    }
}

/* Toast notification */
.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);
}

@media print {
    .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>
                {% if full_view %}
                <a class="doc-btn" id="btn-toggle-view" href="#" title="Summary view">
                    &larr; Summary
                </a>
                {% else %}
                <a class="doc-btn" id="btn-toggle-view" href="#" title="Full rendered view with all sections">
                    Full detail &rarr;
                </a>
                {% endif %}
            </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);
        }

        // Derive slug from current path: /:slug or /:slug/full
        var pathParts = window.location.pathname.replace(/^\//, '').split('/');
        var slug = pathParts[0];
        var rawUrl = window.location.origin + '/' + slug + '?raw=1';
        var summaryUrl = window.location.origin + '/' + slug;
        var fullUrl = window.location.origin + '/' + slug + '/full';

        // Wire up link buttons
        document.getElementById('btn-view-raw').href = rawUrl;
        {% if full_view %}
        document.getElementById('btn-toggle-view').href = summaryUrl;
        {% else %}
        document.getElementById('btn-toggle-view').href = fullUrl;
        {% endif %}

        // Copy URL button
        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');
        }
        update();
        setInterval(update, 1000);
    })();
    </script>
</body>
</html>
