/* === Shared UI components ============================================ */

const { useEffect, useRef, useState, useMemo, useCallback } = React;

/* ---- Logo mark ---- */
function LogoMark({ size = 38 }) {
  return (
    <img
      src="assets/logo-mark.png"
      alt="NMH"
      style={{ width: size, height: size, objectFit: 'contain', filter: 'drop-shadow(0 1px 2px rgba(0,0,0,0.15))' }}
    />
  );
}

/* ---- Image placeholder (striped SVG) ---- */
function Placeholder({ label, aspect = '4/3', radius = 0, style }) {
  return (
    <div className="ph" style={{ aspectRatio: aspect, borderRadius: radius, ...style }}>
      <span className="ph-label">{label}</span>
    </div>
  );
}

/* ---- Photo: real image with subtle reveal + Ken-Burns on hover ---- */
function Photo({ src, alt, aspect = '4/3', radius = 0, kenBurns = false, style, overlay = false, fit = 'cover' }) {
  if (!src) return <Placeholder label={alt || 'IMAGE'} aspect={aspect} radius={radius} style={style}/>;
  return (
    <div style={{ position: 'relative', overflow: 'hidden', aspectRatio: aspect, borderRadius: radius, ...style }}>
      <img
        src={src}
        alt={alt || ''}
        loading="lazy"
        className={kenBurns ? 'ken-burns' : ''}
        style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: fit, transition: 'transform 700ms ease' }}
      />
      {overlay && (
        <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(180deg, transparent 30%, rgba(0,0,0,0.45))', pointerEvents: 'none' }}/>
      )}
    </div>
  );
}

/* ---- Looping video tile ---- */
function VideoTile({ src, poster, aspect = '16/9', style }) {
  return (
    <div style={{ position: 'relative', overflow: 'hidden', aspectRatio: aspect, ...style }}>
      <video
        src={src}
        poster={poster}
        autoPlay
        muted
        loop
        playsInline
        style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' }}
      />
    </div>
  );
}

