// Phone frame + three app screens that cycle: Map w/ boundaries, Circle-an-area, Pin details
const { useState, useEffect, useRef } = React;

function PhoneFrame({ children, width = 340 }) {
  const h = Math.round(width * (852/393));
  return (
    <div style={{
      width, height: h, position: "relative",
      borderRadius: 52, padding: 10,
      background: "linear-gradient(180deg, #23262F, #0b0c10)",
      boxShadow: "0 40px 80px -24px rgba(14,18,32,.35), 0 8px 24px -8px rgba(14,18,32,.2), inset 0 0 0 1px rgba(255,255,255,.08)",
    }}>
      <div style={{
        position: "absolute", inset: 10,
        borderRadius: 44, overflow: "hidden",
        background: "#fff",
      }}>
        {children}
      </div>
      {/* Dynamic island */}
      <div style={{
        position: "absolute", top: 18, left: "50%", transform: "translateX(-50%)",
        width: 100, height: 28, borderRadius: 99, background: "#000", zIndex: 10,
      }}/>
    </div>
  );
}

// Screen 1: Main map with property boundaries
function ScreenMap() {
  return (
    <div style={{ position: "relative", width: "100%", height: "100%", fontFamily: "'SF Pro Text', -apple-system, sans-serif" }}>
      {/* Satellite map tile — stylized */}
      <svg viewBox="0 0 393 852" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0 }}>
        <defs>
          <pattern id="forest" width="18" height="18" patternUnits="userSpaceOnUse">
            <rect width="18" height="18" fill="#3f5a3a"/>
            <circle cx="4" cy="4" r="2.2" fill="#4a6a42"/>
            <circle cx="12" cy="10" r="2.6" fill="#354c32"/>
            <circle cx="6" cy="14" r="1.8" fill="#4a6a42"/>
          </pattern>
          <pattern id="field" width="24" height="24" patternUnits="userSpaceOnUse">
            <rect width="24" height="24" fill="#a8b36c"/>
            <line x1="0" y1="12" x2="24" y2="12" stroke="#959f5c" strokeWidth="0.5"/>
          </pattern>
          <pattern id="field2" width="24" height="24" patternUnits="userSpaceOnUse">
            <rect width="24" height="24" fill="#c5a868"/>
            <line x1="0" y1="0" x2="24" y2="24" stroke="#b3945a" strokeWidth="0.5"/>
          </pattern>
        </defs>
        <rect width="393" height="852" fill="#7d8c5a"/>
        {/* Parcels */}
        <polygon points="0,120 180,80 220,240 40,290" fill="url(#field)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="180,80 393,60 393,220 220,240" fill="url(#field2)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="220,240 393,220 393,420 260,440" fill="url(#forest)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="40,290 220,240 260,440 60,480" fill="url(#field)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="0,290 40,290 60,480 0,500" fill="url(#forest)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="0,500 60,480 260,440 300,620 180,680 0,660" fill="url(#field2)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="260,440 393,420 393,700 300,620" fill="url(#forest)" stroke="#fff" strokeWidth="1.5"/>
        <polygon points="0,660 180,680 300,620 393,700 393,852 0,852" fill="url(#field)" stroke="#fff" strokeWidth="1.5"/>
        {/* Road */}
        <path d="M 0 560 Q 140 540 260 600 T 393 640" fill="none" stroke="#d8d4c5" strokeWidth="8"/>
        <path d="M 0 560 Q 140 540 260 600 T 393 640" fill="none" stroke="#fff" strokeWidth="1" strokeDasharray="6 6"/>
        {/* River */}
        <path d="M 80 0 Q 110 200 60 340 Q 20 480 120 620 Q 200 760 150 852" fill="none" stroke="#6fa6d0" strokeWidth="10" strokeLinecap="round"/>
        {/* Live location */}
        <g transform="translate(196 500)">
          <circle r="48" fill="#01B3F1" fillOpacity="0.18"/>
          <circle r="14" fill="#fff"/>
          <circle r="9" fill="#01B3F1"/>
        </g>
      </svg>

      {/* Status bar */}
      <div style={{ position: "absolute", top: 0, left: 0, right: 0, height: 54, display: "flex", alignItems: "flex-end", justifyContent: "space-between", padding: "0 28px 8px", color: "#fff", fontSize: 15, fontWeight: 600, textShadow: "0 1px 2px rgba(0,0,0,.4)" }}>
        <span>9:41</span>
        <span style={{ fontSize: 12 }}>●●●● 5G ▮</span>
      </div>

      {/* Search bar */}
      <div style={{ position: "absolute", top: 66, left: 16, right: 16, background: "rgba(255,255,255,.95)", backdropFilter: "blur(8px)", borderRadius: 14, padding: "10px 14px", display: "flex", alignItems: "center", gap: 10, fontSize: 13, color: "#6B6F7E", boxShadow: "0 4px 14px rgba(0,0,0,.08)" }}>
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.35-4.35"/></svg>
        <span>Search address or owner…</span>
      </div>

      {/* Tool rail */}
      <div style={{ position: "absolute", right: 14, top: 160, display: "grid", gap: 8 }}>
        {["layers","circle","pin","measure","offline"].map((t,i) => (
          <div key={i} style={{ width: 40, height: 40, borderRadius: 12, background: "#fff", boxShadow: "0 2px 8px rgba(0,0,0,.1)", display: "grid", placeItems: "center", color: "#0E1220", fontSize: 10, fontWeight: 600 }}>
            {t === "circle" ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeDasharray="3 3"><circle cx="12" cy="12" r="8"/></svg> :
             t === "pin" ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M12 22s-8-7-8-13a8 8 0 1116 0c0 6-8 13-8 13z"/><circle cx="12" cy="9" r="3"/></svg> :
             t === "layers" ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M12 2L2 8l10 6 10-6-10-6zM2 16l10 6 10-6M2 12l10 6 10-6"/></svg> :
             t === "measure" ? <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M2 16L16 2l6 6L8 22z"/></svg> :
             <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/><path d="M3 12h18"/></svg>}
          </div>
        ))}
      </div>

      {/* Bottom card */}
      <div style={{ position: "absolute", left: 16, right: 16, bottom: 28, background: "#fff", borderRadius: 18, padding: 14, boxShadow: "0 8px 28px rgba(0,0,0,.12)" }}>
        <div style={{ fontSize: 10, color: "#6B6F7E", fontFamily: "'JetBrains Mono',monospace", letterSpacing: ".08em", textTransform: "uppercase" }}>Parcel · 0.45 ac</div>
        <div style={{ fontSize: 15, fontWeight: 600, color: "#0E1220", marginTop: 2 }}>Stephany Bryan</div>
        <div style={{ fontSize: 12, color: "#6B6F7E", marginTop: 2 }}>4291 Willow Creek Rd</div>
        <div style={{ display: "flex", gap: 12, marginTop: 10, fontSize: 11, color: "#4F69A3", fontWeight: 500 }}>
          <span>Owner details</span>
          <span style={{ color: "#D1D5DB" }}>·</span>
          <span>Save pin</span>
          <span style={{ color: "#D1D5DB" }}>·</span>
          <span>Share</span>
        </div>
      </div>
    </div>
  );
}

