/* ============================================================
   Tweaks panel — controls the live design tokens on <html>.
   Renders only the panel; the page itself is plain HTML/CSS.
   ============================================================ */
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "theme": "forest",
  "motion": "full"
}/*EDITMODE-END*/;

const THEMES = {
  forest:   { name: "Forest & Clay",    swatch: ["#2F4A37", "#C96F47", "#F6F1E7"] },
  charcoal: { name: "Charcoal & Honey", swatch: ["#2B2924", "#C68F38", "#F4F1EC"] },
  slate:    { name: "Slate & Blush",    swatch: ["#3A4654", "#BE8581", "#F1F1F4"] }
};

function TweaksApp() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const root = document.documentElement;

  React.useEffect(() => { root.setAttribute("data-theme", t.theme); }, [t.theme]);
  React.useEffect(() => {
    root.setAttribute("data-motion", t.motion);
    root.style.setProperty("--reveal-dur", t.motion === "subtle" ? ".42s" : ".8s");
    if (t.motion === "off" && window.__revealAll) window.__revealAll();
  }, [t.motion]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="Palette" />
      <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
        {Object.keys(THEMES).map((key) => {
          const th = THEMES[key];
          const active = t.theme === key;
          return (
            <button
              key={key}
              onClick={() => setTweak("theme", key)}
              style={{
                display: "flex", alignItems: "center", gap: 12,
                padding: "9px 11px", borderRadius: 12, cursor: "pointer",
                border: active ? "1.5px solid #111" : "1.5px solid rgba(0,0,0,.12)",
                background: active ? "rgba(0,0,0,.04)" : "transparent",
                textAlign: "left", width: "100%", transition: "border-color .15s, background .15s"
              }}
            >
              <span style={{ display: "flex", flex: "none" }}>
                {th.swatch.map((c, i) => (
                  <span key={i} style={{
                    width: 18, height: 18, borderRadius: "50%", background: c,
                    marginLeft: i ? -6 : 0, border: "2px solid #fff",
                    boxShadow: "0 0 0 1px rgba(0,0,0,.08)"
                  }} />
                ))}
              </span>
              <span style={{ fontSize: 13, fontWeight: 600, color: "#111" }}>{th.name}</span>
              {active && <span style={{ marginLeft: "auto", fontSize: 12, color: "#111" }}>✓</span>}
            </button>
          );
        })}
      </div>

      <TweakSection label="Motion" />
      <TweakRadio
        label="Animation"
        value={t.motion}
        options={["full", "subtle", "off"]}
        onChange={(v) => setTweak("motion", v)}
      />
    </TweaksPanel>
  );
}

ReactDOM.createRoot(document.getElementById("tweaks-root")).render(<TweaksApp />);
