/* global React, UI */
const { useState } = React;

// ====================================================================
// Shared mobile components
// ====================================================================

// Mobile screen scaffold — fits the 420×880 mobile artboard
function MScreen({ children, footer }) {
  return (
    <div style={{ width: "100%", height: "100%", background: "var(--bg)", color: "var(--text)", display: "flex", flexDirection: "column", overflow: "hidden", position: "relative" }}>
      <div style={{ flex: 1, overflow: "auto", scrollbarWidth: "none" }} className="screen">
        {children}
      </div>
      {footer && <div style={{ flex: "0 0 auto" }}>{footer}</div>}
    </div>
  );
}

// Mobile top bar — back left, slot right
function MTopBar({ onBack = () => {}, right, label, sticky }) {
  const { IconBack } = window.UI;
  return (
    <div style={{ position: sticky ? "sticky" : "relative", top: 0, zIndex: 5, display: "flex", alignItems: "center", justifyContent: "space-between", padding: "16px 20px 12px", background: "var(--bg)" }}>
      <button onClick={onBack} style={{ width: 36, height: 36, borderRadius: 18, background: "transparent", border: "none", display: "grid", placeItems: "center", color: "var(--text-2)", cursor: "pointer", padding: 0 }}>
        <IconBack size={20}/>
      </button>
      {label && <div className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", color: "var(--text-3)", textTransform: "uppercase" }}>{label}</div>}
      <div style={{ display: "flex", gap: 4 }}>{right || <div style={{ width: 36 }}/>}</div>
    </div>
  );
}

// Tappable list row
function MRow({ children, onClick, last }) {
  return (
    <div onClick={onClick} style={{ padding: "14px 20px", borderBottom: last ? "none" : "1px solid var(--border-soft)", display: "flex", alignItems: "center", gap: 12, cursor: onClick ? "pointer" : "default" }}>
      {children}
    </div>
  );
}

// Member avatar — small initials block
function MiniAvatar({ initials, size = 40, self, accent }) {
  return (
    <div style={{
      width: size, height: size, flex: `0 0 ${size}px`,
      borderRadius: 10,
      background: self || accent ? "var(--accent-soft)" : "var(--surface-2)",
      border: "1px solid " + (self || accent ? "var(--accent)" : "var(--border)"),
      display: "grid", placeItems: "center",
      fontFamily: "var(--mono)", fontSize: size > 40 ? 13 : 11,
      color: self || accent ? "var(--accent)" : "var(--text-2)",
    }}>{initials}</div>
  );
}

// Sage progress bar
function SageBar({ value, total = 1, height = 4 }) {
  const pct = Math.max(0, Math.min(100, (value / total) * 100));
  return (
    <div style={{ width: "100%", height, borderRadius: height/2, background: "var(--surface-3)", overflow: "hidden" }}>
      <div style={{ width: `${pct}%`, height: "100%", background: "var(--accent)" }}/>
    </div>
  );
}

// Sticky bottom CTA wrapper
function MStickyCTA({ children, hint }) {
  return (
    <div style={{ borderTop: "1px solid var(--border-soft)", padding: "14px 20px 22px", background: "var(--bg)" }}>
      {hint && <div className="mono" style={{ fontSize: 10, letterSpacing: "0.12em", color: "var(--text-3)", textAlign: "center", marginBottom: 10 }}>{hint}</div>}
      {children}
    </div>
  );
}

// Mono eyebrow
function Eyebrow({ children, accent }) {
  return <div className="mono" style={{ fontSize: 10.5, letterSpacing: "0.14em", color: accent ? "var(--accent)" : "var(--text-3)", textTransform: "uppercase" }}>{children}</div>;
}

// Section header — serif italic accent
function MSectionH({ children, right, style }) {
  return (
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", margin: "0 0 14px", ...style }}>
      <h3 style={{ font: "italic 400 22px/1 var(--serif)", color: "var(--accent)", letterSpacing: "-0.005em", margin: 0 }}>{children}</h3>
      {right && <span className="mono" style={{ fontSize: 10.5, letterSpacing: "0.12em", color: "var(--text-3)" }}>{right}</span>}
    </div>
  );
}

// Sparkline (mobile-sized)
function MiniSpark({ data, w = 80, h = 18, color = "var(--text-2)" }) {
  const max = Math.max(...data), min = Math.min(...data);
  const pts = data.map((v, i) => `${(i/(data.length-1))*w},${h - ((v-min)/(max-min||1))*h}`).join(" ");
  return (
    <svg width={w} height={h} style={{ display: "block" }}>
      <polyline fill="none" stroke={color} strokeWidth="1" points={pts}/>
    </svg>
  );
}

// Perforated divider
function Perf({ margin = "20px 0" }) {
  return <div className="perf" style={{ margin }}/>;
}

// Sage pill-button
function SagePill({ children, onClick, ghost, fullWidth, height = 48, fontSize = 14 }) {
  return (
    <button onClick={onClick} style={{
      height, padding: fullWidth ? 0 : "0 22px",
      width: fullWidth ? "100%" : "auto",
      borderRadius: 999,
      border: ghost ? "1px solid var(--border)" : "none",
      background: ghost ? "transparent" : "var(--accent)",
      color: ghost ? "var(--text)" : "#0E0D0B",
      fontFamily: "var(--sans)", fontSize, fontWeight: ghost ? 500 : 600,
      cursor: "pointer",
      display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8,
    }}>{children}</button>
  );
}

// Glyph icons used in notifications + rhythm — drawn as SVG, never emoji
const Glyph = {
  PaperPlane: ({ size = 16, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 11l18-8-7 18-3-7-8-3z"/>
      <path d="M11 13l10-10"/>
    </svg>
  ),
  Check: ({ size = 16, color = "var(--accent)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9.5" fill="none"/>
      <path d="M8 12.5l3 3 5-6"/>
    </svg>
  ),
  Dot: ({ size = 16, color = "var(--text-3)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4">
      <circle cx="12" cy="12" r="9"/>
      <circle cx="12" cy="12" r="2.5" fill={color}/>
    </svg>
  ),
  ArrowRet: ({ size = 16, color = "var(--accent)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M14 6l-6 6 6 6"/>
      <path d="M8 12h13"/>
    </svg>
  ),
  Wave: ({ size = 16, color = "var(--accent)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M2 12c2 0 2-3 4-3s2 6 4 6 2-9 4-9 2 6 4 6 2-3 4-3"/>
    </svg>
  ),
  Pending: ({ size = 16, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9"/>
      <path d="M12 7v5l3 2"/>
    </svg>
  ),
  Share: ({ size = 18, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 3v13"/>
      <path d="M7 8l5-5 5 5"/>
      <path d="M5 14v5a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-5"/>
    </svg>
  ),
  Cog: ({ size = 18, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="3"/>
      <path d="M19 12a7 7 0 0 0-.1-1.2l2-1.5-2-3.4-2.3.9a7 7 0 0 0-2.1-1.2L14 3h-4l-.5 2.6a7 7 0 0 0-2.1 1.2L5.1 5.9 3.1 9.3l2 1.5A7 7 0 0 0 5 12a7 7 0 0 0 .1 1.2l-2 1.5 2 3.4 2.3-.9a7 7 0 0 0 2.1 1.2L10 21h4l.5-2.6a7 7 0 0 0 2.1-1.2l2.3.9 2-3.4-2-1.5c.07-.4.1-.8.1-1.2z"/>
    </svg>
  ),
  Search: ({ size = 18, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="11" r="7"/>
      <path d="M20 20l-3.5-3.5"/>
    </svg>
  ),
  Bell: ({ size = 18, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 8a6 6 0 1 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/>
      <path d="M10 21a2 2 0 0 0 4 0"/>
    </svg>
  ),
  Filter: ({ size = 16, color = "var(--text-2)" }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 5h18M6 12h12M10 19h4"/>
    </svg>
  ),
};

window.M = { MScreen, MTopBar, MRow, MiniAvatar, SageBar, MStickyCTA, Eyebrow, MSectionH, MiniSpark, Perf, SagePill, Glyph };
