/* OK ITHAKI V249526R2 — WALL / SHARE / SINGLE HOME RUNTIME FIX */
(function(){
'use strict';
if(window.__ITHAKI_V249526R2_OWNER__)return;
window.__ITHAKI_V249526R2_OWNER__={version:'V249526R2',wall:true,share:true,singleHome:true,at:Date.now()};
const I=()=>window.ITH=window.ITH||{}; const S=()=>window.state||I().state||{}; const $=id=>document.getElementById(id);
const esc=s=>String(s==null?'':s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
function toast(m,ms=5500){try{if(typeof I().toast==='function')return I().toast(String(m),ms)}catch(e){}const t=$('toast');if(t){t.textContent=String(m);t.classList.add('show');clearTimeout(toast._t);toast._t=setTimeout(()=>t.classList.remove('show'),ms)}}
function room(){return String(S().room||localStorage.getItem('ith_room')||'ithaki-phone-test')}
function device(){let d=String(S().device||localStorage.getItem('ith_device')||localStorage.getItem('ith_device_id')||'');if(!d){d='web_'+Math.random().toString(36).slice(2)+Date.now().toString(36);try{localStorage.setItem('ith_device',d);localStorage.setItem('ith_device_id',d)}catch(e){}}return d}
function apiBase(){let b=String(S().api||'').replace(/\/+$/,'');if(!b||/appassets\.androidplatform\.net/i.test(b)){try{const n=window.OKITHAKI_ANDROID||window.AndroidBridge;if(n&&typeof n.getServerBase==='function')b=String(n.getServerBase()||'').replace(/\/+$/,'')}catch(e){}}return b||(/^https?:/i.test(location.origin)?location.origin:'https://ok-ithaki.gr')}
function fullUrl(u){try{return new URL(String(u||''),apiBase()+'/').toString()}catch(e){return String(u||'')}}
async function request(path,opt={}){const r=await fetch(/^https?:/i.test(path)?path:apiBase()+path,Object.assign({cache:'no-store',credentials:'include'},opt));let d=null;try{d=await r.json()}catch(e){}if(!r.ok||d&&d.ok===false)throw Error(String(d&&d.message||d&&d.reason||'HTTP '+r.status));return d||{ok:true}}

/* Active anonymous wall V249345 first, then safe fallbacks. */
function normalizeWall(d){const a=Array.isArray(d)?d:(d&&Array.isArray(d.posts)?d.posts:(d&&Array.isArray(d.items)?d.items:[]));return a.map(p=>({id:String(p.post_id||p.id||''),text:String(p.text||p.body||''),created:Number(p.created_at||p.created||0),category:String(p.category||'thought')})).filter(p=>p.id&&p.text)}
async function wallLoad(){let last;for(const p of ['/api/wall/list_v249345','/api/wall/list_v249344','/api/wall/list_v249339','/api/wall/list']){try{return normalizeWall(await request(p+'?room_id='+encodeURIComponent(room())+'&limit=120&_v='+Date.now()))}catch(e){last=e}}throw last||Error('Ο Τοίχος δεν απάντησε')}
async function wallPost(text){let last;for(const p of ['/api/wall/post_v249345','/api/wall/post_v249344','/api/wall/post_v249339','/api/wall/post']){try{return await request(p,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({room_id:room(),owner_token:device(),device_id:device(),text:String(text||'').trim()})})}catch(e){last=e}}throw last||Error('Η δημοσίευση δεν στάλθηκε')}
function patchWall(){const open=I().openAnonymousWallV249526||window.openAnonymousWallV249526;if(typeof open!=='function')return;I().loadAnonymousWallV249526R2=wallLoad;I().postAnonymousWallV249526R2=wallPost;
 document.addEventListener('click',async function(e){const send=e.target&&e.target.closest&&e.target.closest('#ith526WallSend');if(!send)return;e.preventDefault();e.stopImmediatePropagation();const ta=$('ith526WallText'),text=String(ta&&ta.value||'').trim();if(!text)return toast('Γράψε πρώτα τη σκέψη σου.');send.disabled=true;try{await wallPost(text);ta.value='';const c=$('ith526WallCount');if(c)c.textContent='0/500';const items=await wallLoad();const list=$('ith526WallList');if(list){list.innerHTML=items.map(p=>'<article class="ith526WallCard" data-wall="'+esc(p.id)+'"><header><span class="ith526AnonAvatar">◌</span><div><b>Ανώνυμη φωνή</b></div></header><p>'+esc(p.text)+'</p></article>').join('')||'<section class="ith526WallEmpty"><b>Ο χώρος περιμένει την πρώτη ανώνυμη φωνή.</b></section>'}toast('Η ανώνυμη σκέψη δημοσιεύτηκε.')}catch(x){toast('Δεν στάλθηκε: '+String(x.message||x))}finally{send.disabled=false}},true);
 document.addEventListener('click',function(e){const b=e.target&&e.target.closest&&e.target.closest('[data-act="anon_wall"],#ith526PlusWall');if(!b)return;setTimeout(async()=>{try{const items=await wallLoad(),list=$('ith526WallList');if(list)list.innerHTML=items.map(p=>'<article class="ith526WallCard" data-wall="'+esc(p.id)+'"><header><span class="ith526AnonAvatar">◌</span><div><b>Ανώνυμη φωνή</b></div></header><p>'+esc(p.text)+'</p></article>').join('')||'<section class="ith526WallEmpty"><b>Ο χώρος περιμένει την πρώτη ανώνυμη φωνή.</b></section>'}catch(x){}},100)},true)}

/* One share chooser, accepting every known friends response and sender owner. */
function normalizeFriends(v){if(Array.isArray(v))return v;if(v&&Array.isArray(v.items))return v.items;if(v&&Array.isArray(v.friends))return v.friends;if(v&&Array.isArray(v.people))return v.people;return[]}
async function loadFriends(){for(const fn of [I().loadServerFriendsV249429,I().loadServerFriends,I().getFriends,window.loadServerFriendsV249429,window.loadServerFriends]){if(typeof fn==='function'){try{const a=normalizeFriends(await fn.call(I(),true));if(a.length)return a}catch(e){}}}return[]}
function peerId(p){return String(p.device_id||p.peer_id||p.member_id||p.user_id||p.id||'')}
async function sendFriend(peer,media){const channel='dm:'+[device(),String(peer)].sort().join(':');const payload=JSON.stringify({body:'',media});for(const fn of [window.sendMessagePayload,I().sendMessagePayload,window.sendDMPayload,I().sendDMPayload]){if(typeof fn==='function'){await fn.call(I(),channel,payload);return true}}throw Error('Η αποστολή μηνύματος δεν είναι διαθέσιμη.')}
async function externalShare(media,blob){const n=window.OKITHAKI_ANDROID||window.AndroidBridge;try{if(blob&&n&&typeof n.shareBase64FileV249523==='function'){const data=await new Promise((res,rej)=>{const r=new FileReader();r.onload=()=>res(String(r.result).split(',')[1]||'');r.onerror=rej;r.readAsDataURL(blob)});const out=String(n.shareBase64FileV249523(media.filename,media.mime_type,data)||'');if(/^(ok|queued)/i.test(out))return true}if(!blob&&n&&typeof n.shareRemoteFileV249524==='function'){const out=String(n.shareRemoteFileV249524(fullUrl(media.url),media.filename,media.mime_type,'',device(),room())||'');if(/^(ok|queued)/i.test(out))return true}}catch(e){}
 let file=blob;if(!file){const r=await fetch(fullUrl(media.url),{cache:'no-store',credentials:'include'});if(!r.ok)throw Error('HTTP '+r.status);file=await r.blob()}const f=new File([file],media.filename||'ITHAKI_file',{type:media.mime_type||file.type||'application/octet-stream'});if(navigator.share&&(!navigator.canShare||navigator.canShare({files:[f]}))){await navigator.share({files:[f],title:'OK ITHAKI'});return true}const a=document.createElement('a');a.href=URL.createObjectURL(f);a.download=f.name;document.body.appendChild(a);a.click();setTimeout(()=>{URL.revokeObjectURL(a.href);a.remove()},1500);toast('Το αρχείο αποθηκεύτηκε επειδή δεν υπάρχει διαθέσιμη εξωτερική κοινοποίηση.');return true}
async function shareChoice(media,blob){$('ith526R2ShareChoice')?.remove();const d=document.createElement('div');d.id='ith526R2ShareChoice';d.innerHTML='<section><header><button data-close>‹</button><b>Κοινοποίηση</b><button data-close>×</button></header><button class="external" data-external>↗ Εξωτερικά</button><div class="label">Αποστολή σε φίλο ΙΘΑΚΗΣ</div><div class="friends"><div class="card">Φόρτωση φίλων…</div></div></section>';document.body.appendChild(d);d.onclick=e=>{if(e.target===d||e.target.closest('[data-close]'))d.remove()};d.querySelector('[data-external]').onclick=async()=>{try{await externalShare(media,blob);d.remove()}catch(e){toast('Η εξωτερική κοινοποίηση απέτυχε: '+String(e.message||e))}};const a=await loadFriends(),box=d.querySelector('.friends');box.innerHTML=a.length?a.map(p=>{const id=peerId(p),name=String(p.nickname||p.name||p.display_name||'Φίλος');return id?'<button class="friend" data-peer="'+esc(id)+'"><span class="avatar">'+esc((name[0]||'Μ').toUpperCase())+'</span><span><b>'+esc(name)+'</b><small>Αποστολή στο μήνυμα</small></span><i>➤</i></button>':''}).join(''):'<div class="card">Δεν βρέθηκαν αποδεκτοί φίλοι.</div>';box.onclick=async e=>{const b=e.target.closest('[data-peer]');if(!b)return;b.disabled=true;try{let m=media;if(blob){if(typeof window.uploadWithProgress!=='function')throw Error('Δεν είναι διαθέσιμη η μεταφόρτωση αρχείου.');const f=blob instanceof File?blob:new File([blob],media.filename,{type:media.mime_type});const up=await window.uploadWithProgress(f,'file',true);m={url:up.url||up.file_url,filename:up.filename||media.filename,mime_type:up.mime_type||media.mime_type,kind:media.kind||'file',size:up.size_bytes||f.size,serverStored:true}}await sendFriend(b.dataset.peer,m);d.remove();toast('Στάλθηκε στον φίλο.')}catch(x){toast('Δεν στάλθηκε στον φίλο: '+String(x.message||x))}finally{b.disabled=false}}}
function patchShare(){const o=I();o.shareActualFileV249526R2=(url,name='ITHAKI_file',mime='application/octet-stream')=>shareChoice({url:fullUrl(url),filename:name||'ITHAKI_file',mime_type:mime||'application/octet-stream',kind:'file'},null);o.shareBlobV249526R2=(blob,name='ITHAKI_file',mime='')=>shareChoice({url:'',filename:name,mime_type:mime||blob.type||'application/octet-stream',kind:'file'},blob);o.shareActualFileV249524=o.shareActualFileV249526R2;o.shareActualFileV249523=o.shareActualFileV249526R2;o.shareBlobV249524=o.shareBlobV249526R2;o.shareBlobV249523=o.shareBlobV249526R2;o.shareMediaFile=(url,name='',mime='')=>{o.shareActualFileV249526R2(url,name||'ITHAKI_file',mime);return false};window.shareMediaFile=o.shareMediaFile}

/* Keep exactly one welcome block and release the old boot mask permanently. */
function stableHome(){document.documentElement.removeAttribute('data-ithaki-v249475-boot');window.__ITHAKI_V249475_NORMAL_BOOT__=false;const h=$('homePage');if(!h)return;const all=[...h.querySelectorAll('.welcomeHero')];all.forEach((x,i)=>{x.dataset.ith526CanonicalHome=i===0?'1':'0';if(i>0)x.remove()});const w=all[0]||h.querySelector('.homeWelcomePro');if(w){const title=w.querySelector('h1');if(title)title.innerHTML='Καλώς ήρθατε στην <span class="okLogo">OK</span> <span class="ithakiLogo">ITHAKI</span>';const sub=w.querySelector('h3');if(sub)sub.textContent='Ταξιδεύουμε μαζί ανθρώπινα, ελεύθερα και ανώνυμα.'}h.classList.add('ith526SingleHome')}
function css(){if($('ith526R2Css'))return;const s=document.createElement('style');s.id='ith526R2Css';s.textContent='html[data-ithaki-v249475-boot="home"] body .app{opacity:1!important;pointer-events:auto!important}html[data-ithaki-v249475-boot="home"] body:before{display:none!important}.ith526SingleHome .welcomeHero[data-ith526-canonical-home="0"]{display:none!important}#ith526R2ShareChoice{position:fixed;inset:0;z-index:2147483600;background:rgba(0,0,0,.78);display:grid;place-items:center;padding:12px;backdrop-filter:blur(9px)}#ith526R2ShareChoice>section{width:min(460px,96vw);max-height:88dvh;overflow:auto;border-radius:22px;background:#081522;color:#fff;border:1px solid rgba(255,255,255,.16);padding:12px}#ith526R2ShareChoice header{display:flex;align-items:center;gap:8px}#ith526R2ShareChoice header b{flex:1;text-align:center}#ith526R2ShareChoice button{color:#fff}#ith526R2ShareChoice header button{width:40px;height:40px;border-radius:50%;background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.15)}#ith526R2ShareChoice .external{width:100%;min-height:56px;margin:10px 0;border-radius:16px;background:rgba(54,240,160,.12);border:1px solid rgba(54,240,160,.4);font-weight:900}#ith526R2ShareChoice .label{margin:12px 3px 7px;color:#ffe39b;font-weight:900}#ith526R2ShareChoice .friend{width:100%;display:grid;grid-template-columns:44px 1fr auto;gap:10px;align-items:center;text-align:left;padding:9px;margin:7px 0;border-radius:15px;background:rgba(255,255,255,.05);border:1px solid rgba(255,255,255,.1)}#ith526R2ShareChoice .avatar{width:42px;height:42px;border-radius:50%;display:grid;place-items:center;background:rgba(255,255,255,.1);font-weight:900}#ith526R2ShareChoice small{display:block;opacity:.7;margin-top:2px}';document.head.appendChild(s)}
function maintain(){css();patchShare();stableHome()}
function install(){css();patchWall();patchShare();stableHome();new MutationObserver(()=>{clearTimeout(install._m);install._m=setTimeout(maintain,45)}).observe(document.body,{subtree:true,childList:true});window.addEventListener('ithaki:page-changed',()=>setTimeout(maintain,30));['pageshow','focus'].forEach(n=>window.addEventListener(n,maintain));setInterval(maintain,900);try{localStorage.setItem('ith_frontend_build','V249526R2-wall-share-single-home');document.documentElement.setAttribute('data-ithaki-v249526r2','wall-share-single-home')}catch(e){}}
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',install,{once:true});else install();
})();
