ature){sync();building=false;return;}
previousSignature=key;grid.replaceChildren();steps=[];
fresh.forEach(function(group,index){
var section=document.createElement('div');section.className='sp-v75-step';
var title=document.createElement('h3');title.textContent=(index+1)+'. '+human(group.key);section.appendChild(title);
var step={el:section,type:group.type,items:[],inputs:group.inputs,select:group.select};
if(group.type==='radio'){
group.inputs.forEach(function(r){var label=readLabel(r);if(!label)return;
var b=addButton(section,label,function(){if(r.disabled)return;if(!r.checked)r.click();progress=Math.max(progress,index+1);show();sync();});
step.items.push({button:b,input:r});
});
}else{
Array.from(group.select.options).forEach(function(option){if(!option.value && /selecione|escolha|--/.test(option.textContent.toLowerCase()))return;
var b=addButton(section,option.textContent.trim(),function(){
if(option.disabled)return;
if(group.select.value!==option.value){group.select.value=option.value;group.select.dispatchEvent(new Event('input',{bubbles:true}));group.select.dispatchEvent(new Event('change',{bubbles:true}));}
progress=Math.max(progress,index+1);show();sync();
});
step.items.push({button:b,value:option.value,disabled:option.disabled});
});
}
if(step.items.length){steps.push(step);grid.appendChild(section);}
});
progress=Math.min(progress,Math.max(0,steps.length-1));show();sync();building=false;
}
btn.addEventListener('click',function(){opened=true;btn.setAttribute('aria-expanded','true');show();panel.scrollIntoView({behavior:'smooth',block:'start'});});
function nativeChange(e){var index=steps.findIndex(function(step){return step.inputs.indexOf(e.target)!==-1;});if(index>=0){progress=Math.max(progress,index+1);show();sync();}}
if(source)source.addEventListener('change',nativeChange,true);
if(originalOptions)originalOptions.addEventListener('change',nativeChange,true);
build();if(!steps.length){panel.remove();chamada.remove();return;}
page.classList.add('sp-v75-ready');page.classList.add('sp-v78-global');
// O layout passa a substituir a exibição dos campos originais só após confirmar que os novos controles foram gerados.
if(window.MutationObserver){
var obs=new MutationObserver(function(){if(building)return;clearTimeout(refreshTimer);refreshTimer=setTimeout(build,180);});
if(source)obs.observe(source,{subtree:true,childList:true});
if(originalOptions)obs.observe(originalOptions,{subtree:true,childList:true});
}
}
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
})();