@php
    $logoRel = $branch?->logo ? 'storage/'.ltrim($branch->logo, '/') : null;
    $logoAbs = $logoRel ? public_path($logoRel) : null;
    $hasLogo = $logoAbs && file_exists($logoAbs);
    $logoSrc = $hasLogo ? (($mode ?? 'web') === 'pdf' ? $logoAbs : asset($logoRel)) : null;
    $schoolName = $branch?->name ?? config('app.name');
    $schoolAddr = trim(($branch?->address ? $branch->address.', ' : '').($branch?->city ?? ''), ', ');
    $schoolPhone = $branch?->phone;
    $schoolEmail = $branch?->email;
@endphp
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admission Form</title>
<style>
    @page { margin: 12px 16px; }
    * { box-sizing: border-box; }
    body { font-family: "DejaVu Sans", Arial, sans-serif; font-size: 10.5px; color: #111; margin: 0; }
    .paper { padding: 4px 8px; }
    table { width: 100%; border-collapse: collapse; }
    td, th { vertical-align: top; padding: 3px 5px; }

    .hdr td { border: none; padding: 2px 4px; }
    .logo { width: 74px; }
    .logo img { width: 66px; height: 66px; object-fit: contain; }
    .school { text-align: center; }
    .school .nm { font-size: 19px; font-weight: bold; color: #16324F; letter-spacing: .3px; }
    .school .sub { font-size: 9px; color: #333; margin-top: 2px; }
    .photo { width: 86px; }
    .photo .box { width: 82px; height: 96px; border: 1px solid #444; text-align: center;
        font-size: 8px; color: #777; padding-top: 40px; }

    .title { background: #16324F; color: #fff; text-align: center; font-size: 13px;
        font-weight: bold; padding: 4px; letter-spacing: 2px; margin: 6px 0 0; }

    .meta td { border: 1px solid #999; font-size: 10px; }
    .meta .k { font-weight: bold; background: #f0f3f7; white-space: nowrap; }

    .sec { background: #e8edf3; border: 1px solid #99a; color: #16324F; font-weight: bold;
        padding: 3px 6px; font-size: 10.5px; margin-top: 7px; }

    .grid td { padding: 5px 5px 2px; }
    .lbl { font-size: 9px; color: #333; }
    .f { border-bottom: 1px dotted #333; height: 15px; }
    .cell { border-bottom: 1px dotted #333; }

    .chk { display: inline-block; width: 10px; height: 10px; border: 1px solid #333;
        margin: 0 3px 0 0; vertical-align: middle; }
    .opt { white-space: nowrap; margin-right: 10px; font-size: 10px; }
    .classgrid td { font-size: 10px; padding: 3px 5px; white-space: nowrap; }

    .lines td { border: 1px solid #aaa; height: 20px; font-size: 9px; }
    .lines .h { background: #f0f3f7; font-weight: bold; text-align: center; }

    .decl { font-size: 9.5px; margin-top: 7px; line-height: 1.4; }
    .sign td { padding-top: 22px; font-size: 9.5px; }
    .sign .u { border-top: 1px solid #333; padding-top: 2px; text-align: center; }

    .office { border: 1px solid #16324F; margin-top: 8px; }
    .office .oh { background: #16324F; color: #fff; font-weight: bold; font-size: 10px;
        padding: 3px 6px; letter-spacing: 1px; }
    .office td { border: 1px solid #bbb; font-size: 9.5px; }
    .office .k { background: #f0f3f7; font-weight: bold; white-space: nowrap; }

    .toolbar { background: #16324F; padding: 10px 14px; text-align: center; }
    .toolbar a, .toolbar button { display: inline-block; background: #fff; color: #16324F;
        border: none; border-radius: 6px; padding: 8px 16px; margin: 0 5px; font-size: 13px;
        font-weight: 600; text-decoration: none; cursor: pointer; }
    .toolbar a.pdf { background: #B3541E; color: #fff; }
    .toolbar a.word { background: #1C7A3E; color: #fff; }
    @media print { .toolbar { display: none; } .paper { padding: 0; } }
    @media screen { body { background: #eceff3; } .sheet { max-width: 800px; margin: 14px auto;
        background: #fff; box-shadow: 0 4px 18px rgba(0,0,0,.15); } }
</style>
</head>
<body>

@if(($mode ?? 'web') === 'web')
    <div class="toolbar">
        <button onclick="window.print()">🖨 Print</button>
        <a class="pdf" href="{{ route('admissions.blank-form', ['format' => 'pdf']) }}">⬇ Download PDF</a>
        <a class="word" href="{{ route('admissions.blank-form', ['format' => 'word']) }}">⬇ Download Word</a>
    </div>
@endif

<div class="sheet">
<div class="paper">

    {{-- ── Header ── --}}
    <table class="hdr">
        <tr>
            <td class="logo">
                @if($logoSrc)<img src="{{ $logoSrc }}" alt="">@endif
            </td>
            <td class="school">
                <div class="nm">{{ $schoolName }}</div>
                @if($schoolAddr)<div class="sub">{{ $schoolAddr }}</div>@endif
                <div class="sub">
                    @if($schoolPhone)Phone: {{ $schoolPhone }}@endif
                    @if($schoolEmail) &nbsp;•&nbsp; {{ $schoolEmail }}@endif
                </div>
            </td>
            <td class="photo">
                <div class="box">Affix recent<br>passport-size<br>photograph</div>
            </td>
        </tr>
    </table>

    <div class="title">STUDENT ADMISSION FORM</div>

    <table class="meta" style="margin-top:6px">
        <tr>
            <td class="k">Form No.</td><td style="width:22%">&nbsp;</td>
            <td class="k">Academic Session</td><td style="width:22%">&nbsp;</td>
            <td class="k">Date</td><td>&nbsp;</td>
        </tr>
    </table>

    {{-- ── A. Class applied for ── --}}
    <div class="sec">A. CLASS APPLIED FOR</div>
    @if($classes->isEmpty())
        <table class="grid"><tr><td><span class="lbl">Class: </span><div class="f"></div></td></tr></table>
    @else
        <table class="classgrid" style="margin-top:4px">
            @foreach($classes->chunk(6) as $row)
                <tr>
                    @foreach($row as $c)
                        <td style="width:16.6%"><span class="chk"></span>{{ $c->name }}</td>
                    @endforeach
                    @for($i = $row->count(); $i < 6; $i++)<td>&nbsp;</td>@endfor
                </tr>
            @endforeach
        </table>
    @endif

    {{-- ── B. Student information ── --}}
    <div class="sec">B. STUDENT INFORMATION</div>
    <table class="grid">
        <tr>
            <td style="width:50%"><div class="lbl">Full Name of Student</div><div class="f"></div></td>
            <td style="width:25%"><div class="lbl">Gender</div>
                <span class="opt"><span class="chk"></span>Male</span><span class="opt"><span class="chk"></span>Female</span></td>
            <td style="width:25%"><div class="lbl">Date of Birth</div><div class="f"></div></td>
        </tr>
        <tr>
            <td><div class="lbl">Place of Birth</div><div class="f"></div></td>
            <td><div class="lbl">B-Form / Birth Cert. No.</div><div class="f"></div></td>
            <td><div class="lbl">Blood Group</div><div class="f"></div></td>
        </tr>
        <tr>
            <td><div class="lbl">Religion</div><div class="f"></div></td>
            <td><div class="lbl">Nationality</div><div class="f"></div></td>
            <td><div class="lbl">Mother Tongue</div><div class="f"></div></td>
        </tr>
    </table>

    {{-- ── C. Previous school ── --}}
    <div class="sec">C. PREVIOUS SCHOOL (if any)</div>
    <table class="grid">
        <tr>
            <td style="width:50%"><div class="lbl">School Name</div><div class="f"></div></td>
            <td style="width:25%"><div class="lbl">Last Class Passed</div><div class="f"></div></td>
            <td style="width:25%"><div class="lbl">Year / Result</div><div class="f"></div></td>
        </tr>
    </table>

    {{-- ── D. Parents / Guardian ── --}}
    <div class="sec">D. PARENTS / GUARDIAN INFORMATION</div>
    <table class="grid">
        <tr>
            <td style="width:34%"><div class="lbl">Father's Name</div><div class="f"></div></td>
            <td style="width:33%"><div class="lbl">CNIC No.</div><div class="f"></div></td>
            <td style="width:33%"><div class="lbl">Occupation</div><div class="f"></div></td>
        </tr>
        <tr>
            <td><div class="lbl">Father's Mobile</div><div class="f"></div></td>
            <td><div class="lbl">Email</div><div class="f"></div></td>
            <td><div class="lbl">Monthly Income</div><div class="f"></div></td>
        </tr>
        <tr>
            <td><div class="lbl">Mother's Name</div><div class="f"></div></td>
            <td><div class="lbl">CNIC No.</div><div class="f"></div></td>
            <td><div class="lbl">Mother's Mobile</div><div class="f"></div></td>
        </tr>
        <tr>
            <td><div class="lbl">Guardian (if not parent)</div><div class="f"></div></td>
            <td><div class="lbl">Relation</div><div class="f"></div></td>
            <td><div class="lbl">Guardian Mobile</div><div class="f"></div></td>
        </tr>
    </table>

    {{-- ── E. Address ── --}}
    <div class="sec">E. ADDRESS &amp; CONTACT</div>
    <table class="grid">
        <tr><td colspan="2"><div class="lbl">Residential Address</div><div class="f"></div></td></tr>
        <tr>
            <td style="width:60%"><div class="lbl">Permanent Address</div><div class="f"></div></td>
            <td style="width:40%"><div class="lbl">Emergency Contact No.</div><div class="f"></div></td>
        </tr>
    </table>

    {{-- ── F. Siblings ── --}}
    <div class="sec">F. SIBLINGS STUDYING IN THIS SCHOOL</div>
    <table class="lines" style="margin-top:4px">
        <tr><td class="h" style="width:8%">#</td><td class="h" style="width:52%">Name</td>
            <td class="h" style="width:20%">Class</td><td class="h" style="width:20%">Section</td></tr>
        <tr><td style="text-align:center">1</td><td></td><td></td><td></td></tr>
        <tr><td style="text-align:center">2</td><td></td><td></td><td></td></tr>
    </table>

    {{-- ── G. Documents ── --}}
    <div class="sec">G. DOCUMENTS ATTACHED</div>
    <table class="grid"><tr><td>
        <span class="opt"><span class="chk"></span>B-Form / Birth Certificate</span>
        <span class="opt"><span class="chk"></span>2 Passport-size Photos</span>
        <span class="opt"><span class="chk"></span>Previous Result / Leaving Certificate</span>
        <span class="opt"><span class="chk"></span>Parent CNIC Copy</span>
        <span class="opt"><span class="chk"></span>Vaccination Record</span>
    </td></tr></table>

    {{-- ── Declaration ── --}}
    <div class="decl">
        <b>Declaration:</b> I hereby declare that the information provided above is true and correct to the
        best of my knowledge. I agree to abide by the rules and regulations of the school and to pay all
        dues regularly.
    </div>
    <table class="sign">
        <tr>
            <td style="width:50%"><div class="u">Signature of Parent / Guardian</div></td>
            <td style="width:25%">&nbsp;</td>
            <td style="width:25%"><div class="u">Date</div></td>
        </tr>
    </table>

    {{-- ── Office use ── --}}
    <table class="office">
        <tr><td class="oh" colspan="4">FOR OFFICE USE ONLY</td></tr>
        <tr>
            <td class="k" style="width:16%">Admission No.</td><td style="width:34%">&nbsp;</td>
            <td class="k" style="width:16%">Date</td><td>&nbsp;</td>
        </tr>
        <tr>
            <td class="k">Class / Section</td><td>&nbsp;</td>
            <td class="k">Admission Fee</td><td>&nbsp;</td>
        </tr>
        <tr>
            <td class="k">Test Result</td><td>&nbsp;</td>
            <td class="k">Remarks</td><td>&nbsp;</td>
        </tr>
        <tr>
            <td colspan="2" style="padding-top:24px"><div class="sign"><div class="u">Admission Officer</div></div></td>
            <td colspan="2" style="padding-top:24px"><div class="sign"><div class="u">Principal</div></div></td>
        </tr>
    </table>

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