Current task: —

LiTightR.lean

4480 lines · 152 declarations · 0 sorries · updated 08-24-2026

Declarations

KindNameLine
lemmatsum_split_finset_complex33
defnearZerosSub61
lemmamem_nearZerosSub64
defseriesDiffTerm87
theoremseriesDiffTerm_summable91
theoremseriesFun_sub_seriesFun100
theoremRexpr_decomp116
theoremnear_sum_refPoint_le148
lemmadirichletTerm_norm_eq203
lemmahasDerivAt_neg_log_add_one_div220
lemmatendsto_neg_log_add_one_div232
lemmaintegrableOn_log_div_sq248
lemmaintegral_log_div_sq_Ioi_three260
lemmaantitoneOn_log_div_sq272
lemmasummable_log_div_sq_tail314
lemmatsum_log_div_sq_tail_le329
theoremdirichletC2_le_one353
theoremone_div_diff_le454
theoremlog_diff_le533
theoremdigamma_diff_le623
lemmalog_one_add_ge823
lemmaneg_log_one_sub_le895
lemmalog_one_point_one_ge967
lemmalog_ten_ge976
lemmalog_ten_le1049
lemmalog_eleven_ge1073
lemmalog_thirteen_le1083
lemmalog_fourteen_ge1114
lemmalog_23_13_le1194
lemmalog_17_14_le1207
lemmalog_two_point_three_le1219
lemmaone_div_exp_one_le1232
lemmalog_le_div_exp1242
lemmalog_log_le_div_exp1252
deffarW1263
lemmafarW_nonneg1265
lemmafarW_le_inv_sq1270
lemmafarW_le_inv_mul_sub1284
lemmahasSum_one_div_four_sub_telescope1301
lemmatsum_one_div_four_sub_telescope1340
lemmasummable_one_div_four_sub_telescope1345
lemmafarW_shift5_summable1350
lemmafarW_shift_summable1374
lemmafarW_exact_four1382
lemmafarW_tsum_le1430
lemmafarW_Ico_le1475
lemmaantitoneOn_one_div_sq1489
lemmahasDerivAt_neg_inv1497
lemmaintegrableOn_one_div_sq1507
lemmaintegral_one_div_sq_Ioi_ten1518
lemmatail11_inv_sq_le1529
lemmaintegrableOn_log_div_sq_ten1549
lemmaantitoneOn_log_div_sq_ten1554
lemmaintegral_log_div_sq_Ioi_ten1559
lemmatail11_log_div_sq_summable1570
lemmatail11_log_div_sq_le1584
lemmatail11_loglog_div_sq_le1606
lemmaantitoneOn_one_div_add_sq1656
lemmahasDerivAt_neg_inv_add1665
lemmatendsto_neg_inv_add1675
lemmaintegrableOn_one_div_add_sq1684
lemmaintegral_one_div_add_sq_Ioi_thirteen1695
lemmatail14_inv_add_sq_le1706
lemmaantitoneOn_log_div_add_sq1726
lemmahasDerivAt_log_div_add_sq_antideriv1781
lemmatendsto_log_div_add_sq_antideriv1821
lemmaintegrableOn_log_div_add_sq1878
lemmaintegral_log_div_add_sq_Ioi_thirteen1891
lemmatail14_log_div_add_sq_summable1912
lemmatail14_log_div_add_sq_le1926
lemmatail14_loglog_div_add_sq_le1949
lemmalog_add_le2001
lemmaexp_two_point_five_le_thirteen2013
lemmalog_thirteen_ge_five2041
lemmaC0hat_two_mul_add_one_le2047
lemmaC0hat_add_one_le_of_fourteen_le2087
lemmaC0hat_two_t_add_one_le2127
lemmaanalyticAt_iteratedDeriv_riemannZeta2172
lemmahasDerivAt_conj_of_conj_eq2183
lemmaiteratedDeriv_riemannZeta_conj2241
lemmaanalyticOrderAt_riemannZeta_conj2268
lemmazeroMult_conj2294
deffarG2309
lemmafarG_nonneg2313
lemmaabs_im_le_one_add_mul2321
lemmafarG_summable2333
lemmaseriesDiffTerm_norm_eq2427
lemmaseriesDiffTerm_norm_le_farG2447
lemmafar_series_norm_le_farG_tsum2531
lemmaunit_family_le2558
defmainFinset2608
lemmamem_mainFinset2612
lemmamainFinset_bucket_range2637
lemmafarG_le_farW_mul2659
lemmamain_bucket_count2700
lemmafarG_main_le2826
deffarGLeft2869
lemmafarGLeft_nonneg2872
lemmafarGLeft_summable2878
lemmafarGLeft_term_le2888
lemmafarGLeft_fiber_finite2940
lemmafarGLeft_fiber_count2959
lemmafarGLeft_fiber_le3004
lemmatail14_inv_add_sq_summable3076
lemmatail14_loglog_div_add_sq_summable3088
deffarGLeftMaj3114
lemmafarGLeftMaj_nonneg3117
lemmafarGLeftMaj_shift_le3129
lemmafarGLeftMaj_combo_summable3157
lemmafarGLeftMaj_shift_summable3172
lemmafarGLeftMaj_summable3178
lemmafarGLeftMaj_series_le3183
lemmafarGLeft_le3258
deffarGRight3290
lemmafarGRight_nonneg3293
lemmafarGRight_summable3299
lemmafarGRight_term_le3309
lemmafarGRight_fiber_finite3329
lemmafarGRight_fiber_count3354
lemmafarGRight_fiber_le3411
lemmaloglog_ge_neg_point373489
lemmafarGRightBridge_nonneg3521
deffarGRightMaj3542
lemmafarGRightMaj_nonneg3545
deffarGRightMajCombo3557
lemmafarGRightMajCombo_nonneg3561
lemmafarGRightMaj_le_combo3568
lemmatail11_inv_sq_summable3594
lemmatail11_loglog_div_sq_summable3606
lemmafarGRightMajCombo_shift_summable3632
lemmafarGRightMaj_summable3651
lemmafarGRightMaj_shift_summable3658
lemmafarGRightMaj_series_le3663
lemmafarGRight_le3757
lemmafarG_tail_eq3790
theoremfar_series_le3866
lemmaneg_log_one_sub_le_four3911
lemmalog_four_thirds_le3990
lemmalog_three_le4002
lemmalog_twelve_ge4025
lemmalog_twelve_le4043
lemmalog_two_point_five_zero_two_four_le4058
defpinchF4071
defpinchF'4074
lemmahasDerivAt_pinchF4076
lemmaantitoneOn_pinchF'4090
lemmapinchF_at_critical4101
lemmapinchF'_at_critical4111
lemmacontinuousOn_pinchF'4119
lemmapinch_tight_R4131
lemmaC0hat_le_of_ten_le4248
theoremlogDeriv_riemannZeta_sub_nearZeros_le_tight4299

Source

/-
NguyenRH1/LiTightR.lean — tier ii′ sub-stage ii′-5f (D-069): the tight
R-bound from the partial-fraction identity (the reference-point method,
D-056 §2, with the D-069 honest piece-2 two-unit count).

§1: the ℂ tsum split (variant of LiCircleBound's tsum_split_finset).
§2: the near-zero subtype finset `nearZerosSub` + membership.
§3: the seriesFun difference term + identity.
§4: the six-piece reference-point decomposition `Rexpr_decomp`.
§5: piece 2 — the near sum at s₀ = 2+it bounded by ZeroCountProp2.
§6: pieces 4/5 — the reciprocal differences ≤ 0.045.

Remaining (5f-ii): piece 1 (dirichletC2 ≤ 0.995), piece 6 (the sharp
digamma remainder via the level-2 EM), piece 3 (the far-sum interval
machine). (5f-iii): the assembly + the pinch lemma + the capstone
`logDeriv_riemannZeta_sub_nearZeros_le_tight`.

Archive references: D-069 (design + Findings 1/2), D-056 §2, journal-035.
-/
import Mathlib
import NguyenRH1.LiSqueeze

open Zeta23 Complex Filter Topology MeasureTheory

noncomputable section

namespace NguyenRH1

/-! ## 1. The ℂ tsum split -/

/-- Splitting a summable complex tsum over a finset (the ℂ variant of
LiCircleBound's `tsum_split_finset`). -/
lemma tsum_split_finset_complex {ι : Type*} [DecidableEq ι] {f : ι → ℂ} (hf : Summable f)
    (s : Finset ι) :
    ∑' i, f i = ∑ i ∈ s, f i + ∑' i, (if i ∈ s then 0 else f i) := by
  classical
  have h1 : Summable (fun i => if i ∈ s then f i else 0) := by
    apply Summable.of_norm_bounded hf.norm
    intro i
    by_cases hi : i ∈ s <;> simp [hi]
  have h2 : Summable (fun i => if i ∈ s then 0 else f i) := by
    apply Summable.of_norm_bounded hf.norm
    intro i
    by_cases hi : i ∈ s <;> simp [hi]
  have h3 : ∑' i, f i
      = ∑' i, ((if i ∈ s then f i else 0) + (if i ∈ s then 0 else f i)) := by
    apply tsum_congr
    intro i
    by_cases hi : i ∈ s <;> simp [hi]
  rw [h3, Summable.tsum_add h1 h2]
  congr 1
  rw [tsum_eq_sum (s := s) (fun b hb => if_neg hb)]
  exact Finset.sum_congr rfl (fun b hb => if_pos hb)

/-! ## 2. The near-zero subtype finset -/

/-- The near-zero subtype finset: the nontrivial zeros with |γ − t| < 1,
as a finset of the zero subtype (the `seriesFun` index type). The
containing ball: |γ−t| < 1 and 0 < re < 1 give ‖ρ‖ ≤ |re| + |im| <
|t| + 3. -/
def nearZerosSub (t : ℝ) : Finset {ρ : ℂ // IsNontrivialZero ρ} :=
  (finite_subtype_norm_lt (|t| + 3)).toFinset.filter fun ρ => |ρ.1.im - t| < 1

lemma mem_nearZerosSub {t : ℝ} {ρ : {ρ : ℂ // IsNontrivialZero ρ}} :
    ρ ∈ nearZerosSub t ↔ |ρ.1.im - t| < 1 := by
  rw [nearZerosSub, Finset.mem_filter]
  refine ⟨fun h => h.2, fun h => ⟨?_, h⟩⟩
  rw [Set.Finite.mem_toFinset]
  show ‖ρ.1‖ < |t| + 3
  have hle : ‖ρ.1‖ ≤ |ρ.1.re| + |ρ.1.im| := Complex.norm_le_abs_re_add_abs_im _
  have hre : |ρ.1.re| ≤ 1 := by
    have h1 : 0 < ρ.1.re := ρ.2.2.1
    have h2 : ρ.1.re < 1 := ρ.2.2.2
    rw [abs_le]
    constructor <;> linarith
  have him : |ρ.1.im| ≤ |t| + 1 := by
    calc |ρ.1.im| = |ρ.1.im - t + t| := by rw [sub_add_cancel]
      _ ≤ |ρ.1.im - t| + |t| := abs_add_le _ _
      _ ≤ 1 + |t| := by linarith [h]
      _ = |t| + 1 := by ring
  linarith [hle, hre, him]

/-! ## 3. The seriesFun difference identity -/

/-- The seriesFun difference term (a def so the tsum split matches
syntactically — no beta-redexes). -/
def seriesDiffTerm (s s₀ : ℂ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : ℂ :=
  (zeroMult ρ.1 : ℂ) * (1 / (s - ρ.1) - 1 / (s₀ - ρ.1))

/-- The difference series is summable (the two seriesFun summabilities). -/
theorem seriesDiffTerm_summable (s s₀ : ℂ) :
    Summable (seriesDiffTerm s s₀) := by
  apply Summable.congr ((seriesSummable s).sub (seriesSummable s₀))
  intro ρ
  show seriesTerm s ρ - seriesTerm s₀ ρ = seriesDiffTerm s s₀ ρ
  rw [seriesDiffTerm, seriesTerm, seriesTerm]
  ring

/-- seriesFun s − seriesFun s₀ as the termwise difference series. -/
theorem seriesFun_sub_seriesFun (s s₀ : ℂ) :
    seriesFun s - seriesFun s₀ = ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
      seriesDiffTerm s s₀ ρ := by
  rw [seriesFun, seriesFun, ← (seriesSummable s).tsum_sub (seriesSummable s₀)]
  refine tsum_congr fun ρ => ?_
  show seriesTerm s ρ - seriesTerm s₀ ρ = seriesDiffTerm s s₀ ρ
  rw [seriesDiffTerm, seriesTerm, seriesTerm]
  ring

/-! ## 4. The reference-point decomposition -/

/-- **The six-piece reference-point decomposition** (D-056 §2, D-069 §3):
with both s and s₀ good (so `partialFraction_identity` applies at both —
B cancels), the R-expression at s equals: the reference value, minus the
near sum at s₀, plus the far difference series, minus the elementary
differences, minus the digamma difference. -/
theorem Rexpr_decomp (hZC : ZeroCountProp) {s s₀ : ℂ}
    (hs : riemannZeta s ≠ 0) (hs0 : s ≠ 0) (hs1 : s ≠ 1)
    (hst : ∀ n : ℕ, 1 ≤ n → s ≠ -2 * (n : ℂ))
    (hs₀ : riemannZeta s₀ ≠ 0) (hs₀0 : s₀ ≠ 0) (hs₀1 : s₀ ≠ 1)
    (hs₀t : ∀ n : ℕ, 1 ≤ n → s₀ ≠ -2 * (n : ℂ)) (t : ℝ) :
    logDeriv riemannZeta s
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s - ρ.1)
      = logDeriv riemannZeta s₀
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
        + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
            if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
        - (1 / s - 1 / s₀) - (1 / (s - 1) - 1 / (s₀ - 1))
        - (Complex.digamma (s / 2) - Complex.digamma (s₀ / 2)) / 2 := by
  obtain ⟨B, hB⟩ := partialFraction_identity hZC
  have hsid := hB s hs hs0 hs1 hst
  have hs₀id := hB s₀ hs₀ hs₀0 hs₀1 hs₀t
  have hdiff := seriesFun_sub_seriesFun s s₀
  have hsplit := tsum_split_finset_complex (seriesDiffTerm_summable s s₀) (nearZerosSub t)
  have hnear : ∑ ρ ∈ nearZerosSub t, seriesDiffTerm s s₀ ρ
      = ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s - ρ.1)
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1) := by
    rw [← Finset.sum_sub_distrib]
    refine Finset.sum_congr rfl fun ρ _ => ?_
    rw [seriesDiffTerm, div_eq_mul_one_div, div_eq_mul_one_div]
    ring
  linear_combination hsid - hs₀id + hdiff + hsplit + hnear

/-! ## 5. Piece 2: the near sum at the reference point -/

/-- **Piece 2** (D-069 §3, honest two-unit form): the near sum at
s₀ = 2 + it is bounded by the ZeroCountProp2 count, since
|s₀ − ρ| ≥ 2 − re ρ ≥ 1 for every nontrivial zero. -/
theorem near_sum_refPoint_le (hZC2 : ZeroCountProp2) {t : ℝ} (ht : 10 ≤ t) :
    ‖∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / ((2 + t * Complex.I) - ρ.1)‖
      ≤ 0.6 * Real.log (t + 2) + 0.886 * Real.log (Real.log (t + 2)) + 2.7 := by
  classical
  have hterm : ∀ ρ : {ρ : ℂ // IsNontrivialZero ρ}, ρ ∈ nearZerosSub t →
      ‖(zeroMult ρ.1 : ℂ) / ((2 + t * Complex.I) - ρ.1)‖ ≤ (zeroMult ρ.1 : ℝ) := by
    intro ρ _
    have hρre : ρ.1.re < 1 := ρ.2.2.2
    have h1 : (1 : ℝ) ≤ ‖(2 + t * Complex.I) - ρ.1‖ := by
      have hre : ((2 + t * Complex.I) - ρ.1).re = 2 - ρ.1.re := by simp
      have h := abs_re_le_norm ((2 + t * Complex.I) - ρ.1)
      rw [hre] at h
      have h2 : (1 : ℝ) ≤ |2 - ρ.1.re| := by
        rw [abs_of_nonneg (by linarith)]
        linarith
      exact h2.trans h
    rw [norm_div]
    have h2 : ‖(zeroMult ρ.1 : ℂ)‖ = (zeroMult ρ.1 : ℝ) := by
      rw [show (zeroMult ρ.1 : ℂ) = ((zeroMult ρ.1 : ℝ) : ℂ) by push_cast; ring,
        Complex.norm_real]
      exact abs_of_nonneg (Nat.cast_nonneg _)
    rw [h2]
    calc (zeroMult ρ.1 : ℝ) / ‖(2 + t * Complex.I) - ρ.1‖
        ≤ (zeroMult ρ.1 : ℝ) / 1 :=
          div_le_div_of_nonneg_left (Nat.cast_nonneg _)
            (by norm_num : (0 : ℝ) < 1) h1
      _ = (zeroMult ρ.1 : ℝ) := div_one _
  have hbridge : ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℝ)
      = ∑ ρ ∈ (nearZerosSub t).image (fun ρ => ρ.1), (zeroMult ρ : ℝ) := by
    rw [Finset.sum_image (fun x _ y _ h => Subtype.ext h)]
  have hsub : (nearZerosSub t).image (fun ρ => ρ.1)
      ⊆ (zeroAbsWindow_finite t).toFinset := by
    intro ρ hρ
    rw [Finset.mem_image] at hρ
    obtain ⟨ρ', hρ'mem, hρ'eq⟩ := hρ
    have hnear := mem_nearZerosSub.mp hρ'mem
    rw [Set.Finite.mem_toFinset]
    show IsNontrivialZero ρ ∧ |ρ.im - t| ≤ 1
    rw [← hρ'eq]
    exact ⟨ρ'.2, hnear.le⟩
  calc ‖∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / ((2 + t * Complex.I) - ρ.1)‖
      ≤ ∑ ρ ∈ nearZerosSub t, ‖(zeroMult ρ.1 : ℂ) / ((2 + t * Complex.I) - ρ.1)‖ :=
        norm_sum_le _ _
    _ ≤ ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℝ) := Finset.sum_le_sum hterm
    _ = ∑ ρ ∈ (nearZerosSub t).image (fun ρ => ρ.1), (zeroMult ρ : ℝ) := hbridge
    _ ≤ ∑ ρ ∈ (zeroAbsWindow_finite t).toFinset, (zeroMult ρ : ℝ) :=
        Finset.sum_le_sum_of_subset_of_nonneg hsub (fun ρ _ _ => Nat.cast_nonneg _)
    _ = (((zeroAbsWindow_finite t).toFinset.sum fun ρ => zeroMult ρ) : ℝ) := rfl
    _ ≤ 0.6 * Real.log (t + 2) + 0.886 * Real.log (Real.log (t + 2)) + 2.7 :=
        hZC2 t ht

/-! ## 6. Piece 1: the Dirichlet bound at the reference point -/

/-- The Dirichlet term norm at 2: ‖LSeries.term Λ 2 n‖ = (if n = 0 then
0 else) Λ(n)/n². -/
lemma dirichletTerm_norm_eq (n : ℕ) :
    ‖LSeries.term (fun n => (ArithmeticFunction.vonMangoldt n : ℂ)) (2 : ℂ) n‖
      = if n = 0 then (0 : ℝ)
        else (ArithmeticFunction.vonMangoldt n) / (n : ℝ) ^ 2 := by
  rcases eq_or_ne n 0 with rfl | hn
  · simp [LSeries.term]
  · rw [LSeries.term_def, if_neg hn, norm_div]
    have h1 : ‖(ArithmeticFunction.vonMangoldt n : ℂ)‖
        = ArithmeticFunction.vonMangoldt n := by
      rw [Complex.norm_real]
      exact abs_of_nonneg ArithmeticFunction.vonMangoldt_nonneg
    have h2 : ‖((n : ℂ)) ^ (2 : ℂ)‖ = (n : ℝ) ^ 2 := by
      rw [show (2 : ℂ) = ((2 : ℕ) : ℂ) by norm_num, cpow_natCast, norm_pow,
        Complex.norm_natCast]
    rw [h1, h2, if_neg hn]

/-- The antiderivative derivative fact: d/dx [−((log x + 1)/x)] = log x/x². -/
lemma hasDerivAt_neg_log_add_one_div {x : ℝ} (hx0 : x ≠ 0) :
    HasDerivAt (fun y => -((Real.log y + 1) / y)) (Real.log x / x ^ 2) x := by
  have h1 := ((Real.hasDerivAt_log hx0).add_const 1).div (hasDerivAt_id' x) hx0
  have h2 := h1.neg
  rw [show (fun y => -((Real.log y + 1) / y))
      = -((fun x => Real.log x + 1) / fun x => x) from by
        ext y; simp only [Pi.neg_apply, Pi.div_apply]]
  rw [show Real.log x / x ^ 2 = -((x⁻¹ * x - (Real.log x + 1) * 1) / x ^ 2) from by
    rw [inv_mul_cancel₀ hx0]; ring]
  exact h2

/-- −((log x + 1)/x) → 0 as x → ∞. -/
lemma tendsto_neg_log_add_one_div :
    Tendsto (fun y => -((Real.log y + 1) / y)) atTop (𝓝 0) := by
  have hA : Tendsto (fun x => Real.log x / x) atTop (𝓝 0) := by
    have h := Real.tendsto_pow_log_div_mul_add_atTop 1 0 1 one_ne_zero
    simpa using h
  have hB : Tendsto (fun x => (1 : ℝ) / x) atTop (𝓝 0) := by
    simpa only [one_div] using tendsto_inv_atTop_zero
  have hC : Tendsto (fun x => Real.log x / x + 1 / x) atTop (𝓝 (0 + 0)) :=
    hA.add hB
  have heq : (fun y => -((Real.log y + 1) / y)) = (fun x => -(Real.log x / x + 1 / x)) := by
    ext x
    rw [add_div]
  rw [heq]
  simpa using hC.neg

/-- log x/x² is integrable on Ioi 3. -/
lemma integrableOn_log_div_sq :
    IntegrableOn (fun x => Real.log x / x ^ 2) (Set.Ioi (3 : ℝ)) volume := by
  apply integrableOn_Ioi_deriv_of_nonneg' (fun x hx =>
    hasDerivAt_neg_log_add_one_div (by
      have h := (Set.mem_Ici.mp hx); linarith))
  · intro x hx
    have hx3 : (3 : ℝ) < x := hx
    have hlog : (0 : ℝ) ≤ Real.log x := Real.log_nonneg (by linarith)
    positivity
  · exact tendsto_neg_log_add_one_div

/-- The integral of log x/x² over Ioi 3 is (log 3 + 1)/3. -/
lemma integral_log_div_sq_Ioi_three :
    ∫ x in Set.Ioi (3 : ℝ), Real.log x / x ^ 2 = (Real.log 3 + 1) / 3 := by
  have h := integral_Ioi_of_hasDerivAt_of_tendsto'
    (fun x hx => hasDerivAt_neg_log_add_one_div (by
      have h := (Set.mem_Ici.mp hx); linarith))
    integrableOn_log_div_sq tendsto_neg_log_add_one_div
  rw [h]
  show (0 : ℝ) - -((Real.log 3 + 1) / 3) = (Real.log 3 + 1) / 3
  ring

/-- log x/x² is antitone on Ici 3 (its derivative (1 − 2 log x)/x³ is
≤ 0 for x ≥ 3 since log x ≥ log 3 ≥ 1). -/
lemma antitoneOn_log_div_sq :
    AntitoneOn (fun x => Real.log x / x ^ 2) (Set.Ici (3 : ℝ)) := by
  apply antitoneOn_of_deriv_nonpos (convex_Ici 3)
  · intro x hx
    have hx0 : x ≠ 0 := by have h := (Set.mem_Ici.mp hx); linarith
    exact ((Real.differentiableAt_log hx0).div (differentiableAt_pow 2)
      (pow_ne_zero 2 hx0)).continuousAt.continuousWithinAt
  · intro x hx
    have hx0 : x ≠ 0 := by
      have h : (3 : ℝ) < x := Set.mem_Ioi.mp (by rwa [interior_Ici] at hx)
      linarith
    exact ((Real.differentiableAt_log hx0).div (differentiableAt_pow 2)
      (pow_ne_zero 2 hx0)).differentiableWithinAt
  · intro x hx
    have hx3 : (3 : ℝ) < x := Set.mem_Ioi.mp (by rwa [interior_Ici] at hx)
    have hx0 : x ≠ 0 := by linarith
    have hlog3 : (1 : ℝ) ≤ Real.log 3 := by
      have h := Real.log_le_log (Real.exp_pos 1)
        (by linarith [Real.exp_one_lt_d9.le] : Real.exp 1 ≤ 3)
      rwa [Real.log_exp] at h
    have hlogx : (1 : ℝ) ≤ Real.log x :=
      hlog3.trans (Real.log_le_log (by positivity : (0 : ℝ) < 3) hx3.le)
    have hderiv : deriv (fun y => Real.log y / y ^ 2) x
        = (1 - 2 * Real.log x) / x ^ 3 := by
      have h := (Real.hasDerivAt_log hx0).div (hasDerivAt_pow 2 x) (pow_ne_zero 2 hx0)
      have h'' : (fun y => Real.log y / y ^ 2) = Real.log / (fun y => y ^ 2) := by
        ext y
        rw [Pi.div_apply]
      rw [h'', h.deriv]
      have hsimp : ((2 : ℕ) : ℝ) * x ^ (2 - 1) = 2 * x := by
        rw [show (2 : ℕ) - 1 = 1 from rfl, pow_one]
        push_cast
        ring
      rw [hsimp]
      have hx1 : x⁻¹ * x ^ 2 = x := by
        rw [pow_two, ← mul_assoc, inv_mul_cancel₀ hx0, one_mul]
      rw [hx1]
      field_simp [hx0]
    rw [hderiv]
    exact div_nonpos_of_nonpos_of_nonneg (by linarith [hlogx]) (by positivity)

/-- Summability of the log-tail (integral test). -/
lemma summable_log_div_sq_tail :
    Summable (fun n : ℕ => Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2) := by
  have h := AntitoneOn.summable_of_integrableOn_Ioi antitoneOn_log_div_sq
    integrableOn_log_div_sq (fun t ht => by
      have ht3 : (3 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  -- h : Summable (fun n : ℕ ↦ (fun x => log x / x²) ↑n); shift by 4
  have h2 := (summable_nat_add_iff (f := fun n : ℕ =>
    Real.log (n : ℝ) / (n : ℝ) ^ 2) 4).mpr h
  refine h2.congr fun n => ?_
  push_cast
  ring_nf

/-- The tail bound: Σ_{n≥4} log n/n² ≤ (log 3 + 1)/3 (integral test). -/
lemma tsum_log_div_sq_tail_le :
    ∑' n : ℕ, Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2 ≤ (Real.log 3 + 1) / 3 := by
  have hle := antitoneOn_log_div_sq.tsum_comp_add_le_integral 3
    integrableOn_log_div_sq (fun t ht => by
      have ht3 : (3 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  -- hle : ∑' n, log ↑(n+3+1)/↑(n+3+1)² ≤ ∫ x in Ioi ↑3, log x/x²
  have hInt3 : (∫ x in Set.Ioi ((3 : ℕ) : ℝ), Real.log x / x ^ 2)
      = (Real.log 3 + 1) / 3 := by
    rw [show ((3 : ℕ) : ℝ) = 3 by norm_num]
    exact integral_log_div_sq_Ioi_three
  rw [hInt3] at hle
  have hcongr : (∑' n : ℕ, Real.log (↑(n + 3 + 1) : ℝ) / (↑(n + 3 + 1) : ℝ) ^ 2)
      = ∑' n : ℕ, Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2 := by
    refine tsum_congr fun n => ?_
    have h1 : ((↑(n + 3 + 1) : ℕ) : ℝ) = (n : ℝ) + 4 := by push_cast; ring
    rw [h1]
  rw [hcongr] at hle
  exact hle

/-- **Piece 1** (D-069 §3): dirichletC2 ≤ 1. The honest value is
−ζ′/ζ(2) ≈ 0.56997; the bound here uses Λ(n) ≤ log n, the exact head
n = 2, 3, and the integral-test tail (log 3 + 1)/3 ≤ 0.7. -/
theorem dirichletC2_le_one : dirichletC2 ≤ 1 := by
  rw [dirichletC2]
  have hsumm_g : Summable (fun n : ℕ =>
      ‖LSeries.term (fun n => (ArithmeticFunction.vonMangoldt n : ℂ)) (2 : ℂ) n‖) := by
    rw [summable_norm_iff]
    exact ArithmeticFunction.LSeriesSummable_vonMangoldt (by norm_num)
  -- the log majorant h n = if n = 0 then 0 else log n/n², and its tsum split
  set h : ℕ → ℝ := fun n => if n = 0 then (0 : ℝ)
    else Real.log n / (n : ℝ) ^ 2 with hh
  have hle_pointwise : ∀ n : ℕ,
      ‖LSeries.term (fun n => (ArithmeticFunction.vonMangoldt n : ℂ)) (2 : ℂ) n‖
        ≤ h n := by
    intro n
    rcases eq_or_ne n 0 with rfl | hn
    · rw [dirichletTerm_norm_eq 0, hh]
      simp
    · rw [dirichletTerm_norm_eq n, if_neg hn, hh]
      show ArithmeticFunction.vonMangoldt n / (n : ℝ) ^ 2
          ≤ if n = 0 then (0 : ℝ) else Real.log (n : ℝ) / (n : ℝ) ^ 2
      rw [if_neg hn]
      exact div_le_div_of_nonneg_right ArithmeticFunction.vonMangoldt_le_log
        (by positivity : (0 : ℝ) ≤ (n : ℝ) ^ 2)
  -- summability of h: the tail n + 4 is summable, and the head is finite
  have hsumh : Summable h := by
    have h1 : Summable (fun n : ℕ => h (n + 4)) := by
      apply summable_log_div_sq_tail.congr
      intro n
      rw [hh]
      show Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2
          = if (n + 4 : ℕ) = 0 then (0 : ℝ)
            else Real.log (↑(n + 4)) / (↑(n + 4) : ℝ) ^ 2
      rw [if_neg (by omega : (n + 4 : ℕ) ≠ 0)]
      push_cast
      ring_nf
    exact (summable_nat_add_iff 4).mp h1
  -- the head/tail split of the h-tsum
  have hsplit : ∑' n : ℕ, h n = (∑ n ∈ Finset.range 4, h n) + ∑' n : ℕ, h (n + 4) := by
    exact (hsumh.sum_add_tsum_nat_add 4).symm
  have hhead : ∑ n ∈ Finset.range 4, h n = Real.log 2 / 4 + Real.log 3 / 9 := by
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_zero]
    have h0 : h 0 = (0 : ℝ) := by rw [hh]; simp
    have h1' : h 1 = (0 : ℝ) := by rw [hh]; simp
    have h2' : h 2 = Real.log 2 / 4 := by
      rw [hh]
      show (if (2 : ℕ) = 0 then (0 : ℝ) else Real.log (↑(2:ℕ)) / (↑(2:ℕ) : ℝ) ^ 2) = _
      rw [if_neg (by norm_num)]
      push_cast
      ring_nf
    have h3' : h 3 = Real.log 3 / 9 := by
      rw [hh]
      show (if (3 : ℕ) = 0 then (0 : ℝ) else Real.log (↑(3:ℕ)) / (↑(3:ℕ) : ℝ) ^ 2) = _
      rw [if_neg (by norm_num)]
      push_cast
      ring_nf
    rw [h0, h1', h2', h3']
    ring
  have htail : ∑' n : ℕ, h (n + 4) ≤ (Real.log 3 + 1) / 3 := by
    have h1 : (∑' n : ℕ, h (n + 4))
        = ∑' n : ℕ, Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2 := by
      refine tsum_congr fun n => ?_
      rw [hh]
      show (if (n + 4 : ℕ) = 0 then (0 : ℝ) else Real.log (↑(n + 4)) / (↑(n + 4) : ℝ) ^ 2)
          = Real.log ((n : ℝ) + 4) / ((n : ℝ) + 4) ^ 2
      rw [if_neg (by omega : (n + 4 : ℕ) ≠ 0)]
      push_cast
      ring_nf
    rw [h1]
    exact tsum_log_div_sq_tail_le
  -- assemble
  have hmain : ∑' n : ℕ,
      ‖LSeries.term (fun n => (ArithmeticFunction.vonMangoldt n : ℂ)) (2 : ℂ) n‖
        ≤ Real.log 2 / 4 + Real.log 3 / 9 + (Real.log 3 + 1) / 3 := by
    calc ∑' n : ℕ,
        ‖LSeries.term (fun n => (ArithmeticFunction.vonMangoldt n : ℂ)) (2 : ℂ) n‖
        ≤ ∑' n : ℕ, h n := Summable.tsum_le_tsum hle_pointwise hsumm_g hsumh
      _ = (∑ n ∈ Finset.range 4, h n) + ∑' n : ℕ, h (n + 4) := hsplit
      _ ≤ (Real.log 2 / 4 + Real.log 3 / 9) + (Real.log 3 + 1) / 3 := by
          linarith [hhead, htail]
  -- the numeric fold: log 2 ≤ 0.6932, log 3 ≤ 1.1
  have hlog2 : Real.log 2 ≤ 0.6932 := Real.log_two_lt_d9.le.trans (by norm_num)
  have hlog3 : Real.log 3 ≤ 1.1 := by
    have h3le : (3 : ℝ) ≤ Real.exp 1.1 := by
      have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 1.1 by norm_num) 6
      -- Σ_{k<6} 1.1^k/k! = 3.00126… ≥ 3
      rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
        Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
        Finset.sum_range_zero] at h
      norm_num at h
      linarith [h]
    have h2 : Real.log 3 ≤ Real.log (Real.exp 1.1) :=
      Real.log_le_log (by norm_num : (0 : ℝ) < 3) h3le
    rwa [Real.log_exp] at h2
  have hlog2nn : (0 : ℝ) ≤ Real.log 2 := Real.log_nonneg (by norm_num)
  have hlog3nn : (0 : ℝ) ≤ Real.log 3 := Real.log_nonneg (by norm_num)
  linarith [hmain, hlog2, hlog3, hlog2nn, hlog3nn]

/-! ## 7. Pieces 4/5: the reciprocal differences -/

/-- **Pieces 4/5** (D-056 §2): the elementary reciprocal differences,
each ≤ 4.5/|t|² ≤ 0.045 (|s₀ − s| = |2 − σ| ≤ 4.5; ‖s‖, ‖s₀‖ ≥ t). -/
theorem one_div_diff_le {t : ℝ} (ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t) (hs0 : s ≠ 0) :
    ‖1 / s - 1 / (2 + t * Complex.I)‖ ≤ 0.045 ∧
      ‖1 / (s - 1) - 1 / ((2 + t * Complex.I) - 1)‖ ≤ 0.045 := by
  have ht0 : (0 : ℝ) < t := by linarith
  -- the difference s₀ − s is the real (2 − s.re)
  have hdiff : (2 + t * Complex.I) - s = ((2 - s.re : ℝ) : ℂ) := by
    rw [Complex.ext_iff]
    constructor
    · simp
    · simp [him]
  have hsdiff : ‖(2 + t * Complex.I) - s‖ = |2 - s.re| := by
    rw [hdiff]
    exact Complex.norm_real _
  have hsdiffle : ‖(2 + t * Complex.I) - s‖ ≤ 4.5 := by
    rw [hsdiff, abs_le]
    constructor <;> linarith
  have hs_norm : (t : ℝ) ≤ ‖s‖ := by
    have h := abs_im_le_norm s
    rw [him, abs_of_nonneg ht0.le] at h
    exact h
  have hs0_norm : (t : ℝ) ≤ ‖2 + t * Complex.I‖ := by
    have h := abs_im_le_norm (2 + t * Complex.I)
    rw [show (2 + t * Complex.I).im = t by simp, abs_of_nonneg ht0.le] at h
    exact h
  have hsm1_norm : (t : ℝ) ≤ ‖s - 1‖ := by
    have h := abs_im_le_norm (s - 1)
    rw [show (s - 1).im = t by simp [him], abs_of_nonneg ht0.le] at h
    exact h
  have hs0m1_norm : (t : ℝ) ≤ ‖(2 + t * Complex.I) - 1‖ := by
    have h := abs_im_le_norm ((2 + t * Complex.I) - 1)
    rw [show ((2 + t * Complex.I) - 1).im = t by simp, abs_of_nonneg ht0.le] at h
    exact h
  have hprods : (100 : ℝ) ≤ ‖s‖ * ‖2 + t * Complex.I‖ := by
    have h1 := mul_le_mul hs_norm hs0_norm ht0.le (by positivity : (0 : ℝ) ≤ ‖s‖)
    nlinarith [h1, ht]
  have hprods1 : (100 : ℝ) ≤ ‖s - 1‖ * ‖(2 + t * Complex.I) - 1‖ := by
    have h1 := mul_le_mul hsm1_norm hs0m1_norm ht0.le (by positivity : (0 : ℝ) ≤ ‖s - 1‖)
    nlinarith [h1, ht]
  constructor
  · -- 1/s − 1/s₀
    have hs₀0 : (2 + t * Complex.I) ≠ 0 := by
      intro h
      rw [h, norm_zero] at hs0_norm
      linarith
    rw [div_sub_div _ _ hs0 hs₀0]
    have hnum : 1 * (2 + t * Complex.I) - s * 1 = (2 + t * Complex.I) - s := by ring
    rw [hnum, norm_div, norm_mul]
    calc ‖(2 + t * Complex.I) - s‖ / (‖s‖ * ‖2 + t * Complex.I‖)
        ≤ 4.5 / (‖s‖ * ‖2 + t * Complex.I‖) :=
          div_le_div_of_nonneg_right hsdiffle (by positivity)
      _ ≤ 4.5 / 100 := div_le_div_of_nonneg_left (by norm_num) (by norm_num) hprods
      _ = 0.045 := by norm_num
  · -- 1/(s−1) − 1/(s₀−1)
    have hs1 : s - 1 ≠ 0 := by
      intro h
      rw [h, norm_zero] at hsm1_norm
      linarith
    have hs₀1 : (2 + t * Complex.I) - 1 ≠ 0 := by
      intro h
      rw [h, norm_zero] at hs0m1_norm
      linarith
    rw [div_sub_div _ _ hs1 hs₀1]
    have hnum : 1 * ((2 + t * Complex.I) - 1) - (s - 1) * 1
        = (2 + t * Complex.I) - s := by ring
    rw [hnum, norm_div, norm_mul]
    calc ‖(2 + t * Complex.I) - s‖ / (‖s - 1‖ * ‖(2 + t * Complex.I) - 1‖)
        ≤ 4.5 / (‖s - 1‖ * ‖(2 + t * Complex.I) - 1‖) :=
          div_le_div_of_nonneg_right hsdiffle (by positivity)
      _ ≤ 4.5 / 100 := div_le_div_of_nonneg_left (by norm_num) (by norm_num) hprods1
      _ = 0.045 := by norm_num

/-! ## 8. Piece 6: the digamma difference -/

/-- The log difference along the constant-imaginary segment: for
s = σ + it with σ ∈ [−5/2, 3] and t ≥ 10: ‖log (s/2 + 2) − log (1 +
(t/2)·I)‖ ≤ 5/t. The segment FTC: every point of the segment has
im = t/2 (so norm ≥ t/2) and re ≥ 3/4 (slit plane); the scalar
difference c = σ/2 + 1 ∈ [−1/4, 5/2]. -/
theorem log_diff_le {t : ℝ} (ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t) :
    ‖Complex.log (s / 2 + 2) - Complex.log (1 + ((t / 2 : ℝ) : ℂ) * Complex.I)‖ ≤ 5 / t := by
  have ht0 : (0 : ℝ) < t := by linarith
  set w₀ : ℂ := 1 + ((t / 2 : ℝ) : ℂ) * Complex.I with hw₀
  set c : ℝ := s.re / 2 + 1 with hc
  have hcle : |c| ≤ 5 / 2 := by
    rw [hc, abs_le]
    constructor <;> linarith
  -- the segment z(y) = w₀ + ↑y·↑c, y ∈ [0,1]
  set z : ℝ → ℂ := fun y => w₀ + (y : ℂ) * ((c : ℝ) : ℂ) with hz
  have hzim : ∀ y : ℝ, (z y).im = t / 2 := by
    intro y
    rw [hz, hw₀]
    simp [Complex.add_im, Complex.mul_im, Complex.ofReal_im, Complex.ofReal_re]
  have hznorm : ∀ y : ℝ, t / 2 ≤ ‖z y‖ := by
    intro y
    have h := abs_im_le_norm (z y)
    rw [hzim y, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)] at h
    exact h
  have hzslit : ∀ y : ℝ, z y ∈ Complex.slitPlane := by
    intro y
    rw [Complex.mem_slitPlane_iff]
    right
    rw [hzim y]
    positivity
  -- the endpoint: z 1 = w₀ + ↑c = s/2 + 2
  have htarget : w₀ + ((c : ℝ) : ℂ) = s / 2 + 2 := by
    rw [hw₀, hc]
    rw [Complex.ext_iff]
    constructor
    · have h1 : (s / 2 + 2).re = s.re / 2 + 2 := by simp
      rw [h1]
      simp only [Complex.add_re, Complex.mul_re, Complex.one_re, Complex.ofReal_re,
        Complex.I_re, Complex.I_im, Complex.ofReal_im, mul_zero, sub_zero, zero_mul]
      ring
    · have h1 : (s / 2 + 2).im = t / 2 := by simp [him]
      rw [h1]
      simp only [Complex.add_im, Complex.mul_im, Complex.one_im, Complex.ofReal_im,
        Complex.I_im, Complex.ofReal_re, mul_one, add_zero, zero_mul]
      ring
  -- the FTC derivative family
  have hanti : ∀ y ∈ Set.uIcc (0 : ℝ) 1,
      HasDerivAt (fun x : ℝ => Complex.log (z x)) (((z y)⁻¹) * ((c : ℝ) : ℂ)) y := by
    intro y _
    have h1 : HasDerivAt (fun u : ℂ => Complex.log (w₀ + u * ((c : ℝ) : ℂ)))
        (((z y)⁻¹) * ((c : ℝ) : ℂ)) ((y : ℝ) : ℂ) := by
      have h2 := (Complex.hasDerivAt_log (hzslit y)).comp ((y : ℝ) : ℂ)
        (((hasDerivAt_id' ((y : ℝ) : ℂ)).mul_const ((c : ℝ) : ℂ)).const_add w₀)
      simpa [hz, Function.comp_def, mul_comm, one_mul] using h2
    exact h1.comp_ofReal
  have hconti : IntervalIntegrable
      (fun y : ℝ => ((z y)⁻¹) * ((c : ℝ) : ℂ)) volume 0 1 := by
    apply ContinuousOn.intervalIntegrable
    apply Continuous.continuousOn
    have hzcont : Continuous z :=
      continuous_const.add (Complex.continuous_ofReal.mul continuous_const)
    exact (hzcont.inv₀ fun y => norm_pos_iff.mp (by
      linarith [hznorm y, ht0])).mul continuous_const
  -- the integral identity and bound
  have hint : ∫ y in (0 : ℝ)..(1 : ℝ), ((z y)⁻¹) * ((c : ℝ) : ℂ)
      = Complex.log (s / 2 + 2) - Complex.log w₀ := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hanti hconti]
    rw [hz]
    simp only [Complex.ofReal_one, one_mul, Complex.ofReal_zero, zero_mul, add_zero]
    rw [htarget]
  rw [← hint]
  have hbound : ∀ y ∈ Set.uIoc (0 : ℝ) 1,
      ‖((z y)⁻¹) * ((c : ℝ) : ℂ)‖ ≤ 5 / t := by
    intro y _
    have hzy : (0 : ℝ) < ‖z y‖ := by
      have h := hznorm y
      linarith [ht0]
    have h1 : ‖((z y)⁻¹) * ((c : ℝ) : ℂ)‖ = ‖z y‖⁻¹ * |c| := by
      rw [norm_mul, norm_inv, Complex.norm_real, Real.norm_eq_abs]
    rw [h1]
    have h2 : ‖z y‖⁻¹ ≤ (t / 2)⁻¹ := by
      rw [inv_le_inv₀ hzy (by positivity : (0 : ℝ) < t / 2)]
      exact hznorm y
    calc ‖z y‖⁻¹ * |c| ≤ (t / 2)⁻¹ * (5 / 2) :=
        mul_le_mul h2 hcle (abs_nonneg _) (by positivity)
      _ = 5 / t := by field_simp
  calc ‖∫ y in (0 : ℝ)..(1 : ℝ), ((z y)⁻¹) * ((c : ℝ) : ℂ)‖
      ≤ (5 / t) * |1 - 0| := intervalIntegral.norm_integral_le_of_norm_le_const hbound
    _ = 5 / t := by norm_num [abs_one]

/-- **Piece 6** (D-069 §3): the digamma difference
½‖ψ(s/2) − ψ(s₀/2)‖ ≤ 6/t. Via the +2 shift (re(w+2) ≥ 3/4), Zeta23's
digamma_stirling at w+2 and w₀, the recurrence ψ(w) = ψ(w+2) − w⁻¹ −
(w+1)⁻¹, and the segment-FTC log bound. -/
theorem digamma_diff_le {t : ℝ} (ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t) :
    ‖(Complex.digamma (s / 2) - Complex.digamma ((2 + t * Complex.I) / 2)) / 2‖
      ≤ 6 / t := by
  have ht0 : (0 : ℝ) < t := by linarith
  set w : ℂ := s / 2 with hw
  set w₀ : ℂ := (2 + t * Complex.I) / 2 with hw₀
  have hw₀eq : w₀ = 1 + ((t / 2 : ℝ) : ℂ) * Complex.I := by
    rw [hw₀]
    rw [show ((t / 2 : ℝ) : ℂ) = (t : ℂ) / 2 by push_cast; ring]
    ring
  have hwim : w.im = t / 2 := by rw [hw]; simp [him]
  have hw₀im : w₀.im = t / 2 := by rw [hw₀eq]; simp
  have himw : w.im ≠ 0 := by rw [hwim]; positivity
  have hwre : w.re = s.re / 2 := by rw [hw]; simp
  have hw₀re : (0 : ℝ) < w₀.re := by rw [hw₀eq]; simp
  -- the recurrence: ψ(w + 2) = ψ(w) + w⁻¹ + (w+1)⁻¹
  have hrec : Complex.digamma (w + 2) = Complex.digamma w + (w⁻¹ + (w + 1)⁻¹) := by
    have h := digamma_add_natCast himw 2
    rw [show w + ((2 : ℕ) : ℂ) = w + 2 by push_cast; ring] at h
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_zero] at h
    simp only [Nat.cast_zero, add_zero, Nat.cast_one, zero_add] at h
    exact h
  -- the Stirling bounds at w + 2 and w₀
  have hvre : (0 : ℝ) < (w + 2).re := by
    have h : (w + 2).re = w.re + 2 := by simp
    rw [h, hwre]
    linarith [hσlo]
  have hvim : (w + 2).im = t / 2 := by
    have h : (w + 2).im = w.im := by simp
    rw [h, hwim]
  have hStir1 := StirlingVert.digamma_stirling hvre (by
    rw [hvim, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)]; linarith)
  have hStir2 := StirlingVert.digamma_stirling hw₀re (by
    rw [hw₀im, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)]; linarith)
  -- the remainders
  set r1 : ℂ := Complex.digamma (w + 2) - Complex.log (w + 2) + (1 / 2) / (w + 2) with hr1
  set r0 : ℂ := Complex.digamma w₀ - Complex.log w₀ + (1 / 2) / w₀ with hr0
  have hr1le : ‖r1‖ ≤ 3 / (t / 2) ^ 2 := by
    have h := hStir1
    rw [hvim] at h
    exact h
  have hr0le : ‖r0‖ ≤ 3 / (t / 2) ^ 2 := by
    have h := hStir2
    rw [hw₀im] at h
    exact h
  -- the decomposition
  have hdecomp : Complex.digamma w - Complex.digamma w₀
      = (Complex.log (w + 2) - Complex.log w₀)
        + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹ - (w + 1)⁻¹ + (r1 - r0) := by
    rw [hr1, hr0]
    linear_combination -hrec
  -- the pieces
  have hL : ‖Complex.log (w + 2) - Complex.log w₀‖ ≤ 5 / t := by
    rw [hw, hw₀eq]
    exact log_diff_le ht hσlo hσhi him
  have hw0norm : t / 2 ≤ ‖w₀‖ := by
    have h1 := abs_im_le_norm w₀
    rw [hw₀im, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)] at h1
    exact h1
  have hw2norm : t / 2 ≤ ‖w + 2‖ := by
    have h1 := abs_im_le_norm (w + 2)
    rw [hvim, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)] at h1
    exact h1
  have hwnorm : t / 2 ≤ ‖w‖ := by
    have h1 := abs_im_le_norm w
    rw [hwim, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)] at h1
    exact h1
  have hw1norm : t / 2 ≤ ‖w + 1‖ := by
    have h1 := abs_im_le_norm (w + 1)
    have h2 : (w + 1).im = t / 2 := by simp [hwim]
    rw [h2, abs_of_nonneg (by linarith : (0 : ℝ) ≤ t / 2)] at h1
    exact h1
  have hw₀0 : w₀ ≠ 0 := by
    intro h
    rw [h, norm_zero] at hw0norm
    linarith
  have hw2 : w + 2 ≠ 0 := by
    intro h
    rw [h, norm_zero] at hw2norm
    linarith
  have hw0 : w ≠ 0 := by
    intro h
    rw [h, norm_zero] at hwnorm
    linarith
  have hw1 : w + 1 ≠ 0 := by
    intro h
    rw [h, norm_zero] at hw1norm
    linarith
  have h12 : ‖(1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)‖ ≤ 5 / t ^ 2 := by
    have h1 : (1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)
        = (1 / 2) * ((w + 2 - w₀) / (w₀ * (w + 2))) := by
      field_simp
    have hsub : w + 2 - w₀ = ((s.re / 2 + 1 : ℝ) : ℂ) := by
      have ht1 : w₀ + ((s.re / 2 + 1 : ℝ) : ℂ) = w + 2 := by
        have hw₀re_val : w₀.re = 1 := by rw [hw₀eq]; simp
        have hre : (w₀ + ((s.re / 2 + 1 : ℝ) : ℂ)).re = (w + 2).re := by
          have hw2re : (w + 2).re = s.re / 2 + 2 := by
            have h : (w + 2).re = w.re + 2 := by simp
            rw [h, hwre]
          rw [Complex.add_re, hw₀re_val, Complex.ofReal_re, hw2re]
          ring
        have him' : (w₀ + ((s.re / 2 + 1 : ℝ) : ℂ)).im = (w + 2).im := by
          have hw2im : (w + 2).im = t / 2 := by
            have h : (w + 2).im = w.im := by simp
            rw [h, hwim]
          rw [Complex.add_im, hw₀im, Complex.ofReal_im, hw2im]
          ring
        rw [Complex.ext_iff]
        exact ⟨hre, him'⟩
      linear_combination -ht1
    have h12n : ‖(1 / 2 : ℂ)‖ = 1 / 2 := by
      rw [show (1 / 2 : ℂ) = ((1 / 2 : ℝ) : ℂ) by push_cast; ring, Complex.norm_real]
      norm_num
    rw [h1, hsub, norm_mul, h12n, norm_div, Complex.norm_real, norm_mul]
    have hc : |s.re / 2 + 1| ≤ 5 / 2 := by
      rw [abs_le]
      constructor <;> linarith
    have hprod : (t / 2) * (t / 2) ≤ ‖w₀‖ * ‖w + 2‖ :=
      mul_le_mul hw0norm hw2norm (by positivity) (by positivity)
    calc (1 / 2) * (|s.re / 2 + 1| / (‖w₀‖ * ‖w + 2‖))
        ≤ (1 / 2) * ((5 / 2) / (‖w₀‖ * ‖w + 2‖)) := by
          apply mul_le_mul_of_nonneg_left _ (by norm_num)
          exact div_le_div_of_nonneg_right hc (by positivity)
      _ ≤ (1 / 2) * ((5 / 2) / ((t / 2) * (t / 2))) := by
          apply mul_le_mul_of_nonneg_left _ (by norm_num)
          exact div_le_div_of_nonneg_left (by norm_num) (by positivity) hprod
      _ = 5 / t ^ 2 := by field_simp
  have hw1b : ‖(w : ℂ)⁻¹‖ ≤ 2 / t := by
    rw [norm_inv]
    have hpos : (0 : ℝ) < ‖w‖ := by positivity
    calc ‖w‖⁻¹ ≤ (t / 2)⁻¹ := by
          rw [inv_le_inv₀ hpos (by positivity : (0 : ℝ) < t / 2)]
          exact hwnorm
      _ = 2 / t := by rw [inv_div]
  have hw1'b : ‖(w + 1 : ℂ)⁻¹‖ ≤ 2 / t := by
    rw [norm_inv]
    have hpos : (0 : ℝ) < ‖w + 1‖ := by positivity
    calc ‖w + 1‖⁻¹ ≤ (t / 2)⁻¹ := by
          rw [inv_le_inv₀ hpos (by positivity : (0 : ℝ) < t / 2)]
          exact hw1norm
      _ = 2 / t := by rw [inv_div]
  -- the norm chain
  have hnorm : ‖Complex.digamma w - Complex.digamma w₀‖
      ≤ 5 / t + 5 / t ^ 2 + (2 / t + 2 / t) + (3 / (t / 2) ^ 2 + 3 / (t / 2) ^ 2) := by
    rw [hdecomp]
    have hc1 : ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹ - (w + 1)⁻¹ + (r1 - r0)‖
        ≤ ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹ - (w + 1)⁻¹‖ + ‖r1 - r0‖ :=
      norm_add_le _ _
    have hc2 : ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹ - (w + 1)⁻¹‖
        ≤ ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹‖ + ‖(w + 1)⁻¹‖ :=
      norm_sub_le _ _
    have hc3 : ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)) - w⁻¹‖
        ≤ ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2))‖ + ‖w⁻¹‖ :=
      norm_sub_le _ _
    have hc4 : ‖(Complex.log (w + 2) - Complex.log w₀)
          + ((1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2))‖
        ≤ ‖Complex.log (w + 2) - Complex.log w₀‖
          + ‖(1 / 2 : ℂ) / w₀ - (1 / 2) / (w + 2)‖ :=
      norm_add_le _ _
    have hc5 : ‖r1 - r0‖ ≤ ‖r1‖ + ‖r0‖ := norm_sub_le _ _
    linarith [hc1, hc2, hc3, hc4, hc5, hL, h12, hw1b, hw1'b, hr1le, hr0le]
  -- the fold to 6/t
  have h2norm : ‖(2 : ℂ)‖ = 2 := by
    rw [show (2 : ℂ) = ((2 : ℝ) : ℂ) by push_cast; ring, Complex.norm_real]
    norm_num
  rw [hw, hw₀, norm_div, h2norm]
  rw [div_le_iff₀ (by norm_num : (0 : ℝ) < 2)]
  have h1 : (3 : ℝ) / (t / 2) ^ 2 = 12 / t ^ 2 := by
    field_simp
    ring
  have hsum : 5 / t + 5 / t ^ 2 + (2 / t + 2 / t) + (12 / t ^ 2 + 12 / t ^ 2)
      = 9 / t + 29 / t ^ 2 := by
    field_simp
    ring
  have hkey : 9 / t + 29 / t ^ 2 ≤ 12 / t := by
    rw [show (12 : ℝ) / t = 12 * t / t ^ 2 by field_simp]
    rw [show 9 / t + 29 / t ^ 2 = (9 * t + 29) / t ^ 2 by field_simp]
    apply div_le_div_of_nonneg_right _ (by positivity : (0 : ℝ) ≤ t ^ 2)
    nlinarith [ht]
  calc ‖Complex.digamma w - Complex.digamma w₀‖
      ≤ 5 / t + 5 / t ^ 2 + (2 / t + 2 / t) + (3 / (t / 2) ^ 2 + 3 / (t / 2) ^ 2) := hnorm
    _ = 9 / t + 29 / t ^ 2 := by rw [h1]; rw [hsum]
    _ ≤ 12 / t := hkey
    _ = 6 / t * 2 := by ring

/-! ## 9. Piece 3a: certified numerics for the far-sum bricks

The integral-comparison machines (`log_one_add_ge`, `neg_log_one_sub_le`)
and the certified log/exp bounds feeding the interval-count bridges and
the weight-sum bricks. -/

/-- log(1+x) ≥ x − x²/2 + x³/3 − x⁴/4 for x ≥ 0 (1/(1+t) ≥ 1−t+t²−t³ on
[0, x], since 1 − (1−t+t²−t³)(1+t) = t⁴ ≥ 0). -/
lemma log_one_add_ge {x : ℝ} (hx : 0 ≤ x) :
    x - x ^ 2 / 2 + x ^ 3 / 3 - x ^ 4 / 4 ≤ Real.log (1 + x) := by
  have hcont1 : ContinuousOn (fun t : ℝ => 1 / (1 + t)) (Set.uIcc 0 x) := by
    rw [Set.uIcc_of_le hx]
    apply ContinuousOn.div continuousOn_const (continuousOn_const.add continuousOn_id)
    intro t ht
    rw [Set.mem_Icc] at ht
    simp only [Pi.add_apply, id]
    linarith [ht.1]
  have hF : ∀ t ∈ Set.uIcc 0 x,
      HasDerivAt (fun y => Real.log (1 + y)) (1 / (1 + t)) t := by
    intro t ht
    rw [Set.uIcc_of_le hx, Set.mem_Icc] at ht
    have h1 : HasDerivAt (fun y : ℝ => 1 + y) (1 : ℝ) t := by
      simpa using (hasDerivAt_id' t).const_add 1
    have h2 := (Real.hasDerivAt_log (by linarith : (1 : ℝ) + t ≠ 0)).comp t h1
    rw [one_div]
    simpa [Function.comp_def] using h2
  have hG : ∀ t ∈ Set.uIcc 0 x,
      HasDerivAt (fun y => y - y ^ 2 / 2 + y ^ 3 / 3 - y ^ 4 / 4)
        (1 - t + t ^ 2 - t ^ 3) t := by
    intro t _
    have ha : HasDerivAt (fun y : ℝ => y) (1 : ℝ) t := hasDerivAt_id' t
    have hb : HasDerivAt (fun y : ℝ => y ^ 2 / 2) t t := by
      have h := (hasDerivAt_pow 2 t).div_const (2 : ℝ)
      rw [show ((2 : ℕ) : ℝ) * t ^ (2 - 1) / 2 = t from by
        rw [show (2 : ℕ) - 1 = 1 from rfl, pow_one]
        push_cast
        ring] at h
      exact h
    have hc : HasDerivAt (fun y : ℝ => y ^ 3 / 3) (t ^ 2) t := by
      have h := (hasDerivAt_pow 3 t).div_const (3 : ℝ)
      rw [show ((3 : ℕ) : ℝ) * t ^ (3 - 1) / 3 = t ^ 2 from by
        rw [show (3 : ℕ) - 1 = 2 from rfl]
        push_cast
        ring] at h
      exact h
    have hd : HasDerivAt (fun y : ℝ => y ^ 4 / 4) (t ^ 3) t := by
      have h := (hasDerivAt_pow 4 t).div_const (4 : ℝ)
      rw [show ((4 : ℕ) : ℝ) * t ^ (4 - 1) / 4 = t ^ 3 from by
        rw [show (4 : ℕ) - 1 = 3 from rfl]
        push_cast
        ring] at h
      exact h
    exact ((ha.sub hb).add hc).sub hd
  have hInt1 : ∫ t in (0 : ℝ)..x, 1 / (1 + t) = Real.log (1 + x) := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hF
      (ContinuousOn.intervalIntegrable hcont1)]
    simp [Real.log_one]
  have hInt2 : ∫ t in (0 : ℝ)..x, (1 - t + t ^ 2 - t ^ 3)
      = x - x ^ 2 / 2 + x ^ 3 / 3 - x ^ 4 / 4 := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hG
      (ContinuousOn.intervalIntegrable (by fun_prop))]
    ring_nf
  have hmono : ∫ t in (0 : ℝ)..x, (1 - t + t ^ 2 - t ^ 3)
      ≤ ∫ t in (0 : ℝ)..x, 1 / (1 + t) := by
    apply intervalIntegral.integral_mono_on hx
      (ContinuousOn.intervalIntegrable (by fun_prop))
      (ContinuousOn.intervalIntegrable hcont1)
    intro t ht
    rw [Set.mem_Icc] at ht
    have h1 : (0 : ℝ) < 1 + t := by linarith [ht.1]
    rw [le_div_iff₀ h1]
    have h2 : (1 - t + t ^ 2 - t ^ 3) * (1 + t) = 1 - t ^ 4 := by ring
    rw [h2]
    have h3 : t ^ 4 ≥ 0 := by positivity
    linarith
  rw [hInt1, hInt2] at hmono
  exact hmono

