/* global React, TOKENS, FONTS, DarkCard, CircleCheck, Ring, Icon, hexA, usePersona, personaStats */
// Eighty4 · macOS Today cockpit + habit card, persona-driven.

function MacWindow({ children, width = 1300, height = 840 }) {
  const T = TOKENS;
  return (
    <div style={{ width, height, borderRadius: 16, overflow: "hidden", background: T.bg, border: `1px solid ${T.hairStrong}`, boxShadow: T.shadowLg, display: "flex", fontFamily: FONTS.sans }}>
      {children}
    </div>
  );
}
const mono = (s = {}) => ({ fontFamily: FONTS.mono, fontSize: 11.5, fontWeight: 600, letterSpacing: 0.5, textTransform: "uppercase", color: TOKENS.textMuted, ...s });

function NavItem({ icon, label, kbd, active }) {
  const T = TOKENS;
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "8px 10px", borderRadius: 9, marginBottom: 2, background: active ? T.accent : "transparent", boxShadow: active ? `0 2px 8px -2px ${hexA(T.accent, 0.5)}` : "none" }}>
      <Icon name={icon} size={16} color={active ? "#fff" : T.textDim} />
      <span style={{ flex: 1, fontSize: 14, fontWeight: active ? 600 : 500, color: active ? "#fff" : T.textDim, letterSpacing: -0.2 }}>{label}</span>
      <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: active ? "rgba(255,255,255,0.7)" : T.textFaint }}>{kbd}</span>
    </div>
  );
}

function MacSidebar({ p }) {
  const T = TOKENS;
  return (
    <aside style={{ width: 212, flexShrink: 0, background: T.bgDeep, borderRight: `1px solid ${T.hair}`, display: "flex", flexDirection: "column", padding: "0 12px" }}>
      <div style={{ display: "flex", gap: 8, padding: "16px 6px 18px" }}>
        {["#ED6A5E", "#F5BF4F", "#61C554"].map((c) => <span key={c} style={{ width: 12, height: 12, borderRadius: 999, background: c }} />)}
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "9px 10px", borderRadius: 11, background: T.card, border: `1px solid ${T.hair}`, boxShadow: T.shadowSm, marginBottom: 12 }}>
        <div style={{ width: 28, height: 28, borderRadius: 8, background: T.accentSoft, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
          <Icon name={p.icon} size={15} color={T.accent} />
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 14, fontWeight: 600, letterSpacing: -0.2, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{p.cycle}</div>
          <div style={{ fontFamily: FONTS.mono, fontSize: 9.5, color: T.textMuted, marginTop: 1 }}>WEEK 3/12</div>
        </div>
        <Icon name="chevD" size={11} color={T.textMuted} />
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 8, padding: "8px 10px", borderRadius: 9, background: T.input, marginBottom: 14 }}>
        <Icon name="search" size={13} color={T.textMuted} />
        <span style={{ flex: 1, fontSize: 13, color: T.textMuted }}>Search & jump…</span>
        <span style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.textFaint }}>⌘K</span>
      </div>
      <NavItem icon="daily" label="Daily" kbd="2" />
      <NavItem icon="home" label="Today" kbd="1" active />
      <NavItem icon="weekly" label="Weekly" kbd="3" />
      <NavItem icon="goals" label="Goals" kbd="4" />
      <NavItem icon="vision" label="Vision" kbd="5" />
      <NavItem icon="habits" label="Habits" kbd="6" />
      <NavItem icon="notes" label="Notes" kbd="7" />
      <div style={{ flex: 1 }} />
      <div style={{ display: "flex", alignItems: "center", gap: 9, padding: "9px 10px", borderRadius: 9, background: T.card, border: `1px solid ${T.hair}`, marginBottom: 14, boxShadow: T.shadowSm }}>
        <Icon name="plus" size={13} color={T.accent} />
        <span style={{ flex: 1, fontSize: 13.5, fontWeight: 500, color: T.textDim }}>Quick capture</span>
        <span style={{ fontFamily: FONTS.mono, fontSize: 10, color: T.textFaint }}>⌘N</span>
      </div>
    </aside>
  );
}

