  Magnetic interaction between photons and dipoles is essential in electronics,
sensing, spectroscopy, and quantum computing. However, its weak strength often
requires resonators to confine and store the photons. Here, we present mode
engineering techniques to create resonators with ultrasmall mode volume and
ultrahigh quality factor. In particular, we show that it is possible to achieve
an arbitrarily small mode volume only limited by materials or fabrication with
minimal quality-factor degradation. We compare mode-engineered cavities in a
trade-off space and show that the magnetic interaction can be strengthened more
than $10^{16}$ times compared to free space. Proof-of-principles experiments
using an ensemble of diamond nitrogen-vacancy spins show good agreement with
our theoretical predictions. These methods enable new applications from
high-cooperativity microwave-spin coupling in quantum computing or compact
electron paramagnetic resonance sensors to fundamental science such as dark
matter searches.
