// ForestLynk — pre-auth flow shared components.
// Phone bezel, Threshold mark, lockups, primitives.

// ─────────────────────────── Threshold mark ───────────────────────────
// 3 concentric canopy rings + amber Sunlight nucleus.
// Brief: this is the brand mark used in the hero lockup (small),
// not the big LogoMark with the dashed satellite tail (that's marketing).
function ThresholdMark({ size = 28, dim = false, pulse = false }) {
  const ringColor = dim ? '#586552' : '#A8C5A0';
  const id = React.useId();
  return (
    <svg width={size} height={size} viewBox="0 0 52 52" fill="none" aria-hidden="true">
      <defs>
        {pulse && (
          <radialGradient id={`fl-glow-${id}`} cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="#D4960C" stopOpacity="0.45"/>
            <stop offset="60%" stopColor="#D4960C" stopOpacity="0.10"/>
            <stop offset="100%" stopColor="#D4960C" stopOpacity="0"/>
          </radialGradient>
        )}
      </defs>
      {pulse && <circle cx="26" cy="26" r="24" fill={`url(#fl-glow-${id})`}/>}
      <circle cx="26" cy="26" r="23" stroke={ringColor} strokeOpacity="0.32" strokeWidth="1.2"/>
      <circle cx="26" cy="26" r="16" stroke={ringColor} strokeOpacity="0.55" strokeWidth="1.3"/>
      <circle cx="26" cy="26" r="9"  stroke={ringColor} strokeOpacity="0.85" strokeWidth="1.4"/>
      <circle cx="26" cy="26" r="3.4" fill="#D4960C"/>
      <circle cx="26" cy="26" r="5.6" fill="#D4960C" fillOpacity="0.18"/>
    </svg>
  );
}

// "LogoWordmark" — Threshold mark + "FORESTLYNK" tracking-wide caps.
// Used in the hero lockup, above the lowercase 'forestlynk' h1.
function LogoWordmark({ scale = 1, color = '#F2EDE4' }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 10 * scale }}>
      <ThresholdMark size={24 * scale} />
      <span style={{
        font: `500 ${12 * scale}px var(--font-sans)`,
        letterSpacing: '0.32em', color, paddingTop: 1,
      }}>FORESTLYNK</span>
    </div>
  );
}

// Lowercase wordmark — 'forest' in fog, 'lynk' in canopy-300.
function ForestlynkH1({ size = 42 }) {
  return (
    <h1 style={{
      font: `500 ${size}px var(--font-sans)`,
      letterSpacing: '-0.04em',
      margin: 0,
      color: '#F2EDE4',
      lineHeight: 1,
    }}>
      forest<span style={{ color: '#76AB78' }}>lynk</span>
    </h1>
  );
}

// ─────────────────────────── Phone bezel ───────────────────────────
// Matches the bezel used across the ForestLynk mobile UI kit.
function Phone({ children, width = 390, height = 800, time = '9:41', label }) {
  return (
    <div style={{
      width, height,
      background: '#000', borderRadius: 48, overflow: 'hidden',
      position: 'relative', boxShadow: '0 30px 80px rgba(0,0,0,0.65), 0 0 0 1px #2D3A22',
      fontFamily: 'var(--font-sans)',
    }} data-screen-label={label}>
      {/* Dynamic island */}
      <div style={{
        position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)',
        width: 122, height: 35, borderRadius: 22, background: '#000', zIndex: 50,
      }}/>
      {/* Inner screen */}
      <div style={{
        position: 'absolute', inset: 6, background: '#0B1306',
        borderRadius: 42, overflow: 'hidden', display: 'flex', flexDirection: 'column',
      }}>
        {/* Status bar */}
        <div style={{
          padding: '16px 28px 8px',
          display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          font: '600 14.5px -apple-system, system-ui', color: '#F2EDE4',
          flexShrink: 0,
        }}>
          <span style={{ paddingLeft: 4 }}>{time}</span>
          <span style={{ display: 'flex', gap: 5, alignItems: 'center', paddingRight: 4 }}>
            <svg width="17" height="11" viewBox="0 0 17 11"><rect x="0" y="7" width="3" height="4" rx="0.6" fill="#F2EDE4"/><rect x="4" y="5" width="3" height="6" rx="0.6" fill="#F2EDE4"/><rect x="8" y="2" width="3" height="9" rx="0.6" fill="#F2EDE4"/><rect x="12" y="0" width="3" height="11" rx="0.6" fill="#F2EDE4"/></svg>
            <svg width="14" 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="#F2EDE4"/><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="#F2EDE4"/><circle cx="8.5" cy="10.5" r="1.5" fill="#F2EDE4"/></svg>
            <svg width="24" height="11" viewBox="0 0 27 13"><rect x="0.5" y="0.5" width="23" height="12" rx="3" stroke="#F2EDE4" strokeOpacity="0.5" fill="none"/><rect x="2" y="2" width="20" height="9" rx="1.8" fill="#F2EDE4"/></svg>
          </span>
        </div>
        {/* App body */}
        <div style={{ flex: 1, position: 'relative', minHeight: 0, overflow: 'hidden' }}>
          {children}
        </div>
        {/* Home indicator */}
        <div style={{
          position: 'absolute', bottom: 8, left: '50%', transform: 'translateX(-50%)',
          width: 132, height: 5, background: '#F2EDE4', borderRadius: 3, opacity: 0.7,
          zIndex: 60,
        }}/>
      </div>
    </div>
  );
}

