{
  "id": 137,
  "slug": "sidenote-marginalia-reader",
  "name": "Sidenote & Marginalia Reader",
  "kind": "ui",
  "family": "Editorial & Reading",
  "sourceSite": {
    "name": "Gwern.net",
    "url": "https://gwern.net/",
    "verifiedStatus": 200
  },
  "tagline": "Tufte-inspired long-form essay reader with aligned right-margin sidenotes on desktop and tap-to-open popovers on mobile.",
  "whyUnique": "Eliminates bottom-of-page footnote jumping by aligning numbered annotations directly beside their anchor sentences on desktop, then transforming them into smooth tap popovers on phones.",
  "theme": "light",
  "mobileStrength": "native-first",
  "tags": [
    "sidenotes",
    "marginalia",
    "editorial",
    "long-form-reading",
    "tufte",
    "popover"
  ],
  "bestFor": [
    "Research essays and academic publications",
    "Annotated legal, policy, and technical specs",
    "Digital books and interactive journalism",
    "Design criticism and living documentation"
  ],
  "palette": [
    {
      "role": "canvas",
      "hex": "#FAF7F0",
      "name": "Archival Ivory"
    },
    {
      "role": "surface",
      "hex": "#FFFFFF",
      "name": "Popover Cream"
    },
    {
      "role": "ink",
      "hex": "#1F1D1A",
      "name": "Bookpress Black"
    },
    {
      "role": "accent",
      "hex": "#9B2C2C",
      "name": "Rubric Crimson"
    },
    {
      "role": "muted",
      "hex": "#6B645B",
      "name": "Margin Umber"
    }
  ],
  "typography": {
    "display": "Newsreader",
    "body": "Newsreader",
    "mono": "JetBrains Mono",
    "googleFontsUrl": "https://fonts.googleapis.com/css2?family=Inter:wght@500;600&family=Newsreader:ital,wght@0,400;0,600;0,700;1,400&display=swap",
    "scaleNotes": "34px editorial serif headline, 18px/1.72 body prose (~64ch measure), 13.5px/1.55 right-margin sidenotes."
  },
  "layoutNotes": "Asymmetric 2-column reading grid (66ch main prose + 260px right marginalia track) on desktop; single-column full-measure prose with inline interactive pills on <=900px.",
  "interactionNotes": "Hovering or clicking an inline superscript highlights its paired margin note on desktop; tapping a numbered pill on mobile opens a clean popover card; reading controls adjust type size and serif/sans.",
  "mobileNotes": "Transforms wide margin columns into zero-overflow inline footnote pills with >=40px tap targets and dismissible annotation cards.",
  "agentGuardrails": {
    "do": [
      "Keep main prose measure between 58ch and 68ch even on wide desktop monitors.",
      "Visually link the inline anchor number and its margin note on hover/focus.",
      "Convert margin sidenotes to tap-to-open popovers or inline disclosures below 900px."
    ],
    "dont": [
      "Never leave right-margin sidenotes overflowing off-screen on 390px mobile.",
      "Avoid microscopic superscript numbers that are impossible to tap with a thumb."
    ]
  },
  "demoFile": "demos/sidenote-marginalia-reader.html",
  "thumbDesktop": "thumbs/sidenote-marginalia-reader-desktop.webp",
  "thumbMobile": "thumbs/sidenote-marginalia-reader-mobile.webp",
  "urls": {
    "page": "https://templates.michaelpitts.net/t/sidenote-marginalia-reader",
    "markdown": "https://templates.michaelpitts.net/t/sidenote-marginalia-reader.md",
    "api": "https://templates.michaelpitts.net/api/templates/sidenote-marginalia-reader",
    "demo": "https://templates.michaelpitts.net/demos/sidenote-marginalia-reader.html",
    "thumbDesktop": "https://templates.michaelpitts.net/thumbs/sidenote-marginalia-reader-desktop.webp",
    "thumbMobile": "https://templates.michaelpitts.net/thumbs/sidenote-marginalia-reader-mobile.webp"
  },
  "agentPrompt": "Design & build using template #137 \"Sidenote & Marginalia Reader\" (ui / Editorial & Reading, light theme).\nFull AI blueprint & standalone starter code: https://templates.michaelpitts.net/t/sidenote-marginalia-reader.md\nLive demo: https://templates.michaelpitts.net/demos/sidenote-marginalia-reader.html\nReal-world inspiration: Gwern.net (https://gwern.net/)\n\nCore aesthetic: Eliminates bottom-of-page footnote jumping by aligning numbered annotations directly beside their anchor sentences on desktop, then transforming them into smooth tap popovers on phones.\nPalette: canvas: #FAF7F0 (Archival Ivory), surface: #FFFFFF (Popover Cream), ink: #1F1D1A (Bookpress Black), accent: #9B2C2C (Rubric Crimson), muted: #6B645B (Margin Umber)\nTypography: Display=\"Newsreader\", Body=\"Newsreader\", Mono=\"JetBrains Mono\"\nLayout: Asymmetric 2-column reading grid (66ch main prose + 260px right marginalia track) on desktop; single-column full-measure prose with inline interactive pills on <=900px.\nInteractions: Hovering or clicking an inline superscript highlights its paired margin note on desktop; tapping a numbered pill on mobile opens a clean popover card; reading controls adjust type size and serif/sans.\nMobile: Transforms wide margin columns into zero-overflow inline footnote pills with >=40px tap targets and dismissible annotation cards.\n\nGuardrails:\n- DO: Keep main prose measure between 58ch and 68ch even on wide desktop monitors.\n- DO: Visually link the inline anchor number and its margin note on hover/focus.\n- DO: Convert margin sidenotes to tap-to-open popovers or inline disclosures below 900px.\n- DON'T: Never leave right-margin sidenotes overflowing off-screen on 390px mobile.\n- DON'T: Avoid microscopic superscript numbers that are impossible to tap with a thumb.",
  "sourceCode": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <title>Marginalia — Sidenote Essay Reader</title>\n  <link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">\n  <link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>\n  <link href=\"https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,400;0,500;0,600;1,400;1,500&family=Inter:wght@400;500;600&display=swap\" rel=\"stylesheet\">\n  <style>\n    :root {\n      --paper: #fbf8f1;\n      --paper-card: #f4efe3;\n      --ink: #1e1c18;\n      --ink-muted: #5a5347;\n      --ink-faint: #867e70;\n      --rule: #e4dcc9;\n      --accent: #9b2c2c;\n      --accent-soft: rgba(155, 44, 44, 0.1);\n      --font-body: 'EB Garamond', Georgia, 'Times New Roman', serif;\n      --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;\n      --prose-size: 20px;\n    }\n\n    body[data-theme=\"sepia\"] {\n      --paper: #f3ead8;\n      --paper-card: #e9dec8;\n      --ink: #292218;\n      --ink-muted: #5e513e;\n      --rule: #d8cab0;\n      --accent: #8b3a1a;\n    }\n\n    body[data-theme=\"night\"] {\n      --paper: #171614;\n      --paper-card: #22201c;\n      --ink: #e8e2d5;\n      --ink-muted: #a69e8f;\n      --ink-faint: #787165;\n      --rule: #322f29;\n      --accent: #e07a5f;\n      --accent-soft: rgba(224, 122, 95, 0.16);\n    }\n\n    body[data-font=\"sans\"] {\n      --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;\n    }\n\n    * { box-sizing: border-box; margin: 0; padding: 0; }\n\n    html, body {\n      min-height: 100%;\n      background: var(--paper);\n      color: var(--ink);\n      font-family: var(--font-body);\n      -webkit-font-smoothing: antialiased;\n      transition: background 0.2s, color 0.2s;\n      overflow-x: hidden;\n    }\n\n    /* Reading Progress Line */\n    .progress-track {\n      position: fixed;\n      top: 0;\n      left: 0;\n      right: 0;\n      height: 3px;\n      background: transparent;\n      z-index: 60;\n    }\n\n    .progress-fill {\n      height: 100%;\n      width: 0%;\n      background: var(--accent);\n      transition: width 0.08s linear;\n    }\n\n    /* Top Reading Controls */\n    .reader-bar {\n      position: sticky;\n      top: 0;\n      z-index: 40;\n      background: color-mix(in srgb, var(--paper) 92%, transparent);\n      backdrop-filter: blur(8px);\n      border-bottom: 1px solid var(--rule);\n    }\n\n    .reader-bar-inner {\n      max-width: 1080px;\n      margin: 0 auto;\n      padding: 10px 24px;\n      display: flex;\n      align-items: center;\n      justify-content: space-between;\n      gap: 12px;\n    }\n\n    .journal-mark {\n      font-family: var(--font-ui);\n      font-size: 12px;\n      font-weight: 600;\n      letter-spacing: 0.09em;\n      text-transform: uppercase;\n      color: var(--ink-muted);\n      white-space: nowrap;\n    }\n\n    .controls {\n      display: flex;\n      align-items: center;\n      gap: 6px;\n      flex-wrap: wrap;\n    }\n\n    .ctrl-btn {\n      appearance: none;\n      border: 1px solid var(--rule);\n      background: var(--paper-card);\n      color: var(--ink);\n      font-family: var(--font-ui);\n      font-size: 12.5px;\n      font-weight: 500;\n      min-height: 36px;\n      min-width: 36px;\n      padding: 0 10px;\n      border-radius: 7px;\n      cursor: pointer;\n      transition: border-color 0.15s, background 0.15s;\n    }\n\n    .ctrl-btn:hover {\n      border-color: var(--ink-faint);\n    }\n\n    .ctrl-btn.active {\n      border-color: var(--accent);\n      color: var(--accent);\n      background: var(--accent-soft);\n    }\n\n    /* Tufte Layout Container */\n    .essay-shell {\n      max-width: 1080px;\n      margin: 0 auto;\n      padding: 52px 24px 110px;\n      position: relative;\n    }\n\n    .essay-header {\n      max-width: 640px;\n      margin-bottom: 36px;\n      padding-bottom: 24px;\n      border-bottom: 1px solid var(--rule);\n    }\n\n    .kicker {\n      font-family: var(--font-ui);\n      font-size: 12px;\n      font-weight: 600;\n      letter-spacing: 0.1em;\n      text-transform: uppercase;\n      color: var(--accent);\n      margin-bottom: 12px;\n    }\n\n    .essay-title {\n      font-size: clamp(30px, 4.2vw, 44px);\n      line-height: 1.12;\n      font-weight: 500;\n      letter-spacing: -0.015em;\n      margin-bottom: 14px;\n    }\n\n    .essay-subtitle {\n      font-size: 20px;\n      line-height: 1.45;\n      color: var(--ink-muted);\n      font-style: italic;\n      margin-bottom: 16px;\n    }\n\n    .byline {\n      font-family: var(--font-ui);\n      font-size: 13px;\n      color: var(--ink-faint);\n      display: flex;\n      gap: 14px;\n      flex-wrap: wrap;\n    }\n\n    /* Essay Body with Right-Margin Sidenotes */\n    .essay-body {\n      position: relative;\n      max-width: 640px;\n      font-size: var(--prose-size);\n      line-height: 1.68;\n    }\n\n    body[data-font=\"sans\"] .essay-body {\n      font-size: calc(var(--prose-size) - 2px);\n      line-height: 1.72;\n    }\n\n    .essay-body p {\n      margin-bottom: 1.35em;\n      position: relative;\n    }\n\n    .essay-body h2 {\n      font-size: 1.45em;\n      font-weight: 500;\n      margin: 1.8em 0 0.65em;\n      letter-spacing: -0.01em;\n    }\n\n    .dropcap::first-letter {\n      float: left;\n      font-size: 3.35em;\n      line-height: 0.84;\n      padding-right: 10px;\n      padding-top: 4px;\n      color: var(--accent);\n      font-weight: 500;\n    }\n\n    .pullquote {\n      margin: 1.6em 0;\n      padding: 6px 0 6px 22px;\n      border-left: 2px solid var(--accent);\n      font-style: italic;\n      color: var(--ink-muted);\n      font-size: 1.08em;\n    }\n\n    /* Sidenote Reference Button */\n    .sn-ref {\n      appearance: none;\n      border: 1px solid transparent;\n      background: var(--accent-soft);\n      color: var(--accent);\n      font-family: var(--font-ui);\n      font-size: 0.62em;\n      font-weight: 700;\n      vertical-align: super;\n      line-height: 1;\n      padding: 3px 6px;\n      border-radius: 99px;\n      cursor: pointer;\n      margin: 0 2px;\n      transition: background 0.15s, color 0.15s, transform 0.1s;\n    }\n\n    .sn-ref:hover,\n    .sn-ref.active {\n      background: var(--accent);\n      color: #fff;\n    }\n\n    /* Desktop Margin Sidenote */\n    .sidenote {\n      float: right;\n      clear: right;\n      margin-right: -340px;\n      width: 288px;\n      margin-top: 4px;\n      margin-bottom: 18px;\n      padding: 10px 14px;\n      border-left: 2px solid var(--rule);\n      font-size: 15px;\n      line-height: 1.5;\n      color: var(--ink-muted);\n      border-radius: 0 8px 8px 0;\n      transition: border-color 0.2s, background 0.2s, color 0.2s;\n      cursor: pointer;\n    }\n\n    .sidenote:hover,\n    .sidenote.highlighted {\n      border-left-color: var(--accent);\n      background: var(--paper-card);\n      color: var(--ink);\n    }\n\n    .sn-num {\n      font-family: var(--font-ui);\n      font-size: 11px;\n      font-weight: 700;\n      color: var(--accent);\n      margin-right: 6px;\n    }\n\n    .sn-figure {\n      margin-top: 8px;\n      padding: 8px;\n      background: var(--paper);\n      border: 1px solid var(--rule);\n      border-radius: 6px;\n    }\n\n    .sn-caption {\n      font-family: var(--font-ui);\n      font-size: 11px;\n      color: var(--ink-faint);\n      margin-top: 5px;\n    }\n\n    /* Mobile Popover Sheet */\n    .popover-backdrop {\n      position: fixed;\n      inset: 0;\n      background: rgba(15, 14, 12, 0.42);\n      opacity: 0;\n      pointer-events: none;\n      transition: opacity 0.2s;\n      z-index: 70;\n    }\n\n    .popover-backdrop.open {\n      opacity: 1;\n      pointer-events: auto;\n    }\n\n    .mobile-popover {\n      position: fixed;\n      left: 12px;\n      right: 12px;\n      bottom: 14px;\n      max-width: 480px;\n      margin: 0 auto;\n      background: var(--paper);\n      border: 1px solid var(--rule);\n      border-top: 3px solid var(--accent);\n      border-radius: 14px;\n      padding: 16px 18px 18px;\n      box-shadow: 0 18px 44px rgba(0, 0, 0, 0.28);\n      transform: translateY(120%);\n      transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);\n      z-index: 80;\n    }\n\n    .mobile-popover.open {\n      transform: translateY(0);\n    }\n\n    .pop-head {\n      display: flex;\n      align-items: center;\n      justify-content: space-between;\n      margin-bottom: 10px;\n    }\n\n    .pop-title {\n      font-family: var(--font-ui);\n      font-size: 12px;\n      font-weight: 700;\n      text-transform: uppercase;\n      letter-spacing: 0.06em;\n      color: var(--accent);\n    }\n\n    .pop-nav {\n      display: flex;\n      align-items: center;\n      gap: 6px;\n    }\n\n    .pop-btn {\n      appearance: none;\n      border: 1px solid var(--rule);\n      background: var(--paper-card);\n      color: var(--ink);\n      font-family: var(--font-ui);\n      font-size: 13px;\n      min-width: 44px;\n      min-height: 44px;\n      border-radius: 8px;\n      cursor: pointer;\n    }\n\n    .pop-body {\n      font-size: 17px;\n      line-height: 1.55;\n      color: var(--ink);\n    }\n\n    /* Responsive Transformation at <= 1020px (includes 390px mobile) */\n    @media (max-width: 1020px) {\n      .essay-shell {\n        padding: 32px 18px 80px;\n      }\n\n      .essay-header, .essay-body {\n        max-width: 65ch;\n        margin-left: auto;\n        margin-right: auto;\n      }\n\n      .sidenote {\n        display: none;\n      }\n\n      .sn-ref {\n        vertical-align: baseline;\n        font-size: 0.72em;\n        padding: 4px 9px;\n        min-height: 28px;\n        display: inline-flex;\n        align-items: center;\n        justify-content: center;\n      }\n\n      .reader-bar-inner {\n        padding: 8px 14px;\n      }\n\n      .ctrl-btn {\n        min-height: 44px;\n        min-width: 44px;\n      }\n    }\n  </style>\n</head>\n<body data-theme=\"paper\" data-font=\"serif\">\n  <div class=\"progress-track\"><div id=\"progressFill\" class=\"progress-fill\"></div></div>\n\n  <header class=\"reader-bar\">\n    <div class=\"reader-bar-inner\">\n      <div class=\"journal-mark\">Marginalia · No. 14</div>\n      <div class=\"controls\" role=\"toolbar\" aria-label=\"Reading controls\">\n        <button id=\"fontDown\" class=\"ctrl-btn\" type=\"button\" aria-label=\"Decrease font size\">A−</button>\n        <button id=\"fontUp\" class=\"ctrl-btn\" type=\"button\" aria-label=\"Increase font size\">A+</button>\n        <button id=\"fontToggle\" class=\"ctrl-btn\" type=\"button\">Serif</button>\n        <button class=\"ctrl-btn active\" type=\"button\" data-theme-btn=\"paper\">Ivory</button>\n        <button class=\"ctrl-btn\" type=\"button\" data-theme-btn=\"sepia\">Sepia</button>\n        <button class=\"ctrl-btn\" type=\"button\" data-theme-btn=\"night\">Night</button>\n      </div>\n    </div>\n  </header>\n\n  <article class=\"essay-shell\">\n    <header class=\"essay-header\">\n      <div class=\"kicker\">Typography &amp; Information Design</div>\n      <h1 class=\"essay-title\">The Geometry of Margin Notes</h1>\n      <p class=\"essay-subtitle\">Why spatial proximity in the right margin preserves reading flow better than endnotes—and how to adapt the pattern for narrow screens.</p>\n      <div class=\"byline\">\n        <span>By Clara Lindholm</span>\n        <span>·</span>\n        <span>6 min read</span>\n        <span>·</span>\n        <span>5 sidenotes</span>\n      </div>\n    </header>\n\n    <div class=\"essay-body\" id=\"essayBody\">\n      <p class=\"dropcap\">\n        A footnote asks the reader to perform a small act of faith: abandon the current sentence, descend to the bottom of the page, locate a matching numeral, and climb back without losing the clause that prompted the detour.<button class=\"sn-ref\" type=\"button\" data-note=\"1\" aria-label=\"Sidenote 1\">1</button>\n        <aside class=\"sidenote\" data-note=\"1\">\n          <span class=\"sn-num\">1</span>Robert Bringhurst notes in <em>The Elements of Typographic Style</em> that marginalia were standard in Renaissance codices long before printers moved notes to the foot of the sheet to save paper.\n        </aside>\n        On the web, where pages have no physical bottom, endnotes become even more disruptive—turning a glance into a round-trip scroll across thousands of pixels.\n      </p>\n\n      <p>\n        Edward Tufte popularized the revival of the wide right margin precisely because human peripheral vision resolves horizontal adjacency effortlessly.<button class=\"sn-ref\" type=\"button\" data-note=\"2\" aria-label=\"Sidenote 2\">2</button>\n        <aside class=\"sidenote\" data-note=\"2\">\n          <span class=\"sn-num\">2</span>Eye-tracking studies on academic layouts show readers return to the primary line 2.4× faster from an aligned margin note than from a bottom-of-page footnote.\n          <div class=\"sn-figure\">\n            <svg viewBox=\"0 0 220 56\" width=\"100%\" height=\"56\" aria-label=\"Return latency comparison chart\">\n              <text x=\"2\" y=\"16\" font-family=\"Inter, sans-serif\" font-size=\"10\" fill=\"currentColor\">Margin</text>\n              <rect x=\"52\" y=\"7\" width=\"54\" height=\"12\" rx=\"3\" fill=\"#9b2c2c\"/>\n              <text x=\"112\" y=\"16\" font-family=\"Inter, sans-serif\" font-size=\"10\" fill=\"currentColor\">0.9s</text>\n              <text x=\"2\" y=\"42\" font-family=\"Inter, sans-serif\" font-size=\"10\" fill=\"currentColor\">Endnote</text>\n              <rect x=\"52\" y=\"33\" width=\"132\" height=\"12\" rx=\"3\" fill=\"#867e70\" opacity=\"0.55\"/>\n              <text x=\"190\" y=\"42\" font-family=\"Inter, sans-serif\" font-size=\"10\" fill=\"currentColor\">2.2s</text>\n            </svg>\n            <div class=\"sn-caption\">Median gaze-return latency (s) across 48 readers.</div>\n          </div>\n        </aside>\n        When commentary sits level with the sentence it qualifies, the reader treats it as an optional second voice rather than an interruption.\n      </p>\n\n      <h2>Measure, Whitespace, and the 65-Character Rule</h2>\n\n      <p>\n        Wide desktop monitors routinely tempt layouts into stretching prose across ninety or more characters per line. Yet classic book typography caps the optimal measure near sixty-five characters.<button class=\"sn-ref\" type=\"button\" data-note=\"3\" aria-label=\"Sidenote 3\">3</button>\n        <aside class=\"sidenote\" data-note=\"3\">\n          <span class=\"sn-num\">3</span>A 640px text column at 20px Garamond averages 64–68 characters per line—leaving roughly 320px on a 1280px viewport for crisp marginalia without crowding.\n        </aside>\n        Rather than leaving that surplus horizontal space empty, a sidenote grid converts dead margin into an active layer for citations, caveats, and small data graphics.\n      </p>\n\n      <blockquote class=\"pullquote\">\n        “The margin is not leftover paper; it is a parallel channel for evidence that would otherwise clog the main argument.”\n      </blockquote>\n\n      <p>\n        Long-form digital essays—from Gwern Branwen’s research notebooks to interactive technical monographs—rely on this dual channel to keep narrative momentum brisk while remaining rigorously sourced.<button class=\"sn-ref\" type=\"button\" data-note=\"4\" aria-label=\"Sidenote 4\">4</button>\n        <aside class=\"sidenote\" data-note=\"4\">\n          <span class=\"sn-num\">4</span>Notice how hovering or clicking either the superscript badge or the margin block itself links the pair visually, confirming correspondence at a glance.\n        </aside>\n      </p>\n\n      <h2>Adapting Marginalia for Pocket Screens</h2>\n\n      <p>\n        On a 390-pixel mobile viewport, there is no right margin to borrow. Forcing two columns onto a phone compresses prose into jagged three-word ribbons. Instead, the margin note must transform into an on-demand popover card triggered by tapping the inline reference pill.<button class=\"sn-ref\" type=\"button\" data-note=\"5\" aria-label=\"Sidenote 5\">5</button>\n        <aside class=\"sidenote\" data-note=\"5\">\n          <span class=\"sn-num\">5</span>Resize the window below 1020px (or view on mobile) and tap any numbered badge to inspect this bottom-anchored popover card with Prev/Next note stepping.\n        </aside>\n        The semantic HTML stays identical—one source of truth—while the presentation honors the physical geometry of whichever glass the reader holds.\n      </p>\n    </div>\n  </article>\n\n  <div id=\"popoverBackdrop\" class=\"popover-backdrop\" aria-hidden=\"true\"></div>\n  <div id=\"mobilePopover\" class=\"mobile-popover\" role=\"dialog\" aria-modal=\"true\" aria-label=\"Sidenote detail\">\n    <div class=\"pop-head\">\n      <span id=\"popTitle\" class=\"pop-title\">Sidenote 1 of 5</span>\n      <div class=\"pop-nav\">\n        <button id=\"popPrev\" class=\"pop-btn\" type=\"button\" aria-label=\"Previous note\">‹</button>\n        <button id=\"popNext\" class=\"pop-btn\" type=\"button\" aria-label=\"Next note\">›</button>\n        <button id=\"popClose\" class=\"pop-btn\" type=\"button\" aria-label=\"Close note\">✕</button>\n      </div>\n    </div>\n    <div id=\"popBody\" class=\"pop-body\"></div>\n  </div>\n\n  <script>\n    const refs = Array.from(document.querySelectorAll('.sn-ref'));\n    const notes = Array.from(document.querySelectorAll('.sidenote'));\n    const backdrop = document.getElementById('popoverBackdrop');\n    const popover = document.getElementById('mobilePopover');\n    const popTitle = document.getElementById('popTitle');\n    const popBody = document.getElementById('popBody');\n\n    let activeNoteId = null;\n    let proseSize = 20;\n\n    function isMobileLayout() {\n      return window.innerWidth <= 1020;\n    }\n\n    function clearHighlights() {\n      refs.forEach(r => r.classList.remove('active'));\n      notes.forEach(n => n.classList.remove('highlighted'));\n    }\n\n    function highlightPair(id) {\n      clearHighlights();\n      const ref = document.querySelector(`.sn-ref[data-note=\"${id}\"]`);\n      const note = document.querySelector(`.sidenote[data-note=\"${id}\"]`);\n      if (ref) ref.classList.add('active');\n      if (note) note.classList.add('highlighted');\n    }\n\n    function openPopover(id) {\n      activeNoteId = Number(id);\n      const note = document.querySelector(`.sidenote[data-note=\"${id}\"]`);\n      if (!note) return;\n      highlightPair(id);\n      popTitle.textContent = `Sidenote ${id} of ${notes.length}`;\n      // Clone note content without duplicate number badge\n      const clone = note.cloneNode(true);\n      const numEl = clone.querySelector('.sn-num');\n      if (numEl) numEl.remove();\n      popBody.innerHTML = clone.innerHTML;\n      backdrop.classList.add('open');\n      popover.classList.add('open');\n    }\n\n    function closePopover() {\n      backdrop.classList.remove('open');\n      popover.classList.remove('open');\n      clearHighlights();\n      activeNoteId = null;\n    }\n\n    refs.forEach(ref => {\n      const id = ref.dataset.note;\n      ref.addEventListener('mouseenter', () => {\n        if (!isMobileLayout()) highlightPair(id);\n      });\n      ref.addEventListener('mouseleave', () => {\n        if (!isMobileLayout() && activeNoteId === null) clearHighlights();\n      });\n      ref.addEventListener('click', () => {\n        if (isMobileLayout()) {\n          openPopover(id);\n        } else {\n          if (activeNoteId === Number(id)) {\n            activeNoteId = null;\n            clearHighlights();\n          } else {\n            activeNoteId = Number(id);\n            highlightPair(id);\n          }\n        }\n      });\n    });\n\n    notes.forEach(note => {\n      const id = note.dataset.note;\n      note.addEventListener('mouseenter', () => highlightPair(id));\n      note.addEventListener('mouseleave', () => {\n        if (activeNoteId === null) clearHighlights();\n      });\n      note.addEventListener('click', () => {\n        activeNoteId = Number(id);\n        highlightPair(id);\n      });\n    });\n\n    document.getElementById('popClose').addEventListener('click', closePopover);\n    backdrop.addEventListener('click', closePopover);\n    document.getElementById('popPrev').addEventListener('click', () => {\n      if (!activeNoteId) return;\n      const prev = activeNoteId === 1 ? notes.length : activeNoteId - 1;\n      openPopover(prev);\n    });\n    document.getElementById('popNext').addEventListener('click', () => {\n      if (!activeNoteId) return;\n      const next = activeNoteId === notes.length ? 1 : activeNoteId + 1;\n      openPopover(next);\n    });\n\n    document.addEventListener('keydown', (e) => {\n      if (e.key === 'Escape') closePopover();\n    });\n\n    // Reading controls\n    document.getElementById('fontDown').addEventListener('click', () => {\n      proseSize = Math.max(16, proseSize - 2);\n      document.documentElement.style.setProperty('--prose-size', `${proseSize}px`);\n    });\n\n    document.getElementById('fontUp').addEventListener('click', () => {\n      proseSize = Math.min(26, proseSize + 2);\n      document.documentElement.style.setProperty('--prose-size', `${proseSize}px`);\n    });\n\n    const fontToggle = document.getElementById('fontToggle');\n    fontToggle.addEventListener('click', () => {\n      const next = document.body.dataset.font === 'serif' ? 'sans' : 'serif';\n      document.body.dataset.font = next;\n      fontToggle.textContent = next === 'serif' ? 'Serif' : 'Sans';\n    });\n\n    document.querySelectorAll('[data-theme-btn]').forEach(btn => {\n      btn.addEventListener('click', () => {\n        document.body.dataset.theme = btn.dataset.themeBtn;\n        document.querySelectorAll('[data-theme-btn]').forEach(b => b.classList.remove('active'));\n        btn.classList.add('active');\n      });\n    });\n\n    // Scroll progress\n    function updateProgress() {\n      const scrollTop = window.scrollY;\n      const maxScroll = Math.max(1, document.documentElement.scrollHeight - window.innerHeight);\n      const pct = Math.min(100, Math.round((scrollTop / maxScroll) * 100));\n      document.getElementById('progressFill').style.width = `${pct}%`;\n    }\n    window.addEventListener('scroll', updateProgress, { passive: true });\n    updateProgress();\n  </script>\n</body>\n</html>\n"
}
