// LeaveTrackerLite — main app: table + filtering/sorting + tweaks

const MONTHS = { Jan:0,Feb:1,Mar:2,Apr:3,May:4,Jun:5,Jul:6,Aug:7,Sep:8,Oct:9,Nov:10,Dec:11 };
function md(s) { // "Jun 6" -> sortable number
  if (!s) return Infinity;
  const [m, d] = s.split(" ");
  return (MONTHS[m] ?? 0) * 31 + parseInt(d, 10);
}

// Accent locked to teal (defined in the :root palette); density locked to comfortable.

const TABS = [
  { key: "all",       label: "All cases" },
  { key: "upcoming",  label: "Upcoming" },
  { key: "attention", label: "Needs attention" },
];
const FILTER_LABEL = {
  active:  "Active leaves",
  returns: "Upcoming returns (next 7 days)",
  docs:    "Missing documentation",
  low:     "Low remaining balance",
};
function matchFilter(c, f) {
  if (f === "all") return true;
  if (f === "active") return c.status === "Active";
  if (f === "upcoming") return c.status === "Upcoming";
  if (f === "attention") return c.needsAttention;
  if (f === "returns") return !c.ongoing && c.retDays != null && c.retDays >= 0 && c.retDays <= 7;
  if (f === "docs") return c.docStatus === "missing";
  if (f === "low") return c.lowBalance;
  return true;
}

/* Default ordering: what needs dealing with first. */
function urgencyScore(c) {
  if (c.retPast) return 0;                                                            // past-due return
  if (c.status === "Exhausted") return 1;                                             // over allotment
  if (c.lowBalance) return 2;                                                         // low balance
  if (!c.ongoing && c.retDays != null && c.retDays >= 0 && c.retDays <= 7) return 3;  // returning soon
  if (c.needsAttention) return 4;                                                     // docs / stale / follow-up
  return 5;
}
function urgencySort(a, b) {
  const d = urgencyScore(a) - urgencyScore(b);
  if (d) return d;
  const ra = a.retDays == null ? Infinity : a.retDays, rb = b.retDays == null ? Infinity : b.retDays;
  if (ra !== rb) return ra - rb;
  return a.name.localeCompare(b.name);
}

const COLS = [
  { key: "name",   label: "Employee",        sort: c => c.name.toLowerCase(),  align: "left" },
  { key: "dept",   label: "Dept / Location", sort: c => c.dept.toLowerCase(),  align: "left" },
  { key: "type",   label: "Leave type",      sort: c => c.type.toLowerCase(),  align: "left" },
  { key: "status", label: "Status",          sort: null,                       align: "left" },
  { key: "start",  label: "Start",           sort: c => md(c.start),           align: "left" },
  { key: "ret",    label: "Exp. return",     sort: c => (c.ongoing ? Infinity : md(c.ret)), align: "left" },
  { key: "bal",    label: "Used / available", sort: c => (c.remaining == null ? Infinity : c.remaining), align: "left" },
  { key: "doc",    label: "Documentation",   sort: c => ({ missing: 0, received: 1, complete: 2 }[c.docStatus]), align: "left" },
  { key: "upd",    label: "Updated",         sort: c => md(c.updated),         align: "right" },
];

function SortHeader({ col, sort, setSort }) {
  const active = sort.key === col.key;
  const sortable = !!col.sort;
  return (
    <th style={{
      textAlign: col.align, padding: "0 9px 11px", fontSize: 11.5, fontWeight: 700, letterSpacing: ".04em",
      textTransform: "uppercase", color: active ? "var(--accent-strong)" : "var(--ink-faint)",
      whiteSpace: "nowrap", userSelect: "none", cursor: sortable ? "pointer" : "default",
    }}
      onClick={sortable ? () => setSort(s => ({ key: col.key, dir: s.key === col.key && s.dir === "asc" ? "desc" : "asc" })) : undefined}>
      <span className="i" style={{ gap: 4, justifyContent: col.align === "right" ? "flex-end" : "flex-start" }}>
        {col.label}
        {sortable && <i className={"ph-bold " + (active ? (sort.dir === "asc" ? "ph-caret-up" : "ph-caret-down") : "ph-caret-up-down")}
          style={{ fontSize: 11, opacity: active ? 1 : 0.45 }} />}
      </span>
    </th>
  );
}

