def merge_dicts(d1, d2):
result = d1.copy()
for key, value in d2.items():
if key in result and isinstance(value, dict) and isinstance(result[key], dict):
result[key] = merge_dicts(result[key], value)
else:
result[key] = value
return result