// Screens 2: Unlock flow (BLE), Access (PIN/QR), Billing, Account, Wayfinding, Onboarding

const Mi2 = () => window.MIcons;
const D2 = () => window.M_DATA;
const { MStatusBar: MSB, MTabBar: MTB } = window.MScreens;

// ─────────────────────────────────────────────────────────────
// UNLOCK — full-screen flow w/ BLE scanning -> success
// ─────────────────────────────────────────────────────────────
const Screen_Unlock = ({ stage = "scanning" }) => {
  const Mi = Mi2(); const d = D2();
  const isOk = stage === "success";
  const isHold = stage === "hold";
  const ringFill = isOk ? "linear-gradient(140deg, #10b981, #047857)" : "radial-gradient(circle at 30% 30%, #6c63ff, #1f1a4a 70%)";

  return (
    <div className="m-page">
      <MSB />
      <div className="m-header" style={{ padding: "8px 18px 0" }}>
        <button className="m-iconbtn"><Mi.close size={18}/></button>
        <h1 style={{ fontSize: 16, fontWeight: 500, color: "var(--m-tx-2)" }}>Unlocking · {d.unit.id}</h1>
        <div style={{ width: 36 }}/>
      </div>

      <div style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: 24, padding: "0 24px" }}>
        {/* Big ring */}
        <div className="m-ring" style={{ position: "relative" }}>
          {!isOk && !isHold && (<>
            <div className="m-scan-ring r1"/>
            <div className="m-scan-ring r2"/>
            <div className="m-scan-ring r3"/>
          </>)}
          <div className="glow" style={{ background: isOk
              ? "radial-gradient(closest-side, rgba(16,185,129,0.45), transparent 70%)"
              : "radial-gradient(closest-side, rgba(124,108,255,0.45), transparent 70%)" }}/>
          <div className="core" style={{ background: ringFill }}>
            <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}>
              {isOk ? <Mi.check size={64} color="white" strokeWidth={2.4}/>
                    : isHold ? <Mi.unlock size={56} color="white" strokeWidth={1.8}/>
                    : <Mi.ble size={56} color="white" strokeWidth={1.8}/>}
              {!isOk && !isHold && <div className="label" style={{ marginTop: 10 }}>Searching</div>}
              {isHold && <div className="label" style={{ marginTop: 10 }}>Hold near lock</div>}
            </div>
          </div>
        </div>

        <div style={{ textAlign: "center" }}>
          <div style={{ fontSize: 24, fontWeight: 600, letterSpacing: "-0.02em" }}>
            {isOk ? "Unlocked" : isHold ? "Almost there" : "Connecting to lock"}
          </div>
          <div style={{ fontSize: 14, color: "var(--m-tx-2)", marginTop: 4, maxWidth: 280 }}>
            {isOk ? "You have 2 minutes to open the door before it locks again."
             : isHold ? "Hold your phone within 30 cm of the lock pad."
             : "Make sure Bluetooth is on. We're finding " + d.unit.locker.id + "."}
          </div>
        </div>

        {/* Telemetry strip */}
        <div className="m-card" style={{ width: "100%", padding: 12, display: "flex", justifyContent: "space-around", gap: 0 }}>
          <Telemetry label="Signal" value={isOk ? "−42 dBm" : "−56 dBm"} />
          <div style={{ width: 1, background: "var(--m-line)" }}/>
          <Telemetry label="Battery" value={`${d.unit.locker.battery}%`} />
          <div style={{ width: 1, background: "var(--m-line)" }}/>
          <Telemetry label="Lock" value={isOk ? "Open" : "Locked"} good={isOk} />
        </div>
      </div>

      <div style={{ padding: "0 18px 30px" }}>
        {!isOk ? (
          <button className="m-btn full" style={{ background: "transparent", border: "1px solid var(--m-line-2)" }}>
            Cancel
          </button>
        ) : (
          <button className="m-btn primary lg full">
            Done
          </button>
        )}
        {!isOk && (
          <div style={{ textAlign: "center", marginTop: 12, fontSize: 12.5, color: "var(--m-tx-3)" }}>
            Trouble? <span style={{ color: "var(--m-accent-2)" }}>Use my PIN instead</span>
          </div>
        )}
      </div>
    </div>
  );
};

