/* global React */
const { useEffect, useRef } = React;

/* ──────────────────────────────────────────────────────────────────────
 *  Phone mockup with bespoke per-app screens
 * ────────────────────────────────────────────────────────────────────── */
function PhoneMock({ app }) {
  return (
    <div className="phone">
      <span className="punch"></span>
      <div className="screen" style={{ background: app.bg || "#111" }}>
        {app.screen}
      </div>
    </div>
  );
}

/* Each screen renderer is intentionally simple — boxes/grids/lines only. */
const ScreenSudoku = () => {
  const cells = [
    [5,3,0,0,7,0,0,0,2],
    [6,0,0,1,9,5,0,0,0],
    [0,9,8,0,0,0,0,6,0],
    [8,0,0,0,6,0,0,0,3],
    [4,0,0,8,0,3,0,0,1],
    [7,0,0,0,2,0,0,0,6],
    [0,6,0,0,0,0,2,8,0],
    [0,0,0,4,1,9,0,0,5],
    [0,0,0,0,8,0,0,7,9],
  ];
  return (
    <div style={{
      background: "#FAF6EC", color: "#1F1A14",
      width: "100%", height: "100%", padding: "20px 10px",
      display: "flex", flexDirection: "column", gap: 10,
      fontFamily: "ui-monospace, monospace",
    }}>
      <div style={{ fontSize: 9, letterSpacing: 1, opacity: .55, textAlign: "center" }}>FUN SUDOKU · EASY</div>
      <div style={{
        display: "grid",
        gridTemplateColumns: "repeat(9,1fr)",
        gap: 1,
        background: "#1F1A14",
        padding: 2,
        border: "2px solid #1F1A14",
      }}>
        {cells.flat().map((n,i) => {
          const row = Math.floor(i/9), col = i%9;
          const thickB = (row+1) % 3 === 0 && row !== 8;
          const thickR = (col+1) % 3 === 0 && col !== 8;
          return (
            <div key={i} style={{
              aspectRatio: 1,
              background: n ? "#FAF6EC" : "#FFF",
              color: n === 5 || n === 8 ? "#C25B3F" : "#1F1A14",
              fontSize: 9, fontWeight: 600,
              display: "grid", placeItems: "center",
              borderRight: thickR ? "1.5px solid #1F1A14" : "0",
              borderBottom: thickB ? "1.5px solid #1F1A14" : "0",
            }}>{n || ""}</div>
          );
        })}
      </div>
      <div style={{ display: "flex", gap: 4, justifyContent: "center", marginTop: 4 }}>
        {[1,2,3,4,5,6,7,8,9].map(n => (
          <div key={n} style={{
            width: 12, height: 14,
            background: n === 7 ? "#C25B3F" : "transparent",
            color: n === 7 ? "#FAF6EC" : "#1F1A14",
            fontSize: 9, fontWeight: 600,
            display: "grid", placeItems: "center",
            border: "1px solid #1F1A14", borderRadius: 2,
          }}>{n}</div>
        ))}
      </div>
    </div>
  );
};

