<?php
require_once __DIR__ . '/auth.php';

header('X-Robots-Tag: noindex, nofollow');
header('Cache-Control: no-store');

if (isset($_GET['deconnexion'])) {
    douceur_deconnecter();
    header('Location: ' . douceur_chemin());
    exit;
}

$erreur = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $login = (string)($_POST['login'] ?? '');
    $mdp = (string)($_POST['mdp'] ?? '');
    if (douceur_verifier($login, $mdp)) {
        douceur_connecter();
        header('Location: ' . douceur_chemin());
        exit;
    }
    sleep(1); // freine les essais au hasard
    $erreur = true;
}

if (douceur_connectee()) {
    readfile(__DIR__ . '/app.html');
    exit;
}
?><!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="robots" content="noindex, nofollow">
<meta name="theme-color" content="#140B26">
<title>Une petite douceur</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>💙</text></svg>">
<style>
  :root{--bg1:#140B26;--bg2:#2A1240;--rose:#FF5C9A;--txt:#FBEFFF;--txt2:#CDB8E6}
  *{box-sizing:border-box;margin:0;padding:0}
  html,body{height:100%}
  body{
    min-height:100dvh;color:var(--txt);overflow:hidden;display:flex;align-items:center;justify-content:center;padding:24px;
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
    background:radial-gradient(120% 80% at 50% 0%,var(--bg2),var(--bg1) 70%);-webkit-tap-highlight-color:transparent;
  }
  .halo{position:fixed;border-radius:50%;filter:blur(60px);opacity:.35;pointer-events:none}
  .halo.a{width:60vmax;height:60vmax;background:#FF3F8E;left:-25vmax;top:-20vmax;animation:drift 18s ease-in-out infinite alternate}
  .halo.b{width:55vmax;height:55vmax;background:#3F6BFF;right:-25vmax;bottom:-20vmax;animation:drift 22s ease-in-out infinite alternate-reverse}
  @keyframes drift{to{transform:translate(8vmax,6vmax) scale(1.1)}}
  form{
    position:relative;z-index:1;width:min(100%,360px);display:flex;flex-direction:column;gap:14px;
    padding:30px 24px 26px;border-radius:26px;background:rgba(255,255,255,.07);
    box-shadow:0 0 0 1px rgba(255,255,255,.1),0 24px 60px rgba(0,0,0,.35);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);
  }
  .coeur{font-size:56px;text-align:center;animation:battre 1.2s ease-in-out infinite}
  @keyframes battre{0%,100%{transform:scale(1)}15%{transform:scale(1.15)}30%{transform:scale(1)}45%{transform:scale(1.1)}}
  h1{font-family:Georgia,serif;font-weight:normal;font-style:italic;font-size:26px;text-align:center;margin-bottom:6px}
  label{font-size:14px;color:var(--txt2);display:flex;flex-direction:column;gap:6px}
  input{
    font:inherit;font-size:17px;color:var(--txt);padding:14px 16px;border-radius:14px;border:1px solid rgba(255,255,255,.18);
    background:rgba(10,5,25,.45);outline:none;width:100%;
  }
  input:focus{border-color:var(--rose);box-shadow:0 0 0 3px rgba(255,92,154,.25)}
  .mdp{position:relative}
  .mdp input{padding-right:52px}
  .oeil{position:absolute;right:6px;bottom:6px;width:40px;height:40px;border:0;background:none;color:var(--txt2);font-size:20px;cursor:pointer}
  .erreur{color:#FFB3C8;font-size:15px;text-align:center}
  button[type=submit]{
    margin-top:6px;font:inherit;font-size:17px;font-weight:600;color:#fff;padding:16px;border:0;border-radius:999px;cursor:pointer;
    background:linear-gradient(135deg,var(--rose),#5B6BFF);box-shadow:0 8px 30px rgba(255,80,150,.4),inset 0 1px 0 rgba(255,255,255,.3);
  }
  button[type=submit]:active{transform:scale(.97)}
  .secoue{animation:secoue .4s}
  @keyframes secoue{20%,60%{transform:translateX(-8px)}40%,80%{transform:translateX(8px)}}
</style>
</head>
<body>
<div class="halo a"></div><div class="halo b"></div>
<form method="post" action="" class="<?= $erreur ? 'secoue' : '' ?>" autocomplete="on">
  <div class="coeur">💙</div>
  <label>Identifiant
    <input name="login" type="text" autocomplete="username" autocapitalize="none" autocorrect="off" spellcheck="false" required
           value="<?= htmlspecialchars((string)($_POST['login'] ?? ''), ENT_QUOTES) ?>">
  </label>
  <label class="mdp">Mot de passe
    <input name="mdp" id="mdp" type="password" autocomplete="current-password" required <?= $erreur ? 'autofocus' : '' ?>>
    <button type="button" class="oeil" aria-label="Afficher le mot de passe" onclick="var i=document.getElementById('mdp');i.type=i.type==='password'?'text':'password';this.textContent=i.type==='password'?'🙈':'🐵'">🙈</button>
  </label>
  <?php if ($erreur): ?><p class="erreur">Oups, ce n'est pas ça 💔</p><?php endif; ?>
  <button type="submit">Entrer</button>
</form>
</body>
</html>
