// pages-account.jsx — Membership, Auth, Admin, Account
const { useEffect: acUseEffect, useRef: acUseRef, useState: acUseState } = React;

// ---------- Membership ----------
function Membership({ onGo, openAuth }) {
  const tiers = window.AppData.TIERS;
  const titleRef = acUseRef(null);

  acUseEffect(() => {
    if (!window.gsap || !titleRef.current) return;
    window.ScrollFX.splitText(titleRef.current.querySelector("[data-split]"));
    const chars = titleRef.current.querySelectorAll(".split-char");
    window.gsap.set(chars, { yPercent: 110, opacity: 0 });
    window.gsap.to(chars, { yPercent: 0, opacity: 1, duration: 1.1, ease: "power4.out", stagger: { each: 0.025, from: "random" } });
    window.gsap.from(".tier", { opacity: 0, y: 40, duration: 0.9, ease: "power3.out", stagger: 0.08, delay: 0.25 });
  }, []);

  const subscribe = (tierId) => {
    if (!window.AppData.currentUser()) { openAuth("/membership"); return; }
    window.AppData.subscribeMembership(tierId);
    onGo("/account");
  };

  return (
    <main className="page-enter">
      <section className="member wrap">
        <div className="member__head">
          <div className="section-label" style={{justifyContent:"center"}}><span>Membership</span></div>
          <h1 ref={titleRef}><span data-split>Own the index.</span></h1>
          <p className="desc">Skip per-script checkout. One plan, every release, every patch — including the back catalogue and the drops we haven't shipped yet.</p>
        </div>
        <div className="tiers">
          {tiers.map(t => (
            <article key={t.id} className={`tier ${t.best ? "is-best" : ""}`}>
              <div>
                <div className="tier__name">{t.name}</div>
                {t.save && <div className="tier__save">↘ {t.save}</div>}
              </div>
              <div>
                <div className="tier__price"><span className="currency">$</span>{t.price}</div>
                <div className="tier__period">{t.period}</div>
              </div>
              <ul className="tier__features">{t.features.map((f, i) => <li key={i}>{f}</li>)}</ul>
              <button className="magnetic" data-magnetic data-cursor="button" onClick={() => subscribe(t.id)}>
                <span className={`btn btn--block magnetic-inner ${t.best ? "" : "btn--ghost"}`}>
                  <span>Choose {t.name}</span><span className="btn__arrow">→</span>
                </span>
              </button>
            </article>
          ))}
        </div>
        <div style={{marginTop:"80px", padding:"40px", background:"var(--bg-2)", borderRadius:"4px", display:"grid", gridTemplateColumns:"1fr 1fr 1fr", gap:"40px"}}>
          <div>
            <div className="section-label"><span>Included with all tiers</span></div>
            <p style={{color:"var(--fg-2)", fontSize:"14px", lineHeight:1.7}}>Source files. Commercial use up to $1M / year. Email support. Lifetime patches on your major version. Cancel anytime, keep the files you downloaded.</p>
          </div>
          <div>
            <div className="section-label"><span>Renewals</span></div>
            <p style={{color:"var(--fg-2)", fontSize:"14px", lineHeight:1.7}}>We renew at the same price you joined at — no surprise hikes. If a price changes, your tier locks for as long as your card keeps charging.</p>
          </div>
          <div>
            <div className="section-label"><span>Refunds</span></div>
            <p style={{color:"var(--fg-2)", fontSize:"14px", lineHeight:1.7}}>14-day refund window, no questions. Drop us a line within two weeks of your first payment and we refund in full.</p>
          </div>
        </div>
      </section>
    </main>
  );
}

