/* global React, I, ScreenHeader, Field, Btn, Pill, StatusPill, useState, useEffect */

// ============ SPLASH ============
function SplashScreen({ go }) {
  useEffect(() => {
    const t = setTimeout(() => go("login", "fade"), 2400);
    return () => clearTimeout(t);
  }, []);
  return (
    <div className="center" style={{ flex: 1, position: "relative", overflow: "hidden", background: "radial-gradient(ellipse at 30% 20%, var(--accent-soft) 0%, var(--paper-2) 60%)" }}>
      {/* mesh blobs */}
      <div style={{ position: "absolute", top: -60, left: -80, width: 280, height: 280, borderRadius: "50%", background: "rgba(16,183,127,0.10)", filter: "blur(40px)" }} />
      <div style={{ position: "absolute", bottom: -80, right: -60, width: 300, height: 300, borderRadius: "50%", background: "rgba(42,110,217,0.10)", filter: "blur(40px)" }} />

      <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 18, zIndex: 1 }}>
        {/* logo */}
        <div className="ripple-wrap" style={{ width: 96, height: 96, position: "relative" }}>
          <span className="ripple" style={{ borderColor: "var(--accent)" }} />
          <span className="ripple r2" style={{ borderColor: "var(--accent)" }} />
          <div style={{
            position: "absolute", inset: 0, borderRadius: "50%",
            background: "var(--paper-2)", border: "1.6px solid var(--ink)",
            display: "grid", placeItems: "center",
            boxShadow: "0 20px 40px -16px rgba(16,183,127,0.4), inset 0 -4px 0 rgba(15,20,25,0.04)"
          }}>
            <svg width="44" height="44" viewBox="0 0 24 24" fill="none" stroke="var(--accent)" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 21s-7-4.5-9.5-9A5 5 0 0112 6a5 5 0 019.5 6c-2.5 4.5-9.5 9-9.5 9z"/><path d="M3 12h4l2-4 4 8 2-4h6" stroke="var(--ink)" strokeWidth="1.6"/></svg>
          </div>
        </div>
        <div style={{ textAlign: "center" }}>
          <div style={{ fontSize: 28, fontWeight: 800, letterSpacing: "-0.4px" }}>ArogyaSync</div>
          <div style={{ fontSize: 12, color: "var(--ink-3)", letterSpacing: 2, marginTop: 4, textTransform: "uppercase" }}>Your Health, Synchronized</div>
        </div>
        <div style={{ width: 200, marginTop: 18 }}>
          <div className="prog"><div className="fill" /></div>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 6, fontSize: 11, color: "var(--ink-4)", marginTop: 18, padding: "6px 14px", border: "1px solid var(--line)", borderRadius: 999, background: "var(--paper-2)" }}>
          <span className="live-dot" style={{ background: "var(--accent)" }} /> Powered by AI Analytics
        </div>
      </div>
      <div style={{ position: "absolute", bottom: 24, fontSize: 10, color: "var(--ink-4)", letterSpacing: 1 }}>v 2.0 • BUILD 26.5</div>
    </div>
  );
}

