#!/usr/bin/env python3
"""
Daily Curiosity #6 — Чому морська вода солона?
Schematic: rain on mountains -> rivers leach mineral salts from rocks ->
salt accumulates in oceans over billions of years (water evaporates, salt
stays). Inset shows Na+ / Cl- ions dissolved in seawater.
"""
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, Circle, FancyBboxPatch, Polygon
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

# ---- Canvas ----
fig, ax = plt.subplots(figsize=(12, 7), dpi=160)
ax.set_xlim(0, 12)
ax.set_ylim(0, 7)
ax.set_aspect('equal')
ax.axis('off')

# Deep sea / sky gradient background
bg = np.linspace(1, 0, 256).reshape(-1, 1)
bg = np.repeat(bg, 256, axis=1)
ax.imshow(
    bg,
    extent=(0, 12, 0, 7),
    cmap=LinearSegmentedColormap.from_list(
        "ocean_sky", ["#02172b", "#0a2a4a", "#1a4870", "#3a6c93"]
    ),
    aspect='auto',
    zorder=0,
)

# ---- Title ----
ax.text(
    6.0, 6.55,
    "Чому морська вода солона?",
    ha='center', va='center',
    fontsize=22, fontweight='bold', color='#fdf6c7',
    zorder=10,
)
ax.text(
    6.0, 6.08,
    "Річки мільярди років зносять у океан розчинені мінерали — а вода випаровується",
    ha='center', va='center',
    fontsize=11, style='italic', color='#cfe6ff',
    zorder=10,
)

# ---- Sun ----
sun = Circle((10.7, 5.4), 0.42, facecolor='#ffd66b',
             edgecolor='#fff1a8', linewidth=2, zorder=4)
ax.add_patch(sun)
for ang in np.linspace(0, 2*np.pi, 12, endpoint=False):
    x0 = 10.7 + 0.55*np.cos(ang)
    y0 = 5.4 + 0.55*np.sin(ang)
    x1 = 10.7 + 0.78*np.cos(ang)
    y1 = 5.4 + 0.78*np.sin(ang)
    ax.plot([x0, x1], [y0, y1], color='#ffd66b', lw=1.8, zorder=4)

# ---- Cloud over mountains ----
for cx, cy, r in [(2.0, 5.2, 0.42), (2.55, 5.3, 0.5), (3.05, 5.2, 0.4),
                  (2.7, 4.95, 0.45), (2.25, 4.95, 0.4)]:
    ax.add_patch(Circle((cx, cy), r, facecolor='#dfe8ef',
                        edgecolor='#9fb3c2', linewidth=1.2, zorder=3))

# Rain drops from cloud
np.random.seed(6)
for _ in range(14):
    rx = np.random.uniform(1.7, 3.3)
    ry = np.random.uniform(3.7, 4.6)
    ax.plot([rx, rx-0.03], [ry, ry-0.22],
            color='#7ec5ff', lw=1.6, solid_capstyle='round', zorder=3)

# ---- Mountains with snow caps ----
mountain1 = Polygon(
    [(0.2, 1.6), (1.6, 4.3), (2.2, 3.6), (3.2, 4.7), (4.6, 1.6)],
    closed=True, facecolor='#3a4a5a', edgecolor='#6f8194',
    linewidth=1.4, zorder=2,
)
ax.add_patch(mountain1)
# Snow caps
ax.add_patch(Polygon([(1.45, 4.05), (1.6, 4.3), (1.78, 4.0), (1.65, 3.85)],
                     facecolor='#f4faff', edgecolor='none', zorder=2.5))
ax.add_patch(Polygon([(2.95, 4.4), (3.2, 4.7), (3.45, 4.35), (3.18, 4.18)],
                     facecolor='#f4faff', edgecolor='none', zorder=2.5))

# ---- River flowing down to ocean ----
river_pts_x = [2.6, 2.95, 3.4, 3.9, 4.55, 5.2]
river_pts_y = [3.55, 3.0, 2.55, 2.15, 1.85, 1.55]
ax.plot(river_pts_x, river_pts_y, color='#7ec5ff', lw=4.5,
        solid_capstyle='round', zorder=2.8)
ax.plot(river_pts_x, river_pts_y, color='#bfe1ff', lw=1.8,
        solid_capstyle='round', zorder=2.9)

# Salt grains being washed off rocks (small white dots along river)
for (sx, sy) in zip(river_pts_x[1:-1], river_pts_y[1:-1]):
    for dx, dy in [(-0.1, 0.05), (0.08, -0.04), (0.0, 0.1)]:
        ax.add_patch(Circle((sx+dx, sy+dy), 0.04,
                            facecolor='#ffffff', edgecolor='none', zorder=3))

