{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://yaml.sh/schema/interface-v2.json",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "page",
    "view"
  ],
  "properties": {
    "version": {
      "const": 2
    },
    "page": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "title"
      ],
      "properties": {
        "title": {
          "type": "string",
          "maxLength": 160,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Page title."
        },
        "description": {
          "type": "string",
          "maxLength": 2000,
          "description": "Optional page description."
        },
        "eyebrow": {
          "type": "string",
          "maxLength": 80,
          "description": "Optional short label above the title."
        }
      }
    },
    "state": {
      "type": "object",
      "maxProperties": 32,
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9_]{0,31}$",
        "not": {
          "enum": [
            "constructor",
            "prototype",
            "__proto__"
          ]
        }
      },
      "additionalProperties": {
        "anyOf": [
          {
            "type": "string",
            "maxLength": 2000
          },
          {
            "type": "number",
            "minimum": -1000000,
            "maximum": 1000000
          },
          {
            "type": "boolean"
          }
        ]
      }
    },
    "view": {
      "$ref": "#/$defs/node"
    }
  },
  "$defs": {
    "node": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": {
        "propertyName": "type"
      },
      "oneOf": [
        {
          "$ref": "#/$defs/stack"
        },
        {
          "$ref": "#/$defs/grid"
        },
        {
          "$ref": "#/$defs/card"
        },
        {
          "$ref": "#/$defs/heading"
        },
        {
          "$ref": "#/$defs/text"
        },
        {
          "$ref": "#/$defs/badge"
        },
        {
          "$ref": "#/$defs/metric"
        },
        {
          "$ref": "#/$defs/divider"
        },
        {
          "$ref": "#/$defs/link"
        },
        {
          "$ref": "#/$defs/input"
        },
        {
          "$ref": "#/$defs/toggle"
        },
        {
          "$ref": "#/$defs/button"
        }
      ]
    },
    "stack": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "children"
      ],
      "properties": {
        "type": {
          "const": "stack"
        },
        "children": {
          "type": "array",
          "minItems": 1,
          "maxItems": 30,
          "items": {
            "$ref": "#/$defs/node"
          },
          "description": "One to 30 nested components."
        },
        "gap": {
          "enum": [
            "sm",
            "md",
            "lg"
          ],
          "description": "Spacing: sm, md (default), or lg."
        },
        "direction": {
          "enum": [
            "vertical",
            "horizontal"
          ],
          "description": "Direction: vertical (default) or horizontal."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "grid": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "children"
      ],
      "properties": {
        "type": {
          "const": "grid"
        },
        "children": {
          "type": "array",
          "minItems": 1,
          "maxItems": 30,
          "items": {
            "$ref": "#/$defs/node"
          },
          "description": "One to 30 nested components."
        },
        "columns": {
          "enum": [
            1,
            2,
            3
          ],
          "description": "Column count: 1, 2 (default), or 3."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "card": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "children"
      ],
      "properties": {
        "type": {
          "const": "card"
        },
        "children": {
          "type": "array",
          "minItems": 1,
          "maxItems": 30,
          "items": {
            "$ref": "#/$defs/node"
          },
          "description": "One to 30 nested components."
        },
        "title": {
          "type": "string",
          "maxLength": 160,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Optional title; accepts {{key}} state text."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "heading": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "text"
      ],
      "properties": {
        "type": {
          "const": "heading"
        },
        "text": {
          "type": "string",
          "maxLength": 160,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Heading text; accepts {{key}} state text."
        },
        "level": {
          "enum": [
            2,
            3
          ],
          "description": "Heading level: 2 (default) or 3."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "text": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "text"
      ],
      "properties": {
        "type": {
          "const": "text"
        },
        "text": {
          "type": "string",
          "maxLength": 2000,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Plain text; accepts {{key}} state text."
        },
        "tone": {
          "enum": [
            "default",
            "muted"
          ],
          "description": "Text tone: default or muted."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "badge": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "text"
      ],
      "properties": {
        "type": {
          "const": "badge"
        },
        "text": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Badge label; accepts {{key}} state text."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "metric": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "label",
        "value"
      ],
      "properties": {
        "type": {
          "const": "metric"
        },
        "label": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "A short description of the value."
        },
        "value": {
          "type": "string",
          "maxLength": 160,
          "minLength": 1,
          "pattern": "\\S",
          "description": "A quoted string; accepts {{key}} state text."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "divider": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "const": "divider"
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "link": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "label",
        "href"
      ],
      "properties": {
        "type": {
          "const": "link"
        },
        "label": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "A meaningful link label."
        },
        "href": {
          "type": "string",
          "maxLength": 2048,
          "minLength": 1,
          "pattern": "\\S",
          "description": "HTTPS, mailto:, or tel: only. No fragments or templates."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "input": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "label",
        "bind"
      ],
      "properties": {
        "type": {
          "const": "input"
        },
        "label": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Visible field label."
        },
        "bind": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Existing state key whose value is a string."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "toggle": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "label",
        "bind"
      ],
      "properties": {
        "type": {
          "const": "toggle"
        },
        "label": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Visible checkbox label."
        },
        "bind": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Existing state key whose value is a boolean."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    },
    "button": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "label",
        "action"
      ],
      "properties": {
        "type": {
          "const": "button"
        },
        "label": {
          "type": "string",
          "maxLength": 80,
          "minLength": 1,
          "pattern": "\\S",
          "description": "Visible button label."
        },
        "action": {
          "enum": [
            "increment",
            "decrement",
            "toggle",
            "reset"
          ],
          "description": "increment/decrement a number, toggle a boolean, or reset all state."
        },
        "target": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "State key for the action. Required except for reset, which forbids it."
        },
        "variant": {
          "enum": [
            "primary",
            "secondary"
          ],
          "description": "Button appearance: primary (default) or secondary."
        },
        "when": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,31}$",
          "description": "Render only when this existing boolean state value is true."
        }
      }
    }
  }
}