// ============ LOGIN ============
function LoginScreen({ go }) {
  const [showPass, setShowPass] = useState(false);
  const [remember, setRemember] = useState(true);
  return (
    <div style={{ flex: 1, padding: "20px 22px 22px", display: "flex", flexDirection: "column", gap: 18, overflow: "auto", position: "relative" }}>
      <div style={{ position: "absolute", top: -10, right: -30, width: 180, height: 180, borderRadius: "50%", background: "rgba(16,183,127,0.08)", filter: "blur(30px)" }} />

      <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 8, marginTop: 8 }}>
        <div style={{ width: 56, height: 56, borderRadius: 14, border: "1.4px solid var(--ink)", background: "var(--paper-2)", display: "grid", placeItems: "center" }}>
          <I.heart />
        </div>
        <div style={{ fontSize: 22, fontWeight: 800, letterSpacing: "-0.3px" }}>Welcome back</div>
        <div style={{ fontSize: 13, color: "var(--ink-3)" }}>Sign in to continue monitoring</div>
      </div>

      <div className="tab-row">
        <div className="t on">Email</div>
        <div className="t">Phone</div>
      </div>

      <div className="stagger" style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        <Field icon={<I.mail />} label="Email address" value="sarah.chen@hospital.com" focus />
        <Field icon={<I.lock />} label="Password" value={showPass ? "•••secure" : "••••••••"} suffix={
          <span className="tap-hot dim" onClick={() => setShowPass(s => !s)}><I.eye /></span>
        } />

        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 2 }}>
          <div className="tap-hot" style={{ display: "flex", alignItems: "center", gap: 8 }} onClick={() => setRemember(r => !r)}>
            <span className={"cb" + (remember ? " on" : "")}>{remember && <I.check />}</span>
            <span style={{ fontSize: 12 }}>Remember me</span>
          </div>
          <div className="tap-hot" style={{ fontSize: 12, color: "var(--ink-2)", fontWeight: 700 }} onClick={() => go("forgot")}>Forgot password?</div>
        </div>

        <Btn variant="primary" onClick={() => go("hospital")}>Sign in →</Btn>

        <div style={{ display: "flex", alignItems: "center", gap: 10, color: "var(--ink-4)", fontSize: 11, margin: "6px 0" }}>
          <div style={{ flex: 1, height: 1, background: "var(--line)" }} /> OR CONTINUE WITH <div style={{ flex: 1, height: 1, background: "var(--line)" }} />
        </div>
        <Btn variant="ghost" onClick={() => go("hospital")}>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}><I.google /> Google</span>
        </Btn>
        <Btn variant="ghost" sm onClick={() => go("hospital")} style={{ height: 42 }}>
          Try Demo Mode
        </Btn>
      </div>

      <div style={{ textAlign: "center", fontSize: 13, color: "var(--ink-3)", marginTop: "auto" }}>
        Don't have an account?{" "}
        <span className="tap-hot" style={{ color: "var(--ink)", fontWeight: 700 }} onClick={() => go("register")}>Register →</span>
      </div>
    </div>
  );
}

// ============ REGISTER ============
function RegisterScreen({ go }) {
  const [terms, setTerms] = useState(false);
  return (
    <div style={{ flex: 1, padding: "16px 22px 22px", display: "flex", flexDirection: "column", gap: 14, overflow: "auto" }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <div className="back" onClick={() => go("login", "back")}><I.back /> Back</div>
        <div className="tap-hot" style={{ fontSize: 12, color: "var(--ink-3)" }}>Help</div>
      </div>
      <div>
        <div style={{ fontSize: 24, fontWeight: 800, letterSpacing: "-0.3px" }}>Create account</div>
        <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 2 }}>Join ArogyaSync clinical network</div>
      </div>

      <div className="stagger" style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        <Field icon={<I.user />} label="Full name" value="" />
        <Field icon={<I.mail />} label="name@hospital.com" value="" />
        <Field icon={<I.lock />} label="Password" suffix={<I.eye />} />

        <div className="tap-hot" style={{ display: "flex", gap: 10, padding: 4, marginTop: 4 }} onClick={() => setTerms(t => !t)}>
          <span className={"cb" + (terms ? " on" : "")}>{terms && <I.check />}</span>
          <span style={{ fontSize: 12, color: "var(--ink-2)", lineHeight: 1.5 }}>
            I accept the <span style={{ fontWeight: 700, textDecoration: "underline" }}>Terms of Service</span> and acknowledge the <span style={{ fontWeight: 700, textDecoration: "underline" }}>HIPAA Privacy Notice</span>
          </span>
        </div>
        {!terms && (
          <div style={{ fontSize: 11, color: "var(--warn)", padding: "8px 12px", background: "var(--warn-soft)", borderRadius: 8, border: "1px solid #ecd58a" }}>
            ⚠ Please accept terms before continuing
          </div>
        )}

        <Btn variant={terms ? "primary" : "ghost"} onClick={() => terms && go("otp")}>Create account</Btn>

        <div style={{ display: "flex", alignItems: "center", gap: 10, color: "var(--ink-4)", fontSize: 11, margin: "2px 0" }}>
          <div style={{ flex: 1, height: 1, background: "var(--line)" }} /> OR <div style={{ flex: 1, height: 1, background: "var(--line)" }} />
        </div>
        <Btn variant="ghost" onClick={() => go("hospital")}>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}><I.google /> Continue with Google</span>
        </Btn>
      </div>

      <div style={{ textAlign: "center", fontSize: 13, color: "var(--ink-3)", marginTop: "auto" }}>
        Already have an account? <span className="tap-hot" style={{ color: "var(--ink)", fontWeight: 700 }} onClick={() => go("login", "back")}>Sign in</span>
      </div>
    </div>
  );
}

