Manual
Almnck.
Price: 50ⓐ

Mobile Margins Implementation Plan

Status: Implemented 2026-01-24

For Claude: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.

Goal: Transform mobile reading from inline footnotes to a swipeable three-column layout with synchronized scrolling.

Architecture: On touch devices (pointer: coarse), the .margins-layout CSS Grid changes from side-by-side columns to 90vw-wide horizontally-scrollable panels with center snap alignment (creating 5vw peek gutters). JavaScript handles footnote/manicule tap navigation and initial positioning.

Tech Stack: CSS scroll-snap, vanilla JavaScript, Playwright for testing

Design Document: doc/process/plans/2026-01-24-mobile-margins-design.md


Task 1: Set Up Playwright Testing Infrastructure

Files: - Create: package.json - Create: playwright.config.js - Create: tests/mobile-margins.spec.js (skeleton) - Modify: Makefile - Modify: .gitignore

Step 1: Create package.json for Playwright

{
  "name": "almanack-tests",
  "private": true,
  "scripts": {
    "test:mobile": "playwright test tests/mobile-margins.spec.js",
    "test:mobile:headed": "playwright test tests/mobile-margins.spec.js --headed"
  },
  "devDependencies": {
    "@playwright/test": "^1.40.0"
  }
}

Step 2: Create playwright.config.js

const { defineConfig, devices } = require('@playwright/test');

module.exports = defineConfig({
  testDir: './tests',
  timeout: 30000,
  use: {
    baseURL: 'http://localhost:1969',
    trace: 'on-first-retry',
  },
  projects: [
    {
      name: 'Mobile Safari',
      use: { ...devices['iPhone 13'] },
    },
    {
      name: 'Mobile Chrome',
      use: { ...devices['Pixel 5'] },
    },
  ],
});

Step 3: Create skeleton test file

Create tests/mobile-margins.spec.js:

const { test, expect } = require('@playwright/test');

test.describe('Mobile Margins', () => {
  test('placeholder - layout loads', async ({ page }) => {
    // TODO: Replace with real test once CSS is implemented
    await page.goto('/');
    await expect(page).toHaveTitle(/Almanack/);
  });
});

Step 4: Add to Makefile

Add after the existing test targets (around line 160):

.PHONY: test-mobile
test-mobile:
    @echo "$(YELLOW)Running mobile margin tests...$(RESET)"
    npx playwright test tests/mobile-margins.spec.js

.PHONY: test-mobile-headed
test-mobile-headed:
    @echo "$(YELLOW)Running mobile margin tests (headed)...$(RESET)"
    npx playwright test tests/mobile-margins.spec.js --headed

Step 5: Add node_modules to .gitignore

Append to .gitignore:

# Node.js (Playwright tests)
node_modules/
playwright-report/
test-results/

Step 6: Install dependencies and verify

Run:

npm install
npx playwright install

Step 7: Run skeleton test

Run: make serve (in one terminal) Run: npm run test:mobile (in another terminal) Expected: 1 test passing (placeholder)

Step 8: Commit

git add package.json playwright.config.js tests/mobile-margins.spec.js Makefile .gitignore
git commit -m "chore: add Playwright infrastructure for mobile testing"

Task 2: CSS - Mobile Swipe Layout (No-JS Fallback)

Files: - Modify: assets/css/almanack.css (lines 194-205, add after)

Step 1: Write the failing Playwright test

Update tests/mobile-margins.spec.js:

const { test, expect } = require('@playwright/test');

test.describe('Mobile Margins - No JS Fallback', () => {
  test('without JS, margins are hidden and footnotes visible', async ({ page }) => {
    await page.setJavaScriptEnabled(false);
    // Navigate to an article with footnotes
    await page.goto('/6024/1/23/every-heart-a-world/');

    // Margins should be hidden
    const leftMargin = page.locator('.margin-left');
    const rightMargin = page.locator('.margin-right');
    await expect(leftMargin).toBeHidden();
    await expect(rightMargin).toBeHidden();

    // Footnotes section should be visible at bottom
    const footnotes = page.locator('.footnotes');
    await expect(footnotes).toBeVisible();
  });
});