const Telemetry = ({ label, value, good }) => (
  <div style={{ flex: 1, textAlign: "center" }}>
    <div style={{ fontSize: 11, color: "var(--m-tx-2)", textTransform: "uppercase", letterSpacing: "0.06em" }}>{label}</div>
    <div style={{ fontSize: 14, fontWeight: 500, marginTop: 2, color: good ? "#6ee7b7" : "var(--m-tx-1)", fontFeatureSettings: "'tnum'" }}>{value}</div>
  </div>
);

// ─────────────────────────────────────────────────────────────
// ACCESS — PIN keypad / QR / digital key
// ─────────────────────────────────────────────────────────────
const Screen_Access = ({ entered = "47" }) => {
  const Mi = Mi2(); const d = D2();
  const filled = entered.length;
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <button className="m-iconbtn"><Mi.back size={18}/></button>
        <h1>Access</h1>
        <button className="m-iconbtn"><Mi.history size={18}/></button>
      </div>

      <div className="m-scroll">
        {/* Mode tabs */}
        <div style={{ display: "flex", background: "var(--m-ink-2)", borderRadius: 14, padding: 4, gap: 4, border: "1px solid var(--m-line)" }}>
          {["PIN", "QR code", "Digital key"].map((m, i) => (
            <button key={m} style={{
              flex: 1, height: 36, borderRadius: 10, border: "none",
              background: i === 0 ? "var(--m-accent)" : "transparent",
              color: i === 0 ? "white" : "var(--m-tx-2)",
              fontSize: 13, fontWeight: 500, cursor: "pointer", fontFamily: "inherit",
            }}>{m}</button>
          ))}
        </div>

        <div style={{ textAlign: "center", margin: "32px 0 8px" }}>
          <div style={{ fontSize: 13, color: "var(--m-tx-2)", marginBottom: 6 }}>Enter your 4-digit PIN at any gate</div>
          <div style={{ fontSize: 12, color: "var(--m-tx-3)" }}>Gate 2 · {d.facility.name.split("·")[1].trim()}</div>
        </div>

        <div style={{ marginTop: 24, marginBottom: 28 }}>
          <div className="m-pin-dots">
            {[0,1,2,3].map(i => <div key={i} className={"m-pin-dot " + (i < filled ? "on" : "")} />)}
          </div>
        </div>

        <div className="m-keypad">
          {[1,2,3,4,5,6,7,8,9].map(n => <button key={n}>{n}</button>)}
          <button className="flat"><Mi.qr size={20} color="var(--m-tx-2)"/></button>
          <button>0</button>
          <button className="flat"><Mi.back size={20} color="var(--m-tx-2)"/></button>
        </div>

        <div style={{ marginTop: 24, padding: "12px 14px", background: "var(--m-ink-2)", borderRadius: 14, display: "flex", alignItems: "center", gap: 10, border: "1px solid var(--m-line)" }}>
          <Mi.shield size={18} color="var(--m-cyan)"/>
          <div style={{ flex: 1, fontSize: 12.5, color: "var(--m-tx-2)" }}>
            PIN access works even when your phone is offline.
          </div>
        </div>
      </div>
      <MTB active="access" />
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// WAYFINDING — facility map with route to your unit
// ─────────────────────────────────────────────────────────────
const Screen_Wayfind = () => {
  const Mi = Mi2(); const d = D2();
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <button className="m-iconbtn"><Mi.back size={18}/></button>
        <h1>Find your unit</h1>
        <button className="m-iconbtn"><Mi.search size={18}/></button>
      </div>

      <div className="m-scroll">
        <div className="m-map tall">
          <svg viewBox="0 0 360 360">
            <defs>
              <pattern id="floorgrid" width="20" height="20" patternUnits="userSpaceOnUse">
                <path d="M 20 0 L 0 0 0 20" fill="none" stroke="rgba(255,255,255,0.04)" strokeWidth="0.5"/>
              </pattern>
              <filter id="glow"><feGaussianBlur stdDeviation="3"/></filter>
            </defs>
            <rect width="360" height="360" fill="url(#floorgrid)"/>

            {/* Building outline */}
            <rect x="20" y="20" width="320" height="320" fill="none" stroke="rgba(255,255,255,0.18)" strokeWidth="1.5" rx="3"/>
            <text x="30" y="38" fill="rgba(255,255,255,0.4)" fontSize="9" fontFamily="Geist Mono">GROUND FLOOR · ZONE B</text>

            {/* Drive lane */}
            <rect x="40" y="170" width="280" height="40" fill="rgba(255,255,255,0.03)" stroke="rgba(255,255,255,0.08)" strokeDasharray="3 4"/>
            <text x="180" y="195" fill="rgba(255,255,255,0.3)" fontSize="9" textAnchor="middle" fontFamily="Geist Mono">DRIVE LANE</text>

            {/* Gate */}
            <g>
              <circle cx="40" cy="190" r="8" fill="#22d3ee" filter="url(#glow)"/>
              <circle cx="40" cy="190" r="6" fill="#22d3ee"/>
              <text x="20" y="218" fill="#67e8f9" fontSize="10" fontFamily="Geist" fontWeight="600">Gate 2</text>
            </g>

            {/* Zone A units */}
            {Array.from({length: 24}).map((_, i) => {
              const r = Math.floor(i/12), c = i % 12;
              return <rect key={"a"+i} className="m-fp-unit" x={50 + c*22} y={50 + r*22} width="20" height="20" rx="2"/>;
            })}
            {/* Zone B units, with B-115 highlighted */}
            {Array.from({length: 18}).map((_, i) => {
              const r = Math.floor(i/9), c = i % 9;
              const isYou = i === 14;
              return <rect key={"b"+i} className={"m-fp-unit" + (isYou ? " you" : "")}
                x={50 + c*22} y={120 + r*22} width="20" height="20" rx="2"/>;
            })}
            {/* Zone D drive-up */}
            {Array.from({length: 8}).map((_, i) => (
              <rect key={"d"+i} className="m-fp-unit" x={50 + i*30} y={240} width="28" height="40" rx="2"/>
            ))}

            {/* Route line */}
            <path d="M 40 190 L 100 190 L 100 145 L 178 145 L 178 142"
              fill="none" stroke="#7c6cff" strokeWidth="2.5" strokeDasharray="0"
              strokeLinecap="round" strokeLinejoin="round" opacity="0.9"/>
            <path d="M 40 190 L 100 190 L 100 145 L 178 145 L 178 142"
              fill="none" stroke="#7c6cff" strokeWidth="6" opacity="0.25" filter="url(#glow)"/>

            {/* You marker */}
            <g transform="translate(178 142)">
              <circle r="14" fill="rgba(124,108,255,0.18)"/>
              <circle r="7" fill="#7c6cff" stroke="white" strokeWidth="1.5"/>
              <text x="0" y="-22" fill="white" fontSize="11" fontWeight="600" textAnchor="middle" fontFamily="Geist">B-115</text>
            </g>

            {/* Compass */}
            <g transform="translate(320 50)">
              <circle r="14" fill="rgba(0,0,0,0.4)" stroke="rgba(255,255,255,0.15)"/>
              <path d="M 0 -8 L 0 8 M -3 -5 L 0 -8 L 3 -5" stroke="white" strokeWidth="1.2" fill="none"/>
              <text x="0" y="-16" fill="rgba(255,255,255,0.6)" fontSize="8" textAnchor="middle" fontFamily="Geist Mono">N</text>
            </g>
          </svg>

          {/* Floating ETA */}
          <div style={{ position: "absolute", top: 14, left: 14, background: "rgba(20,24,36,0.86)", backdropFilter: "blur(20px)", border: "1px solid var(--m-line-2)", borderRadius: 12, padding: "10px 14px" }}>
            <div style={{ fontSize: 11, color: "var(--m-tx-2)", textTransform: "uppercase", letterSpacing: "0.06em" }}>ETA on foot</div>
            <div style={{ fontSize: 18, fontWeight: 600, marginTop: 2 }}>2 min · 80m</div>
          </div>
        </div>

        <div className="m-section">Step-by-step</div>
        <div className="m-stack tight">
          <Step n="1" t="Pull in at Gate 2" s="Drive entrance off Beach Rd" done />
          <Step n="2" t="Tap to unlock at the gate" s="Or enter your 4-digit PIN" active />
          <Step n="3" t="Continue 60m on the drive lane" s="You'll see Zone B on your right" />
          <Step n="4" t="Park near pillar B" s="Walk into the corridor" />
          <Step n="5" t="Find unit B-115" s="6th door on the left" />
        </div>

        <div style={{ marginTop: 16 }}>
          <button className="m-btn primary lg full">
            <Mi.unlock size={18} strokeWidth={2}/> Unlock gate now
          </button>
        </div>
      </div>
    </div>
  );
};

