// top.jsx — 공용 컴포넌트 + 상단 띠 + 헤더 + 히어로(좌: 키카피 / 우: 학부모 후기)

// ─── 공용: 태그 칩 ───
const Tag = ({ children }) => (
  <span style={{
    fontSize: 12, background: "var(--gray-01)", color: "var(--text-2)",
    padding: "5px 12px", borderRadius: "var(--r-pill)", fontWeight: 500,
  }}>{children}</span>
);

// ─── 공용: 솔루션 탭 ───
const SolutionTabs = ({ tabs, activeIdx, setActiveIdx }) => (
  <div style={{ display: "flex", flexWrap: "wrap", gap: 8, marginBottom: 16 }}>
    {tabs.map((tab, i) => (
      <button key={tab.label} onClick={() => setActiveIdx(i)}
        className={`pill-tab ${i === activeIdx ? "is-active" : ""}`}>{tab.label}</button>
    ))}
  </div>
);

// ─── 공용: 탭 이미지 ───
const TabImageSwitcher = ({ tabs, activeIdx, ratio }) => (
  <div style={{ borderRadius: "var(--r-lg)", boxShadow: "var(--shadow-md)", border: "1px solid var(--border)", overflow: "hidden", background: "#fff" }}>
    <div style={{ display: "grid", alignItems: "start", ...(ratio ? { aspectRatio: ratio } : {}) }}>
      {tabs.map((tab, i) => (
        <img key={tab.label} src={tab.image} alt={tab.label}
          style={{ gridArea: "1 / 1", width: "100%", height: ratio ? "100%" : "auto", display: "block",
            objectFit: ratio ? "cover" : undefined, objectPosition: "top", transition: "opacity .3s",
            opacity: i === activeIdx ? 1 : 0, pointerEvents: i === activeIdx ? "auto" : "none" }} />
      ))}
    </div>
  </div>
);

// ─── 상단 띠 ───
function RealtimeCounter() {
  return (
    <div className="top-banner" style={{ background: "var(--primary-light)", borderBottom: "1px solid var(--border)", padding: "10px 24px" }}>
      <div className="wrap" style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 3 }}>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 8, flexWrap: "wrap" }}>
          <span className="top-banner-label" style={{ fontSize: 13.5, color: "var(--text-2)" }}>지금 이 순간, 원장님들께 돌려드리고 있는 시간:</span>
          <span className="tnum" style={{ fontWeight: 800, color: "var(--primary)", fontSize: 15 }}>
            {HOURS_DISPLAY}<span style={{ fontFamily: "var(--font-kr)", fontWeight: 700 }}>시간</span>
          </span>
        </div>
        <span style={{ fontSize: 12.5, color: "var(--primary)", textAlign: "center", wordBreak: "keep-all" }}>
          반복 업무에 쓰이던 시간이 이제 아이들을 더 잘 가르치는 데 쓰이고 있어요
        </span>
      </div>
    </div>
  );
}

// ─── 헤더 ───
function Header() {
  const [open, setOpen] = React.useState(false);
  return (
    <header style={{ position: "sticky", top: 0, zIndex: 50, background: "#fff", borderBottom: "1px solid var(--border)" }}>
      <div className="wrap" style={{ height: 64, display: "flex", alignItems: "center", justifyContent: "space-between", gap: 24 }}>
        {/* 왼쪽: 로고 + 내비 */}
        <div style={{ display: "flex", alignItems: "center", gap: 32, minWidth: 0 }}>
          <a href="/" style={{ display: "flex", alignItems: "center", cursor: "pointer", flexShrink: 0 }}>
            <img src="https://live-public.1hour.ai/assets/1hour_logo.png" alt="원아워 로고" style={{ height: 28 }}
              onError={(e) => { e.target.style.display = "none"; }} />
          </a>
          <nav className="hide-sm" style={{ display: "flex", alignItems: "center", gap: 24 }}>
            <a className="navlink" href="features.html">기능 한눈에 보기</a>
            <a className="navlink" href="solutions.html">솔루션</a>
            <a className="navlink" href="faq.html">FAQ</a>
            <a className="navlink" href={BLOG_URL} target="_blank" rel="noopener noreferrer">블로그</a>
          </nav>
        </div>
        {/* 오른쪽: 계정 액션 (데스크톱) */}
        <div className="nav-actions-desktop" style={{ display: "flex", alignItems: "center", gap: 16, flexShrink: 0 }}>
          <a className="navlink" href={LOGIN_URL}>로그인</a>
          <span style={{ width: 1, height: 16, background: "var(--border)" }} />
          <a className="btn-primary btn-sm" href={FREE_TRIAL_URL}>3일 무료체험 →</a>
        </div>
        {/* 햄버거 (모바일) */}
        <button className="hamburger" aria-label="메뉴 열기" aria-expanded={open} onClick={() => setOpen((v) => !v)}>
          {open ? <IconX size={22} /> : (
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><line x1="3" y1="6" x2="21" y2="6" /><line x1="3" y1="12" x2="21" y2="12" /><line x1="3" y1="18" x2="21" y2="18" /></svg>
          )}
        </button>
      </div>
      {/* 모바일 드로어 */}
      <div className={`mobile-drawer ${open ? "is-open" : ""}`}>
        <a className="navlink" href="features.html">기능 한눈에 보기</a>
        <a className="navlink" href="solutions.html">솔루션</a>
        <a className="navlink" href="faq.html">FAQ</a>
        <a className="navlink" href={BLOG_URL} target="_blank" rel="noopener noreferrer">블로그</a>
        <a className="navlink drawer-login" href={LOGIN_URL}>로그인</a>
        <a className="btn-primary btn-sm drawer-cta" href={FREE_TRIAL_URL}>3일 무료체험 →</a>
      </div>
    </header>
  );
}

