/*
 * Colors from Flexoki by Steph Ango
 * MIT License - Copyright (c) 2023 Steph Ango
 * https://stephango.com/flexoki
 * https://github.com/kepano/flexoki
 */
:root {
    /* Base colors */
    --fx-paper: #fffcf0;
    --fx-base-200: #cecdc3;
    --fx-base-600: #6f6e69;
    --fx-base-850: #343331;
    --fx-base-950: #1c1b1a;
    --fx-black: #100f0f;

    /* Accent colors, one shade each */
    --fx-blue: #205ea6;
    --fx-green: #66800b;
    --fx-red: #af3029;
    --fx-orange: #bc5215;
    --fx-yellow: #ad8301;
    --fx-cyan: #24837b;
    --fx-purple: #5e409d;
    --fx-magenta: #a02f6f;

    /* Light theme */
    --bg: var(--fx-paper);
    --bg-alt: var(--fx-base-200);
    --text: var(--fx-base-950);
    --text-muted: var(--fx-base-600);
    --accent: var(--fx-blue);
    --border: var(--fx-base-200);
}

[data-theme="dark"] {
    /* Dark theme accent colors */
    --fx-blue: #4385be;
    --fx-green: #879a39;
    --fx-red: #d14d41;
    --fx-orange: #da702c;
    --fx-yellow: #d0a215;
    --fx-cyan: #3aa99f;
    --fx-purple: #8b7ec8;
    --fx-magenta: #ce5d97;

    /* Dark theme */
    --bg: var(--fx-black);
    --bg-alt: var(--fx-base-950);
    --text: var(--fx-base-200);
    --text-muted: var(--fx-base-200);
    --accent: var(--fx-blue);
    --border: var(--fx-base-850);
}

/* Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
    width: 100%;
}

.post-title {
    font-size: 2em; /* Same size as h1 */
    font-weight: bold;
    line-height: 1.2;
    margin: 0em 0;
    display: block;
    color: var(--text);
}

/* Base */
body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    transition:
        background 0.3s,
        color 0.3s;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100vw;
}

/* Layout */
.page-container {
    position: relative;
    max-width: 80ch;
    margin: 0 auto;
    padding: 0 1rem;
    flex: 1;
    width: 100%;
    min-width: 0;
}

.toc-sidebar {
    width: 280px;
    position: fixed;
    left: calc(50% - 40ch - 300px);
    top: 80px;
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    padding: 2rem 1rem;
}

.main-content {
    max-width: 80ch;
    padding: 2rem 0;
    margin: 0 auto;
    word-wrap: break-word;
    overflow-wrap: break-word;
    width: 100%;
    min-width: 0;
}

/* Mobile layout improvements */
@media (max-width: 1200px) {
    .page-container {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding: 0 0.5rem;
    }

    .toc-sidebar {
        position: static;
        width: 100%;
        max-height: none;
        padding: 1rem;
        order: 1;
        background: var(--bg-alt);
        border-radius: 6px;
        margin-bottom: 1rem;
    }

    .main-content {
        padding: 1rem;
        order: 2;
        max-width: 100%;
    }
}

/* Table of Contents */
.toc {
    position: sticky;
    top: 0;
}

.toc ul {
    list-style: none;
    padding-left: 0;
    margin: 0;
}

.toc > ul {
    border-left: 2px solid var(--border);
    padding-left: 0;
}

.toc li {
    margin: 0;
}

.toc a {
    display: block;
    padding: 0.5rem 0 0.5rem 1rem;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    line-height: 1.3;
    border-left: 2px solid transparent;
    margin-left: -2px;
    transition: all 0.2s ease;
}

.toc a:hover {
    color: var(--accent);
    border-left-color: var(--accent);
    background: var(--bg-alt);
    text-decoration: none;
}

.toc ul ul {
    padding-left: 1rem;
}

.toc ul ul a {
    font-size: 0.85rem;
    padding-left: 0.75rem;
}

.toc ul ul ul a {
    font-size: 0.8rem;
    padding-left: 0.5rem;
}

