// กับข้าวคุณแม่ — Screen components & shared atoms
// All UI lives here; app.jsx owns state and wires screens together.

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

// ─── tiny helpers ──────────────────────────────────────────────
const fmtBaht = (n) => '฿' + n.toLocaleString('th-TH');
const t = (lang, k) => window.T[lang] && window.T[lang][k] || k;

// Generic food placeholder with stripes + name watermark
function FoodPlaceholder({ c1 = '#ECD9B6', c2 = '#DCC197', label, size = 'md', round = false }) {
  const fs = size === 'sm' ? 11 : size === 'lg' ? 16 : 13;
  return (
    <div style={{
      position: 'relative', width: '100%', height: '100%',
      borderRadius: round ? '50%' : 0, overflow: 'hidden',
      '--c1': c1, '--c2': c2
    }} className="food-ph">
      <div style={{
        position: 'absolute', inset: 0, display: 'flex',
        alignItems: 'flex-end', justifyContent: 'flex-start',
        padding: 8, color: 'rgba(42,26,14,0.42)',
        fontFamily: 'ui-monospace, "SF Mono", monospace', fontSize: fs,
        fontWeight: 500, lineHeight: 1.1,
        textShadow: '0 1px 0 rgba(255,255,255,0.4)'
      }}>{label}</div>
    </div>);

}

// Spice chili icons
function ChiliIcons({ n = 1, color = '#B91C1C', size = 11 }) {
  return (
    <span style={{ display: 'inline-flex', gap: 1.5, verticalAlign: 'middle' }}>
      {[...Array(Math.max(n, 1))].map((_, i) =>
      <svg key={i} width={size} height={size} viewBox="0 0 16 16" style={{ opacity: i < n ? 1 : 0.25 }}>
          <path d="M4 3c2 0 4 1 5 3 1.5 2.5 1 6-1 8-1.5 1.5-4 2-6 1 1-2 1-4 0-6 0-2 1-5 2-6z" fill={color} />
          <path d="M5 3c0-1 1-2 2-2 0 1-1 1.5-1 2.5" stroke="#3F6212" strokeWidth="1.3" fill="none" strokeLinecap="round" />
        </svg>
      )}
    </span>);

}

// Pill / chip
function Chip({ children, active, onClick, style, color = 'var(--terra)' }) {
  return (
    <button onClick={onClick} className="press" style={{
      padding: '7px 14px', borderRadius: 999,
      background: active ? color : 'var(--paper)',
      color: active ? 'var(--ink-on-terra)' : 'var(--brown-700)',
      border: active ? '1px solid ' + color : '1px solid var(--cream-200)',
      fontWeight: 500, fontSize: 14, whiteSpace: 'nowrap',
      boxShadow: active ? '0 2px 6px -2px rgba(194,65,12,0.4)' : 'none',
      ...style
    }}>{children}</button>);

}

// Quantity stepper
function QtyStepper({ qty, setQty, min = 1, max = 99, size = 'md' }) {
  const d = size === 'sm' ? 28 : 36;
  const b = (sign, fn, dis) =>
  <button onClick={fn} disabled={dis} className="press" style={{
    width: d, height: d, borderRadius: 999,
    background: dis ? 'var(--cream-100)' : 'var(--paper)',
    border: '1.5px solid ' + (dis ? 'var(--cream-200)' : 'var(--brown-700)'),
    color: dis ? 'var(--brown-300)' : 'var(--brown-700)',
    fontSize: 18, fontWeight: 600, lineHeight: 1,
    display: 'flex', alignItems: 'center', justifyContent: 'center'
  }}>{sign}</button>;

  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 12 }}>
      {b('−', () => setQty(Math.max(min, qty - 1)), qty <= min)}
      <div style={{
        minWidth: 24, textAlign: 'center', fontWeight: 600,
        fontSize: size === 'sm' ? 16 : 18, color: 'var(--brown-900)'
      }}>{qty}</div>
      {b('+', () => setQty(Math.min(max, qty + 1)), qty >= max)}
    </div>);

}

