     ensureChart();
    });

    document.addEventListener('change', (e) => {
      if (e.target.matches('[data-col]')) {
        const col = parseInt(e.target.dataset.col, 10);
        if (e.target.checked) { if (!state.datasets.includes(col)) state.datasets.push(col); }
        else state.datasets = state.datasets.filter(c => c !== col);
        renderDatasetColorEditors();
        ensureChart();
      }
      if (e.target.matches('[data-color-col]')) {
        const col = parseInt(e.target.dataset.colorC