File size: 22,361 Bytes
c23a2bb
 
 
 
 
 
 
 
5a7eedd
c565c51
c23a2bb
 
5a7eedd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a2bb
 
 
 
 
ae67f47
c23a2bb
 
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a2bb
 
 
 
 
 
9149c92
c23a2bb
 
 
9149c92
 
 
c23a2bb
 
 
 
 
 
 
 
 
 
8507407
 
 
 
 
 
 
 
 
 
7d87e20
 
 
 
 
 
 
 
 
 
 
9149c92
 
 
 
 
 
 
c768a73
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8507407
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a2bb
 
9149c92
 
 
 
 
 
 
 
c23a2bb
 
 
 
 
 
 
 
 
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fbb6c
 
 
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a2bb
 
 
 
 
 
9149c92
7d87e20
93fbb6c
9149c92
c23a2bb
 
 
 
 
 
 
9149c92
 
 
 
c23a2bb
 
 
9149c92
 
c768a73
c23a2bb
 
 
 
 
 
93fbb6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9149c92
 
c23a2bb
9149c92
 
 
 
c23a2bb
9149c92
c23a2bb
9149c92
 
 
 
 
c23a2bb
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23a2bb
9149c92
 
 
c23a2bb
9149c92
 
 
 
 
 
 
c23a2bb
 
 
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
c768a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9149c92
c23a2bb
9149c92
c23a2bb
9149c92
 
 
 
c23a2bb
 
 
 
9149c92
c768a73
9149c92
 
 
 
 
 
 
 
 
 
c23a2bb
9149c92
 
c23a2bb
 
9149c92
 
 
93fbb6c
 
 
 
 
 
 
 
 
 
 
 
 
9149c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fbb6c
9149c92
 
 
 
 
c23a2bb
c768a73
c23a2bb
 