/-- −log(1−y) ≤ y + y²/2 + y³/(3(1−y)) for 0 ≤ y < 1
(1/(1−t) ≤ 1 + t + t²/(1−y) on [0, y]). -/
lemma neg_log_one_sub_le {y : ℝ} (hy0 : 0 ≤ y) (hy1 : y < 1) :
    -Real.log (1 - y) ≤ y + y ^ 2 / 2 + y ^ 3 / (3 * (1 - y)) := by
  have hcont1 : ContinuousOn (fun t : ℝ => 1 / (1 - t)) (Set.uIcc 0 y) := by
    rw [Set.uIcc_of_le hy0]
    apply ContinuousOn.div continuousOn_const (continuousOn_const.sub continuousOn_id)
    intro t ht
    rw [Set.mem_Icc] at ht
    simp only [Pi.sub_apply, id]
    linarith [ht.2, hy1]
  have hF : ∀ t ∈ Set.uIcc 0 y,
      HasDerivAt (fun x => -Real.log (1 - x)) (1 / (1 - t)) t := by
    intro t ht
    rw [Set.uIcc_of_le hy0, Set.mem_Icc] at ht
    have h1 : HasDerivAt (fun x : ℝ => 1 - x) (-1 : ℝ) t := by
      simpa using (hasDerivAt_id' t).const_sub 1
    have h2 := ((Real.hasDerivAt_log (by linarith : (1 : ℝ) - t ≠ 0)).comp t h1).neg
    rw [show (-(Real.log ∘ fun x : ℝ => 1 - x)) = (fun x => -Real.log (1 - x)) from by
      ext y'
      simp only [Pi.neg_apply, Function.comp_apply]] at h2
    simpa [Function.comp_def, mul_neg, neg_neg, one_mul, one_div] using h2
  have hG : ∀ t ∈ Set.uIcc 0 y,
      HasDerivAt (fun x => x + x ^ 2 / 2 + x ^ 3 / (3 * (1 - y)))
        (1 + t + t ^ 2 / (1 - y)) t := by
    intro t _
    have ha : HasDerivAt (fun x : ℝ => x) (1 : ℝ) t := hasDerivAt_id' t
    have hb : HasDerivAt (fun x : ℝ => x ^ 2 / 2) t t := by
      have h := (hasDerivAt_pow 2 t).div_const (2 : ℝ)
      rw [show ((2 : ℕ) : ℝ) * t ^ (2 - 1) / 2 = t from by
        rw [show (2 : ℕ) - 1 = 1 from rfl, pow_one]
        push_cast
        ring] at h
      exact h
    have hc : HasDerivAt (fun x : ℝ => x ^ 3 / (3 * (1 - y))) (t ^ 2 / (1 - y)) t := by
      have h := (hasDerivAt_pow 3 t).div_const (3 * (1 - y))
      rw [show ((3 : ℕ) : ℝ) * t ^ (3 - 1) / (3 * (1 - y)) = t ^ 2 / (1 - y) from by
        rw [show (3 : ℕ) - 1 = 2 from rfl]
        push_cast
        field_simp [show (1 : ℝ) - y ≠ 0 by linarith [hy1]]] at h
      exact h
    exact (ha.add hb).add hc
  have hInt1 : ∫ t in (0 : ℝ)..y, 1 / (1 - t) = -Real.log (1 - y) := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hF
      (ContinuousOn.intervalIntegrable hcont1)]
    simp [Real.log_one]
  have hInt2 : ∫ t in (0 : ℝ)..y, (1 + t + t ^ 2 / (1 - y))
      = y + y ^ 2 / 2 + y ^ 3 / (3 * (1 - y)) := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hG
      (ContinuousOn.intervalIntegrable (by fun_prop))]
    ring_nf
  have hmono : ∫ t in (0 : ℝ)..y, 1 / (1 - t)
      ≤ ∫ t in (0 : ℝ)..y, (1 + t + t ^ 2 / (1 - y)) := by
    apply intervalIntegral.integral_mono_on hy0
      (ContinuousOn.intervalIntegrable hcont1)
      (ContinuousOn.intervalIntegrable (by fun_prop))
    intro t ht
    rw [Set.mem_Icc] at ht
    have h1 : (0 : ℝ) < 1 - t := by linarith [ht.2, hy1]
    have h2 : (0 : ℝ) < 1 - y := by linarith [hy1]
    rw [div_le_iff₀ h1]
    have h3 : (1 + t + t ^ 2 / (1 - y)) * (1 - t)
        = 1 - t ^ 2 + t ^ 2 * (1 - t) / (1 - y) := by field_simp; ring
    rw [h3]
    have h4 : (t : ℝ) ^ 2 ≤ t ^ 2 * (1 - t) / (1 - y) := by
      have h5 : (1 - y) ≤ (1 - t) := by linarith [ht.2]
      have h6 : (0 : ℝ) ≤ t ^ 2 := by positivity
      rw [le_div_iff₀ h2]
      exact mul_le_mul_of_nonneg_left h5 h6
    linarith
  rw [hInt1, hInt2] at hmono
  exact hmono

/-- log 1.1 ≥ 0.0953 (the x = 0.1 alternating bound). -/
lemma log_one_point_one_ge : (0.0953 : ℝ) ≤ Real.log 1.1 := by
  have h := log_one_add_ge (show (0 : ℝ) ≤ (0.1 : ℝ) by norm_num)
  have h2 : (1 : ℝ) + (0.1 : ℝ) = 1.1 := by norm_num
  rw [h2] at h
  norm_num at h ⊢
  linarith [h]

/-- log 10 ≥ 2.3025: log 10 = 1 + 2·log 2 + log(2.5/e), with
log(2.5/e) ≥ −(y + y²/2 + y³/(3(1−y))), y = 1 − 2.5/e ≤ 0.0803021. -/
lemma log_ten_ge : (2.3025 : ℝ) ≤ Real.log 10 := by
  have hlog2 : (0.6931471 : ℝ) ≤ Real.log 2 :=
    (by norm_num : (0.6931471 : ℝ) ≤ 0.6931471803).trans Real.log_two_gt_d9.le
  have he1 : (2.7182818 : ℝ) ≤ Real.exp 1 :=
    (by norm_num : (2.7182818 : ℝ) ≤ 2.7182818283).trans Real.exp_one_gt_d9.le
  have he2 : Real.exp 1 ≤ (2.7182819 : ℝ) :=
    Real.exp_one_lt_d9.le.trans (by norm_num)
  -- y := 1 − 2.5/e and its bounds
  set y : ℝ := 1 - 2.5 / Real.exp 1 with hy
  have hylo : (0 : ℝ) ≤ y := by
    rw [hy]
    have h : (2.5 : ℝ) / Real.exp 1 ≤ 2.5 / 2.7182818 :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 2.5) (by norm_num) he1
    have h2 : (2.5 : ℝ) / 2.7182818 ≤ 1 := by norm_num
    linarith [Real.exp_pos 1, h, h2]
  have hyub : y ≤ (0.0803021 : ℝ) := by
    rw [hy]
    have h : (2.5 : ℝ) / 2.7182819 ≤ 2.5 / Real.exp 1 :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 2.5) (Real.exp_pos 1) he2
    have h2 : (0.9196979 : ℝ) ≤ 2.5 / 2.7182819 := by norm_num
    have h3 := h2.trans h
    linarith
  have h1my : (0.9196979 : ℝ) ≤ 1 - y := by
    rw [hy]
    have h : (2.5 : ℝ) / 2.7182819 ≤ 2.5 / Real.exp 1 :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 2.5) (Real.exp_pos 1) he2
    have h2 : (0.9196979 : ℝ) ≤ 2.5 / 2.7182819 := by norm_num
    have h3 := h2.trans h
    linarith
  have hy1 : y < 1 := by
    have h : (0 : ℝ) < 2.5 / Real.exp 1 := by positivity
    rw [hy]
    linarith
  have hmain := neg_log_one_sub_le hylo hy1
  -- −log(1−y) ≤ 0.0837141
  have hub : -Real.log (1 - y) ≤ (0.0837141 : ℝ) := by
    have h1 : y + y ^ 2 / 2 + y ^ 3 / (3 * (1 - y))
        ≤ (0.0803021 : ℝ) + 0.0803021 ^ 2 / 2 + 0.0803021 ^ 3 / (3 * 0.9196979) := by
      have h2 : y ^ 3 / (3 * (1 - y)) ≤ 0.0803021 ^ 3 / (3 * 0.9196979) := by
        have h4 : (0 : ℝ) < 3 * (1 - y) := by positivity
        rw [div_le_div_iff₀ h4 (by positivity : (0 : ℝ) < 3 * 0.9196979)]
        have h5 : y ^ 3 ≤ 0.0803021 ^ 3 := pow_le_pow_left₀ hylo hyub 3
        have h6 : (3 * 0.9196979 : ℝ) ≤ 3 * (1 - y) := by linarith [h1my]
        calc y ^ 3 * (3 * 0.9196979) ≤ 0.0803021 ^ 3 * (3 * 0.9196979) :=
              mul_le_mul_of_nonneg_right h5 (by norm_num)
          _ ≤ 0.0803021 ^ 3 * (3 * (1 - y)) :=
              mul_le_mul_of_nonneg_left h6 (by positivity)
      have h7 : y ^ 2 ≤ 0.0803021 ^ 2 := pow_le_pow_left₀ hylo hyub 2
      linarith [h2, h7]
    have h8 : (0.0803021 : ℝ) + 0.0803021 ^ 2 / 2 + 0.0803021 ^ 3 / (3 * 0.9196979)
        ≤ 0.0837141 := by norm_num
    linarith [hmain, h1, h8]
  -- log(2.5/e) = log(1−y) ≥ −0.0837141
  have hlog25 : Real.log (2.5 / Real.exp 1) = Real.log (1 - y) := by
    rw [hy]
    congr 1
    ring
  -- the decomposition log 10 = 1 + 2 log 2 + log(2.5/e)
  have hdecomp : Real.log 10 = 1 + 2 * Real.log 2 + Real.log (2.5 / Real.exp 1) := by
    have h1 : (10 : ℝ) = Real.exp 1 * (2 * (2 * (2.5 / Real.exp 1))) := by
      field_simp [Real.exp_ne_zero 1]
      ring
    rw [h1, Real.log_mul (Real.exp_ne_zero 1)
      (by positivity : (2 : ℝ) * (2 * (2.5 / Real.exp 1)) ≠ 0), Real.log_exp]
    have h2 : Real.log (2 * (2 * (2.5 / Real.exp 1)))
        = Real.log 2 + Real.log (2 * (2.5 / Real.exp 1)) :=
      Real.log_mul (by norm_num) (by positivity : (2 : ℝ) * (2.5 / Real.exp 1) ≠ 0)
    rw [h2, Real.log_mul (by norm_num : (2 : ℝ) ≠ 0)
      (by positivity : (2.5 : ℝ) / Real.exp 1 ≠ 0)]
    ring
  linarith [hdecomp, hlog2, hub, hlog25]

/-- log 10 ≤ 2.3026: 10 ≤ (Σ_{k<9} 1.1513ᵏ/k!)² ≤ e^{1.1513·2} = e^{2.3026}. -/
lemma log_ten_le : Real.log 10 ≤ (2.3026 : ℝ) := by
  have hbase : (3.1622865 : ℝ) ≤ Real.exp 1.1513 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 1.1513 by norm_num) 9
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have hsq : (10 : ℝ) ≤ Real.exp 1.1513 * Real.exp 1.1513 := by
    have h1 : (3.1622865 : ℝ) * 3.1622865 ≤ Real.exp 1.1513 * Real.exp 1.1513 :=
      mul_le_mul hbase hbase (by norm_num) (Real.exp_pos 1.1513).le
    have h2 : (3.1622865 : ℝ) * 3.1622865 ≥ 10 := by norm_num
    exact h2.trans h1
  have h2 : Real.exp 1.1513 * Real.exp 1.1513 = Real.exp (1.1513 + 1.1513) := by
    rw [Real.exp_add]
  rw [h2] at hsq
  have h3 : (1.1513 : ℝ) + 1.1513 = 2.3026 := by norm_num
  rw [h3] at hsq
  have h4 : Real.log 10 ≤ Real.log (Real.exp 2.3026) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 10) hsq
  rwa [Real.log_exp] at h4

