using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

public class MapWindow : IDisposable
{
    private const float FRONT_Z    = 1.0f;
    private const float BACK_Z     = -1.0f;
    private const float RIGHT_Z    = 0.5f;
    private const float LEFT_Z     = -0.5f;
    private const float Z_TOLERANCE = 0.1f;

    // ══════════════════════════════════════════════════════════════════
    //  TUNABLES — per (jacket size, view). Absolute values, subtracted.
    //  Paste a CalibrationWindow dump into the tuner to overwrite these,
    //  then "Dump seed" to bake the result back into SeedDefaults().
    // ══════════════════════════════════════════════════════════════════

    public class Tune
    {
        public double OffsetX   = 25.0;   // circle cluster shift (SUBTRACTED)
        public double OffsetY   = 50.0;
        public double SizeScale = 1.0;    // circle diameter multiplier
        public double Cluster   = 1.0;    // 1.0 = no clustering
        public double MapZoom   = 1.0;    // see MapFit mode
        public double MapX      = 0.0;    // map nudge, body pixels
        public double MapY      = 0.0;
        public double MapAlpha  = 0.7;
        public double FitMargin = 40.0;   // Form2: panel − 40 (front: −100)
        // cursor calibration
        public double MarginX   = 0.0;
        public double MarginY   = 0.0;

        public Tune Clone() { return (Tune)MemberwiseClone(); }
    }

    /// How the MAP is sized over the body.
    ///   Native546  — Form2 behaviour: 546x546 drawn at native size, centred.
    ///                MapZoom multiplies that. MapZoom 1.0 == today's output.
    ///   FitBody    — CalibrationWindow behaviour: raw map scaled to fit the
    ///                body, then x MapZoom. Use this to verify a dump.
    public enum MapFit { Native546, FitBody }

    private readonly Dictionary<string, Tune> _tunes = new Dictionary<string, Tune>();
    private MapFit _mapFit = MapFit.FitBody;

    private string TuneKey { get { return JacketSize + ":" + _view; } }

    private Tune T
    {
        get
        {
            string k = TuneKey;
            if (!_tunes.ContainsKey(k)) _tunes[k] = new Tune();
            return _tunes[k];
        }
    }

    private void SeedDefaults()
    {
        // ── L ────────────────────────────────────────────────────────
        Seed("L:front",  9.0,   34.0,   2.75, 1.10,  95, 1.060, -4.0, -14.0, 0.70);
        Seed("L:back",  -2.0,   10.0,   1.95, 0.90,  40, 1.090, 2.0, -7.0, 0.70);
        Seed("L:right", 16.0,  -106.0,  1.00, 0.50,  40, 1.010, -32.0, 71.0, 0.70);
        Seed("L:left", -14.0,  -110.0,  0.80, 0.40,  15, 1.050, 0.0, 5.0, 0.70);

        // ── XL ── (converted from the old XL_* deltas: L_offset − delta)
        Seed("XL:front",  7.0,   17.0,  2.30, 1.05, 50, 1.060, -4.0, -14.0, 0.70);
        Seed("XL:back",   5.0,   10.0,  1.95, 0.90,  40, 1.090, 2.0, -7.0, 0.70);
        Seed("XL:right",  10.0, -108.0,  0.80, 0.40,  40, 1.010, -32.0, 71.0, 0.70);
        Seed("XL:left", -4.0, -101.0,  0.80, 0.40,  40, 1.050, 0.0, 5.0, 0.70);
    }

    private void Seed(string key, double ox, double oy, double size, double cluster,
                      double fitMargin, double zoom, double mx, double my, double alpha)
    {
        _tunes[key] = new Tune
        {
            OffsetX = ox, OffsetY = oy, SizeScale = size, Cluster = cluster,
            FitMargin = fitMargin, MapZoom = zoom, MapX = mx, MapY = my, MapAlpha = alpha
        };
    }

    private const int CircleDiameter = 50;
    private const int MapSize = 546;     // Form2: new Bitmap(mapImage, 546, 546)

    // ── glade widgets ──
    [UI] private DrawingArea  MapArea           = null;
    [UI] private Label        MapTitleLabel     = null;
    [UI] private Label        MapCaseLabel      = null;
    [UI] private Button       MapBackButton     = null;
    [UI] private RadioButton  BodySkeletonRadio = null;
    [UI] private RadioButton  BodyJacketRadio   = null;
    [UI] private ComboBoxText JacketSizeCombo   = null;
    [UI] private RadioButton  ViewFrontRadio    = null;
    [UI] private RadioButton  ViewBackRadio     = null;
    [UI] private RadioButton  ViewRightRadio    = null;
    [UI] private RadioButton  ViewLeftRadio     = null;
    [UI] private Switch       ShowNumbersSwitch = null;
    [UI] private RadioButton  SizeLRadio        = null;
    [UI] private RadioButton  SizeXLRadio       = null;

    // NEW — needed only so the map-page CSS can reach them.
    [UI] private Button       SoundTestButton   = null;
    [UI] private Switch       BlackAudioSwitch  = null;
    [UI] private Label        JacketSizeLabel   = null;
    // For new UI
    [UI] private DrawingArea HeartWaveArea   = null;
    [UI] private DrawingArea BreathWaveArea  = null;
    [UI] private Label       MapContactLabel = null;
    [UI] private Label MapPointHeader   = null;
    [UI] private Label HeartWaveHeader  = null;
    [UI] private Label BreathWaveHeader = null;
    [UI] private Button MapExplainButton = null;
    [UI] private Switch       ShowVolumeMapSwitch = null;
    [UI] private CheckButton  HideMetersCheck   = null;

    private readonly Language    _lang;
    // private readonly SoundLooper _sound = new SoundLooper();
    private readonly AudioEngine _sound = new AudioEngine();
    private AudioEngine.Voice _caseVoice, _heart1, _heart2, _breath;
    // Form2's addHeartSoundcheckBox1 / addBreathSoundcheckbox.
    // No glade widgets for these yet — flip them here to test.
    public bool HeartSoundEnabled  = false;
    public bool BreathSoundEnabled = false;

    private readonly List<RegionSound> _regions = new List<RegionSound>();
    private bool _showRegions = true;   // Ctrl+R toggles the colour overlay
    private bool _showRegionLabels = true;
    private CheckButton _hideLabelsCheck;

    private static readonly string[] RegionLabels = {
        "心音1","心音2","気管","気管支","気管支肺胞","右肺胞","左肺胞" };

    // RGB triples — byte arrays, to avoid a System.Drawing dependency.
    private static readonly byte[][] RegionColors = {
        new byte[]{220, 60, 60}, new byte[]{255,140,  0},
        new byte[]{ 60,160,230}, new byte[]{ 40,100,210},
        new byte[]{ 90,190,140}, new byte[]{180,120,220},
        new byte[]{230,200, 70} };

    private int _lastPx = -1, _lastPy = -1;
    private const int TAG_IDLE_MS  = 250;   // Form2's noDataIntervalMs
    private const int WATCHDOG_MS  = 100;

    private DateTime _lastPosLine = DateTime.MinValue;
    private uint _watchdogId;

    // ── state ──
    private Gdk.Pixbuf _body;        // background, native size
    private Gdk.Pixbuf _map;         // 546x546, black keyed — audio reference
    private Gdk.Pixbuf _mapRaw;      // keyed, ORIGINAL size — used by FitBody mode
    private CaseDefinition _case;
    private string _view = "front";
    private readonly ArduinoConnection _arduino;

    // ── live Arduino position ──
    private float _currentX, _currentY, _currentZ;
    private bool  _hasPosition;

    private static readonly Regex FullRx = new Regex(
        @"(\d+(?:\.\d+)?)\(([+-]?\d+(?:\.\d+)?),([+-]?\d+(?:\.\d+)?),([+-]?\d+(?:\.\d+)?)\)",
        RegexOptions.Compiled);
    private static readonly Regex AvgRx = new Regex(
        @"Avg(\d+(?:\.\d+)?)\(([+-]?\d+(?:\.\d+)?),([+-]?\d+(?:\.\d+)?),([+-]?\d+(?:\.\d+)?)\)",
        RegexOptions.Compiled);
    private string _bodyPath = "(none)";
    private string _mapPath  = "(none)";
    

    public event EventHandler BackRequested;

    private JacketData _jacket;
    private List<float> _highlighted = new List<float>();
    private static bool _listed;

    // ── tuner state ──
    private Window   _tuner;
    private TextView _tunerOut;
    private Label    _tunerInfo, _tunerKey;
    private readonly Dictionary<string, SpinButton> _spins = new Dictionary<string, SpinButton>();
    private CheckButton _tShowBody, _tShowMap, _tShowCircles, _tGuides;
    private ComboBoxText _tFitCombo;
    private bool _building;
    private bool _keysHooked;

    private bool ShowBody    { get { return _tShowBody    == null || _tShowBody.Active; } }
    private bool ShowMap     { get { return (_tShowMap == null || _tShowMap.Active)
                                        && (ShowVolumeMapSwitch == null || ShowVolumeMapSwitch.Active); } }
    private bool ShowCircles { get { return (_tShowCircles == null || _tShowCircles.Active)
                                        && (ShowNumbersSwitch == null || ShowNumbersSwitch.Active); } }
    private bool ShowGuides  { get { return _tGuides != null && _tGuides.Active; } }
    
    // ── debug state ──
    private uint _debugSweepId;
    private int  _debugLevel;
    private int  _debugStep = 5;

    // ── waveform boxes ──
    private WaveData _heartWave, _breathWave;
    private double _heartCursor = -1, _breathCursor = -1;   // 0..1, -1 = hidden
    private bool _audible;                                  // true while a tag has the sound unmuted
    private uint _waveTimerId;
    private readonly System.Diagnostics.Stopwatch _playClock = new System.Diagnostics.Stopwatch();
    private bool _heartLive, _breathLive;   // is this box's sound actually playing?

    // ══════════════════════════════════════════════════════════════════

