  var _inited = false;
  document.addEventListener('tabChanged', function(e) {
    if (e.detail.tab !== 'settings' || _inited) return;
    _inited = true;
  function setNextcloudConnectedStatus(url, user) {
    var statusEl = document.getElementById('nc-status');
    if (!statusEl) return;
    statusEl.textContent = 'Connected to ';
    var strong = document.createElement('strong');
    strong.textContent = url || '';
    statusEl.appendChild(strong);
    statusEl.appendChild(document.createTextNode(' as ' + (user || '')));
  }

  function loadNextcloudStatus() {
    fetch('/api/auth/me', { headers: getAuthHeaders() })
    .then(function(r) { return r.json(); })
    .then(function(data) {
      var pathSection = document.getElementById('nc-webdav-path-section');
      if (data.user && data.user.nextcloud_url) {
        setNextcloudConnectedStatus(data.user.nextcloud_url, data.user.nextcloud_user);
        document.getElementById('nc-url').value = data.user.nextcloud_url;
        document.getElementById('nc-user').value = data.user.nextcloud_user;
        document.getElementById('btn-nc-disconnect').classList.remove('hidden');
        if (pathSection) {
          pathSection.classList.remove('hidden');
          var pathEl = document.getElementById('nc-webdav-path');
          if (pathEl) pathEl.value = data.user.webdav_learning_path || '';
        }
      } else {
        document.getElementById('nc-status').textContent = 'Not connected';
        document.getElementById('btn-nc-disconnect').classList.add('hidden');
        if (pathSection) pathSection.classList.add('hidden');
      }
    });
  }

  window.loadNextcloudStatus = loadNextcloudStatus;

  document.getElementById('btn-nc-connect').addEventListener('click', function() {
    var url = document.getElementById('nc-url').value.trim().replace(/\/+$/, '');
    var user = document.getElementById('nc-user').value.trim();
    var pass = document.getElementById('nc-pass').value.trim();

    if (!url || !user || !pass) { showToast('Fill all Nextcloud fields', 'error'); return; }

    showLoading('Connecting to Nextcloud...');

    fetch('/api/nextcloud/connect', {
      method: 'POST',
      headers: getAuthHeaders(),
      body: JSON.stringify({ nextcloudUrl: url, username: user, appPassword: pass })
    })
    .then(function(r) { return r.json(); })
    .then(function(data) {
      hideLoading();
      if (data.success) {
        showToast(data.message, 'success');
        loadNextcloudStatus();
        document.getElementById('nc-pass').value = '';
      } else {
        showToast(data.error || 'Connection failed', 'error');
      }
    })
    .catch(function(err) { hideLoading(); showToast(err.message, 'error'); });
  });

  document.getElementById('btn-nc-disconnect').addEventListener('click', function() {
    fetch('/api/nextcloud/disconnect', { method: 'POST', headers: getAuthHeaders() })
    .then(function() { showToast('Disconnected', 'info'); loadNextcloudStatus(); });
  });

  document.getElementById('btn-nc-save-path').addEventListener('click', function() {
    var p = (document.getElementById('nc-webdav-path').value || '').trim();
    fetch('/api/user/webdav-path', {
      method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ path: p })
    }).then(function(r) { return r.json(); })
    .then(function(d) { d.success ? showToast('Path saved', 'success') : showToast(d.error || 'Failed', 'error'); })
    .catch(function() { showToast('Failed to save', 'error'); });
  });

  console.log('✅ Nextcloud module loaded');
  });
