// Page: Servizi di Linea
const { useState: useStateLinea } = React;

// ─── Schedule data ──────────────────────────────────────────────────────────
// Each trip = { code, days, notes, times[] } indexed parallel to stops[]
// Time '—' = no stop on this trip
// ANDATA: from origin to terminus

const E5_ANDATA = {
  direction: 'Piacenza → Boscone Cusani',
  validity: 'Feriale Invernale',
  stops: [
    'Piacenza Autostazione',
    'Piacenza P.le Marconi',
    'Piacenza Cittadella',
    'Piacenza b.ra Genova',
    'Piacenza Barriera Torino',
    'Piacenza S. Antonio',
    'Malpaga',
    'Calendasco Piazza',
    'Santimento Macelleria',
    'Boscone Cusani',
  ],
  trips: [
    { code: 'FR', days: 'L-S', note: 'X', times: ['6:30','6:33','—','6:38','—','6:48','6:55','—','—','7:05'] },
    { code: 'FR', days: 'MeS', note: '',  times: ['8:20','8:23','—','8:28','—','8:42','8:50','8:55','—','9:00'] },
    { code: 'FR', days: 'MeS', note: '',  times: ['11:30','11:33','—','11:38','—','11:52','12:00','12:05','—','12:10'] },
    { code: 'FR', days: 'L-S', note: '',  times: ['12:50','12:53','—','12:58','—','13:12','13:20','13:25','—','13:30'] },
    { code: 'SC', days: 'L-S', note: '',  times: ['13:55','—','14:00','14:05','14:07','14:15','14:23','14:30','—','14:35'] },
    { code: 'SC', days: 'L-V', note: 'B', times: ['16:30','—','16:35','16:40','16:42','16:50','16:58','17:05','—','17:10'] },
    { code: 'FR', days: 'L-S', note: 'B', times: ['17:30','17:33','17:39','17:44','17:46','17:54','18:02','18:09','—','18:14'] },
    { code: 'FR', days: 'L-S', note: '',  times: ['18:50','18:53','—','18:58','—','19:12','19:20','19:25','—','19:30'] },
    { code: 'FR', days: 'L-S', note: 'B', times: ['19:25','19:28','—','19:33','—','19:47','19:55','20:00','—','20:05'] },
  ],
  legend: [
    { k: 'X', v: 'La corsa da Calendasco raggiunge Boscone sulla strada del Galeotto, non transita a Santimento.' },
    { k: 'A', v: 'Fermata di carico a S. Antonio Chiesa e Ex Pertite solo nei giorni scolastici.' },
    { k: 'B', v: 'Fermata di scarico a S. Antonio Chiesa e Ex Pertite solo nei giorni scolastici.' },
  ],
};

const E5_RITORNO = {
  direction: 'Boscone Cusani → Piacenza',
  validity: 'Feriale Invernale',
  stops: [
    'Boscone Cusani',
    'Santimento Macelleria',
    'Calendasco Piazza',
    'Malpaga',
    'Piacenza S. Antonio',
    'Piacenza Barriera Torino',
    'Piacenza b.ra Genova',
    'Piacenza Cittadella',
    'Piacenza P.le Marconi',
    'Piacenza Autostazione',
  ],
  trips: [
    { code: 'FR',  days: 'L-S', note: '',  times: ['6:35','6:40','6:45','6:50','7:00','—','—','7:05','7:10','7:13'] },
    { code: 'SC',  days: 'L-V', note: 'A', times: ['7:05','7:10','7:15','7:23','7:25','7:35','7:37','7:40','7:45','7:48'] },
    { code: 'NSC', days: 'L-S', note: '',  times: ['7:05','7:10','7:15','7:23','—','7:35','—','—','7:45','7:48'] },
    { code: 'SC',  days: 'L-V', note: 'A', times: ['7:05','7:10','7:15','7:23','—','—','—','—','7:42','7:42'] },
    { code: 'SC',  days: 'S',   note: '',  times: ['7:05','7:10','7:15','7:23','7:25','—','—','—','7:45','7:50'] },
    { code: 'FR',  days: 'MeS', note: '',  times: ['9:00','9:05','9:10','9:15','—','9:29','—','—','9:34','9:37'] },
    { code: 'FR',  days: 'MeS', note: '',  times: ['12:10','12:15','12:20','12:25','—','12:37','—','—','12:42','12:45'] },
    { code: 'SC',  days: 'L-S', note: '',  times: ['13:50','13:55','14:00','14:05','—','14:19','—','—','14:24','14:27'] },
    { code: 'FR',  days: 'L-S', note: '',  times: ['14:35','14:40','14:45','14:50','—','15:04','—','—','15:09','15:12'] },
    { code: 'SC',  days: 'L-V', note: '',  times: ['17:20','17:25','17:30','17:35','—','17:49','—','—','17:54','17:57'] },
    { code: 'FR',  days: 'L-S', note: '',  times: ['18:20','18:25','18:30','18:35','—','18:49','—','—','18:54','18:57'] },
  ],
  legend: [
    { k: 'A', v: 'Fermata di carico a S. Antonio Chiesa e Ex Pertite solo nei giorni scolastici.' },
  ],
};