/* ---- Reveal on scroll ---- */
function Reveal({ children, delay = 0, as: Tag = 'div', className = '', style = {}, ...rest }) {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver(
      entries => {
        entries.forEach(e => {
          if (e.isIntersecting) {
            e.target.classList.add('is-visible');
            io.unobserve(e.target);
          }
        });
      },
      { threshold: 0.12 }
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return (
    <Tag
      ref={ref}
      className={`reveal ${className}`}
      style={{ '--reveal-delay': `${delay}ms`, ...style }}
      {...rest}
    >
      {children}
    </Tag>
  );
}

/* ---- Inline icons ---- */
const Icon = {
  Sun: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
    </svg>
  ),
  Moon: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
    </svg>
  ),
  Menu: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" {...p}>
      <path d="M3 7h18M3 12h18M3 17h18"/>
    </svg>
  ),
  Close: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" {...p}>
      <path d="M5 5l14 14M19 5l-14 14"/>
    </svg>
  ),
  Arrow: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 12h14M13 5l7 7-7 7"/>
    </svg>
  ),
  ArrowLeft: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M19 12H5M11 5l-7 7 7 7"/>
    </svg>
  ),
  Cal: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" {...p}>
      <rect x="3" y="5" width="18" height="16" rx="1"/><path d="M3 9h18M8 3v4M16 3v4"/>
    </svg>
  ),
  User: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" {...p}>
      <circle cx="12" cy="8" r="4"/><path d="M4 21c0-4 4-7 8-7s8 3 8 7"/>
    </svg>
  ),
  Bed: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 18V8M21 18v-5a3 3 0 0 0-3-3H10v6M3 14h18M3 18h18"/>
    </svg>
  ),
  Wifi: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M2 9a16 16 0 0 1 20 0M5 12.5a11 11 0 0 1 14 0M8.5 16a6 6 0 0 1 7 0"/><circle cx="12" cy="19" r="1" fill="currentColor"/>
    </svg>
  ),
  Snow: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2v20M2 12h20M5 5l14 14M5 19L19 5"/>
    </svg>
  ),
  Fork: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M7 3v8a2 2 0 0 0 2 2v8M11 3v6M5 3v6M17 3c-2 1-3 4-3 7s1 4 3 4v8"/>
    </svg>
  ),
  Drop: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2s7 8 7 13a7 7 0 1 1-14 0c0-5 7-13 7-13z"/>
    </svg>
  ),
  Car: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 17H3v-5l2-5h14l2 5v5h-2M5 17a2 2 0 1 0 4 0M15 17a2 2 0 1 0 4 0M5 12h14"/>
    </svg>
  ),
  Concierge: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2a5 5 0 0 0-5 5v3h10V7a5 5 0 0 0-5-5zM4 13h16l-2 8H6zM12 17v2"/>
    </svg>
  ),
  Mountain: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 20l5-9 4 6 3-4 6 7zM3 20h18"/>
    </svg>
  ),
  Bell: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M6 8a6 6 0 1 1 12 0c0 7 3 9 3 9H3s3-2 3-9zM10 21a2 2 0 0 0 4 0"/>
    </svg>
  ),
  Temple: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2L4 8h16zM5 8v13M19 8v13M5 21h14M9 21v-7h6v7M12 8v3"/>
    </svg>
  ),
  Basket: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 9h18l-2 11H5L3 9zM8 9V5a4 4 0 0 1 8 0v4"/>
    </svg>
  ),
  Pin: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 22s7-7 7-13a7 7 0 1 0-14 0c0 6 7 13 7 13z"/><circle cx="12" cy="9" r="2.5"/>
    </svg>
  ),
  Phone: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6A19.79 19.79 0 0 1 2.12 4.18 2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.8a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.9.35 1.84.59 2.8.72A2 2 0 0 1 22 16.92z"/>
    </svg>
  ),
  Whatsapp: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M17.5 14.4c-.3-.1-1.7-.8-2-1s-.5-.1-.7.1-.7.9-.9 1.1-.4.2-.6.1c-1.6-.7-2.8-1.6-3.8-3.4-.3-.5.3-.5.8-1.5.1-.2 0-.4 0-.5s-.7-1.7-1-2.3c-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.8.4s-1 1-1 2.4 1 2.8 1.2 3 2.1 3.2 5 4.4c.7.3 1.3.5 1.7.6.7.2 1.4.2 1.9.1.6-.1 1.7-.7 2-1.4.2-.7.2-1.3.2-1.4-.1-.1-.3-.2-.7-.4zM12 2C6.5 2 2 6.5 2 12c0 1.8.5 3.4 1.3 4.9L2 22l5.3-1.3c1.4.8 3 1.3 4.7 1.3 5.5 0 10-4.5 10-10S17.5 2 12 2z"/>
    </svg>
  ),
  Mail: (p) => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 7l9 6 9-6M3 7v10a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2z"/>
    </svg>
  ),
  Globe: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/>
    </svg>
  ),
  Star: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M12 2l3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.56 5.82 22 7 14.14 2 9.27l6.91-1.01z"/>
    </svg>
  ),
  Check: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M20 6L9 17l-5-5"/>
    </svg>
  ),
  Plus: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" {...p}>
      <path d="M12 5v14M5 12h14"/>
    </svg>
  ),
  Minus: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" {...p}>
      <path d="M5 12h14"/>
    </svg>
  ),
  Copy: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>
    </svg>
  ),
  Facebook: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z"/>
    </svg>
  ),
  Instagram: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="2" y="2" width="20" height="20" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="0.8" fill="currentColor" stroke="none"/>
    </svg>
  ),
  TikTok: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M19.6 6.8a4.9 4.9 0 0 1-3.6-1.6A4.9 4.9 0 0 1 14.8 2h-3.1v13.9a2.1 2.1 0 0 1-2.1 1.8 2.1 2.1 0 0 1-2.1-2.1 2.1 2.1 0 0 1 2.1-2.1c.2 0 .4 0 .6.1V10.3a5.3 5.3 0 0 0-.6 0 5.2 5.2 0 0 0-5.2 5.2 5.2 5.2 0 0 0 5.2 5.2 5.2 5.2 0 0 0 5.2-5.2V9a8 8 0 0 0 4.8 1.6V7.5a4.9 4.9 0 0 1-1-.7z"/>
    </svg>
  ),
  YouTube: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="M22.5 6.5s-.2-1.6-1-2.3c-.9-1-1.9-1-2.4-1C16.4 3 12 3 12 3s-4.4 0-7.1.2c-.5.1-1.5.1-2.4 1-.7.7-1 2.3-1 2.3S1.3 8.3 1.3 10v1.6c0 1.7.2 3.4.2 3.4s.2 1.6 1 2.3c.9 1 2.1.9 2.6 1C6.5 18.5 12 18.5 12 18.5s4.4 0 7.1-.2c.5-.1 1.5-.1 2.4-1 .7-.7 1-2.3 1-2.3s.2-1.7.2-3.4V10c0-1.7-.2-3.5-.2-3.5zM9.7 13.8V8.3l6.5 2.8-6.5 2.7z"/>
    </svg>
  ),
};

