/* ───── Stars + particles ───── */
(()=>{
  const co=document.getElementById('cosmos'),cs=['rgba(162,155,254,.8)','rgba(85,239,196,.7)','rgba(253,121,168,.7)'];
  for(let i=0;i<150;i++){const s=document.createElement('div');s.className='star'+(Math.random()>.83?' c':'');
    if(s.classList.contains('c'))s.style.setProperty('--sc',cs[Math.random()*3|0]);
    s.style.left=Math.random()*100+'%';s.style.top=Math.random()*100+'%';
    s.style.setProperty('--d',(2+Math.random()*5)+'s');s.style.setProperty('--dl',Math.random()*5+'s');
    s.style.setProperty('--mo',(0.3+Math.random()*.7)+'');co.appendChild(s);}
  const pc=['var(--primary)','var(--cyan)','var(--pink)'];
  for(let i=0;i<20;i++){const p=document.createElement('div');p.className='particle';
    p.style.left=Math.random()*100+'%';p.style.setProperty('--sz',(2+Math.random()*4)+'px');
    p.style.setProperty('--pc',pc[Math.random()*3|0]);p.style.setProperty('--fd',(7+Math.random()*12)+'s');
    p.style.setProperty('--fdl',Math.random()*10+'s');co.appendChild(p);}
})();

/* ───── Theme toggle ───── */
function setTheme(t){
  if(window._themeSwitching)return;
  const cur=document.documentElement.classList.contains('dark')?'dark':'light';
  if(cur===t)return;
  window._themeSwitching=true;
  const fade=document.getElementById('theme-fade');
  fade.style.background=t==='dark'
    ?'radial-gradient(ellipse at center,#0c0b18,#06050e)'
    :'radial-gradient(ellipse at center,#f3f2fa,#fafaff)';
  fade.classList.add('in');
  setTimeout(()=>{
    document.documentElement.classList.toggle('dark',t==='dark');
    document.querySelectorAll('.pill button').forEach(b=>b.classList.toggle('on',(t==='dark'&&b.textContent==='Dark')||(t==='light'&&b.textContent==='Light')));
    setTimeout(()=>{fade.classList.remove('in');window._themeSwitching=false;},50);
  },420);
}

/* ───── Mode switch (사용자 ↔ 개발자) ───── */
function switchMode(mode){
  if(window._modeSwitching)return;
  const shell=document.querySelector('.shell');
  const isAdmin=shell.classList.contains('admin-mode');
  if((mode==='admin'&&isAdmin)||(mode==='main'&&!isAdmin))return;
  window._modeSwitching=true;
  const mainSide=document.getElementById('side');
  const adminSide=document.getElementById('admin-side');
  const mainTabs=document.getElementById('main-tabs');
  const adminTabs=document.getElementById('admin-tabs');
  const bc=document.getElementById('breadcrumb');
  const modeFade=document.getElementById('mode-fade');
  modeFade.classList.add('in');
  const curSide=mode==='admin'?mainSide:adminSide;
  const newSide=mode==='admin'?adminSide:mainSide;
  const curTabs=mode==='admin'?mainTabs:adminTabs;
  const newTabs=mode==='admin'?adminTabs:mainTabs;
  setTimeout(()=>{
    curSide.style.display='none';
    newSide.style.display='';
    curTabs.style.display='none';
    newTabs.style.display='';
    shell.classList.toggle('admin-mode',mode==='admin');
    bc.innerHTML=mode==='admin'?'개발자 &rsaquo; <b>템플릿 목록</b>':'홈 &rsaquo; <b>인사 대시보드</b>';
    document.querySelector('.admin-label').textContent=mode==='admin'?'사용자':'개발자';
    setTimeout(()=>{modeFade.classList.remove('in');window._modeSwitching=false;},300);
  },300);
}

/* ───── Sidebar click ───── */
document.addEventListener('click',function(e){
  const si=e.target.closest('.si');
  if(!si)return;
  const side=si.closest('.side');
  if(!side)return;
  side.querySelectorAll('.si').forEach(i=>i.classList.remove('on'));
  si.classList.add('on');
  const name=si.dataset.name||si.textContent.trim();
  const isAdmin=document.querySelector('.shell').classList.contains('admin-mode');
  document.getElementById('breadcrumb').innerHTML=(isAdmin?'개발자':'홈')+' &rsaquo; <b>'+name+'</b>';
});

/* ───── Tab click + close ───── */
document.addEventListener('click',function(e){
  const tabX=e.target.closest('.tab-x');
  if(tabX){
    e.stopPropagation();const tab=tabX.closest('.tab');if(!tab)return;
    const wasOn=tab.classList.contains('on');const parent=tab.parentElement;tab.remove();
    if(wasOn){const first=parent.querySelector('.tab');if(first)first.classList.add('on');}
    return;
  }
  const tab=e.target.closest('.tab');
  if(!tab)return;
  const parent=tab.parentElement;
  parent.querySelectorAll('.tab').forEach(i=>i.classList.remove('on'));tab.classList.add('on');
});

/* ───── Avatar dropdown ───── */
function toggleAvatarDD(){
  document.getElementById('avatar-dd').classList.toggle('open');
}
document.addEventListener('click',function(e){
  if(!e.target.closest('.avatar-w')){
    document.getElementById('avatar-dd').classList.remove('open');
  }
});

/* ───── Library modal ───── */
function openLib(){
  document.getElementById('lib-backdrop').classList.add('open');
  document.getElementById('lib-modal').classList.add('open');
}
function closeLib(){
  document.getElementById('lib-backdrop').classList.remove('open');
  document.getElementById('lib-modal').classList.remove('open');
}
/* esc 키로 모달 닫기 */
document.addEventListener('keydown',e=>{if(e.key==='Escape')closeLib();});

/* lib category click */
document.querySelectorAll('.lib-cat').forEach(c=>c.addEventListener('click',()=>{
  document.querySelectorAll('.lib-cat').forEach(x=>x.classList.remove('on'));
  c.classList.add('on');
}));
/* lib tag click */
document.querySelectorAll('.lib-tag').forEach(t=>t.addEventListener('click',()=>{
  document.querySelectorAll('.lib-tag').forEach(x=>x.classList.remove('on'));
  t.classList.add('on');
}));
