// programs.jsx — "Our Programs / برامجنا": the MegaTech cashier programs, grouped into two
// colour-coded worlds: Retail (blue) then Restaurant (orange). Each holds Windows + Android.
//
// TO ADD SCREENSHOTS: drop the image in assets/programs/ and add a line to the
// matching `shots` list below (e.g. PROGRAMS.windows.modes.pos.shots):
//   { src: "assets/programs/my-shot.png", wide: true, ar: "وصف الصورة", en: "Caption" }
// Use `wide: true` for PC/landscape captures; leave it out for phone captures.
const { Icon: PgIcon } = window;

const PLATFORMS = [
  { id: "windows", icon: "monitor", ar: "ويندوز", en: "Windows" },
  { id: "android", icon: "smartphone", ar: "أندرويد", en: "Android" },
];
const MODES = [
  { id: "pos", icon: "store", ar: "كاشير المحلات", en: "Shop POS",
    title: { ar: "المحلات والتجزئة", en: "Retail & shops" },
    sub: { ar: "للمحلات التجارية والجملة: فواتير، مخازن، باركود وحسابات.", en: "For shops and wholesale: invoices, stock, barcodes and accounts." },
    points: [{ ar: "فواتير بيع وشراء", en: "Sales & purchase invoices" }, { ar: "مخازن وجرد", en: "Stock & counts" }, { ar: "تقارير الأرباح", en: "Profit reports" }] },
  { id: "rest", icon: "utensils", ar: "كاشير المطاعم", en: "Restaurant",
    title: { ar: "المطاعم والكافيهات", en: "Restaurants & cafés" },
    sub: { ar: "للمطاعم والكافيهات: طاولات، مطبخ، تيك أواي ودليفري.", en: "For restaurants and cafés: tables, kitchen, takeaway and delivery." },
    points: [{ ar: "طاولات وصالات", en: "Tables & halls" }, { ar: "شاشات المطبخ", en: "Kitchen screens" }, { ar: "دليفري وتطبيقات التوصيل", en: "Delivery & delivery apps" }] },
];

// Screenshot lists: [file name, Arabic caption, English caption].
const shotList = (prefix, wide, rows) => rows.map(([f, ar, en]) => ({ src: "assets/programs/" + prefix + "-" + f + ".webp", wide, ar, en }));
const RETAIL_SHOTS = shotList("retail", false, [
  ["sale-invoice", "فاتورة مبيعات", "Sales invoice"],
  ["hub-sales", "قائمة المبيعات", "Sales menu"],
  ["purchase-invoice", "فاتورة مشتريات", "Purchase invoice"],
  ["price-quote", "عرض سعر", "Price quote"],
  ["sale-return", "مرتجع مبيعات", "Sales return"],
  ["purchase-return", "مرتجع مشتريات", "Purchase return"],
  ["invoice-details", "تفاصيل الفواتير", "Invoice details"],
  ["net-profit", "صافي الربح", "Net profit"],
  ["turnover", "معدل الدوران", "Turnover"],
  ["hub-stores", "قائمة المخازن", "Stores menu"],
  ["stock-balance", "رصيد المخزن", "Stock balance"],
  ["item-movement", "حركة الصنف", "Item movement"],
  ["item-totals", "إجماليات الأصناف", "Item totals"],
  ["item-sales-purchases", "مبيعات ومشتريات الصنف", "Item sales & purchases"],
  ["reorder-items", "أصناف وصلت حد الطلب", "Items at reorder level"],
  ["expiring-items", "أصناف قريبة من الانتهاء", "Expiring items"],
  ["stores-transfer", "تحويل بين المخازن", "Store transfer"],
  ["stores-adjustment", "تسوية المخزن", "Stock adjustment"],
  ["stores-loss", "التالف", "Damaged stock"],
  ["salesmen", "المندوبون", "Salesmen"],
  ["sales-by-salesman", "المبيعات حسب المندوب", "Sales by salesman"],
  ["sales-by-employee", "المبيعات حسب الموظف", "Sales by employee"],
  ["sales-by-client-type", "المبيعات حسب نوع العميل", "Sales by customer type"],
  ["sales-by-invoice-type", "المبيعات حسب نوع الفاتورة", "Sales by invoice type"],
]).concat(shotList("retail-dark", false, [
  ["sale-invoice", "فاتورة مبيعات — الوضع الداكن", "Sales invoice — dark mode"],
  ["hub-sales", "قائمة المبيعات — الوضع الداكن", "Sales menu — dark mode"],
]));
const REST_SHOTS = shotList("rest", true, [
  ["pos", "كاشير المطعم على التابلت", "Restaurant cashier on a tablet"],
  ["hub-restaurant", "قائمة المطعم", "Restaurant menu"],
  ["tables", "الطاولات", "Tables"],
  ["table-groups", "مجموعات الطاولات", "Table groups"],
  ["addons", "الإضافات", "Add-ons"],
  ["kitchen-screens", "شاشات المطبخ", "Kitchen screens"],
  ["linked-kitchens", "ربط الأصناف بالمطابخ", "Linked kitchens"],
  ["delivery-staff", "الطيارون", "Delivery drivers"],
  ["delivery-report", "تقرير الدليفري", "Delivery report"],
  ["external-apps", "تطبيقات التوصيل", "Delivery apps"],
  ["external-app-invoices", "فواتير تطبيقات التوصيل", "Delivery-app invoices"],
  ["recipe-consumption", "استهلاك المكونات", "Recipe consumption"],
  ["deleted-orders", "الطلبات المحذوفة", "Deleted orders"],
  ["pos-appearance", "تخصيص شكل شاشة الكاشير", "Cashier screen appearance"],
]).concat(shotList("rest-dark", true, [
  ["pos", "كاشير المطعم — الوضع الداكن", "Restaurant cashier — dark mode"],
  ["hub-restaurant", "قائمة المطعم — الوضع الداكن", "Restaurant menu — dark mode"],
]));