const E54_ANDATA = {
  direction: 'Piacenza → Groppovisdomo',
  validity: 'Feriale Invernale',
  stops: [
    'Piacenza Autostazione',
    'S. Polo',
    'S. Giorgio P.le Marconi',
    'Carpaneto P. Oliveti',
    'Carpaneto Stazione',
    'Cimafava',
    'Viustino',
    'Torre Confalonieri',
    'Celleri',
    'Antonelli',
    'Sariano',
    'La Valle',
    'Gropparello arrivo',
    'Gropparello partenza',
    'Castellana / Magazzeno',
    'Groppovisdomo',
  ],
  trips: [
    { code: 'SC',  days: 'L-S', note: '',    times: ['—','—','—','—','6:25','6:27','6:29','6:31','6:34','6:36','6:38','6:41','6:45','—','—','—'] },
    { code: 'FR',  days: 'L-S', note: '',    times: ['—','—','—','—','7:25','7:28','7:30','7:32','7:35','7:37','7:40','7:45','7:50','—','—','—'] },
    { code: 'FR',  days: 'L-S', note: 'O',   times: ['9:05','9:14','9:18','9:22','9:25','9:28','9:30','9:32','9:35','9:37','9:40','9:45','9:50','10:00','—','10:25'] },
    { code: 'FR',  days: 'noG', note: 'D',   times: ['12:15','12:24','12:28','12:32','12:35','12:38','12:40','12:42','12:45','12:47','12:50','12:55','13:00','13:00','—','13:25'] },
    { code: 'FR',  days: 'L-S', note: 'P',   times: ['13:10','—','—','13:27','13:30','13:33','13:35','13:37','13:40','13:42','13:45','13:50','13:55','13:55','—','14:15'] },
    { code: 'NSC', days: 'L-S', note: 'DIR', times: ['14:00','—','—','14:17','14:20','14:23','14:25','14:27','14:30','14:32','14:35','14:40','14:45','14:45','—','15:10'] },
    { code: 'SC',  days: 'L-S', note: '',    times: ['—','—','—','—','—','—','—','—','—','—','—','—','—','—','—','—'] },
    { code: 'FR',  days: 'L-S', note: 'P',   times: ['17:15','17:24','17:28','17:32','17:35','17:38','17:40','17:42','17:45','17:47','17:50','17:55','18:00','18:00','—','18:25'] },
    { code: 'FR',  days: 'L-V', note: 'D',   times: ['19:10','19:19','19:23','19:27','19:30','19:33','19:35','19:37','19:40','19:42','19:45','19:50','19:55','—','—','—'] },
  ],
  legend: [
    { k: 'O',   v: 'A Carpaneto in coincidenza da Piacenza.' },
    { k: 'D',   v: 'Nella tratta Piacenza-Carpaneto ferma solo a S. Polo Scuole e S. Giorgio P. Marconi.' },
    { k: 'DIR', v: 'Nella tratta Piacenza-Carpaneto non effettua fermate intermedie.' },
    { k: 'P',   v: 'Il tratto deve essere prenotato presso Tempi Agenzia al num. 0523 385268.' },
    { k: 'Giovedì', v: 'Gropparello → Gusano → Gropparello → Castellana → Croviano → Groppovisdomo → Obolo → Montechino → Magazzeno · ore 10:35' },
  ],
};

