// screens.jsx — all app screens

const fav = PRODUCTS.filter(p => ['cafe-leche', 'sandwich', 'medialunas', 'cheesecake', 'cafe-llevar', 'limonada'].includes(p.id));

// ── Quick-access tile ──────────────────────────────
function QuickTile({ icon, label, sub, tone, onClick }) {
  return (
    <button onClick={onClick} className="card" style={{
      textAlign: 'left', cursor: 'pointer', padding: 0, border: '1px solid var(--c-line)',
      background: 'var(--c-surface)', fontFamily: 'var(--font-body)', overflow: 'hidden',
    }}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10, alignItems: 'flex-start', padding: 14 }}>
        <span style={{ width: 40, height: 40, borderRadius: 12, display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'color-mix(in srgb, var(--c-brand) 12%, var(--c-surface))', color: 'var(--c-brand)', flexShrink: 0 }}>
          <Icon name={icon} size={22} sw={1.9} />
        </span>
        <div style={{ width: '100%' }}>
          <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14.5, color: 'var(--c-ink)', lineHeight: 1.2 }}>{label}</div>
          <div style={{ fontSize: 11.5, color: 'var(--c-ink-soft)', marginTop: 3, lineHeight: 1.3 }}>{sub}</div>
        </div>
      </div>
    </button>
  );
}

// ── HOME ────────────────────────────────────────────
function HomeScreen() {
  const { t, lang } = useLang();
  const { go } = useNav();
  return (
    <div className="az-screen" style={{ paddingTop: 16 }}>
      <OpenStrip />
      {/* hero */}
      <div style={{ position: 'relative', marginTop: 14, borderRadius: 'var(--r-lg)', overflow: 'hidden', background: 'linear-gradient(155deg, var(--c-brand-2), var(--c-brand))', color: '#fff', padding: '22px 20px 20px' }}>
        <div style={{ position: 'absolute', inset: 0, opacity: .5, backgroundImage: "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='120' height='120' filter='url(%23n)' opacity='0.06'/%3E%3C/svg%3E\")" }} />
        <div style={{ position: 'absolute', right: -22, top: -18, opacity: .16, color: '#fff' }}><Icon name="mountain" size={150} sw={1} /></div>
        <div style={{ position: 'relative' }}>
          <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 27, lineHeight: 1.08, letterSpacing: '-.01em', whiteSpace: 'pre-line' }}>{t.tagline}</div>
          <p style={{ margin: '11px 0 17px', fontSize: 13.5, lineHeight: 1.5, color: 'rgba(255,255,255,.86)', maxWidth: 280 }}>{t.heroSub}</p>
          <div style={{ display: 'flex', gap: 9 }}>
            <button className="btn btn-primary" onClick={() => go('menu')}><Icon name="coffee" size={18} sw={1.9} />{t.seeMenu}</button>
            <button className="btn" style={{ background: 'rgba(255,255,255,.16)', color: '#fff', padding: '14px 18px', fontSize: 15 }} onClick={() => go('combos')}>{t.combosCta}</button>
          </div>
        </div>
      </div>

      {/* quick access */}
      <div style={{ marginTop: 22 }}>
        <div className="h-eyebrow" style={{ marginBottom: 11 }}>{t.quick}</div>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 11 }}>
          <QuickTile icon="sandwich" label={t.menu} sub={lang === 'en' ? 'Coffee, food & sweets' : 'Café, comida y dulces'} onClick={() => go('menu')} />
          <QuickTile icon="bag" label={t.combosCta} sub={lang === 'en' ? 'Grab & go' : 'Listos para llevar'} onClick={() => go('combos')} />
          <QuickTile icon="mountain" label="Esquel" sub={lang === 'en' ? 'What to do' : 'Qué hacer'} onClick={() => go('esquel')} />
          <QuickTile icon="pin" label={t.where} sub={lang === 'en' ? 'Hours & map' : 'Horarios y mapa'} onClick={() => go('place')} />
        </div>
      </div>

      {/* featured rail */}
      <div style={{ marginTop: 24 }}>
        <SectionHead title={t.featured} sub={lang === 'en' ? 'What everyone grabs.' : 'Lo que todos se llevan.'} />
        <div style={{ display: 'flex', gap: 12, overflowX: 'auto', margin: '0 -18px', padding: '2px 18px 6px', scrollbarWidth: 'none' }}>
          {fav.map(p => <ProductMini key={p.id} p={p} />)}
        </div>
      </div>

      {/* tourism teaser */}
      <button onClick={() => go('esquel')} className="card" style={{ width: '100%', marginTop: 22, padding: 0, overflow: 'hidden', cursor: 'pointer', textAlign: 'left', display: 'block' }}>
        <div style={{ position: 'relative', height: 130 }}>
          <Placeholder icon="leaf" tone="forest" label={null} style={{ height: '100%', borderRadius: 0 }} />
          <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(0deg, rgba(20,40,30,.78), rgba(20,40,30,.05))', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', padding: 16 }}>
            <div className="h-eyebrow" style={{ color: 'var(--c-gold)' }}>{lang === 'en' ? 'Before you travel' : 'Antes de viajar'}</div>
            <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 19, color: '#fff', marginTop: 3 }}>{t.whatToDo}</div>
          </div>
          <span style={{ position: 'absolute', right: 14, bottom: 14, width: 36, height: 36, borderRadius: 999, background: 'rgba(255,255,255,.92)', color: 'var(--c-brand)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name="arrowR" size={18} sw={2.2} /></span>
        </div>
      </button>
    </div>
  );
}