// Bottom nav bar
function BottomNav({ tab, setTab, cartCount, lang }) {
  const tabs = [
  { id: 'menu', label: t(lang, 'nav_menu'), icon:
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
        <path d="M4 5h16M4 12h16M4 19h10" />
      </svg> },
  { id: 'cart', label: t(lang, 'nav_cart'), icon:
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
        <path d="M6 7h13l-1.5 9a2 2 0 0 1-2 1.7H9.5a2 2 0 0 1-2-1.7L5.5 4H3" />
        <circle cx="9" cy="20" r="1" /><circle cx="17" cy="20" r="1" />
      </svg> },
  { id: 'order', label: t(lang, 'nav_order'), icon:
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
        <path d="M7 4h7l4 4v12H7z" /><path d="M14 4v4h4" /><path d="M10 13h5M10 17h3" />
      </svg> },
  { id: 'history', label: t(lang, 'nav_history'), icon:
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
        <path d="M12 7v5l3 2" /><circle cx="12" cy="12" r="9" />
      </svg> }];

  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0,
      paddingBottom: 28, paddingTop: 6, zIndex: 30,
      background: 'linear-gradient(180deg, rgba(251,246,236,0) 0%, var(--cream-50) 30%)'
    }}>
      <div style={{
        margin: '0 14px',
        background: 'rgba(255,252,245,0.9)',
        backdropFilter: 'blur(14px)',
        WebkitBackdropFilter: 'blur(14px)',
        borderRadius: 22,
        border: '1px solid rgba(220,193,151,0.5)',
        boxShadow: 'var(--shadow-md)',
        display: 'flex',
        padding: '6px',
        gap: 2
      }}>
        {tabs.map((tb) => {
          const active = tab === tb.id;
          return (
            <button key={tb.id} onClick={() => setTab(tb.id)} className="press" style={{
              flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center',
              gap: 2, padding: '8px 4px 6px', borderRadius: 16,
              color: active ? 'var(--terra)' : 'var(--brown-500)',
              background: active ? 'var(--terra-100)' : 'transparent',
              position: 'relative'
            }}>
              <div style={{ position: 'relative' }}>
                {tb.icon}
                {tb.id === 'cart' && cartCount > 0 &&
                <div style={{
                  position: 'absolute', top: -6, right: -10, minWidth: 16, height: 16,
                  padding: '0 4px', borderRadius: 999,
                  background: 'var(--terra)', color: 'white',
                  fontSize: 10, fontWeight: 700,
                  display: 'flex', alignItems: 'center', justifyContent: 'center'
                }}>{cartCount}</div>
                }
              </div>
              <div style={{ fontSize: 11, fontWeight: active ? 600 : 500, whiteSpace: 'nowrap' }}>{tb.label}</div>
            </button>);

        })}
      </div>
    </div>);

}