const E54_RITORNO = {
  direction: 'Groppovisdomo → Piacenza',
  validity: 'Feriale Invernale',
  stops: [
    'Groppovisdomo',
    'Magazzeno / Castellana',
    'Gropparello partenza',
    'La Valle',
    'Sariano',
    'Antonelli',
    'Celleri',
    'Torre Confalonieri',
    'Viustino',
    'Cimafava',
    'Carpaneto Stazione',
    'Carpaneto P. Oliveti',
    'S. Giorgio P.le Marconi',
    'S. Polo',
    'Piacenza P.le Marconi',
    'Piacenza Autostazione',
  ],
  trips: [
    { code: 'FR',  days: 'L-S', note: 'O', times: ['6:15','6:45','6:50','6:55','7:00','7:02','7:04','7:07','7:09','7:11','7:13','7:16','7:20','7:22','7:35','7:48'] },
    { code: 'FR',  days: 'L-S', note: 'P', times: ['—','—','6:50','6:55','7:00','7:02','7:04','7:07','7:09','7:11','7:13','7:16','7:20','7:22','7:35','7:48'] },
    { code: 'SC',  days: 'L-S', note: 'D', times: ['—','—','6:45','6:50','6:55','6:57','6:59','7:02','7:04','7:06','7:08','—','—','—','—','—'] },
    { code: 'NSC', days: 'L-S', note: 'O', times: ['—','—','8:00','8:05','8:10','8:12','8:14','8:16','8:18','8:20','8:25','8:28','8:32','8:36','8:47','8:58'] },
    { code: 'SC',  days: 'L-S', note: 'D', times: ['—','—','—','—','—','—','—','—','—','—','—','—','—','—','—','—'] },
    { code: 'FR',  days: 'L-S', note: 'P', times: ['9:20','9:50','10:00','10:05','10:10','10:12','10:14','10:16','10:18','10:20','10:25','10:28','10:32','10:36','10:47','—'] },
    { code: 'FR',  days: 'noG', note: 'D', times: ['—','—','12:00','12:05','12:10','12:12','12:14','12:16','12:18','12:20','12:25','12:28','12:32','12:36','12:47','—'] },
    { code: 'FR',  days: 'L-S', note: 'D', times: ['—','—','—','—','—','—','—','—','—','—','—','—','—','—','—','—'] },
    { code: 'SC',  days: 'L-S', note: 'P', times: ['—','—','—','—','—','—','—','—','—','—','—','—','—','—','—','—'] },
    { code: 'FR',  days: 'L-S', note: 'D', times: ['14:15','14:45','15:05','15:10','15:15','15:17','15:19','15:21','15:23','15:25','15:30','15:33','15:37','15:41','15:52','—'] },
    { code: 'FR',  days: 'L-S', note: 'D', times: ['—','—','18:05','18:10','18:15','18:17','18:19','18:21','18:23','18:25','18:30','18:33','18:37','18:41','18:52','—'] },
  ],
  legend: [
    { k: 'O',   v: 'A Carpaneto in coincidenza per Piacenza.' },
    { k: 'D',   v: 'Nella tratta Piacenza-Carpaneto ferma solo a S. Polo Scuole e S. Giorgio P. Marconi.' },
    { k: 'P',   v: 'Il tratto deve essere prenotato presso Tempi Agenzia al num. 0523 385268.' },
    { k: 'Giovedì', v: 'Magazzeno → Montechino → Obolo → Groppovisdomo → Croviano → Castellana → Gropparello → Gusano → Gropparello · ore 6:45' },
  ],
};

