/* Root variables for themes */
:root {
    --background-color: #f8f8f8;
    --text-color: #333;
    --page-background: #fff;
    --page-border-color: #ddd;
    --separator-color: #ccc;
    --sidebar-background: #ececec;
    --sidebar-width-collapsed: 60px;
    --sidebar-width-expanded: 560px; /* MODIFIED: Was 280px */
}

/* Dark theme */
.dark-theme {
    --background-color: #222;
    --text-color: #eee;
    --page-background: #333;
    --page-border-color: #555;
    --separator-color: #666;
    --sidebar-background: #2a2a2a;
    /* Sidebar width expanded will be inherited from :root, no need to repeat unless specifically different for dark theme */
}

body {
    margin: 0;
    font-family: serif;
    line-height: 1.6;
    display: flex;
    min-height: 100vh;
    background-color: var(--background-color);
    color: var(--text-color);
    padding: 0;
    box-sizing: border-box;
    transition: background-color 0.3s, color 0.3s;
}

/* Side Menu Styles - Positioned on the RIGHT */
#side-menu {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: var(--sidebar-width-collapsed);
    background-color: var(--sidebar-background);
    color: var(--text-color);
    box-shadow: -2px 0 5px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    box-sizing: border-box;
    transition: width 0.3s ease-in-out, background-color 0.3s, color 0.3s;
    display: flex;
    flex-direction: column;
    align-items: center; /* For collapsed state icons */
    overflow-x: hidden;
    padding-top: 0;
}

#side-menu.expanded {
    width: var(--sidebar-width-expanded);
    align-items: stretch; /* MODIFIED: Stretch items like expanded-menu-content */
}

#menu-toggle-container {
    width: 100%;
    display: flex;
    justify-content: center; /* Default for collapsed */
    padding: 10px 0;
    box-sizing: border-box;
    position: sticky;
    top: 0;
    background-color: var(--sidebar-background);
    z-index: 1001;
    flex-shrink: 0; /* Prevent this from shrinking */
}

#side-menu.expanded #menu-toggle-container {
    justify-content: flex-end;
    padding-right: 15px;
}

#menu-toggle {
    font-size: 2em;
    cursor: pointer;
    color: var(--text-color);
    transition: color 0.3s;
    line-height: 1;
    position: relative;
}

.tooltiptext {
  visibility: hidden; width: 120px; background-color: #555; color: #fff;
  text-align: center; border-radius: 6px; padding: 5px 0; position: absolute;
  z-index: 1; bottom: 125%; left: 50%; margin-left: -60px;
  opacity: 0; transition: opacity 0.3s; font-size: 0.5em;
}
#menu-toggle:hover .tooltiptext { visibility: visible; opacity: 1; }

#collapsed-sidebar-content {
    display: flex; flex-direction: column; align-items: center;
    width: 100%; padding-top: 10px; flex-grow: 1; padding-bottom: 10px; overflow: hidden;
}
#side-menu.expanded #collapsed-sidebar-content { display: none; }

#collapsed-book-title {
    writing-mode: vertical-rl; text-orientation: mixed; transform: rotate(180deg);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    max-height: calc(100vh - 320px); /* Adjusted for more buttons */
    font-size: 0.9em; margin-top: 10px; margin-bottom: 15px; cursor: default;
}
#collapsed-progress { font-size: 0.9em; text-align: center; white-space: pre-wrap; line-height: 1.2; margin-bottom: 15px; }
#simple-page-info { display: block; }

/* Buttons on the collapsed sidebar spine */
#highlight-button-collapsed,
#fullscreen-button-collapsed,
#ai-debater-button-collapsed {
    margin-top: 10px;
}
#highlight-button-collapsed { margin-top: auto; }

#highlight-button-collapsed button,
#fullscreen-button-collapsed button,
#ai-debater-button-collapsed button {
    background: none; border: 1px solid var(--text-color); color: var(--text-color);
    border-radius: 50%; width: 36px; height: 36px; font-size: 1.2em;
    cursor: pointer; padding: 0; display: flex; align-items: center; justify-content: center;
    transition: background-color 0.2s, color 0.2s;
}
#highlight-button-collapsed button:hover,
#fullscreen-button-collapsed button:hover,
#ai-debater-button-collapsed button:hover {
    background-color: var(--text-color);
    color: var(--sidebar-background);
}
#fullscreen-button-collapsed { margin-top: 10px; }
#ai-debater-button-collapsed { margin-top: 10px; margin-bottom: 5px; }

/* MODIFIED: expanded-menu-content for flex layout */
#expanded-menu-content {
    width: 100%;
    padding: 0 15px 15px 15px; /* Add padding for content inside */
    box-sizing: border-box;
    flex-grow: 1; /* Takes available vertical space in #side-menu */
    display: flex;
    flex-direction: column;
    overflow: hidden; /* Prevent this main container from scrolling */
}
#expanded-menu-content.hidden { /* Used by script to initially hide it */
    display: none !important; /* Important to override inline styles if any */
}