    public MapWindow(Builder builder, ArduinoConnection arduino, Language language)
    {
        builder.Autoconnect(this);
        _arduino = arduino;

        SeedDefaults();
        LoadCalibFile(false);           // calib.txt overrides seeds if present

        _jacket = JacketData.Load();
        Logger.Write("jacket", "folder: " + _jacket.Folder + " loaded=" + _jacket.Loaded);

        if (MapArea == null || MapTitleLabel == null || MapBackButton == null)
            throw new InvalidOperationException(
                "Glade id mismatch — MapArea=" + (MapArea != null) +
                " MapTitleLabel=" + (MapTitleLabel != null) +
                " MapBackButton=" + (MapBackButton != null));

        _lang = language;

        // ── styling hooks ─────────────────────────────────────────────
        // GTK3's Builder does NOT copy the glade id into the CSS name, so
        // #Id selectors match nothing unless we assign Name ourselves. The
        // shared "mappage" class then lets touch_friendly.css restyle this
        // page without touching the splash / sound / settings pages.
        Widget[] mapWidgets = {
            MapArea, MapTitleLabel, MapCaseLabel, MapBackButton,
            BodySkeletonRadio, BodyJacketRadio, JacketSizeCombo, JacketSizeLabel,
            ViewFrontRadio, ViewBackRadio, ViewRightRadio, ViewLeftRadio,
            ShowNumbersSwitch, BlackAudioSwitch, SoundTestButton, ShowVolumeMapSwitch,
            SizeLRadio, SizeXLRadio, HideMetersCheck
        };
        foreach (var w in mapWidgets)
            if (w != null) w.StyleContext.AddClass("mappage");

        AddClass(MapTitleLabel,    "map-title");
        AddClass(MapContactLabel,  "contact-chip");
        AddClass(MapPointHeader,   "panel-header");
        AddClass(HeartWaveHeader,  "panel-header");
        AddClass(BreathWaveHeader, "panel-header");
        AddClass(MapExplainButton, "nav-button");
        AddClass(MapBackButton,    "nav-button");

        SetName(MapArea,           "MapArea");
        SetName(MapTitleLabel,     "MapTitleLabel");
        SetName(MapCaseLabel,      "MapCaseLabel");
        SetName(BodySkeletonRadio, "BodySkeletonRadio");
        SetName(BodyJacketRadio,   "BodyJacketRadio");
        SetName(JacketSizeCombo,   "JacketSizeCombo");
        SetName(JacketSizeLabel,   "JacketSizeLabel");
        SetName(ViewFrontRadio,    "ViewFrontRadio");
        SetName(ViewBackRadio,     "ViewBackRadio");
        SetName(ViewRightRadio,    "ViewRightRadio");
        SetName(ViewLeftRadio,     "ViewLeftRadio");
        SetName(SoundTestButton,   "SoundTestButton");
        SetName(HideMetersCheck, "HideMetersCheck");
        SetName(ShowNumbersSwitch,   "ShowNumbersSwitch");
        SetName(BlackAudioSwitch,    "BlackAudioSwitch");
        SetName(ShowVolumeMapSwitch, "ShowVolumeMapSwitch");
        SetName(SizeLRadio,          "SizeLRadio");
        SetName(SizeXLRadio,         "SizeXLRadio");

        // Register() assigns Name itself and pulls the label from the CSVs.
        _lang.Register("MapBackButton", MapBackButton);

        // ── signals ───────────────────────────────────────────────────
        MapArea.Drawn         += OnMapDrawn;
        // HeartWaveArea.Drawn  += (o, a) => DrawWave(a, HeartWaveArea,  0.86, 0.22, 0.20);
        // BreathWaveArea.Drawn += (o, a) => DrawWave(a, BreathWaveArea, 0.21, 0.57, 0.78);
        HeartWaveArea.Drawn  += (o, a) => DrawWave(a, HeartWaveArea,  _heartWave,  _heartCursor,  0.86, 0.22, 0.20);
        BreathWaveArea.Drawn += (o, a) => DrawWave(a, BreathWaveArea, _breathWave, _breathCursor, 0.21, 0.57, 0.78);
        MapBackButton.Clicked += OnBackClicked;

        BodySkeletonRadio.Toggled += (s, e) => { if (BodySkeletonRadio.Active) ReloadBody(); };
        BodyJacketRadio.Toggled   += (s, e) => { if (BodyJacketRadio.Active)   ReloadBody(); };
        JacketSizeCombo.Changed   += (s, e) => { ReloadBody(); SyncSpins(); MapArea.QueueDraw(); };

        MapExplainButton.Clicked += (s, e) => { /* open 解説 */ };

        HookView(ViewFrontRadio, "front");
        HookView(ViewBackRadio,  "back");
        HookView(ViewRightRadio, "right");
        HookView(ViewLeftRadio,  "left");

        // HideCircleCheck.Toggled += (s, e) => MapArea.QueueDraw();
        if (ShowNumbersSwitch != null)
            ShowNumbersSwitch.AddNotification("active", (o, a) => MapArea.QueueDraw());
        if (ShowVolumeMapSwitch != null)
            ShowVolumeMapSwitch.AddNotification("active", (o, a) => MapArea.QueueDraw());
        if (BlackAudioSwitch != null)
            BlackAudioSwitch.AddNotification("active", (o, a) =>
            {
                _lastPx = _lastPy = -1;
                UpdateAudioFromPosition();
                Logger.Write("audio", "black-pixel audio " + (BlackAudioEnabled ? "ON" : "OFF"));
            });

        // L / XL buttons drive the hidden JacketSizeCombo, so JacketSize keeps working
        if (SizeLRadio != null)
            SizeLRadio.Toggled  += (s, e) => { if (SizeLRadio.Active)  JacketSizeCombo.Active = 0; };
        if (SizeXLRadio != null)
            SizeXLRadio.Toggled += (s, e) => { if (SizeXLRadio.Active) JacketSizeCombo.Active = 1; };

        // Ctrl+T anywhere, or Ctrl+double-click on the map, opens the tuner.
        MapArea.Realized += (s, e) => HookKeys();
        MapArea.AddEvents((int)Gdk.EventMask.ButtonPressMask);
        MapArea.ButtonPressEvent += (o, a) =>
        {
            if (a.Event.Type == Gdk.EventType.TwoButtonPress &&
                (a.Event.State & Gdk.ModifierType.ControlMask) != 0)
                ToggleTuner();
        };

        _sound.Log += msg => Logger.Write("sound", msg);
        if (!_sound.Init())
            Logger.Write("sound", "OpenAL init failed — session will be silent");   
        // _sound.Init();
        // NEW — live position feed
        _arduino.LineReceived      += OnArduinoLine;
        _watchdogId = GLib.Timeout.Add(WATCHDOG_MS, OnTagWatchdog);
        _arduino.ConnectionChanged += OnArduinoConnectionChanged;
    }

    /// Null-safe: SoundTestButton / BlackAudioCheck / JacketSizeLabel are
    /// optional — an older glade without them must still load.
    private static void SetName(Widget w, string name)
    {
        if (w != null) w.Name = name;
    }

    private static void AddClass(Widget w, string cls)
    {
        if (w != null) w.StyleContext.AddClass(cls);
    }

    private void HookKeys()
    {
        if (_keysHooked) return;
        var top = MapArea.Toplevel as Window;
        if (top == null) return;
        _keysHooked = true;

        top.KeyPressEvent += (o, a) =>
        {
            if ((a.Event.State & Gdk.ModifierType.ControlMask) == 0) return;
            if (a.Event.Key == Gdk.Key.t || a.Event.Key == Gdk.Key.T)
                ToggleTuner();
            else if (a.Event.Key == Gdk.Key.r || a.Event.Key == Gdk.Key.R)
            {
                _showRegions = !_showRegions;
                MapArea.QueueDraw();
            }
        };
    }

    private void HookView(RadioButton rb, string view)
    {
        if (rb == null) return;
        rb.Toggled += (s, e) => { if (rb.Active) SetView(view); };
    }

    // ══════════════════════════════════════════════════════════════════
    //  ARDUINO POSITION
    // ══════════════════════════════════════════════════════════════════

   private void OnArduinoLine(string line)
    {
        var fullMatches = FullRx.Matches(line);
        var avgMatch    = AvgRx.Match(line);

        float avgChipNumber = -1f;
        float x = 0, y = 0, z = 0;
        bool havePosition = false;

        _highlighted.Clear();

         Logger.Write("chip", "raw='" + line + "'  fullMatches=" + fullMatches.Count +   // NEW
                          "  avgMatch=" + avgMatch.Success);  

        if (AppMode.Legacy)
        {
            // For legacy mode, we need to process the position first before applying audio
            double bx, by;
            if (!_hasPosition || !TryGetCursorBodyPoint(out bx, out by))
            {
                MuteRegions();
                return;
            }
            
            if (_case != null) ApplyRegionAudio(bx, by);
            return;                                   // skip the single-voice path entirely
        }        

        if (avgMatch.Success)
        {
            avgChipNumber = ParseF(avgMatch.Groups[1].Value);
            x = ParseF(avgMatch.Groups[2].Value);
            y = ParseF(avgMatch.Groups[3].Value);
            z = ParseF(avgMatch.Groups[4].Value);
            havePosition = true;
        }

        if (fullMatches.Count > 0)
        {
            foreach (Match m in fullMatches)
            {
                float chipNumber = ParseF(m.Groups[1].Value);
                if (Math.Abs(chipNumber - avgChipNumber) < 0.001f) continue; // that's the Avg entry itself
                _highlighted.Add(chipNumber);   // feeds the existing yellow-fill in DrawOneCircle
            }

            if (!havePosition)
            {
                var first = fullMatches[0];
                x = ParseF(first.Groups[2].Value);
                y = ParseF(first.Groups[3].Value);
                z = ParseF(first.Groups[4].Value);
                havePosition = true;
            }
        }

        // if (!havePosition) return;   // status/mode line, not a coordinate line
        if (!havePosition)
        {
            // Reader explicitly says there's nothing there -> clear now, don't
            // wait out the watchdog. Heartbeats and mode echoes fall through and
            // let the watchdog decide, so they can't chop the audio mid-touch.
            if (line.Contains("All tags cleared") || line.Contains("Tags detected: 0"))
                ClearPosition();
            return;
        }

        _lastPosLine = DateTime.Now;        // NEW — feeds the watchdog

        _currentX = x;
        _currentY = y;
        _currentZ = z;
        _hasPosition = _highlighted.Count > 0;   // NEW — marker only shows when a chip is actually on the target

        string newView = DetermineViewFromZ(z);
        if (newView != _view)
            SwitchViewFromArduino(newView);

        MapArea.QueueDraw();
        UpdateAudioFromPosition();  
    }  

