(function(){
'use strict';
function isMobile(){
return !!(window.matchMedia&&window.matchMedia('(max-width: 900px)').matches);
}
function prefersReducedMotion(){
return !!(window.matchMedia&&window.matchMedia('(prefers-reduced-motion: reduce)').matches);
}
function getHeaderOffset(){
var offset=16;
var selectors=['#wpadminbar','header','.site-header','.elementor-location-header','[data-elementor-type="header"]'];
selectors.forEach(function(selector){
document.querySelectorAll(selector).forEach(function(el){
var style=window.getComputedStyle(el);
if(style.display==='none'||style.visibility==='hidden') return;
if(style.position!=='fixed'&&style.position!=='sticky') return;
var rect=el.getBoundingClientRect();
if(rect.height > 0&&rect.bottom > 0&&rect.top <=4){
offset=Math.max(offset, Math.ceil(rect.bottom) + 12);
}});
});
return offset;
}
function findCoursePanel(root, slug){
var found=null;
root.querySelectorAll('[data-course-panel]').forEach(function(panel){
if(!found&&panel.getAttribute('data-course-panel')===slug) found=panel;
});
return found||root.querySelector('.xncm-detail-section');
}
function scrollToCourseDetail(root, slug){
var target=findCoursePanel(root, slug);
if(!target) return;
function run(behavior){
var currentY=window.pageYOffset||document.documentElement.scrollTop||0;
var top=target.getBoundingClientRect().top + currentY - getHeaderOffset();
try {
window.scrollTo({top:Math.max(0, Math.round(top)), behavior:behavior});
} catch(e){
window.scrollTo(0, Math.max(0, Math.round(top)));
}}
window.requestAnimationFrame(function(){
window.requestAnimationFrame(function(){
run(prefersReducedMotion() ? 'auto':'smooth');
window.setTimeout(function(){
if(Math.abs(target.getBoundingClientRect().top - getHeaderOffset()) > 18) run('auto');
}, 650);
});
});
}
function setActiveCourse(root, slug, card, shouldScroll){
root.querySelectorAll('[data-course-card]').forEach(function(el){
var active=el.getAttribute('data-course-card')===slug;
el.classList.toggle('is-active', active);
el.setAttribute('aria-selected', active ? 'true':'false');
el.setAttribute('tabindex', active ? '0':'-1');
});
root.querySelectorAll('[data-course-panel]').forEach(function(el){
var active=el.getAttribute('data-course-panel')===slug;
el.classList.toggle('is-active', active);
if(active){ el.removeAttribute('hidden'); }else{ el.setAttribute('hidden','hidden'); }});
if(card&&isMobile()){
try { card.scrollIntoView({behavior:prefersReducedMotion()?'auto':'smooth', inline:'center', block:'nearest'});} catch(e){}}
window.setTimeout(initCategorySwipeHints, 80);
if(shouldScroll) scrollToCourseDetail(root, slug);
}
function setActiveCategory(panel, id, tab){
panel.querySelectorAll('[data-category-tab]').forEach(function(el){
var active=el.getAttribute('data-category-tab')===id;
el.classList.toggle('is-active', active);
el.setAttribute('aria-selected', active ? 'true':'false');
el.setAttribute('tabindex', active ? '0':'-1');
});
panel.querySelectorAll('[data-category-panel]').forEach(function(el){
var active=el.getAttribute('data-category-panel')===id;
el.classList.toggle('is-active', active);
if(active){ el.removeAttribute('hidden'); }else{ el.setAttribute('hidden','hidden'); }});
if(tab&&isMobile()){
try { tab.scrollIntoView({behavior:prefersReducedMotion()?'auto':'smooth', inline:'center', block:'nearest'});} catch(e){}}
}
function syncTheme(root){
var html=document.documentElement;
var stored='';
try { stored=localStorage.getItem('xnhs2Theme')||localStorage.getItem('xncmTheme')||''; } catch(e){}
var global=stored||html.getAttribute('data-xnhs-theme')||html.getAttribute('data-xncm-theme')||'';
var own=root.getAttribute('data-theme')||'';
var dark=own==='dark'||global==='dark';
root.classList.toggle('xncm-theme-dark', dark);
root.classList.toggle('xncm-theme-light', !dark);
}
document.addEventListener('click', function(ev){
var card=ev.target.closest&&ev.target.closest('[data-course-card]');
if(card){
var root=card.closest('[data-xn-course-menu]');
if(root){ setActiveCourse(root, card.getAttribute('data-course-card'), card, true); }
return;
}
var tab=ev.target.closest&&ev.target.closest('[data-category-tab]');
if(tab){
var panel=tab.closest('[data-course-panel]');
if(panel){ setActiveCategory(panel, tab.getAttribute('data-category-tab'), tab); }}
}, true);
document.addEventListener('keydown', function(ev){
if(ev.key!=='Enter'&&ev.key!==' ') return;
var card=ev.target.closest&&ev.target.closest('[data-course-card]');
if(card){ ev.preventDefault(); card.click(); }}, true);
function showSwipeHint(root, tabs, shell){
if(shell.getAttribute('data-xncm-hint-shown')==='1') return;
var text=root.getAttribute('data-category-swipe-hint-text')||'← 左右にスワイプ →';
var seconds=parseInt(root.getAttribute('data-category-swipe-hint-duration')||'4', 10);
if(!isFinite(seconds)) seconds=4;
seconds=Math.max(1, Math.min(15, seconds));
var old=shell.querySelector('.xncm-category-swipe-hint');
if(old) old.remove();
var hint=document.createElement('span');
hint.className='xncm-category-swipe-hint';
hint.setAttribute('aria-hidden','true');
hint.textContent=text;
shell.appendChild(hint);
shell.setAttribute('data-xncm-hint-shown','1');
window.requestAnimationFrame(function(){
window.requestAnimationFrame(function(){ hint.classList.add('is-visible'); });
});
var hidden=false;
var timer=window.setTimeout(hide, seconds * 1000);
function hide(){
if(hidden) return;
hidden=true;
window.clearTimeout(timer);
hint.classList.remove('is-visible');
tabs.removeEventListener('scroll', hide);
tabs.removeEventListener('touchstart', hide);
tabs.removeEventListener('pointerdown', hide);
window.setTimeout(function(){ if(hint.parentNode) hint.parentNode.removeChild(hint); }, 420);
}
tabs.addEventListener('scroll', hide, {passive:true});
tabs.addEventListener('touchstart', hide, {passive:true});
tabs.addEventListener('pointerdown', hide, {passive:true});
}
function prepareHint(root, tabs){
if(tabs.offsetParent===null||tabs.clientWidth < 1||tabs.children.length < 2) return;
var shell=tabs.parentElement;
if(!shell||!shell.classList.contains('xncm-category-tabs-shell')){
shell=document.createElement('div');
shell.className='xncm-category-tabs-shell';
tabs.parentNode.insertBefore(shell, tabs);
shell.appendChild(tabs);
}
if(shell._xncmHintObserver){
try { shell._xncmHintObserver.disconnect(); } catch(e){}
shell._xncmHintObserver=null;
}
if('IntersectionObserver' in window){
shell._xncmHintObserver=new IntersectionObserver(function(entries, observer){
entries.forEach(function(entry){
if(entry.isIntersecting){
showSwipeHint(root, tabs, shell);
observer.disconnect();
}});
}, {threshold:0.08, rootMargin:'0px 0px -8% 0px'});
shell._xncmHintObserver.observe(shell);
}else{
showSwipeHint(root, tabs, shell);
}}
function initCategorySwipeHints(){
if(!isMobile()) return;
document.querySelectorAll('[data-xn-course-menu]').forEach(function(root){
if(root.getAttribute('data-category-swipe-hint-enabled')==='0') return;
root.querySelectorAll('.xncm-category-tabs').forEach(function(tabs){ prepareHint(root, tabs); });
});
}
function init(){
document.querySelectorAll('[data-xn-course-menu]').forEach(function(root){
syncTheme(root);
root.addEventListener('xncm:coursechange', function(){ window.setTimeout(initCategorySwipeHints, 80); });
});
initCategorySwipeHints();
window.addEventListener('load', function(){ window.setTimeout(initCategorySwipeHints, 80); });
window.addEventListener('resize', function(){ window.setTimeout(initCategorySwipeHints, 120); });
try {
new MutationObserver(function(){
document.querySelectorAll('[data-xn-course-menu]').forEach(syncTheme);
}).observe(document.documentElement,{attributes:true,attributeFilter:['data-xnhs-theme','data-xncm-theme']});
} catch(e){}
window.addEventListener('storage', function(){ document.querySelectorAll('[data-xn-course-menu]').forEach(syncTheme); });
}
if(document.readyState==='loading') document.addEventListener('DOMContentLoaded', init); else init();
})();