const E54_FESTIVO = {
  direction: 'Gropparello → Piacenza',
  validity: 'Festivo Invernale',
  stops: [
    'Gropparello partenza',
    'La Valle',
    'Sariano',
    'Antonelli',
    'Celleri',
    'Torre Confalonieri',
    'Viustino',
    'Cimafava',
    'Carpaneto Str. Provinciale',
    'S. Giorgio P.le Marconi',
    'S. Polo',
    'Piacenza P.le Marconi',
    'Piacenza Autostazione',
  ],
  trips: [
    { code: 'FS', days: '', note: '', times: ['8:10','8:15','8:20','8:22','8:24','8:27','8:29','8:31','8:33','8:40','8:42','8:55','8:58'] },
    { code: 'FS', days: '', note: '', times: ['12:10','12:15','12:20','12:22','12:24','12:27','12:29','12:31','12:33','12:40','12:42','12:55','12:58'] },
    { code: 'FS', days: '', note: '', times: ['17:10','17:15','17:20','17:22','17:24','17:27','17:29','17:31','17:33','17:40','17:42','17:55','17:58'] },
  ],
  legend: [
    { k: 'FS', v: 'Corsa festiva.' },
  ],
};

// ─── Lines ──────────────────────────────────────────────────────────────────
const lineeData = [
  {
    id: 'boscone',
    code: 'E5',
    name: 'Linea Boscone',
    area: 'Val Tidone',
    route: 'Piacenza · Calendasco · Santimento · Boscone Cusani',
    pdf: 'assets/orari/Boscone.pdf',
    schedules: { andata: E5_ANDATA, ritorno: E5_RITORNO },
  },
  {
    id: 'groppovisdomo',
    code: 'E54',
    name: 'Linea Groppovisdomo',
    area: 'Val d\'Arda',
    route: 'Piacenza · Carpaneto · Gropparello · Groppovisdomo',
    pdf: 'assets/orari/Groppovisdomo.pdf',
    schedules: { andata: E54_ANDATA, ritorno: E54_RITORNO, festivo: E54_FESTIVO },
  },
  {
    id: 'gropparello',
    code: 'E51',
    name: 'Linea Gropparello',
    area: 'Val Chero',
    route: 'Piacenza · Carpaneto · Gropparello',
    pdf: 'assets/orari/Gropparello.pdf',
    schedules: null, // PDF is image-based, not transcribed yet
  },
];

