<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Report Card — {{ $student->full_name }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #111; margin: 24px; }
        .header { text-align: center; border-bottom: 3px solid #1e3a5f; padding-bottom: 12px; margin-bottom: 20px; }
        .header h1 { font-size: 20px; margin: 0; color: #1e3a5f; }
        .header p { margin: 4px 0 0; color: #555; }
        .meta { display: table; width: 100%; margin-bottom: 16px; }
        .meta-row { display: table-row; }
        .meta-cell { display: table-cell; padding: 4px 8px 4px 0; width: 50%; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
        th, td { border: 1px solid #ccc; padding: 6px 8px; text-align: left; }
        th { background: #1e3a5f; color: #fff; }
        .summary { background: #f3f4f6; padding: 12px; margin-bottom: 16px; }
        .remarks { margin-top: 16px; }
        .remarks h3 { font-size: 12px; margin-bottom: 6px; }
        .footer { margin-top: 24px; text-align: center; font-size: 9px; color: #888; }
    </style>
</head>
<body>
    <div class="header">
        <h1>{{ $schoolName }}</h1>
        <p>{{ $schoolTagline }} — Student Report Card</p>
    </div>

    <div class="meta">
        <div class="meta-row">
            <div class="meta-cell"><strong>Student:</strong> {{ $student->full_name }}</div>
            <div class="meta-cell"><strong>Admission No:</strong> {{ $student->admission_number }}</div>
        </div>
        <div class="meta-row">
            <div class="meta-cell"><strong>Class:</strong> {{ $schoolClass->name }}</div>
            <div class="meta-cell"><strong>Term:</strong> {{ $term->name }} ({{ $term->academicYear?->name }})</div>
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th>Subject</th>
                <th>Average %</th>
                <th>Grade</th>
                <th>Position</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($subjects as $row)
                <tr>
                    <td>{{ $row->subject?->name }}</td>
                    <td>{{ $row->average_score !== null ? number_format($row->average_score, 1) : '—' }}</td>
                    <td>{{ $row->grade_letter ?? '—' }}</td>
                    <td>{{ $row->class_position ?? '—' }}</td>
                </tr>
            @empty
                <tr><td colspan="4">No approved subject results.</td></tr>
            @endforelse
        </tbody>
    </table>

    <div class="summary">
        <strong>Overall Average:</strong> {{ $reportCard->overall_average !== null ? number_format($reportCard->overall_average, 1).'%' : '—' }}
        &nbsp;|&nbsp;
        <strong>Class Position:</strong> {{ $reportCard->overall_position ?? '—' }}
    </div>

    <div class="remarks">
        @if ($reportCard->class_teacher_remarks)
            <h3>Class Teacher Remarks</h3>
            <p>{{ $reportCard->class_teacher_remarks }}</p>
        @endif
        @if ($reportCard->head_teacher_remarks)
            <h3>Head Teacher Remarks</h3>
            <p>{{ $reportCard->head_teacher_remarks }}</p>
        @endif
    </div>

    <div class="footer">
        Generated {{ now()->format('M j, Y') }} — {{ $reportCard->status === 'published' ? 'Published' : 'Draft' }}
    </div>
</body>
</html>
