// Hero — split layout with chat conversation and Ivan's phone

function ChatAvatar({ kind }) {
  if (kind === "ai") {
    return (
      <div className="cv-av cv-av-ai" aria-hidden="true">
        <SunMark size={18} />
      </div>
    );
  }
  return (
    <div className="cv-av cv-av-cust" aria-hidden="true">
      M
    </div>
  );
}

function ChatPhotoTile({ label }) {
  return (
    <div className="cv-photo">
      <div className="cv-photo-stripes" />
      <span className="mono">{label}</span>
    </div>
  );
}

function HeroChat() {
  const { t } = useLang();
  const c = t.hero.chat;
  return (
    <div className="cv-shell">
      <div className="cv-head">
        <div className="cv-head-row">
          <ChatAvatar kind="ai" />
          <div className="stack">
            <div className="cv-head-name">{c.headerName}</div>
            <div className="cv-head-sub">
              <span className="cv-dot" /> {c.headerSub}
            </div>
          </div>
        </div>
        <div className="cv-head-tag mono">{c.tag}</div>
      </div>

      <div className="cv-body">
        <div className="cv-msg cv-msg-ai">
          <ChatAvatar kind="ai" />
          <div className="cv-bubble cv-bubble-ai">{c.m1}</div>
        </div>
        <div className="cv-msg cv-msg-cust">
          <div className="cv-bubble cv-bubble-cust">{c.m2}</div>
        </div>
        <div className="cv-msg cv-msg-ai">
          <ChatAvatar kind="ai" />
          <div className="cv-bubble cv-bubble-ai">{c.m3}</div>
        </div>
        <div className="cv-msg cv-msg-cust">
          <div className="cv-attach">
            <ChatPhotoTile label="IMG_0421" />
            <ChatPhotoTile label="IMG_0422" />
            <ChatPhotoTile label="IMG_0423" />
          </div>
        </div>
        <div className="cv-msg cv-msg-ai">
          <ChatAvatar kind="ai" />
          <div className="cv-bubble cv-bubble-ai cv-estimate">
            <div className="cv-est-head">
              <span className="eyebrow" style={{ color: "var(--ink)" }}>
                {c.m4}
              </span>
              <span className="mono cv-est-id">#SB-4421</span>
            </div>
            <div className="cv-est-line">
              <span>{c.film}</span>
              <span className="mono tabular">{c.sqft}</span>
            </div>
            <div className="cv-est-line">
              <span>{c.labor}</span>
              <span className="mono tabular">{c.when}</span>
            </div>
            <div className="cv-est-line cv-est-total">
              <span>{c.total}</span>
              <span className="mono tabular">{c.price}</span>
            </div>
            <div className="cv-est-foot mono">{c.sending}</div>
          </div>
        </div>
        <div className="cv-typing" aria-hidden="true">
          <ChatAvatar kind="ai" />
          <div className="cv-bubble cv-bubble-ai cv-typing-bubble">
            <span />
            <span />
            <span />
          </div>
        </div>
      </div>

      <div className="cv-input">
        <div className="cv-input-icons">
          <svg
            width="18"
            height="18"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="1.8"
            strokeLinecap="round"
            strokeLinejoin="round"
          >
            <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
            <polyline points="17 8 12 3 7 8" />
            <line x1="12" y1="3" x2="12" y2="15" />
          </svg>
        </div>
        <input placeholder={c.inputPlaceholder} readOnly />
        <button className="cv-send" aria-label="Send">
          <svg
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
            strokeLinejoin="round"
          >
            <line x1="22" y1="2" x2="11" y2="13" />
            <polygon points="22 2 15 22 11 13 2 9 22 2" />
          </svg>
        </button>
      </div>

      <style>{`
        .cv-shell {
          background: var(--card);
          border: 1px solid var(--hairline);
          border-radius: var(--r-xl);
          box-shadow: var(--shadow);
          overflow: hidden;
          display: flex; flex-direction: column;
          width: 100%; max-width: 460px;
        }
        .cv-head {
          padding: 16px 20px;
          display: flex; align-items: center; justify-content: space-between;
          border-bottom: 1px solid var(--hairline);
          background: linear-gradient(to bottom, color-mix(in srgb, var(--bg-2) 50%, transparent), transparent);
        }
        .cv-head-row { display: inline-flex; gap: 12px; align-items: center; }
        .cv-head-name { font-weight: 600; font-size: 14.5px; }
        .cv-head-sub { font-size: 12px; color: var(--ink-soft); display: inline-flex; align-items: center; gap: 6px; }
        .cv-dot { width: 7px; height: 7px; border-radius: 50%; background: #2db579; box-shadow: 0 0 0 3px color-mix(in srgb, #2db579 25%, transparent); }
        .cv-head-tag {
          font-size: 11px; letter-spacing: 0.16em;
          color: var(--ink-soft);
          padding: 4px 8px;
          border: 1px solid var(--hairline-strong);
          border-radius: 999px;
        }
        .cv-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
        .cv-av {
          width: 28px; height: 28px; flex-shrink: 0;
          border-radius: 50%;
          display: inline-flex; align-items: center; justify-content: center;
          font-size: 12px; font-weight: 600;
          background: var(--ink);
        }
        .cv-av-cust { background: var(--bg-2); color: var(--ink); border: 1px solid var(--hairline); }
        .cv-msg { display: flex; gap: 10px; align-items: flex-end; }
        .cv-msg-cust { justify-content: flex-end; }
        .cv-bubble {
          max-width: 78%; padding: 11px 14px;
          border-radius: 18px; font-size: 14.5px; line-height: 1.4;
        }
        .cv-bubble-ai { background: var(--bg-2); color: var(--ink); border-bottom-left-radius: 6px; }
        .cv-bubble-cust { background: var(--ink); color: var(--bg); border-bottom-right-radius: 6px; }
        [data-theme="dark"] .cv-bubble-cust { background: var(--sun); color: #15110a; }
        .cv-attach { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; max-width: 78%; }
        .cv-photo {
          aspect-ratio: 4 / 3; border-radius: 12px;
          background: var(--glass); color: rgba(255,255,255,0.7);
          font-size: 9.5px; letter-spacing: 0.1em;
          display: flex; align-items: flex-end;
          padding: 6px; position: relative; overflow: hidden;
        }
        .cv-photo-stripes {
          position: absolute; inset: 0;
          background:
            linear-gradient(135deg, transparent 49%, rgba(255,255,255,0.06) 49%, rgba(255,255,255,0.06) 51%, transparent 51%) 0 0 / 14px 14px,
            linear-gradient(180deg, color-mix(in srgb, var(--sun) 30%, transparent), transparent 60%);
        }
        .cv-photo span { position: relative; z-index: 1; }
        .cv-estimate {
          background: var(--bg);
          border: 1px solid var(--hairline-strong);
          padding: 14px; width: 100%; max-width: 360px;
        }
        .cv-est-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
        .cv-est-id { font-size: 11px; color: var(--ink-soft); }
        .cv-est-line {
          display: flex; align-items: center; justify-content: space-between;
          padding: 8px 0; border-top: 1px dashed var(--hairline);
          font-size: 13.5px;
        }
        .cv-est-line:first-of-type { border-top: 0; }
        .cv-est-total {
          font-weight: 600; font-size: 16px; margin-top: 2px;
          padding-top: 12px; border-top: 1px solid var(--ink);
        }
        .cv-est-foot { margin-top: 10px; font-size: 11px; color: var(--ink-soft); letter-spacing: 0.04em; }
        .cv-typing { display: flex; gap: 10px; align-items: flex-end; }
        .cv-typing-bubble { padding: 12px 14px; display: inline-flex; gap: 4px; }
        .cv-typing-bubble span {
          width: 6px; height: 6px; border-radius: 50%; background: var(--ink-soft);
          animation: cv-blink 1.2s infinite;
        }
        .cv-typing-bubble span:nth-child(2) { animation-delay: .15s; }
        .cv-typing-bubble span:nth-child(3) { animation-delay: .3s; }
        @keyframes cv-blink {
          0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
          30% { opacity: 1; transform: translateY(-2px); }
        }
        .cv-input {
          padding: 12px 16px; display: flex; gap: 10px; align-items: center;
          border-top: 1px solid var(--hairline);
        }
        .cv-input-icons { color: var(--ink-soft); display: inline-flex; }
        .cv-input input {
          flex: 1; border: 0; outline: 0; background: transparent;
          font: inherit; color: var(--ink); padding: 6px 0;
        }
        .cv-send {
          width: 36px; height: 36px; border-radius: 50%;
          background: var(--ink); color: var(--bg); border: 0;
          display: inline-flex; align-items: center; justify-content: center;
        }
        [data-theme="dark"] .cv-send { background: var(--sun); color: #15110a; }
      `}</style>
    </div>
  );
}