const WIN_RETAIL_SHOTS = shotList("win", true, [
  ["main-menu", "الشاشة الرئيسية", "Main screen"],
  ["sales-invoice", "فاتورة مبيعات", "Sales invoice"],
  ["purchase-invoice", "فاتورة مشتريات", "Purchase invoice"],
  ["sales-analysis", "تحليل المبيعات", "Sales analysis"],
  ["sales-details", "تفاصيل المبيعات", "Sales details"],
  ["stock-report", "كشف جرد المخزن", "Stock report"],
  ["item-card", "بيانات الصنف", "Item card"],
  ["item-movement", "حركة الصنف", "Item movement"],
  ["customer-statement", "كشف حساب عميل تفصيلي", "Detailed customer statement"],
  ["customer-balances", "أرصدة العملاء", "Customer balances"],
  ["customer-card", "تعريف العملاء والموردين", "Customer & supplier card"],
  ["treasury-movement", "حركة الخزينة والبنك", "Treasury & bank movement"],
  ["trial-balance", "ميزان المراجعة", "Trial balance"],
  ["chart-of-accounts", "شجرة الحسابات", "Chart of accounts"],
  ["menu-sales-purchases", "قائمة المبيعات والمشتريات", "Sales & purchases menu"],
  ["menu-customers", "قائمة العملاء", "Customers menu"],
  ["menu-stores", "قائمة المخازن", "Stores menu"],
  ["menu-vouchers", "قائمة السندات", "Vouchers menu"],
  ["menu-accounts", "قائمة الحسابات", "Accounts menu"],
  ["menu-reports", "قائمة التقارير", "Reports menu"],
  ["menu-settings", "قائمة الإعدادات", "Settings menu"],
]);
const WIN_REST_SHOTS = shotList("win", true, [
  ["restaurant-cashier", "كاشير المطعم", "Restaurant cashier"],
  ["restaurant-delivery-order", "طلب دليفري", "Delivery order"],
  ["restaurant-tables", "الطاولات", "Tables"],
  ["restaurant-table-setup", "تعريف الطاولات", "Table setup"],
  ["restaurant-addons", "الإضافات", "Add-ons"],
  ["menu-restaurant", "قائمة المطعم", "Restaurant menu"],
]);

