;(function () {
/* ToastHost — single global toast slot mounted at the App root. Fires
   via window.showToast(message, kind) with kind ∈ {'success','info','error'}.
   Renders an absolutely-positioned pill near the top of the viewport
   that slides + fades in for ~250ms, holds for 1800ms, then slides
   out. Subsequent showToast calls during a hold reset the timer with
   the new message (last-write-wins, no queue — simpler for a demo). */

const HOLD_MS = 1800;
const ANIM_MS = 250;

const KIND_STYLES = {
  success: { bg: '#0F1F1A', accent: '#079455' },
  info:    { bg: '#0E1420', accent: '#00AFAB' },
  error:   { bg: '#1F1212', accent: '#F04438' },
};

const Check = ({ color }) => (
  <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
    <circle cx="8" cy="8" r="7" stroke={color} strokeWidth="1.5"/>
    <path d="M5 8.2l2 2 4-4" stroke={color} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
  </svg>
);

const ToastHost = () => {
  const [state, setState] = React.useState({ message: '', kind: 'success', visible: false });
  const timerRef = React.useRef(null);
  const exitTimerRef = React.useRef(null);

  /* Wire window.showToast on mount; clean both timers on unmount. */
  React.useEffect(() => {
    window.showToast = (message, kind = 'success') => {
      if (!message) return;
      if (timerRef.current) clearTimeout(timerRef.current);
      if (exitTimerRef.current) clearTimeout(exitTimerRef.current);
      setState({ message, kind, visible: true });
      timerRef.current = setTimeout(() => {
        setState((s) => ({ ...s, visible: false }));
        exitTimerRef.current = setTimeout(() => {
          setState({ message: '', kind: 'success', visible: false });
        }, ANIM_MS);
      }, HOLD_MS);
    };
    return () => {
      delete window.showToast;
      if (timerRef.current) clearTimeout(timerRef.current);
      if (exitTimerRef.current) clearTimeout(exitTimerRef.current);
    };
  }, []);

  if (!state.message) return null;
  const style = KIND_STYLES[state.kind] || KIND_STYLES.success;

  return (
    <div
      style={{
        position: 'absolute',
        top: '20px',
        left: 0,
        right: 0,
        display: 'flex',
        justifyContent: 'center',
        pointerEvents: 'none',
        zIndex: 100,
      }}
    >
      <div
        style={{
          display: 'inline-flex',
          alignItems: 'center',
          gap: '10px',
          backgroundColor: style.bg,
          color: '#fff',
          paddingTop: '10px',
          paddingBottom: '10px',
          paddingLeft: '14px',
          paddingRight: '16px',
          borderRadius: '9999px',
          boxShadow: '0px 12px 24px rgba(14, 20, 32, 0.32)',
          opacity: state.visible ? 1 : 0,
          transform: state.visible ? 'translateY(0)' : 'translateY(-12px)',
          transition: `opacity ${ANIM_MS}ms ease, transform ${ANIM_MS}ms cubic-bezier(0.32, 0.72, 0.24, 1)`,
        }}
      >
        <Check color={style.accent} />
        <span
          style={{
            fontFamily: 'Gilroy',
            fontWeight: 600,
            fontSize: '13px',
            color: '#fff',
            whiteSpace: 'nowrap',
          }}
        >
          {state.message}
        </span>
      </div>
    </div>
  );
};

Object.assign(window, { ToastHost });
})();