/-- log 11 ≥ 2.3978 (log 11 = log 10 + log 1.1). -/
lemma log_eleven_ge : (2.3978 : ℝ) ≤ Real.log 11 := by
  have h1 : (11 : ℝ) = 10 * 1.1 := by norm_num
  have h2 : Real.log 11 = Real.log 10 + Real.log 1.1 := by
    rw [h1]
    exact Real.log_mul (by norm_num : (10 : ℝ) ≠ 0) (by norm_num : (1.1 : ℝ) ≠ 0)
  have h3 := log_ten_ge
  have h4 := log_one_point_one_ge
  linarith [h2, h3, h4]

/-- log 13 ≤ 2.565: 13 ≤ (Σ_{k<7} 0.64125ᵏ/k!)⁴ ≤ e^{2.565}. -/
lemma log_thirteen_le : Real.log 13 ≤ (2.565 : ℝ) := by
  have hbase : (1.8988433 : ℝ) ≤ Real.exp 0.64125 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.64125 by norm_num) 7
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have hsq2 : (1.8988433 : ℝ) * 1.8988433 ≤ Real.exp 0.64125 * Real.exp 0.64125 :=
    mul_le_mul hbase hbase (by norm_num) (Real.exp_pos 0.64125).le
  have hsq2n : (1.8988433 : ℝ) * 1.8988433 ≥ 3.6056054 := by norm_num
  have hsq4 : (3.6056054 : ℝ) * 3.6056054
      ≤ (Real.exp 0.64125 * Real.exp 0.64125) * (Real.exp 0.64125 * Real.exp 0.64125) := by
    have h1 : (3.6056054 : ℝ) ≤ Real.exp 0.64125 * Real.exp 0.64125 := hsq2n.trans hsq2
    exact mul_le_mul h1 h1 (by norm_num) (by positivity)
  have hsq4n : (3.6056054 : ℝ) * 3.6056054 ≥ 13 := by norm_num
  have h2 : (Real.exp 0.64125 * Real.exp 0.64125) * (Real.exp 0.64125 * Real.exp 0.64125)
      = Real.exp (0.64125 * 4) := by
    rw [show (0.64125 : ℝ) * 4 = 0.64125 + 0.64125 + (0.64125 + 0.64125) from by ring,
      Real.exp_add, Real.exp_add]
  have h3 : (0.64125 : ℝ) * 4 = 2.565 := by norm_num
  have h13 : (13 : ℝ) ≤ Real.exp 2.565 := by
    have h4 := hsq4n.trans hsq4
    rw [h2, h3] at h4
    exact h4
  have h5 : Real.log 13 ≤ Real.log (Real.exp 2.565) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 13) h13
  rwa [Real.log_exp] at h5

/-- log 14 ≥ 2.6388: log 14 = log 2 + 2 + log(7/e²), with
log(7/e²) ≥ −(y + y²/2 + y³/(3(1−y))), y = 1 − 7/e² ≤ 0.05266. -/
lemma log_fourteen_ge : (2.6388 : ℝ) ≤ Real.log 14 := by
  have hlog2 : (0.6931471 : ℝ) ≤ Real.log 2 :=
    (by norm_num : (0.6931471 : ℝ) ≤ 0.6931471803).trans Real.log_two_gt_d9.le
  have he1 : (2.7182818 : ℝ) ≤ Real.exp 1 :=
    (by norm_num : (2.7182818 : ℝ) ≤ 2.7182818283).trans Real.exp_one_gt_d9.le
  have he2 : Real.exp 1 ≤ (2.7182819 : ℝ) :=
    Real.exp_one_lt_d9.le.trans (by norm_num)
  have he2sq : Real.exp 1 * Real.exp 1 ≤ (7.3890566 : ℝ) := by
    have h := mul_le_mul he2 he2 (Real.exp_pos 1).le (by norm_num)
    have h2 : (2.7182819 : ℝ) * 2.7182819 ≤ 7.3890566 := by norm_num
    exact h.trans h2
  have he1sq : (7.3890559 : ℝ) ≤ Real.exp 1 * Real.exp 1 := by
    have h := mul_le_mul he1 he1 (by norm_num) (Real.exp_pos 1).le
    have h2 : (7.3890559 : ℝ) ≤ 2.7182818 * 2.7182818 := by norm_num
    exact h2.trans h
  set y : ℝ := 1 - 7 / (Real.exp 1 * Real.exp 1) with hy
  have hylo : (0 : ℝ) ≤ y := by
    rw [hy]
    have h : (7 : ℝ) / (Real.exp 1 * Real.exp 1) ≤ 7 / 7.3890559 :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 7) (by norm_num) he1sq
    have h2 : (7 : ℝ) / 7.3890559 ≤ 1 := by norm_num
    have h3 := h.trans h2
    linarith
  have hyub : y ≤ (0.05266 : ℝ) := by
    rw [hy]
    have h : (7 : ℝ) / 7.3890566 ≤ 7 / (Real.exp 1 * Real.exp 1) :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 7) (by norm_num) he2sq
    have h2 : (0.94734 : ℝ) ≤ 7 / 7.3890566 := by norm_num
    have h3 := h2.trans h
    linarith
  have h1my : (0.94734 : ℝ) ≤ 1 - y := by
    rw [hy]
    have h : (7 : ℝ) / 7.3890566 ≤ 7 / (Real.exp 1 * Real.exp 1) :=
      div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 7) (by norm_num) he2sq
    have h2 : (0.94734 : ℝ) ≤ 7 / 7.3890566 := by norm_num
    have h3 := h2.trans h
    linarith
  have hy1 : y < 1 := by
    have h : (0 : ℝ) < 7 / (Real.exp 1 * Real.exp 1) := by positivity
    rw [hy]
    linarith
  have hmain := neg_log_one_sub_le hylo hy1
  have hub : -Real.log (1 - y) ≤ (0.054098 : ℝ) := by
    have h1 : y + y ^ 2 / 2 + y ^ 3 / (3 * (1 - y))
        ≤ (0.05266 : ℝ) + 0.05266 ^ 2 / 2 + 0.05266 ^ 3 / (3 * 0.94734) := by
      have h2 : y ^ 3 / (3 * (1 - y)) ≤ 0.05266 ^ 3 / (3 * 0.94734) := by
        have h4 : (0 : ℝ) < 3 * (1 - y) := by positivity
        rw [div_le_div_iff₀ h4 (by positivity : (0 : ℝ) < 3 * 0.94734)]
        have h5 : y ^ 3 ≤ 0.05266 ^ 3 := pow_le_pow_left₀ hylo hyub 3
        have h6 : (3 * 0.94734 : ℝ) ≤ 3 * (1 - y) := by linarith [h1my]
        calc y ^ 3 * (3 * 0.94734) ≤ 0.05266 ^ 3 * (3 * 0.94734) :=
              mul_le_mul_of_nonneg_right h5 (by norm_num)
          _ ≤ 0.05266 ^ 3 * (3 * (1 - y)) :=
              mul_le_mul_of_nonneg_left h6 (by positivity)
      have h7 : y ^ 2 ≤ 0.05266 ^ 2 := pow_le_pow_left₀ hylo hyub 2
      linarith [h2, h7]
    have h8 : (0.05266 : ℝ) + 0.05266 ^ 2 / 2 + 0.05266 ^ 3 / (3 * 0.94734)
        ≤ 0.054098 := by norm_num
    linarith [hmain, h1, h8]
  have hlog7 : Real.log (7 / (Real.exp 1 * Real.exp 1)) = Real.log (1 - y) := by
    rw [hy]
    congr 1
    ring
  have hdecomp : Real.log 14
      = Real.log 2 + 2 + Real.log (7 / (Real.exp 1 * Real.exp 1)) := by
    have h1 : (14 : ℝ) = 2 * ((Real.exp 1 * Real.exp 1) * (7 / (Real.exp 1 * Real.exp 1))) := by
      field_simp [Real.exp_ne_zero 1]
      ring
    rw [h1, Real.log_mul (by norm_num : (2 : ℝ) ≠ 0)
      (by positivity : (Real.exp 1 * Real.exp 1) * (7 / (Real.exp 1 * Real.exp 1)) ≠ 0)]
    have h2 : Real.log ((Real.exp 1 * Real.exp 1) * (7 / (Real.exp 1 * Real.exp 1)))
        = Real.log (Real.exp 1 * Real.exp 1) + Real.log (7 / (Real.exp 1 * Real.exp 1)) :=
      Real.log_mul (by positivity : (Real.exp 1 * Real.exp 1) ≠ 0)
        (by positivity : (7 : ℝ) / (Real.exp 1 * Real.exp 1) ≠ 0)
    rw [h2, show Real.exp 1 * Real.exp 1 = Real.exp (1 + 1) from by rw [← Real.exp_add],
      Real.log_exp]
    ring
  linarith [hdecomp, hlog2, hub, hlog7]

/-- log(23/13) ≤ 0.571: 23/13 ≤ e^{0.571}. -/
lemma log_23_13_le : Real.log (23 / 13) ≤ (0.571 : ℝ) := by
  have hbase : (23 / 13 : ℝ) ≤ Real.exp 0.571 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.571 by norm_num) 6
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have h2 : Real.log (23 / 13) ≤ Real.log (Real.exp 0.571) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 23 / 13) hbase
  rwa [Real.log_exp] at h2

/-- log(17/14) ≤ 0.1942: 17/14 ≤ e^{0.1942}. -/
lemma log_17_14_le : Real.log (17 / 14) ≤ (0.1942 : ℝ) := by
  have hbase : (17 / 14 : ℝ) ≤ Real.exp 0.1942 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.1942 by norm_num) 5
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have h2 : Real.log (17 / 14) ≤ Real.log (Real.exp 0.1942) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 17 / 14) hbase
  rwa [Real.log_exp] at h2

/-- log 2.3 ≤ 0.833: 2.3 ≤ e^{0.833}. -/
lemma log_two_point_three_le : Real.log 2.3 ≤ (0.833 : ℝ) := by
  have hbase : (2.3 : ℝ) ≤ Real.exp 0.833 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.833 by norm_num) 8
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have h2 : Real.log 2.3 ≤ Real.log (Real.exp 0.833) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 2.3) hbase
  rwa [Real.log_exp] at h2

/-- 1/e ≤ 0.3679. -/
lemma one_div_exp_one_le : (1 : ℝ) / Real.exp 1 ≤ 0.3679 := by
  have he1 : (2.7182818 : ℝ) ≤ Real.exp 1 :=
    (by norm_num : (2.7182818 : ℝ) ≤ 2.7182818283).trans Real.exp_one_gt_d9.le
  rw [div_le_iff₀ (Real.exp_pos 1)]
  have h1 : (0.3679 : ℝ) * 2.7182818 ≤ 0.3679 * Real.exp 1 :=
    mul_le_mul_of_nonneg_left he1 (by norm_num)
  have h2 : (1 : ℝ) ≤ 0.3679 * 2.7182818 := by norm_num
  exact h2.trans h1

/-- log u ≤ u/e for u > 0 (the tangent at u = e). -/
lemma log_le_div_exp (u : ℝ) (hu : 0 < u) : Real.log u ≤ u / Real.exp 1 := by
  have h := Real.log_le_sub_one_of_pos (x := u / Real.exp 1)
    (div_pos hu (Real.exp_pos 1))
  have h2 : Real.log (u / Real.exp 1) = Real.log u - 1 := by
    rw [Real.log_div hu.ne' (Real.exp_ne_zero 1), Real.log_exp]
  rw [h2] at h
  have h3 : u / Real.exp 1 = u / Real.exp 1 := rfl
  linarith [h]

/-- log log x ≤ (1/e)·log x for x > 1. -/
lemma log_log_le_div_exp {x : ℝ} (hx : 1 < x) :
    Real.log (Real.log x) ≤ (1 / Real.exp 1) * Real.log x := by
  have h1 : (0 : ℝ) < Real.log x := Real.log_pos hx
  have h2 := log_le_div_exp (Real.log x) h1
  rw [div_eq_mul_one_div] at h2
  rw [mul_comm]
  exact h2

/-! ## 10. Piece 3b: the far-sum weight and the S₃ brick -/

/-- The far-sum per-interval weight: 1/(k√(1+k²)). -/
def farW (k : ℕ) : ℝ := 1 / ((k : ℝ) * Real.sqrt (1 + (k : ℝ) ^ 2))

lemma farW_nonneg (k : ℕ) : (0 : ℝ) ≤ farW k := by
  rw [farW]
  positivity

/-- farW k ≤ 1/k² (since √(1+k²) ≥ k). -/
lemma farW_le_inv_sq {k : ℕ} (hk : 1 ≤ k) : farW k ≤ 1 / (k : ℝ) ^ 2 := by
  have hk0 : (0 : ℝ) < (k : ℝ) := by exact_mod_cast hk
  rw [farW]
  have h1 : (k : ℝ) * (k : ℝ) ≤ (k : ℝ) * Real.sqrt (1 + (k : ℝ) ^ 2) := by
    apply mul_le_mul_of_nonneg_left _ hk0.le
    calc (k : ℝ) = Real.sqrt ((k : ℝ) ^ 2) := by
          rw [Real.sqrt_sq_eq_abs, abs_of_nonneg hk0.le]
      _ ≤ Real.sqrt (1 + (k : ℝ) ^ 2) :=
          Real.sqrt_le_sqrt (by linarith [sq_nonneg (k : ℝ)])
  have h2 : (0 : ℝ) < (k : ℝ) * (k : ℝ) := by positivity
  have h3 := one_div_le_one_div_of_le (mul_pos hk0 hk0) h1
  rwa [show (k : ℝ) * (k : ℝ) = (k : ℝ) ^ 2 from by ring] at h3

/-- farW k ≤ 1/(k(k−1)) for k ≥ 2 (since √(1+k²) ≥ k−1). -/
lemma farW_le_inv_mul_sub {k : ℕ} (hk : 2 ≤ k) :
    farW k ≤ 1 / ((k : ℝ) * ((k : ℝ) - 1)) := by
  have hk0 : (0 : ℝ) < (k : ℝ) := by exact_mod_cast (by omega : 1 ≤ k)
  have hk1 : (1 : ℝ) ≤ (k : ℝ) := by exact_mod_cast (by omega : 1 ≤ k)
  rw [farW]
  have h1 : (k : ℝ) * ((k : ℝ) - 1) ≤ (k : ℝ) * Real.sqrt (1 + (k : ℝ) ^ 2) := by
    apply mul_le_mul_of_nonneg_left _ hk0.le
    calc (k : ℝ) - 1 ≤ (k : ℝ) := by linarith
      _ = Real.sqrt ((k : ℝ) ^ 2) := by
          rw [Real.sqrt_sq_eq_abs, abs_of_nonneg hk0.le]
      _ ≤ Real.sqrt (1 + (k : ℝ) ^ 2) :=
          Real.sqrt_le_sqrt (by linarith [sq_nonneg (k : ℝ)])
  have hkk : (1 : ℝ) < (k : ℝ) := by exact_mod_cast (by omega : 1 < k)
  have h2 : (0 : ℝ) < (k : ℝ) * ((k : ℝ) - 1) := by positivity
  exact one_div_le_one_div_of_le h2 h1

/-- The telescoping tail HasSum: (1/(k+4) − 1/(k+5)) has sum 1/4. -/
lemma hasSum_one_div_four_sub_telescope :
    HasSum (fun k : ℕ => 1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5)) (1 / 4) := by
  have hsum : ∀ N : ℕ, ∑ k ∈ Finset.range N, (1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5))
      = 1 / 4 - 1 / ((N : ℝ) + 4) := by
    intro N
    have h := Finset.sum_range_sub' (fun k : ℕ => 1 / ((k : ℝ) + 4)) N
    have h2 : ∀ i : ℕ, 1 / ((i : ℝ) + 4) - 1 / (((i + 1 : ℕ) : ℝ) + 4)
        = 1 / ((i : ℝ) + 4) - 1 / ((i : ℝ) + 5) := by
      intro i
      push_cast
      ring_nf
    rw [Finset.sum_congr rfl (fun i _ => h2 i)] at h
    have h3 : 1 / (((0 : ℕ) : ℝ) + 4) = 1 / 4 := by norm_num
    rw [h3] at h
    exact h
  have hlim : Filter.Tendsto (fun N : ℕ => 1 / ((N : ℝ) + 4)) Filter.atTop (𝓝 0) := by
    have h1 : Filter.Tendsto (fun N : ℕ => ((N + 4 : ℕ) : ℝ)) Filter.atTop Filter.atTop :=
      tendsto_natCast_atTop_atTop.comp (tendsto_add_atTop_nat 4)
    have h2 := tendsto_inv_atTop_zero.comp h1
    have h3 : (fun N : ℕ => (((N + 4 : ℕ) : ℝ))⁻¹) = (fun N : ℕ => (1 / ((N : ℝ) + 4))) := by
      funext N
      push_cast
      ring_nf
    rw [← h3]
    simpa [Function.comp_def] using h2
  have hnn : ∀ k : ℕ, (0 : ℝ) ≤ 1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5) := by
    intro k
    have h1 : 1 / ((k : ℝ) + 5) ≤ 1 / ((k : ℝ) + 4) :=
      one_div_le_one_div_of_le (by positivity : (0 : ℝ) < (k : ℝ) + 4) (by linarith)
    linarith
  rw [hasSum_iff_tendsto_nat_of_nonneg hnn]
  rw [show (fun N => ∑ k ∈ Finset.range N, (1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5)))
      = fun N : ℕ => 1 / 4 - 1 / ((N : ℝ) + 4) from funext hsum]
  have h4 : Filter.Tendsto (fun N : ℕ => (1 : ℝ) / 4 - 1 / ((N : ℝ) + 4)) Filter.atTop
      (𝓝 ((1 : ℝ) / 4 - 0)) :=
    Filter.Tendsto.sub tendsto_const_nhds hlim
  simpa using h4

/-- The telescoping tail: ∑' k, (1/(k+4) − 1/(k+5)) = 1/4. -/
lemma tsum_one_div_four_sub_telescope :
    (∑' k : ℕ, (1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5))) = 1 / 4 :=
  hasSum_one_div_four_sub_telescope.tsum_eq

/-- The telescope family is summable. -/
lemma summable_one_div_four_sub_telescope :
    Summable (fun k : ℕ => 1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5)) :=
  hasSum_one_div_four_sub_telescope.summable

/-- farW (k+5) is summable (telescope-dominated). -/
lemma farW_shift5_summable : Summable (fun k : ℕ => farW (k + 5)) := by
  have htel : Summable (fun k : ℕ => 1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5)) :=
    summable_one_div_four_sub_telescope
  have htel2 : Summable (fun k : ℕ => 1 / ((k : ℝ) + 4) / ((k : ℝ) + 5)) := by
    apply htel.congr
    intro k
    have h1 : ((k : ℝ) + 4) ≠ 0 := by positivity
    have h2 : ((k : ℝ) + 5) ≠ 0 := by positivity
    field_simp
    ring
  apply Summable.of_norm_bounded htel2
  intro k
  have h1 := farW_le_inv_mul_sub (k := k + 5) (by omega : 2 ≤ k + 5)
  have h2 : farW (k + 5) ≤ 1 / ((k : ℝ) + 4) / ((k : ℝ) + 5) := by
    have h3 : ((k + 5 : ℕ) : ℝ) * (((k + 5 : ℕ) : ℝ) - 1)
        = ((k : ℝ) + 5) * ((k : ℝ) + 4) := by push_cast; ring
    calc farW (k + 5) ≤ 1 / (((k + 5 : ℕ) : ℝ) * (((k + 5 : ℕ) : ℝ) - 1)) := h1
      _ = 1 / (((k : ℝ) + 5) * ((k : ℝ) + 4)) := by rw [h3]
      _ = 1 / ((k : ℝ) + 4) / ((k : ℝ) + 5) := by
          rw [div_div, mul_comm ((k : ℝ) + 5) ((k : ℝ) + 4)]
  rw [Real.norm_eq_abs, abs_of_nonneg (farW_nonneg _)]
  exact h2

/-- farW (k+1) is summable. -/
lemma farW_shift_summable : Summable (fun k : ℕ => farW (k + 1)) := by
  apply (summable_nat_add_iff (f := fun k : ℕ => farW (k + 1)) 4).mp
  apply farW_shift5_summable.congr
  intro k
  show farW (k + 4 + 1) = farW (k + 5)
  rw [show k + 4 + 1 = k + 5 from rfl]

