/* ==========================================================================
   Blog — Modern essay typography with margin notes
   Inspired by Tufte CSS, adapted for DigitalMe

   Layout philosophy:
   - Mobile-first: everything is full-width by default
   - Wide desktop (≥1100px): three-column layout via CSS Grid:
     sidebar (180px) | content (720px) | margin notes (260px)
     Grid enforces column boundaries — overlap is structurally impossible.
   - Below 1100px: single centred column, sidebar → hamburger, notes → footnotes
   ========================================================================== */

/* ---------- Fonts ---------- */
:root {
    --blog-font-body: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
    --blog-font-heading: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --blog-font-code: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;
    --blog-font-margin: var(--blog-font-body);

    --blog-text: #1a1a1a;
    --blog-text-light: #555;
    --blog-text-muted: #888;
    --blog-bg: #fffff8;
    --blog-bg-code: #f7f7f4;
    --blog-accent: #5b8c8a;
    --blog-accent-warm: #d4956a;
    --blog-link: #5b8c8a;
    --blog-link-hover: #4a7c7a;
    --blog-border: #e0ddd5;
    --blog-margin-text: #666;
    --blog-margin-bg: transparent;

    --blog-body-size: 1.2rem;
    --blog-line-height: 1.75;
    --blog-margin-size: 0.85rem;
    --blog-content-width: 720px;
    --blog-margin-width: 260px;
    --blog-gap: 28px;
}

/* ---------- Reset & base ---------- */
.blog-page {
    margin: 0;
    padding: 0;
    background: var(--blog-bg);
    color: var(--blog-text);
    font-family: var(--blog-font-body);
    font-size: var(--blog-body-size);
    line-height: var(--blog-line-height);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
}

/* Global image safety */
.blog-page img {
    max-width: 100%;
    height: auto;
}

/* ---------- Navigation ---------- */
.blog-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 auto;
    padding: 24px 24px 0;
}

.blog-nav-logo {
    font-family: var(--blog-font-heading);
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--blog-text);
    text-decoration: none;
    letter-spacing: -0.02em;
}

.blog-nav-logo span {
    color: var(--blog-text-muted);
    font-weight: 400;
}

.blog-nav-links {
    display: flex;
    gap: 24px;
    font-family: var(--blog-font-heading);
    font-size: 0.85rem;
}

.blog-nav-links a {
    color: var(--blog-text-light);
    text-decoration: none;
    transition: color 0.2s;
}

.blog-nav-links a:hover {
    color: var(--blog-accent);
}

/* ---------- Article layout ---------- */
.blog-article {
    margin: 0 auto;
    padding: 0 24px 80px;
    position: relative;
}

/* ---------- Header ---------- */
.blog-header {
    margin: 60px 0 48px;
}

.blog-header h1 {
    font-family: var(--blog-font-heading);
    font-size: 2.6rem;
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: -0.03em;
    color: var(--blog-text);
    margin: 0 0 16px;
}

.blog-lede {
    font-size: 1.35rem;
    line-height: 1.55;
    color: var(--blog-text-light);
    font-style: italic;
    margin: 0 0 24px;
}

.blog-meta {
    font-family: var(--blog-font-heading);
    font-size: 0.85rem;
    color: var(--blog-text-muted);
    display: flex;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
}

.blog-meta-divider {
    width: 3px;
    height: 3px;
    border-radius: 50%;
    background: var(--blog-text-muted);
}

/* ---------- Hero images ---------- */
.blog-hero-images {
    display: flex;
    gap: 24px;
    margin: 0 0 48px;
}

.blog-hero-images figure {
    flex: 1;
    margin: 0;
}

.blog-hero-images img {
    width: 100%;
    max-width: 100%;
    height: 280px;
    object-fit: cover;
    border-radius: 8px;
}

.blog-hero-images figcaption {
    font-family: var(--blog-font-heading);
    font-size: 0.75rem;
    color: var(--blog-text-muted);
    margin-top: 8px;
    text-align: center;
}

