/* Reeve — phone shell, brand mark, and tiny primitives.
   Everything here is theme-agnostic: visual differences between the two
   directions live entirely in CSS keyed off .style-glass / .style-notion. */
const { useState, useRef, useEffect } = React;

/* ---------- ReeveMark — the brand logo ----------
   A single warm ember (filled dot + soft glow) cradled by a thin open arc:
   the assistant quietly keeping one thing warm and held until you decide.
   Built from a circle + an arc only. */
function ReeveMark({ size = 24, stroke = 2 }) {
  const s = size;
  return (
    <svg width={s} height={s} viewBox="0 0 24 24" className="embermark" aria-hidden="true">
      {/* open cradle arc — opens at the top, holding the ember */}
      <path className="em-arc" d="M5.4 7.2 A 8.4 8.4 0 1 0 18.6 7.2" fill="none"
        stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" />
      {/* the ember */}
      <circle className="em-glow" cx="12" cy="12.6" r="5.4" fill="currentColor" opacity="0.16" />
      <circle className="em-dot" cx="12" cy="12.6" r="3.05" fill="currentColor" />
    </svg>
  );
}

/* ---------- SpecMark — the specialist sub-agent's mark ----------
   Same open cradle as Reeve (it's brought in BY Reeve, same family), but the
   ember is credentialed: a focused core ringed once. Tinted to the secondary
   color via CSS (.specmark { color: var(--accent-b) }) so it reads as a
   distinct, domain-expert helper without leaving the palette. */
function SpecMark({ size = 24, stroke = 2 }) {
  const s = size;
  return (
    <svg width={s} height={s} viewBox="0 0 24 24" className="specmark" aria-hidden="true">
      <path className="sm-arc" d="M5.4 7.2 A 8.4 8.4 0 1 0 18.6 7.2" fill="none"
        stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" />
      <circle className="sm-ring" cx="12" cy="12.6" r="5.1" fill="none"
        stroke="currentColor" strokeWidth={stroke * 0.82} opacity="0.42" />
      <circle className="sm-dot" cx="12" cy="12.6" r="2.5" fill="currentColor" />
    </svg>
  );
}

/* ---------- specialist enabled-state store (shared across files) ----------
   One-time authorization model: once on, Reeve dispatches the specialist on
   its own at sensible moments; the user still only approves the final send.
   Persisted to localStorage; default ON (by the time you're in the app you've
   been through the cold-start recommend). Any component subscribes via the
   useSpecialist() hook. */
const SPEC_KEY = "reeve.specialist.on";
let _specOn = true;
try { if (localStorage.getItem(SPEC_KEY) === "0") _specOn = false; } catch (e) {}
const _specSubs = new Set();
function setSpecialistOn(v) {
  _specOn = !!v;
  try { localStorage.setItem(SPEC_KEY, _specOn ? "1" : "0"); } catch (e) {}
  _specSubs.forEach((fn) => fn(_specOn));
}
function useSpecialist() {
  const [on, setOn] = useState(_specOn);
  useEffect(() => {
    const fn = (v) => setOn(v);
    _specSubs.add(fn);
    setOn(_specOn);
    return () => { _specSubs.delete(fn); };
  }, []);
  return [on, setSpecialistOn];
}

function Chev() { return <span className="chev" aria-hidden="true">›</span>; }

/* hiTime — wrap every time phrase in a highlighted <em class="t-time">.
   Covers weekday(+period+clock), standalone clock, durations, relative days,
   and the English email forms (Tuesday / March 10th / 10:30 AM / 3/10). */