function attentionEdge(c, prominence) {
  if (prominence === "subtle" || !c.needsAttention) return { bar: "transparent", bg: "transparent" };
  let col = "var(--amber)";
  if (c.retPast || c.status === "Exhausted") col = "var(--rose)";
  return { bar: col, bg: prominence === "strong" ? (c.retPast || c.status === "Exhausted" ? "var(--rose-soft)" : "var(--amber-soft)") : "transparent" };
}

function Row({ c, vizMode, prominence, onOpen, kbdSelected }) {
  const [hover, setHover] = useState(false);
  const edge = attentionEdge(c, prominence);
  const baseBg = edge.bg !== "transparent" ? edge.bg : "var(--surface)";
  return (
    <tr onClick={() => onOpen(c)} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      data-kbd-selected={kbdSelected ? "true" : undefined}
      style={{ cursor: "pointer", background: hover ? "var(--surface-2)" : baseBg, transition: "background .1s" }}>
      <td style={{ padding: "var(--row-pad-y) 10px var(--row-pad-y) 0", borderBottom: "1px solid var(--line-soft)", position: "relative" }}>
        {c.needsAttention && edge.bar !== "transparent" ? (
          <span style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: 14, display: "flex", alignItems: "stretch" }} onClick={e => e.stopPropagation()}>
            <Tooltip label={c.attention.join("  ·  ")} side="top">
              <span style={{ display: "block", width: 3, margin: "6px 0", borderRadius: 999, background: edge.bar }} />
            </Tooltip>
          </span>
        ) : (
          <span style={{ position: "absolute", left: 0, top: 6, bottom: 6, width: 3, borderRadius: 999, background: edge.bar }} />
        )}
        <span className="i" style={{ gap: 9, justifyContent: "flex-start", paddingLeft: 8 }}>
          <Avatar initials={c.initials} size={30} />
          <span style={{ fontSize: "var(--cell-font)", fontWeight: 600, color: "var(--ink)", whiteSpace: "nowrap" }}>{c.name}</span>
        </span>
      </td>
      <td style={cellStyle}>
        <span style={{ fontSize: "var(--cell-font)", color: "var(--ink-2)" }}>{c.dept}</span>
        <span style={{ display: "block", fontSize: 12.5, color: "var(--ink-faint)" }}>{c.location}</span>
      </td>
      <td style={cellStyle}><TypeTag type={c.type} fmla={c.fmla} weeks={c.weeks} /></td>
      <td style={cellStyle}>
        <span style={{ display: "inline-flex", flexDirection: "column", gap: 5, alignItems: "flex-start" }}>
          <StatusChip status={c.status} />
          {(c.flags || []).map((f, i) => (
            <Tooltip key={i} label={c.attention.join("  ·  ")}>
              <FlagChip>{f}</FlagChip>
            </Tooltip>
          ))}
        </span>
      </td>
      <td style={{ ...cellStyle, whiteSpace: "nowrap" }}>
        <span style={{ display: "inline-flex", flexDirection: "column", gap: 4, alignItems: "flex-start" }}>
          <span className="tnum" style={{ fontSize: "var(--cell-font)", color: "var(--ink-2)" }}>{c.start}</span>
          {c.status === "Upcoming" && c.startDays != null && <Chip color="var(--blue)" bg="var(--blue-soft)" weight={600} style={{ fontSize: 11, padding: "1px 6px" }}>in {c.startDays}d</Chip>}
        </span>
      </td>
      <td style={{ ...cellStyle, whiteSpace: "nowrap" }}><ReturnCell c={c} /></td>
      <td style={cellStyle}><Balance c={c} mode={vizMode} /></td>
      <td style={cellStyle}><DocChip status={c.docStatus} label={c.docLabel} /></td>
      <td style={{ ...cellStyle, textAlign: "right" }}>
        <span className="i" style={{ gap: 6, justifyContent: "flex-end" }}>
          {c.stale ? (
            <Tooltip label={`No update in ${c.stale} days`}>
              <span className="i tnum" style={{ gap: 4, fontSize: 13, color: "var(--amber-deep)", fontWeight: 600 }}>
                <i className="ph ph-clock-counter-clockwise" style={{ fontSize: 13 }} />{c.updated}
              </span>
            </Tooltip>
          ) : (
            <span className="tnum" style={{ fontSize: 13, color: "var(--ink-3)" }}>{c.updated}</span>
          )}
          <i className="ph ph-caret-right" style={{ fontSize: 14, color: hover ? "var(--accent)" : "transparent", transition: "color .1s" }} />
        </span>
      </td>
    </tr>
  );
}
const cellStyle = { padding: "var(--row-pad-y) 9px", borderBottom: "1px solid var(--line-soft)", verticalAlign: "middle" };