const Step = ({ n, t, s, done, active }) => (
  <div className={"m-step" + (active ? " active" : "")} style={done ? { opacity: 0.55 } : {}}>
    <div className="num" style={done ? { background: "var(--m-emerald)", color: "white" } : {}}>
      {done ? <window.MIcons.check size={14} strokeWidth={2.4}/> : n}
    </div>
    <div className="body">
      <div className="t">{t}</div>
      <div className="s">{s}</div>
    </div>
  </div>
);

// ─────────────────────────────────────────────────────────────
// BILLING
// ─────────────────────────────────────────────────────────────
const Screen_Billing = () => {
  const Mi = Mi2(); const d = D2();
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <h1>Billing</h1>
        <button className="m-iconbtn"><Mi.plus size={18}/></button>
      </div>
      <div className="m-scroll">
        <div className="m-card" style={{ background: "linear-gradient(140deg, #1a1f2c 0%, #2a2640 60%, #4338ca 100%)", padding: 20, position: "relative", overflow: "hidden" }}>
          <div style={{ position: "absolute", top: -60, right: -60, width: 200, height: 200, borderRadius: "50%", background: "radial-gradient(closest-side, rgba(124,108,255,0.5), transparent)", filter: "blur(20px)" }}/>
          <div style={{ fontSize: 12.5, color: "rgba(255,255,255,0.7)", marginBottom: 6 }}>Next charge · {d.unit.next_bill}</div>
          <div style={{ fontSize: 38, fontWeight: 600, letterSpacing: "-0.03em", fontFeatureSettings: "'tnum'" }}>$250.00<span style={{ fontSize: 16, fontWeight: 500, color: "rgba(255,255,255,0.6)", marginLeft: 4 }}>NZD</span></div>
          <div style={{ fontSize: 13, color: "rgba(255,255,255,0.7)", marginTop: 6 }}>2 units · auto-pay enabled</div>
          <div style={{ display: "flex", gap: 8, marginTop: 16 }}>
            <button className="m-btn" style={{ background: "rgba(255,255,255,0.14)", borderColor: "rgba(255,255,255,0.18)", color: "white", flex: 1, height: 40, fontSize: 13 }}>Pay now</button>
            <button className="m-btn" style={{ background: "rgba(255,255,255,0.14)", borderColor: "rgba(255,255,255,0.18)", color: "white", flex: 1, height: 40, fontSize: 13 }}>Edit auto-pay</button>
          </div>
        </div>

        <div className="m-section">Payment method</div>
        <div className="m-card flat">
          {d.cards.map((c, i) => (
            <div key={i} className="m-row">
              <div className="ic" style={{ background: c.brand === "Visa" ? "#1a1f71" : "#eb001b" }}>
                <div style={{ fontSize: 11, fontWeight: 700, color: "white", fontFamily: "Geist" }}>{c.brand === "Visa" ? "VISA" : "MC"}</div>
              </div>
              <div className="body">
                <div className="t">{c.brand} •• {c.last4}</div>
                <div className="s">Expires {c.exp}{c.primary ? " · Primary" : ""}</div>
              </div>
              {c.primary && <span className="m-pill">Default</span>}
            </div>
          ))}
          <div className="m-row" style={{ color: "var(--m-accent-2)" }}>
            <div className="ic" style={{ background: "var(--m-accent-soft)" }}><Mi.plus size={16} color="var(--m-accent-2)"/></div>
            <div className="body"><div className="t" style={{ color: "var(--m-accent-2)" }}>Add card or bank account</div></div>
          </div>
        </div>

        <div className="m-section">Activity</div>
        <div className="m-card flat">
          {d.ledger.slice(0, 6).map((l, i) => (
            <div key={i} className="m-row">
              <div className="ic" style={{
                background: l.status === "paid" ? "color-mix(in oklab, var(--m-emerald) 16%, transparent)" : "color-mix(in oklab, var(--m-amber) 16%, transparent)"
              }}>
                <Mi.card size={16} color={l.status === "paid" ? "#6ee7b7" : "#fcd34d"}/>
              </div>
              <div className="body">
                <div className="t">{l.desc}</div>
                <div className="s">{l.date} · {l.status === "paid" ? "Paid" : "Scheduled"}</div>
              </div>
              <div className="end m-num" style={{ fontWeight: 500, color: l.status === "scheduled" ? "var(--m-tx-2)" : "var(--m-tx-1)" }}>
                ${l.amount.toFixed(2)}
              </div>
            </div>
          ))}
        </div>
      </div>
      <MTB active="billing" />
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// ACCOUNT
// ─────────────────────────────────────────────────────────────
const Screen_Account = () => {
  const Mi = Mi2(); const d = D2();
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <h1>Account</h1>
        <button className="m-iconbtn"><Mi.gear size={18}/></button>
      </div>
      <div className="m-scroll">
        <div style={{ display: "flex", flexDirection: "column", alignItems: "center", padding: "12px 0 20px" }}>
          <div style={{ width: 88, height: 88, borderRadius: 28, background: d.tenant.photo_bg, display: "grid", placeItems: "center", color: "white", fontSize: 28, fontWeight: 600, boxShadow: "inset 0 1px 0 rgba(255,255,255,0.2), 0 12px 32px -10px rgba(79,70,229,0.5)" }}>{d.tenant.initials}</div>
          <div style={{ fontSize: 22, fontWeight: 600, marginTop: 14, letterSpacing: "-0.02em" }}>{d.tenant.name}</div>
          <div style={{ fontSize: 13, color: "var(--m-tx-2)", marginTop: 2 }}>Member since {d.tenant.member_since}</div>
        </div>

        {/* Stats grid */}
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 8 }}>
          <Stat2 label="Units" value="2" />
          <Stat2 label="Visits" value="38" />
          <Stat2 label="On-time" value="100%" good />
        </div>

        <div className="m-section">Profile</div>
        <div className="m-card flat">
          <Row icon="user" t="Personal info" s={d.tenant.email}/>
          <Row icon="bell" t="Notifications" s="Lock reminders, billing"/>
          <Row icon="shield" t="Identity & insurance" s="Verified · $5,000 cover"/>
        </div>

        <div className="m-section">Preferences</div>
        <div className="m-card flat">
          <Row icon="lock" t="Auto-lock reminder" s="Notify after 15 min open" rightToggle/>
          <Row icon="ble" t="Bluetooth unlock" s="On" rightToggle defaultOn/>
          <Row icon="qr" t="Touch ID for unlock" s="Required" rightToggle defaultOn/>
        </div>

        <div className="m-section">Plan</div>
        <div className="m-card">
          <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 10 }}>
            <div style={{ width: 40, height: 40, borderRadius: 12, background: "linear-gradient(140deg, var(--m-accent-2), var(--m-accent))", display: "grid", placeItems: "center" }}>
              <Mi.package size={20} color="white"/>
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 14, fontWeight: 500 }}>VaultOS Plus</div>
              <div style={{ fontSize: 12.5, color: "var(--m-tx-2)" }}>Insurance + 24/7 access + climate</div>
            </div>
            <span className="m-pill ok">Active</span>
          </div>
          <button className="m-btn ghost full" style={{ height: 40, fontSize: 13 }}>Manage plan</button>
        </div>

        <div className="m-section">Support</div>
        <div className="m-card flat">
          <Row icon="alert" t="Report an issue" s="Lock, billing, facility"/>
          <Row icon="history" t="Refer a mate" s="Both get $25"/>
        </div>

        <button className="m-btn ghost full" style={{ marginTop: 16, color: "var(--m-rose)", border: "none" }}>Sign out</button>
      </div>
      <MTB active="account" />
    </div>
  );
};

