Dense and cold molecular cores and filaments are surrounded by an envelope of translucent gas. Some of the low- J emission lines of and isotopologues are more sensitive to the conditions either in the translucent environment or in the dense and cold one because their intensities result from a complex interplay of radiative transfer and chemical properties of these heterogeneous lines of sight (LoSs). We extend our previous single-zone modeling with a more realistic approach that introduces multiple layers to take account of possibly varying conditions along the LoS. We used the IRAM-30m data from the ORION-B large program toward the Horsehead nebula in order to demonstrate our method's capability and effectiveness. We propose a cloud model composed of three homogeneous slabs of gas along each LoS, representing an outer envelope and a more shielded inner layer. We used the non-LTE radiative transfer code RADEX to model the line profiles from the kinetic temperature ( the volume density ( kinematics, and chemical properties of the different layers. We then used a fast and robust maximum likelihood estimator to simultaneously fit the observed lines of the and isotopologues. To limit the variance on the estimates, we propose a simple chemical model by constraining the column densities. A single-layer model cannot reproduce the spectral line asymmetries that result from a combination of different radial velocities and absorption effects among layers. A minimal heterogeneous model (three layers only) is sufficient for the Horsehead application, as it provides good fits of the seven fitted lines over a large part of the studied field of view. The decomposition of the intensity into three layers allowed us to discuss the distribution of the estimated physical or chemical properties along the LoS. About 80<!PCT!> of the integrated intensity comes from the outer envelope, while $ of the integrated intensity of the and lines of comes from the inner layer. For the lines of the and the isotopologues, integrated intensities are more equally distributed over the cloud layers. The estimated column density ratio NCeiO in the envelope increases with decreasing visual extinction, and it reaches $25$ in the pillar outskirts. While the inferred of the envelope varies from $25$ to K$ that of the inner layer drops to K$ in the western dense core. The estimated in the inner layer is pccm$ toward the filament, and it increases by a factor of ten toward dense cores. Our proposed method correctly retrieves the physical and chemical properties of the Horsehead nebula. It also offers promising prospects for less supervised model fits of wider-field datasets.