Could anyone tell me what does dafür in this sentence refer to? It's a bit confusing.
Wenn du kleine Lampe benutzt, sorgst du nachts bei Toilettengängen dafür, dass du nicht das Deckenlicht anmachen musst.
First of all: You have to use an article or any other determiner for the subject. An adjective is not a determiner:
Wenn du eine kleine Lampe benutzt, ...
Now for your question:
The verb sorgen (to take care) can be used in two ways:
reflexive, with "um":
Gertrud sorgt sich um ihre Tochter.
Gertrud worries about her daughter.
transitive, with "für"
Gertrud sorgt für ihre Tochter.
Gertrud takes care of her daughter.
So, when the meaning is not "to worry about something/someone", then you have to use a prepositional object that consists of the preposition "für" and something else that has to be used in accusative case. Often it is a nominal group ("ihre Tochter"), but it also can be a pronoun:
Gertrud sorgt für sie. (Gertrud takes care of her.)
Gertrud sorgt für jemanden. (Gertrud takes care of someone.)
Gertrud sorgt für etwas. (Gertrud takes care of something.)
This "etwas" (something) in the last example can be a physical thing that you can touch like a house, but it also can be an action, which changes the meaning a little bit.
Until now "für etwas/jemanden sorgen" (to take care of something/someone) did mean: To do things, that have a positive effect on the thing or person you take care of.
But when "etwas" is an action, "für etwas/jemanden sorgen" means to ensure, that this action will happen. But to describe an action, you need a verb, i.e. you have to add another sentence (here, in this case: a subordinate clause that starts with "dass") that describes this action. And this again makes it necessary, that you replace "etwas" with something, that refers to this sentence, and this is the word "da", but "da" and "für" snap together to one word, and this is "dafür":
Gertrud sorgt dafür, dass die Blumen genug Wasser haben.
Gertrud makes sure that the flowers have enough water.
Nice to know:
The pronominal adverb "dafür" is not separable in standard German. But in some regions people separate it in colloquial speech. Note, that this is not standard German, but you still might hear it:
Standard:
- Dafür musst du einen Hammer nehmen.
You have to take a hammer for that.- Ich kann nichts dafür.
I can't help it.- Ich bin auch dafür.
I also agree.Colloquial in northern regions:
- Da musst du einen Hammer für nehmen.
- Da kann ich nichts für.
- Da bin ich auch für.