// Données partagées : menu réel Pizzeria ILYES (prix en DA)

const ITEMS = [
  // ===== PIZZAS (taille standard) =====
  { id: 'p-marg',      name: 'Margherite',     italianName: 'La Classica',    ingredients: 'Sauce tomate, fromage, olives',                                          price: 350,  badge: 'veg',     category: 'pizzas',     featured: true, photo: 'assets/produits/marguerite.png' },
  { id: 'p-veg',       name: 'Végétarienne',   italianName: 'Du Maraîcher',   ingredients: 'Sauce tomate, fromage, poivrons, oignons, champignons, olives',           price: 500,  badge: 'veg',     category: 'pizzas',     photo: 'assets/produits/vegetarienne.png' },
  { id: 'p-poulhach',  name: 'Poulet Hachée',  italianName: 'Double viande',  ingredients: 'Sauce tomate, fromage, poulet haché, olives',                                  price: 500,  badge: null,      category: 'pizzas',     featured: true, photo: 'assets/produits/poulet-hachee.png' },
  { id: 'p-napo',      name: 'Napolitaine',    italianName: 'La Napoletana',  ingredients: 'Sauce tomate, fromage, viande hachée, olives',                           price: 600,  badge: null,      category: 'pizzas',     photo: 'assets/produits/napolitaine.png' },
  { id: 'p-sici',      name: 'Sicilienne',     italianName: 'Au thon',        ingredients: 'Sauce tomate, fromage, thon, olives',                                    price: 600,  badge: null,      category: 'pizzas',     photo: 'assets/produits/sicilienne.png' },
  { id: 'p-fore',      name: 'Forestière',     italianName: 'Aux champignons',ingredients: 'Sauce tomate, fromage, champignons, olives',                             price: 600,  badge: 'veg',     category: 'pizzas',     photo: 'assets/produits/forestiere.png' },
  { id: 'p-ferm',      name: 'Fermière',       italianName: 'La rustique',    ingredients: 'Sauce tomate, fromage, poulet, olives',                                  price: 600,  badge: null,      category: 'pizzas',     photo: 'assets/produits/fermiere.png' },
  { id: 'p-pepp',      name: 'Pepperoni',      italianName: 'Viande fumée',   ingredients: 'Sauce tomate, fromage, viande fumée, olives',                            price: 600,  badge: 'spicy',   category: 'pizzas',     photo: 'assets/produits/pepperoni.png' },
  { id: 'p-orient',    name: 'Orientale',      italianName: "À l'œuf",        ingredients: 'Sauce tomate, fromage, viande hachée, œuf, olives',                      price: 650,  badge: null,      category: 'pizzas',     photo: 'assets/produits/orientale.png' },
  { id: 'p-4sai',      name: '4 Saisons',      italianName: 'Les 4 saveurs',  ingredients: 'Sauce tomate, fromage, viande hachée, champignons, olives',              price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/4-saisons.png' },
  { id: 'p-capri',     name: 'Capri',          italianName: 'Méditerranéenne',ingredients: 'Sauce tomate, fromage, viande hachée, thon, olives',                     price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/capri.png' },
  { id: 'p-cata',      name: 'Catalone',       italianName: 'Aux champignons',ingredients: 'Sauce tomate, fromage, thon, champignons, olives',                       price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/catalone.png' },
  { id: 'p-ind',       name: 'Indienne',       italianName: 'Au poulet',      ingredients: 'Sauce tomate, fromage, poulet, champignons, olives',                     price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/indienne.png' },
  { id: 'p-paris',     name: 'Parisienne',     italianName: 'Charcutière',    ingredients: 'Sauce tomate, fromage, viande hachée, viande fumée, champignons, olives',price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/parisienne.png' },
  { id: 'p-mex',       name: 'Mexicaine',      italianName: 'La piquante',    ingredients: 'Sauce tomate, fromage, viande hachée, poivre, olives vertes',            price: 700,  badge: 'spicy',   category: 'pizzas',     photo: 'assets/produits/mexicaine.png' },
  { id: 'p-supr',      name: 'Suprême',        italianName: 'Sauce blanche',  ingredients: 'Sauce blanche, fromage, poulet, dinde fumée, olives',                    price: 700,  badge: null,      category: 'pizzas',     photo: 'assets/produits/supreme.png' },
  { id: 'p-oran',      name: 'Oranaise',       italianName: 'Sauce blanche',  ingredients: 'Sauce blanche, fromage, viande hachée, poulet',                          price: 800,  badge: null,      category: 'pizzas',     photo: 'assets/produits/oranaise.png' },
  { id: 'p-4fro',      name: '4 Fromages',     italianName: 'Aux 4 fromages', ingredients: 'Sauce blanche, cheddar, gouda, gruyère, camembert, olives',              price: 800,  badge: 'veg',     category: 'pizzas',     featured: true, photo: 'assets/produits/4-fromages.png' },
  { id: 'p-roy',       name: 'Royale',         italianName: 'La complète',    ingredients: 'Sauce tomate, fromage, viande hachée, thon, champignons, olives',        price: 800,  badge: null,      category: 'pizzas',     photo: 'assets/produits/royale.png' },
  { id: 'p-mais',      name: 'Maison',         italianName: 'Choix du chef',  ingredients: 'La signature de la maison — composition au choix du chef',               price: 1200, badge: 'premium', category: 'pizzas',     featured: true, photo: 'assets/produits/maison.png' },

  // ===== PIZZA MÉGA (XL) =====
  { id: 'm-poulhach',  name: 'Poulet Hachée XL',  italianName: 'Format géant',     ingredients: 'Sauce tomate, poulet, viande hachée, fromage, olives — format XL',                       price: 1500, badge: null,      category: 'mega', photo: 'assets/produits/mega-poulet-hache.png' },
  { id: 'm-choix',     name: 'Pizza au choix XL', italianName: 'Sur mesure',       ingredients: 'À choisir : viande hachée, poulet ou thon — format XL',                                  price: 1800, badge: null,      category: 'mega',  featured: true, photo: 'assets/produits/mega-choix.png' },
  { id: 'm-oran',      name: 'Oranaise XL',       italianName: 'Format géant',     ingredients: 'Poulet, viande hachée, sauce blanche, fromage — format XL',                              price: 2000, badge: null,      category: 'mega', photo: 'assets/produits/mega-oranaise.png' },
  { id: 'm-capri',     name: 'Caprisiosa XL',     italianName: 'Format géant',     ingredients: 'Viande hachée, thon, fromage, olives — format XL',                                       price: 2000, badge: null,      category: 'mega', photo: 'assets/produits/mega-caprisiosa.png' },
  { id: 'm-roy',       name: 'Royal XL',          italianName: 'La complète',      ingredients: 'Viande hachée, thon, champignon, fromage, olives — format XL',                            price: 2500, badge: null,      category: 'mega', photo: 'assets/produits/mega-royal.png' },
  { id: 'm-chef',      name: 'Chef XL',           italianName: 'La signature',     ingredients: 'Bordure fromage, fruits de mer, 4 fromages, poulet — format XL',                          price: 3000, badge: 'premium', category: 'mega', featured: true, photo: 'assets/produits/mega-chef.png' },

  // ===== SANDWICHS =====
  { id: 's-simple',    name: 'Spécial Simple',  italianName: 'Le classique',   ingredients: 'Frite, fromage, œuf, viande hachée',                                      price: 200,  badge: null,    category: 'sandwichs', photo: 'assets/produits/sand-special-simple.jpg' },
  { id: 's-double',    name: 'Spécial Double',  italianName: 'Double portion', ingredients: 'Frite, fromage, œuf, viande hachée double',                              price: 300,  badge: null,    category: 'sandwichs', photo: 'assets/produits/sand-special-double.jpg' },
  { id: 's-libanais',  name: 'Libanais',        italianName: 'Du Liban',       ingredients: 'Fromage, frite, poulet',                                                  price: 350,  badge: null,    category: 'sandwichs', photo: 'assets/produits/sand-libanais.jpg' },
  { id: 's-pouldoub',  name: 'Poulet Double',   italianName: 'Double poulet',  ingredients: 'Fromage, frite, poulet double',                                           price: 500,  badge: null,    category: 'sandwichs',  featured: true, photo: 'assets/produits/sand-poulet-double.jpg' },
  { id: 's-4fro',      name: '4 Fromages',      italianName: 'Aux 4 fromages', ingredients: 'Gouda, gruyère, camembert, mozzarella',                                   price: 600,  badge: 'veg',   category: 'sandwichs', photo: 'assets/produits/sand-4-fromages.jpg' },
  { id: 's-emmental',  name: 'Emmental',        italianName: 'Sauce fromage',  ingredients: 'Sauce fromage, frite, poulet',                                            price: 500,  badge: null,    category: 'sandwichs', photo: 'assets/produits/sand-emmental.jpg' },
  { id: 's-viande',    name: 'Viande',          italianName: 'Sauce fromage',  ingredients: 'Sauce fromage, frite, viande hachée',                                     price: 500,  badge: null,    category: 'sandwichs', photo: 'assets/produits/sand-viande.jpg' },
  { id: 's-mixte',     name: 'Mixte',           italianName: 'Poulet + viande',ingredients: 'Sauce fromage, frite, poulet, viande hachée',                            price: 600,  badge: null,    category: 'sandwichs',  featured: true, photo: 'assets/produits/sand-mixte.jpg' },

  // ===== TACOS =====
  { id: 't-poulet',    name: 'Tacos Poulet',    italianName: 'À la crème',     ingredients: 'Frite, sauce fromage, poulet à la crème',                                 price: 600,  badge: null,    category: 'tacos',  featured: true, photo: 'assets/produits/tacos-poulet.png' },
  { id: 't-viande',    name: 'Tacos Viande',    italianName: 'À la crème',     ingredients: 'Frite, sauce fromage, viande hachée à la crème',                          price: 600,  badge: null,    photo: 'assets/produits/tacos-viande.png',    category: 'tacos' },
  { id: 't-mixte',     name: 'Tacos Mixte',     italianName: 'Poulet + viande',ingredients: 'Frite, sauce fromage, viande hachée, poulet',                            price: 800,  badge: null,    photo: 'assets/produits/tacos-mixte.png',  category: 'tacos' },
  { id: 't-paris',     name: 'Tacos Parisien',  italianName: 'Viande fumée',   ingredients: 'Frite, sauce fromage, poulet, viande fumée',                              price: 700,  badge: null,    photo: 'assets/produits/tacos-parisien.png',  category: 'tacos' },
  { id: 't-mex',       name: 'Tacos Mexicain',  italianName: 'Au poivre',      ingredients: 'Frite, sauce fromage, viande hachée, poivre',                             price: 700,  badge: 'spicy', photo: 'assets/produits/tacos-mexicain.png',  category: 'tacos' },
  { id: 't-ind',       name: 'Tacos Indien',    italianName: 'Champignons',    ingredients: 'Frite, sauce fromage, poulet, champignon à la crème',                     price: 700,  badge: null,    photo: 'assets/produits/tacos-indien.png',  category: 'tacos' },
  { id: 't-supr',      name: 'Tacos Suprême',   italianName: 'Dinde fumée',    ingredients: 'Frite, sauce fromage, poulet, dinde fumée',                               price: 700,  badge: 'premium', photo: 'assets/produits/tacos-supreme.png', category: 'tacos' },

  // ===== CRISPY (Boxes & Poutines) =====
  { id: 'c-boxs',      name: 'Box S',           italianName: '3 morceaux',     ingredients: '3 morceaux de poulet crispy (100% filet), frites, sauces gratuites',     price: 550,  badge: null,    photo: 'assets/produits/box-crispy.jpg', category: 'crispy',  featured: true },
  { id: 'c-boxm',      name: 'Box M',           italianName: '5 morceaux',     ingredients: '5 morceaux de poulet crispy, frites, sauces gratuites',                  price: 850,  badge: null,    photo: 'assets/produits/box-crispy.jpg', category: 'crispy' },
  { id: 'c-boxxl',     name: 'Box XL',          italianName: '8 morceaux',     ingredients: '8 morceaux de poulet crispy, frites, sauces gratuites',                  price: 1300, badge: 'premium', photo: 'assets/produits/box-crispy.jpg', category: 'crispy' },
  { id: 'c-poutc',     name: 'Poutine Crispy',  italianName: '3 morceaux',  ingredients: 'Frites, 3 morceaux poulet crispy coupé, sauce fromagère',                price: 500,  badge: null,    photo: 'assets/produits/poutine-crispy.jpg', category: 'crispy' },
  { id: 'c-poutalg',   name: 'Poutine Algérien', italianName: '5 morceaux',  ingredients: 'Frites, 5 morceaux poulet crispy coupé, sauce fromagère et algérienne',  price: 800,  badge: null,    photo: 'assets/produits/poutine-crispy.jpg', category: 'crispy',  featured: true },
  { id: 'c-poutxl',    name: 'Poutine Party XL',italianName: '7 morceaux',  ingredients: 'Frites, 7 morceaux poulet crispy coupé, sauce fromagère et algérienne',  price: 1200, badge: 'premium', photo: 'assets/produits/poutine-crispy.jpg', category: 'crispy' },

  // ===== HAMBURGERS =====
  { id: 'b-amer',      name: 'Simple Américain', italianName: 'Le classique',  ingredients: 'Pain burger, viande, salade, fromage, sauce burger',                       price: 450,  badge: null,    category: 'burgers',  featured: true, photo: 'assets/produits/burger-simple-americain.png' },
  { id: 'b-pan',       name: 'Simple Panini',    italianName: 'Format panini', ingredients: 'Pain panini grillé, viande, salade, fromage',                              price: 350,  badge: null,    category: 'burgers', photo: 'assets/produits/burger-simple-panini.png' },
  { id: 'b-double',    name: 'Double Américain', italianName: 'Double viande', ingredients: 'Pain burger, double viande, salade, fromage, sauce burger',               price: 600,  badge: null,    category: 'burgers', photo: 'assets/produits/burger-double-americain.png' },
  { id: 'b-doublepan', name: 'Double Panini',    italianName: 'Double viande', ingredients: 'Pain panini grillé, double viande, salade, fromage',                       price: 500,  badge: null,    category: 'burgers', photo: 'assets/produits/burger-double-panini.png' },
  { id: 'b-crispy',    name: 'Crispy Burger',    italianName: 'Au poulet',     ingredients: 'Pain burger, poulet crispy, salade, fromage, sauce',                       price: 500,  badge: null,    category: 'burgers', photo: 'assets/produits/burger-crispy.png' },

  // ===== CHEESE =====
  { photo: 'assets/produits/cheese-simple-americain.png', id: 'b-camer',     name: 'Cheese Américain', italianName: 'Cheddar',       ingredients: 'Pain burger, viande, cheddar fondu, sauce burger',                          price: 400,  badge: null,    category: 'cheese',  featured: true },
  { photo: 'assets/produits/cheese-simple-panini.png', id: 'b-cpan',      name: 'Cheese Panini',    italianName: 'Cheddar',       ingredients: 'Pain panini, viande, cheddar fondu',                                        price: 300,  badge: null,    category: 'cheese' },
  { photo: 'assets/produits/cheese-double-americain.png', id: 'b-cdouble',   name: 'Cheese Double Américain', italianName: 'Double cheddar', ingredients: 'Pain burger, double viande, double cheddar',                        price: 500,  badge: null,    category: 'cheese' },
  { photo: 'assets/produits/cheese-double-panini.png', id: 'b-cdoublepan',name: 'Cheese Double Panini', italianName: 'Double cheddar', ingredients: 'Pain panini, double viande, double cheddar',                            price: 500,  badge: null,    category: 'cheese' },
  { photo: 'assets/produits/cheese-chicken-americain.png', id: 'b-cchik',     name: 'Cheese Chicken Américain', italianName: 'Poulet cheddar', ingredients: 'Pain burger, poulet, cheddar fondu',                                price: 350,  badge: null,    category: 'cheese' },
  { photo: 'assets/produits/cheese-chicken-panini.png', id: 'b-cchikpan',  name: 'Cheese Chicken Panini', italianName: 'Poulet cheddar', ingredients: 'Pain panini, poulet, cheddar fondu',                                   price: 300,  badge: null,    category: 'cheese' },

  // ===== PLATS =====
  { id: 'pl-vh',       name: 'Viande Hachée',    italianName: 'Plat complet',  ingredients: 'Frite, œuf, viande hachée, salade, tomate, macédoine',                     price: 700,  badge: null,    category: 'plats',  featured: true, photo: 'assets/produits/plat-viande.png' },
  { id: 'pl-vhdoub',   name: 'Viande Hachée Double', italianName: 'Double portion', ingredients: 'Frite, œuf, viande hachée double, salade, tomate, macédoine',          price: 800,  badge: null,    category: 'plats', photo: 'assets/produits/plat-viande.png' },
  { id: 'pl-poul',     name: 'Poulet',           italianName: 'Poulet mariné', ingredients: 'Frite, poulet mariné, salade, tomate, macédoine',                          price: 700,  badge: null,    category: 'plats', photo: 'assets/produits/plat-poulet.png' },
  { id: 'pl-pouldoub', name: 'Poulet Double',    italianName: 'Double portion',ingredients: 'Frite, poulet mariné double, salade, tomate, macédoine',                  price: 800,  badge: null,    category: 'plats', photo: 'assets/produits/plat-poulet.png' },
  { id: 'pl-crem',     name: 'Poulet à la Crème', italianName: 'Sauce crème',  ingredients: 'Frite, poulet à la crème, salade, tomate, macédoine',                      price: 800,  badge: null,    category: 'plats', photo: 'assets/produits/plat-poulet-creme.png' },
  { id: 'pl-farci',    name: 'Poulet Farci',     italianName: 'Spécialité',    ingredients: 'Frite, poulet farci, salade, tomate, macédoine',                           price: 1000, badge: 'premium', category: 'plats', photo: 'assets/produits/plat-poulet-farci.png' },
  { id: 'pl-grille',   name: 'Poulet Grillé',    italianName: 'Au charbon',    ingredients: 'Frite, poulet grillé, salade, tomate, macédoine',                          price: 700,  badge: null,    category: 'plats', photo: 'assets/produits/plat-poulet.png' },
  { id: 'pl-mixte',    name: 'Mixte',            italianName: 'Tout en un',    ingredients: 'Frite, poulet, viande hachée, salade, tomate, macédoine, œuf',            price: 1000, badge: 'premium', category: 'plats',  featured: true, photo: 'assets/produits/plat-mixte.png' },

  // ===== FRITES =====
  { photo: 'assets/produits/frites.png', id: 'f-petite',    name: 'Petite Frites',    italianName: 'La barquette',  ingredients: 'Frites maison, sel marin, sauce au choix',                                 price: 200,  badge: null,    category: 'frites' },
  { photo: 'assets/produits/frites.png', id: 'f-grande',    name: 'Grande Frites',    italianName: 'Format familial',ingredients: 'Grande barquette de frites maison, sauce au choix',                       price: 400,  badge: null,    category: 'frites',  featured: true },
];

const CATEGORIES = [
  { id: 'pizzas',    label: 'Nos pizzas',     tagline: 'Au feu de bois, dès 350 DA' },
  { id: 'mega',      label: 'Pizza Méga',     tagline: 'Format XL, à partager' },
  { id: 'sandwichs', label: 'Nos sandwichs',  tagline: 'Pain frais, généreux' },
  { id: 'tacos',     label: 'Nos tacos',      tagline: 'Sauce fromage à la crème' },
  { id: 'crispy',    label: 'Crispy & Box',   tagline: 'Poulet 100% filet' },
  { id: 'burgers',   label: 'Hamburgers',     tagline: 'Américain ou panini' },
  { id: 'cheese',    label: 'Cheese',         tagline: 'Cheddar fondu, généreux' },
  { id: 'plats',     label: 'Nos plats',      tagline: 'Frite, salade, garniture' },
  { id: 'frites',    label: 'Frites',         tagline: 'Petite ou grande barquette' },
];

// ===== Sauces (toutes gratuites sauf fromagère) =====
const SAUCES = [
  { id: 'mayo',      label: 'Mayonnaise',     price: 0 },
  { id: 'ketchup',   label: 'Ketchup',        price: 0 },
  { id: 'harissa',   label: 'Harissa',        price: 0 },
  { id: 'moutarde',  label: 'Moutarde',       price: 0 },
  { id: 'algerienne',label: 'Algérienne',     price: 0 },
  { id: 'ail',       label: "Sauce à l'ail",   price: 0 },
  { id: 'barbecue',  label: 'Barbecue',       price: 0 },
  { id: 'fromagere', label: 'Sauce fromagère',price: 50 },
];

// Catégories qui ont droit à une sauce
const CATS_WITH_SAUCE = ['sandwichs', 'tacos', 'crispy', 'burgers', 'cheese', 'frites'];

function itemHasSauce(item) {
  return CATS_WITH_SAUCE.includes(item.category);
}

// ===== Modes de service =====
const SERVICE_MODES = [
  { id: 'sur-place',  label: 'Sur place',     fee: 0,    icon: '🍽' },
  { id: 'emporter',   label: 'À emporter',    fee: 0,    icon: '🛍' },
  { id: 'livraison',  label: 'Livraison',     fee: 200,  icon: '🛵' },
];
const SERVICE_KEY = 'pizzeria-ilyes-service';

// ===== Devise =====
const CURRENCY = 'DA';
function formatPrice(n) {
  return Math.round(n) + ' ' + CURRENCY;
}

// Cart state — partagé entre toutes les pages via localStorage
const CART_KEY = 'pizzeria-ilyes-cart';

function loadCart() {
  try {
    const raw = JSON.parse(localStorage.getItem(CART_KEY));
    if (!Array.isArray(raw)) return [];
    // Migration : ancien format sauceId (singulier) -> sauceIds (pluriel)
    return raw.map(line => {
      if (line.sauceIds && Array.isArray(line.sauceIds)) return line;
      const ids = line.sauceId ? [line.sauceId] : [];
      const { sauceId, ...rest } = line;
      return { ...rest, sauceIds: ids };
    });
  } catch { return []; }
}

function saveCart(cart) {
  localStorage.setItem(CART_KEY, JSON.stringify(cart));
  window.dispatchEvent(new CustomEvent('cart-changed'));
}

function useCart() {
  const [cart, setCart] = React.useState(loadCart);
  React.useEffect(() => {
    const handler = () => setCart(loadCart());
    window.addEventListener('cart-changed', handler);
    window.addEventListener('storage', handler);
    return () => {
      window.removeEventListener('cart-changed', handler);
      window.removeEventListener('storage', handler);
    };
  }, []);
  return [cart, setCart];
}

function addToCart(itemId, opts = {}) {
  const cart = loadCart();
  const sauceIds = Array.isArray(opts.sauceIds) ? opts.sauceIds : (opts.sauceId ? [opts.sauceId] : []);
  // Fusionner avec une ligne identique (même item + mêmes sauces)
  const sameSet = (a, b) => a.length === b.length && a.every(x => b.includes(x));
  const existing = cart.find(l => l.itemId === itemId && sameSet(l.sauceIds || [], sauceIds));
  if (existing) {
    existing.qty += 1;
  } else {
    cart.push({
      uid: itemId + '_' + Math.random().toString(36).slice(2, 8),
      itemId, qty: 1, sauceIds,
    });
  }
  saveCart(cart);
}

function setLineQuantity(uid, qty) {
  const cart = loadCart();
  const line = cart.find(l => l.uid === uid);
  if (!line) return;
  if (qty <= 0) {
    saveCart(cart.filter(l => l.uid !== uid));
  } else {
    line.qty = qty;
    saveCart(cart);
  }
}

function setLineSauce(uid, sauceId) {
  // Compat: garde l'ancienne API mais bascule en mode mono-sauce
  setLineSauces(uid, sauceId ? [sauceId] : []);
}

function setLineSauces(uid, sauceIds) {
  const cart = loadCart();
  const line = cart.find(l => l.uid === uid);
  if (!line) return;
  line.sauceIds = Array.isArray(sauceIds) ? sauceIds : [];
  saveCart(cart);
}

function lineSauces(line) {
  return Array.isArray(line.sauceIds) ? line.sauceIds : (line.sauceId ? [line.sauceId] : []);
}

function lineUnitPrice(line) {
  const item = ITEMS.find(x => x.id === line.itemId);
  if (!item) return 0;
  let p = item.price;
  for (const sid of lineSauces(line)) {
    const s = SAUCES.find(x => x.id === sid);
    if (s) p += s.price;
  }
  return p;
}

function cartSubtotal(cart) {
  return cart.reduce((sum, line) => sum + lineUnitPrice(line) * line.qty, 0);
}

function loadServiceMode() {
  try { return localStorage.getItem(SERVICE_KEY) || 'sur-place'; }
  catch { return 'sur-place'; }
}

function saveServiceMode(mode) {
  try { localStorage.setItem(SERVICE_KEY, mode); } catch {}
  window.dispatchEvent(new CustomEvent('cart-changed'));
}

function useServiceMode() {
  const [mode, setMode] = React.useState(loadServiceMode);
  React.useEffect(() => {
    const handler = () => setMode(loadServiceMode());
    window.addEventListener('cart-changed', handler);
    return () => window.removeEventListener('cart-changed', handler);
  }, []);
  return [mode, (m) => { saveServiceMode(m); setMode(m); }];
}

function serviceFee(mode) {
  const sm = SERVICE_MODES.find(m => m.id === mode);
  return sm ? sm.fee : 0;
}

function cartTotal(cart, mode) {
  return cartSubtotal(cart) + (mode ? serviceFee(mode) : 0);
}

function cartCount(cart) {
  return cart.reduce((s, line) => s + line.qty, 0);
}

const PIZZAS = ITEMS;

window.ITEMS = ITEMS;
window.PIZZAS = ITEMS;
window.CATEGORIES = CATEGORIES;
window.SAUCES = SAUCES;
window.CATS_WITH_SAUCE = CATS_WITH_SAUCE;
window.itemHasSauce = itemHasSauce;
window.SERVICE_MODES = SERVICE_MODES;
window.CURRENCY = CURRENCY;
window.formatPrice = formatPrice;
window.useCart = useCart;
window.useServiceMode = useServiceMode;
window.addToCart = addToCart;
window.setLineQuantity = setLineQuantity;
window.setLineSauce = setLineSauce;
window.setLineSauces = setLineSauces;
window.lineSauces = lineSauces;
window.lineUnitPrice = lineUnitPrice;
window.cartSubtotal = cartSubtotal;
window.cartTotal = cartTotal;
window.cartCount = cartCount;
window.serviceFee = serviceFee;
window.loadCart = loadCart;