    private static float ParseF(string s)
    {
        return float.Parse(s, CultureInfo.InvariantCulture);
    }

    private void OnArduinoConnectionChanged(bool connected)
    {
        if (!connected)
        {
            _hasPosition = false;
            MuteVoices();
            MapArea.QueueDraw();
        }
    }

    private void UpdateAudioFromPosition()
    {
        if (_caseVoice == null) return;

        double bx, by;
        if (!_hasPosition || !TryGetCursorBodyPoint(out bx, out by))
        {
            MuteVoices();
            return;
        }

        int px, py;
        if (!BodyToMapPixel(bx, by, out px, out py))
        {
            MuteVoices();              // probe is off the map rectangle
            return;
        }

        // Serial lines arrive far faster than the tag moves a whole pixel.
        if (px == _lastPx && py == _lastPy) return;
        _lastPx = px; _lastPy = py;

        var res = MapAudio.Analyze(AudioSourcePixbuf, px, py, BlackAudioEnabled);

        Logger.Write("audio", "map(" + px + "," + py + ") rgba(" +
                    res.R + "," + res.G + "," + res.B + "," + res.A + ") -> " +
                    res.Region + " level=" + res.Level);

        if (!res.Play) { MuteVoices(); return; }

        ApplyLevel(res.Level);
    }

    /// Form2's AdjustAudioVolumes — same /3 and /4 divisors, so the mix
    /// matches the Windows build.
    private void ApplyLevel(int level)
    {
        _audible = level > 0;
        _sound.SetGain(_caseVoice, AudioEngine.VolumeCurve(level));

        if (_heart1 != null) _sound.SetGain(_heart1, AudioEngine.VolumeCurve(level / 3));
        if (_heart2 != null) _sound.SetGain(_heart2, AudioEngine.VolumeCurve(level / 3));
        if (_breath != null) _sound.SetGain(_breath, AudioEngine.VolumeCurve(level / 4));
    }

    private void MuteVoices()
    {
        _audible = false;
        _lastPx = _lastPy = -1; 
        _sound.SetGain(_caseVoice, 0f);
        if (_heart1 != null) _sound.SetGain(_heart1, 0f);
        if (_heart2 != null) _sound.SetGain(_heart2, 0f);
        if (_breath != null) _sound.SetGain(_breath, 0f);
    }
    /// Form2's TimestampTimer_Tick. The reader going quiet is not an event —
    /// nothing arrives to tell us the tag left, so we have to notice ourselves.
    private bool OnTagWatchdog()
    {
        if (_hasPosition &&
            (DateTime.Now - _lastPosLine).TotalMilliseconds >= TAG_IDLE_MS)
        {
            Logger.Write("chip", "tag idle " + TAG_IDLE_MS + "ms — clearing");
            ClearPosition();
        }
        return true;                    // keep the timer alive
    }

    private void ClearPosition()
    {
        _highlighted.Clear();
        _hasPosition = false;
        _currentX = _currentY = _currentZ = 0f;
        MuteVoices();
        MapArea.QueueDraw();
    }

    /// Maps the live Arduino (x,y) into BODY PIXELS using the same
    /// bounding-box scale/offset DrawCircles uses, so the cursor tracks
    /// the same coordinate frame as the numbered circles.
    private bool TryGetCursorBodyPoint(out double bx, out double by)
    {
        bx = by = 0;
        if (!_hasPosition || _jacket == null || _body == null) return false;

        var circles = _jacket.Get(JacketSize, _view);
        if (circles.Count == 0) return false;

        var t = T;
        int panelW = _body.Width;
        int panelH = _body.Height;

        float minX = float.MaxValue, maxX = float.MinValue;
        float minY = float.MaxValue, maxY = float.MinValue;
        foreach (var c in circles)
        {
            if (c.X < minX) minX = c.X;
            if (c.X > maxX) maxX = c.X;
            if (c.Y < minY) minY = c.Y;
            if (c.Y > maxY) maxY = c.Y;
        }

        double availW = panelW - t.FitMargin, availH = panelH - t.FitMargin;
        const double circleRadius = 2.1 / 2;
        double dataW = (maxX - minX) + circleRadius * 2;
        double dataH = (maxY - minY) + circleRadius * 2;
        if (dataW <= 0 || dataH <= 0) return false;

        double scale = Math.Min(availW / dataW, availH / dataH) * 0.9;

        double offsetX = (panelW - (maxX - minX) * scale) / 2 - minX * scale;
        double offsetY = (panelH - (maxY - minY) * scale) / 2 - minY * scale;

        // Form2: raw Arduino units run ~50x smaller than CSV/circle units.
        bx = (_currentX * 50 + t.MarginX) * scale + offsetX;
        by = (_currentY * 50 + t.MarginY) * scale + offsetY;

        if (JacketSize == "XL" && (_view == "front" || _view == "right" || _view == "left"))
        {
            double centerX = panelW / 2.0;
            double offsetFromCenter = bx - centerX;
            bx = centerX - offsetFromCenter;
        }

        return true;
    }

    private void DrawCursor(Cairo.Context cr, double x, double y)
    {
        const double size = 14;

        cr.NewPath();
        // cr.SetSourceRGBA(1.0, 0.0, 0.0, 0.9); //red not that visible
        cr.SetSourceRGBA(1.0, 0.85, 0.0, 0.95); //yellow
        cr.LineWidth = 2.5;
        cr.MoveTo(x - size, y);
        cr.LineTo(x + size, y);
        cr.MoveTo(x, y - size);
        cr.LineTo(x, y + size);
        cr.Stroke();

        cr.NewPath();
        cr.Arc(x, y, 5, 0, 2 * Math.PI);
        cr.Stroke();
    }

    private void DrawCoordHud(Cairo.Context cr, int w, int h)
    {
        string text = string.Format(CultureInfo.InvariantCulture,
            "X={0:F2}  Y={1:F2}  Z={2:F2}  view={3}", _currentX, _currentY, _currentZ, _view);

        cr.SelectFontFace("Monospace", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold);
        cr.SetFontSize(13);
        var ext = cr.TextExtents(text);

        double pad = 6, bx = 8, by = 8;
        double bw = ext.Width + pad * 2, bh = ext.Height + pad * 2;

        cr.NewPath();
        cr.Rectangle(bx, by, bw, bh);
        cr.SetSourceRGBA(0, 0, 0, 0.55);
        cr.Fill();

        cr.MoveTo(bx + pad - ext.XBearing, by + pad + ext.Height);
        cr.SetSourceRGBA(1, 1, 1, 1);
        cr.ShowText(text);
    }

    // ── public API ─────────────────────────────────────────────────────

    public void ShowCase(CaseDefinition def, string title)
    {
        _case = def;
        _view = "front";
        if (ViewFrontRadio != null) ViewFrontRadio.Active = true;

        MapTitleLabel.Text = title ?? "";
        MapCaseLabel.Text  = title ?? "-";

        DisposePixbufs();
        _body = LoadBody();
        
        if (AppMode.Legacy)
        {
            LoadRegions(def);
            var overlay = BuildRegionOverlay(MapSize, MapSize);
            _mapRaw  = overlay != null ? overlay.Copy() : null;
            _map     = overlay;
            _mapPath = "(7 regions composite)";
        }
        else
        {
            // _map  = LoadMap(MapPathForView());
            _map = LoadMap(MapPathForView(), MapNeedsMirror());
        }

        MapArea.QueueDraw();
        UpdateTunerInfo();
        
        if (!AppMode.Legacy)
        {
            _caseVoice = _sound.Warm(SoundWindow.AppFile(def.SoundPath));

            if (HeartSoundEnabled)
            {
                _heart1 = _sound.Warm(AppMode.ResolveSound("SND100.wav"));
                _heart2 = _sound.Warm(AppMode.ResolveSound("SND101.wav"));
            }
            if (BreathSoundEnabled)
                _breath = _sound.Warm(AppMode.ResolveSound("SND200.wav"));


            _sound.MuteAll();               // everything loops silently until a tag lands
        }

         LoadWaveforms(def);
        _playClock.Restart();          // voices were just warmed -> loop position 0
        StartWaveTimer();
        _lastPx = _lastPy = -1;
        StartDebugSweep(); 

        Logger.Write("case", "open: " + title + " | map=" + MapPathForView());
    }

    public void StopSession()
    {
        StopDebugSweep();    
        MuteRegions();                  // NEW: stop legacy audio too
        // _sound.Stop();
        _sound.MuteAll();               // keep voices warm — re-entry is instant
        StopWaveTimer();
        _lastPx = _lastPy = -1;
        Logger.Write("case", "closed");
    }

    /// Raw MAP pixbuf — Form2's originalMapForAudio. Never composited.
    public Gdk.Pixbuf AudioReference { get { return _map; } }

    /// Body-pixel point -> pixel index inside the MAP that is drawn there.
    /// Stays correct in both fit modes, so audio sampling tracks the display.
    public bool BodyToMapPixel(double bx, double by, out int px, out int py)
    {
        px = py = 0;
        double mx, my, mw, mh;
        if (!MapRect(out mx, out my, out mw, out mh)) return false;
        if (mw <= 0 || mh <= 0) return false;

        double u = (bx - mx) / mw, v = (by - my) / mh;
        if (u < 0 || u >= 1 || v < 0 || v >= 1) return false;

        Gdk.Pixbuf src = (_mapFit == MapFit.FitBody && _mapRaw != null) ? _mapRaw : _map;
        if (src == null) return false;

        px = (int)(u * src.Width);
        py = (int)(v * src.Height);
        return true;
    }

    // ── the paint ──────────────────────────────────────────────────────

