/* Bernard Health SeniorCare V1.6.7 — navegación estable y permanencia de pantalla */
(function(){
  'use strict';

  const stateActionSelector=[
    '[data-v163-suspend]','[data-v163-reactivate]','[data-v163-delete]',
    '[data-v162-suspend]','[data-v162-reactivate]','[data-v162-delete]',
    '[data-reactivate-resident]','#residentDeactivateButton',
    '[data-toggle-user]','[data-admin-toggle]','[data-atoggle]'
  ].join(',');

  function restoreScroll(top){
    [0,80,220,500].forEach(delay=>setTimeout(()=>window.scrollTo({top,behavior:'auto'}),delay));
  }

  document.addEventListener('click',event=>{
    const back=event.target.closest?.('[data-back],#v163Back');
    if(back){
      event.preventDefault();
      event.stopImmediatePropagation();
      window.goBack?.();
      return;
    }

    const action=event.target.closest?.(stateActionSelector);
    if(action){
      const top=window.scrollY||0;
      restoreScroll(top);
    }
  },true);
})();