function IvanReviewPhone() {
  const { t } = useLang();
  const h = t.hero;
  return (
    <div className="ivan-phone-wrap">
      <div className="ivan-phone-shadow" />
      <IOSDevice width={320} height={650} dark={false}>
        <div
          style={{
            padding: "52px 16px 18px",
            minHeight: "100%",
            background: "#ffffff",
          }}
        >
          {/* status pill */}
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "space-between",
              padding: "8px 12px",
              marginBottom: 14,
              background: "#fef4dc",
              border: "1px solid #f0d077",
              borderRadius: 999,
            }}
          >
            <div
              style={{ display: "inline-flex", alignItems: "center", gap: 6 }}
            >
              <span
                style={{
                  width: 7,
                  height: 7,
                  borderRadius: "50%",
                  background: "#c97a14",
                  boxShadow: "0 0 0 3px rgba(201,122,20,0.2)",
                }}
              />
              <span
                style={{
                  fontSize: 10.5,
                  fontFamily: "IBM Plex Mono, monospace",
                  letterSpacing: "0.14em",
                  color: "#8a5a08",
                  textTransform: "uppercase",
                  fontWeight: 600,
                }}
              >
                {h.phoneTag}
              </span>
            </div>
            <span
              style={{
                fontSize: 10.5,
                fontFamily: "IBM Plex Mono, monospace",
                color: "#8a5a08",
              }}
            >
              {h.phoneTimeAgo}
            </span>
          </div>

          <div
            style={{
              fontFamily: "'Bricolage Grotesque', serif",
              fontSize: 24,
              fontWeight: 600,
              lineHeight: 1.05,
              letterSpacing: "-0.02em",
              marginBottom: 6,
              color: "#0e0f12",
            }}
          >
            {h.phoneTitle}
          </div>
          <div
            style={{
              fontSize: 12.5,
              color: "#4a4d54",
              marginBottom: 16,
              lineHeight: 1.4,
            }}
          >
            {h.phoneMeta}
          </div>

          {/* big total */}
          <div
            style={{
              background: "#0e0f12",
              color: "#fff",
              borderRadius: 16,
              padding: "16px 18px",
              marginBottom: 12,
              position: "relative",
              overflow: "hidden",
            }}
          >
            <div
              style={{
                position: "absolute",
                top: -30,
                right: -30,
                width: 100,
                height: 100,
                borderRadius: "50%",
                background:
                  "radial-gradient(circle, rgba(244,169,58,0.5), transparent 70%)",
              }}
            />
            <div
              style={{
                fontSize: 9.5,
                fontFamily: "IBM Plex Mono, monospace",
                letterSpacing: "0.18em",
                color: "#a8a8b0",
                marginBottom: 4,
                position: "relative",
              }}
            >
              {h.phoneFromTotal}
            </div>
            <div
              style={{
                fontFamily: "'Bricolage Grotesque', serif",
                fontSize: 30,
                fontWeight: 600,
                letterSpacing: "-0.03em",
                color: "#f4a93a",
                position: "relative",
              }}
            >
              {h.chat.price}
            </div>
            <div
              style={{
                display: "flex",
                justifyContent: "space-between",
                fontSize: 11.5,
                color: "#c4c4cc",
                marginTop: 8,
                borderTop: "1px solid rgba(255,255,255,0.12)",
                paddingTop: 8,
                position: "relative",
              }}
            >
              <span>{h.chat.film}</span>
              <span style={{ fontFamily: "IBM Plex Mono, monospace" }}>
                {h.chat.sqft}
              </span>
            </div>
          </div>

          {/* AI note — high contrast */}
          <div
            style={{
              fontSize: 12,
              color: "#1c1d23",
              marginBottom: 14,
              padding: "10px 12px",
              background: "#f4f3ee",
              borderRadius: 10,
              border: "1px solid #d8d4c4",
              lineHeight: 1.4,
            }}
          >
            <span
              style={{
                fontFamily: "IBM Plex Mono, monospace",
                letterSpacing: "0.1em",
                textTransform: "uppercase",
                fontSize: 9.5,
                color: "#8a6a14",
                fontWeight: 600,
              }}
            >
              {h.phoneAiNote}
            </span>
            {h.phoneAiBody}
          </div>

          {/* photo strip */}
          <div
            style={{
              display: "grid",
              gridTemplateColumns: "repeat(3, 1fr)",
              gap: 6,
              marginBottom: 16,
            }}
          >
            {[1, 2, 3].map((i) => (
              <div
                key={i}
                style={{
                  aspectRatio: "1",
                  borderRadius: 8,
                  background: "linear-gradient(135deg, #3a4150, #5a6070)",
                  position: "relative",
                  overflow: "hidden",
                }}
              >
                <div
                  style={{
                    position: "absolute",
                    inset: 0,
                    background:
                      "linear-gradient(180deg, rgba(244,169,58,0.35), transparent 60%)",
                  }}
                />
              </div>
            ))}
          </div>

          {/* big approve, small edit, tertiary pass */}
          <button
            style={{
              width: "100%",
              padding: "14px",
              borderRadius: 14,
              background: "#0e0f12",
              color: "#ffffff",
              border: 0,
              fontWeight: 600,
              fontSize: 15,
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              gap: 8,
              marginBottom: 8,
              boxShadow: "0 6px 14px rgba(14,15,18,0.25)",
            }}
          >
            <svg
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="2.5"
              strokeLinecap="round"
              strokeLinejoin="round"
            >
              <polyline points="20 6 9 17 4 12" />
            </svg>
            {h.phoneApprove}
          </button>
          <div style={{ display: "flex", gap: 8 }}>
            <button
              style={{
                flex: 1,
                padding: "10px",
                borderRadius: 12,
                background: "#ffffff",
                border: "1px solid #d2cec0",
                color: "#1c1d23",
                fontSize: 13,
                fontWeight: 500,
              }}
            >
              {h.phoneEdit}
            </button>
            <button
              style={{
                flex: 1,
                padding: "10px",
                borderRadius: 12,
                background: "transparent",
                border: "1px solid transparent",
                color: "#8a5a5a",
                fontSize: 13,
                fontWeight: 500,
              }}
            >
              {h.phoneReject}
            </button>
          </div>
        </div>
      </IOSDevice>

      <style>{`
        .ivan-phone-wrap { position: relative; transform: rotate(4deg); transition: transform .4s ease; }
        .ivan-phone-wrap:hover { transform: rotate(0deg) translateY(-4px); }
        .ivan-phone-shadow {
          position: absolute; left: 10%; right: 10%; bottom: -30px; height: 40px;
          background: rgba(20, 20, 28, 0.25); filter: blur(30px);
          border-radius: 50%; z-index: -1;
        }
      `}</style>
    </div>
  );
}