/-- The four exact S₃ terms. -/
lemma farW_exact_four :
    farW 1 + farW 2 + farW 3 + farW 4 ≤ 1 / 1.414 + 1 / 4.46 + 1 / 9.48 + 1 / 16.492 := by
  have hsqrt2 : (1.414 : ℝ) ≤ Real.sqrt 2 :=
    (Real.le_sqrt (by norm_num) (by norm_num)).mpr (by norm_num)
  have hsqrt5 : (2.23 : ℝ) ≤ Real.sqrt 5 :=
    (Real.le_sqrt (by norm_num) (by norm_num)).mpr (by norm_num)
  have hsqrt10 : (3.16 : ℝ) ≤ Real.sqrt 10 :=
    (Real.le_sqrt (by norm_num) (by norm_num)).mpr (by norm_num)
  have hsqrt17 : (4.123 : ℝ) ≤ Real.sqrt 17 :=
    (Real.le_sqrt (by norm_num) (by norm_num)).mpr (by norm_num)
  have h1 : farW 1 ≤ 1 / 1.414 := by
    have h2 : farW 1 = 1 / Real.sqrt 2 := by
      rw [farW, Nat.cast_one, one_pow, show (1 : ℝ) + 1 = 2 by norm_num, one_mul]
    rw [h2]
    exact one_div_le_one_div_of_le (by norm_num : (0 : ℝ) < 1.414) hsqrt2
  have h2' : farW 2 ≤ 1 / 4.46 := by
    have h3 : farW 2 = 1 / ((2 : ℝ) * Real.sqrt 5) := by
      rw [farW, Nat.cast_ofNat, show (2 : ℝ) ^ 2 = 4 by norm_num,
        show (1 : ℝ) + 4 = 5 by norm_num]
    rw [h3]
    have h4 : (2 : ℝ) * 2.23 ≤ (2 : ℝ) * Real.sqrt 5 :=
      mul_le_mul_of_nonneg_left hsqrt5 (by norm_num)
    have h5 : (4.46 : ℝ) = 2 * 2.23 := by norm_num
    rw [h5]
    exact one_div_le_one_div_of_le (by norm_num : (0 : ℝ) < (2 : ℝ) * 2.23) h4
  have h3' : farW 3 ≤ 1 / 9.48 := by
    have h4 : farW 3 = 1 / ((3 : ℝ) * Real.sqrt 10) := by
      rw [farW, Nat.cast_ofNat, show (3 : ℝ) ^ 2 = 9 by norm_num,
        show (1 : ℝ) + 9 = 10 by norm_num]
    rw [h4]
    have h5 : (3 : ℝ) * 3.16 ≤ (3 : ℝ) * Real.sqrt 10 :=
      mul_le_mul_of_nonneg_left hsqrt10 (by norm_num)
    have h6 : (9.48 : ℝ) = 3 * 3.16 := by norm_num
    rw [h6]
    exact one_div_le_one_div_of_le (by norm_num : (0 : ℝ) < (3 : ℝ) * 3.16) h5
  have h4' : farW 4 ≤ 1 / 16.492 := by
    have h5 : farW 4 = 1 / ((4 : ℝ) * Real.sqrt 17) := by
      rw [farW, Nat.cast_ofNat, show (4 : ℝ) ^ 2 = 16 by norm_num,
        show (1 : ℝ) + 16 = 17 by norm_num]
    rw [h5]
    have h6 : (4 : ℝ) * 4.123 ≤ (4 : ℝ) * Real.sqrt 17 :=
      mul_le_mul_of_nonneg_left hsqrt17 (by norm_num)
    have h7 : (16.492 : ℝ) = 4 * 4.123 := by norm_num
    rw [h7]
    exact one_div_le_one_div_of_le (by norm_num : (0 : ℝ) < (4 : ℝ) * 4.123) h6
  linarith [h1, h2', h3', h4']

/-- **The S₃ brick**: Σ_{k≥1} 1/(k√(1+k²)) ≤ 1.3476. -/
lemma farW_tsum_le : (∑' k : ℕ, farW (k + 1)) ≤ 1.3476 := by
  have hsplit : (∑' k : ℕ, farW (k + 1))
      = (∑ k ∈ Finset.range 4, farW (k + 1)) + ∑' k : ℕ, farW (k + 1 + 4) :=
    (farW_shift_summable.sum_add_tsum_nat_add 4).symm
  have hhead : ∑ k ∈ Finset.range 4, farW (k + 1)
      = farW 1 + farW 2 + farW 3 + farW 4 := by
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_zero]
    norm_num
  have htail : (∑' k : ℕ, farW (k + 1 + 4)) ≤ 1 / 4 := by
    have h1 : (∑' k : ℕ, farW (k + 1 + 4)) = ∑' k : ℕ, farW (k + 5) := by
      apply tsum_congr
      intro k
      rw [show k + 1 + 4 = k + 5 from rfl]
    rw [h1]
    have htel : (∑' k : ℕ, farW (k + 5))
        ≤ ∑' k : ℕ, (1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5)) := by
      apply Summable.tsum_le_tsum _ farW_shift5_summable summable_one_div_four_sub_telescope
      intro k
      have h2 := farW_le_inv_mul_sub (k := k + 5) (by omega : 2 ≤ k + 5)
      have h3 : ((k + 5 : ℕ) : ℝ) * (((k + 5 : ℕ) : ℝ) - 1)
          = ((k : ℝ) + 5) * ((k : ℝ) + 4) := by push_cast; ring
      have h4 : 1 / (((k + 5 : ℕ) : ℝ) * (((k + 5 : ℕ) : ℝ) - 1))
          = 1 / ((k : ℝ) + 4) - 1 / ((k : ℝ) + 5) := by
        rw [h3]
        have h5 : ((k : ℝ) + 4) ≠ 0 := by positivity
        have h6 : ((k : ℝ) + 5) ≠ 0 := by positivity
        field_simp
        ring
      exact h2.trans h4.le
    rw [tsum_one_div_four_sub_telescope] at htel
    exact htel
  have hbound : (∑ k ∈ Finset.range 4, farW (k + 1)) + 1 / 4
      ≤ (1 / 1.414 + 1 / 4.46 + 1 / 9.48 + 1 / 16.492) + 1 / 4 := by
    rw [hhead]
    linarith [farW_exact_four]
  have hnum : (1 : ℝ) / 1.414 + 1 / 4.46 + 1 / 9.48 + 1 / 16.492 + 1 / 4 ≤ 1.3476 := by
    norm_num
  calc (∑' k : ℕ, farW (k + 1))
      = (∑ k ∈ Finset.range 4, farW (k + 1)) + ∑' k : ℕ, farW (k + 1 + 4) := hsplit
    _ ≤ (∑ k ∈ Finset.range 4, farW (k + 1)) + 1 / 4 := by linarith [htail]
    _ ≤ (1 / 1.414 + 1 / 4.46 + 1 / 9.48 + 1 / 16.492) + 1 / 4 := hbound
    _ ≤ 1.3476 := hnum

/-- The finite-Ico S₃ bound (for the main bucket family). -/
lemma farW_Ico_le (K : ℕ) : ∑ k ∈ Finset.Ico 1 (K + 1), farW k ≤ 1.3476 := by
  rw [Finset.sum_Ico_eq_sum_range]
  have h2 : ∑ x ∈ Finset.range (K + 1 - 1), farW (1 + x)
      = ∑ x ∈ Finset.range (K + 1 - 1), farW (x + 1) :=
    Finset.sum_congr rfl (fun x _ => by rw [add_comm])
  rw [h2]
  have h1 : ∑ x ∈ Finset.range (K + 1 - 1), farW (x + 1)
      ≤ ∑' k : ℕ, farW (k + 1) :=
    Summable.sum_le_tsum _ (fun i _ => farW_nonneg _) farW_shift_summable
  exact h1.trans farW_tsum_le

/-! ## 11. Piece 3c: the right-tail weight bricks (Σ over k ≥ 11) -/

/-- 1/x² is antitone on Ici 10. -/
lemma antitoneOn_one_div_sq : AntitoneOn (fun x : ℝ => 1 / x ^ 2) (Set.Ici (10 : ℝ)) := by
  intro a ha b hb hab
  rw [Set.mem_Ici] at ha hb
  have ha0 : (0 : ℝ) < a := by linarith
  exact one_div_le_one_div_of_le (pow_pos ha0 2)
    (pow_le_pow_left₀ ha0.le hab 2)

/-- −x⁻¹ has derivative 1/x² (x ≠ 0). -/
lemma hasDerivAt_neg_inv (x : ℝ) (hx0 : x ≠ 0) :
    HasDerivAt (fun y : ℝ => -y⁻¹) (1 / x ^ 2) x := by
  have h2 := (hasDerivAt_id' x).inv hx0
  have h3 := h2.neg
  rw [show (-(fun x : ℝ => x)⁻¹) = (fun y : ℝ => -y⁻¹) from by
    ext y
    simp only [Pi.neg_apply, Pi.inv_apply]] at h3
  rwa [show (-(-1 / x ^ 2)) = 1 / x ^ 2 from by ring] at h3

/-- 1/x² is integrable on Ioi 10. -/
lemma integrableOn_one_div_sq :
    IntegrableOn (fun x : ℝ => 1 / x ^ 2) (Set.Ioi (10 : ℝ)) volume := by
  apply integrableOn_Ioi_deriv_of_nonneg' (fun x hx =>
    hasDerivAt_neg_inv x (by
      have h := (Set.mem_Ici.mp hx); linarith))
  · intro x hx
    have hx10 : (10 : ℝ) < x := hx
    positivity
  · simpa using tendsto_inv_atTop_zero.neg

/-- The integral of 1/x² over Ioi 10 is 1/10. -/
lemma integral_one_div_sq_Ioi_ten :
    ∫ x in Set.Ioi (10 : ℝ), 1 / x ^ 2 = 1 / 10 := by
  have h := integral_Ioi_of_hasDerivAt_of_tendsto'
    (fun x hx => hasDerivAt_neg_inv x (by
      have h := (Set.mem_Ici.mp hx); linarith))
    integrableOn_one_div_sq (by simpa using tendsto_inv_atTop_zero.neg)
  rw [h]
  show (-0 : ℝ) - -((10 : ℝ)⁻¹) = 1 / 10
  norm_num

/-- **RW₀**: Σ_{k≥11} 1/k² ≤ 0.1. -/
lemma tail11_inv_sq_le : (∑' k : ℕ, (1 : ℝ) / ((k : ℝ) + 11) ^ 2) ≤ 0.1 := by
  have hle := antitoneOn_one_div_sq.tsum_comp_add_le_integral 10
    integrableOn_one_div_sq (fun t ht => by
      have ht10 : (10 : ℝ) < t := ht
      positivity)
  have hInt10 : (∫ x in Set.Ioi ((10 : ℕ) : ℝ), 1 / x ^ 2) = 1 / 10 := by
    rw [show ((10 : ℕ) : ℝ) = (10 : ℝ) by norm_num]
    exact integral_one_div_sq_Ioi_ten
  rw [hInt10] at hle
  have hcongr : (∑' n : ℕ, 1 / ((↑(n + 10 + 1) : ℕ) : ℝ) ^ 2)
      = ∑' n : ℕ, (1 : ℝ) / ((n : ℝ) + 11) ^ 2 := by
    refine tsum_congr fun n => ?_
    have h1 : ((↑(n + 10 + 1) : ℕ) : ℝ) = (n : ℝ) + 11 := by push_cast; ring
    rw [h1]
  rw [hcongr] at hle
  have h2 : (1 : ℝ) / 10 = 0.1 := by norm_num
  rw [h2] at hle
  exact hle

/-- log x/x² is integrable on Ioi 10 (restricting the Ioi 3 instance). -/
lemma integrableOn_log_div_sq_ten :
    IntegrableOn (fun x : ℝ => Real.log x / x ^ 2) (Set.Ioi (10 : ℝ)) volume :=
  integrableOn_log_div_sq.mono_set (Set.Ioi_subset_Ioi (by norm_num : (3 : ℝ) ≤ 10))

/-- log x/x² is antitone on Ici 10 (restricting the Ici 3 instance). -/
lemma antitoneOn_log_div_sq_ten :
    AntitoneOn (fun x : ℝ => Real.log x / x ^ 2) (Set.Ici (10 : ℝ)) :=
  antitoneOn_log_div_sq.mono (Set.Ici_subset_Ici.mpr (by norm_num : (3 : ℝ) ≤ 10))

/-- The integral of log x/x² over Ioi 10 is (log 10 + 1)/10. -/
lemma integral_log_div_sq_Ioi_ten :
    ∫ x in Set.Ioi (10 : ℝ), Real.log x / x ^ 2 = (Real.log 10 + 1) / 10 := by
  have h := integral_Ioi_of_hasDerivAt_of_tendsto'
    (fun x hx => hasDerivAt_neg_log_add_one_div (by
      have h := (Set.mem_Ici.mp hx); linarith))
    integrableOn_log_div_sq_ten tendsto_neg_log_add_one_div
  rw [h]
  show (0 : ℝ) - -((Real.log 10 + 1) / 10) = (Real.log 10 + 1) / 10
  ring

/-- The shifted log-family is summable. -/
lemma tail11_log_div_sq_summable :
    Summable (fun k : ℕ => Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2) := by
  have h := antitoneOn_log_div_sq_ten.summable_of_integrableOn_Ioi
    integrableOn_log_div_sq_ten (fun t ht => by
      have ht10 : (10 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  have h2 := (summable_nat_add_iff (f := fun n : ℕ =>
    Real.log (n : ℝ) / (n : ℝ) ^ 2) 11).mpr h
  refine h2.congr fun n => ?_
  push_cast
  ring_nf

/-- **RW₁**: Σ_{k≥11} log k/k² ≤ 0.3303. -/
lemma tail11_log_div_sq_le :
    (∑' k : ℕ, Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2) ≤ 0.3303 := by
  have hle := antitoneOn_log_div_sq_ten.tsum_comp_add_le_integral 10
    integrableOn_log_div_sq_ten (fun t ht => by
      have ht10 : (10 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  have hInt10 : (∫ x in Set.Ioi ((10 : ℕ) : ℝ), Real.log x / x ^ 2)
      = (Real.log 10 + 1) / 10 := by
    rw [show ((10 : ℕ) : ℝ) = (10 : ℝ) by norm_num]
    exact integral_log_div_sq_Ioi_ten
  rw [hInt10] at hle
  have hcongr : (∑' n : ℕ, Real.log (↑(n + 10 + 1) : ℕ) / ((↑(n + 10 + 1) : ℕ) : ℝ) ^ 2)
      = ∑' n : ℕ, Real.log ((n : ℝ) + 11) / ((n : ℝ) + 11) ^ 2 := by
    refine tsum_congr fun n => ?_
    have h1 : ((↑(n + 10 + 1) : ℕ) : ℝ) = (n : ℝ) + 11 := by push_cast; ring
    rw [h1]
  rw [hcongr] at hle
  have h1 := log_ten_le
  linarith [hle, h1]

/-- **RW₂**: Σ_{k≥11} log log k/k² ≤ 0.1216 (via log log k ≤ (1/e)·log k). -/
lemma tail11_loglog_div_sq_le :
    (∑' k : ℕ, Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2) ≤ 0.1216 := by
  have hpt : ∀ k : ℕ, Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2
      ≤ (1 / Real.exp 1) * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2) := by
    intro k
    have hk1 : (1 : ℝ) < (k : ℝ) + 11 := by
      have h : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
      linarith
    have h2 := log_log_le_div_exp hk1
    have h3 : (0 : ℝ) ≤ ((k : ℝ) + 11) ^ 2 := by positivity
    have h4 : Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2
        ≤ ((1 / Real.exp 1) * Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2 :=
      div_le_div_of_nonneg_right h2 h3
    rw [mul_div_assoc] at h4
    exact h4
  have hsumm2 : Summable (fun k : ℕ =>
      (1 / Real.exp 1) * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)) :=
    tail11_log_div_sq_summable.mul_left _
  have hsumm : Summable (fun k : ℕ =>
      Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2) := by
    apply Summable.of_norm_bounded hsumm2
    intro k
    rw [Real.norm_eq_abs]
    rw [abs_of_nonneg (by
      have hk1 : (1 : ℝ) ≤ Real.log ((k : ℝ) + 11) := by
        have h1 := log_eleven_ge
        have h2 : Real.log 11 ≤ Real.log ((k : ℝ) + 11) :=
          Real.log_le_log (by norm_num : (0 : ℝ) < 11) (by
            have h : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
            linarith)
        linarith [h1, h2]
      have h3 : (0 : ℝ) ≤ Real.log (Real.log ((k : ℝ) + 11)) := Real.log_nonneg hk1
      positivity : (0 : ℝ) ≤ Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2)]
    exact hpt k
  have hle := Summable.tsum_le_tsum hpt hsumm hsumm2
  rw [tsum_mul_left] at hle
  have h1 := tail11_log_div_sq_le
  have h2 := one_div_exp_one_le
  have h4 : (1 / Real.exp 1) * (∑' k : ℕ, Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)
      ≤ (1 / Real.exp 1) * 0.3303 :=
    mul_le_mul_of_nonneg_left h1 (by positivity : (0 : ℝ) ≤ 1 / Real.exp 1)
  have h3 : (1 / Real.exp 1) * 0.3303 ≤ 0.1216 := by
    calc (1 / Real.exp 1) * 0.3303 ≤ 0.3679 * 0.3303 :=
        mul_le_mul_of_nonneg_right h2 (by norm_num)
      _ ≤ 0.1216 := by norm_num
  linarith [hle, h4, h3]

/-! ## 12. Piece 3d: the left-tail weight bricks (Σ over j ≥ 14) -/

/-- 1/(x+10)² is antitone on Ici 13. -/
lemma antitoneOn_one_div_add_sq :
    AntitoneOn (fun x : ℝ => 1 / (x + 10) ^ 2) (Set.Ici (13 : ℝ)) := by
  intro a ha b hb hab
  rw [Set.mem_Ici] at ha hb
  have ha0 : (0 : ℝ) < a + 10 := by linarith
  exact one_div_le_one_div_of_le (pow_pos ha0 2)
    (pow_le_pow_left₀ ha0.le (by linarith : a + 10 ≤ b + 10) 2)

/-- −(x+10)⁻¹ has derivative 1/(x+10)² (x + 10 ≠ 0). -/
lemma hasDerivAt_neg_inv_add (x : ℝ) (hx10 : x + 10 ≠ 0) :
    HasDerivAt (fun y : ℝ => -(y + 10)⁻¹) (1 / (x + 10) ^ 2) x := by
  have h2 := ((hasDerivAt_id' x).add_const 10).inv hx10
  have h3 := h2.neg
  rw [show (-(fun y : ℝ => y + 10)⁻¹) = (fun y : ℝ => -(y + 10)⁻¹) from by
    ext y
    simp only [Pi.neg_apply, Pi.inv_apply]] at h3
  rwa [show (-(-1 / (x + 10) ^ 2)) = 1 / (x + 10) ^ 2 from by ring] at h3

/-- −(y+10)⁻¹ tends to 0 at infinity. -/
lemma tendsto_neg_inv_add :
    Filter.Tendsto (fun y : ℝ => -(y + 10)⁻¹) Filter.atTop (𝓝 0) := by
  have h1 : Filter.Tendsto (fun y : ℝ => y + 10) Filter.atTop Filter.atTop :=
    tendsto_atTop_add_const_right Filter.atTop 10 tendsto_id
  have h2 : Filter.Tendsto (fun y : ℝ => (y + 10)⁻¹) Filter.atTop (𝓝 0) :=
    tendsto_inv_atTop_zero.comp h1
  simpa [Function.comp_def, Pi.neg_apply] using h2.neg

/-- 1/(x+10)² is integrable on Ioi 13. -/
lemma integrableOn_one_div_add_sq :
    IntegrableOn (fun x : ℝ => 1 / (x + 10) ^ 2) (Set.Ioi (13 : ℝ)) volume := by
  apply integrableOn_Ioi_deriv_of_nonneg' (fun x hx =>
    hasDerivAt_neg_inv_add x (by
      have h := (Set.mem_Ici.mp hx); linarith))
  · intro x hx
    have hx13 : (13 : ℝ) < x := hx
    positivity
  · exact tendsto_neg_inv_add

/-- The integral of 1/(x+10)² over Ioi 13 is 1/23. -/
lemma integral_one_div_add_sq_Ioi_thirteen :
    ∫ x in Set.Ioi (13 : ℝ), 1 / (x + 10) ^ 2 = 1 / 23 := by
  have h := integral_Ioi_of_hasDerivAt_of_tendsto'
    (fun x hx => hasDerivAt_neg_inv_add x (by
      have h := (Set.mem_Ici.mp hx); linarith))
    integrableOn_one_div_add_sq tendsto_neg_inv_add
  rw [h]
  show (0 : ℝ) - -((13 + 10 : ℝ)⁻¹) = 1 / 23
  norm_num

/-- **LA₀**: Σ_{j≥14} 1/(j+10)² ≤ 1/23. -/
lemma tail14_inv_add_sq_le :
    (∑' j : ℕ, (1 : ℝ) / (((j : ℝ) + 14) + 10) ^ 2) ≤ 1 / 23 := by
  have hle := antitoneOn_one_div_add_sq.tsum_comp_add_le_integral 13
    integrableOn_one_div_add_sq (fun t ht => by
      have ht13 : (13 : ℝ) < t := ht
      positivity)
  have hInt13 : (∫ x in Set.Ioi ((13 : ℕ) : ℝ), 1 / (x + 10) ^ 2) = 1 / 23 := by
    rw [show ((13 : ℕ) : ℝ) = (13 : ℝ) by norm_num]
    exact integral_one_div_add_sq_Ioi_thirteen
  rw [hInt13] at hle
  have hcongr : (∑' n : ℕ, 1 / (((↑(n + 13 + 1) : ℕ) : ℝ) + 10) ^ 2)
      = ∑' n : ℕ, (1 : ℝ) / (((n : ℝ) + 14) + 10) ^ 2 := by
    refine tsum_congr fun n => ?_
    have h1 : ((↑(n + 13 + 1) : ℕ) : ℝ) = (n : ℝ) + 14 := by push_cast; ring
    rw [h1]
  rw [hcongr] at hle
  exact hle

/-- log x/(x+10)² is antitone on Ici 13 (derivative
((x+10)/x − 2 log x)/(x+10)³ ≤ 0 since (x+10)/x ≤ 2 ≤ 2 log x). -/
lemma antitoneOn_log_div_add_sq :
    AntitoneOn (fun x : ℝ => Real.log x / (x + 10) ^ 2) (Set.Ici (13 : ℝ)) := by
  apply antitoneOn_of_deriv_nonpos (convex_Ici 13)
  · intro x hx
    have hx0 : x ≠ 0 := by have h := (Set.mem_Ici.mp hx); linarith
    have hx10 : (x + 10) ^ 2 ≠ 0 := by
      have h := (Set.mem_Ici.mp hx)
      positivity
    exact ((Real.differentiableAt_log hx0).div (by
      fun_prop : DifferentiableAt ℝ (fun y : ℝ => (y + 10) ^ 2) x) hx10)
      |>.continuousAt.continuousWithinAt
  · intro x hx
    have hx0 : x ≠ 0 := by
      have h : (13 : ℝ) < x := Set.mem_Ioi.mp (by rwa [interior_Ici] at hx)
      linarith
    have hx10 : (x + 10) ^ 2 ≠ 0 := by
      have h : (13 : ℝ) < x := Set.mem_Ioi.mp (by rwa [interior_Ici] at hx)
      positivity
    exact ((Real.differentiableAt_log hx0).div (by
      fun_prop : DifferentiableAt ℝ (fun y : ℝ => (y + 10) ^ 2) x) hx10)
      |>.differentiableWithinAt
  · intro x hx
    have hx13 : (13 : ℝ) < x := Set.mem_Ioi.mp (by rwa [interior_Ici] at hx)
    have hx0 : x ≠ 0 := by linarith
    have hx10 : (x + 10) ≠ 0 := by linarith
    have hderiv : deriv (fun y => Real.log y / (y + 10) ^ 2) x
        = ((x + 10) / x - 2 * Real.log x) / (x + 10) ^ 3 := by
      have h1 : HasDerivAt (fun y : ℝ => (y + 10) ^ 2) (2 * (x + 10)) x := by
        have h := (hasDerivAt_pow 2 (x + 10)).comp x ((hasDerivAt_id' x).add_const 10)
        rw [show ((2 : ℕ) : ℝ) * (x + 10) ^ (2 - 1) * 1 = 2 * (x + 10) from by
          rw [show (2 : ℕ) - 1 = 1 from rfl, pow_one]
          push_cast
          ring] at h
        exact h
      have h2 := (Real.hasDerivAt_log hx0).div h1 (by positivity : (x + 10) ^ 2 ≠ 0)
      rw [show (fun y : ℝ => Real.log y / (y + 10) ^ 2)
          = Real.log / fun y => (y + 10) ^ 2 from by
        ext y; simp only [Pi.div_apply]]
      rw [h2.deriv]
      field_simp [hx0, hx10]
    rw [hderiv]
    have hlog1 : (1 : ℝ) ≤ Real.log x := by
      have h1 : Real.exp 1 ≤ 13 := Real.exp_one_lt_d9.le.trans (by norm_num)
      have h2 : (1 : ℝ) ≤ Real.log 13 := by
        rw [Real.le_log_iff_exp_le (by norm_num : (0 : ℝ) < 13)]
        exact h1
      exact h2.trans (Real.log_le_log (by positivity : (0 : ℝ) < 13) hx13.le)
    have hbound : (x + 10) / x ≤ 2 := by
      rw [div_le_iff₀ (by linarith : (0 : ℝ) < x)]
      linarith
    have h3 : (x + 10) / x - 2 * Real.log x ≤ 0 := by linarith [hlog1, hbound]
    exact div_nonpos_of_nonpos_of_nonneg h3 (by positivity)

/-- The antiderivative of log x/(x+10)²:
F(x) = −log x/(x+10) + (1/10)·log(x/(x+10)). -/
lemma hasDerivAt_log_div_add_sq_antideriv (x : ℝ) (hx0 : x ≠ 0) (hx10 : x + 10 ≠ 0) :
    HasDerivAt (fun y : ℝ => -Real.log y / (y + 10) + (1 / 10) * Real.log (y / (y + 10)))
      (Real.log x / (x + 10) ^ 2) x := by
  have hF1 : HasDerivAt (fun y : ℝ => -Real.log y / (y + 10))
      (-(1 / (x * (x + 10))) + Real.log x / (x + 10) ^ 2) x := by
    have h1 := ((Real.hasDerivAt_log hx0).div ((hasDerivAt_id' x).add_const 10) hx10).neg
    rw [show (-(Real.log / fun y => y + 10)) = (fun y : ℝ => -Real.log y / (y + 10)) from by
      ext y; simp only [Pi.neg_apply, Pi.div_apply, neg_div]] at h1
    rw [show (-((x⁻¹ * (x + 10) - Real.log x * 1) / (x + 10) ^ 2))
        = -(1 / (x * (x + 10))) + Real.log x / (x + 10) ^ 2 from by
      field_simp [hx0, hx10]
      ring] at h1
    exact h1
  have hF2 : HasDerivAt (fun y : ℝ => (1 / 10) * Real.log (y / (y + 10)))
      (1 / (x * (x + 10))) x := by
    have hratio : HasDerivAt (fun y : ℝ => y / (y + 10)) (10 / (x + 10) ^ 2) x := by
      have h := (hasDerivAt_id' x).div ((hasDerivAt_id' x).add_const 10) hx10
      rw [show (1 * (x + 10) - x * 1) / (x + 10) ^ 2 = 10 / (x + 10) ^ 2 from by
        field_simp [hx10]
        ring] at h
      exact h
    have hxr : x / (x + 10) ≠ 0 := by
      rw [div_ne_zero_iff]
      exact ⟨hx0, hx10⟩
    have h2 := (Real.hasDerivAt_log hxr).comp x hratio
    have h3 := h2.const_mul (1 / 10 : ℝ)
    rw [show (1 / 10 : ℝ) * ((x / (x + 10))⁻¹ * (10 / (x + 10) ^ 2))
        = 1 / (x * (x + 10)) from by
      field_simp [hx0, hx10]] at h3
    exact h3
  have hF := hF1.add hF2
  rw [show (-(1 / (x * (x + 10))) + Real.log x / (x + 10) ^ 2) + 1 / (x * (x + 10))
      = Real.log x / (x + 10) ^ 2 from by ring] at hF
  rw [show (fun y : ℝ => -Real.log y / (y + 10))
        + (fun y : ℝ => (1 / 10) * Real.log (y / (y + 10)))
      = (fun y : ℝ => -Real.log y / (y + 10) + (1 / 10) * Real.log (y / (y + 10))) from by
    ext y; simp only [Pi.add_apply]] at hF
  exact hF

/-- The antiderivative tends to 0 at infinity. -/
lemma tendsto_log_div_add_sq_antideriv :
    Filter.Tendsto (fun y : ℝ => -Real.log y / (y + 10) + (1 / 10) * Real.log (y / (y + 10)))
      Filter.atTop (𝓝 0) := by
  have hratio : Filter.Tendsto (fun x : ℝ => x / (x + 10)) Filter.atTop (𝓝 1) := by
    have h21 : Filter.Tendsto (fun x : ℝ => (10 : ℝ) / x) Filter.atTop (𝓝 0) := by
      have hT : Filter.Tendsto (fun x : ℝ => x⁻¹) Filter.atTop (𝓝 0) :=
        tendsto_inv_atTop_zero
      simpa [div_eq_mul_inv] using hT.const_mul 10
    have h22 : Filter.Tendsto (fun x : ℝ => (1 : ℝ) + (10 : ℝ) / x) Filter.atTop
        (𝓝 (1 + 0)) :=
      tendsto_const_nhds.add h21
    have h23 : (fun x : ℝ => 1 / (1 + 10 / x)) =ᶠ[Filter.atTop]
        (fun x : ℝ => x / (x + 10)) := by
      rw [Filter.eventuallyEq_iff_exists_mem]
      refine ⟨Set.Ioi (0 : ℝ), Filter.Ioi_mem_atTop 0, fun x hx => ?_⟩
      rw [Set.mem_Ioi] at hx
      have hx0 : x ≠ 0 := by linarith
      show (1 : ℝ) / (1 + 10 / x) = x / (x + 10)
      field_simp [hx0]
    have h24 : Filter.Tendsto (fun x : ℝ => 1 / (1 + (10 : ℝ) / x)) Filter.atTop
        (𝓝 (1 / (1 + 0))) := by
      apply Filter.Tendsto.div tendsto_const_nhds h22
      norm_num
    have h25 : Filter.Tendsto (fun x : ℝ => 1 / (1 + (10 : ℝ) / x)) Filter.atTop
        (𝓝 1) := by
      simpa using h24
    exact h25.congr' h23
  have hT1 : Filter.Tendsto (fun x : ℝ => Real.log x / (x + 10)) Filter.atTop (𝓝 0) := by
    have h1 : Filter.Tendsto (fun x : ℝ => Real.log x / x) Filter.atTop (𝓝 0) := by
      have h := Real.tendsto_pow_log_div_mul_add_atTop 1 0 1 one_ne_zero
      simpa using h
    have h3 : (fun x : ℝ => Real.log x / (x + 10))
        = (fun x : ℝ => (Real.log x / x) * (x / (x + 10))) := by
      funext x
      by_cases hx : x = 0
      · rw [hx]
        norm_num
      · field_simp [hx]
    rw [h3]
    have h4 := h1.mul hratio
    simp only [zero_mul] at h4
    exact h4
  have hT2 : Filter.Tendsto (fun x : ℝ => (1 / 10) * Real.log (x / (x + 10))) Filter.atTop
      (𝓝 0) := by
    have h2 : ContinuousAt Real.log 1 := Real.continuousAt_log (by norm_num : (1 : ℝ) ≠ 0)
    have h3 := (h2.tendsto.comp hratio).const_mul (1 / 10 : ℝ)
    rw [Real.log_one] at h3
    simpa using h3
  have hT := hT1.neg.add hT2
  have heq : (fun y : ℝ => -Real.log y / (y + 10) + (1 / 10) * Real.log (y / (y + 10)))
      = (fun y : ℝ => -(Real.log y / (y + 10)) + (1 / 10) * Real.log (y / (y + 10))) := by
    ext y
    simp only [neg_div]
  rw [heq]
  simpa using hT

/-- log x/(x+10)² is integrable on Ioi 13. -/
lemma integrableOn_log_div_add_sq :
    IntegrableOn (fun x : ℝ => Real.log x / (x + 10) ^ 2) (Set.Ioi (13 : ℝ)) volume := by
  apply integrableOn_Ioi_deriv_of_nonneg' (fun x hx =>
    hasDerivAt_log_div_add_sq_antideriv x (by
      have h := (Set.mem_Ici.mp hx); linarith) (by
      have h := (Set.mem_Ici.mp hx); linarith))
  · intro x hx
    have hx13 : (13 : ℝ) < x := hx
    have hlog : (0 : ℝ) ≤ Real.log x := Real.log_nonneg (by linarith)
    positivity
  · exact tendsto_log_div_add_sq_antideriv

/-- The integral of log x/(x+10)² over Ioi 13 is log 13/23 + log(23/13)/10. -/
lemma integral_log_div_add_sq_Ioi_thirteen :
    ∫ x in Set.Ioi (13 : ℝ), Real.log x / (x + 10) ^ 2
      = Real.log 13 / 23 + Real.log (23 / 13) / 10 := by
  have h := integral_Ioi_of_hasDerivAt_of_tendsto'
    (fun x hx => hasDerivAt_log_div_add_sq_antideriv x (by
      have h := (Set.mem_Ici.mp hx); linarith) (by
      have h := (Set.mem_Ici.mp hx); linarith))
    integrableOn_log_div_add_sq tendsto_log_div_add_sq_antideriv
  rw [h]
  have h1 : -Real.log 13 / (13 + 10) + (1 / 10) * Real.log (13 / (13 + 10))
      = -(Real.log 13 / 23 + Real.log (23 / 13) / 10) := by
    have h2 : Real.log (13 / (13 + 10)) = -Real.log (23 / 13) := by
      rw [show (13 : ℝ) / (13 + 10) = (23 / 13)⁻¹ from by norm_num, Real.log_inv]
    rw [h2]
    ring
  rw [h1]
  show (0 : ℝ) - -(Real.log 13 / 23 + Real.log (23 / 13) / 10)
      = Real.log 13 / 23 + Real.log (23 / 13) / 10
  ring

/-- The shifted log-family is summable. -/
lemma tail14_log_div_add_sq_summable :
    Summable (fun j : ℕ => Real.log ((j : ℝ) + 14) / (((j : ℝ) + 14) + 10) ^ 2) := by
  have h := antitoneOn_log_div_add_sq.summable_of_integrableOn_Ioi
    integrableOn_log_div_add_sq (fun t ht => by
      have ht13 : (13 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  have h2 := (summable_nat_add_iff (f := fun n : ℕ =>
    Real.log (n : ℝ) / ((n : ℝ) + 10) ^ 2) 14).mpr h
  refine h2.congr fun n => ?_
  push_cast
  ring_nf

/-- **LA₁**: Σ_{j≥14} log j/(j+10)² ≤ 0.1687. -/
lemma tail14_log_div_add_sq_le :
    (∑' j : ℕ, Real.log ((j : ℝ) + 14) / (((j : ℝ) + 14) + 10) ^ 2) ≤ 0.1687 := by
  have hle := antitoneOn_log_div_add_sq.tsum_comp_add_le_integral 13
    integrableOn_log_div_add_sq (fun t ht => by
      have ht13 : (13 : ℝ) < t := ht
      have hlog : (0 : ℝ) ≤ Real.log t := Real.log_nonneg (by linarith)
      positivity)
  have hInt13 : (∫ x in Set.Ioi ((13 : ℕ) : ℝ), Real.log x / (x + 10) ^ 2)
      = Real.log 13 / 23 + Real.log (23 / 13) / 10 := by
    rw [show ((13 : ℕ) : ℝ) = (13 : ℝ) by norm_num]
    exact integral_log_div_add_sq_Ioi_thirteen
  rw [hInt13] at hle
  have hcongr : (∑' n : ℕ, Real.log (↑(n + 13 + 1) : ℕ) / ((↑(n + 13 + 1) : ℕ) + 10) ^ 2)
      = ∑' n : ℕ, Real.log ((n : ℝ) + 14) / (((n : ℝ) + 14) + 10) ^ 2 := by
    refine tsum_congr fun n => ?_
    have h1 : ((↑(n + 13 + 1) : ℕ) : ℝ) = (n : ℝ) + 14 := by push_cast; ring
    rw [h1]
  rw [hcongr] at hle
  have h1 := log_thirteen_le
  have h2 := log_23_13_le
  linarith [hle, h1, h2]

/-- **LA₂**: Σ_{j≥14} log log j/(j+10)² ≤ 0.0621 (via log log j ≤ (1/e)·log j). -/
lemma tail14_loglog_div_add_sq_le :
    (∑' j : ℕ, Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2) ≤ 0.0621 := by
  have hpt : ∀ j : ℕ, Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2
      ≤ (1 / Real.exp 1) * (Real.log ((j : ℝ) + 14) / (((j : ℝ) + 14) + 10) ^ 2) := by
    intro j
    have hj1 : (1 : ℝ) < (j : ℝ) + 14 := by
      have h : (0 : ℝ) ≤ (j : ℝ) := Nat.cast_nonneg j
      linarith
    have h2 := log_log_le_div_exp hj1
    have h3 : (0 : ℝ) ≤ (((j : ℝ) + 14) + 10) ^ 2 := by positivity
    have h4 : Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2
        ≤ ((1 / Real.exp 1) * Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2 :=
      div_le_div_of_nonneg_right h2 h3
    rw [mul_div_assoc] at h4
    exact h4
  have hsumm2 : Summable (fun j : ℕ =>
      (1 / Real.exp 1) * (Real.log ((j : ℝ) + 14) / (((j : ℝ) + 14) + 10) ^ 2)) :=
    tail14_log_div_add_sq_summable.mul_left _
  have hsumm : Summable (fun j : ℕ =>
      Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2) := by
    apply Summable.of_norm_bounded hsumm2
    intro j
    rw [Real.norm_eq_abs]
    rw [abs_of_nonneg (by
      have hj1 : (1 : ℝ) ≤ Real.log ((j : ℝ) + 14) := by
        have h1 := log_fourteen_ge
        have h2 : Real.log 14 ≤ Real.log ((j : ℝ) + 14) :=
          Real.log_le_log (by norm_num : (0 : ℝ) < 14) (by
            have h : (0 : ℝ) ≤ (j : ℝ) := Nat.cast_nonneg j
            linarith)
        linarith [h1, h2]
      have h3 : (0 : ℝ) ≤ Real.log (Real.log ((j : ℝ) + 14)) := Real.log_nonneg hj1
      positivity : (0 : ℝ) ≤
        Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2)]
    exact hpt j
  have hle := Summable.tsum_le_tsum hpt hsumm hsumm2
  rw [tsum_mul_left] at hle
  have h1 := tail14_log_div_add_sq_le
  have h2 := one_div_exp_one_le
  have h4 : (1 / Real.exp 1) * (∑' j : ℕ,
      Real.log ((j : ℝ) + 14) / (((j : ℝ) + 14) + 10) ^ 2)
      ≤ (1 / Real.exp 1) * 0.1687 :=
    mul_le_mul_of_nonneg_left h1 (by positivity : (0 : ℝ) ≤ 1 / Real.exp 1)
  have h3 : (1 / Real.exp 1) * 0.1687 ≤ 0.0621 := by
    calc (1 / Real.exp 1) * 0.1687 ≤ 0.3679 * 0.1687 :=
        mul_le_mul_of_nonneg_right h2 (by norm_num)
      _ ≤ 0.0621 := by norm_num
  linarith [hle, h4, h3]

/-! ## 13. Piece 3e: the C0hat interval-count bridges -/

/-- log(a + c) ≤ log a + c/a (the log-increment machine). -/
lemma log_add_le (a c : ℝ) (ha : 0 < a) (hc : 0 ≤ c) :
    Real.log (a + c) ≤ Real.log a + c / a := by
  have hca : (0 : ℝ) ≤ c / a := div_nonneg hc ha.le
  have h1ca : (0 : ℝ) < 1 + c / a := by linarith
  have h1 : a + c = a * (1 + c / a) := by field_simp [ha.ne']
  rw [h1, Real.log_mul ha.ne' h1ca.ne']
  have h2 : Real.log (1 + c / a) ≤ c / a := by
    have h3 := Real.log_le_sub_one_of_pos (x := 1 + c / a) h1ca
    rwa [show (1 : ℝ) + c / a - 1 = c / a from by ring] at h3
  linarith [h2]

/-- e^2.5 ≤ 13 (via e² ≤ 7.3890566 and e^0.5 ≤ 1.6488). -/
lemma exp_two_point_five_le_thirteen : Real.exp 2.5 ≤ 13 := by
  have he2 : Real.exp 1 ≤ (2.7182819 : ℝ) := Real.exp_one_lt_d9.le.trans (by norm_num)
  have he5 : Real.exp 0.5 ≤ (1.6488 : ℝ) := by
    have h3' : Real.exp 0.5 ^ 2 ≤ (1.6488 : ℝ) ^ 2 := by
      have h2 : Real.exp 0.5 ^ 2 = Real.exp 1 := by
        rw [sq, ← Real.exp_add]
        congr 1
        norm_num
      rw [h2]
      calc Real.exp 1 ≤ 2.7182819 := he2
        _ ≤ (1.6488 : ℝ) ^ 2 := by norm_num
    exact le_of_pow_le_pow_left₀ (by norm_num : (2 : ℕ) ≠ 0)
      (by norm_num : (0 : ℝ) ≤ 1.6488) h3'
  have h1 : Real.exp 2.5 = Real.exp 1 * Real.exp 1 * Real.exp 0.5 := by
    rw [← Real.exp_add, ← Real.exp_add]
    congr 1
    norm_num
  rw [h1]
  have h5 : Real.exp 1 * Real.exp 1 * Real.exp 0.5 ≤ 2.7182819 * 2.7182819 * 1.6488 := by
    apply mul_le_mul
    · exact mul_le_mul he2 he2 (Real.exp_pos 1).le (by norm_num)
    · exact he5
    · exact (Real.exp_pos 0.5).le
    · positivity
  have h6 : (2.7182819 : ℝ) * 2.7182819 * 1.6488 ≤ 13 := by norm_num
  exact h5.trans h6

/-- log 13 ≥ 2.5. -/
lemma log_thirteen_ge_five : (2.5 : ℝ) ≤ Real.log 13 := by
  rw [Real.le_log_iff_exp_le (by norm_num : (0 : ℝ) < 13)]
  exact exp_two_point_five_le_thirteen

/-- **The 2k+1 bridge** (k ≥ 11): C0hat(2k+1) ≤
0.433·(log k + 0.833) + 0.886·(log log k + 0.3475) + 2.9. -/
lemma C0hat_two_mul_add_one_le {k : ℕ} (hk : 11 ≤ k) :
    C0hat (2 * (k : ℝ) + 1)
      ≤ 0.433 * (Real.log k + 0.833) + 0.886 * (Real.log (Real.log k) + 0.3475) + 2.9 := by
  have hkR : (11 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk
  have hk0 : (0 : ℝ) < (k : ℝ) := by linarith
  have hlogk : (2.3978 : ℝ) ≤ Real.log k := by
    have h2a := log_eleven_ge
    have h2b : Real.log 11 ≤ Real.log k := Real.log_le_log (by norm_num : (0 : ℝ) < 11) hkR
    linarith [h2a, h2b]
  have hlogpos : (0 : ℝ) < Real.log k := by linarith [hlogk]
  have hinc1 : Real.log (2 * (k : ℝ) + 3) ≤ Real.log k + 0.833 := by
    have h2 : (2 : ℝ) * (k : ℝ) + 3 ≤ 2.3 * (k : ℝ) := by linarith [hkR]
    have h3 : Real.log (2 * (k : ℝ) + 3) ≤ Real.log (2.3 * (k : ℝ)) :=
      Real.log_le_log (by positivity : (0 : ℝ) < 2 * (k : ℝ) + 3) h2
    have h4 : Real.log (2.3 * (k : ℝ)) = Real.log 2.3 + Real.log k :=
      Real.log_mul (by norm_num : (2.3 : ℝ) ≠ 0) hk0.ne'
    rw [h4] at h3
    have h5 := log_two_point_three_le
    linarith [h3, h5]
  have hinc2 : Real.log (Real.log (2 * (k : ℝ) + 3))
      ≤ Real.log (Real.log k) + 0.3475 := by
    have h6 : Real.log (Real.log (2 * (k : ℝ) + 3)) ≤ Real.log (Real.log k + 0.833) :=
      Real.log_le_log (Real.log_pos (by linarith [hkR] : (1 : ℝ) < 2 * (k : ℝ) + 3)) hinc1
    have h7 : Real.log (Real.log k + 0.833)
        ≤ Real.log (Real.log k) + 0.833 / Real.log k :=
      log_add_le (Real.log k) 0.833 hlogpos (by norm_num)
    have h10 : (0.833 : ℝ) / Real.log k ≤ 0.3475 := by
      rw [div_le_iff₀ hlogpos]
      have h11 : (0.3475 : ℝ) * 2.3978 ≤ 0.3475 * Real.log k :=
        mul_le_mul_of_nonneg_left hlogk (by norm_num)
      have h12 : (0.833 : ℝ) ≤ 0.3475 * 2.3978 := by norm_num
      exact h12.trans h11
    linarith [h6, h7, h10]
  rw [C0hat]
  have harg : (2 : ℝ) * (k : ℝ) + 1 + 2 = 2 * (k : ℝ) + 3 := by ring
  rw [harg]
  linarith [hinc1, hinc2]

/-- **The j+1 bridge** (j ≥ 14): C0hat(j+1) ≤
0.433·(log j + 0.1942) + 0.886·(log log j + 0.0736) + 2.9. -/
lemma C0hat_add_one_le_of_fourteen_le {j : ℕ} (hj : 14 ≤ j) :
    C0hat ((j : ℝ) + 1)
      ≤ 0.433 * (Real.log j + 0.1942) + 0.886 * (Real.log (Real.log j) + 0.0736) + 2.9 := by
  have hjR : (14 : ℝ) ≤ (j : ℝ) := by exact_mod_cast hj
  have hj0 : (0 : ℝ) < (j : ℝ) := by linarith
  have hlogj : (2.6388 : ℝ) ≤ Real.log j := by
    have h2a := log_fourteen_ge
    have h2b : Real.log 14 ≤ Real.log j := Real.log_le_log (by norm_num : (0 : ℝ) < 14) hjR
    linarith [h2a, h2b]
  have hlogpos : (0 : ℝ) < Real.log j := by linarith [hlogj]
  have hinc1 : Real.log ((j : ℝ) + 3) ≤ Real.log j + 0.1942 := by
    have h2 : (j : ℝ) + 3 ≤ (17 / 14 : ℝ) * (j : ℝ) := by linarith [hjR]
    have h3 : Real.log ((j : ℝ) + 3) ≤ Real.log ((17 / 14 : ℝ) * (j : ℝ)) :=
      Real.log_le_log (by positivity : (0 : ℝ) < (j : ℝ) + 3) h2
    have h4 : Real.log ((17 / 14 : ℝ) * (j : ℝ))
        = Real.log (17 / 14) + Real.log j :=
      Real.log_mul (by norm_num : (17 / 14 : ℝ) ≠ 0) hj0.ne'
    rw [h4] at h3
    have h5 := log_17_14_le
    linarith [h3, h5]
  have hinc2 : Real.log (Real.log ((j : ℝ) + 3))
      ≤ Real.log (Real.log j) + 0.0736 := by
    have h6 : Real.log (Real.log ((j : ℝ) + 3)) ≤ Real.log (Real.log j + 0.1942) :=
      Real.log_le_log (Real.log_pos (by linarith [hjR] : (1 : ℝ) < (j : ℝ) + 3)) hinc1
    have h7 : Real.log (Real.log j + 0.1942)
        ≤ Real.log (Real.log j) + 0.1942 / Real.log j :=
      log_add_le (Real.log j) 0.1942 hlogpos (by norm_num)
    have h10 : (0.1942 : ℝ) / Real.log j ≤ 0.0736 := by
      rw [div_le_iff₀ hlogpos]
      have h11 : (0.0736 : ℝ) * 2.6388 ≤ 0.0736 * Real.log j :=
        mul_le_mul_of_nonneg_left hlogj (by norm_num)
      have h12 : (0.1942 : ℝ) ≤ 0.0736 * 2.6388 := by norm_num
      exact h12.trans h11
    linarith [h6, h7, h10]
  rw [C0hat]
  have harg : (j : ℝ) + 1 + 2 = (j : ℝ) + 3 := by ring
  rw [harg]
  linarith [hinc1, hinc2]

/-- **The 2t+1 increment** (t ≥ 10): C0hat(2t+1) ≤ C0hat(t+1) + 0.55. -/
lemma C0hat_two_t_add_one_le {t : ℝ} (ht : 10 ≤ t) :
    C0hat (2 * t + 1) ≤ C0hat (t + 1) + 0.55 := by
  have ht3 : (0 : ℝ) < t + 3 := by linarith
  have hlog13 : (2.5 : ℝ) ≤ Real.log (t + 3) := by
    have h1 : Real.log 13 ≤ Real.log (t + 3) :=
      Real.log_le_log (by norm_num : (0 : ℝ) < 13) (by linarith : (13 : ℝ) ≤ t + 3)
    have h2 := log_thirteen_ge_five
    linarith [h1, h2]
  have hlogpos : (0 : ℝ) < Real.log (t + 3) := by linarith [hlog13]
  have hinc1 : Real.log (2 * t + 3) ≤ Real.log 2 + Real.log (t + 3) := by
    have h1 : (2 : ℝ) * t + 3 ≤ 2 * (t + 3) := by linarith
    have h2 : Real.log (2 * t + 3) ≤ Real.log (2 * (t + 3)) :=
      Real.log_le_log (by positivity : (0 : ℝ) < 2 * t + 3) h1
    have h3 : Real.log (2 * (t + 3)) = Real.log 2 + Real.log (t + 3) :=
      Real.log_mul (by norm_num : (2 : ℝ) ≠ 0) ht3.ne'
    rw [h3] at h2
    exact h2
  have hinc2 : Real.log (Real.log (2 * t + 3))
      ≤ Real.log (Real.log (t + 3)) + 0.6932 / 2.5 := by
    have h6 : Real.log (Real.log (2 * t + 3))
        ≤ Real.log (Real.log (t + 3) + Real.log 2) := by
      apply Real.log_le_log (Real.log_pos (by linarith [ht] : (1 : ℝ) < 2 * t + 3))
      linarith [hinc1]
    have h7 : Real.log (Real.log (t + 3) + Real.log 2)
        ≤ Real.log (Real.log (t + 3)) + Real.log 2 / Real.log (t + 3) :=
      log_add_le (Real.log (t + 3)) (Real.log 2) hlogpos (Real.log_nonneg (by norm_num))
    have hlog2le : Real.log 2 ≤ 0.6932 := Real.log_two_lt_d9.le.trans (by norm_num)
    have h10 : Real.log 2 / Real.log (t + 3) ≤ 0.6932 / 2.5 := by
      rw [div_le_div_iff₀ hlogpos (by norm_num : (0 : ℝ) < 2.5)]
      have h11 : (0.6932 : ℝ) * 2.5 ≤ 0.6932 * Real.log (t + 3) :=
        mul_le_mul_of_nonneg_left hlog13 (by norm_num)
      have h12 : Real.log 2 * 2.5 ≤ 0.6932 * 2.5 :=
        mul_le_mul_of_nonneg_right hlog2le (by norm_num)
      linarith [h11, h12]
    linarith [h6, h7, h10]
  have hlog2le : Real.log 2 ≤ 0.6932 := Real.log_two_lt_d9.le.trans (by norm_num)
  rw [C0hat, C0hat]
  have harg1 : (2 : ℝ) * t + 1 + 2 = 2 * t + 3 := by ring
  have harg2 : (t : ℝ) + 1 + 2 = t + 3 := by ring
  rw [harg1, harg2]
  linarith [hinc1, hinc2, hlog2le]

/-! ## 14. Piece 3f: the conjugation machinery (zeroMult_conj) -/

/-- ζ is analytic at iterated-derivatives level (off the pole). -/
lemma analyticAt_iteratedDeriv_riemannZeta (n : ℕ) {z : ℂ} (hz : z ≠ 1) :
    AnalyticAt ℂ (iteratedDeriv n riemannZeta) z := by
  induction n with
  | zero => exact analyticAt_riemannZeta hz
  | succ m ihm =>
    rw [iteratedDeriv_succ]
    exact ihm.deriv

/-- **The conjugate-derivative lemma**: if f commutes with conjugation at
z and near conj z, and is differentiable at z, then f has derivative
conj(deriv f z) at conj z. -/
lemma hasDerivAt_conj_of_conj_eq {f : ℂ → ℂ} {z : ℂ}
    (hzid : f ((starRingEnd ℂ) z) = (starRingEnd ℂ) (f z))
    (hconj : ∀ᶠ s in 𝓝 ((starRingEnd ℂ) z),
      f ((starRingEnd ℂ) s) = (starRingEnd ℂ) (f s))
    (hdiff : DifferentiableAt ℂ f z) :
    HasDerivAt f ((starRingEnd ℂ) (deriv f z)) ((starRingEnd ℂ) z) := by
  have hslope : (fun x : ℂ => (f x - f z) / (x - z)) = slope f z := by
    funext x
    rw [slope, vsub_eq_sub, smul_eq_mul, div_eq_mul_inv, mul_comm]
  have h1 : Filter.Tendsto (fun x : ℂ => (f x - f z) / (x - z)) (𝓝[≠] z)
      (𝓝 (deriv f z)) := by
    rw [hslope]
    exact hasDerivAt_iff_tendsto_slope.mp hdiff.hasDerivAt
  have hmap : Filter.Tendsto (starRingEnd ℂ) (𝓝[≠] ((starRingEnd ℂ) z)) (𝓝[≠] z) := by
    have hcont : Continuous ((starRingEnd ℂ) : ℂ → ℂ) := Complex.continuous_conj
    have hT : Filter.Tendsto (starRingEnd ℂ) (𝓝[≠] ((starRingEnd ℂ) z)) (𝓝 z) := by
      have h := hcont.tendsto ((starRingEnd ℂ) z)
      rw [starRingEnd_self_apply] at h
      exact h.mono_left nhdsWithin_le_nhds
    have hp : Filter.Tendsto (starRingEnd ℂ) (𝓝[≠] ((starRingEnd ℂ) z)) (Filter.principal {z}ᶜ) := by
      rw [Filter.tendsto_principal, eventually_nhdsWithin_iff]
      exact Filter.Eventually.of_forall (fun x (hx : x ≠ (starRingEnd ℂ) z) => by
        show (starRingEnd ℂ) x ∈ {z}ᶜ
        rw [Set.mem_compl_singleton_iff]
        intro hbad
        apply hx
        have h3 := congrArg (starRingEnd ℂ) hbad
        rwa [starRingEnd_self_apply] at h3)
    have h2 := hT.inf hp
    rw [inf_idem] at h2
    have hunfold : (𝓝[≠] z) = 𝓝 z ⊓ Filter.principal {z}ᶜ := rfl
    rw [hunfold]
    exact h2
  have h2 : Filter.Tendsto (fun x : ℂ =>
      (starRingEnd ℂ) ((f ((starRingEnd ℂ) x) - f z) / (((starRingEnd ℂ) x) - z)))
      (𝓝[≠] ((starRingEnd ℂ) z)) (𝓝 ((starRingEnd ℂ) (deriv f z))) := by
    have hcont : Continuous ((starRingEnd ℂ) : ℂ → ℂ) := Complex.continuous_conj
    exact (hcont.tendsto _).comp (h1.comp hmap)
  have h4 : (fun x : ℂ => (f x - f ((starRingEnd ℂ) z)) / (x - (starRingEnd ℂ) z))
      =ᶠ[𝓝 ((starRingEnd ℂ) z)] fun x : ℂ =>
      (starRingEnd ℂ) ((f ((starRingEnd ℂ) x) - f z) / (((starRingEnd ℂ) x) - z)) := by
    exact hconj.mono (fun s hs => by
      show (f s - f ((starRingEnd ℂ) z)) / (s - (starRingEnd ℂ) z)
        = (starRingEnd ℂ) ((f ((starRingEnd ℂ) s) - f z) / ((starRingEnd ℂ) s - z))
      rw [hs, hzid]
      simp only [map_sub, map_div₀, starRingEnd_self_apply])
  have h3 : (fun x : ℂ => (f x - f ((starRingEnd ℂ) z)) / (x - (starRingEnd ℂ) z))
      =ᶠ[𝓝[≠] ((starRingEnd ℂ) z)] fun x : ℂ =>
      (starRingEnd ℂ) ((f ((starRingEnd ℂ) x) - f z) / (((starRingEnd ℂ) x) - z)) :=
    h4.filter_mono nhdsWithin_le_nhds
  have hslope2 : (fun x : ℂ => (f x - f ((starRingEnd ℂ) z)) / (x - (starRingEnd ℂ) z))
      = slope f ((starRingEnd ℂ) z) := by
    funext x
    rw [slope, vsub_eq_sub, smul_eq_mul, div_eq_mul_inv, mul_comm]
  rw [hslope2] at h3
  exact hasDerivAt_iff_tendsto_slope.mpr (Filter.Tendsto.congr' h3.symm h2)

/-- The iterated derivatives of ζ commute with conjugation (off the pole). -/
lemma iteratedDeriv_riemannZeta_conj (n : ℕ) {z : ℂ} (hz : z ≠ 1) :
    iteratedDeriv n riemannZeta ((starRingEnd ℂ) z)
      = (starRingEnd ℂ) (iteratedDeriv n riemannZeta z) := by
  induction n generalizing z with
  | zero =>
    exact _root_.riemannZeta_conj z
  | succ m ihm =>
    have hzid := ihm hz
    have hconj : ∀ᶠ s in 𝓝 ((starRingEnd ℂ) z),
        iteratedDeriv m riemannZeta ((starRingEnd ℂ) s)
          = (starRingEnd ℂ) (iteratedDeriv m riemannZeta s) := by
      have hne : ∀ᶠ s in 𝓝 ((starRingEnd ℂ) z), s ≠ 1 := by
        have h1 : ((starRingEnd ℂ) z) ≠ 1 := by
          intro h
          apply hz
          have h2 := congrArg (starRingEnd ℂ) h
          rwa [starRingEnd_self_apply, map_one] at h2
        exact Filter.eventually_of_mem
          (isOpen_compl_singleton.mem_nhds (Set.mem_compl_singleton_iff.mpr h1))
          (fun s hs => Set.mem_compl_singleton_iff.mp hs)
      exact hne.mono (fun s hs => ihm hs)
    have hd : DifferentiableAt ℂ (iteratedDeriv m riemannZeta) z :=
      (analyticAt_iteratedDeriv_riemannZeta m hz).differentiableAt
    rw [iteratedDeriv_succ]
    exact (hasDerivAt_conj_of_conj_eq hzid hconj hd).deriv

/-- The analytic order of ζ is conjugation-invariant (off the pole). -/
lemma analyticOrderAt_riemannZeta_conj {z : ℂ} (hz : z ≠ 1) :
    analyticOrderAt riemannZeta ((starRingEnd ℂ) z) = analyticOrderAt riemannZeta z := by
  have hzconj : ((starRingEnd ℂ) z) ≠ 1 := by
    intro h
    apply hz
    have h2 := congrArg (starRingEnd ℂ) h
    rwa [starRingEnd_self_apply, map_one] at h2
  have htop : analyticOrderAt riemannZeta z ≠ ⊤ := zeta_order_ne_top_of_ne hz
  set n := analyticOrderNatAt riemannZeta z with hn
  have hzn : analyticOrderAt riemannZeta z = (n : ℕ∞) :=
    (Nat.cast_analyticOrderNatAt htop).symm
  obtain ⟨hz1, hz2⟩ :=
    (analyticOrderAt_eq_nat_iff_iteratedDeriv_eq_zero (analyticAt_riemannZeta hz)).mp hzn
  have hkey : analyticOrderAt riemannZeta ((starRingEnd ℂ) z) = (n : ℕ∞) :=
    (analyticOrderAt_eq_nat_iff_iteratedDeriv_eq_zero (analyticAt_riemannZeta hzconj)).mpr
      ⟨fun k hk => by
        rw [iteratedDeriv_riemannZeta_conj k hz, hz1 k hk, map_zero],
       by
        rw [iteratedDeriv_riemannZeta_conj n hz]
        intro hbad
        apply hz2
        have h3 := congrArg (starRingEnd ℂ) hbad
        rwa [starRingEnd_self_apply, map_zero] at h3⟩
  rw [hzn, hkey]

/-- **zeroMult is conjugation-invariant** (the left-tail count bridge). -/
lemma zeroMult_conj {ρ : ℂ} (hρ : IsNontrivialZero ρ) :
    zeroMult ((starRingEnd ℂ) ρ) = zeroMult ρ := by
  have hρ1 : ρ ≠ 1 := by
    rintro rfl
    have h := hρ.2.2
    rw [Complex.one_re] at h
    exact (lt_irrefl (1 : ℝ)) h
  show (analyticOrderAt riemannZeta ((starRingEnd ℂ) ρ)).toNat
      = (analyticOrderAt riemannZeta ρ).toNat
  rw [analyticOrderAt_riemannZeta_conj hρ1]

/-! ## 15. Piece 3g: the farG majorant and the norm reduction -/

/-- The far-term majorant: 4.5·m/(x√(1+x²)) with x = |γ − t| (0 for near
zeros). -/
def farG (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : ℝ :=
  if ρ ∈ nearZerosSub t then (0 : ℝ)
  else 4.5 * (zeroMult ρ.1 : ℝ) / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))

lemma farG_nonneg (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : (0 : ℝ) ≤ farG t ρ := by
  classical
  by_cases h : ρ ∈ nearZerosSub t
  · rw [farG, if_pos h]
  · rw [farG, if_neg h]
    positivity

/-- The absolute-value estimate: |γ| ≤ (1+t)·x for x = |γ−t| ≥ 1, t ≥ 0. -/
lemma abs_im_le_one_add_mul {t : ℝ} (ht : 0 ≤ t) {γ : ℝ} (hx : 1 ≤ |γ - t|) :
    |γ| ≤ (1 + t) * |γ - t| := by
  have h1 : |γ| ≤ |γ - t| + t := by
    calc |γ| = |γ - t + t| := by rw [sub_add_cancel]
      _ ≤ |γ - t| + |t| := abs_add_le _ _
      _ = |γ - t| + t := by rw [abs_of_nonneg ht]
  have h2 : t ≤ t * |γ - t| := by
    have h3 : t * 1 ≤ t * |γ - t| := mul_le_mul_of_nonneg_left hx ht
    rwa [mul_one] at h3
  linarith [h1, h2]

/-- farG is summable (dominated by 18(1+t)²·m/(1+‖γ‖²)). -/
lemma farG_summable {t : ℝ} (ht : 0 ≤ t) : Summable (farG t) := by
  classical
  have hbase : Summable (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
      (18 * (1 + t) ^ 2) * ((zeroMult ρ : ℝ) / (1 + Complex.normSq (gammaOf (ρ : ℂ))))) :=
    (WeilEF.zero_sum_inv_sq zetaSeam).mul_left _
  apply Summable.of_norm_bounded hbase
  intro ρ
  rw [Real.norm_eq_abs, abs_of_nonneg (farG_nonneg t ρ)]
  rw [farG]
  by_cases h : ρ ∈ nearZerosSub t
  · rw [if_pos h]
    apply mul_nonneg (by positivity : (0 : ℝ) ≤ 18 * (1 + t) ^ 2)
    apply div_nonneg (Nat.cast_nonneg _)
    have h2 := Complex.normSq_nonneg (gammaOf (ρ : ℂ))
    linarith
  · rw [if_neg h]
    have hx1 : (1 : ℝ) ≤ |ρ.1.im - t| :=
      (not_lt).mp (fun hlt => h (mem_nearZerosSub.mpr hlt))
    -- the normSq(gammaOf) comparison (the tailTerm_summable pattern)
    have hgam : Complex.normSq (gammaOf (ρ : ℂ))
        = (ρ : ℂ).im ^ 2 + (1 / 2 - (ρ : ℂ).re) ^ 2 := by
      rw [Complex.normSq_apply, WeilEF.gammaOf_re, WeilEF.gammaOf_im]
      ring
    have hcmp : 1 + Complex.normSq (gammaOf (ρ : ℂ)) ≤ 2 * (1 + (ρ : ℂ).im ^ 2) := by
      rw [hgam]
      have hre : ((ρ : ℂ).re - 1 / 2) ^ 2 ≤ 1 / 4 := by
        have h0 := ρ.2.2.1
        have h1 := ρ.2.2.2
        nlinarith [sq_nonneg ((ρ : ℂ).re - 1 / 2)]
      nlinarith [sq_nonneg (ρ : ℂ).im]
    -- the (1+t)²x² comparison
    have hγbound : |ρ.1.im| ≤ (1 + t) * |ρ.1.im - t| := abs_im_le_one_add_mul ht hx1
    have h4 : 1 + ρ.1.im ^ 2 ≤ 2 * (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 := by
      have h5 : ρ.1.im ^ 2 ≤ (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 := by
        calc ρ.1.im ^ 2 = |ρ.1.im| ^ 2 := by rw [sq_abs]
          _ ≤ ((1 + t) * |ρ.1.im - t|) ^ 2 :=
              pow_le_pow_left₀ (abs_nonneg _) hγbound 2
          _ = (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 := by ring
      have h8 : (1 : ℝ) ≤ (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 := by
        have h9 : (1 : ℝ) ≤ (1 + t) ^ 2 := by
          have h10 : (1 : ℝ) ≤ 1 + t := by linarith [ht]
          nlinarith [h10]
        have h11 : (1 : ℝ) ≤ |ρ.1.im - t| ^ 2 := by
          have h12 := pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 1) hx1 2
          rwa [one_pow] at h12
        calc (1 : ℝ) = 1 * 1 := by ring
          _ ≤ (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 :=
              mul_le_mul h9 h11 (by positivity) (by positivity)
      linarith [h5, h8]
    -- 1/(x√(1+x²)) ≤ 1/x² ≤ 2(1+t)²/(1+γ²)
    have h6 : 1 / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))
        ≤ 1 / |ρ.1.im - t| ^ 2 := by
      have h7a : |ρ.1.im - t| ≤ Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
        calc |ρ.1.im - t| = Real.sqrt ((ρ.1.im - t) ^ 2) := by rw [Real.sqrt_sq_eq_abs]
          _ ≤ Real.sqrt (1 + (ρ.1.im - t) ^ 2) :=
            Real.sqrt_le_sqrt (by linarith [sq_nonneg (ρ.1.im - t)])
      have h7 : |ρ.1.im - t| * |ρ.1.im - t|
          ≤ |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) :=
        mul_le_mul_of_nonneg_left h7a (abs_nonneg _)
      have h8 : (0 : ℝ) < |ρ.1.im - t| * |ρ.1.im - t| := by positivity
      have h9 := one_div_le_one_div_of_le h8 h7
      rwa [← pow_two] at h9
    have h10 : 1 / |ρ.1.im - t| ^ 2 ≤ (2 * (1 + t) ^ 2) / (1 + ρ.1.im ^ 2) := by
      rw [div_le_div_iff₀ (by positivity : (0 : ℝ) < |ρ.1.im - t| ^ 2)
        (by positivity : (0 : ℝ) < 1 + ρ.1.im ^ 2)]
      calc (1 : ℝ) * (1 + ρ.1.im ^ 2) = 1 + ρ.1.im ^ 2 := by ring
        _ ≤ 2 * (1 + t) ^ 2 * |ρ.1.im - t| ^ 2 := h4
    -- m/(1+γ²) ≤ 2·m/(1+normSq)
    have hpos1 : (0 : ℝ) < 1 + Complex.normSq (gammaOf (ρ : ℂ)) := by
      have h := Complex.normSq_nonneg (gammaOf (ρ : ℂ)); linarith
    have hpos2 : (0 : ℝ) < 1 + (ρ : ℂ).im ^ 2 := by positivity
    have hfrac : 1 / (1 + (ρ : ℂ).im ^ 2) ≤ 2 / (1 + Complex.normSq (gammaOf (ρ : ℂ))) := by
      rw [div_le_div_iff₀ hpos2 hpos1]
      nlinarith [hcmp]
    have hm : (0 : ℝ) ≤ (zeroMult ρ : ℝ) := Nat.cast_nonneg _
    -- assemble
    have h61 : (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))⁻¹
        ≤ (2 * (1 + t) ^ 2) / (1 + ρ.1.im ^ 2) := by
      rw [← one_div]
      exact h6.trans h10
    calc 4.5 * (zeroMult ρ : ℝ) / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))
        = (4.5 * (zeroMult ρ : ℝ)) * (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))⁻¹ := by
          rw [div_eq_mul_inv]
      _ ≤ (4.5 * (zeroMult ρ : ℝ)) * ((2 * (1 + t) ^ 2) / (1 + ρ.1.im ^ 2)) :=
          mul_le_mul_of_nonneg_left h61
            (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) hm)
      _ = (9 * (1 + t) ^ 2) * ((zeroMult ρ : ℝ) * (1 / (1 + ρ.1.im ^ 2))) := by ring
      _ ≤ (9 * (1 + t) ^ 2) * ((zeroMult ρ : ℝ) * (2 / (1 + Complex.normSq (gammaOf (ρ : ℂ))))) :=
          mul_le_mul_of_nonneg_left (mul_le_mul_of_nonneg_left hfrac hm)
            (by positivity : (0 : ℝ) ≤ 9 * (1 + t) ^ 2)
      _ = (18 * (1 + t) ^ 2) * ((zeroMult ρ : ℝ) / (1 + Complex.normSq (gammaOf (ρ : ℂ)))) := by
          ring

/-- The per-term norm identity for the far difference term. -/
lemma seriesDiffTerm_norm_eq {s s₀ : ℂ} (ρ : {ρ : ℂ // IsNontrivialZero ρ})
    (hs : s ≠ ρ.1) (hs₀ : s₀ ≠ ρ.1) :
    ‖seriesDiffTerm s s₀ ρ‖
      = (zeroMult ρ.1 : ℝ) * ‖s₀ - s‖ / (‖s - ρ.1‖ * ‖s₀ - ρ.1‖) := by
  have h1 : seriesDiffTerm s s₀ ρ
      = (zeroMult ρ.1 : ℂ) * ((s₀ - s) / ((s - ρ.1) * (s₀ - ρ.1))) := by
    rw [seriesDiffTerm]
    have h2 : 1 / (s - ρ.1) - 1 / (s₀ - ρ.1)
        = (s₀ - s) / ((s - ρ.1) * (s₀ - ρ.1)) := by
      field_simp [sub_ne_zero.mpr hs, sub_ne_zero.mpr hs₀]
      ring
    rw [h2]
  rw [h1, norm_mul, norm_div, norm_mul]
  have h3 : ‖(zeroMult ρ.1 : ℂ)‖ = (zeroMult ρ.1 : ℝ) := by
    rw [show (zeroMult ρ.1 : ℂ) = ((zeroMult ρ.1 : ℝ) : ℂ) by push_cast; ring,
      Complex.norm_real]
    exact abs_of_nonneg (Nat.cast_nonneg _)
  rw [h3, mul_div_assoc]

/-- The per-term far bound: the far difference term is bounded by farG. -/
lemma seriesDiffTerm_norm_le_farG {t : ℝ} (_ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t)
    (hζ : riemannZeta s ≠ 0) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) :
    ‖if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖
      ≤ farG t ρ := by
  classical
  by_cases h : ρ ∈ nearZerosSub t
  · rw [if_pos h, norm_zero, farG, if_pos h]
  · rw [if_neg h, farG, if_neg h]
    have hx1 : (1 : ℝ) ≤ |ρ.1.im - t| :=
      (not_lt).mp (fun hlt => h (mem_nearZerosSub.mpr hlt))
    -- s ≠ ρ and s₀ ≠ ρ
    have hs : s ≠ ρ.1 := by
      intro hbad
      apply hζ
      rw [hbad]
      exact ρ.2.1
    have hs₀ : (2 + t * Complex.I) ≠ ρ.1 := by
      intro hbad
      have hre := ρ.2.2.2
      have h1 : (2 + t * Complex.I).re = ρ.1.re := by rw [hbad]
      simp at h1
      linarith [h1, hre]
    rw [seriesDiffTerm_norm_eq ρ hs hs₀]
    -- the three factors
    have hdiff : (2 + t * Complex.I) - s = ((2 - s.re : ℝ) : ℂ) := by
      rw [Complex.ext_iff]
      constructor
      · simp
      · simp [him]
    have hsdiff : ‖(2 + t * Complex.I) - s‖ = |2 - s.re| := by
      rw [hdiff]
      exact Complex.norm_real _
    have hsdiffle : ‖(2 + t * Complex.I) - s‖ ≤ 4.5 := by
      rw [hsdiff, abs_le]
      constructor <;> linarith
    have hx : ‖s - ρ.1‖ ≥ |ρ.1.im - t| := by
      have h1 := abs_im_le_norm (s - ρ.1)
      have h2 : (s - ρ.1).im = t - ρ.1.im := by simp [him]
      rw [h2, abs_sub_comm] at h1
      exact h1
    have hx₀ : ‖(2 + t * Complex.I) - ρ.1‖ ≥ Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
      have hβ : |2 - ρ.1.re| ≥ 1 := by
        have h1 := ρ.2.2.2
        rw [abs_of_pos (by linarith : (0 : ℝ) < 2 - ρ.1.re)]
        linarith
      have h1 : ‖(2 + t * Complex.I) - ρ.1‖ ^ 2
          = ((2 + t * Complex.I) - ρ.1).re ^ 2 + ((2 + t * Complex.I) - ρ.1).im ^ 2 := by
        rw [← Complex.normSq_eq_norm_sq, Complex.normSq_apply]
        ring
      have h2 : (1 : ℝ) + (ρ.1.im - t) ^ 2 ≤ ‖(2 + t * Complex.I) - ρ.1‖ ^ 2 := by
        rw [h1]
        have hre : ((2 + t * Complex.I) - ρ.1).re = 2 - ρ.1.re := by simp
        have him2 : ((2 + t * Complex.I) - ρ.1).im = t - ρ.1.im := by simp
        rw [hre, him2]
        have h3 : (2 - ρ.1.re) ^ 2 ≥ 1 := by
          have h4 := pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 1) hβ 2
          rwa [one_pow, sq_abs] at h4
        have h5 : (t - ρ.1.im) ^ 2 = (ρ.1.im - t) ^ 2 := by ring
        rw [h5]
        linarith [h3, sq_nonneg (ρ.1.im - t)]
      have h6 := Real.sqrt_le_sqrt h2
      rwa [Real.sqrt_sq_eq_abs, abs_of_nonneg (norm_nonneg _)] at h6
    -- assemble
    have hpos1 : (0 : ℝ) < ‖s - ρ.1‖ * ‖(2 + t * Complex.I) - ρ.1‖ := by
      have h7 : (0 : ℝ) < ‖s - ρ.1‖ := norm_pos_iff.mpr (sub_ne_zero.mpr hs)
      have h8 : (0 : ℝ) < ‖(2 + t * Complex.I) - ρ.1‖ :=
        norm_pos_iff.mpr (sub_ne_zero.mpr hs₀)
      positivity
    have hpos2 : (0 : ℝ) < |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
      positivity
    have h9 : ‖(2 + t * Complex.I) - s‖ ≤ 4.5 := hsdiffle
    have hm : (0 : ℝ) ≤ (zeroMult ρ.1 : ℝ) := Nat.cast_nonneg _
    rw [div_le_div_iff₀ hpos1 hpos2]
    calc (zeroMult ρ.1 : ℝ) * ‖(2 + t * Complex.I) - s‖
          * (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))
        ≤ (zeroMult ρ.1 : ℝ) * 4.5 * (‖s - ρ.1‖ * ‖(2 + t * Complex.I) - ρ.1‖) := by
          apply mul_le_mul _ _ (by positivity) (by positivity)
          · exact mul_le_mul_of_nonneg_left h9 hm
          · exact mul_le_mul hx hx₀ (Real.sqrt_nonneg _) (norm_nonneg _)
      _ = (4.5 * (zeroMult ρ.1 : ℝ)) * (‖s - ρ.1‖ * ‖(2 + t * Complex.I) - ρ.1‖) := by ring

/-- **The norm reduction**: the far-series norm is bounded by the farG
tsum. -/
lemma far_series_norm_le_farG_tsum {t : ℝ} (ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t)
    (hζ : riemannZeta s ≠ 0) :
    ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
      if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖
      ≤ ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farG t ρ := by
  classical
  have hnorm_summ : Summable (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
      ‖if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖) := by
    apply Summable.of_norm_bounded (farG_summable (by linarith : (0 : ℝ) ≤ t))
    intro ρ
    rw [Real.norm_eq_abs, abs_of_nonneg (norm_nonneg _)]
    exact seriesDiffTerm_norm_le_farG ht hσlo hσhi him hζ ρ
  have h1 := norm_tsum_le_tsum_norm hnorm_summ
  have h2 : (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
      ‖if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖)
      ≤ ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farG t ρ :=
    Summable.tsum_le_tsum (fun ρ => seriesDiffTerm_norm_le_farG ht hσlo hσhi him hζ ρ)
      hnorm_summ (farG_summable (by linarith : (0 : ℝ) ≤ t))
  exact h1.trans h2

/-! ## 16. Piece 3h: the main bucket machine -/

/-- **The generic bucket-family bound** (the unit-interval analog of
LiCircleBound's `dyadic_family_le`): with bucket index j(ρ) ∈ Ico 1 (K+1),
per-element bound g ρ ≤ W(j(ρ))·m, and per-bucket mult count ≤ cnt k,
the FS-sum of g is at most Σ_{k ∈ Ico 1 (K+1)} W k · cnt k. -/
lemma unit_family_le (FS : Finset {ρ : ℂ // IsNontrivialZero ρ})
    (j : {ρ : ℂ // IsNontrivialZero ρ} → ℕ) (K : ℕ)
    (g : {ρ : ℂ // IsNontrivialZero ρ} → ℝ) (W : ℕ → ℝ) (cnt : ℕ → ℝ)
    (hW : ∀ k : ℕ, 0 ≤ W k)
    (hj : ∀ ρ ∈ FS, 1 ≤ j ρ ∧ j ρ ≤ K)
    (hterm : ∀ ρ ∈ FS, g ρ ≤ W (j ρ) * (zeroMult (ρ : ℂ) : ℝ))
    (hcount : ∀ k : ℕ,
      ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), (zeroMult (ρ : ℂ) : ℝ) ≤ cnt k) :
    ∑ ρ ∈ FS, g ρ ≤ ∑ k ∈ Finset.Ico 1 (K + 1), W k * cnt k := by
  classical
  have h1 : ∑ ρ ∈ FS, g ρ
      ≤ ∑ ρ ∈ FS, W (j ρ) * (zeroMult (ρ : ℂ) : ℝ) :=
    Finset.sum_le_sum (fun ρ hρ => hterm ρ hρ)
  have hmaps : ∀ ρ ∈ FS, j ρ ∈ Finset.Ico 1 (K + 1) := by
    intro ρ hρ
    rw [Finset.mem_Ico]
    have h1' := (hj ρ hρ).1
    have h2' := (hj ρ hρ).2
    exact ⟨h1', by omega⟩
  have h2 : ∑ ρ ∈ FS, W (j ρ) * (zeroMult (ρ : ℂ) : ℝ)
      = ∑ k ∈ Finset.Ico 1 (K + 1),
        ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), W (j ρ) * (zeroMult (ρ : ℂ) : ℝ) :=
    (Finset.sum_fiberwise_of_maps_to hmaps
      (fun ρ => W (j ρ) * (zeroMult (ρ : ℂ) : ℝ))).symm
  have h3 : ∀ k ∈ Finset.Ico 1 (K + 1),
      ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), W (j ρ) * (zeroMult (ρ : ℂ) : ℝ)
      = W k * ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), (zeroMult (ρ : ℂ) : ℝ) := by
    intro k hk
    have hterm' : ∀ ρ ∈ FS.filter (fun ρ => j ρ = k),
        W (j ρ) * (zeroMult (ρ : ℂ) : ℝ) = W k * (zeroMult (ρ : ℂ) : ℝ) := by
      intro ρ hρ
      rw [Finset.mem_filter] at hρ
      rw [hρ.2]
    rw [Finset.sum_congr rfl hterm', Finset.mul_sum]
  have h4 : ∀ k ∈ Finset.Ico 1 (K + 1),
      W k * ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), (zeroMult (ρ : ℂ) : ℝ)
        ≤ W k * cnt k :=
    fun k hk => mul_le_mul_of_nonneg_left (hcount k) (hW k)
  calc ∑ ρ ∈ FS, g ρ
      ≤ ∑ ρ ∈ FS, W (j ρ) * (zeroMult (ρ : ℂ) : ℝ) := h1
    _ = ∑ k ∈ Finset.Ico 1 (K + 1),
          ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), W (j ρ) * (zeroMult (ρ : ℂ) : ℝ) := h2
    _ = ∑ k ∈ Finset.Ico 1 (K + 1),
          W k * ∑ ρ ∈ FS.filter (fun ρ => j ρ = k), (zeroMult (ρ : ℂ) : ℝ) :=
        Finset.sum_congr rfl h3
    _ ≤ ∑ k ∈ Finset.Ico 1 (K + 1), W k * cnt k :=
        Finset.sum_le_sum h4

/-- The main finset: far zeros with bucket k = ⌊x⌋₊ ≤ ⌊t⌋₊ (x = |γ−t|,
equivalently x < ⌊t⌋₊+1; contained in the ball ‖ρ‖ < 2t+2). -/
def mainFinset (t : ℝ) : Finset {ρ : ℂ // IsNontrivialZero ρ} :=
  (finite_subtype_norm_lt (2 * t + 2)).toFinset.filter fun ρ =>
    1 ≤ |ρ.1.im - t| ∧ |ρ.1.im - t| < (Nat.floor t : ℝ) + 1

lemma mem_mainFinset {t : ℝ} (ht : 0 ≤ t) {ρ : {ρ : ℂ // IsNontrivialZero ρ}} :
    ρ ∈ mainFinset t ↔
      1 ≤ |ρ.1.im - t| ∧ |ρ.1.im - t| < (Nat.floor t : ℝ) + 1 := by
  rw [mainFinset, Finset.mem_filter]
  refine ⟨fun h => h.2, fun h => ⟨?_, h⟩⟩
  rw [Set.Finite.mem_toFinset]
  show ‖ρ.1‖ < 2 * t + 2
  have hle : ‖ρ.1‖ ≤ |ρ.1.re| + |ρ.1.im| := Complex.norm_le_abs_re_add_abs_im _
  have hre : |ρ.1.re| ≤ 1 := by
    have h1 : 0 < ρ.1.re := ρ.2.2.1
    have h2 : ρ.1.re < 1 := ρ.2.2.2
    rw [abs_le]
    constructor <;> linarith
  have him : |ρ.1.im| ≤ t + |ρ.1.im - t| := by
    calc |ρ.1.im| = |ρ.1.im - t + t| := by rw [sub_add_cancel]
      _ ≤ |ρ.1.im - t| + |t| := abs_add_le _ _
      _ = |ρ.1.im - t| + t := by rw [abs_of_nonneg ht]
      _ = t + |ρ.1.im - t| := by ring
  have hft : (Nat.floor t : ℝ) ≤ t := Nat.floor_le ht
  have hx := h.2
  have htr : (Nat.floor t : ℝ) + 1 ≤ t + 1 := by linarith [hft]
  have hx2 : |ρ.1.im - t| < t + 1 := lt_of_lt_of_le hx htr
  linarith [hle, hre, him, hx2]

/-- The bucket index of a main-finset element lies in [1, ⌊t⌋₊]. -/
lemma mainFinset_bucket_range {t : ℝ} (ht : 0 ≤ t)
    {ρ : {ρ : ℂ // IsNontrivialZero ρ}} (hρ : ρ ∈ mainFinset t) :
    1 ≤ Nat.floor |ρ.1.im - t| ∧ Nat.floor |ρ.1.im - t| ≤ Nat.floor t := by
  have hx := (mem_mainFinset ht).mp hρ
  constructor
  · have h1 := Nat.floor_mono hx.1
    rwa [Nat.floor_one] at h1
  · by_contra hbad
    push Not at hbad
    have h2 : Nat.floor t + 1 ≤ Nat.floor |ρ.1.im - t| := hbad
    have h3 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ (Nat.floor |ρ.1.im - t| : ℝ) := by
      exact_mod_cast h2
    have h4 : (Nat.floor |ρ.1.im - t| : ℝ) ≤ |ρ.1.im - t| :=
      Nat.floor_le (abs_nonneg _)
    have h5 : ((Nat.floor t : ℕ) : ℝ) + 1 ≤ |ρ.1.im - t| := by
      have h6 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by
        push_cast; ring
      rw [h6] at h3
      linarith [h3, h4]
    linarith [hx.2, h5]

/-- farG is bounded by 4.5·farW(⌊x⌋₊)·m (the monotone weight chain). -/
lemma farG_le_farW_mul {t : ℝ} (ρ : {ρ : ℂ // IsNontrivialZero ρ})
    (hx : 1 ≤ |ρ.1.im - t|) :
    farG t ρ ≤ 4.5 * farW (Nat.floor |ρ.1.im - t|) * (zeroMult ρ.1 : ℝ) := by
  classical
  have hfar : ρ ∉ nearZerosSub t := fun hmem => by
    have h' := mem_nearZerosSub.mp hmem
    linarith [h', hx]
  rw [farG, if_neg hfar, farW]
  have hk1 : (1 : ℝ) ≤ ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) := by
    have h1 := Nat.floor_mono hx
    rw [Nat.floor_one] at h1
    exact_mod_cast h1
  have hkx : ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) ≤ |ρ.1.im - t| :=
    Nat.floor_le (abs_nonneg _)
  have hsq : (ρ.1.im - t) ^ 2 = |ρ.1.im - t| ^ 2 := by rw [sq_abs]
  have hmono : ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ)
        * Real.sqrt (1 + ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) ^ 2)
      ≤ |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
    rw [hsq]
    apply mul_le_mul hkx _ (Real.sqrt_nonneg _) (abs_nonneg _)
    apply Real.sqrt_le_sqrt
    have h1 : (0 : ℝ) ≤ ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) := by positivity
    nlinarith [hkx, h1, abs_nonneg (ρ.1.im - t)]
  have hm : (0 : ℝ) ≤ (zeroMult ρ.1 : ℝ) := Nat.cast_nonneg _
  have hpos : (0 : ℝ) < ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ)
      * Real.sqrt (1 + ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) ^ 2) := by
    have h1 : (0 : ℝ) < ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) := by linarith [hk1]
    positivity
  rw [show (4.5 : ℝ) * (zeroMult ρ.1 : ℝ) / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))
      = (4.5 * (zeroMult ρ.1 : ℝ)) * (1 / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))) from by
        rw [div_eq_mul_inv, one_div]]
  rw [show ((4.5 : ℝ) * (1 / (((Nat.floor |ρ.1.im - t| : ℕ) : ℝ)
        * Real.sqrt (1 + ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) ^ 2))) * (zeroMult ρ.1 : ℝ))
      = (4.5 * (zeroMult ρ.1 : ℝ)) * (1 / (((Nat.floor |ρ.1.im - t| : ℕ) : ℝ)
        * Real.sqrt (1 + ((Nat.floor |ρ.1.im - t| : ℕ) : ℝ) ^ 2))) from by ring]
  exact mul_le_mul_of_nonneg_left (one_div_le_one_div_of_le hpos hmono)
    (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) hm)

/-- **The main bucket count**: the mult-weighted count of main-finset
zeros with bucket k (1 ≤ k ≤ ⌊t⌋₊) is at most 2·C0hat(t+1) + 0.55
(the two-window perimeter bound + NoLowZeros emptiness for low windows). -/
lemma main_bucket_count (hZC : ZeroCountProp) (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t)
    {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k ≤ Nat.floor t) :
    ∑ ρ ∈ (mainFinset t).filter (fun ρ => Nat.floor |ρ.1.im - t| = k), (zeroMult ρ.1 : ℝ)
      ≤ 2 * C0hat (t + 1) + 0.55 := by
  classical
  set FS := (mainFinset t).filter (fun ρ => Nat.floor |ρ.1.im - t| = k) with hFS
  have hk2R : (k : ℝ) ≤ t :=
    (by exact_mod_cast hk2 : (k : ℝ) ≤ (Nat.floor t : ℝ)).trans
      (Nat.floor_le (by linarith : (0 : ℝ) ≤ t))
  -- the right window count
  have htk2 : (2 : ℝ) ≤ t + (k : ℝ) := by
    have h1 : (1 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk1
    linarith [ht, h1]
  have hright : ((zeroWindow_finite (t + (k : ℝ))).toFinset.sum fun ρ => zeroMult ρ : ℝ)
      ≤ C0hat (t + (k : ℝ) + 1) := hZC (t + (k : ℝ)) htk2
  have hright2 : C0hat (t + (k : ℝ) + 1) ≤ C0hat (t + 1) + 0.55 := by
    have h1 : C0hat (t + (k : ℝ) + 1) ≤ C0hat (2 * t + 1) := by
      apply C0hat_mono (by linarith [ht, hk1] : (1 : ℝ) ≤ t + (k : ℝ) + 1)
      linarith [hk2R]
    exact h1.trans (C0hat_two_t_add_one_le ht)
  -- the left window count (case split on k ≤ t−3)
  have hleft : ((zeroWindow_finite (t - (k : ℝ) - 1)).toFinset.sum fun ρ => zeroMult ρ : ℝ)
      ≤ C0hat (t + 1) := by
    by_cases hcase : (3 : ℝ) ≤ t - (k : ℝ)
    · have h1 : (2 : ℝ) ≤ t - (k : ℝ) - 1 := by linarith [hcase]
      have h2 := hZC (t - (k : ℝ) - 1) h1
      have h3 : C0hat (t - (k : ℝ)) ≤ C0hat (t + 1) := by
        apply C0hat_mono (by linarith [hcase] : (1 : ℝ) ≤ t - (k : ℝ))
        linarith [ht]
      have h4 : t - (k : ℝ) - 1 + 1 = t - (k : ℝ) := by ring
      rw [h4] at h2
      exact h2.trans h3
    · push Not at hcase
      have hempty : (zeroWindow_finite (t - (k : ℝ) - 1)).toFinset = ∅ := by
        rw [Finset.eq_empty_iff_forall_notMem]
        intro ρ hρ
        rw [Set.Finite.mem_toFinset] at hρ
        obtain ⟨hz, h1, h2⟩ := hρ
        have h3 : 14 ≤ |ρ.im| := hNLZ ρ hz
        have h4 : ρ.im ≤ t - (k : ℝ) := by linarith [h2]
        have h5 : t - (k : ℝ) < 3 := hcase
        have h6 : t - (k : ℝ) - 1 ≥ -1 := by linarith [hk2R]
        have h7 : ρ.im ≥ -1 := by linarith [h1, h6]
        have h8 : |ρ.im| < 14 := by
          rw [abs_lt]
          constructor <;> linarith [h4, h5, h7]
        linarith [h3, h8]
      rw [hempty, Finset.sum_empty]
      have h9 : (0 : ℝ) ≤ C0hat (t + 1) := by
        have h10 := two_le_C0hat (by linarith [ht] : (1 : ℝ) ≤ t + 1)
        linarith
      exact h9
  -- the image bridge: bucket ⊆ right ∪ left windows
  have hbridge : ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ)
      = ∑ ρ ∈ FS.image (fun ρ => ρ.1), (zeroMult ρ : ℝ) := by
    rw [Finset.sum_image (fun x _ y _ h => Subtype.ext h)]
  have hsub : FS.image (fun ρ => ρ.1)
      ⊆ (zeroWindow_finite (t + (k : ℝ))).toFinset ∪
        (zeroWindow_finite (t - (k : ℝ) - 1)).toFinset := by
    intro z hz
    obtain ⟨ρ, hρ, rfl⟩ := Finset.mem_image.mp hz
    rw [hFS, Finset.mem_filter] at hρ
    obtain ⟨hmem, heq⟩ := hρ
    have hx := (mem_mainFinset (by linarith : (0 : ℝ) ≤ t)).mp hmem
    have hk : Nat.floor |ρ.1.im - t| = k := heq
    have hxk : (k : ℝ) ≤ |ρ.1.im - t| := by
      have h1 := Nat.floor_le (abs_nonneg (ρ.1.im - t))
      rw [hk] at h1
      exact h1
    have hxk1 : |ρ.1.im - t| < (k : ℝ) + 1 := by
      have h1 := Nat.lt_floor_add_one |ρ.1.im - t|
      rwa [hk] at h1
    rcases le_or_gt t ρ.1.im with him | him
    · -- γ ≥ t: x = γ − t ∈ [k, k+1): γ ∈ [t+k, t+k+1) ⊆ zeroWindow (t+k)
      apply Finset.mem_union_left
      rw [Set.Finite.mem_toFinset]
      refine ⟨ρ.2, ?_, ?_⟩
      · have h1 : |ρ.1.im - t| = ρ.1.im - t :=
          abs_of_nonneg (show (0 : ℝ) ≤ ρ.1.im - t from by linarith [him])
        rw [h1] at hxk
        linarith [hxk]
      · have h1 : |ρ.1.im - t| = ρ.1.im - t :=
          abs_of_nonneg (show (0 : ℝ) ≤ ρ.1.im - t from by linarith [him])
        rw [h1] at hxk1
        linarith [hxk1]
    · -- γ < t: x = t − γ ∈ [k, k+1): γ ∈ (t−k−1, t−k] ⊆ zeroWindow (t−k−1)
      apply Finset.mem_union_right
      rw [Set.Finite.mem_toFinset]
      refine ⟨ρ.2, ?_, ?_⟩
      · have h1 : |ρ.1.im - t| = t - ρ.1.im := by
          rw [abs_of_neg (show ρ.1.im - t < 0 from by linarith [him])]
          ring
        rw [h1] at hxk1
        linarith [hxk1]
      · have h1 : |ρ.1.im - t| = t - ρ.1.im := by
          rw [abs_of_neg (show ρ.1.im - t < 0 from by linarith [him])]
          ring
        rw [h1] at hxk
        linarith [hxk]
  have hdisj : Disjoint (zeroWindow_finite (t + (k : ℝ))).toFinset
      (zeroWindow_finite (t - (k : ℝ) - 1)).toFinset := by
    rw [Finset.disjoint_left]
    intro z hz1 hz2
    rw [Set.Finite.mem_toFinset] at hz1 hz2
    have h1 : t + (k : ℝ) ≤ z.im := hz1.2.1
    have h2 : z.im ≤ t - (k : ℝ) := by linarith [hz2.2.2]
    have h3 : (1 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk1
    linarith [h1, h2, h3]
  have hW1 := (zeroWindow_finite (t + (k : ℝ)))
  have hW2 := (zeroWindow_finite (t - (k : ℝ) - 1))
  calc ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ)
      = ∑ z ∈ FS.image (fun ρ => ρ.1), (zeroMult z : ℝ) := hbridge
    _ ≤ ∑ z ∈ hW1.toFinset ∪ hW2.toFinset, (zeroMult z : ℝ) :=
        Finset.sum_le_sum_of_subset_of_nonneg hsub (fun z _ _ => Nat.cast_nonneg _)
    _ = ∑ z ∈ hW1.toFinset, (zeroMult z : ℝ) + ∑ z ∈ hW2.toFinset, (zeroMult z : ℝ) :=
        Finset.sum_union hdisj
    _ ≤ (C0hat (t + 1) + 0.55) + C0hat (t + 1) := by
        have h1 : (((hW1).toFinset.sum fun ρ => zeroMult ρ) : ℝ)
            ≤ C0hat (t + 1) + 0.55 := hright.trans hright2
        have h2 : (((hW2).toFinset.sum fun ρ => zeroMult ρ) : ℝ)
            ≤ C0hat (t + 1) := hleft
        linarith [h1, h2]
    _ = 2 * C0hat (t + 1) + 0.55 := by ring

/-- **The main bound**: the main-finset farG sum is at most
4.5·(2·C0hat(t+1) + 0.55)·1.3476. -/
lemma farG_main_le (hZC : ZeroCountProp) (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t) :
    ∑ ρ ∈ mainFinset t, farG t ρ
      ≤ 4.5 * (2 * C0hat (t + 1) + 0.55) * 1.3476 := by
  classical
  have hU := unit_family_le (mainFinset t) (fun ρ => Nat.floor |ρ.1.im - t|)
    (Nat.floor t) (farG t) (fun k => 4.5 * farW k)
    (fun _ => 2 * C0hat (t + 1) + 0.55)
    (fun k => mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) (farW_nonneg k))
    (fun ρ hρ => mainFinset_bucket_range (by linarith : (0 : ℝ) ≤ t) hρ)
    (fun ρ hρ => farG_le_farW_mul ρ
      ((mem_mainFinset (by linarith : (0 : ℝ) ≤ t)).mp hρ).1)
    (fun k => by
      by_cases hk : 1 ≤ k ∧ k ≤ Nat.floor t
      · exact main_bucket_count hZC hNLZ ht hk.1 hk.2
      · have hempty : (mainFinset t).filter (fun ρ => Nat.floor |ρ.1.im - t| = k) = ∅ := by
          rw [Finset.eq_empty_iff_forall_notMem]
          intro ρ hρ
          rw [Finset.mem_filter] at hρ
          obtain ⟨hmem, heq⟩ := hρ
          have hj := mainFinset_bucket_range (by linarith : (0 : ℝ) ≤ t) hmem
          rw [heq] at hj
          exact hk hj
        rw [hempty, Finset.sum_empty]
        have h2 := two_le_C0hat (by linarith [ht] : (1 : ℝ) ≤ t + 1)
        linarith [h2])
  have hsum : ∑ k ∈ Finset.Ico 1 (Nat.floor t + 1), (4.5 * farW k) * (2 * C0hat (t + 1) + 0.55)
      = (4.5 * (2 * C0hat (t + 1) + 0.55)) * ∑ k ∈ Finset.Ico 1 (Nat.floor t + 1), farW k := by
    rw [Finset.mul_sum]
    apply Finset.sum_congr rfl
    intro k _
    ring
  calc ∑ ρ ∈ mainFinset t, farG t ρ
      ≤ ∑ k ∈ Finset.Ico 1 (Nat.floor t + 1), (4.5 * farW k) * (2 * C0hat (t + 1) + 0.55) := hU
    _ = (4.5 * (2 * C0hat (t + 1) + 0.55)) * ∑ k ∈ Finset.Ico 1 (Nat.floor t + 1), farW k := hsum
    _ ≤ (4.5 * (2 * C0hat (t + 1) + 0.55)) * 1.3476 :=
        mul_le_mul_of_nonneg_left (farW_Ico_le _)
          (by
            have h2 := two_le_C0hat (by linarith [ht] : (1 : ℝ) ≤ t + 1)
            positivity)

/-! ## 17. Piece 3i: the left-tail machine (γ < 0) -/

/-- The left-tail summand (farG restricted to γ < 0). -/
def farGLeft (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : ℝ :=
  if ρ.1.im < 0 then farG t ρ else 0

lemma farGLeft_nonneg (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : (0 : ℝ) ≤ farGLeft t ρ := by
  by_cases h : ρ.1.im < 0
  · rw [farGLeft, if_pos h]
    exact farG_nonneg t ρ
  · rw [farGLeft, if_neg h]

lemma farGLeft_summable {t : ℝ} (ht : 0 ≤ t) : Summable (farGLeft t) := by
  apply Summable.of_norm_bounded (farG_summable ht)
  intro ρ
  rw [Real.norm_eq_abs, abs_of_nonneg (farGLeft_nonneg t ρ)]
  by_cases h : ρ.1.im < 0
  · rw [farGLeft, if_pos h]
  · rw [farGLeft, if_neg h]
    exact farG_nonneg t ρ

/-- The per-element left-tail bound: farGLeft ≤ 4.5·m/(10+⌊|γ|⌋₊)². -/
lemma farGLeft_term_le {t : ℝ} (ht : 10 ≤ t) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) :
    farGLeft t ρ ≤ 4.5 * (zeroMult ρ.1 : ℝ) / (10 + (Nat.floor |ρ.1.im| : ℝ)) ^ 2 := by
  classical
  by_cases hγ : ρ.1.im < 0
  · rw [farGLeft, if_pos hγ]
    have hxfar : ρ ∉ nearZerosSub t := by
      intro hmem
      have h1 := mem_nearZerosSub.mp hmem
      have h2 : |ρ.1.im - t| = t - ρ.1.im := by
        rw [abs_of_neg (by linarith [hγ] : ρ.1.im - t < 0)]
        ring
      rw [h2] at h1
      linarith [h1, ht, hγ]
    rw [farG, if_neg hxfar]
    have hx : |ρ.1.im - t| = t + |ρ.1.im| := by
      rw [abs_of_neg (by linarith [hγ] : ρ.1.im - t < 0)]
      have h3 : |ρ.1.im| = -ρ.1.im := abs_of_neg hγ
      rw [h3]
      ring
    have hℓ : (Nat.floor |ρ.1.im| : ℝ) ≤ |ρ.1.im| := Nat.floor_le (abs_nonneg _)
    have hx1 : (10 : ℝ) + (Nat.floor |ρ.1.im| : ℝ) ≤ |ρ.1.im - t| := by
      rw [hx]
      linarith [hℓ, ht]
    have hmono : ((10 : ℝ) + (Nat.floor |ρ.1.im| : ℝ)) ^ 2
        ≤ |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
      have h2 : ((10 : ℝ) + (Nat.floor |ρ.1.im| : ℝ)) ^ 2 ≤ |ρ.1.im - t| ^ 2 := by
        have h3 : (0 : ℝ) ≤ (10 : ℝ) + (Nat.floor |ρ.1.im| : ℝ) := by positivity
        exact pow_le_pow_left₀ h3 hx1 2
      have h4 : |ρ.1.im - t| ^ 2 ≤ |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
        have h5 : |ρ.1.im - t| ≤ Real.sqrt (1 + (ρ.1.im - t) ^ 2) := by
          calc |ρ.1.im - t| = Real.sqrt ((ρ.1.im - t) ^ 2) := by rw [Real.sqrt_sq_eq_abs]
            _ ≤ Real.sqrt (1 + (ρ.1.im - t) ^ 2) :=
              Real.sqrt_le_sqrt (by linarith [sq_nonneg (ρ.1.im - t)])
        have h6 : (0 : ℝ) ≤ |ρ.1.im - t| := abs_nonneg _
        calc |ρ.1.im - t| ^ 2 = |ρ.1.im - t| * |ρ.1.im - t| := by ring
          _ ≤ |ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2) :=
            mul_le_mul_of_nonneg_left h5 h6
      exact h2.trans h4
    have hm : (0 : ℝ) ≤ (zeroMult ρ.1 : ℝ) := Nat.cast_nonneg _
    have hpos : (0 : ℝ) < ((10 : ℝ) + (Nat.floor |ρ.1.im| : ℝ)) ^ 2 := by positivity
    rw [show (4.5 : ℝ) * (zeroMult ρ.1 : ℝ) / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))
        = (4.5 * (zeroMult ρ.1 : ℝ)) * (1 / (|ρ.1.im - t| * Real.sqrt (1 + (ρ.1.im - t) ^ 2))) from by
          rw [div_eq_mul_inv, one_div]]
    rw [show (4.5 : ℝ) * (zeroMult ρ.1 : ℝ) / (10 + (Nat.floor |ρ.1.im| : ℝ)) ^ 2
        = (4.5 * (zeroMult ρ.1 : ℝ)) * (1 / ((10 + (Nat.floor |ρ.1.im| : ℝ)) ^ 2)) from by
          rw [div_eq_mul_inv, one_div]]
    apply mul_le_mul_of_nonneg_left _ (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) hm)
    exact one_div_le_one_div_of_le hpos hmono
  · rw [farGLeft, if_neg hγ]
    positivity

/-- The left-tail fiber {⌊|γ|⌋₊ = ℓ} is finite. -/
lemma farGLeft_fiber_finite (ℓ : ℕ) :
    ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ} :
      Set {ρ : ℂ // IsNontrivialZero ρ}).Finite := by
  apply (finite_subtype_norm_lt ((ℓ : ℝ) + 2)).subset
  intro ρ hρ
  show ‖ρ.1‖ < (ℓ : ℝ) + 2
  have hmem : Nat.floor |ρ.1.im| = ℓ := hρ
  have h1 : |ρ.1.im| < (ℓ : ℝ) + 1 := by
    have h2 := Nat.lt_floor_add_one |ρ.1.im|
    rwa [hmem] at h2
  have hle : ‖ρ.1‖ ≤ |ρ.1.re| + |ρ.1.im| := Complex.norm_le_abs_re_add_abs_im _
  have hre : |ρ.1.re| ≤ 1 := by
    have h3 : 0 < ρ.1.re := ρ.2.2.1
    have h4 : ρ.1.re < 1 := ρ.2.2.2
    rw [abs_le]
    constructor <;> linarith
  linarith [hle, hre, h1]

/-- The per-fiber left-tail count (via conj + zeroWindow + zeroMult_conj). -/
lemma farGLeft_fiber_count (hZC : ZeroCountProp) (_hNLZ : NoLowZeros) {t : ℝ} (_ht : 10 ≤ t)
    (ℓ : ℕ) (hℓ : 14 ≤ ℓ) :
    ∑ ρ ∈ (farGLeft_fiber_finite ℓ).toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
      ρ.1.im < 0), (zeroMult ρ.1 : ℝ)
      ≤ C0hat ((ℓ : ℝ) + 1) := by
  classical
  set FS := (farGLeft_fiber_finite ℓ).toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
    ρ.1.im < 0) with hFS
  have hsub : FS.image (fun ρ => (starRingEnd ℂ) ρ.1)
      ⊆ (zeroWindow_finite (ℓ : ℝ)).toFinset := by
    intro z hz
    obtain ⟨ρ, hρ, rfl⟩ := Finset.mem_image.mp hz
    rw [hFS, Finset.mem_filter] at hρ
    obtain ⟨hmem, hγ⟩ := hρ
    rw [Set.Finite.mem_toFinset] at hmem
    have hmem2 : Nat.floor |ρ.1.im| = ℓ := hmem
    have hγ2 : ((starRingEnd ℂ) ρ.1).im = -ρ.1.im := by simp
    have hneg : |ρ.1.im| = -ρ.1.im := abs_of_neg hγ
    have hℓ1 : (ℓ : ℝ) ≤ |ρ.1.im| := by
      have h1 := Nat.floor_le (abs_nonneg ρ.1.im)
      rwa [hmem2] at h1
    have hℓ2 : |ρ.1.im| < (ℓ : ℝ) + 1 := by
      have h1 := Nat.lt_floor_add_one |ρ.1.im|
      rwa [hmem2] at h1
    rw [Set.Finite.mem_toFinset]
    refine ⟨isNontrivialZero_conj ρ.2, ?_, ?_⟩
    · rw [hγ2]
      linarith [hℓ1, hneg]
    · rw [hγ2]
      linarith [hℓ2, hneg]
  have h1 : ∑ z ∈ FS.image (fun ρ => (starRingEnd ℂ) ρ.1), (zeroMult z : ℝ)
      = ∑ ρ ∈ FS, (zeroMult ((starRingEnd ℂ) ρ.1) : ℝ) :=
    Finset.sum_image (fun x _ y _ h => Subtype.ext ((starRingEnd ℂ).injective h))
  have h2 : ∑ ρ ∈ FS, (zeroMult ((starRingEnd ℂ) ρ.1) : ℝ)
      = ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ) :=
    Finset.sum_congr rfl (fun ρ _ => by rw [zeroMult_conj ρ.2])
  calc ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ)
      = ∑ z ∈ FS.image (fun ρ => (starRingEnd ℂ) ρ.1), (zeroMult z : ℝ) := by
        rw [h1, h2]
    _ ≤ ∑ z ∈ (zeroWindow_finite (ℓ : ℝ)).toFinset, (zeroMult z : ℝ) :=
        Finset.sum_le_sum_of_subset_of_nonneg hsub (fun z _ _ => Nat.cast_nonneg _)
    _ = (((zeroWindow_finite (ℓ : ℝ)).toFinset.sum fun ρ => zeroMult ρ) : ℝ) := rfl
    _ ≤ C0hat ((ℓ : ℝ) + 1) := hZC (ℓ : ℝ) (by exact_mod_cast (by omega : 2 ≤ ℓ))

/-- The per-fiber left-tail bound (ENNReal form). -/
lemma farGLeft_fiber_le (hZC : ZeroCountProp) (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t) (ℓ : ℕ) :
    ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ},
      ENNReal.ofReal (farGLeft t b.1)
      ≤ ENNReal.ofReal (if 14 ≤ ℓ then 4.5 * C0hat ((ℓ : ℝ) + 1) / (10 + (ℓ : ℝ)) ^ 2 else 0) := by
  classical
  have hfin : ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ} :
      Set {ρ : ℂ // IsNontrivialZero ρ}).Finite := farGLeft_fiber_finite ℓ
  have htsum : ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ},
      ENNReal.ofReal (farGLeft t b.1)
      = ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
        ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ}).indicator
          (fun ρ => ENNReal.ofReal (farGLeft t ρ)) ρ :=
    tsum_subtype _ (fun ρ => ENNReal.ofReal (farGLeft t ρ))
  rw [htsum]
  rw [tsum_eq_sum (s := hfin.toFinset) (fun ρ hρ => by
    apply Set.indicator_of_notMem
    rwa [Set.Finite.mem_toFinset] at hρ)]
  rw [Finset.sum_congr rfl (fun ρ hρ =>
    Set.indicator_of_mem (by rwa [Set.Finite.mem_toFinset] at hρ) _)]
  rw [← ENNReal.ofReal_sum_of_nonneg (fun ρ _ => farGLeft_nonneg t ρ)]
  apply ENNReal.ofReal_le_ofReal
  by_cases hℓ : 14 ≤ ℓ
  · rw [if_pos hℓ]
    rw [← Finset.sum_filter_add_sum_filter_not hfin.toFinset
      (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => ρ.1.im < 0) (farGLeft t)]
    have hzero : ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
        ¬ ρ.1.im < 0), farGLeft t ρ = 0 := by
      apply Finset.sum_eq_zero
      intro ρ hρ
      rw [Finset.mem_filter] at hρ
      rw [farGLeft, if_neg hρ.2]
    rw [hzero, add_zero]
    calc ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => ρ.1.im < 0),
        farGLeft t ρ
        ≤ ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => ρ.1.im < 0),
          (4.5 / (10 + (ℓ : ℝ)) ^ 2) * (zeroMult ρ.1 : ℝ) := by
          apply Finset.sum_le_sum
          intro ρ hρ
          have h1 := farGLeft_term_le ht ρ
          rw [Finset.mem_filter] at hρ
          obtain ⟨hmem, _⟩ := hρ
          rw [Set.Finite.mem_toFinset] at hmem
          have hmem2 : Nat.floor |ρ.1.im| = ℓ := hmem
          rw [hmem2] at h1
          rw [show (4.5 : ℝ) / (10 + (ℓ : ℝ)) ^ 2 * (zeroMult ρ.1 : ℝ)
              = 4.5 * (zeroMult ρ.1 : ℝ) / (10 + (ℓ : ℝ)) ^ 2 from by ring]
          exact h1
      _ = (4.5 / (10 + (ℓ : ℝ)) ^ 2) *
          ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => ρ.1.im < 0),
            (zeroMult ρ.1 : ℝ) := by
          rw [Finset.mul_sum]
      _ ≤ (4.5 / (10 + (ℓ : ℝ)) ^ 2) * C0hat ((ℓ : ℝ) + 1) :=
          mul_le_mul_of_nonneg_left (farGLeft_fiber_count hZC hNLZ ht ℓ hℓ)
            (by positivity)
      _ = 4.5 * C0hat ((ℓ : ℝ) + 1) / (10 + (ℓ : ℝ)) ^ 2 := by ring
  · rw [if_neg hℓ]
    apply le_of_eq
    apply Finset.sum_eq_zero
    intro ρ hρ
    rw [Set.Finite.mem_toFinset] at hρ
    by_cases hγ : ρ.1.im < 0
    · have h1 : 14 ≤ |ρ.1.im| := hNLZ ρ.1 ρ.2
      have h2 : 14 ≤ Nat.floor |ρ.1.im| := by
        have h3 := Nat.floor_mono h1
        rw [Nat.floor_ofNat] at h3
        exact h3
      have h2eq : Nat.floor |ρ.1.im| = ℓ := hρ
      rw [h2eq] at h2
      exact absurd h2 hℓ
    · rw [farGLeft, if_neg hγ]

/-- The shifted LA₀-family is summable. -/
lemma tail14_inv_add_sq_summable :
    Summable (fun j : ℕ => (1 : ℝ) / (((j : ℝ) + 14) + 10) ^ 2) := by
  have h := antitoneOn_one_div_add_sq.summable_of_integrableOn_Ioi
    integrableOn_one_div_add_sq (fun t ht => by
      have ht13 : (13 : ℝ) < t := ht
      positivity)
  have h2 := (summable_nat_add_iff (f := fun n : ℕ => (1 : ℝ) / ((n : ℝ) + 10) ^ 2) 14).mpr h
  refine h2.congr fun n => ?_
  push_cast
  ring_nf

/-- The shifted LA₂-family is summable. -/
lemma tail14_loglog_div_add_sq_summable :
    Summable (fun j : ℕ =>
      Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2) := by
  apply Summable.of_norm_bounded (tail14_log_div_add_sq_summable.mul_left (1 / Real.exp 1))
  intro j
  rw [Real.norm_eq_abs]
  rw [abs_of_nonneg (by
    have hj1 : (1 : ℝ) ≤ Real.log ((j : ℝ) + 14) := by
      have h1 := log_fourteen_ge
      have h2 : Real.log 14 ≤ Real.log ((j : ℝ) + 14) :=
        Real.log_le_log (by norm_num : (0 : ℝ) < 14) (by
          have h : (0 : ℝ) ≤ (j : ℝ) := Nat.cast_nonneg j
          linarith)
      linarith [h1, h2]
    have h3 : (0 : ℝ) ≤ Real.log (Real.log ((j : ℝ) + 14)) := Real.log_nonneg hj1
    positivity : (0 : ℝ) ≤ Real.log (Real.log ((j : ℝ) + 14)) / (((j : ℝ) + 14) + 10) ^ 2)]
  have hj1 : (1 : ℝ) < (j : ℝ) + 14 := by
    have h : (0 : ℝ) ≤ (j : ℝ) := Nat.cast_nonneg j
    linarith
  have h4 := log_log_le_div_exp hj1
  have h5 : (0 : ℝ) ≤ (((j : ℝ) + 14) + 10) ^ 2 := by positivity
  have h6 := div_le_div_of_nonneg_right h4 h5
  rw [mul_div_assoc] at h6
  exact h6

/-- The left-tail majorant series (if-form on ℕ). -/
def farGLeftMaj : ℕ → ℝ := fun ℓ =>
  if 14 ≤ ℓ then 4.5 * C0hat ((ℓ : ℝ) + 1) / (10 + (ℓ : ℝ)) ^ 2 else 0

lemma farGLeftMaj_nonneg (ℓ : ℕ) : (0 : ℝ) ≤ farGLeftMaj ℓ := by
  by_cases h : 14 ≤ ℓ
  · rw [farGLeftMaj, if_pos h]
    have h1 : (1 : ℝ) ≤ (ℓ : ℝ) + 1 := by
      have h2 : (0 : ℝ) ≤ (ℓ : ℝ) := Nat.cast_nonneg ℓ
      linarith
    have h3 := two_le_C0hat h1
    positivity
  · rw [farGLeftMaj, if_neg h]

set_option maxHeartbeats 800000 in
/-- The pointwise majorant bound for the shifted farGLeftMaj. -/
lemma farGLeftMaj_shift_le (ℓ : ℕ) :
    farGLeftMaj (ℓ + 14)
      ≤ 4.5 * ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((ℓ : ℝ) + 14) + 10) ^ 2)
          + (0.433 * (Real.log ((ℓ : ℝ) + 14) / (((ℓ : ℝ) + 14) + 10) ^ 2))
          + (0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) / (((ℓ : ℝ) + 14) + 10) ^ 2))) := by
  have hℓ : 14 ≤ ℓ + 14 := by omega
  rw [farGLeftMaj, if_pos hℓ]
  have h1 := C0hat_add_one_le_of_fourteen_le (j := ℓ + 14) (by omega : 14 ≤ ℓ + 14)
  have hcast : ((ℓ + 14 : ℕ) : ℝ) = (ℓ : ℝ) + 14 := by push_cast; ring
  have h9 : (0.433 * (Real.log ((ℓ : ℝ) + 14) + 0.1942)
      + 0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) + 0.0736) + 2.9)
      = (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9)
        + 0.433 * Real.log ((ℓ : ℝ) + 14) + 0.886 * Real.log (Real.log ((ℓ : ℝ) + 14)) := by ring
  have h2 : 4.5 * C0hat (↑(ℓ + 14) + 1) / (10 + ((ℓ + 14 : ℕ) : ℝ)) ^ 2
      ≤ 4.5 * (0.433 * (Real.log ((ℓ : ℝ) + 14) + 0.1942)
          + 0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) + 0.0736) + 2.9)
        / (10 + ((ℓ + 14 : ℕ) : ℝ)) ^ 2 := by
    rw [hcast] at h1
    apply div_le_div_of_nonneg_right _ (by positivity : (0 : ℝ) ≤ (10 + ((ℓ + 14 : ℕ) : ℝ)) ^ 2)
    apply mul_le_mul_of_nonneg_left _ (by norm_num : (0 : ℝ) ≤ 4.5)
    rw [hcast]
    exact h1
  exact h2.trans (le_of_eq (by
    have hD : ((10 : ℝ) + ((ℓ + 14 : ℕ) : ℝ)) = ((ℓ : ℝ) + 14) + 10 := by rw [hcast]; ring
    rw [hD, h9, mul_add, mul_add, add_div, add_div, mul_add]
    ring))

