/* global React, M */

// ====================================================================
// C1 — Home Rhythm card · 3 states
// ====================================================================
function RhythmCard({ state = "keep" }) {
  // states: "build" (week 2 of 4), "keep" (week 9), "interrupted"
  if (state === "build") return <RhythmBuildCard/>;
  if (state === "interrupted") return <RhythmInterruptedCard/>;
  return <RhythmKeepCard/>;
}

function CardShell({ children }) {
  return (
    <div style={{ background: "var(--surface-2)", border: "1px solid var(--border-soft)", borderRadius: 14, padding: 22, position: "relative" }}>
      {children}
    </div>
  );
}

function Pill({ filled, faded, outlined, children, sub, lockTag }) {
  let style = { border: "1px solid var(--border)", color: "var(--text-2)", background: "transparent" };
  if (filled) style = { border: "1px solid transparent", color: "#0E0D0B", background: "var(--accent)" };
  if (faded) style = { border: "1px dashed var(--border)", color: "var(--text-3)", background: "transparent", opacity: 0.7 };
  return (
    <div style={{ ...style, padding: "8px 12px", borderRadius: 999, fontSize: 11.5, fontWeight: filled ? 600 : 500, display: "inline-flex", alignItems: "center", gap: 6, whiteSpace: "nowrap" }}>
      {filled && <span style={{ width: 12, height: 12, borderRadius: "50%", background: "rgba(0,0,0,0.18)", display: "inline-flex", alignItems: "center", justifyContent: "center", fontSize: 9 }}>✓</span>}
      <span>{children}</span>
      {sub && <span style={{ opacity: 0.7, fontWeight: 400 }}>{sub}</span>}
      {lockTag && <span className="mono" style={{ fontSize: 8, letterSpacing: "0.12em", marginLeft: 4, opacity: 0.7 }}>{lockTag}</span>}
    </div>
  );
}