Step 2: Run test to verify it fails

Run: npm run test:mobile Expected: FAIL (margins may currently show on mobile without JS)

Step 3: Add CSS for no-JS fallback on touch devices

In assets/css/almanack.css, replace the existing @media (max-width: 800px) block (lines 196-205) with:

/* Touch devices: no-JS fallback - single column with inline footnotes */
@media (pointer: coarse) {
    .margins-layout {
        display: block;
        max-width: 43em;
        padding: 0;
    }
    .margin-left,
    .margin-right {
        display: none;
    }
    .footnotes {
        display: block;
    }
}

/* Keep narrow-screen fallback for non-touch devices */
@media (pointer: fine) and (max-width: 800px) {
    .margins-layout {
        display: block;
        max-width: 43em;
        padding: 0;
    }
    .margin {
        display: none;
    }
}

Step 4: Run test to verify it passes

Run: npm run test:mobile Expected: PASS

Step 5: Commit

git add assets/css/almanack.css tests/mobile-margins.spec.js
git commit -m "feat(mobile): add no-JS fallback for touch devices"

Task 3: CSS - Swipe Layout When JS Enabled

Files: - Modify: assets/css/almanack.css

Step 1: Write the failing test

Add to tests/mobile-margins.spec.js:

test.describe('Mobile Margins - Swipe Layout', () => {
  test('with JS, swipe layout is active', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');

    // Wait for JS to initialize
    await page.waitForSelector('.swipe-margins-enabled');

    // Check that margins-layout has horizontal scroll
    const layout = page.locator('.margins-layout');
    const overflowX = await layout.evaluate(el =>
      window.getComputedStyle(el).overflowX
    );
    expect(overflowX).toBe('auto');

    // Check grid has 3 columns of 100vw each
    const gridCols = await layout.evaluate(el =>
      window.getComputedStyle(el).gridTemplateColumns
    );
    // Should be three equal values (100vw each, computed to pixels)
    const cols = gridCols.split(' ');
    expect(cols.length).toBe(3);
  });

  test('margins are visible in swipe mode', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');

    const leftMargin = page.locator('.margin-left');
    const rightMargin = page.locator('.margin-right');

    // Margins exist and have content
    await expect(leftMargin).toBeAttached();
    await expect(rightMargin).toBeAttached();
  });
});

Step 2: Run test to verify it fails

Run: npm run test:mobile Expected: FAIL (no .swipe-margins-enabled class yet)

Step 3: Add CSS for JS-enabled swipe layout

Add to assets/css/almanack.css after the no-JS fallback rules:

/* Touch devices: JS-enabled swipe layout */
@media (pointer: coarse) {
    .swipe-margins-enabled .margins-layout {
        display: grid;
        grid-template-columns: 100vw 100vw 100vw;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x mandatory;
        scroll-behavior: smooth;
        scroll-padding-inline: 5vw;

        /* Hide scrollbar */
        scrollbar-width: none;
        -ms-overflow-style: none;

        /* Reset constraints */
        max-width: none;
        width: 100%;
        padding: 0;
        gap: 0;
    }

    .swipe-margins-enabled .margins-layout::-webkit-scrollbar {
        display: none;
    }

    .swipe-margins-enabled .margin-left,
    .swipe-margins-enabled .body-text,
    .swipe-margins-enabled .margin-right {
        scroll-snap-align: start;
        scroll-snap-stop: always;
        min-height: 100vh;
        box-sizing: border-box;
        padding: 1rem;
    }

    .swipe-margins-enabled .margin-left,
    .swipe-margins-enabled .margin-right {
        display: block;
    }

    .swipe-margins-enabled .footnotes {
        display: none;
    }
}

Step 4: Tests still fail - need JS to add the class

Run: npm run test:mobile Expected: FAIL (need Task 4 to add JS)

Step 5: Commit CSS only