/-- The combo majorant is summable. -/
lemma farGLeftMaj_combo_summable : Summable (fun ℓ : ℕ =>
    4.5 * ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((ℓ : ℝ) + 14) + 10) ^ 2)
        + (0.433 * (Real.log ((ℓ : ℝ) + 14) / (((ℓ : ℝ) + 14) + 10) ^ 2))
        + (0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) / (((ℓ : ℝ) + 14) + 10) ^ 2)))) := by
  have hAB : Summable (fun ℓ : ℕ =>
      (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((ℓ : ℝ) + 14) + 10) ^ 2)
      + 0.433 * (Real.log ((ℓ : ℝ) + 14) / (((ℓ : ℝ) + 14) + 10) ^ 2)) :=
    (tail14_inv_add_sq_summable.mul_left _).add (tail14_log_div_add_sq_summable.mul_left _)
  have hABC : Summable (fun ℓ : ℕ =>
      (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((ℓ : ℝ) + 14) + 10) ^ 2)
      + 0.433 * (Real.log ((ℓ : ℝ) + 14) / (((ℓ : ℝ) + 14) + 10) ^ 2)
      + 0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) / (((ℓ : ℝ) + 14) + 10) ^ 2)) :=
    hAB.add (tail14_loglog_div_add_sq_summable.mul_left _)
  exact hABC.mul_left _

