const App = {
  toggle(id){
    const el = document.getElementById(id);
    if(!el) return;
    el.classList.toggle('open');
  }
};

document.addEventListener('click', (e)=>{
  document.querySelectorAll('.menu.open').forEach(m=>{
    const wrap = m.closest('.lang');
    if(wrap && !wrap.contains(e.target)) m.classList.remove('open');
  });
});

document.addEventListener('DOMContentLoaded', ()=>{
  // Mod selection
  const mods = document.querySelectorAll('[data-mod]');
  const sel = new Set();
  const list = document.getElementById('selectedList');
  const count = document.getElementById('selCount');

  function render(){
    if(!list || !count) return;
    count.textContent = String(sel.size);
    if(sel.size===0){
      list.innerHTML = '<span class="muted">None</span>';
      return;
    }
    list.innerHTML = [...sel].map(x=>`<span class="badge">${x}</span>`).join(' ');
  }

  mods.forEach(btn=>{
    btn.addEventListener('click', ()=>{
      const key = btn.dataset.mod;
      if(btn.classList.contains('disabled')) return;
      if(sel.has(key)){ sel.delete(key); btn.classList.remove('selected'); }
      else { sel.add(key); btn.classList.add('selected'); }
      render();
    });
  });

  // Upload UI
  const drop = document.getElementById('drop');
  const input = document.getElementById('fileInput');
  const meta = document.getElementById('fileMeta');

  function setFile(f){
    if(!meta) return;
    meta.textContent = `${f.name} • ${(f.size/1024/1024).toFixed(2)} MB`;
  }

  if(input){
    input.addEventListener('change', ()=>{
      if(input.files && input.files[0]) setFile(input.files[0]);
    });
  }
  if(drop && input){
    drop.addEventListener('click', ()=> input.click());
    drop.addEventListener('dragover', (e)=>{ e.preventDefault(); drop.classList.add('drag'); });
    drop.addEventListener('dragleave', ()=> drop.classList.remove('drag'));
    drop.addEventListener('drop', (e)=>{
      e.preventDefault(); drop.classList.remove('drag');
      const f = e.dataTransfer.files && e.dataTransfer.files[0];
      if(f) setFile(f);
    });
  }

  render();
});