// ============================================================
// FRAGTA — icons, logo, sample data, shared atoms
// ============================================================
const { useState, useEffect, useRef } = React;

// ---- ICONS (functional line set, 24-grid) ----------------------------------
const PATHS = {
  inbox:   'M3 12h5l2 3h4l2-3h5M5 5h14a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1Z',
  grid:    'M4 4h7v7H4zM13 4h7v7h-7M4 13h7v7H4M13 13h7v7h-7',
  mail:    'M4 6h16v12H4zM4 7l8 6 8-6',
  catalog: 'M4 5h7v14H4zM13 5h7v14h-7M7 9h1M7 12h1M16 9h1M16 12h1',
  network: 'M6 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4ZM18 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4ZM12 21a2 2 0 1 0 0-4 2 2 0 0 0 0 4ZM7.5 6.5l3.2 9M16.5 6.5l-3.2 9',
  users:   'M9 11a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM3 20a6 6 0 0 1 12 0M17 11a3 3 0 1 0-1-5.8M21 20a6 6 0 0 0-5-5.9',
  shield:  'M12 3l7 3v5c0 4.4-3 7.5-7 9-4-1.5-7-4.6-7-9V6l7-3Z',
  cog:     'M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM19.4 13a1.7 1.7 0 0 0 .3 1.9l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-2.9 1.2V21a2 2 0 1 1-4 0v-.1A1.7 1.7 0 0 0 7 19.4l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0-1.2-2.9H3a2 2 0 1 1 0-4h.1A1.7 1.7 0 0 0 4.6 7l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.9.3H9.5A1.7 1.7 0 0 0 10.6 3V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 2.9 1.2l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.9V9.5a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1Z',
  info:    'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM12 11v5M12 7.5v.5',
  send:    'M22 2 11 13M22 2l-7 20-4-9-9-4 20-7Z',
  clock:   'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM12 7v5l3 2',
  check:   'M5 12l5 5L20 6',
  checkc:  'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM8.5 12l2.5 2.5L15.5 9',
  x:       'M6 6l12 12M18 6 6 18',
  xc:      'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM9 9l6 6M15 9l-6 6',
  search:  'M11 18a7 7 0 1 0 0-14 7 7 0 0 0 0 14ZM20 20l-4-4',
  plus:    'M12 5v14M5 12h14',
  chevR:   'M9 6l6 6-6 6',
  chevD:   'M6 9l6 6 6-6',
  arrowR:  'M5 12h14M13 6l6 6-6 6',
  arrowL:  'M19 12H5M11 18l-6-6 6-6',
  refresh: 'M21 12a9 9 0 1 1-3-6.7M21 4v4h-4',
  trash:   'M4 7h16M9 7V5h6v2M6 7l1 13h10l1-13M10 11v6M14 11v6',
  spark:   'M12 3l1.8 5.6L19 10l-5.2 1.4L12 17l-1.8-5.6L5 10l5.2-1.4L12 3Z',
  filter:  'M3 5h18l-7 8v5l-4 2v-7L3 5Z',
  pin:     'M12 21s7-5.6 7-11a7 7 0 1 0-14 0c0 5.4 7 11 7 11ZM12 12a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z',
  phone:   'M5 4h3l2 5-2 1a12 12 0 0 0 5 5l1-2 5 2v3a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2Z',
  edit:    'M4 20h4L18 10l-4-4L4 16v4ZM14 6l4 4',
  doc:     'M6 3h8l4 4v14H6zM14 3v4h4',
  upload:  'M12 16V4M7 9l5-5 5 5M5 20h14',
  box:     'M12 3 4 7v10l8 4 8-4V7l-8-4ZM4 7l8 4 8-4M12 11v10',
  truck:   'M3 6h11v9H3zM14 9h4l3 3v3h-7M7 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4ZM18 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4Z',
  globe:   'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18ZM3 12h18M12 3c2.5 2.5 2.5 15 0 18M12 3c-2.5 2.5-2.5 15 0 18',
  bolt:    'M13 2 4 14h6l-1 8 9-12h-6l1-8Z',
  bell:    'M6 9a6 6 0 0 1 12 0c0 5 2 6 2 6H4s2-1 2-6ZM10 19a2 2 0 0 0 4 0',
  moon:    'M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z',
  sun:     'M12 17a5 5 0 1 0 0-10 5 5 0 0 0 0 10ZM12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4',
  logout:  'M15 4h4a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-4M10 12h10M16 8l4 4-4 4',
  history: 'M3 12a9 9 0 1 0 3-6.7M3 4v4h4M12 8v4l3 2',
  building:'M4 21V5a1 1 0 0 1 1-1h9a1 1 0 0 1 1 1v16M15 21V9h4a1 1 0 0 1 1 1v11M7 8h2M7 12h2M7 16h2',
};
function Icon({ n, style }) {
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"
         strokeLinecap="round" strokeLinejoin="round" style={style} aria-hidden="true">
      <path d={PATHS[n] || ''} />
    </svg>
  );
}

function BrandMark() { return <span className="brand-mark" aria-hidden="true"></span>; }

