Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

74.3 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Statecraft.Game.Map;
using Statecraft.Game.Map.Units;
using Statecraft.Sim;
using UnityEngine.UIElements;

namespace Statecraft.Game.UI
{
    /// <summary>The info panel (bottom-left): the selected hex's land, height and the settlements it owns, what the player
    /// built there, and the two map actions (transit camp, housing project) with the reason when one is not possible
    /// (MapActions.CheckReason and Simulation.Preview of the exact queue a press would leave). Tile yields are deliberately not
    /// shown (owner decision, M3 rulings 2). A selected unit
    /// (<see cref="ShowUnit"/>) shows its card instead (<see cref="UnitCard"/>), with a button to the Defence screen (owner
    /// ruling, 2026-10-11) and, in the GDD loop, the Move, Fortify, Hold and Attack orders (GDD Phase 5 PR 15, #754),
    /// and Send home after the unit's front signs its armistice (#1309).</summary>
    public sealed class InfoPanelPresenter : System.IDisposable
    {
        readonly Button confirmMove;
        (Command command, GameSession session, int turn)? pendingMove;
        readonly System.Action? clearSelection;
        readonly EventCallback<NavigationCancelEvent> onCancel;

        /// <summary>The class of a fact row (Game.uxml): a sign, then the fact's line; hidden while the line is empty.</summary>
        public const string RowClass = "info-row";
        /// <summary>On the panel while it shows a unit's card: the rows keep their lines but drop the hex signs (Statecraft.uss).</summary>
        public const string UnitClass = "info-unit";
        /// <summary>On the panel while it shows the GDD loop's city or site part (#381): a second column (Statecraft.uss).</summary>
        public const string GddClass = "info-gdd-shown";
        readonly List<(VisualElement row, Label line)> rows = new List<(VisualElement, Label)>();

        readonly VisualElement panel; readonly SimBridge bridge; readonly UiText text;
        /// <summary>The card's orders row (info-orders): a press on it that misses a button stays the card's (#1728).</summary>
        readonly VisualElement? orders;
        readonly Button campButton, housingButton, defenceButton, moveButton, fortifyButton, holdButton, attackButton, sendHomeButton, skipButton, reinforceButton;
        /// <summary>The Reinforce order the card offers now (#1510), or null: what its button queues.</summary>
        Reinforce? reinforce;
        readonly System.Action onCamp, onHousing, onDefence, onMove, onFortify, onHold, onAttack, onSendHome, onSkip, onReinforce;
        readonly System.Func<PlacedToken?> selectedUnit;
        readonly System.Action? unitDone;
        bool unitMode;
        int? pioneer;
        GameSession? pioneerSession;
        readonly VisualElement pioneerList;
        readonly Button pioneerReturn;
        readonly System.Action onPioneerReturn;
        readonly System.Action<int>? selectPioneer;
        /// <summary>Lights the hexes the Move or Attack pick offers on the map (none: clears them); null: nothing is drawn.</summary>
        readonly System.Action<IReadOnlyCollection<(int q, int r)>>? showReachable;
        /// <summary>Draws a multi-turn go-to route and its arrival-turn numbers; null keeps picking functional without it.</summary>
        readonly System.Action<IReadOnlyList<(int q, int r, int turn)>>? showRoute;
        /// <summary>Draws the move arrow through the hexes given (none: hides it); null keeps picking functional without it.</summary>
        readonly System.Action<IReadOnlyList<(int q, int r)>>? showArrow;
        /// <summary>Whether the last arrow passed to showArrow had hexes: an empty arrow is passed only to clear a drawn one, so
        /// a pointer crossing the map with no arrow drawn never touches the map's mesh (review of #2134, C3).</summary>
        bool arrowDrawn;
        /// <summary>While the player picks a Move's or an Attack's hex: the unit, the hexes the sim gave (Movement.Reachable's,
        /// or those Combat.Check takes: the only ones a click takes), the session and turn they were read on (a turn or a
        /// load ends the pick, as the hexes change), and whether it is the attack's.</summary>
        (int unit, HashSet<(int q, int r)> hexes, GameSession session, int turn, bool attack, (int q, int r)? touchRoute)? pick;
        (int q, int r)? shownRoute;
        /// <summary>TakesClickThrough's last answer, by hex: the pointer asks it every frame it hovers the card, and a go-to's
        /// route is a whole-map search. Forgotten when the pick ends and on every render (an order or a new state).</summary>
        ((int q, int r) hex, bool takes)? clickThrough;
        /// <summary>A sim unit's name on screen, for the attack's targets (GameRoot: its token's card title); null, or no
        /// name, gives the hex's coordinates.</summary>
        readonly System.Func<int, string?>? unitName;
        readonly Label title, subtitle, region, settlements, localitiesLabel, placement, reason;
        readonly System.Func<int, int, IReadOnlyList<Statecraft.Game.Map.Kits.Localities.Place>>? localitiesAt;
        readonly UiIcon thumb;
        /// <summary>The GDD loop's part (GddHexPanel, #381): a city's counts and orders, or the designated site's.</summary>
        readonly VisualElement gdd;
        /// <summary>The hex's city projects (GddHexPanel, #1122), under the facts.</summary>
        readonly VisualElement projects;
        /// <summary>The map stop the panel is opened from: the last place to put the focus when a press leaves its whole
        /// part with nothing that can take it (#41, "Still open" item 2).</summary>
        readonly VisualElement? mapFocus;
        readonly IReadOnlyDictionary<string, string> settlementNames;
        readonly MapRegions? regions;
        readonly System.Func<string, (int q, int r)>? townHex;
        readonly IReadOnlyDictionary<string, string>? townNamesHe;
        /// <summary>Whether the last press in the panel came from the keyboard: only then does a rebuilt GDD part give the
        /// focus back (RenderLines).</summary>
        readonly PressSource presses;