// ============ OTP ============
function OtpScreen({ go }) {
  const [code, setCode] = useState("");
  const [timer, setTimer] = useState(55);
  useEffect(() => {
    if (timer <= 0) return;
    const t = setTimeout(() => setTimer(timer - 1), 1000);
    return () => clearTimeout(t);
  }, [timer]);
  useEffect(() => {
    if (code.length === 6) {
      const t = setTimeout(() => go("hospital", "fade"), 600);
      return () => clearTimeout(t);
    }
  }, [code]);
  const press = (k) => {
    if (k === "del") setCode(c => c.slice(0, -1));
    else if (code.length < 6) setCode(c => c + k);
  };
  return (
    <div style={{ flex: 1, padding: "16px 22px 22px", display: "flex", flexDirection: "column", gap: 18 }}>
      <div className="back" onClick={() => go("register", "back")}><I.back /> Back</div>
      <div style={{ textAlign: "center" }}>
        <div style={{ width: 48, height: 48, margin: "0 auto 10px", border: "1.4px solid var(--ink)", borderRadius: 12, display: "grid", placeItems: "center", background: "var(--paper-2)" }}>
          <I.shield />
        </div>
        <div style={{ fontSize: 22, fontWeight: 800, letterSpacing: "-0.3px" }}>Verify your identity</div>
        <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 4 }}>We sent a 6-digit code to<br /><span style={{ color: "var(--ink-2)", fontWeight: 700 }}>sarah.chen@hospital.com</span></div>
      </div>

      <div className="otp">
        {[0, 1, 2, 3, 4, 5].map(i => (
          <div key={i} className={"cell" + (code[i] ? " filled" : "") + (i === code.length ? " active" : "")}>{code[i] || ""}</div>
        ))}
      </div>

      <div style={{ textAlign: "center", fontSize: 12, color: "var(--ink-3)" }}>
        {timer > 0 ? <>Resend code in <span className="mono" style={{ color: "var(--ink)", fontWeight: 700 }}>{timer}s</span></> : <span className="tap-hot" style={{ color: "var(--accent)", fontWeight: 800 }} onClick={() => setTimer(55)}>Resend code</span>}
      </div>

      <div style={{ marginTop: "auto" }}>
        <div className="keypad">
          {["1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "del"].map((k, i) => (
            k === "" ? <div key={i} /> :
            <div key={i} className="k" onClick={() => press(k)}>
              {k === "del" ? <I.back /> : k}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

// ============ FORGOT PASSWORD ============
function ForgotScreen({ go }) {
  const [sent, setSent] = useState(false);
  return (
    <div style={{ flex: 1, padding: "16px 22px 22px", display: "flex", flexDirection: "column", gap: 16 }}>
      <div className="back" onClick={() => go("login", "back")}><I.back /> Back</div>

      {!sent ? (
        <>
          <div style={{ width: 64, height: 64, border: "1.4px solid var(--ink)", borderRadius: 16, display: "grid", placeItems: "center", margin: "10px 0 4px", background: "var(--paper-2)" }}>
            <I.lock />
          </div>
          <div>
            <div style={{ fontSize: 24, fontWeight: 800, letterSpacing: "-0.3px" }}>Reset password</div>
            <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 4, lineHeight: 1.5 }}>Enter your email and we'll send you a link to reset your password.</div>
          </div>
          <Field icon={<I.mail />} label="Email address" value="" focus />
          <Btn variant="primary" onClick={() => setSent(true)}>Send reset link</Btn>
        </>
      ) : (
        <div className="enter-up" style={{ animation: "enterUp 380ms cubic-bezier(.2,.85,.3,1) both" }}>
          <div style={{ width: 64, height: 64, border: "1.4px solid var(--accent)", background: "var(--accent-soft)", color: "var(--accent)", borderRadius: 16, display: "grid", placeItems: "center", margin: "10px 0 16px" }}>
            <I.check />
          </div>
          <div style={{ fontSize: 24, fontWeight: 800 }}>Check your email</div>
          <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 6, lineHeight: 1.5 }}>We've sent reset instructions to <strong style={{ color: "var(--ink)" }}>sarah.chen@hospital.com</strong>. Check your inbox.</div>
          <div style={{ marginTop: 20 }}>
            <Btn variant="ghost" onClick={() => go("login", "back")}>Back to sign in</Btn>
          </div>
        </div>
      )}
    </div>
  );
}

Object.assign(window, { SplashScreen, LoginScreen, RegisterScreen, OtpScreen, ForgotScreen });