// ---- SAMPLE DATA -----------------------------------------------------------
// Forespørgsler (RFQ inbox)
const REQUESTS = [
  {
    id: 'A00541', from: 'Anders Bjerg', email: 'ab@nordpil-spedition.example',
    fromShort: 'Anders Bjerg', route: ['Dubai', 'Lynge'], vol: 'LCL · 2 kolli', when: '26. jun 2026 · 10:57', status: 'wait', conf: 90,
    subject: 'VS: INQUIRY || LCL || UAE || DENMARK || CLEARANCE & DELIVERY || A00541',
    body: ['Dear Team,\n\nGood day. Hope you are doing well.\n\nPlease advise us your best ', 'b:Clearance and Delivery charges', ' on the below ', 'hl:LCL shipment', ' moving by ECU Worldwide ex ', 'hl:Jebel Ali, UAE', '.\n\n',
      'b:Shipment details:', '\n',
      'POL: Jebel Ali (AEJEA)\n',
      'POD: ', 'b:Port of Copenhagen (DKCPH)', '\n',
      'Commodity: Personal Effects (used household goods)\n',
      'Packages: 2 wooden crates\n',
      'Gross weight: 480 kgs   Volume: 1.00 CBM\n',
      'Incoterm: DAP — door delivery required\n',
      'Delivery address: ', 'hl:Julemosegård 77, 3540 Lynge, Denmark', '\n\n',
      'Please include customs clearance, THC, and final mile delivery in your quote. ETA at destination is appreciated.\n\nBest regards,\nReza A.\nAir & Sea Logistics ME — Dubai\nT +971 4 880 11 22'],
    parsed: [
      ['Afsender', 'Operations · airsealogistics-me.com', 88],
      ['Rute', 'UAE → Danmark (Lynge)', 95],
      ['Volumen', 'LCL · 2 kolli · 1 CBM', 90],
      ['Service', 'Søfragt · clearance + delivery', 92],
      ['Krav', 'POD Port of Copenhagen', 86],
    ],
    offers: [
      { rk: 1, nm: 'Bæltvej Jylland / Fyn', md: 'Danmark · told + levering', pr: '4.450 DKK' },
      { rk: 2, nm: 'Lowvex International BV', md: 'Holland · all-in', pr: '625 EUR' },
      { rk: 3, nm: 'Bækbo Tank Logistik', md: 'Polen · LCL', pr: '550 EUR' },
    ],
  },
  {
    id: 'SC-44712', from: 'Thomas Krogh', email: 'tk@nordpil-spedition.example',
    fromShort: 'Thomas Krogh', route: ['Guangzhou', 'Svendborg'], vol: '1×40HC', when: '25. jun 2026 · 13:56', status: 'sent', conf: 93, replies: 6,
    subject: 'VS: RFQ: EXW Guangzhou City to DK5700 Svendborg',
    body: ['Hi,\n\nPlease quote ', 'b:EXW', ' all-in ex factory to door, see details below.\n\n',
      'Pickup: ', 'hl:Guangzhou City (factory, Baiyun District)', '\n',
      'Delivery: door ', 'hl:DK-5700 Svendborg', '\n',
      'Volume: ', 'b:1 × 40HC', '\n',
      'Gross weight: approx. 8,600 kg\n',
      'Cartons: 410 ctn   CBM: 58.0\n',
      'Commodity: consumer electronics (non-hazardous)\n',
      'Incoterm: EXW — customs clearance + delivery included\n',
      'ETD: asap, goods ready\n\n',
      'Need your sea freight rate, destination charges, DK import clearance and trucking to Svendborg. Please advise transit time as well.\n\nThanks,\nLin Zhao\nGuangzhou'],
    parsed: [
      ['Afsender', 'Thomas Krogh · nordpil-spedition.example', 96],
      ['Rute', 'Kina (Guangzhou) → Danmark (Svendborg)', 95],
      ['Volumen', '1×40HC · 58 CBM · 8,6 t', 92],
      ['Service', 'Søfragt · EXW · told + levering', 90],
      ['Krav', 'Door delivery DK-5700', 88],
    ],
    offers: [
      { rk: 1, nm: 'Østpil Logistik ApS', md: 'Danmark · FCL kina-rute', pr: '2.480 USD' },
      { rk: 2, nm: 'Vantor Logistics', md: 'Polen · konkurrencedygtig', pr: '2.310 USD' },
      { rk: 3, nm: 'Havnelink', md: 'Polen · ledig plads', pr: '2.560 USD' },
    ],
  },
  {
    id: 'SC-44698', from: 'Thomas Krogh', email: 'tk@nordpil-spedition.example',
    fromShort: 'Thomas Krogh', route: ['Shanghai', 'New York'], vol: '1×20GP', when: '25. jun 2026 · 22:40', status: 'sent', conf: 91, replies: 5,
    subject: 'Fw: New Shipment of Marketing Inserts (PO #SH-1002)',
    body: ['Hi team,\n\nWe have a new shipment of ', 'b:marketing inserts / printed material', ' ready under ', 'hl:PO #SH-1002', '. Kindly quote ocean freight as below.\n\n',
      'POL: ', 'b:Shanghai (CNSHA)', '   Incoterm: ', 'b:FOB Shanghai', '\n',
      'POD: ', 'hl:New York (USNYC)', '\n',
      'Volume: 1 × 20GP\n',
      'Pallets: 26   Gross weight: ~11,200 kg   CBM: 21.4\n',
      'Commodity: printed inserts, palletised, stackable\n',
      'Ready date: within this week\n\n',
      'Please advise all-in rate, free time at destination and total transit time. Customer is rate-sensitive, so your sharpest offer is appreciated.\n\nBR,\nMaria S.\nSupply Chain'],
    parsed: [
      ['Afsender', 'Thomas Krogh · nordpil-spedition.example', 95],
      ['Rute', 'Kina (Shanghai) → USA (New York)', 94],
      ['Volumen', '1×20GP · 21,4 CBM · 11,2 t', 90],
      ['Service', 'Søfragt · FOB', 89],
      ['Krav', 'PO #SH-1002 · transittid ønskes', 84],
    ],
    offers: [
      { rk: 1, nm: 'Jarex Spedycja', md: 'Polen · transpacific', pr: '4,75 USD/kg' },
      { rk: 2, nm: 'Lowvex International BV', md: 'Holland · all-in', pr: '1.980 USD' },
      { rk: 3, nm: 'Polvex Transport', md: 'Polen · ledig', pr: '2.140 USD' },
    ],
  },
  {
    id: 'SC-44655', from: 'Thomas Krogh', email: 'tk@nordpil-spedition.example',
    fromShort: 'Thomas Krogh', route: ['Guangzhou', 'København'], vol: 'LCL · 6 pll', when: '25. jun 2026 · 11:08', status: 'wait', conf: 71,
    subject: 'VS: Vedr. forsendelse fra Kina',
    body: ['Hej,\n\nTak for snakken tidligere. Vi har en forsendelse på vej fra ', 'hl:Kina', ' til ', 'hl:København', ', og jeg vil gerne høre, hvad I kan gøre på pris.\n\n',
      'Afsender: fabrik i Guangzhou\n',
      'Modtager: vores lager, ', 'hl:2670 Greve', '\n',
      'Volumen: ', 'b:ca. 6 paller, LCL', '\n',
      'Vægt: endnu ikke bekræftet (ca. 1.800 kg?)\n',
      'Vare: blandede forbrugsvarer\n\n',
      'Jeg mangler stadig de præcise mål og endelig vægt fra leverandøren — dem sender jeg, så snart jeg har dem. Send gerne et foreløbigt estimat i mellemtiden.\n\nMvh\nKundeservice'],
    parsed: [
      ['Afsender', 'Thomas Krogh · nordpil-spedition.example', 90],
      ['Rute', 'Kina → Danmark (København)', 82],
      ['Volumen', 'LCL · ~6 paller', 70],
      ['Service', 'Søfragt', 75],
      ['Krav', 'Afventer mål + vægt fra kunde', 55],
    ],
    offers: [
      { rk: 1, nm: 'Østpil Logistik ApS', md: 'Danmark · LCL konsol', pr: 'Afventer' },
      { rk: 2, nm: 'Bækbo Tank Logistik', md: 'Polen · LCL', pr: 'Afventer' },
    ],
  },
  {
    id: 'BE-22401', from: 'Thomas Krogh', email: 'tk@nordpil-spedition.example',
    fromShort: 'Thomas Krogh', route: ['Zandhoven', 'Bogense'], vol: '5 paller', when: '24. jun 2026 · 11:08', status: 'sent', conf: 96, replies: 5,
    subject: 'VS: lidt oplysninger',
    body: ['Hej,\n\nVi skal bruge et tilbud på en mindre sending general cargo fra Belgien til Fyn.\n\n',
      'Afhentning: ', 'hl:2240 Zandhoven, Belgien', '\n',
      'Levering: ', 'hl:5400 Bogense, Danmark', '\n',
      'Gods: ', 'b:5 paller', ', general cargo, ikke farligt\n',
      'Mål pr. palle: 120 × 80 × 250 cm\n',
      'Samlet vægt: ca. 4.200 kg   LDM: 6,0\n',
      'Incoterm: ', 'b:EXW', '\n',
      'Afhentning fleksibel i indeværende uge.\n\n',
      'Send jeres bedste pris samt forventet transittid. På forhånd tak.\n\nMvh\nIndkøb'],
    parsed: [
      ['Afsender', 'Thomas Krogh · nordpil-spedition.example', 97],
      ['Rute', 'Belgien (Zandhoven) → Danmark (Bogense)', 96],
      ['Volumen', '5 paller · 12 CBM · 4,2 t', 92],
      ['Service', 'Vejtransport · stykgods', 94],
      ['Krav', 'EXW · ingen særlige krav', 90],
    ],
    offers: [
      { rk: 1, nm: 'Østpil Logistik ApS', md: 'Danmark · BE→DK rute', pr: '1.180 €' },
      { rk: 2, nm: 'Bæltvej Jylland / Fyn', md: 'Danmark · import Benelux', pr: '1.240 €' },
      { rk: 3, nm: 'Freshlane Logistik', md: 'Polen · ledig kapacitet', pr: '1.310 €' },
    ],
  },
  {
    id: 'RQ-31022', from: 'Thomas Krogh', email: 'tk@nordpil-spedition.example',
    fromShort: 'Thomas Krogh', route: ['Hamburg', 'Tallinn'], vol: '1 pll · 2 optioner', when: '26. jun 2026 · 09:48', status: 'new', conf: 84,
    subject: 'request',
    body: ['Hi,\n\nNeed ', 'b:2 prices', ' for the same cargo, ', 'b:loading tomorrow!', ' Please be quick.\n\n',
      'Cargo: 1 PLL, 120 × 80 × 50 cm, 120 kg, non-stackable\n',
      'Delivery (both options): ', 'hl:Tallinn, Estonia', ' — ASAP\n\n',
      'b:Option 1', ' — load ', 'hl:Hamburg, Germany', '\n',
      'b:Option 2', ' — load ', 'hl:DK-5700 Svendborg', '\n\n',
      'Whichever is faster/cheaper works. Please send rates for both with earliest possible delivery date.\n\nThanks!'],
    parsed: [
      ['Afsender', 'Thomas Krogh · nordpil-spedition.example', 92],
      ['Rute', '2 optioner: Hamburg / DK-5700 → Tallinn', 80],
      ['Volumen', '1 paller · 120×80×50 cm · 120 kg', 90],
      ['Service', 'Vejtransport · stykgods', 86],
      ['Krav', 'Loading i morgen · levering Tallinn ASAP', 78],
    ],
    offers: [
      { rk: 1, nm: 'Østpil Logistik ApS', md: 'Option 1 · Hamburg → Tallinn', pr: '740 €' },
      { rk: 2, nm: 'Polvex Transport', md: 'Polen · Baltikum-rute', pr: '690 €' },
      { rk: 3, nm: 'Vantor Logistics', md: 'Option 2 · DK → Tallinn', pr: '810 €' },
    ],
  },
  {
    id: 'EC-7741', from: 'Bredgaard Industri A/S', email: 'transport@bredgaard.example',
    fromShort: 'Bredgaard Industri', route: ['Kolding', 'Wittenburg'], vol: '2 paller', when: '26. jun 2026 · 07:31', status: 'new', conf: 95,
    subject: 'Pickup 22/04 — Kolding → Wittenburg, same day',
    body: ['Hi,\n\nWe have an urgent ', 'b:same-day / direct', ' transport. Please confirm availability and price asap.\n\n',
      'Pickup: ', 'b:22/04 at 08:00', '\n',
      'Shipper: ', 'hl:Bredgaard Industri A/S, Albuen 64, DK-6000 Kolding', '\n',
      'Consignee: ', 'hl:Siemens Gamesa Renewable, Auf der Heide 1, DE-19243 Wittenburg', '\n',
      'Cargo: 2 pallets, 900 kg total, 120 × 100 × 160 cm\n',
      'Service: ', 'b:dedicated van/truck, same-day delivery', '\n',
      'Booking ref must be quoted at delivery gate.\n\n',
      'This is time-critical for a production line — please prioritise.\n\nBR,\nTransport, Bredgaard Industri'],
    parsed: [
      ['Afsender', 'Bredgaard Industri A/S', 98],
      ['Rute', 'Kolding (DK) → Wittenburg (DE)', 96],
      ['Volumen', '2 paller · 900 kg', 94],
      ['Pickup', '22. apr · 08:00 · same-day', 93],
      ['Krav', 'Levering Siemens Gamesa · ekspres', 88],
    ],
    offers: [
      { rk: 1, nm: 'Vestpil Transport', md: 'Kolding · direkte same-day', pr: '880 €' },
      { rk: 2, nm: 'Norvex Logistik', md: 'Hamborg · ekspres', pr: '960 €' },
      { rk: 3, nm: 'Bavex Cargo', md: 'DE-rute · ledig', pr: '1.020 €' },
    ],
  },
  {
    id: 'NR-44823', from: 'Byfeldt Retail · Logistik', email: 'disposition@byfeldt-retail.example',
    fromShort: 'Byfeldt Retail', route: ['Verona', 'Hamborg'], vol: '34 paller', when: '24. jun 2026 · 08:42', status: 'sent', conf: 96, replies: 7,
    subject: 'Forespørgsel #NR-44823 — Verona → Hamborg, 34 paller',
    body: ['Hej,\n\nVi har brug for et tilbud på en fuld trailer stykgods fra vores lager i Norditalien til Hamborg.\n\n',
      'Afhentning: ', 'hl:Byfeldt Retail DC, Via Pisano 14, 37135 Verona', '\n',
      'Levering: ', 'hl:Hamborg (HafenCity terminal)', '\n',
      'Gods: ', 'b:34 EUR-paller', ', møbler/stykgods\n',
      'Vægt: 12.400 kg   LDM: 13,6\n',
      'Afhentning: ', 'hl:12. maj fra kl. 09:00', '\n',
      'Krav: Tautliner OK, lift ved levering, ', 'b:ingen farligt gods', '\n\n',
      'Vi skal svare vores kunde i dag, så svar gerne inden dagen er omme. Tak!\n\nMvh\nByfeldt Retail · Logistik'],
    parsed: [
      ['Afsender', 'Byfeldt Retail A/S', 99],
      ['Rute', 'Verona (IT) → Hamborg (DE)', 97],
      ['Volumen', '34 EUR-paller · 12,4 t', 95],
      ['Pickup', '12. maj · fra 09:00', 98],
      ['Krav', 'Tautliner · ikke ADR', 92],
    ],
    offers: [
      { rk: 1, nm: 'Adriax Trans', md: 'Verona · ledig 12. maj 09:30', pr: '1.980 €' },
      { rk: 2, nm: 'Bavex Cargo',   md: 'München · ledig kapacitet',     pr: '2.090 €' },
      { rk: 3, nm: 'Norvex Logistik', md: 'Hamborg · mest pålidelig',     pr: '2.210 €' },
    ],
  },
  {
    id: 'SC-30441', from: 'Saltholm Foods', email: 'logistik@saltholm-foods.example',
    fromShort: 'Saltholm Foods', route: ['Göteborg', 'Rotterdam'], vol: '22 paller', when: '23. jun 2026 · 09:12', status: 'done', conf: 89,
    subject: 'Køletransport — Göteborg → Rotterdam',
    body: ['Hej,\n\nVi skal bruge køletransport og hører gerne jeres pris.\n\n',
      'Afhentning: ', 'hl:Saltholm Foods AB, Hamngatan 4, Göteborg', '\n',
      'Levering: ', 'hl:Rotterdam (Maasvlakte)', '\n',
      'Gods: ', 'b:22 paller, kølevarer', '\n',
      'Temperatur: ', 'b:+2 °C konstant', ' — ATP-certifikat påkrævet\n',
      'Vægt: 8.800 kg\n',
      'Afhentning: ', 'hl:10. maj', '   ADR: nej\n\n',
      'Bekræft venligst at I kan holde temperaturen hele vejen, og send pris + transittid.\n\nMvh\nLogistik, Saltholm Foods'],
    parsed: [['Afsender','Saltholm Foods AB',95],['Rute','Göteborg (SE) → Rotterdam (NL)',93],['Volumen','22 paller · køl',88],['Pickup','10. maj',91],['Krav','Frigo +2°C · ADR nej',85]],
    offers: [
      { rk: 1, nm: 'Frostvik Logistik', md: 'Göteborg · køl-spec', pr: '2.480 €' },
      { rk: 2, nm: 'Vestpil Transport', md: 'Aarhus · frigo', pr: '2.610 €' },
      { rk: 3, nm: 'Sundbro Frakt', md: 'Malmö · ledig', pr: '2.720 €' },
    ],
  },
  {
    id: 'XX-90112', from: 'Ukendt afsender', email: 'noreply@spam-route.net',
    fromShort: 'Ukendt afsender', route: ['—', '—'], vol: '—', when: '23. jun 2026 · 07:55', status: 'err', conf: 22,
    subject: 'RE: din ordre (kunne ikke fortolkes)',
    body: ['Mailen kunne ikke fortolkes som en fragtforespørgsel. ', 'b:Manglende rute og volumen', '. Markér manuelt eller afvis.'],
    parsed: [['Afsender','— (lav tillid)',22],['Rute','Ikke fundet',0],['Volumen','Ikke fundet',0],['Pickup','—',0],['Krav','—',0]],
    offers: [],
  },
];

