document.addEventListener('DOMContentLoaded', () => {
  document.querySelectorAll('.secret-toggle').forEach((button) => {
    button.addEventListener('click', () => {
      const inputId = button.getAttribute('aria-controls');
      const input = inputId ? document.getElementById(inputId) : button.closest('.secret-control')?.querySelector('.p2p-secret');
      if (!input) return;
      const reveal = input.type === 'password';
      input.type = reveal ? 'text' : 'password';
      button.textContent = reveal ? 'Ocultar' : 'Mostrar';
    });
  });

  const clearButton = document.getElementById('clearP2pConfig');
  clearButton?.addEventListener('click', () => {
    const url = document.getElementById('p2pNodeUrl');
    const key = document.getElementById('p2pNodeKey');
    if (url) url.value = '';
    if (key) key.value = '';
    document.getElementById('p2pForm')?.requestSubmit();
  });
});
