find_t_star <- function(y, n, t_max = 50, grid = 500) {

  f_root <- function(tt) {
    phi <- mean(exp(1i * tt * y))
    Re(phi * Conj(phi)) - 1 / sqrt(n)
  }

  t_grid <- seq(1e-4, t_max, length.out = grid)
  f_vals <- vapply(t_grid, f_root, numeric(1))

  idx <- which(diff(sign(f_vals)) != 0)

  if (length(idx) == 0) {
    return(max(t_grid))  # ← THE fallback
  }

  uniroot(
    f_root,
    lower = t_grid[idx[1]],
    upper = t_grid[idx[1] + 1]
  )$root
}