/* Typography */
h1,
h2,
h3,
h4,
h5,
h6 {
    margin-bottom: 1rem;
    line-height: 1.3;
    font-weight: 600;
    scroll-margin-top: 80px; /* Account for sticky header height */
}

h1 {
    font-size: 2rem;
}
h2 {
    font-size: 1.5rem;
    margin-top: 2rem;
}
h3 {
    font-size: 1.25rem;
    margin-top: 1.5rem;
}

p {
    margin-bottom: 1.25rem;
    color: var(--text-muted);
}

/* Blockquotes */
blockquote {
    margin: 1.5rem 0;
    padding: 0.75rem 1.25rem;
    border-left: 4px solid var(--accent);
    background: var(--bg-alt);
    border-radius: 0 6px 6px 0;
    font-style: italic;
}

blockquote p {
    margin-bottom: 0.75rem;
    color: var(--text);
}

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

blockquote cite {
    display: block;
    margin-top: 0.5rem;
    font-size: 0.9rem;
    color: var(--text-muted);
    font-style: normal;
}

/* Links */
a {
    color: var(--accent);
    text-decoration: none;
    transition: opacity 0.2s;
}

a:hover {
    opacity: 0.8;
    text-decoration: underline;
}

/* Lists */
ul,
ol {
    margin-bottom: 1.25rem;
    padding-left: 2rem;
}

li {
    margin-bottom: 0.5rem;
    color: var(--text-muted);
}

/* Code */
code {
    background: var(--bg-alt);
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    font-family: "SF Mono", Monaco, Consolas, monospace;
    font-size: 0.9rem;
    word-break: break-word;
}

pre {
    background: var(--bg-alt);
    padding: 1rem;
    border-radius: 6px;
    overflow-x: auto;
    margin-bottom: 1.25rem;
    max-width: 100%;
    width: 100%;
}

pre code {
    background: none;
    padding: 0;
    word-break: normal;
}

/* Header */
header {
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding: 1rem 0;
    position: sticky;
    top: 0;
    z-index: 1000;
    width: 100%;
    overflow-x: hidden;
}

nav {
    max-width: 80ch;
    margin: 0 auto;
    padding: 0 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    min-width: 0;
}

.nav-brand a {
    font-weight: 700;
    color: var(--text);
}

.nav-menu {
    display: flex;
    list-style: none;
    gap: 1.5rem;
    align-items: center;
    padding: 0;
    margin: 0;
    flex-wrap: wrap;
    min-width: 0;
}

.nav-menu li {
    margin: 0;
}

.nav-menu a {
    color: var(--text-muted);
    font-weight: 500;
    padding: 0.5rem;
    display: block;
}

/* Theme toggle */
.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 4px;
    width: 2.5rem;
    height: 2.5rem;
    cursor: pointer;
    color: var(--text-muted);
    transition: all 0.2s;
}

.theme-toggle:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* Footer */
footer {
    border-top: 1px solid var(--border);
    margin-top: 2rem;
    padding: 0.5rem 0;
    background: var(--bg);
    width: 100%;
    overflow-x: hidden;
}

.footer-content {
    max-width: 80ch;
    margin: 0 auto;
    padding: 0 1rem;
    text-align: center;
    width: 100%;
    min-width: 0;
}

.footer-content p {
    margin-bottom: 0.5rem;
}

.footer-links {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    margin: 0;
    flex-wrap: wrap;
    min-width: 0;
}

.footer-links a {
    color: var(--text-muted);
    font-weight: 500;
    padding: 0.5rem;
    display: block;
}

/* Images */
img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    border: 1px solid var(--border);
    margin: 1rem 0;
    display: block;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition:
        transform 0.2s ease,
        box-shadow 0.2s ease;
}

img:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] img {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] img:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

/* Tables */
.table-wrapper {
    overflow-x: auto;
    margin-bottom: 1.25rem;
    border-radius: 6px;
    border: 1px solid var(--border);
}

table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
    font-size: 0.9rem;
    min-width: 400px;
}