lemma farGLeftMaj_shift_summable : Summable (fun ℓ : ℕ => farGLeftMaj (ℓ + 14)) := by
  apply Summable.of_norm_bounded farGLeftMaj_combo_summable
  intro ℓ
  rw [Real.norm_eq_abs, abs_of_nonneg (farGLeftMaj_nonneg _)]
  exact farGLeftMaj_shift_le ℓ

lemma farGLeftMaj_summable : Summable farGLeftMaj :=
  (summable_nat_add_iff (f := farGLeftMaj) 14).mp farGLeftMaj_shift_summable

set_option maxHeartbeats 800000 in
/-- **The left-tail series bound**. -/
lemma farGLeftMaj_series_le :
    (∑' ℓ : ℕ, farGLeftMaj ℓ)
      ≤ 4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
          + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23)) := by
  have hsplit : (∑' ℓ : ℕ, farGLeftMaj ℓ)
      = (∑ ℓ ∈ Finset.range 14, farGLeftMaj ℓ) + ∑' ℓ : ℕ, farGLeftMaj (ℓ + 14) :=
    (farGLeftMaj_summable.sum_add_tsum_nat_add 14).symm
  have hzero : ∑ ℓ ∈ Finset.range 14, farGLeftMaj ℓ = 0 := by
    apply Finset.sum_eq_zero
    intro ℓ hℓ
    rw [Finset.mem_range] at hℓ
    rw [farGLeftMaj, if_neg (by omega : ¬14 ≤ ℓ)]
  have hle := Summable.tsum_le_tsum farGLeftMaj_shift_le farGLeftMaj_shift_summable
    farGLeftMaj_combo_summable
  have hA0 := tail14_inv_add_sq_le
  have hA1 := tail14_log_div_add_sq_le
  have hA2 := tail14_loglog_div_add_sq_le
  have heval : (∑' ℓ : ℕ,
      4.5 * ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((ℓ : ℝ) + 14) + 10) ^ 2)
          + (0.433 * (Real.log ((ℓ : ℝ) + 14) / (((ℓ : ℝ) + 14) + 10) ^ 2))
          + (0.886 * (Real.log (Real.log ((ℓ : ℝ) + 14)) / (((ℓ : ℝ) + 14) + 10) ^ 2))))
      ≤ 4.5 * ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / 23)
          + 0.433 * 0.1687 + 0.886 * 0.0621) := by
    rw [tsum_mul_left]
    apply mul_le_mul_of_nonneg_left _ (by norm_num : (0 : ℝ) ≤ 4.5)
    have hdecomp : (∑' x : ℕ,
        ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)
            + (0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2))
            + (0.886 * (Real.log (Real.log ((x : ℝ) + 14)) / (((x : ℝ) + 14) + 10) ^ 2))))
        = (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (∑' x : ℕ, (1 / (((x : ℝ) + 14) + 10) ^ 2))
            + 0.433 * (∑' x : ℕ, (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2))
            + 0.886 * (∑' x : ℕ, (Real.log (Real.log ((x : ℝ) + 14)) / (((x : ℝ) + 14) + 10) ^ 2)) := by
      have e1 := Summable.tsum_add
        (f := fun x : ℕ => (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)
            + 0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2))
        (g := fun x : ℕ => 0.886 * (Real.log (Real.log ((x : ℝ) + 14)) / (((x : ℝ) + 14) + 10) ^ 2))
        ((tail14_inv_add_sq_summable.mul_left _).add (tail14_log_div_add_sq_summable.mul_left _))
        (tail14_loglog_div_add_sq_summable.mul_left _)
      have e2 := Summable.tsum_add
        (f := fun x : ℕ => (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2))
        (g := fun x : ℕ => 0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2))
        (tail14_inv_add_sq_summable.mul_left _) (tail14_log_div_add_sq_summable.mul_left _)
      rw [show (∑' x : ℕ,
          ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)
              + (0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2))
              + (0.886 * (Real.log (Real.log ((x : ℝ) + 14)) / (((x : ℝ) + 14) + 10) ^ 2))))
          = ∑' x : ℕ, ((fun x : ℕ =>
              (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)
              + 0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2)) x
            + (fun x : ℕ => 0.886 * (Real.log (Real.log ((x : ℝ) + 14)) / (((x : ℝ) + 14) + 10) ^ 2)) x)
        from rfl]
      rw [e1]
      rw [show (∑' x : ℕ, (fun x : ℕ =>
          (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)
          + 0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2)) x)
          = ∑' x : ℕ, ((fun x : ℕ =>
              (0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / (((x : ℝ) + 14) + 10) ^ 2)) x
            + (fun x : ℕ => 0.433 * (Real.log ((x : ℝ) + 14) / (((x : ℝ) + 14) + 10) ^ 2)) x)
        from rfl]
      rw [e2, tsum_mul_left, tsum_mul_left, tsum_mul_left]
    rw [hdecomp]
    have hle1 := mul_le_mul_of_nonneg_left hA0 (by positivity : (0 : ℝ) ≤
      0.433 * 0.1942 + 0.886 * 0.0736 + 2.9)
    have hle2 := mul_le_mul_of_nonneg_left hA1 (by positivity : (0 : ℝ) ≤ 0.433)
    have hle3 := mul_le_mul_of_nonneg_left hA2 (by positivity : (0 : ℝ) ≤ 0.886)
    linarith [hle1, hle2, hle3]
  calc (∑' ℓ : ℕ, farGLeftMaj ℓ)
      = (∑ ℓ ∈ Finset.range 14, farGLeftMaj ℓ) + ∑' ℓ : ℕ, farGLeftMaj (ℓ + 14) := hsplit
    _ = 0 + ∑' ℓ : ℕ, farGLeftMaj (ℓ + 14) := by rw [hzero]
    _ ≤ 4.5 * ((0.433 * 0.1942 + 0.886 * 0.0736 + 2.9) * (1 / 23)
        + 0.433 * 0.1687 + 0.886 * 0.0621) := by linarith [hle, heval]
    _ = 4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
        + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23)) := by ring

