(function() { // Default: HKD → JPY document.getElementById('fx-from').value = 'HKD'; document.getElementById('fx-to').value = 'JPY'; setTimeout(fxInit, 50); ['fx-amount','fx-from','fx-to'].forEach(id => { document.getElementById(id).addEventListener('input', fxCalc); document.getElementById(id).addEventListener('change', fxCalc); }); })(); // Static fallback rates (vs HKD = 1) — updated 2026-Q1 estimates const FX_FALLBACK = { HKD: 1.0, USD: 0.128, EUR: 0.117, JPY: 19.2, GBP: 0.099, KRW: 175, TWD: 4.12, THB: 4.31, SGD: 0.171, MYR: 0.567, CNY: 0.917, AUD: 0.196, NZD: 0.213, CAD: 0.176, CHF: 0.114, PHP: 7.32, IDR: 2030, VND: 3220, INR: 10.85, AED: 0.470 }; let fxRates = null; let fxUpdated = null; async function fxInit() { try { // Try Supabase live rates const supaUrl = 'https://itawskzbsbjodlljbuwt.supabase.co/functions/v1/fx-rates'; const r = await fetch(supaUrl, { method: 'POST' }); if (r.ok) { const j = await r.json(); if (j && j.rates) { fxRates = j.rates; fxUpdated = j.updated; } } } catch(e) { /* fallback */ } if (!fxRates) { fxRates = FX_FALLBACK; fxUpdated = '2026-Q1 (fallback)'; } document.getElementById('fx-updated').textContent = '更新:' + fxUpdated; fxCalc(); fxRenderTiles(); } function fxCalc() { try { const amount = parseFloat(document.getElementById('fx-amount').value) || 0; const from = document.getElementById('fx-from').value; const to = document.getElementById('fx-to').value; if (!fxRates) return; // All rates vs HKD=1 const hkd = amount / fxRates[from]; const out = hkd * fxRates[to]; document.getElementById('fx-out').value = out.toFixed(to === 'JPY' || to === 'KRW' || to === 'IDR' || to === 'VND' ? 0 : 2); // Show result const result = document.getElementById('fx-result'); result.style.display = 'block'; result.innerHTML = '