@media (min-width: 768px) {
    table {
        min-width: 0;
    }

    .table-wrapper {
        border: none;
        overflow-x: visible;
    }
}

th,
td {
    border: 1px solid var(--border);
    padding: 0.5rem;
    text-align: left;
    vertical-align: top;
}

th {
    background: var(--bg-alt);
    font-weight: 600;
    color: var(--text);
}

td {
    color: var(--text-muted);
}

/* Table images should be smaller */
td img {
    max-width: 150px;
    margin: 0.5rem 0;
    display: inline-block;
}

/* Collapsible Tables */
.collapsible-table-wrapper {
    margin-bottom: 1.5rem;
}

.table-toggle-btn {
    background: var(--bg-alt);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.75rem 1rem;
    margin-top: 0.5rem;
    cursor: pointer;
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    justify-content: center;
}

.table-toggle-btn:hover {
    background: var(--accent);
    color: var(--bg);
    border-color: var(--accent);
}

.toggle-icon {
    font-size: 0.8rem;
    transition: transform 0.2s ease;
}

/* Very small mobile screens */
@media (max-width: 400px) {
    nav {
        padding: 0 0.5rem;
        flex-direction: column;
        gap: 0.75rem;
        align-items: stretch;
    }

    .nav-brand {
        text-align: center;
    }

    .nav-menu {
        justify-content: center;
        gap: 1rem;
        flex-wrap: nowrap;
    }

    .nav-menu a {
        padding: 0.25rem 0.5rem;
        font-size: 0.9rem;
    }

    .theme-toggle {
        width: 2rem;
        height: 2rem;
        font-size: 0.8rem;
    }
}

/* Small mobile optimizations */
@media (max-width: 640px) {
    .page-container {
        padding: 0 0.25rem;
    }

    .main-content {
        padding: 1rem 0.5rem;
    }

    .nav-menu {
        gap: 0.75rem;
    }

    .footer-links {
        gap: 0.75rem;
    }

    h1,
    .post-title {
        font-size: 1.75rem;
    }

    h2 {
        font-size: 1.375rem;
    }

    h3 {
        font-size: 1.125rem;
    }

    table {
        font-size: 0.8rem;
        min-width: 300px;
    }

    th,
    td {
        padding: 0.375rem 0.25rem;
    }

    td img {
        max-width: 100px;
    }

    pre {
        padding: 0.75rem;
        font-size: 0.8rem;
    }

    .toc-sidebar {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
    }

    .toc a {
        padding: 0.375rem 0 0.375rem 0.75rem;
        font-size: 0.85rem;
    }
}

/* Footnote styling */
.footnote-item {
    scroll-margin-top: 80px; /* Account for sticky header height */
}

.footnote-ref {
    scroll-margin-top: 80px; /* Account for sticky header height when returning from footnotes */
}

/* Target the actual anchor elements that the return arrows link to */
[id^="fnref"] {
    scroll-margin-top: 80px; /* Account for sticky header height when returning from footnotes */
}

/* Scroll to top button */
.scroll-to-top {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    width: 3rem;
    height: 3rem;
    background: var(--bg-alt);
    color: var(--text);
    border: 2px solid var(--border);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    text-decoration: none;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.2s ease, background 0.2s ease, border-color 0.2s ease;
    z-index: 999;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.scroll-to-top:hover {
    transform: translateY(-3px);
    background: var(--accent);
    color: var(--bg);
    border-color: var(--accent);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

[data-theme="dark"] .scroll-to-top {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] .scroll-to-top:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

/* Show button when page is scrolled - using :target pseudo-class workaround */
/* Since pure CSS can't detect scroll position, we'll use a small JS alternative */
/* For truly no-JS, the button will always show after initial page load */
body:not(.at-top) .scroll-to-top {
    opacity: 1;
    pointer-events: all;
}

@media (max-width: 640px) {
    .scroll-to-top {
        bottom: 1rem;
        right: 1rem;
        width: 2.5rem;
        height: 2.5rem;
        font-size: 1.25rem;
    }
}