const PIPELINE_STATS = [
  { ico: 'mail',   tone: 'accent', n: 15, l: 'E-mails i alt', delta: '+7 i dag' },
  { ico: 'clock',  tone: 'warn',   n: 2,  l: 'Afventer godkendelse' },
  { ico: 'send',   tone: 'good',   n: 9,  l: 'Forespørgsel sendt' },
  { ico: 'checkc', tone: 'good',   n: 1,  l: 'Afsluttet' },
  { ico: 'xc',     tone: 'err',    n: 1,  l: 'Fejl' },
];

const STATUS_LABEL = { new: 'Ny', wait: 'Afventer', sent: 'Sendt', done: 'Afsluttet', err: 'Fejl' };
const STATUS_BADGE = { new: 'b-new', wait: 'b-wait', sent: 'b-sent', done: 'b-done', err: 'b-err' };

// Katalog — vognmænd
const CARRIERS = [
  { id: 'BRT', nm: 'Adriax Trans', land: 'Italien', flag: 'IT', contact: 'Marco Rossi', tel: '+39 045 88 12 03', mail: 'dispo@adriax-trans.example',
    services: ['ROAD','FTL','LTL'], type: 'Stykgods & fuldlæs', from: 'Italien', to: ['Tyskland','Østrig','Danmark'], score: 94, created: '04. feb 2026' },
  { id: 'ALP', nm: 'Bavex Cargo', land: 'Tyskland', flag: 'DE', contact: 'Lena Bauer', tel: '+49 89 23 55 70', mail: 'ops@bavex-cargo.example',
    services: ['ROAD','FTL','INTERMODAL'], type: 'Fuldlæs & intermodal', from: 'Tyskland', to: ['Danmark','Polen','Italien'], score: 91, created: '11. jan 2026' },
  { id: 'HAN', nm: 'Norvex Logistik', land: 'Tyskland', flag: 'DE', contact: 'Jürgen Holm', tel: '+49 40 76 21 09', mail: 'booking@norvex-log.example',
    services: ['ROAD','SEA','LTL'], type: 'Stykgods & sø', from: 'Tyskland', to: ['Danmark','Sverige','Norge'], score: 88, created: '22. nov 2025' },
  { id: '3WI', nm: 'Birchway Forwarding', land: 'Canada', flag: 'CA', contact: 'Mario Martins', tel: '+1 905 555 0142', mail: 'dispo@birchway-fwd.example',
    services: ['AIR','COURIER','EXPRESS','ROAD','SEA','GENERAL'], type: 'Stykgods & full-load', from: 'Canada', to: ['Sverige','UAE'], score: 76, created: '12. mar 2026' },
  { id: 'NOH', nm: 'Vestpil Transport', land: 'Danmark', flag: 'DK', contact: 'Pernille Storm', tel: '+45 70 22 41 18', mail: 'dispo@vestpil.example',
    services: ['ROAD','FTL','LTL'], type: 'Stykgods & fuldlæs', from: 'Danmark', to: ['Tyskland','Sverige','Polen'], score: 93, created: '02. okt 2025' },
  { id: 'TRP', nm: 'Lubex Spedycja', land: 'Polen', flag: 'PL', contact: 'Piotr Kowalski', tel: '+48 71 330 22 14', mail: 'ruch@lubex.example',
    services: ['ROAD','FTL'], type: 'Fuldlæs', from: 'Polen', to: ['Tyskland','Danmark'], score: 85, created: '18. dec 2025' },
  { id: 'FRN', nm: 'Frostvik Logistik', land: 'Sverige', flag: 'SE', contact: 'Anna Lind', tel: '+46 31 12 88 40', mail: 'kyl@frostvik.example',
    services: ['ROAD','FRIGO','FTL'], type: 'Køletransport', from: 'Sverige', to: ['Holland','Tyskland','Danmark'], score: 90, created: '07. feb 2026' },
  { id: 'BLM', nm: 'Sundbro Frakt', land: 'Sverige', flag: 'SE', contact: 'Erik Sahl', tel: '+46 40 55 21 09', mail: 'book@sundbro.example',
    services: ['ROAD','LTL','SEA'], type: 'Stykgods', from: 'Sverige', to: ['Danmark','Polen','Tyskland'], score: 82, created: '29. jan 2026' },
];

