// COVIA Back-to-Top FAB with 5-platform share fan-out
// Floating button bottom-right. Appears after ~0.8 viewport of scroll.
// Hover reveals LINE / FB / X / TG / Threads sliding out to the left (stagger).
//
// Share URL is hardcoded to https://covia.io/ (production) — localhost URLs
// won't yield a working OG preview on share platforms.

function CoviaBackToTop() {
  const [visible, setVisible] = React.useState(false);

  React.useEffect(() => {
    const onScroll = () => {
      setVisible(window.scrollY > window.innerHeight * 0.8);
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const scrollToTop = () => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  };

  const SHARE_URL = 'https://covia.io/';
  const enc = encodeURIComponent(SHARE_URL);

  // SVG icons (stroke/fill inherit currentColor so hover state recolors)
  const icons = {
    line: (
      <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
        <path d="M12 3C6.5 3 2 6.5 2 10.8c0 3 2.2 5.6 5.4 6.8.2.1.5.3.6.5.1.2 0 .5 0 .7l-.1.6c0 .2-.1.8.7.4 3-1.3 4.5-2.2 6.8-4.5 1.8-1.8 2.6-3.6 2.6-4.5 0-4.3-4.5-7.8-10-7.8Zm-3.8 9.5H6c-.2 0-.4-.2-.4-.4v-3.4c0-.2.2-.4.4-.4.2 0 .4.2.4.4v3H8.2c.2 0 .4.2.4.4s-.2.4-.4.4Zm1.3-.4c0 .2-.2.4-.4.4s-.4-.2-.4-.4V8.7c0-.2.2-.4.4-.4s.4.2.4.4v3.4Zm4 0c0 .2-.1.3-.3.4h-.1c-.1 0-.3-.1-.3-.2l-1.8-2.4v2.2c0 .2-.2.4-.4.4s-.4-.2-.4-.4V8.7c0-.2.1-.3.3-.4h.1c.1 0 .3.1.3.2L13 10.9V8.7c0-.2.2-.4.4-.4s.4.2.4.4v3.4Zm2.6-2.1c.2 0 .4.2.4.4s-.2.4-.4.4H15v.6h1.1c.2 0 .4.2.4.4s-.2.4-.4.4H14.6c-.2 0-.4-.2-.4-.4V8.7c0-.2.2-.4.4-.4h1.5c.2 0 .4.2.4.4s-.2.4-.4.4H15V10h1.1Z"/>
      </svg>
    ),
    fb: (
      <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
        <path d="M22 12c0-5.5-4.5-10-10-10S2 6.5 2 12c0 5 3.7 9.1 8.4 9.9V14.9H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.3.2 2.3.2v2.5h-1.3c-1.3 0-1.7.8-1.7 1.6V12h2.9l-.5 2.9h-2.4v7C18.3 21.1 22 17 22 12Z"/>
      </svg>
    ),
    x: (
      <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
        <path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231 5.451-6.231Zm-1.161 17.52h1.833L7.084 4.126H5.117L17.083 19.77Z"/>
      </svg>
    ),
    tg: (
      <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
        <path d="M9.78 18.65l.28-4.23 7.68-6.92c.34-.31-.07-.46-.52-.19L7.74 13.3 3.64 12c-.88-.25-.89-.86.2-1.3l15.97-6.16c.73-.27 1.43.18 1.15 1.3l-2.72 12.81c-.19.91-.74 1.13-1.5.71L12.6 16.3l-1.99 1.93c-.23.23-.42.42-.83.42Z"/>
      </svg>
    ),
    threads: (
      <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
        <path d="M17.55 10.76c-.09-.04-.18-.08-.27-.12-.15-2.84-1.7-4.46-4.27-4.48h-.04c-1.54 0-2.82.66-3.6 1.85l1.42.97c.58-.88 1.5-1.07 2.19-1.07h.02c.85.01 1.49.26 1.89.74.3.36.5.85.59 1.47-.67-.11-1.4-.15-2.17-.1-2.18.13-3.59 1.4-3.49 3.17.05.9.5 1.67 1.26 2.17.65.42 1.49.63 2.36.58 1.15-.06 2.05-.5 2.69-1.3.48-.61.78-1.4.92-2.39.56.34.98.79 1.2 1.33.39.92.41 2.44-.82 3.68-1.08 1.08-2.38 1.55-4.33 1.56-2.17-.02-3.81-.72-4.88-2.07-1-1.27-1.51-3.1-1.53-5.44.02-2.34.53-4.17 1.53-5.44 1.07-1.35 2.71-2.05 4.88-2.07 2.19.02 3.86.72 4.96 2.09.54.67.95 1.52 1.22 2.51l1.7-.46c-.33-1.22-.84-2.27-1.54-3.13C17.76.92 15.68.02 12.91 0h-.01c-2.77.02-4.82.92-6.1 2.67C5.65 4.26 4.97 6.49 4.95 9.29v.01c.02 2.79.7 5.02 2.03 6.61 1.28 1.75 3.33 2.65 6.1 2.67h.01c2.47-.02 4.21-.66 5.64-2.09 1.87-1.86 1.82-4.2 1.21-5.63-.44-1.03-1.29-1.87-2.45-2.43Zm-4.32 5.18c-.96.05-1.96-.38-2.01-1.3-.04-.68.49-1.44 2.06-1.53.18-.01.36-.02.53-.02.57 0 1.1.05 1.58.16-.18 2.23-1.23 2.65-2.16 2.69Z"/>
      </svg>
    ),
  };

  const shares = [
    { id: 'line',    name: 'LINE',     url: `https://social-plugins.line.me/lineit/share?url=${enc}`, icon: icons.line },
    { id: 'fb',      name: 'Facebook', url: `https://www.facebook.com/sharer/sharer.php?u=${enc}`,    icon: icons.fb },
    { id: 'x',       name: 'X',        url: `https://twitter.com/intent/tweet?url=${enc}`,            icon: icons.x },
    { id: 'tg',      name: 'Telegram', url: `https://t.me/share/url?url=${enc}`,                      icon: icons.tg },
    { id: 'threads', name: 'Threads',  url: `https://www.threads.net/intent/post?text=${enc}`,        icon: icons.threads },
  ];
  const lastIdx = shares.length - 1;

  return (
    <div className={'fab' + (visible ? ' is-visible' : '')} aria-hidden={!visible}>
      <div className="fab-shares" role="group" aria-label="分享到社交平台">
        {shares.map((s, i) => (
          <a
            key={s.id}
            href={s.url}
            target="_blank"
            rel="noopener noreferrer"
            className="fab-share"
            style={{ '--i': i, '--last': lastIdx }}
            aria-label={`分享到 ${s.name}`}
            title={s.name}
          >
            {s.icon}
          </a>
        ))}
      </div>
      <button
        type="button"
        className="fab-top"
        onClick={scrollToTop}
        aria-label="回到頂端"
        title="回到頂端"
      >
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <path d="M12 19V5M5 12l7-7 7 7" />
        </svg>
      </button>
    </div>
  );
}

const fabStyle = document.createElement('style');
fabStyle.textContent = `
  .fab {
    position: fixed;
    bottom: clamp(20px, 3vh, 36px);
    right: clamp(20px, 3vw, 36px);
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 10px;
    opacity: 0;
    transform: translateY(16px);
    pointer-events: none;
    transition:
      opacity 480ms cubic-bezier(0.22, 1, 0.36, 1),
      transform 480ms cubic-bezier(0.22, 1, 0.36, 1);
  }
  .fab.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
  }

  .fab-shares {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-right: 2px;
  }
  .fab-share {
    width: 38px; height: 38px;
    display: grid; place-items: center;
    border-radius: 50%;
    background: oklch(0.16 0.008 260 / 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    color: var(--text-1);
    text-decoration: none;
    opacity: 0;
    transform: translateX(16px) scale(0.82);
    pointer-events: none;
    transition:
      opacity 320ms cubic-bezier(0.22, 1, 0.36, 1),
      transform 320ms cubic-bezier(0.22, 1, 0.36, 1),
      background 200ms, color 200ms, border-color 200ms;
    /* Closing: rightmost (highest i) exits first */
    transition-delay: calc((var(--last, 4) - var(--i, 0)) * 40ms);
  }
  .fab-share svg {
    width: 16px; height: 16px;
    display: block;
  }
  .fab:hover .fab-share,
  .fab:focus-within .fab-share {
    opacity: 1;
    transform: translateX(0) scale(1);
    pointer-events: auto;
    /* Opening: rightmost (closest to main button) enters first */
    transition-delay: calc((var(--last, 4) - var(--i, 0)) * 50ms);
  }
  .fab-share:hover {
    background: var(--gold);
    color: var(--surface-0);
    border-color: var(--gold);
    transform: translateY(-2px) scale(1);
  }
  .fab-share:focus-visible {
    outline: 2px solid var(--gold);
    outline-offset: 2px;
  }

  .fab-top {
    width: 48px; height: 48px;
    display: grid; place-items: center;
    border-radius: 50%;
    background: oklch(0.16 0.008 260 / 0.88);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    color: var(--text-1);
    cursor: pointer;
    box-shadow: 0 8px 24px -8px oklch(0 0 0 / 0.5);
    transition:
      background 200ms, color 200ms, border-color 200ms,
      transform 200ms cubic-bezier(0.22, 1, 0.36, 1),
      box-shadow 200ms;
    padding: 0;
  }
  .fab-top:hover {
    background: var(--gold);
    color: var(--surface-0);
    border-color: var(--gold);
    transform: translateY(-3px);
    box-shadow: 0 12px 32px -6px oklch(0.78 0.16 75 / 0.35);
  }
  .fab-top:focus-visible {
    outline: 2px solid var(--gold);
    outline-offset: 3px;
  }

  @media (prefers-reduced-motion: reduce) {
    .fab, .fab-share, .fab-top { transition-duration: 120ms !important; }
    .fab-top:hover { transform: none; }
    .fab-share:hover { transform: none; }
  }
`;
document.head.appendChild(fabStyle);

window.CoviaBackToTop = CoviaBackToTop;