        /// <param name="localitiesAt">The 1948 localities that lie in a hex (null: the panel lists none); they are listed under the
        /// settlements with today's name where the shared rule (Localities.TodayName) allows (#245).</param>
        /// <param name="nav">Opens the Defence screen from a unit's card (null: the button does nothing).</param>
        /// <param name="selectedUnit">The selected unit's token now (null: none), read at every render so the card follows
        /// the state.</param>
        /// <param name="regions">What the map outlines (the 1948 sub-districts and the 1949 armistice lines in force); the
        /// panel names the selected hex's sub-district or armistice zone, and any armistice line through it, with its
        /// agreement (null: no region line).</param>
        /// <param name="townHex">The hex each settlement's own point lies in, by id (<see cref="TownHex"/>; null: no note on
        /// a town drawn from the water hex next door, #204).</param>
        /// <param name="townNamesHe">Each settlement's Hebrew name (name_he), by id: the Hebrew UI names the town with it in the
        /// title, the "Settlements" line and that note (#899), or with the English name where it has none (null: English
        /// names).</param>
        /// <param name="showReachable">Lights the hexes a Move can go to while the player picks one, and clears them with none
        /// (GameRoot: MapScene.ShowReachable; null: the pick still works, with nothing lit). The Attack pick lights its hexes
        /// with it too.</param>
        /// <param name="showArrow">Draws the move arrow (#2134) through the unit's hex and the sim's path to the hovered hex,
        /// and hides it with none (GameRoot: MapScene.ShowMoveArrow; null: no arrow).</param>
        /// <param name="unitName">A sim unit's name on screen, by id (GameRoot: its token's card title), for the Attack pick's
        /// targets (null: the hex's coordinates).</param>
        public InfoPanelPresenter(VisualElement root, SimBridge bridge, UiText text, IReadOnlyDictionary<string, string> settlementNames,
            ScreenNav? nav = null, System.Func<PlacedToken?>? selectedUnit = null, MapRegions? regions = null,
            System.Func<int, int, IReadOnlyList<Statecraft.Game.Map.Kits.Localities.Place>>? localitiesAt = null,
            System.Func<string, (int q, int r)>? townHex = null, IReadOnlyDictionary<string, string>? townNamesHe = null,
            System.Action<IReadOnlyCollection<(int q, int r)>>? showReachable = null, System.Func<int, string?>? unitName = null,
            System.Action? unitDone = null,
            System.Action<IReadOnlyList<(int q, int r, int turn)>>? showRoute = null, System.Action? clearSelection = null, System.Action<int>? selectPioneer = null,
            System.Action<IReadOnlyList<(int q, int r)>>? showArrow = null)
        {
            panel = root.Q<VisualElement>("info-panel"); this.bridge = bridge; this.text = text;
            // #40: the panel is a ScrollView, so Game.uxml's filigree frame lands in its scrolled content, inside the padding
            // and clipped by the viewport. Framing the panel needs it on the ScrollView itself, as on the panel before.
            if (panel is ScrollView scroll)
                foreach (var frame in scroll.contentContainer.Children().OfType<UiFrame>().ToList()) scroll.hierarchy.Add(frame);
            orders = panel.Q<VisualElement>("info-orders");
            confirmMove = new Button(ConfirmMove) { name = "info-confirm-move" };
            confirmMove.RegisterCallback<KeyDownEvent>(OnConfirmKey);
            confirmMove.style.whiteSpace = WhiteSpace.Normal;
            panel.Q<Label>("info-title").parent.Insert(2, confirmMove);
            DiscardMove();
            ScreenNav.InfoIntentCovered += OnInfoCovered;
            this.selectPioneer = selectPioneer;
            pioneerList = panel.Q<VisualElement>("info-pioneers");
            pioneerReturn = panel.Q<Button>("info-pioneer-return");
            onPioneerReturn = ReturnPioneer;
            pioneerReturn.clicked += onPioneerReturn;
            this.clearSelection = clearSelection;
            this.showArrow = showArrow;
            onCancel = e => { if (!e.isPropagationStopped && Escape()) e.StopPropagation(); };
            panel.RegisterCallback(onCancel);
            this.settlementNames = settlementNames; this.regions = regions; this.localitiesAt = localitiesAt; this.townHex = townHex;
            this.townNamesHe = townNamesHe;
            title = panel.Q<Label>("info-title"); subtitle = panel.Q<Label>("info-subtitle"); region = panel.Q<Label>("info-region"); settlements = panel.Q<Label>("info-settlements");
            localitiesLabel = panel.Q<Label>("info-localities");
            placement = panel.Q<Label>("info-placement"); reason = panel.Q<Label>("info-reason");
            thumb = panel.Q<UiIcon>("info-thumb");
            gdd = panel.Q<VisualElement>(GddHexPanel.HostName);
            projects = panel.Q<VisualElement>(GddHexPanel.ProjectsHostName);
            mapFocus = root.Q<VisualElement>("map-focus");
            mapFocus?.RegisterCallback<NavigationSubmitEvent>(OnStopSubmit, TrickleDown.TrickleDown);
            mapFocus?.RegisterCallback<KeyDownEvent>(OnStopKey, TrickleDown.TrickleDown);
            foreach (var row in panel.Query(className: RowClass).ToList()) rows.Add((row, row.Q<Label>()));
            campButton = panel.Q<Button>("build-camp"); housingButton = panel.Q<Button>("build-housing");
            onCamp = () => Build(MapActions.TransitCamp); onHousing = () => Build(MapActions.Housing);
            campButton.clicked += onCamp; housingButton.clicked += onHousing;
            defenceButton = panel.Q<Button>("info-defence");
            this.selectedUnit = selectedUnit ?? (() => null);
            onDefence = () => nav?.Open("defence");
            defenceButton.clicked += onDefence;
            defenceButton.text = text["unit.defence"];
            this.showReachable = showReachable;
            this.unitDone = unitDone;
            this.showRoute = showRoute;
            moveButton = panel.Q<Button>("info-move");
            onMove = () => TogglePick(attack: false);
            moveButton.clicked += onMove;
            moveButton.text = text["unit.order.move"];
            fortifyButton = panel.Q<Button>("info-fortify"); holdButton = panel.Q<Button>("info-hold");
            onFortify = () => GiveOrder(Orders.Fortify); onHold = () => GiveOrder(Orders.Hold);
            fortifyButton.clicked += onFortify; holdButton.clicked += onHold;
            fortifyButton.text = text["unit.order.fortify"]; holdButton.text = text["unit.order.hold"];
            presses = new PressSource(panel);
            this.unitName = unitName;
            attackButton = panel.Q<Button>("info-attack");
            onAttack = () => TogglePick(attack: true);
            attackButton.clicked += onAttack;
            attackButton.text = text["unit.order.attack"];
            sendHomeButton = panel.Q<Button>("info-send-home");
            onSendHome = () => GiveOrder(Demobilization.SendHome);
            sendHomeButton.clicked += onSendHome;
            sendHomeButton.text = text["unit.order.send_home"];
            skipButton = panel.Q<Button>("info-skip");
            onSkip = Skip;
            skipButton.clicked += onSkip;
            skipButton.text = text["unit.order.skip"];
            reinforceButton = panel.Q<Button>("info-reinforce");
            onReinforce = () => { if (reinforce is Reinforce r) { bridge.Queue(r); Render(); } };
            reinforceButton.clicked += onReinforce;
            bridge.Changed += OnStateChanged;
            panel.parent?.RegisterCallback<GeometryChangedEvent>(OnHudGeometry);
            Render();
        }

        void OnHudGeometry(GeometryChangedEvent e) => panel.EnableInClassList(ShortClass, e.newRect.height < ShortBelowPx);

        /// <summary>Unsubscribes from the bridge and the buttons (the owner calls this when disabled).</summary>
        public void Dispose() { mapFocus?.UnregisterCallback<NavigationSubmitEvent>(OnStopSubmit, TrickleDown.TrickleDown); mapFocus?.UnregisterCallback<KeyDownEvent>(OnStopKey, TrickleDown.TrickleDown); pioneerReturn.clicked -= onPioneerReturn; pioneerList.Clear(); panel.UnregisterCallback(onCancel); presses.Dispose(); bridge.Changed -= OnStateChanged; ScreenNav.InfoIntentCovered -= OnInfoCovered; confirmMove.UnregisterCallback<KeyDownEvent>(OnConfirmKey); confirmMove.clicked -= ConfirmMove; confirmMove.RemoveFromHierarchy(); panel.parent?.UnregisterCallback<GeometryChangedEvent>(OnHudGeometry); campButton.clicked -= onCamp; housingButton.clicked -= onHousing; defenceButton.clicked -= onDefence; moveButton.clicked -= onMove; fortifyButton.clicked -= onFortify; holdButton.clicked -= onHold; attackButton.clicked -= onAttack; sendHomeButton.clicked -= onSendHome; skipButton.clicked -= onSkip; reinforceButton.clicked -= onReinforce; EndPick(); }

        /// <summary>Below this window height (logical px; 1366x657 is about 998) the panel gets ShortClass, and the city's own
        /// hex folds its projects behind their toggle as on a stacked bar (GddHexPanel.ToggleName): there the full list ran into
        /// the left column (InfoPanelLayoutTests).</summary>
        public const float ShortBelowPx = 1040f;
        public const string ShortClass = "info-short";

        /// <summary>Escape dismisses a reachable selection and its pick, without queuing an order (playtest B9). Covers keep
        /// priority. Focus on one of the panel's controls, which the clear hides, goes to the map stop (as after a press, #41).</summary>
        public bool Escape()
        {
            var hud = panel.parent;
            if (Covered || !panel.enabledInHierarchy || hud?.ClassListContains(ScreenNav.ScreenOpenClass) == true
                || (Selected == null && !ShowsUnit)) return false;
            var focused = panel.panel?.focusController?.focusedElement as VisualElement;
            bool inside = focused != null && (focused == panel || panel.Contains(focused));
            Clear(); clearSelection?.Invoke();
            if (inside) FocusReturn.To(mapFocus);
            return true;
        }

        public (int q, int r)? Selected { get; private set; }

        /// <summary>True while the panel shows a unit's card.</summary>
        public bool ShowsUnit => pioneer != null || (unitMode && selectedUnit() != null);

        /// <summary>The pioneer group whose card the panel shows, or null (the advisor puts it back, GameRoot's keepSelection).</summary>
        public int? Pioneer => pioneer;

        /// <summary>True while a screen covers the panel (a wide screen, or any screen in the two-row HUD): a map click
        /// then changes neither the hex nor the unit shown (#210; units-in-play merge note).</summary>
        public bool Covered
        {
            get
            {
                var hud = panel.parent;
                return hud != null && (hud.ClassListContains(ScreenNav.WideOpenClass)
                    || (hud.ClassListContains(ScreenNav.ScreenOpenClass) && hud.ClassListContains(HudPresenter.TwoRowClass)));
            }
        }

        /// <summary>Shows the hex; ignored while a screen covers the panel, so a map click cannot change a panel the player
        /// cannot see.</summary>
        public void Show(int q, int r)
        {
            DiscardMove();
            if (Covered) return;   // covered (#210)
            if (unitMode || Selected != (q, r)) projects.RemoveFromClassList(GddHexPanel.OpenClass);   // a newly chosen hex starts with its city projects folded
            EndPick();
            pioneer = null; pioneerSession = null; unitMode = false; Selected = (q, r);
            Render();
        }

        /// <summary>Shows the selected unit's card (the unit comes from the selectedUnit callback); ignored while covered.</summary>
        public void ShowUnit()
        {
            DiscardMove();
            if (Covered) return;
            EndPick();
            pioneer = null; pioneerSession = null; unitMode = true; Selected = null; Render();
        }

        /// <summary>Shows nothing selected (the hint), as before the first click; ignored while covered.</summary>
        public void Clear()
        {
            DiscardMove();
            if (Covered) return;
            EndPick();
            pioneer = null; pioneerSession = null; unitMode = false; Selected = null; Render();
        }

