`
}).join('')
// Plugin sections after the days — every value is host-vetted plain text and
// still escHtml'd here (same treatment as the core content above).
const pluginSectionsHtml = pluginSections.length === 0 ? '' : `
${pluginSections.map(s => `
${escHtml(s.title)}
${(s.paragraphs || []).map(p => `
${escHtml(p)}
`).join('')}
${s.table ? `
${s.table.headers.map(h => `
${escHtml(h)}
`).join('')}
${s.table.rows.map(row => `
${row.map(cell => `
${escHtml(cell)}
`).join('')}
`).join('')}
` : ''}
`).join('')}
`
const html = `
${escHtml(trip?.title || tr('pdf.travelPlan'))}
${coverImg ? `` : ''}
${coverImg
? `
`
: ``}
${escHtml(tr('pdf.travelPlan'))}
${escHtml(trip?.title || 'My Trip')}
${trip?.description ? `
${escHtml(trip.description)}
` : ''}
${range ? `
${range}
` : ''}
${sorted.length}
${escHtml(tr('dashboard.days'))}
${places?.length || 0}
${escHtml(tr('dashboard.places'))}
${totalAssigned}
${escHtml(tr('pdf.planned'))}
${totalCost > 0 ? `
${totalCost.toLocaleString(loc)}
${escHtml(tr('pdf.costLabel'))}
` : ''}
${daysHtml}
${pluginSectionsHtml}
`
// Open in modal with srcdoc iframe (no URL loading = no X-Frame-Options issue)
const overlay = document.createElement('div')
overlay.id = 'pdf-preview-overlay'
overlay.style.cssText = 'position:fixed;inset:0;background:rgba(0,0,0,0.7);z-index:9999;display:flex;align-items:center;justify-content:center;padding:8px;'
overlay.onclick = (e) => { if (e.target === overlay) overlay.remove() }
const card = document.createElement('div')
card.style.cssText = 'width:100%;max-width:1000px;height:95vh;background:var(--bg-card);border-radius:12px;overflow:hidden;display:flex;flex-direction:column;box-shadow:0 20px 60px rgba(0,0,0,0.3);'
const header = document.createElement('div')
header.style.cssText = 'display:flex;align-items:center;justify-content:space-between;padding:10px 16px;border-bottom:1px solid var(--border-primary);flex-shrink:0;'
header.innerHTML = `
${escHtml(trip?.title || tr('pdf.travelPlan'))}
`
const iframe = document.createElement('iframe')
iframe.style.cssText = 'flex:1;width:100%;border:none;'
// No script runs inside the document (print is parent-initiated), so withhold
// allow-scripts to keep the sandbox tight.
iframe.sandbox = 'allow-same-origin allow-modals'
iframe.srcdoc = html
card.appendChild(header)
card.appendChild(iframe)
overlay.appendChild(card)
document.body.appendChild(overlay)
const closeBtn = header.querySelector('#pdf-close-btn')
if (closeBtn) closeBtn.onclick = () => overlay.remove()
const printBtn = header.querySelector('#pdf-print-btn')
if (printBtn) printBtn.onclick = () => { iframe.contentWindow?.print() }
}