// screens-brand.jsx — Branding. Vault's living style guide.
//
// WHAT THIS SCREEN IS FOR
// Every colour, radius, shadow and type step in Vault, on one page, rendered
// from the same source the app renders from. It is a mirror, not a copy: the
// swatches read their values out of the live document via VaultTheme.get(), and
// the component samples below are the real shared classes from theme.css. If a
// token changes in theme.css, this screen changes with it. If a screen stops
// matching this page, that screen is wrong.
//
// The catalogue (which tokens exist, how they group, what each is for) lives in
// lib-theme.js. Adding a token there and in theme.css is all it takes to appear
// here — there is no list in this file to keep in step.
//
// WHY IT IS READ-ONLY
// Editing tokens live would need a theme_tokens table, RLS, a write path and a
// resolution order against the stylesheet. That is worth building, but it is a
// different job from establishing the system, and an editable style guide with
// no system underneath it just moves the drift somewhere new. Read-only first.

(function () {
  const { useState, useEffect } = React;

  const T = () => window.VaultTheme || { GROUPS: [], SHAPE: [], ELEVATION: [], TYPE: [], WEIGHT: [] };
  const A = (t, n, over) => (window.VaultAlpha ? window.VaultAlpha(t, n, over) : "transparent");

  // A token's resolved value, re-read whenever the theme flips. data-theme is on
  // <html>, so a MutationObserver on the attribute is what makes this live.
  function useThemeTick() {
    const [tick, setTick] = useState(0);
    useEffect(() => {
      const el = document.documentElement;
      const obs = new MutationObserver(() => setTick(t => t + 1));
      obs.observe(el, { attributes: true, attributeFilter: ["data-theme", "data-density"] });
      return () => obs.disconnect();
    }, []);
    return tick;
  }

  function Swatch({ token, note, tick }) {
    const val = window.VaultTheme ? window.VaultTheme.get(token) : "";
    const isAlpha = /rgba|color-mix/.test(val);
    return (
      <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "7px 0",
        borderBottom: "1px solid var(--line-2)" }}>
        {/* Checkerboard behind the chip so alpha tokens read as alpha. */}
        <span style={{
          width: 34, height: 34, borderRadius: "var(--r-chip)", flexShrink: 0,
          border: "1px solid var(--line-strong)",
          backgroundColor: "var(" + token + ")",
          backgroundImage: isAlpha
            ? "linear-gradient(45deg,var(--surface-3) 25%,transparent 25%,transparent 75%,var(--surface-3) 75%),linear-gradient(45deg,var(--surface-3) 25%,transparent 25%,transparent 75%,var(--surface-3) 75%)"
            : "none",
          backgroundSize: "10px 10px", backgroundPosition: "0 0, 5px 5px",
        }}/>
        <span style={{ minWidth: 0, flex: 1 }}>
          <span style={{ display: "block", fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)",
            color: "var(--ink)", fontWeight: "var(--w-medium)" }}>{token}</span>
          <span style={{ display: "block", fontSize: "var(--t-small)", color: "var(--muted)" }}>{note}</span>
        </span>
        <span style={{ fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)", color: "var(--muted-2)",
          whiteSpace: "nowrap", maxWidth: 190, overflow: "hidden", textOverflow: "ellipsis" }}>{val}</span>
      </div>
    );
  }

  function Card({ title, note, children }) {
    return (
      <section className="v-card" style={{ padding: "14px 16px", marginBottom: 14 }}>
        <div className="v-h3" style={{ marginBottom: note ? 2 : 10 }}>{title}</div>
        {note && <div style={{ fontSize: "var(--t-small)", color: "var(--muted)", marginBottom: 10,
          maxWidth: 720, lineHeight: 1.5 }}>{note}</div>}
        {children}
      </section>
    );
  }

  function BrandScreen() {
    const tick = useThemeTick();
    const th = T();

    return (
      <div style={{ background: "var(--bg)", minHeight: "100%", color: "var(--ink)", padding: "18px 22px" }}>
        <div className="v-kicker" style={{ marginBottom: 4 }}>Administrative Controls</div>
        <h1 className="v-h1" style={{ marginBottom: 4 }}>Branding</h1>
        <div style={{ fontSize: "var(--t-body)", color: "var(--ink-3)", maxWidth: 760,
          lineHeight: 1.55, marginBottom: 18 }}>
          Every token Vault ships, read live from <code style={{ fontFamily: "var(--f-mono)",
          fontSize: "var(--t-micro)" }}>theme.css</code>. Screens reference these; they do not declare
          their own values. A hex literal in a screen file is a bug — it will not follow the theme.
        </div>

        {/* ---- colour ------------------------------------------------------ */}
        {th.GROUPS.map(g => (
          <Card key={g.id} title={g.label} note={g.note}>
            <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill,minmax(330px,1fr))",
              gap: "0 26px" }}>
              {g.tokens.map(([token, note]) => (
                <Swatch key={token} token={token} note={note} tick={tick}/>
              ))}
            </div>
          </Card>
        ))}

        {/* ---- shape ------------------------------------------------------- */}
        <Card title="Shape" note="Four radius steps replacing the twenty in use before the sweep.">
          <div style={{ display: "flex", gap: 22, flexWrap: "wrap" }}>
            {th.SHAPE.map(([token, note]) => (
              <div key={token} style={{ textAlign: "center" }}>
                <div style={{ width: 74, height: 54, background: "var(--accent-soft)",
                  border: "1px solid var(--accent-soft-2)", borderRadius: "var(" + token + ")",
                  marginBottom: 6 }}/>
                <div style={{ fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)",
                  color: "var(--ink)" }}>{token}</div>
                <div style={{ fontSize: "var(--t-small)", color: "var(--muted)", maxWidth: 120 }}>{note}</div>
              </div>
            ))}
          </div>
        </Card>

        {/* ---- elevation --------------------------------------------------- */}
        <Card title="Elevation"
          note="Cards are flat. Only layers that float above the page carry a shadow — if something sitting IN the page needs separation, the answer is a border.">
          <div style={{ display: "flex", gap: 22, flexWrap: "wrap", padding: "6px 0 10px" }}>
            {th.ELEVATION.map(([token, note]) => (
              <div key={token} style={{ textAlign: "center" }}>
                <div style={{ width: 128, height: 58, background: "var(--surface)",
                  border: "1px solid var(--line)", borderRadius: "var(--r-card)",
                  boxShadow: "var(" + token + ")", marginBottom: 8 }}/>
                <div style={{ fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)",
                  color: "var(--ink)" }}>{token}</div>
                <div style={{ fontSize: "var(--t-small)", color: "var(--muted)", maxWidth: 150,
                  margin: "0 auto" }}>{note}</div>
              </div>
            ))}
          </div>
        </Card>

        {/* ---- type -------------------------------------------------------- */}
        <Card title="Type" note="Eight steps replacing fifteen ad-hoc sizes, and three weights replacing six.">
          {th.TYPE.map(([token, note]) => (
            <div key={token} style={{ display: "flex", alignItems: "baseline", gap: 14,
              padding: "6px 0", borderBottom: "1px solid var(--line-2)" }}>
              <span style={{ fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)",
                color: "var(--muted)", width: 108, flexShrink: 0 }}>{token}</span>
              <span style={{ fontSize: "var(" + token + ")", color: "var(--ink)",
                fontWeight: "var(--w-medium)", flex: 1 }}>
                Outstanding Process Actions
              </span>
              <span style={{ fontSize: "var(--t-small)", color: "var(--muted)" }}>{note}</span>
            </div>
          ))}
          <div style={{ display: "flex", gap: 24, marginTop: 12 }}>
            {th.WEIGHT.map(([token, label]) => (
              <span key={token} style={{ fontSize: "var(--t-card)", fontWeight: "var(" + token + ")" }}>
                {label}
                <span style={{ fontFamily: "var(--f-mono)", fontSize: "var(--t-micro)",
                  color: "var(--muted)", marginLeft: 7 }}>{token}</span>
              </span>
            ))}
          </div>
        </Card>

        {/* ---- kicker ------------------------------------------------------ */}
        <Card title="Kicker"
          note="One spec, replacing the forty-eight that were in use. Tracking is in px, as the reference screen sets it.">
          <div className="v-kicker">Call Batches · 1,178</div>
        </Card>

        {/* ---- components -------------------------------------------------- */}
        <Card title="Components"
          note="The shared classes from theme.css, rendered here exactly as a screen would get them.">

          <div className="v-kicker" style={{ marginBottom: 8 }}>Headings</div>
          <div className="v-h1" style={{ marginBottom: 4 }}>Page title</div>
          <div className="v-h2" style={{ marginBottom: 4 }}>Section heading</div>
          <div className="v-h3" style={{ marginBottom: 16 }}>Card title</div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Buttons</div>
          <div style={{ display: "flex", gap: 9, flexWrap: "wrap", marginBottom: 16 }}>
            <button className="btn primary">Push to Actions</button>
            <button className="btn">Secondary</button>
            <button className="btn ghost">Ghost</button>
            <button className="btn sm">Small</button>
            <button className="btn" disabled>Disabled</button>
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Pills</div>
          <div style={{ display: "flex", gap: 8, flexWrap: "wrap", marginBottom: 16 }}>
            {[["ok", "On track"], ["warn", "Due soon"], ["risk", "Overdue"],
              ["info", "Informational"], ["accent", "Selected"]].map(([k, label]) => (
              <span key={k} className={"pill " + k}><span className="dot"/>{label}</span>
            ))}
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Priority</div>
          <div style={{ display: "flex", gap: 8, marginBottom: 16 }}>
            {[["--prio-high", "High"], ["--prio-med", "Medium"], ["--prio-low", "Low"]].map(([t, l]) => (
              <span key={t} style={{ display: "inline-flex", alignItems: "center", gap: 6,
                fontSize: "var(--t-micro)", fontWeight: "var(--w-medium)",
                padding: "4px 10px", borderRadius: "var(--r-pill)",
                color: "var(" + t + ")", background: A(t, .10) }}>
                <span style={{ width: 6, height: 6, borderRadius: "50%", background: "var(" + t + ")" }}/>
                {l}
              </span>
            ))}
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Sequential ramp — C1 to C9</div>
          <div style={{ display: "flex", gap: 5, marginBottom: 16, flexWrap: "wrap" }}>
            {[1,2,3,4,5,6,7,8,9].map(n => (
              <span key={n} style={{ width: 40, padding: "6px 0", textAlign: "center",
                borderRadius: "var(--r-chip)", background: "var(--seq-" + n + ")",
                color: "#fff", fontSize: "var(--t-micro)", fontWeight: "var(--w-medium)",
                fontFamily: "var(--f-mono)" }}>C{n}</span>
            ))}
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Meeting types</div>
          <div style={{ display: "flex", gap: 8, marginBottom: 16, flexWrap: "wrap" }}>
            {[["--evt-update","Update"],["--evt-conf","Conf"],["--evt-intro","Intro"],
              ["--evt-call","Call"],["--evt-meeting","Mtg"],["--evt-ooo","OOO"]].map(([t,l]) => (
              <span key={t} style={{ display: "inline-flex", alignItems: "center", gap: 6,
                fontSize: "var(--t-micro)", color: "var(--ink-2)" }}>
                <span style={{ width: 3, height: 15, borderRadius: 2, background: "var(" + t + ")" }}/>
                {l}
              </span>
            ))}
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>User Display</div>
          <div style={{ display: "flex", gap: 26, flexWrap: "wrap", marginBottom: 16 }}>
            {[[{}, "Plain"], [{ presence: "online" }, "Online"], [{ presence: "away" }, "Away"],
              [{ presence: "offline" }, "Offline"], [{ verified: true }, "Verified"],
              [{ badge: 2 }, "Notification"]].map(([props, label]) => (
              <div key={label} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 8 }}>
                {window.Avatar
                  ? <window.Avatar id={window.VAULT_FIRM && window.VAULT_FIRM.PEOPLE && window.VAULT_FIRM.PEOPLE[0] ? window.VAULT_FIRM.PEOPLE[0].id : "bscott"} px={26} {...props}/>
                  : null}
                <span style={{ fontSize: "var(--t-small)", color: "var(--muted)" }}>{label}</span>
              </div>
            ))}
          </div>
          <div style={{ fontSize: "var(--t-small)", color: "var(--muted)", marginBottom: 16, maxWidth: 620, lineHeight: 1.5 }}>
            Presence has no data source yet — it needs a heartbeat table and a writer. The states
            render so the capability can be turned on without touching call sites. Nothing defaults
            to Offline: claiming someone is offline with no source would be a lie.
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Inputs</div>
          <div style={{ display: "flex", gap: 9, flexWrap: "wrap", marginBottom: 16 }}>
            <window.VaultSearch value="" onChange={() => {}} placeholder="Search targets"/>
            <select><option>All Sets</option><option>Orphan Calls</option></select>
            <input type="date"/>
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Empty state</div>
          <div className="v-card" style={{ marginBottom: 16 }}>
            <div className="v-empty">Nothing matches this filter.</div>
          </div>

          <div className="v-kicker" style={{ marginBottom: 8 }}>Table</div>
          <div className="v-card" style={{ overflow: "hidden", marginBottom: 4 }}>
            <div className="v-thead" style={{ display: "grid",
              gridTemplateColumns: "2fr 1fr 1fr", padding: "9px 12px" }}>
              <span>Project</span><span>Code</span><span>Owner</span>
            </div>
            {[["Owen Equipment", "C2", "gburton"], ["Mo-Vac", "C4", "jscheftz"]].map(r => (
              <div key={r[0]} className="v-trow" style={{ display: "grid",
                gridTemplateColumns: "2fr 1fr 1fr", padding: "9px 12px" }}>
                <span>{r[0]}</span>
                <span style={{ fontFamily: "var(--f-mono)" }}>{r[1]}</span>
                <span>{r[2]}</span>
              </div>
            ))}
          </div>
          <div style={{ fontSize: "var(--t-small)", color: "var(--muted)" }}>
            The same classes drive real tables and the CSS-grid tables, so both render identically.
          </div>
        </Card>

        <div style={{ fontSize: "var(--t-small)", color: "var(--muted)", padding: "4px 0 24px",
          maxWidth: 760, lineHeight: 1.55 }}>
          To add a token: declare it in <code style={{ fontFamily: "var(--f-mono)" }}>theme.css</code> with
          its dark-mode counterpart, then add its name and description to the catalogue in
          <code style={{ fontFamily: "var(--f-mono)" }}> lib-theme.js</code>. It appears here on its own.
        </div>
      </div>
    );
  }

  window.BrandScreen = BrandScreen;
})();