git add assets/css/almanack.css tests/mobile-margins.spec.js
git commit -m "feat(mobile): add swipe layout CSS for touch devices"

Task 4: JavaScript - Touch Detection and Class Toggle

Files: - Modify: assets/js/sidenotes.js

Step 1: Tests from Task 3 should now be our target

The tests written in Task 3 expect .swipe-margins-enabled class. No new test needed.

Step 2: Add touch detection to sidenotes.js

At the top of the IIFE in assets/js/sidenotes.js, after the CONFIG object (around line 21), add:

  /**
   * Check if device has touch/coarse pointer
   */
  function isTouchDevice() {
    return window.matchMedia('(pointer: coarse)').matches;
  }

Step 3: Add swipe mode initialization

In the init() function (around line 819), after isEnabled = true; add:

    // Enable swipe mode on touch devices
    if (isTouchDevice()) {
      document.body.classList.add('swipe-margins-enabled');
      initSwipeMode();
    }

Step 4: Add initSwipeMode function

Add before the init() function:

  /**
   * Initialize swipe mode for touch devices
   * Scrolls to center column on load
   */
  function initSwipeMode() {
    const layout = document.querySelector('.margins-layout');
    if (!layout) return;

    // Scroll to center column (main content) - instant, no animation
    const centerColumn = layout.querySelector('.body-text');
    if (centerColumn) {
      // Use scrollLeft for instant positioning before paint
      layout.scrollLeft = centerColumn.offsetLeft;
    }
  }

Step 5: Run tests

Run: npm run test:mobile Expected: PASS (both swipe layout tests)

Step 6: Commit

git add assets/js/sidenotes.js
git commit -m "feat(mobile): add touch detection and swipe mode initialization"

Task 5: Test - Initial Scroll Position

Files: - Modify: tests/mobile-margins.spec.js - Modify: assets/js/sidenotes.js (if needed)

Step 1: Write the test

Add to tests/mobile-margins.spec.js:

test.describe('Mobile Margins - Initial State', () => {
  test('starts centered on main column', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');

    // Small delay for scroll positioning
    await page.waitForTimeout(100);

    const layout = page.locator('.margins-layout');
    const scrollLeft = await layout.evaluate(el => el.scrollLeft);
    const viewportWidth = await page.evaluate(() => window.innerWidth);

    // Should be scrolled to ~100vw (the main column)
    // Allow some tolerance for scroll-padding
    expect(scrollLeft).toBeGreaterThan(viewportWidth * 0.8);
    expect(scrollLeft).toBeLessThan(viewportWidth * 1.2);
  });
});

Step 2: Run test

Run: npm run test:mobile Expected: Should PASS if Task 4 was done correctly

Step 3: Commit test

git add tests/mobile-margins.spec.js
git commit -m "test(mobile): verify initial scroll centers on main column"

Task 6: JavaScript - Footnote Tap Navigation

Files: - Modify: assets/js/sidenotes.js - Modify: tests/mobile-margins.spec.js

Step 1: Write the failing test

Add to tests/mobile-margins.spec.js:

test.describe('Mobile Margins - Footnote Navigation', () => {
  test('tapping footnote navigates to sidenote', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');
    await page.waitForTimeout(200);

    // Find first footnote reference
    const footnoteRef = page.locator('.footnote-ref').first();
    await footnoteRef.tap();

    // Wait for scroll animation
    await page.waitForTimeout(600);

    // Should have scrolled horizontally away from center
    const layout = page.locator('.margins-layout');
    const scrollLeft = await layout.evaluate(el => el.scrollLeft);
    const viewportWidth = await page.evaluate(() => window.innerWidth);

    // Should be at left margin (0) or right margin (2*vw), not center
    const atCenter = scrollLeft > viewportWidth * 0.8 && scrollLeft < viewportWidth * 1.2;
    expect(atCenter).toBe(false);

    // Sidenote should be highlighted
    const highlighted = page.locator('.sidenote.highlighted');
    await expect(highlighted).toBeVisible();
  });
});

Step 2: Run test to verify it fails