# Label: "розчинені солі"
ax.annotate(
    "розчинені солі\n(Na⁺, Cl⁻, Mg²⁺, K⁺, Ca²⁺...)",
    xy=(4.0, 2.3), xytext=(4.7, 3.7),
    ha='left', va='center', fontsize=9.5, color='#fdf6c7',
    arrowprops=dict(arrowstyle='->', color='#fdf6c7', lw=1.3),
    zorder=6,
)

# ---- Ocean (lower part) ----
ocean_y = 1.6
ax.add_patch(Polygon(
    [(0, 0), (12, 0), (12, ocean_y), (0, ocean_y)],
    closed=True, facecolor='#0e3b62', edgecolor='none', zorder=1.5,
))
# Wave line
xs = np.linspace(0, 12, 400)
ax.plot(xs, ocean_y + 0.06*np.sin(xs*2.3),
        color='#1f5b8c', lw=1.5, zorder=1.6)
ax.plot(xs, ocean_y - 0.05 + 0.04*np.sin(xs*3.1+1),
        color='#0a2c4d', lw=1.0, zorder=1.6)

# Subtle ocean texture lines
for y in [1.2, 0.85, 0.5, 0.2]:
    ax.plot(xs, y + 0.03*np.sin(xs*2.1+y*5),
            color='#13476f', lw=0.8, alpha=0.6, zorder=1.7)

# ---- Evaporation arrows from ocean to sun ----
for sx, sy in [(7.6, 1.4), (8.4, 1.3), (9.2, 1.45), (10.0, 1.3)]:
    arr = FancyArrowPatch(
        (sx, sy), (sx+0.4, sy+1.6),
        arrowstyle='->', mutation_scale=14,
        color='#bfe1ff', lw=1.5, alpha=0.85, zorder=4,
        connectionstyle="arc3,rad=0.25",
    )
    ax.add_patch(arr)

ax.text(9.0, 3.2, "вода випаровується,\nа сіль лишається",
        ha='center', va='center', fontsize=10,
        color='#bfe1ff', style='italic', zorder=6,
        bbox=dict(boxstyle='round,pad=0.35', facecolor='#0e2742',
                  edgecolor='#3a6c93', linewidth=1))

# ---- Ions in the ocean (Na+ and Cl-) ----
ion_positions = [
    (1.2, 1.0, 'Na⁺', '#ff8a65'),
    (2.0, 0.55, 'Cl⁻', '#7ec5ff'),
    (2.9, 0.95, 'Na⁺', '#ff8a65'),
    (3.7, 0.45, 'Cl⁻', '#7ec5ff'),
    (4.5, 0.85, 'Na⁺', '#ff8a65'),
    (5.4, 1.2, 'Cl⁻', '#7ec5ff'),
    (6.2, 0.6, 'Na⁺', '#ff8a65'),
    (7.0, 1.05, 'Cl⁻', '#7ec5ff'),
    (7.8, 0.4, 'Na⁺', '#ff8a65'),
    (5.0, 0.4, 'Cl⁻', '#7ec5ff'),
    (1.6, 0.4, 'Na⁺', '#ff8a65'),
    (3.4, 1.25, 'Cl⁻', '#7ec5ff'),
]
for ix, iy, label, col in ion_positions:
    ax.add_patch(Circle((ix, iy), 0.22, facecolor=col,
                        edgecolor='white', linewidth=1.2, zorder=3))
    ax.text(ix, iy, label, ha='center', va='center',
            fontsize=8.2, fontweight='bold', color='#0e2742', zorder=4)

# ---- River label ----
ax.text(2.4, 5.85, "хмари + дощ",
        ha='center', va='center', fontsize=10, color='#cfe6ff',
        style='italic', zorder=6)

# ---- Numbers / fact box ----
fact = (
    "≈ 3,5% солі у морській воді  •  35 г солі на 1 л\n"
    "Якби всю сіль океанів розсипати по суходолу — шар ≈ 150 метрів"
)
ax.text(6.0, 0.18, fact, ha='center', va='center',
        fontsize=10.5, color='#fdf6c7', fontweight='bold', zorder=7,
        bbox=dict(boxstyle='round,pad=0.45', facecolor='#02172b',
                  edgecolor='#fdf6c7', linewidth=1.3))

# ---- Save ----
out = "/sessions/zen-brave-cray/mnt/daily-curiosity/daily-curiosity-06.png"
plt.savefig(out, dpi=160, bbox_inches='tight',
            facecolor='#02172b', edgecolor='none')
plt.close(fig)
print(f"Saved: {out}")
