// pdf-docs.jsx — print-ready PDF documents: registration list, attendance list, attendance certificate.
// Everything is rendered into a hidden same-origin iframe and sent to the browser's print → "Save as PDF".

const PRINT_FONTS = '<link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&family=IBM+Plex+Sans+Arabic:wght@400;500;600;700&display=swap" rel="stylesheet">';

function absUrl(p) { try { return new URL(p, location.href).href; } catch (e) { return p; } }
function clusterLogoSrc() { return (window.__resources && window.__resources.clusterLogo) || absUrl('assets/cluster-logo.png'); }

function arDate(d) {
  try { return new Date(d).toLocaleDateString('ar-EG', { year: 'numeric', month: 'long', day: 'numeric' }); }
  catch (e) { return String(d || ''); }
}
function arDateTime(d) {
  try { return new Date(d).toLocaleString('ar-EG', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }
  catch (e) { return String(d || ''); }
}
const esc = (s) => String(s == null ? '' : s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');

/* ---------- official stamp of أكاديمية المبتكرين ---------- */
function academySealSVG(size = 150) {
  const stars = [0, 60, 120, 180, 240, 300].map(a => {
    const r = 78, x = 100 + r * Math.cos((a - 90) * Math.PI / 180), y = 100 + r * Math.sin((a - 90) * Math.PI / 180);
    return `<circle cx="${x.toFixed(1)}" cy="${y.toFixed(1)}" r="2.6" fill="#0f6b52"/>`;
  }).join('');
  return `<svg viewBox="0 0 200 200" width="${size}" height="${size}" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="94" fill="#ffffff" fill-opacity=".55" stroke="#0f6b52" stroke-width="3.4"/>
<circle cx="100" cy="100" r="86" fill="none" stroke="#0f6b52" stroke-width="1.1"/>
<circle cx="100" cy="100" r="68" fill="none" stroke="#0f6b52" stroke-width="1.8"/>
${stars}
<g fill="none" stroke="#0f6b52" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" transform="translate(100,52) scale(.92)">
<path d="M0-14c-4-3-10-2-12.6 1.8-3.2.7-4.9 3.7-4 6.9-2.2 2-2.3 5.2-.2 7.3-.7 2.9 1.4 5.7 4.5 6.2 1.6 2.6 5.2 3.7 8.3 2.2"/>
<path d="M0-16v32" stroke-width="1.7"/>
<path d="M4 -6h7M11-6l3.6 3M3 4h6M9 4l3.2 3" stroke-width="1.5"/>
<circle cx="11" cy="-6" r="2.2" fill="#0f6b52" stroke="none"/><circle cx="14.6" cy="-3" r="1.8" fill="#0f6b52" stroke="none"/>
<circle cx="9" cy="4" r="2.2" fill="#0f6b52" stroke="none"/><circle cx="12.2" cy="7" r="1.7" fill="#0f6b52" stroke="none"/>
</g>
<text x="100" y="104" text-anchor="middle" font-family="Tajawal, 'IBM Plex Sans Arabic', sans-serif" font-size="19" font-weight="800" fill="#0f6b52">أكاديمية المبتكرين</text>
<text x="100" y="124" text-anchor="middle" font-family="Tajawal, sans-serif" font-size="12.5" font-weight="600" fill="#0f6b52">صحة المجتمع للابتكار</text>
<line x1="58" y1="133" x2="142" y2="133" stroke="#0f6b52" stroke-width="1"/>
<text x="100" y="150" text-anchor="middle" font-family="Tajawal, sans-serif" font-size="11.5" font-weight="700" fill="#0f6b52">ختم معتمد · CERTIFIED</text>
<text x="100" y="166" text-anchor="middle" font-family="Tajawal, sans-serif" font-size="10" font-weight="500" fill="#0f6b52">تجمع الرياض الصحي الأول</text>
</svg>`;
}

/* ---------- shared print shell ---------- */
const PRINT_BASE = `*{box-sizing:border-box;-webkit-print-color-adjust:exact;print-color-adjust:exact}
body{margin:0;font-family:Tajawal,'IBM Plex Sans Arabic',system-ui,sans-serif;color:#12211d;background:#fff}
h1,h2,h3{margin:0;font-weight:800}
table{width:100%;border-collapse:collapse}
th,td{border:1px solid #c9d8d2;padding:7px 9px;font-size:11.5pt;text-align:start;vertical-align:middle}
th{background:#eef6f3;font-weight:700;font-size:11pt}
tr{page-break-inside:avoid}
thead{display:table-header-group}
.mut{color:#5c7069}
@page{margin:12mm}`;

function printDoc(bodyHtml, css, title) {
  const html = `<!DOCTYPE html><html lang="ar" dir="rtl"><head><meta charset="utf-8"><title>${esc(title || '')}</title>${PRINT_FONTS}<style>${PRINT_BASE}${css || ''}</style></head><body>${bodyHtml}</body></html>`;
  const prev = document.getElementById('__print_frame');
  if (prev) prev.remove();
  const f = document.createElement('iframe');
  f.id = '__print_frame';
  f.setAttribute('title', 'print');
  f.style.cssText = 'position:fixed;left:-9999px;top:0;width:1000px;height:1200px;border:0;opacity:0;';
  // window.__docPreview shows the document on screen instead of printing (used for design review)
  f.onload = () => {    const run = () => { try { f.contentWindow.focus(); f.contentWindow.print(); } catch (e) {} };
    // give webfonts + logo a moment so the PDF is not missing glyphs
    try { f.contentDocument.fonts.ready.then(() => setTimeout(run, 350)); } catch (e) { setTimeout(run, 700); }
  };
  document.body.appendChild(f);
  f.srcdoc = html;
}

function docHeader(title, sub) {
  return `<div style="display:flex;align-items:center;gap:14px;border-bottom:2.5px solid #0f6b52;padding-bottom:12px">
  <img src="${clusterLogoSrc()}" alt="" style="height:52px;width:auto;object-fit:contain"/>
  <div style="flex:1">
    <div style="font-size:9.5pt;font-weight:600;color:#5c7069;letter-spacing:.2px">المملكة العربية السعودية · وزارة الصحة · تجمع الرياض الصحي الأول</div>
    <div style="font-size:16pt;font-weight:800;margin-top:2px">${esc(title)}</div>
    <div style="font-size:10.5pt;color:#5c7069">${esc(sub || 'إدارة تجربة الموظف · صحة المجتمع للابتكار · أكاديمية المبتكرين')}</div>
  </div>
  <div style="text-align:center;flex:none">${academySealSVG(70)}</div>
</div>`;
}

function courseFacts(c, L) {
  const rows = [
    ['اسم الدورة', L(c.title)],
    ['المقدّم / المدرّب', L(c.presenter)],
    ['التاريخ', L(c.date)],
    ['المدة', L(c.dur)],
    ['النمط', c.mode === 'virtual' ? 'افتراضي (عن بُعد)' : 'حضوري'],
    ['الحضور المستهدف', (c.target === '' || c.target == null) ? '—' : c.target],
    ['هدف الدورة', L(c.objective)],
  ].filter(r => r[1] !== '' && r[1] != null);
  return `<table style="margin-top:14px"><tbody>${rows.map(r => `<tr><th style="width:32%">${esc(r[0])}</th><td>${esc(r[1])}</td></tr>`).join('')}</tbody></table>`;
}

function signBlock(labels) {
  return `<div style="display:flex;gap:18px;margin-top:26px;page-break-inside:avoid">${labels.map(l => `<div style="flex:1;text-align:center"><div style="border-bottom:1px dashed #8fa8a0;height:42px"></div><div style="font-size:10.5pt;font-weight:600;margin-top:6px">${esc(l)}</div></div>`).join('')}</div>`;
}

/* ---------- 1. registration list ---------- */
function printRegistrations(course, rows, L) {
  const body = `${docHeader('قائمة المسجّلين في الدورة', 'كشف تسجيل رسمي · Registration sheet')}
${courseFacts(course, L)}
<div style="display:flex;justify-content:space-between;align-items:center;margin-top:20px">
  <h2 style="font-size:13pt">المسجّلون (${rows.length})</h2>
  <span class="mut" style="font-size:10pt">طُبع في ${esc(arDateTime(Date.now()))}</span>
</div>
<table style="margin-top:8px"><thead><tr>
<th style="width:34px">م</th><th>الاسم</th><th style="width:90px">الرقم الوظيفي</th><th>الإدارة</th><th>البريد الإلكتروني</th><th style="width:110px">الجوال</th><th style="width:105px">تاريخ التسجيل</th>
</tr></thead><tbody>
${rows.length ? rows.map((r, i) => `<tr><td>${i + 1}</td><td style="font-weight:600">${esc(r.name)}</td><td>${esc(r.empId)}</td><td>${esc(r.dept)}</td><td style="font-size:10pt" dir="ltr">${esc(r.email)}</td><td dir="ltr">${esc(r.phone)}</td><td style="font-size:10pt">${esc(arDate(r.at))}</td></tr>`).join('') : '<tr><td colspan="7" style="text-align:center;padding:22px" class="mut">لا يوجد مسجّلون بعد</td></tr>'}
</tbody></table>
${signBlock(['قائدة الابتكار', 'سفير الابتكار', 'مدير إدارة تجربة الموظف'])}`;
  printDoc(body, '', 'قائمة المسجّلين — ' + L(course.title));
}

/* ---------- 2. attendance list ---------- */
function printAttendance(course, rows, L) {
  const present = rows.filter(r => r.attended);
  const pct = rows.length ? Math.round((present.length / rows.length) * 100) : 0;
  const body = `${docHeader('كشف حضور الدورة', 'كشف حضور رسمي · Attendance sheet')}
${courseFacts(course, L)}
<div style="display:flex;gap:10px;margin-top:18px">
  ${[['عدد المسجّلين', rows.length], ['عدد الحاضرين', present.length], ['نسبة الحضور', pct + '٪']].map(k => `<div style="flex:1;border:1px solid #c9d8d2;border-radius:8px;padding:10px 12px;background:#f6fbf9"><div class="mut" style="font-size:10pt">${k[0]}</div><div style="font-size:17pt;font-weight:800;color:#0f6b52">${k[1]}</div></div>`).join('')}
</div>
<h2 style="font-size:13pt;margin-top:20px">الحاضرون (${present.length})</h2>
<table style="margin-top:8px"><thead><tr>
<th style="width:34px">م</th><th>الاسم</th><th style="width:90px">الرقم الوظيفي</th><th>الإدارة</th><th style="width:135px">وقت تسجيل الحضور</th><th style="width:70px">الشهادة</th><th style="width:120px">التوقيع</th>
</tr></thead><tbody>
${present.length ? present.map((r, i) => `<tr><td>${i + 1}</td><td style="font-weight:600">${esc(r.name)}</td><td>${esc(r.empId)}</td><td>${esc(r.dept)}</td><td style="font-size:10pt">${esc(arDateTime(r.attendedAt))}</td><td style="font-size:9.5pt" dir="ltr">${esc(r.certRef || '')}</td><td></td></tr>`).join('') : '<tr><td colspan="7" style="text-align:center;padding:22px" class="mut">لم يُسجّل أي حضور بعد</td></tr>'}
</tbody></table>
${rows.length > present.length ? `<h2 style="font-size:12.5pt;margin-top:22px">مسجّلون لم يحضروا (${rows.length - present.length})</h2>
<table style="margin-top:8px"><thead><tr><th style="width:34px">م</th><th>الاسم</th><th style="width:90px">الرقم الوظيفي</th><th>الإدارة</th><th style="width:110px">الجوال</th></tr></thead><tbody>
${rows.filter(r => !r.attended).map((r, i) => `<tr><td>${i + 1}</td><td>${esc(r.name)}</td><td>${esc(r.empId)}</td><td>${esc(r.dept)}</td><td dir="ltr">${esc(r.phone)}</td></tr>`).join('')}
</tbody></table>` : ''}
${signBlock(['منسّق الدورة', 'قائدة الابتكار', 'المدرّب / المقدّم'])}`;
  printDoc(body, '', 'كشف الحضور — ' + L(course.title));
}

/* ---------- 3. attendance certificate (one page, landscape) ---------- */
function certRefFor(course, person) {
  const base = String(course.id || '') + '|' + String(person.empId || person.email || person.name || '');
  let h = 0;
  for (let i = 0; i < base.length; i++) h = (h * 31 + base.charCodeAt(i)) % 100000;
  return 'IBT-CERT-' + new Date().getFullYear() + '-' + String(h).padStart(5, '0');
}

function printCertificate(course, person, L) {
  const ref = person.certRef || certRefFor(course, person);
  const css = certCSS();
  printDoc(certSheet(course, person, L, ref), css, 'شهادة حضور — ' + (person.name || ''));
  return ref;
}

// all attendees in one print job — one page per certificate
function printCertificates(course, people, L) {
  if (!people || !people.length) return;
  const body = people.map((p, i) => certSheet(course, p, L, p.certRef || certRefFor(course, p), i < people.length - 1)).join('');
  printDoc(body, certCSS(), 'شهادات الحضور — ' + L(course.title));
}

function certCSS() {
  return `@page{size:A4 landscape;margin:0}
body{background:#fff}
.sheet{width:297mm;height:210mm;padding:12mm;position:relative;overflow:hidden}
.brk{page-break-after:always}
.frame{position:absolute;inset:8mm;border:2.5px solid #0f6b52;border-radius:4px}
.frame:before{content:"";position:absolute;inset:4mm;border:1px solid #c9a227;border-radius:2px}
.corner{position:absolute;width:16mm;height:16mm;border:0 solid #c9a227}
.wm{position:absolute;inset:0;display:grid;place-items:center;opacity:.05}
.in{position:relative;height:100%;display:flex;flex-direction:column;padding:14mm 20mm;text-align:center}`;
}

function certSheet(course, person, L, ref, brk) {
  return `<div class="sheet${brk ? ' brk' : ''}"><div class="frame">
  <div class="corner" style="inset-inline-start:0;top:0;border-top-width:4px;border-inline-start-width:4px"></div>
  <div class="corner" style="inset-inline-end:0;top:0;border-top-width:4px;border-inline-end-width:4px"></div>
  <div class="corner" style="inset-inline-start:0;bottom:0;border-bottom-width:4px;border-inline-start-width:4px"></div>
  <div class="corner" style="inset-inline-end:0;bottom:0;border-bottom-width:4px;border-inline-end-width:4px"></div>
</div>
<div class="wm">${academySealSVG(420)}</div>
<div class="in">
  <div style="display:flex;align-items:center;justify-content:space-between;gap:16px">
    <img src="${clusterLogoSrc()}" alt="" style="height:20mm;width:auto;object-fit:contain"/>
    <div style="text-align:center;flex:1">
      <div style="font-size:10.5pt;font-weight:600;color:#5c7069">المملكة العربية السعودية · وزارة الصحة</div>
      <div style="font-size:13pt;font-weight:800;color:#0f6b52;margin-top:2px">تجمع الرياض الصحي الأول · صحة المجتمع</div>
      <div style="font-size:10.5pt;font-weight:600;color:#5c7069;margin-top:2px">إدارة تجربة الموظف · أكاديمية المبتكرين</div>
    </div>
    <div style="width:20mm"></div>
  </div>

  <div style="margin-top:6mm">
    <div style="font-size:30pt;font-weight:800;letter-spacing:.5px">شهادة حضور</div>
    <div style="font-size:11pt;font-weight:600;color:#c9a227;letter-spacing:2px;margin-top:1mm">CERTIFICATE OF ATTENDANCE</div>
  </div>

  <div style="font-size:12.5pt;color:#5c7069;margin-top:6mm">تشهد أكاديمية المبتكرين — صحة المجتمع للابتكار بأن</div>
  <div style="margin-top:3mm"><span style="display:inline-block;white-space:nowrap;font-size:26pt;font-weight:800;color:#0f6b52;border-bottom:1.5px solid #c9a227;padding:0 14mm 2mm">${esc(person.name || '—')}</span></div>
  <div style="font-size:12.5pt;color:#5c7069;margin-top:5mm">قد حضر وأكمل بنجاح</div>
  <div style="font-size:17pt;font-weight:800;margin-top:2mm">${esc(L(course.title))}</div>

  <div style="display:flex;justify-content:center;gap:8mm;margin-top:5mm;flex-wrap:wrap">
    ${[['التاريخ', L(course.date)], ['المدة', L(course.dur) || '—'], ['المقدّم', L(course.presenter)], ['النمط', course.mode === 'virtual' ? 'عن بُعد' : 'حضوري']].filter(x => x[1]).map(x => `<div style="min-width:44mm;border:1px solid #dfe9e5;border-radius:6px;padding:3mm 5mm;background:#f8fbfa"><div style="font-size:9.5pt;color:#5c7069">${esc(x[0])}</div><div style="font-size:12pt;font-weight:700">${esc(x[1])}</div></div>`).join('')}
  </div>

  <div style="margin-top:auto;display:flex;align-items:flex-end;justify-content:space-between;gap:10mm">
    <div style="flex:1;text-align:center"><div style="border-bottom:1px solid #12211d;height:12mm"></div><div style="font-size:11pt;font-weight:700;margin-top:2mm">خلود دغريري</div><div style="font-size:9.5pt;color:#5c7069">قائدة الابتكار — صحة المجتمع</div></div>
    <div style="flex:none;text-align:center">${academySealSVG(112)}</div>
    <div style="flex:1;text-align:center"><div style="border-bottom:1px solid #12211d;height:12mm"></div><div style="font-size:11pt;font-weight:700;margin-top:2mm">د. عمرو كامل</div><div style="font-size:9.5pt;color:#5c7069">سفير الابتكار — أكاديمية المبتكرين</div></div>
  </div>
  <div style="display:flex;justify-content:space-between;font-size:9.5pt;color:#5c7069;margin-top:4mm;border-top:1px solid #dfe9e5;padding-top:2mm">
    <span dir="ltr">رقم الشهادة: ${esc(ref)}</span>
    <span>تاريخ الإصدار: ${esc(arDate(person.attendedAt || Date.now()))}</span>
    <span>هذه الشهادة صادرة إلكترونياً من منصة ابتكار · صحة المجتمع</span>
  </div>
</div></div>`;
}

/* ---------- PoAS digital record (A4 portrait) ---------- */
function printPoasRecord(rec, qrDataUrl) {
  const m = rec.metrics || {};
  const rows = [
    ['عنوان المفهوم / الاختراع', rec.title],
    ['التصنيف', rec.classification],
    ['الجدة البنيوية (ΔP)', Number(m.deltaP).toFixed(1)],
    ['سرعة الانتشار والتبنّي (Φnet)', Number(m.phiNet).toFixed(1)],
    ['مقاومة النظام والتكلفة (Ξfric)', Number(m.xiFric).toFixed(1)],
    ['أُس التوسّع الحجمي (Ωscale)', '10^' + m.omegaScale],
    ['رقم المرجع', rec.ref],
    ['تاريخ التوثيق', arDateTime(rec.submittedAt)],
  ];
  const body = `${docHeader('وثيقة توثيق فكرة · PoAS', 'بروتوكول بصمة التوليف التكويني · Proof of Axiomatic Synthesis')}
<div style="display:flex;gap:16px;margin-top:18px;align-items:stretch">
  <div style="flex:1;border:1px solid #c9d8d2;border-radius:10px;padding:16px 18px;background:#f6fbf9;text-align:center;display:flex;flex-direction:column;justify-content:center">
    <div class="mut" style="font-size:10.5pt">مؤشر التوليف الموحّد</div>
    <div style="font-size:44pt;font-weight:800;color:#0f6b52;line-height:1.1">${esc(rec.score)}</div>
    <div style="font-size:12.5pt;font-weight:700">${esc(rec.classification)}</div>
  </div>
  ${qrDataUrl ? `<div style="flex:none;border:1px solid #c9d8d2;border-radius:10px;padding:12px;text-align:center;background:#fff"><img src="${qrDataUrl}" alt="QR" style="width:38mm;height:38mm;display:block"/><div class="mut" style="font-size:9pt;margin-top:4px">امسح للتحقق</div></div>` : ''}
</div>
<table style="margin-top:16px"><tbody>${rows.filter(r => r[1] != null && r[1] !== '').map(r => `<tr><th style="width:36%">${esc(r[0])}</th><td>${esc(r[1])}</td></tr>`).join('')}</tbody></table>
<div style="margin-top:16px;border:1px dashed #8fa8a0;border-radius:8px;padding:12px 14px;background:#fafcfb">
  <div class="mut" style="font-size:10pt">بصمة الاندماج · Fusion Fingerprint</div>
  <div dir="ltr" style="font-family:ui-monospace,Menlo,monospace;font-size:11pt;word-break:break-all;margin-top:4px">${esc(rec.hash)}</div>
</div>
<p class="mut" style="font-size:10.5pt;line-height:1.7;margin-top:14px">هذه الوثيقة تُثبِت تاريخ ووقت توثيق الفكرة داخل منصة ابتكار — صحة المجتمع، ولا تغني عن إيداع براءة اختراع لدى الهيئة المختصة. للمسار الرسمي تواصل مع فريق الابتكار.</p>
${signBlock(['مقدّم الفكرة', 'قائدة الابتكار', 'سفير الابتكار'])}`;
  printDoc(body, '', 'وثيقة PoAS — ' + rec.ref);
}

/* ---------- digital-seal priority receipt: owner's copy (A4 portrait) ---------- */
function printSealCertificate(sub, seal, L) {
  const pick = (v) => (v == null ? '' : (typeof v === 'object' ? ((L ? L(v) : v.ar) || v.en || '') : v));
  const iso = new Date(seal.sealedAt).toISOString();
  const utc = iso.replace('T', ' ').slice(0, 19) + ' UTC';
  const owner = sub.inventors || sub.name || '—';
  const kindLbl = (seal.kind || sub.kind) === 'patent' ? 'إفصاح براءة اختراع' : 'فكرة ابتكارية';
  const verifyPayload = ['PoAS-v1', seal.ref, seal.fingerprint, seal.hash, iso, 'v' + seal.version.toFixed(1)].join('|');
  let qr = '';
  try { qr = window.QR ? window.QR.dataUrl(verifyPayload, { scale: 5, margin: 2 }) : ''; } catch (e) { qr = ''; }
  const rows = [
    ['رقم المرجع', seal.ref],
    ['نوع الأصل الفكري', kindLbl],
    ['عنوان الأصل الفكري', sub.title],
    ['مالك الأصل / المخترع', owner],
    ['المجال / التصنيف', pick(sub.field) || pick(sub.category) || pick(sub.dept)],
    ['إصدار الختم', 'v' + seal.version.toFixed(1) + (seal.prevHash ? ' · سابقتها ' + seal.prevHash.slice(0, 12) : '')],
    ['مؤشر PoAS الموحّد', seal.score + '  ·  INV ' + Number(seal.metrics.INV).toFixed(1) + '  ·  INN ' + Number(seal.metrics.INN).toFixed(1)],
  ];
  const codeBox = (label, value, big) => `<div style="border:1px dashed #8fa8a0;border-radius:8px;padding:11px 14px;background:#fafcfb;margin-top:8px">
  <div class="mut" style="font-size:9.5pt">${esc(label)}</div>
  <div dir="ltr" style="font-family:ui-monospace,Menlo,Consolas,monospace;font-size:${big ? '13pt' : '10.5pt'};font-weight:${big ? 700 : 500};color:${big ? '#0f6b52' : '#12211d'};word-break:break-all;letter-spacing:.3px;margin-top:4px">${esc(value)}</div>
</div>`;
  const body = `${docHeader('إشعار أسبقية · بصمة رقمية مختومة', 'نسخة مالك الأصل الفكري · Digital Seal — Owner\u2019s Copy')}
<div style="display:flex;gap:14px;margin-top:12px;align-items:stretch">
  <div style="flex:1">
    ${codeBox('بصمة الاندماج · Fusion fingerprint', seal.fingerprint, true)}
    ${codeBox('الهاش الكامل SHA-256 · Full hash', seal.hash)}
    ${codeBox('الختم الزمني الموثّق · Trusted timestamp (ISO 8601)', utc)}
  </div>
  ${qr ? `<div style="flex:none;width:44mm;border:1px solid #c9d8d2;border-radius:10px;padding:10px;text-align:center;background:#fff;display:flex;flex-direction:column;justify-content:center"><img src="${qr}" alt="QR" style="width:36mm;height:36mm;display:block;margin:0 auto"/><div class="mut" style="font-size:8.5pt;margin-top:5px;line-height:1.4">امسح للتحقّق من البصمة</div></div>` : ''}
</div>
<table style="margin-top:11px" class="tight"><tbody>${rows.filter(r => r[1] != null && r[1] !== '').map(r => `<tr><th style="width:36%">${esc(r[0])}</th><td>${esc(r[1])}</td></tr>`).join('')}</tbody></table>
<div style="margin-top:11px;border:1px solid #c9d8d2;border-radius:10px;padding:13px 16px;background:#f6fbf9">
  <div style="font-size:11.5pt;font-weight:800;color:#0f6b52">إقرار الأسبقية</div>
  <p class="mut" style="font-size:10.5pt;line-height:1.75;margin:6px 0 0">تشهد منصة صحة المجتمع للابتكار بأن مضمون الأصل الفكري المذكور أعلاه قد أُودع وخُتم إلكترونياً في التاريخ والوقت المبيّنين، وأن بصمة الاندماج والهاش الكامل مستخرجان من المضمون نفسه بخوارزمية SHA-256؛ فأي تعديل لاحق على المضمون يُنتج هاشاً مختلفاً ويُبطل مطابقة هذه النسخة. المضمون الأصلي محفوظ مشفّراً في «خزنة الابتكار» ولا يُفصح عنه إلا بموافقة المالك.</p>
  <p class="mut" style="font-size:10pt;line-height:1.7;margin:8px 0 0">هذه الوثيقة إثبات أسبقية وتاريخ داخلي، ولا تُعد تسجيلاً لبراءة اختراع ولا تغني عن الإيداع لدى الهيئة السعودية للملكية الفكرية (SAIP).</p>
</div>
<div style="margin-top:10px;border-top:1px solid #dfe9e5;padding-top:7px;display:flex;justify-content:space-between;font-size:9.5pt;color:#5c7069">
  <span>طُبعت هذه النسخة في ${esc(arDateTime(Date.now()))}</span>
  <span dir="ltr">PoAS-v1 · SHA-256 · ${esc(seal.ref)}</span>
</div>
${signBlock(['مالك الأصل الفكري', 'قائدة الابتكار', 'سفير الابتكار']).replace('margin-top:26px', 'margin-top:14px')}`;
  printDoc(body, '.tight td,.tight th{padding:5px 9px}', 'إشعار أسبقية — ' + (seal.ref || ''));
}

Object.assign(window, { printDoc, printRegistrations, printAttendance, printCertificate, printCertificates, printPoasRecord, printSealCertificate, certRefFor, academySealSVG, arDate, arDateTime, clusterLogoSrc });