// Screen 2: Circle an area
function ScreenCircle() {
  return (
    <div style={{ position: "relative", width: "100%", height: "100%", fontFamily: "'SF Pro Text', -apple-system, sans-serif" }}>
      <svg viewBox="0 0 393 852" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0 }}>
        <defs>
          <pattern id="forest2" width="18" height="18" patternUnits="userSpaceOnUse">
            <rect width="18" height="18" fill="#3f5a3a"/>
            <circle cx="4" cy="4" r="2.2" fill="#4a6a42"/>
            <circle cx="12" cy="10" r="2.6" fill="#354c32"/>
          </pattern>
          <pattern id="field3" width="24" height="24" patternUnits="userSpaceOnUse">
            <rect width="24" height="24" fill="#b9b884"/>
          </pattern>
        </defs>
        <rect width="393" height="852" fill="#8a9668"/>
        <polygon points="0,150 120,120 200,220 80,280" fill="url(#field3)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="120,120 280,100 300,220 200,220" fill="url(#forest2)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="280,100 393,110 393,260 300,220" fill="url(#field3)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="80,280 200,220 300,220 280,400 150,420" fill="url(#field3)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="300,220 393,260 393,440 280,400" fill="url(#forest2)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="0,280 80,280 150,420 0,480" fill="url(#forest2)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="0,480 150,420 280,400 393,440 393,620 0,640" fill="url(#field3)" stroke="#fff" strokeWidth="1.2"/>
        <polygon points="0,640 393,620 393,852 0,852" fill="url(#forest2)" stroke="#fff" strokeWidth="1.2"/>
        {/* Circled region */}
        <circle cx="196" cy="340" r="130" fill="#01B3F1" fillOpacity="0.12" stroke="#01B3F1" strokeWidth="2.5" strokeDasharray="6 5"/>
        {/* Highlight pins inside */}
        <circle cx="150" cy="290" r="6" fill="#DE1E20"/>
        <circle cx="210" cy="260" r="6" fill="#DE1E20"/>
        <circle cx="270" cy="320" r="6" fill="#DE1E20"/>
        <circle cx="180" cy="380" r="6" fill="#DE1E20"/>
        <circle cx="250" cy="420" r="6" fill="#DE1E20"/>
      </svg>

      <div style={{ position: "absolute", top: 0, left: 0, right: 0, height: 54, display: "flex", alignItems: "flex-end", justifyContent: "space-between", padding: "0 28px 8px", color: "#fff", fontSize: 15, fontWeight: 600, textShadow: "0 1px 2px rgba(0,0,0,.4)" }}>
        <span>9:41</span>
        <span style={{ fontSize: 12 }}>●●●● 5G ▮</span>
      </div>

      {/* Bottom sheet — properties list */}
      <div style={{ position: "absolute", left: 0, right: 0, bottom: 0, background: "#fff", borderRadius: "22px 22px 0 0", padding: "14px 16px 24px", boxShadow: "0 -8px 28px rgba(0,0,0,.12)", maxHeight: 360 }}>
        <div style={{ width: 40, height: 4, background: "#E5E7EB", borderRadius: 99, margin: "0 auto 14px" }}/>
        <div style={{ display: "flex", alignItems: "baseline", justifyContent: "space-between", marginBottom: 12 }}>
          <div>
            <div style={{ fontSize: 17, fontWeight: 600, color: "#0E1220" }}>5 properties</div>
            <div style={{ fontSize: 11, color: "#6B6F7E", fontFamily: "'JetBrains Mono',monospace", marginTop: 2 }}>IN SELECTED AREA · 12.4 ac</div>
          </div>
          <div style={{ fontSize: 12, color: "#4F69A3", fontWeight: 500 }}>Export</div>
        </div>
        {[
          { name: "J. Morrison", addr: "2104 Ridge Line Rd", ac: "3.1" },
          { name: "Kestrel Holdings LLC", addr: "2140 Ridge Line Rd", ac: "2.8" },
          { name: "D. & M. Alvarez", addr: "2180 Ridge Line Rd", ac: "1.9" },
        ].map((p,i) => (
          <div key={i} style={{ padding: "10px 0", borderTop: i === 0 ? "none" : "1px solid #F0EFE9", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
            <div>
              <div style={{ fontSize: 14, fontWeight: 500, color: "#0E1220" }}>{p.name}</div>
              <div style={{ fontSize: 12, color: "#6B6F7E" }}>{p.addr}</div>
            </div>
            <div style={{ fontSize: 11, color: "#6B6F7E", fontFamily: "'JetBrains Mono',monospace" }}>{p.ac} ac</div>
          </div>
        ))}
      </div>
    </div>
  );
}

// Screen 3: Pin detail / property info
function ScreenDetail() {
  return (
    <div style={{ position: "relative", width: "100%", height: "100%", background: "#FAF8F4", fontFamily: "'SF Pro Text', -apple-system, sans-serif" }}>
      <div style={{ position: "absolute", top: 0, left: 0, right: 0, height: 54, display: "flex", alignItems: "flex-end", justifyContent: "space-between", padding: "0 28px 8px", color: "#0E1220", fontSize: 15, fontWeight: 600 }}>
        <span>9:41</span>
        <span style={{ fontSize: 12 }}>●●●● 5G ▮</span>
      </div>
      {/* Mini map top */}
      <div style={{ position: "absolute", top: 54, left: 0, right: 0, height: 230 }}>
        <svg viewBox="0 0 393 230" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
          <rect width="393" height="230" fill="#8a9668"/>
          <polygon points="60,40 240,30 280,150 80,170" fill="#c5a868" stroke="#fff" strokeWidth="2"/>
          <polygon points="280,150 240,30 393,20 393,180" fill="#3f5a3a" stroke="#fff" strokeWidth="2"/>
          <circle cx="170" cy="110" r="8" fill="#DE1E20"/>
        </svg>
        <div style={{ position: "absolute", top: 12, left: 14, background: "#fff", borderRadius: 10, padding: "6px 10px", fontSize: 11, fontWeight: 600, display: "flex", alignItems: "center", gap: 6, boxShadow: "0 2px 8px rgba(0,0,0,.08)" }}>
          <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M15 18l-6-6 6-6"/></svg>
          Back
        </div>
      </div>
      {/* Detail card */}
      <div style={{ position: "absolute", top: 260, left: 16, right: 16, background: "#fff", borderRadius: 18, padding: 20, boxShadow: "0 4px 14px rgba(0,0,0,.04)" }}>
        <div style={{ fontSize: 11, color: "#6B6F7E", fontFamily: "'JetBrains Mono',monospace", textTransform: "uppercase", letterSpacing: ".08em" }}>Parcel 014-227-031</div>
        <div style={{ fontSize: 22, fontWeight: 600, color: "#0E1220", marginTop: 4, fontFamily: "'Instrument Serif',serif" }}>Stephany Bryan</div>
        <div style={{ fontSize: 13, color: "#6B6F7E", marginTop: 2 }}>4291 Willow Creek Rd · Brazos County</div>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14, marginTop: 16 }}>
          {[
            ["Acreage","24.8 ac"],
            ["Last sale","Apr 2022"],
            ["Assessed","$312,400"],
            ["Zoning","AG-2"],
          ].map(([k,v]) => (
            <div key={k}>
              <div style={{ fontSize: 10, color: "#6B6F7E", fontFamily: "'JetBrains Mono',monospace", textTransform: "uppercase", letterSpacing: ".08em" }}>{k}</div>
              <div style={{ fontSize: 15, color: "#0E1220", fontWeight: 500, marginTop: 2 }}>{v}</div>
            </div>
          ))}
        </div>
        <div style={{ marginTop: 18, padding: "10px 12px", background: "#F2EFE7", borderRadius: 10, fontSize: 12, color: "#2B2F3D", display: "flex", alignItems: "center", gap: 8 }}>
          <span style={{ width: 8, height: 8, borderRadius: 99, background: "#61B39E" }}/>
          Owner contact available
        </div>
      </div>
      {/* CTA */}
      <div style={{ position: "absolute", left: 16, right: 16, bottom: 36, display: "grid", gridTemplateColumns: "1fr 1fr", gap: 10 }}>
        <div style={{ padding: 14, background: "#fff", border: "1px solid #E5E7EB", borderRadius: 14, fontSize: 14, fontWeight: 500, color: "#0E1220", textAlign: "center" }}>Save pin</div>
        <div style={{ padding: 14, background: "#0E1220", borderRadius: 14, fontSize: 14, fontWeight: 500, color: "#fff", textAlign: "center" }}>Contact owner</div>
      </div>
    </div>
  );
}