// ─────────────────────────── Field primitive ───────────────────────────
// Used for both email and handle inputs. The hairline is the focus state;
// errors get a calm bark/danger 1.5px ring (never red-flashing).
function FLField({
  value, onChange, placeholder, type = 'text', focused = false,
  state = 'idle', // idle | invalid | checking | available | submitting
  leadIcon, trailing, mono = false, readOnly = false, autoFocus = false,
  ariaLabel,
}) {
  const borderColor =
    state === 'invalid'  ? '#C66449' :
    state === 'available'? '#76AB78' :
    focused              ? '#76AB78' :
                            '#2D3A22';
  const ring =
    state === 'invalid'  ? '0 0 0 3px rgba(198,100,73,0.18)' :
    state === 'available'? '0 0 0 3px rgba(118,171,120,0.18)' :
    focused              ? '0 0 0 3px rgba(118,171,120,0.18)' :
                            'none';
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 10,
      background: '#0B1306',
      border: `${focused || state === 'invalid' || state === 'available' ? 1.5 : 0.5}px solid ${borderColor}`,
      borderRadius: 12, padding: '14px 14px',
      boxShadow: ring,
      transition: 'border-color 220ms cubic-bezier(.32,.72,.34,1), box-shadow 220ms cubic-bezier(.32,.72,.34,1)',
    }}>
      {leadIcon}
      <input
        type={type}
        value={value}
        readOnly={readOnly || !onChange}
        autoFocus={autoFocus}
        onChange={onChange ? e => onChange(e.target.value) : () => {}}
        placeholder={placeholder}
        autoCapitalize="none"
        autoCorrect="off"
        spellCheck={false}
        aria-label={ariaLabel}
        style={{
          flex: 1, minWidth: 0, background: 'transparent', border: 'none', outline: 'none',
          font: `400 16px ${mono ? "var(--font-mono)" : "var(--font-sans)"}`,
          color: '#F2EDE4',
          caretColor: '#76AB78',
          letterSpacing: mono ? '-0.01em' : 'normal',
        }}/>
      {trailing}
    </div>
  );
}

// Primary CTA — canopy bg, fog-on-canopy text. Brief: don't use sunlight here.
function FLPrimaryButton({ children, onClick, disabled = false, loading = false, full = true, ariaLabel }) {
  const inert = disabled || loading;
  return (
    <button
      onClick={inert ? undefined : onClick}
      disabled={inert}
      aria-label={ariaLabel}
      style={{
        width: full ? '100%' : 'auto',
        padding: '14px 18px',
        background: inert ? '#1A2812' : '#3A7A3D',
        color: inert ? '#586552' : '#F2EDE4',
        border: 'none',
        borderRadius: 12,
        font: '500 15px var(--font-sans)',
        letterSpacing: '-0.01em',
        cursor: inert ? 'default' : 'pointer',
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 10,
        transition: 'background 220ms cubic-bezier(.32,.72,.34,1), color 220ms',
      }}>
      {loading && (
        <span style={{
          width: 14, height: 14, borderRadius: '50%',
          border: '1.5px solid rgba(242,237,228,0.4)',
          borderTopColor: '#F2EDE4',
          animation: 'fl-spin 720ms linear infinite',
        }}/>
      )}
      <span>{children}</span>
    </button>
  );
}

// Calm bark-toned helper / inline-error text.
function FLHelper({ tone = 'tertiary', children, mono = false }) {
  const color =
    tone === 'error'  ? '#E08C6E' :
    tone === 'good'   ? '#A8C5A0' :
    tone === 'warm'   ? '#D6BFA8' :
    tone === 'secondary' ? '#C9D2BB' :
                       '#8A9678';
  return (
    <p style={{
      font: `400 12.5px/1.5 ${mono ? 'var(--font-mono)' : 'var(--font-sans)'}`,
      color, margin: 0,
    }}>{children}</p>
  );
}

Object.assign(window, {
  ThresholdMark, LogoWordmark, ForestlynkH1, Phone, FLField, FLPrimaryButton, FLHelper,
});