const ScreenGravity = () => (
  <div style={{
    background: "radial-gradient(circle at 50% 70%, #2A1B3D 0%, #0A0518 100%)",
    width: "100%", height: "100%", position: "relative", overflow: "hidden",
    color: "#fff", fontFamily: "ui-monospace, monospace",
  }}>
    <div style={{ position: "absolute", top: 18, left: 14, fontSize: 9, opacity: .65, letterSpacing: 1 }}>LV 07</div>
    <div style={{ position: "absolute", top: 18, right: 14, fontSize: 9, opacity: .65, letterSpacing: 1 }}>×3</div>
    {/* stars */}
    {[[20,40],[80,30],[140,80],[40,120],[120,140],[60,200],[100,260]].map(([x,y],i) => (
      <div key={i} style={{
        position: "absolute", left: x, top: y,
        width: 2, height: 2, borderRadius: 50,
        background: "#fff", opacity: .65,
      }}/>
    ))}
    {/* orbit ellipses */}
    <svg viewBox="0 0 160 320" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
      <ellipse cx="80" cy="220" rx="70" ry="36" fill="none" stroke="#C25B3F" strokeWidth="0.8" opacity=".55" strokeDasharray="3 3"/>
      <ellipse cx="80" cy="220" rx="50" ry="22" fill="none" stroke="#D9A441" strokeWidth="0.8" opacity=".5"/>
      <circle cx="80" cy="220" r="14" fill="#1F1A14" stroke="#C25B3F" strokeWidth="1.2"/>
      <circle cx="148" cy="220" r="3" fill="#D9A441"/>
      <circle cx="118" cy="200" r="2.5" fill="#fff"/>
    </svg>
    <div style={{ position: "absolute", bottom: 14, left: 0, right: 0, textAlign: "center", fontSize: 16, fontWeight: 700, letterSpacing: 2 }}>
      00:42
    </div>
  </div>
);

