<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<title>Book of the ERP - كتاب النظام</title>
<style>
@page {
    size: A4;
    margin: 20mm 15mm 25mm 15mm;
}
@page :first {
    margin: 0;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: 'Noto Sans Arabic', 'Noto Sans', 'Segoe UI', Tahoma, Arial, sans-serif;
    font-size: 13px;
    line-height: 1.7;
    color: #1A1A2E;
    direction: rtl;
}

/* Cover Page */
.cover-page {
    page-break-after: always;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1A1A2E 0%, #2D1B69 50%, #4C1D95 100%);
    color: #fff;
    text-align: center;
    padding: 40px;
}
.cover-logo {
    width: 120px;
    height: 120px;
    background: rgba(255,255,255,0.1);
    border-radius: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
    border: 2px solid rgba(255,255,255,0.2);
}
.cover-logo svg {
    width: 60px;
    height: 60px;
    fill: none;
    stroke: #fff;
    stroke-width: 1.5;
}
.cover-title {
    font-size: 48px;
    font-weight: 900;
    margin-bottom: 12px;
    letter-spacing: -1px;
}
.cover-subtitle {
    font-size: 22px;
    font-weight: 300;
    opacity: 0.85;
    margin-bottom: 8px;
}
.cover-desc {
    font-size: 16px;
    opacity: 0.6;
    margin-bottom: 60px;
}
.cover-meta {
    font-size: 12px;
    opacity: 0.5;
    border-top: 1px solid rgba(255,255,255,0.15);
    padding-top: 20px;
}

/* Table of Contents */
.toc-page {
    page-break-after: always;
    padding: 40px 0;
}
.toc-title {
    font-size: 28px;
    font-weight: 800;
    color: #1A1A2E;
    margin-bottom: 30px;
    padding-bottom: 12px;
    border-bottom: 3px solid #8B5CF6;
}
.toc-section {
    margin-bottom: 20px;
}
.toc-section-title {
    font-size: 16px;
    font-weight: 700;
    color: #4C1D95;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.toc-section-title::before {
    content: '';
    width: 8px;
    height: 8px;
    background: #8B5CF6;
    border-radius: 50%;
    flex-shrink: 0;
}
.toc-items {
    padding-right: 24px;
    list-style: none;
}
.toc-items li {
    font-size: 12px;
    color: #374151;
    padding: 2px 0;
    display: flex;
    align-items: center;
    gap: 6px;
}
.toc-items li::before {
    content: '\2022';
    color: #9CA3AF;
}
.toc-count {
    display: inline-block;
    background: #F3F4F6;
    color: #6B7280;
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 8px;
    margin-right: 8px;
}

/* Section Headers */
.section-header {
    page-break-before: always;
    page-break-after: avoid;
    padding: 60px 0 30px;
    text-align: center;
    border-bottom: 3px solid #8B5CF6;
    margin-bottom: 30px;
}
.section-header h2 {
    font-size: 30px;
    font-weight: 900;
    color: #1A1A2E;
    margin-bottom: 8px;
}
.section-header p {
    font-size: 14px;
    color: #6B7280;
}
.section-header .section-count {
    display: inline-block;
    background: #EDE9FE;
    color: #7C3AED;
    font-size: 12px;
    font-weight: 600;
    padding: 4px 12px;
    border-radius: 12px;
    margin-top: 12px;
}

/* Tutorial Blocks */
.tutorial-block {
    page-break-inside: avoid;
    margin-bottom: 30px;
    border: 1px solid #E5E7EB;
    border-radius: 12px;
    overflow: hidden;
}
.tutorial-block-header {
    background: #F9FAFB;
    padding: 16px 20px;
    border-bottom: 1px solid #E5E7EB;
    display: flex;
    align-items: center;
    gap: 12px;
}
.tutorial-block-header h3 {
    font-size: 16px;
    font-weight: 700;
    color: #1A1A2E;
    margin: 0;
}
.tutorial-block-header .tutorial-subtitle {
    font-size: 12px;
    color: #6B7280;
    margin: 2px 0 0;
}
.tutorial-block-header .tutorial-num {
    width: 28px;
    height: 28px;
    background: #8B5CF6;
    color: #fff;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
}
.tutorial-block-body {
    padding: 20px;
}

/* Screenshot in tutorial */
.tutorial-screenshot {
    width: 100%;
    max-height: 400px;
    object-fit: contain;
    border: 1px solid #E5E7EB;
    border-radius: 8px;
    margin-bottom: 16px;
}

