<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="icon" type="image/png" href="{{ logo }}">
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="0">
  <title>{{ as.character(title) }}</title>
  <script type="text/javascript">
//<![CDATA[
(function () {
  const root = document.documentElement;

  function getNumVar(name) {
    const v = root.style.getPropertyValue(name);
    const n = v ? Number(v.trim()) : 0;
    return Number.isFinite(n) ? n : 0;
  }

  function freezeScreenVarsMonotonic(sw, sh) {
    const prevW = getNumVar('--screen-width');
    const prevH = getNumVar('--screen-height');

    // never shrink; only grow
    const nextW = Math.max(prevW, sw);
    const nextH = Math.max(prevH, sh);

    root.style.setProperty('--screen-width',  String(nextW)); // unitless for *1px
    root.style.setProperty('--screen-height', String(nextH));
  }

  function readScreen() {
    const s = window.screen;
    if (!s || !s.width || !s.height) return null;
    return { w: s.width, h: s.height };
  }

  function initWithRetry(tries = 20) {
    const v = readScreen();
    if (v) {
      freezeScreenVarsMonotonic(v.w, v.h);
      return true;
    }
    if (tries <= 0) return false;
    requestAnimationFrame(() => initWithRetry(tries - 1));
  }

  // init early
  initWithRetry();

  // BFCache restore
  window.addEventListener('pageshow', (e) => {
    if (e.persisted) initWithRetry();
  });

  // DPR change handler: keep it (as you want), but NEVER shrink the stored values
  let lastDpr = window.devicePixelRatio || 1;
  let mql = window.matchMedia(`(resolution: ${lastDpr}dppx)`);

  function onDprChange() {
    const newDpr = window.devicePixelRatio || 1;
    if (newDpr === lastDpr) return;
    lastDpr = newDpr;

    if (mql.removeEventListener) mql.removeEventListener('change', onDprChange);
    else if (mql.removeListener) mql.removeListener(onDprChange);

    mql = window.matchMedia(`(resolution: ${lastDpr}dppx)`);
    if (mql.addEventListener) mql.addEventListener('change', onDprChange);
    else if (mql.addListener) mql.addListener(onDprChange);

    initWithRetry(); // will only grow, never shrink
  }

  if (mql.addEventListener) mql.addEventListener('change', onDprChange);
  else if (mql.addListener) mql.addListener(onDprChange);
})();
//]]>
  </script>
  <script type="text/javascript">
//<![CDATA[
  window.dq_report2_iframe = true;
//]]>
</script>
  {{ deps }}
</head>
<body>
{{ content }}
</body>
</html>