    /// Where the MAP lands, in BODY PIXELS. Single source of truth for both
    /// the draw and BodyToMapPixel.
    private bool MapRect(out double mx, out double my, out double mw, out double mh)
    {
        mx = my = mw = mh = 0;
        if (_body == null) return false;

        var t = T;

        if (_mapFit == MapFit.FitBody)
        {
            if (_mapRaw == null) return false;
            double ms = Math.Min((double)_body.Width  / _mapRaw.Width,
                                 (double)_body.Height / _mapRaw.Height) * t.MapZoom;
            mw = _mapRaw.Width * ms;
            mh = _mapRaw.Height * ms;
        }
        else
        {
            if (_map == null) return false;
            mw = _map.Width  * t.MapZoom;
            mh = _map.Height * t.MapZoom;
        }

        mx = (_body.Width  - mw) / 2.0 + t.MapX;
        my = (_body.Height - mh) / 2.0 + t.MapY;
        return true;
    }

    private void OnMapDrawn(object o, DrawnArgs args)
    {
        var cr = args.Cr;
        int w  = MapArea.AllocatedWidth;
        int h  = MapArea.AllocatedHeight;

        cr.SetSourceRGB(1, 1, 1);
        cr.Rectangle(0, 0, w, h);
        cr.Fill();

        if (_body == null) { args.RetVal = true; return; }

        // ONE scale factor, derived from the body — this is what keeps the
        // overlay registered. Zoom-to-fit, same as PictureBoxSizeMode.Zoom.
        double s  = Math.Min((double)w / _body.Width, (double)h / _body.Height);
        double ox = (w - _body.Width  * s) / 2.0;
        double oy = (h - _body.Height * s) / 2.0;

        cr.Save();
        cr.Translate(ox, oy);
        cr.Scale(s, s);                 // everything below is in BODY PIXELS

        if (ShowBody)
        {
            Gdk.CairoHelper.SetSourcePixbuf(cr, _body, 0, 0);
            cr.Paint();
        }

        double mx, my, mw, mh;
        bool haveMap = MapRect(out mx, out my, out mw, out mh);

        if (haveMap && ShowMap)
        {
            Gdk.Pixbuf src = (_mapFit == MapFit.FitBody && _mapRaw != null) ? _mapRaw : _map;
            double sx = mw / src.Width, sy = mh / src.Height;

            cr.Save();
            cr.Translate(mx, my);
            cr.Scale(sx, sy);
            Gdk.CairoHelper.SetSourcePixbuf(cr, src, 0, 0);
            // Guard the existing map-overlay draw with (!AppMode.Legacy || _showRegions)
            if (!AppMode.Legacy || _showRegions)
                cr.PaintWithAlpha(T.MapAlpha);
            cr.Restore();
        }

        if (ShowCircles)
            DrawCircles(cr);            // body-pixel coords; transform does the rest

        double cx, cy;
        if (TryGetCursorBodyPoint(out cx, out cy))
            DrawCursor(cr, cx, cy);

        if (ShowGuides)
        {
            if (haveMap)
            {
                cr.NewPath();
                cr.SetSourceRGBA(1, 0, 0, 0.6);
                cr.LineWidth = 1.5;
                cr.Rectangle(mx, my, mw, mh);
                cr.Stroke();
            }

            cr.NewPath();
            cr.SetSourceRGBA(0, 0, 1, 0.35);
            cr.LineWidth = 1.0;
            cr.MoveTo(_body.Width / 2.0, 0);
            cr.LineTo(_body.Width / 2.0, _body.Height);
            cr.MoveTo(0, _body.Height / 2.0);
            cr.LineTo(_body.Width, _body.Height / 2.0);
            cr.Stroke();
        }

        cr.Restore();
        // NEW — screen-space readout, unaffected by the body scale/translate above
        if (_hasPosition)
            DrawCoordHud(cr, w, h); //show the coordinate on top of the image

        // At the very end, before args.RetVal = true, add:
        bool hideMeters = HideMetersCheck == null || HideMetersCheck.Active;
        if (AppMode.Legacy && _regions.Count > 0 && !hideMeters)
            DrawRegionMeters(cr);

        args.RetVal = true;
    }

    private void DrawCircles(Cairo.Context cr)
    {
        if (_jacket == null || _body == null) return;

        var circles = _jacket.Get(JacketSize, _view);
        if (circles.Count == 0) return;

        var t = T;

        // "panel" is the body pixbuf — Form2's gridPictureBox was 546x546
        int panelW = _body.Width;
        int panelH = _body.Height;

        float minX = float.MaxValue, maxX = float.MinValue;
        float minY = float.MaxValue, maxY = float.MinValue;
        foreach (var c in circles)
        {
            if (c.X < minX) minX = c.X;
            if (c.X > maxX) maxX = c.X;
            if (c.Y < minY) minY = c.Y;
            if (c.Y > maxY) maxY = c.Y;
        }

        double availW = panelW - t.FitMargin, availH = panelH - t.FitMargin;
        const double circleRadius = 2.1 / 2;
        double dataW = (maxX - minX) + circleRadius * 2;
        double dataH = (maxY - minY) + circleRadius * 2;
        if (dataW <= 0 || dataH <= 0) return;

        double scale = Math.Min(availW / dataW, availH / dataH) * 0.9;

        double offsetX = (panelW - (maxX - minX) * scale) / 2 - minX * scale;
        double offsetY = (panelH - (maxY - minY) * scale) / 2 - minY * scale;

        // cluster centroid, in already-offset space
        double avgX = 0, avgY = 0;
        foreach (var c in circles)
        {
            avgX += c.X * scale + offsetX - t.OffsetX;
            avgY += c.Y * scale + offsetY - t.OffsetY;
        }
        avgX /= circles.Count;
        avgY /= circles.Count;

        cr.SelectFontFace("Sans", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold);

        foreach (var c in circles)
            DrawOneCircle(cr, c, t, scale, offsetX, offsetY, avgX, avgY);
    }

    private void DrawOneCircle(Cairo.Context cr, Circle c, Tune t,
                               double scale, double ox, double oy,
                               double avgX, double avgY)
    {
        double x = c.X * scale + ox - t.OffsetX;
        double y = c.Y * scale + oy - t.OffsetY;

        if (Math.Abs(t.Cluster - 1.0) > 0.001)
        {
            x = avgX + (x - avgX) * t.Cluster;
            y = avgY + (y - avgY) * t.Cluster;
        }

        double d = CircleDiameter * scale * t.SizeScale;
        double r = d / 2;

        bool hot = false;
        foreach (float n in _highlighted)
            if (Math.Abs(c.Number - n) < 0.001f) { hot = true; break; }

        cr.NewPath();
        cr.Arc(x, y, r, 0, 2 * Math.PI);

        if (hot)
        {
            cr.SetSourceRGBA(1.0, 0.922, 0.231, 1.0);   // Color.FromArgb(255,235,59)
            cr.FillPreserve();
        }

        cr.SetSourceRGBA(0.5, 0.5, 0.5, 1.0);           // BorderColor = Gray
        cr.LineWidth = Math.Max(1.0, scale);
        cr.Stroke();

        string text = (c.Number % 1 == 0)
            ? ((int)c.Number).ToString()
            : c.Number.ToString("F1", CultureInfo.InvariantCulture);

        cr.SetFontSize(Math.Max(6.0, d / (c.Inner ? 3 : 4)));
        var ext = cr.TextExtents(text);
        cr.MoveTo(x - ext.Width / 2 - ext.XBearing, y + ext.Height / 2);
        cr.SetSourceRGBA(0.2, 0.2, 0.2, 1.0);           // TextColor
        cr.ShowText(text);
        cr.NewPath();
    }

    // ══════════════════════════════════════════════════════════════════
    //  TUNER
    // ══════════════════════════════════════════════════════════════════

    public void ToggleTuner()
    {
        if (_tuner != null) { _tuner.Destroy(); _tuner = null; return; }
        BuildTuner();
    }

    private void BuildTuner()
    {
        _tuner = new Window("MapWindow tuner  —  Ctrl+T to close");
        _tuner.SetDefaultSize(430, 760);
        _tuner.DeleteEvent += (o, a) => { _tuner = null; };

        var scroll = new ScrolledWindow();
        var col = new Box(Orientation.Vertical, 4);
        col.MarginLeft = col.MarginRight = col.MarginTop = col.MarginBottom = 8;

        _tunerKey = new Label(); _tunerKey.Xalign = 0;
        col.PackStart(_tunerKey, false, false, 0);

        _tunerInfo = new Label(); _tunerInfo.Xalign = 0;
        _tunerInfo.Selectable = true;
        col.PackStart(_tunerInfo, false, false, 0);

        col.PackStart(Header("Sound map"), false, false, 0);

        _tFitCombo = new ComboBoxText();
        _tFitCombo.AppendText("Native546 (Form2)");
        _tFitCombo.AppendText("FitBody (calibrator)");
        _tFitCombo.Active = (_mapFit == MapFit.FitBody) ? 1 : 0;
        _tFitCombo.Changed += (s, e) =>
        {
            if (_building) return;
            _mapFit = _tFitCombo.Active == 1 ? MapFit.FitBody : MapFit.Native546;
            UpdateTunerInfo();
            MapArea.QueueDraw();
        };
        col.PackStart(Row("Map fit mode", _tFitCombo), false, false, 0);

        AddSpin(col, "MapZoom",  "Zoom",          0.10, 3.00, 0.01, 2);
        AddSpin(col, "MapX",     "Offset X (px)", -600, 600, 1, 0);
        AddSpin(col, "MapY",     "Offset Y (px)", -600, 600, 1, 0);
        AddSpin(col, "MapAlpha", "Opacity",       0.00, 1.00, 0.05, 2);

        col.PackStart(Header("Circle cluster"), false, false, 0);
        AddSpin(col, "OffsetX",   "Offset X (px)",  -600, 600, 1, 0);
        AddSpin(col, "OffsetY",   "Offset Y (px)",  -600, 600, 1, 0);
        AddSpin(col, "SizeScale", "Circle size",    0.05, 4.00, 0.05, 2);
        AddSpin(col, "Cluster",   "Cluster factor", 0.05, 2.00, 0.05, 2);
        AddSpin(col, "FitMargin", "Fit margin (px)", 0, 400, 5, 0);

        col.PackStart(Header("Cursor (Arduino)"), false, false, 0);
        AddSpin(col, "MarginX", "Margin X (raw units)", -2000, 2000, 1, 0);
        AddSpin(col, "MarginY", "Margin Y (raw units)", -2000, 2000, 1, 0);

        col.PackStart(Header("Display"), false, false, 0);
        _tShowBody    = Chk(col, "Show body",    true);
        _tShowMap     = Chk(col, "Show map",     true);
        _tShowCircles = Chk(col, "Show circles", true);
        _tGuides      = Chk(col, "Show guides",  false);

        col.PackStart(Header("Images"), false, false, 0);
        var imgRow = new Box(Orientation.Horizontal, 4);
        imgRow.PackStart(Btn("Reload images", () =>
        {
            ReloadBody();
            if (_map != null) { _map.Dispose(); _map = null; }
            if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }
            // _map = LoadMap(MapPathForView());
            _map = LoadMap(MapPathForView(), MapNeedsMirror());
            UpdateTunerInfo();
            MapArea.QueueDraw();
        }), true, true, 0);
        imgRow.PackStart(Btn("Cycle view", () =>
        {
            string[] v = { "front", "back", "right", "left" };
            int i = Array.IndexOf(v, _view);
            string next = v[(i + 1) % v.Length];
            RadioButton rb = next == "back" ? ViewBackRadio
                           : next == "right" ? ViewRightRadio
                           : next == "left" ? ViewLeftRadio : ViewFrontRadio;
            if (rb != null) rb.Active = true; else SetView(next);
        }), true, true, 0);
        col.PackStart(imgRow, false, false, 0);