.menu-section {
    /* padding-left & right are now on #expanded-menu-content */
    /* margin-bottom is now handled by :not(:last-child) */
    flex-shrink: 0; /* Prevent sections like Settings toggle from shrinking too much */
}
.menu-section:not(:last-child) {
    margin-bottom: 15px;
}
.menu-section:first-child { /* For the Settings toggle section */
    margin-top: 10px;
}

.menu-section h4 { margin-top: 0; margin-bottom: 10px; } /* Default for section titles */

#collapsible-settings-content .menu-subsection:not(:last-child) {
    margin-bottom: 10px;
}
.menu-subsection h5 { margin-top: 0; margin-bottom: 5px; font-size: 1em; }
.menu-section label { display: block; margin-bottom: 5px; cursor: pointer; }


/* Styles for collapsible Settings section */
#settings-toggle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    /* margin-bottom: 10px; /* Removed, handled by .menu-section margin */
}

#settings-arrow {
    margin-left: 8px;
    font-size: 0.8em;
    transition: transform 0.2s ease-in-out;
}

.collapsible-content.hidden { /* General class for collapsible content, managed by script */
    display: none;
}
/* If script uses #expanded-menu-content .hidden, this is a fallback */
#expanded-menu-content > .hidden {
    display: none !important;
}


#progress-section-expanded { /* Script handles display: none when menu is expanded */
    /* flex-shrink: 0; */ /* Already part of .menu-section */
}
#progress-section-expanded #menu-page-info { font-weight: bold; }

.menu-subsection #fullscreen-button-expanded {
    padding: 6px 12px; background-color: var(--sidebar-background); color: var(--text-color);
    border: 1px solid var(--page-border-color); border-radius: 4px; cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s; font-size: 0.9em; margin-top: 5px;
}
.menu-subsection #fullscreen-button-expanded:hover { border-color: var(--text-color); }

/* MODIFIED: ai-debater-section for flex layout */
#ai-debater-section {
    border-top: 1px solid var(--separator-color);
    padding-top: 15px;
    display: flex;
    flex-direction: column;
    flex-grow: 1; /* Key to fill remaining space */
    min-height: 0; /* Essential for nested flex scrolling */
}

/* MODIFIED: ai-chat-window for flex layout */
#ai-chat-window {
    display: flex;
    flex-direction: column;
    /* height: 300px; /* REMOVED fixed height */
    /* max-height: calc(100vh - 400px); /* REMOVED to allow full flex grow */
    flex-grow: 1; /* Key to fill #ai-debater-section */
    border: 1px solid var(--page-border-color);
    border-radius: 4px;
    overflow: hidden; /* Keep, scrolling is for messages */
    min-height: 0; /* Essential for #ai-chat-messages to scroll */
}

#ai-chat-messages {
    flex-grow: 1;
    overflow-y: auto; /* This is where scrolling for messages happens */
    padding: 10px;
    background-color: var(--background-color);
}
.chat-message { margin-bottom: 10px; padding: 8px; border-radius: 4px; line-height: 1.4; max-width: 90%; }
.chat-message.user { background-color: var(--separator-color); margin-left: auto; text-align: right; }
.chat-message.ai { background-color: var(--page-background); border: 1px solid var(--page-border-color); margin-right: auto; }

#ai-chat-input-area {
    display: flex;
    padding: 10px;
    border-top: 1px solid var(--page-border-color);
    background-color: var(--sidebar-background);
    flex-shrink: 0; /* Prevent input area from shrinking */
}
#ai-user-input {
    flex-grow: 1; padding: 8px; border: 1px solid var(--page-border-color);
    border-radius: 4px; resize: none; margin-right: 8px; min-height: 40px;
    font-family: inherit; font-size: 0.9em;
    background-color: var(--background-color); color: var(--text-color);
}
#ai-send-button {
    padding: 8px 15px; border: none; background-color: var(--text-color);
    color: var(--sidebar-background); border-radius: 4px; cursor: pointer;
    transition: opacity 0.2s;
}
#ai-send-button:hover { opacity: 0.8; }


#book-container {
    flex-grow: 1; display: flex; justify-content: center; padding: 20px; gap: 20px;
    overflow: hidden; box-sizing: border-box; position: relative;
    margin-right: var(--sidebar-width-collapsed);
    transition: margin-right 0.3s ease-in-out;
}
body.menu-expanded #book-container { margin-right: var(--sidebar-width-expanded); }

.page {
    background-color: var(--page-background); padding: 20px; overflow-y: hidden;
    box-sizing: border-box; max-width: 100%; scrollbar-width: none; -ms-overflow-style: none;
}
.page::-webkit-scrollbar { display: none; }
.text-justify .page p { text-align: justify; }
.page h1, .page h2, .page h3, .page .section-number { margin-top: 2.5em; margin-bottom: 0.5em; padding: 0; }
.page p, .page hr, .page em { margin-top: 0; margin-bottom: 1em; padding: 0; }
.page h1 { font-size: 2em; text-align: center; }
.page h2 { font-size: 1.5em; text-align: center; }
.page h3 { font-size: 1.2em; text-align: center; }
.page .section-number { display: block; text-align: center; font-size: 1.1em; font-weight: bold; }
.page hr { border: none; border-top: 1px solid var(--separator-color); margin: 1em auto; width: 50%; }
.page em { font-style: italic; display: block; }
#page-right { display: none; } /* Hidden by default, shown in @media query */