function RhythmBuildCard() {
  return (
    <CardShell>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
        <span className="mono" style={{ fontSize: 10, letterSpacing: "0.14em", color: "var(--text-3)", textTransform: "uppercase" }}>Build your Rhythm · week 2 of 4</span>
        <span className="mono" style={{ fontSize: 11, color: "var(--accent)" }}>↗</span>
      </div>
      <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
        <Pill filled>1to1 completed</Pill>
        <Pill outlined>2 requests sent <span className="mono" style={{ fontSize: 10, opacity: 0.7, marginLeft: 4 }}>1 / 2</span></Pill>
        <Pill faded lockTag="UNLOCKS WK 5">Connection given</Pill>
      </div>
      <div style={{ marginTop: 14, display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <span className="muted" style={{ fontSize: 11.5 }}>Halfway through your build phase.</span>
        <span style={{ color: "var(--accent)", fontSize: 12, cursor: "pointer" }}>Read more →</span>
      </div>
    </CardShell>
  );
}

function RhythmKeepCard() {
  return (
    <CardShell>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
        <span className="mono" style={{ fontSize: 10, letterSpacing: "0.14em", color: "var(--text-3)", textTransform: "uppercase" }}>Keep your Rhythm</span>
        <span className="mono" style={{ fontSize: 10, color: "var(--accent)", letterSpacing: "0.12em", textTransform: "uppercase" }}>9 weeks running</span>
      </div>
      <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
        <Pill filled>1to1 completed</Pill>
        <Pill filled>Connection given</Pill>
        <Pill outlined>2 requests sent <span className="mono" style={{ fontSize: 10, opacity: 0.7, marginLeft: 4 }}>1 / 2</span></Pill>
      </div>
      <div style={{ marginTop: 14, display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <span className="muted" style={{ fontSize: 11.5 }}>Connection given to <span className="serif-i" style={{ color: "var(--text)" }}>Eliza Hart</span>.</span>
        <span style={{ color: "var(--accent)", fontSize: 12, cursor: "pointer" }}>Read more →</span>
      </div>
    </CardShell>
  );
}

function RhythmInterruptedCard() {
  return (
    <CardShell>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
        <span className="mono" style={{ fontSize: 10, letterSpacing: "0.14em", color: "var(--text-3)", textTransform: "uppercase" }}>Start your Rhythm · new week</span>
        <span className="mono" style={{ fontSize: 11, color: "var(--text-3)" }}>↗</span>
      </div>
      <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
        <Pill outlined>1to1 completed</Pill>
        <Pill outlined>Connection given</Pill>
        <Pill outlined>2 requests sent</Pill>
      </div>
      <div className="muted" style={{ fontSize: 11.5, marginTop: 14 }}>Last Rhythm held: 8 weeks (ending 27 April).</div>
    </CardShell>
  );
}

function RhythmCardsTriptych() {
  return (
    <div style={{ width: "100%", height: "100%", padding: 28, background: "var(--bg)", color: "var(--text)", display: "flex", flexDirection: "column", gap: 18 }}>
      <div className="mono" style={{ fontSize: 10, letterSpacing: "0.14em", color: "var(--text-3)", textTransform: "uppercase" }}>Home · Rhythm card · three states</div>
      <RhythmBuildCard/>
      <RhythmKeepCard/>
      <RhythmInterruptedCard/>
    </div>
  );
}

// ====================================================================
// C2 — Onboarding Rhythm intro
// ====================================================================
function RhythmOnboardingScreen() {
  const { MScreen, Eyebrow, MStickyCTA, SagePill } = window.M;
  return (
    <MScreen footer={
      <MStickyCTA hint="">
        <SagePill fullWidth>Got it · Take me in</SagePill>
      </MStickyCTA>
    }>
      <div style={{ padding: "32px 28px 0" }}>
        <Eyebrow>One last thing</Eyebrow>
        <h1 className="serif" style={{ fontSize: 42, lineHeight: 1.05, letterSpacing: "-0.02em", margin: "16px 0 0" }}>
          Build your <span className="serif-i" style={{ color: "var(--accent)" }}>Rhythm.</span>
        </h1>
        <p style={{ fontSize: 15.5, lineHeight: 1.55, color: "var(--text-2)", marginTop: 18, textWrap: "pretty" }}>
          Most users do their best work on 1to1 in a weekly cadence. We call it <span className="serif-i" style={{ color: "var(--text)" }}>Rhythm</span> — three small actions per week that, over time, build the kind of network that actually returns calls. We start you on a 4-week build phase so you can find your feet.
        </p>
      </div>

      <div className="perf" style={{ margin: "32px 28px" }}/>

      <div style={{ padding: "0 28px", display: "flex", flexDirection: "column", gap: 12 }}>
        <PillCard n="01" title="Complete 1 1to1" body="Show up, run a great meeting, submit your debrief."/>
        <PillCard n="02" title="Send 2 meeting requests" body="Reach out to 2 people whose Meet Sheets you'd like to meet."/>
        <PillCard n="03" title="Give 1 connection" body="After your first month, introduce someone to someone who can help them." lockTag="UNLOCKS AT WEEK 5"/>
      </div>

      <div style={{ padding: "28px 28px 32px" }}>
        <div className="muted" style={{ fontSize: 12, lineHeight: 1.6 }}>
          We'll send a calm Wednesday status and a Sunday recap. No pressure, no nags.
        </div>
      </div>
    </MScreen>
  );
}

function PillCard({ n, title, body, lockTag }) {
  return (
    <div style={{ display: "flex", alignItems: "flex-start", gap: 14, padding: "16px 18px", borderRadius: 14, background: "var(--surface-2)", border: "1px solid var(--border-soft)", opacity: lockTag ? 0.72 : 1 }}>
      <div style={{ flex: "0 0 32px", height: 32, borderRadius: 16, border: "1px solid " + (lockTag ? "var(--border)" : "var(--accent)"), display: "grid", placeItems: "center" }}>
        <span className="mono" style={{ fontSize: 10.5, color: lockTag ? "var(--text-3)" : "var(--accent)", letterSpacing: "0.04em" }}>{n}</span>
      </div>
      <div style={{ flex: 1 }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 8 }}>
          <span className="serif-i" style={{ fontSize: 17, color: "var(--text)" }}>{title}</span>
          {lockTag && <span className="mono muted-2" style={{ fontSize: 9, letterSpacing: "0.14em", whiteSpace: "nowrap" }}>{lockTag}</span>}
        </div>
        <div className="muted" style={{ fontSize: 12.5, lineHeight: 1.5, marginTop: 4 }}>{body}</div>
      </div>
    </div>
  );
}

// ====================================================================
// C3 — Rhythm detail view
// ====================================================================
function RhythmDetailScreen() {
  const { MScreen, MTopBar, Eyebrow, Glyph } = window.M;

  return (
    <MScreen>
      <MTopBar right={<button style={{ width: 36, height: 36, borderRadius: 18, background: "transparent", border: "none", color: "var(--text-2)", cursor: "pointer" }}><Glyph.Share/></button>}/>

      {/* Hero */}
      <div style={{ padding: "8px 24px 28px" }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline" }}>
          <Eyebrow>Keep your Rhythm</Eyebrow>
          <span className="mono" style={{ fontSize: 10.5, color: "var(--accent)", letterSpacing: "0.12em", textTransform: "uppercase" }}>9 weeks running</span>
        </div>
        <h1 className="serif" style={{ fontSize: 36, lineHeight: 1.05, letterSpacing: "-0.02em", margin: "12px 0 0" }}>
          This week in <span className="serif-i" style={{ color: "var(--accent)" }}>1to1.</span>
        </h1>
      </div>

      {/* Component cards */}
      <div style={{ padding: "0 24px", display: "flex", flexDirection: "column", gap: 12 }}>
        <ComponentCard n="01" eyebrow="01 · 1to1 completed" title="Met with Priya Anand" body="Tuesday 5 May, 8:00 — The Pantry, Robina. Debrief submitted." status="done"/>
        <ComponentCard n="02" eyebrow="02 · Connection given" title="Intro made: James Whitford → Priya Anand" body="Confirmed by Priya within the same week. Counts." status="done"/>
        <ComponentCard n="03" eyebrow="03 · 2 requests sent" title="1 of 2 sent" body="Sent to Devon Park yesterday. One more this week." status="open"/>
      </div>

      {/* History */}
      <div className="perf" style={{ margin: "32px 24px" }}/>
      <div style={{ padding: "0 24px" }}>
        <Eyebrow>Last 12 weeks</Eyebrow>
        <div style={{ display: "flex", gap: 4, marginTop: 16, alignItems: "flex-end", height: 50 }}>
          {Array(12).fill(0).map((_, i) => (
            <div key={i} style={{ flex: 1, height: 28 + (i%4)*5, background: "var(--accent)", opacity: 0.55 + (i/24), borderRadius: 3 }}/>
          ))}
        </div>
        <div style={{ display: "flex", justifyContent: "space-between", marginTop: 8 }}>
          <span className="mono muted-2" style={{ fontSize: 9, letterSpacing: "0.1em" }}>WK 1 · 23 FEB</span>
          <span className="mono muted-2" style={{ fontSize: 9, letterSpacing: "0.1em" }}>WK 12 · THIS WEEK</span>
        </div>
      </div>

      {/* Group context */}
      <div className="perf" style={{ margin: "32px 24px" }}/>
      <div style={{ padding: "0 24px 4px" }}>
        <Eyebrow>In your group this week</Eyebrow>
        <div style={{ marginTop: 14, padding: 18, background: "var(--surface-2)", border: "1px solid var(--border-soft)", borderRadius: 12 }}>
          <div className="serif-i" style={{ fontSize: 18, lineHeight: 1.45 }}>
            <span style={{ color: "var(--accent)" }}>11 of 14</span> members at Rhythm this week — Coast Referral Collective.
          </div>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 14 }}>
            <div style={{ flex: 1, height: 4, background: "var(--surface-3)", borderRadius: 2, overflow: "hidden", marginRight: 14 }}>
              <div style={{ width: `${(11/14)*100}%`, height: "100%", background: "var(--accent)" }}/>
            </div>
            <span style={{ color: "var(--accent)", fontSize: 12, cursor: "pointer", whiteSpace: "nowrap" }}>View group →</span>
          </div>
        </div>
      </div>

      {/* Footer */}
      <div style={{ padding: "30px 24px 40px" }}>
        <div className="muted" style={{ fontSize: 11.5, lineHeight: 1.6 }}>
          Connections only count if confirmed by the person you introduced — they have to confirm within the same week.
        </div>
      </div>
    </MScreen>
  );
}

function ComponentCard({ eyebrow, title, body, status }) {
  const done = status === "done";
  return (
    <div style={{ padding: 18, borderRadius: 12, background: "var(--surface-2)", border: "1px solid " + (done ? "var(--border-soft)" : "var(--border)") }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 10 }}>
        <span className="mono" style={{ fontSize: 10, letterSpacing: "0.14em", color: done ? "var(--accent)" : "var(--text-3)", textTransform: "uppercase" }}>{eyebrow}</span>
        <span style={{ width: 16, height: 16, borderRadius: "50%", border: "1px solid " + (done ? "var(--accent)" : "var(--border)"), display: "grid", placeItems: "center", fontSize: 9, color: "var(--accent)", background: done ? "var(--accent-soft)" : "transparent" }}>{done ? "✓" : ""}</span>
      </div>
      <div className="serif-i" style={{ fontSize: 17, lineHeight: 1.35, color: "var(--text)" }}>{title}</div>
      <div className="muted" style={{ fontSize: 12.5, lineHeight: 1.5, marginTop: 8 }}>{body}</div>
    </div>
  );
}

// ====================================================================
// C4 — Rhythm milestone
// ====================================================================
function RhythmMilestone() {
  const { MScreen, Eyebrow, MStickyCTA, SagePill } = window.M;
  return (
    <MScreen footer={
      <div style={{ padding: "0 28px 32px", display: "flex", gap: 10 }}>
        <SagePill ghost>Share</SagePill>
        <SagePill fullWidth>Onwards</SagePill>
      </div>
    }>
      <div style={{ display: "flex", justifyContent: "flex-end", padding: "16px 20px" }}>
        <button style={{ background: "transparent", border: "none", color: "var(--text-3)", fontSize: 18, cursor: "pointer" }}>✕</button>
      </div>

      <div style={{ display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", padding: "40px 28px 0", textAlign: "center" }}>
        <Eyebrow>Milestone</Eyebrow>
        <h1 className="serif-i" style={{ fontSize: 96, lineHeight: 1, letterSpacing: "-0.03em", margin: "24px 0 0", color: "var(--accent)" }}>
          12 weeks.
        </h1>

        <p style={{ fontSize: 16, lineHeight: 1.55, color: "var(--text-2)", marginTop: 28, maxWidth: 320, textWrap: "pretty" }}>
          Three months of held Rhythm. The compounding starts here.
        </p>

        <div style={{ display: "grid", gridTemplateColumns: "repeat(6, 1fr)", gap: 6, marginTop: 56, width: 200 }}>
          {Array(12).fill(0).map((_, i) => (
            <div key={i} style={{ aspectRatio: "1", background: "var(--accent)", opacity: 0.5 + (i/24), borderRadius: 3 }}/>
          ))}
        </div>

        <div style={{ marginTop: 40 }}>
          <span className="mono muted-2" style={{ fontSize: 10, letterSpacing: "0.14em" }}>NEXT MARKER · 26 WEEKS</span>
        </div>
      </div>
    </MScreen>
  );
}

window.RhythmCard = RhythmCard;
window.RhythmCardsTriptych = RhythmCardsTriptych;
window.RhythmOnboardingScreen = RhythmOnboardingScreen;
window.RhythmDetailScreen = RhythmDetailScreen;
window.RhythmMilestone = RhythmMilestone;
