/* Burg Leuk — Tweaks. Stimmung (Abend/Tageslicht), editorial accent, display weight.
   Applies to <html data-stimmung> and the .site element's CSS vars. */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "stimmung": "Abend",
  "akzent": "Burgunderrot",
  "displayWeight": 700
}/*EDITMODE-END*/;

const ACCENTS = {
  "Burgunderrot": { on: "var(--apricot-700)", dark: "var(--apricot-300)", sw: "#7E2D3C" },
  "Olivgrün":     { on: "var(--salbei-700)", dark: "var(--salbei-300)", sw: "#525a3b" },
  "Taupe":        { on: "var(--taupe-500)",  dark: "var(--sand-300)",   sw: "#6f655a" },
};

function BurgTweaks() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  React.useEffect(() => {
    const root = document.documentElement;
    root.dataset.stimmung = t.stimmung === "Tageslicht" ? "tag" : "abend";
    const site = document.querySelector(".site") || document.body;
    const ac = ACCENTS[t.akzent] || ACCENTS.Burgunderrot;
    site.style.setProperty("--site-accent", ac.on);
    site.style.setProperty("--site-accent-on-dark", ac.dark);
    site.style.setProperty("--disp-weight", String(t.displayWeight));
    if (window.__burgNavUpdate) window.__burgNavUpdate();
  }, [t]);

  return (
    <TweaksPanel title="Burg Leuk">
      <TweakSection label="Stimmung" />
      <TweakRadio
        label="Welt"
        value={t.stimmung}
        options={["Abend", "Tageslicht"]}
        onChange={(v) => setTweak("stimmung", v)}
      />
      <div style={{ fontSize: 11, lineHeight: 1.5, opacity: 0.6, padding: "2px 2px 6px" }}>
        {t.stimmung === "Tageslicht"
          ? "Tageslicht — warmes Kalkstein-Papier, ruhig und hell."
          : "Abend — Noir, Kerzenlicht, der lange Sommerabend."}
      </div>

      <TweakSection label="Akzent" />
      <TweakColor
        label="Editorial-Akzent"
        value={t.akzent}
        options={["Burgunderrot", "Olivgrün", "Taupe"]}
        swatches={{ "Burgunderrot": "#7E2D3C", "Olivgrün": "#525a3b", "Taupe": "#6f655a" }}
        onChange={(v) => setTweak("akzent", v)}
      />
      <div style={{ fontSize: 11, lineHeight: 1.5, opacity: 0.6, padding: "2px 2px 6px" }}>
        Burgunderrot bleibt dem wichtigsten Knopf vorbehalten — der Akzent färbt nur Labels &amp; Linien.
      </div>

      <TweakSection label="Typografie" />
      <TweakSlider
        label="Display-Gewicht"
        value={t.displayWeight}
        min={400} max={800} step={100}
        onChange={(v) => setTweak("displayWeight", v)}
      />
    </TweaksPanel>
  );
}

/* Adapter: label-based color swatches mapped to token values. */
window.TweakColor = function ({ swatches, options, value, onChange, label }) {
  return (
    <TweakRow label={label}>
      <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
        {options.map((opt) => {
          const active = value === opt;
          return (
            <button
              key={opt}
              onClick={() => onChange(opt)}
              title={opt}
              style={{
                display: "flex", alignItems: "center", gap: 7,
                padding: "5px 11px 5px 6px", borderRadius: 999,
                border: active ? "1.5px solid #111" : "1px solid rgba(0,0,0,.18)",
                background: active ? "rgba(0,0,0,.04)" : "transparent",
                cursor: "pointer", fontSize: 12, fontWeight: active ? 600 : 500,
              }}
            >
              <span style={{ width: 16, height: 16, borderRadius: "50%", background: (swatches && swatches[opt]) || opt, boxShadow: "inset 0 0 0 1px rgba(0,0,0,.15)" }} />
              {opt}
            </button>
          );
        })}
      </div>
    </TweakRow>
  );
};

(function mountBurgTweaks() {
  let host = document.getElementById("burg-tweaks-root");
  if (!host) { host = document.createElement("div"); host.id = "burg-tweaks-root"; document.body.appendChild(host); }
  ReactDOM.createRoot(host).render(<BurgTweaks />);
})();