#loading-area {
    text-align: center; padding: 20px; border: 1px solid var(--page-border-color);
    background-color: var(--page-background); color: var(--text-color); max-width: 600px;
    margin: 50px auto; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    transition: background-color 0.3s, color 0.3s, border-color 0.3s; position: relative; z-index: 1;
}
.load-option { margin-bottom: 20px; border-bottom: 1px dashed var(--separator-color); padding-bottom: 20px; }
.load-option:last-child { border-bottom: none; padding-bottom: 0; }
.load-option h3 { margin-top: 0; }
#book-search, #url-input {
    padding: 8px; box-sizing: border-box; background-color: var(--background-color);
    color: var(--text-color); border: 1px solid var(--page-border-color);
    transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}
#book-search { width: calc(100% - 18px); margin-bottom: 10px; }
#url-input { margin-right: 5px; width: calc(60% - 18px); }
#repository-books-list, #recent-books-list, #saved-books-list {
    list-style: none; padding: 0; margin: 10px 0; max-height: 200px; overflow-y: auto;
    border: 1px solid var(--page-border-color); background-color: var(--background-color); text-align: left;
}
#repository-books-list li, #recent-books-list li, #saved-books-list li {
    padding: 8px; border-bottom: 1px solid var(--page-border-color); cursor: pointer;
    transition: background-color 0.2s; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#repository-books-list li:last-child, #recent-books-list li:last-child, #saved-books-list li:last-child { border-bottom: none; }
#repository-books-list li:hover, #recent-books-list li:hover, #saved-books-list li:hover { background-color: var(--separator-color); }
#load-url, #clear-recent-books, #clear-saved-books {
    padding: 8px 15px; cursor: pointer; background-color: var(--sidebar-background);
    color: var(--text-color); border: 1px solid var(--page-border-color);
    transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}
#clear-recent-books, #clear-saved-books { margin-top: 10px; padding: 5px 10px; }
#loading-status { color: var(--text-color); font-style: italic; transition: color 0.3s; }

body.book-loaded #loading-area { display: none; }
body.book-loaded #book-container { display: flex !important; } /* Ensure it's flex */
body.book-loaded #side-menu { display: flex !important; } /* Ensure it's flex */

body.book-loaded #collapsed-book-title { display: block; }
body.book-loaded #collapsed-progress { display: block; }
body.book-loaded #ai-debater-button-collapsed { display: block; }
body.book-loaded #fullscreen-button-collapsed { display: block; }
body.book-loaded #highlight-button-collapsed {display: block; }

@media (min-width: 768px) {
    #book-container { flex-direction: row; }
    .page { flex-basis: 48%; max-height: calc(100vh - 40px); border: none; box-shadow: none; }
    #page-right { display: block; }
    #book-container::after {
        content: ''; position: absolute; top: 20px; bottom: 20px; left: 50%;
        width: 1px; background-color: var(--separator-color);
        transform: translateX(-50%); transition: background-color 0.3s;
    }
}

#overlay-mobile {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.5); z-index: 999;
    opacity: 0; visibility: hidden;
    transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
    display: none; /* Script toggles .active class */
}
#overlay-mobile.active { display: block; opacity: 1; visibility: visible; }

@media (prefers-color-scheme: dark) {
    body.system-theme:not(.light-theme) {
        --background-color: #222; --text-color: #eee;
        --page-background: #333; --page-border-color: #555;
        --separator-color: #666; --sidebar-background: #2a2a2a;
    }
}

@media (max-width: 767px) {
    .page { max-height: calc(100vh - 40px); }
    #side-menu {
        /* Uses the new wider var(--sidebar-width-expanded) for off-screen positioning and width */
        right: calc(-1 * var(--sidebar-width-expanded));
        width: var(--sidebar-width-expanded);
        transition: right 0.3s ease-in-out, width 0.3s ease-in-out; /* Added width transition */
        box-shadow: -2px 0 5px rgba(0,0,0,0.2);
    }
    #side-menu.expanded {
        right: 0;
    }
    #collapsed-sidebar-content { display: none !important; } /* Important for mobile when expanded */
    #book-container { margin-right: 0 !important; } /* No margin when sidebar is off-screen or overlaying */
    
    #menu-toggle-container { /* On mobile, this is the visible part to tap */
        position: fixed; /* Keeps it on screen if #side-menu slides out */
        top: 0px;
        right: 0px; /* Attach to the right edge of viewport */
        width: auto; /* Let content define width */
        padding: 10px 15px;
        z-index: 1001; /* Above side-menu before it's fully expanded */
        background-color: var(--sidebar-background);
        border-bottom-left-radius: 5px;
    }
     #side-menu.expanded #menu-toggle-container { /* When menu is open on mobile */
        position: sticky; /* Revert to sticky within the opened menu */
        justify-content: flex-end;
        /* padding-right: 15px; /* Ensured */
     }
}