function Table({ rows, vizMode, prominence, sort, setSort, onOpen, kbdIdx, query, onClearSearch }) {
  return (
    <div style={{ overflowX: "auto" }}>
      <table style={{ width: "100%", borderCollapse: "collapse", minWidth: 1060 }}>
        <thead>
          <tr>{COLS.map(col => <SortHeader key={col.key} col={col} sort={sort} setSort={setSort} />)}</tr>
        </thead>
        <tbody>
          {rows.map((c, i) => <Row key={c.name} c={c} vizMode={vizMode} prominence={prominence} onOpen={onOpen} kbdSelected={i === kbdIdx} />)}
        </tbody>
      </table>
      {rows.length === 0 && (
        <div className="i" style={{ flexDirection: "column", gap: 10, padding: "56px 0", color: "var(--ink-faint)" }}>
          <i className={"ph " + (query ? "ph-magnifying-glass" : "ph-tray")} style={{ fontSize: 30 }} />
          <span style={{ fontSize: 14.5 }}>{query ? <span>No cases match “{query}”.</span> : "No cases match this view."}</span>
          {query && (
            <button onClick={onClearSearch} className="i" style={{ gap: 6, height: 32, padding: "0 13px", borderRadius: 8, border: "1px solid var(--line)", background: "var(--surface)", color: "var(--ink-2)", fontWeight: 600, fontSize: 13 }}>
              <i className="ph ph-x" style={{ fontSize: 12 }} /> Clear search
            </button>
          )}
        </div>
      )}
    </div>
  );
}

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "attention": "medium"
}/*EDITMODE-END*/;

/* In-product balance display switcher — a real user preference, persisted */
const VIZ_OPTS = [
  { v: "bar",     icon: "ph-chart-bar-horizontal", label: "Bar" },
  { v: "ring",    icon: "ph-chart-donut",          label: "Ring" },
  { v: "numbers", icon: "ph-list-numbers",         label: "Numbers" },
];
function VizSwitcher({ viz, setViz }) {
  return (
    <div className="i" style={{ background: "var(--surface-2)", border: "1px solid var(--line)", borderRadius: 9, padding: 3, gap: 2 }} role="group" aria-label="Balance display">
      {VIZ_OPTS.map(o => {
        const on = viz === o.v;
        return (
          <Tooltip key={o.v} label={o.label + " display"} side="top">
            <button onClick={() => setViz(o.v)} aria-pressed={on} className="i" style={{
              width: 32, height: 28, borderRadius: 7, border: "none",
              background: on ? "var(--surface)" : "transparent", boxShadow: on ? "var(--shadow-card)" : "none",
              color: on ? "var(--accent-strong)" : "var(--ink-faint)",
            }}>
              <i className={(on ? "ph-fill " : "ph ") + o.icon} style={{ fontSize: 16 }} />
            </button>
          </Tooltip>
        );
      })}
    </div>
  );
}

