@php
    $gradingCode = $pdfMeta['exam_code'] ?? ($paper->paper_id ?? 'unknown');
    $studentName = $pdfMeta['student_name'] ?? ($student['name'] ?? ($paper->student_id ?? '________'));
    $paperHeaderTitle = $pdfMeta['header_title'] ?? ($studentName . '｜' . $gradingCode . '｜未知类型');
    $generateDateTime = now()->format('Y年m月d日 H:i:s');
    $appendScanSheet = config('exam.pdf_grading_append_scan_sheet', false);
@endphp
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{{ $pdfMeta['grading_pdf_title'] ?? ($paper->paper_name ?? '答案详解预览') }}</title>
    <link rel="stylesheet" href="/css/katex/katex.min.css">
    <style>
        @page {
            size: A4;
            margin: 2.2cm 2cm 2.3cm 2cm;
            @top-left { content: "知了数学·{{ $generateDateTime }}"; font-size: 13px; color: #666; }
            @top-center { content: "{{ $studentName }}"; font-size: 13px; color: #666; }
            @top-right {
                content: "{{ $gradingCode }}";
                font-size: 19px;
                font-weight: 600;
                font-family: "Noto Sans", "Liberation Sans", "Nimbus Sans", sans-serif;
                color: #222;
            }
            @bottom-left { content: "{{ $paperHeaderTitle }}"; font-size: 11px; color: #666; }
            @bottom-right { content: counter(page) "/" counter(pages); font-size: 13px; color: #666; }
        }
        body {
            font-family: "Noto Serif", "Noto Serif CJK SC", "Noto Sans CJK SC", "Noto Sans", "STSongti-SC", "PingFang SC", "Songti SC", serif;
            line-height: 1.65;
            color: #000;
            background: #fff;
            font-size: 14px;
        }
        .page { max-width: 720px; margin: 0 auto; padding: 0 12px; }
        .header { text-align: center; margin-bottom: 1.1rem; border-bottom: 2px solid #000; padding-bottom: 0.8rem; }
        @include('pdf.partials.answer-detail-styles')
        @include('pdf.partials.grading-scan-sheet-styles')
    </style>
</head>
<body style="page-break-before: always;">
    <div class="page">
        <div class="header">
            <div style="font-size:22px;font-weight:bold;">答案详解</div>
            <div style="display:flex;justify-content:space-between;font-size:14px;margin-top:8px;">
                <span>老师：{{ $teacher['name'] ?? '________' }}</span>
                <span>年级：@formatGrade($student['grade'] ?? '________')</span>
                @if(!empty($pdfMeta['assemble_type_label']) && $pdfMeta['assemble_type_label'] !== '未知类型')
                    <span>类型：{{ $pdfMeta['assemble_type_label'] }}</span>
                @endif
                <span>姓名：{{ $student['name'] ?? '________' }}</span>
                <span>得分：________</span>
            </div>
        </div>

        @include('pdf.partials.answer-detail-page', ['questions' => $questions])
    </div>

    @if($appendScanSheet)
        @include('pdf.partials.grading-scan-sheet', [
            'questions' => $questions,
            'gradingCode' => $gradingCode,
            'teacher' => $teacher,
            'student' => $student,
            'pdfMeta' => $pdfMeta ?? [],
        ])
    @endif

    <script src="/js/katex.min.js"></script>
    <script src="/js/auto-render.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            try {
                renderMathInElement(document.body, {
                    delimiters: [
                        {left: '$$', right: '$$', display: true},
                        {left: '$', right: '$', display: false},
                        {left: '\\(', right: '\\)', display: false},
                        {left: '\\[', right: '\\]', display: true}
                    ],
                    throwOnError: false,
                    strict: false,
                    trust: true
                });
            } catch (e) {}
        });
    </script>
</body>
</html>