// ── MENU ────────────────────────────────────────────
function MenuScreen() {
  const { t, lang } = useLang();
  const { go } = useNav();
  const [active, setActive] = React.useState('cafe');
  const refs = React.useRef({});
  const onChip = (id) => { setActive(id); const el = refs.current[id]; if (el) el.scrollIntoView({ block: 'start', behavior: 'smooth' }); };
  return (
    <div>
      {/* sticky category chips */}
      <div style={{ position: 'sticky', top: 0, zIndex: 5, background: 'var(--c-bg)', padding: '12px 0 10px', boxShadow: '0 6px 12px -10px rgba(0,0,0,.25)' }}>
        <div style={{ display: 'flex', gap: 8, overflowX: 'auto', padding: '0 18px', scrollbarWidth: 'none' }}>
          {CATEGORIES.map(c => (
            <button key={c.id} className={'chip' + (active === c.id ? ' is-on' : '')} onClick={() => onChip(c.id)}>
              <Icon name={c.icon} size={16} sw={1.9} />{lang === 'en' ? c.en : c.label}
            </button>
          ))}
        </div>
      </div>
      <div className="az-screen" style={{ paddingTop: 6 }}>
        {/* combos promo */}
        <button onClick={() => go('combos')} className="card" style={{ width: '100%', cursor: 'pointer', textAlign: 'left', display: 'flex', alignItems: 'center', gap: 12, padding: 12, marginBottom: 18, background: 'color-mix(in srgb, var(--c-gold) 14%, var(--c-surface))' }}>
          <span style={{ width: 42, height: 42, borderRadius: 12, background: 'var(--c-accent)', color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}><Icon name="bag" size={22} sw={1.9} /></span>
          <span style={{ flex: 1 }}>
            <span style={{ display: 'block', fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 15 }}>{t.combosTitle}</span>
            <span style={{ display: 'block', fontSize: 12, color: 'var(--c-ink-soft)' }}>{t.combosSub}</span>
          </span>
          <Icon name="chevR" size={20} sw={2} style={{ color: 'var(--c-ink-soft)' }} />
        </button>

        {CATEGORIES.map(c => (
          <div key={c.id} ref={el => refs.current[c.id] = el} style={{ scrollMarginTop: 64, marginBottom: 22 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, margin: '0 0 12px' }}>
              <span style={{ color: 'var(--c-brand)' }}><Icon name={c.icon} size={20} sw={1.9} /></span>
              <h3 style={{ fontWeight: 700, fontSize: 18 }}>{lang === 'en' ? c.en : c.label}</h3>
            </div>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              {PRODUCTS.filter(p => p.cat === c.id).map(p => <ProductCard key={p.id} p={p} />)}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── COMBOS ──────────────────────────────────────────
function CombosScreen() {
  const { t, lang } = useLang();
  return (
    <div className="az-screen" style={{ paddingTop: 18 }}>
      <SectionHead eyebrow={lang === 'en' ? 'To go' : 'Para llevar'} title={t.combosTitle} sub={t.combosSub} />
      <div style={{ display: 'flex', flexDirection: 'column', gap: 13 }}>
        {COMBOS.map(c => <ComboCard key={c.id} c={c} />)}
      </div>
      <p style={{ fontSize: 12, color: 'var(--c-ink-soft)', textAlign: 'center', marginTop: 18, lineHeight: 1.5 }}>
        {lang === 'en' ? 'Combos can be customized at the counter.' : 'Los combos se pueden ajustar en el mostrador.'}
      </p>
    </div>
  );
}

// ── recommendation chip (for Esquel) ───────────────
function RecItem({ id }) {
  const p = productById(id);
  const cart = useCart();
  const { lang } = useLang();
  if (!p) return null;
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 9, padding: '7px 7px 7px 10px', borderRadius: 999, border: '1px solid var(--c-line)', background: 'var(--c-bg)' }}>
      <span style={{ color: 'var(--c-brand)', display: 'flex' }}><Icon name={p.icon} size={16} sw={1.9} /></span>
      <span style={{ fontSize: 12.5, fontWeight: 600, color: 'var(--c-ink)', whiteSpace: 'nowrap' }}>{lang === 'en' && p.en ? p.en : p.name}</span>
      <button className="add-btn" style={{ width: 28, height: 28 }} onClick={() => cart.add(id)} aria-label="Agregar"><Icon name="plus" size={16} sw={2.3} /></button>
    </div>
  );
}

// ── ESQUEL (tourism → products) ─────────────────────
function EsquelScreen() {
  const { t, lang } = useLang();
  const cart = useCart();
  return (
    <div className="az-screen" style={{ paddingTop: 18 }}>
      <SectionHead eyebrow={lang === 'en' ? 'Patagonia' : 'Patagonia'} title={t.esquelTitle} sub={t.esquelSub} />
      <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
        {ACTIVITIES.map(a => (
          <div key={a.id} className="card" style={{ overflow: 'hidden' }}>
            <div style={{ position: 'relative', height: 122 }}>
              <Placeholder icon={a.icon} tone={a.tone} label={null} style={{ height: '100%', borderRadius: 0 }} />
              <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(0deg, rgba(20,30,25,.7), rgba(20,30,25,0) 60%)', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', padding: 14 }}>
                <span style={{ alignSelf: 'flex-start', fontSize: 10.5, fontWeight: 700, letterSpacing: '.04em', textTransform: 'uppercase', color: '#fff', background: 'rgba(0,0,0,.3)', padding: '4px 9px', borderRadius: 999, marginBottom: 6 }}>{a.meta}</span>
                <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 18, color: '#fff', lineHeight: 1.1 }}>{lang === 'en' ? a.en : a.name}</div>
              </div>
            </div>
            <div style={{ padding: '13px 14px 14px' }}>
              <p style={{ margin: '0 0 13px', fontSize: 13, color: 'var(--c-ink-soft)', lineHeight: 1.5 }}>{a.blurb}</p>
              <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 9 }}>
                <Icon name="bag" size={15} sw={1.9} style={{ color: 'var(--c-accent)' }} />
                <span className="h-eyebrow">{t.forTheRoad}</span>
              </div>
              <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
                {a.recs.map(id => <RecItem key={id} id={id} />)}
              </div>
              {a.comboId && (() => { const c = COMBOS.find(x => x.id === a.comboId); return (
                <button className="btn btn-ghost btn-block" style={{ marginTop: 12, justifyContent: 'space-between' }} onClick={() => cart.add(a.comboId)}>
                  <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}><Icon name="bag" size={17} sw={1.9} />{c.name}</span>
                  <span style={{ fontWeight: 700, color: 'var(--c-accent-ink)' }}>{fmt(c.price)} +</span>
                </button>
              ); })()}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

// ── PLACE (location & hours) ────────────────────────
function PlaceScreen() {
  const { t, lang } = useLang();
  const { go } = useNav();
  return (
    <div className="az-screen" style={{ paddingTop: 18 }}>
      <SectionHead eyebrow={lang === 'en' ? 'Bus terminal' : 'Terminal de ómnibus'} title={t.placeTitle} />
      {/* map placeholder */}
      <div style={{ position: 'relative', borderRadius: 'var(--r)', overflow: 'hidden', height: 150, border: '1px solid var(--c-line)' }}>
        <Placeholder icon="pin" tone="lake" label="Mapa" style={{ height: '100%', borderRadius: 0 }} />
      </div>
      <div className="card" style={{ padding: 14, marginTop: 13, display: 'flex', gap: 11, alignItems: 'flex-start' }}>
        <span style={{ color: 'var(--c-accent)', marginTop: 1 }}><Icon name="pin" size={22} sw={1.9} /></span>
        <div>
          <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 15 }}>Terminal de Ómnibus de Esquel</div>
          <div style={{ fontSize: 12.5, color: 'var(--c-ink-soft)', marginTop: 2, lineHeight: 1.5 }}>
            {lang === 'en' ? 'Ground floor · across from platforms 4–7.' : 'Planta baja · frente a las plataformas 4 a 7.'}
          </div>
        </div>
      </div>

      {/* hours */}
      <div style={{ marginTop: 20 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 11 }}>
          <Icon name="clock" size={18} sw={1.9} style={{ color: 'var(--c-brand)' }} />
          <h3 style={{ fontWeight: 700, fontSize: 17 }}>{t.hoursTitle}</h3>
        </div>
        <div className="card" style={{ overflow: 'hidden' }}>
          {HOURS.map((r, i) => (
            <div key={i} style={{ display: 'flex', justifyContent: 'space-between', padding: '13px 15px', fontSize: 13.5, borderTop: i ? '1px solid var(--c-line)' : 'none' }}>
              <span style={{ color: 'var(--c-ink)' }}>{r.d}</span>
              <span style={{ fontWeight: 700, color: 'var(--c-ink)' }}>{r.h}</span>
            </div>
          ))}
        </div>
      </div>

      {/* contact buttons */}
      <div style={{ display: 'flex', gap: 10, marginTop: 18 }}>
        <button className="btn" style={{ flex: 1, background: '#1f8a4c', color: '#fff', padding: '13px' }}><Icon name="whatsapp" size={18} sw={1.8} />WhatsApp</button>
        <button className="btn btn-ghost" style={{ flex: 1 }} onClick={() => go('menu')}><Icon name="coffee" size={17} sw={1.9} />{t.seeMenu}</button>
      </div>
      <div className="card" style={{ marginTop: 16, padding: 14, display: 'flex', gap: 10, alignItems: 'flex-start', background: 'color-mix(in srgb, var(--c-brand) 8%, var(--c-surface))' }}>
        <span style={{ color: 'var(--c-brand)' }}><Icon name="info" size={20} sw={1.9} /></span>
        <p style={{ margin: 0, fontSize: 12.5, color: 'var(--c-ink-soft)', lineHeight: 1.5 }}>
          {lang === 'en' ? 'Order from the app and pick it up at the counter — skip the wait before your bus.' : 'Pedí desde la app y retirá en el mostrador, sin perder tiempo antes de tu colectivo.'}
        </p>
      </div>
    </div>
  );
}

