// tweaks-panel.jsx — Panel de ajustes de interfaz
const { useState: useStateTwk } = React;

function useTweaks(defaults) {
  const [tweaks, setTweaksState] = useStateTwk(() => {
    try {
      const saved = localStorage.getItem("g-tweaks");
      return saved ? { ...defaults, ...JSON.parse(saved) } : defaults;
    } catch (e) {
      return defaults;
    }
  });
  const setTweak = (key, value) => {
    setTweaksState((prev) => {
      const next = { ...prev, [key]: value };
      try { localStorage.setItem("g-tweaks", JSON.stringify(next)); } catch (e) {}
      return next;
    });
  };
  return [tweaks, setTweak];
}

function TweaksPanel({ children }) {
  const [open, setOpen] = useStateTwk(false);
  return (
    <>
      <button onClick={() => setOpen(!open)} title="Ajustes de interfaz" style={{
        position: "fixed", bottom: 24, right: 24, zIndex: 100,
        width: 46, height: 46, borderRadius: 999,
        background: "var(--surface)", border: "1px solid var(--line)",
        boxShadow: "0 4px 20px -4px rgba(0,0,0,.2)",
        cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center",
        color: "var(--ink-3)",
      }}>
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <circle cx="12" cy="12" r="3"/>
          <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
        </svg>
      </button>
      {open && (
        <div style={{
          position: "fixed", bottom: 80, right: 24, zIndex: 100, width: 290,
          background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 16,
          boxShadow: "0 20px 60px -16px rgba(0,0,0,.35)", overflow: "hidden",
          animation: "g-pop .15s ease",
        }}>
          <div style={{ padding: "14px 16px 12px", borderBottom: "1px solid var(--line)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
            <h3 style={{ margin: 0, fontSize: 14, fontWeight: 700, color: "var(--ink)" }}>Ajustes de interfaz</h3>
            <button onClick={() => setOpen(false)} style={{ border: "none", background: "transparent", cursor: "pointer", color: "var(--ink-3)", padding: 4, borderRadius: 7, display: "flex" }}>
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M18 6L6 18M6 6l12 12"/></svg>
            </button>
          </div>
          <div style={{ padding: "12px 16px 16px", display: "flex", flexDirection: "column", gap: 14 }}>
            {children}
          </div>
        </div>
      )}
    </>
  );
}

function TweakSection({ label }) {
  return (
    <div style={{ fontSize: 10.5, fontWeight: 800, letterSpacing: ".06em", textTransform: "uppercase", color: "var(--ink-3)", paddingTop: 6, marginTop: 2, borderTop: "1px solid var(--line)" }}>
      {label}
    </div>
  );
}

function TweakColor({ label, value, options, onChange }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--ink-2)" }}>{label}</span>
      <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
        {options.map((c) => (
          <button key={c} onClick={() => onChange(c)} title={c} style={{
            width: 28, height: 28, borderRadius: 99, background: c, border: "none", cursor: "pointer",
            boxShadow: value === c ? `0 0 0 2px var(--surface), 0 0 0 4px ${c}` : "none",
            transition: "box-shadow .15s",
          }} />
        ))}
      </div>
    </div>
  );
}

function TweakToggle({ label, value, onChange }) {
  return (
    <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12 }}>
      <span style={{ fontSize: 13, color: "var(--ink-2)" }}>{label}</span>
      <button onClick={() => onChange(!value)} style={{
        width: 42, height: 24, borderRadius: 99, border: "none", cursor: "pointer",
        background: value ? "var(--accent)" : "var(--line-2)", position: "relative",
        transition: "background .2s", flexShrink: 0,
      }}>
        <span style={{
          position: "absolute", top: 3, left: value ? 21 : 3, width: 18, height: 18,
          borderRadius: 99, background: "#fff", transition: "left .2s",
          boxShadow: "0 1px 4px rgba(0,0,0,.25)",
        }} />
      </button>
    </div>
  );
}

function TweakRadio({ label, value, options, onChange }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--ink-2)" }}>{label}</span>
      <div style={{ display: "flex", gap: 5 }}>
        {options.map((o) => (
          <button key={o} onClick={() => onChange(o)} style={{
            flex: 1, padding: "7px 4px", borderRadius: 8, border: "1px solid",
            borderColor: value === o ? "var(--accent)" : "var(--line)",
            background: value === o ? "var(--accent-soft)" : "var(--field)",
            color: value === o ? "var(--accent-ink)" : "var(--ink-3)",
            cursor: "pointer", fontFamily: "inherit", fontSize: 12, fontWeight: 600,
            textTransform: "capitalize",
          }}>{o}</button>
        ))}
      </div>
    </div>
  );
}

function TweakSelect({ label, value, options, onChange }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <span style={{ fontSize: 12.5, fontWeight: 600, color: "var(--ink-2)" }}>{label}</span>
      <select value={value} onChange={(e) => onChange(e.target.value)} className="g-input" style={{ ...window.inputStyle, fontSize: 13, padding: "7px 10px" }}>
        {options.map((o) => <option key={o} value={o}>{o}</option>)}
      </select>
    </div>
  );
}

Object.assign(window, { useTweaks, TweaksPanel, TweakSection, TweakColor, TweakToggle, TweakRadio, TweakSelect });