function StatTile({ label, value, icon, amber }) {
  const T = TOKENS;
  return (
    <div style={{ minWidth: 84, background: T.card, border: `1px solid ${T.hair}`, borderRadius: 11, padding: "10px 13px", boxShadow: T.shadowSm }}>
      <div style={{ display: "flex", alignItems: "center", gap: 5, marginBottom: 7 }}>
        <Icon name={icon} size={11} color={amber ? T.amber : T.textMuted} />
        <span style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.4, textTransform: "uppercase", color: T.textMuted, whiteSpace: "nowrap" }}>{label}</span>
      </div>
      <div style={{ fontFamily: FONTS.mono, fontSize: 20.5, fontWeight: 600, color: amber ? T.amber : T.ink, letterSpacing: -0.5 }}>{value}</div>
    </div>
  );
}

function MacPanel({ label, trailing, action, children }) {
  const T = TOKENS;
  return (
    <div style={{ background: T.card, border: `1px solid ${T.hair}`, borderRadius: 12, padding: 15, boxShadow: T.shadowSm }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 10 }}>
        <span style={mono()}>{label}</span>
        {action ? <span style={{ display: "inline-flex", alignItems: "center", gap: 3, fontSize: 11.5, fontWeight: 600, color: T.accent }}>{action}<Icon name="chevR" size={10} color={T.accent} /></span>
          : trailing ? <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: T.textMuted }}>{trailing}</span> : null}
      </div>
      {children}
    </div>
  );
}