// App header
function AppHeader({ title, subtitle, onBack, right, dark = false }) {
  return (
    <div style={{
      padding: '52px 18px 14px', position: 'sticky', top: 0, zIndex: 20,
      background: dark ?
      'linear-gradient(180deg, rgba(42,26,14,0.95) 0%, rgba(42,26,14,0.85) 80%, rgba(42,26,14,0) 100%)' :
      'linear-gradient(180deg, rgba(251,246,236,1) 0%, rgba(251,246,236,0.95) 80%, rgba(251,246,236,0) 100%)',
      color: dark ? 'var(--cream-50)' : 'var(--brown-900)',
      backdropFilter: 'blur(8px)'
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        {onBack &&
        <button onClick={onBack} className="press" style={{
          width: 36, height: 36, borderRadius: 999,
          background: dark ? 'rgba(255,255,255,0.12)' : 'var(--paper)',
          border: dark ? '1px solid rgba(255,255,255,0.18)' : '1px solid var(--cream-200)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          color: dark ? 'var(--cream-50)' : 'var(--brown-700)'
        }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="M15 18l-6-6 6-6" />
            </svg>
          </button>
        }
        <div style={{ flex: 1, minWidth: 0 }}>
          {subtitle && <div style={{ fontSize: 12, fontWeight: 500, opacity: 0.65, marginBottom: 2 }}>{subtitle}</div>}
          <div style={{
            fontFamily: 'var(--font-display)',
            fontSize: 26, fontWeight: 700, lineHeight: 1.1,
            letterSpacing: '-0.01em'
          }}>{title}</div>
        </div>
        {right}
      </div>
    </div>);

}

// ─────────────────────────────────────────────────────────────
// WELCOME (Table landing — after scanning the table QR)
// ─────────────────────────────────────────────────────────────
function WelcomeScreen({ lang, tableNo, onStart }) {
  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: 'linear-gradient(180deg, #FCEDD2 0%, #F1D9A6 60%, #E8C887 100%)',
      display: 'flex', flexDirection: 'column',
      paddingTop: 64, paddingBottom: 60
    }}>
      {/* warm sun + steam pattern */}
      <div style={{
        position: 'absolute', top: -80, right: -80, width: 280, height: 280,
        borderRadius: '50%', background: 'radial-gradient(circle, #FFD37A 0%, transparent 70%)',
        opacity: 0.7
      }} />

      <div style={{ padding: '0 28px', textAlign: 'center', position: 'relative' }} className="fade-up">
        <div style={{
          display: 'inline-flex', alignItems: 'center', gap: 6,
          padding: '6px 14px', borderRadius: 999,
          background: 'rgba(255,252,245,0.7)',
          border: '1px solid rgba(154,51,10,0.18)',
          color: 'var(--terra-700)',
          fontSize: 12, fontWeight: 600, letterSpacing: '0.04em',
          marginBottom: 22
        }}>
          <span style={{ fontSize: 14 }}>✺</span> EST. 2018 · ตามสั่งคุณแม่
        </div>

        <h1 style={{
          fontFamily: 'var(--font-display)',
          fontSize: 56, lineHeight: 0.95, fontWeight: 700,
          color: 'var(--brown-900)', margin: 0,
          letterSpacing: '-0.01em'
        }}>{t(lang, 'brand')}</h1>
        <div style={{
          fontSize: 15, color: 'var(--brown-700)', marginTop: 10,
          fontWeight: 400, margin: "10px 0px 0px", height: "20px", textAlign: "center", letterSpacing: "0px", lineHeight: "0.4"
        }}>{t(lang, 'tagline')}</div>
      </div>

      {/* hero plate */}
      <div style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '20px 0', position: 'relative' }}>
        <div style={{
          width: 260, height: 260, borderRadius: '50%',
          background:
          'radial-gradient(circle at 30% 30%, #FFFCF5 0%, #F5EBD7 35%, #ECD9B6 65%, #C8A572 100%)',
          boxShadow: '0 30px 60px -20px rgba(94,56,22,0.4), inset 0 -10px 20px rgba(94,56,22,0.15), inset 0 4px 12px rgba(255,255,255,0.6)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          position: 'relative'
        }}>
          <div style={{
            width: 180, height: 180, borderRadius: '50%',
            background:
            'repeating-conic-gradient(from 0deg, #E8B473 0deg 30deg, #D89738 30deg 60deg)',
            opacity: 0.7,
            filter: 'blur(4px)',
            mask: 'radial-gradient(circle, black 60%, transparent 100%)',
            WebkitMask: 'radial-gradient(circle, black 60%, transparent 100%)'
          }} />
          <div style={{
            position: 'absolute', color: 'rgba(42,26,14,0.55)',
            fontFamily: 'ui-monospace, monospace', fontSize: 12,
            letterSpacing: '0.08em'
          }}>[ photo of mom's signature dish ]</div>
        </div>
      </div>

      <div style={{ padding: '0 24px' }} className="fade-up">
        <div style={{
          background: 'var(--paper)',
          borderRadius: 22,
          padding: '18px 20px',
          boxShadow: 'var(--shadow-md)',
          border: '1px solid var(--cream-200)',
          marginBottom: 14,
          display: 'flex', alignItems: 'center', gap: 14
        }}>
          <div style={{
            width: 52, height: 52, borderRadius: 14,
            background: 'var(--terra-100)',
            color: 'var(--terra-700)',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: 'var(--font-display)', fontSize: 28, fontWeight: 700,
            border: '1px dashed var(--terra)'
          }}>{tableNo}</div>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 12, color: 'var(--brown-500)', fontWeight: 500 }}>{t(lang, 'welcome_hi')} 🌿</div>
            <div style={{ fontSize: 16, fontWeight: 600, color: 'var(--brown-900)' }}>
              {t(lang, 'welcome_table')} {tableNo}
            </div>
          </div>
        </div>

        <button onClick={onStart} className="press" style={{
          width: '100%', padding: '16px',
          background: 'var(--terra)',
          color: 'var(--ink-on-terra)',
          borderRadius: 18,
          fontSize: 17, fontWeight: 600,
          boxShadow: '0 10px 24px -10px rgba(194,65,12,0.6)',
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8
        }}>
          {t(lang, 'welcome_start')}
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
            <path d="M5 12h14M13 6l6 6-6 6" />
          </svg>
        </button>
        <div style={{
          textAlign: 'center', fontSize: 11, color: 'var(--brown-500)',
          marginTop: 12
        }}>{t(lang, 'welcome_note')}</div>
      </div>
    </div>);

}