const Row = ({ icon, t, s, rightToggle, defaultOn }) => {
  const Mi = Mi2();
  const Icon = Mi[icon];
  return (
    <div className="m-row">
      <div className="ic"><Icon size={16} color="var(--m-tx-2)"/></div>
      <div className="body">
        <div className="t">{t}</div>
        <div className="s">{s}</div>
      </div>
      {rightToggle ? (
        <div style={{
          width: 38, height: 22, borderRadius: 99,
          background: defaultOn ? "var(--m-accent)" : "var(--m-ink-3)",
          position: "relative", transition: "150ms",
        }}>
          <div style={{
            position: "absolute", top: 2, left: defaultOn ? 18 : 2,
            width: 18, height: 18, borderRadius: 99, background: "white",
            transition: "150ms", boxShadow: "0 1px 3px rgba(0,0,0,0.3)",
          }}/>
        </div>
      ) : (
        <Mi.chevron size={16} color="var(--m-tx-3)"/>
      )}
    </div>
  );
};

const Stat2 = ({ label, value, good }) => (
  <div className="m-card" style={{ padding: 12, textAlign: "center" }}>
    <div style={{ fontSize: 22, fontWeight: 600, letterSpacing: "-0.02em", color: good ? "#6ee7b7" : "var(--m-tx-1)", fontFeatureSettings: "'tnum'" }}>{value}</div>
    <div style={{ fontSize: 11.5, color: "var(--m-tx-2)", marginTop: 2 }}>{label}</div>
  </div>
);