        col.PackStart(Header("Constants"), false, false, 0);

        var r1 = new Box(Orientation.Horizontal, 4);
        r1.PackStart(Btn("Apply pasted C#", ApplyPastedCSharp), true, true, 0);
        r1.PackStart(Btn("Dump C# (calib)", () => Emit(DumpCalibStyle())), true, true, 0);
        col.PackStart(r1, false, false, 0);

        var r2 = new Box(Orientation.Horizontal, 4);
        r2.PackStart(Btn("Dump seed", () => Emit(DumpSeed())), true, true, 0);
        r2.PackStart(Btn("Reset view", () =>
        {
            _tunes.Remove(TuneKey);
            var fresh = new Dictionary<string, Tune>(_tunes);
            SeedDefaults();
            foreach (var kv in fresh) if (kv.Key != TuneKey) _tunes[kv.Key] = kv.Value;
            SyncSpins(); MapArea.QueueDraw();
        }), true, true, 0);
        col.PackStart(r2, false, false, 0);

        var r3 = new Box(Orientation.Horizontal, 4);
        r3.PackStart(Btn("Load calib.txt", () => { LoadCalibFile(true); SyncSpins(); MapArea.QueueDraw(); }), true, true, 0);
        r3.PackStart(Btn("Save calib.txt", SaveCalibFile), true, true, 0);
        col.PackStart(r3, false, false, 0);

        _tunerOut = new TextView();
        _tunerOut.OverrideFont(Pango.FontDescription.FromString("Monospace 9"));
        _tunerOut.WrapMode = WrapMode.None;
        var outScroll = new ScrolledWindow();
        outScroll.HeightRequest = 220;
        outScroll.Add(_tunerOut);
        col.PackStart(outScroll, true, true, 0);

        col.PackStart(new Label(
            "Paste a CalibrationWindow dump above, then Apply pasted C#.") { Xalign = 0 },
            false, false, 0);

        scroll.Add(col);
        _tuner.Add(scroll);
        _tuner.ShowAll();

