/* OK ITHAKI V249439 — restored V249290 emergency neighborhood; V249438 automatic Mesh cancelled. */
(function(){'use strict';
  const VER='V249439-restore-v249290-neighborhood-no-auto-mesh';
  const $=id=>document.getElementById(id);
  const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
  const escJs=s=>String(s??'').replace(/\\/g,'\\\\').replace(/'/g,"\\'").replace(/\r?\n/g,'\\n');
  function state(){return (window.ITH&&ITH.state)||window.state||null}
  function toast(t){try{if(window.ITH&&typeof ITH.toast==='function')return ITH.toast(t)}catch(_){}const x=$('toast');if(x){x.textContent=t;x.classList.add('show');setTimeout(()=>x.classList.remove('show'),2800)}}
  function closePanels(){try{window.ITH&&ITH.closePlusMenuV249429&&ITH.closePlusMenuV249429('v249290-neighborhood')}catch(_){}try{const m=$('memberMenu');if(m)m.classList.remove('show','localPanel')}catch(_){}}
  function channel(){return 'space:emergency_neighborhood_mesh_v249270'}
  function spaceId(){return 'emergency_neighborhood_mesh_v249270'}
  function prepare(){const s=state();if(!s)return null;try{if(typeof window.v249270EnsureEmergencySpace==='function')window.v249270EnsureEmergencySpace()}catch(_){}s.currentSpace=spaceId();s.currentSpaceChannel=channel();s.spaceScreen='emergency';s.page='spaces';try{window.ITH&&ITH.wsSubscribe&&ITH.wsSubscribe(s.currentSpaceChannel)}catch(_){}return s}
  const quickTexts=()=>['Είμαι καλά. Χρειάζεται κάποιος βοήθεια;','Χρειάζομαι βοήθεια κοντά μου.','Υπάρχει κάποιος κοντά; Απαντήστε με ένα OK.','Πού υπάρχει ασφαλές σημείο;','Υπάρχει νερό / φαρμακείο / γιατρός κοντά;','Υπάρχει ρεύμα ή σήμα στο τετράγωνο;','Βρήκα ασφαλές σημείο. Μπορώ να βοηθήσω.','Στέλνω φωτογραφία/βίντεο για ενημέρωση.'];
  function setEmergencyTextV249290(text){prepare();const i=$('spaceInput');if(i){i.value=String(text||'');i.focus();try{i.setSelectionRange(i.value.length,i.value.length)}catch(_){}}return false}
  function scrollEmergencyMessagesV249290(){const b=$('spaceMsgs');if(b){b.classList.add('emergencyMsgScrollV249290');b.scrollTop=b.scrollHeight+9999}}
  async function sendEmergencyTextV249290(){prepare();const i=$('spaceInput');if(!i||!String(i.value||'').trim()){toast('Γράψε ένα μήνυμα.');return false}try{const f=(window.ITH&&ITH.sendSpaceText)||window.sendSpaceText;if(typeof f!=='function')throw new Error('send unavailable');await Promise.resolve(f.call(window.ITH||window));setTimeout(scrollEmergencyMessagesV249290,140)}catch(e){toast('Δεν στάλθηκε: '+String(e&&e.message||e))}return false}
  function openEmergencyMediaV249290(kind,camera){prepare();try{const f=(window.ITH&&ITH.openSpaceMedia)||window.openSpaceMedia;if(typeof f==='function')return f.call(window.ITH||window,kind,!!camera);const choose=window.ITH&&ITH.chooseFile;if(typeof choose==='function')return choose('space',kind,channel())}catch(e){toast('Δεν άνοιξε το αρχείο.')}return false}
  function emergencyCloseV249290(){try{if(window.ITH&&typeof ITH.stableRouteV249429==='function')return ITH.stableRouteV249429('home','v249290-exit');if(window.ITH&&typeof ITH.page==='function')return ITH.page('home')}catch(_){}return false}
  function emergencyBackV249290(){try{if(window.ITH&&typeof ITH.stableRouteV249429==='function')return ITH.stableRouteV249429('spaces','v249290-back');if(window.ITH&&typeof ITH.renderSpaces==='function')return ITH.renderSpaces()}catch(_){}return emergencyCloseV249290()}
  function quickButtons(){return quickTexts().map((t,i)=>`<button class="${i===0?'primary':i===1?'warn':''}" data-v249439-quick="${i}">${esc(t.length>34?t.slice(0,34)+'…':t)}</button>`).join('')}
  async function openLegacyNeighborhoodV249439(){
    closePanels();prepare();
    try{if(window.ITH&&typeof ITH.stableRouteV249429==='function')ITH.stableRouteV249429('spaces','v249290-neighborhood');else if(window.ITH&&typeof ITH.page==='function')ITH.page('spaces')}catch(_){}
    await new Promise(r=>setTimeout(r,60));const e=$('spacesPage');if(!e){toast('Δεν βρέθηκε η οθόνη Γειτονιάς.');return false}
    e.innerHTML=`<div class="emergencyShellV249290 legacyNeighborhood439"><div class="emergencyTopV249290"><button class="icon" data-v249439-back title="Πίσω">‹</button><div><b>🆘 Γειτονιά έκτακτης ανάγκης</b><div class="sub">μήνυμα · καρδιές · φωτογραφία · βίντεο · αρχείο</div></div><span class="spacer"></span><span class="emergencyStatusPillV249290">ελεύθερη · χειροκίνητη</span><button class="icon" data-v249439-close title="Έξοδος">×</button></div><div class="emergencyIntroV249290"><b>Ήρεμα, σύντομα, χρήσιμα.</b><p>Η παλιά ελεύθερη οθόνη V249290. Δεν ξεκινά αυτόματα Mesh ή μικρο-server. Γράψε μήνυμα ή διάλεξε έτοιμο κείμενο.</p></div><div class="emergencyQuickGridV249290">${quickButtons()}</div><div class="emergencyChatV249290"><div id="spaceMsgs" class="messages emergencyMsgScrollV249290"><div class="card">Φόρτωση μηνυμάτων γειτονιάς...</div></div><div class="emergencyComposerV249290"><button title="Φωτογραφία" data-v249439-media="photo">🖼️</button><button title="Βίντεο" data-v249439-media="video">🎥</button><button title="Αρχείο" data-v249439-media="file">📎</button><textarea id="spaceInput" placeholder="Γράψε σύντομα ή διάλεξε έτοιμο κείμενο..."></textarea><button class="send" data-v249439-send>➤</button></div><div class="emergencyMediaNoteV249290"><b>Σημείωση:</b> Τα μηνύματα χρησιμοποιούν την υπάρχουσα σύνδεση ΙΘΑΚΗΣ ή έναν τοπικό server που ανοίγεις εσύ. Καμία υπηρεσία Mesh δεν ξεκινά μόνη της.</div></div></div>`;
    e.querySelector('[data-v249439-back]').onclick=emergencyBackV249290;e.querySelector('[data-v249439-close]').onclick=emergencyCloseV249290;e.querySelector('[data-v249439-send]').onclick=sendEmergencyTextV249290;
    e.querySelectorAll('[data-v249439-quick]').forEach(b=>b.onclick=()=>setEmergencyTextV249290(quickTexts()[Number(b.dataset.v249439Quick||0)]));e.querySelectorAll('[data-v249439-media]').forEach(b=>b.onclick=()=>openEmergencyMediaV249290(b.dataset.v249439Media,false));
    try{const f=(window.ITH&&ITH.loadSpaceMessages)||window.loadSpaceMessages;if(typeof f==='function')await Promise.resolve(f.call(window.ITH||window,true));else throw new Error('load unavailable')}catch(_){const b=$('spaceMsgs');if(b)b.innerHTML='<div class="card"><b>Δεν φορτώθηκαν μηνύματα.</b><div class="date">Έλεγξε την κανονική σύνδεση ή τον τοπικό server.</div></div>'}
    installMessageObserver();setTimeout(()=>{decorateAll();scrollEmergencyMessagesV249290()},120);return false
  }
  async function reaction(mid,emoji,btn){const s=state()||{};btn.disabled=true;try{const r=await fetch('/api/reactions/toggle',{method:'POST',headers:{'Content-Type':'application/json'},credentials:'same-origin',body:JSON.stringify({target_id:'space_message:'+mid,device_id:String(s.device||localStorage.getItem('ith_device_id')||'anonymous'),emoji,nickname:String(s.name||localStorage.getItem('ith_nickname')||'Μέλος')})});if(!r.ok)throw new Error(String(r.status));const d=await r.json();btn.classList.toggle('active',!!d.active);btn.querySelector('small').textContent=String(Number(d.count||0))}catch(_){toast('Δεν καταγράφηκε η αντίδραση.')}finally{btn.disabled=false}return false}
  async function loadCounts(b,mid){try{const r=await fetch('/api/reactions/list?target_id='+encodeURIComponent('space_message:'+mid),{cache:'no-store',credentials:'same-origin'});if(!r.ok)return;const d=await r.json(),m={};(d.reactions||[]).forEach(x=>m[String(x.emoji||'')]=Number(x.count||0));b.querySelectorAll('[data-legacy-reaction439]').forEach(q=>q.querySelector('small').textContent=String(m[q.dataset.legacyReaction439]||0))}catch(_){}}
  function messageId(b){let mid=String(b.dataset.spaceMessageId||'');if(mid)return mid;const x=[...b.querySelectorAll('button')].map(q=>q.getAttribute('onclick')||'').find(v=>/pinSpaceMessageV249281/.test(v));const m=x&&x.match(/pinSpaceMessageV249281\(['"]([^'"]+)/);return m?m[1]:''}
  function decorate(b){if(!b||b.dataset.legacyReactions439==='1')return;const mid=messageId(b);if(!mid)return;b.dataset.legacyReactions439='1';const row=document.createElement('div');row.className='legacyReactionRow439';row.innerHTML=['❤️','🤝','🙏'].map(x=>`<button type="button" data-legacy-reaction439="${x}"><span>${x}</span><small>0</small></button>`).join('');row.onclick=e=>{const q=e.target.closest('[data-legacy-reaction439]');if(q)reaction(mid,q.dataset.legacyReaction439,q)};b.appendChild(row);loadCounts(b,mid)}
  function decorateAll(){document.querySelectorAll('.legacyNeighborhood439 #spaceMsgs .bubble').forEach(decorate)}
  function installMessageObserver(){const b=$('spaceMsgs');if(b&&!b.__legacyObserver439){b.__legacyObserver439=new MutationObserver(decorateAll);b.__legacyObserver439.observe(b,{childList:true,subtree:true})}}
  function css(){if($('legacyNeighborhoodCss439'))return;const s=document.createElement('style');s.id='legacyNeighborhoodCss439';s.textContent=`.emergencyShellV249290{height:calc(100dvh - 76px);min-height:480px;display:flex;flex-direction:column;gap:9px;padding:10px;box-sizing:border-box;overflow:hidden;background:linear-gradient(180deg,rgba(4,15,27,.98),rgba(2,8,15,.99))}.emergencyTopV249290{display:flex;align-items:center;gap:9px;padding:9px 10px;border-radius:18px;background:rgba(255,255,255,.055);border:1px solid rgba(255,255,255,.1)}.emergencyTopV249290 .icon{width:42px;height:42px;border-radius:14px;border:1px solid rgba(255,255,255,.15);background:rgba(255,255,255,.07);color:#fff;font-size:23px}.emergencyTopV249290 b{color:#ffe39b}.emergencyTopV249290 .sub{font-size:11px;color:#a9bed0}.emergencyTopV249290 .spacer{flex:1}.emergencyStatusPillV249290{border-radius:999px;padding:6px 9px;background:rgba(53,231,164,.12);border:1px solid rgba(53,231,164,.35);color:#98f4cf;font-weight:900;font-size:11px}.emergencyIntroV249290{padding:11px 13px;border-radius:17px;background:rgba(255,217,110,.07);border:1px solid rgba(255,217,110,.16)}.emergencyIntroV249290 p{margin:4px 0 0;color:#bdd0df;font-size:12px}.emergencyQuickGridV249290{display:flex;gap:6px;overflow-x:auto;padding:1px 0 4px}.emergencyQuickGridV249290 button{flex:0 0 auto;max-width:240px;border-radius:999px;padding:8px 11px;border:1px solid rgba(255,255,255,.13);background:rgba(255,255,255,.065);color:#fff}.emergencyQuickGridV249290 button.primary{background:rgba(53,231,164,.15)}.emergencyQuickGridV249290 button.warn{background:rgba(255,100,110,.14)}.emergencyChatV249290{flex:1;min-height:0;display:flex;flex-direction:column;border:1px solid rgba(255,255,255,.1);border-radius:20px;overflow:hidden}.emergencyMsgScrollV249290{flex:1;min-height:0;overflow-y:auto!important;padding:10px;overscroll-behavior:contain;-webkit-overflow-scrolling:touch}.emergencyComposerV249290{display:grid;grid-template-columns:auto auto auto 1fr auto;gap:7px;padding:9px;border-top:1px solid rgba(255,255,255,.1);background:rgba(255,255,255,.045);align-items:end}.emergencyComposerV249290 textarea{width:100%;min-height:42px;max-height:110px;resize:none;border-radius:18px;border:1px solid rgba(255,255,255,.14);background:rgba(0,0,0,.24);color:#fff;padding:10px 12px;box-sizing:border-box}.emergencyComposerV249290 button{min-height:42px;border-radius:16px;border:1px solid rgba(255,255,255,.14);background:rgba(255,255,255,.075);color:#fff;padding:9px}.emergencyComposerV249290 button.send{background:linear-gradient(135deg,#2eea9b,#ffe071);color:#06131e;border:0}.emergencyMediaNoteV249290{padding:0 10px 8px;font-size:11px;color:#9fb5c7}.legacyReactionRow439{display:flex;gap:7px;align-items:center;margin-top:7px}.legacyReactionRow439 button{display:inline-flex;gap:4px;align-items:center;min-height:31px;padding:4px 9px;border-radius:999px;border:1px solid rgba(255,255,255,.14);background:rgba(255,255,255,.055);color:#fff}.legacyReactionRow439 button.active{background:rgba(255,69,112,.17);border-color:rgba(255,102,137,.55)}.legacyReactionRow439 small{color:#ffd897}@media(max-width:760px){.emergencyShellV249290{height:calc(100dvh - 58px);padding:7px;gap:7px}.emergencyIntroV249290{display:none}.emergencyComposerV249290{grid-template-columns:auto auto auto 1fr auto;gap:5px;padding:7px}.emergencyStatusPillV249290{display:none}}`;document.head.appendChild(s)}
  function install(){css();window.ITH=window.ITH||{};Object.assign(window.ITH,{openLegacyNeighborhoodV249439,setEmergencyTextV249290,sendEmergencyTextV249290,openEmergencyMediaV249290,scrollEmergencyMessagesV249290,emergencyBackV249290,emergencyCloseV249290});document.documentElement.setAttribute('data-ithaki-v249439',VER);decorateAll()}
  install();new MutationObserver(install).observe(document.documentElement,{childList:true,subtree:true});setInterval(install,900);
})();
