// Résonance primitives — orbs, glass, icons, chrome

const RZ = {
  orange: "#FC442C",
  blue: "#1772FE",
  mauve: "#8A5B95",
  white: "#FAF8F5",
  black: "#18140F",
  fg1: "#18140F",
  fg2: "#5C5650",
  fg3: "#A09890",
};

// ─────────────────────────────────────────────────────────────
// Orb background — the two-orb signature
// ─────────────────────────────────────────────────────────────
function OrbBackground({ intent = "journal", intensity = 1 }) {
  // intent: journal (orange dom), nuance (blue dom), echo (mauve center), onboard, result-*
  const presets = {
    journal: [
      { c: RZ.orange, size: 360, x: -60, y: -120, op: 0.55 },
      { c: RZ.orange, size: 220, x: 220, y: 540, op: 0.32 },
      { c: RZ.blue, size: 160, x: 260, y: 620, op: 0.25 },
    ],
    nuance: [
      { c: RZ.blue, size: 380, x: 180, y: -100, op: 0.55 },
      { c: RZ.blue, size: 220, x: -80, y: 500, op: 0.32 },
      { c: RZ.orange, size: 150, x: 40, y: 600, op: 0.28 },
    ],
    echo: [
      { c: RZ.mauve, size: 420, x: 60, y: 140, op: 0.6 },
      { c: RZ.mauve, size: 220, x: 120, y: 280, op: 0.45 },
      { c: RZ.orange, size: 140, x: -40, y: -60, op: 0.35 },
      { c: RZ.blue, size: 140, x: 260, y: 700, op: 0.35 },
    ],
    onboard: [
      { c: RZ.orange, size: 340, x: -80, y: -80, op: 0.5 },
      { c: RZ.blue, size: 340, x: 140, y: 500, op: 0.5 },
    ],
    "result-resonance": [
      { c: RZ.mauve, size: 380, x: 60, y: 140, op: 0.65 },
      { c: RZ.orange, size: 180, x: -40, y: 40, op: 0.35 },
      { c: RZ.blue, size: 180, x: 240, y: 260, op: 0.35 },
    ],
    "result-equilibre": [
      { c: RZ.blue, size: 320, x: 60, y: 140, op: 0.55 },
      { c: RZ.orange, size: 220, x: 140, y: 240, op: 0.4 },
    ],
    "result-tension": [
      { c: RZ.orange, size: 280, x: -40, y: 180, op: 0.5 },
      { c: RZ.blue, size: 280, x: 200, y: 260, op: 0.5 },
    ],
    "result-dissonance": [
      { c: RZ.orange, size: 280, x: -120, y: 100, op: 0.58 },
      { c: RZ.blue, size: 280, x: 240, y: 340, op: 0.58 },
    ],
    "result-alerte": [
      { c: RZ.orange, size: 420, x: 40, y: 120, op: 0.75 },
      { c: RZ.orange, size: 260, x: 140, y: 340, op: 0.55 },
    ],
    settings: [
      { c: RZ.mauve, size: 280, x: -60, y: 600, op: 0.3 },
      { c: RZ.blue, size: 240, x: 200, y: -60, op: 0.3 },
    ],
    error: [{ c: RZ.orange, size: 320, x: 60, y: 180, op: 0.35 }],
  };
  const orbs = presets[intent] || presets.journal;
  return (
    <div
      style={{
        position: "absolute",
        inset: 0,
        overflow: "hidden",
        pointerEvents: "none",
        background: RZ.white,
      }}
    >
      {orbs.map((o, i) => (
        <div
          key={i}
          className="rz-drift"
          style={{
            position: "absolute",
            left: o.x,
            top: o.y,
            width: o.size,
            height: o.size,
            borderRadius: "50%",
            background: `radial-gradient(closest-side, ${o.c} 0%, ${o.c}00 70%)`,
            opacity: o.op * intensity,
            filter: "blur(60px)",
            animationDelay: `${i * 1.3}s`,
          }}
        />
      ))}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Glass card
// ─────────────────────────────────────────────────────────────
function Glass({
  raised = false,
  style = {},
  onClick,
  children,
  className = "",
  glassOpacity = 1,
}) {
  const base = raised ? 0.7 : 0.5;
  const bg = `rgba(255,255,255,${Math.max(0.25, Math.min(0.92, base * glassOpacity))})`;
  const blur = raised ? 28 : 20;
  return (
    <div
      onClick={onClick}
      className={className}
      style={{
        background: bg,
        backdropFilter: `blur(${blur}px)`,
        WebkitBackdropFilter: `blur(${blur}px)`,
        border: "1px solid rgba(255,255,255,0.70)",
        borderRadius: 16,
        boxShadow: raised
          ? "0 16px 40px -16px rgba(24, 20, 15, 0.12)"
          : "0 8px 24px -12px rgba(24, 20, 15, 0.08)",
        ...style,
      }}
    >
      {children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Icons — outline, 1.5 stroke, 24×24
// ─────────────────────────────────────────────────────────────
const Icon = ({
  name,
  size = 22,
  color,
  strokeWidth = 1.5,
  style = {},
  ariaLabel,
}) => {
  const c = color || RZ.fg1;
  const label = ariaLabel || name;
  const common = {
    width: size,
    height: size,
    viewBox: "0 0 24 24",
    fill: "none",
    stroke: c,
    strokeWidth,
    strokeLinecap: "round",
    strokeLinejoin: "round",
    style,
    role: "img",
    "aria-label": label,
    focusable: "false",
  };
  const paths = {
    book: (
      <>
        <path d="M4 4h7v16H4z" />
        <path d="M13 4h7v16h-7z" />
        <path d="M11 4v16" />
      </>
    ),
    mic: (
      <>
        <rect x="9" y="3" width="6" height="12" rx="3" />
        <path d="M5 11a7 7 0 0 0 14 0" />
        <path d="M12 18v3" />
      </>
    ),
    waves: (
      <>
        <path d="M3 12c2-4 4-4 6 0s4 4 6 0 4-4 6 0" />
        <path d="M3 17c2-3 4-3 6 0s4 3 6 0 4-3 6 0" />
      </>
    ),
    back: <path d="M15 4l-8 8 8 8" />,
    close: (
      <>
        <path d="M6 6l12 12" />
        <path d="M18 6L6 18" />
      </>
    ),
    plus: (
      <>
        <path d="M12 5v14" />
        <path d="M5 12h14" />
      </>
    ),
    settings: (
      <>
        <circle cx="12" cy="12" r="3" />
        <path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 11-2.83 2.83l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 11-4 0v-.09a1.65 1.65 0 00-1-1.51 1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 11-2.83-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 110-4h.09a1.65 1.65 0 001.51-1 1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 112.83-2.83l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 114 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 112.83 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 110 4h-.09a1.65 1.65 0 00-1.51 1z" />
      </>
    ),
    run: (
      <>
        <circle cx="14" cy="4" r="2" />
        <path d="M4 18l4-3 3 3 2-5 4 2 3-4" />
      </>
    ),
    bike: (
      <>
        <circle cx="5" cy="17" r="4" />
        <circle cx="19" cy="17" r="4" />
        <path d="M12 17l-3-7h4l3 5 3-5" />
      </>
    ),
    swim: (
      <>
        <path d="M3 15c2-1 3 1 5 1s3-2 5-1 3 1 5 1 3-2 5-1" />
        <circle cx="16" cy="7" r="2" />
        <path d="M7 11l3-2 3 2" />
      </>
    ),
    trail: (
      <>
        <path d="M3 20l5-9 4 5 3-3 6 7" />
        <circle cx="7" cy="5" r="2" />
      </>
    ),
    strength: (
      <>
        <path d="M6 9v6M18 9v6M3 11v2M21 11v2M9 7v10M15 7v10" />
      </>
    ),
    strava: (
      <path d="M8.5 2L2 14h4l2.5-4.5L11 14h4L8.5 2zm7 10l-2 4 4 4 4-4-2-4h-2l-2 2-2-2h2z" />
    ),
    sun: (
      <>
        <circle cx="12" cy="12" r="4" />
        <path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" />
      </>
    ),
    chat: <path d="M21 12a8 8 0 0 1-11.5 7.2L4 21l1.8-5.5A8 8 0 1 1 21 12z" />,
    check: <path d="M4 12l5 5L20 6" />,
    chev: <path d="M9 6l6 6-6 6" />,
    share: (
      <>
        <circle cx="18" cy="5" r="3" />
        <circle cx="6" cy="12" r="3" />
        <circle cx="18" cy="19" r="3" />
        <path d="M8.6 10.5l6.8-4M8.6 13.5l6.8 4" />
      </>
    ),
    sparkle: (
      <>
        <path d="M12 3l2 5 5 2-5 2-2 5-2-5-5-2 5-2 2-5z" />
      </>
    ),
    play: <path d="M6 4l14 8-14 8z" />,
    pause: (
      <>
        <rect x="6" y="4" width="4" height="16" rx="1" />
        <rect x="14" y="4" width="4" height="16" rx="1" />
      </>
    ),
    search: (
      <>
        <circle cx="11" cy="11" r="7" />
        <path d="M21 21l-5-5" />
      </>
    ),
    filter: <path d="M3 6h18M6 12h12M10 18h4" />,
    bell: (
      <>
        <path d="M6 9a6 6 0 0112 0c0 7 3 7 3 9H3c0-2 3-2 3-9z" />
        <path d="M10 21h4" />
      </>
    ),
    lock: (
      <>
        <rect x="4" y="10" width="16" height="11" rx="2" />
        <path d="M8 10V7a4 4 0 018 0v3" />
      </>
    ),
    trash: (
      <>
        <path d="M4 7h16M9 7V4h6v3M6 7l1 14h10l1-14" />
      </>
    ),
    calendar: (
      <>
        <rect x="3" y="5" width="18" height="16" rx="2" />
        <path d="M3 10h18M8 3v4M16 3v4" />
      </>
    ),
    clock: (
      <>
        <circle cx="12" cy="12" r="9" />
        <path d="M12 7v5l3 2" />
      </>
    ),
    heart: (
      <path d="M12 21s-7-4.5-9-9a5 5 0 019-3 5 5 0 019 3c-2 4.5-9 9-9 9z" />
    ),
    location: (
      <>
        <path d="M12 21s-7-7-7-12a7 7 0 0114 0c0 5-7 12-7 12z" />
        <circle cx="12" cy="9" r="2.5" />
      </>
    ),
    volume: (
      <>
        <path d="M4 9v6h4l5 4V5L8 9H4z" />
        <path d="M17 8a5 5 0 010 8" />
      </>
    ),
    arrowRight: (
      <>
        <path d="M5 12h14" />
        <path d="M13 5l7 7-7 7" />
      </>
    ),
    arrowLeft: (
      <>
        <path d="M19 12H5" />
        <path d="M11 5l-7 7 7 7" />
      </>
    ),
    question: (
      <>
        <circle cx="12" cy="12" r="9" />
        <path d="M9.5 9a2.5 2.5 0 115 0c0 1.5-2.5 2-2.5 4" />
        <circle cx="12" cy="17" r="0.6" fill={c} stroke="none" />
      </>
    ),
    wifi: (
      <>
        <path d="M2 9a15 15 0 0120 0" />
        <path d="M5 13a10 10 0 0114 0" />
        <path d="M8.5 17a5 5 0 017 0" />
        <circle cx="12" cy="20" r="0.8" fill={c} stroke="none" />
      </>
    ),
  };
  return <svg {...common}>{paths[name] || null}</svg>;
};

// ─────────────────────────────────────────────────────────────
// Tab bar
// ─────────────────────────────────────────────────────────────
function TabBar({ active = "journal", onChange }) {
  const tabs = [
    { id: "journal", label: "Journal", icon: "book" },
    { id: "nuance", label: "Nuance", icon: "mic" },
    { id: "echo", label: "ÉCHO", icon: "waves" },
  ];
  return (
    <div
      style={{
        position: "absolute",
        left: 12,
        right: 12,
        bottom: 24,
        zIndex: 40,
        background: "rgba(255,255,255,0.80)",
        backdropFilter: "blur(20px) saturate(180%)",
        WebkitBackdropFilter: "blur(20px) saturate(180%)",
        border: "1px solid rgba(255,255,255,0.70)",
        borderRadius: 22,
        padding: "10px 6px 12px",
        display: "flex",
        justifyContent: "space-around",
        boxShadow: "0 -4px 20px -10px rgba(24,20,15,0.10)",
      }}
    >
      {tabs.map((t) => {
        const on = active === t.id;
        const color = on ? RZ.fg1 : RZ.fg3;
        return (
          <div
            key={t.id}
            onClick={() => onChange && onChange(t.id)}
            style={{
              display: "flex",
              flexDirection: "column",
              alignItems: "center",
              gap: 3,
              padding: "4px 14px",
              cursor: "pointer",
              minWidth: 64,
            }}
          >
            <Icon
              name={t.icon}
              size={22}
              color={color}
              strokeWidth={on ? 1.8 : 1.5}
            />
            <div
              style={{
                fontSize: 10,
                fontWeight: on ? 700 : 500,
                color,
                letterSpacing: t.id === "echo" ? 1.2 : 0.4,
              }}
            >
              {t.label}
            </div>
          </div>
        );
      })}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// iOS status bar (light)
// ─────────────────────────────────────────────────────────────
function StatusBar() {
  return (
    <div
      style={{
        height: 54,
        paddingTop: 18,
        padding: "18px 28px 0",
        display: "flex",
        justifyContent: "space-between",
        alignItems: "center",
        position: "relative",
        zIndex: 20,
        pointerEvents: "none",
      }}
    >
      <div
        style={{
          fontFamily: '-apple-system, "SF Pro", system-ui',
          fontWeight: 590,
          fontSize: 17,
          lineHeight: "22px",
          color: RZ.fg1,
        }}
      >
        9:41
      </div>
      <div style={{ width: 126, height: 0 }} />
      <div style={{ display: "flex", gap: 6, alignItems: "center" }}>
        <svg width="17" height="11" viewBox="0 0 19 12">
          <rect x="0" y="7.5" width="3.2" height="4.5" rx="0.7" fill={RZ.fg1} />
          <rect x="4.8" y="5" width="3.2" height="7" rx="0.7" fill={RZ.fg1} />
          <rect
            x="9.6"
            y="2.5"
            width="3.2"
            height="9.5"
            rx="0.7"
            fill={RZ.fg1}
          />
          <rect x="14.4" y="0" width="3.2" height="12" rx="0.7" fill={RZ.fg1} />
        </svg>
        <svg width="15" height="11" viewBox="0 0 17 12">
          <path
            d="M8.5 3.2C10.8 3.2 12.9 4.1 14.4 5.6L15.5 4.5C13.7 2.7 11.2 1.5 8.5 1.5C5.8 1.5 3.3 2.7 1.5 4.5L2.6 5.6C4.1 4.1 6.2 3.2 8.5 3.2Z"
            fill={RZ.fg1}
          />
          <path
            d="M8.5 6.8C9.9 6.8 11.1 7.3 12 8.2L13.1 7.1C11.8 5.9 10.2 5.1 8.5 5.1C6.8 5.1 5.2 5.9 3.9 7.1L5 8.2C5.9 7.3 7.1 6.8 8.5 6.8Z"
            fill={RZ.fg1}
          />
          <circle cx="8.5" cy="10.5" r="1.5" fill={RZ.fg1} />
        </svg>
        <svg width="25" height="12" viewBox="0 0 27 13">
          <rect
            x="0.5"
            y="0.5"
            width="23"
            height="12"
            rx="3.5"
            stroke={RZ.fg1}
            strokeOpacity="0.4"
            fill="none"
          />
          <rect x="2" y="2" width="20" height="9" rx="2" fill={RZ.fg1} />
          <path
            d="M25 4.5V8.5C25.8 8.2 26.5 7.2 26.5 6.5C26.5 5.8 25.8 4.8 25 4.5Z"
            fill={RZ.fg1}
            fillOpacity="0.4"
          />
        </svg>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Top bar — back + title + trailing
// ─────────────────────────────────────────────────────────────
function TopBar({ onBack, title, trailing, subtitle }) {
  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
        padding: "8px 16px 12px",
        position: "relative",
        zIndex: 10,
      }}
    >
      {onBack ? (
        <div
          onClick={onBack}
          style={{
            width: 36,
            height: 36,
            borderRadius: "50%",
            background: "rgba(255,255,255,0.6)",
            backdropFilter: "blur(16px)",
            border: "1px solid rgba(255,255,255,0.7)",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            cursor: "pointer",
            flexShrink: 0,
          }}
        >
          <Icon name="back" size={18} />
        </div>
      ) : (
        <div style={{ width: 36 }} />
      )}
      <div
        style={{ textAlign: "center", flex: 1, minWidth: 0, padding: "0 8px" }}
      >
        {title && (
          <div
            style={{
              fontSize: 15,
              fontWeight: 600,
              color: RZ.fg1,
              letterSpacing: -0.2,
            }}
          >
            {title}
          </div>
        )}
        {subtitle && (
          <div style={{ fontSize: 11, color: RZ.fg3, marginTop: 2 }}>
            {subtitle}
          </div>
        )}
      </div>
      <div style={{ width: 36, display: "flex", justifyContent: "flex-end" }}>
        {trailing || <div style={{ width: 36 }} />}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Buttons
// ─────────────────────────────────────────────────────────────
function PrimaryBtn({ children, onClick, style = {}, disabled }) {
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      style={{
        background: RZ.black,
        color: RZ.white,
        border: "none",
        borderRadius: 14,
        padding: "15px 20px",
        width: "100%",
        fontFamily: "Inter",
        fontSize: 15,
        fontWeight: 600,
        letterSpacing: -0.1,
        cursor: disabled ? "default" : "pointer",
        opacity: disabled ? 0.4 : 1,
        boxShadow: "0 6px 20px -8px rgba(24,20,15,0.35)",
        ...style,
      }}
    >
      {children}
    </button>
  );
}
function SecondaryBtn({ children, onClick, style = {} }) {
  return (
    <button
      onClick={onClick}
      style={{
        background: "rgba(255,255,255,0.60)",
        backdropFilter: "blur(16px)",
        WebkitBackdropFilter: "blur(16px)",
        color: RZ.fg1,
        border: "1px solid rgba(255,255,255,0.80)",
        borderRadius: 14,
        padding: "14px 20px",
        width: "100%",
        fontFamily: "Inter",
        fontSize: 14,
        fontWeight: 500,
        cursor: "pointer",
        ...style,
      }}
    >
      {children}
    </button>
  );
}
function GhostBtn({ children, onClick, style = {} }) {
  return (
    <button
      onClick={onClick}
      style={{
        background: "transparent",
        color: RZ.fg2,
        border: "none",
        padding: "12px 16px",
        fontFamily: "Inter",
        fontSize: 13,
        fontWeight: 500,
        cursor: "pointer",
        ...style,
      }}
    >
      {children}
    </button>
  );
}
function Chip({ children, on, onClick, icon, style = {} }) {
  return (
    <button
      onClick={onClick}
      style={{
        display: "inline-flex",
        alignItems: "center",
        gap: 6,
        padding: icon ? "8px 13px 8px 10px" : "8px 14px",
        borderRadius: 20,
        border: on ? "none" : "1px solid rgba(24,20,15,0.10)",
        background: on ? RZ.black : "rgba(255,255,255,0.60)",
        color: on ? RZ.white : RZ.fg1,
        fontFamily: "Inter",
        fontSize: 12,
        fontWeight: 500,
        cursor: "pointer",
        letterSpacing: -0.1,
        ...style,
      }}
    >
      {icon && <Icon name={icon} size={14} color={on ? RZ.white : RZ.fg1} />}
      {children}
    </button>
  );
}

// ─────────────────────────────────────────────────────────────
// State badge
// ─────────────────────────────────────────────────────────────
const STATE_META = {
  resonance: {
    label: "Harmonie",
    color: RZ.mauve,
    bg: "rgba(138,91,149,0.14)",
    text: "#6A3F75",
    tag: "result-resonance",
  },
  equilibre: {
    label: "Équilibre fragile",
    color: "#4F8BCF",
    bg: "rgba(23,114,254,0.14)",
    text: "#0F4FB8",
    tag: "result-equilibre",
  },
  tension: {
    label: "Tension",
    color: "#C66A3A",
    bg: "rgba(252,68,44,0.12)",
    text: "#A83E1E",
    tag: "result-tension",
  },
  dissonance: {
    label: "Dissonance",
    color: RZ.orange,
    bg: "rgba(252,68,44,0.16)",
    text: "#A82E1A",
    tag: "result-dissonance",
  },
  alerte: {
    label: "Alerte récupération",
    color: RZ.orange,
    bg: "rgba(252,68,44,0.22)",
    text: "#8A1E0A",
    tag: "result-alerte",
  },
};

function StateBadge({ state, size = "sm" }) {
  const m = STATE_META[state];
  if (!m) return null;
  const pad = size === "lg" ? "6px 14px" : "3px 9px";
  const fs = size === "lg" ? 12 : 10;
  return (
    <span
      style={{
        background: m.bg,
        color: m.text,
        padding: pad,
        borderRadius: 20,
        fontSize: fs,
        fontWeight: 600,
        letterSpacing: 0.2,
        display: "inline-flex",
        alignItems: "center",
        gap: 6,
      }}
    >
      <span
        style={{
          width: 6,
          height: 6,
          borderRadius: "50%",
          background: m.color,
          display: "inline-block",
        }}
      />
      {m.label}
    </span>
  );
}

// ─────────────────────────────────────────────────────────────
// Nuance visualisation — two orbs in a configuration
// ─────────────────────────────────────────────────────────────
function NuanceViz({ state = "resonance", size = 240, animated = true }) {
  // configurations: left orange, right blue, with offsets per state
  const cfg =
    {
      resonance: {
        ox: 0.5,
        oy: 0.5,
        bx: 0.5,
        by: 0.5,
        orbS: 0.42,
        blur: 0.18,
        mauve: 0.55,
      },
      equilibre: {
        ox: 0.38,
        oy: 0.48,
        bx: 0.56,
        by: 0.52,
        orbS: 0.38,
        blur: 0.12,
        mauve: 0.22,
      },
      tension: {
        ox: 0.28,
        oy: 0.44,
        bx: 0.68,
        by: 0.56,
        orbS: 0.34,
        blur: 0.1,
        mauve: 0.0,
      },
      dissonance: {
        ox: 0.14,
        oy: 0.4,
        bx: 0.84,
        by: 0.62,
        orbS: 0.32,
        blur: 0.08,
        mauve: 0.0,
      },
      alerte: {
        ox: 0.5,
        oy: 0.5,
        bx: -0.5,
        by: 0.5,
        orbS: 0.56,
        blur: 0.22,
        mauve: 0.0,
      },
    }[state] || {};
  const s = size;
  const blur = s * cfg.blur;
  const orbS = s * cfg.orbS;
  return (
    <div
      style={{
        position: "relative",
        width: s,
        height: s,
        animation: animated ? "rz-breath 6s ease-in-out infinite" : "none",
      }}
    >
      <div
        className={animated ? "rz-drift" : ""}
        style={{
          position: "absolute",
          width: orbS,
          height: orbS,
          borderRadius: "50%",
          left: `${cfg.ox * 100}%`,
          top: `${cfg.oy * 100}%`,
          transform: "translate(-50%,-50%)",
          background: `radial-gradient(closest-side, ${RZ.orange} 0%, ${RZ.orange}00 70%)`,
          filter: `blur(${blur}px)`,
          opacity: state === "alerte" ? 0.95 : 0.85,
        }}
      />
      {state !== "alerte" && (
        <div
          className={animated ? "rz-drift" : ""}
          style={{
            position: "absolute",
            width: orbS,
            height: orbS,
            borderRadius: "50%",
            left: `${cfg.bx * 100}%`,
            top: `${cfg.by * 100}%`,
            transform: "translate(-50%,-50%)",
            background: `radial-gradient(closest-side, ${RZ.blue} 0%, ${RZ.blue}00 70%)`,
            filter: `blur(${blur}px)`,
            opacity: 0.85,
            animationDelay: "1.5s",
          }}
        />
      )}
      {cfg.mauve > 0 && (
        <div
          style={{
            position: "absolute",
            width: orbS * 0.8,
            height: orbS * 0.8,
            borderRadius: "50%",
            left: "50%",
            top: "50%",
            transform: "translate(-50%,-50%)",
            background: `radial-gradient(closest-side, ${RZ.mauve} 0%, ${RZ.mauve}00 70%)`,
            filter: `blur(${blur}px)`,
            opacity: cfg.mauve,
            mixBlendMode: "multiply",
          }}
        />
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Notification toast (iOS style)
// ─────────────────────────────────────────────────────────────
function Toast({ children, icon = "sparkle", onDismiss }) {
  return (
    <div
      style={{
        position: "absolute",
        top: 58,
        left: 12,
        right: 12,
        zIndex: 80,
        background: "rgba(250,248,245,0.86)",
        backdropFilter: "blur(24px) saturate(180%)",
        WebkitBackdropFilter: "blur(24px) saturate(180%)",
        border: "1px solid rgba(255,255,255,0.8)",
        borderRadius: 20,
        padding: "12px 14px",
        display: "flex",
        alignItems: "center",
        gap: 12,
        boxShadow: "0 16px 40px -12px rgba(24,20,15,0.18)",
        animation: "rz-slide-down 400ms cubic-bezier(0.4,0,0.2,1)",
      }}
    >
      <div
        style={{
          width: 34,
          height: 34,
          borderRadius: 9,
          background: RZ.black,
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          flexShrink: 0,
        }}
      >
        <Icon name={icon} size={18} color={RZ.white} />
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>{children}</div>
      {onDismiss && (
        <div onClick={onDismiss} style={{ padding: 4, cursor: "pointer" }}>
          <Icon name="close" size={14} color={RZ.fg3} />
        </div>
      )}
    </div>
  );
}

Object.assign(window, {
  RZ,
  OrbBackground,
  Glass,
  Icon,
  TabBar,
  StatusBar,
  TopBar,
  PrimaryBtn,
  SecondaryBtn,
  GhostBtn,
  Chip,
  StateBadge,
  NuanceViz,
  Toast,
  STATE_META,
});
