(function () {
  function ensureId(el){ if(!el.id){ el.id = 'a11y-' + Math.random().toString(36).slice(2); } return el.id; }

  // Αντιστοίχιση για το booking
  var NAME_MAP = { arrival:'Άφιξη', checkin:'Άφιξη', departure:'Αναχώρηση', checkout:'Αναχώρηση', adults:'Ενήλικες', children:'Παιδιά', kids:'Παιδιά' };

  function nameFromSelect(select){
    if (!select) return '';
    // 1) Προσπάθησε από <label for="id">
    var txt = '';
    if (select.id){
      var lab = document.querySelector('label[for="'+select.id+'"]');
      if (lab) txt = lab.textContent.trim();
    }
    // 2) Fallbacks
    if (!txt) txt = select.getAttribute('aria-label') || select.getAttribute('title') || '';
    if (!txt && select.name){
      var low = select.name.toLowerCase();
      for (var key in NAME_MAP){ if (low.indexOf(key)>-1){ txt = NAME_MAP[key]; break; } }
    }
    // 3) Γλώσσα
    var form = select.closest('form');
    if (!txt && form && /lang/.test(form.id) && /dropdown/.test(form.id)) txt = 'Επιλογή γλώσσας';
    return txt || 'Επιλογή';
  }

  function labelSelectricInputs(root){
    var scope = root || document;
    var inputs = scope.querySelectorAll('input.selectric-input');
    for (var i=0;i<inputs.length;i++){
      var inp = inputs[i];
      // Αν έχει ήδη όνομα, άστο
      if (inp.hasAttribute('aria-label') || inp.hasAttribute('aria-labelledby')) continue;

      // Βρες το wrapper της φόρμας
      var item = inp.closest('.js-form-item, .form-item');
      var label = item ? item.querySelector('label') : null;

      if (label){
        ensureId(label);
        inp.setAttribute('aria-labelledby', label.id);
      } else {
        // ψάξε το πραγματικό <select> στο ίδιο item ή ακριβώς πριν
        var sel = (item && item.querySelector('select')) || inp.previousElementSibling;
        var text = nameFromSelect(sel);
        inp.setAttribute('aria-label', text);
      }
    }
  }

  // Τρέξε τώρα, στο load και σε αλλαγές DOM
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function(){ labelSelectricInputs(document); });
  } else { labelSelectricInputs(document); }
  window.addEventListener('load', function(){ labelSelectricInputs(document); });
  [300, 800, 1500].forEach(function(ms){ setTimeout(function(){ labelSelectricInputs(document); }, ms); });
  new MutationObserver(function(muts){
    muts.forEach(function(m){
      for (var i=0;i<m.addedNodes.length;i++){
        var n = m.addedNodes[i];
        if (n.nodeType === 1){ // ELEMENT_NODE
          if (n.matches && n.matches('input.selectric-input')) labelSelectricInputs(n.parentNode);
          else if (n.querySelectorAll) labelSelectricInputs(n);
        }
      }
    });
  }).observe(document.documentElement, {childList:true, subtree:true});
})();
