:root {
    --bg-main: #0f111a;
    --bg-pane: #1a1d27;
    --bg-panel: rgba(30, 34, 45, 0.7);
    --border-color: rgba(255, 255, 255, 0.1);
    --text-primary: #f8f9fa;
    --text-muted: #9ba1a6;
    --accent: #6c5ce7;
    --accent-hover: #5a4bcf;
    --user-msg-bg: #6c5ce7;
    --ai-msg-bg: #2d3243;
    --handle-color: #3f4458;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    color: var(--text-primary);
    background-color: var(--bg-main);
    height: 100vh;
    overflow: hidden;
}

/* Typography Utilities */
.text-center {
    text-align: center;
}

.text-muted {
    color: var(--text-muted);
}

.text-accent {
    color: var(--accent);
}

.text-sm {
    font-size: 0.85rem;
}

.mb-4 {
    margin-bottom: 1rem;
}

.mt-2 {
    margin-top: 0.5rem;
}

.mt-4 {
    margin-top: 1rem;
}

.w-full {
    width: 100%;
}

.inline-block {
    display: inline-block;
}

/* Layout */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.left-pane {
    flex: 1;
    min-width: 300px;
    background-color: var(--bg-pane);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

.right-pane {
    width: 400px;
    min-width: 300px;
    max-width: 800px;
    background-color: var(--bg-pane);
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--border-color);
}

.resize-handle {
    width: 5px;
    cursor: col-resize;
    background-color: var(--handle-color);
    transition: background-color 0.2s;
    z-index: 10;
}

.resize-handle:hover,
.resize-handle:active {
    background-color: var(--accent);
}

/* Glassmorphism */
.glass-panel {
    background: var(--bg-panel);
    backdrop-filter: blur(10px);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

/* UI Elements */
.btn {
    display: inline-block;
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--accent);
    color: white;
    box-shadow: 0 4px 15px rgba(108, 92, 231, 0.3);
}

.btn-primary:hover {
    background-color: var(--accent-hover);
    transform: translateY(-1px);
}

.icon-btn {
    background: transparent;
    color: var(--text-muted);
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0.5rem;
    border-radius: 50%;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
}

.input-field {
    width: 100%;
    background-color: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.8rem 1rem;
    border-radius: 8px;
    font-size: 1rem;
    font-family: inherit;
}

.input-field:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(108, 92, 231, 0.2);
}

#pdf-viewer,
#epub-viewer {
    width: 100%;
    height: 100%;
    border: none;
    background-color: #f0f0f0;
    /* standard browser iframe bg */
}

#epub-viewer {
    padding: 2% 10%;
    overflow-y: hidden;
}

/* EPUB Specific Controls */
.epub-controls {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 1rem;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    padding: 0.5rem 1rem;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 100;
}

.epub-controls .icon-btn {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

.epub-controls .icon-btn:hover {
    background: var(--accent);
}

.close-doc-btn {
    position: absolute;
    top: 15px;
    right: 15px;
    z-index: 100;
    background-color: rgba(0, 0, 0, 0.6);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 0.6rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    backdrop-filter: blur(4px);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
    font-size: 0.9rem;
}

.close-pdf-btn:hover {
    background-color: #e74c3c;
    border-color: #e74c3c;
    transform: translateY(-1px);
}

.hidden {
    display: none !important;
}

/* Chat Interface */
.chat-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(26, 29, 39, 0.9);
}

.header-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.header-title h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    scroll-behavior: smooth;
}

.message {
    max-width: 85%;
    padding: 0.8rem 1.2rem;
    border-radius: 12px;
    font-size: 0.95rem;
    line-height: 1.5;
    word-break: break-word;
    animation: fadeIn 0.3s ease;
}

.system-message {
    align-self: center;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    font-size: 0.85rem;
    border-radius: 8px;
    max-width: 90%;
    text-align: center;
}

.user-message {
    align-self: flex-end;
    background: var(--user-msg-bg);
    border-bottom-right-radius: 4px;
}

.ai-message {
    align-self: flex-start;
    background: var(--ai-msg-bg);
    border-bottom-left-radius: 4px;
}

.ai-message p {
    margin-bottom: 0.8rem;
}

.ai-message p:last-child {
    margin-bottom: 0;
}