// ─── Schedule Table component ───────────────────────────────────────────────
function ScheduleTable({ data }) {
  if (!data) return null;
  return (
    <div style={{ background: '#fff', border: '1px solid #e8e8e8', borderRadius: 6, overflow: 'hidden' }}>
      {/* Header */}
      <div style={{ background: '#1a1a1a', color: '#fff', padding: '14px 20px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 8 }}>
        <div>
          <div style={{ fontFamily: 'Barlow Condensed, sans-serif', fontSize: 18, fontWeight: 600, letterSpacing: '0.02em' }}>{data.direction}</div>
          <div style={{ fontFamily: 'Inter, sans-serif', fontSize: 11, color: 'rgba(255,255,255,0.65)', letterSpacing: '0.08em', textTransform: 'uppercase', marginTop: 2 }}>{data.validity}</div>
        </div>
      </div>
      {/* Table scroll wrapper */}
      <div style={{ overflowX: 'auto' }}>
        <table style={{ width: '100%', borderCollapse: 'collapse', fontFamily: 'Inter, sans-serif', fontSize: 12, minWidth: 600 }}>
          <thead>
            <tr style={{ background: '#f8f8f8', borderBottom: '2px solid #ddd' }}>
              <th style={{ ...thSticky, textAlign: 'left' }}>Fermata</th>
              {data.trips.map((t, i) => (
                <th key={i} style={thCell}>
                  <div style={{ fontSize: 10, fontWeight: 700, color: '#3f90b9', letterSpacing: '0.05em' }}>{t.code}</div>
                  <div style={{ fontSize: 10, color: '#666', marginTop: 2 }}>{t.days}</div>
                  {t.note && <div style={{ fontSize: 9, color: '#aaa', marginTop: 2, fontWeight: 700 }}>{t.note}</div>}
                </th>
              ))}
            </tr>
          </thead>
          <tbody>
            {data.stops.map((stop, si) => (
              <tr key={si} style={{ borderBottom: '1px solid #f0f0f0' }}>
                <td style={{ ...tdSticky, fontWeight: 600, color: '#222' }}>{stop}</td>
                {data.trips.map((t, ti) => {
                  const time = t.times[si];
                  const isBlank = !time || time === '—';
                  return (
                    <td key={ti} style={{ ...tdCell, color: isBlank ? '#ccc' : '#444', fontWeight: isBlank ? 400 : 500 }}>
                      {isBlank ? '—' : time}
                    </td>
                  );
                })}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
      {/* Legend */}
      {data.legend && data.legend.length > 0 && (
        <div style={{ background: '#f8f8f8', padding: '14px 20px', borderTop: '1px solid #eee' }}>
          <div style={{ fontFamily: 'Inter, sans-serif', fontSize: 11, fontWeight: 700, color: '#666', letterSpacing: '0.08em', textTransform: 'uppercase', marginBottom: 8 }}>Legenda</div>
          <div style={{ display: 'grid', gap: 4 }}>
            {data.legend.map((l, i) => (
              <div key={i} style={{ display: 'flex', gap: 10, fontFamily: 'Inter, sans-serif', fontSize: 12, color: '#555', lineHeight: 1.5 }}>
                <span style={{ flexShrink: 0, fontWeight: 700, color: '#3f90b9', minWidth: 70 }}>{l.k}</span>
                <span>{l.v}</span>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}

const thSticky = { padding: '12px 16px', fontSize: 11, fontWeight: 700, color: '#666', letterSpacing: '0.06em', textTransform: 'uppercase', whiteSpace: 'nowrap', position: 'sticky', left: 0, background: '#f8f8f8', zIndex: 2, borderRight: '1px solid #ddd' };
const thCell = { padding: '8px 10px', textAlign: 'center', whiteSpace: 'nowrap' };
const tdSticky = { padding: '11px 16px', whiteSpace: 'nowrap', position: 'sticky', left: 0, background: '#fff', zIndex: 1, borderRight: '1px solid #f0f0f0', fontFamily: 'Inter, sans-serif', fontSize: 13 };
const tdCell = { padding: '10px 10px', textAlign: 'center', fontFamily: 'Inter, sans-serif', fontSize: 13, whiteSpace: 'nowrap' };

// ─── Page ───────────────────────────────────────────────────────────────────
function PageLinea() {
  const [active, setActive] = useStateLinea('boscone');
  const [tab, setTab] = useStateLinea('andata');
  const [pdfOpen, setPdfOpen] = useStateLinea(false);
  const linea = lineeData.find(l => l.id === active);
  const tabs = linea.schedules ? Object.keys(linea.schedules) : [];
  const tabLabels = { andata: 'Andata', ritorno: 'Ritorno', festivo: 'Festivo' };
  // Ensure tab is valid when switching lines
  const currentTab = tabs.includes(tab) ? tab : tabs[0];
  const currentSchedule = linea.schedules ? linea.schedules[currentTab] : null;

  return (
    <div>
      <PageHeader
        title="Servizi di Linea"
        sub="Trasporto pubblico locale gestito da Rossi Pietro Bus sul territorio piacentino. Consulta gli orari online o scarica il PDF."
        breadcrumb="Home / Servizi / Servizi di Linea"
      />

      <section className="rp-section" style={{ background: '#f8f8f8' }}>
        <div className="rp-container">
          {/* Line cards */}
          <div className="rp-linea-cards" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16, marginBottom: 36 }}>
            {lineeData.map(l => {
              const isActive = active === l.id;
              return (
                <button key={l.id} onClick={() => { setActive(l.id); setTab('andata'); setPdfOpen(false); }} style={{
                  background: isActive ? '#1a1a1a' : '#fff',
                  border: '1px solid',
                  borderColor: isActive ? '#1a1a1a' : '#e8e8e8',
                  borderRadius: 8,
                  padding: '20px 22px',
                  textAlign: 'left',
                  cursor: 'pointer',
                  transition: 'all 0.2s',
                  fontFamily: 'Inter, sans-serif',
                  display: 'flex', flexDirection: 'column', gap: 6,
                }}
                onMouseEnter={e => { if (!isActive) { e.currentTarget.style.borderColor = '#3f90b9'; } }}
                onMouseLeave={e => { if (!isActive) { e.currentTarget.style.borderColor = '#e8e8e8'; } }}
                >
                  <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 4 }}>
                    <span style={{ background: '#ffbf01', color: '#1a1a1a', fontSize: 11, fontWeight: 700, padding: '3px 8px', borderRadius: 3, letterSpacing: '0.06em' }}>{l.code}</span>
                    <span style={{ fontSize: 11, fontWeight: 600, color: isActive ? 'rgba(255,255,255,0.55)' : '#888', letterSpacing: '0.06em', textTransform: 'uppercase' }}>{l.area}</span>
                  </div>
                  <div style={{ fontFamily: 'Barlow Condensed, sans-serif', fontSize: 22, fontWeight: 600, color: isActive ? '#fff' : '#222', lineHeight: 1.1 }}>{l.name}</div>
                  <div style={{ fontSize: 12, color: isActive ? 'rgba(255,255,255,0.65)' : '#666', lineHeight: 1.5, marginTop: 2 }}>{l.route}</div>
                </button>
              );
            })}
          </div>

          {/* Toolbar */}
          <div style={{ background: '#fff', border: '1px solid #e8e8e8', borderBottom: 'none', borderRadius: '8px 8px 0 0', padding: '14px 20px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 14 }}>
            {/* Tabs */}
            <div style={{ display: 'flex', gap: 4 }}>
              {tabs.length > 0 ? tabs.map(t => (
                <button key={t} onClick={() => setTab(t)} style={{
                  background: t === currentTab ? '#3f90b9' : 'transparent',
                  color: t === currentTab ? '#fff' : '#666',
                  border: 'none',
                  borderRadius: 4,
                  padding: '8px 18px',
                  fontSize: 13,
                  fontWeight: 600,
                  cursor: 'pointer',
                  fontFamily: 'Inter, sans-serif',
                  transition: 'all 0.2s',
                  letterSpacing: '0.02em',
                }}>
                  {tabLabels[t] || t}
                </button>
              )) : (
                <span style={{ fontFamily: 'Inter, sans-serif', fontSize: 13, color: '#888' }}>Orari disponibili solo via PDF</span>
              )}
            </div>
            {/* Actions */}
            <div style={{ display: 'flex', gap: 8 }}>
              <button onClick={() => setPdfOpen(o => !o)} style={{
                display: 'inline-flex', alignItems: 'center', gap: 6,
                background: pdfOpen ? '#1a1a1a' : '#fff', color: pdfOpen ? '#fff' : '#444',
                border: '1px solid', borderColor: pdfOpen ? '#1a1a1a' : '#ddd',
                padding: '8px 14px', borderRadius: 4, fontSize: 12, fontWeight: 600,
                cursor: 'pointer', fontFamily: 'Inter, sans-serif', transition: 'all 0.2s',
              }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17a5 5 0 1 1 0-10 5 5 0 0 1 0 10zm0-8a3 3 0 1 0 0 6 3 3 0 0 0 0-6z"/></svg>
                {pdfOpen ? 'Nascondi PDF' : 'Vedi PDF'}
              </button>
              <a href={linea.pdf} download style={{
                display: 'inline-flex', alignItems: 'center', gap: 6,
                background: '#3f90b9', color: '#fff', textDecoration: 'none',
                padding: '8px 16px', borderRadius: 4, fontSize: 12, fontWeight: 700,
                fontFamily: 'Inter, sans-serif', transition: 'background 0.2s',
              }}
                onMouseEnter={e => e.currentTarget.style.background = '#1a1a1a'}
                onMouseLeave={e => e.currentTarget.style.background = '#3f90b9'}
              >
                <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/></svg>
                Scarica PDF
              </a>
            </div>
          </div>

          {/* Schedule table */}
          {currentSchedule && !pdfOpen && (
            <div style={{ borderRadius: '0 0 8px 8px', overflow: 'hidden', border: '1px solid #e8e8e8', borderTop: 'none' }}>
              <ScheduleTable data={currentSchedule} />
            </div>
          )}

          {/* Empty state for Gropparello */}
          {!currentSchedule && !pdfOpen && (
            <div style={{ background: '#fff', border: '1px solid #e8e8e8', borderTop: 'none', borderRadius: '0 0 8px 8px', padding: '48px 32px', textAlign: 'center' }}>
              <svg width="42" height="42" viewBox="0 0 24 24" fill="#ddd" style={{ marginBottom: 12 }}><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 14H7v-2h5v2zm5-4H7v-2h10v2zm0-4H7V7h10v2z"/></svg>
              <p style={{ fontFamily: 'Inter, sans-serif', fontSize: 14, color: '#888', margin: '0 0 18px' }}>La tabella HTML per questa linea sarà disponibile a breve. Consulta intanto il PDF ufficiale.</p>
              <button onClick={() => setPdfOpen(true)} style={{ background: '#3f90b9', color: '#fff', border: 'none', padding: '10px 22px', borderRadius: 4, fontSize: 13, fontWeight: 600, cursor: 'pointer', fontFamily: 'Inter, sans-serif' }}>Visualizza PDF</button>
            </div>
          )}

          {/* PDF view */}
          {pdfOpen && (
            <div style={{ background: '#fff', padding: 16, border: '1px solid #e8e8e8', borderTop: 'none', borderRadius: '0 0 8px 8px' }}>
              <object data={linea.pdf + '#view=FitH'} type="application/pdf" style={{ width: '100%', height: 720, borderRadius: 4, border: '1px solid #e8e8e8' }}>
                <div style={{ padding: '60px 40px', textAlign: 'center', background: '#f8f8f8' }}>
                  <p style={{ fontFamily: 'Inter, sans-serif', fontSize: 14, color: '#666', marginBottom: 16 }}>Il browser non supporta la visualizzazione PDF.</p>
                  <a href={linea.pdf} target="_blank" rel="noopener noreferrer" style={{ display: 'inline-block', background: '#3f90b9', color: '#fff', padding: '12px 28px', borderRadius: 3, fontSize: 14, fontWeight: 700, textDecoration: 'none', fontFamily: 'Inter, sans-serif' }}>Apri PDF in una nuova scheda</a>
                </div>
              </object>
            </div>
          )}

          {/* Info bar */}
          <div style={{ marginTop: 28, padding: '18px 22px', background: '#fff', border: '1px solid #eee', borderLeft: '4px solid #ffbf01', borderRadius: 4, display: 'flex', alignItems: 'flex-start', gap: 14 }}>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="#3f90b9" style={{ flexShrink: 0, marginTop: 1 }}><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>
            <div>
              <p style={{ fontFamily: 'Inter, sans-serif', fontSize: 14, color: '#222', fontWeight: 600, margin: '0 0 4px' }}>Informazioni utili</p>
              <p style={{ fontFamily: 'Inter, sans-serif', fontSize: 13, color: '#555', lineHeight: 1.65, margin: 0 }}>
                <strong>FR</strong> = Feriale · <strong>SC</strong> = Scolastico · <strong>NSC</strong> = Non scolastico · <strong>FS</strong> = Festivo · <strong>L-S</strong> = Lun-Sab · <strong>L-V</strong> = Lun-Ven · <strong>MeS</strong> = Mercoledì e Sabato · <strong>noG</strong> = escluso Giovedì.
                <br />Gli orari sono soggetti a variazione nei giorni festivi e in caso di scioperi o eventi straordinari. Per informazioni: <strong>0523/856382</strong> · <strong>info@rossipietrobus.com</strong>
              </p>
            </div>
          </div>
        </div>
      </section>
    </div>
  );
}

// Responsive: cards stack on mobile
const lineaStyleTag = document.createElement('style');
lineaStyleTag.textContent = `
  @media (max-width: 900px) {
    .rp-linea-cards { grid-template-columns: 1fr !important; }
  }
`;
document.head.appendChild(lineaStyleTag);

Object.assign(window, { PageLinea });