const SERVICE_FILTERS = ['ROAD','SEA','AIR','FTL','LTL','FRIGO','INTERMODAL','COURIER','EXPRESS'];

const PROSPECTS = [
  { nm: 'Rhenovex Spedition', land: 'Tyskland', flag: 'DE', contact: 'Klaus Werner', mail: 'info@rhenovex.example', tel: '+49 40 55 12 88',
    services: ['ROAD','FTL'], commodities: ['General cargo','Industrigods'], from: ['Tyskland'], to: ['Danmark','Sverige'], loadTypes: ['Stykgods (LTL)','Full-load (FTL)'],
    source: 'Indkommende e-mail', date: '31. maj 2026', seenIn: 'VS: Tilbud DE→DK · 31-05-2026', note: 'Tilbød konkurrencedygtig pris på DE→DK',
    msg: 'Hi, we have regular capacity Germany to Scandinavia and can offer competitive rates DE→DK. Let us know if we can quote on your shipments.' },
  { nm: 'Iberox Cargo SL', land: 'Spanien', flag: 'ES', contact: 'Sofia Marin', mail: 'ops@iberox.example', tel: '+34 91 220 44 10',
    services: ['ROAD','FRIGO'], commodities: ['Fødevarer (køl)','Frugt & grønt'], from: ['Spanien'], to: ['Danmark','Sverige','Norge'], loadTypes: ['Full-load (FTL)'],
    source: 'Manuelt tilføjet', date: '30. maj 2026', seenIn: 'Tilføjet af Jonas Holt', note: 'Køl-kapacitet ES→Skandinavien',
    msg: 'Buenos días, tenemos capacidad de transporte refrigerado desde España hacia Escandinavia (+2/+6 °C). Disponibles para cotizar.' },
  { nm: 'Albionex Freight Ltd', land: 'Storbritannien', flag: 'GB', contact: 'James Reed', mail: 'book@albionex.example', tel: '+44 1304 22 90 51',
    services: ['ROAD','SEA','LTL'], commodities: ['General cargo'], from: ['Storbritannien'], to: ['Danmark','Tyskland','Holland'], loadTypes: ['Stykgods (LTL)','Full-load (FTL)'],
    source: 'Indkommende e-mail', date: '28. maj 2026', seenIn: 'RE: Ferry + haulage UK→EU · 28-05-2026', note: 'Færge + landtransport UK→EU',
    msg: 'Hello, Albionex Freight offers combined ferry and road haulage UK to mainland EU, LTL and FTL. Happy to be added to your carrier list.' },
];