const PROGRAMS = {
  windows: {
    name: { ar: "برنامج ميجاتك للكمبيوتر", en: "MegaTech for Windows" },
    meta: [
      { ar: "الإصدار 3.3.3", en: "Version 3.3.3" },
      { ar: "قاعدة بيانات SQL Server", en: "SQL Server database" },
      { ar: "فروع ومستخدمون متعددون", en: "Multi-branch, multi-user" },
      { ar: "يتصل به تطبيق الموبايل", en: "The mobile app connects to it" },
    ],
    modes: {
      pos: {
        tagline: { ar: "أسهل برنامج لإدارة المخازن والمبيعات", en: "Sales, stock and accounts in one program" },
        desc: {
          ar: "البرنامج الأساسي الذي يدير المبيعات والمشتريات والمخازن والحسابات على أجهزة ويندوز. يناسب المحلات التجارية، وتُفعَّل فيه الأقسام التي يحتاجها نشاطك فقط. وتطبيق الموبايل يتصل به ويعمل على نفس بياناته.",
          en: "The main program that runs sales, purchases, stock and accounting on Windows. It suits shops, and you switch on only the sections your business needs. The mobile app connects to it and works on the same data.",
        },
        features: [
          { ic: "register", t: { ar: "المبيعات ونقطة البيع", en: "Sales & point of sale" },
            d: { ar: "فواتير بيع نقدية وآجلة، ومرتجعات وعروض أسعار، طرق دفع متعددة، فتح درج النقدية، شاشة عرض للعميل، وعروض وخصومات.", en: "Cash and credit sales invoices, returns and quotations, multiple payment methods, cash-drawer opening, customer display and promotions." } },
          { ic: "tag", t: { ar: "مستويات الأسعار", en: "Price levels" },
            d: { ar: "أسعار قطاعي وجملة ونصف جملة ووكيل.", en: "Retail, wholesale, half-wholesale and agent prices." } },
          { ic: "warehouse", t: { ar: "المشتريات والمخازن", en: "Purchases & stock" },
            d: { ar: "مخازن متعددة وتحويل بينها، جرد وتسوية وتالف، تنبيه حد الطلب، متابعة تواريخ الصلاحية وحركة الصنف.", en: "Multiple warehouses with transfers, stock count, adjustment and damage, reorder alerts, expiry tracking and item movement." } },
          { ic: "scan", t: { ar: "الباركود", en: "Barcodes" },
            d: { ar: "إنشاء الباركود وطباعته، وطباعة ملصقات متعددة للأصناف.", en: "Create and print barcodes, including multi-label sheets." } },
          { ic: "user", t: { ar: "العملاء والموردون", en: "Customers & suppliers" },
            d: { ar: "أرصدة وكشوف حساب تفصيلية، مجموعات عملاء، تقرير آخر سداد، وإرسال رسائل واتساب.", en: "Balances and detailed statements, customer groups, last-payment report and WhatsApp messages." } },
          { ic: "cash", t: { ar: "الحسابات والخزينة", en: "Accounts & treasury" },
            d: { ar: "خزن وبنوك وسندات قبض وصرف، شجرة حسابات وقيود، ميزان مراجعة، أرباح وخسائر، ومركز مالي.", en: "Safes, banks, receipt and payment vouchers, chart of accounts, journal entries, trial balance, P&L and balance sheet." } },
          { ic: "chart", t: { ar: "التقارير والضرائب", en: "Reports & tax" },
            d: { ar: "نحو 100 تقرير: صافي المبيعات والأرباح، تحليل المبيعات، الحركة اليومية، الورديات، والإقرار الضريبي.", en: "Around 100 reports: net sales and profit, sales analysis, daily activity, shifts and the periodic tax return." } },
          { ic: "lock", t: { ar: "المستخدمون والأمان", en: "Users & safety" },
            d: { ar: "صلاحيات لكل شاشة، موظفون ومرتبات، نسخ احتياطي واسترجاع، وتحديثات تلقائية.", en: "Per-screen permissions, employees and payroll, backup and restore, and automatic updates." } },
        ],
        extras: [
          { ar: "التصنيع والتشغيل", en: "Manufacturing & job-work" },
          { ar: "مراكز التكلفة", en: "Cost centres" },
        ],
        shots: WIN_RETAIL_SHOTS,
      },
      rest: {
        tagline: { ar: "من الطاولة إلى المطبخ إلى الخزينة", en: "From the table to the kitchen to the till" },
        desc: {
          ar: "قسم المطاعم والكافيهات في برنامج ميجاتك: كاشير سريع للصالة والتيك أواي والدليفري، مع إدارة الطاولات والمطبخ، ومربوط بالمخازن والحسابات في نفس البرنامج. ويمكن أخذ الطلبات من التابلت أو الموبايل عبر التطبيق المتصل به.",
          en: "The restaurant and café section of MegaTech: a fast cashier for dine-in, takeaway and delivery, with table and kitchen management, tied to stock and accounts in the same program. Orders can also be taken on a tablet or phone through the app that connects to it.",
        },
        features: [
          { ic: "register", t: { ar: "كاشير المطعم", en: "Restaurant cashier" },
            d: { ar: "شاشة بيع مخصصة للمطاعم والكافيهات، مع تقسيم الفاتورة بين العملاء.", en: "A sales screen built for restaurants and cafés, with bill splitting between guests." } },
          { ic: "grid", t: { ar: "إدارة الطاولات", en: "Table management" },
            d: { ar: "مجموعات طاولات، حجز الطاولات، ونقل الطلب من طاولة إلى أخرى.", en: "Table groups, table reservations, and moving an order from one table to another." } },
          { ic: "plus-circle", t: { ar: "الإضافات", en: "Add-ons" },
            d: { ar: "تعريف إضافات الأصناف وإضافتها إلى الطلب.", en: "Define item add-ons and attach them to an order." } },
          { ic: "printer", t: { ar: "شاشات وطابعات المطبخ", en: "Kitchen screens & printers" },
            d: { ar: "يصل الطلب إلى شاشة المطبخ أو طابعة المطبخ الخاصة بكل قسم.", en: "Orders reach the kitchen screen or the kitchen printer for each section." } },
          { ic: "truck", t: { ar: "تيك أواي ودليفري", en: "Takeaway & delivery" },
            d: { ar: "تسجيل الطيارين وتقرير الدليفري، مع طلبات تطبيقات التوصيل ورسومها.", en: "Delivery drivers and a delivery report, plus delivery-app orders and their fees." } },
          { ic: "warehouse", t: { ar: "مكونات الأصناف", en: "Recipes & ingredients" },
            d: { ar: "تقرير استهلاك الخامات حسب مكونات كل صنف، مربوط بالمخازن.", en: "Ingredient consumption by recipe, tied to your stock." } },
          { ic: "clock", t: { ar: "الورديات", en: "Shifts" },
            d: { ar: "فتح وإغلاق الوردية وتقرير تفصيلي لكل وردية.", en: "Open and close shifts with a detailed report for each one." } },
          { ic: "chart", t: { ar: "تقارير المطعم", en: "Restaurant reports" },
            d: { ar: "تقرير الطلبات المحذوفة والدليفري، مع تقارير المبيعات والأرباح.", en: "Deleted-orders and delivery reports, alongside sales and profit reports." } },
        ],
        extras: [
          { ar: "المخازن والمشتريات", en: "Stock & purchases" },
          { ar: "الحسابات والخزينة", en: "Accounts & treasury" },
          { ar: "صلاحيات المستخدمين", en: "User permissions" },
          { ar: "شاشة عرض للعميل", en: "Customer display" },
        ],
        shots: WIN_REST_SHOTS,
      },
    },
  },

  android: {
    name: { ar: "تطبيق ميجاتك POS", en: "MegaTech POS app" },
    meta: [
      { ar: "يعمل مع برنامج ميجاتك للكمبيوتر", en: "Works with MegaTech for Windows" },
      { ar: "اتصال مباشر بدون مزامنة", en: "Live connection, no syncing" },
      { ar: "أندرويد 7.0 فأحدث", en: "Android 7.0 and later" },
      { ar: "الإصدار 1.1.2", en: "Version 1.1.2" },
    ],
    modes: {
      pos: {
        tagline: { ar: "برنامج الكمبيوتر في جيبك", en: "Your Windows program, in your pocket" },
        desc: {
          ar: "تطبيق الموبايل والتابلت المكمّل لبرنامج ميجاتك للكمبيوتر. يتصل بالبرنامج ويعمل على نفس بياناته: نفس الأصناف والأسعار والعملاء والمستخدمين. الفاتورة التي تُسجَّل على الموبايل تظهر في برنامج الكمبيوتر فوراً والعكس، بدون مزامنة أو نقل بيانات.",
          en: "The phone and tablet companion to MegaTech for Windows. It connects to the Windows program and works on the same data: the same items, prices, customers and users. An invoice saved on the phone appears in the Windows program straight away, and vice versa, with no syncing or data transfer.",
        },
        features: [
          { ic: "register", t: { ar: "فاتورة سريعة", en: "Fast invoicing" },
            d: { ar: "بيع وشراء من نفس الشاشة، خصم بالنسبة أو بالجنيه، ضريبة، نقدي أو آجل، ودفع كاش أو شبكة أو متعدد.", en: "Sales and purchases on one screen, discount by % or amount, tax, cash or credit, and cash, card or split payment." } },
          { ic: "scan", t: { ar: "مسح الباركود بالكاميرا", en: "Camera barcode scanning" },
            d: { ar: "امسح باركود الصنف بكاميرا الموبايل، أو ابحث باسم الصنف.", en: "Scan an item's barcode with the phone camera, or search by name." } },
          { ic: "tag", t: { ar: "عروض الأسعار", en: "Price quotes" },
            d: { ar: "أنشئ عرض سعر للعميل ثم حوّله إلى فاتورة، مع دعم العملاء المؤقتين.", en: "Create a quote for a customer and turn it into an invoice, with temporary customers supported." } },
          { ic: "chart", t: { ar: "لوحة متابعة وتقارير", en: "Dashboard & reports" },
            d: { ar: "صافي المبيعات والمشتريات وأرصدة الخزن برسوم بيانية، وتقارير تُصدَّر PDF وتُشارَك.", en: "Net sales, purchases and cash balances in charts, with reports you can export to PDF and share." } },
          { ic: "printer", t: { ar: "الطباعة", en: "Printing" },
            d: { ar: "طابعات حرارية بلوتوث وطابعات شبكة، إيصالات 80 مم، ومشاركة الفاتورة PDF.", en: "Bluetooth thermal and network printers, 80 mm receipts, and PDF invoice sharing." } },
          { ic: "user", t: { ar: "العملاء والحسابات", en: "Customers & accounts" },
            d: { ar: "أرصدة وكشوف حساب تُرسل على واتساب، سندات قبض وصرف، شيكات مستحقة، وميزان مراجعة.", en: "Balances and statements sent by WhatsApp, receipt and payment vouchers, cheques due and trial balance." } },
          { ic: "warehouse", t: { ar: "المخزون", en: "Stock" },
            d: { ar: "رصيد المخزن، حركة الصنف، حد الطلب، الأصناف القريبة من الانتهاء، والتحويل بين المخازن.", en: "Stock balance, item movement, reorder level, expiring items and store-to-store transfers." } },
          { ic: "shield", t: { ar: "اتصال آمن وصلاحيات", en: "Secure access & permissions" },
            d: { ar: "اتصال آمن ببرنامج الكمبيوتر من داخل المحل أو خارجه، بنفس المستخدمين والصلاحيات المعرّفة فيه.", en: "A secure connection to the Windows program from inside or outside the shop, with the same users and permissions defined there." } },
        ],
        extras: [
          { ar: "حضور الموظفين من أجهزة البصمة", en: "Attendance from fingerprint devices" },
          { ar: "تحديثات من داخل التطبيق", en: "In-app updates" },
          { ar: "وضع داكن", en: "Dark mode" },
        ],
        shots: [
          { src: "assets/programs/android-dashboard.png", ar: "لوحة المتابعة", en: "Dashboard" },
        ].concat(RETAIL_SHOTS, [
          { src: "assets/programs/android-dashboard-wide.png", wide: true, ar: "لوحة المتابعة على الشاشات الكبيرة", en: "Dashboard on a large screen" },
        ]),
      },
      rest: {
        tagline: { ar: "خذ الطلب من عند الطاولة", en: "Take the order at the table" },
        desc: {
          ar: "كاشير المطعم على التابلت أو الموبايل، متصل بقسم المطاعم في برنامج ميجاتك للكمبيوتر. سجّل طلبات الصالة والتيك أواي والدليفري وأرسلها للمطبخ، وكل طلب يظهر فوراً في برنامج الكمبيوتر بنفس الطاولات والأصناف والورديات.",
          en: "The restaurant cashier on a tablet or phone, connected to the restaurant section of MegaTech for Windows. Take dine-in, takeaway and delivery orders and send them to the kitchen; every order appears straight away in the Windows program, with the same tables, items and shifts.",
        },
        features: [
          { ic: "register", t: { ar: "أنواع الطلبات", en: "Order types" },
            d: { ar: "صالة، تيك أواي، دليفري، وطلبات تطبيقات التوصيل من نفس الشاشة.", en: "Dine-in, takeaway, delivery and delivery-app orders from one screen." } },
          { ic: "grid", t: { ar: "الطاولات", en: "Tables" },
            d: { ar: "اختر الطاولة وسجّل الطلب عليها مباشرة.", en: "Pick the table and take the order against it." } },
          { ic: "plus-circle", t: { ar: "إضافات وملاحظات", en: "Add-ons & notes" },
            d: { ar: "أضف الإضافات لكل صنف واكتب ملاحظات للمطبخ.", en: "Add extras to each item and write notes for the kitchen." } },
          { ic: "printer", t: { ar: "إرسال للمطبخ", en: "Send to kitchen" },
            d: { ar: "يُطبع الطلب في المطبخ، وإذا توقفت الطابعة تنتظر التذاكر في قائمة حتى تعود.", en: "Orders print in the kitchen; if the printer is down, tickets wait in a queue until it is back." } },
          { ic: "truck", t: { ar: "الدليفري", en: "Delivery" },
            d: { ar: "رسوم التوصيل وتحصيل الطيارين، مع إظهار بيانات آخر متصل عند ربط السنترال.", en: "Delivery fees and driver collection, with last-caller details when linked to the call centre." } },
          { ic: "cash", t: { ar: "الدفع", en: "Payment" },
            d: { ar: "تحصيل نقدي أو بالشبكة.", en: "Take payment in cash or by card." } },
          { ic: "clock", t: { ar: "الورديات", en: "Shifts" },
            d: { ar: "فتح وإغلاق الوردية من التطبيق، مع تقرير الوردية.", en: "Open and close the shift in the app, with a shift report." } },
          { ic: "chart", t: { ar: "متابعة من أي مكان", en: "Follow from anywhere" },
            d: { ar: "لوحة متابعة بالمبيعات والأصناف الأكثر مبيعاً وأرصدة الخزن.", en: "A dashboard with sales, best-selling items and cash balances." } },
        ],
        extras: [
          { ar: "طابعات بلوتوث وشبكة", en: "Bluetooth & network printers" },
          { ar: "صلاحيات المستخدمين", en: "User permissions" },
          { ar: "وضع داكن", en: "Dark mode" },
        ],
        shots: REST_SHOTS,
      },
    },
  },
};