        SyncSpins();
        UpdateTunerInfo();
    }

    // ── tuner widget helpers ──

    private Label Header(string text)
    {
        var l = new Label();
        l.Markup = "<b>" + text + "</b>";
        l.Xalign = 0;
        l.MarginTop = 8;
        return l;
    }

    private Box Row(string label, Widget w)
    {
        var box = new Box(Orientation.Horizontal, 6);
        var l = new Label(label);
        l.Xalign = 0;
        l.WidthRequest = 130;
        box.PackStart(l, false, false, 0);
        box.PackStart(w, true, true, 0);
        return box;
    }

    private Button Btn(string label, System.Action onClick)
    {
        var b = new Button(label);
        b.Clicked += (s, e) => onClick();
        return b;
    }

    private CheckButton Chk(Box parent, string label, bool active)
    {
        var c = new CheckButton(label);
        c.Active = active;
        c.Toggled += (s, e) => MapArea.QueueDraw();
        parent.PackStart(c, false, false, 0);
        return c;
    }

    private void AddSpin(Box parent, string field, string label,
                         double min, double max, double step, uint digits)
    {
        var sb = new SpinButton(min, max, step);
        sb.Digits = digits;
        sb.Numeric = true;
        sb.ValueChanged += (s, e) =>
        {
            if (_building) return;
            SetField(field, sb.Value);
            UpdateTunerInfo();
            MapArea.QueueDraw();
        };
        _spins[field] = sb;
        parent.PackStart(Row(label, sb), false, false, 0);
    }

    private void SetField(string field, double v)
    {
        var t = T;
        switch (field)
        {
            case "OffsetX":   t.OffsetX   = v; break;
            case "OffsetY":   t.OffsetY   = v; break;
            case "SizeScale": t.SizeScale = v; break;
            case "Cluster":   t.Cluster   = v; break;
            case "MapZoom":   t.MapZoom   = v; break;
            case "MapX":      t.MapX      = v; break;
            case "MapY":      t.MapY      = v; break;
            case "MapAlpha":  t.MapAlpha  = v; break;
            case "FitMargin": t.FitMargin = v; break;
            case "MarginX": t.MarginX = v; break;   
            case "MarginY": t.MarginY = v; break;
        }
    }

    private double GetField(string field)
    {
        var t = T;
        switch (field)
        {
            case "OffsetX":   return t.OffsetX;
            case "OffsetY":   return t.OffsetY;
            case "SizeScale": return t.SizeScale;
            case "Cluster":   return t.Cluster;
            case "MapZoom":   return t.MapZoom;
            case "MapX":      return t.MapX;
            case "MapY":      return t.MapY;
            case "MapAlpha":  return t.MapAlpha;
            case "FitMargin": return t.FitMargin;
            case "MarginX": return t.MarginX;
            case "MarginY": return t.MarginY;
        }
        return 0;
    }

    private void SyncSpins()
    {
        if (_tuner == null) return;
        _building = true;
        foreach (var kv in _spins) kv.Value.Value = GetField(kv.Key);
        if (_tunerKey != null) _tunerKey.Markup = "<b>Editing: " + TuneKey + "</b>";
        _building = false;
    }

    private void UpdateTunerInfo()
    {
        if (_tunerInfo == null) return;

        var sb = new StringBuilder();
        sb.Append("body: ").Append(_bodyPath);
        if (_body != null) sb.Append("  [").Append(_body.Width).Append("x").Append(_body.Height).Append("]");
        else sb.Append("  [MISSING]");
        sb.AppendLine();

        sb.Append("map:  ").Append(_mapPath);
        if (_mapRaw != null) sb.Append("  raw[").Append(_mapRaw.Width).Append("x").Append(_mapRaw.Height).Append("]");
        if (_map != null) sb.Append("  used[").Append(_map.Width).Append("x").Append(_map.Height).Append("]");
        else sb.Append("  [MISSING]");
        sb.AppendLine();

        double mx, my, mw, mh;
        if (MapRect(out mx, out my, out mw, out mh))
            sb.AppendFormat(CultureInfo.InvariantCulture,
                "rect: x={0:F1} y={1:F1} w={2:F1} h={3:F1}\n", mx, my, mw, mh);

        if (_mapFit == MapFit.FitBody)
            sb.AppendLine("NOTE: FitBody changes display geometry; AudioReference\n" +
                          "      is still the 546 pixbuf — sample via BodyToMapPixel.");

        _tunerInfo.Text = sb.ToString();
    }

    private void Emit(string msg)
    {
        if (_tunerOut == null) { Logger.Write("tune", msg); return; }
        _tunerOut.Buffer.Text = msg + "\n\n" + _tunerOut.Buffer.Text;
    }

    // ── C# constant round-trip ──

    private static readonly Regex ConstRx = new Regex(
        @"(?<xl>XL_)?(?<view>FRONT|BACK|RIGHT|LEFT)_" +
        @"(?<field>OFFSET_X|OFFSET_Y|SIZE|CLUSTER|FIT_MARGIN|MAP_ZOOM|MAP_X|MAP_Y|MAP_ALPHA)" +
        @"\s*=\s*(?<val>-?[0-9]*\.?[0-9]+)",
        RegexOptions.IgnoreCase | RegexOptions.Compiled);

    /// Parses a CalibrationWindow "Dump" block pasted into the text view.
    private void ApplyPastedCSharp()
    {
        if (_tunerOut == null) return;
        string text = _tunerOut.Buffer.Text;

        var inv = CultureInfo.InvariantCulture;
        int applied = 0;
        var touched = new List<string>();

        foreach (Match m in ConstRx.Matches(text))
        {
            string size = m.Groups["xl"].Success ? "XL" : "L";
            string view = m.Groups["view"].Value.ToLower();
            string key  = size + ":" + view;

            double v;
            if (!double.TryParse(m.Groups["val"].Value, NumberStyles.Float, inv, out v)) continue;

            if (!_tunes.ContainsKey(key)) _tunes[key] = new Tune();
            var t = _tunes[key];

            switch (m.Groups["field"].Value.ToUpper())
            {
                case "OFFSET_X":   t.OffsetX   = v; break;
                case "OFFSET_Y":   t.OffsetY   = v; break;
                case "SIZE":       t.SizeScale = v; break;
                case "CLUSTER":    t.Cluster   = v; break;
                case "FIT_MARGIN": t.FitMargin = v; break;
                case "MAP_ZOOM":   t.MapZoom   = v; break;
                case "MAP_X":      t.MapX      = v; break;
                case "MAP_Y":      t.MapY      = v; break;
                case "MAP_ALPHA":  t.MapAlpha  = v; break;
                default: continue;
            }
            applied++;
            if (!touched.Contains(key)) touched.Add(key);
        }

        SyncSpins();
        UpdateTunerInfo();
        MapArea.QueueDraw();

        if (applied == 0)
            Emit("no constants recognised — expected lines like\n" +
                 "  private const float FRONT_OFFSET_X = 25.0f;");
        else
            Emit("applied " + applied + " values to: " + string.Join(", ", touched.ToArray()) +
                 "\nIf MAP_ZOOM came from the calibrator, switch fit mode to FitBody.");
    }

    /// Same format CalibrationWindow emits — round-trips through Apply.
    private string DumpCalibStyle()
    {
        var inv = CultureInfo.InvariantCulture;
        var sb = new StringBuilder();
        sb.AppendLine("// MapWindow tuner " + DateTime.Now.ToString("yyyy-MM-dd HH:mm") +
                      "  fit=" + _mapFit);

        foreach (string size in new[] { "L", "XL" })
        {
            sb.AppendLine("// ---- " + size + " ----");
            foreach (string view in new[] { "front", "back", "right", "left" })
            {
                string k = size + ":" + view;
                if (!_tunes.ContainsKey(k)) continue;
                var t = _tunes[k];
                string p = (size == "XL" ? "XL_" : "") + view.ToUpper();

                sb.AppendLine("private const float " + p + "_OFFSET_X   = " + t.OffsetX.ToString("F1", inv) + "f;");
                sb.AppendLine("private const float " + p + "_OFFSET_Y   = " + t.OffsetY.ToString("F1", inv) + "f;");
                sb.AppendLine("private const float " + p + "_SIZE       = " + t.SizeScale.ToString("F2", inv) + "f;");
                sb.AppendLine("private const float " + p + "_CLUSTER    = " + t.Cluster.ToString("F2", inv) + "f;");
                sb.AppendLine("private const float " + p + "_FIT_MARGIN = " + t.FitMargin.ToString("F0", inv) + "f;");
                sb.AppendLine("private const double " + p + "_MAP_ZOOM  = " + t.MapZoom.ToString("F3", inv) + ";");
                sb.AppendLine("private const double " + p + "_MAP_X     = " + t.MapX.ToString("F1", inv) + ";");
                sb.AppendLine("private const double " + p + "_MAP_Y     = " + t.MapY.ToString("F1", inv) + ";");
                sb.AppendLine("private const double " + p + "_MAP_ALPHA = " + t.MapAlpha.ToString("F2", inv) + ";");
                sb.AppendLine();
            }
        }
        return sb.ToString();
    }

    /// Paste this straight over SeedDefaults() to bake values into the build.
    private string DumpSeed()
    {
        var inv = CultureInfo.InvariantCulture;
        var sb = new StringBuilder();
        sb.AppendLine("private void SeedDefaults()");
        sb.AppendLine("{");
        foreach (string size in new[] { "L", "XL" })
            foreach (string view in new[] { "front", "back", "right", "left" })
            {
                string k = size + ":" + view;
                if (!_tunes.ContainsKey(k)) continue;
                var t = _tunes[k];
                sb.AppendFormat(inv,
                    "    Seed(\"{0}\", {1:F1}, {2:F1}, {3:F2}, {4:F2}, {5:F0}, {6:F3}, {7:F1}, {8:F1}, {9:F2});\n",
                    k, t.OffsetX, t.OffsetY, t.SizeScale, t.Cluster,
                    t.FitMargin, t.MapZoom, t.MapX, t.MapY, t.MapAlpha);
            }
        sb.AppendLine("}");
        sb.AppendLine("// _mapFit = MapFit." + _mapFit + ";");
        return sb.ToString();
    }

    // ── calib.txt (same tab format CalibrationWindow writes) ──

    private static string CalibPath { get { return SoundWindow.AppFile("calib.txt"); } }

    private void LoadCalibFile(bool verbose)
    {
        string p = CalibPath;
        if (!File.Exists(p))
        {
            if (verbose) Emit("calib.txt not found at " + p);
            return;
        }

        var inv = CultureInfo.InvariantCulture;
        int n = 0;
        foreach (string line in File.ReadAllLines(p))
        {
            if (line.StartsWith("#") || line.Trim().Length == 0) continue;
            string[] f = line.Split('\t');
            if (f.Length < 10) continue;

            try
            {
                var t = new Tune();
                t.OffsetX   = double.Parse(f[1], inv);
                t.OffsetY   = double.Parse(f[2], inv);
                t.SizeScale = double.Parse(f[3], inv);
                t.Cluster   = double.Parse(f[4], inv);
                t.MapZoom   = double.Parse(f[5], inv);
                t.MapX      = double.Parse(f[6], inv);
                t.MapY      = double.Parse(f[7], inv);
                t.MapAlpha  = double.Parse(f[8], inv);
                t.FitMargin = double.Parse(f[9], inv);
                _tunes[f[0]] = t;
                n++;
            }
            catch { }
        }

        Logger.Write("tune", "loaded " + n + " entries from " + p);
        if (verbose) Emit("loaded " + n + " entries from " + p +
                          "\ncalib.txt zoom values assume FitBody mode.");
    }

    private void SaveCalibFile()
    {
        var inv = CultureInfo.InvariantCulture;
        var sb = new StringBuilder();
        sb.AppendLine("# size:view offsetX offsetY sizeScale cluster mapZoom mapX mapY mapAlpha fitMargin");
        foreach (var kv in _tunes)
        {
            var t = kv.Value;
            sb.AppendLine(string.Join("\t", new[]
            {
                kv.Key,
                t.OffsetX.ToString(inv),   t.OffsetY.ToString(inv),
                t.SizeScale.ToString(inv), t.Cluster.ToString(inv),
                t.MapZoom.ToString(inv),   t.MapX.ToString(inv),
                t.MapY.ToString(inv),      t.MapAlpha.ToString(inv),
                t.FitMargin.ToString(inv)
            }));
        }

        try
        {
            File.WriteAllText(CalibPath, sb.ToString());
            Emit("saved " + CalibPath);
        }
        catch (Exception ex) { Emit("save failed: " + ex.Message); }
    }

    // ── loading ────────────────────────────────────────────────────────

    private void SetView(string view)
    {
        if (_view == view) return;
        _view = view;

        ReloadBody();

        if (_map != null)    { _map.Dispose();    _map = null; }
        if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }
        // _map = LoadMap(MapPathForView());
        _map = LoadMap(MapPathForView(), MapNeedsMirror());

        if (AppMode.Legacy)
        {
            RebuildLegacyMap();
            UpdateTunerInfo();
            MapArea.QueueDraw();
            Logger.Write("view", "-> " + view + " (legacy)");
            return;
        }
        if (_map != null)    { _map.Dispose();    _map = null; }
        if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }
        _map = LoadMap(MapPathForView(), MapNeedsMirror());

        SyncSpins();
        UpdateTunerInfo();
        MapArea.QueueDraw();
        Logger.Write("view", "-> " + view);
    }

    private void ReloadBody()
    {
        if (AppMode.Legacy) 
            RebuildLegacyMap();
        else
        {
            if (_map != null) { _map.Dispose(); _map = null; }
            if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }
            _map = LoadMap(MapPathForView(), MapNeedsMirror());
        }
        UpdateTunerInfo();
        MapArea.QueueDraw();
    }

    private Gdk.Pixbuf LoadBody()
    {
        string stem = BodyStem();
        string full = SoundWindow.AppFile(Path.Combine("map", stem + ".png"));
        _bodyPath = "map/" + stem + ".png";

        if (!File.Exists(full))
        {
            Logger.Write("body", "not found: map/" + stem + ".png");
            ListFolderOnce("map");
            return null;
        }

        return Load(full);
    }

    private static void ListFolderOnce(string rel)
    {
        if (_listed) return;
        _listed = true;

        string dir = SoundWindow.AppFile(rel);
        if (!Directory.Exists(dir)) { Logger.Write("body", "folder missing: " + dir); return; }

        foreach (string f in Directory.GetFiles(dir))
            Logger.Write("body", "found: " + Path.GetFileName(f));
    }

    private string BodyStem()
    {
        bool skeleton = (BodySkeletonRadio != null && BodySkeletonRadio.Active);

        if (skeleton)
        {
            switch (_view)
            {
                case "back":  return "jacketBackBody";
                case "right": return "jacketRightBody";
                case "left":  return "jacketLeftBody";
                default:      return "jacketFrontBody";
            }
        }

        switch (_view)
        {
            case "back":  return "jacketBack";
            case "right": return "jacketRight";
            case "left":  return "jacketLeft";
            default:      return "jacketFront";
        }
    }

    /// Back view with no dedicated Image_Back in the CSV — Form2's
    /// CreateFlippedImage fallback. Computed from _view every call, so it
    /// cannot go stale when the view changes.
    private bool MapNeedsMirror()
    {
        return _case != null && _view == "back" && string.IsNullOrEmpty(_case.MapBack);
    }

    private string MapPathForView()
    {
        if (_case == null) return null;

        string rel;
        switch (_view)
        {
            case "back":  rel = _case.MapBack;  break;
            case "right": rel = _case.MapRight; break;
            case "left":  rel = _case.MapLeft;  break;
            default:      rel = _case.MapFront; break;
        }

        // Form2 falls back to a mirrored front map when there's no dedicated back.
        if (string.IsNullOrEmpty(rel)) rel = _case.MapFront;

        return string.IsNullOrEmpty(rel) ? null : SoundWindow.AppFile(rel);
    }

    private static Gdk.Pixbuf Load(string path)
    {
        if (string.IsNullOrEmpty(path) || !File.Exists(path))
        {
            Logger.Write("map", "image not found: " + path);
            return null;
        }

        try { return new Gdk.Pixbuf(path); }
        catch (Exception ex)
        {
            Logger.Write("map", "load failed " + path + ": " + ex.Message);
            return null;
        }
    }

    private Gdk.Pixbuf LoadMap(string path, bool mirror)
    {
        _mapPath = path ?? "(none)";

        var raw = Load(path);
        if (raw == null) return null;

        // Key out black FIRST, then scale. Scaling first leaves near-black
        // edge pixels (1,0,2) that AddAlpha won't match — that's the dark halo.
        var keyed = raw.AddAlpha(true, 0, 0, 0);
        raw.Dispose();

         if (mirror)
        {
            var flipped = keyed.Flip(true);
            keyed.Dispose();
            keyed = flipped;
            Logger.Write("map", "mirrored front map for back view");
        }

        if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }   // defensive

        // Keep the keyed original for FitBody mode — the calibrator never
        // resized to 546, so its zoom numbers refer to THIS pixbuf.
        _mapRaw = keyed.Copy();

        var sized = keyed.ScaleSimple(MapSize, MapSize, Gdk.InterpType.Bilinear);
        keyed.Dispose();
        return sized;
    }

    // ── plumbing ───────────────────────────────────────────────────────

    private void OnBackClicked(object sender, EventArgs e)
    {
        StopSession();
        var h = BackRequested;
        if (h != null) h(this, EventArgs.Empty);
    }

    private void DisposePixbufs()
    {
        if (_body != null)   { _body.Dispose();   _body = null; }
        if (_map != null)    { _map.Dispose();    _map = null; }
        if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }
    }

    public void Dispose()
    {
        StopWaveTimer();
        _arduino.LineReceived      -= OnArduinoLine;
        _arduino.ConnectionChanged -= OnArduinoConnectionChanged;
        if (_watchdogId != 0) { GLib.Source.Remove(_watchdogId); _watchdogId = 0; }
        
        // NEW: cleanup legacy regions
        foreach (var r in _regions) r.Dispose();
        _regions.Clear();
        
        _sound.Dispose();
        DisposePixbufs();
        if (_tuner != null) { _tuner.Destroy(); _tuner = null; }
    }

    private string JacketSize
    {
        get { return (JacketSizeCombo != null ? JacketSizeCombo.ActiveText : null) ?? "L"; }
    }

    private string DetermineViewFromZ(float z)
    {
        float frontDiff = Math.Abs(z - FRONT_Z);
        float backDiff  = Math.Abs(z - BACK_Z);
        float rightDiff = Math.Abs(z - RIGHT_Z);
        float leftDiff  = Math.Abs(z - LEFT_Z);

        float minDiff = Math.Min(Math.Min(frontDiff, backDiff), Math.Min(rightDiff, leftDiff));
        if (minDiff > Z_TOLERANCE) return _view;   // no clean match — stay put

        if (minDiff == frontDiff) return "front";
        if (minDiff == backDiff)  return "back";
        if (minDiff == rightDiff) return "right";
        return "left";
    }

    /// Drives the same radio buttons a manual click would, so the UI and
    /// _view stay in sync (SetView no-ops if it's already the active view).
    private void SwitchViewFromArduino(string view)
    {
        RadioButton rb = view == "back"  ? ViewBackRadio
                        : view == "right" ? ViewRightRadio
                        : view == "left"  ? ViewLeftRadio
                        : ViewFrontRadio;
        if (rb != null) rb.Active = true;
        else SetView(view);
    }

    //=========================================
    //debug
    private void StartDebugSweep()
    {
        if (!AppConfig.DebugMode) return;
        if (_debugSweepId != 0) return;          // already running
        if (_sound.MainVoice == null) return;    // no case loaded yet

        _debugLevel = 0;
        _debugStep  = 5;

        _debugSweepId = GLib.Timeout.Add(50, () =>
        {
            _debugLevel += _debugStep;

            if (_debugLevel >= 255) { _debugLevel = 255; _debugStep = -5; }
            else if (_debugLevel <= 0) { _debugLevel = 0; _debugStep = 5; }

            float gain = AudioEngine.VolumeCurve(_debugLevel);
            _sound.SetGain(_sound.MainVoice, gain);

            if (_debugLevel % 50 == 0)
                Logger.Write("sound", string.Format("sweep level={0}/255 gain={1:F3}",
                                                    _debugLevel, gain));
            return true;                          // keep firing
        });

        Logger.Write("sound", "debug volume sweep started");
    }

    private void StopDebugSweep()
    {
        if (_debugSweepId == 0) return;
        GLib.Source.Remove(_debugSweepId);
        _debugSweepId = 0;
        Logger.Write("sound", "debug volume sweep stopped");
    }
    
    private void DrawWave(DrawnArgs args, DrawingArea area, WaveData wave, double cursor,double r, double g, double b)
    {
        var cr = args.Cr;
        int w = area.AllocatedWidth, h = area.AllocatedHeight;
        double mid = h / 2.0;

        // cr.SetSourceRGB(0.85, 0.85, 0.85);
        cr.SetSourceRGB(0.965, 0.969, 0.976);
        cr.Rectangle(0, 0, w, h);
        cr.Fill();

        cr.SetSourceRGB(r, g, b);

        if (wave == null || wave.Samples == null || w < 2)
        {
            // no sound for this box — flat line as before
            cr.LineWidth = 1.5;
            cr.MoveTo(0, mid);
            cr.LineTo(w, mid);
            cr.Stroke();
            args.RetVal = true;
            return;
        }

        float[] mins, maxs;
        wave.GetPeaks(w, out mins, out maxs);

        double amp = mid * 0.9;
        cr.LineWidth = 1;
        for (int x = 0; x < w; x++)
        {
            double y1 = mid - maxs[x] * amp;
            double y2 = mid - mins[x] * amp;
            if (y2 - y1 < 1) y2 = y1 + 1;        // quiet parts stay visible
            cr.MoveTo(x + 0.5, y1);
            cr.LineTo(x + 0.5, y2);
        }
        cr.Stroke();

        if (cursor >= 0)
        {
            double cx = Math.Min(w - 1, cursor * w);
            cr.SetSourceRGB(0.1, 0.1, 0.1);      // dark cursor, contrasts with red and blue waves
            cr.LineWidth = 2;
            cr.MoveTo(cx, 0);
            cr.LineTo(cx, h);
            cr.Stroke();
        }

        args.RetVal = true;
    }

    private void LoadWaveforms(CaseDefinition def)
    {
        _heartCursor = _breathCursor = -1;

        // Legacy has no SoundPath; show the first active region's sound instead
        string caseSound = def.SoundPath;
        if (def.IsLegacy)
        {
            caseSound = null;
            for (int k = 0; k < 7 && caseSound == null; k++)
                if (AppMode.ResolveSound(def.RegionSounds[k]) != null)
                    caseSound = def.RegionSounds[k];
        }

        WaveData caseWave = LoadWave(caseSound);

        if (def.IsHeart)
        {
            _heartWave  = caseWave;
            _breathWave = LoadWave("SND200.wav");   // default breath sound
            _heartLive  = true;
            _breathLive = BreathSoundEnabled;
        }
        else
        {
            _breathWave = caseWave;
            _heartWave  = LoadWave("SND100.wav");   // default heart sound
            _breathLive = true;
            _heartLive  = HeartSoundEnabled;
        }

        HeartWaveArea.QueueDraw();
        BreathWaveArea.QueueDraw();
    }

    private static WaveData LoadWave(string csvValue)
    {
        string full = AppMode.ResolveSound(csvValue);   // null-safe, uses shared sound/ folder
        if (full == null)
        {
            Logger.Write("wave", "no sound for waveform (" + (csvValue ?? "null") + ")");
            return null;                                // draw code already shows a flat line
        }

        string err;
        WaveData w = WaveData.Load(full, out err);
        if (w == null) Logger.Write("wave", "load failed " + full + ": " + err);
        else Logger.Write("wave", "loaded " + full + " (" +
                        w.Duration.ToString("F2", CultureInfo.InvariantCulture) + " s)");
        return w;
    }

    // private void LoadWaveforms(CaseDefinition def)
    // {
    //     _heartCursor = _breathCursor = -1;

    //     WaveData caseWave = LoadWave(def.SoundPath);

    //     if (def.IsHeart)
    //     {
    //         _heartWave  = caseWave;
    //         _breathWave = LoadWave("sound/SND200.wav");   // default breath sound
    //         _heartLive  = true;
    //         _breathLive = BreathSoundEnabled;             // only "playing" if that voice was warmed
    //     }
    //     else
    //     {
    //         _breathWave = caseWave;
    //         _heartWave  = LoadWave("sound/SND100.wav");   // default heart sound
    //         _breathLive = true;
    //         _heartLive  = HeartSoundEnabled;
    //     }

    //     HeartWaveArea.QueueDraw();
    //     BreathWaveArea.QueueDraw();
    // }

    // private static WaveData LoadWave(string rel)
    // {
    //     string err;
    //     WaveData w = WaveData.Load(SoundWindow.AppFile(rel), out err);
    //     if (w == null) Logger.Write("wave", "load failed " + rel + ": " + err);
    //     else Logger.Write("wave", "loaded " + rel + " (" +
    //                       w.Duration.ToString("F2", CultureInfo.InvariantCulture) + " s)");
    //     return w;
    // }

    private void StartWaveTimer()
    {
        if (_waveTimerId != 0) return;
        _waveTimerId = GLib.Timeout.Add(16, OnWaveTick);   // ~60 fps
    }

    private void StopWaveTimer()
    {
        if (_waveTimerId != 0) { GLib.Source.Remove(_waveTimerId); _waveTimerId = 0; }
        _audible = false;
        _heartCursor = _breathCursor = -1;
        if (HeartWaveArea != null)  HeartWaveArea.QueueDraw();
        if (BreathWaveArea != null) BreathWaveArea.QueueDraw();
    }

    // private bool OnWaveTick()
    // {
    //     if (!_audible) return true;              // muted: cursor stays frozen

    //     double t = _playClock.Elapsed.TotalSeconds;
    //     _heartCursor  = CursorFor(_heartWave,  t);
    //     _breathCursor = CursorFor(_breathWave, t);

    //     HeartWaveArea.QueueDraw();
    //     BreathWaveArea.QueueDraw();
    //     return true;
    // }
        private bool OnWaveTick()
    {
        if (!_audible)
        {
            // muted: hide the cursor (redraw only once, when it disappears)
            if (_heartCursor >= 0 || _breathCursor >= 0)
            {
                _heartCursor = _breathCursor = -1;
                HeartWaveArea.QueueDraw();
                BreathWaveArea.QueueDraw();
            }
            return true;
        }

        double t = _playClock.Elapsed.TotalSeconds;
        _heartCursor  = _heartLive  ? CursorFor(_heartWave,  t) : -1;
        _breathCursor = _breathLive ? CursorFor(_breathWave, t) : -1;

        HeartWaveArea.QueueDraw();
        BreathWaveArea.QueueDraw();
        return true;
    }

    /// Where the looping voice is right now, 0..1 across the box.
    private static double CursorFor(WaveData w, double seconds)
    {
        if (w == null || w.Duration <= 0) return -1;
        return (seconds % w.Duration) / w.Duration;
    }

    /// Form2's checkBoxBlackAudio. Defaults ON; if the glade id is missing,
    /// the null check keeps it ON rather than silently killing the audio.
    private bool BlackAudioEnabled
    {
        get { return BlackAudioSwitch == null || BlackAudioSwitch.Active; }
    }

    /// Must match the pixbuf BodyToMapPixel indexes into, or px/py land
    /// on the wrong image.
    private Gdk.Pixbuf AudioSourcePixbuf
    {
        get { return (_mapFit == MapFit.FitBody && _mapRaw != null) ? _mapRaw : _map; }
    }

    /// Body-pixel point -> 0..1 position inside the drawn map rect.
    public bool BodyToMapUV(double bx, double by, out double u, out double v)
    {
        u = v = 0;
        double mx, my, mw, mh;
        if (!MapRect(out mx, out my, out mw, out mh)) return false;
        if (mw <= 0 || mh <= 0) return false;

        u = (bx - mx) / mw;
        v = (by - my) / mh;
        return (u >= 0 && u < 1 && v >= 0 && v < 1);
    }

    private void LoadRegions(CaseDefinition def)
    {
        _sound.MuteAll();
        foreach (var r in _regions) r.Dispose();
        _regions.Clear();

        for (int k = 0; k < 7; k++)
        {
            string mapPath = AppMode.ResolveMap(def.RegionMaps[k]);
            string sndPath = AppMode.ResolveSound(def.RegionSounds[k]);
            if (mapPath == null || sndPath == null) continue;          // region disabled

            var pb = Load(mapPath);                                    // existing helper
            if (pb == null)
            {
                Logger.Write("map", "region " + RegionLabels[k] + " map missing: " + mapPath);
                continue;
            }

            var voice = _sound.Warm(sndPath, "R" + k + ":" + sndPath);
            if (voice == null) { pb.Dispose(); continue; }
            _sound.SetGain(voice, 0f);

            var rs = new RegionSound
            {
                Index = k, Name = RegionLabels[k],
                Map = pb, Voice = voice
            };
            rs.CachePixels();
            _regions.Add(rs);

            Logger.Write("map", "region " + RegionLabels[k] + ": " +
                        Path.GetFileName(mapPath) + " + " + Path.GetFileName(sndPath));
        }

        Logger.Write("map", _regions.Count + "/7 regions active");
    }

    /// Call with the same body-pixel coords the single-map path uses.
    private void ApplyRegionAudio(double bx, double by)
    {
        double u, v;
        if (!BodyToMapUV(bx, by, out u, out v)) { MuteRegions(); return; }
        if (_view == "back") u = 1.0 - u;          // mirrored front map

        foreach (var r in _regions) r.Volume = r.VolumeAt(u, v);

        // Heart conflict: only the louder of the two heart modules plays.
        RegionSound h1 = _regions.Find(r => r.Index == 0);
        RegionSound h2 = _regions.Find(r => r.Index == 1);
        if (h1 != null && h2 != null)
        {
            if (h1.Volume >= h2.Volume) h2.Volume = 0f;
            else                        h1.Volume = 0f;
        }

        foreach (var r in _regions) _sound.SetGain(r.Voice, r.Volume);
        MapArea.QueueDraw();                     // repaint the meters
    }

    private void MuteRegions()
    {
        foreach (var r in _regions) { r.Volume = 0f; _sound.SetGain(r.Voice, 0f); }
        MapArea.QueueDraw();
    }

    /// Brightest region wins each pixel; alpha tracks intensity.
    private Gdk.Pixbuf BuildRegionOverlay(int w, int h, bool mirror = false)
    {
        if (_regions.Count == 0) return null;

        int stride = w * 4;
        var px   = new byte[stride * h];
        var best = new byte[w * h];

        foreach (var r in _regions)
        {
            byte[] c = RegionColors[r.Index];

            for (int y = 0; y < h; y++)
            {
                double v = (y + 0.5) / h;
                for (int x = 0; x < w; x++)
                {
                    double u = (x + 0.5) / w;
                    if (mirror) u = 1.0 - u;
                    int val = r.IntensityAt(u, v);
                    if (val < 12) continue;

                    int k = y * w + x;
                    if (val <= best[k]) continue;
                    best[k] = (byte)val;

                    int o = y * stride + x * 4;
                    px[o]     = c[0];
                    px[o + 1] = c[1];
                    px[o + 2] = c[2];
                    px[o + 3] = (byte)(val * 0.55);
                }
            }
        }

        return new Gdk.Pixbuf(px, Gdk.Colorspace.Rgb, true, 8, w, h, stride);
    }

    private void DrawRegionMeters(Cairo.Context cr)
    {
        if (!_showRegionLabels) return;

        const double rowH = 30, barX = 140, barW = 170, barH = 14, pad = 12;
        double y0 = pad;

        cr.Save();
        cr.NewPath();
        cr.SetSourceRGBA(0, 0, 0, 0.72);
        cr.Rectangle(pad - 6, y0 - 6, barX + barW + 12, 7 * rowH + 12);
        cr.Fill();

        var layout = Pango.CairoHelper.CreateLayout(cr);
        layout.FontDescription = Pango.FontDescription.FromString("Noto Sans CJK JP Bold 15");

        for (int k = 0; k < 7; k++)
        {
            RegionSound r = _regions.Find(rs => rs.Index == k);
            bool active = r != null && r.Enabled;
            double vol = active ? r.Volume : 0.0;
            double y = y0 + k * rowH;

            // label
            layout.SetText(RegionLabels[k]);
            int lw, lh;
            layout.GetPixelSize(out lw, out lh);
            if (active) cr.SetSourceRGB(1, 1, 1);
            else        cr.SetSourceRGB(0.55, 0.55, 0.55);
            cr.MoveTo(pad, y + (rowH - lh) / 2.0);
            Pango.CairoHelper.ShowLayout(cr, layout);
            cr.NewPath();

            // bar background
            double by = y + (rowH - barH) / 2.0;
            cr.SetSourceRGB(0.30, 0.30, 0.30);
            cr.Rectangle(barX, by, barW, barH);
            cr.Fill();

            // bar fill
            if (active)
            {
                byte[] c = RegionColors[k];
                cr.SetSourceRGB(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
                cr.Rectangle(barX, by, barW * vol, barH);
                cr.Fill();
            }
        }

        layout.Dispose();
        cr.Restore();
    }

    // private void DrawRegionMeters(Cairo.Context cr)
    // {
    //     const double rowH = 18, barX = 92, barW = 120, pad = 10;
    //     double y0 = pad;

    //     cr.Save();
    //     cr.SetSourceRGBA(1, 1, 1, 0.82);
    //     cr.Rectangle(pad - 4, y0 - 4, barX + barW + 8, 7 * rowH + 8);
    //     cr.Fill();

    //     cr.SelectFontFace("Sans", Cairo.FontSlant.Normal, Cairo.FontWeight.Normal);
    //     cr.SetFontSize(11);

    //     for (int k = 0; k < 7; k++)
    //     {
    //         RegionSound r = _regions.Find(rs => rs.Index == k);
    //         double vol = (r != null && r.Enabled) ? r.Volume : 0.0;
    //         double y = y0 + k * rowH;

    //         cr.SetSourceRGB(0.15, 0.15, 0.15);
    //         cr.MoveTo(pad, y + rowH - 5);
    //         cr.ShowText(RegionLabels[k]);
    //         cr.NewPath();

    //         cr.SetSourceRGB(0.86, 0.86, 0.86);
    //         cr.Rectangle(barX, y + 3, barW, 11);
    //         cr.Fill();

    //         if (r != null)
    //         {
    //             byte[] c = RegionColors[k];
    //             cr.SetSourceRGB(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
    //             cr.Rectangle(barX, y + 3, barW * vol, 11);
    //             cr.Fill();
    //         }
    //         else
    //         {
    //             cr.SetSourceRGB(0.6, 0.6, 0.6);
    //             cr.MoveTo(barX, y + 8.5); cr.LineTo(barX + barW, y + 8.5);
    //             cr.LineWidth = 1; cr.Stroke();
    //         }
    //     }
    //     cr.Restore();
    // }

    /// Legacy: rebuild the 7-region composite for the current view.
    /// Back = mirrored front (same as Form2's CreateFlippedImage fallback).
    private void RebuildLegacyMap()
    {
        if (_map != null)    { _map.Dispose();    _map = null; }
        if (_mapRaw != null) { _mapRaw.Dispose(); _mapRaw = null; }

        bool mirrored = _view == "back";
        bool shown    = _view == "front" || mirrored;

        if (shown)
        {
            var overlay = BuildRegionOverlay(MapSize, MapSize, mirrored);
            _mapRaw  = overlay != null ? overlay.Copy() : null;
            _map     = overlay;
            _mapPath = mirrored ? "(7 regions, mirrored)" : "(7 regions composite)";
        }
        else _mapPath = "(no legacy map for " + _view + ")";

        foreach (var r in _regions) r.Enabled = shown;
        if (!shown) MuteRegions();
    }

    // private void AddHideLabelsOption()
    // {
    //     if (_hideLabelsCheck != null || ShowVolumeMapSwitch == null) return;

    //     _hideLabelsCheck = new CheckButton(_lang != null && _lang.IsEnglish ? "Hide labels" : "ラベルを隠す");
    //     _hideLabelsCheck.StyleContext.AddClass("mappage");
    //     _hideLabelsCheck.Toggled += (s, e) =>
    //     {
    //         _showRegionLabels = !_hideLabelsCheck.Active;
    //         MapArea.QueueDraw();
    //     };

    //     var box = HideMapCheck.Parent as Box;
    //     if (box != null)
    //     {
    //         box.PackStart(_hideLabelsCheck, false, false, 0);
    //         box.ReorderChild(_hideLabelsCheck, Array.IndexOf(box.Children, HideMapCheck) + 1);
    //     }
    //     else if (HideMapCheck.Parent is Container)
    //         ((Container)HideMapCheck.Parent).Add(_hideLabelsCheck);

    //     _hideLabelsCheck.Show();
    // }

}