<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><?php if($team['color_primary']): ?><span style="display:inline-block;width:20px;height:20px;background:<?=e($team['color_primary'])?>;border-radius:50%;margin-right:8px"></span><?php endif; ?><?= e($team['name_en']) ?></h4>
    <div><?php if(auth_can('teams.manage')): ?><a href="/sports/teams/<?=$team['id']?>/edit" class="btn btn-warning btn-sm"><i class="fas fa-edit me-1"></i>Edit</a><?php endif; ?> <a href="/sports/teams" class="btn btn-secondary btn-sm">Back</a></div>
</div>
<div class="row g-3">
    <div class="col-md-4"><div class="card"><div class="card-body">
        <table class="table table-sm mb-0">
            <tr><th>Code</th><td><?=e($team['team_code'])?></td></tr>
            <tr><th>Sport</th><td><?=e($team['sport_name']??'')?></td></tr>
            <tr><th>Type</th><td><?=ucfirst($team['team_type'])?></td></tr>
            <tr><th>Coach</th><td><?=e($team['coach_name']??'N/A')?></td></tr>
            <tr><th>Season</th><td><?=e($team['season_name']??'N/A')?></td></tr>
            <tr><th>Players</th><td><?=$team['current_players']?>/<?=$team['max_players']?:'∞'?></td></tr>
            <tr><th>Record</th><td><span class="text-success"><?=$team['wins']?>W</span> <span class="text-danger"><?=$team['losses']?>L</span> <span class="text-muted"><?=$team['draws']?>D</span></td></tr>
            <tr><th>Points</th><td><strong><?=$team['points']?></strong></td></tr>
            <tr><th>Status</th><td><?=status_badge($team['status'])?></td></tr>
        </table>
    </div></div></div>
    <div class="col-md-8">
        <ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-roster">Roster (<?=count($members)?>)</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-matches">Recent Matches</a></li></ul>
        <div class="tab-content border border-top-0 p-3">
            <div class="tab-pane fade show active" id="tab-roster">
                <?php if(auth_can('teams.manage')): ?>
                <form method="POST" action="/sports/teams/<?=$team['id']?>/add-member" data-ajax="true" class="row g-2 mb-3">
                    <?=csrf_field()?><div class="col-md-3"><input type="number" name="member_id" class="form-control form-control-sm" placeholder="Member ID" required></div>
                    <div class="col-md-2"><input type="number" name="jersey_number" class="form-control form-control-sm" placeholder="Jersey #"></div>
                    <div class="col-md-3"><input type="text" name="position" class="form-control form-control-sm" placeholder="Position"></div>
                    <div class="col-md-2"><select name="role" class="form-select form-select-sm"><option value="player">Player</option><option value="captain">Captain</option><option value="substitute">Sub</option></select></div>
                    <div class="col-md-2"><button type="submit" class="btn btn-primary btn-sm w-100"><i class="fas fa-plus"></i></button></div>
                </form>
                <?php endif; ?>
                <table class="table table-sm table-hover">
                    <thead><tr><th>#</th><th>Player</th><th>Position</th><th>Role</th><th></th></tr></thead>
                    <tbody>
                    <?php foreach($members as $m): ?>
                    <tr><td><?=e($m['jersey_number']??'-')?></td><td><?=e($m['member_name'])?> <small class="text-muted"><?=e($m['membership_number'])?></small></td><td><?=e($m['position']??'')?></td><td><span class="badge bg-<?=$m['role']==='captain'?'warning':'secondary'?>"><?=ucfirst($m['role'])?></span></td>
                    <td><?php if(auth_can('teams.manage')): ?><form method="POST" action="/sports/teams/<?=$team['id']?>/remove-member" data-ajax="true" style="display:inline"><?=csrf_field()?><input type="hidden" name="team_member_id" value="<?=$m['id']?>"><button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-user-minus"></i></button></form><?php endif; ?></td></tr>
                    <?php endforeach; ?>
                    <?php if(empty($members)): ?><tr><td colspan="5" class="text-muted text-center">No players</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="tab-pane fade" id="tab-matches">
                <table class="table table-sm"><thead><tr><th>Date</th><th>Home</th><th>Score</th><th>Away</th><th>Status</th></tr></thead><tbody>
                <?php foreach($recentMatches as $rm): ?>
                <tr><td><?=format_date($rm['match_date'])?></td><td><?=e($rm['home_name']??'')?></td><td><?=$rm['status']==='completed'?$rm['home_score'].' - '.$rm['away_score']:'-'?></td><td><?=e($rm['away_name']??'')?></td><td><?=status_badge($rm['status'])?></td></tr>
                <?php endforeach; ?>
                <?php if(empty($recentMatches)): ?><tr><td colspan="5" class="text-muted text-center">No matches</td></tr><?php endif; ?>
                </tbody></table>
            </div>
        </div>
    </div>
</div>