const TIME_RE = /周[一二三四五六日](?:上午|下午|早上|晚上)?(?:\s?\d{1,2}:\d{2}\s?(?:[AaPp][Mm])?)?|\d{1,2}:\d{2}\s?(?:[AaPp][Mm])?|\d+\s?(?:天|小时|分钟|秒)(?![一-鿿])|前一天|明早|明天|今天|当天|上午|下午|早上|晚上|(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day|(?:January|February|March|April|May|June|July|August|September|October|November|December)\s?\d{1,2}(?:st|nd|rd|th)?|\d{1,2}\/\d{1,2}/g;
function hiTime(text) {
  if (typeof text !== "string") return text;
  const out = []; let last = 0; let m;
  const re = new RegExp(TIME_RE);
  while ((m = re.exec(text)) !== null) {
    if (m.index > last) out.push(text.slice(last, m.index));
    out.push(<em className="t-time" key={out.length}>{m[0]}</em>);
    last = m.index + m[0].length;
    if (m.index === re.lastIndex) re.lastIndex++;
  }
  if (last < text.length) out.push(text.slice(last));
  return out;
}

function Avatar({ dot }) {
  return <span className="avatar">D{dot ? <i className="avatar-dot" /> : null}</span>;
}
function Chip({ children, tone }) { return <span className={`chip ${tone || ""}`}>{children}</span>; }

/* official multicolor Google "G" for the sign-in button */
function GoogleG() {
  return (
    <svg className="gauth-g" viewBox="0 0 48 48" width="18" height="18" aria-hidden="true">
      <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z" />
      <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z" />
      <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z" />
      <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z" />
    </svg>
  );
}

/* Apple logo glyph for the sign-in button */
function AppleLogo() {
  return (
    <svg className="aauth-g" viewBox="0 0 24 24" width="17" height="17" aria-hidden="true">
      <path fill="currentColor" d="M17.05 12.54c-.02-2.07 1.69-3.06 1.77-3.11-.96-1.41-2.46-1.6-3-1.62-1.28-.13-2.5.75-3.15.75-.65 0-1.65-.73-2.71-.71-1.39.02-2.68.81-3.4 2.05-1.45 2.52-.37 6.25 1.04 8.29.69 1 1.51 2.12 2.58 2.08 1.04-.04 1.43-.67 2.69-.67 1.25 0 1.61.67 2.71.65 1.12-.02 1.83-1.02 2.51-2.02.79-1.16 1.12-2.28 1.14-2.34-.03-.01-2.18-.84-2.2-3.32zM15.0 5.86c.57-.69.96-1.65.85-2.61-.82.03-1.82.55-2.41 1.24-.53.61-.99 1.59-.87 2.53.92.07 1.86-.47 2.43-1.16z" />
    </svg>
  );
}

/* ---------- iOS status bar (Dynamic Island era) ---------- */
function StatusBar() {
  return (
    <div className="statusbar">
      <span className="sb-time">9:41</span>
      <span className="sb-right">
        <span className="sb-cell" aria-hidden="true"><i /><i /><i /><i /></span>
        <span className="sb-wifi" aria-hidden="true" />
        <span className="sb-bat" aria-hidden="true"><b /></span>
      </span>
    </div>
  );
}

/* ---------- phone frame ---------- */
function Phone({ styleId, children }) {
  return (
    <div className={`phone style-${styleId}`}>
      <div className="phone-screen">
        <div className="screen-mesh" aria-hidden="true">
          <i className="mesh-blob b1" /><i className="mesh-blob b2" /><i className="mesh-blob b3" /><i className="mesh-blob b4" />
        </div>
        <div className="screen-body">{children}</div>
        <StatusBar />
        <span className="di" aria-hidden="true" />
        <span className="home-ind" aria-hidden="true" />
      </div>
    </div>
  );
}

/* ---------- chrome shared by screens ---------- */
function AppTop() {
  return (
    <div className="apptop">
      <span className="brandrow"><span className="bm"><ReeveMark size={18} /></span><b>Reeve</b></span>
      <span className="apptop-right">
        <span className="watch-ind"><i className="breath" />在盯</span>
        <Avatar dot />
      </span>
    </div>
  );
}
function BackTop({ title, onBack, right }) {
  return (
    <div className="navbar">
      {onBack
        ? <button className="nav-back" onClick={onBack} aria-label="返回">
            <span className="nav-chev" aria-hidden="true">‹</span><span className="nav-back-tx">Reeve</span>
          </button>
        : <span className="nav-back" />}
      <b className="nav-title">{title}</b>
      <span className="nav-action">{right || null}</span>
    </div>
  );
}
function QuickBar() {
  return (
    <div className="quickbar">
      <span className="qb-field">交给我一件要盯的事…</span>
      <span className="qb-mic" aria-hidden="true"><i /><i /><i /></span>
      <span className="qb-send" aria-hidden="true">↑</span>
    </div>
  );
}

/* expose */
Object.assign(window, { ReeveMark, SpecMark, useSpecialist, setSpecialistOn, Chev, Avatar, Chip, GoogleG, AppleLogo, StatusBar, Phone, AppTop, BackTop, QuickBar, hiTime });
