# Why residual in Galerkin method is orthogonal to basis functions?

Let's consider equation of the form: $L(f(x)) = g(x)$

In Galerkin method we substitute f(x) with it's approximation and we get residual of the form: $$r(x) = \sum_{i=1}^N c_i \cdot \phi_i(x) - g(x)$$ Where the $c_i$ are coefficients and $\phi_i(x), i=1\ldots N$ is a set of basis functions. And ideally it should be equal to zero. Now we need to find $c_i$ to make residual as close as possible to zero.

Galerkin method states that in order to find coefficients $c_i$ the following integral should be equal to zero because residual is orthogonal to basis functions:

$$\int_a^b r(x) \phi_i(x)dx = 0, i=1\ldots N$$

Which I don't understand, let's consider $r(x)$ where $N=2$

$$r(x) = c_1\cdot \phi_1(x) + c_2\cdot \phi_2(x) - g(x)$$

How can $r(x)$ be orthogonal to $\phi_1(x)$ if there is a term $c_1\cdot \phi_1(x)$ in the residual?