/* Comportamentos exclusivos da página app_cores.php */

function normalizeHex(v){v=String(v||'').trim();if(!v)return '#000000';if(v.indexOf('0x')===0||v.indexOf('0X')===0)v='#'+v.substring(2);if(v[0]!=='#')v='#'+v;return v.toUpperCase();}
function syncPreview(){
  var accent=normalizeHex(document.querySelector('[name="accent_color"]').value);
  var border=normalizeHex(document.querySelector('[name="border_color"]').value);
  var button=normalizeHex(document.querySelector('[name="button_color"]').value);
  var text=normalizeHex(document.querySelector('[name="text_on_accent"]').value);
  var box=document.getElementById('previewBox');
  if(box){box.style.setProperty('--app-accent',accent);box.style.setProperty('--app-border',border);box.style.setProperty('--app-button',button);box.style.setProperty('--app-text',text);}
}
document.querySelectorAll('.color-picker').forEach(function(p){p.addEventListener('input',function(){var t=document.getElementById(p.dataset.target);if(t)t.value=p.value.toUpperCase();syncPreview();});});
document.querySelectorAll('.color-text').forEach(function(t){t.addEventListener('input',syncPreview);});
document.querySelectorAll('.opacity-range').forEach(function(r){r.addEventListener('input',function(){var v=document.getElementById(r.id+'_val');if(v)v.textContent=r.value+'%';});});
syncPreview();