        void RequestPioneer(int id)
        {
            if (Covered || !panel.enabledInHierarchy) return;
            if (selectPioneer != null) selectPioneer(id); else ShowPioneer(id);
        }

        public void ShowPioneer(int id)
        {
            if (Covered || !panel.enabledInHierarchy || !bridge.State.Units.TryGetValue(id, out var u) || !Projects.IsPioneer(u.Type)) return;
            EndPick();
            showReachable?.Invoke(System.Array.Empty<(int q, int r)>());
            showRoute?.Invoke(System.Array.Empty<(int q, int r, int turn)>());
            clearSelection?.Invoke();
            unitMode = false; pioneer = id; pioneerSession = bridge.Session; Selected = (u.Q, u.R);
            projects.RemoveFromClassList(GddHexPanel.OpenClass);
            Render();
            if (presses.ByKeyboard) FocusReturn.ToOrWithin(pioneerReturn, mapFocus);
        }

        bool PioneerWorkOrdered(int id) => GddHexPanel.PioneerReturnOrder(bridge, id).WorkOrdered;

        /// <summary>The card's own checks (the card opened in this session, the game not over, the panel neither disabled
        /// nor covered) around the rows' guarded Return.</summary>
        bool CardCanAct => pioneerSession != null && pioneerSession == bridge.Session && bridge.State.GameOver == null
            && panel.enabledInHierarchy && !Covered;

        bool CanReturnPioneer(int id) => CardCanAct && GddHexPanel.PioneerReturnOrder(bridge, id) is (true, false);

        void ReturnPioneer()
        {
            if (pioneer is int id && CardCanAct) GddHexPanel.PioneerReturnOrder(bridge, id, submit: true);
        }

        void RenderPioneerList(int q, int r)
        {
            var groups = bridge.State.Units.Values.Where(u => Projects.IsPioneer(u.Type) && u.Q == q && u.R == r).OrderBy(u => u.Id).ToList();
            if (groups.Count == 0) return;
            pioneerList.style.display = DisplayStyle.Flex;
            foreach (var u in groups)
            {
                var id = u.Id;
                var b = new Button(() => RequestPioneer(id)) { name = "pioneer-select-" + id.ToString(CultureInfo.InvariantCulture),
                    text = text.Format("pioneer.card.open", GddHexPanel.Group(text, bridge.State, id)) };
                b.AddToClassList("pioneer-link"); pioneerList.Add(b);
            }
        }

        /// <summary>The shown unit's sim id and the hexes its Move can go to (Movement.Reachable: the sim's answer, spec section
        /// 2.6), or null when there is no Move to give: no unit card, no sim unit for it, no hex in reach, or the game is over.</summary>
        (int unit, IReadOnlyDictionary<(int Q, int R), int> hexes)? MoveTargets()
        {
            var unit = unitMode ? selectedUnit() : null;
            if (unit == null || !(UnitCard.SimUnit(bridge.Content, bridge.State, unit.Value.Token) is int id)) return null;
            return MoveReach(bridge, id) is { } reach ? (id, reach) : null;
        }

        /// <summary>The hexes sim unit <paramref name="id"/>'s Move can go to now, as its card offers them (Movement.Reachable with
        /// the turn's spent points), or null when its card has no Move: the game is over, Send home is queued for it, or no hex
        /// is in reach. The advisor's move step picks its unit by it too (AdvisorTargets.UnitToShow).</summary>
        public static IReadOnlyDictionary<(int Q, int R), int>? MoveReach(SimBridge bridge, int id)
        {
            if (bridge.State.GameOver != null || bridge.GoingHome(id)) return null;
            var reach = Movement.Reachable(bridge.Content, bridge.State, id, bridge.Spent);
            return reach.Count == 0 ? null : reach;
        }

        /// <summary>The shown unit's sim id, the hexes its attack can name (each unit's hex the sim's Combat.Check takes: the
        /// sim's answer, nothing reckoned here), and the sim's reasons for the hexes it refuses that Combat.Preview gives odds
        /// for (an enemy in the unit's reach: no Arms for it, or the attack it already has). Null when the unit can take no
        /// order (OrderUnit).</summary>
        (int unit, List<(int q, int r)> hexes, List<string> refused)? AttackTargets()
        {
            if (!(OrderUnit() is int id)) return null;
            var c = bridge.Content; var s = bridge.State;
            var hexes = new List<(int q, int r)>(); var refused = new List<string>();
            foreach (var (q, r) in s.Units.Values.Select(u => (u.Q, u.R)).Distinct())
            {
                var why = Combat.Check(c, s, id, q, r, spent: bridge.Spent);
                if (why == null) hexes.Add((q, r));
                else if (Combat.Preview(c, s, id, q, r) != null && !refused.Contains(why)) refused.Add(why);
            }
            return (id, hexes, refused);
        }

        /// <summary>Move's or Attack's button: starts its pick (the sim's hexes lit), or ends it when pressed again; a press
        /// of the other ends the open pick and starts its own. A unit that can move never stays without a pick: RenderLines
        /// opens Move's again at once (a second press of Move keeps its hexes lit; a second press of Attack goes back to Move).</summary>
        void TogglePick(bool attack)
        {
            var again = pick?.attack == attack;
            EndPick();
            (int unit, List<(int q, int r)> hexes)? offer = null;
            if (!again && attack && AttackTargets() is { } a && a.hexes.Count > 0) offer = (a.unit, a.hexes);
            if (!again && !attack && MoveTargets() is { } m) offer = (m.unit, m.hexes.Keys.Select(h => (h.Q, h.R)).ToList());
            if (offer is { } o)
            {
                pick = (o.unit, new HashSet<(int q, int r)>(o.hexes), bridge.Session, bridge.State.Turn, attack, null);
                showReachable?.Invoke(o.hexes);
            }
            Render();
        }

        /// <summary>Q-UI-1's preview of the shown unit's attack on (q, r), for the card: the defenders' names and the sim's
        /// strength ratio (Combat.Preview), then its combat_table row's outcome bands (Combat.Outcomes, capped at what the units can lose: #2553); empty where Preview
        /// has none. Every number is the sim's, read on the state after the turn's committed orders.</summary>
        string Odds(int unit, int q, int r)
        {
            var c = bridge.Content; var s = bridge.State;
            if (!(Combat.Preview(c, s, unit, q, r) is { } p)) return "";
            // The enemy units there (Combat's defenders: a side, not Israel's; Check has made sure there is one).
            var names = s.Units.Values.Where(u => u.Q == q && u.R == r && u.Side != "" && u.Side != Movement.PlayerSide).Select(u => unitName?.Invoke(u.Id)).Where(n => !string.IsNullOrEmpty(n)).ToList();
            var who = names.Count > 0 ? string.Join(", ", names) : string.Format(CultureInfo.InvariantCulture, "{0},{1}", q, r);
            var bands = Combat.Outcomes(c, s, p.Row, unit, q, r).Select(b => text.Format(b.Retreat ? "unit.attack_band_retreat" : "unit.attack_band",
                b.Faces, b.Of, b.AttackersLose, b.DefendersLose));
            return text.Format("unit.attack_odds", who, p.RatioPercent) + "\n" + string.Join("\n", bands);   // one band a line
        }

        /// <summary>The shown unit's sim id, or null: no unit card, no sim unit for it, or the game is over.</summary>
        int? ShownUnit()
        {
            var unit = unitMode ? selectedUnit() : null;
            return unit == null || bridge.State.GameOver != null ? null : UnitCard.SimUnit(bridge.Content, bridge.State, unit.Value.Token);
        }

        /// <summary>The shown unit's sim id, or null when it can take no order: none is shown (ShownUnit), or Send home is
        /// queued for it (the bridge refuses it every other order, spec 3.2).</summary>
        int? OrderUnit() => ShownUnit() is int id && !bridge.GoingHome(id) ? id : (int?)null;

        /// <summary>Fortify, Hold or Send home's button: queues the sim's order for the shown unit (one per unit: another
        /// UnitOrder is replaced). A refusal is raised by the bridge (Refused); the card redraws either way.</summary>
        void GiveOrder(string order)
        {
            if (!(OrderUnit() is int id)) return;
            if (bridge.Queue(new UnitOrder(id, order)) == null) UnitFinished();
            Render();
        }

        void Skip()
        {
            if (!(OrderUnit() is int id)) return;
            var keyboard = presses.ByKeyboard;
            bridge.Skip(id);
            UnitFinished();
            Render();
            // Skip hides its current button before cycling. Keep keyboard navigation on the next card, or End turn.
            if (keyboard) FocusReturn.To(skipButton.style.display.value == DisplayStyle.Flex
                ? skipButton : panel.panel?.visualTree.Q<Button>("end-turn"));
        }

        void UnitFinished()
        {
            if (bridge.NeedsOrders.Count > 0) unitDone?.Invoke();
        }