function TableCard({ title, rows, total, sort, setSort, filter, setFilter, viz, setViz, prominence, onOpen, showTabs, kbdIdx, query, onClearSearch }) {
  const tab = filter === "attention" ? "attention" : filter === "upcoming" ? "upcoming" : "all";
  const showClear = filter !== "all" && filter !== "attention" && filter !== "upcoming";
  const col = COLS.find(x => x.key === sort.key);
  const [showClosed, setShowClosed] = useState(false);
  const tabCounts = {
    upcoming: window.CASES.filter(c => c.status === "Upcoming").length,
    attention: window.CASES.filter(c => c.needsAttention).length,
  };
  return (
    <React.Fragment>
      <section style={{ background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 16, boxShadow: "var(--shadow-card)", overflow: "hidden" }}>
        <div style={{ padding: "16px 18px 14px", borderBottom: "1px solid var(--line)", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 16, flexWrap: "wrap" }}>
          <div className="i" style={{ gap: 11, justifyContent: "flex-start" }}>
            <h2 style={{ margin: 0, fontSize: 16.5, fontWeight: 700, letterSpacing: "-.01em", color: "var(--ink)", whiteSpace: "nowrap" }}>{title}</h2>
            <span className="i tnum" style={{ minWidth: 22, height: 22, padding: "0 8px", borderRadius: 999, background: "var(--surface-2)", border: "1px solid var(--line)", color: "var(--ink-3)", fontSize: 12.5, fontWeight: 700 }}>{rows.length}</span>
          </div>
          <div className="i no-print" style={{ gap: 10 }}>
            {showTabs && (
              <div className="i" style={{ background: "var(--surface-2)", border: "1px solid var(--line)", borderRadius: 9, padding: 3, gap: 2 }}>
                {TABS.map(tb => {
                  const on = tab === tb.key;
                  const n = tabCounts[tb.key];
                  return (
                    <button key={tb.key} onClick={() => setFilter(tb.key)} className="i" style={{
                      gap: 6, padding: "5px 12px", borderRadius: 7, border: "none",
                      background: on ? "var(--surface)" : "transparent", boxShadow: on ? "var(--shadow-card)" : "none",
                      color: on ? "var(--ink)" : "var(--ink-3)", fontWeight: 600, fontSize: 13.5, whiteSpace: "nowrap",
                    }}>
                      {tb.key === "attention" && <i className={(on ? "ph-fill " : "ph ") + "ph-warning-circle"} style={{ fontSize: 15, color: on ? "var(--amber)" : "var(--ink-faint)" }} />}
                      {tb.label}
                      {n != null && <span className="i tnum" style={{ minWidth: 18, height: 18, padding: "0 5px", borderRadius: 999, background: on ? (tb.key === "attention" ? "var(--amber-soft)" : "var(--blue-soft)") : "var(--surface)", border: on ? "none" : "1px solid var(--line)", color: on ? (tb.key === "attention" ? "var(--amber-deep)" : "var(--blue)") : "var(--ink-3)", fontSize: 11, fontWeight: 700 }}>{n}</span>}
                    </button>
                  );
                })}
              </div>
            )}
            <VizSwitcher viz={viz} setViz={setViz} />
            <button className="i" style={{ gap: 7, height: 34, padding: "0 13px", borderRadius: 9, border: "1px solid var(--line)", background: "var(--surface)", color: "var(--ink-2)", fontWeight: 600, fontSize: 13.5 }}>
              <i className="ph ph-funnel" style={{ fontSize: 15, color: "var(--ink-3)" }} /> Filter
            </button>
            <button className="i" style={{ gap: 7, height: 34, padding: "0 13px", borderRadius: 9, border: "1px solid var(--line)", background: "var(--surface)", color: "var(--ink-2)", fontWeight: 600, fontSize: 13.5 }}>
              <i className="ph ph-export" style={{ fontSize: 15, color: "var(--ink-3)" }} /> Export
            </button>
          </div>
        </div>

        {showClear && (
          <div className="i" style={{ gap: 9, justifyContent: "flex-start", padding: "11px 18px", background: "var(--accent-soft)", borderBottom: "1px solid var(--line)" }}>
            <i className="ph-fill ph-funnel-simple" style={{ fontSize: 14, color: "var(--accent)" }} />
            <span style={{ fontSize: 13.5, color: "var(--accent-deep)", fontWeight: 600 }}>Filtered: {FILTER_LABEL[filter]}</span>
            <button onClick={() => setFilter("all")} className="i" style={{ gap: 4, border: "none", background: "transparent", color: "var(--accent-strong)", fontSize: 13, fontWeight: 600, marginLeft: 2 }}>
              <i className="ph ph-x" style={{ fontSize: 12 }} /> Clear
            </button>
          </div>
        )}

        <div style={{ padding: "6px 2px 2px" }}>
          <Table rows={rows} vizMode={viz} prominence={prominence} sort={sort} setSort={setSort} onOpen={onOpen} kbdIdx={kbdIdx} query={query} onClearSearch={onClearSearch} />
        </div>
      </section>

      <div className="i" style={{ justifyContent: "space-between", marginTop: 16, padding: "0 4px" }}>
        <span className="i" style={{ gap: 8, fontSize: 12.5, color: "var(--ink-faint)" }}>
          <span>Showing {rows.length} of {total} cases · sorted by {col ? col.label.toLowerCase() : "urgency"}</span>
          {sort.key !== "urgency" && (
            <button onClick={() => setSort({ key: "urgency", dir: "asc" })} className="i no-print" style={{ gap: 4, border: "none", background: "transparent", color: "var(--accent-strong)", fontSize: 12.5, fontWeight: 600, padding: 0 }}>
              <i className="ph ph-arrow-counter-clockwise" style={{ fontSize: 12 }} /> Reset to urgency
            </button>
          )}
          <button onClick={() => setShowClosed(s => !s)} className="i no-print" style={{ gap: 4, border: "none", background: "transparent", color: "var(--ink-3)", fontSize: 12.5, fontWeight: 500, padding: 0 }}>
            · {window.CLOSED.length} closed in last 30 days <i className={"ph ph-caret-" + (showClosed ? "up" : "down")} style={{ fontSize: 11 }} />
          </button>
        </span>
        <span className="i" style={{ gap: 7, color: "var(--ink-faint)", fontSize: 12.5 }}>
          <i className="ph ph-shield-check" style={{ fontSize: 14 }} /> Workflow tracking tool — not legal advice.
        </span>
      </div>

      {showClosed && (
        <div className="no-print" style={{ marginTop: 10, background: "var(--surface-2)", border: "1px solid var(--line)", borderRadius: 12, padding: "6px 16px" }}>
          {window.CLOSED.map((c, i) => (
            <div key={c.name} className="i" style={{ justifyContent: "flex-start", gap: 12, padding: "9px 0", borderBottom: i < window.CLOSED.length - 1 ? "1px solid var(--line)" : "none" }}>
              <Avatar initials={c.initials} size={26} />
              <span style={{ fontSize: 13.5, fontWeight: 600, color: "var(--ink-2)", width: 130 }}>{c.name}</span>
              <span style={{ fontSize: 13, color: "var(--ink-3)", width: 190 }}>{c.dept} · {c.location}</span>
              <span style={{ fontSize: 13, color: "var(--ink-3)", width: 120 }}>{c.type}</span>
              <span style={{ fontSize: 13, color: "var(--ink-3)", flex: 1 }}>{c.outcome}</span>
              <span className="i tnum" style={{ gap: 5, fontSize: 12.5, color: "var(--ink-faint)" }}>
                <i className="ph ph-check-circle" style={{ fontSize: 14 }} /> Closed {c.closed}
              </span>
            </div>
          ))}
        </div>
      )}
    </React.Fragment>
  );
}

