function toClipboard(url) {
    if (navigator.clipboard && window.isSecureContext) {
      // Modern approach
      navigator.clipboard.writeText(url)
        .then(() => {
          alert('Link in die Zwischenablage kopiert!');
        })
        .catch(err => {
          console.error('Fehler beim Kopieren: ', err);
        });
    } else {
      // Fallback for older browsers
      const textarea = document.createElement('textarea');
      textarea.value = url;
      textarea.style.position = 'fixed';  // Verhindert Scrollen
      textarea.style.opacity = '0';
      document.body.appendChild(textarea);
      textarea.focus();
      textarea.select();
      try {
        document.execCommand('copy');
        alert('Token-URL in die Zwischenablage kopiert!');
      } catch (err) {
        console.error('Fallback: Kopieren fehlgeschlagen', err);
      }
      document.body.removeChild(textarea);
    }
  }

function printToken() {
  const printable = document.getElementById('tokenPrintable');
  if (!printable) {
    console.error('Token printable content not found.');
    return;
  }

  const clone = printable.cloneNode(true);
  clone.querySelectorAll('.token-action-buttons').forEach(el => el.remove());

  const styles = Array.from(document.querySelectorAll('link[rel="stylesheet"], style'))
    .map(node => node.outerHTML)
    .join('\n');

  const popup = window.open('', '_blank', 'width=900,height=1200');
  if (!popup) {
    alert('Bitte Popups erlauben, um das PDF zu speichern.');
    return;
  }

  popup.document.write(`<!doctype html>
    <html>
      <head>
        <title>Token PDF</title>
        ${styles}
        <style>
          body { padding: 24px; }
          .token-action-buttons { display: none !important; }
          .card { box-shadow: none !important; }
        </style>
      </head>
      <body>${clone.outerHTML}</body>
    </html>`);
  popup.document.close();
  popup.focus();
  popup.onload = () => {
    popup.print();
    popup.close();
  };
}