const USERS = [
  { nm: 'Jonas Holt', un: 'jho', mail: 'jho@nordpil-spedition.example', login: '19-05-2026', active: true, admin: true },
  { nm: 'Mette Krogh', un: 'mkr', mail: 'mkr@nordpil-spedition.example', login: '01-06-2026', active: true, admin: false },
  { nm: 'Anders Vinther', un: 'avi', mail: 'avi@nordpil-spedition.example', login: '01-06-2026', active: true, admin: true },
  { nm: 'Camilla Bach', un: 'cba', mail: 'cba@nordpil-spedition.example', login: '24-04-2026', active: true, admin: false },
  { nm: 'Mikkel Dahl', un: 'mda', mail: 'mda@nordpil-spedition.example', login: '—', active: true, admin: true },
  { nm: 'Line Toft', un: 'lto', mail: 'lto@nordpil-spedition.example', login: '28-05-2026', active: true, admin: false },
  { nm: 'Rasmus Bredahl', un: 'rbr', mail: 'rbr@nordpil-spedition.example', login: '30-05-2026', active: true, admin: false },
  { nm: 'Trine Skov', un: 'tsk', mail: 'tsk@nordpil-spedition.example', login: '18-05-2026', active: true, admin: false },
];

// ---- AI analyse (rich extraction) + outbound RFQ matching ----------------
const CITY_GEO = {
  'Verona':    ['Italien', 'IT', '37121'],
  'Hamborg':   ['Tyskland', 'DE', '20095'],
  'Praha':     ['Tjekkiet', 'CZ', '110 00'],
  'Aarhus':    ['Danmark', 'DK', '8000'],
  'Gdansk':    ['Polen', 'PL', '80-001'],
  'München':   ['Tyskland', 'DE', '80331'],
  'København': ['Danmark', 'DK', '1050'],
  'Göteborg':  ['Sverige', 'SE', '411 01'],
  'Rotterdam': ['Holland', 'NL', '3011'],
  'Dubai':     ['UAE', 'AE', ''],
  'Lynge':     ['Danmark', 'DK', '3540'],
  'Guangzhou': ['Kina', 'CN', ''],
  'Svendborg': ['Danmark', 'DK', '5700'],
  'Shanghai':  ['Kina', 'CN', ''],
  'New York':  ['USA', 'US', '10001'],
  'Zandhoven': ['Belgien', 'BE', '2240'],
  'Bogense':   ['Danmark', 'DK', '5400'],
  'Hamburg':   ['Tyskland', 'DE', '20095'],
  'Tallinn':   ['Estland', 'EE', '10111'],
  'Kolding':   ['Danmark', 'DK', '6000'],
  'Wittenburg':['Tyskland', 'DE', '19243'],
};
function geo(city) { return CITY_GEO[city] || ['—', '--', '']; }