        /// <summary>#2134: the hexes the move arrow runs through: unit <paramref name="u"/>'s hex, then <paramref name="path"/>
        /// (the sim's Movement.Path or Movement.Route hexes, in order). None for an empty path, and none for a unit that is
        /// not Israel's (owner, 1 Nov 2026: "this needs to be for israeli units only"), by the sim's own rule (Movement:
        /// a side other than Israel's and the legacy loop's "" is the enemy's).</summary>
        internal static IReadOnlyList<(int q, int r)> ArrowHexes(Unit u, IEnumerable<(int Q, int R)> path)
        {
            if (u.Side != "" && u.Side != Movement.PlayerSide) return System.Array.Empty<(int, int)>();
            var hexes = new List<(int q, int r)> { (u.Q, u.R) };
            foreach (var (q, r) in path) hexes.Add((q, r));
            return hexes.Count > 1 ? hexes : (IReadOnlyList<(int q, int r)>)System.Array.Empty<(int, int)>();
        }

        /// <summary>The move arrow to (q, r) in pick <paramref name="p"/>: through Movement.Path's hexes for a lit hex (what
        /// Move spends), else through the go-to's <paramref name="route"/> (RouteTo's); none in an Attack pick.</summary>
        IReadOnlyList<(int q, int r)> ArrowTo(
            (int unit, HashSet<(int q, int r)> hexes, GameSession session, int turn, bool attack, (int q, int r)? touchRoute) p,
            int q, int r, IReadOnlyList<(int q, int r, int turn)> route)
        {
            if (p.attack || !bridge.State.Units.TryGetValue(p.unit, out var u)) return System.Array.Empty<(int, int)>();
            return p.hexes.Contains((q, r))
                ? ArrowHexes(u, Movement.Path(bridge.Content, bridge.State, p.unit, q, r, bridge.Spent))
                : ArrowHexes(u, route.Select(h => (h.q, h.r)));
        }

        void ShowArrow(IReadOnlyList<(int q, int r)> hexes)
        {
            if (hexes.Count == 0 && !arrowDrawn) return;
            arrowDrawn = hexes.Count > 0;
            showArrow?.Invoke(hexes);
        }

        void EndPick()
        {
            DiscardMove();
            if (pick == null) return;
            pick = null;
            shownRoute = null;
            clickThrough = null;
            showReachable?.Invoke(System.Array.Empty<(int q, int r)>());
            showRoute?.Invoke(System.Array.Empty<(int q, int r, int turn)>());
            ShowArrow(System.Array.Empty<(int q, int r)>());
        }

        IReadOnlyList<(int q, int r, int turn)> RouteTo(
            (int unit, HashSet<(int q, int r)> hexes, GameSession session, int turn, bool attack, (int q, int r)? touchRoute) p,
            int q, int r)
        {
            if (p.attack || p.hexes.Contains((q, r))) return System.Array.Empty<(int, int, int)>();
            var now = bridge.Session.Now;
            return Movement.Route(bridge.Content, now.State, p.unit, q, r, now.Spent)
                .Select(h => (h.Q, h.R, h.Turn)).ToList();
        }

        /// <summary>Clears a hovered route (the mouse left the map): once, and so that the same hex shows it again later.</summary>
        public void HideRoute()
        {
            if (shownRoute == null) return;
            shownRoute = null;
            showRoute?.Invoke(System.Array.Empty<(int q, int r, int turn)>());
            ShowArrow(System.Array.Empty<(int q, int r)>());
        }

        /// <summary>Shows the route to the hovered unlit hex while Move is picking, and the move arrow to the hovered lit or
        /// routed hex (#2134); otherwise clears them. GameRoot asks every frame: the same hex again does nothing.</summary>
        public void ShowRouteTo(int q, int r)
        {
            if (shownRoute == (q, r)) return;
            shownRoute = (q, r);
            var route = pick is { } p ? RouteTo(p, q, r) : System.Array.Empty<(int, int, int)>();
            showRoute?.Invoke(route);
            ShowArrow(pick is { } a ? ArrowTo(a, q, r, route) : System.Array.Empty<(int q, int r)>());
        }

        /// <summary>#1617 (player report): the lit hexes of an open Move or Attack pick show through this card (its navy is
        /// translucent), so a click on the card over one of them is meant for the map. True when a pick is open,
        /// <paramref name="picked"/> (the UI element under the pointer) is this card or inside it but not inside one of its
        /// buttons, and the hex under the pointer (<paramref name="hexUnder"/>, asked only then) is one PickTarget takes: lit,
        /// or, for Move, an unlit hex with a route (go-to, #1398). The orders row is not text (#1728): it wraps, so it has gaps
        /// beside and between its buttons, and a press there is a missed button, not a map order. One pixel above Skip, it
        /// ordered a go-to that ended its first leg in Nazareth and opened that capture's decision over the card.</summary>
        public bool TakesClickThrough(VisualElement? picked, System.Func<(int q, int r)?> hexUnder)
        {
            if (!(pick is { } p) || picked == null || (picked != panel && !panel.Contains(picked))) return false;
            for (var e = picked; e != panel; e = e.parent) if (e is Button || e == orders) return false;
            if (!(hexUnder() is { } h)) return false;
            if (clickThrough is { } known && known.hex == h) return known.takes;
            var takes = Takes(p, h.q, h.r);
            clickThrough = (h, takes);
            return takes;
        }

        /// <summary>True when (q, r) is one of a city's settlement hexes (GameRoot.CityAt): a left click there shows the city
        /// even while Move picks (owner, 25 Oct 2026: "Clicking another unit or a city still selects it").</summary>
        public bool IsCity(int q, int r) => GameRoot.CityAt(bridge.Content, bridge.State, q, r) != null;

        // True for the one event dispatch of an Enter or Space on the map stop: the pick it makes (MapFocusPresenter) runs inside it.
        bool stopPress;
        void OnStopSubmit(NavigationSubmitEvent e) => FlagStopPress();
        void OnStopKey(KeyDownEvent e) { if (e.keyCode == UnityEngine.KeyCode.Space) FlagStopPress(); }
        void FlagStopPress() { stopPress = true; mapFocus!.schedule.Execute(() => stopPress = false); }

        void DiscardMove()
        {
            pendingMove = null;
            confirmMove.style.display = DisplayStyle.None;
        }

        /// <summary>A new state (an order, a turn, a load): the hovered hex's route and arrow were read on the old one, so they
        /// are hidden, and GameRoot's next hover draws them from the new one.</summary>
        void OnStateChanged() { DiscardMove(); HideRoute(); Render(); }

        void OnInfoCovered(VisualElement root)
        {
            if (root == panel || root.Contains(panel)) DiscardMove();
        }

        /// <summary>Captures the original mover before ordinary selection changes the card. The returned action
        /// offers its confirmation after that selection. No command is queued until the button is activated.
        /// <paramref name="friendDrawn"/>: another friendly unit's token is drawn on (q, r) (GameRoot asks the unit layer);
        /// a token stands at its anchor, which need not be its sim hex.</summary>
        internal System.Action? PrepareFriendlyMove(int q, int r, bool friendDrawn = false)
        {
            DiscardMove();
            var c = bridge.Content; var s = bridge.State;
            if (!panel.enabledInHierarchy || panel.parent?.ClassListContains(ScreenNav.ScreenOpenClass) == true
                || pick is not { attack: false } p || p.session != bridge.Session || p.turn != s.Turn
                || MoveTargets() is not { } moves || moves.unit != p.unit || !Takes(p, q, r)) return null;
            // Route allows travel towards a currently full hex; this explicit confirmation must not offer it.
            var moving = s.Units[p.unit];
            var limit = c.RulesAt(s.Date).Mvp!.StackLimit!.Value;
            if (s.Units.Values.Count(u => u.Id != p.unit && u.Side == moving.Side && u.Q == q && u.R == r) >= limit) return null;
            var occupants = s.Units.Values.Where(u => u.Q == q && u.R == r).ToList();
            if (occupants.Any(u => u.Side != "" && u.Side != Movement.PlayerSide)
                || (!IsCity(q, r) && !friendDrawn && !occupants.Any(u => u.Id != p.unit))) return null;
            var token = selectedUnit();
            if (token == null) return null;
            var mover = UnitCard.For(token.Value, s, text, c.IsGdd, c, townNamesHe).Title;
            var target = HexPlaces.Of(string.Format(CultureInfo.InvariantCulture, "{0},{1}", q, r), c, text, townNamesHe);
            Command command = p.hexes.Contains((q, r)) ? new Move(p.unit, q, r) : new UnitOrder(p.unit, Movement.GoTo, q, r);
            return () =>
            {
                if (bridge.Session != p.session || bridge.State.Turn != p.turn || !panel.enabledInHierarchy) return;
                pendingMove = (command, p.session, p.turn);
                confirmMove.text = text.Format("unit.confirm_move", mover, target);
                confirmMove.style.display = DisplayStyle.Flex;
                // A pick made at the map stop (its Enter or Space) is a keyboard pick: the offer takes the focus, so Enter
                // takes it and it is not several Tabs away (#2340). A pointer pick gets none, even while the stop still has it.
                if (stopPress) FocusReturn.To(confirmMove);
            };
        }

