<?php
use App\Core\Auth;
use App\Core\Config;
$title = $title ?? 'Authentification';
$base = rtrim(Config::baseUrl(), '/');
$brandName = Config::get('app_name', 'NOVALINK');

$publicRoot = Config::publicPath();
$resolveAssetUrl = static function (array $candidates, string $defaultPublicPath) use ($base, $publicRoot): string {
  foreach ($candidates as $candidate) {
    $candidate = trim((string)$candidate);
    if ($candidate === '') {
      continue;
    }

    if (!str_starts_with($candidate, '/')) {
      $candidate = '/storage/uploads/' . ltrim($candidate, '/');
    }

    $fsPath = $publicRoot . DIRECTORY_SEPARATOR . ltrim(str_replace('/', DIRECTORY_SEPARATOR, $candidate), DIRECTORY_SEPARATOR);
    if (is_file($fsPath)) {
      return $base . $candidate;
    }
  }

  return $base . $defaultPublicPath;
};

$bgUrl = $resolveAssetUrl([
  Config::get('branding_login_bg', ''),
  Config::get('mobile_login_bg', ''),
  '/storage/uploads/login-bg.jpg',
], '/assets/login-bg-default.svg');

$logoUrl = $resolveAssetUrl([
  Config::get('branding_logo', ''),
  Config::get('mobile_logo', ''),
  '/storage/uploads/logo.png',
], '/assets/logo-default.svg');