function ProgramShots({ shots, lang, onOpen }) {
  if (!shots.length) {
    return (
      <div className="progshots progshots--empty">
        <PgIcon name="image" size={22} />
        <span>{lang === "en" ? "Screenshots coming soon — ask us for a live demo." : "صور البرنامج قريباً — اطلب عرضاً حياً من فريقنا."}</span>
      </div>
    );
  }
  const ref = React.useRef(null);
  const slide = (dir) => { const el = ref.current; if (el) el.scrollBy({ left: dir * el.clientWidth * 0.8, behavior: "smooth" }); };
  return (
    <div className="progshotswrap">
      <div className="progshots__bar">
        <span className="progshots__count"><PgIcon name="image" size={16} />{lang === "en" ? shots.length + " screenshots" : shots.length + " صورة من البرنامج"}</span>
        {shots.length > 2 && (
          <span className="progshots__nav">
            <button onClick={() => slide(lang === "en" ? -1 : 1)} aria-label={lang === "en" ? "Previous" : "السابق"}><PgIcon name="chevron-left" size={18} className="flip-x" /></button>
            <button onClick={() => slide(lang === "en" ? 1 : -1)} aria-label={lang === "en" ? "Next" : "التالي"}><PgIcon name="chevron-right" size={18} className="flip-x" /></button>
          </span>
        )}
      </div>
      <div className="progshots" ref={ref}>
        {shots.map((s) => (
          <figure key={s.src} className={"progshot" + (s.wide ? " progshot--wide" : " progshot--phone")}>
            <button className="progshot__btn" onClick={() => onOpen(s)} aria-label={s[lang]}>
              <img src={s.src} alt={s[lang]} loading="lazy" />
            </button>
            <figcaption>{s[lang]}</figcaption>
          </figure>
        ))}
      </div>
    </div>
  );
}