// ─────────────────────────────────────────────────────────────
// ONBOARDING — three landing screens & pick-a-unit
// ─────────────────────────────────────────────────────────────
const Screen_Onboard_Welcome = () => {
  const Mi = Mi2();
  return (
    <div className="m-page" style={{ background: "var(--m-ink-0)" }}>
      <div className="m-onboard-bg"/>
      <div style={{ position: "relative", zIndex: 1, display: "flex", flexDirection: "column", height: "100%" }}>
        <MSB />
        <div style={{ flex: 1, padding: "0 28px", display: "flex", flexDirection: "column", justifyContent: "center" }}>
          <div className="m-logomark" style={{ marginBottom: 32 }}>
            <svg width="40" height="40" viewBox="0 0 40 40" fill="none">
              <path d="M8 12L20 6L32 12V24L20 30L8 24V12Z" stroke="white" strokeWidth="2" strokeLinejoin="round"/>
              <path d="M14 17L20 14L26 17V21L20 24L14 21V17Z" fill="white"/>
            </svg>
          </div>
          <div style={{ fontSize: 36, fontWeight: 600, lineHeight: 1.05, letterSpacing: "-0.03em" }}>
            Storage that<br/>opens with a tap.
          </div>
          <div style={{ fontSize: 15, color: "var(--m-tx-2)", marginTop: 18, lineHeight: 1.5, maxWidth: 320 }}>
            Find a unit, sign your lease, and unlock the gate — all from your phone. No keys, no queues.
          </div>
        </div>
        <div style={{ padding: "0 22px 32px" }}>
          <button className="m-btn primary lg full">Get started</button>
          <div style={{ textAlign: "center", marginTop: 14, fontSize: 13.5, color: "var(--m-tx-2)" }}>
            Already a customer? <span style={{ color: "var(--m-accent-2)", fontWeight: 500 }}>Sign in</span>
          </div>
        </div>
      </div>
    </div>
  );
};