        void OnConfirmKey(KeyDownEvent e)
        {
            if (e.keyCode != UnityEngine.KeyCode.Space) return;
            // Enter arrives as NavigationSubmit; Space needs the same explicit path as the map stop.
            using var submit = NavigationSubmitEvent.GetPooled();
            submit.target = confirmMove; confirmMove.SendEvent(submit);
            e.StopPropagation();
        }

        void ConfirmMove()
        {
            var proposal = pendingMove;
            var keyboard = presses.ByKeyboard;
            DiscardMove();   // a reentrant Changed, or a second activation, cannot repeat the command
            if (proposal is not { } p || p.session != bridge.Session || p.turn != bridge.State.Turn
                || !panel.enabledInHierarchy || Covered || panel.parent?.ClassListContains(ScreenNav.ScreenOpenClass) == true) return;
            bridge.Queue(p.command);   // existing bridge/sim validation; the button itself confirms a far route too
            Render();
            if (keyboard) FocusReturn.To(mapFocus);
        }

        /// <summary>True while the open pick is a Move's (not an Attack's).</summary>
        public bool MovePicking => pick is { attack: false };
        /// <summary>True while a Move or an Attack pick is open (GameRoot marks the hovered hex then, #2201).</summary>
        public bool Picking => pick != null;

        /// <summary>True when a pick is open and PickTarget would give an order for (q, r): a lit hex, or, for Move, an unlit
        /// hex with a go-to route. A right click (GameRoot.OnHexOrdered) gives one only then.</summary>
        public bool PickTakes(int q, int r) => pick is { } p && Takes(p, q, r);

        (int q, int r, HashSet<(int q, int r)> hexes, PreviewResult now, bool takes)? hoverTakes;

        /// <summary>PickTakes for the hex under the pointer, which GameRoot.MarkHover asks every frame. PickTakes runs RouteTo, a
        /// search of the whole map, so the answer is kept and worked out again only when the hex, the pick (each pick opens with
        /// its own lit set) or the session's preview changes.</summary>
        public bool PickTakesHovered(int q, int r)
        {
            if (!(pick is { } p)) return false;
            var now = bridge.Session.Now;
            if (hoverTakes is { } m && m.q == q && m.r == r && ReferenceEquals(m.hexes, p.hexes) && ReferenceEquals(m.now, now))
                return m.takes;
            var takes = Takes(p, q, r);
            hoverTakes = (q, r, p.hexes, now, takes);
            return takes;
        }

        bool Takes((int unit, HashSet<(int q, int r)> hexes, GameSession session, int turn, bool attack, (int q, int r)? touchRoute) p,
            int q, int r) => p.hexes.Contains((q, r)) || RouteTo(p, q, r).Count > 0;   // RouteTo: PickTarget's go-to rule

        /// <summary>A map click while the player picks a Move's or an Attack's hex (GameRoot.OnHexClicked asks first): a lit
        /// hex gives the Move or attack; an unlit hex with a route gives go-to. Touch previews that route on the first tap and
        /// confirms it on a second tap of the same hex. A hex with no target ends the pick and selects as usual.</summary>
        public bool PickTarget(int q, int r, bool touch = false)
        {
            if (!(pick is { } p)) return false;
            Command? command = p.hexes.Contains((q, r))
                ? p.attack ? new UnitOrder(p.unit, Combat.Attack, q, r) : new Move(p.unit, q, r)
                : null;
            if (command == null && !p.attack)
            {
                var route = RouteTo(p, q, r);
                if (route.Count > 0)
                {
                    if (touch && p.touchRoute != (q, r))
                    {
                        pick = (p.unit, p.hexes, p.session, p.turn, p.attack, (q, r));
                        shownRoute = (q, r);
                        showRoute?.Invoke(route);
                        ShowArrow(ArrowTo(p, q, r, route));
                        Render();
                        return true;
                    }
                    command = new UnitOrder(p.unit, Movement.GoTo, q, r);
                }
            }
            EndPick();
            if (command == null) return false;
            var refused = bridge.Queue(command);   // a refusal is raised by the bridge (Refused)
            if (refused == null && (command is UnitOrder || Movement.PointsLeft(bridge.Content, bridge.State, p.unit, bridge.Spent) == 0))
            {
                UnitFinished();
                Render();
            }
            else if (refused == null && command is Move && MoveTargets() is { }) TogglePick(false);
            else Render();   // a refused Queue raises no Changed
            return true;
        }