function ProgramSection({ os, mode, lang, onOpen, onBuy }) {
  const prog = PROGRAMS[os];
  const m = prog.modes[mode];
  const platform = PLATFORMS.find((p) => p.id === os);
  const modeInfo = MODES.find((x) => x.id === mode);
  return (
    <section className={"prog prog--" + mode} id={"prog-" + os + "-" + mode}>
      <div className="prog__head">
        <span className="prog__ic"><PgIcon name={platform.icon} size={30} /></span>
        <div className="prog__headtxt">
          <div className="prog__badges">
            <span className="badge prog__modebadge"><PgIcon name={modeInfo.icon} size={13} />{modeInfo[lang]}</span>
            <span className="badge badge--neutral">{platform[lang]}</span>
          </div>
          <h2 className="prog__name">{prog.name[lang]}</h2>
          <div className="prog__tagline">{m.tagline[lang]}</div>
        </div>
        {onBuy && (
          <button className="btn prog__buy" onClick={onBuy}>
            <PgIcon name="cart" size={18} />{lang === "en" ? "Buy from the store" : "اشترِ من المتجر"}
          </button>
        )}
      </div>
      <p className="prog__desc">{m.desc[lang]}</p>
      <ul className="prog__meta">
        {prog.meta.map((x) => <li key={x.en}><PgIcon name="check-circle" size={16} />{x[lang]}</li>)}
      </ul>

      <ProgramShots shots={m.shots} lang={lang} onOpen={onOpen} />

      <div className="proggrid">
        {m.features.map((f) => (
          <div className="progfeat" key={f.t.en}>
            <span className="progfeat__ic"><PgIcon name={f.ic} size={22} /></span>
            <h3 className="progfeat__t">{f.t[lang]}</h3>
            <p className="progfeat__d">{f.d[lang]}</p>
          </div>
        ))}
      </div>

      <div className="prog__extras">
        <span className="prog__extraslabel">{lang === "en" ? "Also included" : "ويشمل أيضاً"}</span>
        {m.extras.map((x) => <span className="badge badge--neutral" key={x.en}>{x[lang]}</span>)}
      </div>
    </section>
  );
}

