<?php

if (!function_exists('cmsBuilderCanUseLightweightRuntime')) {
    function cmsBuilderCanUseLightweightRuntime(array $layout)
    {
        $page = is_array($layout['page'] ?? null) ? $layout['page'] : [];
        if (!empty($page['free_layout_mode'])
            || !empty($page['frontend_extra_sections'])
            || !empty($page['frontend_canvas_blocks'])) {
            return false;
        }
        foreach ((array) ($page['site_widgets'] ?? []) as $enabled) {
            if (!empty($enabled)) {
                return false;
            }
        }
        return true;
    }
}

if (!function_exists('cmsRenderFrontendBuilderRuntime')) {
    function cmsRenderFrontendBuilderRuntime(array $options = [])
    {
        $boot = [
            'builder_type' => (string) ($options['builder_type'] ?? 'home'),
            'builder_ref_id' => intval($options['builder_ref_id'] ?? 0),
            'page_title' => (string) ($options['page_title'] ?? ''),
            'layout' => is_array($options['layout'] ?? null) ? $options['layout'] : ['page' => [], 'sections' => []],
        ];
        $json = json_encode(
            $boot,
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
        );

        ob_start();
        ?>
<style id="cms-builder-runtime-base-style">
.cms-builder-free-transform{transform:var(--cms-builder-free-transform,none) var(--cms-builder-origin-transform,none)!important;transform-origin:var(--cms-builder-transform-origin,top left)!important}
body.cms-builder-device-mobile{margin-left:auto;margin-right:auto}.cms-builder-extra-zone{margin:36px auto 0;max-width:1200px;padding:0 20px}
</style>
<div id="cms_frontend_builder_runtime" hidden></div>
<script>
window.CMS_FRONTEND_BUILDER_BOOT=<?php echo $json ?: '{}'; ?>;
window.CMS_FRONTEND_BUILDER_RUNTIME='lite';
(function(boot){
'use strict';
var page=boot.layout&&boot.layout.page?boot.layout.page:{};
var lastDevice='';
function qs(s,r){return(r||document).querySelector(s)}
function qsa(s,r){return Array.prototype.slice.call((r||document).querySelectorAll(s))}
function device(){return window.innerWidth<=768?'mobile':'pc'}
function cssEscape(v){var text=String(v||''),slash=String.fromCharCode(92);return text.split(slash).join(slash+slash).split('"').join(slash+'"')}
function byPath(path){if(!path)return null;try{return document.querySelector(path)}catch(e){return null}}
function normalizeRef(value){if(!value)return{path:'',key:''};if(typeof value==='string')return{path:value,key:''};return{path:typeof value.path==='string'?value.path:'',key:typeof value.key==='string'?value.key:''}}
function byRef(ref){ref=normalizeRef(ref);return(ref.key?qs('[data-builder-key="'+cssEscape(ref.key)+'"]'):null)||byPath(ref.path)}
function bucketRef(value){if(value.indexOf('key:')===0)return{key:value.slice(4),path:''};if(value.indexOf('path:')===0)return{path:value.slice(5),key:''};return{path:value,key:''}}
function keySegment(node){
 var tag=(node.tagName||'node').toLowerCase();
 if(node.id)return tag+'#id:'+node.id;
 if(node.hasAttribute('data-builder-extra-id'))return tag+'#extra:'+(node.getAttribute('data-builder-extra-id')||'');
 if(node.hasAttribute('data-business-item-type')||node.hasAttribute('data-business-item-id'))return tag+'#biz:'+(node.getAttribute('data-business-item-type')||'')+':'+(node.getAttribute('data-business-item-id')||'');
 var index=1,sibling=node;while((sibling=sibling.previousElementSibling)){if(sibling.tagName===node.tagName)index++}return tag+':'+index;
}
function assignKeys(){
 var seed='page:'+String(boot.builder_type||'page')+':'+String(boot.builder_ref_id||0);
 qsa('body *').forEach(function(node){
  if(!(node instanceof Element)||node.closest('#cms_frontend_builder_runtime,#cms_builder_page_widgets')||/^(script|style|link|meta|head|noscript)$/i.test(node.tagName||'')||node.hasAttribute('data-builder-key'))return;
  var chain=[],current=node;while(current&&current instanceof Element&&current!==document.body){chain.unshift(keySegment(current));if(current.id)break;current=current.parentElement}if(current===document.body)chain.unshift('body');node.setAttribute('data-builder-key',seed+'|'+chain.join('/'));
 });
}
function applyOverrides(){
 var overrides=device()==='mobile'?(page.frontend_mobile_overrides||{}):(page.frontend_overrides||{});
 Object.keys(overrides).forEach(function(key){
  var node=byRef(bucketRef(key)),data=overrides[key]||{};if(!node)return;
  if(Object.prototype.hasOwnProperty.call(data,'text'))node.textContent=data.text==null?'':String(data.text);
  if(Object.prototype.hasOwnProperty.call(data,'html'))node.innerHTML=data.html==null?'':String(data.html);
  if(Object.prototype.hasOwnProperty.call(data,'src')&&node.tagName.toLowerCase()==='img')node.setAttribute('src',data.src||'');
  if(Object.prototype.hasOwnProperty.call(data,'href')&&node.tagName.toLowerCase()==='a')node.setAttribute('href',data.href||'#');
  if(Object.prototype.hasOwnProperty.call(data,'style')){if(data.style)node.setAttribute('style',data.style);else node.removeAttribute('style');if(data.free_transform||String(data.style||'').indexOf('--cms-builder-free-transform')!==-1)node.classList.add('cms-builder-free-transform');else node.classList.remove('cms-builder-free-transform')}
 });
}
function applyOrders(){
 (page.frontend_block_orders||[]).forEach(function(entry){if(!entry||!entry.parent||!Array.isArray(entry.paths))return;var parent=byRef(entry.parent);if(!parent)return;entry.paths.forEach(function(ref){var child=byRef(ref);if(child&&child.parentNode===parent)parent.appendChild(child)})});
}
function applyVisibility(){
 qsa('[data-builder-runtime-hidden],[data-builder-runtime-deleted]').forEach(function(node){node.removeAttribute('data-builder-runtime-hidden');node.removeAttribute('data-builder-runtime-deleted');node.style.removeProperty('display')});
 var hidden=device()==='mobile'?(page.frontend_mobile_hidden_paths||[]):(page.frontend_hidden_paths||[]);
 var deleted=device()==='mobile'?(page.frontend_mobile_deleted_paths||[]):(page.frontend_deleted_paths||[]);
 hidden.forEach(function(ref){var node=byRef(ref);if(node){node.setAttribute('data-builder-runtime-hidden','1');node.style.setProperty('display','none','important')}});
 deleted.forEach(function(ref){var node=byRef(ref);if(node){node.setAttribute('data-builder-runtime-deleted','1');node.style.setProperty('display','none','important')}});
}
function safeCss(value,fallback){value=String(value||'').replace(/[{};]/g,'').trim();return value||fallback}
function applyVisuals(){
 var style=qs('#cms_builder_page_style');if(!style){style=document.createElement('style');style.id='cms_builder_page_style';document.head.appendChild(style)}
 var mobile=device()==='mobile',width=mobile?(parseInt(page.mobile_page_width,10)||375):(parseInt(page.page_width,10)||1200),padding=mobile?(parseInt(page.mobile_page_padding,10)||0):(parseInt(page.page_padding,10)||0);
 var preset=['compact','wide'].indexOf(page.layout_preset)!==-1?page.layout_preset:'classic',gap=preset==='compact'?12:(preset==='wide'?28:18),radius=preset==='compact'?12:(preset==='wide'?24:18),shadow=preset==='wide'?'0 16px 34px rgba(15,23,42,.08)':(preset==='compact'?'0 6px 18px rgba(15,23,42,.04)':'0 10px 24px rgba(15,23,42,.06)');
 var theme=safeCss(page.theme_color,'#5f7eeb'),font=safeCss(page.font_family,'inherit'),background='transparent';if((page.background_mode||'default')==='default')background=page.theme_surface==='soft'?'linear-gradient(180deg,rgba(255,255,255,.98),rgba(95,126,235,.05))':'transparent';else if(page.background_mode==='custom'&&page.background_color)background=safeCss(page.background_color,'transparent');
 // Keep the historical template main element full-width; constrain only explicit template containers and builder additions.
 var containers='.w1200,.wrap,header .wrap,footer .wrap,.footer-columns,.footer-contact,#cms_builder_extra_zone',blocks='main > *,section,article,.k740-banner,.k740-search,.productind,.odds,.about-k740,.qa-news,#cms_builder_extra_zone > *';
 var css=':root{--cms-builder-theme:'+theme+'}body{background:'+background+';font-family:'+font+';padding-left:'+padding+'px;padding-right:'+padding+'px}'+containers+'{max-width:'+width+'px;width:100%;margin-left:auto;margin-right:auto}'+blocks+'{margin-left:auto;margin-right:auto;transition:max-width .2s ease,margin .2s ease,border-radius .2s ease,box-shadow .2s ease,padding .2s ease}main > * + *,#cms_builder_extra_zone > * + *{margin-top:'+gap+'px}body[data-builder-layout-preset="'+preset+'"] '+blocks+'{border-radius:'+radius+'px}body[data-builder-layout-preset="compact"] main > *,body[data-builder-layout-preset="compact"] #cms_builder_extra_zone > *{padding-left:0;padding-right:0}';
 if(preset!=='compact')css+='body[data-builder-layout-preset="'+preset+'"] main > *,body[data-builder-layout-preset="'+preset+'"] #cms_builder_extra_zone > *{box-shadow:'+shadow+'}';
 css+='.builder-card{border-radius:'+radius+'px;box-shadow:'+shadow+';overflow:hidden}.builder-extra-button a{background:'+theme+'}footer{'+(page.footer_background_color?'background:'+safeCss(page.footer_background_color,'')+';':'')+(page.footer_text_color?'color:'+safeCss(page.footer_text_color,'')+';':'')+'text-align:'+(page.footer_text_align==='left'||page.footer_text_align==='right'?page.footer_text_align:'center')+';padding-top:'+(parseInt(page.footer_padding_top,10)||20)+'px;padding-bottom:'+(parseInt(page.footer_padding_bottom,10)||20)+'px}';
 style.textContent=css;document.body.setAttribute('data-builder-layout-preset',preset);document.body.classList.toggle('cms-builder-device-mobile',mobile);
}
function applyHead(){var title=page.title||boot.page_title||document.title||'';if(page.browser_title_visible)document.title=title;var href=page.favicon_url||'',icon=qs('#cms_builder_favicon');if(href&&!icon){icon=document.createElement('link');icon.id='cms_builder_favicon';icon.rel='icon';document.head.appendChild(icon)}if(icon&&href)icon.href=href}
function apply(){assignKeys();applyOverrides();applyOrders();applyVisibility();applyVisuals();applyHead();lastDevice=device()}
function ready(){apply();var timer=null;window.addEventListener('resize',function(){clearTimeout(timer);timer=setTimeout(function(){if(device()!==lastDevice)apply()},160)})}
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',ready);else ready();
})(window.CMS_FRONTEND_BUILDER_BOOT||{});
</script>
        <?php
        return (string) ob_get_clean();
    }
}
