// hydor-page-misc.jsx — Reports (P&L), Activity (audit), Settings.

/* ============================ REPORTS ============================ */
function ReportsPage() {
  const s = useHydor();
  const d = useDerive();
  const push = useToast();
  const role = useRole();
  const canMoney = Hydor.can(role, "seeCostProfit");
  const canFin = Hydor.can(role, "seeFinance");
  const years = d.yearList.length ? d.yearList : [new Date().getFullYear()];
  const [year, setYear] = React.useState(years[years.length - 1]);
  React.useEffect(() => { if (!years.includes(year)) setYear(years[years.length - 1]); }, [years.join()]);

  const qkeys = [1, 2, 3, 4].map((n) => year + "-Q" + n);
  const qd = qkeys.map((k) => d.quarters[k] || { sales: 0, cogs: 0, gp: 0, fixed: 0, op: 0, costs: 0, net: 0, taxCollected: 0, taxPaid: 0 });
  const tot = qd.reduce((a, x) => { Object.keys(x).forEach((k) => { if (typeof x[k] === "number") a[k] = (a[k] || 0) + x[k]; }); return a; }, {});
  tot.taxDue = (tot.taxCollected || 0) - (tot.taxPaid || 0);
  const alloc = tot.net > 10000 ? 7 : tot.net > 5000 ? 5 : 0;
  const allocAmt = tot.net * (alloc / 100);
  const catEntries = Object.entries(d.costByCategory || {}).sort((a, b) => b[1] - a[1]);
  const catMax = catEntries.length ? catEntries[0][1] : 1;

  const lines = [
    ["Sales revenue", "sales", false], ["Cost of goods sold (FIFO)", "cogs", true],
    ["Gross profit", "gp", false, true], ["Fixed costs", "fixed", true], ["Operational costs", "op", true],
    ["Net profit", "net", false, true],
  ];
  const exportPL = () => {
    const rows = [["HYDOR Profit & Loss", year], [], ["Line", "Q1", "Q2", "Q3", "Q4", "Year"]];
    lines.forEach(([label, key]) => rows.push([label, ...qd.map((q) => (q[key] || 0).toFixed(3)), (tot[key] || 0).toFixed(3)]));
    rows.push([], ["GST collected", ...qd.map((q) => (q.taxCollected || 0).toFixed(3)), (tot.taxCollected || 0).toFixed(3)]);
    rows.push(["GST paid (input)", ...qd.map((q) => (q.taxPaid || 0).toFixed(3)), (tot.taxPaid || 0).toFixed(3)]);
    rows.push(["GST due", "", "", "", "", tot.taxDue.toFixed(3)]);
    Hydor.exportCSV(`hydor-pnl-${year}.csv`, rows); push("P&L exported to CSV.");
  };

  return (
    <div className="content-narrow">
      <div className="toolbar no-print">
        <select className="sel" style={{ width: 120 }} value={year} onChange={(e) => setYear(+e.target.value)}>
          {years.map((y) => <option key={y} value={y}>{y}</option>)}
        </select>
        <div className="grow" />
        <Btn icon="download" onClick={exportPL}>Export CSV</Btn>
        <Btn icon="print" onClick={() => window.print()}>Print / PDF</Btn>
      </div>

      <div className="hx-panel hx-panel-pad" style={{ marginBottom: 14 }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", marginBottom: 22 }}>
          <div>
            <div className="side-name" style={{ fontSize: 22, letterSpacing: ".1em" }}>HYDOR</div>
            <div className="hx-sub" style={{ marginTop: 4 }}>{s.meta.company} · {canMoney ? "Profit & Loss Statement" : "Department Performance Report"}</div>
          </div>
          <div style={{ textAlign: "right" }}><div className="hx-eyebrow">Fiscal Year</div><div className="hx-num" style={{ fontSize: 24, fontWeight: 600 }}>{year}</div></div>
        </div>
        {canMoney ? (
        <table className="hx-table">
          <thead><tr><th>Line item</th><th className="r">Q1</th><th className="r">Q2</th><th className="r">Q3</th><th className="r">Q4</th><th className="r">Full Year</th></tr></thead>
          <tbody>
            {lines.map(([label, key, neg, strong]) => (
              <tr key={key} style={strong ? { background: "#ffffff04" } : null}>
                <td style={{ fontWeight: strong ? 700 : 500 }}>{label}</td>
                {qd.map((q, i) => <td key={i} className="r num" style={{ color: neg ? "var(--text-2)" : "var(--text)" }}>{neg && q[key] ? "(" + Hydor.fmtMoney0(q[key]) + ")" : Hydor.fmtMoney0(q[key] || 0)}</td>)}
                <td className="r num" style={{ fontWeight: 700, color: key === "net" ? (tot.net >= 0 ? "var(--pos)" : "var(--neg)") : neg ? "var(--text-2)" : "var(--text)" }}>{neg && tot[key] ? "(" + Hydor.fmtMoney0(tot[key]) + ")" : Hydor.fmtMoney0(tot[key] || 0)}</td>
              </tr>
            ))}
          </tbody>
        </table>
        ) : (
          <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "14px 16px", background: "var(--bg-2)", border: "1px solid var(--border)", borderRadius: 11, fontSize: 13, color: "var(--text-2)" }}>
            <Icon name="lock" size={16} style={{ color: "var(--text-3)" }} />
            Revenue, profit and tax figures are restricted to the founder and co-founder. Your report below shows team and department performance by volume and target attainment.
          </div>
        )}
      </div>

      {/* department performance — visible to all report viewers */}
      <div className="hx-panel hx-panel-pad" style={{ marginBottom: 14 }}>
        <h3 className="hx-h2" style={{ marginBottom: 14 }}>Department performance (this month)</h3>
        <table className="hx-table">
          <thead><tr><th>Department</th><th className="r">Headcount</th><th className="r">Target</th><th style={{ width: 200 }}>Attainment</th>{canMoney && <th className="r">Sales (this month)</th>}{canMoney && <th className="r">GP (all-time)</th>}</tr></thead>
          <tbody>
            {d.departments.sort((a, b) => b.monthSales - a.monthSales).map((dep) => (
              <tr key={dep.department}>
                <td style={{ fontWeight: 500 }}>{dep.department}</td>
                <td className="r num" style={{ color: "var(--text-2)" }}>{dep.headcount}</td>
                <td className="r num" style={{ color: "var(--text-2)" }}>{dep.target > 0 ? (dep.metric === "visits" ? dep.target + " visits" : Hydor.fmtMoney0(dep.target)) : "—"}</td>
                <td>{dep.target > 0 ? (
                  <div className="hx-perfbar"><div className="hx-perfbar-track"><div className={"hx-perfbar-fill " + ((dep.attainment || 0) >= 1 ? "over" : "under")} style={{ width: Math.min(100, (dep.attainment || 0) * 100) + "%" }} /></div>
                    <div className={"hx-perfbar-val " + ((dep.attainment || 0) >= 1 ? "over" : "under")}>{dep.attainment != null ? (dep.attainment * 100).toFixed(0) + "%" : "—"}</div></div>
                ) : <span style={{ color: "var(--text-3)", fontSize: 12 }}>—</span>}</td>
                {canMoney && <td className="r num">{dep.metric === "visits" ? (dep.monthVisits + " visits") : Hydor.fmtMoney0(dep.monthSales)}</td>}
                {canMoney && <td className="r num" style={{ color: "var(--pos)" }}>{Hydor.fmtMoney0(dep.gp)}</td>}
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      {/* cost by category — finance roles */}
      {canFin && catEntries.length > 0 && (
        <div className="hx-panel hx-panel-pad" style={{ marginBottom: 14 }}>
          <h3 className="hx-h2" style={{ marginBottom: 14 }}>Cost by category{s.meta.csIncentive ? "" : ""} · {Hydor.fmtMoney0(catEntries.reduce((a, x) => a + x[1], 0))} JOD total</h3>
          {catEntries.map(([cat, amt]) => (
            <div key={cat} style={{ marginBottom: 11 }}>
              <div style={{ display: "flex", justifyContent: "space-between", fontSize: 12.5, marginBottom: 4 }}>
                <span style={{ color: "var(--text-2)" }}>{cat}</span><span className="hx-num">{Hydor.fmtMoney0(amt)} JOD</span>
              </div>
              <div className="hx-progress"><i style={{ width: (amt / catMax * 100) + "%" }} /></div>
            </div>
          ))}
          <p className="hx-sub" style={{ marginTop: 8, fontSize: 11.5 }}>Recurring costs (rent, salaries, utilities) post automatically — manage them in Settings → Recurring cost registry.</p>
        </div>
      )}

      {canMoney && (
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14 }}>
        <div className="hx-panel hx-panel-pad">
          <h3 className="hx-h2" style={{ marginBottom: 14 }}>GST / Sales Tax ({(d.taxRate * 100).toFixed(0)}%)</h3>
          <RowKV label="Output GST (collected on sales)" value={Hydor.fmtMoney(tot.taxCollected || 0)} />
          <RowKV label="Input GST (paid, deductible)" value={Hydor.fmtMoney(tot.taxPaid || 0)} />
          <div style={{ borderTop: "1px solid var(--border)", margin: "10px 0" }} />
          <RowKV label="Net GST due" value={Hydor.fmtMoney(tot.taxDue)} strong color={tot.taxDue >= 0 ? "var(--warn)" : "var(--pos)"} />
        </div>
        <div className="hx-panel hx-panel-pad">
          <h3 className="hx-h2" style={{ marginBottom: 14 }}>Profit Allocation</h3>
          <RowKV label="Net profit" value={Hydor.fmtMoney0(tot.net) + " JOD"} />
          <RowKV label={`Development fund (${alloc}%)`} value={Hydor.fmtMoney0(allocAmt) + " JOD"} color="var(--accent-3)" />
          <RowKV label="Retained" value={Hydor.fmtMoney0(tot.net - allocAmt) + " JOD"} strong color="var(--pos)" />
          <p className="hx-sub" style={{ marginTop: 12, fontSize: 11.5 }}>Allocation tier: 7% above 10k net · 5% above 5k · else 0%.</p>
        </div>
      </div>
      )}
    </div>
  );
}
function RowKV({ label, value, strong, color }) {
  return (
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", padding: "7px 0" }}>
      <span style={{ fontSize: 13, color: "var(--text-2)", fontWeight: strong ? 600 : 400 }}>{label}</span>
      <span className="hx-num" style={{ fontSize: strong ? 16 : 14, fontWeight: strong ? 700 : 500, color: color || "var(--text)" }}>{value}</span>
    </div>
  );
}

