{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Refined Math ModelSpec v1",
  "description": "Importable mathematical model. Native validation additionally checks names, shapes, finite domains, numeric policy and checked arithmetic. Use strings for exact decimal, fraction or large integer data.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "title", "variables", "data", "constraints", "goal"],
  "properties": {
    "schemaVersion": { "const": 1 },
    "title": { "type": "string", "maxLength": 120 },
    "numericPolicy": { "enum": ["auto", "exact", "real"], "default": "auto" },
    "tolerance": {
      "type": "object", "additionalProperties": false,
      "properties": {
        "absolute": { "type": "number", "minimum": 0, "maximum": 0.1, "default": 1e-8 },
        "relative": { "type": "number", "minimum": 0, "maximum": 0.1, "default": 1e-8 }
      }
    },
    "variables": {
      "type": "array", "minItems": 1, "maxItems": 16,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["name", "kind", "shape", "lower", "upper"],
        "properties": {
          "name": { "$ref": "#/$defs/name" },
          "kind": { "enum": ["integer", "binary", "permutation", "real"] },
          "shape": { "type": "array", "minItems": 1, "maxItems": 2, "items": { "type": "integer", "minimum": 1, "maximum": 512 } },
          "lower": { "$ref": "#/$defs/bound" },
          "upper": { "$ref": "#/$defs/bound" }
        }
      }
    },
    "data": {
      "type": "array", "maxItems": 32,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["name", "values"],
        "properties": {
          "name": { "$ref": "#/$defs/name" },
          "values": { "type": "array", "minItems": 1, "maxItems": 512, "items": { "$ref": "#/$defs/number" } }
        }
      }
    },
    "constraints": { "type": "array", "maxItems": 64, "items": { "$ref": "#/$defs/expression" } },
    "goal": {
      "type": "object", "additionalProperties": false, "required": ["mode"],
      "properties": {
        "mode": { "enum": ["find", "minimize", "maximize", "count"] },
        "expression": { "type": "string", "maxLength": 4096 }
      },
      "allOf": [
        {
          "if": { "properties": { "mode": { "enum": ["minimize", "maximize"] } } },
          "then": { "required": ["expression"], "properties": { "expression": { "$ref": "#/$defs/expression" } } }
        }
      ]
    }
  },
  "$defs": {
    "name": { "type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,31}$" },
    "expression": { "type": "string", "minLength": 1, "maxLength": 4096 },
    "bound": {
      "description": "Discrete bounds must be integers within +/-1e9; real bounds within +/-1e12. Binary domains are 0..1.",
      "oneOf": [
        { "type": "number", "minimum": -1000000000000, "maximum": 1000000000000 },
        { "$ref": "#/$defs/numberString" }
      ]
    },
    "numberString": { "type": "string", "minLength": 1, "maxLength": 100, "pattern": "^[-+]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)(?:[eE][-+]?[0-9]+)?(?:/[-+]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)(?:[eE][-+]?[0-9]+)?)?$" },
    "number": {
      "oneOf": [
        { "$ref": "#/$defs/numberString" },
        { "type": "number", "description": "Exact mode accepts safe integer JSON numbers only. Prefer strings for portable exact data." }
      ]
    }
  }
}
