<?php
session_start();
require_once '../config.php';

if (isset($_SESSION['cliente_id'])) {
    header('Location: painel.php');
    exit;
}

$erro = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $senha = trim($_POST['senha'] ?? '');

    if (empty($email) || empty($senha)) {
        $erro = 'Preencha e-mail e senha.';
    } else {
        $stmt = $pdo->prepare("SELECT * FROM clientes WHERE email = ? LIMIT 1");
        $stmt->execute([$email]);
        $cliente = $stmt->fetch();

        if ($cliente && password_verify($senha, $cliente['senha'])) {
            if ($cliente['status'] !== 'ativo') {
                $erro = 'Seu acesso está bloqueado. Entre em contato com o suporte.';
            } else {
                $_SESSION['cliente_id'] = $cliente['id'];
                $_SESSION['cliente_nome'] = $cliente['nome'];
                $_SESSION['cliente_email'] = $cliente['email'];

                header('Location: painel.php');
                exit;
            }
        } else {
            $erro = 'E-mail ou senha inválidos.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Área do Cliente - PromoCreative LED Mapper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{
    box-sizing:border-box;
}

body{
    margin:0;
    min-height:100vh;
    font-family:Arial,Helvetica,sans-serif;
    color:#fff;
    background:
        radial-gradient(circle at 15% 15%, rgba(0,170,255,.28), transparent 28%),
        radial-gradient(circle at 85% 80%, rgba(255,122,0,.22), transparent 30%),
        linear-gradient(135deg,#050914,#081426 55%,#050914);
    display:flex;
    align-items:center;
    justify-content:center;
    padding:24px;
    overflow:hidden;
}

body::before{
    content:"";
    position:absolute;
    inset:0;
    background:
        linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px);
    background-size:42px 42px;
    mask-image:linear-gradient(to bottom, rgba(0,0,0,.9), transparent);
    pointer-events:none;
}

.login-wrapper{
    width:100%;
    max-width:1080px;
    display:grid;
    grid-template-columns:1.1fr .9fr;
    background:rgba(8,15,29,.72);
    border:1px solid rgba(255,255,255,.10);
    border-radius:34px;
    overflow:hidden;
    box-shadow:0 40px 120px rgba(0,0,0,.55);
    backdrop-filter:blur(22px);
    position:relative;
    z-index:2;
}

.visual{
    padding:46px;
    background:
        radial-gradient(circle at top left, rgba(0,170,255,.28), transparent 38%),
        linear-gradient(145deg, rgba(255,255,255,.07), rgba(255,255,255,.015));
    border-right:1px solid rgba(255,255,255,.08);
    position:relative;
    min-height:620px;
    display:flex;
    flex-direction:column;
    justify-content:space-between;
}

.logo-area img{
    max-width:190px;
    max-height:80px;
    object-fit:contain;
    display:block;
    margin-bottom:22px;
}

.logo-fallback{
    width:68px;
    height:68px;
    border-radius:22px;
    background:linear-gradient(135deg,#ff7a00,#00aaff);
    display:flex;
    align-items:center;
    justify-content:center;
    font-weight:900;
    font-size:22px;
    margin-bottom:22px;
}

.visual h1{
    margin:0;
    font-size:42px;
    line-height:1.08;
    letter-spacing:-1px;
}

.visual p{
    color:#aabbe0;
    font-size:16px;
    line-height:1.7;
    max-width:440px;
}

.features{
    display:grid;
    gap:14px;
    margin-top:28px;
}

.feature{
    background:rgba(255,255,255,.06);
    border:1px solid rgba(255,255,255,.08);
    border-radius:18px;
    padding:16px;
    color:#d7e4ff;
}

.feature span{
    display:block;
    color:#7f95bd;
    font-size:13px;
    margin-top:4px;
}

.form-area{
    padding:46px;
    display:flex;
    align-items:center;
    justify-content:center;
}

.form-box{
    width:100%;
    max-width:390px;
}

.form-box h2{
    margin:0;
    font-size:30px;
}

.form-box .subtitle{
    margin:10px 0 28px;
    color:#8fa3c7;
    line-height:1.6;
}

.erro{
    padding:14px;
    border-radius:16px;
    background:rgba(255,60,60,.12);
    border:1px solid rgba(255,60,60,.35);
    color:#ffc1c1;
    margin-bottom:18px;
    text-align:center;
    font-size:14px;
}

label{
    display:block;
    margin:16px 0 8px;
    color:#c8d6f0;
    font-size:14px;
}

.input-group{
    position:relative;
}

.input-group input{
    width:100%;
    padding:16px 16px;
    border-radius:16px;
    border:1px solid rgba(255,255,255,.12);
    background:rgba(5,11,21,.88);
    color:#fff;
    outline:none;
    font-size:15px;
}

.input-group input:focus{
    border-color:#00aaff;
    box-shadow:0 0 0 4px rgba(0,170,255,.10);
}

button{
    width:100%;
    margin-top:24px;
    padding:17px;
    border:0;
    border-radius:18px;
    background:linear-gradient(135deg,#ff7a00,#00aaff);
    color:#fff;
    font-weight:800;
    font-size:15px;
    cursor:pointer;
    box-shadow:0 16px 36px rgba(0,170,255,.22);
}

button:hover{
    transform:translateY(-1px);
    opacity:.95;
}

.support{
    margin-top:22px;
    text-align:center;
    color:#7f95bd;
    font-size:13px;
}

.support a{
    color:#00d4ff;
    text-decoration:none;
    font-weight:bold;
}

.footer{
    margin-top:34px;
    color:#5f7198;
    font-size:12px;
    text-align:center;
}

.glow{
    position:absolute;
    width:240px;
    height:240px;
    border-radius:50%;
    background:rgba(0,170,255,.18);
    filter:blur(50px);
    right:-80px;
    bottom:-60px;
}

@media(max-width:900px){
    body{
        overflow:auto;
    }

    .login-wrapper{
        grid-template-columns:1fr;
    }

    .visual{
        min-height:auto;
        padding:32px;
        border-right:0;
        border-bottom:1px solid rgba(255,255,255,.08);
    }

    .visual h1{
        font-size:32px;
    }

    .form-area{
        padding:32px;
    }
}

@media(max-width:520px){
    body{
        padding:14px;
    }

    .visual,
    .form-area{
        padding:24px;
    }

    .visual h1{
        font-size:28px;
    }
}
</style>
</head>

<body>

<div class="login-wrapper">

    <div class="visual">
        <div>
            <div class="logo-area">
                <!-- COLOQUE SUA LOGO AQUI -->
                <!-- Exemplo: /licencas/assets/logo.png -->
                <?php if (file_exists('../assets/logo.png')): ?>
                    <img src="../assets/logo.png" alt="PromoCreative LED Mapper">
                <?php else: ?>
                    <div class="logo-fallback">PC</div>
                <?php endif; ?>
            </div>

            <h1>Gerencie sua licença com segurança.</h1>

            <p>
                Acesse sua conta para visualizar sua chave de ativação,
                validade, computadores autorizados e opções de renovação.
            </p>

            <div class="features">
                <div class="feature">
                    Licença por computador
                    <span>Cada máquina ativada fica registrada no sistema.</span>
                </div>

                <div class="feature">
                    Avisos de vencimento
                    <span>Veja quantos dias faltam para sua licença expirar.</span>
                </div>

                <div class="feature">
                    Renovação online
                    <span>Área preparada para pagamentos e atualizações futuras.</span>
                </div>
            </div>
        </div>

        <div class="footer">
            PromoCreative Studio © <?= date('Y'); ?>
        </div>

        <div class="glow"></div>
    </div>

    <div class="form-area">
        <div class="form-box">
            <h2>Área do Cliente</h2>
            <div class="subtitle">
                Entre com os dados enviados no momento da compra.
            </div>

            <?php if ($erro): ?>
                <div class="erro"><?= htmlspecialchars($erro); ?></div>
            <?php endif; ?>

            <form method="POST">
                <label>E-mail</label>
                <div class="input-group">
                    <input type="email" name="email" placeholder="seuemail@dominio.com" required>
                </div>

                <label>Senha</label>
                <div class="input-group">
                    <input type="password" name="senha" placeholder="Digite sua senha" required>
                </div>

                <button type="submit">Entrar no painel</button>
            </form>

            <div class="support">
                Precisa de ajuda?
                <a href="https://wa.me/5581999591897" target="_blank">Falar com suporte</a>
            </div>
        </div>
    </div>

</div>

</body>
</html>