/* ---------- Content sections ---------- */
.blog-section {
    position: relative;
    margin-bottom: 40px;
}

.blog-section p {
    margin: 0 0 1.2em;
}

.blog-section h2 {
    font-family: var(--blog-font-heading);
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--blog-text);
    margin: 56px 0 20px;
}

.blog-section h3 {
    font-family: var(--blog-font-heading);
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--blog-text);
    margin: 40px 0 16px;
}

/* ---------- Emphasis & strong ---------- */
.blog-section strong {
    font-weight: 700;
    color: var(--blog-text);
}

.blog-section em {
    font-style: italic;
}

/* ---------- Links ---------- */
.blog-section a {
    color: var(--blog-link);
    text-decoration: underline;
    text-decoration-color: rgba(91, 140, 138, 0.3);
    text-underline-offset: 2px;
    transition: text-decoration-color 0.2s;
}

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

/* ---------- Block quotes ---------- */
.blog-section blockquote {
    margin: 32px 0;
    padding: 0 0 0 24px;
    border-left: 3px solid var(--blog-accent);
    font-style: italic;
    color: var(--blog-text-light);
}

.blog-section blockquote p {
    margin-bottom: 0.6em;
}

/* ---------- Callout boxes ---------- */
/* A blockquote whose first child is a <strong> becomes a callout.
   Write in markdown as:  > **Title**
                          > Content here...                          */
.blog-section blockquote.blog-callout {
    font-style: normal;
    color: var(--blog-text);
    background: #f9f7f4;
    border-left: none;
    border: 1px solid var(--blog-border);
    border-radius: 8px;
    padding: 28px 32px;
    margin: 40px 0;
}

.blog-section blockquote.blog-callout p:first-child strong {
    font-family: var(--blog-font-heading);
    font-size: 1.1rem;
    display: block;
    margin-bottom: 12px;
    color: var(--blog-text);
}

.blog-section blockquote.blog-callout p {
    margin-bottom: 0.8em;
}

.blog-section blockquote.blog-callout p:last-child {
    margin-bottom: 0;
}

.blog-section blockquote.blog-callout a {
    color: var(--blog-accent);
    font-weight: 600;
}

/* ---------- Code ---------- */
.blog-section code {
    font-family: var(--blog-font-code);
    font-size: 0.88em;
    background: var(--blog-bg-code);
    padding: 2px 6px;
    border-radius: 3px;
}

.blog-section pre {
    background: var(--blog-bg-code);
    padding: 20px 24px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 24px 0;
    line-height: 1.5;
}

.blog-section pre code {
    background: none;
    padding: 0;
}

/* Highlight.js overrides — keep our background, let hljs handle token colours */
.blog-section pre code.hljs {
    background: var(--blog-bg-code);
    padding: 0;
}

/* ---------- Tables ---------- */
.blog-section table {
    border-collapse: collapse;
    margin: 24px 0;
    font-size: 0.92rem;
    line-height: 1.5;
}

.blog-section thead th {
    text-align: left;
    font-weight: 600;
    border-bottom: 2px solid var(--blog-border);
    padding: 8px 12px;
}

.blog-section tbody td {
    border-bottom: 1px solid var(--blog-border);
    padding: 8px 12px;
}

.blog-section tbody tr:last-child td {
    border-bottom: none;
}

.blog-section table code {
    font-size: 0.85em;
}

/* ---------- Lists ---------- */
.blog-section ul,
.blog-section ol {
    margin: 0 0 1.2em;
    padding-left: 1.5em;
}

.blog-section li {
    margin-bottom: 0.4em;
}

/* ---------- Horizontal rule ---------- */
.blog-section hr {
    border: none;
    border-top: 1px solid var(--blog-border);
    margin: 48px 0;
}

