<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// ============================================================
//  AÉRYA PRO — login.php
// ============================================================
require_once __DIR__ . '/config.php';
session_start();

if (current_user()) { header('Location: index.php'); exit; }

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email    = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    
    $stmt = db()->prepare('SELECT * FROM users WHERE email = ? LIMIT 1');
    $stmt->execute([$email]);
    $user = $stmt->fetch();
    
    if ($user && password_verify($password, $user['password_hash'])) {
        $_SESSION['user_id'] = $user['id'];
        header('Location: index.php'); exit;
    } else {
        $error = 'Email ou mot de passe incorrect.';
    }
}
?><!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AÉRYA PRO — Connexion</title>
<link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,300;0,400&family=DM+Sans:wght@400;500;700&display=swap" rel="stylesheet">
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#0d0c0a;color:#ede8e0;font-family:'DM Sans',sans-serif;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:1.5rem}
.card{background:#1a1916;border:1px solid #2e2b26;border-radius:20px;padding:2rem 1.8rem;width:100%;max-width:400px}
.logo{font-family:'Cormorant Garamond',serif;font-size:1.8rem;font-weight:300;letter-spacing:.2em;color:#c9a96e;text-align:center;margin-bottom:.3rem}
.logo em{font-style:italic;color:#e8cc99}
.subtitle{text-align:center;font-size:.75rem;color:#7a7060;margin-bottom:1.8rem;letter-spacing:.1em}
.field{margin-bottom:.9rem}
label{display:block;font-size:.72rem;color:#7a7060;margin-bottom:.25rem;letter-spacing:.05em}
input{width:100%;background:#0d0c0a;border:1px solid #2e2b26;border-radius:10px;padding:.65rem .9rem;color:#ede8e0;font-size:.88rem;font-family:'DM Sans',sans-serif;outline:none}
input:focus{border-color:#c9a96e}
.btn{width:100%;padding:.8rem;background:linear-gradient(135deg,#c9a96e,#a07840);color:#0d0c0a;border:none;border-radius:10px;font-size:.9rem;font-weight:700;cursor:pointer;font-family:'DM Sans',sans-serif;margin-top:.5rem}
.error{background:rgba(192,57,43,.15);border:1px solid rgba(192,57,43,.3);border-radius:8px;padding:.6rem .9rem;font-size:.8rem;color:#e57373;margin-bottom:1rem}
.links{text-align:center;margin-top:1rem;font-size:.78rem;color:#7a7060}
.links a{color:#c9a96e;text-decoration:none}
</style>
</head>
<body>
<div class="card">
    <div class="logo">AÉRYA <em>pro</em></div>
    <div class="subtitle">DEVIS VOCAL POUR ARTISANS</div>
    
    <?php if ($error): ?>
    <div class="error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    
    <form method="POST">
        <div class="field">
            <label>EMAIL</label>
            <input type="email" name="email" required>
        </div>
        <div class="field">
            <label>MOT DE PASSE</label>
            <input type="password" name="password" required>
        </div>
        <button type="submit" class="btn">SE CONNECTER</button>
    </form>
    
    <div class="links">
        <a href="register.php">Pas encore inscrit ? Créer un compte</a>
    </div>
</div>
</body>
</html>