$favicon = $resolveAssetUrl([
  Config::get('branding_favicon', ''),
  Config::get('mobile_icon', ''),
  '/storage/uploads/favicon.ico',
], '/assets/favicon.svg');
?>
<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= htmlspecialchars($title) ?></title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="<?= htmlspecialchars(base_url('/assets/vendor/adminlte/adminlte.min.css')) ?>">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  <link rel="icon" href="<?= htmlspecialchars($favicon) ?>">

  <style>
    :root {
      --auth-accent: #0f766e;
      --auth-accent-dark: #0b5c56;
      --auth-panel-bg: rgba(255,255,255,0.90);
      --auth-input-border: #dce5ee;
      --auth-input-focus: #0f766e;
      --auth-text: #102033;
      --auth-muted: #5f7288;
      --auth-radius: 1rem;
      --auth-border-soft: rgba(148, 163, 184, 0.18);
      --auth-shadow-lg: 0 28px 80px rgba(15, 23, 42, 0.20);
    }

    *, *::before, *::after { box-sizing: border-box; }

    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
      font-family: "Manrope", "Segoe UI", sans-serif;
      overflow: hidden;
    }

    body {
      display: flex;
      min-height: 100vh;
      min-height: 100dvh;
      width: 100%;
      background:
        radial-gradient(circle at top left, rgba(16, 185, 129, 0.18), transparent 24%),
        radial-gradient(circle at bottom right, rgba(14, 116, 144, 0.18), transparent 28%),
        linear-gradient(135deg, #07111f 0%, #0b1829 44%, #13253d 100%);
      overflow: hidden;
    }

    .auth-panel-left {
      display: none;
      flex: 1;
      min-height: 100vh;
      min-height: 100dvh;
      position: relative;
      overflow: hidden;
    }

    @media (min-width: 992px) {
      .auth-panel-left {
        display: flex;
        align-items: flex-end;
      }
    }

    .auth-panel-left__bg {
      position: absolute;
      inset: 0;
      background: url('<?= htmlspecialchars($bgUrl) ?>') center/cover no-repeat;
      transform: scale(1.03);
    }

    .auth-panel-left__bg::after {
      content: '';
      position: absolute;
      inset: 0;
      background: linear-gradient(160deg, rgba(7, 17, 31, 0.44) 0%, rgba(7, 17, 31, 0.82) 100%);
    }

    .auth-panel-left::before,
    .auth-panel-left::after {
      content: '';
      position: absolute;
      border-radius: 999px;
      filter: blur(8px);
      z-index: 0;
    }

    .auth-panel-left::before {
      width: 220px;
      height: 220px;
      right: 12%;
      top: 12%;
      background: rgba(16, 185, 129, 0.18);
    }

    .auth-panel-left::after {
      width: 160px;
      height: 160px;
      left: 10%;
      bottom: 18%;
      background: rgba(59, 130, 246, 0.16);
    }

    .auth-panel-left__content {
      position: relative;
      z-index: 1;
      padding: 3rem;
      color: #fff;
      max-width: 620px;
    }

    .auth-panel-left__logo {
      display: block;
      position: absolute;
      top: 2rem;
      left: 2.5rem;
      z-index: 2;
      padding: .85rem 1rem;
      border-radius: 1.35rem;
      background: rgba(255,255,255,0.94);
      border: 1px solid rgba(255,255,255,0.62);
      box-shadow: 0 18px 40px rgba(15, 23, 42, 0.18);
      backdrop-filter: blur(12px);
    }

    .auth-panel-left__logo img {
      display: block;
      width: auto;
      height: auto;
      max-width: min(220px, 24vw);
      max-height: 88px;
      object-fit: contain;
    }

    .auth-panel-left__tagline {
      font-size: clamp(2rem, 3.6vw, 3.45rem);
      font-weight: 800;
      line-height: 1.2;
      margin-bottom: 0.9rem;
      letter-spacing: -0.04em;
      max-width: 12ch;
    }

    .auth-panel-left__sub {
      font-size: 1rem;
      opacity: 0.78;
      max-width: 420px;
      line-height: 1.7;
    }

    .auth-panel-left__highlights {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 1rem;
      margin-top: 2rem;
      max-width: 520px;
    }

    .auth-panel-left__highlight {
      padding: 1rem 1.05rem;
      border-radius: 1.2rem;
      background: rgba(255,255,255,0.10);
      border: 1px solid rgba(255,255,255,0.14);
      backdrop-filter: blur(14px);
    }

    .auth-panel-left__highlight strong {
      display: block;
      font-size: .95rem;
      margin-bottom: .3rem;
    }

    .auth-panel-left__highlight span {
      font-size: .84rem;
      opacity: .8;
      line-height: 1.55;
    }

    .auth-panel-left__dots {
      display: flex;
      gap: 6px;
      margin-top: 1.6rem;
    }

    .auth-panel-left__dots span {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: rgba(255,255,255,.4);
    }

    .auth-panel-left__dots span.active {
      background: #fff;
      width: 20px;
      border-radius: 3px;
    }

    .auth-panel-right {
      width: 100%;
      display: flex;
      align-items: flex-start;
      justify-content: center;
      min-height: 100vh;
      min-height: 100dvh;
      padding: 2rem 1.5rem;
      background: var(--auth-panel-bg);
      overflow-y: auto;
      backdrop-filter: blur(16px);
    }

    @media (min-width: 992px) {
      .auth-panel-right {
        width: 520px;
        flex-shrink: 0;
        border-left: 1px solid var(--auth-border-soft);
      }
    }

    .auth-form-wrap {
      width: 100%;
      max-width: 420px;
      margin-block: auto;
      animation: slideUp .5s cubic-bezier(.22,.68,0,1.2) both;
    }

    .auth-mobile-logo {
      display: flex;
      align-items: center;
      gap: .625rem;
      margin-bottom: 2rem;
      padding: .8rem .95rem;
      border-radius: 1.2rem;
      background: rgba(255,255,255,0.92);
      border: 1px solid rgba(203, 213, 225, 0.8);
      box-shadow: 0 16px 36px rgba(15, 23, 42, 0.08);
      width: fit-content;
      max-width: 100%;
    }

    @media (min-width: 992px) {
      .auth-mobile-logo { display: none; }
    }

    .auth-mobile-logo img {
      display: block;
      width: auto;
      height: auto;
      max-width: min(200px, 52vw);
      max-height: 64px;
      object-fit: contain;
    }

    .auth-mobile-logo__name {
      font-size: 1.16rem;
      font-weight: 800;
      color: var(--auth-text);
      letter-spacing: -.02em;
    }

    .auth-login-intro {
      margin-bottom: 1.4rem;
    }

    .auth-kicker {
      display: inline-flex;
      align-items: center;
      gap: .45rem;
      padding: .42rem .7rem;
      border-radius: 999px;
      background: rgba(15, 118, 110, 0.10);
      color: var(--auth-accent);
      font-size: .74rem;
      font-weight: 800;
      letter-spacing: .06em;
      text-transform: uppercase;
      margin-bottom: .9rem;
    }

    .auth-title {
      font-size: clamp(1.8rem, 2.4vw, 2.4rem);
      line-height: 1.06;
      font-weight: 800;
      letter-spacing: -.045em;
      color: var(--auth-text);
      margin: 0;
    }

    .auth-subtitle {
      margin: .7rem 0 0;
      color: var(--auth-muted);
      font-size: .96rem;
      line-height: 1.7;
    }

    .auth-quick-status {
      display: flex;
      flex-wrap: wrap;
      gap: .55rem;
      margin-top: 1rem;
    }

    .auth-quick-status__item {
      display: inline-flex;
      align-items: center;
      gap: .45rem;
      padding: .55rem .75rem;
      border-radius: 999px;
      background: #f4f8fb;
      border: 1px solid #e3ebf3;
      color: #37506a;
      font-size: .8rem;
      font-weight: 700;
    }

    .auth-form-card {
      margin-top: 1.25rem;
      padding: 1.2rem;
      border-radius: 1.4rem;
      background: rgba(255,255,255,0.82);
      border: 1px solid rgba(203, 213, 225, 0.7);
      box-shadow: 0 18px 40px rgba(15, 23, 42, 0.08);
    }

    .auth-form-card__head {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 1rem;
      margin-bottom: 1rem;
    }

    .auth-form-card__eyebrow {
      font-size: .74rem;
      text-transform: uppercase;
      letter-spacing: .08em;
      font-weight: 800;
      color: var(--auth-muted);
    }

    .auth-form-card__title {
      font-size: 1.1rem;
      font-weight: 800;
      letter-spacing: -.02em;
      color: var(--auth-text);
      margin-top: .1rem;
    }

    .auth-form-card__icon {
      width: 48px;
      height: 48px;
      border-radius: 16px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, rgba(15,118,110,0.14), rgba(14,116,144,0.14));
      color: var(--auth-accent);
      font-size: 1.1rem;
    }

    .auth-fieldset {
      display: grid;
      gap: .1rem;
    }

    .auth-inline-link {
      font-size: .8125rem;
      color: var(--auth-accent);
      font-weight: 700;
    }

    .auth-form-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .auth-form-row__meta {
      display: inline-flex;
      align-items: center;
      gap: .4rem;
      font-size: .8rem;
      color: var(--auth-muted);
      font-weight: 700;
    }

    .auth-submit-btn {
      min-height: 50px;
    }

    .auth-trust-list {
      display: grid;
      gap: .8rem;
    }

    .auth-trust-list__item {
      display: grid;
      grid-template-columns: auto 1fr;
      gap: .75rem;
      align-items: start;
      padding: .9rem 1rem;
      border-radius: 1rem;
      background: #f8fbfd;
      border: 1px solid #e5edf4;
    }

    .auth-trust-list__item i {
      width: 38px;
      height: 38px;
      border-radius: 12px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      background: rgba(15,118,110,0.1);
      color: var(--auth-accent);
      font-size: 1rem;
    }

    .auth-trust-list__item strong {
      display: block;
      color: var(--auth-text);
      font-size: .92rem;
      margin-bottom: .2rem;
    }

    .auth-trust-list__item span {
      display: block;
      color: var(--auth-muted);
      font-size: .82rem;
      line-height: 1.55;
    }

    .auth-panel-right .form-control,
    .auth-panel-right .form-select {
      border-color: var(--auth-input-border);
      border-radius: var(--auth-radius);
      padding: .72rem 1rem;
      font-size: .95rem;
      color: var(--auth-text);
      background: #f8fbfd;
      transition: border-color .2s, box-shadow .2s, background .2s;
    }

    .auth-panel-right .form-control:focus {
      border-color: var(--auth-input-focus);
      background: #fff;
      box-shadow: 0 0 0 3px rgba(15,118,110,.12);
    }

    .auth-panel-right .input-group-text {
      background: #f1f5f9;
      border-color: var(--auth-input-border);
      border-radius: var(--auth-radius) 0 0 var(--auth-radius);
      color: var(--auth-muted);
    }

    .auth-panel-right .input-group .form-control {
      border-radius: 0 var(--auth-radius) var(--auth-radius) 0;
    }

    .auth-panel-right .input-group .btn {
      border-color: var(--auth-input-border);
      border-radius: 0 var(--auth-radius) var(--auth-radius) 0;
      background: #f1f5f9;
      color: var(--auth-muted);
    }

    .auth-panel-right .input-group .btn:hover {
      background: #e2e8f0;
    }

    .auth-panel-right .btn-primary {
      background: var(--auth-accent);
      border-color: var(--auth-accent);
      border-radius: var(--auth-radius);
      padding: .78rem 1.5rem;
      font-weight: 700;
      font-size: .95rem;
      letter-spacing: .01em;
      transition: background .2s, transform .15s, box-shadow .2s;
      box-shadow: 0 10px 26px rgba(15,118,110,.26);
    }

    .auth-panel-right .btn-primary:hover {
      background: var(--auth-accent-dark);
      border-color: var(--auth-accent-dark);
      box-shadow: 0 14px 30px rgba(15,118,110,.32);
    }

    .auth-panel-right .btn-primary:active {
      transform: scale(.985);
    }

    .auth-panel-right .form-check-input:checked {
      background-color: var(--auth-accent);
      border-color: var(--auth-accent);
    }

    .auth-panel-right label.form-label {
      font-size: .875rem;
      font-weight: 700;
      color: var(--auth-text);
      margin-bottom: .45rem;
    }

    .auth-panel-right .alert {
      border: none;
      border-radius: var(--auth-radius);
      font-size: .875rem;
    }

    .auth-alert {
      box-shadow: 0 12px 28px rgba(239, 68, 68, 0.10);
    }

    .auth-divider {
      display: flex;
      align-items: center;
      gap: .75rem;
      margin: 1.4rem 0;
      color: #b3c0cf;
      font-size: .78rem;
      letter-spacing: .08em;
      text-transform: uppercase;
      font-weight: 800;
    }

    .auth-divider::before,
    .auth-divider::after {
      content: '';
      flex: 1;
      height: 1px;
      background: #e2e8f0;
    }

    @keyframes slideUp {
      from { opacity: 0; transform: translateY(20px); }
      to   { opacity: 1; transform: translateY(0); }
    }

    @media (max-width: 991.98px) {
      .auth-panel-right {
        background: linear-gradient(180deg, rgba(255,255,255,0.98) 0%, rgba(248,250,252,0.98) 100%);
      }

      .auth-form-card {
        padding: 1rem;
      }
    }

    @media (max-width: 575.98px) {
      .auth-panel-right {
        padding: 1rem .9rem;
      }

      .auth-title {
        font-size: 1.7rem;
      }

      .auth-login-intro {
        margin-bottom: 1rem;
      }

      .auth-form-card {
        margin-top: 1rem;
        padding: .95rem;
      }

      .auth-form-row {
        align-items: flex-start;
      }
    }

    /* Tablette paysage 10" (~800px hauteur) : compacter pour tenir sans scroll */
    @media (max-height: 860px) and (min-width: 992px) {
      .auth-panel-right {
        padding: 1rem 1.5rem;
      }

      .auth-login-intro {
        margin-bottom: 0.6rem;
      }

      .auth-quick-status {
        margin-top: 0.4rem;
        gap: 0.3rem;
      }

      .auth-quick-status__item {
        padding: .28rem .55rem;
        font-size: .73rem;
        min-height: auto;
      }

      .auth-kicker {
        margin-bottom: .5rem;
      }

      .auth-form-card {
        margin-top: 0.5rem;
        padding: .85rem;
      }

      .auth-form-card__head {
        margin-bottom: .55rem;
      }

      .auth-trust-list,
      .auth-divider {
        display: none;
      }

      .auth-submit-btn {
        min-height: 44px;
      }

      .auth-form-row {
        margin-bottom: .75rem !important;
      }
    }

    /* Très petit écran paysage (<680px hauteur) : masquer aussi le sous-titre */
    @media (max-height: 680px) and (min-width: 992px) {
      .auth-subtitle,
      .auth-quick-status {
        display: none;
      }

      .auth-login-intro {
        margin-bottom: 0.4rem;
      }

      .auth-panel-right {
        padding: .75rem 1.5rem;
      }
    }
  </style>
