{
  "id": "mod_morskyi_bii",
  "type": "module",
  "version": "1.1",
  "context": {
    "title": "Морський бій",
    "icon": "🚢"
  },
  "state": {
    "layouts": [
      {
        "mask": [
          "S",
          "S",
          "S",
          "S",
          "",
          "S",
          "S",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "S",
          "S",
          "",
          "S",
          "S",
          "",
          "S",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          ""
        ],
        "fleet": [
          [
            0,
            1,
            2,
            3
          ],
          [
            5,
            6,
            7
          ],
          [
            20,
            21,
            22
          ],
          [
            24,
            25
          ],
          [
            27,
            28
          ],
          [
            40,
            41
          ],
          [
            43
          ],
          [
            45
          ],
          [
            47
          ],
          [
            49
          ]
        ]
      },
      {
        "mask": [
          "S",
          "",
          "S",
          "S",
          "S",
          "",
          "S",
          "S",
          "",
          "S",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "S",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "S",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          ""
        ],
        "fleet": [
          [
            0,
            10,
            20,
            30
          ],
          [
            50,
            60,
            70
          ],
          [
            2,
            3,
            4
          ],
          [
            6,
            7
          ],
          [
            22,
            23
          ],
          [
            46,
            47
          ],
          [
            9
          ],
          [
            25
          ],
          [
            27
          ],
          [
            29
          ]
        ]
      },
      {
        "mask": [
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "S",
          "",
          "S",
          "S",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "S",
          "S",
          "S",
          "",
          "S",
          "S",
          "S",
          "S"
        ],
        "fleet": [
          [
            96,
            97,
            98,
            99
          ],
          [
            92,
            93,
            94
          ],
          [
            39,
            49,
            59
          ],
          [
            80,
            90
          ],
          [
            73,
            74
          ],
          [
            76,
            77
          ],
          [
            51
          ],
          [
            53
          ],
          [
            55
          ],
          [
            57
          ]
        ]
      },
      {
        "mask": [
          "S",
          "S",
          "S",
          "",
          "",
          "S",
          "",
          "S",
          "S",
          "S",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "",
          "S",
          "",
          "S",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "S",
          "",
          "S",
          "",
          "S",
          "",
          "",
          "S",
          "",
          "",
          "S",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          ""
        ],
        "fleet": [
          [
            5,
            15,
            25,
            35
          ],
          [
            0,
            1,
            2
          ],
          [
            7,
            8,
            9
          ],
          [
            22,
            32
          ],
          [
            27,
            28
          ],
          [
            48,
            58
          ],
          [
            50
          ],
          [
            52
          ],
          [
            55
          ],
          [
            30
          ]
        ]
      }
    ],
    "playerShips": [
      "S",
      "S",
      "S",
      "S",
      "",
      "S",
      "S",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "S",
      "S",
      "",
      "S",
      "S",
      "",
      "S",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "S",
      "",
      "S",
      "",
      "S",
      "",
      "S",
      "",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
    ],
    "playerFleet": [
      [
        0,
        1,
        2,
        3
      ],
      [
        5,
        6,
        7
      ],
      [
        20,
        21,
        22
      ],
      [
        24,
        25
      ],
      [
        27,
        28
      ],
      [
        40,
        41
      ],
      [
        43
      ],
      [
        45
      ],
      [
        47
      ],
      [
        49
      ]
    ],
    "enemyShips": [
      "S",
      "",
      "S",
      "S",
      "S",
      "",
      "S",
      "S",
      "",
      "S",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "",
      "S",
      "S",
      "",
      "S",
      "",
      "S",
      "",
      "S",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "S",
      "",
      "",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "S",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
    ],
    "enemyFleet": [
      [
        0,
        10,
        20,
        30
      ],
      [
        50,
        60,
        70
      ],
      [
        2,
        3,
        4
      ],
      [
        6,
        7
      ],
      [
        22,
        23
      ],
      [
        46,
        47
      ],
      [
        9
      ],
      [
        25
      ],
      [
        27
      ],
      [
        29
      ]
    ],
    "playerShots": [
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
    ],
    "cpuShots": [
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
    ],
    "turn": "player",
    "winner": "",
    "message": "Стріляйте по флоту ворога!",
    "cpuKnow": [],
    "cpuCand": [],
    "cpuOpen": [],
    "cpuRIdxCand": 0,
    "cpuRIdxOpen": 0,
    "cpuTarget": -1,
    "cpuThinking": false,
    "pSel": 0,
    "eSel": 1,
    "showToast": false,
    "toastText": ""
  },
  "dependencies": [
    {
      "service": {
        "audio": {
          "id": "mb_audio"
        }
      }
    },
    {
      "repository": {
        "local": {
          "id": "mb_store",
          "config": {}
        }
      }
    }
  ],
  "children": [
    {
      "id": "mb_scroll",
      "type": "scroll",
      "style": {
        "layout": {
          "maxWidth": "infinity",
          "maxHeight": "infinity"
        },
        "scroll": {
          "dismissesKeyboard": "interactively"
        }
      },
      "children": [
        {
          "id": "mb_root",
          "type": "vstack",
          "context": {
            "_spacing": 16,
            "_alignment": "center"
          },
          "style": {
            "layout": {
              "maxWidth": "infinity",
              "padding": {
                "top": 16,
                "bottom": 28,
                "leading": 14,
                "trailing": 14
              }
            }
          },
          "children": [
            {
              "id": "mb_status",
              "type": "vstack",
              "context": {
                "_spacing": 8,
                "_alignment": "center",
                "enemySunk": "{{ sum(map(${enemyFleet}, count(@{item}) == sum(map(@{item}, ${playerShots}[@{item}] == 'H' ? 1 : 0)) ? 1 : 0)) }}",
                "playerSunk": "{{ sum(map(${playerFleet}, count(@{item}) == sum(map(@{item}, ${cpuShots}[@{item}] == 'H' ? 1 : 0)) ? 1 : 0)) }}"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 16
                },
                "common": {
                  "background": {
                    "gradient": {
                      "linear": {
                        "colors": [
                          "#12557E",
                          "#2C88B5"
                        ],
                        "startPoint": "topLeading",
                        "endPoint": "bottomTrailing"
                      }
                    }
                  },
                  "foreground": {
                    "color": "white"
                  },
                  "cornerRadius": 20
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.22
                  },
                  "radius": 12,
                  "y": 6
                },
                "animation": {
                  "kind": "spring",
                  "value": "${turn}",
                  "duration": 0.3,
                  "bounce": 0.3
                }
              },
              "children": [
                {
                  "id": "mb_status_icon",
                  "type": "image",
                  "context": {
                    "_source": "{{ notEmpty(${winner}) ? (${winner} == 'player' ? 'sf://trophy.fill' : 'sf://xmark.octagon.fill') : (${turn} == 'player' ? 'sf://scope' : 'sf://hourglass') }}"
                  },
                  "style": {
                    "layout": {
                      "width": 26,
                      "height": 26
                    },
                    "common": {
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "mb_status_title",
                  "type": "text",
                  "context": {
                    "_text": "{{ notEmpty(${winner}) ? (${winner} == 'player' ? 'Перемога!' : 'Поразка') : (${turn} == 'player' ? 'Ваш хід' : 'Хід ворога') }}"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "title2",
                        "weight": "bold"
                      },
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "mb_status_msg",
                  "type": "text",
                  "context": {
                    "_text": "${message}"
                  },
                  "style": {
                    "common": {
                      "font": "subheadline",
                      "foreground": {
                        "color": {
                          "color": "white",
                          "opacity": 0.9
                        }
                      }
                    },
                    "text": {
                      "multilineTextAlignment": "center"
                    }
                  }
                },
                {
                  "id": "mb_counts",
                  "type": "hstack",
                  "context": {
                    "_spacing": 18,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "padding": {
                        "top": 4
                      }
                    }
                  },
                  "children": [
                    {
                      "id": "mb_c_enemy",
                      "type": "label",
                      "context": {
                        "_title": "{{ 'Потоплено: ' + cast(@{enemySunk}, 'str') + '/10' }}",
                        "_image": "sf://target"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "caption",
                            "weight": "semibold"
                          },
                          "foreground": {
                            "color": "white"
                          }
                        }
                      }
                    },
                    {
                      "id": "mb_c_player",
                      "type": "label",
                      "context": {
                        "_title": "{{ 'Втрачено: ' + cast(@{playerSunk}, 'str') + '/10' }}",
                        "_image": "sf://shield.lefthalf.filled"
                      },
                      "style": {
                        "common": {
                          "font": {
                            "kind": "caption",
                            "weight": "semibold"
                          },
                          "foreground": {
                            "color": {
                              "color": "white",
                              "opacity": 0.9
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "mb_enemy_card",
              "type": "vstack",
              "context": {
                "_spacing": 8,
                "_alignment": "leading"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 12
                },
                "common": {
                  "background": {
                    "light": "#EAF4FA",
                    "dark": "#0E2233"
                  },
                  "foreground": {
                    "light": "#0E2233",
                    "dark": "#EAF4FA"
                  },
                  "cornerRadius": 18
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.14
                  },
                  "radius": 8,
                  "y": 4
                }
              },
              "children": [
                {
                  "id": "mb_enemy_card_hdr",
                  "type": "label",
                  "context": {
                    "_title": "Флот ворога",
                    "_image": "sf://target"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "headline"
                      }
                    }
                  }
                },
                {
                  "id": "mb_enemy_card_sub",
                  "type": "text",
                  "context": {
                    "_text": "Торкніться клітинки, щоб вистрілити"
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                },
                {
                  "id": "mb_eb_grid",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 3
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        }
                      ]
                    },
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "mb_eb_dyn",
                      "type": "dynamic",
                      "context": {
                        "_data": "${playerShots}"
                      },
                      "prototype": {
                        "id": "mb_ec",
                        "type": "button",
                        "context": {
                          "_label": ""
                        },
                        "style": {
                          "layout": {
                            "height": 30,
                            "maxWidth": "infinity"
                          },
                          "button": {
                            "custom": {
                              "common": {
                                "background": "{{ switch(@{item} == 'H', {'color':'#E5533D'}, @{item} == 'M', {'light':'#A9C0CE','dark':'#22384A'}, (notEmpty(${winner}) && ${enemyShips[@{index}]} == 'S'), {'light':'#8D99A6','dark':'#4A5A6B'}, {'light':'#C7E6F4','dark':'#123049'}) }}",
                                "cornerRadius": 5
                              }
                            }
                          },
                          "animation": {
                            "kind": "spring",
                            "value": "@{item}",
                            "duration": 0.3,
                            "bounce": 0.3
                          }
                        },
                        "children": [
                          {
                            "id": "mb_ec_ic",
                            "type": "image",
                            "context": {
                              "_source": "{{ @{item} == 'H' ? 'sf://flame.fill' : 'sf://circle.fill' }}"
                            },
                            "style": {
                              "layout": {
                                "width": 13,
                                "height": 13
                              },
                              "common": {
                                "foreground": "{{ @{item} == 'H' ? {'color':'white'} : {'color':'secondary'} }}",
                                "opacity": "{{ @{item} == 'H' ? 1 : (@{item} == 'M' ? 0.5 : ((notEmpty(${winner}) && ${enemyShips[@{index}]} == 'S') ? 0.85 : 0)) }}"
                              }
                            }
                          }
                        ],
                        "events": {
                          "onTap": [
                            {
                              "condition": {
                                "id": "mb_ec_guard",
                                "input": "{{ ${turn} == 'player' && empty(${winner}) && ${playerShots[@{index}]} == '' }}",
                                "output": {
                                  "success": [
                                    {
                                      "state": {
                                        "id": "mb_fire_player",
                                        "input": {
                                          "playerShots": "{{ setAt(${playerShots}, @{index}, ${enemyShips[@{index}]} == 'S' ? 'H' : 'M') }}",
                                          "winner": "{{ (sum(map(${playerShots}, @{item} == 'H' ? 1 : 0)) + (${enemyShips[@{index}]} == 'S' ? 1 : 0)) == 20 ? 'player' : '' }}",
                                          "turn": "{{ (sum(map(${playerShots}, @{item} == 'H' ? 1 : 0)) + (${enemyShips[@{index}]} == 'S' ? 1 : 0)) == 20 ? 'player' : 'cpu' }}",
                                          "message": "{{ ${enemyShips[@{index}]} == 'S' ? ((sum(map(${playerShots}, @{item} == 'H' ? 1 : 0)) + 1) == 20 ? 'Ви знищили весь флот ворога — Перемога!' : 'Влучання! Тепер хід ворога.') : 'Мимо. Хід ворога.' }}",
                                          "toastText": "{{ ${enemyShips[@{index}]} == 'S' ? '🔥 Влучання!' : '· Мимо' }}",
                                          "showToast": true
                                        },
                                        "output": {
                                          "success": [
                                            {
                                              "repo": {
                                                "id": "mb_persist_pl",
                                                "input": {
                                                  "repositoryId": "mb_store",
                                                  "operation": "create",
                                                  "params": {
                                                    "collection": "save",
                                                    "documentId": "game",
                                                    "rewrite": true,
                                                    "body": {
                                                      "playerShips": "${playerShips}",
                                                      "enemyShips": "${enemyShips}",
                                                      "playerFleet": "${playerFleet}",
                                                      "enemyFleet": "${enemyFleet}",
                                                      "playerShots": "${playerShots}",
                                                      "cpuShots": "${cpuShots}",
                                                      "turn": "${turn}",
                                                      "winner": "${winner}",
                                                      "message": "${message}"
                                                    }
                                                  }
                                                }
                                              }
                                            },
                                            {
                                              "service": {
                                                "id": "mb_hap_pl",
                                                "input": {
                                                  "serviceId": "mb_audio",
                                                  "params": {
                                                    "haptic": "{{ ${enemyShips[@{index}]} == 'S' ? 'success' : 'light' }}"
                                                  }
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  ],
                                  "failure": [
                                    {
                                      "state": {
                                        "id": "mb_ec_blocked",
                                        "input": {
                                          "message": "{{ notEmpty(${winner}) ? 'Гру завершено — почніть нову.' : (${turn} == 'cpu' ? 'Зачекайте — ходить ворог…' : 'Сюди вже стріляли.') }}",
                                          "toastText": "{{ notEmpty(${winner}) ? 'Гру завершено' : (${turn} == 'cpu' ? 'Хід ворога' : 'Вже стріляли') }}",
                                          "showToast": true
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "mb_player_card",
              "type": "vstack",
              "context": {
                "_spacing": 8,
                "_alignment": "leading"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": 12
                },
                "common": {
                  "background": {
                    "light": "#EAF4FA",
                    "dark": "#0c2234"
                  },
                  "foreground": {
                    "light": "#0E2233",
                    "dark": "#EAF4FA"
                  },
                  "cornerRadius": 18
                },
                "shadow": {
                  "color": {
                    "color": "black",
                    "opacity": 0.14
                  },
                  "radius": 8,
                  "y": 4
                }
              },
              "children": [
                {
                  "id": "mb_player_card_hdr",
                  "type": "label",
                  "context": {
                    "_title": "Ваш флот",
                    "_image": "sf://sailboat.fill"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "headline"
                      }
                    }
                  }
                },
                {
                  "id": "mb_player_card_sub",
                  "type": "text",
                  "context": {
                    "_text": "Постріли ворога по вашому полю"
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                },
                {
                  "id": "mb_pb_grid",
                  "type": "grid",
                  "context": {
                    "_gridType": "lazyVGrid",
                    "_spacing": 3
                  },
                  "style": {
                    "grid": {
                      "columns": [
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        },
                        {
                          "kind": "flexible",
                          "minimum": 22,
                          "maximum": 44,
                          "spacing": 3
                        }
                      ]
                    },
                    "layout": {
                      "maxWidth": "infinity"
                    }
                  },
                  "children": [
                    {
                      "id": "mb_pb_dyn",
                      "type": "dynamic",
                      "context": {
                        "_data": "${cpuShots}"
                      },
                      "prototype": {
                        "id": "mb_pc",
                        "type": "vstack",
                        "context": {
                          "_alignment": "center"
                        },
                        "style": {
                          "layout": {
                            "height": 30,
                            "maxWidth": "infinity"
                          },
                          "common": {
                            "background": "{{ switch(@{item} == 'H', {'color':'#E5533D'}, @{item} == 'M', {'light':'#A9C0CE','dark':'#22384A'}, ${playerShips[@{index}]} == 'S', {'light':'#5B7086','dark':'#3D5570'}, {'light':'#C7E6F4','dark':'#123049'}) }}",
                            "cornerRadius": 5
                          },
                          "animation": {
                            "kind": "spring",
                            "value": "@{item}",
                            "duration": 0.3,
                            "bounce": 0.3
                          }
                        },
                        "children": [
                          {
                            "id": "mb_pc_ic",
                            "type": "image",
                            "context": {
                              "_source": "{{ @{item} == 'H' ? 'sf://flame.fill' : 'sf://circle.fill' }}"
                            },
                            "style": {
                              "layout": {
                                "width": 13,
                                "height": 13
                              },
                              "common": {
                                "foreground": "{{ @{item} == 'H' ? {'color':'white'} : {'color':'secondary'} }}",
                                "opacity": "{{ @{item} == 'H' ? 1 : (@{item} == 'M' ? 0.5 : 0) }}"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            },
            {
              "id": "mb_legend",
              "type": "hstack",
              "context": {
                "_spacing": 14,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": {
                    "top": 2
                  }
                }
              },
              "children": [
                {
                  "id": "mb_lg_hit",
                  "type": "label",
                  "context": {
                    "_title": "влучання",
                    "_image": "sf://flame.fill"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "#E5533D"
                      }
                    }
                  }
                },
                {
                  "id": "mb_lg_miss",
                  "type": "label",
                  "context": {
                    "_title": "мимо",
                    "_image": "sf://circle.fill"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "secondary"
                      }
                    }
                  }
                },
                {
                  "id": "mb_lg_ship",
                  "type": "label",
                  "context": {
                    "_title": "ваш корабель",
                    "_image": "sf://square.fill"
                  },
                  "style": {
                    "common": {
                      "font": "caption2",
                      "foreground": {
                        "color": "#5B7086"
                      }
                    }
                  }
                }
              ]
            },
            {
              "id": "mb_actions",
              "type": "hstack",
              "context": {
                "_spacing": 10,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "padding": {
                    "top": 2
                  }
                }
              },
              "children": [
                {
                  "id": "mb_newgame_btn",
                  "type": "button",
                  "context": {
                    "_label": "Нова гра",
                    "_image": "sf://arrow.counterclockwise"
                  },
                  "style": {
                    "button": {
                      "custom": {
                        "layout": {
                          "maxWidth": "infinity",
                          "minHeight": 48
                        },
                        "common": {
                          "background": {
                            "gradient": {
                              "linear": {
                                "colors": [
                                  "#12557E",
                                  "#2C88B5"
                                ],
                                "startPoint": "leading",
                                "endPoint": "trailing"
                              }
                            }
                          },
                          "foreground": {
                            "color": "white"
                          },
                          "cornerRadius": 16,
                          "font": {
                            "kind": "headline"
                          }
                        }
                      }
                    }
                  },
                  "events": {
                    "onTap": [
                      {
                        "state": {
                          "id": "mb_ng_pick",
                          "input": {
                            "pSel": "{{ random(0, 3) }}",
                            "eSel": "{{ random(0, 3) }}"
                          },
                          "output": {
                            "success": [
                              {
                                "condition": {
                                  "id": "mb_seq_ng",
                                  "input": "{{ true }}",
                                  "output": {
                                    "success": [
                                      {
                                        "state": {
                                          "id": "mb_ng_apply",
                                          "input": {
                                            "playerShips": "{{ ${layouts[${pSel}].mask} }}",
                                            "playerFleet": "{{ ${layouts[${pSel}].fleet} }}",
                                            "enemyShips": "{{ ${layouts[${eSel}].mask} }}",
                                            "enemyFleet": "{{ ${layouts[${eSel}].fleet} }}",
                                            "playerShots": "{{ map(range(100), '') }}",
                                            "cpuShots": "{{ map(range(100), '') }}",
                                            "turn": "player",
                                            "winner": "",
                                            "cpuThinking": false,
                                            "message": "Нова гра. Стріляйте по флоту ворога!",
                                            "toastText": "Нова гра",
                                            "showToast": true
                                          },
                                          "output": {
                                            "success": [
                                              {
                                                "repo": {
                                                  "id": "mb_persist_ng",
                                                  "input": {
                                                    "repositoryId": "mb_store",
                                                    "operation": "create",
                                                    "params": {
                                                      "collection": "save",
                                                      "documentId": "game",
                                                      "rewrite": true,
                                                      "body": {
                                                        "playerShips": "${playerShips}",
                                                        "enemyShips": "${enemyShips}",
                                                        "playerFleet": "${playerFleet}",
                                                        "enemyFleet": "${enemyFleet}",
                                                        "playerShots": "${playerShots}",
                                                        "cpuShots": "${cpuShots}",
                                                        "turn": "${turn}",
                                                        "winner": "${winner}",
                                                        "message": "${message}"
                                                      }
                                                    }
                                                  }
                                                }
                                              },
                                              {
                                                "service": {
                                                  "id": "mb_hap_ng",
                                                  "input": {
                                                    "serviceId": "mb_audio",
                                                    "params": {
                                                      "haptic": "success"
                                                    }
                                                  }
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    ]
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            },
            {
              "id": "mb_toast",
              "type": "text",
              "context": {
                "_text": "${toastText}"
              },
              "style": {
                "layout": {
                  "padding": {
                    "top": 9,
                    "bottom": 9,
                    "leading": 16,
                    "trailing": 16
                  }
                },
                "common": {
                  "background": {
                    "material": "ultraThin"
                  },
                  "foreground": {
                    "color": "primary"
                  },
                  "cornerRadius": 18,
                  "font": "subheadline",
                  "opacity": "{{ ${showToast} ? 1.0 : 0.0 }}",
                  "scale": "{{ ${showToast} ? 1.0 : 0.9 }}"
                },
                "animation": {
                  "kind": "spring",
                  "value": "${showToast}",
                  "duration": 0.35,
                  "bounce": 0.25
                }
              },
              "events": {
                "onChange": {
                  "observed": "showToast",
                  "actions": [
                    {
                      "condition": {
                        "id": "mb_toast_guard",
                        "input": "${showToast}",
                        "output": {
                          "success": [
                            {
                              "interval": {
                                "id": "mb_toast_timer",
                                "input": {
                                  "delay": 1.6
                                },
                                "output": {
                                  "success": [
                                    {
                                      "state": {
                                        "id": "mb_hide_toast",
                                        "input": {
                                          "showToast": false
                                        }
                                      }
                                    },
                                    {
                                      "interval": {
                                        "id": "mb_toast_timer",
                                        "input": {
                                          "cancel": true
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            },
            {
              "id": "mb_note",
              "type": "text",
              "context": {
                "_text": "Кораблі розставляються випадково (1×4, 2×3, 3×2, 4×1-палубні). Гравець і комп'ютер стріляють по черзі — по одному пострілу за хід."
              },
              "style": {
                "common": {
                  "font": "caption2",
                  "foreground": {
                    "color": "secondary"
                  }
                },
                "text": {
                  "multilineTextAlignment": "center"
                }
              }
            }
          ],
          "events": {
            "onAppear": [
              {
                "repo": {
                  "id": "mb_load",
                  "input": {
                    "repositoryId": "mb_store",
                    "operation": "read",
                    "params": {
                      "collection": "save",
                      "documentId": "game"
                    }
                  },
                  "output": {
                    "success": [
                      {
                        "state": {
                          "id": "mb_restore",
                          "input": {
                            "playerShips": "@{mb_load.playerShips}",
                            "enemyShips": "@{mb_load.enemyShips}",
                            "playerFleet": "@{mb_load.playerFleet}",
                            "enemyFleet": "@{mb_load.enemyFleet}",
                            "playerShots": "@{mb_load.playerShots}",
                            "cpuShots": "@{mb_load.cpuShots}",
                            "winner": "@{mb_load.winner}",
                            "turn": "player",
                            "message": "Ваш хід — стріляйте по флоту ворога!",
                            "cpuThinking": false
                          }
                        }
                      }
                    ],
                    "failure": []
                  }
                }
              }
            ]
          }
        }
      ]
    }
  ],
  "events": {
    "onChange": {
      "observed": "turn",
      "actions": [
        {
          "condition": {
            "id": "mb_cpu_guard",
            "input": "{{ ${turn} == 'cpu' && empty(${winner}) && !${cpuThinking} }}",
            "output": {
              "success": [
                {
                  "state": {
                    "id": "mb_cpu_think",
                    "input": {
                      "cpuThinking": true,
                      "message": "Ворог цілиться…"
                    },
                    "output": {
                      "success": [
                        {
                          "interval": {
                            "id": "mb_cpu_delay",
                            "input": {
                              "delay": 0.55
                            },
                            "output": {
                              "success": [
                                {
                                  "interval": {
                                    "id": "mb_cpu_delay",
                                    "input": {
                                      "cancel": true
                                    }
                                  }
                                },
                                {
                                  "state": {
                                    "id": "mb_cpu_know",
                                    "input": {
                                      "cpuKnow": "{{ map(range(100), { 'i': @{item}, 'o': (${cpuShots}[@{item}] == '' ? 1 : 0), 'a': ((@{item} >= 10 && ${cpuShots}[@{item} - 10] == 'H') || (@{item} <= 89 && ${cpuShots}[@{item} + 10] == 'H') || (@{item} % 10 >= 1 && ${cpuShots}[@{item} - 1] == 'H') || (@{item} % 10 <= 8 && ${cpuShots}[@{item} + 1] == 'H') ? 1 : 0) }) }}"
                                    },
                                    "output": {
                                      "success": [
                                        {
                                          "condition": {
                                            "id": "mb_seq_know",
                                            "input": "{{ true }}",
                                            "output": {
                                              "success": [
                                                {
                                                  "state": {
                                                    "id": "mb_cpu_prep",
                                                    "input": {
                                                      "cpuCand": "{{ ${cpuKnow[o == 1 && a == 1]} }}",
                                                      "cpuOpen": "{{ ${cpuKnow[o == 1]} }}"
                                                    },
                                                    "output": {
                                                      "success": [
                                                        {
                                                          "condition": {
                                                            "id": "mb_seq_prep",
                                                            "input": "{{ true }}",
                                                            "output": {
                                                              "success": [
                                                                {
                                                                  "state": {
                                                                    "id": "mb_cpu_ridx",
                                                                    "input": {
                                                                      "cpuRIdxCand": "{{ count(${cpuCand}) > 0 ? random(0, count(${cpuCand}) - 1) : 0 }}",
                                                                      "cpuRIdxOpen": "{{ count(${cpuOpen}) > 0 ? random(0, count(${cpuOpen}) - 1) : 0 }}"
                                                                    },
                                                                    "output": {
                                                                      "success": [
                                                                        {
                                                                          "condition": {
                                                                            "id": "mb_seq_ridx",
                                                                            "input": "{{ true }}",
                                                                            "output": {
                                                                              "success": [
                                                                                {
                                                                                  "state": {
                                                                                    "id": "mb_cpu_pick",
                                                                                    "input": {
                                                                                      "cpuTarget": "{{ count(${cpuCand}) > 0 ? ${cpuCand[${cpuRIdxCand}].i} : ${cpuOpen[${cpuRIdxOpen}].i} }}"
                                                                                    },
                                                                                    "output": {
                                                                                      "success": [
                                                                                        {
                                                                                          "condition": {
                                                                                            "id": "mb_seq_pick",
                                                                                            "input": "{{ true }}",
                                                                                            "output": {
                                                                                              "success": [
                                                                                                {
                                                                                                  "state": {
                                                                                                    "id": "mb_cpu_fire",
                                                                                                    "input": {
                                                                                                      "cpuShots": "{{ setAt(${cpuShots}, ${cpuTarget}, ${playerShips[${cpuTarget}]} == 'S' ? 'H' : 'M') }}",
                                                                                                      "winner": "{{ (sum(map(${cpuShots}, @{item} == 'H' ? 1 : 0)) + (${playerShips[${cpuTarget}]} == 'S' ? 1 : 0)) == 20 ? 'cpu' : '' }}",
                                                                                                      "turn": "{{ (sum(map(${cpuShots}, @{item} == 'H' ? 1 : 0)) + (${playerShips[${cpuTarget}]} == 'S' ? 1 : 0)) == 20 ? 'cpu' : 'player' }}",
                                                                                                      "cpuThinking": false,
                                                                                                      "message": "{{ (sum(map(${cpuShots}, @{item} == 'H' ? 1 : 0)) + (${playerShips[${cpuTarget}]} == 'S' ? 1 : 0)) == 20 ? 'Ворог потопив увесь ваш флот — Поразка.' : (${playerShips[${cpuTarget}]} == 'S' ? 'Ворог влучив у ваш корабель! Ваш хід.' : 'Ворог схибив. Ваш хід.') }}",
                                                                                                      "toastText": "{{ ${playerShips[${cpuTarget}]} == 'S' ? 'Ворог влучив' : 'Ворог схибив' }}",
                                                                                                      "showToast": true
                                                                                                    },
                                                                                                    "output": {
                                                                                                      "success": [
                                                                                                        {
                                                                                                          "repo": {
                                                                                                            "id": "mb_persist_cpu",
                                                                                                            "input": {
                                                                                                              "repositoryId": "mb_store",
                                                                                                              "operation": "create",
                                                                                                              "params": {
                                                                                                                "collection": "save",
                                                                                                                "documentId": "game",
                                                                                                                "rewrite": true,
                                                                                                                "body": {
                                                                                                                  "playerShips": "${playerShips}",
                                                                                                                  "enemyShips": "${enemyShips}",
                                                                                                                  "playerFleet": "${playerFleet}",
                                                                                                                  "enemyFleet": "${enemyFleet}",
                                                                                                                  "playerShots": "${playerShots}",
                                                                                                                  "cpuShots": "${cpuShots}",
                                                                                                                  "turn": "${turn}",
                                                                                                                  "winner": "${winner}",
                                                                                                                  "message": "${message}"
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        },
                                                                                                        {
                                                                                                          "service": {
                                                                                                            "id": "mb_hap_cpu",
                                                                                                            "input": {
                                                                                                              "serviceId": "mb_audio",
                                                                                                              "params": {
                                                                                                                "haptic": "{{ ${playerShips[${cpuTarget}]} == 'S' ? 'warning' : 'light' }}"
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      ]
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              ]
                                                                                            }
                                                                                          }
                                                                                        }
                                                                                      ]
                                                                                    }
                                                                                  }
                                                                                }
                                                                              ]
                                                                            }
                                                                          }
                                                                        }
                                                                      ]
                                                                    }
                                                                  }
                                                                }
                                                              ]
                                                            }
                                                          }
                                                        }
                                                      ]
                                                    }
                                                  }
                                                }
                                              ]
                                            }
                                          }
                                        }
                                      ]
                                    }
                                  }
                                }
                              ],
                              "failure": [
                                {
                                  "state": {
                                    "id": "mb_cpu_delay_fail",
                                    "input": {
                                      "cpuThinking": false,
                                      "turn": "player",
                                      "message": "Ворог пропускає хід. Ваш хід."
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}