/* ---------- Inline HTML blocks (e.g. side-by-side images via <div><table>) ---------- */
.blog-section div {
    max-width: 100%;
}

.blog-section table img {
    max-width: 100%;
    height: auto;
}

/* ---------- Images ---------- */
.blog-section img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
}

.blog-section figure {
    margin: 32px 0;
}

.blog-section figure img {
    width: 100%;
    height: auto;
}

.blog-section figcaption {
    font-family: var(--blog-font-heading);
    font-size: 0.8rem;
    color: var(--blog-text-muted);
    margin-top: 8px;
}

/* ==========================================================================
   Margin notes — Tufte-style
   Hidden by default (mobile/tablet). Wide desktop shows them in CSS Grid column.
   ========================================================================== */

.margin-ref {
    font-family: var(--blog-font-heading);
    font-size: 0.7rem;
    color: var(--blog-accent);
    cursor: pointer;
    position: relative;
    top: -0.4em;
    font-weight: 600;
    margin-left: 1px;
    user-select: none;
    transition: color 0.2s;
    text-decoration: none;
}

.margin-ref:hover {
    color: var(--blog-accent-warm);
}

.margin-note {
    display: none;
}

.margin-note-num {
    font-family: var(--blog-font-heading);
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--blog-accent);
    margin-right: 4px;
}

/* ==========================================================================
   Footnotes — visible by default, hidden on wide desktop where margin notes show
   ========================================================================== */

.blog-footnotes h2 {
    font-family: var(--blog-font-heading);
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--blog-text);
    margin: 48px 0 16px;
    padding-top: 24px;
    border-top: 1px solid var(--blog-border);
}

.blog-footnotes ol {
    margin: 0;
    padding-left: 1.5em;
    font-size: 0.9rem;
    line-height: 1.6;
    color: var(--blog-text-light);
}

.blog-footnotes li {
    margin-bottom: 12px;
    padding-left: 4px;
}

.blog-footnotes li a,
.blog-footnotes li code,
.blog-footnotes li em {
    font-size: inherit;
}

.fn-backref {
    color: var(--blog-accent);
    text-decoration: none;
    font-size: 0.85em;
    white-space: nowrap;
}

/* ---------- Footer ---------- */
.blog-footer {
    margin: 64px 0 0;
    padding: 32px 0 0;
    border-top: 1px solid var(--blog-border);
    font-family: var(--blog-font-heading);
    font-size: 0.85rem;
    color: var(--blog-text-muted);
}

.blog-footer a {
    color: var(--blog-accent);
    text-decoration: none;
}

.blog-footer a:hover {
    text-decoration: underline;
}

/* ==========================================================================
   Layout wrapper
   Mobile: single centred column.
   Wide desktop: CSS Grid with sidebar | article (which itself is a 2-col grid).
   ========================================================================== */

.blog-layout {
    /* Mobile: single column, no grid */
    max-width: var(--blog-content-width);
    margin: 0 auto;
}

/* ==========================================================================
   Blog index sidebar — hidden by default, shown on desktop
   ========================================================================== */

.blog-index {
    display: none;
}

.blog-index-inner {
    /* Inner wrapper for sticky positioning within grid cell */
}

.blog-index-header {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--blog-text-muted);
    margin-bottom: 12px;
}

.blog-index-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.blog-index-list li {
    margin-bottom: 12px;
}

.blog-index-list li a {
    font-size: 0.82rem;
    line-height: 1.35;
    color: var(--blog-text-light);
    text-decoration: none;
    display: block;
    transition: color 0.2s;
    overflow-wrap: break-word;
    word-wrap: break-word;
}

.blog-index-list li a:hover {
    color: var(--blog-accent);
}

.blog-index-list li.active a {
    color: var(--blog-text);
    font-weight: 600;
}

.blog-index-date {
    display: block;
    font-size: 0.7rem;
    color: var(--blog-text-muted);
    margin-top: 2px;
}

