Design decisions where the spec is open:

- Payload shape: a dict with an "invoices" list. A bare list is also accepted, and empty or None input yields an empty result.
- Charge is the invoice's line total in cents plus fee_cents. Lines are summed as exact Decimals and rounded once, because rounding=invoice.
- Invoices below min_invoice_cents are skipped (charge < 25).
- Event deduplication uses a dict keyed by eid. A repeated eid keeps the first key's position but takes the last value.
- The ceiling is the principal only (charge minus fee), since fee_refundable is false.
- Overflow is rejected: a refund whose cents would push the refunded total over the ceiling is skipped entirely.
- The age check requires 0 <= day - created < 45, which is exclusive because inclusive_limit is false.
- A void sets active to false. Restore re-enables refunds only when void_is_tombstone is false, so under this policy restore works after a void.
- Status is "VOID" if inactive, "REFUNDED" if refunded == charge, otherwise "OPEN".
- Because the ceiling excludes the fee, refunded can never equal charge when the fee is positive. That is acceptable.
- Invoices are sorted by (created, id).
- The payload is never mutated.

```json
{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "policy.py": "POLICY = {\"currency\": \"CAD\", \"duplicate_event\": \"last\", \"fee_refundable\": False, \"inclusive_limit\": False, \"max_refund_age_days\": 45, \"min_invoice_cents\": 25, \"order\": \"created\", \"refund_overflow\": \"reject\", \"refunds_first\": False, \"rounding\": \"invoice\", \"status_case\": \"upper\", \"void_is_tombstone\": False}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef to_cents(x):\n    return int((Decimal(str(x)) * 100).quantize(Decimal(1), rounding=ROUND_HALF_UP))\n\ndef dec_cents(x):\n    return Decimal(str(x)) * 100\n\ndef rnd(d):\n    return int(d.quantize(Decimal(1), rounding=ROUND_HALF_UP))\n", "core.py": "from helpers import dec_cents, rnd\n\ndef run(payload, policy):\n    if not payload:\n        return {\"invoices\": []}\n    if isinstance(payload, dict):\n        invs = payload.get(\"invoices\") or []\n    else:\n        invs = payload\n    out = []\n    for inv in invs:\n        if inv.get(\"currency\") != policy[\"currency\"]:\n            continue\n        ds = [dec_cents(l) for l in inv.get(\"lines\", [])]\n        if policy[\"rounding\"] == \"per_line\":\n            principal = sum(rnd(d) for d in ds)\n        else:\n            principal = rnd(sum(ds)) if ds else 0\n        fee = int(inv.get(\"fee_cents\", 0))\n        charge = principal + fee\n        if charge < policy[\"min_invoice_cents\"]:\n            continue\n        evs = {}\n        for e in inv.get(\"events\", []):\n            k = e[\"eid\"]\n            if k in evs and policy[\"duplicate_event\"] != \"last\":\n                continue\n            evs[k] = e\n        evl = list(evs.values())\n        if policy[\"refunds_first\"]:\n            evl = [e for e in evl if e[\"kind\"] == \"refund\"] + [e for e in evl if e[\"kind\"] != \"refund\"]\n        ceiling = charge if policy[\"fee_refundable\"] else principal\n        created = inv[\"created\"]\n        active = True\n        dead = False\n        refunded = 0\n        for e in evl:\n            kind = e[\"kind\"]\n            if kind == \"void\":\n                active = False\n                if policy[\"void_is_tombstone\"]:\n                    dead = True\n            elif kind == \"restore\":\n                if not dead:\n                    active = True\n            elif kind == \"refund\":\n                if not active:\n                    continue\n                age = e[\"day\"] - created\n                lim = policy[\"max_refund_age_days\"]\n                ok = age >= 0 and (age <= lim if policy[\"inclusive_limit\"] else age < lim)\n                if not ok:\n                    continue\n                c = int(e[\"cents\"])\n                room = ceiling - refunded\n                if c > room:\n                    if policy[\"refund_overflow\"] == \"reject\":\n                        continue\n                    c = room\n                refunded += c\n        if not active:\n            st = \"void\"\n        elif refunded == charge:\n            st = \"refunded\"\n        else:\n            st = \"open\"\n        st = st.upper() if policy[\"status_case\"] == \"upper\" else st.lower()\n        out.append({\"id\": inv[\"id\"], \"charge\": charge, \"refunded\": refunded, \"balance\": charge - refunded, \"status\": st, \"_c\": created})\n    if policy[\"order\"] == \"created\":\n        out.sort(key=lambda r: (r[\"_c\"], r[\"id\"]))\n    else:\n        out.sort(key=lambda r: r[\"id\"])\n    for r in out:\n        del r[\"_c\"]\n    return {\"invoices\": out}\n"}}
```