Run: npm run test:mobile Expected: FAIL (tap doesn’t navigate in swipe mode yet)

Step 3: Modify footnote click handler

In assets/js/sidenotes.js, find the footnote click handler (around line 337) and replace it with:

    // Click on footnote ref - different behavior for touch vs desktop
    footnoteRef.addEventListener('click', (e) => {
      e.preventDefault();

      if (isTouchDevice() && document.body.classList.contains('swipe-margins-enabled')) {
        // Mobile: navigate to sidenote in margin
        navigateToSidenote(sidenote);
      } else if (viewportIsWideEnough()) {
        // Desktop: just highlight
        sidenote.classList.add('highlighted');
        setTimeout(() => sidenote.classList.remove('highlighted'), 2000);
      }
      // Narrow non-touch: allow default anchor behavior
    });

Step 4: Add navigateToSidenote function

Add before the createSidenote function:

  /**
   * Navigate to a sidenote in swipe mode
   * Scrolls horizontally to the margin column, vertically to the sidenote
   */
  function navigateToSidenote(sidenote) {
    const layout = document.querySelector('.margins-layout');
    if (!layout) return;

    // Determine which margin the sidenote is in
    const isLeftMargin = sidenote.classList.contains('margin-left');
    const targetColumn = isLeftMargin
      ? layout.querySelector('.margin-left')
      : layout.querySelector('.margin-right');

    if (!targetColumn) return;

    // Scroll horizontally to the column
    layout.scrollTo({
      left: targetColumn.offsetLeft,
      behavior: 'smooth'
    });

    // Scroll vertically to center the sidenote
    setTimeout(() => {
      sidenote.scrollIntoView({ behavior: 'smooth', block: 'center' });
    }, 300); // Wait for horizontal scroll to settle

    // Highlight
    sidenote.classList.add('highlighted');
    setTimeout(() => sidenote.classList.remove('highlighted'), 2000);
  }

Step 5: Run test

Run: npm run test:mobile Expected: PASS

Step 6: Commit

git add assets/js/sidenotes.js tests/mobile-margins.spec.js
git commit -m "feat(mobile): add footnote tap navigation to sidenotes"

Task 7: JavaScript - Backref Navigation

Files: - Modify: assets/js/sidenotes.js - Modify: tests/mobile-margins.spec.js

Step 1: Write the failing test

Add to tests/mobile-margins.spec.js:

test('tapping backref returns to main column', async ({ page }) => {
  await page.goto('/6024/1/23/every-heart-a-world/');
  await page.waitForSelector('.swipe-margins-enabled');
  await page.waitForTimeout(200);

  // Navigate to sidenote first
  const footnoteRef = page.locator('.footnote-ref').first();
  await footnoteRef.tap();
  await page.waitForTimeout(600);

  // Find and tap backref in the sidenote
  const backref = page.locator('.sidenote .footnote-backref').first();
  await backref.tap();
  await page.waitForTimeout(600);

  // Should be back at center column
  const layout = page.locator('.margins-layout');
  const scrollLeft = await layout.evaluate(el => el.scrollLeft);
  const viewportWidth = await page.evaluate(() => window.innerWidth);

  expect(scrollLeft).toBeGreaterThan(viewportWidth * 0.8);
  expect(scrollLeft).toBeLessThan(viewportWidth * 1.2);
});

Step 2: Run test to verify it fails

Run: npm run test:mobile Expected: FAIL

Step 3: Add backref handler in createSidenote

In assets/js/sidenotes.js, in the createSidenote function, after removing the backref from content (around line 312), add:

    // Handle backref clicks for swipe mode
    const contentBackrefs = content.querySelectorAll('a[href^="#fnref"]');
    contentBackrefs.forEach(backref => {
      backref.addEventListener('click', (e) => {
        if (isTouchDevice() && document.body.classList.contains('swipe-margins-enabled')) {
          e.preventDefault();
          navigateToAnchor(footnoteRef);
        }
      });
    });

Step 4: Add navigateToAnchor function