.ai-message pre {
    background: #1e1e1e !important;
    padding: 1rem;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.ai-message code {
    font-family: 'Fira Code', monospace;
    font-size: 0.9em;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

.chat-input-container {
    padding: 1rem;
    background: rgba(26, 29, 39, 0.95);
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

#chat-input {
    flex: 1;
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.8rem 1rem;
    border-radius: 20px;
    font-size: 0.95rem;
    font-family: inherit;
    resize: none;
    max-height: 150px;
    min-height: 44px;
    overflow-y: auto;
    transition: border-color 0.2s;
}

#chat-input:focus {
    outline: none;
    border-color: var(--accent);
}

.send-btn {
    background: var(--accent);
    color: white;
    margin-bottom: 2px;
}

.send-btn:hover {
    background: var(--accent-hover);
    color: white;
}

.send-btn:disabled {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.3);
    cursor: not-allowed;
}

/* Modals */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    width: 90%;
    max-width: 450px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.modal-header h2 {
    font-size: 1.2rem;
    font-weight: 600;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

/* Scrollbar formatting */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.25);
}

/* === Light Theme === */
body.light-theme {
    --bg-main: #f0f2f5;
    --bg-pane: #ffffff;
    --bg-panel: rgba(255, 255, 255, 0.85);
    --border-color: rgba(0, 0, 0, 0.08);
    --text-primary: #1a1a2e;
    --text-muted: #6b7280;
    --ai-msg-bg: #eef0f4;
    --handle-color: #d1d5db;
}

body.light-theme #pdf-viewer,
body.light-theme #epub-viewer {
    background-color: #fff;
}

body.light-theme .close-doc-btn,
body.light-theme .close-pdf-btn {
    background-color: rgba(255, 255, 255, 0.9);
    color: #1a1a2e;
    border-color: rgba(0, 0, 0, 0.12);
}

body.light-theme .epub-controls {
    background: rgba(255, 255, 255, 0.92);
    border-color: rgba(0, 0, 0, 0.1);
}

body.light-theme .epub-controls .icon-btn {
    color: #1a1a2e;
    background: rgba(0, 0, 0, 0.05);
}

body.light-theme .system-message {
    background: rgba(0, 0, 0, 0.04);
}

body.light-theme .loading-overlay {
    background: rgba(240, 242, 245, 0.92);
}

body.light-theme .toc-sidebar {
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.1);
}

body.light-theme ::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
}

body.light-theme ::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.25);
}

body.light-theme #chat-input {
    background-color: rgba(0, 0, 0, 0.03);
}

body.light-theme .ai-message pre {
    background: #1e1e1e !important;
}

/* === Header Actions === */
.header-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

/* === Loading Overlay === */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 17, 26, 0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 300;
    gap: 1rem;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading-overlay p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* === TOC Sidebar === */
.toc-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    width: 300px;
    height: 100%;
    background: var(--bg-pane);
    border-right: 1px solid var(--border-color);
    z-index: 200;
    display: flex;
    flex-direction: column;
    animation: slideIn 0.2s ease;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
}

.toc-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.toc-header h3 {
    font-size: 1rem;
    font-weight: 600;
}

.toc-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
}

.toc-item {
    display: block;
    padding: 0.6rem 1.5rem;
    color: var(--text-primary);
    text-decoration: none;
    font-size: 0.9rem;
    transition: background 0.15s, border-color 0.15s;
    border-left: 3px solid transparent;
}

.toc-item:hover {
    background: rgba(108, 92, 231, 0.1);
    border-left-color: var(--accent);
}

.toc-subitem {
    padding-left: 2.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

@keyframes slideIn {
    from {
        transform: translateX(-100%);
    }

    to {
        transform: translateX(0);
    }
}

/* === Selection Popup === */
.selection-popup {
    position: absolute;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 150;
    animation: fadeIn 0.2s ease;
}

.btn-sm {
    padding: 0.4rem 1rem;
    font-size: 0.85rem;
    border-radius: 20px;
}

/* === Mobile Responsive === */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }

    .left-pane {
        min-width: 100%;
        min-height: 40vh;
        flex: 1;
    }

    .right-pane {
        width: 100% !important;
        min-width: 100%;
        max-width: 100%;
        height: 60vh;
        flex: none;
        border-left: none;
        border-top: 1px solid var(--border-color);
    }

    .resize-handle {
        display: none;
    }

    .toc-sidebar {
        width: 100%;
    }

    .epub-controls {
        bottom: 10px;
        padding: 0.4rem 0.8rem;
        gap: 0.5rem;
    }

    .glass-panel {
        padding: 1.5rem;
        margin: 1rem;
    }
}