===== ISTEM =====
Solve the following programming task.

TASK: Payment and Refund Reconciliation

Write a function 'reconcile(transactions)' that processes a list of payment and refund transactions in order and returns a dict with two keys, 'balances' and 'rejected'.

Each transaction is a dict with the keys 'id' (str), 'type' ('payment' or 'refund'), 'account' (str) and 'amount' (int, in cents). A refund also has 'refers_to', the id of the payment it refunds. You can assume every transaction has these keys with these types.

Process the transactions in list order:
- A transaction whose id has already appeared earlier in the list is rejected with reason 'duplicate', whether or not the earlier one was accepted.
- A transaction whose amount is 0 or negative is rejected with reason 'invalid_amount'.
- A refund is rejected with reason 'unknown_payment' if 'refers_to' is not the id of an earlier, accepted payment. This includes ids of refunds, ids of rejected payments, and ids of payments that appear later in the list.
- A refund is rejected with reason 'account_mismatch' if its account differs from the account of the payment it refers to.
- A refund is rejected with reason 'over_refund' if the accepted refunds for that payment, including this one, would add up to more than the payment's amount.
- If more than one reason applies, use the first one in the order listed above.
- An accepted payment adds its amount to its account's balance; an accepted refund subtracts its amount. Rejected transactions change nothing.

Return:
- 'balances': a dict mapping each account that appears in at least one accepted transaction to its final balance in cents. Accounts that appear only in rejected transactions are not included.
- 'rejected': a list of (id, reason) tuples, in the order the rejected transactions appeared.

STARTER CODE (python):
def reconcile(transactions: list) -> dict:
    # Write your code here
    pass


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```python
def reconcile(transactions: list) -> dict:
    balances = {}
    rejected = []
    seen_ids = set()
    accepted_payments = {}  # id -> {account, amount, refunded_total}
    
    for tx in transactions:
        tx_id = tx['id']
        tx_type = tx['type']
        tx_account = tx['account']
        tx_amount = tx['amount']
        
        # 1. Duplicate check
        if tx_id in seen_ids:
            rejected.append((tx_id, 'duplicate'))
            seen_ids.add(tx_id)
            continue
            
        # 2. Invalid amount check
        if tx_amount <= 0:
            rejected.append((tx_id, 'invalid_amount'))
            seen_ids.add(tx_id)
            continue
            
        if tx_type == 'payment':
            # Accept payment
            seen_ids.add(tx_id)
            accepted_payments[tx_id] = {'account': tx_account, 'amount': tx_amount, 'refunded_total': 0}
            balances[tx_account] = balances.get(tx_account, 0) + tx_amount
            
        elif tx_type == 'refund':
            ref_id = tx['refers_to']
            
            # 3. Unknown payment check
            if ref_id not in accepted_payments:
                rejected.append((tx_id, 'unknown_payment'))
                seen_ids.add(tx_id)
                continue
                
            # 4. Account mismatch check
            if accepted_payments[ref_id]['account'] != tx_account:
                rejected.append((tx_id, 'account_mismatch'))
                seen_ids.add(tx_id)
                continue
                
            # 5. Over refund check
            if accepted_payments[ref_id]['refunded_total'] + tx_amount > accepted_payments[ref_id]['amount']:
                rejected.append((tx_id, 'over_refund'))
                seen_ids.add(tx_id)
                continue
            
            # Accept refund
            seen_ids.add(tx_id)
            accepted_payments[ref_id]['refunded_total'] += tx_amount
            balances[tx_account] -= tx_amount
            
    return {'balances': balances, 'rejected': rejected}
```