Add after navigateToSidenote:

  /**
   * Navigate back to the main column and footnote anchor
   */
  function navigateToAnchor(anchor) {
    const layout = document.querySelector('.margins-layout');
    if (!layout) return;

    const bodyText = layout.querySelector('.body-text');
    if (!bodyText) return;

    // Scroll horizontally to main column
    layout.scrollTo({
      left: bodyText.offsetLeft,
      behavior: 'smooth'
    });

    // Scroll vertically to the anchor
    setTimeout(() => {
      anchor.scrollIntoView({ behavior: 'smooth', block: 'center' });
    }, 300);

    // Highlight the anchor briefly
    anchor.classList.add('highlighted');
    setTimeout(() => anchor.classList.remove('highlighted'), 2000);
  }

Step 5: Run test

Run: npm run test:mobile Expected: PASS

Step 6: Commit

git add assets/js/sidenotes.js tests/mobile-margins.spec.js
git commit -m "feat(mobile): add backref navigation to return to main column"

Task 8: Test - Swipe Gesture Navigation

Files: - Modify: tests/mobile-margins.spec.js

Step 1: Write the test

Add to tests/mobile-margins.spec.js:

test.describe('Mobile Margins - Swipe Gestures', () => {
  test('swipe left navigates to right margin', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');
    await page.waitForTimeout(200);

    // Get viewport dimensions
    const viewportWidth = await page.evaluate(() => window.innerWidth);
    const viewportHeight = await page.evaluate(() => window.innerHeight);

    // Swipe left (finger moves right to left)
    await page.touchscreen.tap(viewportWidth * 0.8, viewportHeight / 2);
    await page.mouse.down();
    await page.mouse.move(viewportWidth * 0.2, viewportHeight / 2, { steps: 10 });
    await page.mouse.up();

    await page.waitForTimeout(500);

    // Should have snapped to right margin (scroll ~200vw)
    const layout = page.locator('.margins-layout');
    const scrollLeft = await layout.evaluate(el => el.scrollLeft);
    expect(scrollLeft).toBeGreaterThan(viewportWidth * 1.5);
  });

  test('swipe right navigates to left margin', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');
    await page.waitForTimeout(200);

    const viewportWidth = await page.evaluate(() => window.innerWidth);
    const viewportHeight = await page.evaluate(() => window.innerHeight);

    // Swipe right (finger moves left to right)
    await page.touchscreen.tap(viewportWidth * 0.2, viewportHeight / 2);
    await page.mouse.down();
    await page.mouse.move(viewportWidth * 0.8, viewportHeight / 2, { steps: 10 });
    await page.mouse.up();

    await page.waitForTimeout(500);

    // Should have snapped to left margin (scroll ~0)
    const layout = page.locator('.margins-layout');
    const scrollLeft = await layout.evaluate(el => el.scrollLeft);
    expect(scrollLeft).toBeLessThan(viewportWidth * 0.5);
  });
});

Step 2: Run test

Run: npm run test:mobile Expected: Should PASS (CSS scroll-snap handles this natively)

Step 3: Commit

git add tests/mobile-margins.spec.js
git commit -m "test(mobile): verify swipe gesture navigation"

Task 9: CSS - Peek Gutter Visibility

Files: - Modify: assets/css/almanack.css - Modify: tests/mobile-margins.spec.js

Step 1: Write the failing test

Add to tests/mobile-margins.spec.js:

test.describe('Mobile Margins - Peek Gutters', () => {
  test('manicules visible in gutters', async ({ page }) => {
    await page.goto('/6024/1/23/every-heart-a-world/');
    await page.waitForSelector('.swipe-margins-enabled');
    await page.waitForSelector('.sidenotes-ready');

    // At rest on main column, manicules should be partially visible
    // Check that sidenote-number elements exist in margins
    const leftManicule = page.locator('.margin-left .sidenote-number').first();
    const rightManicule = page.locator('.margin-right .sidenote-number').first();

    // They should exist
    await expect(leftManicule).toBeAttached();
    await expect(rightManicule).toBeAttached();
  });
});