function ProgramsPage({ lang, t, onBackToStore, onContact, products, cardStyle, onAdd, onView, favs, onFav }) {
  const { ProductGrid } = window;
  const [zoom, setZoom] = React.useState(null);
  // Products sold for these programs: every active product in the store's "software" category.
  // They are listed in the "Buy the software" block near the end of the page, with add-to-cart.
  const software = (products || []).filter((p) => p.cat === "software" && p.status !== "draft");
  const goBuy = software.length
    ? () => { const el = document.getElementById("prog-buy"); if (el) el.scrollIntoView({ behavior: "smooth", block: "start" }); }
    : null;
  React.useEffect(() => {
    if (!zoom) return;
    const onKey = (e) => { if (e.key === "Escape") setZoom(null); };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [zoom]);

  return (
    <main className="programs">
      <div className="container">
        <nav className="crumbs">
          <button onClick={onBackToStore}>{t.storefront}</button>
          <PgIcon name="chevron-right" size={15} className="flip-x" />
          <span style={{ color: "var(--fg1)" }}>{t.navPrograms}</span>
        </nav>

        <header className="programs__head">
          <div className="section__overline">{t.navPrograms}</div>
          <h1 className="programs__title">{lang === "en" ? "Software that runs your whole business" : "برامج تدير نشاطك من أوله لآخره"}</h1>
          <p className="programs__sub">{lang === "en"
            ? "MegaTech for Windows is the main program, and the Android app connects to it and works on the same data."
            : "برنامج ميجاتك للكمبيوتر هو الأساس، وتطبيق الأندرويد يتصل به ويعمل على نفس بياناته."}</p>
        </header>

        <div className="progpick">
          {MODES.map((x) => (
            <a key={x.id} className={"progpick__card prog--" + x.id} href={"#world-" + x.id}
              onClick={(e) => { e.preventDefault(); const el = document.getElementById("world-" + x.id); if (el) el.scrollIntoView({ behavior: "smooth", block: "start" }); }}>
              <span className="progpick__ic"><PgIcon name={x.icon} size={34} /></span>
              <span className="progpick__txt">
                <strong>{x.title[lang]}</strong>
                <span>{x.sub[lang]}</span>
              </span>
              <PgIcon name="chevron-down" size={22} />
            </a>
          ))}
        </div>

        {MODES.map((x) => (
          <div key={x.id} className={"progworld prog--" + x.id} id={"world-" + x.id}>
            <header className="progworld__head">
              <span className="progworld__ic"><PgIcon name={x.icon} size={40} /></span>
              <div className="progworld__txt">
                <h2>{x.title[lang]}</h2>
                <p>{x.sub[lang]}</p>
                <ul>{x.points.map((pt) => <li key={pt.en}><PgIcon name="check" size={14} />{pt[lang]}</li>)}</ul>
              </div>
            </header>
            {PLATFORMS.map((p) => (
              <ProgramSection key={p.id + "-" + x.id} os={p.id} mode={x.id} lang={lang} onOpen={setZoom} onBuy={goBuy} />
            ))}
          </div>
        ))}

        {software.length > 0 && (
          <section className="progbuy" id="prog-buy">
            <header className="progbuy__head">
              <div className="section__overline">{lang === "en" ? "Store" : "المتجر"}</div>
              <h2 className="section__title">{lang === "en" ? "Buy the software" : "اشترِ البرنامج"}</h2>
              <p>{lang === "en"
                ? "Add it to your cart here, or open the product for the full details."
                : "أضف البرنامج إلى السلة من هنا، أو افتح صفحة المنتج لمعرفة كل التفاصيل."}</p>
            </header>
            <ProductGrid products={software} lang={lang} t={t} cardStyle={cardStyle || "standard"}
              onAdd={onAdd} onView={onView} favs={favs || []} onFav={onFav} />
          </section>
        )}

        <section className="progcta">
          <div>
            <h2>{lang === "en" ? "See it running on your own data" : "شاهد البرنامج يعمل على نشاطك"}</h2>
            <p>{lang === "en" ? "Our team installs it, sets it up and trains your staff." : "فريقنا يركّب البرنامج ويجهّزه ويدرّب موظفيك."}</p>
          </div>
          <button className="btn btn--onbrand btn--lg" onClick={onContact}>
            <PgIcon name="headset" size={20} />{lang === "en" ? "Request a demo" : "اطلب عرضاً تجريبياً"}
          </button>
        </section>
      </div>

      {zoom && (
        <div className="proglightbox" onClick={() => setZoom(null)} role="dialog" aria-modal="true" aria-label={zoom[lang]}>
          <button className="proglightbox__x" aria-label={lang === "en" ? "Close" : "إغلاق"}><PgIcon name="x" size={22} /></button>
          <img src={zoom.src} alt={zoom[lang]} />
        </div>
      )}
    </main>
  );
}

Object.assign(window, { ProgramsPage, PROGRAMS });