// ---------- Auth ----------
function Auth({ onGo, returnTo }) {
  const [mode, setMode] = acUseState("login");
  const [email, setEmail] = acUseState("");
  const [pw, setPw] = acUseState("");
  const [name, setName] = acUseState("");
  const [err, setErr] = acUseState("");

  acUseEffect(() => {
    if (!window.gsap) return;
    window.gsap.from(".auth__card > *", { opacity: 0, y: 24, duration: 0.7, ease: "power3.out", stagger: 0.06 });
  }, [mode]);

  const submit = (e) => {
    e.preventDefault(); setErr("");
    try {
      if (mode === "login") window.AppData.login({ email, password: pw });
      else window.AppData.signup({ email, password: pw, name });
      onGo(returnTo || "/account");
    } catch (e) { setErr(e.message); }
  };

  return (
    <main className="page-enter">
      <section className="auth">
        <div className="auth__card">
          <div className="section-label"><span>{mode === "login" ? "Sign in" : "Create an account"}</span></div>
          <h1>{mode === "login" ? "Welcome back." : "Make it yours."}</h1>
          <p className="sub">{mode === "login" ? "Sign in to access your purchases, downloads and membership." : "It takes one field. We don't email you anything you didn't ask for."}</p>
          <form onSubmit={submit}>
            <div className="auth__error">{err}</div>
            {mode === "signup" && (
              <div className="auth__field">
                <label>Display name</label>
                <input value={name} onChange={e => setName(e.target.value)} placeholder="Aiyana K." />
              </div>
            )}
            <div className="auth__field">
              <label>Email</label>
              <input type="email" value={email} onChange={e => setEmail(e.target.value)} placeholder="you@studio.com" required />
            </div>
            <div className="auth__field">
              <label>Password</label>
              <input type="password" value={pw} onChange={e => setPw(e.target.value)} placeholder="••••••••" required minLength="3" />
            </div>
            <button type="submit" className="magnetic" data-magnetic data-cursor="button" style={{marginTop:"24px", display:"block", width:"100%"}}>
              <span className="btn btn--block magnetic-inner"><span>{mode === "login" ? "Sign in" : "Create account"}</span><span className="btn__arrow">→</span></span>
            </button>
          </form>
          <p className="auth__toggle">
            {mode === "login" ? "New here? " : "Already a member? "}
            <b onClick={() => setMode(mode === "login" ? "signup" : "login")}>{mode === "login" ? "Create an account" : "Sign in"}</b>
          </p>
          <p style={{marginTop:"40px", padding:"16px 20px", background:"var(--bg-2)", borderRadius:"4px", fontSize:"12px", color:"var(--fg-3)", lineHeight:1.6}}>
            <span style={{color:"var(--accent)", fontWeight:600}}>Try admin:</span> sign in with <code style={{color:"var(--fg)"}}>admin@studio.pro</code> / <code style={{color:"var(--fg)"}}>admin</code> to access the back office.
          </p>
        </div>
      </section>
    </main>
  );
}

// ---------- Account ----------
function Account({ onGo }) {
  const user = window.AppData.currentUser();
  if (!user) { setTimeout(() => onGo("/login"), 0); return null; }
  const scripts = window.AppData.getScripts();
  const owned = scripts.filter(s => (user.owned || []).includes(s.id));
  const tier = user.membership ? window.AppData.TIERS.find(t => t.id === user.membership) : null;
  const expires = user.membershipExpires ? new Date(user.membershipExpires) : null;

  acUseEffect(() => {
    if (!window.gsap) return;
    window.gsap.from(".account > *", { opacity: 0, y: 24, duration: 0.7, ease: "power3.out", stagger: 0.06 });
  }, []);

  return (
    <main className="page-enter">
      <section className="account">
        <div className="wrap">
          <div className="account__hero">
            <div>
              <div className="sub">Welcome back</div>
              <h1>{user.name}</h1>
              <p style={{color:"var(--fg-2)", fontSize:"14px", marginTop:"16px"}}>{user.email} · joined {new Date(user.joined).toLocaleDateString()}</p>
              <div style={{display:"flex", gap:"12px", marginTop:"32px"}}>
                {user.role === "admin" && (
                  <a className="btn" href="#/admin" onClick={(e)=>{e.preventDefault(); onGo("/admin");}} data-cursor="button">Open admin →</a>
                )}
                <button className="btn btn--ghost" onClick={() => { window.AppData.logout(); onGo("/"); }} data-cursor="button">Sign out</button>
              </div>
            </div>
            <div className="account__plan">
              <span className="tag">Current plan</span>
              <div className="name">{tier ? tier.name : "No membership"}</div>
              {tier ? (
                <div className="meta">Renews · {expires?.toLocaleDateString()}</div>
              ) : (
                <a href="#/membership" style={{color:"var(--accent)", fontSize:"13px", letterSpacing:"0.1em", textTransform:"uppercase"}} onClick={(e)=>{e.preventDefault(); onGo("/membership");}}>Choose a plan →</a>
              )}
            </div>
          </div>

          <div className="section-label" style={{marginTop:"56px"}}><span>Your downloads · {owned.length}</span></div>
          {owned.length === 0 ? (
            <div style={{padding:"60px 40px", background:"var(--bg-2)", textAlign:"center", color:"var(--fg-3)", fontSize:"14px", letterSpacing:"0.1em", textTransform:"uppercase"}}>
              No scripts owned yet. <a href="#/scripts" style={{color:"var(--accent)"}} onClick={(e)=>{e.preventDefault(); onGo("/scripts");}}>Browse →</a>
            </div>
          ) : (
            <div className="account__downloads">
              {owned.map(s => (
                <div key={s.id} className="account__download">
                  <div>
                    <div className="name">{s.title}</div>
                    <div className="file">{s.files?.archive || (s.id + ".zip")} · {s.fileSize}</div>
                  </div>
                  <div style={{display:"flex", gap:"8px"}}>
                    <a href="#" className="dl" onClick={(e)=>e.preventDefault()} data-cursor="button">↓ Source ZIP</a>
                    <a href="#" className="dl" style={{borderColor:"var(--line-2)", color:"var(--fg-2)"}} onClick={(e)=>e.preventDefault()} data-cursor="button">README</a>
                  </div>
                </div>
              ))}
            </div>
          )}
        </div>
      </section>
    </main>
  );
}

