        return;
    }

    if(k === 'Enter'){
        e.preventDefault();
        enterItem();
        return;
    }
}, true);

setTimeout(setFocus, 500);
</script>

<script>
function quickPlay(row){
    const video=document.getElementById('mainVideo');
    if(!row || !video) return false;

    const url=row.getAttribute('data-play');

    if(!url) return false;

    document.querySelectorAll('.row').forEach(r=>r.classList.remove('active'));
    row.classList.add('active');

    if(typeof updateTopFromRow === 'function'){
        updateTopFromRow(row, true);
    }

/* FOCO_PLAYER_AUTOMATICO_FINAL */
setTimeout(function(){
    document.querySelectorAll('.focus').forEach(function(x){x.classList.remove('focus');});
    var box=document.querySelector('.videoBox');
    if(box){
        box.classList.add('focus');
        box.setAttribute('tabindex','0');
        box.focus();
    }
},250);

    video.muted = false;
    video.volume = 1.0;
    if(video.src !== url){
        video.src = url;
    }
    video.play().catch(()=>{});

    return true;
}

document.addEventListener('click',function(e){
    const row=e.target.closest('.row');
    if(row){
        e.preventDefault();
        quickPlay(row);
    }
},true);
</script>

<script>
document.addEventListener('DOMContentLoaded',function(){
    const v=document.getElementById('mainVideo');
    if(v){
        v.muted=false;
        v.volume=1.0;
        v.addEventListener('dblclick',function(){
            if(v.requestFullscreen) v.requestFullscreen();
            else if(v.webkitRequestFullscreen) v.webkitRequestFullscreen();
        });
    }
});
</script>

<script>
let lastRow = null;
let lastTime = 0;

function abrirFullscreenActual(){
    const v=document.getElementById('mainVideo');
    const focused=document.querySelector('.focus.row');
    const active=document.querySelector('.row.active');

    let url = "";

    if(focused && focused.getAttribute('data-play')) url = focused.getAttribute('data-play');
    else if(active && active.getAttribute('data-play')) url = active.getAttribute('data-play');
    else if(v && v.currentSrc) url = v.currentSrc;
    else if(v && v.src) url = v.src;

    if(!url) return;

    if(window.PuntozAndroid && PuntozAndroid.abrirFullscreen){
        PuntozAndroid.abrirFullscreen(url);
        return;
    }

    if(v.requestFullscreen) v.requestFullscreen();
    else if(v.webkitRequestFullscreen) v.webkitRequestFullscreen();
}

document.addEventListener('keydown',function(e){
    if(e.key !== 'Enter') return;

    const el=document.querySelector('.focus');

    if(el && el.classList.contains('videoBox')){
        e.preventDefault();
        abrirFullscreenActual();
        return;
    }

    if(el && el.classList.contains('row')){
        const now=Date.now();

        if(lastRow === el && (now-lastTime) < 700){
            e.preventDefault();
            if(typeof quickPlay === 'function') quickPlay(el);
            setTimeout(abrirFullscreenActual,250);
            return;
        }

        lastRow = el;
        lastTime = now;
    }
},true);

document.addEventListener('dblclick',function(e){
    const row=e.target.closest('.row');
    if(row){
        if(typeof quickPlay === 'function') quickPlay(row);
        setTimeout(abrirFullscreenActual,250);
    }
},true);
</script>





<script>
/* GLOW PROGRAMA SIGUE AL CANAL NAVEGADO */
(function(){
  function syncProgGlow(){
    document.querySelectorAll('.prog36.focusProg').forEach(function(x){
      x.classList.remove('focusProg');
    });

    var row = document.querySelector('.row36.focus');
    if(!row) return;

    var prog = row.querySelector('.prog36.nowProg');
    if(!prog) prog = row.querySelector('.prog36');

    if(prog) prog.classList.add('focusProg');
  }

  document.addEventListener('keydown', function(){
    setTimeout(syncProgGlow, 60);
  }, true);

  document.addEventListener('click', function(){
    setTimeout(syncProgGlow, 60);
  }, true);

  setTimeout(syncProgGlow, 600);
})();
</script>



<script>
/* TOP PANEL SIEMPRE SIGUE CANAL ENFOCADO - OPCION A */
let topStartTsLive = <?=intval($startTs)?>;
let topEndTsLive = <?=intval($endTs)?>;
let topCurrentRow = null;
let topRefreshBusy = false;
let topLastRefresh = 0;

function pzHora(ts){
    ts = parseInt(ts || 0, 10);
    if(!ts) return "";
    const d = new Date(ts * 1000);
    return String(d.getHours()).padStart(2,'0') + ":" + String(d.getMinutes()).padStart(2,'0');
}

function updateTopFromRow(row, alsoActive){
    if(!row) return;

    const title = document.getElementById('channelTitle');
    const nowBox = document.querySelector('.epgProgramNow');
    const nextBox = document.querySelector('.epgProgramNext');
    const timeNow = document.querySelector('.epgTimeSimple');
    const timeNext = document.querySelector('.epgNextTime');

    const name = row.getAttribute('data-name') || 'Canal';
    const nowTitle = row.getAttribute('data-now') || 'Programación en Actualización';
    const nextTitle = row.getAttribute('data-next') || 'Programación en Actualización';

    const st = parseInt(row.getAttribute('data-start') || '0', 10);
    const en = parseInt(row.getAttribute('data-end') || '0', 10);
    const ns = parseInt(row.getAttribute('data-next-start') || '0', 10);
    const ne = parseInt(row.getAttribute('data-next-end') || '0', 10);

    if(title) title.textContent = name;
    if(nowBox) nowBox.textContent = nowTitle;
    if(nextBox) nextBox.textContent = nextTitle;
    if(timeNow) timeNow.textContent = (st && en) ? (pzHora(st) + " - " + pzHora(en)) : "";
    if(timeNext) timeNext.textContent = (ns && ne) ? (pzHora(ns) + " - " + pzHora(ne)) : "";

    topStartTsLive = st;
    topEndTsLive = en;
    topCurrentRow = row;

    updateTopEpgProgress();
}


async function refreshTopEpgFromServer(row){
    if(!row || topRefreshBusy) return;

    const sid = row.getAttribute('data-stream-id') || '';
    if(!sid) return;

    const nowMs = Date.now();
    if(nowMs - topLastRefresh < 10000) return;

    topRefreshBusy = true;
    topLastRefresh = nowMs;

    try{
        const url = 'tv_clean.php?ajax_epg=1&stream_id=' + encodeURIComponent(sid) + '&_=' + Date.now();
        const r = await fetch(url, {cache:'no-store'});
        const j = await r.json();

        row.setAttribute('data-now', j.now || 'Programacion en Actualizacion');
        row.setAttribute('data-next', j.next || 'Programacion en Actualizacion');
        row.setAttribute('data-start', j.start || 0);
        row.setAttribute('data-end', j.end || 0);
        row.setAttribute('data-next-start', j.next_start || 0);
        row.setAttribute('data-next-end', j.next_end || 0);

        updateTopFromRow(row, false);
    }catch(e){}
    topRefreshBusy = false;
}

function updateTopEpgProgress(){
    const fill = document.getElementById('epgBarFillLive');
    const remain = document.getElementById('epgRemainLive');

    if(!fill || !remain || !topStartTsLive || !topEndTsLive || topEndTsLive <= topStartTsLive){