// ── CART ────────────────────────────────────────────
function CartScreen() {
  const { t, lang } = useLang();
  const cart = useCart();
  const { go } = useNav();
  const [name, setName] = React.useState('');
  if (cart.count === 0) {
    return (
      <div className="az-screen" style={{ paddingTop: 60, textAlign: 'center', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
        <span style={{ width: 72, height: 72, borderRadius: 999, background: 'var(--c-surface)', border: '1px solid var(--c-line)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--c-ink-soft)' }}><Icon name="bag" size={32} sw={1.6} /></span>
        <h3 style={{ fontWeight: 700, fontSize: 19, marginTop: 18 }}>{t.empty}</h3>
        <p className="h-sub" style={{ marginTop: 6, maxWidth: 220 }}>{t.emptySub}</p>
        <button className="btn btn-dark" style={{ marginTop: 18 }} onClick={() => go('menu')}>{t.seeMenu}</button>
      </div>
    );
  }
  return (
    <div className="az-screen" style={{ paddingTop: 18 }}>
      <SectionHead title={t.cart} />
      <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
        {cart.lines.map(({ id, qty }) => {
          const p = productById(id);
          return (
            <div key={id} className="card" style={{ display: 'flex', alignItems: 'center', gap: 11, padding: 10 }}>
              <Placeholder icon={p.icon} tone={p.tone || 'brand'} style={{ width: 52, height: 52, flexShrink: 0, borderRadius: 'var(--r-sm)' }} />
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14, lineHeight: 1.15 }}>{lang === 'en' && p.en ? p.en : p.name}</div>
                <div style={{ fontSize: 13, color: 'var(--c-accent-ink)', fontWeight: 700, marginTop: 3 }}>{fmt(p.price * qty)}</div>
              </div>
              <QtyControl id={id} />
            </div>
          );
        })}
      </div>

      {/* pickup + name */}
      <div className="card" style={{ marginTop: 16, padding: 14 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 9, marginBottom: 12 }}>
          <span style={{ color: 'var(--c-brand)' }}><Icon name="bag" size={18} sw={1.9} /></span>
          <span style={{ fontWeight: 700, fontSize: 14 }}>{t.pickup}</span>
        </div>
        <label style={{ fontSize: 12, color: 'var(--c-ink-soft)', fontWeight: 600 }}>{t.name}</label>
        <input value={name} onChange={e => setName(e.target.value)} placeholder={t.namePh}
          style={{ width: '100%', marginTop: 6, padding: '12px 14px', borderRadius: 'var(--r-sm)', border: '1px solid var(--c-line)', background: 'var(--c-bg)', fontSize: 14, fontFamily: 'var(--font-body)', color: 'var(--c-ink)', outline: 'none' }} />
      </div>

      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', margin: '18px 2px 14px' }}>
        <span style={{ fontSize: 14, color: 'var(--c-ink-soft)' }}>{t.total} · {cart.count} {cart.count === 1 ? t.item : t.items}</span>
        <span style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 24, color: 'var(--c-ink)' }}>{fmt(cart.total)}</span>
      </div>
      <button className="btn btn-primary btn-block" onClick={() => go('confirm')}><Icon name="check" size={19} sw={2.3} />{t.confirm}</button>
    </div>
  );
}

