function ss_vals = get_ss_values(myoutfilename,...
                                 myvar_list_,...
                                 mylm_list_)

% Compute steady-state values for all endogenous variables.
%
% Calls _paramfile and the structural part of _ss_defs (before the M_ loop)
% to get base variable values (c1c, c1rpcpd, etc.), then creates _ss copies
% by looping over the LHS names found in that structural section, then calls
% _lmss as a script to compute the Lagrange multiplier steady states.
%
% INPUT:
%   outfilename - base name of the policy-problem output file (no extension)
%
% OUTPUT:
%   ss_vals - struct; field names are variable names, values are SS values

%% Load parameters (c1beta, c1sigma, c1css, Util1ss, etc.)
eval([myoutfilename, '_paramfile']);

%% Run structural SS assignments from _ss_defs (sets c1c, c1rpcpd, etc.)
%  Stop before the M_ loop — we replicate it below without needing M_.
ss_defs_txt = fileread([myoutfilename, '_ss_defs.m']);
lmss_call   = [myoutfilename, '_lmss'];
mloop_pos   = strfind(ss_defs_txt, 'nvars = M_.endo_nbr');
lmss_pos    = strfind(ss_defs_txt, lmss_call);

if ~isempty(mloop_pos)
    struct_end = mloop_pos(1) - 1;
elseif ~isempty(lmss_pos)
    nl = find(ss_defs_txt(1:lmss_pos(1)) == newline, 1, 'last');
    struct_end = max(nl, 1);
else
    struct_end = length(ss_defs_txt);
end
eval(ss_defs_txt(1:struct_end));   % sets c1c, c1rpcpd, c1pid, lag vars, etc.

%% Create _ss copies from the LHS variable names in the structural section
for kk = 1:length(myvar_list_)
    vname = myvar_list_{kk};
    if endsWith(vname,'_ss')
        vname=vname(1:end-3);
    end
    eval([vname, '_ss = ', vname, ';']);
end

%%% Call _lmss as a script to compute Lagrange multiplier steady states
eval(lmss_call);   % runs in this function's workspace; sets lmss_vals, lmult1..N

endovarlist = [myvar_list_;
               mylm_list_];
n_endovar = length(endovarlist);

%% Collect all variable values into the output struct
ss_vals = struct();
for kk = 1:n_endovar
    vname = endovarlist{kk};
    if endsWith(vname,'_ss')
        vname = vname(1:end-3);
    end
    try
        val = eval(vname);
        if isscalar(val) && isfinite(val)
            ss_vals.(vname) = val;
        else
            ss_vals.(vname) = 0;
        end
    catch %#ok<CTCH>
        ss_vals.(vname) = 0;
    end
end