const Screen_Onboard_PickSize = () => {
  const Mi = Mi2();
  const sizes = [
    { id: 1, label: "Locker",   sub: "1.0 × 1.5 m · holds boxes & bags",     dim: "1.5m²", price: 65,  ic: "package" },
    { id: 2, label: "Standard", sub: "2.4 × 3.0 m · 1-bedroom worth",        dim: "7.2m²", price: 185, ic: "unit", selected: true },
    { id: 3, label: "Large",    sub: "3.0 × 4.5 m · 2–3 bedrooms + couch",   dim: "13.5m²", price: 295, ic: "unit" },
    { id: 4, label: "Drive-up", sub: "3.0 × 4.5 m · van or trailer access",  dim: "13.5m²", price: 245, ic: "package" },
  ];
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <button className="m-iconbtn"><Mi.back size={18}/></button>
        <h1 style={{ fontSize: 16, fontWeight: 500, color: "var(--m-tx-2)" }}>Step 2 of 4</h1>
        <div style={{ width: 36 }}/>
      </div>
      <div className="m-scroll">
        <div style={{ height: 3, background: "var(--m-ink-3)", borderRadius: 99, marginBottom: 24, overflow: "hidden" }}>
          <div style={{ width: "50%", height: "100%", background: "var(--m-accent)" }}/>
        </div>

        <div style={{ fontSize: 26, fontWeight: 600, letterSpacing: "-0.02em", marginBottom: 6 }}>Pick your size</div>
        <div style={{ fontSize: 14, color: "var(--m-tx-2)", marginBottom: 20 }}>You can upgrade or downgrade any time.</div>

        <div className="m-stack tight">
          {sizes.map(s => {
            const Icon = Mi[s.ic];
            return (
              <div key={s.id} className="m-card" style={{
                padding: 14, display: "flex", alignItems: "center", gap: 14,
                border: s.selected ? "1.5px solid var(--m-accent)" : "1px solid var(--m-line)",
                background: s.selected ? "color-mix(in oklab, var(--m-accent) 10%, var(--m-ink-2))" : "var(--m-ink-2)",
              }}>
                <div style={{ width: 50, height: 50, borderRadius: 12, background: s.selected ? "var(--m-accent)" : "var(--m-ink-3)", display: "grid", placeItems: "center" }}>
                  <Icon size={22} color={s.selected ? "white" : "var(--m-tx-2)"}/>
                </div>
                <div style={{ flex: 1 }}>
                  <div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
                    <div style={{ fontSize: 15, fontWeight: 600 }}>{s.label}</div>
                    <span style={{ fontSize: 11, color: "var(--m-tx-3)", fontFamily: "Geist Mono" }}>{s.dim}</span>
                  </div>
                  <div style={{ fontSize: 12.5, color: "var(--m-tx-2)", marginTop: 2 }}>{s.sub}</div>
                </div>
                <div style={{ textAlign: "right" }}>
                  <div style={{ fontSize: 16, fontWeight: 600, fontFeatureSettings: "'tnum'" }}>${s.price}</div>
                  <div style={{ fontSize: 11, color: "var(--m-tx-3)" }}>/ month</div>
                </div>
              </div>
            );
          })}
        </div>

        <div className="m-section">Helpful for</div>
        <div style={{ display: "flex", gap: 6, flexWrap: "wrap" }}>
          {["1-bed apartment", "Studio gear", "Business stock", "Seasonal items", "Sports kit"].map(t =>
            <span key={t} className="m-pill">{t}</span>
          )}
        </div>
      </div>
      <div style={{ padding: "0 18px 30px", display: "flex", gap: 10 }}>
        <button className="m-btn" style={{ width: 80 }}>Back</button>
        <button className="m-btn primary" style={{ flex: 1 }}>Continue · Standard</button>
      </div>
    </div>
  );
};