// ─────────────────────────────────────────────────────────────
// MENU
// ─────────────────────────────────────────────────────────────
function MenuItemCard({ item, lang, layout, onTap }) {
  const name = lang === 'th' ? item.th : item.en;
  const desc = lang === 'th' ? item.descTh : item.descEn;
  const phLabel = lang === 'th' ? `[ ${item.th} ]` : `[ ${item.en} ]`;

  if (layout === 'list') {
    return (
      <button onClick={onTap} className="press" style={{
        width: '100%',
        display: 'flex', gap: 14, alignItems: 'stretch',
        background: 'var(--paper)',
        borderRadius: 18, padding: 10,
        border: '1px solid var(--cream-200)',
        boxShadow: 'var(--shadow-sm)',
        textAlign: 'left'
      }}>
        <div style={{
          width: 88, height: 88, borderRadius: 12, overflow: 'hidden', flexShrink: 0
        }}>
          <FoodPlaceholder c1={item.c1} c2={item.c2} label={phLabel} size="sm" />
        </div>
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'space-between', minWidth: 0, padding: '2px 0' }}>
          <div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 2 }}>
              <div style={{ fontSize: 15, fontWeight: 600, color: 'var(--brown-900)' }}>{name}</div>
              {item.popular &&
              <span style={{
                fontSize: 9, fontWeight: 600, padding: '2px 6px', borderRadius: 4,
                background: 'var(--mustard-100)', color: 'var(--mustard)',
                letterSpacing: '0.04em'
              }}>{t(lang, 'popular').toUpperCase()}</span>
              }
              {item.spicy >= 2 && <ChiliIcons n={item.spicy} size={10} />}
            </div>
            <div style={{
              fontSize: 12, color: 'var(--brown-500)', lineHeight: 1.35,
              overflow: 'hidden', display: '-webkit-box', WebkitLineClamp: 2,
              WebkitBoxOrient: 'vertical'
            }}>{desc}</div>
          </div>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
            <div style={{
              fontFamily: 'var(--font-display)', fontSize: 18, fontWeight: 700,
              color: 'var(--terra-700)'
            }}>{fmtBaht(item.basePrice)}</div>
            <div style={{
              width: 26, height: 26, borderRadius: 8,
              background: 'var(--terra)', color: 'white',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontSize: 18, lineHeight: 0
            }}>+</div>
          </div>
        </div>
      </button>);

  }

  // grid
  return (
    <button onClick={onTap} className="press" style={{
      display: 'flex', flexDirection: 'column',
      background: 'var(--paper)',
      borderRadius: 18, padding: 8,
      border: '1px solid var(--cream-200)',
      boxShadow: 'var(--shadow-sm)',
      textAlign: 'left',
      gap: 8
    }}>
      <div style={{ height: 100, borderRadius: 12, overflow: 'hidden', position: 'relative' }}>
        <FoodPlaceholder c1={item.c1} c2={item.c2} label={phLabel} size="sm" />
        {item.popular &&
        <div style={{
          position: 'absolute', top: 6, left: 6,
          background: 'var(--brown-900)', color: 'var(--cream-50)',
          fontSize: 10, fontWeight: 700, letterSpacing: '0.02em',
          padding: '3px 7px', borderRadius: 4,
          display: 'inline-flex', alignItems: 'center', gap: 3,
          whiteSpace: 'nowrap'
        }}>
            <span style={{ color: 'var(--mustard)' }}>★</span> {lang === 'th' ? 'ฮิต' : 'HOT'}
          </div>
        }
        {item.spicy >= 2 &&
        <div style={{
          position: 'absolute', top: 6, right: 6,
          background: 'rgba(255,252,245,0.92)', padding: '3px 5px', borderRadius: 6
        }}><ChiliIcons n={item.spicy} size={9} /></div>
        }
      </div>
      <div style={{ padding: '2px 4px 4px', display: 'flex', flexDirection: 'column', gap: 4, flex: 1 }}>
        <div style={{
          fontSize: 14, fontWeight: 600, color: 'var(--brown-900)', lineHeight: 1.2
        }}>{name}</div>
        <div style={{
          fontSize: 11, color: 'var(--brown-500)', lineHeight: 1.3,
          overflow: 'hidden', display: '-webkit-box', WebkitLineClamp: 2,
          WebkitBoxOrient: 'vertical', flex: 1
        }}>{desc}</div>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 2 }}>
          <div style={{
            fontFamily: 'var(--font-display)', fontSize: 16, fontWeight: 700,
            color: 'var(--terra-700)'
          }}>{fmtBaht(item.basePrice)}</div>
          <div style={{
            width: 24, height: 24, borderRadius: 7,
            background: 'var(--terra)', color: 'white',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 16, lineHeight: 0
          }}>+</div>
        </div>
      </div>
    </button>);

}