function MacToday() {
  const T = TOKENS;
  const p = usePersona();
  const st = personaStats(p);
  const now = p.schedule.find((s) => s.now) || p.schedule[1];
  const taskRow = (t, i, arr) => (
    <div key={i} style={{ display: "flex", alignItems: "center", gap: 9, padding: "7px 0", borderTop: i ? `1px solid ${T.hair}` : "none" }}>
      <CircleCheck checked={t.done} size={17} color={T.accent} onClick={() => {}} />
      <span style={{ flex: 1, fontSize: 14.5, letterSpacing: -0.1, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", textDecoration: t.done ? "line-through" : "none", opacity: t.done ? 0.5 : 1 }}>{t.t}</span>
      {t.streak != null && <span style={{ display: "inline-flex", alignItems: "center", gap: 2, fontFamily: FONTS.mono, fontSize: 12, color: T.amber, fontWeight: 600 }}><Icon name="flame" size={10} color={T.amber} />{t.streak}</span>}
    </div>
  );

  return (
    <div style={{ flex: 1, minWidth: 0, display: "flex" }}>
      <div style={{ flex: 1, minWidth: 0, padding: "24px 28px", overflow: "hidden" }}>
        <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 20, marginBottom: 18 }}>
          <div>
            <div style={mono()}>Friday, May 30</div>
            <div style={{ fontSize: 32, fontWeight: 700, letterSpacing: -1, lineHeight: "36px", marginTop: 4 }}>Good morning.</div>
            <div style={{ fontSize: 15, color: T.textDim, marginTop: 4 }}>One clear focus beats ten open tabs.</div>
          </div>
          <div style={{ display: "flex", gap: 8, flexShrink: 0 }}>
            <StatTile label="Tasks done" value={`${st.taskDone}/${st.taskTotal}`} icon="daily" />
            <StatTile label="Habits" value={`${st.habitsDone}/${st.habitsTotal}`} icon="habits" />
            <StatTile label="Best streak" value={`${st.best}d`} icon="flame" amber />
            <StatTile label="Cycle" value="W3/12" icon="target" />
          </div>
        </div>

        <DarkCard pressable={false} style={{ padding: 0, marginBottom: 16, overflow: "hidden" }}>
          <div style={{ display: "flex", alignItems: "stretch" }}>
            <div style={{ flex: 1, minWidth: 0, padding: 20 }}>
              <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 14 }}>
                <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>Today's mission</span>
                <div style={{ flex: 1 }} />
                <Ring size={38} stroke={3.5} progress={st.taskDone / st.taskTotal} color="#fff"><span style={{ fontFamily: FONTS.mono, fontSize: 9, fontWeight: 600, color: "rgba(255,255,255,0.85)" }}>{st.taskDone}/{st.taskTotal}</span></Ring>
              </div>
              <div style={{ fontSize: 25, fontWeight: 600, letterSpacing: -0.6, color: "#fff", lineHeight: "31px" }}>{p.mission}</div>
              <div style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 16 }}>
                <div style={{ display: "inline-flex", alignItems: "center", gap: 8, background: T.accent, borderRadius: 999, padding: "7px 13px 7px 8px" }}>
                  <CircleCheck checked={false} size={18} color="#fff" onClick={() => {}} />
                  <span style={{ color: "#fff", fontSize: 12.5, fontWeight: 600 }}>Mark done</span>
                </div>
                <div style={{ display: "inline-flex", alignItems: "center", gap: 6, background: "rgba(255,255,255,0.1)", border: "1px solid rgba(255,255,255,0.18)", borderRadius: 999, padding: "7px 13px", color: "#fff", fontSize: 12.5, fontWeight: 600 }}>Open Daily <Icon name="arrowR" size={12} color="#fff" /></div>
              </div>
            </div>
            <div style={{ width: 188, flexShrink: 0, borderLeft: "1px solid rgba(255,255,255,0.1)", padding: "18px", display: "flex", flexDirection: "column", justifyContent: "center", gap: 4 }}>
              <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                <span style={{ width: 5, height: 5, borderRadius: 3, background: T.accent, boxShadow: `0 0 0 3px ${hexA(T.accent, 0.3)}` }} />
                <span style={{ fontFamily: FONTS.mono, fontSize: 9.5, fontWeight: 600, letterSpacing: 0.4, textTransform: "uppercase", color: "rgba(255,255,255,0.55)" }}>Now</span>
              </div>
              <div style={{ fontSize: 13.5, fontWeight: 600, color: "#fff", letterSpacing: -0.2, lineHeight: "18px", marginTop: 3 }}>{now.label}</div>
              <div style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: "rgba(255,255,255,0.5)", marginTop: 2 }}>started {now.time}</div>
              <div style={{ marginTop: 12 }}>
                <div style={{ height: 4, borderRadius: 999, background: "rgba(255,255,255,0.14)", overflow: "hidden" }}><div style={{ width: "32%", height: "100%", background: T.accent }} /></div>
                <div style={{ fontFamily: FONTS.mono, fontSize: 9.5, color: "rgba(255,255,255,0.5)", marginTop: 5 }}>32% THROUGH THE DAY</div>
              </div>
            </div>
          </div>
        </DarkCard>

        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16 }}>
          <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
            <MacPanel label="Next tasks" trailing={`${st.taskDone}/${st.taskTotal}`} action="Daily">{p.secondary.map(taskRow)}</MacPanel>
            <MacPanel label="Habits" trailing={`${st.habitsTotal - st.habitsDone} left`} action="All">{p.habits.slice(0, 3).map(taskRow)}</MacPanel>
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
            <MacPanel label="This week · W3" action="Weekly">
              <div style={{ display: "flex", gap: 11 }}>
                <div style={{ width: 3, alignSelf: "stretch", borderRadius: 2, background: T.amber }} />
                <div>
                  <div style={{ fontSize: 16, fontWeight: 600, letterSpacing: -0.3, lineHeight: "21px" }}>{p.weekPriority}</div>
                  <div style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: T.textMuted, marginTop: 5 }}>{p.weekSupport.length} supporting · 9 weeks left</div>
                </div>
              </div>
            </MacPanel>
            <MacPanel label="Goals" trailing="3" action="All">
              {p.goals.map((g, i) => (
                <div key={i} style={{ display: "flex", alignItems: "center", gap: 10, padding: "8px 0", borderTop: i ? `1px solid ${T.hair}` : "none" }}>
                  <Ring size={30} stroke={3} progress={g.pct / 100} color={T.accent}><span style={{ fontFamily: FONTS.mono, fontSize: 8, fontWeight: 700, color: T.accent }}>{g.pct}</span></Ring>
                  <span style={{ flex: 1, fontSize: 14.5, letterSpacing: -0.1, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{g.title}</span>
                  <span style={{ fontFamily: FONTS.mono, fontSize: 12, color: T.textMuted }}>{g.ratio}</span>
                </div>
              ))}
            </MacPanel>
          </div>
        </div>
      </div>

      {/* agenda rail */}
      <aside style={{ width: 296, flexShrink: 0, background: T.bgDeep, borderLeft: `1px solid ${T.hair}`, padding: "20px 20px" }}>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 16 }}>
          <span style={mono()}>Agenda</span>
          <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, color: T.accent }}>EDIT</span>
        </div>
        <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
          {p.schedule.map((b, i) => (
            <div key={i} style={{ display: "flex", gap: 12 }}>
              <span style={{ fontFamily: FONTS.mono, fontSize: 12, color: T.textMuted, width: 32, flexShrink: 0, paddingTop: 11 }}>{b.time}</span>
              <div style={{ flex: 1, display: "flex", gap: 9, padding: "9px 12px", borderRadius: 9, background: b.now ? T.accent : hexA(T.accent, 0.09), boxShadow: b.now ? `0 3px 10px -2px ${hexA(T.accent, 0.5)}` : "none" }}>
                <span style={{ width: 3, alignSelf: "stretch", borderRadius: 2, background: b.now ? "#fff" : T.accent, flexShrink: 0 }} />
                <div>
                  <div style={{ fontSize: 14, fontWeight: 600, color: b.now ? "#fff" : T.ink, letterSpacing: -0.2 }}>{b.label}</div>
                  {b.now && <div style={{ fontFamily: FONTS.mono, fontSize: 9.5, color: "rgba(255,255,255,0.75)", marginTop: 2, textTransform: "uppercase", letterSpacing: 0.4 }}>Now</div>}
                </div>
              </div>
            </div>
          ))}
        </div>
      </aside>
    </div>
  );
}