const SOON = {
  employees: { icon: "ph-users-three", title: "Employees directory", body: "A roster of everyone at Northgate — current balances, leave history, and contact details in one place." },
  io:        { icon: "ph-arrows-down-up", title: "Import & export", body: "Bring leave records in from your existing spreadsheet, or export the current view to CSV or Excel for payroll." },
  settings:  { icon: "ph-gear-six", title: "Settings", body: "Configure leave types, follow-up reminders, locations, and who on your team can see each case." },
};
function ComingSoon({ view }) {
  const s = SOON[view] || SOON.settings;
  return (
    <div style={{ display: "flex", alignItems: "center", justifyContent: "center", minHeight: 420 }}>
      <div style={{ maxWidth: 420, textAlign: "center", background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 16, boxShadow: "var(--shadow-card)", padding: "44px 40px" }}>
        <span className="i" style={{ width: 56, height: 56, borderRadius: 14, background: "var(--accent-soft)", margin: "0 auto 20px" }}>
          <i className={"ph " + s.icon} style={{ fontSize: 27, color: "var(--accent)" }} />
        </span>
        <h2 style={{ margin: "0 0 8px", fontSize: 19, fontWeight: 700, letterSpacing: "-.01em", color: "var(--ink)" }}>{s.title}</h2>
        <p style={{ margin: "0 0 20px", fontSize: 14.5, lineHeight: 1.5, color: "var(--ink-3)" }}>{s.body}</p>
        <span className="i" style={{ gap: 6, padding: "5px 12px", borderRadius: 999, background: "var(--surface-2)", border: "1px solid var(--line)", color: "var(--ink-3)", fontSize: 12.5, fontWeight: 600 }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "var(--amber)" }} /> In development
        </span>
      </div>
    </div>
  );
}