/* ==========================================================================
   Hamburger button — hidden on wide desktop (≥1100px), visible below
   ========================================================================== */

.blog-hamburger {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    margin: -8px -8px -8px 0;
    z-index: 1001;
    position: relative;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    -webkit-tap-highlight-color: transparent;
}

.blog-hamburger span {
    display: block;
    width: 22px;
    height: 2px;
    background: var(--blog-text);
    border-radius: 1px;
    transition: transform 0.3s ease, opacity 0.2s ease;
    transform-origin: center;
}

/* Animate to X when open */
.blog-hamburger.open span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}

.blog-hamburger.open span:nth-child(2) {
    opacity: 0;
}

.blog-hamburger.open span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

/* ==========================================================================
   Mobile slide-down menu — hidden by default
   ========================================================================== */

.blog-mobile-menu {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--blog-bg);
    z-index: 1000;
    padding: 72px 24px 32px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(-8px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s;
}

.blog-mobile-menu.open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0);
}

.blog-mobile-menu-nav {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.blog-mobile-menu-nav a {
    font-family: var(--blog-font-heading);
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--blog-text);
    text-decoration: none;
    padding: 8px 0;
    transition: color 0.2s;
}

.blog-mobile-menu-nav a:hover {
    color: var(--blog-accent);
}

.blog-mobile-menu-divider {
    height: 1px;
    background: var(--blog-border);
    margin: 16px 0;
}

.blog-mobile-menu-header {
    font-family: var(--blog-font-heading);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--blog-text-muted);
    margin-bottom: 12px;
}

.blog-mobile-menu-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.blog-mobile-menu-list li {
    margin-bottom: 16px;
}

.blog-mobile-menu-list li a {
    font-family: var(--blog-font-heading);
    font-size: 1rem;
    line-height: 1.4;
    color: var(--blog-text-light);
    text-decoration: none;
    display: block;
    padding: 4px 0;
    transition: color 0.2s;
}

.blog-mobile-menu-list li a:hover {
    color: var(--blog-accent);
}

.blog-mobile-menu-list li.active a {
    color: var(--blog-text);
    font-weight: 600;
}

.blog-mobile-menu-date {
    display: block;
    font-family: var(--blog-font-heading);
    font-size: 0.75rem;
    color: var(--blog-text-muted);
    margin-top: 2px;
}

/* ==========================================================================
   Responsive — three tiers
   1. Mobile (< 780px): single column, hamburger menu, footnotes
   2. Content-only (780px – 1099px): centred 720px column, hamburger, footnotes
   3. Full 3-col (≥ 1100px): sidebar(180) | content(720) | margin-notes(260)
      Grid enforces column boundaries — overlap is structurally impossible.
   ========================================================================== */

/* ---------- Tier 2: Centred content column (no sidebar, no margin notes) ---- */
@media (min-width: 780px) and (max-width: 1099px) {
    .blog-nav {
        max-width: var(--blog-content-width);
        margin: 0 auto;
        padding: 24px 0 0;
    }

    .blog-article {
        max-width: var(--blog-content-width);
        margin: 0 auto;
        padding: 0 0 80px;
    }
}

/* ---------- Tier 3: Full three-column layout ---- */
/* Sidebar(180) + gap(24) + content(720) + gap(28) + margin(260) + padding(48) = 1260px.
   Breakpoint 1100px is tight but the grid prevents any overlap regardless. */
