/* ================================
   CONCOURS — STYLE GLOBAL
================================ */

.concours-wrap{
    margin-top:20px;
}

/* Header liste */
.concours-header-card h1{
    margin-top:0;
    font-size:26px;
}
.concours-header-card p{
    margin-bottom:0;
}

/* Grid liste */
.concours-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill,minmax(260px,1fr));
    gap:18px;
    margin-top:18px;
}
.concours-card{
    background:#111;
    border-radius:14px;
    padding:14px 16px;
    border:1px solid #2f2f2f;
}
body:not(.dark) .concours-card{
    background:#fffaf0;
    border-color:#e2d4a6;
}
.concours-card-header{
    display:flex;
    justify-content:space-between;
    gap:8px;
    align-items:center;
}
.concours-card-header h2{
    margin:0;
    font-size:18px;
}
.concours-status{
    padding:4px 9px;
    border-radius:999px;
    font-size:12px;
    font-weight:600;
    background:#333;
    color:#fff;
}
.status-running{background:#9EFD38;color:#000;}
.status-draft{background:#555;color:#fff;}
.status-finished{background:#ffc857;color:#000;}
.status-archived{background:#777;color:#fff;}

.concours-category{
    margin:8px 0;
    font-size:14px;
    opacity:.9;
}
.concours-reward,
.concours-dates,
.concours-stats-line{
    font-size:13px;
    margin:4px 0;
}
.concours-card-footer{
    margin-top:10px;
    display:flex;
    gap:8px;
    flex-wrap:wrap;
}

/* Boutons */
.btn.primary{
    background:#8FD83A;
    color:#000;
    border:none;
}
.btn.primary:hover{
    background:#a6ff4d;
}

/* ================================
   PAGE VIEW
================================ */

.concours-view-head{
    display:grid;
    grid-template-columns: minmax(0,2fr) minmax(0,1.3fr);
    gap:18px;
}
@media(max-width:900px){
    .concours-view-head{
        grid-template-columns:1fr;
    }
}
.concours-view-main h1{
    margin-top:0;
    font-size:26px;
}
.concours-view-category{
    font-size:15px;
    margin:4px 0 6px;
}
.concours-view-status{
    font-size:14px;
    margin:4px 0;
}
.concours-view-dates,
.concours-view-reward,
.concours-view-stats{
    font-size:14px;
    margin:3px 0;
}
.concours-view-rules{
    background:#111;
    border-radius:14px;
    padding:14px 16px;
    border:1px solid #333;
}
body:not(.dark) .concours-view-rules{
    background:#fffaf0;
    border-color:#e2d4a6;
}
.concours-view-rules h2{
    margin-top:0;
}

/* Participations grid */
.participations-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill,minmax(260px,1fr));
    gap:18px;
    margin-top:12px;
}
.participation-card{
    background:#111;
    border-radius:14px;
    border:1px solid #333;
    overflow:hidden;
    display:flex;
    flex-direction:column;
}
body:not(.dark) .participation-card{
    background:#fffaf0;
    border-color:#e2d4a6;
}
.participation-media img,
.participation-media video{
    width:100%;
    height:180px;
    object-fit:cover;
    display:block;
}
.participation-placeholder{
    height:180px;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:13px;
    opacity:.7;
}
.participation-content{
    padding:10px 12px 12px;
}
.participation-content h3{
    margin:0 0 4px;
    font-size:16px;
}
.participation-author{
    font-size:13px;
    opacity:.85;
    margin:0 0 6px;
}
.participation-desc{
    font-size:13px;
    max-height:90px;
    overflow:auto;
}
.participation-votes{
    font-size:13px;
    font-weight:600;
    margin-top:6px;
}
.vote-form{
    margin-top:8px;
}

/* Pagination */
.pagination{
    margin-top:12px;
    display:flex;
    flex-wrap:wrap;
    gap:6px;
}
.pagination .page{
    padding:3px 9px;
    border-radius:999px;
    border:1px solid #555;
    font-size:13px;
    text-decoration:none;
}
.pagination .current{
    background:#8FD83A;
    color:#000;
    border-color:#8FD83A;
}

/* ================================
   TROPHIES
================================ */

.concours-trophies h2{
    margin-top:0;
}
.trophy-grid{
    display:flex;
    gap:14px;
    flex-wrap:wrap;
}
.trophy-card{
    flex:1 1 160px;
    min-width:160px;
    border-radius:16px;
    padding:10px 12px;
    text-align:center;
    border:1px solid #333;
    background:#111;
}
body:not(.dark) .trophy-card{
    background:#fffaf0;
    border-color:#e2d4a6;
}
.trophy-medal{
    font-size:26px;
}
.trophy-title{
    font-weight:600;
    margin-top:4px;
}
.trophy-votes{
    font-size:13px;
    margin-top:4px;
}
.trophy-first{
    box-shadow:0 0 12px rgba(255,223,0,.5);
}
.trophy-second{
    box-shadow:0 0 10px rgba(180,180,180,.5);
}
.trophy-third{
    box-shadow:0 0 10px rgba(205,127,50,.5);
}

/* ================================
   FORMULAIRES ADMIN / PARTICIPATION
================================ */

.admin-form-card{
    position:relative;
    padding-top:18px;
}
.admin-form-card::before{
    content:"Admin Concours";
    position:absolute;
    top:6px;
    right:18px;
    font-size:11px;
    text-transform:uppercase;
    letter-spacing:.08em;
    opacity:.6;
}
.admin-form-header h1{
    margin-top:0;
    font-size:24px;
}
.admin-form-header p{
    margin-bottom:14px;
    opacity:.9;
}

/* grille 2 colonnes */
.form-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(260px,1fr));
    gap:14px 20px;
}
.field{
    display:flex;
    flex-direction:column;
}
.field-full{
    grid-column:1/-1;
}
.field label{
    font-weight:600;
    margin-bottom:3px;
    font-size:14px;
}
.field input[type="text"],
.field input[type="datetime-local"],
.field textarea,
.field select{
    border-radius:8px;
    border:1px solid #444;
    padding:7px 9px;
    font-size:14px;
    background:#0d0d0d;
    color:#f5f5f5;
}
body:not(.dark) .field input[type="text"],
body:not(.dark) .field input[type="datetime-local"],
body:not(.dark) .field textarea,
body:not(.dark) .field select{
    background:#fff;
    color:#000;
    border-color:#ccc;
}
.field small{
    font-size:11px;
    opacity:.7;
}
.form-actions{
    grid-column:1/-1;
    margin-top:6px;
    display:flex;
    gap:10px;
    flex-wrap:wrap;
}

