{
  "id": "mod_twenty48",
  "type": "module",
  "version": "1.1",
  "context": {
    "layout": "phone",
    "title": "2048",
    "icon": "🔢",
    "highest": "{{ max(${board}) }}",
    "won": "{{ @{highest} >= 2048 }}",
    "neighbors": "{{ map(range(16), {'ok': ((@{item} % 4 < 3 ? ${board}[@{item}] == ${board}[@{item} + 1] : false) || (@{item} < 12 ? ${board}[@{item}] == ${board}[@{item} + 4] : false))}) }}",
    "gameOver": "{{ !contains(${board}, 0) && count(@{neighbors[ok == true]}) == 0 }}",
    "paused": "{{ @{won} && !${continued} }}",
    "blocked": "{{ ${loading} || ${busy} || @{paused} || @{gameOver} }}",
    "candidateCells": "{{ map(${candidate}, {'i': @{index}, 'v': @{item}}) }}",
    "holes": "@{candidateCells[v == 0]}",
    "gain": "{{ sum(${merged}, 'gain') }}",
    "emptyIndices": "{{ map(@{holes}, @{item.i}) }}"
  },
  "state": {
    "board": [
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    "score": 0,
    "best": 0,
    "continued": false,
    "direction": "left",
    "work": [],
    "lines": [],
    "packed": [],
    "pairs": [],
    "merged": [],
    "candidate": [],
    "busy": false,
    "loading": true,
    "confirmNew": false,
    "saveError": "",
    "note": "",
    "seed": 0,
    "offset": 1
  },
  "dependencies": [
    {
      "repository": {
        "local": {
          "id": "twenty48_game_store",
          "config": {}
        }
      }
    },
    {
      "service": {
        "audio": {
          "id": "tile_audio"
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "new_game",
          "input": {
            "seed": "{{ random(0,15) }}",
            "offset": "{{ random(1,15) }}",
            "busy": true,
            "confirmNew": false
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "seed_start",
                  "input": "seed_board"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "seed_board",
          "input": {
            "board": "{{ map(range(16), (@{item} == ${seed} || @{item} == (${seed} + ${offset}) % 16) ? (random() < 0.9 ? 2 : 4) : 0) }}",
            "score": 0,
            "continued": false,
            "busy": false,
            "loading": false,
            "note": "Tap an arrow to slide every tile.",
            "saveError": ""
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "save_new",
                  "input": "save_game"
                }
              },
              {
                "service": {
                  "id": "new_haptic",
                  "input": {
                    "serviceId": "tile_audio",
                    "params": {
                      "haptic": "light"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "repo": {
          "id": "save_game",
          "input": {
            "repositoryId": "twenty48_game_store",
            "operation": "create",
            "params": {
              "collection": "game",
              "documentId": "session",
              "rewrite": true,
              "body": {
                "board": "${board}",
                "score": "${score}",
                "best": "${best}",
                "continued": "${continued}"
              }
            }
          },
          "output": {
            "success": [
              {
                "state": {
                  "id": "save_ok",
                  "input": {
                    "saveError": ""
                  }
                }
              }
            ],
            "failure": [
              {
                "state": {
                  "id": "save_failed",
                  "input": {
                    "saveError": "Game is playable, but progress could not be saved."
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_orient",
          "input": {
            "work": "{{ map(range(16), ${board}[switch(${direction} == 'left', @{item}, ${direction} == 'right', cast(@{item}/4,'int')*4+3-@{item}%4, ${direction} == 'up', (@{item}%4)*4+cast(@{item}/4,'int'), (3-@{item}%4)*4+cast(@{item}/4,'int'))]) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_lines",
                  "input": "move_lines"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_lines",
          "input": {
            "lines": "{{ map(range(4), [{'v': ${work}[@{item}*4]}, {'v': ${work}[@{item}*4+1]}, {'v': ${work}[@{item}*4+2]}, {'v': ${work}[@{item}*4+3]}]) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_pack",
                  "input": "move_pack"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_pack",
          "input": {
            "packed": "{{ map(${lines}, map(@{item[v != 0]}, @{item.v}) + [0,0,0,0]) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_pairs",
                  "input": "move_pairs"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_pairs",
          "input": {
            "pairs": "{{ map(${packed}, {'a': @{item}[0], 'b': @{item}[1], 'c': @{item}[2], 'd': @{item}[3], 'ab': (@{item}[0] > 0 && @{item}[0] == @{item}[1]), 'bc': (@{item}[1] > 0 && @{item}[1] == @{item}[2]), 'cd': (@{item}[2] > 0 && @{item}[2] == @{item}[3])}) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_merge",
                  "input": "move_merge"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_merge",
          "input": {
            "merged": "{{ map(${pairs}, {'tiles': switch(@{item.ab}, (@{item.cd} ? [@{item.a}*2,@{item.c}*2,0,0] : [@{item.a}*2,@{item.c},@{item.d},0]), @{item.bc}, [@{item.a},@{item.b}*2,@{item.d},0], @{item.cd}, [@{item.a},@{item.b},@{item.c}*2,0], [@{item.a},@{item.b},@{item.c},@{item.d}]), 'gain': switch(@{item.ab}, @{item.a}*2+(@{item.cd} ? @{item.c}*2 : 0), @{item.bc}, @{item.b}*2, @{item.cd}, @{item.c}*2, 0)}) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_flatten",
                  "input": "move_flatten"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_flatten",
          "input": {
            "work": "{{ ${merged[0].tiles} + ${merged[1].tiles} + ${merged[2].tiles} + ${merged[3].tiles} }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_restore",
                  "input": "move_restore"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "move_restore",
          "input": {
            "candidate": "{{ map(range(16), ${work}[switch(${direction} == 'left', @{item}, ${direction} == 'right', cast(@{item}/4,'int')*4+3-@{item}%4, ${direction} == 'up', (@{item}%4)*4+cast(@{item}/4,'int'), (@{item}%4)*4+3-cast(@{item}/4,'int'))]) }}"
          },
          "output": {
            "success": [
              {
                "template": {
                  "id": "to_commit",
                  "input": "move_commit"
                }
              }
            ]
          }
        }
      }
    },
    {
      "action": {
        "condition": {
          "id": "move_commit",
          "input": "{{ ${candidate} != ${board} }}",
          "output": {
            "success": [
              {
                "state": {
                  "id": "spawn_and_score",
                  "input": {
                    "board": "{{ setAt(${candidate}, @{emptyIndices}[random(0,count(@{emptyIndices})-1)], random() < 0.9 ? 2 : 4) }}",
                    "score": "{{ ${score} + @{gain} }}",
                    "best": "{{ max([${best}, ${score} + @{gain}]) }}",
                    "busy": false,
                    "note": "{{ @{gain} > 0 ? '+' + cast(@{gain},'str') + ' points' : 'Tiles moved' }}"
                  },
                  "output": {
                    "success": [
                      {
                        "template": {
                          "id": "save_move",
                          "input": "save_game"
                        }
                      },
                      {
                        "condition": {
                          "id": "move_feedback",
                          "input": "@{paused}",
                          "output": {
                            "success": [
                              {
                                "service": {
                                  "id": "win_haptic",
                                  "input": {
                                    "serviceId": "tile_audio",
                                    "params": {
                                      "haptic": "success"
                                    }
                                  }
                                }
                              }
                            ],
                            "failure": [
                              {
                                "service": {
                                  "id": "move_haptic",
                                  "input": {
                                    "serviceId": "tile_audio",
                                    "params": {
                                      "haptic": "light"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              }
            ],
            "failure": [
              {
                "state": {
                  "id": "no_move",
                  "input": {
                    "busy": false,
                    "note": "No tiles moved. Try another direction."
                  }
                }
              }
            ]
          }
        }
      }
    }
  ],
  "children": [
    {
      "id": "play_scroll",
      "type": "scroll",
      "style": {
        "layout": {
          "maxWidth": "infinity",
          "maxHeight": "infinity"
        },
        "common": {
          "background": {
            "light": "#F6F2EB",
            "dark": "#151A21"
          },
          "foreground": {
            "color": "primary"
          }
        }
      },
      "children": [
        {
          "id": "play_stack",
          "type": "vstack",
          "context": {
            "_spacing": 20
          },
          "style": {
            "layout": {
              "padding": {
                "horizontal": 20,
                "top": 12,
                "bottom": 24
              },
              "maxWidth": "infinity"
            }
          },
          "children": [
            {
              "id": "score_strip",
              "type": "hstack",
              "context": {
                "_spacing": 18,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 18
                },
                "common": {
                  "background": {
                    "gradient": {
                      "linear": {
                        "colors": [
                          "#633923",
                          "#925225"
                        ],
                        "startPoint": "topLeading",
                        "endPoint": "bottomTrailing"
                      }
                    }
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 22
                },
                "shadow": {
                  "color": "#000000",
                  "radius": 10,
                  "x": 0,
                  "y": 4
                }
              },
              "children": [
                {
                  "id": "score_group",
                  "type": "vstack",
                  "context": {
                    "_spacing": 4,
                    "_alignment": "leading"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity",
                      "alignment": "leading"
                    }
                  },
                  "children": [
                    {
                      "id": "score_caption",
                      "type": "label",
                      "context": {
                        "_title": "SCORE",
                        "_image": "sf://bolt.fill"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "caption",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "#FFE0B5"
                          }
                        }
                      }
                    },
                    {
                      "id": "score_value",
                      "type": "text",
                      "context": {
                        "_text": "${score}",
                        "_format": {
                          "number": {
                            "style": "decimal",
                            "maximumFractionDigits": 0
                          }
                        }
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "largeTitle",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "white"
                          }
                        },
                        "animation": {
                          "kind": "spring",
                          "value": "${score}",
                          "duration": 0.25
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "best_group",
                  "type": "vstack",
                  "context": {
                    "_spacing": 4,
                    "_alignment": "trailing"
                  },
                  "children": [
                    {
                      "id": "best_caption",
                      "type": "label",
                      "context": {
                        "_title": "BEST",
                        "_image": "sf://trophy.fill"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "caption",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "#FFE0B5"
                          }
                        }
                      }
                    },
                    {
                      "id": "best_value",
                      "type": "text",
                      "context": {
                        "_text": "${best}",
                        "_format": {
                          "number": {
                            "style": "decimal",
                            "maximumFractionDigits": 0
                          }
                        }
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "title2",
                            "weight": "bold"
                          },
                          "foreground": {
                            "color": "white"
                          }
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "board_card",
              "type": "vstack",
              "context": {
                "_spacing": 12,
                "_alignment": "leading"
              },
              "style": {
                "layout": {
                  "padding": 18,
                  "maxWidth": "infinity"
                },
                "common": {
                  "background": {
                    "gradient": {
                      "linear": {
                        "colors": [
                          "#283340",
                          "#1B232D"
                        ],
                        "startPoint": "topLeading",
                        "endPoint": "bottomTrailing"
                      }
                    }
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 24
                },
                "shadow": {
                  "color": "#000000",
                  "radius": 12,
                  "x": 0,
                  "y": 6
                }
              },
              "children": [
                {
                  "id": "board_heading",
                  "type": "hstack",
                  "context": {
                    "_spacing": 8
                  },
                  "children": [
                    {
                      "id": "board_status_icon",
                      "type": "image",
                      "context": {
                        "_source": "{{ switch(${loading}, 'sf://hourglass', @{paused}, 'sf://trophy.fill', @{gameOver}, 'sf://flag.checkered', 'sf://square.grid.2x2.fill') }}"
                      },
                      "style": {
                        "layout": {
                          "width": 18,
                          "height": 18
                        },
                        "common": {
                          "foreground": {
                            "color": "#FFD890"
                          }
                        }
                      }
                    },
                    {
                      "id": "board_status",
                      "type": "text",
                      "context": {
                        "_text": "{{ switch(${loading}, 'Restoring game…', @{paused}, '2048 reached!', @{gameOver}, 'No moves remain', ${continued}, 'Beyond 2048', 'Make the 2048 tile') }}"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "headline",
                            "weight": "semibold"
                          },
                          "foreground": {
                            "color": "white"
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "tile_grid",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 7
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "flexible",
                          "minimum": 44
                        },
                        {
                          "kind": "flexible",
                          "minimum": 44
                        },
                        {
                          "kind": "flexible",
                          "minimum": 44
                        },
                        {
                          "kind": "flexible",
                          "minimum": 44
                        }
                      ]
                    },
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "tiles",
                      "type": "dynamic",
                      "context": {
                        "_data": "${board}"
                      },
                      "prototype": {
                        "id": "tile",
                        "type": "text",
                        "context": {
                          "_text": "{{ @{item} == 0 ? '' : cast(@{item},'str') }}"
                        },
                        "style": {
                          "layout": {
                            "height": 60,
                            "maxWidth": "infinity"
                          },
                          "common": {
                            "font": {
                              "kind": "title2",
                              "weight": "bold"
                            },
                            "cornerRadius": 10,
                            "background": "{{ switch(@{item} == 0, '#35414E', @{item} == 2, '#F0E9DA', @{item} == 4, '#E9D6B0', @{item} == 8, '#F3AF67', @{item} == 16, '#EF8B52', @{item} == 32, '#DA6545', @{item} == 64, '#BF4337', @{item} == 128, '#EDC45B', @{item} == 256, '#E7B540', @{item} == 512, '#DDA42D', @{item} == 1024, '#F6D76D', @{item} == 2048, '#FFE99B', '#7653A8') }}",
                            "foreground": "{{ (@{item} == 32 || @{item} == 64 || @{item} > 2048) ? { 'color':'white' } : { 'color':'#2B251E' } }}"
                          },
                          "text": {
                            "lineLimit": 1,
                            "minimumScaleFactor": 0.55
                          },
                          "animation": {
                            "kind": "spring",
                            "value": "@{item}",
                            "duration": 0.22,
                            "bounce": 0.15
                          },
                          "accessibility": {
                            "label": "{{ @{item} == 0 ? 'Empty cell' : cast(@{item},'str') }}"
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "board_hint",
                  "type": "text",
                  "context": {
                    "_text": "{{ switch(${loading}, 'Your last board will appear here.', @{paused}, 'You made it. Keep playing for a higher score.', @{gameOver}, 'Start a new game to try again. Your best stays.', notEmpty(${note}), ${note}, 'Equal tiles merge. Each merge adds to your score.') }}"
                  },
                  "style": {
                    "layout": {
                      "minHeight": 32
                    },
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "#D0D9E3"
                      }
                    },
                    "animation": {
                      "kind": "easeOut",
                      "value": "${note}",
                      "duration": 0.15
                    }
                  }
                }
              ]
            },
            {
              "id": "controls_card",
              "type": "vstack",
              "context": {
                "_spacing": 12
              },
              "style": {
                "layout": {
                  "padding": 18,
                  "maxWidth": "infinity"
                },
                "common": {
                  "background": {
                    "color": "surface"
                  },
                  "foreground": {
                    "color": "onSurface"
                  },
                  "cornerRadius": 22
                },
                "shadow": {
                  "color": "#000000",
                  "radius": 8,
                  "x": 0,
                  "y": 3
                },
                "modal": {
                  "alert": {
                    "isPresented": "${confirmNew}",
                    "title": "Start a new game?",
                    "message": "This board and its score will be replaced. Your best score stays.",
                    "actions": [
                      {
                        "id": "confirm_restart",
                        "type": "button",
                        "context": {
                          "_label": "Start new game"
                        },
                        "events": {
                          "onTap": [
                            {
                              "template": {
                                "id": "restart_confirmed",
                                "input": "new_game"
                              }
                            }
                          ]
                        }
                      },
                      {
                        "id": "keep_game",
                        "type": "button",
                        "context": {
                          "_label": "Keep this game"
                        },
                        "events": {
                          "onTap": [
                            {
                              "state": {
                                "id": "dismiss_restart",
                                "input": {
                                  "confirmNew": false
                                }
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              },
              "children": [
                {
                  "id": "direction_row",
                  "type": "hstack",
                  "context": {
                    "_spacing": 8
                  },
                  "children": [
                    {
                      "id": "directions",
                      "type": "dynamic",
                      "context": {
                        "_data": [
                          {
                            "key": "left",
                            "name": "Left",
                            "icon": "sf://arrow.left"
                          },
                          {
                            "key": "up",
                            "name": "Up",
                            "icon": "sf://arrow.up"
                          },
                          {
                            "key": "down",
                            "name": "Down",
                            "icon": "sf://arrow.down"
                          },
                          {
                            "key": "right",
                            "name": "Right",
                            "icon": "sf://arrow.right"
                          }
                        ]
                      },
                      "prototype": {
                        "id": "direction_button",
                        "type": "button",
                        "style": {
                          "layout": {
                            "minHeight": 60,
                            "maxWidth": "infinity"
                          },
                          "button": {
                            "standard": {
                              "type": "bordered",
                              "shape": "roundedRectangle"
                            }
                          },
                          "common": {
                            "disabled": "@{blocked}",
                            "tint": {
                              "light": "#805025",
                              "dark": "#F2BE71"
                            }
                          }
                        },
                        "children": [
                          {
                            "id": "direction_content",
                            "type": "vstack",
                            "context": {
                              "_spacing": 4
                            },
                            "style": {
                              "layout": {
                                "minHeight": 44,
                                "maxWidth": "infinity"
                              }
                            },
                            "children": [
                              {
                                "id": "direction_icon",
                                "type": "image",
                                "context": {
                                  "_source": "@{item.icon}"
                                },
                                "style": {
                                  "layout": {
                                    "width": 22,
                                    "height": 22
                                  },
                                  "common": {
                                    "foreground": {
                                      "color": "onSurface"
                                    }
                                  }
                                }
                              },
                              {
                                "id": "direction_name",
                                "type": "text",
                                "context": {
                                  "_text": "@{item.name}"
                                },
                                "style": {
                                  "common": {
                                    "font": "caption2",
                                    "foreground": {
                                      "color": "onSurface"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        ],
                        "events": {
                          "onTap": [
                            {
                              "condition": {
                                "id": "move_allowed",
                                "input": "{{ !@{blocked} }}",
                                "output": {
                                  "success": [
                                    {
                                      "state": {
                                        "id": "request_move",
                                        "input": {
                                          "direction": "@{item.key}",
                                          "busy": true
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "template": {
                                                "id": "run_move",
                                                "input": "move_orient"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "continue_gate",
                  "type": "conditional",
                  "context": {
                    "_condition": "@{paused}",
                    "_trueComponent": {
                      "id": "continue_button",
                      "type": "button",
                      "style": {
                        "layout": {
                          "minHeight": 44,
                          "maxWidth": "infinity"
                        },
                        "button": {
                          "standard": {
                            "type": "borderedProminent",
                            "shape": "capsule"
                          }
                        },
                        "common": {
                          "tint": {
                            "light": "#805025",
                            "dark": "#805025"
                          }
                        }
                      },
                      "children": [
                        {
                          "id": "continue_label",
                          "type": "label",
                          "context": {
                            "_title": "Keep playing",
                            "_image": "sf://play.fill"
                          },
                          "style": {
                            "common": {
                              "foreground": {
                                "color": "white"
                              }
                            }
                          }
                        }
                      ],
                      "events": {
                        "onTap": [
                          {
                            "state": {
                              "id": "continue_game",
                              "input": {
                                "continued": true,
                                "note": "Keep merging. Your score keeps growing."
                              },
                              "output": {
                                "success": [
                                  {
                                    "template": {
                                      "id": "save_continue",
                                      "input": "save_game"
                                    }
                                  },
                                  {
                                    "service": {
                                      "id": "continue_haptic",
                                      "input": {
                                        "serviceId": "tile_audio",
                                        "params": {
                                          "haptic": "light"
                                        }
                                      }
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "id": "new_button",
                  "type": "button",
                  "style": {
                    "layout": {
                      "maxWidth": "infinity",
                      "minHeight": 44
                    },
                    "button": {
                      "standard": {
                        "type": "bordered",
                        "shape": "capsule"
                      }
                    },
                    "common": {
                      "disabled": "{{ ${loading} || ${busy} }}",
                      "tint": {
                        "light": "#805025",
                        "dark": "#F2BE71"
                      }
                    }
                  },
                  "children": [
                    {
                      "id": "new_label",
                      "type": "label",
                      "context": {
                        "_title": "New game",
                        "_image": "sf://arrow.clockwise"
                      },
                      "style": {
                        "common": {
                          "foreground": {
                            "color": "onSurface"
                          }
                        }
                      }
                    }
                  ],
                  "events": {
                    "onTap": [
                      {
                        "state": {
                          "id": "ask_restart",
                          "input": {
                            "confirmNew": true
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "id": "controls_hint",
                  "type": "text",
                  "context": {
                    "_text": "{{ switch(${loading}, 'Controls are ready when the game loads.', ${busy}, 'Moving tiles…', @{paused}, 'Choose Keep playing to continue.', @{gameOver}, 'There are no available moves. Choose New game.', 'Use the arrows to slide. Equal tiles combine once per move.') }}"
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "secondary"
                      }
                    },
                    "text": {
                      "multilineTextAlignment": "center"
                    }
                  }
                }
              ]
            },
            {
              "id": "save_feedback",
              "type": "vstack",
              "context": {
                "_spacing": 8
              },
              "children": [
                {
                  "id": "save_error_gate",
                  "type": "conditional",
                  "context": {
                    "_condition": "{{ notEmpty(${saveError}) }}",
                    "_trueComponent": {
                      "id": "save_error_card",
                      "type": "vstack",
                      "context": {
                        "_spacing": 10
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": 18
                        },
                        "common": {
                          "background": {
                            "color": "surface"
                          },
                          "foreground": {
                            "color": "onSurface"
                          },
                          "cornerRadius": 18
                        },
                        "shadow": {
                          "color": "#000000",
                          "radius": 6,
                          "x": 0,
                          "y": 2
                        }
                      },
                      "children": [
                        {
                          "id": "save_error_label",
                          "type": "label",
                          "context": {
                            "_title": "${saveError}",
                            "_image": "sf://exclamationmark.triangle"
                          },
                          "style": {
                            "common": {
                              "font": "caption",
                              "foreground": {
                                "color": "onSurface"
                              }
                            }
                          }
                        },
                        {
                          "id": "retry_save",
                          "type": "button",
                          "style": {
                            "layout": {
                              "minHeight": 44
                            },
                            "button": {
                              "standard": {
                                "type": "bordered",
                                "shape": "capsule"
                              }
                            }
                          },
                          "children": [
                            {
                              "id": "retry_label",
                              "type": "label",
                              "context": {
                                "_title": "Retry save",
                                "_image": "sf://arrow.clockwise"
                              },
                              "style": {
                                "common": {
                                  "foreground": {
                                    "color": "onSurface"
                                  }
                                }
                              }
                            }
                          ],
                          "events": {
                            "onTap": [
                              {
                                "template": {
                                  "id": "retry_persist",
                                  "input": "save_game"
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                },
                {
                  "id": "autosave_caption",
                  "type": "label",
                  "context": {
                    "_title": "{{ notEmpty(${saveError}) ? 'Progress is not saved yet' : 'Auto-saved on this device' }}",
                    "_image": "sf://internaldrive"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "events": {
    "onAppear": [
      {
        "repo": {
          "id": "load_session",
          "input": {
            "repositoryId": "twenty48_game_store",
            "operation": "read",
            "params": {
              "collection": "game",
              "documentId": "session"
            }
          },
          "output": {
            "success": [
              {
                "condition": {
                  "id": "valid_session",
                  "input": "{{ count(@{load_session.board}) == 16 }}",
                  "output": {
                    "success": [
                      {
                        "state": {
                          "id": "restore_session",
                          "input": {
                            "board": "@{load_session.board}",
                            "score": "@{load_session.score}",
                            "best": "@{load_session.best}",
                            "continued": "@{load_session.continued}",
                            "loading": false,
                            "busy": false
                          }
                        }
                      }
                    ],
                    "failure": [
                      {
                        "template": {
                          "id": "init_bad_session",
                          "input": "new_game"
                        }
                      }
                    ]
                  }
                }
              }
            ],
            "failure": [
              {
                "template": {
                  "id": "init_first_game",
                  "input": "new_game"
                }
              }
            ]
          }
        }
      }
    ]
  }
}