const ANALYSE = {
  'A00541':   { goods: 'Personal Effects', service: 'Søfragt', loadType: 'LCL – Søfragt dellast', incoterm: '', equipment: '', weight: '', packages: '2', dims: '', cbm: '1,0', ldm: '', special: 'Clearance & delivery requested · POD Port of Copenhagen' },
  'RQ-31022': { goods: 'General cargo', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: '', equipment: 'Standard truck', weight: '120 kg', packages: '1 pll', dims: '120×80×50 cm', cbm: '0,5', ldm: '0,4', special: '2 prisoptioner i én mail · loading i morgen · levering Tallinn ASAP' },
  'EC-7741':  { goods: 'Vindmølle-komponenter', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: 'EXW', equipment: 'Standard truck', weight: '900 kg', packages: '2 pll', dims: '', cbm: '', ldm: '1,6', special: 'Pickup 22/04 08:00 · same-day levering til Siemens Gamesa' },
  'SC-44712': { goods: 'Forbrugerelektronik', service: 'Søfragt', loadType: 'FCL – 1×40HC', incoterm: 'EXW', equipment: '40HC', weight: '8.600 kg', packages: '410 ctn', dims: '', cbm: '58,0', ldm: '', special: 'EXW Guangzhou · told + levering til DK5700' },
  'SC-44698': { goods: 'Reklametryk / inserts', service: 'Søfragt', loadType: 'FCL – 1×20GP', incoterm: 'FOB', equipment: '20GP', weight: '11.200 kg', packages: '26 pll', dims: '', cbm: '21,4', ldm: '', special: 'PO #SH-1002 · FOB Shanghai' },
  'SC-44655': { goods: 'General cargo', service: 'Søfragt', loadType: 'LCL – Søfragt dellast', incoterm: 'EXW', equipment: '', weight: '1.850 kg', packages: '6 pll', dims: '120×100×140 cm', cbm: '5,8', ldm: '', special: 'Mangler oplysninger — afventer kunde' },
  'BE-22401': { goods: 'General cargo', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: 'EXW', equipment: 'Standard truck', weight: '4.200 kg', packages: '5 pll', dims: '120×80×250 cm', cbm: '12,0', ldm: '6,0', special: 'Ingen særlige krav' },
  'NR-44823': { goods: 'Møbler / stykgods', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: 'EXW', equipment: 'Tautliner', weight: '12.400 kg', packages: '34', dims: '120×80×220 cm', cbm: '24,8', ldm: '13,6', special: 'Lift ved levering · ikke ADR' },
  'VL-10231': { goods: 'General cargo', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: 'EXW', equipment: 'Tautliner', weight: '9.400 kg', packages: '28', dims: '120×80×180 cm', cbm: '18,2', ldm: '10,1', special: 'Ikke ADR' },
  'BT-77120': { goods: 'Stykgods', service: 'Vejtransport', loadType: 'LTL – Stykgods / dellast', incoterm: 'FCA', equipment: 'Standard truck', weight: '6.100 kg', packages: '18', dims: '120×80×160 cm', cbm: '11,8', ldm: '6,4', special: 'Fleksibel afhentning uge 21' },
  'SC-30441': { goods: 'Fødevarer (køl)', service: 'Vejtransport', loadType: 'LTL – Køl / frigo', incoterm: 'CPT', equipment: 'Frigo +2 °C', weight: '8.800 kg', packages: '22', dims: '120×80×200 cm', cbm: '16,9', ldm: '9,2', special: 'Temperatur +2 °C · ATP-certifikat' },
};
function getAnalyse(req) {
  const a = ANALYSE[req.id] || {};
  const [oC, , oZip] = geo(req.route[0]);
  const [dC, , dZip] = geo(req.route[1]);
  return {
    conf: req.conf,
    originCountry: oC, originCity: req.route[0], originZip: oZip,
    destCountry: dC, destCity: req.route[1], destZip: dZip,
    goods: a.goods || 'General cargo', service: a.service || 'Vejtransport',
    loadType: a.loadType || 'LTL – Stykgods / dellast', incoterm: a.incoterm || 'EXW',
    equipment: a.equipment || 'Standard truck', weight: a.weight || '—', packages: a.packages || '—',
    dims: a.dims || '—', cbm: a.cbm || '', ldm: a.ldm || '', special: a.special || '—',
  };
}