const ScreenTracker = () => {
  const days = Array.from({ length: 49 }, (_, i) => {
    const r = (i * 13 + 7) % 5;
    return r;
  });
  const palette = ["#EAE2D2", "#E4C4B0", "#D89880", "#C25B3F", "#8B3F2A"];
  return (
    <div style={{
      background: "#FAF6EC", color: "#1F1A14",
      width: "100%", height: "100%", padding: "20px 14px",
      display: "flex", flexDirection: "column", gap: 12,
      fontFamily: "var(--f-body, system-ui)",
    }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline" }}>
        <div style={{ fontFamily: "Georgia, serif", fontSize: 14 }}>October</div>
        <div style={{ fontSize: 8, letterSpacing: 1, opacity: .55, textTransform: "uppercase" }}>14d streak</div>
      </div>
      <div style={{ fontFamily: "Georgia, serif", fontSize: 22, lineHeight: 1.1, fontStyle: "italic" }}>
        Read 30<br/>minutes
      </div>
      <div style={{
        display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: 3,
      }}>
        {days.map((v,i) => (
          <div key={i} style={{
            aspectRatio: 1,
            background: palette[v],
            borderRadius: 3,
          }}/>
        ))}
      </div>
      <div style={{ display: "flex", gap: 6, marginTop: "auto" }}>
        <div style={{ flex: 1, height: 30, borderRadius: 6, background: "#EAE2D2", display: "grid", placeItems: "center", fontSize: 9, letterSpacing: 1 }}>WATER</div>
        <div style={{ flex: 1, height: 30, borderRadius: 6, background: "#1F1A14", color: "#FAF6EC", display: "grid", placeItems: "center", fontSize: 9, letterSpacing: 1 }}>+ NEW</div>
      </div>
    </div>
  );
};

const ScreenFlapzy = () => (
  <div style={{
    background: "linear-gradient(180deg, #87CEEB 0%, #B8E0F0 60%, #DEB887 60%, #C19A6B 100%)",
    width: "100%", height: "100%", position: "relative", overflow: "hidden",
  }}>
    <div style={{ position: "absolute", top: 18, left: 0, right: 0, textAlign: "center", fontFamily: "ui-monospace, monospace", fontSize: 22, fontWeight: 700, color: "#fff", textShadow: "1px 2px 0 #1F1A14" }}>
      27
    </div>
    {/* clouds */}
    <div style={{ position: "absolute", top: 60, left: 14, width: 28, height: 10, background: "#fff", borderRadius: 10, opacity: .85 }}/>
    <div style={{ position: "absolute", top: 100, right: 22, width: 36, height: 12, background: "#fff", borderRadius: 12, opacity: .85 }}/>
    {/* pipes */}
    <div style={{ position: "absolute", top: 0, right: 26, width: 30, height: 90, background: "#5E7C5A", border: "2px solid #1F1A14", borderTop: 0 }}/>
    <div style={{ position: "absolute", top: 90, right: 22, width: 38, height: 8, background: "#5E7C5A", border: "2px solid #1F1A14" }}/>
    <div style={{ position: "absolute", bottom: 60, right: 26, width: 30, height: 70, background: "#5E7C5A", border: "2px solid #1F1A14" }}/>
    <div style={{ position: "absolute", bottom: 130, right: 22, width: 38, height: 8, background: "#5E7C5A", border: "2px solid #1F1A14" }}/>
    {/* bird */}
    <div style={{
      position: "absolute", top: 150, left: 38,
      width: 26, height: 22,
      background: "#F5D547",
      border: "2px solid #1F1A14",
      borderRadius: "50%",
    }}>
      <div style={{ position: "absolute", top: 4, right: 2, width: 6, height: 6, background: "#fff", border: "1.5px solid #1F1A14", borderRadius: 50 }}/>
      <div style={{ position: "absolute", top: 11, right: -4, width: 8, height: 4, background: "#C25B3F", border: "1.5px solid #1F1A14" }}/>
    </div>
  </div>
);

const Screen195 = () => (
  <div style={{
    background: "#FAF6EC", color: "#1F1A14",
    width: "100%", height: "100%", padding: "18px 14px",
    display: "flex", flexDirection: "column", gap: 12,
    fontFamily: "Georgia, serif",
  }}>
    <div style={{ fontSize: 9, letterSpacing: 1, opacity: .6, fontFamily: "ui-monospace, monospace", textTransform: "uppercase" }}>QUESTION 14 / 25</div>
    {/* Flag placeholder — three vertical stripes */}
    <div style={{
      width: "100%", aspectRatio: "5/3",
      borderRadius: 6,
      overflow: "hidden",
      display: "flex",
      border: "1.5px solid #1F1A14",
    }}>
      <div style={{ flex: 1, background: "#0055A4" }}/>
      <div style={{ flex: 1, background: "#fff" }}/>
      <div style={{ flex: 1, background: "#EF4135" }}/>
    </div>
    <div style={{ fontSize: 16, lineHeight: 1.2 }}>
      Which country flies<br/>this flag?
    </div>
    <div style={{ display: "grid", gap: 5, marginTop: "auto" }}>
      {["Italy","France","Netherlands","Russia"].map((c,i) => (
        <div key={c} style={{
          padding: "7px 10px",
          border: "1.5px solid #1F1A14",
          borderRadius: 6,
          fontSize: 11,
          background: c === "France" ? "#C25B3F" : "transparent",
          color: c === "France" ? "#FAF6EC" : "#1F1A14",
          display: "flex", justifyContent: "space-between",
        }}>
          <span>{c}</span>
          <span style={{ fontFamily: "ui-monospace, monospace", opacity: .55 }}>{["A","B","C","D"][i]}</span>
        </div>
      ))}
    </div>
  </div>
);

const ScreenComing = () => (
  <div style={{
    background: "linear-gradient(135deg, #1F1A14 0%, #2A2218 100%)",
    width: "100%", height: "100%",
    display: "grid", placeItems: "center",
    color: "#FAF6EC",
    fontFamily: "ui-monospace, monospace",
    fontSize: 11, letterSpacing: 2,
    position: "relative", overflow: "hidden",
  }}>
    <div style={{
      position: "absolute", inset: 0,
      backgroundImage: "repeating-linear-gradient(45deg, rgba(255,255,255,.04) 0 8px, transparent 8px 16px)",
    }}/>
    <div style={{ textAlign: "center", position: "relative", lineHeight: 1.5 }}>
      ?<br/><br/>
      SOON
    </div>
  </div>
);

window.PhoneMock = PhoneMock;
window.Screens = {
  ScreenSudoku, ScreenGravity, ScreenTracker, ScreenFlapzy, Screen195, ScreenComing
};