@media (min-width: 1100px) {
    /* Hide hamburger — inline nav links + sidebar handle navigation */
    .blog-hamburger {
        display: none;
    }

    .blog-nav-links {
        display: flex;
    }

    /* Widen nav to match full grid width */
    .blog-nav {
        max-width: calc(180px + 24px + var(--blog-content-width) + var(--blog-gap) + var(--blog-margin-width));
        padding: 24px 24px 0;
    }

    /* Outer grid: sidebar | article area */
    .blog-layout {
        display: grid;
        grid-template-columns: 180px 1fr;
        gap: 0 24px;
        max-width: calc(180px + 24px + var(--blog-content-width) + var(--blog-gap) + var(--blog-margin-width));
        margin: 0 auto;
        padding: 0 24px;
        align-items: start;
        overflow: hidden;
    }

    /* Sidebar: constrained to its 180px grid column */
    .blog-index {
        display: block;
        font-family: var(--blog-font-heading);
        max-width: 180px;
        overflow: hidden;
    }

    .blog-index-inner {
        position: sticky;
        top: 24px;
        max-height: calc(100vh - 48px);
        overflow-y: auto;
        overflow-x: hidden;
        padding: 24px 0;
    }

    /* Article area: contains content + margin notes as a 2-col grid */
    .blog-article {
        max-width: calc(var(--blog-content-width) + var(--blog-gap) + var(--blog-margin-width));
        padding: 0 0 80px;
        min-width: 0;
    }

    /* When no sidebar exists, article spans all columns */
    .blog-article:only-child {
        grid-column: 1 / -1;
        margin: 0 auto;
    }

    /* Each section is a 2-col grid: content | margin notes.
     * All block elements default to column 1 (content).
     * Margin notes go to column 2. Grid enforces boundaries.
     * dense packing places margin notes beside their preceding paragraph. */
    .blog-section {
        display: grid;
        grid-template-columns: var(--blog-content-width) var(--blog-margin-width);
        gap: 0 var(--blog-gap);
        grid-auto-flow: dense;
    }

    /* All content elements stay in column 1 */
    .blog-section > * {
        grid-column: 1;
        min-width: 0;
    }

    /* Margin notes go in column 2 */
    .blog-section > .margin-note {
        display: block;
        grid-column: 2;
        font-family: var(--blog-font-margin);
        font-size: var(--blog-margin-size);
        line-height: 1.5;
        color: var(--blog-margin-text);
        padding: 0;
        margin: 0;
        /* Align with the previous content row */
        align-self: start;
    }

    .blog-section > .margin-note a {
        color: var(--blog-link);
        text-decoration: underline;
        text-decoration-color: rgba(91, 140, 138, 0.3);
        text-underline-offset: 2px;
    }

    .blog-section > .margin-note a:hover {
        text-decoration-color: var(--blog-link-hover);
    }

    /* Cap non-grid elements (header, hero, footer) to content width */
    .blog-header,
    .blog-hero-images,
    .blog-footer {
        max-width: var(--blog-content-width);
        box-sizing: border-box;
    }

    /* Hide footnotes — margin notes are visible in their grid column */
    .blog-footnotes {
        display: none;
    }
}

/* ---------- Below full layout — show hamburger, hide inline nav links ---- */
@media (max-width: 1099px) {
    .blog-hamburger {
        display: flex;
    }

    .blog-nav-links {
        display: none;
    }
}

/* ---------- Narrow screens — phone portrait ---------- */
@media (max-width: 680px) {
    :root {
        --blog-body-size: 1.05rem;
    }

    .blog-nav {
        padding: 16px 16px 0;
    }

    .blog-nav-links {
        gap: 16px;
    }

    .blog-article {
        padding: 0 16px 60px;
    }

    .blog-header {
        margin: 40px 0 32px;
    }

    .blog-header h1 {
        font-size: 1.9rem;
    }

    .blog-lede {
        font-size: 1.1rem;
    }

    .blog-hero-images {
        flex-direction: column;
        gap: 16px;
    }

    .blog-hero-images img {
        height: auto;
        max-height: 300px;
    }

    .blog-section h2 {
        font-size: 1.35rem;
        margin-top: 40px;
    }
}

/* ---------- Print ---------- */
@media print {
    .blog-nav,
    .blog-index {
        display: none;
    }

    .blog-article {
        max-width: 100%;
    }
}