/* Steps (for detailed tutorials) */
.tut-page { max-width: 100%; }
.tut-header {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 20px;
    padding: 16px;
    background: #F9FAFB;
    border-radius: 10px;
    border: 1px solid #E5E7EB;
}
.tut-header-icon {
    width: 44px;
    height: 44px;
    background: #8B5CF6;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.tut-header h1 {
    font-size: 18px;
    font-weight: 800;
    color: #1A1A2E;
    margin: 0 0 2px;
}
.tut-header p {
    font-size: 12px;
    color: #6B7280;
    margin: 0;
}
.tut-step {
    position: relative;
    padding: 14px 50px 14px 14px;
    margin-bottom: 10px;
    background: #fff;
    border: 1px solid #E5E7EB;
    border-radius: 8px;
    page-break-inside: avoid;
}
.tut-step-num {
    position: absolute;
    right: 12px;
    top: 14px;
    width: 28px;
    height: 28px;
    background: #EDE9FE;
    color: #7C3AED;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 800;
}
.tut-step-title {
    font-size: 13px;
    font-weight: 700;
    color: #1A1A2E;
    margin: 0 0 4px;
}
.tut-step-body {
    font-size: 12px;
    color: #374151;
    line-height: 1.7;
}
.tut-step-body ul { margin: 6px 0; padding-right: 16px; }
.tut-step-body li { margin-bottom: 3px; }
.tut-step-body .field {
    display: inline-block;
    background: #F3F4F6;
    color: #1A1A2E;
    padding: 0 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 600;
}
.tut-step-body .warn {
    display: block;
    background: #FEF3C7;
    border: 1px solid #F59E0B40;
    border-radius: 6px;
    padding: 8px 10px;
    margin: 6px 0;
    font-size: 11px;
    color: #92400E;
}
.tut-step-body .info {
    display: block;
    background: #DBEAFE;
    border: 1px solid #3B82F640;
    border-radius: 6px;
    padding: 8px 10px;
    margin: 6px 0;
    font-size: 11px;
    color: #1E40AF;
}
.tut-step-body .success {
    display: block;
    background: #ECFDF5;
    border: 1px solid #05966940;
    border-radius: 6px;
    padding: 8px 10px;
    margin: 6px 0;
    font-size: 11px;
    color: #065F46;
}
.tut-diagram {
    background: #F8FAFC;
    border: 1px solid #E2E8F0;
    border-radius: 8px;
    padding: 12px;
    margin: 8px 0;
    font-family: monospace;
    font-size: 10px;
    direction: ltr;
    text-align: left;
    line-height: 1.5;
    overflow: hidden;
}

/* Category Separator */
.category-header {
    margin: 24px 0 12px;
    padding: 8px 14px;
    background: #F3F4F6;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 700;
    color: #374151;
    page-break-after: avoid;
}

/* Footer */
.page-footer {
    position: fixed;
    bottom: 10mm;
    left: 15mm;
    right: 15mm;
    font-size: 9px;
    color: #9CA3AF;
    text-align: center;
    border-top: 1px solid #F3F4F6;
    padding-top: 6px;
}

/* Screenshot image within content */
.tutorial-block-body img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    border: 1px solid #E5E7EB;
    margin: 8px 0;
}

/* Print optimizations */
@media print {
    body { font-size: 11px; }
    .cover-page { height: 297mm; }
    .no-print { display: none; }
}
</style>
</head>
<body>

<!-- Cover Page -->
<div class="cover-page">
    <div class="cover-logo">
        <svg viewBox="0 0 24 24"><path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </div>
    <div class="cover-title">Book of the ERP</div>
    <div class="cover-subtitle">كتاب النظام الشامل</div>
    <div class="cover-desc">الدليل التفصيلي لجميع عمليات نظام إدارة النادي</div>
    <div class="cover-meta">
        تم التوليد في: <?= $data['generatedAt'] ?> &nbsp;|&nbsp;
        عدد الأقسام: <?= count($data['sections']) ?> &nbsp;|&nbsp;
        إجمالي الشروحات: <?php
            $totalTutorials = 0;
            foreach ($data['sections'] as $s) {
                $totalTutorials += count($s['tutorials']);
            }
            echo $totalTutorials;
        ?>
    </div>
</div>

<!-- Table of Contents -->
<div class="toc-page">
    <h1 class="toc-title">فهرس المحتويات</h1>
    <?php $sectionNum = 0; foreach ($data['sections'] as $sectionKey => $section): $sectionNum++; ?>
    <div class="toc-section">
        <div class="toc-section-title">
            <?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?>
            <span class="toc-count"><?= count($section['tutorials']) ?> شرح</span>
        </div>
        <ul class="toc-items">
            <?php foreach ($section['tutorials'] as $slug => $tutorial): ?>
            <li><?= htmlspecialchars($tutorial['title'], ENT_QUOTES, 'UTF-8') ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <?php endforeach; ?>
</div>

<!-- Sections & Tutorials -->
<?php $sectionNum = 0; foreach ($data['sections'] as $sectionKey => $section): $sectionNum++; ?>

<div class="section-header">
    <h2>القسم <?= $sectionNum ?>: <?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?></h2>
    <?php if (!empty($section['subtitle'])): ?>
    <p><?= htmlspecialchars($section['subtitle'], ENT_QUOTES, 'UTF-8') ?></p>
    <?php endif; ?>
    <div class="section-count"><?= count($section['tutorials']) ?> شرح في هذا القسم</div>
</div>

<?php if (!empty($section['screenshot']) && file_exists($section['screenshot'])): ?>
<div style="text-align:center;margin-bottom:24px;">
    <img src="file://<?= $section['screenshot'] ?>" class="tutorial-screenshot" alt="<?= htmlspecialchars($section['title'], ENT_QUOTES, 'UTF-8') ?>">
</div>
<?php endif; ?>

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

<div class="tutorial-block">
    <div class="tutorial-block-header">
        <div class="tutorial-num"><?= $tutorialNum ?></div>
        <div>
            <h3><?= htmlspecialchars($tutorial['title'], ENT_QUOTES, 'UTF-8') ?></h3>
            <?php if (!empty($tutorial['subtitle'])): ?>
            <div class="tutorial-subtitle"><?= htmlspecialchars($tutorial['subtitle'], ENT_QUOTES, 'UTF-8') ?></div>
            <?php endif; ?>
        </div>
    </div>
    <div class="tutorial-block-body">
        <?php if (!empty($tutorial['htmlContent'])): ?>
            <?= $tutorial['htmlContent'] ?>
        <?php else: ?>
            <p style="color:#6B7280;font-style:italic;font-size:12px;">
                <?= htmlspecialchars($tutorial['subtitle'] ?? $tutorial['title'], ENT_QUOTES, 'UTF-8') ?>
            </p>
        <?php endif; ?>
    </div>
</div>

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

</body>
</html>