Step 2: Run test

Run: npm run test:mobile Expected: Should PASS if sidenotes are being created (manicules are part of sidenotes)

Step 3: Commit

git add tests/mobile-margins.spec.js
git commit -m "test(mobile): verify manicules in peek gutters"

Task 10: Manual Visual Verification

Files: None (manual testing)

Step 1: Build and serve

source bin/a-activate
make sync && make build
make serve

Step 2: Open Chrome DevTools

  1. Navigate to an article with footnotes
  2. Open DevTools (F12)
  3. Toggle device toolbar (Ctrl+Shift+M)
  4. Select iPhone 13 or similar
  5. Reload page

Step 3: Verify visually

  • [ ] Main column centered on load
  • [ ] Peek gutters visible on left and right edges
  • [ ] Manicules (☞/☜) visible in gutters
  • [ ] Tap footnote → smooth scroll to margin
  • [ ] Tap backref → smooth scroll back to main
  • [ ] Swipe left → right margin snaps into view
  • [ ] Swipe right → left margin snaps into view
  • [ ] Scroll syncs across all columns

Step 4: Document any issues

Create issues for any visual bugs found.


Task 11: Final Cleanup and Documentation

Files: - Modify: doc/process/plans/2026-01-24-mobile-margins-design.md - Create: CONSENSX.md entry (optional)

Step 1: Update design doc with completion status

Add to top of design document:

**Status:** Implemented 2026-01-24

Step 2: Run full test suite

make test
npm run test:mobile

Expected: All tests passing

Step 3: Final commit

git add -A
git commit -m "docs: mark mobile margins design as implemented"

Summary

Task Description Tests
1 Playwright setup 1 placeholder
2 CSS no-JS fallback 1 test
3 CSS swipe layout 2 tests
4 JS touch detection (uses Task 3 tests)
5 Initial scroll position 1 test
6 Footnote tap navigation 1 test
7 Backref navigation 1 test
8 Swipe gestures 2 tests
9 Peek gutters 1 test
10 Manual verification -
11 Cleanup -

Total automated tests: 10


Post-Implementation Refinements

The following adjustments were made during implementation based on testing feedback:

Column Sizing and Snap Alignment

Original plan: 100vw columns with scroll-snap-align: start

Final implementation: 90vw columns with scroll-snap-align: center

This creates 5vw peek gutters on each side of the centered column, allowing manicules to remain partially visible as affordances for swiping.

Sidenote Constraints

Added max-width: 25em to sidenotes in swipe mode. Without this, sidenotes stretched to full viewport width, which was visually overwhelming and didn’t match the desktop proportions.

Flexbox Alignment for Sidenotes

Original approach: margin-left: auto to push left-margin sidenotes right

Final implementation: display: flex; flex-direction: column on aside containers with align-self: flex-end on sidenotes

The original approach failed because margin-left: auto doesn’t work for horizontal positioning in a column-direction flexbox. Using align-self properly positions sidenotes at the edge nearest the main column.

Manicule Nudging

Added -0.5rem negative margin toward the main column to nudge manicules further into the peek gutter, improving visibility at smaller viewport sizes (tested across 320px-430px widths).

Comfort Zone for Vertical Scrolling

Added a “comfort zone” check (10%-90% of viewport height) before triggering vertical scroll on footnote navigation. If the sidenote is already within this zone after horizontal scroll, no vertical scroll occurs—reducing disorienting motion when the sidenote was already visible.

Manicule Click Handlers

Made manicules (sidenote numbers like ☞1) clickable on touch devices. Tapping the manicule in the peek gutter navigates to show the full sidenote, providing an alternative to swiping.

Body Padding Reset

Added body.swipe-margins-enabled { padding: 0; } to prevent body padding from offsetting the snap positions.

Selector Specificity Fix

Changed .margin-left selectors to aside.margin-left to avoid matching .sidenote.margin-left elements (sidenotes placed in the left margin). The original selectors were applying display: block and min-height: 100vh to individual sidenotes, causing layout issues