const apiKeyStorageKey = 'ctv-api-key'; function getStorage(): Storage | undefined { if (typeof window === 'undefined') { return undefined; } try { return window.localStorage; } catch { return undefined; } } export function getStoredApiKey(): string | null { const value = getStorage()?.getItem(apiKeyStorageKey); return value && value.trim().length > 0 ? value : null; } export function setStoredApiKey(apiKey: string): void { const trimmed = apiKey.trim(); if (trimmed.length === 0) { clearStoredApiKey(); return; } getStorage()?.setItem(apiKeyStorageKey, trimmed); } export function clearStoredApiKey(): void { getStorage()?.removeItem(apiKeyStorageKey); }