// Settings panel — reuses the About side-panel chrome. Theme section only (v1f).
// Theme persisted in localStorage "rd:theme" and applied as data-theme on <html>.

const { useState: useStateSet, useEffect: useEffectSet } = React;

const THEMES = [
  { id: "linen", name: "Linen", note: "Warm off-white, deep teal", sw: ["#F7F5EE", "#F2EFE6", "#1B1F1D", "#2F6661"] },
  { id: "slate", name: "Slate", note: "Cool grey, muted indigo",   sw: ["#F4F5F7", "#E9EBEF", "#1A1C20", "#4A5BA6"] },
  { id: "paper", name: "Paper", note: "Pure white, terracotta",    sw: ["#FFFFFF", "#F7F7F7", "#2B2520", "#B85C38"] },
  { id: "dusk",  name: "Dusk",  note: "Warm-to-cool gradient, plum", sw: ["#F6F1EE", "#E4E8F2", "#2A2530", "#6B4E7A"], gradient: "linear-gradient(160deg, #F6F1EE 0%, #ECE6F0 55%, #E4E8F2 100%)" },
  { id: "tide",  name: "Tide",  note: "Sea-glass gradient, deep sea", sw: ["#EEF6F5", "#E9EEF8", "#1F2B2E", "#2E6E73"], gradient: "linear-gradient(160deg, #EEF6F5 0%, #E6F0F3 50%, #E9EEF8 100%)" },
  { id: "night", name: "Night", note: "Dark, sage",                sw: ["#16181C", "#1E2126", "#E8E6E1", "#7FA88F"] },
];
const THEME_KEY = "rd:theme";

function readTheme() {
  try { const t = localStorage.getItem(THEME_KEY); return THEMES.some(x => x.id === t) ? t : "linen"; } catch { return "linen"; }
}
function applyTheme(id) {
  if (id === "linen") document.documentElement.removeAttribute("data-theme");
  else document.documentElement.setAttribute("data-theme", id);
}
// Apply immediately on script load so there's no flash.
applyTheme(readTheme());

function SettingsPanel({ open, onClose }) {
  const [theme, setTheme] = useStateSet(readTheme);
  useEffectSet(() => { applyTheme(theme); try { localStorage.setItem(THEME_KEY, theme); } catch {} }, [theme]);
  useEffectSet(() => {
    if (!open) return;
    function onKey(e) { if (e.key === "Escape") onClose(); }
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [open, onClose]);

  return (
    <>
      <div className={`about-overlay ${open ? "open" : ""}`} onClick={onClose} aria-hidden="true" />
      <aside className={`about-panel ${open ? "open" : ""}`} aria-label="Settings" aria-hidden={!open}>
        <header className="about-head">
          <div>
            <div className="about-eyebrow">Settings</div>
            <h2 className="about-title">Preferences</h2>
          </div>
          <button type="button" className="icon-btn" aria-label="Close" onClick={onClose}>
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
          </button>
        </header>
        <div className="about-body">
          <section className="about-section">
            <h3>Theme</h3>
            <p className="settings-help">Changes surfaces, text, and accent. Status colors for BMI, food security, and flags stay the same in every theme.</p>
            <div className="theme-list" role="radiogroup" aria-label="Theme">
              {THEMES.map(t => (
                <button
                  key={t.id}
                  type="button"
                  role="radio"
                  aria-checked={theme === t.id}
                  className={`theme-row ${theme === t.id ? "is-active" : ""}`}
                  onClick={() => setTheme(t.id)}
                >
                  <span className="theme-swatches" aria-hidden="true">
                    {t.sw.map((c, i) => <span key={i} className="theme-sw" style={{ background: (i === 0 && t.gradient) ? t.gradient : c }} />)}
                  </span>
                  <span className="theme-text">
                    <span className="theme-name">{t.name}{t.id === "linen" && <span className="theme-default"> · default</span>}</span>
                    <span className="theme-note">{t.note}</span>
                  </span>
                  {theme === t.id && (
                    <svg className="theme-check" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 6 9 17l-5-5"/></svg>
                  )}
                </button>
              ))}
            </div>
          </section>
        </div>
      </aside>
    </>
  );
}

Object.assign(window, { SettingsPanel, THEMES });
