/* ============================================================
   RB Card Styles (CSS only, no inline PHP styling)
   ============================================================ */

/* Container for app card (used by [app_card]) */
.rb-app-card {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 16px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 16px;
  background: #fff;
  font-family: "Inter", sans-serif;
  color: #0D0D0D;
}

.rb-app-media {
  display: flex;
  align-items: center;
  justify-content: center;
}

.rb-app-logo,
.rb-card-image {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

.rb-app-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rb-app-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  color: #0D0D0D;
}

.rb-app-rating {
  font-size: 14px;
  color: #6b7280;
}

.rb-app-badge {
  font-size: 13px;
  font-weight: 500;
  color: #374151;
}

.rb-app-excerpt {
  font-size: 14px;
  line-height: 1.5;
  color: #374151;
}

.rb-app-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
}

/* Join button ([join_link]) */
.join-link {
  display: inline-block;
  background: #374151;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  padding: 8px 14px;
  border-radius: 8px;
  border: 2px solid #374151;
}

.join-link:hover {
  background: #374151;
  color: #fff;
}

/* ============================================================
   Code Only ([code_only])
   ============================================================ */

.rb-app-code-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}

.rb-app-code-row .code-label {
  font-weight: 600;
  font-size: 14px;
  color: #0D0D0D;
  margin: 0;
}

.code-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 8px 12px;
}

.code-pill .rb-ref-code {
  font-weight: 500;
  font-size: 16px;
  color: #0D0D0D;
  margin: 0;
}

/* ============================================================
   Star Rating
   ============================================================ */

.rb-star-rating .star.full { color: #fbbf24; }
.rb-star-rating .star.half { color: #fbbf24; }
.rb-star-rating .star.empty { color: #d1d5db; }
.rb-star-rating .rating-value { font-size: 13px; color: #6b7280; }

/* ============================================================
   Copy Icon (SVG) and "Copied" Animation
   ============================================================ */

.copy-code-icon {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #f8fafc;
  cursor: pointer;
  font-size: 0;
  box-sizing: border-box;
  line-height: 0;
  transition: background-color .15s ease, border-color .15s ease, transform .06s ease;
}

.copy-code-icon::before { content: none !important; }

.copy-code-icon svg {
  display: block !important;
  width: 16px !important;
  height: 16px !important;
  stroke: #374151 !important;
  fill: none !important;
  pointer-events: none;
}

.copy-code-icon:hover { background: #f3f4f6; border-color: #cfd4dc; }
.copy-code-icon:active { transform: scale(0.98); }

.copy-code-icon.copied { background: #ecfdf5; border-color: #86efac; }
.copy-code-icon.copied svg { stroke: #10b981 !important; }

/* ============================================================
   Stable Pill Layout + "Copied" Text (No Flicker)
   ============================================================ */

.code-pill {
  --copied-slot: 72px;
  position: relative;
  padding-right: calc(12px + var(--copied-slot));
}

.code-pill .copy-code-icon {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
  will-change: opacity, transform;
}

.copy-code-icon::after {
  content: "copied";
  position: absolute;
  right: 40px;
  top: 50%;
  width: calc(var(--copied-slot) - 40px);
  text-align: left;
  transform: translate(8px, -50%);
  font-size: 14px;
  font-weight: 600;
  color: #10b981;
  opacity: 0;
  pointer-events: none;
  backface-visibility: hidden;
  transition: opacity .18s ease, transform .18s ease;
}

.copy-code-icon.copied::after {
  opacity: 1;
  transform: translate(0, -50%);
}

.copy-code-icon.copied svg {
  opacity: 1;
  transition: opacity .18s ease;
}

/* ============================================================
   JS: Copy & Share (class toggle only)
   ============================================================ */

document.addEventListener('click', function (e) {
  // ===== COPY button =====
  const btn = e.target.closest('.copy-code-icon');
  if (btn) {
    const row = btn.closest('.rb-app-code-row');
    const code =
      btn.getAttribute('data-code') ||
      row?.getAttribute('data-code') ||
      row?.querySelector('.rb-ref-code')?.textContent?.trim() ||
      "";

    if (!code) return;

    function markCopied() {
      // Reset any previous timers
      if (btn._copiedTimer) {
        clearTimeout(btn._copiedTimer);
        btn._copiedTimer = null;
      }
      btn.classList.add('copied');
      btn._copiedTimer = setTimeout(() => {
        btn.classList.remove('copied');
        btn._copiedTimer = null;
      }, 3000); // <-- keeps text visible for 3 seconds
    }

    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(code).then(markCopied);
    } else {
      const ta = document.createElement('textarea');
      ta.value = code;
      ta.setAttribute('readonly', '');
      ta.style.position = 'absolute';
      ta.style.left = '-9999px';
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand('copy'); } catch (err) {}
      document.body.removeChild(ta);
      markCopied();
    }
  }

  // ===== SHARE button =====
  const shareBtn = e.target.closest('.rb-share-btn');
  if (shareBtn) {
    const card = shareBtn.closest('.rb-app-card');
    const link = card?.querySelector('.rb-app-cta .join-link');
    const url = link ? link.getAttribute('href') : window.location.href;
    const title = (card?.querySelector('.rb-app-title')?.textContent || document.title).trim();
    const text = (card?.querySelector('.rb-app-excerpt')?.textContent || '').trim();

    function markShared() {
      shareBtn.classList.add('copied');
      setTimeout(() => shareBtn.classList.remove('copied'), 1200);
    }

    if (navigator.share) {
      navigator.share({ title, text, url }).catch(() => {
        navigator.clipboard?.writeText(url).then(markShared);
      });
    } else {
      navigator.clipboard?.writeText(url).then(markShared);
    }
  }
});

// No innerHTML swaps, no icon injection—CSS handles icons via ::before