</head>
<body>
  <div class="auth-panel-left">
    <div class="auth-panel-left__bg"></div>
    <a href="<?= htmlspecialchars($base) ?>/dashboard" class="auth-panel-left__logo">
      <img src="<?= htmlspecialchars($logoUrl) ?>" alt="<?= htmlspecialchars($brandName) ?>" loading="eager" decoding="async" onerror="this.style.display='none'">
    </a>
    <div class="auth-panel-left__content">
      <p class="auth-panel-left__tagline"><?= htmlspecialchars($brandName) ?></p>
      <p class="auth-panel-left__sub">Plateforme centralisée de gestion des interventions, tickets et référentiels pour un pilotage plus fluide au quotidien.</p>
      <div class="auth-panel-left__highlights">
        <div class="auth-panel-left__highlight">
          <strong>Vision consolidée</strong>
          <span>Suivez incidents, opérations et indicateurs sans changer d’outil.</span>
        </div>
        <div class="auth-panel-left__highlight">
          <strong>Décision rapide</strong>
          <span>Repérez les points d’attention et reprenez les actions critiques immédiatement.</span>
        </div>
      </div>
      <div class="auth-panel-left__dots">
        <span class="active"></span>
        <span></span>
        <span></span>
      </div>
    </div>
  </div>

  <div class="auth-panel-right">
    <div class="auth-form-wrap">
      <div class="auth-mobile-logo">
        <img src="<?= htmlspecialchars($logoUrl) ?>" alt="<?= htmlspecialchars($brandName) ?>" loading="eager" decoding="async" onerror="this.style.display='none'">
        <span class="auth-mobile-logo__name"><?= htmlspecialchars($brandName) ?></span>
      </div>

      <?= $content ?? '' ?>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script src="<?= htmlspecialchars(base_url('/assets/vendor/adminlte/adminlte.min.js')) ?>"></script>
</body>
</html>