function PhoneMockup({ screen = "cycle", width = 340 }) {
  const [idx, setIdx] = useState(0);
  const screens = [<ScreenMap key="m"/>, <ScreenCircle key="c"/>, <ScreenDetail key="d"/>];
  useEffect(() => {
    if (screen !== "cycle") return;
    const t = setInterval(() => setIdx(i => (i + 1) % screens.length), 3800);
    return () => clearInterval(t);
  }, [screen]);

  const current = screen === "cycle" ? idx :
    screen === "map" ? 0 : screen === "circle" ? 1 : 2;

  return (
    <div style={{ position: "relative", display: "flex", flexDirection: "column", alignItems: "center", gap: 18 }}>
      <PhoneFrame width={width}>
        <div style={{ position: "relative", width: "100%", height: "100%" }}>
          {screens.map((s, i) => (
            <div key={i} style={{
              position: "absolute", inset: 0,
              opacity: i === current ? 1 : 0,
              transition: "opacity .6s ease",
            }}>{s}</div>
          ))}
        </div>
      </PhoneFrame>
      {screen === "cycle" && (
        <div style={{ display: "flex", gap: 6 }}>
          {screens.map((_,i) => (
            <button key={i} onClick={() => setIdx(i)} style={{
              width: i === current ? 24 : 6, height: 6, borderRadius: 99,
              background: i === current ? "var(--ink)" : "var(--line)",
              border: "none", padding: 0, cursor: "pointer",
              transition: "all .3s",
            }}/>
          ))}
        </div>
      )}
    </div>
  );
}

window.PhoneMockup = PhoneMockup;
window.ScreenMap = ScreenMap;
window.ScreenCircle = ScreenCircle;
window.ScreenDetail = ScreenDetail;
window.PhoneFrame = PhoneFrame;