const Screen_Onboard_Confirm = () => {
  const Mi = Mi2(); const d = D2();
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header">
        <button className="m-iconbtn"><Mi.back size={18}/></button>
        <h1 style={{ fontSize: 16, fontWeight: 500, color: "var(--m-tx-2)" }}>Step 4 of 4</h1>
        <div style={{ width: 36 }}/>
      </div>
      <div className="m-scroll">
        <div style={{ height: 3, background: "var(--m-ink-3)", borderRadius: 99, marginBottom: 24, overflow: "hidden" }}>
          <div style={{ width: "100%", height: "100%", background: "var(--m-accent)" }}/>
        </div>

        <div style={{ fontSize: 26, fontWeight: 600, letterSpacing: "-0.02em", marginBottom: 6 }}>Review & sign</div>
        <div style={{ fontSize: 14, color: "var(--m-tx-2)", marginBottom: 20 }}>You'll get keyless access the moment you sign.</div>

        <div className="m-card flat">
          <div className="m-row"><div className="body"><div className="t">Unit</div></div><div className="end">{d.unit.id} · Standard</div></div>
          <div className="m-row"><div className="body"><div className="t">Size</div></div><div className="end">{d.unit.size}</div></div>
          <div className="m-row"><div className="body"><div className="t">Climate</div></div><div className="end">Constant 20°C</div></div>
          <div className="m-row"><div className="body"><div className="t">Insurance</div></div><div className="end">Up to $5,000</div></div>
          <div className="m-row"><div className="body"><div className="t">Move-in</div></div><div className="end">Today, 14:00 NZST</div></div>
        </div>

        <div className="m-section">You'll pay today</div>
        <div className="m-card">
          <Line t="First month" v="$185.00"/>
          <Line t="Refundable bond" v="$185.00"/>
          <Line t="VaultOS Plus" v="$0.00" sub="Free for 30 days"/>
          <div style={{ height: 1, background: "var(--m-line)", margin: "10px 0" }}/>
          <Line t="Total" v="$370.00" big/>
          <div style={{ fontSize: 12, color: "var(--m-tx-3)", marginTop: 6 }}>Then $185/month on the 14th. Cancel any time, no penalty.</div>
        </div>

        <div className="m-card" style={{ marginTop: 12, display: "flex", alignItems: "center", gap: 12 }}>
          <div style={{ width: 40, height: 40, borderRadius: 11, background: "#1a1f71", display: "grid", placeItems: "center", color: "white", fontSize: 11, fontWeight: 700 }}>VISA</div>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 14, fontWeight: 500 }}>Visa •• 4218</div>
            <div style={{ fontSize: 12.5, color: "var(--m-tx-2)" }}>Expires 08/27</div>
          </div>
          <span style={{ fontSize: 12.5, color: "var(--m-accent-2)", fontWeight: 500 }}>Change</span>
        </div>

        <div style={{ display: "flex", alignItems: "flex-start", gap: 10, marginTop: 16, padding: "10px 12px", background: "var(--m-ink-2)", borderRadius: 12, border: "1px solid var(--m-line)" }}>
          <div style={{ width: 18, height: 18, borderRadius: 5, background: "var(--m-accent)", display: "grid", placeItems: "center", flexShrink: 0, marginTop: 1 }}>
            <Mi.check size={12} color="white" strokeWidth={3}/>
          </div>
          <div style={{ fontSize: 12.5, color: "var(--m-tx-2)", lineHeight: 1.5 }}>
            I agree to the <span style={{ color: "var(--m-accent-2)" }}>tenancy agreement</span> and confirm I'm storing only allowed items.
          </div>
        </div>
      </div>
      <div style={{ padding: "0 18px 30px" }}>
        <button className="m-btn primary lg full">
          <Mi.lock size={18} strokeWidth={2}/> Sign & pay $370.00
        </button>
      </div>
    </div>
  );
};