function MenuScreen({ lang, layout, setLayout, onOpenItem, tableNo }) {
  const [cat, setCat] = useState('all');
  const [q, setQ] = useState('');

  const filtered = useMemo(() => {
    return window.MENU.filter((m) => {
      if (cat !== 'all' && m.cat !== cat) return false;
      if (q) {
        const hay = (m.th + ' ' + m.en + ' ' + m.descTh + ' ' + m.descEn).toLowerCase();
        if (!hay.includes(q.toLowerCase())) return false;
      }
      return true;
    });
  }, [cat, q]);

  const popular = useMemo(() => window.MENU.filter((m) => m.popular).slice(0, 5), []);

  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'auto', background: 'var(--cream-50)' }} className="scroll-hide">
      {/* Header */}
      <AppHeader
        title={t(lang, 'menu_title')}
        subtitle={`${t(lang, 'welcome_table')} ${tableNo} · ${t(lang, 'brand')}`}
        right={
        <div style={{ display: 'flex', background: 'var(--paper)', borderRadius: 999, padding: 3, border: '1px solid var(--cream-200)' }}>
            {['grid', 'list'].map((L) =>
          <button key={L} onClick={() => setLayout(L)} className="press" style={{
            padding: '6px 10px', borderRadius: 999,
            background: layout === L ? 'var(--brown-900)' : 'transparent',
            color: layout === L ? 'var(--cream-50)' : 'var(--brown-500)'
          }} aria-label={L}>
                {L === 'grid' ?
            <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><rect x="1" y="1" width="6" height="6" rx="1" /><rect x="9" y="1" width="6" height="6" rx="1" /><rect x="1" y="9" width="6" height="6" rx="1" /><rect x="9" y="9" width="6" height="6" rx="1" /></svg> :
            <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><rect x="1" y="2" width="14" height="2" rx="1" /><rect x="1" y="7" width="14" height="2" rx="1" /><rect x="1" y="12" width="14" height="2" rx="1" /></svg>
            }
              </button>
          )}
          </div>
        } />
      

      {/* Search */}
      <div style={{ padding: '0 18px 12px' }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          background: 'var(--paper)', padding: '10px 14px', borderRadius: 14,
          border: '1px solid var(--cream-200)'
        }}>
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="var(--brown-500)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <circle cx="11" cy="11" r="7" /><path d="m20 20-3.5-3.5" />
          </svg>
          <input
            value={q}
            onChange={(e) => setQ(e.target.value)}
            placeholder={t(lang, 'menu_search')}
            style={{
              flex: 1, border: 'none', outline: 'none', background: 'transparent',
              fontSize: 14, color: 'var(--brown-900)'
            }} />
          
          {q &&
          <button onClick={() => setQ('')} style={{ color: 'var(--brown-500)', fontSize: 18 }}>×</button>
          }
        </div>
      </div>

      {/* Categories */}
      <div style={{
        display: 'flex', gap: 8, padding: '0 18px 16px',
        overflowX: 'auto'
      }} className="scroll-hide">
        {window.CATEGORIES.map((c) =>
        <Chip key={c.id} active={cat === c.id} onClick={() => setCat(c.id)}>
            {lang === 'th' ? c.th : c.en}
          </Chip>
        )}
      </div>

      {/* Popular strip (only when viewing all) */}
      {cat === 'all' && !q &&
      <div style={{ marginBottom: 18 }}>
          <div style={{
          display: 'flex', alignItems: 'baseline', justifyContent: 'space-between',
          padding: '0 18px 8px', gap: 10
        }}>
            <div style={{
            fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 700,
            color: 'var(--brown-900)', whiteSpace: 'nowrap'
          }}>
              <span style={{ color: 'var(--terra)' }}>♥</span> {t(lang, 'popular')}
            </div>
            <div style={{ fontSize: 11, color: 'var(--brown-500)', whiteSpace: 'nowrap' }}>5/{window.MENU.length}</div>
          </div>
          <div style={{
          display: 'flex', gap: 12, padding: '0 18px',
          overflowX: 'auto'
        }} className="scroll-hide">
            {popular.map((m) => {
            const name = lang === 'th' ? m.th : m.en;
            return (
              <button key={m.id} onClick={() => onOpenItem(m)} className="press" style={{
                flexShrink: 0, width: 140,
                display: 'flex', flexDirection: 'column', gap: 6,
                background: 'var(--paper)', padding: 8, borderRadius: 16,
                border: '1px solid var(--cream-200)', textAlign: 'left',
                boxShadow: 'var(--shadow-sm)'
              }}>
                  <div style={{ height: 92, borderRadius: 10, overflow: 'hidden' }}>
                    <FoodPlaceholder c1={m.c1} c2={m.c2} label={`[ ${name} ]`} size="sm" />
                  </div>
                  <div style={{ fontSize: 13, fontWeight: 600, color: 'var(--brown-900)', lineHeight: 1.2 }}>{name}</div>
                  <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14, color: 'var(--terra-700)' }}>{fmtBaht(m.basePrice)}</div>
                </button>);

          })}
          </div>
        </div>
      }

      {/* Section header */}
      <div style={{
        padding: '0 18px 10px', gap: 10,
        display: 'flex', alignItems: 'baseline', justifyContent: 'space-between'
      }}>
        <div style={{
          fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 700,
          color: 'var(--brown-900)', whiteSpace: 'nowrap'
        }}>
          {cat === 'all' ?
          lang === 'th' ? 'เมนูทั้งหมด' : 'Full menu' :
          lang === 'th' ? window.CATEGORIES.find((c) => c.id === cat).th : window.CATEGORIES.find((c) => c.id === cat).en
          }
        </div>
        <div style={{ fontSize: 12, color: 'var(--brown-500)', whiteSpace: 'nowrap' }}>
          {filtered.length} {lang === 'th' ? 'รายการ' : 'items'}
        </div>
      </div>

      {/* Items */}
      <div style={{
        padding: '0 18px 140px',
        display: layout === 'grid' ? 'grid' : 'flex',
        gridTemplateColumns: layout === 'grid' ? '1fr 1fr' : undefined,
        flexDirection: layout === 'list' ? 'column' : undefined,
        gap: 12
      }}>
        {filtered.map((m) =>
        <MenuItemCard key={m.id} item={m} lang={lang} layout={layout} onTap={() => onOpenItem(m)} />
        )}
        {filtered.length === 0 &&
        <div style={{
          gridColumn: '1 / -1',
          textAlign: 'center', padding: '40px 0',
          color: 'var(--brown-500)', fontSize: 14
        }}>
            {lang === 'th' ? 'ไม่พบเมนูที่ค้นหา' : 'No menu items found'}
          </div>
        }
      </div>
    </div>);

}

// expose
Object.assign(window, {
  FoodPlaceholder, ChiliIcons, Chip, QtyStepper, BottomNav, AppHeader,
  fmtBaht, tt: t,
  WelcomeScreen, MenuScreen
});