<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<title>Book of the ERP - كتاب النظام</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Noto Sans Arabic', 'Segoe UI', Tahoma, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.8;
    color: #1f2937;
    direction: rtl;
    background: #f1f5f9;
    padding: 24px;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

.container { max-width: 800px; margin: 0 auto; }

/* ── COVER ── */
.cover {
    padding: 80px 48px;
    background: linear-gradient(160deg, #0F0A2A, #1A1145 30%, #2D1B69 60%, #4C1D95);
    border-radius: 16px;
    color: #fff;
    text-align: center;
    position: relative;
    overflow: hidden;
    margin-bottom: 32px;
}
.cover::after {
    content: '';
    position: absolute;
    bottom: 0; left: 0; right: 0; height: 5px;
    background: linear-gradient(90deg, #8B5CF6, #EC4899, #F59E0B, #10B981, #3B82F6);
}
.cover-icon {
    width: 100px; height: 100px;
    margin: 0 auto 32px;
    background: rgba(255,255,255,0.08);
    border-radius: 28px;
    display: flex; align-items: center; justify-content: center;
    border: 2px solid rgba(255,255,255,0.12);
}
.cover-icon svg { width: 50px; height: 50px; fill: none; stroke: #fff; stroke-width: 1.5; }
.cover h1 { font-size: 44px; font-weight: 900; margin-bottom: 8px; }
.cover .sub { font-size: 20px; opacity: 0.85; margin-bottom: 12px; }
.cover .desc { font-size: 14px; opacity: 0.6; max-width: 420px; margin: 0 auto 40px; }
.cover-stats { display: flex; gap: 40px; justify-content: center; margin-bottom: 40px; }
.cover-stats span { display: block; }
.cover-stats .num { font-size: 32px; font-weight: 900; }
.cover-stats .lbl { font-size: 11px; opacity: 0.5; }
.cover .meta { font-size: 11px; opacity: 0.4; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 16px; }

/* ── TOC ── */
.toc {
    padding: 40px;
    background: #fff;
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    margin-bottom: 32px;
}
.toc h2 { font-size: 26px; font-weight: 900; text-align: center; margin-bottom: 8px; }
.toc .toc-sub { font-size: 13px; color: #6b7280; text-align: center; margin-bottom: 28px; }
.toc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.toc-entry { padding: 12px 16px; background: #f9fafb; border-radius: 10px; border-right: 4px solid var(--c); }
.toc-entry-head { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 700; color: #1a2332; }
.toc-num { width: 22px; height: 22px; border-radius: 6px; background: var(--c); color: #fff; font-size: 10px; font-weight: 800; display: inline-flex; align-items: center; justify-content: center; }
.toc-badge { margin-right: auto; background: #ede9fe; color: #7c3aed; font-size: 9px; font-weight: 700; padding: 2px 8px; border-radius: 10px; }
.toc-entry ul { list-style: none; margin: 6px 0 0; padding: 0; }
.toc-entry li { font-size: 11px; color: #6b7280; padding: 1px 0; }

/* ── SECTION DIVIDER ── */
.divider {
    margin: 40px 0 28px;
    padding: 56px 40px;
    background: #fafbff;
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    text-align: center;
    position: relative;
    overflow: hidden;
}
.divider::before {
    content: ''; position: absolute; top: 0; left: 0; right: 0; height: 5px; background: var(--c);
}
.divider-icon {
    width: 68px; height: 68px; margin: 0 auto 20px;
    background: var(--c); border-radius: 18px;
    display: flex; align-items: center; justify-content: center;
}
.divider-icon svg { width: 34px; height: 34px; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.divider h2 { font-size: 30px; font-weight: 900; color: #1a2332; margin-bottom: 8px; }
.divider .divider-sub { font-size: 14px; color: #6b7280; margin-bottom: 20px; }
.divider-stats { display: inline-flex; gap: 20px; background: #fff; padding: 12px 24px; border-radius: 10px; border: 1px solid #e5e7eb; }
.divider-stats .stat-num { display: block; font-size: 20px; font-weight: 900; color: var(--c); }
.divider-stats .stat-lbl { font-size: 10px; color: #9ca3af; font-weight: 600; }

/* ── CATEGORY ── */
.cat-head {
    margin: 24px 0 14px;
    padding: 10px 18px;
    background: #f9fafb;
    border-radius: 10px;
    border-right: 5px solid var(--c);
    font-size: 15px; font-weight: 800; color: #1a2332;
}

/* ── TUTORIAL ── */
.tut {
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    margin-bottom: 20px;
    overflow: visible;
}
.tut-head {
    padding: 14px 20px;
    background: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
    display: flex; align-items: center; gap: 12px;
}
.tut-num {
    width: 30px; height: 30px; background: var(--c); color: #fff;
    border-radius: 8px; display: flex; align-items: center; justify-content: center;
    font-size: 13px; font-weight: 800; flex-shrink: 0;
}
.tut-head h3 { font-size: 15px; font-weight: 700; color: #1a2332; margin: 0; }
.tut-head .tsub { font-size: 11px; color: #6b7280; margin: 2px 0 0; }
.tut-body { padding: 18px 22px; }

/* ── SCREENSHOT ── */
.tut-img {
    margin: 0 0 14px;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    overflow: hidden;
}
.tut-img img { width: 100%; height: auto; display: block; }

/* ── STEP ── */
.step {
    padding: 14px 18px;
    margin-bottom: 10px;
    background: #fafbfc;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    border-right: 4px solid var(--c);
    display: flex;
    gap: 12px;
    align-items: flex-start;
}
.step-num {
    width: 28px; height: 28px; flex-shrink: 0;
    background: #ede9fe; color: #7c3aed;
    border-radius: 8px; display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: 800;
}
.step-content { flex: 1; min-width: 0; }
.step-title { font-size: 13px; font-weight: 700; color: #1a2332; margin-bottom: 4px; }
.step-body { font-size: 12px; color: #374151; line-height: 1.9; }
.step-body ul { margin: 6px 0; padding-right: 16px; }
.step-body li { margin-bottom: 3px; }
.step-body .field {
    display: inline-block; background: #f3f4f6; color: #1a2332;
    padding: 1px 7px; border-radius: 4px; font-size: 11px; font-weight: 600; border: 1px solid #e5e7eb;
}
.step-body .warn {
    display: block; background: #fffbeb; border: 1px solid #fde68a; border-right: 4px solid #f59e0b;
    border-radius: 8px; padding: 10px 14px; margin: 8px 0; font-size: 11px; color: #92400e;
}
.step-body .info {
    display: block; background: #eff6ff; border: 1px solid #bfdbfe; border-right: 4px solid #3b82f6;
    border-radius: 8px; padding: 10px 14px; margin: 8px 0; font-size: 11px; color: #1e40af;
}
.step-body .success {
    display: block; background: #ecfdf5; border: 1px solid #a7f3d0; border-right: 4px solid #10b981;
    border-radius: 8px; padding: 10px 14px; margin: 8px 0; font-size: 11px; color: #065f46;
}
.step-body .tip {
    display: block; background: #f5f3ff; border: 1px solid #ddd6fe; border-right: 4px solid #8b5cf6;
    border-radius: 8px; padding: 10px 14px; margin: 8px 0; font-size: 11px; color: #5b21b6;
}
.step-body .tut-diagram {
    background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px;
    padding: 12px 14px; margin: 8px 0; font-family: monospace; font-size: 10px;
    direction: ltr; text-align: left; line-height: 1.5; white-space: pre-wrap;
}

/* ── SECTION SCREENSHOT ── */
.sec-img { margin: 0 0 24px; text-align: center; }
.sec-img img { max-width: 100%; border-radius: 12px; border: 1px solid #e2e8f0; }

/* ── BACK ── */
.back {
    margin-top: 40px;
    padding: 60px;
    background: linear-gradient(160deg, #0F0A2A, #1A1145, #2D1B69);
    border-radius: 16px;
    color: #fff;
    text-align: center;
    position: relative;
    overflow: hidden;
}
.back::after {
    content: ''; position: absolute; bottom: 0; left: 0; right: 0; height: 5px;
    background: linear-gradient(90deg, #8B5CF6, #EC4899, #F59E0B, #10B981, #3B82F6);
}
.back-icon {
    width: 60px; height: 60px; margin: 0 auto 16px;
    background: rgba(255,255,255,0.08); border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
    border: 1px solid rgba(255,255,255,0.1);
}
.back-icon svg { width: 30px; height: 30px; fill: none; stroke: #fff; stroke-width: 1.5; }
.back h2 { font-size: 20px; font-weight: 800; margin-bottom: 6px; opacity: 0.9; }
.back p { font-size: 12px; opacity: 0.5; }

/* ── PRINT ── */
@media print {
    @page { size: A4; margin: 20mm 16mm 22mm 16mm; }
    body { background: #fff; padding: 0; font-size: 11px; }
    .container { max-width: 100%; }
    .no-print { display: none !important; }
    .cover { border-radius: 0; min-height: 90vh; margin-bottom: 0; page-break-after: always; }
    .toc { page-break-after: always; margin-bottom: 0; border: none; border-radius: 0; }
    .divider { page-break-before: always; page-break-after: always; min-height: 70vh; margin: 0; border: none; border-radius: 0; }
    .tut { page-break-inside: avoid; border-radius: 0; }
    .step { page-break-inside: avoid; }
    .back { page-break-before: always; min-height: 70vh; margin: 0; border-radius: 0; }
}
</style>
</head>
<body>
<div class="container">

<?php
$colors = ['#8B5CF6','#3B82F6','#10B981','#F59E0B','#EF4444','#06B6D4','#EC4899','#14B8A6','#F97316','#6366F1','#84CC16','#0EA5E9','#D946EF','#22C55E','#E11D48','#0891B2','#7C3AED','#059669','#DC2626','#2563EB','#8B5CF6','#3B82F6','#10B981','#F59E0B','#EF4444','#06B6D4','#EC4899','#14B8A6','#F97316','#6366F1'];
$totalTutorials = 0;
$totalSections = count($data['sections']);
foreach ($data['sections'] as $s) { $totalTutorials += count($s['tutorials']); }
$icons = [
    'membership' => '<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>',
    'sports-activity' => '<circle cx="12" cy="12" r="10"/><path d="M12 8v4l3 3"/>',
    'treasury' => '<rect x="2" y="4" width="20" height="16" rx="2"/><path d="M12 8v8"/><path d="M8 12h8"/>',
    'default' => '<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/>',
];
?>

<!-- COVER -->
<div class="cover">
    <div class="cover-icon">
        <svg viewBox="0 0 24 24"><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z" stroke-linecap="round" stroke-linejoin="round"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </div>
    <h1>Book of the ERP</h1>
    <div class="sub">كتاب النظام الشامل — الإصدار الأول</div>
    <div class="desc">الدليل التدريبي التفصيلي لجميع عمليات وأقسام نظام إدارة النادي</div>
    <div class="cover-stats">
        <div><span class="num"><?= $totalSections ?></span><span class="lbl">قسم</span></div>
        <div><span class="num"><?= $totalTutorials ?></span><span class="lbl">شرح</span></div>
    </div>
    <div class="meta">تم التوليد تلقائياً — <?= $data['generatedAt'] ?></div>
</div>

<!-- TOC -->
<div class="toc">
    <h2>فهرس المحتويات</h2>
    <div class="toc-sub"><?= $totalSections ?> قسم — <?= $totalTutorials ?> شرح</div>
    <div class="toc-grid">
    <?php $sn = 0; foreach ($data['sections'] as $sk => $sec): $sn++; $c = $colors[$sn-1] ?? '#8B5CF6'; ?>
        <div class="toc-entry" style="--c:<?= $c ?>;">
            <div class="toc-entry-head">
                <span class="toc-num"><?= $sn ?></span>
                <?= htmlspecialchars($sec['title'], ENT_QUOTES, 'UTF-8') ?>
                <span class="toc-badge"><?= count($sec['tutorials']) ?></span>
            </div>
            <ul>
            <?php $ti = 0; foreach ($sec['tutorials'] as $t): $ti++; if ($ti > 4) break; ?>
                <li><?= htmlspecialchars($t['title'], ENT_QUOTES, 'UTF-8') ?></li>
            <?php endforeach; ?>
            <?php if (count($sec['tutorials']) > 4): ?>
                <li style="color:#9ca3af;font-style:italic;">+ <?= count($sec['tutorials']) - 4 ?> آخر</li>
            <?php endif; ?>
            </ul>
        </div>
    <?php endforeach; ?>
    </div>
</div>

<!-- SECTIONS -->
<?php $sectionNum = 0; foreach ($data['sections'] as $sectionKey => $section): $sectionNum++; $color = $colors[$sectionNum-1] ?? '#8B5CF6'; ?>

<div class="divider" style="--c:<?= $color ?>;">
    <div class="divider-icon">
        <svg viewBox="0 0 24 24"><?= $icons[$sectionKey] ?? $icons['default'] ?></svg>
    </div>
    <h2><?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?></h2>
    <?php if (!empty($section['subtitle'])): ?>
    <div class="divider-sub"><?= htmlspecialchars($section['subtitle'], ENT_QUOTES, 'UTF-8') ?></div>
    <?php endif; ?>
    <div class="divider-stats">
        <div><span class="stat-num"><?= count($section['tutorials']) ?></span><span class="stat-lbl">شرح</span></div>
        <div><span class="stat-num"><?= count($section['categories'] ?? []) ?></span><span class="stat-lbl">تصنيف</span></div>
    </div>
</div>

<?php if (!empty($section['screenshot']) && file_exists($section['screenshot'])): ?>
<?php $webPath = str_replace(realpath(__DIR__ . '/../../../../public'), '', $section['screenshot']); ?>
<div class="sec-img"><img src="<?= $webPath ?>" alt="<?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?>"></div>
<?php endif; ?>

<?php
$tutNum = 0;
$curCat = '';
foreach ($section['tutorials'] as $slug => $tutorial):
    $tutNum++;
    $cat = $tutorial['category'] ?? '';
    if ($cat !== $curCat && !empty($section['categories'][$cat])):
        $curCat = $cat;
?>
<div class="cat-head" style="--c:<?= $color ?>;"><?= htmlspecialchars($section['categories'][$cat]['label'], ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>

<div class="tut" style="--c:<?= $color ?>;">
    <div class="tut-head">
        <div class="tut-num"><?= $tutNum ?></div>
        <div>
            <h3><?= htmlspecialchars($tutorial['title'], ENT_QUOTES, 'UTF-8') ?></h3>
            <?php if (!empty($tutorial['subtitle'])): ?>
            <div class="tsub"><?= htmlspecialchars($tutorial['subtitle'], ENT_QUOTES, 'UTF-8') ?></div>
            <?php endif; ?>
        </div>
    </div>
    <div class="tut-body">
        <?php if (!empty($tutorial['images'])): ?>
            <?php foreach ($tutorial['images'] as $img): ?>
            <div class="tut-img"><img src="<?= htmlspecialchars($img, ENT_QUOTES, 'UTF-8') ?>" alt=""></div>
            <?php endforeach; ?>
        <?php endif; ?>

        <?php if (!empty($tutorial['steps'])): ?>
            <?php foreach ($tutorial['steps'] as $si => $step): ?>
            <div class="step" style="--c:<?= $color ?>;">
                <div class="step-num"><?= $si + 1 ?></div>
                <div class="step-content">
                    <?php if (!empty($step['title'])): ?>
                    <div class="step-title"><?= htmlspecialchars($step['title'], ENT_QUOTES, 'UTF-8') ?></div>
                    <?php endif; ?>
                    <div class="step-body"><?= $step['body'] ?? '' ?></div>
                </div>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p style="color:#6b7280;padding:4px 0;"><?= htmlspecialchars($tutorial['subtitle'] ?? $tutorial['title'], ENT_QUOTES, 'UTF-8') ?></p>
        <?php endif; ?>
    </div>
</div>

<?php endforeach; ?>
<?php endforeach; ?>

<!-- BACK -->
<div class="back">
    <div class="back-icon">
        <svg viewBox="0 0 24 24"><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z" stroke-linecap="round" stroke-linejoin="round"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </div>
    <h2>Book of the ERP</h2>
    <p>كتاب النظام الشامل — الإصدار الأول</p>
    <p style="margin-top:12px;opacity:0.3;font-size:10px;"><?= $data['generatedAt'] ?></p>
</div>

</div>
</body>
</html>
