document.querySelectorAll(".btn-copy").forEach(button => {

    button.addEventListener("click", async () => {

        const text = button.dataset.copy;

        try {

            // Browser modern
            if (navigator.clipboard && window.isSecureContext) {

                await navigator.clipboard.writeText(text);

            } else {

                // Fallback Android lama
                const textarea = document.createElement("textarea");

                textarea.value = text;

                textarea.style.position = "fixed";

                textarea.style.left = "-9999px";

                document.body.appendChild(textarea);

                textarea.focus();

                textarea.select();

                document.execCommand("copy");

                textarea.remove();

            }

            const old = button.innerHTML;

            button.innerHTML =
                "<i class='fa-solid fa-check'></i> Berhasil Disalin";

            setTimeout(() => {

                button.innerHTML = old;

            }, 2000);

        } catch (err) {

            alert("Gagal menyalin. Silakan salin manual.");

        }

    });

});