const HEADERS = {
  dashboard: { title: "Dashboard",       subtitle: "Who's out, who's back soon, and what needs a follow-up this week.", showDate: true },
  cases:     { title: "Leave Cases",     subtitle: "Every active leave on record — search, sort, and open any case.",   showDate: false },
  attention: { title: "Needs Attention", subtitle: "Cases with a missing document, a passed return date, or a low remaining balance.", showDate: false },
  employees: { title: "Employees",       subtitle: "Your team directory and leave history.", showDate: false },
  io:        { title: "Import / Export", subtitle: "Move leave data in and out of LeaveTrackerLite.", showDate: false },
  settings:  { title: "Settings",        subtitle: "Configure how LeaveTrackerLite works for your team.", showDate: false },
};

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [view, setView] = useState("dashboard");
  const [query, setQuery] = useState("");
  const [filter, setFilter] = useState("all");
  const [sort, setSort] = useState({ key: "urgency", dir: "asc" });
  const [open, setOpen] = useState(null);
  const [kbd, setKbd] = useState(-1);
  const [viz, setVizState] = useState(() => {
    try { return localStorage.getItem("ltl-balance-display") || "bar"; } catch (e) { return "bar"; }
  });
  function setViz(v) {
    setVizState(v);
    try { localStorage.setItem("ltl-balance-display", v); } catch (e) {}
  }

  function nav(v) {
    setView(v);
    setOpen(null);
    setQuery("");
    if (v === "attention") setFilter("attention");
    else if (v === "dashboard" || v === "cases") setFilter("all");
  }

  const isTable = view === "dashboard" || view === "cases" || view === "attention";
  const effFilter = view === "attention" ? "attention" : filter;

  let rows = window.CASES.filter(c => matchFilter(c, effFilter));
  if (query.trim()) {
    const q = query.toLowerCase();
    rows = rows.filter(c => (c.name + " " + c.dept + " " + c.location + " " + c.type).toLowerCase().includes(q));
  }
  const col = COLS.find(x => x.key === sort.key);
  if (col && col.sort) {
    rows = [...rows].sort((a, b) => {
      const va = col.sort(a), vb = col.sort(b);
      const cmp = va < vb ? -1 : va > vb ? 1 : 0;
      return sort.dir === "asc" ? cmp : -cmp;
    });
  } else {
    rows = [...rows].sort(urgencySort);
  }
  const total = window.CASES.filter(c => matchFilter(c, effFilter)).length;
  const head = HEADERS[view];

  // Keyboard: ↑/↓ move row selection, Enter opens, Esc clears (search input excluded).
  useEffect(() => { setKbd(-1); }, [view, filter, query]);
  useEffect(() => {
    function onKey(e) {
      if (open || !isTable) return;
      const t = e.target;
      if (t && /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) return;
      if (e.key === "ArrowDown") { e.preventDefault(); setKbd(k => Math.min(k + 1, rows.length - 1)); }
      else if (e.key === "ArrowUp") { e.preventDefault(); setKbd(k => Math.max(k - 1, 0)); }
      else if (e.key === "Enter" && kbd >= 0 && rows[kbd]) setOpen(rows[kbd]);
      else if (e.key === "Escape") setKbd(-1);
    }
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  });
  useEffect(() => {
    if (kbd < 0) return;
    const el = document.querySelector('tr[data-kbd-selected="true"]');
    if (!el) return;
    const r = el.getBoundingClientRect();
    if (r.bottom > window.innerHeight - 24) window.scrollBy({ top: r.bottom - window.innerHeight + 90, behavior: "smooth" });
    else if (r.top < 90) window.scrollBy({ top: r.top - 140, behavior: "smooth" });
  }, [kbd]);

  return (
    <div style={{ display: "flex" }}>
      <Sidebar active={view} onNav={nav} />
      <main style={{ flex: 1, minWidth: 0, padding: "26px 28px 60px", maxWidth: 1440, margin: "0 auto" }}>
        <Header title={head.title} subtitle={head.subtitle} showDate={head.showDate} query={query} setQuery={setQuery} />

        {view === "dashboard" && <SummaryRow activeFilter={filter} onPick={f => setFilter(f)} />}

        {isTable && (
          <TableCard
            title={view === "attention" ? "Cases needing attention" : effFilter === "upcoming" ? "Upcoming leaves" : effFilter === "all" ? "Leave cases" : "Active leave cases"}
            rows={rows} total={total} sort={sort} setSort={setSort}
            filter={effFilter} setFilter={setFilter} viz={viz} setViz={setViz} prominence={t.attention} onOpen={setOpen}
            showTabs={view !== "attention"} kbdIdx={kbd} query={query} onClearSearch={() => setQuery("")} />
        )}

        {!isTable && <ComingSoon view={view} />}
      </main>

      {open && <Drawer c={open} onClose={() => setOpen(null)} vizMode={viz} />}

      <TweaksPanel>
        <TweakSection label="Needs-attention emphasis" />
        <TweakRadio label="Prominence" value={t.attention}
          options={[{ value: "subtle", label: "Subtle" }, { value: "medium", label: "Medium" }, { value: "strong", label: "Strong" }]}
          onChange={v => setTweak("attention", v)} />
      </TweaksPanel>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