function HeroBadge() {
  const { t } = useLang();
  return (
    <div className="hero-badge">
      <span className="hero-badge-pulse" />
      <span className="mono">{t.hero.badge}</span>
      <span>{t.hero.badgeText}</span>
    </div>
  );
}

function Hero({ onBookClick, layout }) {
  const { t } = useLang();
  return (
    <section className="hero section" id="top">
      <div className="rays" aria-hidden="true" />
      <div className="wrap hero-wrap" data-layout={layout}>
        <div className="hero-text">
          <HeroBadge />
          <h1 className="hero-h1">
            {t.hero.h1a}
            <br />
            <em className="hero-em">{t.hero.h1b}</em>
            <br />
            <span className="hero-h1-soft">{t.hero.h1c}</span>
          </h1>
          <p className="hero-sub">{t.hero.sub}</p>
          <div className="hero-cta">
            <button className="btn btn-primary" onClick={onBookClick}>
              {t.hero.ctaPrimary} <Arrow />
            </button>
            <a href="#work" className="btn btn-ghost">
              {t.hero.ctaSecondary}
            </a>
          </div>
          <div className="hero-stats">
            {t.hero.stats.map((s, i) => (
              <Stat key={i} n={s.n} l={s.l} />
            ))}
          </div>
        </div>
        <div className="hero-stage">
          <HeroChat />
          <div className="hero-phone-slot">
            <IvanReviewPhone />
            <div className="hero-phone-cap mono">{t.hero.phoneCaption}</div>
          </div>
        </div>
      </div>
      <style>{`
        .hero { padding-top: 16px; padding-bottom: 40px; }
        @media (min-width: 880px) { .hero { padding-top: 16px; padding-bottom: 48px; } }
        .hero-wrap {
          display: grid; grid-template-columns: 1fr;
          gap: 56px; align-items: start;
        }
        @media (min-width: 1000px) {
          .hero-wrap { grid-template-columns: 1.25fr 0.85fr; gap: 72px;  }
        }
        .hero-wrap[data-layout="centered"] {
          grid-template-columns: 1fr; text-align: center; justify-items: center;
        }
        .hero-wrap[data-layout="centered"] .hero-cta { justify-content: center; }
        .hero-wrap[data-layout="centered"] .hero-stats { justify-content: center; }
        .hero-wrap[data-layout="centered"] .hero-stage { width: 100%; max-width: 720px; }
        .hero-wrap[data-layout="centered"] .hero-phone-slot { display: none; }
        .hero-h1 {
          font-size: clamp(40px, 5vw, 84px); font-weight: 600;
          line-height: 1.04; letter-spacing: -0.03em;
          margin: 24px 0 24px;
        }
        @media (min-width: 880px) {
          .hero-h1 { line-height: 0.98; letter-spacing: -0.035em; }
        }
@media (min-width: 1000px) {
 .hero-badge { margin-top: 72px; } 
}

        .hero-em {
          font-style: normal; font-weight: 500;
          background: linear-gradient(110deg, var(--sun) 30%, var(--sun-deep) 70%);
          -webkit-background-clip: text; background-clip: text; color: transparent;
          padding-right: 0.1em;
        }
        .hero-h1-soft { color: var(--ink-soft); font-weight: 500; }
        .hero-sub {
          font-size: clamp(16px, 1.6vw, 19px);
          color: var(--ink-soft);
          max-width: 52ch; line-height: 1.5;
          margin: 0 0 32px;
        }
        .hero-cta { display: inline-flex; gap: 12px; flex-wrap: wrap; margin-bottom: 48px; }
        .hero-stats {
          display: flex; gap: 20px 24px; flex-wrap: wrap;
          padding-top: 24px;
          border-top: 1px solid var(--hairline);
        }
        @media (min-width: 700px) {
          .hero-stats { gap: 36px; }
        }
        .hero-stage {
          position: relative; display: flex; flex-direction: column;
          align-items: center; gap: 32px;

        }
        .hero-phone-slot {
          position: relative;
          margin-top: -120px; margin-right: -40px;
          align-self: flex-end;
          display: none;
        }
        @media (min-width: 1000px) { .hero-phone-slot { display: block; } }
        .hero-phone-cap {
          position: absolute; right: 100%; bottom: 80px;
          width: 180px;
          font-size: 11px; color: var(--ink-soft);
          line-height: 1.5; padding-right: 24px;
          text-align: right; letter-spacing: 0.02em;
        }
        .hero-badge {
          display: inline-flex; align-items: center; gap: 10px;
          padding: 6px 14px 6px 8px;
          background: var(--card);
          border: 1px solid var(--hairline);
          border-radius: 999px;
          font-size: 12.5px; color: var(--ink-soft);
        }
        .hero-badge .mono {
          padding: 2px 7px; border-radius: 999px;
          background: var(--sun); color: #15110a;
          font-size: 10px; letter-spacing: 0.15em;
        }
        .hero-badge-pulse {
          width: 6px; height: 6px; border-radius: 50%;
          background: var(--sun);
          box-shadow: 0 0 0 4px color-mix(in srgb, var(--sun) 30%, transparent);
        }
      `}</style>
    </section>
  );
}

function Stat({ n, l }) {
  return (
    <div className="stat">
      <div className="stat-n">{n}</div>
      <div className="stat-l">{l}</div>
      <style>{`
        .stat { display: flex; flex-direction: column; gap: 2px; }
        .stat-n { font-family: var(--display); font-size: 26px; font-weight: 600; letter-spacing: -0.02em; }
        .stat-l { font-size: 12px; color: var(--ink-soft); }
      `}</style>
    </div>
  );
}

function Arrow() {
  return (
    <svg
      width="14"
      height="14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <line x1="5" y1="12" x2="19" y2="12" />
      <polyline points="12 5 19 12 12 19" />
    </svg>
  );
}

Object.assign(window, { Hero, HeroChat, IvanReviewPhone, Arrow, Stat });
