    <script>

document.addEventListener("DOMContentLoaded", function () {
    const fontFamilySelector = document.getElementById('fontFamily');
    const fontSizeInput = document.getElementById('fontSize');
    const textAlignSelector = document.getElementById('textAlign');
    const colorSchemeSelector = document.getElementById('colorScheme');
    const lineHeightSelector = document.getElementById('lineHeight');
    const applyButton = document.getElementById('applySettings');

    // Load saved settings
    const savedFont = localStorage.getItem('custom-font-family') || 'Merriweather';
    const savedFontSize = localStorage.getItem('custom-font-size') || '16px';
    const savedTextAlign = localStorage.getItem('custom-text-align') || 'justify';
    const savedColorScheme = localStorage.getItem('custom-color-scheme') || '#000,#fff';
    const savedLineHeight = localStorage.getItem('custom-line-height') || '1.5'; 

    // Apply saved settings
    const [savedTextColor, savedBackgroundColor] = savedColorScheme.split(',');
    document.documentElement.style.setProperty('--custom-font-family', savedFont);
    document.documentElement.style.setProperty('--custom-font-size', savedFontSize);
    document.documentElement.style.setProperty('--custom-text-align', savedTextAlign);
    document.documentElement.style.setProperty('--custom-text-color', savedTextColor);
    document.documentElement.style.setProperty('--custom-background-color', savedBackgroundColor);
    document.documentElement.style.setProperty('--custom-line-height', savedLineHeight);  // Thiết lập giãn dòng

    // Initialize UI with saved settings
    fontFamilySelector.value = savedFont.replace(/['"]/g, '');
    fontSizeInput.value = parseInt(savedFontSize, 10);
    textAlignSelector.value = savedTextAlign;
    colorSchemeSelector.value = savedColorScheme;
    lineHeightSelector.value = savedLineHeight;  // Hiển thị giãn dòng

    // Function to apply font settings including line-height
    function applyFontSettings(font, size, align, colorScheme, lineHeight) {
        const [textColor, backgroundColor] = colorScheme.split(',');
        document.documentElement.style.setProperty('--custom-font-family', `'${font}'`);
        document.documentElement.style.setProperty('--custom-font-size', `${size}px`);
        document.documentElement.style.setProperty('--custom-text-align', align);
        document.documentElement.style.setProperty('--custom-text-color', textColor);
        document.documentElement.style.setProperty('--custom-background-color', backgroundColor);
        document.documentElement.style.setProperty('--custom-line-height', lineHeight);  // Gán giãn dòng
        localStorage.setItem('custom-font-family', font);
        localStorage.setItem('custom-font-size', `${size}px`);
        localStorage.setItem('custom-text-align', align);
        localStorage.setItem('custom-color-scheme', colorScheme);
        localStorage.setItem('custom-line-height', lineHeight);  // Lưu giãn dòng
    }

    // Event listener for apply button
    applyButton.addEventListener('click', function () {
        const selectedFont = fontFamilySelector.value;
        const fontSize = fontSizeInput.value;
        const textAlign = textAlignSelector.value;
        const colorScheme = colorSchemeSelector.value;
        const lineHeight = lineHeightSelector.value;  // Nhận giá trị giãn dòng
        applyFontSettings(selectedFont, fontSize, textAlign, colorScheme, lineHeight);
    });
});

      const resetButton = document.getElementById('resetSettings');

// Event listener for reset button
resetButton.addEventListener('click', function () {
    localStorage.clear(); // Clear all local storage data
    location.reload(); // Reload the page
});

const toggleSettingsButton = document.getElementById('toggleSettings');
const fontSettings = document.getElementById('fontSettings');

toggleSettingsButton.addEventListener('click', (e) => {
    fontSettings.style.display =
        fontSettings.style.display === 'none' || fontSettings.style.display === '' ? 'block' : 'none';
    e.stopPropagation(); // Ngăn chặn sự kiện "click" lan ra bên ngoài
});


document.addEventListener('click', (e) => {
    if (!fontSettings.contains(e.target) && e.target !== toggleSettingsButton) {
        fontSettings.style.display = 'none';
    }
});
        function copyCurrentUrl() {
    navigator.clipboard.writeText(window.location.href)
      .then(() => alert('Đường dẫn chương đã được lưu vào bộ nhớ tạm.'))
      .catch(err => console.error('Lỗi sao chép URL: ', err));
  }
document.getElementById('closeSettings').addEventListener('click', () => {
    document.getElementById('fontSettings').style.display = 'none';
});

        function redirectToPage() {
    // Lấy URL từ sessionStorage (URL của trang A)
    const sourceUrl = sessionStorage.getItem('sourceUrl') || 'https://www.kunsnovel.pro.vn/'; 
    // Chuyển hướng về trang A
    window.location.href = sourceUrl;

    // Xóa URL khỏi sessionStorage sau khi chuyển hướng
    sessionStorage.removeItem('sourceUrl');
  }

  function redirectToPage() {
    // Quay lại trang trước, tương đương với nút "Back" trên trình duyệt
    window.history.back();
  }
    </script>