/* ───────────────────────────────────────────────────────────────────────────
   Editorial microfeatures — shared across the long-form pages.
   Keys off only the universal tokens (--text, --bg, --font) every page defines,
   with internal fallbacks so it adapts to each page's palette + light/dark for
   free. Namespaced `mfx-` to avoid colliding with MERFISH's `mf-` classes.
   Everything degrades gracefully and respects prefers-reduced-motion.
   ─────────────────────────────────────────────────────────────────────────── */

:root {
    --mfx-accent: var(--accent, #1f9e89);
    --mfx-rule: color-mix(in srgb, var(--text, #1f1e1d) 14%, transparent);
    --mfx-surface: color-mix(in srgb, var(--bg, #f0eee6) 92%, var(--text, #1f1e1d));
    --mfx-shadow: 0 8px 30px color-mix(in srgb, var(--text, #1f1e1d) 22%, transparent);
    --mfx-font-sans: var(--font-sans, -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif);
    --mfx-caption: var(--caption, color-mix(in srgb, var(--text, #1f1e1d) 55%, transparent));
}

/* ─── Self-linking headings ─────────────────────────────────────────────── */
.mfx-anchor {
    margin-left: 0.35em;
    font-size: 0.7em;
    font-weight: 400;
    text-decoration: none;
    color: var(--mfx-accent);
    opacity: 0;
    transition: opacity 0.18s ease;
    cursor: pointer;
    vertical-align: middle;
    -webkit-user-select: none;
    user-select: none;
}
h2:hover > .mfx-anchor,
h3:hover > .mfx-anchor,
.mfx-anchor:focus-visible { opacity: 0.65; }
.mfx-anchor:hover { opacity: 1; }
@media (pointer: coarse) { .mfx-anchor { opacity: 0.4; } }

/* ─── External-link icons ───────────────────────────────────────────────── */
a[data-mfx-ext]:not([data-mfx-noicon])::after {
    content: '';
    display: inline-block;
    width: 0.62em;
    height: 0.62em;
    margin-left: 0.18em;
    vertical-align: 0.02em;
    background-color: currentColor;
    opacity: 0.5;
    -webkit-mask: var(--mfx-ext-icon) center / contain no-repeat;
    mask: var(--mfx-ext-icon) center / contain no-repeat;
    /* default: north-east arrow */
    --mfx-ext-icon: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2024%2024'%3E%3Cpath%20d='M7%204h13v13h-2.6V8.45L5.9%2019.95%204.05%2018.1%2015.55%206.6H7z'/%3E%3C/svg%3E");
}
a[data-mfx-ext]:hover::after { opacity: 0.85; }
/* destination-specific marks */
a[data-mfx-host="github.com"] {
    --mfx-ext-icon: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2024%2024'%3E%3Cpath%20d='M12%20.5C5.37.5%200%205.87%200%2012.5c0%205.3%203.44%209.8%208.21%2011.39.6.11.82-.26.82-.58v-2.2c-3.34.73-4.04-1.42-4.04-1.42-.55-1.39-1.34-1.76-1.34-1.76-1.09-.75.08-.73.08-.73%201.21.09%201.84%201.24%201.84%201.24%201.07%201.84%202.81%201.31%203.5%201%20.11-.78.42-1.31.76-1.61-2.67-.3-5.47-1.34-5.47-5.95%200-1.31.47-2.39%201.24-3.23-.13-.3-.54-1.52.12-3.18%200%200%201.01-.32%203.3%201.23a11.5%2011.5%200%200%201%206%200c2.29-1.55%203.3-1.23%203.3-1.23.66%201.66.25%202.88.12%203.18.77.84%201.24%201.92%201.24%203.23%200%204.62-2.81%205.64-5.49%205.94.43.37.81%201.1.81%202.22v3.29c0%20.32.22.7.83.58A12%2012%200%200%200%2024%2012.5C24%205.87%2018.63.5%2012%20.5z'/%3E%3C/svg%3E");
}
a[data-mfx-host="en.wikipedia.org"], a[data-mfx-host="wikipedia.org"] {
    --mfx-ext-icon: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2024%2024'%3E%3Cpath%20d='M1%204.2h6.3v1.1l-1.5.2c-.3%200-.5.2-.3.7l3.1%207.6h.1l2.3-5.6-.9-2-1.2-.2V4.2H14v1.1l-1.2.2c-.4.1-.4.3-.3.7l3%207.4h.1l2.7-7.2c.2-.6%200-.8-.4-.9l-1-.2V4.2H23v1.1l-1.3.3c-.5.1-.7.4-1%20.9l-4.4%2011.2h-1.3l-3-7.3-3.2%207.3H7.6L3%206.4c-.2-.6-.4-.8-.9-.9L1%205.3z'/%3E%3C/svg%3E");
}
a[data-mfx-host="arxiv.org"], a[data-mfx-host="doi.org"], a[data-mfx-host="science.org"], a[data-mfx-host="nature.com"] {
    --mfx-ext-icon: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2024%2024'%3E%3Cpath%20d='M6%202h8l4%204v16H6z'%20fill='none'%20stroke='black'%20stroke-width='1.8'/%3E%3Cpath%20d='M13%202v5h5M9%2012h6M9%2015h6M9%2018h4'%20fill='none'%20stroke='black'%20stroke-width='1.6'%20stroke-linecap='round'/%3E%3C/svg%3E");
}
/* don't ride on image/icon-only links */
a[data-mfx-noicon]::after { content: none; }

/* ─── Reading progress bar ──────────────────────────────────────────────── */
.mfx-progress {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    z-index: 60;
    background: transparent;
    pointer-events: none;
}
.mfx-progress__fill {
    height: 100%;
    width: 100%;
    transform: scaleX(0);
    transform-origin: 0 50%;
    background: linear-gradient(90deg,
        color-mix(in srgb, var(--mfx-accent) 70%, var(--text)),
        var(--mfx-accent));
    transition: transform 0.08s linear;
}

/* ─── Hover link previews ───────────────────────────────────────────────── */
.mfx-preview {
    position: fixed;
    z-index: 90;
    max-width: 22rem;
    min-width: 12rem;
    padding: 0.7rem 0.85rem;
    background: var(--bg, #f0eee6);
    color: var(--text, #1f1e1d);
    border: 1px solid var(--mfx-rule);
    border-radius: 10px;
    box-shadow: var(--mfx-shadow);
    font-family: var(--font, Georgia, serif);
    font-size: 0.82rem;
    line-height: 1.45;
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.16s ease, transform 0.16s ease;
    pointer-events: none;
}
.mfx-preview.is-in { opacity: 1; transform: none; }
.mfx-preview__host {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-size: 0.68rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: color-mix(in srgb, var(--text) 55%, var(--bg));
    margin-bottom: 0.3rem;
}
.mfx-preview__host svg { width: 0.95em; height: 0.95em; opacity: 0.7; flex: none; }
.mfx-preview__title { font-weight: 600; }
.mfx-preview__desc { margin-top: 0.2rem; opacity: 0.85; }
.mfx-preview__desc,
.mfx-preview__title {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.mfx-preview__title { -webkit-line-clamp: 2; }
.mfx-preview__desc { -webkit-line-clamp: 3; }

/* ─── Sidenotes / marginnotes (Tufte-style) ─────────────────────────────── */
.mfx-sn-ref {
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-size: 0.62em;
    font-weight: 600;
    vertical-align: super;
    line-height: 0;
    color: var(--mfx-accent);
    cursor: pointer;
    padding: 0 0.15em;
    -webkit-user-select: none;
    user-select: none;
}
.mfx-sn-ref::before { content: '['; opacity: 0.5; }
.mfx-sn-ref::after { content: ']'; opacity: 0.5; }
.mfx-sn-toggle { position: absolute; opacity: 0; width: 0; height: 0; }
.mfx-sn {
    font-size: 0.8rem;
    line-height: 1.5;
    color: color-mix(in srgb, var(--text) 78%, var(--bg));
}
.mfx-sn__num {
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-size: 0.85em;
    font-weight: 600;
    color: var(--mfx-accent);
    margin-right: 0.4em;
}

/* Wide: float both inline notes and asides into the right margin.
   Geometry (11rem note, -12rem pull) fits the widest content column we use
   (~800px case studies) once the viewport clears 1200px without clipping. */
@media (min-width: 1200px) {
    .mfx-sn {
        float: right;
        clear: right;
        width: 11rem;
        margin: 0.25rem -12rem 0.9rem 0;
        position: relative;
        text-align: left;
    }
    .mfx-sn-ref { color: var(--mfx-accent); }
}
/* Narrow: inline notes collapse (tap the ref); asides stay visible as a block */
@media (max-width: 1199px) {
    .mfx-sn--inline,
    .mfx-sn--aside {
        margin: 0.7rem 0;
        padding: 0.6rem 0.8rem;
        border-left: 2px solid var(--mfx-accent);
        background: color-mix(in srgb, var(--text) 4%, transparent);
        border-radius: 0 6px 6px 0;
    }
    .mfx-sn--inline { display: none; }
    .mfx-sn-toggle:checked ~ .mfx-sn--inline { display: block; }
    .mfx-sn--aside { display: block; }
    .mfx-sn-ref { padding: 0.1em 0.35em; }
}

/* ─── "Copied" toast ────────────────────────────────────────────────────── */
.mfx-toast {
    position: fixed;
    bottom: max(20px, env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(8px);
    z-index: 100;
    padding: 0.5rem 0.9rem;
    background: var(--text, #1f1e1d);
    color: var(--bg, #f0eee6);
    border-radius: 999px;
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-size: 0.72rem;
    letter-spacing: 0.04em;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}
.mfx-toast.is-in { opacity: 0.95; transform: translateX(-50%); }

@media (prefers-reduced-motion: reduce) {
    .mfx-anchor,
    .mfx-progress__fill,
    .mfx-preview,
    .mfx-toast { transition: none; }
}

/* ─── "More letters" related-entries module (Dispatch issue/interlude pages) ───────────── */
.mfx-more-letters {
    max-width: 620px;
    margin: 0 auto;
    padding: 0 1.5rem;
}
.mfx-more-letters-label {
    font-family: var(--mfx-font-sans);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--mfx-accent);
    text-align: center;
    margin-bottom: 1.5rem;
}
.mfx-more-letters-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.75rem;
}
.mfx-more-letter {
    display: block;
    text-decoration: none;
    color: var(--text, #1f1e1d);
    padding-top: 1.1rem;
    border-top: 1px solid var(--mfx-rule);
    transition: opacity 0.2s ease, border-color 0.3s ease;
}
.mfx-more-letter:hover { opacity: 0.62; }
.mfx-more-letter-num {
    display: block;
    font-family: var(--mfx-font-sans);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.08em;
    color: var(--mfx-accent);
    margin-bottom: 0.4rem;
}
.mfx-more-letter-title {
    display: block;
    font-family: var(--mfx-font-sans);
    font-size: 17px;
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: 0.4rem;
}
.mfx-more-letter-desc {
    display: -webkit-box;
    font-family: var(--font, inherit);
    font-size: 14px;
    line-height: 1.5;
    color: var(--mfx-caption);
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.mfx-more-letter-all {
    display: flex;
    align-items: center;
    min-height: 3.2rem;
}
.mfx-more-letter-all .mfx-more-letter-title { margin-bottom: 0; }
@media (max-width: 640px) {
    .mfx-more-letters-grid { grid-template-columns: 1fr; gap: 1.25rem; }
}
@media (prefers-reduced-motion: reduce) {
    .mfx-more-letter { transition: none; }
}