function MacTodayWindow() {
  const p = usePersona();
  return <MacWindow><MacSidebar p={p} /><MacToday /></MacWindow>;
}

// habit flip card front (persona habit)
function HabitCardFront({ name = "Morning walk", streak = 18, done = true }) {
  const T = TOKENS;
  const days = ["M", "T", "W", "T", "F", "S", "S"];
  const teaser = [1, 1, 1, 0, 1, 1, 2];
  return (
    <div style={{ width: 300, height: 272, background: T.card, border: `1px solid ${T.hair}`, borderRadius: T.rXl, boxShadow: T.shadowMd, padding: 20, display: "flex", flexDirection: "column", fontFamily: FONTS.sans }}>
      <div style={{ display: "flex", alignItems: "flex-start", gap: 10 }}>
        <div style={{ flex: 1, fontSize: 16, fontWeight: 600, letterSpacing: -0.3, lineHeight: "20px", color: T.ink, display: "-webkit-box", WebkitLineClamp: 2, WebkitBoxOrient: "vertical", overflow: "hidden" }}>{name}</div>
        <div style={{ display: "flex", gap: 6, flexShrink: 0 }}>
          <div style={{ width: 28, height: 28, borderRadius: 8, background: T.input, display: "flex", alignItems: "center", justifyContent: "center" }}><Icon name="pencil" size={13} color={T.textMuted} /></div>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 4, height: 28, padding: "0 10px", borderRadius: 8, background: T.input }}><span style={{ fontSize: 11.5, fontWeight: 600, color: T.textDim }}>History</span></div>
        </div>
      </div>
      <div style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: 10 }}>
        <div style={{ width: 62, height: 62, borderRadius: 999, background: done ? T.accent : T.accentSoft, border: done ? "none" : `1.5px solid ${hexA(T.accent, 0.4)}`, display: "flex", alignItems: "center", justifyContent: "center", boxShadow: done ? `0 6px 18px -4px ${hexA(T.accent, 0.6)}` : "none" }}>
          <Icon name="habits" size={28} color={done ? "#fff" : T.accent} strokeWidth={2} />
        </div>
        <div style={{ fontSize: 13.5, fontWeight: 500, color: T.textDim }}>{done ? "Done today" : "Did you do this today?"}</div>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 4, fontFamily: FONTS.mono, fontSize: 12, color: T.amber, fontWeight: 600 }}><Icon name="flame" size={12} color={T.amber} />{streak}-day streak</div>
      </div>
      <div style={{ display: "flex", justifyContent: "space-between" }}>
        {teaser.map((d, i) => (
          <div key={i} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 5 }}>
            <div style={{ width: 22, height: 22, borderRadius: 6, background: d === 1 ? T.accent : "transparent", border: d === 2 ? `1.5px solid ${T.accent}` : d === 0 ? `1px solid ${T.hair}` : "none" }} />
            <span style={{ fontFamily: FONTS.mono, fontSize: 9.5, color: T.textMuted }}>{days[i]}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

Object.assign(window, { MacWindow, MacTodayWindow, MacToday, MacSidebar, HabitCardFront });
