{
  "id": "classic_tetris",
  "type": "module",
  "version": "1.2",
  "context": {
    "layout": "phone",
    "title": "Classic Tetris",
    "icon": "🧱",
    "shapes": [
      [
        [
          4,
          5,
          6,
          7
        ],
        [
          2,
          6,
          10,
          14
        ],
        [
          8,
          9,
          10,
          11
        ],
        [
          1,
          5,
          9,
          13
        ]
      ],
      [
        [
          1,
          2,
          5,
          6
        ],
        [
          1,
          2,
          5,
          6
        ],
        [
          1,
          2,
          5,
          6
        ],
        [
          1,
          2,
          5,
          6
        ]
      ],
      [
        [
          1,
          4,
          5,
          6
        ],
        [
          1,
          5,
          6,
          9
        ],
        [
          4,
          5,
          6,
          9
        ],
        [
          1,
          4,
          5,
          9
        ]
      ],
      [
        [
          1,
          2,
          4,
          5
        ],
        [
          1,
          5,
          6,
          10
        ],
        [
          5,
          6,
          8,
          9
        ],
        [
          0,
          4,
          5,
          9
        ]
      ],
      [
        [
          0,
          1,
          5,
          6
        ],
        [
          2,
          5,
          6,
          9
        ],
        [
          4,
          5,
          9,
          10
        ],
        [
          1,
          4,
          5,
          8
        ]
      ],
      [
        [
          0,
          4,
          5,
          6
        ],
        [
          1,
          2,
          5,
          9
        ],
        [
          4,
          5,
          6,
          10
        ],
        [
          1,
          5,
          8,
          9
        ]
      ],
      [
        [
          2,
          4,
          5,
          6
        ],
        [
          1,
          5,
          9,
          10
        ],
        [
          4,
          5,
          6,
          8
        ],
        [
          0,
          1,
          5,
          9
        ]
      ]
    ],
    "palette": [
      "#142138",
      "#39CDE0",
      "#FFD45C",
      "#B38AFF",
      "#65D89B",
      "#FF7188",
      "#729DFF",
      "#FFAD69"
    ],
    "pieceNames": [
      "I",
      "O",
      "T",
      "S",
      "Z",
      "J",
      "L"
    ],
    "activeShape": "{{ @{shapes}[${piece}][${rotation}] }}",
    "activeCells": "{{ map(@{activeShape}, (${y} + cast(@{item} / 4, 'int')) * 10 + ${x} + @{item} % 4) }}",
    "candidateShape": "{{ @{shapes}[${piece}][${nr}] }}",
    "candidateChecks": "{{ map(@{candidateShape}, (${nx} + @{item} % 4 < 0 || ${nx} + @{item} % 4 >= 10 || ${ny} + cast(@{item} / 4, 'int') < 0 || ${ny} + cast(@{item} / 4, 'int') >= 20) ? false : ${board}[(${ny} + cast(@{item} / 4, 'int')) * 10 + ${nx} + @{item} % 4] == 0) }}",
    "fits": "{{ !contains(@{candidateChecks}, false) }}",
    "rowStats": "{{ map(range(20), {'row': @{item}, 'keep': contains(slice(${board}, @{item} * 10, @{item} * 10 + 10), 0)}) }}",
    "keptRows": "${retainedRows}",
    "clearCount": "{{ 20 - count(@{keptRows}) }}",
    "compacted": "{{ map(range(200), @{item} < @{clearCount} * 10 ? 0 : ${board}[@{keptRows}[cast(@{item} / 10, 'int') - @{clearCount}] * 10 + @{item} % 10]) }}",
    "level": "{{ cast(${lines} / 10, 'int') + 1 }}",
    "clearPoints": "{{ switch(@{clearCount} == 1, 40, @{clearCount} == 2, 100, @{clearCount} == 3, 300, @{clearCount} == 4, 1200, 0) * @{level} }}",
    "speedTicks": "{{ max([2, 10 - cast(${lines} / 10, 'int')]) }}",
    "playing": "{{ ${started} && !${paused} && !${over} && !${loading} && ${mode} != 'locking' }}",
    "status": "{{ switch(${loading}, 'Loading…', !${started}, 'Ready to stack', ${over}, 'Game over', ${paused}, 'Paused', ${mode} == 'locking', 'Locking…', ${fast}, 'Fast drop', 'In play') }}",
    "bestNow": "{{ max([${best}, ${score}]) }}"
  },
  "state": {
    "board": [],
    "piece": 2,
    "next": 0,
    "x": 3,
    "y": 0,
    "rotation": 0,
    "nx": 3,
    "ny": 0,
    "nr": 0,
    "mode": "",
    "started": false,
    "paused": true,
    "over": false,
    "fast": false,
    "tick": 0,
    "score": 0,
    "lines": 0,
    "best": 0,
    "loading": true,
    "saveError": false,
    "restartPrompt": false,
    "note": "",
    "retainedRows": []
  },
  "dependencies": [
    {
      "repository": {
        "local": {
          "id": "classic_tetris_portrait_v1",
          "config": {}
        }
      }
    },
    {
      "service": {
        "audio": {
          "id": "tetris_audio"
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "new_game",
          "input": {
            "board": "{{ map(range(200), 0) }}",
            "piece": "{{ random(0, 6) }}",
            "next": "{{ random(0, 6) }}",
            "x": 3,
            "y": 0,
            "rotation": 0,
            "nx": 3,
            "ny": 0,
            "nr": 0,
            "mode": "",
            "started": true,
            "paused": false,
            "over": false,
            "fast": false,
            "tick": 0,
            "score": 0,
            "lines": 0,
            "best": "@{bestNow}",
            "note": "",
            "restartPrompt": false
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "start_gravity",
                  "input": "sync_gravity"
                }
              },
              {
                "template": {
                  "id": "save_new",
                  "input": "persist_game"
                }
              },
              {
                "service": {
                  "id": "start_touch",
                  "input": {
                    "serviceId": "tetris_audio",
                    "params": {
                      "haptic": "medium"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "step_down",
          "input": {
            "nx": "${x}",
            "ny": "{{ ${y} + 1 }}",
            "nr": "${rotation}",
            "mode": "fall",
            "tick": 0
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "test_gravity",
                  "input": "attempt_move"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "condition": {
          "id": "attempt_move",
          "input": "{{ count(@{shapes}[${piece}][${nr}]) == 4 && !contains(map(@{shapes}[${piece}][${nr}], (${nx} + @{item} % 4 < 0 || ${nx} + @{item} % 4 >= 10 || ${ny} + cast(@{item} / 4, 'int') < 0 || ${ny} + cast(@{item} / 4, 'int') >= 20) ? false : ${board}[(${ny} + cast(@{item} / 4, 'int')) * 10 + ${nx} + @{item} % 4] == 0), false) }}",
          "output": {
            "success": [
              {
                "state": {
                  "id": "commit_move",
                  "input": {
                    "x": "${nx}",
                    "y": "${ny}",
                    "rotation": "${nr}",
                    "note": "",
                    "score": "{{ ${score} + (${mode} == 'soft' ? 1 : 0) }}"
                  }
                }
              }
            ],
            "failure": [
              {
                "condition": {
                  "id": "landing_check",
                  "input": "{{ ${mode} == 'fall' || ${mode} == 'soft' }}",
                  "output": {
                    "success": [
                      {
                        "template": {
                          "id": "land_piece",
                          "input": "lock_piece"
                        }
                      }
                    ],
                    "failure": [
                      {
                        "state": {
                          "id": "blocked_move",
                          "input": {
                            "note": "{{ ${mode} == 'rotate' ? 'Rotation blocked — move away first.' : 'Edge or block in the way.' }}"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "condition": {
          "id": "lock_piece",
          "input": "{{ count(@{shapes}[${piece}][${rotation}]) == 4 && !contains(map(@{shapes}[${piece}][${rotation}], (${x} + @{item} % 4 < 0 || ${x} + @{item} % 4 >= 10 || ${y} + 1 + cast(@{item} / 4, 'int') < 0 || ${y} + 1 + cast(@{item} / 4, 'int') >= 20) ? false : ${board}[(${y} + 1 + cast(@{item} / 4, 'int')) * 10 + ${x} + @{item} % 4] == 0), false) }}",
          "output": {
            "success": [
              {
                "state": {
                  "id": "continue_descent",
                  "input": {
                    "y": "{{ ${y} + 1 }}",
                    "nx": "${x}",
                    "ny": "{{ ${y} + 1 }}",
                    "nr": "${rotation}",
                    "mode": "",
                    "tick": 0,
                    "note": "",
                    "score": "{{ ${score} + (${mode} == 'soft' ? 1 : 0) }}"
                  }
                }
              }
            ],
            "failure": [
              {
                "state": {
                  "id": "settle_blocked_piece",
                  "input": {
                    "board": "{{ setAt(setAt(setAt(setAt(${board}, (${y} + cast(@{shapes}[${piece}][${rotation}][0] / 4, 'int')) * 10 + ${x} + @{shapes}[${piece}][${rotation}][0] % 4, ${piece} + 1), (${y} + cast(@{shapes}[${piece}][${rotation}][1] / 4, 'int')) * 10 + ${x} + @{shapes}[${piece}][${rotation}][1] % 4, ${piece} + 1), (${y} + cast(@{shapes}[${piece}][${rotation}][2] / 4, 'int')) * 10 + ${x} + @{shapes}[${piece}][${rotation}][2] % 4, ${piece} + 1), (${y} + cast(@{shapes}[${piece}][${rotation}][3] / 4, 'int')) * 10 + ${x} + @{shapes}[${piece}][${rotation}][3] % 4, ${piece} + 1) }}",
                    "mode": "locking"
                  },
                  "output": {
                    "success": [
                      {
                        "template": {
                          "id": "resolve_lines",
                          "input": "snapshot_rows"
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "clear_lines",
          "input": {
            "board": "{{ count(${retainedRows}) == 20 ? ${board} : map(range(200), @{item} < (20 - count(${retainedRows})) * 10 ? 0 : ${board}[${retainedRows}[cast(@{item} / 10, 'int') - (20 - count(${retainedRows}))] * 10 + @{item} % 10]) }}",
            "score": "{{ ${score} + switch(20 - count(${retainedRows}) == 1, 40, 20 - count(${retainedRows}) == 2, 100, 20 - count(${retainedRows}) == 3, 300, 20 - count(${retainedRows}) == 4, 1200, 0) * (cast(${lines} / 10, 'int') + 1) }}",
            "lines": "{{ ${lines} + 20 - count(${retainedRows}) }}",
            "note": "{{ count(${retainedRows}) < 20 ? cast(20 - count(${retainedRows}), 'str') + (count(${retainedRows}) == 19 ? ' line cleared' : ' lines cleared') : '' }}"
          },
          "output": {
            "success": [
              {
                "service": {
                  "id": "lock_feedback",
                  "input": {
                    "serviceId": "tetris_audio",
                    "params": {
                      "haptic": "{{ notEmpty(${note}) ? 'success' : 'light' }}"
                    }
                  }
                }
              },
              {
                "template": {
                  "id": "advance_piece",
                  "input": "spawn_piece"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "spawn_piece",
          "input": {
            "piece": "${next}",
            "next": "{{ random(0, 6) }}",
            "x": 3,
            "y": 0,
            "rotation": 0,
            "nx": 3,
            "ny": 0,
            "nr": 0,
            "fast": false,
            "tick": 0,
            "mode": "locking"
          },
          "output": {
            "success": [
              {
                "condition": {
                  "id": "spawn_fits",
                  "input": "{{ count(@{shapes}[${piece}][0]) == 4 && !contains(map(@{shapes}[${piece}][0], ${board}[cast(@{item} / 4, 'int') * 10 + 3 + @{item} % 4] == 0), false) }}",
                  "output": {
                    "success": [
                      {
                        "state": {
                          "id": "spawn_ready",
                          "input": {
                            "mode": ""
                          },
                          "output": {
                            "success": [
                              {
                                "template": {
                                  "id": "save_locked",
                                  "input": "persist_game"
                                }
                              }
                            ]
                          }
                        }
                      }
                    ],
                    "failure": [
                      {
                        "state": {
                          "id": "finish_game",
                          "input": {
                            "over": true,
                            "paused": true,
                            "mode": "",
                            "note": "No room for the next piece."
                          },
                          "output": {
                            "success": [
                              {
                                "template": {
                                  "id": "stop_over_gravity",
                                  "input": "sync_gravity"
                                }
                              },
                              {
                                "service": {
                                  "id": "over_feedback",
                                  "input": {
                                    "serviceId": "tetris_audio",
                                    "params": {
                                      "haptic": "error"
                                    }
                                  }
                                }
                              },
                              {
                                "template": {
                                  "id": "save_over",
                                  "input": "persist_game"
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "repo": {
          "id": "persist_game",
          "input": {
            "repositoryId": "classic_tetris_portrait_v1",
            "operation": "create",
            "params": {
              "collection": "session",
              "documentId": "current",
              "rewrite": true,
              "body": {
                "board": "${board}",
                "piece": "${piece}",
                "next": "${next}",
                "x": "${x}",
                "y": "${y}",
                "rotation": "${rotation}",
                "started": "${started}",
                "over": "${over}",
                "score": "${score}",
                "lines": "${lines}",
                "best": "@{bestNow}"
              }
            }
          },
          "output": {
            "success": [
              {
                "state": {
                  "id": "save_ok",
                  "input": {
                    "saveError": false
                  }
                }
              }
            ],
            "failure": [
              {
                "state": {
                  "id": "save_failed",
                  "input": {
                    "saveError": true
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "repo": {
          "id": "load_game",
          "input": {
            "repositoryId": "classic_tetris_portrait_v1",
            "operation": "read",
            "params": {
              "collection": "session",
              "documentId": "current"
            }
          },
          "output": {
            "success": [
              {
                "condition": {
                  "id": "valid_snapshot",
                  "input": "{{ count(@{load_game.board}) == 200 }}",
                  "output": {
                    "success": [
                      {
                        "state": {
                          "id": "restore_game",
                          "input": {
                            "board": "@{load_game.board}",
                            "piece": "@{load_game.piece}",
                            "next": "@{load_game.next}",
                            "x": "@{load_game.x}",
                            "y": "@{load_game.y}",
                            "rotation": "@{load_game.rotation}",
                            "started": "@{load_game.started}",
                            "over": "{{ @{load_game.over} && contains(map(@{shapes}[@{load_game.piece}][0], @{load_game.board}[cast(@{item} / 4, 'int') * 10 + 3 + @{item} % 4] != 0), true) }}",
                            "score": "@{load_game.score}",
                            "lines": "@{load_game.lines}",
                            "best": "@{load_game.best}",
                            "paused": true,
                            "loading": false,
                            "fast": false,
                            "tick": 0,
                            "mode": "",
                            "note": ""
                          }
                        }
                      }
                    ],
                    "failure": [
                      {
                        "template": {
                          "id": "empty_snapshot",
                          "input": "empty_game"
                        }
                      }
                    ]
                  }
                }
              }
            ],
            "failure": [
              {
                "template": {
                  "id": "first_launch",
                  "input": "empty_game"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "empty_game",
          "input": {
            "board": "{{ map(range(200), 0) }}",
            "loading": false,
            "paused": true,
            "started": false,
            "over": false,
            "score": 0,
            "lines": 0,
            "note": ""
          }
        }
      }
    },
    {
      "action": {
        "service": {
          "id": "tap_feedback",
          "input": {
            "serviceId": "tetris_audio",
            "params": {
              "haptic": "light"
            }
          }
        }
      }
    },
    {
      "action": {
        "condition": {
          "id": "sync_gravity",
          "input": "{{ ${started} && !${paused} && !${over} && !${loading} }}",
          "output": {
            "success": [
              {
                "state": {
                  "id": "arm_gravity",
                  "input": {
                    "tick": 0
                  }
                }
              }
            ],
            "failure": [
              {
                "state": {
                  "id": "disarm_gravity",
                  "input": {
                    "tick": 0
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "snapshot_rows",
          "input": {
            "retainedRows": "{{ map(range(20), contains(slice(${board}, @{item} * 10, @{item} * 10 + 10), 0) ? @{item} : -1) - [-1] }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "apply_line_clear",
                  "input": "clear_lines"
                }
              }
            ]
          }
        }
      }
    }
  ],
  "children": [
    {
      "id": "game_root",
      "type": "vstack",
      "context": {
        "_spacing": 14
      },
      "style": {
        "layout": {
          "padding": {
            "top": 8,
            "horizontal": 16,
            "bottom": 12
          },
          "maxWidth": "infinity",
          "maxHeight": "infinity"
        },
        "modal": {
          "alert": {
            "isPresented": "${restartPrompt}",
            "title": "Start a new game?",
            "message": "This board will be cleared. Your best score stays.",
            "actions": [
              {
                "id": "confirm_restart",
                "type": "button",
                "context": {
                  "_label": "New game"
                },
                "events": {
                  "onTap": [
                    {
                      "template": {
                        "id": "confirmed_new",
                        "input": "new_game"
                      }
                    }
                  ]
                }
              },
              {
                "id": "keep_game",
                "type": "button",
                "context": {
                  "_label": "Keep game"
                },
                "events": {
                  "onTap": [
                    {
                      "state": {
                        "id": "dismiss_restart",
                        "input": {
                          "restartPrompt": false
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      },
      "children": [
        {
          "id": "score_strip",
          "type": "hstack",
          "context": {
            "_spacing": 20,
            "_alignment": "center"
          },
          "style": {
            "layout": {
              "maxWidth": "infinity",
              "padding": {
                "horizontal": 18,
                "vertical": 12
              }
            },
            "common": {
              "background": {
                "material": "ultraThin"
              },
              "foreground": {
                "color": "onSurface"
              },
              "cornerRadius": 20
            },
            "shadow": {
              "color": {
                "color": "black",
                "opacity": 0.1
              },
              "radius": 10,
              "y": 4
            }
          },
          "children": [
            {
              "id": "score_group",
              "type": "vstack",
              "context": {
                "_spacing": 2,
                "_alignment": "leading"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "alignment": "leading"
                }
              },
              "children": [
                {
                  "id": "score_caption",
                  "type": "hstack",
                  "context": {
                    "_spacing": 5
                  },
                  "children": [
                    {
                      "id": "score_symbol",
                      "type": "image",
                      "context": {
                        "_source": "sf://sparkles"
                      },
                      "style": {
                        "layout": {
                          "width": 12,
                          "height": 12
                        },
                        "common": {
                          "foreground": {
                            "color": "secondary"
                          }
                        }
                      }
                    },
                    {
                      "id": "score_caption_text",
                      "type": "text",
                      "context": {
                        "_text": "{{ ${over} ? 'FINAL SCORE' : 'SCORE' }}"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "caption2",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "secondary"
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "score_value",
                  "type": "text",
                  "context": {
                    "_text": "${score}",
                    "_format": {
                      "number": {
                        "style": "decimal",
                        "maximumFractionDigits": 0
                      }
                    }
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "title",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "onSurface"
                      }
                    },
                    "text": {
                      "lineLimit": 1,
                      "minimumScaleFactor": 0.6
                    }
                  }
                }
              ]
            },
            {
              "id": "lines_group",
              "type": "vstack",
              "context": {
                "_spacing": 4
              },
              "children": [
                {
                  "id": "lines_caption",
                  "type": "text",
                  "context": {
                    "_text": "LINES"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                },
                {
                  "id": "lines_value",
                  "type": "text",
                  "context": {
                    "_text": "${lines}",
                    "_format": {
                      "number": {
                        "maximumFractionDigits": 0,
                        "style": "decimal"
                      }
                    }
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "title3",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "onSurface"
                      }
                    }
                  }
                }
              ]
            },
            {
              "id": "level_group",
              "type": "vstack",
              "context": {
                "_spacing": 4
              },
              "children": [
                {
                  "id": "level_caption",
                  "type": "text",
                  "context": {
                    "_text": "LEVEL"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                },
                {
                  "id": "level_value",
                  "type": "text",
                  "context": {
                    "_text": "@{level}",
                    "_format": {
                      "number": {
                        "maximumFractionDigits": 0,
                        "style": "decimal"
                      }
                    }
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "title3",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "onSurface"
                      }
                    }
                  }
                }
              ]
            }
          ]
        },
        {
          "id": "cabinet",
          "type": "hstack",
          "context": {
            "_spacing": 14,
            "_alignment": "top"
          },
          "style": {
            "layout": {
              "padding": 12,
              "maxWidth": "infinity"
            },
            "common": {
              "background": {
                "gradient": {
                  "linear": {
                    "colors": [
                      "#14253E",
                      "#0B1425"
                    ],
                    "startPoint": "topLeading",
                    "endPoint": "bottomTrailing"
                  }
                }
              },
              "foreground": {
                "color": "white"
              },
              "cornerRadius": 22
            },
            "shadow": {
              "color": {
                "color": "black",
                "opacity": 0.28
              },
              "radius": 14,
              "y": 7
            }
          },
          "children": [
            {
              "id": "field_stack",
              "type": "zstack",
              "style": {
                "layout": {
                  "width": 188,
                  "height": 378
                },
                "common": {
                  "background": {
                    "color": "#0A1020"
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 4
                }
              },
              "children": [
                {
                  "id": "playfield",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 2
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 17,
                          "spacing": 2
                        }
                      ]
                    },
                    "layout": {
                      "width": 188,
                      "height": 378
                    }
                  },
                  "children": [
                    {
                      "id": "board_cells",
                      "type": "dynamic",
                      "context": {
                        "_data": "${board}"
                      },
                      "prototype": {
                        "id": "board_cell",
                        "type": "roundedrectangle",
                        "context": {
                          "isActive": "{{ ${started} && !${over} && contains(@{activeCells}, @{index}) }}",
                          "cellValue": "{{ @{isActive} ? ${piece} + 1 : @{item} }}"
                        },
                        "style": {
                          "layout": {
                            "width": 17,
                            "height": 17
                          },
                          "common": {
                            "cornerRadius": 2
                          },
                          "shape": {
                            "fill": "{{ @{palette}[@{cellValue}] }}",
                            "stroke": {
                              "content": "{{ @{isActive} ? {'color': 'white', 'opacity': 0.9} : {'color': 'white', 'opacity': 0.09} }}",
                              "style": {
                                "lineWidth": "{{ @{isActive} ? 1.2 : 0.5 }}"
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "loading_board",
                  "type": "conditional",
                  "context": {
                    "_condition": "${loading}",
                    "_trueComponent": {
                      "id": "board_spinner",
                      "type": "progress",
                      "style": {
                        "common": {
                          "tint": {
                            "color": "white"
                          }
                        }
                      }
                    }
                  }
                },
                {
                  "id": "ready_overlay",
                  "type": "conditional",
                  "context": {
                    "_condition": "{{ !${started} && !${loading} }}",
                    "_trueComponent": {
                      "id": "ready_panel",
                      "type": "vstack",
                      "context": {
                        "_spacing": 10
                      },
                      "style": {
                        "layout": {
                          "padding": 18
                        },
                        "common": {
                          "background": {
                            "color": "#172D49"
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 18
                        }
                      },
                      "children": [
                        {
                          "id": "ready_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://square.grid.3x3.fill"
                          },
                          "style": {
                            "layout": {
                              "width": 32,
                              "height": 32
                            },
                            "common": {
                              "foreground": {
                                "color": "#82E4E8"
                              }
                            }
                          }
                        },
                        {
                          "id": "ready_text",
                          "type": "text",
                          "context": {
                            "_text": "Make room.\nKeep falling."
                          },
                          "style": {
                            "common": {
                              "font": {
                                "kind": "headline",
                                "weight": "bold"
                              },
                              "foreground": {
                                "color": "white"
                              }
                            },
                            "text": {
                              "multilineTextAlignment": "center"
                            }
                          }
                        },
                        {
                          "id": "ready_hint",
                          "type": "text",
                          "context": {
                            "_text": "Tap Start below"
                          },
                          "style": {
                            "common": {
                              "font": "caption",
                              "foreground": {
                                "color": "white"
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            },
            {
              "id": "side_rail",
              "type": "vstack",
              "context": {
                "_spacing": 16,
                "_alignment": "leading"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "height": 378
                }
              },
              "children": [
                {
                  "id": "next_heading",
                  "type": "text",
                  "context": {
                    "_text": "NEXT"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "caption2",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "#ACBDD3"
                      }
                    }
                  }
                },
                {
                  "id": "next_preview",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 2
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "fixed",
                          "size": 12,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 12,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 12,
                          "spacing": 2
                        },
                        {
                          "kind": "fixed",
                          "size": 12,
                          "spacing": 2
                        }
                      ]
                    },
                    "layout": {
                      "width": 54,
                      "height": 54
                    }
                  },
                  "children": [
                    {
                      "id": "preview_cells",
                      "type": "dynamic",
                      "context": {
                        "_data": "{{ range(16) }}"
                      },
                      "prototype": {
                        "id": "preview_cell",
                        "type": "roundedrectangle",
                        "context": {
                          "filled": "{{ ${started} && contains(@{shapes}[${next}][0], @{item}) }}"
                        },
                        "style": {
                          "layout": {
                            "width": 12,
                            "height": 12
                          },
                          "common": {
                            "cornerRadius": 2
                          },
                          "shape": {
                            "fill": "{{ @{filled} ? @{palette}[${next} + 1] : '#1C304A' }}"
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "next_name",
                  "type": "text",
                  "context": {
                    "_text": "{{ ${started} ? @{pieceNames}[${next}] + ' piece' : '—' }}"
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "rail_spacer",
                  "type": "spacer"
                },
                {
                  "id": "best_icon",
                  "type": "image",
                  "context": {
                    "_source": "sf://trophy"
                  },
                  "style": {
                    "layout": {
                      "width": 20,
                      "height": 20
                    },
                    "common": {
                      "foreground": {
                        "color": "#FFD45C"
                      }
                    }
                  }
                },
                {
                  "id": "best_cluster",
                  "type": "vstack",
                  "context": {
                    "_spacing": 4,
                    "_alignment": "leading"
                  },
                  "children": [
                    {
                      "id": "best_heading",
                      "type": "text",
                      "context": {
                        "_text": "BEST"
                      },
                      "style": {
                        "common": {
                          "font": "caption2",
                          "foreground": {
                            "color": "#ACBDD3"
                          }
                        }
                      }
                    },
                    {
                      "id": "best_value",
                      "type": "text",
                      "context": {
                        "_text": "@{bestNow}",
                        "_format": {
                          "number": {
                            "style": "decimal",
                            "maximumFractionDigits": 0
                          }
                        }
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "headline",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "white"
                          }
                        },
                        "text": {
                          "lineLimit": 1,
                          "minimumScaleFactor": 0.5
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "status_chip",
                  "type": "text",
                  "context": {
                    "_text": "@{status}"
                  },
                  "style": {
                    "layout": {
                      "padding": {
                        "vertical": 8,
                        "horizontal": 6
                      },
                      "maxWidth": "infinity"
                    },
                    "common": {
                      "background": "{{ ${over} ? '#633048' : '#264764' }}",
                      "foreground": {
                        "color": "white"
                      },
                      "cornerRadius": 9,
                      "font": {
                        "kind": "caption",
                        "weight": "semibold"
                      }
                    },
                    "animation": {
                      "kind": "spring",
                      "value": "@{status}",
                      "duration": 0.25
                    }
                  }
                }
              ]
            }
          ]
        },
        {
          "id": "controls",
          "type": "vstack",
          "context": {
            "_spacing": 10
          },
          "style": {
            "layout": {
              "maxWidth": "infinity"
            }
          },
          "children": [
            {
              "id": "movement_pad",
              "type": "hstack",
              "context": {
                "_spacing": 8
              },
              "children": [
                {
                  "id": "movement_buttons",
                  "type": "dynamic",
                  "context": {
                    "_data": [
                      {
                        "name": "Left",
                        "symbol": "sf://arrow.left",
                        "dx": -1,
                        "dy": 0,
                        "dr": 0,
                        "mode": "move"
                      },
                      {
                        "name": "Rotate",
                        "symbol": "sf://arrow.clockwise",
                        "dx": 0,
                        "dy": 0,
                        "dr": 1,
                        "mode": "rotate"
                      },
                      {
                        "name": "Right",
                        "symbol": "sf://arrow.right",
                        "dx": 1,
                        "dy": 0,
                        "dr": 0,
                        "mode": "move"
                      },
                      {
                        "name": "Down",
                        "symbol": "sf://arrow.down",
                        "dx": 0,
                        "dy": 1,
                        "dr": 0,
                        "mode": "soft"
                      }
                    ]
                  },
                  "prototype": {
                    "id": "move_button",
                    "type": "button",
                    "style": {
                      "layout": {
                        "minWidth": 44,
                        "height": 50,
                        "maxWidth": "infinity"
                      },
                      "button": {
                        "standard": {
                          "type": "bordered",
                          "shape": "roundedRectangle"
                        }
                      },
                      "common": {
                        "tint": {
                          "light": "#246476",
                          "dark": "#82D9DC"
                        },
                        "disabled": "{{ !@{playing} }}"
                      }
                    },
                    "children": [
                      {
                        "id": "move_content",
                        "type": "vstack",
                        "context": {
                          "_spacing": 3
                        },
                        "children": [
                          {
                            "id": "move_icon",
                            "type": "image",
                            "context": {
                              "_source": "@{item.symbol}"
                            },
                            "style": {
                              "layout": {
                                "width": 19,
                                "height": 19
                              }
                            }
                          },
                          {
                            "id": "move_label",
                            "type": "text",
                            "context": {
                              "_text": "@{item.name}"
                            },
                            "style": {
                              "common": {
                                "font": {
                                  "kind": "caption2",
                                  "weight": "semibold"
                                }
                              }
                            }
                          }
                        ]
                      }
                    ],
                    "events": {
                      "onTap": [
                        {
                          "condition": {
                            "id": "input_running",
                            "input": "@{playing}",
                            "output": {
                              "success": [
                                {
                                  "state": {
                                    "id": "candidate_move",
                                    "input": {
                                      "nx": "{{ ${x} + @{item.dx} }}",
                                      "ny": "{{ ${y} + @{item.dy} }}",
                                      "nr": "{{ (${rotation} + @{item.dr}) % 4 }}",
                                      "mode": "@{item.mode}",
                                      "tick": "{{ @{item.mode} == 'soft' ? 0 : ${tick} }}"
                                    },
                                    "output": {
                                      "success": [
                                        {
                                          "template": {
                                            "id": "try_control_move",
                                            "input": "attempt_move"
                                          }
                                        },
                                        {
                                          "template": {
                                            "id": "control_touch",
                                            "input": "tap_feedback"
                                          }
                                        }
                                      ]
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            },
            {
              "id": "session_actions",
              "type": "hstack",
              "context": {
                "_spacing": 10
              },
              "children": [
                {
                  "id": "play_pause",
                  "type": "button",
                  "style": {
                    "layout": {
                      "minHeight": 46,
                      "maxWidth": "infinity"
                    },
                    "button": {
                      "standard": {
                        "type": "borderedProminent",
                        "shape": "capsule"
                      }
                    },
                    "common": {
                      "tint": {
                        "light": "#256373",
                        "dark": "#A4E5DF"
                      },
                      "disabled": "${loading}"
                    }
                  },
                  "children": [
                    {
                      "id": "play_pause_content",
                      "type": "hstack",
                      "context": {
                        "_spacing": 6
                      },
                      "children": [
                        {
                          "id": "play_pause_icon",
                          "type": "image",
                          "context": {
                            "_source": "{{ @{playing} ? 'sf://pause.fill' : 'sf://play.fill' }}"
                          },
                          "style": {
                            "layout": {
                              "width": 16,
                              "height": 16
                            }
                          }
                        },
                        {
                          "id": "play_pause_label",
                          "type": "text",
                          "context": {
                            "_text": "{{ switch(${loading}, 'Loading', !${started}, 'Start', ${over}, 'Play again', ${paused}, 'Resume', 'Pause') }}"
                          },
                          "style": {
                            "common": {
                              "font": {
                                "kind": "subheadline",
                                "weight": "bold"
                              }
                            }
                          }
                        }
                      ]
                    }
                  ],
                  "events": {
                    "onTap": [
                      {
                        "condition": {
                          "id": "start_or_toggle",
                          "input": "{{ !${started} || ${over} }}",
                          "output": {
                            "success": [
                              {
                                "template": {
                                  "id": "start_from_button",
                                  "input": "new_game"
                                }
                              }
                            ],
                            "failure": [
                              {
                                "state": {
                                  "id": "toggle_pause",
                                  "input": {
                                    "paused": "{{ !${paused} }}",
                                    "fast": false,
                                    "tick": 0,
                                    "note": ""
                                  },
                                  "output": {
                                    "success": [
                                      {
                                        "template": {
                                          "id": "toggle_gravity",
                                          "input": "sync_gravity"
                                        }
                                      },
                                      {
                                        "template": {
                                          "id": "save_pause",
                                          "input": "persist_game"
                                        }
                                      },
                                      {
                                        "template": {
                                          "id": "pause_touch",
                                          "input": "tap_feedback"
                                        }
                                      }
                                    ]
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "id": "fast_button",
                  "type": "button",
                  "style": {
                    "layout": {
                      "minHeight": 46,
                      "maxWidth": "infinity"
                    },
                    "button": {
                      "standard": {
                        "type": "bordered",
                        "shape": "capsule"
                      }
                    },
                    "common": {
                      "tint": {
                        "light": "#246476",
                        "dark": "#82D9DC"
                      },
                      "disabled": "{{ !@{playing} }}"
                    }
                  },
                  "children": [
                    {
                      "id": "fast_content",
                      "type": "hstack",
                      "context": {
                        "_spacing": 5
                      },
                      "children": [
                        {
                          "id": "fast_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://arrow.down.to.line"
                          },
                          "style": {
                            "layout": {
                              "width": 16,
                              "height": 16
                            }
                          }
                        },
                        {
                          "id": "fast_label",
                          "type": "text",
                          "context": {
                            "_text": "{{ ${fast} ? 'Dropping' : 'Fast drop' }}"
                          },
                          "style": {
                            "common": {
                              "font": {
                                "kind": "subheadline",
                                "weight": "semibold"
                              }
                            }
                          }
                        }
                      ]
                    }
                  ],
                  "events": {
                    "onTap": [
                      {
                        "condition": {
                          "id": "fast_running",
                          "input": "{{ ${started} && !${paused} && !${over} && !${loading} && ${mode} != 'locking' }}",
                          "output": {
                            "success": [
                              {
                                "state": {
                                  "id": "enable_fast",
                                  "input": {
                                    "fast": true,
                                    "tick": 0,
                                    "note": ""
                                  },
                                  "output": {
                                    "success": [
                                      {
                                        "template": {
                                          "id": "fast_gravity",
                                          "input": "sync_gravity"
                                        }
                                      },
                                      {
                                        "template": {
                                          "id": "fast_first_step",
                                          "input": "step_down"
                                        }
                                      },
                                      {
                                        "template": {
                                          "id": "fast_touch",
                                          "input": "tap_feedback"
                                        }
                                      }
                                    ]
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "id": "restart_button",
                  "type": "button",
                  "style": {
                    "layout": {
                      "width": 44,
                      "height": 46
                    },
                    "button": {
                      "standard": {
                        "type": "bordered",
                        "shape": "circle"
                      }
                    },
                    "common": {
                      "disabled": "{{ !${started} || ${loading} }}"
                    },
                    "accessibility": {
                      "label": "Restart game"
                    }
                  },
                  "children": [
                    {
                      "id": "restart_icon",
                      "type": "image",
                      "context": {
                        "_source": "sf://arrow.counterclockwise"
                      },
                      "style": {
                        "layout": {
                          "width": 18,
                          "height": 18
                        }
                      }
                    }
                  ],
                  "events": {
                    "onTap": [
                      {
                        "state": {
                          "id": "ask_restart",
                          "input": {
                            "paused": true,
                            "fast": false,
                            "restartPrompt": true
                          },
                          "output": {
                            "success": [
                              {
                                "template": {
                                  "id": "save_before_restart",
                                  "input": "persist_game"
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            },
            {
              "id": "control_hint",
              "type": "text",
              "context": {
                "_text": "{{ switch(${loading}, 'Restoring your game…', !${started}, 'Start to play · Fill a horizontal row to clear it.', ${over}, 'Final score above · Play again for a new board.', ${paused}, 'Game paused · Resume to use the controls.', ${mode} == 'locking', 'Locking piece…', notEmpty(${note}), ${note}, 'Down: one row · Fast drop: fall faster until landing') }}"
              },
              "style": {
                "layout": {
                  "minHeight": 28,
                  "maxWidth": "infinity"
                },
                "common": {
                  "font": "caption",
                  "foreground": {
                    "color": "secondary"
                  }
                },
                "text": {
                  "multilineTextAlignment": "center",
                  "lineLimit": 2
                }
              }
            }
          ]
        },
        {
          "id": "storage_feedback",
          "type": "vstack",
          "context": {
            "_spacing": 6
          },
          "children": [
            {
              "id": "storage_error",
              "type": "conditional",
              "context": {
                "_condition": "${saveError}",
                "_trueComponent": {
                  "id": "retry_storage",
                  "type": "button",
                  "style": {
                    "layout": {
                      "minHeight": 44,
                      "maxWidth": "infinity"
                    },
                    "button": {
                      "standard": {
                        "type": "bordered",
                        "shape": "capsule"
                      }
                    }
                  },
                  "children": [
                    {
                      "id": "storage_error_row",
                      "type": "hstack",
                      "context": {
                        "_spacing": 8
                      },
                      "children": [
                        {
                          "id": "storage_warning_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://exclamationmark.triangle"
                          },
                          "style": {
                            "layout": {
                              "width": 18,
                              "height": 18
                            }
                          }
                        },
                        {
                          "id": "storage_error_text",
                          "type": "text",
                          "context": {
                            "_text": "Game not saved · Retry"
                          },
                          "style": {
                            "common": {
                              "font": "caption"
                            }
                          }
                        }
                      ]
                    }
                  ],
                  "events": {
                    "onTap": [
                      {
                        "template": {
                          "id": "retry_save",
                          "input": "persist_game"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ],
  "events": {
    "onAppear": [
      {
        "template": {
          "id": "open_load",
          "input": "load_game"
        }
      },
      {
        "interval": {
          "id": "gravity_clock",
          "input": {
            "delay": 0.1
          },
          "output": {
            "success": [
              {
                "condition": {
                  "id": "gravity_running",
                  "input": "{{ ${started} && !${paused} && !${over} && !${loading} && ${mode} != 'locking' }}",
                  "output": {
                    "success": [
                      {
                        "state": {
                          "id": "count_tick",
                          "input": {
                            "tick": "{{ ${tick} + 1 }}"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    ],
    "onDisappear": [
      {
        "interval": {
          "id": "gravity_clock",
          "input": {
            "cancel": true
          }
        }
      },
      {
        "state": {
          "id": "leave_paused",
          "input": {
            "paused": true
          },
          "output": {
            "success": [
              {
                "condition": {
                  "id": "save_if_loaded",
                  "input": "{{ !${loading} }}",
                  "output": {
                    "success": [
                      {
                        "template": {
                          "id": "leave_save",
                          "input": "persist_game"
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    ],
    "onChange": {
      "observed": "tick",
      "actions": [
        {
          "condition": {
            "id": "gravity_due",
            "input": "{{ ${started} && !${paused} && !${over} && !${loading} && ${mode} != 'locking' && ${tick} > 0 && (${fast} || ${tick} >= @{speedTicks}) }}",
            "output": {
              "success": [
                {
                  "template": {
                    "id": "gravity_step",
                    "input": "step_down"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}