// ─── 후기 카드 (캐러셀) ───
function ReviewCard() {
  const [idx, setIdx] = React.useState(0);
  const n = REVIEWS.length;
  const go = (d) => setIdx((p) => (p + d + n) % n);
  React.useEffect(() => {
    const id = setInterval(() => setIdx((p) => (p + 1) % n), 6000);
    return () => clearInterval(id);
  }, [n]);
  const r = REVIEWS[idx];
  const initial = r.name.trim().charAt(0);
  const isParent = r.role === "학부모";
  const badgeStyle = isParent
    ? { background: "var(--primary)", color: "#fff" }
    : { background: "var(--ink)", color: "#fff" };

  return (
    <div className="review-card" style={{
      background: "#fff", border: "1px solid var(--border)", borderRadius: "var(--r-lg)",
      boxShadow: "var(--shadow-md)", padding: 32, display: "flex", flexDirection: "column",
      gap: 24, minHeight: 320, justifyContent: "space-between",
    }}>
      <div>
        {/* 별점 + 역할 배지 */}
        <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 20 }}>
          <div style={{ display: "flex", gap: 2, color: "var(--primary)" }}>
            {[0, 1, 2, 3, 4].map((i) => <IconStarFill key={i} size={20} />)}
          </div>
        </div>
        <p className="review-quote" style={{ fontSize: 18, fontWeight: 600, color: "var(--text-1)", lineHeight: 1.65, letterSpacing: "-0.01em", margin: 0, wordBreak: "keep-all", textWrap: "balance" }}>
          “{r.quote}”
        </p>
      </div>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
        <div style={{ display: "flex", alignItems: "flex-start", gap: 12 }}>
          <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", gap: 8 }}>
            <span style={{ fontSize: 12, fontWeight: 700, padding: "3px 10px", borderRadius: "var(--r-pill)", ...badgeStyle }}>{r.role}</span>
            <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
              <div style={{ width: 40, height: 40, borderRadius: "50%", background: "var(--primary-light)", color: "var(--primary-dark)", display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 800, fontSize: 16, flexShrink: 0 }}>{initial}</div>
              <span style={{ fontSize: 14, fontWeight: 700, color: "var(--text-1)" }}>{r.name}</span>
            </div>
          </div>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <button className="carousel-btn" onClick={() => go(-1)} aria-label="이전 후기"><IconChevLeft size={18} /></button>
          <button className="carousel-btn" onClick={() => go(1)} aria-label="다음 후기"><IconChevRight size={18} /></button>
        </div>
      </div>
    </div>
  );
}

// ─── 히어로 ───
function Hero() {
  return (
    <section className="hero" style={{ padding: "64px 0 48px", background: "#fff" }}>
      <div className="wrap">
        <div className="hero-grid">
          {/* 좌측 — 키카피 */}
          <div style={{ flex: 1, paddingTop: 8 }}>
            {/* 수상 배지 바 */}
            <div style={{ display: "flex", width: "fit-content", maxWidth: "100%", alignItems: "center", gap: 14, background: "#fff", border: "1px solid var(--border)", borderRadius: "var(--r-pill)", boxShadow: "var(--shadow-xs)", padding: "8px 16px", marginBottom: 28 }}>
              <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
                <img src="https://live-public.1hour.ai/assets/landing/award-1.jpg" alt="" style={{ width: 20, height: 20, borderRadius: "50%", objectFit: "cover" }} onError={(e) => { e.target.style.display = "none"; }} />
                <span style={{ fontSize: 13, fontWeight: 600, color: "var(--text-2)", whiteSpace: "nowrap" }}>교육부장관상 수상</span>
              </span>
              <span style={{ width: 1, height: 16, background: "var(--border)" }} />
              <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
                <img src="https://live-public.1hour.ai/assets/landing/award-2.jpg" alt="" style={{ width: 20, height: 20, borderRadius: "50%", objectFit: "cover" }} onError={(e) => { e.target.style.display = "none"; }} />
                <span style={{ fontSize: 13, fontWeight: 600, color: "var(--text-2)", whiteSpace: "nowrap" }}>동아시아 에듀테크 150</span>
              </span>
            </div>

            <h1 style={{ fontSize: "clamp(2.2rem,3.6vw,3rem)", fontWeight: 800, color: "var(--text-1)", lineHeight: 1.22, letterSpacing: "-0.025em", margin: "0 0 24px" }}>
              <span style={{ color: "var(--primary)" }}>수업 준비</span>부터<br />
              <span style={{ color: "var(--primary)" }}>운영</span>까지, <span style={{ color: "var(--primary)" }}>한 번에</span><br />
              영어 교육 올인원 플랫폼
            </h1>
            <p className="hero-sub" style={{ fontSize: 17, color: "var(--text-2)", margin: "0 0 36px", lineHeight: 1.6, wordBreak: "keep-all" }}>
              선생님은 수업에 집중하고, <span style={{ color: "var(--text-1)", fontWeight: 700 }}>아이들은 더 깊이 배웁니다.</span>
            </p>
            <a className="btn-primary btn-xl" href={FREE_TRIAL_URL}>3일 무료체험 시작하기 →</a>
            <p style={{ fontSize: 13, color: "var(--text-3)", margin: "16px 0 0" }}>카드 등록 불필요 · 정기결제 없이 즉시 사용</p>
          </div>

          {/* 우측 — 학부모 후기 */}
          <div style={{ flex: 1, width: "100%" }}>
            <ReviewCard />
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { Tag, SolutionTabs, TabImageSwitcher, RealtimeCounter, Header, Hero, ReviewCard });