/* ============================ ACTIVITY (audit) ============================ */
function ActivityPage() {
  const s = useHydor();
  const grouped = {};
  s.audit.forEach((a) => { const day = a.ts.slice(0, 10); (grouped[day] = grouped[day] || []).push(a); });
  const days = Object.keys(grouped).sort().reverse();
  return (
    <div className="content-narrow">
      {s.audit.length === 0 ? (
        <div className="hx-panel"><Empty icon="activity" title="No activity yet">Every change you make — products, transactions, costs, settings — is logged here for a full audit trail.</Empty></div>
      ) : (
        <div className="hx-panel hx-panel-pad">
          {days.map((day) => (
            <div key={day} style={{ marginBottom: 22 }}>
              <div className="hx-eyebrow" style={{ marginBottom: 12 }}>{Hydor.fmtDate(day)}</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 2 }}>
                {grouped[day].map((a) => (
                  <div key={a.id} style={{ display: "flex", alignItems: "center", gap: 12, padding: "9px 0", borderBottom: "1px solid var(--border-2)" }}>
                    <span style={{ width: 7, height: 7, borderRadius: "50%", background: "var(--accent)", flex: "none" }} />
                    <span style={{ fontSize: 13.5, fontWeight: 500, width: 200 }}>{a.action}</span>
                    <span style={{ fontSize: 13, color: "var(--text-2)", flex: 1 }}>{a.detail}</span>
                    <span className="hx-num" style={{ fontSize: 12, color: "var(--text-3)" }}>{new Date(a.ts).toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit" })}</span>
                  </div>
                ))}
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

/* ============================ SETTINGS ============================ */
function SettingsPage() {
  const s = useHydor();
  const d = useDerive();
  const push = useToast();
  const [confirm, setConfirm] = React.useState(null);
  const fileRef = React.useRef();

  const onImport = (e) => {
    const file = e.target.files[0]; if (!file) return;
    const r = new FileReader();
    r.onload = () => { try { Hydor.importData(JSON.parse(r.result)); push("Backup restored."); } catch (err) { push(err.message, "err"); } };
    r.readAsText(file); e.target.value = "";
  };

  return (
    <div className="content-narrow" style={{ maxWidth: 820 }}>
      {/* business */}
      <SettingCard title="Business profile" desc="Shown on reports and invoices.">
        <div className="form-grid">
          <Field label="Company name"><input className="inp" value={s.meta.company} onChange={(e) => Hydor.setMeta({ company: e.target.value })} /></Field>
          <Field label="GST / Sales-tax rate" hint="Jordan standard is 16%">
            <Affix affix="%"><input className="inp num" type="number" value={(s.meta.taxRate * 100).toFixed(0)} onChange={(e) => Hydor.setMeta({ taxRate: Hydor.num(e.target.value) / 100 })} style={{ paddingRight: 30 }} /></Affix>
          </Field>
          <Field label="Dead-stock threshold" hint="Days without a sale before flagged">
            <Affix affix="days"><input className="inp num" type="number" value={s.meta.deadStockDays} onChange={(e) => Hydor.setMeta({ deadStockDays: Hydor.num(e.target.value, 90) })} style={{ paddingRight: 48 }} /></Affix>
          </Field>
          <Field label="Default reorder point"><input className="inp num" type="number" value={s.meta.defaultReorder} onChange={(e) => Hydor.setMeta({ defaultReorder: Hydor.num(e.target.value) })} /></Field>
        </div>
      </SettingCard>

      {/* security / access */}
      <SecuritySettings s={s} push={push} />

      {/* access levels overview */}
      <AccessCard />

      {/* recurring cost registry */}
      <RecurringSettings s={s} push={push} />

      {/* attendance rules */}
      <AttendanceRules s={s} push={push} />

      {/* cloud sync */}
      <CloudSettings push={push} />

      {/* devices & sync */}
      <DevicesCard s={s} push={push} fileRef={fileRef} onImport={onImport} />

      {/* period locking */}
      <SettingCard title="Period locking" desc="Lock a closed quarter so its transactions can't be edited or back-dated into.">
        {d.quarterKeys.filter((k) => { const q = d.quarters[k]; return q.sales || q.costs; }).length === 0
          ? <p className="hx-sub">No quarters with activity yet.</p>
          : (
            <div className="chip-row">
              {d.quarterKeys.filter((k) => { const q = d.quarters[k]; return q.sales || q.costs; }).map((k) => {
                const locked = s.meta.lockedQuarters.includes(k);
                return <button key={k} className={"chip" + (locked ? " on" : "")} onClick={() => { Hydor.toggleLock(k); push(locked ? k + " unlocked." : k + " locked."); }}>
                  <Icon name={locked ? "lock" : "unlock"} size={12} style={{ verticalAlign: "middle", marginRight: 5 }} />{k}
                </button>;
              })}
            </div>
          )}
      </SettingCard>

      {/* data */}
      <SettingCard title="Backup & data" desc="HYDOR stores everything in this browser. Export regularly to keep a safe copy.">
        <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
          <Btn icon="download" onClick={() => { Hydor.exportBackup(); push("Backup downloaded."); }}>Export backup</Btn>
          <Btn icon="upload" onClick={() => fileRef.current.click()}>Restore from file</Btn>
          <input ref={fileRef} type="file" accept="application/json,.json" style={{ display: "none" }} onChange={onImport} />
          <div className="grow" />
          {s.transactions.length === 0 && s.products.length === 0
            ? <Btn onClick={() => setConfirm("demo")}>Load demo data</Btn>
            : <Btn variant="danger" icon="trash" onClick={() => setConfirm("reset")}>Reset all data</Btn>}
        </div>
        <p className="hx-sub" style={{ marginTop: 12, fontSize: 11.5 }}>
          {s.products.length} products · {s.transactions.filter((t) => !t.deleted).length} transactions · {s.costs.filter((c) => !c.deleted).length} costs · {s.parties.length} contacts
        </p>
      </SettingCard>

      {confirm === "reset" && <Confirm title="Reset everything?" danger confirmLabel="Erase all data"
        body="This permanently deletes all products, transactions, costs and contacts from this browser. Export a backup first if you might need it."
        onConfirm={() => { Hydor.resetData(); push("All data cleared."); }} onClose={() => setConfirm(null)} />}
      {confirm === "demo" && <Confirm title="Load demo data?" confirmLabel="Load demo"
        body="Fills HYDOR with a sample year of products, purchases, sales and costs so you can explore every feature. You can reset later."
        onConfirm={() => { Hydor.loadDemo(); push("Demo data loaded."); }} onClose={() => setConfirm(null)} />}
    </div>
  );
}
const ROLE_OPTS = Hydor.ROLE_KEYS.map((k) => [k, Hydor.ROLE_INFO[k].label]);
const roleColor = { owner: "purple", cofounder: "blue", manager: "blue", teamlead: "blue", staff: "green", accountant: "amber" };
const DEPTS = ["Management", "Sales", "Customer Success", "Call Center", "Installation & Technical", "Finance", "Operations"];

function SecuritySettings({ s, push }) {
  const auth = s.meta.auth || { enabled: false };
  const me = Hydor.currentUser();
  const [form, setForm] = React.useState(null);   // {editing} | {} (new)
  const [del, setDel] = React.useState(null);
  const [showPw, setShowPw] = React.useState({});

  const users = s.users.slice().sort((a, b) => {
    const order = { owner: 0, cofounder: 1, manager: 2, teamlead: 3, staff: 4, accountant: 5 };
    return ((order[a.role] ?? 6) - (order[b.role] ?? 6)) || a.name.localeCompare(b.name);
  });

  return (
    <SettingCard title="User accounts & access" desc="Create an account for each person, give them a role and password, and they sign in as themselves. Sales staff get an employee code that tags their sales.">
      <label style={{ display: "flex", alignItems: "center", gap: 10, fontSize: 13.5, cursor: "pointer", marginBottom: 20 }}>
        <input type="checkbox" checked={auth.enabled} onChange={(e) => Hydor.setAuthEnabled(e.target.checked)} />
        <span>Require login to open HYDOR <span style={{ color: "var(--text-3)", fontSize: 12 }}>(when off, anyone opening the file gets full access)</span></span>
      </label>

      <div className="hx-panel" style={{ overflow: "hidden", border: "1px solid var(--border)", marginBottom: 14 }}>
        <table className="hx-table">
          <thead><tr><th>Account</th><th>Role</th><th>Code</th><th>Password</th><th>Status</th><th></th></tr></thead>
          <tbody>
            {users.map((u) => (
              <tr key={u.id}>
                <td><span className="hx-emp" style={{ gap: 10 }}><span className="hx-emp-badge">{u.name.slice(0, 1).toUpperCase()}</span><span style={{ fontWeight: 500 }}>{u.name}{me && me.id === u.id && <span style={{ color: "var(--text-3)", fontSize: 11.5, marginLeft: 6 }}>(you)</span>}</span></span></td>
                <td><Badge color={roleColor[u.role]}>{ROLE_OPTS.find((r) => r[0] === u.role)[1]}</Badge></td>
                <td>{u.code ? <span className="hx-code">{u.code}</span> : <span style={{ color: "var(--text-3)" }}>—</span>}</td>
                <td><span className="hx-num" style={{ fontSize: 12.5, color: "var(--text-2)" }}>{showPw[u.id] ? u.password : "••••••"}</span>
                  <span className="linkish" style={{ fontSize: 11, marginLeft: 8 }} onClick={() => setShowPw((x) => ({ ...x, [u.id]: !x[u.id] }))}>{showPw[u.id] ? "hide" : "show"}</span></td>
                <td>{u.active ? <Badge color="green" dot>active</Badge> : <Badge color="gray" dot>disabled</Badge>}</td>
                <td className="r no-print">
                  <div style={{ display: "flex", gap: 2, justifyContent: "flex-end" }}>
                    <Btn variant="ghost" sm icon="edit" onClick={() => setForm({ editing: u })} />
                    <Btn variant="ghost" sm icon="trash" onClick={() => setDel(u)} />
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <Btn variant="primary" icon="plus" onClick={() => setForm({})}>New account</Btn>
        <p className="hx-sub" style={{ fontSize: 11.5, margin: 0 }}>Access gate stored in this browser — good for keeping staff to their own view, not bank-grade encryption.</p>
      </div>

      {form && <UserForm editing={form.editing} onClose={() => setForm(null)} push={push} />}
      {del && <Confirm title="Delete account?" danger confirmLabel="Delete" body={`Delete the account "${del.name}"? They will no longer be able to sign in. Sales already recorded under their code stay in the ledger.`}
        onConfirm={() => { try { Hydor.deleteUser(del.id); push("Account deleted."); } catch (e) { push(e.message, "err"); } }} onClose={() => setDel(null)} />}
    </SettingCard>
  );
}

function UserForm({ editing, onClose, push }) {
  const [f, setF] = React.useState(editing
    ? { name: editing.name, role: editing.role, code: editing.code, department: editing.department || "Sales", password: editing.password, active: editing.active, salary: editing.salary || 0, monthlyTarget: editing.monthlyTarget || 0, teamTarget: editing.teamTarget || 0, reportsTo: editing.reportsTo || "", hireDate: editing.hireDate || "" }
    : { name: "", role: "staff", code: "", department: "Sales", password: "", active: true, salary: 0, monthlyTarget: 0, teamTarget: 0, reportsTo: "", hireDate: "" });
  const [show, setShow] = React.useState(false);
  const set = (k, v) => setF((p) => ({ ...p, [k]: v }));
  const teamLeads = Hydor.getState().users.filter((u) => u.active && u.role === "teamlead" && (!editing || u.id !== editing.id));
  const save = async () => {
    try {
      let rec;
      if (editing) {
        Hydor.updateUser(editing.id, f);
        rec = Hydor.getState().users.find((u) => u.id === editing.id);
        // sync to Railway accounts API so login works from all devices
        if (window.HYDOR_API_BASE && window.HydorAPI && window.HydorAPI.isAuthed()) {
          try { await window.HydorAPI.updateAccount(editing.id, { ...f }); } catch (e) { console.warn("Railway account sync failed:", e.message); }
        }
        push("Account updated.");
      } else {
        rec = Hydor.addUser(f);
        // sync to Railway accounts API so login works from all devices
        if (window.HYDOR_API_BASE && window.HydorAPI && window.HydorAPI.isAuthed()) {
          try { await window.HydorAPI.post("/accounts", { ...f, id: rec.id }); } catch (e) { console.warn("Railway account sync failed:", e.message); }
        }
        push(`Account for ${f.name.trim()} created.`);
      }
      onClose();
    } catch (e) { push(e.message, "err"); }
  };
  const gen = () => set("password", Math.random().toString(36).slice(2, 8));
  return (
    <Modal title={editing ? "Edit account" : "New account"} onClose={onClose} foot={<>
      <Btn variant="ghost" onClick={onClose}>Cancel</Btn>
      <Btn variant="primary" icon="check" onClick={save}>{editing ? "Save changes" : "Create account"}</Btn>
    </>}>
      <div className="form-grid">
        <Field label="Person's name" span2><input className="inp" value={f.name} onChange={(e) => set("name", e.target.value)} placeholder="e.g. Omar Haddad" autoFocus /></Field>
        <Field label="Access level" hint={Hydor.ROLE_INFO[f.role] ? Hydor.ROLE_INFO[f.role].desc : ""}>
          <select className="sel" value={f.role} onChange={(e) => set("role", e.target.value)}>
            {ROLE_OPTS.map(([v, l]) => <option key={v} value={v}>{l}</option>)}
          </select>
        </Field>
        <Field label="Department">
          <select className="sel" value={f.department} onChange={(e) => set("department", e.target.value)}>
            {DEPTS.map((dd) => <option key={dd}>{dd}</option>)}
          </select>
        </Field>
        <Field label="Account ID / code" hint="What they type to sign in & tags their sales">
          <input className="inp" value={f.code} onChange={(e) => set("code", e.target.value.toUpperCase())} placeholder="e.g. OM" maxLength={6} />
        </Field>
        <Field label={f.department === "Call Center" ? "Monthly visits target" : "Monthly sales target"} hint={f.department === "Call Center" ? "Number of completed visits / month (0 = none)" : (f.role === "teamlead" ? "Personal target (0 = none)" : "0 = none")}>
          {f.department === "Call Center"
            ? <Affix affix="visits"><input className="inp num" type="number" value={f.monthlyTarget} onChange={(e) => set("monthlyTarget", e.target.value)} style={{ paddingRight: 52 }} /></Affix>
            : <Affix affix="JOD"><input className="inp num" type="number" value={f.monthlyTarget} onChange={(e) => set("monthlyTarget", e.target.value)} style={{ paddingRight: 44 }} /></Affix>}
        </Field>
        {f.role === "teamlead" && (
          <Field label="Team sales target" hint="Combined monthly target for the whole team"><Affix affix="JOD"><input className="inp num" type="number" value={f.teamTarget} onChange={(e) => set("teamTarget", e.target.value)} style={{ paddingRight: 44 }} /></Affix></Field>
        )}
        {(f.role === "staff" || f.role === "teamlead") && teamLeads.length > 0 && (
          <Field label="Reports to (Team Manager)" span2 hint="Their sales roll up to this team manager">
            <select className="sel" value={f.reportsTo} onChange={(e) => set("reportsTo", e.target.value)}>
              <option value="">— none —</option>
              {teamLeads.map((u) => <option key={u.id} value={u.id}>{u.name}</option>)}
            </select>
          </Field>
        )}
        <Field label="Monthly salary" hint="Auto-posts to Salaries each month"><Affix affix="JOD"><input className="inp num" type="number" value={f.salary} onChange={(e) => set("salary", e.target.value)} style={{ paddingRight: 44 }} /></Affix></Field>
        <Field label="Hire date"><input className="inp" type="date" value={f.hireDate} onChange={(e) => set("hireDate", e.target.value)} /></Field>
        <Field label="Password" span2>
          <div style={{ display: "flex", gap: 8 }}>
            <div className="inp-affix" style={{ flex: 1 }}>
              <input className="inp" type={show ? "text" : "password"} value={f.password} onChange={(e) => set("password", e.target.value)} placeholder="Set a password" />
              <button className="affix" style={{ pointerEvents: "auto", cursor: "pointer", background: "none", border: "none", color: "var(--text-3)", fontFamily: "var(--font)", fontSize: 11 }} onClick={() => setShow((x) => !x)} tabIndex={-1}>{show ? "HIDE" : "SHOW"}</button>
            </div>
            <Btn icon="refresh" onClick={gen} title="Generate a random password">Generate</Btn>
          </div>
        </Field>
        {editing && (
          <label className="span2" style={{ display: "flex", alignItems: "center", gap: 9, fontSize: 13, color: "var(--text-2)", cursor: "pointer" }}>
            <input type="checkbox" checked={f.active} onChange={(e) => set("active", e.target.checked)} /> Account active (can sign in)
          </label>
        )}
      </div>
    </Modal>
  );
}


function DevicesCard({ s, push, fileRef, onImport }) {
  const fs = useFS();
  return (
    <SettingCard title="Saving & devices" desc="Where your data is kept, and how to move it between computers.">
      {/* primary: auto-save to file */}
      <div style={{ padding: "16px 18px", background: fs.connected ? "#34d3a00e" : "var(--bg-2)", border: "1px solid " + (fs.connected ? "#34d3a044" : "var(--border)"), borderRadius: 12, marginBottom: 14 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 8 }}>
          <Icon name="database" size={18} style={{ color: fs.connected ? "var(--pos)" : "var(--accent-2)" }} />
          <span style={{ fontSize: 14, fontWeight: 600 }}>Automatic saving to a file {fs.connected && <Badge color="green" dot>on</Badge>}</span>
        </div>
        {!fs.supported ? (
          <p className="hx-sub" style={{ fontSize: 12.5, lineHeight: 1.55, margin: 0 }}>
            This browser can’t save directly to a file. Your data is stored inside this browser — use <b style={{ color: "var(--text-2)" }}>Export backup</b> below regularly, or open HYDOR in <b style={{ color: "var(--text-2)" }}>Chrome or Edge</b> to enable one-click automatic file saving.
          </p>
        ) : fs.connected ? (
          <>
            <p className="hx-sub" style={{ fontSize: 12.5, lineHeight: 1.55, margin: "0 0 12px" }}>
              Every change is saved automatically to <b className="hx-num" style={{ color: "var(--text)" }}>{fs.fileName}</b>. You can close HYDOR anytime — your data is safe on disk. Open this same file on another computer to continue there.
            </p>
            <div style={{ display: "flex", gap: 8 }}>
              <Btn sm icon="download" onClick={() => { window.HydorFS.writeNow(); push("Saved."); }}>Save now</Btn>
              <Btn sm variant="ghost" onClick={() => { window.HydorFS.disconnect(); push("Auto-save turned off."); }}>Turn off</Btn>
            </div>
          </>
        ) : (
          <>
            <p className="hx-sub" style={{ fontSize: 12.5, lineHeight: 1.55, margin: "0 0 12px" }}>
              Bind HYDOR to a file on this computer. After a one-time setup, everything saves automatically and is never lost when you close the app — exactly like a normal accounting program.
            </p>
            <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
              <Btn sm variant="primary" icon="save" onClick={() => window.HydorFS.connect().then(() => push("Auto-save on.")).catch((e) => e && e.name !== "AbortError" && push(e.message, "err"))}>Create data file</Btn>
              <Btn sm icon="upload" onClick={() => window.HydorFS.openExisting().then(() => push("Data file loaded.")).catch((e) => e && e.name !== "AbortError" && push(e.message, "err"))}>Open existing file</Btn>
            </div>
          </>
        )}
      </div>

      {/* secondary: manual backup + multi-user note */}
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14, marginBottom: 16 }}>
        <div style={{ padding: "14px 16px", background: "var(--bg-2)", border: "1px solid var(--border)", borderRadius: 11 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 9, marginBottom: 7 }}>
            <Icon name="download" size={16} style={{ color: "var(--accent-2)" }} />
            <span style={{ fontSize: 13.5, fontWeight: 600 }}>Manual backup / restore</span>
          </div>
          <p className="hx-sub" style={{ fontSize: 12, lineHeight: 1.55, margin: 0 }}>
            Export a snapshot file anytime as a safety copy, or to move data to another computer, then restore it there.
          </p>
        </div>
        <div style={{ padding: "14px 16px", background: "var(--bg-2)", border: "1px solid var(--border)", borderRadius: 11 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 9, marginBottom: 7 }}>
            <Icon name="layers" size={16} style={{ color: "var(--accent-3)" }} />
            <span style={{ fontSize: 13.5, fontWeight: 600 }}>Live shared sync (multi-user)</span>
          </div>
          <p className="hx-sub" style={{ fontSize: 12, lineHeight: 1.55, margin: 0 }}>
            For several people editing the same books at once across devices, HYDOR needs a hosted cloud database. That’s a follow-on step — ask and I’ll wire it up.
          </p>
        </div>
      </div>
      <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
        <Btn icon="download" onClick={() => { Hydor.exportBackup(); push("Backup downloaded."); }}>Export backup</Btn>
        <Btn icon="upload" onClick={() => fileRef.current.click()}>Restore from file</Btn>
      </div>
    </SettingCard>
  );
}

function SettingCard({ title, desc, children }) {
  return (
    <div className="hx-panel hx-panel-pad" style={{ marginBottom: 14 }}>
      <h3 className="hx-h2" style={{ marginBottom: 3 }}>{title}</h3>
      <p className="hx-sub" style={{ marginBottom: 18 }}>{desc}</p>
      {children}
    </div>
  );
}

/* ---- access levels overview ---- */
const CAP_ROWS = [
  ["Dashboard & analytics", "seeDashboard"],
  ["Cost, profit & margin figures", "seeCostProfit"],
  ["Salaries & incentive amounts", "seeMoney"],
  ["Company costs & receivables/payables", "seeFinance"],
  ["Add purchases & stock adjustments", "postPurchases"],
  ["Suppliers & all clients", "seeAllContacts"],
  ["Print / export PDF", "canPrint"],
  ["Notification centre", "seeNotifications"],
  ["Settings & accounts", "seeSettings"],
  ["Approve leave", "approveLeave"],
  ["Set targets & incentive rules", "setTargets"],
  ["Sell below the markup floor", "sellBelowFloor"],
  ["Manage products & inventory", "manageProducts"],
  ["Customer finder / prospecting", "findCustomers"],
];
function AccessCard() {
  return (
    <SettingCard title="Access levels" desc="What each role can see and do. Money figures are limited to founder and co-founder. Only the founder sees the notification centre and settings.">
      <div style={{ overflowX: "auto" }}>
        <table className="hx-table" style={{ minWidth: 640 }}>
          <thead><tr><th>Capability</th>{Hydor.ROLE_KEYS.map((r) => <th key={r} className="r" style={{ textAlign: "center" }}>{Hydor.ROLE_INFO[r].short}</th>)}</tr></thead>
          <tbody>
            {CAP_ROWS.map(([label, cap]) => (
              <tr key={cap}>
                <td style={{ fontSize: 12.5 }}>{label}</td>
                {Hydor.ROLE_KEYS.map((r) => (
                  <td key={r} style={{ textAlign: "center" }}>
                    {Hydor.can(r, cap)
                      ? <Icon name="check" size={15} style={{ color: "var(--pos)" }} />
                      : <span style={{ color: "var(--text-3)" }}>·</span>}
                  </td>
                ))}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </SettingCard>
  );
}

/* ---- attendance rules ---- */
function AttendanceRules({ s, push }) {
  const a = s.meta.attendance || { workStart: "09:00", graceMin: 10, tier2Min: 30, tier3Min: 60 };
  const [f, setF] = React.useState({ workStart: a.workStart, graceMin: a.graceMin, tier2Min: a.tier2Min, tier3Min: a.tier3Min });
  const set = (k, v) => setF((p) => ({ ...p, [k]: v }));
  const save = () => {
    Hydor.setMeta({ attendance: { workStart: f.workStart || "09:00", graceMin: Hydor.num(f.graceMin, 10), tier2Min: Hydor.num(f.tier2Min, 30), tier3Min: Hydor.num(f.tier3Min, 60) } });
    push("Attendance rules saved.");
  };
  return (
    <SettingCard title="Attendance & lateness rules" desc="Sets the shift start and the lateness thresholds that drive notifications and actions. Late check-ins alert the founder (here) and the manager (in the HR panel) so they can decide the action.">
      <div className="form-grid" style={{ maxWidth: 560 }}>
        <Field label="Shift start time" hint="Scheduled check-in time"><input className="inp" type="time" value={f.workStart} onChange={(e) => set("workStart", e.target.value)} /></Field>
        <Field label="Grace period (min)" hint="No alert within this window"><input className="inp num" type="number" value={f.graceMin} onChange={(e) => set("graceMin", e.target.value)} /></Field>
      </div>
      <div style={{ margin: "16px 0", display: "flex", flexDirection: "column", gap: 10 }}>
        <RuleRow color="var(--warn)" head={"Late > " + f.graceMin + " min"} body="Notify founder & manager — choose verbal warning, written reprimand or salary deduction." />
        <RuleRow color="var(--neg)" head={"Late > " + f.tier2Min + " min"} body="Salary deduction applies — amount set by the founder or manager." />
        <RuleRow color="var(--neg)" head={"Late > " + f.tier3Min + " min, no prior notice"} body="Referred to the investigation committee." />
      </div>
      <div className="form-grid" style={{ maxWidth: 560 }}>
        <Field label="Deduction threshold (min)"><input className="inp num" type="number" value={f.tier2Min} onChange={(e) => set("tier2Min", e.target.value)} /></Field>
        <Field label="Investigation threshold (min)"><input className="inp num" type="number" value={f.tier3Min} onChange={(e) => set("tier3Min", e.target.value)} /></Field>
      </div>
      <div style={{ marginTop: 16 }}><Btn variant="primary" icon="check" onClick={save}>Save rules</Btn></div>
    </SettingCard>
  );
}
function RuleRow({ color, head, body }) {
  return (
    <div style={{ display: "flex", gap: 12, alignItems: "flex-start", padding: "11px 14px", background: "var(--bg-2)", border: "1px solid var(--border)", borderRadius: 11 }}>
      <span style={{ width: 9, height: 9, borderRadius: "50%", background: color, flex: "none", marginTop: 4 }}></span>
      <div><div style={{ fontSize: 13, fontWeight: 600 }}>{head}</div><div style={{ fontSize: 12, color: "var(--text-2)", marginTop: 2, lineHeight: 1.5 }}>{body}</div></div>
    </div>
  );
}

/* ---- cloud sync (Firebase) ---- */
function useCloud() {
  const safeGet = () => { try { return window.HydorCloud && window.HydorCloud.getStatus ? window.HydorCloud.getStatus() : { available: false }; } catch(e) { return { available: false }; } };
  const [st, setSt] = React.useState(safeGet);
  React.useEffect(() => { try { return window.HydorCloud && window.HydorCloud.onStatus ? window.HydorCloud.onStatus((s) => setSt({ ...s })) : undefined; } catch(e) { return undefined; } }, []);
  return st;
}
function CloudSettings({ push }) {
  const cloud = useCloud();
  const [open, setOpen] = React.useState(false);     // setup form
  const [guide, setGuide] = React.useState(false);
  const [cfgText, setCfgText] = React.useState("");
  const [ws, setWs] = React.useState("main");
  const [busy, setBusy] = React.useState(false);
  const has = window.HydorCloud;

  const dot = cloud.connected ? "var(--pos)" : cloud.connecting ? "var(--warn)" : cloud.error ? "var(--neg)" : "var(--text-3)";
  const stateLabel = cloud.connected ? "Live — syncing in real time" : cloud.connecting ? "Connecting…" : cloud.configured ? "Configured (not connected)" : "Not set up";

  const doConnect = async () => {
    if (!cfgText.trim()) return push("Paste your Firebase configuration first.", "err");
    setBusy(true);
    try { await window.HydorCloud.configure(cfgText, ws); push("Connected — your data now syncs live."); setOpen(false); setCfgText(""); }
    catch (e) { push(e.message, "err"); }
    finally { setBusy(false); }
  };
  const reconnect = async () => { setBusy(true); try { await window.HydorCloud.connect(); push("Reconnected."); } catch (e) { push(e.message, "err"); } finally { setBusy(false); } };
  const copyCode = () => { const c = window.HydorCloud.connectionCode(); if (!c) return; navigator.clipboard && navigator.clipboard.writeText(c); push("Connection code copied — share it with your team to link their device."); };

  return (
    <SettingCard title="Cloud sync — real-time, multi-device" desc="Connect a free Firebase project so every employee works on the same live data from any device. Changes appear instantly for everyone. Until you connect, HYDOR runs locally on this device only.">
      <div style={{ display: "flex", alignItems: "center", gap: 11, padding: "12px 14px", border: "1px solid var(--border)", borderRadius: 11, background: "var(--bg-2)", marginBottom: 14 }}>
        <span style={{ width: 9, height: 9, borderRadius: "50%", background: dot, boxShadow: cloud.connected ? "0 0 8px var(--pos)" : "none", flex: "none" }} />
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 13.5, fontWeight: 600 }}>{stateLabel}</div>
          <div style={{ fontSize: 12, color: "var(--text-3)" }}>
            {cloud.workspaceId ? "Workspace: " + cloud.workspaceId + " · " : ""}
            {cloud.lastSync ? "last sync " + new Date(cloud.lastSync).toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit", second: "2-digit" }) : (has && has.sdkReady ? "Firebase ready" : "Firebase SDK loading / offline")}
          </div>
        </div>
        {cloud.syncing && <Icon name="refresh" size={15} style={{ color: "var(--accent-2)" }} />}
      </div>

      {cloud.error && <div style={{ fontSize: 12.5, color: "var(--neg)", background: "#ff6b7d12", border: "1px solid #ff6b7d33", borderRadius: 9, padding: "9px 12px", marginBottom: 14, lineHeight: 1.5 }}>{cloud.error}</div>}

      {!cloud.connected ? (
        <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
          <Btn variant="primary" icon="database" onClick={() => setOpen((x) => !x)}>{cloud.configured ? "Re-enter configuration" : "Set up cloud sync"}</Btn>
          {cloud.configured && <Btn icon="refresh" onClick={reconnect} disabled={busy}>Reconnect</Btn>}
          <Btn variant="ghost" onClick={() => setGuide((x) => !x)}>{guide ? "Hide" : "How do I get this?"}</Btn>
        </div>
      ) : (
        <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
          <Btn icon="refresh" onClick={() => { window.HydorCloud.pushNow(); push("Synced."); }}>Sync now</Btn>
          <Btn icon="share" onClick={copyCode}>Copy team connection code</Btn>
          <div className="grow" />
          <Btn variant="danger" icon="x" onClick={() => { window.HydorCloud.disconnect(); push("Disconnected — back to local-only on this device."); }}>Disconnect</Btn>
        </div>
      )}

      {open && !cloud.connected && (
        <div style={{ marginTop: 16, paddingTop: 16, borderTop: "1px solid var(--border)" }}>
          <Field label="Firebase configuration" hint="Paste the whole firebaseConfig snippet from Firebase → Project settings → Your apps. Or paste a teammate's HYDOR connection code.">
            <textarea className="inp" rows={7} style={{ fontFamily: "var(--mono, monospace)", fontSize: 12, lineHeight: 1.5, resize: "vertical" }} value={cfgText} onChange={(e) => setCfgText(e.target.value)} placeholder={'const firebaseConfig = {\n  apiKey: "AIza…",\n  authDomain: "your-app.firebaseapp.com",\n  projectId: "your-app",\n  appId: "1:…"\n};'} />
          </Field>
          <div style={{ display: "flex", gap: 12, alignItems: "flex-end", marginTop: 12 }}>
            <Field label="Workspace ID" hint="Everyone on your team uses the same ID to share data">
              <input className="inp" value={ws} onChange={(e) => setWs(e.target.value)} placeholder="main" style={{ width: 180 }} />
            </Field>
            <div className="grow" />
            <Btn variant="primary" icon="check" onClick={doConnect} disabled={busy}>{busy ? "Connecting…" : "Connect"}</Btn>
          </div>
        </div>
      )}

      {guide && (
        <div style={{ marginTop: 16, paddingTop: 16, borderTop: "1px solid var(--border)", fontSize: 12.5, color: "var(--text-2)", lineHeight: 1.6 }}>
          <b style={{ color: "var(--text)" }}>One-time setup (about 5 minutes, free):</b>
          <ol style={{ margin: "10px 0 0", paddingLeft: 20, display: "flex", flexDirection: "column", gap: 7 }}>
            <li>Go to <span className="hx-num" style={{ color: "var(--accent-2)" }}>console.firebase.google.com</span> and create a project (the free “Spark” plan is enough).</li>
            <li>In <b>Build → Firestore Database</b>, click <b>Create database</b> (start in test mode, or set the rule below).</li>
            <li>In <b>Build → Authentication → Sign-in method</b>, enable <b>Anonymous</b>.</li>
            <li>In <b>Project settings → Your apps</b>, add a <b>Web app</b> (&lt;/&gt; icon) and copy the <span className="hx-num">firebaseConfig</span> block.</li>
            <li>Paste it above, pick a workspace ID, and press Connect.</li>
            <li>For each employee: open HYDOR on their device, and either paste the same config or use <b>Copy team connection code</b> from your device and paste that here.</li>
          </ol>
          <div style={{ marginTop: 12, padding: "10px 12px", background: "var(--bg-2)", border: "1px solid var(--border)", borderRadius: 9 }}>
            <div style={{ fontSize: 11.5, color: "var(--text-3)", marginBottom: 6 }}>Recommended Firestore security rule (authenticated users only):</div>
            <pre style={{ margin: 0, fontFamily: "var(--mono, monospace)", fontSize: 11.5, color: "var(--text-2)", whiteSpace: "pre-wrap" }}>{`rules_version = '2';
service cloud.firestore {
  match /databases/{db}/documents {
    match /workspaces/{ws} {
      allow read, write: if request.auth != null;
    }
  }
}`}</pre>
          </div>
          <p style={{ marginTop: 12, fontSize: 11.5, color: "var(--text-3)" }}>
            <b>Note:</b> this prototype stores the whole workspace in one cloud document gated by anonymous sign-in — perfect for a small team going live quickly. For stronger security (per-person Firebase logins, encrypted salary fields, audit-proof rules) and unlimited history, ask me to build the hardened production version.
          </p>
        </div>
      )}
    </SettingCard>
  );
}
function RecurringSettings({ s, push }) {
  const [form, setForm] = React.useState(null);
  const byId = {}; s.users.forEach((u) => (byId[u.id] = u));
  const total = s.recurring.filter((r) => r.active).reduce((a, r) => a + r.amount, 0);
  return (
    <SettingCard title="Recurring cost registry" desc="Fixed monthly costs — rent, salaries, utilities — post automatically into Company Costs each month so nothing is forgotten. Salaries sync from each employee's HR record.">
      <div className="hx-panel" style={{ overflow: "hidden", border: "1px solid var(--border)", marginBottom: 14 }}>
        {s.recurring.length === 0 ? (
          <Empty icon="cost" title="No recurring costs yet">Add rent, utilities or other monthly costs and they'll be posted for you automatically.</Empty>
        ) : (
          <table className="hx-table">
            <thead><tr><th>Cost</th><th>Category</th><th>Day</th><th className="r">Amount / mo</th><th>Status</th><th></th></tr></thead>
            <tbody>
              {s.recurring.map((r) => (
                <tr key={r.id}>
                  <td style={{ fontWeight: 500 }}>{r.label}{r.userId && <Badge color="gray">salary</Badge>}</td>
                  <td style={{ color: "var(--text-2)", fontSize: 12.5 }}>{r.category}</td>
                  <td className="num" style={{ color: "var(--text-2)" }}>{r.dayOfMonth}</td>
                  <td className="r num">{Hydor.fmtMoney0(r.amount)}</td>
                  <td>{r.active ? <Badge color="green" dot>active</Badge> : <Badge color="gray" dot>paused</Badge>}</td>
                  <td className="r no-print"><div style={{ display: "flex", gap: 2, justifyContent: "flex-end" }}>
                    {!r.userId && <Btn variant="ghost" sm icon="edit" onClick={() => setForm({ editing: r })} />}
                    {!r.userId && <Btn variant="ghost" sm icon="trash" onClick={() => { Hydor.deleteRecurring(r.id); push("Removed."); }} />}
                    {r.userId && <span className="hx-sub" style={{ fontSize: 11 }}>edit in HR</span>}
                  </div></td>
                </tr>
              ))}
            </tbody>
          </table>
        )}
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <Btn variant="primary" icon="plus" onClick={() => setForm({})}>Add recurring cost</Btn>
        <Btn icon="refresh" onClick={() => { const n = Hydor.runRecurring(); push(n ? `Posted ${n} due cost${n !== 1 ? "s" : ""}.` : "Everything is up to date."); }}>Post due now</Btn>
        <div className="grow" style={{ flex: 1 }} />
        <span className="hx-sub">Total <b className="hx-num" style={{ color: "var(--text)" }}>{Hydor.fmtMoney0(total)} JOD/mo</b></span>
      </div>
      {form && <RecurringForm editing={form.editing} onClose={() => setForm(null)} push={push} />}
    </SettingCard>
  );
}

function RecurringForm({ editing, onClose, push }) {
  const s = Hydor.getState();
  const cats = s.meta.costCategories || ["Other"];
  const [f, setF] = React.useState(editing
    ? { label: editing.label, amount: editing.amount, category: editing.category, costType: editing.costType, dayOfMonth: editing.dayOfMonth, startDate: editing.startDate }
    : { label: "", amount: "", category: "Rent", costType: "fixed", dayOfMonth: 1, startDate: new Date().toISOString().slice(0, 10) });
  const set = (k, v) => setF((p) => ({ ...p, [k]: v }));
  const save = () => {
    if (!f.label.trim()) return push("Give the cost a name.", "err");
    if (Hydor.num(f.amount, 0) <= 0) return push("Enter an amount.", "err");
    if (editing) { Hydor.updateRecurring(editing.id, f); push("Recurring cost updated."); }
    else { Hydor.addRecurring(f); push("Recurring cost added & posted for due months."); }
    onClose();
  };
  return (
    <Modal title={editing ? "Edit recurring cost" : "Add recurring cost"} onClose={onClose} foot={<><Btn variant="ghost" onClick={onClose}>Cancel</Btn><Btn variant="primary" icon="check" onClick={save}>{editing ? "Save" : "Add"}</Btn></>}>
      <div className="form-grid">
        <Field label="Description" span2><input className="inp" value={f.label} onChange={(e) => set("label", e.target.value)} placeholder="e.g. Showroom rent" autoFocus /></Field>
        <Field label="Amount / month"><Affix affix="JOD"><input className="inp num" type="number" value={f.amount} onChange={(e) => set("amount", e.target.value)} style={{ paddingRight: 44 }} /></Affix></Field>
        <Field label="Posts on day" hint="Day of month (1–28)"><input className="inp num" type="number" min="1" max="28" value={f.dayOfMonth} onChange={(e) => set("dayOfMonth", e.target.value)} /></Field>
        <Field label="Category"><select className="sel" value={f.category} onChange={(e) => set("category", e.target.value)}>{cats.map((c) => <option key={c}>{c}</option>)}</select></Field>
        <Field label="Cost type"><select className="sel" value={f.costType} onChange={(e) => set("costType", e.target.value)}><option value="fixed">Fixed</option><option value="operational">Operational</option></select></Field>
        <Field label="Start from" span2 hint="Months from this date forward will be posted"><input className="inp" type="date" value={f.startDate} onChange={(e) => set("startDate", e.target.value)} /></Field>
      </div>
    </Modal>
  );
}

Object.assign(window, { ReportsPage, ActivityPage, SettingsPage });