/-- **The left-tail bound** (the ENNReal fiberwise assembly). -/
lemma farGLeft_le (hZC : ZeroCountProp) (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t) :
    ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGLeft t ρ
      ≤ 4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
          + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23)) := by
  have hC0 : (0 : ℝ) ≤ 4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
      + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23)) := by
    positivity
  rw [← ENNReal.ofReal_le_ofReal_iff hC0]
  rw [ENNReal.ofReal_tsum_of_nonneg (fun ρ => farGLeft_nonneg t ρ)
    (farGLeft_summable (by linarith : (0 : ℝ) ≤ t))]
  rw [← ENNReal.tsum_fiberwise (fun ρ => ENNReal.ofReal (farGLeft t ρ))
    (fun ρ => Nat.floor |ρ.1.im|)]
  have hper : ∀ ℓ : ℕ, ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {ℓ},
      ENNReal.ofReal (farGLeft t b.1)
      ≤ ENNReal.ofReal (farGLeftMaj ℓ) := by
    intro ℓ
    have h := farGLeft_fiber_le hZC hNLZ ht ℓ
    rwa [farGLeftMaj] at *
  calc ∑' j : ℕ, ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im|) ⁻¹' {j},
        ENNReal.ofReal (farGLeft t b.1)
      ≤ ∑' j : ℕ, ENNReal.ofReal (farGLeftMaj j) := ENNReal.tsum_le_tsum hper
    _ = ENNReal.ofReal (∑' j : ℕ, farGLeftMaj j) := by
        rw [ENNReal.ofReal_tsum_of_nonneg farGLeftMaj_nonneg farGLeftMaj_summable]
    _ ≤ ENNReal.ofReal (4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
          + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23))) :=
        ENNReal.ofReal_le_ofReal farGLeftMaj_series_le


/-! ## 18. Piece 3j: the right-tail machine (γ > 2t) -/

/-- The right-tail summand: farG restricted to tail zeros (x ≥ ⌊t⌋₊+1)
on the right side (γ ≥ t, hence γ > 2t). -/
def farGRight (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : ℝ :=
  if (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im) then farG t ρ else 0

lemma farGRight_nonneg (t : ℝ) (ρ : {ρ : ℂ // IsNontrivialZero ρ}) : (0 : ℝ) ≤ farGRight t ρ := by
  by_cases h : (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im)
  · rw [farGRight, if_pos h]
    exact farG_nonneg t ρ
  · rw [farGRight, if_neg h]

lemma farGRight_summable {t : ℝ} (ht : 0 ≤ t) : Summable (farGRight t) := by
  apply Summable.of_norm_bounded (farG_summable ht)
  intro ρ
  rw [Real.norm_eq_abs, abs_of_nonneg (farGRight_nonneg t ρ)]
  by_cases h : (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im)
  · rw [farGRight, if_pos h]
  · rw [farGRight, if_neg h]
    exact farG_nonneg t ρ

/-- The per-element right-tail bound: farGRight ≤ 4.5·farW(⌊x⌋₊)·m. -/
lemma farGRight_term_le {t : ℝ} (ρ : {ρ : ℂ // IsNontrivialZero ρ}) :
    farGRight t ρ ≤ 4.5 * farW (Nat.floor |ρ.1.im - t|) * (zeroMult ρ.1 : ℝ) := by
  classical
  by_cases h : (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im)
  · rw [farGRight, if_pos h]
    have hx1 : (1 : ℝ) ≤ |ρ.1.im - t| := by
      have h1 : (1 : ℝ) ≤ (Nat.floor t : ℝ) + 1 := by
        have h2 : (0 : ℝ) ≤ (Nat.floor t : ℝ) := Nat.cast_nonneg _
        linarith
      have h3 : (Nat.floor t : ℝ) + 1 ≤ |ρ.1.im - t| := by
        have h4 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ |ρ.1.im - t| := by exact_mod_cast h.1
        have h5 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
        rwa [h5] at h4
      linarith [h1, h3]
    exact farG_le_farW_mul ρ hx1
  · rw [farGRight, if_neg h]
    exact mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) (farW_nonneg _))
      (Nat.cast_nonneg _)

/-- The right-tail fiber {⌊x⌋₊ = k} is finite. -/
lemma farGRight_fiber_finite {t : ℝ} (ht : 0 ≤ t) (k : ℕ) :
    ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k} :
      Set {ρ : ℂ // IsNontrivialZero ρ}).Finite := by
  apply (finite_subtype_norm_lt (t + (k : ℝ) + 2)).subset
  intro ρ hρ
  show ‖ρ.1‖ < t + (k : ℝ) + 2
  have hmem : Nat.floor |ρ.1.im - t| = k := hρ
  have h1 : |ρ.1.im - t| < (k : ℝ) + 1 := by
    have h2 := Nat.lt_floor_add_one |ρ.1.im - t|
    rwa [hmem] at h2
  have hle : ‖ρ.1‖ ≤ |ρ.1.re| + |ρ.1.im| := Complex.norm_le_abs_re_add_abs_im _
  have hre : |ρ.1.re| ≤ 1 := by
    have h3 : 0 < ρ.1.re := ρ.2.2.1
    have h4 : ρ.1.re < 1 := ρ.2.2.2
    rw [abs_le]
    constructor <;> linarith
  have him : |ρ.1.im| ≤ t + |ρ.1.im - t| := by
    calc |ρ.1.im| = |ρ.1.im - t + t| := by rw [sub_add_cancel]
      _ ≤ |ρ.1.im - t| + |t| := abs_add_le _ _
      _ = |ρ.1.im - t| + t := by rw [abs_of_nonneg ht]
      _ = t + |ρ.1.im - t| := by ring
  linarith [hle, hre, him, h1]

/-- **The per-fiber right-tail count**: the mult-weighted count of
right-tail zeros with bucket k (⌊t⌋₊+1 ≤ k) is ≤ C0hat(2k+1). -/
lemma farGRight_fiber_count (hZC : ZeroCountProp) {t : ℝ} (ht : 10 ≤ t)
    {k : ℕ} (hk : Nat.floor t + 1 ≤ k) :
    ∑ ρ ∈ (farGRight_fiber_finite (by linarith : (0 : ℝ) ≤ t) k).toFinset.filter
      (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im), (zeroMult ρ.1 : ℝ)
      ≤ C0hat (2 * (k : ℝ) + 1) := by
  classical
  set FS := (farGRight_fiber_finite (by linarith : (0 : ℝ) ≤ t) k).toFinset.filter
    (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im) with hFS
  have htk : (2 : ℝ) ≤ t + (k : ℝ) := by
    have h1 : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
    linarith [ht, h1]
  have hcount : ((zeroWindow_finite (t + (k : ℝ))).toFinset.sum fun ρ => zeroMult ρ : ℝ)
      ≤ C0hat (t + (k : ℝ) + 1) := hZC (t + (k : ℝ)) htk
  have hmono : C0hat (t + (k : ℝ) + 1) ≤ C0hat (2 * (k : ℝ) + 1) := by
    apply C0hat_mono (by linarith [ht] : (1 : ℝ) ≤ t + (k : ℝ) + 1)
    have h1 : (Nat.floor t : ℝ) + 1 ≤ (k : ℝ) := by
      have h2 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk
      have h3 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
      rwa [h3] at h2
    have h4 : t ≤ (k : ℝ) := by
      have h5 : t ≤ (Nat.floor t : ℝ) + 1 := by
        have h6 := Nat.lt_floor_add_one t
        linarith [h6]
      linarith [h1, h5]
    linarith [h4]
  have hsub : FS.image (fun ρ => ρ.1) ⊆ (zeroWindow_finite (t + (k : ℝ))).toFinset := by
    intro z hz
    obtain ⟨ρ, hρ, rfl⟩ := Finset.mem_image.mp hz
    rw [hFS, Finset.mem_filter] at hρ
    obtain ⟨hmem, him⟩ := hρ
    rw [Set.Finite.mem_toFinset] at hmem
    have hmem2 : Nat.floor |ρ.1.im - t| = k := hmem
    have hxk : (k : ℝ) ≤ |ρ.1.im - t| := by
      have h1 := Nat.floor_le (abs_nonneg (ρ.1.im - t))
      rwa [hmem2] at h1
    have hxk1 : |ρ.1.im - t| < (k : ℝ) + 1 := by
      have h1 := Nat.lt_floor_add_one |ρ.1.im - t|
      rwa [hmem2] at h1
    have hx : |ρ.1.im - t| = ρ.1.im - t := abs_of_nonneg (by linarith [him])
    rw [Set.Finite.mem_toFinset]
    refine ⟨ρ.2, ?_, ?_⟩
    · rw [hx] at hxk
      linarith [hxk, him]
    · rw [hx] at hxk1
      linarith [hxk1, him]
  have hbridge : ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ)
      = ∑ ρ ∈ FS.image (fun ρ => ρ.1), (zeroMult ρ : ℝ) := by
    rw [Finset.sum_image (fun x _ y _ h => Subtype.ext h)]
  calc ∑ ρ ∈ FS, (zeroMult ρ.1 : ℝ)
      = ∑ z ∈ FS.image (fun ρ => ρ.1), (zeroMult z : ℝ) := hbridge
    _ ≤ ∑ z ∈ (zeroWindow_finite (t + (k : ℝ))).toFinset, (zeroMult z : ℝ) :=
        Finset.sum_le_sum_of_subset_of_nonneg hsub (fun z _ _ => Nat.cast_nonneg _)
    _ = (((zeroWindow_finite (t + (k : ℝ))).toFinset.sum fun ρ => zeroMult ρ) : ℝ) := rfl
    _ ≤ C0hat (t + (k : ℝ) + 1) := hcount
    _ ≤ C0hat (2 * (k : ℝ) + 1) := hmono

/-- The per-fiber right-tail bound (ENNReal form). -/
lemma farGRight_fiber_le (hZC : ZeroCountProp) {t : ℝ} (ht : 10 ≤ t) (k : ℕ) :
    ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k},
      ENNReal.ofReal (farGRight t b.1)
      ≤ ENNReal.ofReal (if Nat.floor t + 1 ≤ k then 4.5 * farW k * C0hat (2 * (k : ℝ) + 1) else 0) := by
  classical
  have hfin : ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k} :
      Set {ρ : ℂ // IsNontrivialZero ρ}).Finite := farGRight_fiber_finite (by linarith : (0 : ℝ) ≤ t) k
  have htsum : ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k},
      ENNReal.ofReal (farGRight t b.1)
      = ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
        ((fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k}).indicator
          (fun ρ => ENNReal.ofReal (farGRight t ρ)) ρ :=
    tsum_subtype _ (fun ρ => ENNReal.ofReal (farGRight t ρ))
  rw [htsum]
  rw [tsum_eq_sum (s := hfin.toFinset) (fun ρ hρ => by
    apply Set.indicator_of_notMem
    rwa [Set.Finite.mem_toFinset] at hρ)]
  rw [Finset.sum_congr rfl (fun ρ hρ =>
    Set.indicator_of_mem (by rwa [Set.Finite.mem_toFinset] at hρ) _)]
  rw [← ENNReal.ofReal_sum_of_nonneg (fun ρ _ => farGRight_nonneg t ρ)]
  apply ENNReal.ofReal_le_ofReal
  by_cases hk : Nat.floor t + 1 ≤ k
  · rw [if_pos hk]
    rw [← Finset.sum_filter_add_sum_filter_not hfin.toFinset
      (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im) (farGRight t)]
    have hzero : ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} =>
        ¬ t ≤ ρ.1.im), farGRight t ρ = 0 := by
      apply Finset.sum_eq_zero
      intro ρ hρ
      rw [Finset.mem_filter] at hρ
      rw [farGRight, if_neg (fun h => hρ.2 h.2)]
    rw [hzero, add_zero]
    calc ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im),
        farGRight t ρ
        ≤ ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im),
          (4.5 * farW k) * (zeroMult ρ.1 : ℝ) := by
          apply Finset.sum_le_sum
          intro ρ hρ
          have h1 := farGRight_term_le (t := t) ρ
          rw [Finset.mem_filter] at hρ
          obtain ⟨hmem, _⟩ := hρ
          rw [Set.Finite.mem_toFinset] at hmem
          have hmem2 : Nat.floor |ρ.1.im - t| = k := hmem
          rw [hmem2] at h1
          exact h1
      _ = (4.5 * farW k) *
          ∑ ρ ∈ hfin.toFinset.filter (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => t ≤ ρ.1.im),
            (zeroMult ρ.1 : ℝ) := by
          rw [Finset.mul_sum]
      _ ≤ (4.5 * farW k) * C0hat (2 * (k : ℝ) + 1) :=
          mul_le_mul_of_nonneg_left (farGRight_fiber_count hZC ht hk)
            (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) (farW_nonneg k))
  · rw [if_neg hk]
    apply le_of_eq
    apply Finset.sum_eq_zero
    intro ρ hρ
    rw [Set.Finite.mem_toFinset] at hρ
    have hmem2 : Nat.floor |ρ.1.im - t| = k := hρ
    rw [farGRight, if_neg (fun h => by
      have hxlt : |ρ.1.im - t| < (k : ℝ) + 1 := by
        have h9 := Nat.lt_floor_add_one |ρ.1.im - t|
        rwa [hmem2] at h9
      have hge : (Nat.floor t : ℝ) + 1 ≤ |ρ.1.im - t| := by
        have h10 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ |ρ.1.im - t| := by exact_mod_cast h.1
        have h11 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
        rwa [h11] at h10
      have hk' : Nat.floor t + 1 ≤ k := by
        have h12 : (Nat.floor t : ℝ) + 1 < (k : ℝ) + 1 := by linarith [hge, hxlt]
        have h13 : Nat.floor t + 1 < k + 1 := by
          have h16 : ((Nat.floor t + 1 : ℕ) : ℝ) < ((k + 1 : ℕ) : ℝ) := by
            have h17 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
            have h18 : ((k + 1 : ℕ) : ℝ) = (k : ℝ) + 1 := by push_cast; ring
            rwa [h17, h18]
          exact_mod_cast h16
        exact Nat.lt_succ_iff.mp h13
      exact hk hk')]

/-- loglog k ≥ −0.37 for k ≥ 1 (the bridge nonnegativity input). -/
lemma loglog_ge_neg_point37 {k : ℕ} (hk : 1 ≤ k) :
    (-0.37 : ℝ) ≤ Real.log (Real.log k) := by
  rcases eq_or_ne k 1 with rfl | hk1
  · rw [Nat.cast_one, Real.log_one, Real.log_zero]
    norm_num
  · have hk2 : 2 ≤ k := by omega
    have h1 : (0.693 : ℝ) ≤ Real.log 2 :=
      (by norm_num : (0.693 : ℝ) ≤ 0.6931471803).trans Real.log_two_gt_d9.le
    have h2 : (0.693 : ℝ) ≤ Real.log k := by
      have h3 : Real.log 2 ≤ Real.log k :=
        Real.log_le_log (show (0 : ℝ) < 2 by norm_num)
          (show (2 : ℝ) ≤ (k : ℝ) from by exact_mod_cast hk2)
      linarith [h1, h3]
    have h4 : Real.log 0.693 ≤ Real.log (Real.log k) :=
      Real.log_le_log (by norm_num : (0 : ℝ) < 0.693) h2
    have h5 : (-0.37 : ℝ) ≤ Real.log 0.693 := by
      have h6 : (1 / 0.693 : ℝ) ≤ Real.exp 0.37 := by
        have h7 := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.37 by norm_num) 5
        rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
          Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_zero] at h7
        norm_num at h7
        have h8 : (1 / 0.693 : ℝ) ≤ 1.447671 := by norm_num
        linarith [h7, h8]
      have h7 : Real.log (1 / 0.693) ≤ 0.37 := by
        have h8 := Real.log_le_log (by norm_num : (0 : ℝ) < 1 / 0.693) h6
        rwa [Real.log_exp] at h8
      rw [Real.log_div (by norm_num : (1 : ℝ) ≠ 0) (by norm_num : (0.693 : ℝ) ≠ 0),
        Real.log_one] at h7
      linarith [h7]
    linarith [h4, h5]

/-- The right-tail bridge is nonnegative for all k. -/
lemma farGRightBridge_nonneg (k : ℕ) :
    (0 : ℝ) ≤ 0.433 * (Real.log k + 0.833)
      + 0.886 * (Real.log (Real.log k) + 0.3475) + 2.9 := by
  by_cases hk0 : k = 0
  · rw [hk0]
    simp only [Nat.cast_zero, Real.log_zero]
    norm_num
  · have hk1 : 1 ≤ k := by omega
    have h1 : (0 : ℝ) ≤ Real.log k := Real.log_nonneg (by exact_mod_cast hk1 : (1 : ℝ) ≤ (k : ℝ))
    have h2 := loglog_ge_neg_point37 hk1
    have h3 : (0 : ℝ) ≤ (0.433 : ℝ) * (Real.log k + 0.833) := by
      apply mul_nonneg (by norm_num)
      linarith [h1]
    have h4 : (0.886 : ℝ) * ((-0.37 : ℝ) + 0.3475) ≤ 0.886 * (Real.log (Real.log k) + 0.3475) :=
      mul_le_mul_of_nonneg_left (by linarith [h2] : (-0.37 : ℝ) + 0.3475
        ≤ Real.log (Real.log k) + 0.3475) (by norm_num)
    have h5 : (0.433 : ℝ) * 0.833 ≤ 0.433 * (Real.log k + 0.833) :=
      mul_le_mul_of_nonneg_left (by linarith [h1] : (0.833 : ℝ) ≤ Real.log k + 0.833) (by norm_num)
    linarith [h3, h4, h5]

/-- The right-tail majorant series (if-form on ℕ). -/
def farGRightMaj (t : ℝ) : ℕ → ℝ := fun k =>
  if Nat.floor t + 1 ≤ k then 4.5 * farW k * C0hat (2 * (k : ℝ) + 1) else 0

lemma farGRightMaj_nonneg {t : ℝ} (k : ℕ) : (0 : ℝ) ≤ farGRightMaj t k := by
  by_cases hk : Nat.floor t + 1 ≤ k
  · rw [farGRightMaj, if_pos hk]
    have h1 : (1 : ℝ) ≤ 2 * (k : ℝ) + 1 := by
      have h2 : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
      linarith
    have h3 := two_le_C0hat h1
    have h4 : (0 : ℝ) ≤ 4.5 * farW k := mul_nonneg (by norm_num : (0 : ℝ) ≤ 4.5) (farW_nonneg k)
    exact mul_nonneg h4 (by linarith [h3])
  · rw [farGRightMaj, if_neg hk]

/-- The right-tail combo form (the brick decomposition target). -/
def farGRightMajCombo : ℕ → ℝ := fun k =>
  4.5 * (1 / (k : ℝ) ^ 2) * (0.433 * (Real.log k + 0.833)
    + 0.886 * (Real.log (Real.log k) + 0.3475) + 2.9)

lemma farGRightMajCombo_nonneg (k : ℕ) : (0 : ℝ) ≤ farGRightMajCombo k := by
  rw [farGRightMajCombo]
  have h := farGRightBridge_nonneg k
  have h2 : (0 : ℝ) ≤ 4.5 * (1 / (k : ℝ) ^ 2) := by positivity
  exact mul_nonneg h2 h

/-- The pointwise combo bound for the farGRightMaj. -/
lemma farGRightMaj_le_combo {t : ℝ} (ht : 10 ≤ t) (k : ℕ) :
    farGRightMaj t k ≤ farGRightMajCombo k := by
  by_cases hk : Nat.floor t + 1 ≤ k
  · rw [farGRightMaj, if_pos hk, farGRightMajCombo]
    have hk11 : 11 ≤ k := by
      have h1 : 10 ≤ Nat.floor t := by
        have h2 := Nat.floor_mono ht
        rwa [Nat.floor_ofNat] at h2
      omega
    have h1 := C0hat_two_mul_add_one_le hk11
    have h2 := farW_le_inv_sq (by omega : 1 ≤ k)
    have hnn := farGRightBridge_nonneg k
    have h4 : 4.5 * farW k ≤ 4.5 * (1 / (k : ℝ) ^ 2) :=
      mul_le_mul_of_nonneg_left h2 (by norm_num : (0 : ℝ) ≤ 4.5)
    have hC0nn : (0 : ℝ) ≤ C0hat (2 * (k : ℝ) + 1) := by
      have h6 : (1 : ℝ) ≤ 2 * (k : ℝ) + 1 := by
        have h7 : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
        linarith
      have h8 := two_le_C0hat h6
      linarith [h8]
    have h9 : (0 : ℝ) ≤ 4.5 * (1 / (k : ℝ) ^ 2) := by positivity
    exact mul_le_mul h4 h1 hC0nn h9
  · rw [farGRightMaj, if_neg hk]
    exact farGRightMajCombo_nonneg k

/-- The shifted LA₀-family for k ≥ 11 is summable. -/
lemma tail11_inv_sq_summable :
    Summable (fun k : ℕ => (1 : ℝ) / ((k : ℝ) + 11) ^ 2) := by
  have h := antitoneOn_one_div_sq.summable_of_integrableOn_Ioi
    integrableOn_one_div_sq (fun t ht => by
      have ht10 : (10 : ℝ) < t := ht
      positivity)
  have h2 := (summable_nat_add_iff (f := fun n : ℕ => (1 : ℝ) / (n : ℝ) ^ 2) 11).mpr h
  refine h2.congr fun n => ?_
  push_cast
  ring_nf

/-- The shifted LA₂-family for k ≥ 11 is summable. -/
lemma tail11_loglog_div_sq_summable :
    Summable (fun k : ℕ =>
      Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2) := by
  apply Summable.of_norm_bounded (tail11_log_div_sq_summable.mul_left (1 / Real.exp 1))
  intro k
  rw [Real.norm_eq_abs]
  rw [abs_of_nonneg (by
    have hk1 : (1 : ℝ) ≤ Real.log ((k : ℝ) + 11) := by
      have h1 := log_eleven_ge
      have h2 : Real.log 11 ≤ Real.log ((k : ℝ) + 11) :=
        Real.log_le_log (by norm_num : (0 : ℝ) < 11) (by
          have h : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
          linarith)
      linarith [h1, h2]
    have h3 : (0 : ℝ) ≤ Real.log (Real.log ((k : ℝ) + 11)) := Real.log_nonneg hk1
    positivity : (0 : ℝ) ≤ Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2)]
  have hk1 : (1 : ℝ) < (k : ℝ) + 11 := by
    have h : (0 : ℝ) ≤ (k : ℝ) := Nat.cast_nonneg k
    linarith
  have h4 := log_log_le_div_exp hk1
  have h5 : (0 : ℝ) ≤ ((k : ℝ) + 11) ^ 2 := by positivity
  have h6 := div_le_div_of_nonneg_right h4 h5
  rw [mul_div_assoc] at h6
  exact h6

/-- The shifted combo is summable (RW-brick families). -/
lemma farGRightMajCombo_shift_summable :
    Summable (fun k : ℕ => farGRightMajCombo (k + 11)) := by
  have hAB : Summable (fun k : ℕ =>
      (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((k : ℝ) + 11) ^ 2)
      + 0.433 * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)) :=
    (tail11_inv_sq_summable.mul_left _).add (tail11_log_div_sq_summable.mul_left _)
  have hABC : Summable (fun k : ℕ =>
      (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((k : ℝ) + 11) ^ 2)
      + 0.433 * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)
      + 0.886 * (Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2)) :=
    hAB.add (tail11_loglog_div_sq_summable.mul_left _)
  have hsum := hABC.mul_left 4.5
  apply Summable.congr hsum
  intro k
  rw [farGRightMajCombo]
  have hcast : ((k + 11 : ℕ) : ℝ) = (k : ℝ) + 11 := by push_cast; ring
  rw [hcast]
  ring

lemma farGRightMaj_summable {t : ℝ} (ht : 10 ≤ t) : Summable (farGRightMaj t) := by
  apply (summable_nat_add_iff (f := farGRightMaj t) 11).mp
  apply Summable.of_norm_bounded farGRightMajCombo_shift_summable
  intro k
  rw [Real.norm_eq_abs, abs_of_nonneg (farGRightMaj_nonneg (k + 11))]
  exact farGRightMaj_le_combo ht (k + 11)

lemma farGRightMaj_shift_summable {t : ℝ} (ht : 10 ≤ t) :
    Summable (fun k : ℕ => farGRightMaj t (k + 11)) :=
  (summable_nat_add_iff (f := farGRightMaj t) 11).mpr (farGRightMaj_summable ht)

/-- **The right-tail series bound**. -/
lemma farGRightMaj_series_le {t : ℝ} (ht : 10 ≤ t) :
    (∑' k : ℕ, farGRightMaj t k)
      ≤ 4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
          + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1) := by
  have hsplit : (∑' k : ℕ, farGRightMaj t k)
      = (∑ k ∈ Finset.range 11, farGRightMaj t k) + ∑' k : ℕ, farGRightMaj t (k + 11) :=
    ((farGRightMaj_summable ht).sum_add_tsum_nat_add 11).symm
  have hzero : ∑ k ∈ Finset.range 11, farGRightMaj t k = 0 := by
    apply Finset.sum_eq_zero
    intro k hk
    rw [Finset.mem_range] at hk
    rw [farGRightMaj, if_neg (by
      have h1 : 10 ≤ Nat.floor t := by
        have h2 := Nat.floor_mono ht
        rwa [Nat.floor_ofNat] at h2
      omega : ¬Nat.floor t + 1 ≤ k)]
  have hle := Summable.tsum_le_tsum (fun k => farGRightMaj_le_combo ht (k + 11))
    (farGRightMaj_shift_summable ht) farGRightMajCombo_shift_summable
  have hW0 := tail11_inv_sq_le
  have hW1 := tail11_log_div_sq_le
  have hW2 := tail11_loglog_div_sq_le
  have heval : (∑' k : ℕ,
      4.5 * ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((k : ℝ) + 11) ^ 2)
          + 0.433 * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)
          + 0.886 * (Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2)))
      ≤ 4.5 * ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * 0.1
          + 0.433 * 0.3303 + 0.886 * 0.1216) := by
    rw [tsum_mul_left]
    apply mul_le_mul_of_nonneg_left _ (by norm_num : (0 : ℝ) ≤ 4.5)
    have hdecomp : (∑' x : ℕ,
        ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)
            + 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2)
            + 0.886 * (Real.log (Real.log ((x : ℝ) + 11)) / ((x : ℝ) + 11) ^ 2)))
        = (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (∑' x : ℕ, (1 / ((x : ℝ) + 11) ^ 2))
            + 0.433 * (∑' x : ℕ, (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2))
            + 0.886 * (∑' x : ℕ, (Real.log (Real.log ((x : ℝ) + 11)) / ((x : ℝ) + 11) ^ 2)) := by
      have e1 := Summable.tsum_add
        (f := fun x : ℕ => (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)
            + 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2))
        (g := fun x : ℕ => 0.886 * (Real.log (Real.log ((x : ℝ) + 11)) / ((x : ℝ) + 11) ^ 2))
        ((tail11_inv_sq_summable.mul_left _).add (tail11_log_div_sq_summable.mul_left _))
        (tail11_loglog_div_sq_summable.mul_left _)
      have e2 := Summable.tsum_add
        (f := fun x : ℕ => (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2))
        (g := fun x : ℕ => 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2))
        (tail11_inv_sq_summable.mul_left _) (tail11_log_div_sq_summable.mul_left _)
      rw [show (∑' x : ℕ,
          ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)
              + 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2)
              + 0.886 * (Real.log (Real.log ((x : ℝ) + 11)) / ((x : ℝ) + 11) ^ 2)))
          = ∑' x : ℕ, ((fun x : ℕ =>
              (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)
              + 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2)) x
            + (fun x : ℕ => 0.886 * (Real.log (Real.log ((x : ℝ) + 11)) / ((x : ℝ) + 11) ^ 2)) x)
        from rfl]
      rw [e1]
      rw [show (∑' x : ℕ, (fun x : ℕ =>
          (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)
          + 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2)) x)
          = ∑' x : ℕ, ((fun x : ℕ =>
              (0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((x : ℝ) + 11) ^ 2)) x
            + (fun x : ℕ => 0.433 * (Real.log ((x : ℝ) + 11) / ((x : ℝ) + 11) ^ 2)) x)
        from rfl]
      rw [e2, tsum_mul_left, tsum_mul_left, tsum_mul_left]
    rw [hdecomp]
    have hle1 := mul_le_mul_of_nonneg_left hW0 (by positivity : (0 : ℝ) ≤
      0.433 * 0.833 + 0.886 * 0.3475 + 2.9)
    have hle2 := mul_le_mul_of_nonneg_left hW1 (by positivity : (0 : ℝ) ≤ 0.433)
    have hle3 := mul_le_mul_of_nonneg_left hW2 (by positivity : (0 : ℝ) ≤ 0.886)
    linarith [hle1, hle2, hle3]
  have hcombo_eq : (∑' k : ℕ, farGRightMajCombo (k + 11))
      = ∑' k : ℕ, 4.5 * ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * (1 / ((k : ℝ) + 11) ^ 2)
          + 0.433 * (Real.log ((k : ℝ) + 11) / ((k : ℝ) + 11) ^ 2)
          + 0.886 * (Real.log (Real.log ((k : ℝ) + 11)) / ((k : ℝ) + 11) ^ 2)) := by
    apply tsum_congr
    intro k
    rw [farGRightMajCombo]
    have hcast : ((k + 11 : ℕ) : ℝ) = (k : ℝ) + 11 := by push_cast; ring
    rw [hcast]
    ring
  have hle2 : (∑' k : ℕ, farGRightMaj t (k + 11))
      ≤ 4.5 * ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * 0.1
          + 0.433 * 0.3303 + 0.886 * 0.1216) := by
    rw [hcombo_eq] at hle
    exact hle.trans heval
  calc (∑' k : ℕ, farGRightMaj t k)
      = (∑ k ∈ Finset.range 11, farGRightMaj t k) + ∑' k : ℕ, farGRightMaj t (k + 11) := hsplit
    _ = 0 + ∑' k : ℕ, farGRightMaj t (k + 11) := by rw [hzero]
    _ ≤ 4.5 * ((0.433 * 0.833 + 0.886 * 0.3475 + 2.9) * 0.1
        + 0.433 * 0.3303 + 0.886 * 0.1216) := by linarith [hle2]
    _ = 4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
        + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1) := by ring