/* ---- Theme toggle ---- */
function ThemeToggle({ theme, onToggle }) {
  return (
    <button
      className="theme-toggle"
      onClick={onToggle}
      aria-label={`Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`}
      title={`${theme === 'dark' ? 'Light' : 'Dark'} mode`}
    >
      <span className="knob">
        {theme === 'dark' ? <Icon.Moon /> : <Icon.Sun />}
      </span>
    </button>
  );
}

/* ---- Language toggle (EN/नेपाली) ---- */
function LangToggle({ lang, onChange }) {
  return (
    <button
      className="lang-toggle"
      onClick={() => onChange(lang === 'en' ? 'ne' : 'en')}
      aria-label="Toggle language"
      title="Language"
    >
      <Icon.Globe />
      <span className="lang-letters">
        <span className={lang === 'en' ? 'on' : ''}>EN</span>
        <span className="sep">·</span>
        <span className={lang === 'ne' ? 'on' : ''}>ने</span>
      </span>
    </button>
  );
}

/* ---- Navigation ---- */
function Nav({ page, onNav, theme, onToggleTheme, lang, onChangeLang, onBook }) {
  const t = useT();
  const [mobileOpen, setMobileOpen] = useState(false);
  const items = [
    { id: 'home',        label: t('nav_home') },
    { id: 'rooms',       label: t('nav_rooms') },
    { id: 'dining',      label: t('nav_dining') },
    { id: 'experiences', label: t('nav_experiences') },
    { id: 'gallery',     label: t('nav_gallery') },
    { id: 'contact',     label: t('nav_contact') },
  ];
  return (
    <header className="nav">
      <div className="nav-inner">
        <div className="nav-brand" onClick={() => { onNav('home'); setMobileOpen(false); }}>
          <LogoMark size={42} />
          <div>
            <div className="name">{t('brand_name')}</div>
            <div className="sub">{t('brand_sub')}</div>
          </div>
        </div>

        <nav className={`nav-links ${mobileOpen ? 'open' : ''}`}>
          {items.map(it => (
            <button
              key={it.id}
              className="nav-link"
              data-active={page === it.id}
              onClick={() => { onNav(it.id); setMobileOpen(false); }}
            >
              {it.label}
            </button>
          ))}
        </nav>

        <div className="nav-actions">
          <LangToggle lang={lang} onChange={onChangeLang} />
          <ThemeToggle theme={theme} onToggle={onToggleTheme} />
          <button className="btn btn-primary nav-book-btn" onClick={onBook}>
            {t('book_stay')}
          </button>
          <button
            className="icon-btn nav-mobile-btn"
            onClick={() => setMobileOpen(o => !o)}
            aria-label="Menu"
          >
            {mobileOpen ? <Icon.Close /> : <Icon.Menu />}
          </button>
        </div>
      </div>
    </header>
  );
}

