diff --git a/404.html b/404.html index 8f3b9db..a1104dc 100644 --- a/404.html +++ b/404.html @@ -5,6 +5,17 @@ 404 — Havox + + + diff --git a/icons/LICENSE-Tabler.txt b/icons/LICENSE-Tabler.txt new file mode 100644 index 0000000..3e82379 --- /dev/null +++ b/icons/LICENSE-Tabler.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020-2026 Paweł Kuna + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/icons/moon.svg b/icons/moon.svg new file mode 100644 index 0000000..211356c --- /dev/null +++ b/icons/moon.svg @@ -0,0 +1,15 @@ + + + diff --git a/icons/sun.svg b/icons/sun.svg new file mode 100644 index 0000000..9685d7a --- /dev/null +++ b/icons/sun.svg @@ -0,0 +1,16 @@ + + + + diff --git a/images/crack-the-quote-preview.webp b/images/crack-the-quote-preview.webp index a0c518a..584cd88 100644 Binary files a/images/crack-the-quote-preview.webp and b/images/crack-the-quote-preview.webp differ diff --git a/index.html b/index.html index ee8e5d0..706ab10 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,17 @@ name="description" content="Portfolio of John Gatward, a software engineer specialising in backend development, interactive visualisations, and self-hosted systems." /> - + + +
@@ -129,7 +149,7 @@
2026 → present
- Catppuccin Mocha. Single page. Professional. + Catppuccin Mocha & Latte. Single page. Professional.
diff --git a/script.js b/script.js index 6df7ed7..c51d640 100644 --- a/script.js +++ b/script.js @@ -128,8 +128,45 @@ function initProjectFilters() { let selectedFilter = 'all'; let isExpanded = false; + const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)'); + const canAnimate = projectCards.every( + (card) => typeof card.animate === 'function' + ); + const animations = new Map(); + const cancelAnimations = () => { + animations.forEach((animation) => animation.cancel()); + animations.clear(); + }; - const applyFilter = () => { + if (canAnimate) { + window.addEventListener('resize', cancelAnimations); + reducedMotion.addEventListener('change', () => { + if (reducedMotion.matches) cancelAnimations(); + }); + projectGrid?.addEventListener('focusin', (event) => { + const card = event.target.closest('.project-card'); + animations.get(card)?.cancel(); + animations.delete(card); + }); + } + + const applyFilter = (animate = false) => { + const shouldAnimate = animate && canAnimate && !reducedMotion.matches; + // Capture the current visual positions before cancelling an interrupted move. + const previous = new Map( + shouldAnimate + ? projectCards + .filter((card) => !card.hidden) + .map((card) => [ + card, + { + rect: card.getBoundingClientRect(), + opacity: window.getComputedStyle(card).opacity, + }, + ]) + : [] + ); + cancelAnimations(); const matchingCards = projectCards.filter( (card) => selectedFilter === 'all' || @@ -139,6 +176,9 @@ function initProjectFilters() { ? matchingCards : matchingCards.slice(0, DEFAULT_VISIBLE_PROJECTS); const shownSet = new Set(shownCards); + const focusedCard = projectCards.find((card) => + card.contains(document.activeElement) + ); projectCards.forEach((card) => { card.hidden = !shownSet.has(card); }); @@ -171,6 +211,41 @@ function initProjectFilters() { ? 'Show fewer projects' : `Show ${remainingCount} more project${remainingCount === 1 ? '' : 's'}`; } + if (focusedCard && !shownSet.has(focusedCard)) { + filterChips + .find((chip) => chip.dataset.filter === selectedFilter) + .focus(); + } + + if (shouldAnimate) { + const positions = shownCards.map((card) => [ + card, + card.getBoundingClientRect(), + ]); + positions.forEach(([card, rect]) => { + const before = previous.get(card); + const x = before ? before.rect.left - rect.left : 0; + const y = before ? before.rect.top - rect.top : 12; + const opacity = before ? before.opacity : 0; + if ( + card.contains(document.activeElement) || + (x === 0 && y === 0 && Number(opacity) === 1) + ) { + return; + } + const animation = card.animate( + [ + { transform: `translate(${x}px, ${y}px)`, opacity }, + { transform: 'translate(0px, 0px)', opacity: 1 }, + ], + { duration: 280, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)' } + ); + animations.set(card, animation); + animation.addEventListener('finish', () => { + if (animations.get(card) === animation) animations.delete(card); + }); + }); + } }; filterChips.forEach((chip) => { @@ -178,14 +253,14 @@ function initProjectFilters() { selectedFilter = chip.dataset.filter || 'all'; isExpanded = false; - applyFilter(); + applyFilter(true); }); }); if (seeMoreButton) { seeMoreButton.addEventListener('click', () => { isExpanded = !isExpanded; - applyFilter(); + applyFilter(true); if (!isExpanded) { seeMoreButton.scrollIntoView({ block: 'nearest', behavior: 'instant' }); } @@ -196,7 +271,7 @@ function initProjectFilters() { resetButton.addEventListener('click', () => { selectedFilter = 'all'; isExpanded = false; - applyFilter(); + applyFilter(true); filterChips.find((chip) => chip.dataset.filter === 'all').focus(); }); } diff --git a/style.css b/style.css index 1af6c75..d5ec06c 100644 --- a/style.css +++ b/style.css @@ -6,8 +6,9 @@ src: url('fonts/geist-variable.ttf') format('truetype'); } -/* ─── Catppuccin Mocha ─────────────────────────────────── */ +/* ─── Catppuccin themes ────────────────────────────────── */ :root { + color-scheme: light dark; --font-body: 'Geist', system-ui, sans-serif; --font-mono: 'JetBrains Mono', monospace; --base: #1e1e2e; @@ -36,6 +37,70 @@ --pink: #f5c2e7; --flamingo: #f2cdcd; --rosewater: #f5e0dc; + --nav-background: rgba(17, 17, 27, 0.85); + --accent-soft: rgba(203, 166, 247, 0.15); + --accent-selected: rgba(203, 166, 247, 0.18); + --accent-border: rgba(203, 166, 247, 0.7); + --on-accent: #11111b; + --link-accent: var(--mauve); +} + +:root[data-theme='light'] { + color-scheme: light; +} + +:root[data-theme='dark'] { + color-scheme: dark; +} + +/* Catppuccin Latte / Mocha; older browsers retain the original Mocha palette. */ +@supports (color: light-dark(#eff1f5, #1e1e2e)) { + :root { + --base: light-dark(#eff1f5, #1e1e2e); + --mantle: light-dark(#e6e9ef, #181825); + --crust: light-dark(#dce0e8, #11111b); + --surface0: light-dark(#ccd0da, #313244); + --surface1: light-dark(#bcc0cc, #45475a); + --surface2: light-dark(#acb0be, #585b70); + --overlay0: light-dark(#9ca0b0, #6c7086); + --overlay1: light-dark(#8c8fa1, #7f849c); + --overlay2: light-dark(#7c7f93, #9399b2); + --text: light-dark(#4c4f69, #cdd6f4); + --subtext0: light-dark(#60647b, #a6adc8); + --subtext1: light-dark(#5c5f77, #bac2de); + --lavender: light-dark(#7287fd, #b4befe); + --blue: light-dark(#1e66f5, #89b4fa); + --sapphire: light-dark(#209fb5, #74c7ec); + --sky: light-dark(#04a5e5, #89dceb); + --teal: light-dark(#179299, #94e2d5); + --green: light-dark(#40a02b, #a6e3a1); + --yellow: light-dark(#df8e1d, #f9e2af); + --peach: light-dark(#fe640b, #fab387); + --maroon: light-dark(#e64553, #eba0ac); + --red: light-dark(#d20f39, #f38ba8); + --mauve: light-dark(#8839ef, #cba6f7); + --pink: light-dark(#ea76cb, #f5c2e7); + --flamingo: light-dark(#dd7878, #f2cdcd); + --rosewater: light-dark(#dc8a78, #f5e0dc); + --nav-background: light-dark( + rgba(239, 241, 245, 0.94), + rgba(17, 17, 27, 0.85) + ); + --accent-soft: light-dark( + rgba(136, 57, 239, 0.03), + rgba(203, 166, 247, 0.15) + ); + --accent-selected: light-dark( + rgba(136, 57, 239, 0.03), + rgba(203, 166, 247, 0.18) + ); + --accent-border: light-dark( + rgba(136, 57, 239, 0.7), + rgba(203, 166, 247, 0.7) + ); + --on-accent: light-dark(#eff1f5, #11111b); + --link-accent: light-dark(#8235e6, #cba6f7); + } } *, @@ -80,7 +145,7 @@ footer { ::selection { background: var(--mauve); - color: var(--crust); + color: var(--on-accent); } :focus-visible { @@ -96,7 +161,7 @@ footer { padding: 0.6rem 1rem; border-radius: 4px; background: var(--mauve); - color: var(--crust); + color: var(--on-accent); font-size: 0.8rem; font-weight: 700; text-decoration: none; @@ -138,7 +203,7 @@ nav { justify-content: space-between; padding: 0 3rem; height: 56px; - background: rgba(17, 17, 27, 0.85); + background: var(--nav-background); backdrop-filter: blur(12px); border-bottom: 1px solid var(--surface0); } @@ -160,6 +225,48 @@ nav { list-style: none; } +.nav-actions { + display: flex; + align-items: center; + gap: 1.5rem; +} + +.theme-toggle { + display: inline-flex; + align-items: center; + justify-content: center; + min-height: 44px; + width: 44px; + padding: 0.5rem; + border: 1px solid transparent; + border-radius: 4px; + background: transparent; + color: var(--subtext1); + cursor: pointer; + transition: + color 0.2s, + background-color 0.2s; +} + +.theme-toggle:hover { + background: var(--accent-soft); + color: var(--mauve); +} + +.theme-icon { + display: block; + width: 22px; + height: 22px; + background: currentColor; + -webkit-mask: url('icons/sun.svg') center / contain no-repeat; + mask: url('icons/sun.svg') center / contain no-repeat; +} + +.theme-toggle[data-theme='dark'] .theme-icon { + -webkit-mask-image: url('icons/moon.svg'); + mask-image: url('icons/moon.svg'); +} + .nav-links a, .mobile-nav-links a { display: flex; @@ -298,7 +405,7 @@ section.full-width { .btn-primary { background: var(--mauve); - color: var(--crust); + color: var(--on-accent); } .btn-primary:hover { @@ -320,6 +427,7 @@ section.full-width { } .btn:active, +.theme-toggle:active, .filter-chip:active, .projects-more-btn:active { transform: scale(0.98); @@ -495,7 +603,7 @@ section.full-width { font-size: 0.6rem; letter-spacing: 0.08em; text-transform: uppercase; - background: rgba(203, 166, 247, 0.15); + background: var(--accent-soft); color: var(--mauve); padding: 0.1rem 0.45rem; border-radius: 3px; @@ -570,7 +678,7 @@ section.full-width { letter-spacing: 0.04em; font-weight: 700; background: var(--surface0); - color: var(--subtext1); + color: var(--text); } /* ─── Tutorials / Projects shared ─────────────────────── */ @@ -662,10 +770,17 @@ section.full-width { #project-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 1rem; + background: transparent; + border: none; + border-radius: 0; + overflow: visible; } #project-grid .card { padding: 1.2rem; + border: 1px solid var(--surface0); + border-radius: 8px; } .project-filters { @@ -699,8 +814,8 @@ section.full-width { } .filter-chip.active { - background: rgba(203, 166, 247, 0.18); - border-color: rgba(203, 166, 247, 0.7); + background: var(--accent-selected); + border-color: var(--accent-border); color: var(--mauve); } @@ -848,13 +963,13 @@ section.full-width { align-items: center; min-height: 44px; padding: 0 0.25rem; - color: var(--mauve); + color: var(--link-accent); text-decoration: underline; text-underline-offset: 2px; } .card-source a:hover { - color: var(--mauve); + color: var(--link-accent); } .card-desc a { @@ -863,13 +978,13 @@ section.full-width { display: inline-flex; align-items: center; min-height: 44px; - color: var(--mauve); + color: var(--link-accent); text-decoration: underline; text-underline-offset: 2px; } .card-desc a:hover { - color: var(--mauve); + color: var(--link-accent); } /* ─── Contact ──────────────────────────────────────────── */ @@ -1074,6 +1189,10 @@ footer a:hover { } @media (max-width: 540px) { + .nav-actions { + gap: 0.75rem; + } + .nav-links { display: none; } diff --git a/tests/script.test.js b/tests/script.test.js index 5ad8e85..545092f 100644 --- a/tests/script.test.js +++ b/tests/script.test.js @@ -37,6 +37,7 @@ function element(dataset = {}, textContent = '') { scrollIntoView() { this.scrolledIntoView = true; }, + contains: () => false, }; } @@ -112,6 +113,126 @@ function fixture({ missingFilter = false, reducedMotion = false } = {}) { }; } +function animatedFixture(options) { + const f = fixture(options); + const animations = []; + const listeners = new Map(); + const media = { + matches: options?.reducedMotion ?? false, + addEventListener: (name, listener) => + listeners.set(`motion-${name}`, listener), + }; + f.context.window.matchMedia = () => media; + f.context.window.addEventListener = (name, listener) => + listeners.set(name, listener); + f.context.window.getComputedStyle = () => ({ opacity: '1' }); + for (const card of f.cards) { + card.getBoundingClientRect = () => { + const index = f.visible().indexOf(card); + return { left: (index % 3) * 300, top: Math.floor(index / 3) * 200 }; + }; + card.animate = (frames, timing) => { + const animation = { + card, + frames, + timing, + cancelled: false, + cancel() { + this.cancelled = true; + }, + addEventListener(name, listener) { + if (name === 'finish') this.finish = listener; + }, + }; + animations.push(animation); + return animation; + }; + } + return { ...f, animations, listeners, media }; +} + +test('filtering moves retained cards and fades in new cards without delaying results', () => { + const f = animatedFixture(); + f.start(); + assert.equal(f.animations.length, 0); + const rust = f.chips.find((chip) => chip.dataset.filter === 'rust'); + rust.click(); + assert.equal(f.visible().length, 1); + assert.equal( + f.controls.get('#project-count').textContent, + 'Showing 1 of 1 project (Rust)' + ); + assert.ok( + f.animations.some( + (animation) => animation.frames[0].transform !== 'translate(0px, 0px)' + ) + ); + assert.ok( + f.animations.every((animation) => animation.timing.duration === 280) + ); + f.chips[0].click(); + assert.equal(f.visible().length, 9); + assert.ok( + f.animations.some((animation) => animation.frames[0].opacity === 0) + ); +}); + +test('rapid filter changes cancel old animations and finish on the latest selection', () => { + const f = animatedFixture(); + f.start(); + f.chips.find((chip) => chip.dataset.filter === 'rust').click(); + const previous = [...f.animations]; + f.chips[0].click(); + assert.ok(previous.every((animation) => animation.cancelled)); + const current = f.animations.filter((animation) => !animation.cancelled); + previous.forEach((animation) => animation.finish()); + f.listeners.get('resize')(); + assert.ok(current.every((animation) => animation.cancelled)); + assert.equal(f.visible().length, 9); +}); + +test('reduced motion is instant and enabling it cancels in-flight animations', () => { + const reduced = animatedFixture({ reducedMotion: true }); + reduced.start(); + reduced.chips.find((chip) => chip.dataset.filter === 'rust').click(); + assert.equal(reduced.visible().length, 1); + assert.equal(reduced.animations.length, 0); + + const f = animatedFixture(); + f.start(); + f.chips.find((chip) => chip.dataset.filter === 'rust').click(); + f.media.matches = true; + f.listeners.get('motion-change')(); + assert.ok(f.animations.every((animation) => animation.cancelled)); + const count = f.animations.length; + f.chips[0].click(); + assert.equal(f.animations.length, count); + assert.equal(f.visible().length, 9); +}); + +test('filtering restores focus when the focused project becomes hidden', () => { + const f = fixture(); + f.start(); + const focused = f.cards.find((card) => !card.dataset.tech.includes('rust')); + f.context.document.activeElement = focused; + focused.contains = (target) => target === focused; + const rust = f.chips.find((chip) => chip.dataset.filter === 'rust'); + rust.click(); + assert.equal(focused.hidden, true); + assert.equal(rust.focused, true); +}); + +test('keyboard focus immediately settles a moving project', () => { + const f = animatedFixture(); + f.start(); + f.chips.find((chip) => chip.dataset.filter === 'rust').click(); + const animation = f.animations[0]; + f.controls.get('#project-grid').dispatch('focusin', { + target: { closest: () => animation.card }, + }); + assert.equal(animation.cancelled, true); +}); + test('projects start collapsed with an accurate count and remaining label', () => { const f = fixture(); f.start(); diff --git a/tests/theme.test.js b/tests/theme.test.js new file mode 100644 index 0000000..85ed8a5 --- /dev/null +++ b/tests/theme.test.js @@ -0,0 +1,187 @@ +const assert = require('node:assert/strict'); +const { readFileSync } = require('node:fs'); +const { resolve } = require('node:path'); +const { test } = require('node:test'); +const { runInNewContext } = require('node:vm'); + +const root = resolve(__dirname, '..'); +const script = readFileSync(resolve(root, 'theme.js'), 'utf8'); +const html = readFileSync(resolve(root, 'index.html'), 'utf8'); + +function fixture({ + dark = false, + saved = null, + storageError = null, + writeError = null, +} = {}) { + const dataset = {}; + const storage = new Map(saved === null ? [] : [['havox-theme', saved]]); + const meta = [{ content: '#1e1e2e' }, { content: '#eff1f5' }]; + const listeners = new Map(); + const warnings = []; + const attributes = new Map(); + const button = { + hidden: true, + dataset: {}, + setAttribute: (name, value) => attributes.set(name, value), + getAttribute: (name) => attributes.get(name), + addEventListener: (name, listener) => listeners.set(name, listener), + }; + const media = { + matches: dark, + addEventListener: (name, listener) => + listeners.set(`system-${name}`, listener), + }; + const context = { + document: { + documentElement: { dataset }, + querySelectorAll: () => meta, + querySelector: () => button, + addEventListener: (name, listener) => listeners.set(name, listener), + }, + window: { + matchMedia: (query) => { + assert.equal(query, '(prefers-color-scheme: dark)'); + return media; + }, + }, + localStorage: { + getItem: (key) => { + if (storageError) throw storageError; + return storage.get(key) ?? null; + }, + setItem: (key, value) => { + if (storageError) throw storageError; + if (writeError) throw writeError; + storage.set(key, value); + }, + }, + console: { warn: (...args) => warnings.push(args) }, + }; + runInNewContext(script, context); + return { + dataset, + storage, + meta, + button, + warnings, + ready: () => listeners.get('DOMContentLoaded')(), + toggle: () => listeners.get('click')(), + setSystemDark(value) { + media.matches = value; + listeners.get('system-change')(); + }, + }; +} + +test('system preference applies immediately and follows operating-system changes', () => { + for (const dark of [false, true]) { + const f = fixture({ dark }); + assert.equal(f.dataset.theme, undefined); + assert.ok( + f.meta.every((meta) => meta.content === (dark ? '#1e1e2e' : '#eff1f5')) + ); + assert.equal(f.button.hidden, true); + f.ready(); + assert.equal(f.button.hidden, false); + assert.equal(f.button.dataset.theme, dark ? 'dark' : 'light'); + assert.equal( + f.button.getAttribute('aria-label'), + `Switch to ${dark ? 'light' : 'dark'} theme` + ); + f.setSystemDark(!dark); + assert.equal(f.button.dataset.theme, dark ? 'light' : 'dark'); + assert.equal( + f.button.getAttribute('aria-label'), + `Switch to ${dark ? 'dark' : 'light'} theme` + ); + assert.ok( + f.meta.every((meta) => meta.content === (dark ? '#eff1f5' : '#1e1e2e')) + ); + } +}); + +test('saved themes override the system before first paint', () => { + for (const saved of ['light', 'dark']) { + const f = fixture({ dark: saved === 'light', saved }); + assert.equal(f.dataset.theme, saved); + f.ready(); + assert.equal(f.button.dataset.theme, saved); + f.setSystemDark(saved === 'dark'); + assert.equal(f.dataset.theme, saved); + assert.ok( + f.meta.every( + (meta) => meta.content === (saved === 'dark' ? '#1e1e2e' : '#eff1f5') + ) + ); + } + assert.ok(html.indexOf('src="theme.js"') < html.indexOf('href="style.css"')); + const notFound = readFileSync(resolve(root, '404.html'), 'utf8'); + assert.ok( + notFound.indexOf('src="theme.js"') < notFound.indexOf('href="style.css"') + ); + assert.match( + notFound, + /class="theme-toggle"[\s\S]*?type="button"[\s\S]*?hidden/ + ); +}); + +test('the button toggles the effective theme and persists the explicit choice', () => { + const f = fixture({ dark: true }); + f.ready(); + f.toggle(); + assert.equal(f.dataset.theme, 'light'); + assert.equal(f.storage.get('havox-theme'), 'light'); + f.toggle(); + assert.equal(f.dataset.theme, 'dark'); + assert.equal(f.storage.get('havox-theme'), 'dark'); + f.setSystemDark(false); + assert.equal(f.dataset.theme, 'dark'); + assert.equal(f.button.dataset.theme, 'dark'); + assert.ok(f.meta.every((meta) => meta.content === '#1e1e2e')); + const reloaded = fixture({ + dark: false, + saved: f.storage.get('havox-theme'), + }); + reloaded.ready(); + assert.equal(reloaded.button.dataset.theme, 'dark'); + reloaded.toggle(); + assert.equal(reloaded.dataset.theme, 'light'); +}); + +test('invalid saved values do not become theme attributes', () => { + const f = fixture({ saved: 'unexpected' }); + f.ready(); + assert.equal(f.dataset.theme, undefined); + assert.equal(f.button.dataset.theme, 'light'); +}); + +test('blocked storage warns but does not prevent switching themes', () => { + const storageError = new Error('Storage denied'); + storageError.name = 'SecurityError'; + const f = fixture({ storageError }); + f.ready(); + f.toggle(); + assert.equal(f.dataset.theme, 'dark'); + assert.ok(f.meta.every((meta) => meta.content === '#1e1e2e')); + assert.equal(f.warnings.length, 2); +}); + +test('unexpected storage errors are not swallowed', () => { + assert.throws( + () => fixture({ storageError: new Error('Unexpected failure') }), + /Unexpected failure/ + ); +}); + +test('full storage does not prevent toggling in either direction', () => { + const writeError = new Error('Storage full'); + writeError.name = 'QuotaExceededError'; + const f = fixture({ writeError }); + f.ready(); + f.toggle(); + assert.equal(f.dataset.theme, 'dark'); + f.toggle(); + assert.equal(f.dataset.theme, 'light'); + assert.equal(f.warnings.length, 2); +}); diff --git a/theme.js b/theme.js new file mode 100644 index 0000000..0573a97 --- /dev/null +++ b/theme.js @@ -0,0 +1,59 @@ +const themeRoot = document.documentElement; +const systemTheme = window.matchMedia('(prefers-color-scheme: dark)'); +let selectedTheme = 'system'; +let themeButton; + +try { + const savedTheme = localStorage.getItem('havox-theme'); + if (savedTheme === 'light' || savedTheme === 'dark') { + selectedTheme = savedTheme; + } +} catch (error) { + if (error.name !== 'SecurityError') throw error; + console.warn('Theme preference storage is unavailable.', error); +} + +function applyTheme() { + if (selectedTheme === 'system') { + delete themeRoot.dataset.theme; + } else { + themeRoot.dataset.theme = selectedTheme; + } + const dark = + selectedTheme === 'dark' || + (selectedTheme === 'system' && systemTheme.matches); + document.querySelectorAll('meta[name="theme-color"]').forEach((meta) => { + meta.content = dark ? '#1e1e2e' : '#eff1f5'; + }); + if (themeButton) { + const action = `Switch to ${dark ? 'light' : 'dark'} theme`; + themeButton.dataset.theme = dark ? 'dark' : 'light'; + themeButton.setAttribute('aria-label', action); + themeButton.title = `${dark ? 'Dark' : 'Light'} theme. ${action}`; + } +} + +// Restore the preference before the stylesheet paints the page. +applyTheme(); +systemTheme.addEventListener('change', applyTheme); + +document.addEventListener('DOMContentLoaded', () => { + themeButton = document.querySelector('.theme-toggle'); + applyTheme(); + themeButton.hidden = false; + themeButton.addEventListener('click', () => { + selectedTheme = themeButton.dataset.theme === 'dark' ? 'light' : 'dark'; + applyTheme(); + try { + localStorage.setItem('havox-theme', selectedTheme); + } catch (error) { + if ( + error.name !== 'SecurityError' && + error.name !== 'QuotaExceededError' + ) { + throw error; + } + console.warn('Theme preference could not be saved.', error); + } + }); +});