// ── CONFIRM ─────────────────────────────────────────
function ConfirmScreen() {
  const { t } = useLang();
  const cart = useCart();
  const { go } = useNav();
  const num = React.useMemo(() => 'Z-' + (10 + Math.floor(Math.random() * 89)), []);
  React.useEffect(() => () => {}, []);
  return (
    <div className="az-screen" style={{ paddingTop: 64, textAlign: 'center', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
      <span style={{ width: 84, height: 84, borderRadius: 999, background: 'var(--c-brand)', color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 10px 26px rgba(35,74,58,.34)' }}><Icon name="check" size={42} sw={2.4} /></span>
      <h2 style={{ fontWeight: 800, fontSize: 24, marginTop: 20 }}>{t.done}</h2>
      <p className="h-sub" style={{ marginTop: 7, maxWidth: 240 }}>{t.doneSub}</p>
      <div className="card" style={{ marginTop: 22, padding: '18px 30px', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
        <span style={{ fontSize: 12, letterSpacing: '.12em', textTransform: 'uppercase', color: 'var(--c-ink-soft)', fontWeight: 700 }}>{t.order}</span>
        <span style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 42, color: 'var(--c-accent-ink)', lineHeight: 1.1 }}>{num}</span>
      </div>
      <button className="btn btn-ghost" style={{ marginTop: 26 }} onClick={() => { cart.clear(); go('home'); }}>{t.newOrder}</button>
    </div>
  );
}

Object.assign(window, {
  HomeScreen, MenuScreen, CombosScreen, EsquelScreen, PlaceScreen, CartScreen, ConfirmScreen,
});
