function plot_time_histories_bearings(t, x, env, ss, dobp, xbp, flow, fup)
%PLOT_TIME_HISTORIES  Figure 2: raw / band-pass / envelope time traces
%   with live t-min / t-max edit boxes that rescale all subplots together.
%
%   plot_time_histories(t, x, env, ss, false)                  % 2 panels
%   plot_time_histories(t, x, env, ss, true, xbp, flow, fup)   % 3 panels

    % colors (matching the slide)
    c_raw = [0.00 0.45 0.74];
    c_bp  = [0.20 0.55 0.30];
    c_env = [0.80 0.20 0.10];

    hp = figure(2); clf; set(hp, 'Color', 'w', 'Position', [100 100 900 600]);

    if dobp
        nrows = 3;   % raw, filtered, envelope
    else
        nrows = 2;   % raw, envelope
    end

    % shrink the subplot width to leave room for the edit boxes on the right
    axw = 0.74;          % normalized axes width
    axl = 0.09;          % left margin

    ax = gobjects(nrows,1);

    % --- raw ---
    ax(1) = subplot(nrows, 1, 1);
    plot(ax(1), t, x, 'Color', c_raw);
    title(ax(1), 'Raw'); ylabel(ax(1), ss); grid(ax(1), 'on');

    if dobp
        % --- band-pass filtered ---
        ax(2) = subplot(nrows, 1, 2);
        plot(ax(2), t, xbp, 'Color', c_bp);
        title(ax(2), sprintf('Band-pass %g–%g Hz', flow, fup));
        ylabel(ax(2), ss); grid(ax(2), 'on');
        % --- envelope ---
        ax(3) = subplot(nrows, 1, 3);
        plot(ax(3), t, env, 'Color', c_env);
        title(ax(3), 'Envelope'); ylabel(ax(3), ss); grid(ax(3), 'on');
    else
        % --- envelope ---
        ax(2) = subplot(nrows, 1, 2);
        plot(ax(2), t, env, 'Color', c_env);
        title(ax(2), 'Envelope'); ylabel(ax(2), ss); grid(ax(2), 'on');
    end

    xlabel(ax(nrows), 'Time (sec)');   % label only the bottom panel

    % reposition each axes to the narrower width (keep subplot heights)
    for k = 1:nrows
        p = get(ax(k), 'Position');
        p(1) = axl; p(3) = axw;
        set(ax(k), 'Position', p);
    end

    % initial time limits = full record
    tmin0 = t(1);
    tmax0 = t(end);
    set(ax, 'XLim', [tmin0 tmax0]);

    % --- t max edit box (upper right) ---
    uicontrol('Style','text','Units','normalized', ...
        'Position',[0.86 0.80 0.12 0.05],'String','t max (s)', ...
        'BackgroundColor',get(hp,'Color'),'HorizontalAlignment','left');
    hMax = uicontrol('Style','edit','Units','normalized', ...
        'Position',[0.86 0.75 0.12 0.05],'String',num2str(tmax0));

    % --- t min edit box (below it) ---
    uicontrol('Style','text','Units','normalized', ...
        'Position',[0.86 0.66 0.12 0.05],'String','t min (s)', ...
        'BackgroundColor',get(hp,'Color'),'HorizontalAlignment','left');
    hMin = uicontrol('Style','edit','Units','normalized', ...
        'Position',[0.86 0.61 0.12 0.05],'String',num2str(tmin0));

    % data range, for clamping
    trange = [t(1) t(end)];

    % wire callbacks (axes vector + handles + data range passed explicitly)
    set(hMax,'Callback',{@updateTimeLimits, ax, hMin, hMax, trange});
    set(hMin,'Callback',{@updateTimeLimits, ax, hMin, hMax, trange});

    % widen the figure (kept from your original)
    pos = get(hp, 'Position');
    pos(3) = 1.5 * pos(3);
    set(hp, 'Position', pos);
end

function updateTimeLimits(~, ~, ax, hMin, hMax, trange)
    lo = str2double(get(hMin,'String'));
    hi = str2double(get(hMax,'String'));
    if isnan(lo) || isnan(hi) || hi <= lo
        % invalid — restore current limits to the boxes
        cur = get(ax(1),'XLim');
        set(hMin,'String',num2str(cur(1)));
        set(hMax,'String',num2str(cur(2)));
        return;
    end
    % clamp to the available data range
    lo = max(lo, trange(1));
    hi = min(hi, trange(2));
    set(ax, 'XLim', [lo hi]);          % set on all subplots at once
    set(hMin,'String',num2str(lo));    % reflect any clamping back to boxes
    set(hMax,'String',num2str(hi));
end