c768a73
c23a2bb
c65db4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CompactAI Papers</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Geist:wght@300..700&family=Geist+Mono:wght@400;500&family=Fredoka:wght@700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="everywhere.css">
</head>
<body>
  <svg width="0" height="0" style="position:absolute;pointer-events:none" aria-hidden="true">
    <defs>
      <filter id="puffy-3d-gold" x="-20%" y="-20%" width="140%" height="140%" color-interpolation-filters="sRGB">
        <feGaussianBlur in="SourceAlpha" stdDeviation="5" result="blur" />
        <feDiffuseLighting in="blur" surfaceScale="10" diffuseConstant="1.2" lighting-color="#ffeaa7" result="diffuse">
          <feDistantLight azimuth="225" elevation="50" />
        </feDiffuseLighting>
        <feComposite in="diffuse" in2="SourceAlpha" operator="in" result="diffuse-clipped" />
        <feFlood flood-color="#f1b51c" result="base-gold" />
        <feBlend mode="multiply" in="base-gold" in2="diffuse-clipped" result="shaded-body" />
        <feSpecularLighting in="blur" surfaceScale="10" specularConstant="1.9" specularExponent="40" lighting-color="#ffffff" result="specular">
          <feDistantLight azimuth="225" elevation="50" />
        </feSpecularLighting>
        <feComposite in="specular" in2="SourceAlpha" operator="in" result="specular-clipped" />
        <feComposite in="specular-clipped" in2="shaded-body" operator="arithmetic" k2="1" k3="1" result="lit-gold" />
        <feComposite in="lit-gold" in2="SourceAlpha" operator="in" />
      </filter>
    </defs>
  </svg>
  <a href="#mainContent" class="skip-link">Skip to papers</a>
  <div class="sr-only" aria-live="polite" aria-atomic="true" id="announcer"></div>

  <header class="site-header">
    <div class="container">
      <h1 class="site-title">Compact<span class="gold-shadow"><span class="gold">AI</span></span> Papers</h1>
      <p class="site-subtitle">Research &amp; ideas from the community</p>
      <div class="header-divider"></div>

      <div class="toolbar" aria-label="Paper tools">
        <label class="toolbar-search">
          <span class="sr-only">Search papers</span>
          <input id="paperSearch" type="search" placeholder="Search title, author, tags" autocomplete="off">
        </label>
        <label class="toolbar-select">
          <span class="sr-only">Sort papers</span>
          <select id="paperSort">
            <option value="featured">Featured first</option>
            <option value="newest">Newest first</option>
            <option value="title">Title A to Z</option>
            <option value="author">Author A to Z</option>
          </select>
        </label>
        <button class="toolbar-chip is-active" type="button" data-filter="all">All</button>
        <button class="toolbar-chip" type="button" data-filter="proved">Proved</button>
        <button class="toolbar-chip" type="button" data-filter="featured">Featured</button>
        <button class="toolbar-reset" type="button" id="resetFilters">Reset</button>
      </div>
    </div>
  </header>

  <main class="container" id="mainContent" aria-label="Papers collection">
    <h2 class="section-label">Papers</h2>
    <ul class="papers-grid" id="papersGrid" role="list"></ul>
    <p class="collection-status" id="collectionStatus" aria-live="polite"></p>
  </main>

  <section class="papermaker-callout" aria-label="Papermaker role">
    <div class="container">
      <p>Have an idea worth writing up? If a mod spots it and likes it, you might earn the <span class="role-name">@Papermaker</span> role &mdash; a nod that you helped push CompactAI forward.</p>
    </div>
  </section>

  <footer class="site-footer">
    <div class="container">
      <span>CompactAI</span>
    </div>
  </footer>

  <script>
    var PAPERS = [
      {
        id: "FewbutLong-md",
        title: "Long context, fewer samples",
        date: "2026-05",
        author: "Costikoooo",
        tags: ["dataset", "pre-processing", "training"],
        proved: false,
        featured: false,
        superFeatured: false,
      },
      {
        id: "STM_paper-md",
        title: "STM and the Circle Thing",
        date: "2026-05",
        author: "Dragonoid",
        tags: ["training", "experimental"],
        proved: true,
        featured: true,
        superFeatured: true,
        proof_script_content: "import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport random\nimport numpy as np\n\n# STM (Subtractive Training Method) - Definitive Proof\n# Fix: Added Mastery Floor to prevent premature hard-task removal.\n\nN_EASY, N_HARD = 1000, 250\nMASTERY_THRESHOLD = 0.15\nEPOCHS = 25\n\ndef generate_math(difficulty=\"easy\"):\n    if difficulty == \"easy\":\n        a, b = random.randint(0, 9), random.randint(0, 9)\n    else:\n        a, b = random.randint(10, 99), random.randint(10, 99)\n    return f\"{a}+{b}={a+b}\", difficulty\n\ntrain_raw = [generate_math(\"easy\") for _ in range(N_EASY)] + [generate_math(\"hard\") for _ in range(N_HARD)]\ntest_easy = [generate_math(\"easy\") for _ in range(200)]\ntest_hard = [generate_math(\"hard\") for _ in range(200)]\n\nchars = list(\"0123456789+=\") + [\"<PAD>\"]\nchar2idx = {c: i for i, c in enumerate(chars)}\nPAD_IDX, EQ_IDX = char2idx[\"<PAD>\"], char2idx[\"=\"]\n\ndef encode(data_list):\n    max_l = max(len(d[0]) for d in data_list)\n    X, Y = [], []\n    for text, _ in data_list:\n        enc = [char2idx[c] for c in text] + [PAD_IDX] * (max_l - len(text) + 1)\n        X.append(enc[:-1]); Y.append(enc[1:])\n    return torch.tensor(X), torch.tensor(Y)\n\nclass STM_Model(nn.Module):\n    def __init__(self, vocab_size, hidden_size=128):\n        super().__init__()\n        self.embedding = nn.Embedding(vocab_size, hidden_size)\n        self.rnn = nn.GRU(hidden_size, hidden_size, batch_first=True)\n        self.fc = nn.Linear(hidden_size, vocab_size)\n    def forward(self, x):\n        out, _ = self.rnn(self.embedding(x))\n        return self.fc(out)\n\nmodel = STM_Model(len(chars))\noptimizer = optim.Adam(model.parameters(), lr=0.003)\ncriterion = nn.CrossEntropyLoss(ignore_index=PAD_IDX, reduction='none')\n\ndef eval_acc(model, data_list):\n    model.eval()\n    X, Y = encode(data_list)\n    with torch.no_grad():\n        out = model(X).argmax(dim=-1)\n        correct = 0\n        for i in range(len(data_list)):\n            eq_pos = (X[i] == EQ_IDX).nonzero(as_tuple=True)[0].item()\n            pred = out[i, eq_pos:eq_pos+4]\n            target = Y[i, eq_pos:eq_pos+4]\n            mask = (target != PAD_IDX)\n            if torch.equal(pred[mask], target[mask]): correct += 1\n        return correct / len(data_list)\n\ndef get_metrics(model, data_list):\n    model.eval()\n    X, Y = encode(data_list)\n    with torch.no_grad():\n        out = model(X).view(-1, len(chars))\n        loss = criterion(out, Y.view(-1)).view(X.size(0), X.size(1))\n        ans_mask = ((X == EQ_IDX).cumsum(1) - (X == EQ_IDX).float()) * (Y != PAD_IDX).float()\n        return ((loss * ans_mask).sum(1) / (ans_mask.sum(1) + 1e-8)).numpy()\n\ncurrent_dataset = list(train_raw)\nprev_losses = get_metrics(model, current_dataset)\ntotal_tokens = 0\ninitial_tokens = len(train_raw) * 12\n\nfor epoch in range(1, EPOCHS + 1):\n    model.train()\n    X, Y = encode(current_dataset)\n    total_tokens += X.numel()\n    indices = np.random.permutation(len(current_dataset))\n    for i in range(0, len(current_dataset), 64):\n        idx = indices[i:i+64]\n        optimizer.zero_grad()\n        loss = nn.CrossEntropyLoss(ignore_index=PAD_IDX)(model(X[idx]).view(-1, len(chars)), Y[idx].view(-1))\n        loss.backward(); optimizer.step()\n\n    curr_losses = get_metrics(model, current_dataset)\n    drops = prev_losses - curr_losses\n    drop_cutoff = drops.mean() + (drops.std() * 0.5)\n\n    keep_indices = []\n    for i in range(len(current_dataset)):\n        if (drops[i] > drop_cutoff and curr_losses[i] < 0.5) or curr_losses[i] < MASTERY_THRESHOLD:\n            pass\n        else:\n            keep_indices.append(i)\n\n    if not keep_indices: break\n    current_dataset = [current_dataset[i] for i in keep_indices]\n    prev_losses = curr_losses[keep_indices]\n\nprint(\"STM proof complete.\")"
      },
      {
        id: "trueact-md",
        title: "TrueACT: A Different Kind of Neuron",
        date: "2026-05",
        author: "CompactAI",
        tags: ["architecture", "neurons", "math", "experimental"],
        proved: false,
        featured: false
      },
      {
        id: "apollonian_gasket-md",
        title: "Token Embeddings Inside an Integer Apollonian Gasket",
        date: "2026-05",
        author: "Mage",
        tags: ["embeddings", "geometry", "number-theory", "experimental"],
        proved: false,
        featured: false
      },
      {
        id: "overta_hypothesis-md",
        title: "The Overta Hypothesis: Knowledge-Free Foundation Models",
        date: "2026-05",
        author: "Amy",
        tags: ["training", "alignment", "reasoning", "experimental"],
        proved: false,
        featured: false
      },
      {
        id: "attention_experiment-md",
        title: "An Experiment With Attention",
        date: "2026-05",
        author: "Costikoooo",
        tags: ["attention", "benchmark", "experimental"],
        proved: false,
        featured: false
      },
      {
        id: "sparrow_fant-md",
        title: "Sparrow, FANT, and the Weird Stuff That Works",
        date: "2026-05",
        author: "Crownelius",
        tags: ["math", "small-models", "experimental"],
        proved: false,
        featured: false
      }
    ];

    (function() {
      var grid = document.getElementById('papersGrid');
      var searchInput = document.getElementById('paperSearch');
      var sortSelect = document.getElementById('paperSort');
      var resetButton = document.getElementById('resetFilters');
      var filterButtons = document.querySelectorAll('[data-filter]');
      var collectionStatus = document.getElementById('collectionStatus');
      var currentFilter = 'all';
      var viewerContainer = null;
      var modalState = null;

      function announce(msg) {
        var el = document.getElementById('announcer');
        if (el) {
          el.textContent = '';
          setTimeout(function() { el.textContent = msg; }, 50);
        }
      }

      function escapeHtml(str) {
        var div = document.createElement('div');
        div.appendChild(document.createTextNode(str));
        return div.innerHTML;
      }

      function getPaperById(paperId) {
        for (var i = 0; i < PAPERS.length; i++) {
          if (PAPERS[i].id === paperId) return PAPERS[i];
        }
        return null;
      }

      function matchesFilter(paper) {
        if (currentFilter === 'proved' && !paper.proved) return false;
        if (currentFilter === 'featured' && !paper.featured) return false;
        return true;
      }

      function matchesSearch(paper, query) {
        if (!query) return true;
        return [paper.title, paper.author, paper.date, (paper.tags || []).join(' ')].join(' ').toLowerCase().indexOf(query) !== -1;
      }

      function sortPapers(items) {
        var sorted = items.slice();
        var sortValue = sortSelect.value;
        sorted.sort(function(a, b) {
          if (sortValue === 'title') return a.title.localeCompare(b.title);
          if (sortValue === 'author') return (a.author || '').localeCompare(b.author || '');
          if (sortValue === 'newest') return (b.date || '').localeCompare(a.date || '');
          if (b.featured !== a.featured) return (b.featured ? 1 : 0) - (a.featured ? 1 : 0);
          return 0;
        });
        return sorted;
      }

      function renderGrid() {
        var query = searchInput.value.trim().toLowerCase();
        var visible = sortPapers(PAPERS.filter(function(paper) {
          return matchesFilter(paper) && matchesSearch(paper, query);
        }));
        grid.innerHTML = '';

        if (visible.length === 0) {
          grid.innerHTML = '<li class="empty-state" role="listitem"><p>No papers match these filters.</p><button type="button" id="clearEmptyState">Clear filters</button></li>';
          document.getElementById('clearEmptyState').addEventListener('click', resetFilters);
          collectionStatus.textContent = '0 papers shown.';
          return;
        }

        visible.forEach(function(paper, index) {
          grid.appendChild(createCard(paper, index));
        });

        collectionStatus.textContent = visible.length + ' papers shown.';
        requestAnimationFrame(function() {
          observeCards();
        });
      }

      function setFilter(nextFilter) {
        currentFilter = nextFilter;
        filterButtons.forEach(function(button) {
          button.classList.toggle('is-active', button.getAttribute('data-filter') === nextFilter);
        });
        renderGrid();
      }

      function resetFilters() {
        currentFilter = 'all';
        searchInput.value = '';
        sortSelect.value = 'featured';
        filterButtons.forEach(function(button) {
          button.classList.toggle('is-active', button.getAttribute('data-filter') === 'all');
        });
        renderGrid();
        announce('Filters cleared.');
      }

      function syncUrl(paperId) {
        var url = new URL(window.location.href);
        if (paperId) {
          url.searchParams.set('paper', paperId);
        } else {
          url.searchParams.delete('paper');
        }
        window.history.pushState({ paper: paperId }, '', url.toString());
      }

      function createCard(paper, index) {
        var li = document.createElement('li');
        li.setAttribute('role', 'listitem');

        var card = document.createElement('a');
        card.className = 'paper-card' + (paper.featured ? ' featured' : '') + (paper.superFeatured ? ' super-featured' : '');
        card.style.setProperty('--card-index', index);
        card.href = '#paper-' + paper.id;
        card.setAttribute('aria-label', 'Read paper: ' + paper.title);

        card.innerHTML =
          '<h3 class="card-title">' + escapeHtml(paper.title) + '</h3>' +
          (paper.author ? '<div class="card-author">' + escapeHtml(paper.author) + '</div>' : '') +
          '<div class="card-meta" id="meta-' + paper.id + '">' +
            (paper.date ? '<span>' + escapeHtml(paper.date) + '</span>' : '') +
            ((paper.tags || []).map(function(tag) {
              return '<span class="card-tag">' + escapeHtml(tag) + '</span>';
            }).join(' ')) +
            (paper.proved ? '<span class="card-proved">&check; PROVED</span>' : '') +
          '</div>' +
          '<span class="card-arrow" aria-hidden="true">&rarr;</span>';

        card.addEventListener('click', function(e) {
          e.preventDefault();
          window.open('Papers/' + paper.id + '.html', '_blank');
        });

        li.appendChild(card);
        return li;
      }

      var cardObserver = null;

      function observeCards() {
        var cards = grid.querySelectorAll('.paper-card');
        if (!cards.length) return;

        if (cardObserver) {
          cardObserver.disconnect();
        }

        if (!('IntersectionObserver' in window)) {
          cards.forEach(function(card) {
            card.classList.add('is-visible');
          });
          return;
        }

        cardObserver = new IntersectionObserver(function(entries, observer) {
          entries.forEach(function(entry) {
            if (entry.isIntersecting) {
              entry.target.classList.add('is-visible');
              observer.unobserve(entry.target);
            }
          });
        }, {
          threshold: 0.18,
          rootMargin: '0px 0px -8% 0px'
        });

        cards.forEach(function(card) {
          card.classList.remove('is-visible');
          cardObserver.observe(card);
        });
      }

      function openPaper(paper, triggerEl, pushState) {
        closeViewer(false);

        modalState = {
          previousFocus: triggerEl || document.activeElement,
          paper: paper
        };

        if (pushState !== false) syncUrl(paper.id);

        viewerContainer = document.createElement('div');
        viewerContainer.className = 'paper-overlay';
        viewerContainer.setAttribute('role', 'dialog');
        viewerContainer.setAttribute('aria-modal', 'true');
        viewerContainer.setAttribute('aria-labelledby', 'paperDialogTitle');

        viewerContainer.innerHTML =
          '<div class="paper-view" role="document">' +
            '<button class="paper-view-close" type="button" aria-label="Close paper viewer">&times;</button>' +
            '<div class="paper-page">' +
              '<div class="paper-page-shell">' +
                '<h2 id="paperDialogTitle" class="paper-page-title">' + escapeHtml(paper.title) + '</h2>' +
                '<div class="paper-page-meta">' + escapeHtml(paper.author || 'CompactAI') + (paper.date ? ' · ' + escapeHtml(paper.date) : '') + '</div>' +
                '<div class="paper-page-actions">' +
                  '<a class="paper-view-link" href="Papers/' + escapeHtml(paper.id) + '.html" target="_blank" rel="noopener">Open in new tab</a>' +
                  '<button class="paper-view-link" type="button" data-action="copy">Copy link</button>' +
                '</div>' +
                '<iframe class="paper-view-frame" title="' + escapeHtml(paper.title) + '" src="Papers/' + paper.id + '.html"></iframe>' +
              '</div>' +
            '</div>' +
          '</div>';

        document.body.appendChild(viewerContainer);
        document.body.style.overflow = 'hidden';
        document.querySelector('main').setAttribute('inert', '');

        var closeBtn = viewerContainer.querySelector('.paper-view-close');
        var copyBtn = viewerContainer.querySelector('[data-action="copy"]');
        var iframe = viewerContainer.querySelector('.paper-view-frame');

        function close() {
          closeViewer(true);
        }

        closeBtn.addEventListener('click', close);

        copyBtn.addEventListener('click', function() {
          var link = window.location.origin + window.location.pathname + '?paper=' + encodeURIComponent(paper.id);
          if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(link).then(function() {
              announce('Paper link copied.');
            });
          }
        });

        viewerContainer.addEventListener('click', function(e) {
          if (e.target === viewerContainer) close();
        });

        iframe.addEventListener('load', function() {
          try {
            var doc = iframe.contentDocument;
            var shell = doc.querySelector('.paper-page-shell');
            if (shell) {
              var titleEl = shell.querySelector('.paper-page-title');
              var metaEl = shell.querySelector('.paper-page-meta');
              if (titleEl) titleEl.style.display = 'none';
              if (metaEl) metaEl.style.display = 'none';
            }
            var body = doc.body;
            var html = doc.documentElement;
            if (body) {
              iframe.style.height = Math.max(body.scrollHeight, html.scrollHeight) + 'px';
              iframe.style.overflow = 'hidden';
            }
          } catch(e) {}
          announce('Opened paper viewer: ' + paper.title);
        });

        document.addEventListener('keydown', function handler(e) {
          if (!viewerContainer) {
            document.removeEventListener('keydown', handler);
            return;
          }
          if (e.key === 'Escape') {
            close();
            document.removeEventListener('keydown', handler);
          }
          if (e.key === 'Tab') {
            var focusable = viewerContainer.querySelectorAll('button, a');
            var first = focusable[0];
            var last = focusable[focusable.length - 1];
            if (e.shiftKey && document.activeElement === first) {
              last.focus();
              e.preventDefault();
            } else if (!e.shiftKey && document.activeElement === last) {
              first.focus();
              e.preventDefault();
            }
          }
        });

        closeBtn.focus();
      }

      function closeViewer(syncHistory) {
        if (!viewerContainer) return;
        var previousFocus = modalState && modalState.previousFocus;
        if (viewerContainer.classList.contains('closing')) return;
        viewerContainer.classList.add('closing');
        setTimeout(function() {
          if (!viewerContainer) return;
          viewerContainer.remove();
          viewerContainer = null;
          modalState = null;
          document.body.style.overflow = '';
          document.querySelector('main').removeAttribute('inert');
          if (syncHistory !== false) syncUrl(null);
          if (previousFocus && typeof previousFocus.focus === 'function') previousFocus.focus();
          announce('Paper viewer closed.');
        }, 320);
      }

      searchInput.addEventListener('input', renderGrid);
      sortSelect.addEventListener('change', renderGrid);
      resetButton.addEventListener('click', resetFilters);
      filterButtons.forEach(function(button) {
        button.addEventListener('click', function() {
          setFilter(button.getAttribute('data-filter'));
        });
      });

      window.addEventListener('popstate', function() {
        var params = new URLSearchParams(window.location.search);
        var paperId = params.get('paper');
        if (!paperId) {
          closeViewer(false);
          return;
        }
        var paper = getPaperById(paperId);
        if (paper) openPaper(paper, null, false);
      });

      renderGrid();
      observeCards();

      var initialPaperId = new URLSearchParams(window.location.search).get('paper');
      if (initialPaperId) {
        var initialPaper = getPaperById(initialPaperId);
        if (initialPaper) openPaper(initialPaper, null, false);
      }

    })();
  </script>

</body>
</html>