/-- **The right-tail bound** (the ENNReal fiberwise assembly). -/
lemma farGRight_le (hZC : ZeroCountProp) {t : ℝ} (ht : 10 ≤ t) :
    ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGRight t ρ
      ≤ 4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
          + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1) := by
  have hC0 : (0 : ℝ) ≤ 4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
      + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1) := by
    positivity
  rw [← ENNReal.ofReal_le_ofReal_iff hC0]
  rw [ENNReal.ofReal_tsum_of_nonneg (fun ρ => farGRight_nonneg t ρ)
    (farGRight_summable (by linarith : (0 : ℝ) ≤ t))]
  rw [← ENNReal.tsum_fiberwise (fun ρ => ENNReal.ofReal (farGRight t ρ))
    (fun ρ => Nat.floor |ρ.1.im - t|)]
  have hper : ∀ k : ℕ, ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {k},
      ENNReal.ofReal (farGRight t b.1)
      ≤ ENNReal.ofReal (farGRightMaj t k) := by
    intro k
    have h := farGRight_fiber_le hZC ht k
    rwa [farGRightMaj] at *
  calc ∑' j : ℕ, ∑' b : (fun ρ : {ρ : ℂ // IsNontrivialZero ρ} => Nat.floor |ρ.1.im - t|) ⁻¹' {j},
        ENNReal.ofReal (farGRight t b.1)
      ≤ ∑' j : ℕ, ENNReal.ofReal (farGRightMaj t j) := ENNReal.tsum_le_tsum hper
    _ = ENNReal.ofReal (∑' j : ℕ, farGRightMaj t j) := by
        rw [ENNReal.ofReal_tsum_of_nonneg (fun k => farGRightMaj_nonneg k)
          (farGRightMaj_summable ht)]
    _ ≤ ENNReal.ofReal (4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
          + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1)) :=
        ENNReal.ofReal_le_ofReal (farGRightMaj_series_le ht)

/-! ## 19. Piece 3k: the farG tail split and the piece-3 assembly -/

/-- **The tail split**: outside the main finset, farG = farGRight +
farGLeft pointwise (the main finset contains no γ < 0 zeros by
NoLowZeros, and no far zeros by construction). -/
lemma farG_tail_eq (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t)
    (ρ : {ρ : ℂ // IsNontrivialZero ρ}) :
    (if ρ ∈ mainFinset t then (0 : ℝ) else farG t ρ) = farGRight t ρ + farGLeft t ρ := by
  classical
  by_cases hmem : ρ ∈ mainFinset t
  · rw [if_pos hmem]
    have hx := (mem_mainFinset (by linarith : (0 : ℝ) ≤ t)).mp hmem
    have hright : farGRight t ρ = 0 := by
      rw [farGRight, if_neg (fun h => by
        have h1 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ |ρ.1.im - t| := by exact_mod_cast h.1
        have h2 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
        rw [h2] at h1
        linarith [hx.2, h1] : ¬ (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im))]
    have hleft : farGLeft t ρ = 0 := by
      rw [farGLeft, if_neg (by
        intro hγ
        have h1 : 14 ≤ |ρ.1.im| := hNLZ ρ.1 ρ.2
        have h2 : |ρ.1.im - t| = t + |ρ.1.im| := by
          rw [abs_of_neg (by linarith [hγ, ht] : ρ.1.im - t < 0)]
          have h3 : |ρ.1.im| = -ρ.1.im := abs_of_neg hγ
          rw [h3]
          ring
        have h4 : t + 14 ≤ |ρ.1.im - t| := by rw [h2]; linarith [h1, ht]
        have h5 : (Nat.floor t : ℝ) ≤ t := Nat.floor_le (by linarith : (0 : ℝ) ≤ t)
        have h6 : (Nat.floor t : ℝ) + 1 ≤ t + 1 := by linarith [h5]
        linarith [hx.2, h6, h4] : ¬ ρ.1.im < 0)]
    rw [hright, hleft, add_zero]
  · rw [if_neg hmem]
    by_cases hnear : ρ ∈ nearZerosSub t
    · have hz : farG t ρ = 0 := by rw [farG, if_pos hnear]
      rw [hz]
      have hright : farGRight t ρ = 0 := by
        rw [farGRight, if_neg (fun h => by
          have h2 := mem_nearZerosSub.mp hnear
          have h4 : (Nat.floor t : ℝ) + 1 ≤ |ρ.1.im - t| := by exact_mod_cast h.1
          linarith [h2, h4] : ¬ (Nat.floor t + 1 ≤ |ρ.1.im - t| ∧ t ≤ ρ.1.im))]
      rw [hright, zero_add]
      by_cases hγ0 : ρ.1.im < 0
      · rw [farGLeft, if_pos hγ0, hz]
      · rw [farGLeft, if_neg hγ0]
    · -- far (∉ nearZerosSub): x ≥ 1; ∉ main ⟹ x ≥ ⌊t⌋₊+1
      have hbig : (Nat.floor t : ℝ) + 1 ≤ |ρ.1.im - t| := by
        by_contra hsmall
        apply hmem
        rw [mem_mainFinset (by linarith : (0 : ℝ) ≤ t)]
        refine ⟨?_, ?_⟩
        · have h1 := hnear
          rw [mem_nearZerosSub] at h1
          exact (not_lt).mp h1
        · have h3 : ¬((Nat.floor t : ℝ) + 1 ≤ |ρ.1.im - t|) := by
            intro h4
            apply hsmall
            have h5 : ((Nat.floor t + 1 : ℕ) : ℝ) ≤ |ρ.1.im - t| := by exact_mod_cast h4
            have h6 : ((Nat.floor t + 1 : ℕ) : ℝ) = (Nat.floor t : ℝ) + 1 := by push_cast; ring
            rwa [h6] at h5
          exact (not_le).mp h3
      by_cases hγ : t ≤ ρ.1.im
      · rw [farGRight, if_pos ⟨hbig, hγ⟩]
        rw [farGLeft, if_neg (by linarith [hγ, ht] : ¬ ρ.1.im < 0)]
        rw [add_zero]
      · push Not at hγ
        have hγ0 : ρ.1.im < 0 := by
          have h2 : |ρ.1.im - t| = t - ρ.1.im := by
            rw [abs_of_neg (by linarith [hγ] : ρ.1.im - t < 0)]
            ring
          have h4 : t < |ρ.1.im - t| := by
            have h5 := Nat.lt_floor_add_one t
            linarith [hbig, h5]
          have h3 : t < t - ρ.1.im := by
            rw [h2] at h4
            exact h4
          linarith [h3]
        rw [farGRight, if_neg (fun h => absurd h.2 ((not_le).mpr hγ)), farGLeft, if_pos hγ0, zero_add]

/-- **PIECE 3 (ii′-5f-ii)**: the far difference series is bounded by
12.1284·C0hat(t+1) + 7.2425 (D-056's six-piece assembly, piece 3). -/
theorem far_series_le (hZC : ZeroCountProp) (hNLZ : NoLowZeros) {t : ℝ} (ht : 10 ≤ t)
    {s : ℂ} (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t)
    (hζ : riemannZeta s ≠ 0) :
    ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
      if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖
      ≤ 12.1284 * C0hat (t + 1) + 7.2425 := by
  classical
  have hnorm := far_series_norm_le_farG_tsum ht hσlo hσhi him hζ
  have hsplit := tsum_split_finset (farG_summable (by linarith : (0 : ℝ) ≤ t)) (mainFinset t)
  have htail : (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
      if ρ ∈ mainFinset t then (0 : ℝ) else farG t ρ)
      = (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGRight t ρ)
        + ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGLeft t ρ := by
    rw [← Summable.tsum_add (farGRight_summable (by linarith : (0 : ℝ) ≤ t))
      (farGLeft_summable (by linarith : (0 : ℝ) ≤ t))]
    apply tsum_congr
    intro ρ
    rw [farG_tail_eq hNLZ ht ρ]
  have hmain := farG_main_le hZC hNLZ ht
  have hright := farGRight_le hZC ht
  have hleft := farGLeft_le hZC hNLZ ht
  calc ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
        if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s (2 + t * Complex.I) ρ‖
      ≤ ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farG t ρ := hnorm
    _ = (∑ ρ ∈ mainFinset t, farG t ρ)
        + ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
          (if ρ ∈ mainFinset t then (0 : ℝ) else farG t ρ) := hsplit
    _ = (∑ ρ ∈ mainFinset t, farG t ρ)
        + ((∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGRight t ρ)
          + ∑' ρ : {ρ : ℂ // IsNontrivialZero ρ}, farGLeft t ρ) := by rw [htail]
    _ ≤ (4.5 * (2 * C0hat (t + 1) + 0.55) * 1.3476)
        + ((4.5 * (0.433 * (0.833 * 0.1 + 0.3303)
              + 0.886 * (0.3475 * 0.1 + 0.1216) + 2.9 * 0.1))
          + (4.5 * (0.433 * (0.1942 * (1 / 23) + 0.1687)
              + 0.886 * (0.0736 * (1 / 23) + 0.0621) + 2.9 * (1 / 23)))) :=
        add_le_add hmain (add_le_add hright hleft)
    _ ≤ 12.1284 * C0hat (t + 1) + 7.2425 := by
        have hC0nn : (0 : ℝ) ≤ C0hat (t + 1) := by
          have h1 := two_le_C0hat (by linarith [ht] : (1 : ℝ) ≤ t + 1)
          linarith
        nlinarith [hC0nn]

/-! ## 20. Piece 5f-iii: the pinch lemma and the capstone -/

/-- The 4-term alternating bound: −log(1−y) ≤ y + y²/2 + y³/3 + y⁴/(4(1−y)). -/
lemma neg_log_one_sub_le_four {y : ℝ} (hy0 : 0 ≤ y) (hy1 : y < 1) :
    -Real.log (1 - y) ≤ y + y ^ 2 / 2 + y ^ 3 / 3 + y ^ 4 / (4 * (1 - y)) := by
  have hcont1 : ContinuousOn (fun t : ℝ => 1 / (1 - t)) (Set.uIcc 0 y) := by
    rw [Set.uIcc_of_le hy0]
    apply ContinuousOn.div continuousOn_const (continuousOn_const.sub continuousOn_id)
    intro t ht
    rw [Set.mem_Icc] at ht
    simp only [Pi.sub_apply, id]
    linarith [ht.2, hy1]
  have hF : ∀ t ∈ Set.uIcc 0 y,
      HasDerivAt (fun x => -Real.log (1 - x)) (1 / (1 - t)) t := by
    intro t ht
    rw [Set.uIcc_of_le hy0, Set.mem_Icc] at ht
    have h1 : HasDerivAt (fun x : ℝ => 1 - x) (-1 : ℝ) t := by
      simpa using (hasDerivAt_id' t).const_sub 1
    have h2 := ((Real.hasDerivAt_log (by linarith [ht.2, hy1] : (1 : ℝ) - t ≠ 0)).comp t h1).neg
    rw [show (-(Real.log ∘ fun x : ℝ => 1 - x)) = (fun x => -Real.log (1 - x)) from by
      ext y'
      simp only [Pi.neg_apply, Function.comp_apply]] at h2
    simpa [Function.comp_def, mul_neg, neg_neg, one_mul, one_div] using h2
  have hG : ∀ t ∈ Set.uIcc 0 y,
      HasDerivAt (fun x => x + x ^ 2 / 2 + x ^ 3 / 3 + x ^ 4 / (4 * (1 - y)))
        (1 + t + t ^ 2 + t ^ 3 / (1 - y)) t := by
    intro t _
    have ha : HasDerivAt (fun x : ℝ => x) (1 : ℝ) t := hasDerivAt_id' t
    have hb : HasDerivAt (fun x : ℝ => x ^ 2 / 2) t t := by
      have h := (hasDerivAt_pow 2 t).div_const (2 : ℝ)
      rw [show ((2 : ℕ) : ℝ) * t ^ (2 - 1) / 2 = t from by
        rw [show (2 : ℕ) - 1 = 1 from rfl, pow_one]
        push_cast
        ring] at h
      exact h
    have hc : HasDerivAt (fun x : ℝ => x ^ 3 / 3) (t ^ 2) t := by
      have h := (hasDerivAt_pow 3 t).div_const (3 : ℝ)
      rw [show ((3 : ℕ) : ℝ) * t ^ (3 - 1) / 3 = t ^ 2 from by
        rw [show (3 : ℕ) - 1 = 2 from rfl]
        push_cast
        ring] at h
      exact h
    have hd : HasDerivAt (fun x : ℝ => x ^ 4 / (4 * (1 - y))) (t ^ 3 / (1 - y)) t := by
      have h := (hasDerivAt_pow 4 t).div_const (4 * (1 - y))
      rw [show ((4 : ℕ) : ℝ) * t ^ (4 - 1) / (4 * (1 - y)) = t ^ 3 / (1 - y) from by
        rw [show (4 : ℕ) - 1 = 3 from rfl]
        push_cast
        field_simp [show (1 : ℝ) - y ≠ 0 by linarith [hy1]]] at h
      exact h
    exact (((ha.add hb).add hc).add hd)
  have hInt1 : ∫ t in (0 : ℝ)..y, 1 / (1 - t) = -Real.log (1 - y) := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hF
      (ContinuousOn.intervalIntegrable hcont1)]
    simp [Real.log_one]
  have hInt2 : ∫ t in (0 : ℝ)..y, (1 + t + t ^ 2 + t ^ 3 / (1 - y))
      = y + y ^ 2 / 2 + y ^ 3 / 3 + y ^ 4 / (4 * (1 - y)) := by
    rw [intervalIntegral.integral_eq_sub_of_hasDerivAt hG
      (ContinuousOn.intervalIntegrable (by fun_prop))]
    ring_nf
  have hmono : ∫ t in (0 : ℝ)..y, 1 / (1 - t)
      ≤ ∫ t in (0 : ℝ)..y, (1 + t + t ^ 2 + t ^ 3 / (1 - y)) := by
    apply intervalIntegral.integral_mono_on hy0
      (ContinuousOn.intervalIntegrable hcont1)
      (ContinuousOn.intervalIntegrable (by fun_prop))
    intro t ht
    rw [Set.mem_Icc] at ht
    have h1 : (0 : ℝ) < 1 - t := by linarith [ht.2, hy1]
    have h2 : (0 : ℝ) < 1 - y := by linarith [hy1]
    rw [div_le_iff₀ h1]
    have h3 : (1 + t + t ^ 2 + t ^ 3 / (1 - y)) * (1 - t)
        = 1 - t ^ 3 + t ^ 3 * (1 - t) / (1 - y) := by field_simp; ring
    rw [h3]
    have h4 : (t : ℝ) ^ 3 ≤ t ^ 3 * (1 - t) / (1 - y) := by
      have h5 : (1 - y) ≤ (1 - t) := by linarith [ht.2]
      have h6 : (0 : ℝ) ≤ t ^ 3 := pow_nonneg ht.1 3
      rw [le_div_iff₀ h2]
      exact mul_le_mul_of_nonneg_left h5 h6
    linarith
  rw [hInt1, hInt2] at hmono
  exact hmono

/-- log(4/3) ≤ 0.28777 (the 4-term bound at y = 1/4). -/
lemma log_four_thirds_le : Real.log (4 / 3) ≤ 0.28777 := by
  have h1 : Real.log (4 / 3) = -Real.log (3 / 4) := by
    rw [show (4 : ℝ) / 3 = (3 / 4)⁻¹ from by norm_num, Real.log_inv]
  rw [h1]
  have h2 : (3 : ℝ) / 4 = 1 - 1 / 4 := by norm_num
  rw [h2]
  have h3 := neg_log_one_sub_le_four (y := 1 / 4) (by norm_num) (by norm_num)
  have h4 : (1 : ℝ) / 4 + (1 / 4) ^ 2 / 2 + (1 / 4) ^ 3 / 3 + (1 / 4) ^ 4 / (4 * (1 - 1 / 4))
      ≤ 0.28777 := by norm_num
  linarith [h3, h4]

/-- e^0.0987 ≥ 1.10374, hence log 3 ≤ 1.0987. -/
lemma log_three_le : Real.log 3 ≤ 1.0987 := by
  have h1 : (3 : ℝ) ≤ Real.exp 1.0987 := by
    have h2 : Real.exp 1.0987 = Real.exp 1 * Real.exp 0.0987 := by
      rw [← Real.exp_add]
      congr 1
      norm_num
    rw [h2]
    have h3 : (1.10373 : ℝ) ≤ Real.exp 0.0987 := by
      have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.0987 by norm_num) 6
      rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
        Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
        Finset.sum_range_zero] at h
      norm_num at h
      linarith [h]
    have h4 : (2.7182818 : ℝ) ≤ Real.exp 1 :=
      (by norm_num : (2.7182818 : ℝ) ≤ 2.7182818283).trans Real.exp_one_gt_d9.le
    have h5 : (3 : ℝ) ≤ 2.7182818 * 1.10373 := by norm_num
    have h6 := mul_le_mul h4 h3 (by positivity : (0 : ℝ) ≤ 1.10373) (Real.exp_pos 1).le
    exact h5.trans h6
  have h2 := Real.log_le_log (show (0 : ℝ) < 3 by norm_num) h1
  rwa [Real.log_exp] at h2

/-- log 12 ≥ 2.4848: log 12 = 4·log 2 − log(4/3). -/
lemma log_twelve_ge : (2.4848 : ℝ) ≤ Real.log 12 := by
  have hlog2 : (0.6931471 : ℝ) ≤ Real.log 2 :=
    (by norm_num : (0.6931471 : ℝ) ≤ 0.6931471803).trans Real.log_two_gt_d9.le
  have h43 := log_four_thirds_le
  have hdecomp : Real.log 12 = 4 * Real.log 2 - Real.log (4 / 3) := by
    have h1 : (12 : ℝ) = 2 ^ 4 * (3 / 4) := by norm_num
    rw [h1, Real.log_mul (by norm_num : ((2 : ℝ) ^ 4) ≠ 0) (by norm_num : (3 / 4 : ℝ) ≠ 0)]
    have h2 : Real.log ((2 : ℝ) ^ 4) = 4 * Real.log 2 := by
      rw [Real.log_pow]
      push_cast
      ring
    have h3 : Real.log (3 / 4) = -Real.log (4 / 3) := by
      rw [show (3 : ℝ) / 4 = (4 / 3)⁻¹ from by norm_num, Real.log_inv]
    rw [h2, h3]
    ring
  linarith [hdecomp, hlog2, h43]

/-- log 12 ≤ 2.5024: log 12 = 2·log 2 + log 3. -/
lemma log_twelve_le : Real.log 12 ≤ 2.5024 := by
  have hlog2 : Real.log 2 ≤ 0.69315 := Real.log_two_lt_d9.le.trans (by norm_num)
  have h3 := log_three_le
  have hdecomp : Real.log 12 = 2 * Real.log 2 + Real.log 3 := by
    have h1 : (12 : ℝ) = 4 * 3 := by norm_num
    rw [h1, Real.log_mul (by norm_num : (4 : ℝ) ≠ 0) (by norm_num : (3 : ℝ) ≠ 0)]
    have h2 : Real.log 4 = 2 * Real.log 2 := by
      have h3' : (4 : ℝ) = 2 ^ 2 := by norm_num
      rw [h3', Real.log_pow]
      push_cast
      ring
    rw [h2]
  linarith [hdecomp, hlog2, h3]

/-- e^0.9173 ≥ 2.5024 (partial sums), hence log 2.5024 ≤ 0.9173. -/
lemma log_two_point_five_zero_two_four_le : Real.log 2.5024 ≤ 0.9173 := by
  have hbase : (2.5024 : ℝ) ≤ Real.exp 0.9173 := by
    have h := Real.sum_le_exp_of_nonneg (show (0 : ℝ) ≤ 0.9173 by norm_num) 8
    rw [Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_succ,
      Finset.sum_range_succ, Finset.sum_range_succ, Finset.sum_range_zero] at h
    norm_num at h
    linarith [h]
  have h2 : Real.log 2.5024 ≤ Real.log (Real.exp 0.9173) :=
    Real.log_le_log (by norm_num : (0 : ℝ) < 2.5024) hbase
  rwa [Real.log_exp] at h2

/-- The pinch function f(L) = 11.6321·log L − 4.6484·L. -/
def pinchF (L : ℝ) : ℝ := 11.6318 * Real.log L - 4.6484 * L

/-- Its derivative f′(L) = 11.6321/L − 4.6484. -/
def pinchF' (L : ℝ) : ℝ := 11.6318 / L - 4.6484

lemma hasDerivAt_pinchF {x : ℝ} (hx0 : x ≠ 0) :
    HasDerivAt pinchF (pinchF' x) x := by
  have h1 := (Real.hasDerivAt_log hx0).const_mul 11.6318
  have h2 := (hasDerivAt_id' x).const_mul 4.6484
  have h4 := h1.sub h2
  unfold pinchF pinchF'
  rw [show (11.6318 : ℝ) / x - 4.6484 = 11.6318 * x⁻¹ - 4.6484 * 1 from by
    rw [div_eq_mul_inv, mul_one]]
  rw [show (fun L : ℝ => 11.6318 * Real.log L - 4.6484 * L)
      = (fun L : ℝ => 11.6318 * Real.log L) - (fun L : ℝ => 4.6484 * L) from by
    ext y; simp only [Pi.sub_apply]]
  simpa only [Pi.sub_apply] using h4

/-- f′ is antitone on Ici c (c > 0). -/
lemma antitoneOn_pinchF' {c : ℝ} (hc : 0 < c) :
    AntitoneOn pinchF' (Set.Ici c) := by
  intro a ha b hb hab
  rw [Set.mem_Ici] at ha hb
  rw [pinchF', pinchF']
  have ha0 : (0 : ℝ) < a := by linarith [ha, hc]
  have h1 : 11.6318 / b ≤ 11.6318 / a := by
    apply div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 11.6318) ha0 hab
  linarith [h1]

/-- f(2.5024) ≤ −0.96203 (certified evaluation). -/
lemma pinchF_at_critical : pinchF 2.5024 ≤ -0.96203 := by
  rw [show pinchF 2.5024 = 11.6318 * Real.log 2.5024 - 4.6484 * 2.5024 from rfl]
  have h1 := log_two_point_five_zero_two_four_le
  have h2 : (4.6484 : ℝ) * 2.5024 = 11.63215616 := by norm_num
  have h3 : 11.6318 * Real.log 2.5024 ≤ 11.6318 * 0.9173 :=
    mul_le_mul_of_nonneg_left h1 (by norm_num)
  have h4 : (11.6318 : ℝ) * 0.9173 = 10.66985014 := by norm_num
  linarith [h3, h2, h4]

/-- f′(2.5024) ≤ 0 (the certified sign). -/
lemma pinchF'_at_critical : pinchF' 2.5024 ≤ 0 := by
  rw [show pinchF' 2.5024 = 11.6318 / 2.5024 - 4.6484 from rfl]
  have h1 : (11.6318 : ℝ) ≤ 4.6484 * 2.5024 := by norm_num
  have h2 : (11.6318 : ℝ) / 2.5024 ≤ 4.6484 :=
    (div_le_iff₀ (show (0 : ℝ) < 2.5024 by norm_num)).mpr h1
  linarith [h2]

/-- The pinch-function derivative's continuity on Ici c (c > 0). -/
lemma continuousOn_pinchF' {c : ℝ} (hc : 0 < c) :
    ContinuousOn pinchF' (Set.Ici c) := by
  apply ContinuousOn.sub (ContinuousOn.div continuousOn_const continuousOn_id
    (fun x hx => by
      have h1 : c ≤ x := hx
      have h2 : (0 : ℝ) < x := by linarith [h1, hc]
      exact h2.ne'))
  exact continuousOn_const

/-- **The pinch lemma**: for L ≥ log 12,
11.6321·log L − 4.6484·L ≤ 0.3975 (the LL−L concave bound; max at the
certified tangent point 2.5024). -/
lemma pinch_tight_R {L : ℝ} (hL : Real.log 12 ≤ L) :
    11.6318 * Real.log L - 4.6484 * L ≤ 0.397094 := by
  have h12le := log_twelve_le
  have hlog12 : (0 : ℝ) < Real.log 12 := by
    have h1 := log_twelve_ge
    linarith [h1]
  have hLL : (0 : ℝ) < L := by linarith [hL, hlog12]
  have hanti := antitoneOn_pinchF' hlog12
  by_cases hcase : (2.5024 : ℝ) ≤ L
  · have hval := pinchF_at_critical
    have hsign := pinchF'_at_critical
    have hint : ∫ x in (2.5024 : ℝ)..L, pinchF' x = pinchF L - pinchF 2.5024 := by
      rw [intervalIntegral.integral_eq_sub_of_hasDerivAt (fun x hx =>
        hasDerivAt_pinchF (by
          have h2 := hx
          rw [Set.uIcc_of_le hcase, Set.mem_Icc] at h2
          have h3 : (0 : ℝ) < x := by linarith [h2.1]
          exact h3.ne'))
        (ContinuousOn.intervalIntegrable (by
          apply ContinuousOn.mono (continuousOn_pinchF' (by linarith : (0 : ℝ) < 2.5024))
          rw [Set.uIcc_of_le hcase]
          exact Set.Icc_subset_Ici_self))]
    have hbound : ∫ x in (2.5024 : ℝ)..L, pinchF' x ≤ 0 := by
      have h0 : (∫ x in (2.5024 : ℝ)..L, (0 : ℝ)) = 0 := by
        rw [intervalIntegral.integral_const (0 : ℝ), smul_zero]
      rw [← h0]
      apply intervalIntegral.integral_mono_on hcase
        (ContinuousOn.intervalIntegrable (by
          apply ContinuousOn.mono (continuousOn_pinchF' (by linarith : (0 : ℝ) < 2.5024))
          rw [Set.uIcc_of_le hcase]
          exact Set.Icc_subset_Ici_self))
        (ContinuousOn.intervalIntegrable continuousOn_const)
      · intro x hx
        rw [Set.mem_Icc] at hx
        have h1 : pinchF' x ≤ pinchF' 2.5024 := by
          apply hanti (Set.mem_Ici.mpr (by linarith [h12le] : (Real.log 12 : ℝ) ≤ 2.5024))
            (Set.mem_Ici.mpr (by linarith [hx.1, h12le] : (Real.log 12 : ℝ) ≤ x)) hx.1
        show pinchF' x ≤ (0 : ℝ)
        linarith [h1, hsign]
    have hval' : pinchF L ≤ -0.96203 := by
      have hF : pinchF L = pinchF 2.5024 + ∫ x in (2.5024 : ℝ)..L, pinchF' x := by
        rw [hint]
        ring
      rw [hF]
      linarith [hval, hbound]
    have h6 : pinchF L = 11.6318 * Real.log L - 4.6484 * L := rfl
    linarith [hval', h6]
  · push Not at hcase
    have hval := pinchF_at_critical
    have hsign := pinchF'_at_critical
    have hint : ∫ x in L..(2.5024 : ℝ), pinchF' x = pinchF 2.5024 - pinchF L := by
      rw [intervalIntegral.integral_eq_sub_of_hasDerivAt (fun x hx =>
        hasDerivAt_pinchF (by
          have h2 := hx
          rw [Set.uIcc_of_le hcase.le, Set.mem_Icc] at h2
          have h3 : (0 : ℝ) < x := by linarith [h2.1, hLL]
          exact h3.ne'))
        (ContinuousOn.intervalIntegrable (by
          apply ContinuousOn.mono (continuousOn_pinchF' hLL)
          rw [Set.uIcc_of_le hcase.le]
          exact Set.Icc_subset_Ici_self))]
    have hbound : (pinchF' 2.5024) * ((2.5024 : ℝ) - L)
        ≤ ∫ x in L..(2.5024 : ℝ), pinchF' x := by
      rw [show (pinchF' 2.5024) * ((2.5024 : ℝ) - L)
          = ((2.5024 : ℝ) - L) • (pinchF' 2.5024) from by
        rw [smul_eq_mul, mul_comm]]
      rw [← intervalIntegral.integral_const (pinchF' 2.5024)]
      apply intervalIntegral.integral_mono_on hcase.le
        (ContinuousOn.intervalIntegrable continuousOn_const)
        (ContinuousOn.intervalIntegrable (by
          apply ContinuousOn.mono (continuousOn_pinchF' hLL)
          rw [Set.uIcc_of_le hcase.le]
          exact Set.Icc_subset_Ici_self))
      · intro x hx
        rw [Set.mem_Icc] at hx
        exact hanti (Set.mem_Ici.mpr (by linarith [hx.1, hL] : (Real.log 12 : ℝ) ≤ x))
          (Set.mem_Ici.mpr (by linarith [hx.2, h12le] : (Real.log 12 : ℝ) ≤ 2.5024)) hx.2
    have hcorr : (-pinchF' 2.5024) * ((2.5024 : ℝ) - L) ≤ 0.001 := by
      have h1 : (0 : ℝ) ≤ -pinchF' 2.5024 := by linarith [hsign]
      have h2 : -pinchF' 2.5024 = (4.6484 : ℝ) - (11.6318 : ℝ) / 2.5024 := by
        unfold pinchF'
        ring
      have h3 : (4.6484 : ℝ) - (11.6318 : ℝ) / 2.5024 ≤ 0.001 := by
        have h6 : (4.6474 : ℝ) ≤ (11.6318 : ℝ) / 2.5024 := by
          rw [le_div_iff₀ (show (0 : ℝ) < 2.5024 by norm_num)]
          have h7 : (4.6474 : ℝ) * 2.5024 ≤ 11.6318 := by norm_num
          exact h7
        linarith [h6]
      have h6 : ((2.5024 : ℝ) - L) ≤ (2.5024 : ℝ) - Real.log 12 := by linarith [hL]
      have h7 : (0 : ℝ) ≤ (2.5024 : ℝ) - L := by linarith [hcase]
      have h8 : (0 : ℝ) ≤ (2.5024 : ℝ) - Real.log 12 := by linarith [hL]
      have h10 : (-pinchF' 2.5024) * ((2.5024 : ℝ) - L)
          ≤ 0.001 * ((2.5024 : ℝ) - Real.log 12) := by
        apply mul_le_mul _ h6 h7 (by positivity : (0 : ℝ) ≤ 0.001)
        rw [h2]
        exact h3
      have h11 : (0.001 : ℝ) * ((2.5024 : ℝ) - Real.log 12) ≤ 0.001 := by
        have h12 := log_twelve_ge
        linarith [h12]
      exact h10.trans h11
    have hval' : pinchF L ≤ -0.96203 + 0.001 := by
      have hF : pinchF L = pinchF 2.5024 - ∫ x in L..(2.5024 : ℝ), pinchF' x := by
        rw [hint]
        ring
      have hsub : -(∫ x in L..(2.5024 : ℝ), pinchF' x)
          ≤ -(pinchF' 2.5024) * ((2.5024 : ℝ) - L) := by
        linarith [hbound]
      have h13 : pinchF L
          ≤ pinchF 2.5024 + (-pinchF' 2.5024) * ((2.5024 : ℝ) - L) := by
        rw [hF]
        linarith [hsub]
      linarith [hval, hcorr, h13]
    have h6 : pinchF L = 11.6318 * Real.log L - 4.6484 * L := rfl
    linarith [hval', h6]

/-- The C0hat L/LL conversion: C0hat(t+1) ≤
0.433·(L + 1/12) + 0.886·(LL + 0.03354) + 2.9 for t ≥ 10. -/
lemma C0hat_le_of_ten_le {t : ℝ} (ht : 10 ≤ t) :
    C0hat (t + 1) ≤ 0.433 * (Real.log (t + 2) + 1 / 12)
      + 0.886 * (Real.log (Real.log (t + 2)) + 0.03354) + 2.9 := by
  have ht2 : (0 : ℝ) < t + 2 := by linarith
  have ht3 : (0 : ℝ) < t + 3 := by linarith
  have hlog12 := log_twelve_ge
  have hL : (2.4848 : ℝ) ≤ Real.log (t + 2) := by
    have h1 : Real.log 12 ≤ Real.log (t + 2) :=
      Real.log_le_log (by norm_num : (0 : ℝ) < 12) (by linarith : (12 : ℝ) ≤ t + 2)
    linarith [hlog12, h1]
  have hLpos : (0 : ℝ) < Real.log (t + 2) := by linarith [hL]
  have hinc1 : Real.log (t + 3) ≤ Real.log (t + 2) + 1 / 12 := by
    have h1 : (t : ℝ) + 3 = (t + 2) * (1 + 1 / (t + 2)) := by
      field_simp [ht2.ne']
      ring
    rw [h1, Real.log_mul ht2.ne' (by
      have h21 : (0 : ℝ) < 1 + 1 / (t + 2) := by positivity
      exact h21.ne' : (1 : ℝ) + 1 / (t + 2) ≠ 0)]
    have h2 : Real.log (1 + 1 / (t + 2)) ≤ 1 / (t + 2) := by
      have h21 := Real.log_le_sub_one_of_pos (x := 1 + 1 / (t + 2))
        (by positivity : (0 : ℝ) < 1 + 1 / (t + 2))
      rwa [show (1 : ℝ) + 1 / (t + 2) - 1 = 1 / (t + 2) from by ring] at h21
    have h3 : (1 : ℝ) / (t + 2) ≤ 1 / 12 := by
      apply div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 1) (by norm_num : (0 : ℝ) < 12)
      linarith
    linarith [h2, h3]
  have hinc2 : Real.log (Real.log (t + 3))
      ≤ Real.log (Real.log (t + 2)) + 0.03354 := by
    have h4 : Real.log (Real.log (t + 3)) ≤ Real.log (Real.log (t + 2) + 1 / 12) :=
      Real.log_le_log (Real.log_pos (by linarith [ht] : (1 : ℝ) < t + 3)) hinc1
    have h5 : Real.log (Real.log (t + 2) + 1 / 12)
        ≤ Real.log (Real.log (t + 2)) + (1 / 12) / Real.log (t + 2) :=
      log_add_le (Real.log (t + 2)) (1 / 12) hLpos (by norm_num)
    have h6 : (1 / 12 : ℝ) / Real.log (t + 2) ≤ 0.03354 := by
      rw [div_le_iff₀ hLpos]
      have h7 : (0.03354 : ℝ) * 2.4848 ≤ 0.03354 * Real.log (t + 2) :=
        mul_le_mul_of_nonneg_left hL (by norm_num)
      have h8 : (1 / 12 : ℝ) ≤ 0.03354 * 2.4848 := by norm_num
      exact h8.trans h7
    linarith [h4, h5, h6]
  rw [C0hat]
  have harg : (t : ℝ) + 1 + 2 = t + 3 := by ring
  rw [harg]
  linarith [hinc1, hinc2]

/-! ## 21. Piece 5f-iii capstone: the tight R-bound -/

/-- **The tight R-bound** (ii′-5f capstone; D-056's Theorem R with the
D-069 honest piece-2 two-unit count): for s = σ + it with σ ∈ [−5/2, 3],
t ≥ 10, and ζ s ≠ 0:
‖logDeriv ζ s − Σ_{ρ near} m_ρ/(s−ρ)‖ ≤ 10.5·log(t+2) + 48. -/
theorem logDeriv_riemannZeta_sub_nearZeros_le_tight
    (hZC : ZeroCountProp) (hZC2 : ZeroCountProp2) (hNLZ : NoLowZeros)
    {t : ℝ} (ht : 10 ≤ t) {s : ℂ}
    (hσlo : -5 / 2 ≤ s.re) (hσhi : s.re ≤ 3) (him : s.im = t)
    (hζ : riemannZeta s ≠ 0) :
    ‖logDeriv riemannZeta s
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s - ρ.1)‖
      ≤ 10.5 * Real.log (|t| + 2) + 48 := by
  classical
  -- the elementary conditions for Rexpr_decomp
  have hs0 : s ≠ 0 := by
    intro h
    rw [h, Complex.zero_im] at him
    linarith [ht]
  have hs1 : s ≠ 1 := by
    intro h
    rw [h, Complex.one_im] at him
    linarith [ht]
  have hst : ∀ n : ℕ, 1 ≤ n → s ≠ -2 * (n : ℂ) := by
    intro n _ h
    have him2 : (-2 * (n : ℂ)).im = 0 := by simp
    rw [h, him2] at him
    linarith [ht]
  set s₀ : ℂ := 2 + t * Complex.I with hs₀def
  have hs₀ : riemannZeta s₀ ≠ 0 := by
    apply riemannZeta_ne_zero_of_one_le_re
    rw [hs₀def]
    simp
  have hs₀0 : s₀ ≠ 0 := by
    intro h
    have h1 : s₀.re = (0 : ℂ).re := by rw [h]
    rw [hs₀def] at h1
    simp at h1
  have hs₀1 : s₀ ≠ 1 := by
    intro h
    have h1 : s₀.re = (1 : ℂ).re := by rw [h]
    rw [hs₀def] at h1
    simp at h1
  have hs₀t : ∀ n : ℕ, 1 ≤ n → s₀ ≠ -2 * (n : ℂ) := by
    intro n hn h
    have h1 : s₀.re = (-2 * (n : ℂ)).re := by rw [h]
    rw [hs₀def] at h1
    simp at h1
    have hn1 : (1 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn
    linarith [h1, hn1]
  -- the pieces
  have hdec := Rexpr_decomp hZC hζ hs0 hs1 hst hs₀ hs₀0 hs₀1 hs₀t t
  have h1 : ‖logDeriv riemannZeta s₀‖ ≤ dirichletC2 := by
    apply norm_logDeriv_zeta_le_dirichlet
    rw [hs₀def]
    simp
  have h1b := dirichletC2_le_one
  have h2 := near_sum_refPoint_le hZC2 ht
  have h3 := far_series_le hZC hNLZ ht hσlo hσhi him hζ
  have h45 := one_div_diff_le ht hσlo hσhi him hs0
  have h6 := digamma_diff_le ht hσlo hσhi him
  -- the norm chain
  have hnorm : ‖logDeriv riemannZeta s
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s - ρ.1)‖
      ≤ ‖logDeriv riemannZeta s₀‖
        + ‖∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)‖
        + ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
            if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ‖
        + ‖1 / s - 1 / s₀‖
        + ‖1 / (s - 1) - 1 / (s₀ - 1)‖
        + ‖(Complex.digamma (s / 2) - Complex.digamma (s₀ / 2)) / 2‖ := by
    rw [hdec]
    have hc1 : ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
          - (1 / s - 1 / s₀) - (1 / (s - 1) - 1 / (s₀ - 1))
          - (Complex.digamma (s / 2) - Complex.digamma (s₀ / 2)) / 2‖
        ≤ ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
          - (1 / s - 1 / s₀) - (1 / (s - 1) - 1 / (s₀ - 1))‖
          + ‖(Complex.digamma (s / 2) - Complex.digamma (s₀ / 2)) / 2‖ :=
      norm_sub_le _ _
    have hc2 : ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
          - (1 / s - 1 / s₀) - (1 / (s - 1) - 1 / (s₀ - 1))‖
        ≤ ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
          - (1 / s - 1 / s₀)‖
          + ‖1 / (s - 1) - 1 / (s₀ - 1)‖ :=
      norm_sub_le _ _
    have hc3 : ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)
          - (1 / s - 1 / s₀)‖
        ≤ ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)‖
          + ‖1 / s - 1 / s₀‖ :=
      norm_sub_le _ _
    have hc4 : ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)
          + (∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ)‖
        ≤ ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)‖
          + ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
              if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ‖ :=
      norm_add_le _ _
    have hc5 : ‖logDeriv riemannZeta s₀
          - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)‖
        ≤ ‖logDeriv riemannZeta s₀‖
          + ‖∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)‖ :=
      norm_sub_le _ _
    linarith [hc1, hc2, hc3, hc4, hc5]
  -- the L/LL conversion and the fold
  have hconv := C0hat_le_of_ten_le ht
  have habs : |t| = t := abs_of_nonneg (by linarith [ht])
  have hL : Real.log 12 ≤ Real.log (t + 2) := by
    have h1 : Real.log 12 ≤ Real.log (t + 2) :=
      Real.log_le_log (by norm_num : (0 : ℝ) < 12) (by linarith [ht] : (12 : ℝ) ≤ t + 2)
    exact h1
  have hpinch := pinch_tight_R hL
  have h6t : 6 / t ≤ 0.6 := by
    rw [div_le_iff₀ (by linarith [ht] : (0 : ℝ) < t)]
    have h1 : (0.6 : ℝ) * t ≥ 6 := by linarith [ht]
    linarith [h1]
  have h2' := h2
  rw [← hs₀def] at h2'
  have h45a := h45.1
  have h45b := h45.2
  rw [← hs₀def] at h45a h45b
  calc ‖logDeriv riemannZeta s
        - ∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s - ρ.1)‖
      ≤ ‖logDeriv riemannZeta s₀‖
        + ‖∑ ρ ∈ nearZerosSub t, (zeroMult ρ.1 : ℂ) / (s₀ - ρ.1)‖
        + ‖∑' ρ : {ρ : ℂ // IsNontrivialZero ρ},
            if ρ ∈ nearZerosSub t then (0 : ℂ) else seriesDiffTerm s s₀ ρ‖
        + ‖1 / s - 1 / s₀‖
        + ‖1 / (s - 1) - 1 / (s₀ - 1)‖
        + ‖(Complex.digamma (s / 2) - Complex.digamma (s₀ / 2)) / 2‖ := hnorm
    _ ≤ 1 + (0.6 * Real.log (t + 2) + 0.886 * Real.log (Real.log (t + 2)) + 2.7)
        + (12.1284 * C0hat (t + 1) + 7.2425)
        + 0.045 + 0.045 + 6 / t := by
          linarith [h1, h1b, h2', h3, h45a, h45b, h6, h6t]
    _ ≤ 10.5 * Real.log (|t| + 2) + 48 := by
          rw [habs]
          have hp : 12.1284 * C0hat (t + 1)
              ≤ 12.1284 * (0.433 * (Real.log (t + 2) + 1 / 12)
                  + 0.886 * (Real.log (Real.log (t + 2)) + 0.03354) + 2.9) :=
            mul_le_mul_of_nonneg_left hconv (by norm_num)
          have hp2 : 12.1284 * (0.433 * (Real.log (t + 2) + 1 / 12)
              + 0.886 * (Real.log (Real.log (t + 2)) + 0.03354) + 2.9)
              = 12.1284 * 0.433 * (Real.log (t + 2) + 1 / 12)
                + 12.1284 * 0.886 * (Real.log (Real.log (t + 2)) + 0.03354)
                + 12.1284 * 2.9 := by ring
          have hp3 : 12.1284 * 0.433 * (Real.log (t + 2) + 1 / 12)
              = 5.2515972 * Real.log (t + 2) + 0.4376331 := by
            ring_nf
          have hp4 : 12.1284 * 0.886 * (Real.log (Real.log (t + 2)) + 0.03354)
              = 10.7457624 * Real.log (Real.log (t + 2)) + 12.1284 * 0.886 * 0.03354 := by
            ring
          have hp4b : (12.1284 : ℝ) * 0.886 * 0.03354 ≤ 0.360413 := by norm_num
          have hp5 : (12.1284 : ℝ) * 2.9 = 35.17236 := by norm_num
          have hr1 : (5.8515972 : ℝ) ≤ 5.8516 := by norm_num
          have hr2 : (10.7457624 : ℝ) ≤ 11.6318 := by norm_num
          have hLLnn : (0 : ℝ) ≤ Real.log (Real.log (t + 2)) := by
            apply Real.log_nonneg
            have h12 := log_twelve_ge
            linarith [hL, h12]
          have hLnn : (0 : ℝ) ≤ Real.log (t + 2) := by
            have h12 := log_twelve_ge
            linarith [hL, h12]
          linarith [hp, hp2, hp3, hp4, hp4b, hp5, h6t, hpinch, hr1, hr2, hLLnn, hLnn]

end NguyenRH1

end