/* scope guard: file-scoped IIFE — see index.html */
(function () {
/* Stoic+ — shared icons & primitives.
   Lucide line icons inlined as React components (stroke style, ~2px,
   rounded caps). Every visual value comes from colors_and_type.css —
   no raw palette hex, no magic spacing. Exported to `window` so the
   screen files (loaded after this one) can pull them off the global. */

const I = ({ d, size = 22, sw = 2, children, fill = "none" }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={fill} stroke="currentColor"
    strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    {d ? <path d={d} /> : children}
  </svg>
);

const Icons = {
  Check: (p) => <I {...p}><polyline points="20 6 9 17 4 12" /></I>,
  AtSign: (p) => <I {...p}><circle cx="12" cy="12" r="4" /><path d="M16 8v5a3 3 0 0 0 6 0v-1a10 10 0 1 0-3.92 7.94" /></I>,
  Flame: (p) => <I {...p} d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z" />,
  ChevronRight: (p) => <I {...p}><polyline points="9 18 15 12 9 6" /></I>,
  ArrowRight: (p) => <I {...p}><line x1="5" y1="12" x2="19" y2="12" /><polyline points="12 5 19 12 12 19" /></I>,
  ArrowLeft: (p) => <I {...p}><line x1="19" y1="12" x2="5" y2="12" /><polyline points="12 19 5 12 12 5" /></I>,
  Crown: (p) => <I {...p} d="M11.562 3.266a.5.5 0 0 1 .876 0L15.39 8.87a1 1 0 0 0 1.516.294L21.183 5.5a.5.5 0 0 1 .798.519l-2.834 10.246a1 1 0 0 1-.956.734H5.81a1 1 0 0 1-.957-.734L2.02 6.02a.5.5 0 0 1 .798-.519l4.276 3.664a1 1 0 0 0 1.516-.294z" />,
  Home: (p) => <I {...p}><path d="M3 9.5 12 3l9 6.5" /><path d="M5 10v10a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V10" /></I>,
  Trend: (p) => <I {...p}><polyline points="22 7 13.5 15.5 8.5 10.5 2 17" /><polyline points="16 7 22 7 22 13" /></I>,
  Message: (p) => <I {...p} d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8z" />,
  Settings: (p) => <I {...p}><circle cx="12" cy="12" r="3" /><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" /></I>,
  ChevronDown: (p) => <I {...p}><polyline points="6 9 12 15 18 9" /></I>,
  Zap: (p) => <I {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2" /></I>,
  Moon: (p) => <I {...p} d="M12 3a6.36 6.36 0 0 0 9 9 9 9 0 1 1-9-9z" />,
  Target: (p) => <I {...p}><circle cx="12" cy="12" r="10" /><circle cx="12" cy="12" r="6" /><circle cx="12" cy="12" r="2" /></I>,
  Smile: (p) => <I {...p}><circle cx="12" cy="12" r="10" /><path d="M8 14s1.5 2 4 2 4-2 4-2" /><line x1="9" y1="9" x2="9.01" y2="9" /><line x1="15" y1="9" x2="15.01" y2="9" /></I>,
  Shield: (p) => <I {...p} d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z" />,
  X: (p) => <I {...p}><line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" /></I>,
  Pencil: (p) => <I {...p}><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5z" /><path d="m15 5 4 4" /></I>,
  CloudOff: (p) => <I {...p}><path d="m2 2 20 20" /><path d="M5.78 5.78A7 7 0 0 0 9 19h8.5a4.5 4.5 0 0 0 1.3-.19" /><path d="M21.53 16.5A4.5 4.5 0 0 0 17.5 10h-1.79A7 7 0 0 0 10 5.07" /></I>,
  Heart: (p) => <I {...p} d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7z" />,
  Download: (p) => <I {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><polyline points="7 10 12 15 17 10" /><line x1="12" y1="15" x2="12" y2="3" /></I>,
  Lock: (p) => <I {...p}><rect x="3" y="11" width="18" height="11" rx="2" /><path d="M7 11V7a5 5 0 0 1 10 0v4" /></I>,
  LogOut: (p) => <I {...p}><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" /><polyline points="16 17 21 12 16 7" /><line x1="21" y1="12" x2="9" y2="12" /></I>,
  Refresh: (p) => <I {...p}><path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /><path d="M3 3v5h5" /><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16" /><path d="M21 21v-5h-5" /></I>,
  Card: (p) => <I {...p}><rect x="2" y="5" width="20" height="14" rx="2" /><line x1="2" y1="10" x2="22" y2="10" /></I>,
  Sliders: (p) => <I {...p}><line x1="4" y1="21" x2="4" y2="14" /><line x1="4" y1="10" x2="4" y2="3" /><line x1="12" y1="21" x2="12" y2="12" /><line x1="12" y1="8" x2="12" y2="3" /><line x1="20" y1="21" x2="20" y2="16" /><line x1="20" y1="12" x2="20" y2="3" /><line x1="1" y1="14" x2="7" y2="14" /><line x1="9" y1="8" x2="15" y2="8" /><line x1="17" y1="16" x2="23" y2="16" /></I>,
  Snow: (p) => <I {...p}><line x1="2" y1="12" x2="22" y2="12" /><line x1="12" y1="2" x2="12" y2="22" /><path d="m20 16-4-4 4-4" /><path d="m4 8 4 4-4 4" /><path d="m16 4-4 4-4-4" /><path d="m8 20 4-4 4 4" /></I>,
  ShieldCheck: (p) => <I {...p}><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z" /><path d="m9 12 2 2 4-4" /></I>,
  Beaker: (p) => <I {...p}><path d="M10 2v7.31" /><path d="M14 9.3V1.99" /><path d="M8.5 2h7" /><path d="M14 9.3a6.5 6.5 0 1 1-4 0" /><path d="M5.58 16.5h12.85" /></I>,
};

/* ---- Primitives ---- */

const SectionLabel = ({ children, style }) => (
  <div style={{ fontSize: 12, fontWeight: 600, letterSpacing: "0.02em", textTransform: "uppercase", color: "var(--color-text-muted)", ...style }}>{children}</div>
);

const ScoreNumber = ({ value, size = 72 }) => (
  <div style={{ fontSize: size, fontWeight: 700, letterSpacing: "-0.03em", color: "var(--color-text)", lineHeight: 1, fontVariantNumeric: "tabular-nums" }}>{value}</div>
);

const CheckBadge = ({ size = 64 }) => (
  <div className="glass glass-lit glass-amber check-badge" style={{ width: size, height: size, borderRadius: 9999, display: "flex", alignItems: "center", justifyContent: "center", color: "var(--color-amber)" }}>
    <svg width={size * 0.5} height={size * 0.5} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round">
      <polyline className="check-draw" points="20 6 9 17 4 12" />
    </svg>
  </div>
);

const StreakPill = ({ count = 12 }) => (
  <div className="glass glass-lit" style={{ display: "inline-flex", alignItems: "center", gap: 7, borderRadius: 9999, padding: "8px 14px" }}>
    <span style={{ color: "var(--color-amber)", display: "flex" }}><Icons.Flame size={17} /></span>
    <span style={{ color: "var(--color-amber)", fontWeight: 700, fontSize: 16 }}>{count}</span>
    <span style={{ color: "var(--color-text-muted)", fontSize: 14, fontWeight: 500 }}>day streak</span>
  </div>
);

const PrimaryButton = ({ children, onClick, icon = true }) => (
  <button onClick={onClick} className="press" style={{ width: "100%", display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8, background: "var(--grad-amber)", color: "#1a1206", border: "none", borderRadius: 9999, padding: "16px 22px", fontSize: 17, fontWeight: 700, fontFamily: "var(--font-sans)", cursor: "pointer", minHeight: 44 }}>
    {children}{icon && <Icons.ArrowRight size={19} sw={2.4} />}
  </button>
);

/* Glass CTA — amber-tinted liquid glass, the futuristic primary action */
const GlassButton = ({ children, onClick, icon = true }) => (
  <button onClick={onClick} className="glass glass-lit glass-amber press" style={{ width: "100%", display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8, borderRadius: 9999, padding: "17px 22px", fontSize: 17, fontWeight: 700, fontFamily: "var(--font-sans)", color: "var(--color-amber)", cursor: "pointer", minHeight: 44 }}>
    {children}{icon && <Icons.ArrowRight size={19} sw={2.4} />}
  </button>
);

const GhostButton = ({ children, onClick }) => (
  <button onClick={onClick} className="press" style={{ width: "100%", background: "var(--color-surface-2)", color: "var(--color-text)", border: "1px solid var(--color-border)", borderRadius: 9999, padding: "15px 22px", fontSize: 16, fontWeight: 600, fontFamily: "var(--font-sans)", cursor: "pointer", minHeight: 44 }}>{children}</button>
);

const LinkButton = ({ children, onClick }) => (
  <button onClick={onClick} style={{ background: "none", border: "none", color: "var(--color-amber)", fontSize: 15, fontWeight: 600, fontFamily: "var(--font-sans)", cursor: "pointer", padding: 6 }}>{children}</button>
);

const Card = ({ children, style, surface = 1, glass = false, lit = true, onClick }) => {
  if (glass) {
    return (
      <div onClick={onClick} className={"glass " + (lit ? "glass-lit " : "") + (onClick ? "press" : "")}
        style={{ borderRadius: 20, padding: 18, cursor: onClick ? "pointer" : "default", ...style }}>{children}</div>
    );
  }
  const bg = surface === 2 ? "var(--color-surface-2)" : surface === 3 ? "var(--color-surface-3)" : "var(--color-surface-1)";
  return (
    <div onClick={onClick} className={onClick ? "press" : ""} style={{ background: bg, border: "1px solid var(--color-border)", borderRadius: 20, padding: 18, cursor: onClick ? "pointer" : "default", ...style }}>{children}</div>
  );
};

/* ---- Liquid-glass bottom sheet / modal ----
   A blurred scrim + a glass panel rising from the bottom.
   open: bool · onClose: fn · children: sheet body */
const Sheet = ({ open, onClose, title, children }) => {
  if (!open) return null;
  return (
    <div style={{ position: "absolute", inset: 0, zIndex: 40, display: "flex", flexDirection: "column", justifyContent: "flex-end" }}>
      <div className="glass-scrim sheet-scrim" onClick={onClose} style={{ position: "absolute", inset: 0 }} />
      <div className="glass glass-lit glass-strong sheet-panel" style={{ position: "relative", borderRadius: "28px 28px 0 0", borderBottom: "none", padding: "12px 20px calc(26px + env(safe-area-inset-bottom))" }}>
        <div style={{ width: 38, height: 5, borderRadius: 9999, background: "rgba(255,255,255,0.25)", margin: "0 auto 16px" }} />
        {title && <div style={{ fontSize: 20, fontWeight: 600, letterSpacing: "-0.01em", color: "var(--color-text)", marginBottom: 14 }}>{title}</div>}
        {children}
      </div>
    </div>
  );
};

const StepProgress = ({ total, current }) => (
  <div style={{ display: "flex", gap: 7, flex: 1 }}>
    {Array.from({ length: total }).map((_, i) => (
      <div key={i} className="glass-lit" style={{ position: "relative", height: 8, borderRadius: 9999, flex: 1, background: i < current ? "linear-gradient(150deg, rgba(224,214,150,0.7), rgba(187,176,101,0.45))" : "rgba(255,255,255,0.07)", boxShadow: i < current ? "inset 0 1px 0 rgba(255,255,255,0.4)" : "inset 0 1px 0 rgba(255,255,255,0.12)" }} />
    ))}
  </div>
);

/* ============================================================
   Shared model — the 5 tracked dimensions + Live Monitor math.
   Monitor = weighted average of dimensions (1–10), shown as /10.
   Weights are specified in the handoff (not invented).
   ============================================================ */
const DIMENSIONS = [
  { key: "energy", label: "Energy",        icon: "Zap",    weight: 0.25 },
  { key: "sleep",  label: "Sleep Quality", icon: "Moon",   weight: 0.25 },
  { key: "focus",  label: "Focus",         icon: "Target", weight: 0.20 },
  { key: "mood",   label: "Mood",          icon: "Smile",  weight: 0.15 },
  { key: "drive",  label: "Drive",         icon: "Flame",  weight: 0.15 },
];
const DEFAULT_VALUES = { energy: 7, sleep: 6, focus: 8, mood: 7, drive: 6 };
const monitorOf = (v) => DIMENSIONS.reduce((a, d) => a + (v[d.key] || 0) * d.weight, 0);
function feelingFor(c) {
  if (c >= 8) return "Sharp";
  if (c >= 6.5) return "Steady";
  if (c >= 5) return "Even";
  if (c >= 3.5) return "Worn";
  return "Drained";
}

/* Read-only dimension row (logged Today, day detail).
   Hand-rolled glass card — a single inset-ring edge, no CSS border and
   no ::before, so there is exactly ONE edge (no double outline). This is
   the single-edge rule called out in the handoff. */
const ReadOnlyMetric = ({ d, value }) => {
  const Icon = Icons[d.icon];
  const pct = ((value - 1) / 9) * 100;
  return (
    <div style={{
      borderRadius: 16, padding: "13px 15px",
      background: "var(--glass-fill)",
      WebkitBackdropFilter: "blur(28px) saturate(150%)",
      backdropFilter: "blur(28px) saturate(150%)",
      border: "none",
      boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.15), inset 0 1px 0 0 rgba(255,255,255,0.28), 0 12px 30px -16px rgba(0,0,0,0.7)"
    }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 10 }}>
        <span style={{ display: "inline-flex", alignItems: "center", gap: 9, fontSize: 15, fontWeight: 600, color: "var(--color-text)" }}>
          <span style={{ color: "var(--color-text-muted)", display: "flex" }}><Icon size={18} /></span>{d.label}
        </span>
        <span style={{ color: "var(--color-amber)", fontSize: 17, fontWeight: 700, fontVariantNumeric: "tabular-nums" }}>{value}</span>
      </div>
      <div style={{ height: 8, borderRadius: 9999, background: "rgba(255,255,255,0.06)", boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.08)" }}>
        <div style={{ width: pct + "%", height: "100%", borderRadius: 9999, background: "linear-gradient(150deg, rgba(224,214,150,0.55), rgba(187,176,101,0.32) 60%, rgba(200,189,118,0.42))", WebkitBackdropFilter: "blur(6px) saturate(150%)", backdropFilter: "blur(6px) saturate(150%)", boxShadow: "inset 0 1px 0 rgba(255,255,255,0.45), inset 0 0 0 1px rgba(224,214,150,0.4)" }} />
      </div>
    </div>
  );
};

/* Inline banner (offline / system notices) */
const Banner = ({ icon, children, onAction, actionLabel }) => {
  const Icon = icon ? Icons[icon] : null;
  return (
    <div className="glass glass-lit" style={{ display: "flex", alignItems: "center", gap: 11, borderRadius: 14, padding: "11px 14px", marginBottom: 16 }}>
      {Icon && <span style={{ color: "var(--color-text-muted)", display: "flex", flex: "none" }}><Icon size={17} /></span>}
      <span style={{ flex: 1, fontSize: 13, fontWeight: 500, color: "var(--color-text-muted)", lineHeight: 1.4 }}>{children}</span>
      {onAction && <button onClick={onAction} className="press" style={{ background: "none", border: "none", color: "var(--color-amber)", fontSize: 13, fontWeight: 600, fontFamily: "var(--font-sans)", cursor: "pointer", flex: "none" }}>{actionLabel}</button>}
    </div>
  );
};

/* Settings list row */
const ListRow = ({ icon, label, value, onClick, danger, last }) => {
  const Icon = icon ? Icons[icon] : null;
  return (
    <button onClick={onClick} className="press" style={{ width: "100%", display: "flex", alignItems: "center", gap: 13, background: "none", border: "none", borderBottom: last ? "none" : "1px solid var(--color-border)", padding: "15px 4px", cursor: "pointer", fontFamily: "var(--font-sans)", textAlign: "left", minHeight: 44 }}>
      {Icon && <span style={{ color: danger ? "#E5736B" : "var(--color-text-muted)", display: "flex", flex: "none" }}><Icon size={19} /></span>}
      <span style={{ flex: 1, fontSize: 16, fontWeight: 500, color: danger ? "#E5736B" : "var(--color-text)" }}>{label}</span>
      {value && <span style={{ fontSize: 14, color: "var(--color-text-faint)", fontWeight: 500 }}>{value}</span>}
      <span style={{ color: "var(--color-text-faint)", display: "flex", flex: "none" }}><Icons.ChevronRight size={18} sw={2} /></span>
    </button>
  );
};

/* Toggle switch */
const Toggle = ({ on, onChange }) => (
  <button onClick={() => onChange(!on)} className="press" aria-pressed={on} style={{ width: 46, height: 28, borderRadius: 9999, border: "none", cursor: "pointer", padding: 3, background: on ? "var(--grad-amber)" : "var(--color-surface-3)", display: "flex", justifyContent: on ? "flex-end" : "flex-start", transition: "background var(--dur-fast)", flex: "none" }}>
    <span style={{ width: 22, height: 22, borderRadius: 9999, background: "#fff", boxShadow: "0 1px 3px rgba(0,0,0,0.5)" }} />
  </button>
);

/* Shared screen top bar: journey day (left) + streak pill (right) */
const TopBar = ({ day, streak }) => (
  <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12, marginBottom: 20 }}>
    <span style={{ fontSize: 14, fontWeight: 500, color: "var(--color-text-muted)", whiteSpace: "nowrap" }}>{day ? `Day ${day} of your journey` : "Welcome to Stoic+"}</span>
    {streak != null && (streak > 0
      ? <StreakPill count={streak} />
      : <span className="glass glass-lit" style={{ display: "inline-flex", alignItems: "center", gap: 7, borderRadius: 9999, padding: "8px 14px", color: "var(--color-text-muted)", fontSize: 14, fontWeight: 500 }}><Icons.Flame size={16} />No streak yet</span>)}
  </div>
);

Object.assign(window, { Icons, SectionLabel, ScoreNumber, CheckBadge, StreakPill, PrimaryButton, GlassButton, GhostButton, LinkButton, Card, Sheet, StepProgress, DIMENSIONS, DEFAULT_VALUES, monitorOf, feelingFor, ReadOnlyMetric, Banner, ListRow, Toggle, TopBar });
})();