// ---------- Admin: file upload field ----------
function FileField({ label, value, onChange, accept, hint, multi }) {
  const fileRef = acUseRef(null);
  const display = !value ? "" : (multi ? `${value.length} 个文件` : (typeof value === "string" ? value : value.name));
  return (
    <div className="admin__form-field">
      <label>{label}</label>
      <div style={{display:"flex", gap:"8px", alignItems:"center"}}>
        <input type="text" readOnly value={display} placeholder="未选择文件" style={{flex:1, cursor:"none"}} />
        <button type="button" className="btn btn--ghost" style={{padding:"10px 16px", fontSize:"11px"}} onClick={() => fileRef.current?.click()} data-cursor="button">选择…</button>
      </div>
      <input type="file" ref={fileRef} hidden accept={accept} multiple={multi} onChange={(e) => {
        const f = multi ? Array.from(e.target.files).map(x=>x.name) : (e.target.files[0]?.name || "");
        onChange(f);
      }} />
      {hint && <div style={{fontSize:"11px", color:"var(--fg-3)", marginTop:"4px"}}>{hint}</div>}
    </div>
  );
}

// ---------- Admin ----------
function Admin({ onGo }) {
  const user = window.AppData.currentUser();
  if (!user || user.role !== "admin") { setTimeout(() => onGo("/login"), 0); return null; }

  const [scripts, setScripts] = acUseState(() => window.AppData.getScripts());
  const [editing, setEditing] = acUseState(null);
  const [tab, setTab] = acUseState("scripts");

  acUseEffect(() => {
    const upd = () => setScripts(window.AppData.getScripts());
    window.addEventListener("pa:scripts", upd);
    return () => window.removeEventListener("pa:scripts", upd);
  }, []);

  const startNew = () => {
    setEditing({
      __new: true,
      title: "", software: "AE", category: "", price: 0,
      summary: "", tags: [], version: "1.0.0", fileSize: "", compatibility: "",
      files: { video: "", cover: "", detail: "", gallery: [], archive: "", readme: "" }
    });
  };

  const save = (e) => {
    e.preventDefault();
    const p = editing;
    if (p.__new) {
      const { __new, ...rest } = p;
      window.AppData.addScript(rest);
    } else {
      window.AppData.updateScript(p.id, p);
    }
    setEditing(null);
  };

  const orders = window.AppData.getOrders();
  const users = window.AppData.getUsers();
  const settings = window.AppData.getSettings();
  const BG_VIDEOS = window.AppData.BG_VIDEOS;
  const [bgPick, setBgPick] = acUseState(() => {
    const s = window.AppData.getSettings();
    return s.bgVideoId;
  });
  const [customUrl, setCustomUrl] = acUseState(() => window.AppData.getSettings().customBgVideoUrl || "");
  const [customName, setCustomName] = acUseState(() => window.AppData.getSettings().customBgVideoName || "");
  const customFileRef = acUseRef(null);
  const applyBgSelection = (id) => {
    setBgPick(id);
    window.AppData.saveSettings({ bgVideoId: id });
  };
  const applyCustom = () => {
    if (!customUrl.trim()) { alert("请先填写视频链接或上传文件"); return; }
    window.AppData.saveSettings({ bgVideoId: "__custom", customBgVideoUrl: customUrl, customBgVideoName: customName || "自定义上传" });
    setBgPick("__custom");
  };
  const onCustomFile = (e) => {
    const f = e.target.files?.[0];
    if (!f) return;
    if (f.size > 60 * 1024 * 1024) {
      if (!confirm(`文件大小 ${(f.size/1024/1024).toFixed(1)} MB —— 过大可能导致存储失败，是否继续？`)) return;
    }
    const url = URL.createObjectURL(f);
    setCustomUrl(url);
    setCustomName(f.name);
  };

  return (
    <main className="page-enter">
      <section className="admin">
        <div className="admin__head">
          <div>
            <div className="sub">后台</div>
            <h1>工作室管理</h1>
          </div>
          <div style={{display:"flex", gap:"12px"}}>
            <a className="btn btn--ghost" href="#/" onClick={(e)=>{e.preventDefault(); onGo("/");}} data-cursor="button">查看站点 →</a>
            <button className="btn" onClick={startNew} data-cursor="button">+ 新增脚本</button>
          </div>
        </div>

        <div className="admin__layout">
          <aside className="admin__side">
            <h4>管理</h4>
            <a href="#" className={tab === "scripts" ? "is-active" : ""} onClick={(e)=>{e.preventDefault(); setTab("scripts");}} data-cursor="button">脚本 · {scripts.length}</a>
            <a href="#" className={tab === "orders" ? "is-active" : ""} onClick={(e)=>{e.preventDefault(); setTab("orders");}} data-cursor="button">订单 · {orders.length}</a>
            <a href="#" className={tab === "users" ? "is-active" : ""} onClick={(e)=>{e.preventDefault(); setTab("users");}} data-cursor="button">客户 · {users.length}</a>
            <a href="#" className={tab === "settings" ? "is-active" : ""} onClick={(e)=>{e.preventDefault(); setTab("settings");}} data-cursor="button">站点设置</a>
            <h4 style={{marginTop:"32px"}}>快捷操作</h4>
            <a href="#" onClick={(e)=>{e.preventDefault(); if(confirm("将所有脚本重置为默认值？")) { localStorage.removeItem("pa_scripts_v1"); setScripts(window.AppData.getScripts()); }}} data-cursor="button">重置为默认值</a>
            <a href="#" onClick={(e)=>{e.preventDefault(); onGo("/admin/inline");}} data-cursor="button">前台行内编辑 →</a>
          </aside>

          <div>
            {editing ? (
              <form onSubmit={save} className="admin__edit-form">
                <div style={{display:"flex", justifyContent:"space-between", alignItems:"center", marginBottom:"24px"}}>
                  <div>
                    <div className="sub" style={{fontSize:"11px", letterSpacing:"0.18em", color:"var(--fg-3)", textTransform:"uppercase"}}>{editing.__new ? "新增脚本" : "编辑中"}</div>
                    <h2 className="display" style={{fontSize:"32px", marginTop:"4px"}}>{editing.title || "未命名脚本"}</h2>
                  </div>
                  <button type="button" className="btn btn--ghost" style={{padding:"10px 16px", fontSize:"11px"}} onClick={() => setEditing(null)} data-cursor="button">取消</button>
                </div>

                <div className="admin__form-row">
                  <div className="admin__form-field full">
                    <label>名称</label>
                    <input value={editing.title} onChange={e => setEditing({ ...editing, title: e.target.value })} />
                  </div>
                  <div className="admin__form-field">
                    <label>软件</label>
                    <select value={editing.software} onChange={e => setEditing({ ...editing, software: e.target.value })}>
                      <option>AE</option><option>Blender</option><option>UE5</option>
                    </select>
                  </div>
                  <div className="admin__form-field">
                    <label>分类</label>
                    <input value={editing.category} onChange={e => setEditing({ ...editing, category: e.target.value })} placeholder="例如：字体动效" />
                  </div>
                  <div className="admin__form-field">
                    <label>价格（人民币）</label>
                    <input type="number" value={editing.price} onChange={e => setEditing({ ...editing, price: parseFloat(e.target.value) || 0 })} />
                  </div>
                  <div className="admin__form-field">
                    <label>版本号</label>
                    <input value={editing.version} onChange={e => setEditing({ ...editing, version: e.target.value })} />
                  </div>
                  <div className="admin__form-field">
                    <label>兼容性</label>
                    <input value={editing.compatibility} onChange={e => setEditing({ ...editing, compatibility: e.target.value })} placeholder="例如：AE 2023+" />
                  </div>
                  <div className="admin__form-field">
                    <label>文件大小</label>
                    <input value={editing.fileSize} onChange={e => setEditing({ ...editing, fileSize: e.target.value })} placeholder="例如：184 MB" />
                  </div>
                  <div className="admin__form-field full">
                    <label>简介</label>
                    <textarea value={editing.summary} onChange={e => setEditing({ ...editing, summary: e.target.value })} />
                  </div>
                </div>

                <h3 className="display" style={{fontSize:"20px", marginTop:"32px", marginBottom:"4px"}}>媒体与文件</h3>
                <p style={{fontSize:"12px", color:"var(--fg-3)", marginBottom:"24px"}}>上传介绍视频或图片、详情图、5 张主图、源码压缩包以及 README 文档。</p>

                <div className="admin__form-row">
                  <FileField label="介绍视频 / 图片" value={editing.files?.video} accept="video/*,image/*" hint="MP4 / WebM / JPG / PNG · 推荐 1920×1080"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, video: v } })} />
                  <FileField label="封面图" value={editing.files?.cover} accept="image/*" hint="卡片缩略图 · 正方形或 4:5"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, cover: v } })} />
                  <FileField label="详情主图" value={editing.files?.detail} accept="image/*" hint="详情页大图"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, detail: v } })} />
                  <FileField label="商品五图" value={editing.files?.gallery} multi accept="image/*" hint="正好 5 张"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, gallery: v } })} />
                  <FileField label="源码压缩包（.zip）" value={editing.files?.archive} accept=".zip,.rar,.7z" hint="最终交付包"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, archive: v } })} />
                  <FileField label="README.md" value={editing.files?.readme} accept=".md,.txt,.pdf" hint="安装说明 + 参数 + 注意事项"
                    onChange={v => setEditing({ ...editing, files: { ...editing.files, readme: v } })} />
                </div>

                <div style={{display:"flex", gap:"12px", marginTop:"32px", justifyContent:"flex-end"}}>
                  {!editing.__new && (
                    <button type="button" className="btn btn--ghost" onClick={() => { if (confirm("确认删除该脚本？")) { window.AppData.removeScript(editing.id); setEditing(null); } }} data-cursor="button" style={{color:"var(--accent)", borderColor:"var(--accent)"}}>删除</button>
                  )}
                  <button type="submit" className="btn" data-cursor="button"><span>{editing.__new ? "发布脚本" : "保存修改"}</span><span className="btn__arrow">→</span></button>
                </div>
              </form>
            ) : tab === "scripts" ? (
              <table className="admin__table">
                <thead>
                  <tr><th></th><th>名称</th><th>软件</th><th>分类</th><th>价格</th><th>下载量</th><th></th></tr>
                </thead>
                <tbody>
                  {scripts.map(s => {
                    const p = window.AppData.PALETTES[s.palette % window.AppData.PALETTES.length];
                    return (
                      <tr key={s.id}>
                        <td><div className="admin__row-thumb"><div className="blob" style={{background:`radial-gradient(circle at 30% 30%, ${p[0]}, transparent 60%), radial-gradient(circle at 70% 70%, ${p[1]}, transparent 60%)`}}></div></div></td>
                        <td style={{color:"var(--fg)"}}>{s.title}</td>
                        <td><span className="admin__tag">{s.software}</span></td>
                        <td>{s.category}</td>
                        <td style={{color:"var(--fg)"}}>¥{s.price}</td>
                        <td>{s.downloads.toLocaleString()}</td>
                        <td><button onClick={() => setEditing(s)} style={{color:"var(--accent)", fontSize:"11px", letterSpacing:"0.12em", textTransform:"uppercase"}} data-cursor="button">编辑 →</button></td>
                      </tr>
                    );
                  })}
                </tbody>
              </table>
            ) : tab === "orders" ? (
              <table className="admin__table">
                <thead><tr><th>订单号</th><th>商品</th><th>总额</th><th>时间</th></tr></thead>
                <tbody>
                  {orders.length === 0 ? <tr><td colSpan="4" style={{textAlign:"center", padding:"40px"}}>暂无订单</td></tr> :
                    orders.map(o => (
                      <tr key={o.id}>
                        <td className="mono" style={{color:"var(--fg)"}}>{o.id}</td>
                        <td>{o.items.map(i=>i.title).join("，")}</td>
                        <td style={{color:"var(--fg)"}}>¥{o.total.toFixed(2)}</td>
                        <td>{new Date(o.date).toLocaleString()}</td>
                      </tr>
                    ))}
                </tbody>
              </table>
            ) : tab === "users" ? (
              <table className="admin__table">
                <thead><tr><th>用户名</th><th>邮箱</th><th>身份</th><th>会员套餐</th><th>注册时间</th></tr></thead>
                <tbody>
                  {users.length === 0 ? <tr><td colSpan="5" style={{textAlign:"center", padding:"40px"}}>暂无客户</td></tr> :
                    users.map(u => (
                      <tr key={u.id}>
                        <td style={{color:"var(--fg)"}}>{u.name}</td>
                        <td>{u.email}</td>
                        <td><span className="admin__tag">{u.role === "admin" ? "管理员" : "会员"}</span></td>
                        <td>{u.membership || "—"}</td>
                        <td>{new Date(u.joined).toLocaleDateString()}</td>
                      </tr>
                    ))}
                </tbody>
              </table>
            ) : (
              <div>
                <div style={{display:"flex", justifyContent:"space-between", alignItems:"flex-end", marginBottom:"24px"}}>
                  <div>
                    <div className="sub" style={{fontSize:"11px", letterSpacing:"0.18em", color:"var(--fg-3)", textTransform:"uppercase"}}>站点设置</div>
                    <h2 className="display" style={{fontSize:"32px", marginTop:"4px"}}>背景视频</h2>
                    <p style={{color:"var(--fg-2)", fontSize:"14px", marginTop:"8px", maxWidth:"560px", lineHeight:1.6}}>从 10 段精选背景循环中挑一段用于首页，或上传自定义 MP4。视频在滚动时背景位置不动，跟随滚动逐帧切换关键帧。</p>
                  </div>
                  <div style={{textAlign:"right"}}>
                    <div style={{fontSize:"11px", color:"var(--fg-3)", letterSpacing:"0.15em", textTransform:"uppercase"}}>当前启用</div>
                    <div className="display" style={{fontSize:"22px", color:"var(--fg)", marginTop:"4px"}}>{(window.AppData.getActiveBgVideo()?.name) || "—"}</div>
                  </div>
                </div>

                <div style={{display:"grid", gridTemplateColumns:"repeat(2, 1fr)", gap:"4px", marginBottom:"40px"}}>
                  {BG_VIDEOS.map(v => {
                    const isActive = bgPick === v.id;
                    return (
                      <button key={v.id} type="button" onClick={() => applyBgSelection(v.id)} data-cursor="ring" data-cursor-label="使用"
                        style={{
                          background: "var(--bg-2)",
                          border: isActive ? "1px solid var(--accent)" : "1px solid var(--line)",
                          textAlign: "left", padding: 0, overflow: "hidden", position: "relative",
                          display: "flex", alignItems: "stretch", gap: 0,
                          fontFamily: "inherit", color: "var(--fg)", cursor: "none"
                        }}>
                        <div style={{
                          width: "160px", height: "100px", flex: "none", position: "relative",
                          background: `radial-gradient(circle at 30% 30%, ${v.poster[0]}, transparent 60%), radial-gradient(circle at 70% 70%, ${v.poster[1]}, transparent 60%), radial-gradient(circle at 50% 50%, ${v.poster[2]}, transparent 50%)`,
                          filter: "blur(0.5px)"
                        }}>
                          {isActive && (
                            <div style={{position:"absolute", top:"8px", left:"8px", padding:"4px 8px", background:"var(--accent)", color:"var(--bg)", fontSize:"9px", fontWeight:700, letterSpacing:"0.15em", borderRadius:"999px"}}>启用中</div>
                          )}
                        </div>
                        <div style={{padding:"16px 20px", flex:1, display:"flex", flexDirection:"column", justifyContent:"space-between", minWidth: 0}}>
                          <div>
                            <div className="display" style={{fontSize:"16px", marginBottom:"4px", letterSpacing:"-0.01em"}}>{v.name}</div>
                            <div style={{fontSize:"12px", color:"var(--fg-3)", lineHeight:1.45}}>{v.desc}</div>
                          </div>
                          <div style={{fontSize:"10px", color:"var(--fg-3)", letterSpacing:"0.1em", textTransform:"uppercase", marginTop:"6px"}}>{v.id}</div>
                        </div>
                      </button>
                    );
                  })}
                </div>

                <div style={{padding:"32px", background:"var(--bg-2)", borderRadius:"4px", border: bgPick === "__custom" ? "1px solid var(--accent)" : "1px solid var(--line)"}}>
                  <div style={{display:"flex", justifyContent:"space-between", alignItems:"flex-start", marginBottom:"20px"}}>
                    <div>
                      <div className="sub" style={{fontSize:"11px", letterSpacing:"0.18em", color:"var(--fg-3)", textTransform:"uppercase"}}>或自行上传</div>
                      <h3 className="display" style={{fontSize:"24px", marginTop:"4px"}}>自定义背景</h3>
                    </div>
                    {bgPick === "__custom" && <span className="admin__tag" style={{background:"var(--accent)", color:"var(--bg)"}}>启用中</span>}
                  </div>
                  <div className="admin__form-row">
                    <div className="admin__form-field full">
                      <label>上传 MP4 / WebM（推荐 4K）</label>
                      <div style={{display:"flex", gap:"8px", alignItems:"center"}}>
                        <input type="text" readOnly value={customName} placeholder="未选择文件" style={{flex:1, cursor:"none"}} />
                        <button type="button" className="btn btn--ghost" style={{padding:"10px 16px", fontSize:"11px"}} onClick={() => customFileRef.current?.click()} data-cursor="button">选择文件…</button>
                        <input type="file" ref={customFileRef} hidden accept="video/mp4,video/webm,video/*" onChange={onCustomFile} />
                      </div>
                      <div style={{fontSize:"11px", color:"var(--fg-3)", marginTop:"6px"}}>建议 MP4（H.264）2560×1440 及以上 · 一镜到底 · 小于 60 MB</div>
                    </div>
                    <div className="admin__form-field full">
                      <label>或粘贴视频直链</label>
                      <input type="text" value={customUrl.startsWith("blob:") ? "" : customUrl} onChange={(e) => setCustomUrl(e.target.value)} placeholder="https://example.com/your-video.mp4" />
                    </div>
                  </div>
                  <div style={{display:"flex", gap:"12px", marginTop:"16px", justifyContent:"flex-end", alignItems:"center"}}>
                    {customUrl && (
                      <span style={{fontSize:"11px", color:"var(--fg-3)", letterSpacing:"0.1em", marginRight:"auto"}}>↘ {customName || "已填写链接"} —— 等待启用</span>
                    )}
                    <button type="button" className="btn btn--ghost" onClick={() => { setCustomUrl(""); setCustomName(""); }} data-cursor="button" style={{padding:"10px 16px", fontSize:"11px"}}>清空</button>
                    <button type="button" className="btn" onClick={applyCustom} data-cursor="button" disabled={!customUrl}><span>启用自定义视频</span><span className="btn__arrow">→</span></button>
                  </div>
                </div>

                <div style={{marginTop:"32px", padding:"20px 24px", background:"transparent", border:"1px solid var(--line)", borderRadius:"4px", fontSize:"12px", color:"var(--fg-3)", lineHeight:1.6, letterSpacing:"0.04em"}}>
                  <strong style={{color:"var(--fg)"}}>说明</strong> —— 浏览器内上传会使用临时 blob 链接，仅在当前会话有效。正式部署时建议改为上传到 CDN，由后端返回最终的 MP4 公开链接。
                </div>
              </div>
            )}
          </div>
        </div>
      </section>
    </main>
  );
}

window.AccountPages = { Membership, Auth, Account, Admin };