        string GoToLine(int unit)
        {
            var now = bridge.Session.Now;
            if (!now.State.Units.TryGetValue(unit, out var u) || u.Order != Movement.GoTo) return "";
            var parts = u.OrderTarget.Split(',');
            if (parts.Length != 2 || !int.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var q)
                || !int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var r)) return "";
            // An operation card's go-to marches round enemy-held places it was not sent to (R-ENEMY-005): count that route.
            var route = Movement.Route(bridge.Content, now.State, unit, q, r, now.Spent, card: now.State.CardOrders.ContainsKey(unit));
            return route.Count == 0 ? "" : text.Format("unit.go_to_given", System.Math.Max(0, route[route.Count - 1].Turn - 1));
        }

        void Build(string action)
        {
            if (Selected == null) return;
            // The bridge reports a refusal itself; the sim enforces legality and the per-turn cap when the turn runs.
            bridge.Queue(new MapAction(action, Selected.Value.q, Selected.Value.r));
        }

        /// <summary>The terrain sign: the biome's glyph on the biome's own map colour (MapBuilder.BiomeColours), hidden when no
        /// hex (or no hex on the map) is selected.</summary>
        void ShowThumb(string? biome)
        {
            var known = biome != null && MapBuilder.BiomeColours.ContainsKey(biome);
            thumb.style.display = known ? DisplayStyle.Flex : DisplayStyle.None;
            thumb.Glyph = known ? "biome-" + biome : "";
            thumb.style.backgroundColor = known ? new StyleColor(MapBuilder.BiomeColours[biome!]) : new StyleColor(StyleKeyword.Null);
        }

        /// <summary>The region lines for a hex on a date: its armistice zone (name, agreement, date) if one is in force, else
        /// its 1948 sub-district; then each armistice line in force through it. <paramref name="jerusalem"/>: the hex is the
        /// Jerusalem settlement's (see ControlText).</summary>
        public static string RegionText(MapRegions regions, UiText text, int q, int r, System.DateTime date, bool jerusalem = false,
            bool? livePlayerHolder = null)
        {
            // Isolate a Hebrew date from the surrounding English fallback names: otherwise the row's
            // LTR paragraph can place its day before its year/month (the mixed-language #558 browser regression).
            string DateText(System.DateTime signed)
            {
                var value = Control1948.DateText(signed, k => text[k]);
                return UiDirection.HasRightToLeft(value) ? "\u2067" + value + "\u2069" : value;
            }
            var parts = new List<string>();
            var zone = regions.ZoneAt(q, r, date);
            var control = ControlText(regions, text, q, r, date, jerusalem, livePlayerHolder);
            // a demilitarized zone is named by its own line below; the control line would only repeat "Demilitarized zone"
            if (control != "" && !(livePlayerHolder == null && zone != null && regions.Control?.At(q, r, date).side == Control1948.Side.Dmz)) parts.Add(control);
            // the sub-district only while the map draws the sub-district bands (before the first sourced line, or with no side data)
            var sub = regions.Subdistricts.ShowsOn(date) && regions.ControlPeriod(date) == null ? regions.Subdistricts.SubdistrictAt(q, r) : null;
            string N(string key, string english) => regions.Names.Pick(key, english, text["ui.lang"] == UiLanguage.Hebrew);
            if (zone != null) parts.Add(text.Format("info.armistice_zone", N("zone." + zone.Key + ".name", zone.Name),
                N("zone." + zone.Key + ".agreement", zone.Agreement), DateText(zone.Signed)));
            else if (sub != null) parts.Add(text.Format("info.region", N("subdistrict." + sub.Key + ".name", sub.Name),
                N("subdistrict." + sub.Key + ".district", sub.District)));
            foreach (var l in regions.LinesAt(q, r, date))
                parts.Add(text.Format(l.LowAccuracy ? "info.armistice_line_low" : "info.armistice_line", N("line." + l.Key + ".name", l.Name),
                    N("line." + l.Key + ".agreement", l.Agreement), DateText(l.Signed)));
            return string.Join("\n", parts);
        }

        /// <summary>Who held the hex on a date (owner rulings 2026-10-14; the colours claim military control, nothing else):
        /// "Held by Israel", "Held by Arab forces (Egypt)" while the period's legend is the 1948 one, the side of the armistice
        /// line where that hex's front has switched; "" with no side data or before the first period. The hex the game names
        /// Jerusalem also holds the Old City, so the data puts it on the Arab side from 11 June 1948; its panel names the
        /// Israeli-held West Jerusalem instead, and the map keeps its colour (#2039, owner ruling 2 Nov 2026: "Keep hexes, fix
        /// the panel").</summary>
        public static string ControlText(MapRegions regions, UiText text, int q, int r, System.DateTime date, bool jerusalem = false,
            bool? livePlayerHolder = null)
        {
            if (livePlayerHolder != null) return text[livePlayerHolder.Value ? "info.control_israel" : "info.control.enemy_held"];
            var p = regions.ControlPeriod(date);
            if (p == null || regions.Control == null) return "";
            var (side, holder) = regions.Control.At(q, r, date);
            bool armistice = p.Legend == Control1948.Legend.Armistice || (p.Legend == Control1948.Legend.Mixed && ArmisticeHere(p, side, holder));
            string By(string plain, string by) => holder == null ? text[plain] : text.Format(by, text["control.holder_" + holder]);
            if (jerusalem && side == Control1948.Side.Arab)
                return text[armistice ? "info.control_side_israel_west_jerusalem" : "info.control_israel_west_jerusalem"];
            // From the start, 15 May 1948 (owner, 3 November 2026, "Israeli-held from 15 May (Recommended)":
            // https://github.com/eyalgolan/statecraft-israel/issues/2039#issuecomment-6610644690): before 11 June the data puts the hex in no-man's land
            // (code m), and the panel still names West Jerusalem as Israeli-held. "On May 14, the British withdrew from the New
            // City of Jerusalem as well, and Operation Pitchfork commenced. Haganah and Irgun forces seized the 'security zones'
            // (most of them in Jewish-owned buildings) without a fight..." (Lapidot, Besiege, ch. "The Arab Legion attacks
            // Jerusalem", https://www.daat.ac.il/DAAT/english/history/lapidot/30.htm, checked 2026-11-03).
            // The hex is no-man's land only in the opening period (control_hexes_1948_49.yaml: legend adjusted, not armistice or mixed),
            // so the armistice wording never applies here.
            if (jerusalem && side == Control1948.Side.NoMansLand) return text["info.control_israel_west_jerusalem"];
            return side switch
            {
                Control1948.Side.Israel => text[armistice ? "info.control_side_israel" : "info.control_israel"],
                Control1948.Side.Arab => armistice ? By("info.control_side_arab", "info.control_side_arab_by") : By("info.control_arab", "info.control_arab_by"),
                Control1948.Side.Contested => text["info.control_contested"],
                Control1948.Side.NoMansLand => text["info.control_no_mans_land"],
                Control1948.Side.Dmz => text["info.control_dmz"],
                Control1948.Side.OffData => "",   // beyond the frontier or the sea: the data says nothing there
                _ => text["info.control_none"],
            };
        }

        /// <summary>In a period where only some fronts have switched, the armistice wording is used for the other side when its
        /// holder's front has switched (Egypt, Jordan, Syria, Lebanon); the Israeli side keeps "held by" (which front faces a
        /// hex is not in the data).</summary>
        static bool ArmisticeHere(Control1948.Period p, Control1948.Side side, string? holder) =>
            side == Control1948.Side.Arab && holder != null && p.Fronts.Contains(holder);

        /// <summary>The "1948 localities" line for a hex: each place's 1948 name, with today's name where Localities.TodayName
        /// allows ("Beisan (today: Beit She'an)"); empty for a hex with none. The Hebrew UI names a place by its name_he, by the
        /// English name where it has none (#899), and drops a today name that is the same Hebrew word (Umm el Fahm and Umm al-Fahm
        /// are both one name in Hebrew: 48 rows that would otherwise show a today name).</summary>
        public static string LocalitiesText(UiText text, IReadOnlyList<Statecraft.Game.Map.Kits.Localities.Place> places)
        {
            if (places.Count == 0) return "";
            bool hebrew = text["ui.lang"] == "he";
            var names = places.Select(p =>
            {
                var name = hebrew && !string.IsNullOrWhiteSpace(p.NameHe) ? p.NameHe! : p.Settlement.Name;
                return Statecraft.Game.Map.Kits.Localities.TodayName(p, hebrew) is string today && today != name
                    ? text.Format("info.locality_today", name, today) : name;
            });
            return text.Format("info.localities", string.Join(", ", names));
        }

        /// <summary>The hex each settlement's own point lies in, by id, picked as the map picks hexes (HexPicker, the rule of
        /// tools/hexgrid.py). It differs from the settlement's hex in settlement_hexes.yaml only where hexgrid.settlement_hex
        /// moved a town in a water hex to its nearest land neighbour (Tiberias, #204), so no content/map field is needed.</summary>
        public static System.Func<string, (int q, int r)> TownHex(IEnumerable<Settlement> settlements, HexFile hexes)
        {
            var at = settlements.ToDictionary(s => s.Id, s => HexPicker.FromLonLat(s.Lon, s.Lat, hexes.bounds, hexes.hex_size_km));
            return id => at[id];
        }

        /// <summary>A settlement's name in the UI's language: its name_he under Hebrew, the English name where it has none.</summary>
        string TownName(string id) =>
            text["ui.lang"] == UiLanguage.Hebrew && townNamesHe != null && townNamesHe.TryGetValue(id, out var he)
                && !string.IsNullOrWhiteSpace(he) ? he : settlementNames[id];

        void ShowLocalities(string line)
        {
            localitiesLabel.text = line;
            localitiesLabel.style.display = line.Length == 0 ? DisplayStyle.None : DisplayStyle.Flex;
        }

        public void Render()
        {
            var focused = presses.ByKeyboard ? panel.focusController?.focusedElement as VisualElement : null;
            var keepPioneerFocus = focused != null && (pioneerList.Contains(focused) || focused == pioneerReturn);
            var focusName = keepPioneerFocus ? focused!.name : null;
            clickThrough = null;
            RenderLines();
            if (keepPioneerFocus)
                FocusReturn.ToOrWithin(panel.Q(focusName), mapFocus, pioneerList);
            panel.EnableInClassList(GddClass, gdd.style.display.value == DisplayStyle.Flex);
            // A fact row shows only when its line has something to say (an empty row would leave a lone sign).
            foreach (var (row, line) in rows)
                row.style.display = string.IsNullOrEmpty(line.text) ? DisplayStyle.None : DisplayStyle.Flex;
        }

        void RenderLines()
        {
            var c = bridge.Content; var s = bridge.State;
            pioneerList.Clear(); pioneerList.style.display = DisplayStyle.None;
            pioneerReturn.style.display = DisplayStyle.None;
            if (pioneer is int selected && (pioneerSession != bridge.Session || !s.Units.TryGetValue(selected, out var live) || !Projects.IsPioneer(live.Type)))
            {
                if (pioneerSession != bridge.Session) Selected = null;
                pioneer = null; pioneerSession = null;
            }
            var camp = campButton; var housing = housingButton;
            var unit = unitMode ? selectedUnit() : null;
            // A unit that has left the state (destroyed or surrendered in its own attack) closes its card; a row with no
            // sim unit at all (an aircraft squadron, the legacy loop) keeps its card.
            if (unit != null && (unit.Value.Token.SimId != null || c.StartingForces.Any(f => f.Row == unit.Value.Token.Unit.Id))
                && UnitCard.SimUnit(c, s, unit.Value.Token) == null)
            {
                unitMode = false;
                unit = null;
            }
            defenceButton.style.display = unit != null ? DisplayStyle.Flex : DisplayStyle.None;
            // A pick read on another turn or session, or for a unit no longer shown, is over: its hexes are not the sim's now.
            if (pick is { } open
                && (open.session != bridge.Session || open.turn != s.Turn || unit == null || UnitCard.SimUnit(c, s, unit.Value.Token) != open.unit))
                EndPick();
            var moves = MoveTargets();
            if (pick is { attack: false } && moves == null) EndPick();   // no Move any more (Send home queued, no points, game over)
            // Civ (owner, 25 Oct 2026: "the unit must move when you click on the fucking lit hex"; spec 2.1, "Selecting a unit
            // lights its one-turn reach"): a shown unit that can move has its Move pick open, with no press of Move. Its
            // button stays (keyboard, the guide); pressing it, or Attack again, comes back here and lights the reach again.
            if (pick == null && moves is { } armed)
            {
                var hexes = armed.hexes.Keys.Select(h => (h.Q, h.R)).ToList();
                pick = (armed.unit, new HashSet<(int q, int r)>(hexes), bridge.Session, s.Turn, false, null);
                showReachable?.Invoke(hexes);
            }
            moveButton.style.display = moves != null ? DisplayStyle.Flex : DisplayStyle.None;
            // Fortify and Hold (#754): each offered only where the sim's Orders.Check takes it; else the sim's reason, in the UI's
            // language (#2045), on Israel's units only (an enemy's card would only repeat "That unit is not yours").
            var orderUnit = OrderUnit(); var shownUnit = ShownUnit();
            var own = unit?.Token.Unit.Side == UnitData.Israel;
            var refusals = new List<string>();
            foreach (var (order, button) in new[] { (Orders.Fortify, fortifyButton), (Orders.Hold, holdButton) })
            {
                var why = orderUnit is int ou ? Orders.Check(c, s, ou, order) : null;
                button.style.display = orderUnit != null && why == null ? DisplayStyle.Flex : DisplayStyle.None;
                if (why != null && own && SimBridge.ReasonText(text, why, c, townNamesHe) is var said && !refusals.Contains(said)) refusals.Add(said);
            }
            // Attack (#754): offered where the sim's Combat.Check takes some hex; its refusals in reach show as the others', in the
            // UI's language too (#2072), each once.
            var attacks = AttackTargets();
            attackButton.style.display = attacks is { } at && at.hexes.Count > 0 ? DisplayStyle.Flex : DisplayStyle.None;
            foreach (var why in attacks?.refused ?? new List<string>())
                if (own && SimBridge.ReasonText(text, why, c, townNamesHe) is var said && !refusals.Contains(said)) refusals.Add(said);
            // Send home (#1309): Demobilization.Check is the complete eligibility rule. A refusal hides the order and adds
            // no card copy; the player sees it only after the relevant armistice makes the order available.
            var sendHomeWhy = orderUnit is int du ? Demobilization.Check(c, s, du, bridge.Session.Now.Attacked) : null;
            sendHomeButton.style.display = orderUnit != null && sendHomeWhy == null ? DisplayStyle.Flex : DisplayStyle.None;
            skipButton.style.display = own == true && orderUnit != null && bridge.NeedsOrders.Contains(orderUnit.Value) ? DisplayStyle.Flex : DisplayStyle.None;
            // Reinforce (#1510, R-TRAIN-004): offered for a unit of Israel's that has lost strength and stands in a city, for
            // the missing strength up to the city's free civilians; a preview of the exact queue a press would leave decides,
            // and its refusal shows as the other orders' do. None while one is queued for the unit.
            reinforce = null;
            var reinforceQueued = shownUnit is int rq && bridge.QueuedLike(new Reinforce(rq, "", 0)) != null;
            if (orderUnit is int ru && own && !reinforceQueued && s.Units.TryGetValue(ru, out var formation) && Training.Room(s, formation) > 0
                && c.SettlementHexes.FirstOrDefault(h => h.Q == formation.Q && h.R == formation.R && s.Cities.ContainsKey(h.Id)) is { } home)
            {
                // At least one group, so a city with no free civilians still shows the sim's refusal; the preview decides.
                var n = System.Math.Max(1, System.Math.Min(Training.Room(s, formation), Training.Available(s, home.Id, bridge.Queued)));
                var press = new Reinforce(ru, home.Id, n);
                var queue = bridge.Queued.ToList();
                var old = bridge.QueuedLike(press);
                var slot = old == null ? -1 : queue.IndexOf(old);
                if (slot >= 0) queue[slot] = press; else queue.Add(press);
                var preview = Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList());
                var refusal = preview.Rejected.FirstOrDefault(x => ReferenceEquals(x.Command, press))?.Reason;
                // #2978 (owner, 9 November 2026: "Enabled + toast (Recommended)"): an Attention shortfall keeps the button and the bridge
                // refuses the press with order.no_attention, so only the sim's Reinforce rule is asked, on the state the queue leaves.
                if (refusal != null && s.GameOver == null && !bridge.CanPay(press))
                    refusal = Training.Reinforce(c, Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(bridge.Queued).ToList()).State, ru, home.Id, n, new List<Cause>());
                if (refusal == null) reinforce = press;
                else if (SimBridge.ReasonText(text, refusal, c, townNamesHe) is var said && !refusals.Contains(said)) refusals.Add(said);
            }
            reinforceButton.style.display = reinforce != null ? DisplayStyle.Flex : DisplayStyle.None;
            if (reinforce is Reinforce offer) reinforceButton.text = text.Format("unit.order.reinforce", offer.Count);
            panel.EnableInClassList(UnitClass, unit != null || pioneer != null);
            if (pioneer is int pid)
            {
                var card = PioneerCard.For(c, s, text, pid, TownName);
                Selected = (s.Units[pid].Q, s.Units[pid].R);
                panel.EnableInClassList("info-empty", false);
                title.text = card.Title; subtitle.text = card.Track;
                settlements.text = card.Location + "\n" + card.Status;
                if (Projects.IsBusy(s, pid)) settlements.text += "\n" + text["pioneer.card.stop_first"];
                region.text = placement.text = "";
                reason.text = PioneerWorkOrdered(pid) ? text["pioneer.card.work_ordered"] : s.GameOver != null ? text["order.game_over"] : "";
                ShowLocalities(""); ShowThumb(null);
                camp.style.display = housing.style.display = DisplayStyle.None;
                pioneerReturn.text = text.Format("pioneer.return", card.Title);
                pioneerReturn.style.display = DisplayStyle.Flex; pioneerReturn.SetEnabled(CanReturnPioneer(pid));
                GddHexPanel.Hide(gdd); GddHexPanel.Hide(projects);
                return;
            }
            if (unit != null)
            {
                var card = UnitCard.For(unit.Value, s, text, c.IsGdd, c, townNamesHe);
                panel.EnableInClassList("info-empty", false);
                // The orders' state under the card's lines: the pick asks for a hex (an attack's with each lit hex's odds, Q-UI-1).
                var order = pick is { attack: true } ap ? string.Join("\n", new[] { text["unit.attack_pick"] }.Concat(ap.hexes.Select(h => Odds(ap.unit, h.q, h.r))).Where(x => x != ""))
                    : pick != null ? text["unit.move_pick"] : "";
                var standing = shownUnit is int su && s.Units.TryGetValue(su, out var shown)
                    ? shown.Order == Orders.Fortify || shown.Order == Orders.Fortifying ? text["unit.fortify_queued"]
                    : shown.Order == Orders.Hold ? text["unit.hold_queued"]
                    : bridge.QueuedLike(new UnitOrder(su, Demobilization.SendHome)) != null ? text["unit.send_home_queued"] : ""
                    : "";
                var points = shownUnit is int pu && s.Units.TryGetValue(pu, out var military) && military.Side == Movement.PlayerSide
                    && Movement.Allowance(c, military) > 0
                    ? text.Format("unit.points", Movement.PointsLeft(c, s, pu, bridge.Spent), Movement.Allowance(c, military)) : "";
                var routeLine = shownUnit is int routeUnit ? GoToLine(routeUnit) : "";
                var lines = new[] { card.Body, points, order, routeLine, standing, reinforceQueued ? text["unit.reinforce_queued"] : "" }.Where(x => x != "");
                title.text = card.Title; subtitle.text = card.Subtitle; settlements.text = string.Join("\n", lines);
                ShowLocalities("");
                region.text = "";   // a unit's card names no sub-district (the last hex's would stay under it)
                placement.text = card.Footer; reason.text = string.Join("\n", new[] { card.Note }.Where(x => x != "").Concat(refusals));
                camp.style.display = DisplayStyle.None; housing.style.display = DisplayStyle.None;
                ShowThumb(null);
                GddHexPanel.Hide(gdd); GddHexPanel.Hide(projects);
                return;
            }
            panel.EnableInClassList("info-empty", Selected == null);
            if (Selected == null)
            {
                title.text = text["info.hint"];
                subtitle.text = region.text = settlements.text = placement.text = reason.text = "";
                ShowLocalities("");
                camp.style.display = DisplayStyle.None; housing.style.display = DisplayStyle.None;
                ShowThumb(null);
                GddHexPanel.Hide(gdd); GddHexPanel.Hide(projects);
                return;
            }
            var (q, r) = Selected.Value;
            var hex = c.HexAt(q, r);
            ShowThumb(hex?.Biome);
            var owned = c.SettlementHexes.Where(x => x.Q == q && x.R == r).Select(x => TownName(x.Id)).ToList();   // strict: every settlement hex has a name (final review M4)
            title.text = owned.Count > 0 ? owned[0] : hex == null ? text["info.off_map"] : text["info.biome." + hex.Biome];
            // The GDD loop's city or designated site (#381); a site's hex has no settlement, so its name is the title.
            // GddHexPanel rebuilds its part on every render, so a button pressed from the keyboard (Train, Assign, Build) went
            // with the focus and the next Tab started again at Cabinet (#41, the keyboard-only turn): the rebuilt button of
            // the same name takes it back. A pointer press leaves no focus (PointerBlur), so it gets none. PressSource reads
            // "keyboard" until the first pointer press in the panel; here that only means a focus nobody pressed stays put.
            // A hex with two sites (the Hula swamp and its field) or two corridor segments gives each part buttons of the same
            // names (site-build-<unit>, site-stop), so the button is looked up inside the part it was in.
            var focused = presses.ByKeyboard ? panel.focusController?.focusedElement as VisualElement : null;
            var keep = focused != null && (gdd.Contains(focused) || projects.Contains(focused)) ? focused.name : null;
            // Which host it was in, read before Render while it is still in the tree: afterwards a button that is gone
            // from gdd would look as though it had been in projects.
            var keptIn = keep == null ? null : gdd.Contains(focused!) ? gdd : projects;
            var part = keep == null ? null : focused!.parent;
            while (part != null && !(part.name is { } pn && (pn.StartsWith("site-part-", System.StringComparison.Ordinal) || pn.StartsWith("corridor-part-", System.StringComparison.Ordinal))))
                part = part.parent;
            var partName = part?.name;
            var siteName = GddHexPanel.Render(gdd, projects, bridge, text, q, r, TownName, RequestPioneer);
            RenderPioneerList(q, r);
            if (!string.IsNullOrEmpty(keep))
            {
                // The rebuilt button of the same name; failing that the nearest control that can take the focus, the
                // part first, then the host it was in, then the other host, and last the map stop (#41, "Still open"
                // item 2). A press can leave its whole part unfocusable: Build disables every Build in the part once one
                // group is ordered, and Stop and a project's Cancel leave a state line and a disabled button.
                var rebuiltPart = partName != null ? gdd.Q(partName) : null;
                var again = rebuiltPart != null ? rebuiltPart.Q(keep) : keptIn!.Q(keep);
                FocusReturn.ToOrWithin(again, mapFocus, rebuiltPart, keptIn, keptIn == gdd ? projects : gdd);
            }
            if (siteName != null && owned.Count == 0) title.text = siteName;
            subtitle.text = hex == null ? "" : text.Format("info.hex", text["info.biome." + hex.Biome], hex.ElevM);   // no debug coordinates in player text (playtest 2, P2-6)
            // The armistice zone in force, else the sub-district on 15 May 1948, and any armistice line in force through the
            // hex, each with its agreement (true lines drawn to the nearest hex edge: the owner's ruling).
            var livePlaces = c.SettlementHexes.Where(x => x.Q == q && x.R == r && s.Settlements.ContainsKey(x.Id))
                .OrderBy(x => x.Id, StringComparer.Ordinal).Select(x => (hex: x, record: s.Settlements[x.Id])).ToList();
            var liveHolder = livePlaces.Select(x => SettlementControlView.PlayerHolds(x.record)).FirstOrDefault(x => x != null);
            region.text = regions == null ? "" : RegionText(regions, text, q, r, s.Date,
                c.SettlementHexes.Any(x => x.Q == q && x.R == r && x.Id == "jerusalem"), liveHolder);
            settlements.text = owned.Count > 0 ? text.Format("info.owned", string.Join(", ", owned)) : "";
            foreach (var x in livePlaces)
                if (SettlementControlView.PanelText(text, TownName(x.hex.Id), x.record) is string detail)
                    settlements.text += (settlements.text == "" ? "" : "\n") + detail;
            // A town in a water hex is drawn on its nearest land hex (Tiberias, in the lake, on the 70 m hex): one line says
            // so, so this hex's elevation does not read as the town's (#204).
            if (townHex != null)
                foreach (var town in c.SettlementHexes.Where(x => x.Q == q && x.R == r && townHex(x.Id) != (q, r)))
                    settlements.text += "\n" + text.Format("info.town_in_water", TownName(town.Id));
            ShowLocalities(localitiesAt == null ? "" : LocalitiesText(text, localitiesAt(q, r)));
            var here = MapActions.At(s, q, r);
            var queued = bridge.QueuedLike(new MapAction(MapActions.Housing, q, r)) as MapAction;
            // The action's name from the UI strings (#929): PlayerText.MapAction is the content's English name (#924's tools
            // test keeps the English string equal to it).
            placement.text = queued != null ? text.Format("info.queued", PlayerText.Capitalised(text["info.map_action." + queued.Action]))
                : here != null ? text.Format("info.built", PlayerText.Capitalised(text["info.map_action." + here.Action]),
                    Control1948.LineDateText(here.Date.ToString("yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), k => text[k])) : "";   // PlayerText.Month is always English (#701)
            // The GDD loop refuses every map action and builds with city projects instead (R-PROJ-004): it has no map-action
            // button and no reason, so its refusal is not asked for (the UI has no words for it).
            string? Refusal(string action)
            {
                // SimBridge.Queue replaces this hex's order in place, or appends it. Preview that exact queue after the committed
                // orders. This costs one preview for each map-action kind the panel considers while the selected-hex panel renders,
                // never from a per-frame callback; an earlier order can make an action that the current state refuses valid.
                var press = new MapAction(action, q, r);
                var old = bridge.QueuedLike(press);
                var queue = bridge.Queued.ToList();
                var slot = old == null ? -1 : queue.IndexOf(old);
                // An action the Attention left cannot pay for keeps its button, as before the preview: the bridge refuses the
                // press and says why (order.no_attention, SimBridge.Queue). Only the rules are asked, on the state the rest of
                // the queue leaves (the press replaces this hex's order, so that order is left out too).
                if (!bridge.CanPay(press))
                {
                    if (slot >= 0) queue.RemoveAt(slot);
                    var rule = MapActions.CheckReason(c, Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList()).State,
                        action, q, r);
                    return rule == null ? null : MapActionReasonText(rule, text);
                }
                if (slot >= 0) queue[slot] = press; else queue.Add(press);
                var preview = Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList());
                var rejected = preview.Rejected.FirstOrDefault(x => ReferenceEquals(x.Command, press));
                if (rejected == null) return null;

                var exact = MapActions.CheckReason(c, preview.State, action, q, r);
                return exact != null ? MapActionReasonText(exact, text)
                    : SimBridge.ReasonText(text, rejected.Reason, c, bridge.TownNamesHe);
            }

            var campWhy = c.IsGdd ? null : Refusal(MapActions.TransitCamp);
            var housingWhy = c.IsGdd ? null : Refusal(MapActions.Housing);
            // eras.yaml systems.map_actions (#58): an action the era does not allow has no button and no reason
            var allowed = c.IsGdd ? new System.Collections.Generic.List<string>() : c.SystemsAt(s.Date).MapActions;
            bool campOn = allowed.Contains(MapActions.TransitCamp), housingOn = allowed.Contains(MapActions.Housing);
            // Both refused for the same reason (in 1948: "...can be placed from 1 January 1950"; or not land, too far, the
            // turn's cap): neither can be ordered on this hex now, so the reason is said once and no button shows (#398).
            bool shared = campOn && housingOn && campWhy != null && campWhy == housingWhy;
            camp.style.display = campOn && !shared ? DisplayStyle.Flex : DisplayStyle.None;
            housing.style.display = housingOn && !shared ? DisplayStyle.Flex : DisplayStyle.None;
            camp.SetEnabled(campOn && campWhy == null); housing.SetEnabled(housingOn && housingWhy == null);
            if (!campOn) campWhy = null;
            if (!housingOn) housingWhy = null;
            // with no rule for an action there is no cost to show (it is disabled with the sim's reason anyway)
            camp.text = c.RulesAt(s.Date).MapActions.TryGetValue(MapActions.TransitCamp, out var cr) ? text.Format("info.build_camp", PlayerText.Money(c, "treasury", cr.Cost)) : text["info.build_camp_plain"];
            housing.text = c.RulesAt(s.Date).MapActions.TryGetValue(MapActions.Housing, out var hr) ? text.Format("info.build_housing", PlayerText.Money(c, "treasury", hr.Cost)) : text["info.build_housing_plain"];
            reason.text = string.Join("\n", new[] { campWhy, housingWhy }.Where(x => x != null).Distinct());   // the sim's reasons, each once
        }

        static string MapActionReasonText(MapActionReason reason, UiText text)
        {
            string Action(string id) => text["info.map_action." + id];
            string Date(string iso) => Control1948.DateText(System.DateTime.ParseExact(iso, "yyyy-MM-dd", CultureInfo.InvariantCulture), k => text[k]);
            var key = "info.map_action_reason." + reason.Kind;
            switch (reason.Kind)
            {
                case "action_from": case "action_until": return text.Format(key, PlayerText.Capitalised(Action(reason.Values[0])), Date(reason.Values[1]));
                case "all_from": return text.Format(key, Date(reason.Values[0]));
                case "too_far": {
                    var distance = int.Parse(reason.Values[0], CultureInfo.InvariantCulture);
                    return text.Format(key + (distance == 1 ? "_one" : "_many"), distance);
                }
                case "turn_cap": return text.Format(key, int.Parse(reason.Values[0], CultureInfo.InvariantCulture));
                case "occupied": return text.Format(key, Action(reason.Values[0]));
                default: return text[key];
            }
        }
    }
}