const Line = ({ t, v, sub, big }) => (
  <div style={{ display: "flex", alignItems: "baseline", justifyContent: "space-between", padding: "4px 0" }}>
    <div>
      <div style={{ fontSize: big ? 16 : 14, fontWeight: big ? 600 : 400, color: "var(--m-tx-1)" }}>{t}</div>
      {sub && <div style={{ fontSize: 11.5, color: "var(--m-tx-3)", marginTop: 1 }}>{sub}</div>}
    </div>
    <div style={{ fontSize: big ? 18 : 14, fontWeight: big ? 600 : 500, fontFeatureSettings: "'tnum'", letterSpacing: "-0.01em" }}>{v}</div>
  </div>
);

// ─────────────────────────────────────────────────────────────
// LOCK-UP REMINDER (push notification + in-app banner)
// ─────────────────────────────────────────────────────────────
const Screen_LockReminder = () => {
  const Mi = Mi2(); const d = D2();
  return (
    <div className="m-page">
      <MSB />
      <div className="m-header" style={{ padding: "8px 18px 0" }}>
        <button className="m-iconbtn"><Mi.close size={18}/></button>
        <h1 style={{ fontSize: 16, fontWeight: 500, color: "var(--m-tx-2)" }}>Reminder</h1>
        <div style={{ width: 36 }}/>
      </div>

      <div style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", padding: "0 24px", textAlign: "center" }}>
        <div style={{ width: 96, height: 96, borderRadius: 28, background: "color-mix(in oklab, var(--m-amber) 16%, var(--m-ink-2))", border: "1px solid rgba(245,158,11,0.3)", display: "grid", placeItems: "center", marginBottom: 24, position: "relative" }}>
          <Mi.unlock size={42} color="#fcd34d" strokeWidth={1.6}/>
          <div style={{ position: "absolute", top: -4, right: -4, width: 22, height: 22, borderRadius: 99, background: "var(--m-rose)", display: "grid", placeItems: "center", color: "white", fontSize: 12, fontWeight: 600 }}>!</div>
        </div>
        <div style={{ fontSize: 24, fontWeight: 600, letterSpacing: "-0.02em" }}>Did you lock up?</div>
        <div style={{ fontSize: 14, color: "var(--m-tx-2)", marginTop: 8, maxWidth: 280, lineHeight: 1.5 }}>
          Unit <strong style={{ color: "var(--m-tx-1)" }}>{d.unit.id}</strong> has been open for 32 minutes. We'll keep reminding you until it's secure.
        </div>

        <div className="m-card" style={{ marginTop: 28, width: "100%", padding: 14 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
            <div style={{ width: 8, height: 8, borderRadius: 99, background: "var(--m-amber)", boxShadow: "0 0 0 4px rgba(245,158,11,0.2)" }}/>
            <div style={{ flex: 1, textAlign: "left" }}>
              <div style={{ fontSize: 13, fontWeight: 500 }}>Sensor: door open</div>
              <div style={{ fontSize: 12, color: "var(--m-tx-2)" }}>Last closed at 14:08 today</div>
            </div>
            <span className="m-pill warn">32m</span>
          </div>
        </div>
      </div>

      <div style={{ padding: "0 18px 30px", display: "flex", gap: 10 }}>
        <button className="m-btn" style={{ flex: 1 }}>I'm still inside</button>
        <button className="m-btn primary" style={{ flex: 1 }}>
          <Mi.lock size={16} strokeWidth={2}/> Lock now
        </button>
      </div>
    </div>
  );
};

window.MScreens2 = {
  Screen_Unlock, Screen_Access, Screen_Wayfind, Screen_Billing, Screen_Account,
  Screen_Onboard_Welcome, Screen_Onboard_PickSize, Screen_Onboard_Confirm,
  Screen_LockReminder,
};