/* ---- Footer ---- */
function Footer({ onNav, onBook }) {
  const t = useT();
  return (
    <footer className="footer">
      <div className="container">
        <div className="footer-grid">
          <div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginBottom: 20 }}>
              <LogoMark size={48} />
              <div>
                <div style={{ fontFamily: 'var(--font-display)', fontSize: 22, letterSpacing: '0.04em', color: 'var(--gold)' }}>
                  {t('brand_name')}
                </div>
                <div style={{ fontSize: 10, letterSpacing: '0.32em', textTransform: 'uppercase', color: 'rgba(245,239,230,0.6)', marginTop: 2 }}>
                  {t('brand_sub')}
                </div>
              </div>
            </div>
            <p style={{ fontSize: 14, lineHeight: 1.7, opacity: 0.75, maxWidth: 320 }}>
              {t('foot_tag')}
            </p>
          </div>

          <div>
            <h4>{t('foot_explore')}</h4>
            <ul>
              <li><a onClick={() => onNav('rooms')} style={{cursor:'pointer'}}>{t('nav_rooms')}</a></li>
              <li><a onClick={() => onNav('dining')} style={{cursor:'pointer'}}>{t('nav_dining')}</a></li>
              <li><a onClick={() => onNav('experiences')} style={{cursor:'pointer'}}>{t('nav_experiences')}</a></li>
              <li><a onClick={() => onNav('gallery')} style={{cursor:'pointer'}}>{t('nav_gallery')}</a></li>
            </ul>
          </div>

          <div>
            <h4>{t('foot_stay')}</h4>
            <ul>
              <li><a onClick={onBook} style={{cursor:'pointer'}}>{t('reserve')}</a></li>
              <li><a onClick={() => onNav('contact')} style={{cursor:'pointer'}}>{t('nav_contact')}</a></li>
            </ul>
          </div>

          <div>
            <h4>{t('foot_connect')}</h4>
            <ul>
              <li><a href="tel:+97791410144">+977 91-410144</a></li>
              <li><a href="tel:+9779848860565">+977 9848860565</a></li>
              <li><a href="mailto:maheshworihotel33@gmail.com">maheshworihotel33@gmail.com</a></li>
              <li><a href="https://wa.me/9779848860565">WhatsApp</a></li>
            </ul>
            <div style={{ display: 'flex', gap: 14, marginTop: 20 }}>
              <a href="https://www.facebook.com/maheshworihotel/" target="_blank" rel="noopener noreferrer" style={{ color: 'var(--gold)', opacity: 0.8, transition: 'opacity 0.2s' }} onMouseEnter={e => e.currentTarget.style.opacity=1} onMouseLeave={e => e.currentTarget.style.opacity=0.8} aria-label="Facebook"><Icon.Facebook /></a>
              <a href="https://www.instagram.com/maheshworihotel12" target="_blank" rel="noopener noreferrer" style={{ color: 'var(--gold)', opacity: 0.8, transition: 'opacity 0.2s' }} onMouseEnter={e => e.currentTarget.style.opacity=1} onMouseLeave={e => e.currentTarget.style.opacity=0.8} aria-label="Instagram"><Icon.Instagram /></a>
              <a href="https://www.tiktok.com/@maheshworihotel" target="_blank" rel="noopener noreferrer" style={{ color: 'var(--gold)', opacity: 0.8, transition: 'opacity 0.2s' }} onMouseEnter={e => e.currentTarget.style.opacity=1} onMouseLeave={e => e.currentTarget.style.opacity=0.8} aria-label="TikTok"><Icon.TikTok /></a>
              <a href="https://www.youtube.com/@hotelmaheshwori3929" target="_blank" rel="noopener noreferrer" style={{ color: 'var(--gold)', opacity: 0.8, transition: 'opacity 0.2s' }} onMouseEnter={e => e.currentTarget.style.opacity=1} onMouseLeave={e => e.currentTarget.style.opacity=0.8} aria-label="YouTube"><Icon.YouTube /></a>
            </div>
          </div>
        </div>

        <div className="footer-bottom">
          <span>© 2026 New Maheshwori Hotel &amp; Lodge. {t('rights')}</span>
          <span>{t('foot_crafted')}</span>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { LogoMark, Placeholder, Photo, VideoTile, Reveal, Icon, ThemeToggle, LangToggle, Nav, Footer });
