{
  "id": "candy_blocks",
  "type": "module",
  "version": "1.1",
  "context": {
    "layout": "phone",
    "title": "Candy Blocks",
    "icon": "🍬"
  },
  "state": {
    "board": [
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    "tray": [
      0,
      1,
      2
    ],
    "used": [
      false,
      false,
      false
    ],
    "selected": -1,
    "score": 0,
    "best": 0,
    "pendingRows": [
      false,
      false,
      false,
      false,
      false,
      false,
      false,
      false
    ],
    "pendingCols": [
      false,
      false,
      false,
      false,
      false,
      false,
      false,
      false
    ],
    "lastShape": 0,
    "lastClearCount": 0,
    "gameOver": false,
    "status": "Pick a candy block, then tap the board.",
    "lastAnchor": 0
  },
  "dependencies": [
    {
      "repository": {
        "local": {
          "id": "block_bonbon_store",
          "config": {}
        }
      }
    },
    {
      "service": {
        "audio": {
          "id": "bb_audio"
        }
      }
    }
  ],
  "events": {
    "onAppear": [
      {
        "repo": {
          "id": "bb_load_best",
          "input": {
            "repositoryId": "block_bonbon_store",
            "operation": "read",
            "params": {
              "collection": "scores",
              "documentId": "best"
            }
          },
          "output": {
            "success": [
              {
                "state": {
                  "id": "bb_hydrate_best",
                  "input": {
                    "best": "{{ @{bb_load_best.best} }}"
                  }
                }
              }
            ]
          }
        }
      },
      {
        "state": {
          "id": "bb_seed_open",
          "input": {
            "board": "{{ map(range(64), 0) }}",
            "tray": "{{ [random(0,3), random(0,3), random(0,3)] }}",
            "used": [
              false,
              false,
              false
            ],
            "selected": -1,
            "score": 0,
            "pendingRows": [
              false,
              false,
              false,
              false,
              false,
              false,
              false,
              false
            ],
            "pendingCols": [
              false,
              false,
              false,
              false,
              false,
              false,
              false,
              false
            ],
            "lastClearCount": 0,
            "gameOver": false,
            "status": "Pick a candy block, then tap the board."
          }
        }
      }
    ],
    "onChange": {
      "observed": "best",
      "actions": [
        {
          "condition": {
            "id": "bb_best_persist_guard",
            "input": "{{ ${best} > 0 }}",
            "output": {
              "success": [
                {
                  "repo": {
                    "id": "bb_persist_best_on_change",
                    "input": {
                      "repositoryId": "block_bonbon_store",
                      "operation": "create",
                      "params": {
                        "collection": "scores",
                        "documentId": "best",
                        "rewrite": true,
                        "body": {
                          "best": "${best}"
                        }
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      ]
    }
  },
  "children": [
    {
      "id": "bb_root",
      "type": "zstack",
      "style": {
        "layout": {
          "maxWidth": "infinity",
          "maxHeight": "infinity"
        },
        "common": {
          "background": {
            "gradient": {
              "linear": {
                "colors": [
                  "#06162E",
                  "#081B3D",
                  "#11143B"
                ],
                "startPoint": "topLeading",
                "endPoint": "bottomTrailing"
              }
            }
          },
          "foreground": {
            "color": "white"
          }
        }
      },
      "children": [
        {
          "id": "bb_main",
          "type": "vstack",
          "context": {
            "_spacing": 18,
            "_alignment": "center"
          },
          "style": {
            "layout": {
              "maxWidth": "infinity",
              "maxHeight": "infinity",
              "padding": {
                "top": 14,
                "bottom": 20,
                "leading": 14,
                "trailing": 14
              }
            }
          },
          "children": [
            {
              "id": "bb_stats",
              "type": "vstack",
              "context": {
                "_spacing": 8,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity"
                }
              },
              "children": [
                {
                  "id": "bb_score_row",
                  "type": "hstack",
                  "context": {
                    "_spacing": 10,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "bb_score_chip",
                      "type": "hstack",
                      "context": {
                        "_spacing": 8,
                        "_alignment": "center"
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": {
                            "top": 10,
                            "bottom": 10,
                            "leading": 14,
                            "trailing": 14
                          }
                        },
                        "common": {
                          "background": {
                            "color": "#132B54",
                            "opacity": 0.86
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 18
                        },
                        "shadow": {
                          "color": {
                            "color": "black",
                            "opacity": 0.25
                          },
                          "radius": 8,
                          "y": 4
                        }
                      },
                      "children": [
                        {
                          "id": "bb_score_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://sparkles"
                          },
                          "style": {
                            "layout": {
                              "width": 20,
                              "height": 20
                            },
                            "common": {
                              "foreground": {
                                "color": "#FFD84D"
                              }
                            }
                          }
                        },
                        {
                          "id": "bb_score_texts",
                          "type": "vstack",
                          "context": {
                            "_spacing": 0,
                            "_alignment": "leading"
                          },
                          "children": [
                            {
                              "id": "bb_score_label",
                              "type": "text",
                              "context": {
                                "_text": "Score"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#B8C7E6"
                                  }
                                }
                              }
                            },
                            {
                              "id": "bb_score_value",
                              "type": "text",
                              "context": {
                                "_text": "${score}",
                                "_format": {
                                  "number": {
                                    "style": "decimal",
                                    "maximumFractionDigits": 0
                                  }
                                }
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "title2",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                }
                              }
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "id": "bb_best_chip",
                      "type": "hstack",
                      "context": {
                        "_spacing": 8,
                        "_alignment": "center"
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": {
                            "top": 10,
                            "bottom": 10,
                            "leading": 14,
                            "trailing": 14
                          }
                        },
                        "common": {
                          "background": {
                            "color": "#2A245F",
                            "opacity": 0.82
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 18
                        },
                        "shadow": {
                          "color": {
                            "color": "black",
                            "opacity": 0.2
                          },
                          "radius": 8,
                          "y": 4
                        }
                      },
                      "children": [
                        {
                          "id": "bb_best_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://crown.fill"
                          },
                          "style": {
                            "layout": {
                              "width": 20,
                              "height": 20
                            },
                            "common": {
                              "foreground": {
                                "color": "#FF9F0A"
                              }
                            }
                          }
                        },
                        {
                          "id": "bb_best_texts",
                          "type": "vstack",
                          "context": {
                            "_spacing": 0,
                            "_alignment": "leading"
                          },
                          "children": [
                            {
                              "id": "bb_best_label",
                              "type": "text",
                              "context": {
                                "_text": "Best"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#B8C7E6"
                                  }
                                }
                              }
                            },
                            {
                              "id": "bb_best_value",
                              "type": "text",
                              "context": {
                                "_text": "${best}",
                                "_format": {
                                  "number": {
                                    "style": "decimal",
                                    "maximumFractionDigits": 0
                                  }
                                }
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "title2",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                }
                              }
                            }
                          ]
                        }
                      ]
                    }
                  ]
                },
                {
                  "id": "bb_status",
                  "type": "hstack",
                  "context": {
                    "_spacing": 6,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "padding": {
                        "top": 6,
                        "bottom": 6,
                        "leading": 12,
                        "trailing": 12
                      }
                    },
                    "common": {
                      "background": {
                        "color": "#FFFFFF",
                        "opacity": 0.08
                      },
                      "foreground": {
                        "color": "white"
                      },
                      "cornerRadius": 14
                    },
                    "animation": {
                      "kind": "spring",
                      "value": "${status}",
                      "duration": 0.25,
                      "bounce": 0.2
                    }
                  },
                  "children": [
                    {
                      "id": "bb_status_icon",
                      "type": "image",
                      "context": {
                        "_source": "{{ ${gameOver} ? 'sf://flag.checkered' : (${lastClearCount} > 1 ? 'sf://bolt.fill' : 'sf://hand.tap.fill') }}"
                      },
                      "style": {
                        "layout": {
                          "width": 15,
                          "height": 15
                        },
                        "common": {
                          "foreground": {
                            "color": "#64D2FF"
                          }
                        }
                      }
                    },
                    {
                      "id": "bb_status_text",
                      "type": "text",
                      "context": {
                        "_text": "${status}"
                      },
                      "style": {
                        "text": {
                          "multilineTextAlignment": "center"
                        },
                        "common": {
                          "font": "caption",
                          "foreground": {
                            "color": "#DCE8FF"
                          }
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "bb_board_card",
              "type": "vstack",
              "context": {
                "_spacing": 10,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 12
                },
                "common": {
                  "background": {
                    "color": "#071A35",
                    "opacity": 0.92
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 24
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.36
                  },
                  "radius": 14,
                  "y": 8
                }
              },
              "children": [
                {
                  "id": "bb_board_grid",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 4
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        },
                        {
                          "kind": "flexible",
                          "minimum": 34,
                          "maximum": 44,
                          "spacing": 4
                        }
                      ]
                    },
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "bb_cells",
                      "type": "dynamic",
                      "context": {
                        "_data": "${board}"
                      },
                      "prototype": {
                        "id": "bb_cell",
                        "type": "button",
                        "context": {
                          "_label": "",
                          "i": "@{index}",
                          "v": "@{item}",
                          "clearing": "{{ ${pendingRows}[cast(@{index}/8,'int')] || ${pendingCols}[@{index}%8] }}"
                        },
                        "style": {
                          "button": {
                            "custom": {
                              "layout": {
                                "height": 40,
                                "maxWidth": "infinity"
                              },
                              "common": {
                                "background": "{{ @{v} == 0 ? {'color':'#10264D'} : (@{clearing} ? {'color':'#FFFFFF'} : switch(@{v} == 1, {'color':'#FF4FA3'}, @{v} == 2, {'color':'#35D1FF'}, @{v} == 3, {'color':'#FFE15A'}, {'color':'#55F28A'})) }}",
                                "cornerRadius": 9
                              }
                            }
                          },
                          "common": {
                            "opacity": "{{ @{clearing} ? 0.35 : 1 }}",
                            "scale": "{{ @{clearing} ? 0.72 : (@{v} == 0 ? 0.96 : 1) }}"
                          },
                          "shadow": {
                            "color": {
                              "color": "black",
                              "opacity": 0.18
                            },
                            "radius": 3,
                            "y": 2
                          },
                          "animation": {
                            "kind": "spring",
                            "value": "{{ @{v} + (${pendingRows}[cast(@{index}/8,'int')] ? 10 : 0) + (${pendingCols}[@{index}%8] ? 20 : 0) }}",
                            "duration": 0.32,
                            "bounce": 0.42
                          }
                        },
                        "events": {
                          "onTap": [
                            {
                              "condition": {
                                "id": "bb_can_place",
                                "input": "{{ !${gameOver} && ${selected} >= 0 && !${used}[${selected}] && switch(${tray}[${selected}] == 0, ${board}[@{i}] == 0, ${tray}[${selected}] == 1, (@{i}%8) <= 6 && ${board}[@{i}] == 0 && ${board}[@{i}+1] == 0, ${tray}[${selected}] == 2, @{i} < 56 && ${board}[@{i}] == 0 && ${board}[@{i}+8] == 0, ${tray}[${selected}] == 3, (@{i}%8) <= 6 && @{i} < 56 && ${board}[@{i}] == 0 && ${board}[@{i}+1] == 0 && ${board}[@{i}+8] == 0 && ${board}[@{i}+9] == 0, false) }}",
                                "output": {
                                  "success": [
                                    {
                                      "state": {
                                        "id": "bb_accept_place",
                                        "input": {
                                          "lastAnchor": "@{i}",
                                          "lastShape": "{{ ${tray}[${selected}] }}",
                                          "used": "{{ ((${used}[0] ? 1 : 0) + (${used}[1] ? 1 : 0) + (${used}[2] ? 1 : 0) == 2) ? [false,false,false] : setAt(${used}, ${selected}, true) }}",
                                          "tray": "{{ ((${used}[0] ? 1 : 0) + (${used}[1] ? 1 : 0) + (${used}[2] ? 1 : 0) == 2) ? [random(0,3), random(0,3), random(0,3)] : ${tray} }}",
                                          "selected": -1,
                                          "pendingRows": [
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false
                                          ],
                                          "pendingCols": [
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false,
                                            false
                                          ],
                                          "status": "Placed."
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "service": {
                                                "id": "bb_place_haptic",
                                                "input": {
                                                  "serviceId": "bb_audio",
                                                  "params": {
                                                    "haptic": "light",
                                                    "sound": "keyPress"
                                                  }
                                                }
                                              }
                                            },
                                            {
                                              "interval": {
                                                "id": "bb_place_tick",
                                                "input": {
                                                  "delay": 0.01
                                                },
                                                "output": {
                                                  "success": [
                                                    {
                                                      "interval": {
                                                        "id": "bb_place_tick",
                                                        "input": {
                                                          "cancel": true
                                                        }
                                                      }
                                                    },
                                                    {
                                                      "state": {
                                                        "id": "bb_apply_place",
                                                        "input": {
                                                          "board": "{{ map(range(64), switch(${lastShape} == 0, @{item} == ${lastAnchor} ? ${lastShape}+1 : ${board}[@{item}], ${lastShape} == 1, (@{item} == ${lastAnchor} || @{item} == ${lastAnchor}+1) ? ${lastShape}+1 : ${board}[@{item}], ${lastShape} == 2, (@{item} == ${lastAnchor} || @{item} == ${lastAnchor}+8) ? ${lastShape}+1 : ${board}[@{item}], ${lastShape} == 3, (@{item} == ${lastAnchor} || @{item} == ${lastAnchor}+1 || @{item} == ${lastAnchor}+8 || @{item} == ${lastAnchor}+9) ? ${lastShape}+1 : ${board}[@{item}], ${board}[@{item}]) ) }}"
                                                        },
                                                        "output": {
                                                          "success": [
                                                            {
                                                              "interval": {
                                                                "id": "bb_mark_tick",
                                                                "input": {
                                                                  "delay": 0.08
                                                                },
                                                                "output": {
                                                                  "success": [
                                                                    {
                                                                      "interval": {
                                                                        "id": "bb_mark_tick",
                                                                        "input": {
                                                                          "cancel": true
                                                                        }
                                                                      }
                                                                    },
                                                                    {
                                                                      "state": {
                                                                        "id": "bb_mark_lines",
                                                                        "input": {
                                                                          "pendingRows": "{{ map(range(8), ${board}[@{item}*8+0] != 0 && ${board}[@{item}*8+1] != 0 && ${board}[@{item}*8+2] != 0 && ${board}[@{item}*8+3] != 0 && ${board}[@{item}*8+4] != 0 && ${board}[@{item}*8+5] != 0 && ${board}[@{item}*8+6] != 0 && ${board}[@{item}*8+7] != 0) }}",
                                                                          "pendingCols": "{{ map(range(8), ${board}[0+@{item}] != 0 && ${board}[8+@{item}] != 0 && ${board}[16+@{item}] != 0 && ${board}[24+@{item}] != 0 && ${board}[32+@{item}] != 0 && ${board}[40+@{item}] != 0 && ${board}[48+@{item}] != 0 && ${board}[56+@{item}] != 0) }}"
                                                                        },
                                                                        "output": {
                                                                          "success": [
                                                                            {
                                                                              "condition": {
                                                                                "id": "bb_clear_feedback",
                                                                                "input": "{{ sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0)) > 0 }}",
                                                                                "output": {
                                                                                  "success": [
                                                                                    {
                                                                                      "service": {
                                                                                        "id": "bb_clear_sound",
                                                                                        "input": {
                                                                                          "serviceId": "bb_audio",
                                                                                          "params": {
                                                                                            "haptic": "success",
                                                                                            "sound": "sentMessage"
                                                                                          }
                                                                                        }
                                                                                      }
                                                                                    }
                                                                                  ]
                                                                                }
                                                                              }
                                                                            },
                                                                            {
                                                                              "interval": {
                                                                                "id": "bb_clear_tick",
                                                                                "input": {
                                                                                  "delay": 0.34
                                                                                },
                                                                                "output": {
                                                                                  "success": [
                                                                                    {
                                                                                      "interval": {
                                                                                        "id": "bb_clear_tick",
                                                                                        "input": {
                                                                                          "cancel": true
                                                                                        }
                                                                                      }
                                                                                    },
                                                                                    {
                                                                                      "state": {
                                                                                        "id": "bb_clear_score",
                                                                                        "input": {
                                                                                          "board": "{{ map(range(64), (${pendingRows}[cast(@{item}/8,'int')] || ${pendingCols}[@{item}%8]) ? 0 : ${board}[@{item}]) }}",
                                                                                          "lastClearCount": "{{ sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0)) }}",
                                                                                          "score": "{{ ${score} + switch(${lastShape} == 0, 1, ${lastShape} == 3, 4, 2) + ((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) * 10) + (((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) > 1) ? (((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) - 1) * 15) : 0) }}",
                                                                                          "best": "{{ max([${best}, (${score} + switch(${lastShape} == 0, 1, ${lastShape} == 3, 4, 2) + ((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) * 10) + (((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) > 1) ? (((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) - 1) * 15) : 0))]) }}",
                                                                                          "pendingRows": [
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false
                                                                                          ],
                                                                                          "pendingCols": [
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false,
                                                                                            false
                                                                                          ],
                                                                                          "status": "{{ (sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) > 1 ? 'Combo clear bonus!' : ((sum(map(${pendingRows}, @{item} ? 1 : 0)) + sum(map(${pendingCols}, @{item} ? 1 : 0))) == 1 ? 'Line cleared.' : 'Choose the next candy block.') }}"
                                                                                        },
                                                                                        "output": {
                                                                                          "success": [
                                                                                            {
                                                                                              "repo": {
                                                                                                "id": "bb_save_best",
                                                                                                "input": {
                                                                                                  "repositoryId": "block_bonbon_store",
                                                                                                  "operation": "create",
                                                                                                  "params": {
                                                                                                    "collection": "scores",
                                                                                                    "documentId": "best",
                                                                                                    "rewrite": true,
                                                                                                    "body": {
                                                                                                      "best": "${best}"
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              }
                                                                                            },
                                                                                            {
                                                                                              "condition": {
                                                                                                "id": "bb_no_fit",
                                                                                                "input": "{{ sum(map(range(64), ((!${used}[0] && switch(${tray}[0] == 0, ${board}[@{item}] == 0, ${tray}[0] == 1, (@{item}%8)<=6 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0, ${tray}[0] == 2, @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+8]==0, ${tray}[0] == 3, (@{item}%8)<=6 && @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0 && ${board}[@{item}+8]==0 && ${board}[@{item}+9]==0, false)) || (!${used}[1] && switch(${tray}[1] == 0, ${board}[@{item}] == 0, ${tray}[1] == 1, (@{item}%8)<=6 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0, ${tray}[1] == 2, @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+8]==0, ${tray}[1] == 3, (@{item}%8)<=6 && @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0 && ${board}[@{item}+8]==0 && ${board}[@{item}+9]==0, false)) || (!${used}[2] && switch(${tray}[2] == 0, ${board}[@{item}] == 0, ${tray}[2] == 1, (@{item}%8)<=6 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0, ${tray}[2] == 2, @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+8]==0, ${tray}[2] == 3, (@{item}%8)<=6 && @{item}<56 && ${board}[@{item}]==0 && ${board}[@{item}+1]==0 && ${board}[@{item}+8]==0 && ${board}[@{item}+9]==0, false))) ? 1 : 0)) == 0 }}",
                                                                                                "output": {
                                                                                                  "success": [
                                                                                                    {
                                                                                                      "state": {
                                                                                                        "id": "bb_set_game_over",
                                                                                                        "input": {
                                                                                                          "gameOver": true,
                                                                                                          "selected": -1,
                                                                                                          "status": "No candy block fits. Compare your score, then play again."
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  ]
                                                                                                }
                                                                                              }
                                                                                            }
                                                                                          ]
                                                                                        }
                                                                                      }
                                                                                    }
                                                                                  ]
                                                                                }
                                                                              }
                                                                            }
                                                                          ]
                                                                        }
                                                                      }
                                                                    }
                                                                  ]
                                                                }
                                                              }
                                                            }
                                                          ]
                                                        }
                                                      }
                                                    }
                                                  ]
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ],
                                  "failure": [
                                    {
                                      "state": {
                                        "id": "bb_bad_place",
                                        "input": {
                                          "status": "{{ ${selected} < 0 ? 'Pick one of the three shapes first.' : 'That shape does not fit there.' }}"
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "service": {
                                                "id": "bb_bad_haptic",
                                                "input": {
                                                  "serviceId": "bb_audio",
                                                  "params": {
                                                    "haptic": "warning",
                                                    "sound": "keyPress"
                                                  }
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "bb_hint_row",
                  "type": "hstack",
                  "context": {
                    "_spacing": 6,
                    "_alignment": "center"
                  },
                  "children": [
                    {
                      "id": "bb_hint_icon",
                      "type": "image",
                      "context": {
                        "_source": "sf://square.grid.3x3.fill"
                      },
                      "style": {
                        "layout": {
                          "width": 14,
                          "height": 14
                        },
                        "common": {
                          "foreground": {
                            "color": "#64D2FF"
                          }
                        }
                      }
                    },
                    {
                      "id": "bb_hint_text",
                      "type": "text",
                      "context": {
                        "_text": "Rows and columns flash before they clear. Combos add bonus points."
                      },
                      "style": {
                        "common": {
                          "font": "caption",
                          "foreground": {
                            "color": "#B8C7E6"
                          }
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "bb_tray_card",
              "type": "vstack",
              "context": {
                "_spacing": 10,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 14
                },
                "common": {
                  "background": {
                    "color": "#0B2247",
                    "opacity": 0.86
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 22
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.28
                  },
                  "radius": 12,
                  "y": 7
                }
              },
              "children": [
                {
                  "id": "bb_tray_label_row",
                  "type": "hstack",
                  "context": {
                    "_spacing": 8,
                    "_alignment": "center"
                  },
                  "children": [
                    {
                      "id": "bb_tray_icon",
                      "type": "image",
                      "context": {
                        "_source": "sf://tray.full.fill"
                      },
                      "style": {
                        "layout": {
                          "width": 18,
                          "height": 18
                        },
                        "common": {
                          "foreground": {
                            "color": "#FF4FA3"
                          }
                        }
                      }
                    },
                    {
                      "id": "bb_tray_label",
                      "type": "text",
                      "context": {
                        "_text": "Tap a shape, then choose its top-left board cell"
                      },
                      "style": {
                        "common": {
                          "font": "caption",
                          "foreground": {
                            "color": "#DCE8FF"
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "bb_tray_row",
                  "type": "hstack",
                  "context": {
                    "_spacing": 10,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "bb_tray_shapes",
                      "type": "dynamic",
                      "context": {
                        "_data": "{{ range(3) }}"
                      },
                      "prototype": {
                        "id": "bb_shape_btn",
                        "type": "button",
                        "context": {
                          "shape": "{{ ${tray}[@{index}] }}",
                          "slot": "@{index}",
                          "gone": "{{ ${used}[@{index}] }}",
                          "picked": "{{ ${selected} == @{index} }}"
                        },
                        "style": {
                          "button": {
                            "custom": {
                              "layout": {
                                "height": 92,
                                "maxWidth": "infinity"
                              },
                              "common": {
                                "background": "{{ @{gone} ? {'color':'#FFFFFF','opacity':0.06} : (@{picked} ? {'color':'#3A7BFF','opacity':0.55} : {'color':'#132B54','opacity':0.95}) }}",
                                "cornerRadius": 18
                              }
                            }
                          },
                          "common": {
                            "opacity": "{{ @{gone} ? 0.32 : 1 }}",
                            "scale": "{{ @{picked} ? 1.06 : 1 }}"
                          },
                          "shadow": {
                            "color": {
                              "color": "#64D2FF",
                              "opacity": 0.2
                            },
                            "radius": 8,
                            "y": 4
                          },
                          "animation": {
                            "kind": "spring",
                            "value": "{{ ${selected} == @{index} }}",
                            "duration": 0.25,
                            "bounce": 0.35
                          }
                        },
                        "children": [
                          {
                            "id": "bb_shape_preview",
                            "type": "grid",
                            "context": {
                              "_gridType": "lazyVGrid",
                              "_spacing": 3
                            },
                            "style": {
                              "grid": {
                                "columns": [
                                  {
                                    "kind": "fixed",
                                    "size": 20,
                                    "spacing": 3
                                  },
                                  {
                                    "kind": "fixed",
                                    "size": 20,
                                    "spacing": 3
                                  },
                                  {
                                    "kind": "fixed",
                                    "size": 20,
                                    "spacing": 3
                                  }
                                ]
                              },
                              "layout": {
                                "maxWidth": "infinity"
                              }
                            },
                            "children": [
                              {
                                "id": "bb_preview_cells",
                                "type": "dynamic",
                                "context": {
                                  "_data": "{{ switch(@{shape} == 0, [0,0,0,0,1,0,0,0,0], @{shape} == 1, [0,0,0,2,2,0,0,0,0], @{shape} == 2, [0,3,0,0,3,0,0,0,0], @{shape} == 3, [4,4,0,4,4,0,0,0,0], [0,0,0,0,0,0,0,0,0]) }}"
                                },
                                "prototype": {
                                  "id": "bb_preview_cell",
                                  "type": "roundedrectangle",
                                  "style": {
                                    "layout": {
                                      "width": 20,
                                      "height": 20
                                    },
                                    "shape": {
                                      "fill": "{{ @{item} == 0 ? {'color':'#0A1B38','opacity':0.35} : switch(@{item} == 1, {'color':'#FF4FA3'}, @{item} == 2, {'color':'#35D1FF'}, @{item} == 3, {'color':'#FFE15A'}, {'color':'#55F28A'}) }}"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        ],
                        "events": {
                          "onTap": [
                            {
                              "condition": {
                                "id": "bb_shape_available",
                                "input": "{{ !${gameOver} && !${used}[@{index}] }}",
                                "output": {
                                  "success": [
                                    {
                                      "state": {
                                        "id": "bb_select_shape",
                                        "input": {
                                          "selected": "@{index}",
                                          "status": "Shape selected. Tap a valid empty spot."
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "service": {
                                                "id": "bb_select_haptic",
                                                "input": {
                                                  "serviceId": "bb_audio",
                                                  "params": {
                                                    "haptic": "light",
                                                    "sound": "keyPress"
                                                  }
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ],
                                  "failure": [
                                    {
                                      "state": {
                                        "id": "bb_select_blocked",
                                        "input": {
                                          "status": "{{ ${gameOver} ? 'Game over. Use Play again to start fresh.' : 'That tray space is already used.' }}"
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "service": {
                                                "id": "bb_select_warn",
                                                "input": {
                                                  "serviceId": "bb_audio",
                                                  "params": {
                                                    "haptic": "warning"
                                                  }
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                },
                {
                  "id": "bb_tray_note",
                  "type": "text",
                  "context": {
                    "_text": "A new trio appears after all three are used."
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "#B8C7E6"
                      }
                    }
                  }
                }
              ]
            }
          ]
        },
        {
          "id": "bb_game_over",
          "type": "conditional",
          "context": {
            "_condition": "${gameOver}",
            "_trueComponent": {
              "id": "bb_game_over_card",
              "type": "vstack",
              "context": {
                "_spacing": 14,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 22
                },
                "common": {
                  "background": {
                    "gradient": {
                      "linear": {
                        "colors": [
                          "#FF4FA3",
                          "#7B61FF",
                          "#35D1FF"
                        ],
                        "startPoint": "topLeading",
                        "endPoint": "bottomTrailing"
                      }
                    }
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 26
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.42
                  },
                  "radius": 18,
                  "y": 10
                },
                "animation": {
                  "kind": "spring",
                  "value": "${gameOver}",
                  "duration": 0.4,
                  "bounce": 0.4
                }
              },
              "children": [
                {
                  "id": "bb_over_icon",
                  "type": "image",
                  "context": {
                    "_source": "sf://flag.checkered.circle.fill"
                  },
                  "style": {
                    "layout": {
                      "width": 44,
                      "height": 44
                    },
                    "common": {
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "bb_over_head",
                  "type": "text",
                  "context": {
                    "_text": "No more fits"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "title",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "bb_over_scores",
                  "type": "hstack",
                  "context": {
                    "_spacing": 12,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "bb_final_score_box",
                      "type": "vstack",
                      "context": {
                        "_spacing": 2,
                        "_alignment": "center"
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": 12
                        },
                        "common": {
                          "background": {
                            "color": "#000000",
                            "opacity": 0.22
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 16
                        }
                      },
                      "children": [
                        {
                          "id": "bb_final_label",
                          "type": "text",
                          "context": {
                            "_text": "Score"
                          },
                          "style": {
                            "common": {
                              "font": "caption",
                              "foreground": {
                                "color": "#EAF6FF"
                              }
                            }
                          }
                        },
                        {
                          "id": "bb_final_value",
                          "type": "text",
                          "context": {
                            "_text": "${score}",
                            "_format": {
                              "number": {
                                "style": "decimal",
                                "maximumFractionDigits": 0
                              }
                            }
                          },
                          "style": {
                            "common": {
                              "font": {
                                "kind": "title2",
                                "weight": "bold"
                              },
                              "foreground": {
                                "color": "white"
                              }
                            }
                          }
                        }
                      ]
                    },
                    {
                      "id": "bb_final_best_box",
                      "type": "vstack",
                      "context": {
                        "_spacing": 2,
                        "_alignment": "center"
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": 12
                        },
                        "common": {
                          "background": {
                            "color": "#000000",
                            "opacity": 0.22
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 16
                        }
                      },
                      "children": [
                        {
                          "id": "bb_final_best_label",
                          "type": "text",
                          "context": {
                            "_text": "Saved best"
                          },
                          "style": {
                            "common": {
                              "font": "caption",
                              "foreground": {
                                "color": "#EAF6FF"
                              }
                            }
                          }
                        },
                        {
                          "id": "bb_final_best_value",
                          "type": "text",
                          "context": {
                            "_text": "${best}",
                            "_format": {
                              "number": {
                                "style": "decimal",
                                "maximumFractionDigits": 0
                              }
                            }
                          },
                          "style": {
                            "common": {
                              "font": {
                                "kind": "title2",
                                "weight": "bold"
                              },
                              "foreground": {
                                "color": "white"
                              }
                            }
                          }
                        }
                      ]
                    }
                  ]
                },
                {
                  "id": "bb_over_copy",
                  "type": "text",
                  "context": {
                    "_text": "Best score stays saved on this device."
                  },
                  "style": {
                    "text": {
                      "multilineTextAlignment": "center"
                    },
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "#F4FBFF"
                      }
                    }
                  }
                },
                {
                  "id": "bb_play_again",
                  "type": "button",
                  "context": {
                    "_label": "Play again"
                  },
                  "style": {
                    "button": {
                      "standard": {
                        "type": "borderedProminent",
                        "shape": "capsule"
                      }
                    },
                    "layout": {
                      "padding": {
                        "top": 6,
                        "bottom": 6,
                        "leading": 28,
                        "trailing": 28
                      }
                    },
                    "common": {
                      "tint": {
                        "color": "white"
                      },
                      "foreground": {
                        "color": "#11143B"
                      }
                    }
                  },
                  "events": {
                    "onTap": [
                      {
                        "state": {
                          "id": "bb_play_again_state",
                          "input": {
                            "board": "{{ map(range(64), 0) }}",
                            "tray": "{{ [random(0,3), random(0,3), random(0,3)] }}",
                            "used": [
                              false,
                              false,
                              false
                            ],
                            "selected": -1,
                            "score": 0,
                            "pendingRows": [
                              false,
                              false,
                              false,
                              false,
                              false,
                              false,
                              false,
                              false
                            ],
                            "pendingCols": [
                              false,
                              false,
                              false,
                              false,
                              false,
                              false,
                              false,
                              false
                            ],
                            "lastClearCount": 0,
                            "gameOver": false,
                            "status": "Pick a candy block, then tap the board."
                          },
                          "output": {
                            "success": [
                              {
                                "service": {
                                  "id": "bb_restart_haptic",
                                  "input": {
                                    "serviceId": "bb_audio",
                                    "params": {
                                      "haptic": "success",
                                      "sound": "keyPress"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      ]
    }
  ]
}