/* Admin stats */
.concours-admin-head h1{margin-top:0;}
.concours-admin-stats{
    display:flex;
    gap:10px;
    flex-wrap:wrap;
    margin:10px 0;
}
.stat-card{
    min-width:120px;
    padding:8px 10px;
    border-radius:10px;
    background:#111;
    border:1px solid #333;
}
body:not(.dark) .stat-card{
    background:#fffaf0;
    border-color:#e2d4a6;
}
.stat-card span{
    font-size:12px;
    opacity:.8;
}
.stat-card strong{
    font-size:18px;
}
.admin-actions{
    margin-top:8px;
}

<?php
// modules/fermes/admin/index.php
require_once __DIR__ . '/../../../inc/config.php';
require_once __DIR__ . '/../../../inc/functions.php';

if (!is_admin()) {
    redirect(BASE_URL);
}

$pdo = db();

$sql = "
    SELECT f.*,
           (SELECT COUNT(*) FROM fermes_animaux fa WHERE fa.ferme_id = f.id) AS nb_animaux,
           (SELECT COUNT(*) FROM fermes_vehicules fv WHERE fv.ferme_id = f.id) AS nb_vehicules,
           (SELECT COUNT(*) FROM fermes_demandes_materiel dm WHERE dm.ferme_id = f.id AND dm.status = 'pending') AS nb_demandes
    FROM fermes f
    ORDER BY f.created_at DESC
";
$stmt = $pdo->query($sql);
$fermes = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Admin - Fermes</title>
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>assets/css/style.css">
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>modules/fermes/assets/fermes.css">
    <style>
        .admin-table{width:100%;border-collapse:collapse;font-size:.9rem;}
        .admin-table th,.admin-table td{padding:8px;border-bottom:1px solid #e5e5e5;}
        .admin-table th{text-align:left;background:#f3f3f3;}
    </style>
</head>
<body>
<div class="fermes-wrapper">
    <div class="fermes-header">
        <div>
            <div class="fermes-title">Administration des fermes</div>
            <div class="fermes-badge">👑 Panel administrateur</div>
        </div>
        <a class="btn-ferme" href="<?php echo BASE_URL; ?>modules/fermes/admin/edit.php">
            + Nouvelle ferme
        </a>
    </div>

    <?php if (empty($fermes)): ?>
        <p>Aucune ferme enregistrée.</p>
    <?php else: ?>
        <table class="admin-table">
            <thead>
            <tr>
                <th>Nom</th>
                <th>Statut</th>
                <th>Animaux</th>
                <th>Véhicules</th>
                <th>Demandes en attente</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($fermes as $f): ?>
                <tr>
                    <td><?php echo htmlspecialchars($f['name']); ?></td>
                    <td><?php echo htmlspecialchars($f['status']); ?></td>
                    <td><?php echo (int)$f['nb_animaux']; ?></td>
                    <td><?php echo (int)$f['nb_vehicules']; ?></td>
                    <td><?php echo (int)$f['nb_demandes']; ?></td>
                    <td>
                        <a href="<?php echo BASE_URL; ?>modules/fermes/admin/edit.php?id=<?php echo (int)$f['id']; ?>">Éditer</a> |
                        <a href="<?php echo BASE_URL; ?>modules/fermes/admin/animaux.php?ferme_id=<?php echo (int)$f['id']; ?>">Animaux</a> |
                        <a href="<?php echo BASE_URL; ?>modules/fermes/admin/vehicules.php?ferme_id=<?php echo (int)$f['id']; ?>">Véhicules</a> |
                        <a href="<?php echo BASE_URL; ?>modules/fermes/admin/demandes.php?ferme_id=<?php echo (int)$f['id']; ?>">Demandes</a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
</body>
</html>
