async function initialize() {
  syncThemeControl();
  refreshIcons();
  applicationShell.initialize();
  elements.tradeDate.value = todayString();
  const initialUrl = new URL(window.location.href);
  if (initialUrl.searchParams.has("date")) {
    initialUrl.searchParams.delete("date");
    history.replaceState(null, "", initialUrl);
  }
  elements.tradeDate.max = todayString();
  document.querySelector("#journalDate").value = elements.tradeDate.value;
  document.querySelector("#journalDate").max = todayString();
  document.querySelector("#tradeLogDate").value = elements.tradeDate.value;
  document.querySelector("#tradeLogDate").max = todayString();
  document.querySelector("#backfillStart").value = todayString();
  document.querySelector("#backfillEnd").value = todayString();
  document.querySelector("#backfillStart").max = todayString();
  document.querySelector("#backfillEnd").max = todayString();
  document.querySelector("#qiObservationDate").value = elements.tradeDate.value;
  document.querySelector("#qiObservationDate").max = todayString();
  document.querySelector("#accountBirthDate").max = todayString();
  document.querySelector("#alertDate").value = todayString();
  bindEvents();
  try {
    const session = await apiRequest("/api/auth/me");
    if (!session.authenticated) {
      const params = new URLSearchParams();
      if (session.registration_required) params.set("mode", "register");
      const query = params.toString();
      window.location.replace("/login/" + (query ? `?${query}` : ""));
      return;
    }
    await applyAuthenticatedSession(session);
  } catch (error) {
    showAuthGate(error.message || "无法连接本地服务");
  }
}

async function startAuthenticatedApp() {
  if (state.started) return;
  state.started = true;
  const searchParams = new URLSearchParams(window.location.search);
  const requestedHeavenPanel = searchParams.get("heaven");
  if (["trend", "fortune", "heart"].includes(requestedHeavenPanel)) {
    state.heavenPanel = requestedHeavenPanel;
  }
  const requestedView = window.XiaobaiPages.resolve(searchParams.get("view"));
  if (
    requestedView
    && window.XiaobaiPages.has(requestedView)
    && document.getElementById(requestedView)?.classList.contains("workspace-view")
  ) {
    openView(requestedView, false);
    if (requestedView !== searchParams.get("view")) {
      const url = new URL(window.location.href);
      url.searchParams.set("view", requestedView);
      history.replaceState(null, "", url);
    }
  }
  loadDashboard();
  loadAlerts();
  if (new URLSearchParams(window.location.search).get("settings") === "1") {
    setTimeout(openSettings, 0);
  }
}


function openView(viewId, updateHash = true) {
  if (!applicationShell.page(viewId) || !pageModules.has(viewId)) return;
  const previousView = state.activeView;
  pageModules.beforeMount(viewId, previousView);
  if (!applicationShell.mount(viewId, { updateUrl: updateHash })) return;
  pageModules.afterMount(viewId, previousView);
}


if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", initialize, { once: true });
} else {
  initialize();
}




function bindEvents() {
  bindDashboardEvents();
  bindSessionEvents();
  bindMarketEvents();
  bindThemeEvents();
  pageModules.bind();
  bindAdminEvents();
  bindSharedTableEvents();
}
