Why doesn't the T of "lunatic" flap in American English?
American English flap is regulated predominantly by stress. In order for "t" to get flapped, the syllable immediately before "t" must be stressed as strongly, or more strongly than, the following syllable where "t" is in the onset.
For example:
atom [ˈæ.t̬əm] --> the syllable [ˈæ] is stressed, the syllable [t̬əm] is unstressed, so there is a flap.
vs.
atomic [ə.ˈtɑ:.mɪk] --> the syllable [ə] is unstressed, the syllable [ˈtɑ:] is stressed, so there is no flap.
The words lunatic and heretic have a very weak, one might say "secondary", stress on the syllable tic. As a consequence, the preceeding syllable is NOT stronger than the syllable with "t", and therefore there is no flap.
heretic [ˈhe.rə.ˌtɪk] --> the syllable [rə] is extremely weak, the syllable [ˌtɪk] is also weak, but slightly stronger with some stress, so there is no flap.
lunatic [ˈlu:.nə.ˌtɪk] --> the syllable [nə] is completely unstressed, whereas the syllable [ˌtɪk] has some weak stress, so there is no flap.
We can compare these words to, say, erratic or clarity, where there is a flap.
erratic [e.ˈræ.t̬ɪk] -> the syllable [ræ] is stressed, and stronger than the weak syllable [tɪk], so there is a flap.
clarity [ˈkle.rə.t̬i] -> the syllable [rə] is unstressed, but the syllable [t̬i] is also unstressed, so both are equally strong, and there is a flap.
A great reference that should answer you question in greater detail is Eddington and Elzinga (2008), referenced below. Here is the table with their empirical corpus evidence they collected that summarises the relevant stress pattern and how likely it is to find a flap in them. The relevant row for this question has been highlighted.
References
Eddington, David and Dirk Elzinga (2008) The Phonetic Context of American English Flapping: Quantitative Evidence. Language and Speech 51.3, 245-266.