Why is gold a safe haven investment? To me, it seems like gold is the ultimate example of something where the value should go down when the economy, stock market etc go down.

Just like Ferraris, and other frivolous consumer luxury goods, gold has almost no inherent value, beyond what we choose to assign to it. If the economy plummets, people will buy considerably fewer products that are made of gold, much like they will buy considerably fewer Ferraris.

Why does gold's value increase when times are tough? Why do we not see trouble on the stock market lead to increased prices for consumer essentials such as grains, textiles etc - things that have almost no change in demand when there is a change in income because they have real value (stopping people dying).

Note: Answers should be appropriate for countries that have left the gold standard. If there are any countries still on it, answers for those countries aren't what I am looking for.
You could see it as a coordination game and an example of a rational bubble. Because everybody knows that people start buying gold and the price goes up when the economy tanks, it is actually a good strategy to do just that. Similar things happen for example with the Swiss Franc.
The idea that the stock of gold is fixed and hence its price is stable, is empirically just plain wrong.
First, gold production varies with production cost and the demand for gold, just as in other industries. It is also dominated by a couple of countries and big mining companies with tons of market power.
Second, the price of gold is highly volatile, just as other commodities. The standard deviation of gold prices is much higher compared to for example Treasuries or G7 countries' equity indices.