It's the foundation. If attached, it's probably going to be footings below frost line, stem wall, stick framed walls, and trusses, and siding and roofing to match your house. All those same systems on detached will probably cost a little more - the fourth wall. I'd plan upwards of $100 sq ft - before insulation or interior finishes - turnkey contractor.
But detached could be noticeably less expensive. A post frame building - built for you, is probably in the $50-60 SQ ft range. Metal siding and roof, no insulation or interior finishes. Now, you can sheath and put same siding as house on it, and do same for roof.
I'm planning a 28 or 30x 36 detached post frame, metal roof, local rough sawn board and batten, and figuring $20,000 all DIY (DIM for myself?) before slab, another $8000 to 10,000 contract, no insulation or interior finishes. It's a barn for storage, period.
Lots of variables. Many choices.