const LANG_BY_FLAG = { DK: 'Dansk', DE: 'Tysk', IT: 'Italiensk', PL: 'Engelsk', SE: 'Svensk', NL: 'Engelsk', CA: 'Engelsk', GB: 'Engelsk', ES: 'Engelsk', CZ: 'Engelsk' };
function getMatched(req) {
  if (!req.offers.length) return [];
  const names = req.offers.map(o => o.nm);
  const pool = CARRIERS.filter(c => names.includes(c.nm));
  for (const c of CARRIERS) { if (pool.length >= 6) break; if (!pool.includes(c)) pool.push(c); }
  return pool.map((c, i) => ({
    id: c.id, nm: c.nm, contact: c.contact, mail: c.mail, land: c.land, flag: c.flag,
    lang: LANG_BY_FLAG[c.flag] || 'Engelsk',
    ref: req.id + '-' + String(i + 1).padStart(2, '0'),
    score: 112 - i * 9,
  }));
}
function buildDraft(req, a, m) {
  return {
    subject: 'Tilbudsforespørgsel — ' + req.route[0] + ' → ' + req.route[1] + ' | Ref: ' + m.ref,
    body:
      'Kære ' + m.contact + ',\n\n' +
      'Vi har en ny forsendelse og vil gerne høre jeres bedste pris:\n\n' +
      '• Oprindelse: ' + a.originZip + ' ' + a.originCity + ', ' + a.originCountry + '\n' +
      '• Destination: ' + a.destZip + ' ' + a.destCity + ', ' + a.destCountry + '\n' +
      '• Servicetype: ' + a.service + '\n' +
      '• Lasttype: ' + a.loadType + '\n' +
      '• Bruttovægt: ' + a.weight + '\n' +
      '• Antal kolli: ' + a.packages + '\n' +
      '• Mål: ' + a.dims + '\n\n' +
      'Send venligst jeres pris og forventede transittid. På forhånd tak.\n\n' +
      'Med venlig hilsen\nNordpil Spedition',
  };
}

// ---- shared atoms ----------------------------------------------------------
function Avatar({ name, accent }) {
  const init = name.split(' ').map(w => w[0]).slice(0, 2).join('').toUpperCase();
  return <span className={'avatar' + (accent ? ' accent' : '')}>{init}</span>;
}
function StatusBadge({ s }) {
  return (
    <span className={'badge ' + STATUS_BADGE[s]}>
      {s === 'new' && <span className="d" style={{ background: 'currentColor' }}></span>}
      {STATUS_LABEL[s]}
    </span>
  );
}
function ServiceTag({ s }) { return <span className="tag">{s}</span>; }

Object.assign(window, {
  Icon, BrandMark, Avatar, StatusBadge, ServiceTag,
  REQUESTS, PIPELINE_STATS, STATUS_LABEL, STATUS_BADGE,
  CARRIERS, SERVICE_FILTERS, PROSPECTS, USERS,
  getAnalyse, getMatched, buildDraft,
});
