diff --git a/components/MatrixGroup.tsx b/components/MatrixGroup.tsx
new file mode 100644
index 0000000..26f0f67
--- /dev/null
+++ b/components/MatrixGroup.tsx
@@ -0,0 +1,139 @@
+"use client";
+
+import type { MatrixGroupConfig, FieldType } from "@/types/form";
+import type { UseFormRegister, FieldValues } from "react-hook-form";
+
+interface MatrixGroupProps {
+ group: MatrixGroupConfig;
+ register: UseFormRegister;
+}
+
+/**
+ * Renders a matrix of repeating fields as an HTML table.
+ *
+ * - Row labels live in the leftmost column (sticky on horizontal scroll).
+ * - Column headers carry the period labels (M1..M12, Q1..Q4, …).
+ * - Each row's `type` controls the cell input rendering: currency cells get
+ * a $ prefix on the row label rather than per-cell to save space; percent
+ * cells append %.
+ * - On narrow viewports the table scrolls horizontally; the metric column
+ * stays pinned via position:sticky.
+ *
+ * Cells are still individual react-hook-form fields, registered by
+ * `field.fields[colIndex]`. The matrix only changes layout — submission and
+ * validation behave exactly like the standalone-input version.
+ */
+export function MatrixGroup({ group, register }: MatrixGroupProps) {
+ return (
+
+
+
+ {group.label}
+
+ {group.intro && (
+
{group.intro}
+ )}
+
+
+
+
+
+
+
+ Metric
+
+ {group.cols.map((c) => (
+
+ {c.label}
+
+ ))}
+
+
+
+ {group.rows.map((row, rowIdx) => (
+
+
+ {decoratedRowLabel(row.label, row.type)}
+
+ {row.fields.map((fieldName, colIdx) => (
+
+
+
+ ))}
+
+ ))}
+
+
+
+
+ );
+}
+
+function decoratedRowLabel(label: string, type: FieldType): string {
+ if (type === "currency") return `${label} ($)`;
+ if (type === "percent") return `${label} (%)`;
+ return label;
+}
+
+interface CellInputProps {
+ fieldName: string;
+ type: FieldType;
+ label: string; // for aria-label only
+ register: UseFormRegister;
+}
+
+/**
+ * A compact, table-cell-sized input. No visible label (the row + column
+ * already provide context); a screen-reader label is set via aria-label.
+ * Supports currency / percent / number / text — uniform sizing across cells.
+ */
+function CellInput({ fieldName, type, label, register }: CellInputProps) {
+ const isNumeric = type === "currency" || type === "percent" || type === "number";
+ const baseClass =
+ "w-full rounded border border-stone-200 bg-white px-2 py-1 text-right text-stone-900 " +
+ "tabular-nums shadow-sm transition " +
+ "focus:border-leaf-500 focus:outline-none focus:ring-2 focus:ring-leaf-500/30";
+
+ if (isNumeric) {
+ return (
+
+ );
+ }
+
+ return (
+
+ );
+}
diff --git a/components/StageSection.tsx b/components/StageSection.tsx
index 9ee72ee..1576e42 100644
--- a/components/StageSection.tsx
+++ b/components/StageSection.tsx
@@ -1,9 +1,10 @@
"use client";
-import { useId, useState } from "react";
+import { useId, useMemo, useState } from "react";
import type { StageSectionConfig, SelectOption } from "@/types/form";
import type { UseFormRegister, FieldValues, FieldErrors } from "react-hook-form";
import { FieldRenderer } from "./fields/FieldRenderer";
+import { MatrixGroup } from "./MatrixGroup";
import { evaluate } from "@/lib/conditional";
interface StageSectionProps {
@@ -38,7 +39,19 @@ export function StageSection({
const headingId = useId();
const panelId = useId();
- const visibleFields = section.fields.filter((f) => evaluate(f.visibleWhen, formValues));
+ // Names of fields that appear inside any matrix group; excluded from the
+ // standalone per-field grid so they don't render twice.
+ const matrixFieldNames = useMemo(() => {
+ const names = new Set();
+ for (const m of section.matrixGroups ?? []) {
+ for (const row of m.rows) for (const f of row.fields) names.add(f);
+ }
+ return names;
+ }, [section.matrixGroups]);
+
+ const visibleFields = section.fields.filter(
+ (f) => evaluate(f.visibleWhen, formValues) && !matrixFieldNames.has(f.name),
+ );
return (
{section.intro}