/* Book Reader - Notion-Style CSS */

/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --font-sans: "Inter", sans-serif;
    --text-color: rgb(25, 25, 24);
    --bg-color: #fafafa;
    --border-color: rgba(55, 53, 47, 0.12);
    --space-8: 8px;
    --space-12: 12px;
    --space-16: 16px;
    --space-20: 20px;
    --space-24: 24px;
    --space-32: 32px;
}

body {
    font-family: var(--font-sans);
    color: var(--text-color);
    background-color: var(--bg-color);
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow: hidden;
}

/* Library View */
.container {
    max-width: 900px;
    margin: 0 auto;
    padding: var(--space-32) var(--space-24);
}

.library-view {
    height: calc(100vh - 55px);
    overflow-y: auto;
}

/* ========== APP HEADER ========== */
.app-header {
    text-align: center;
    margin-bottom: var(--space-24);
    padding: var(--space-16) var(--space-24);
    margin: calc(-1 * var(--space-32)) calc(-1 * var(--space-24)) var(--space-24);
}

.app-header__title {
    font-size: 36px;
    font-weight: 700;
    line-height: 1.15;
    letter-spacing: -0.025em;
    color: var(--text-color);
    margin: 0;
    padding-bottom: var(--space-16);
}

.app-header__title::after {
    content: "";
    display: block;
    width: 40%;
    height: 1px;
    background: var(--border-color);
    margin: var(--space-16) auto 0;
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 24px;
    padding-bottom: 32px;
}

.book-card {
    cursor: pointer;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
    background: #fff;
    border: 0.5px solid rgba(55, 53, 47, 0.16);
}

.book-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.book-cover {
    width: 100%;
    height: 238px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
    color: rgba(255, 255, 255, 0.95);
    line-height: 1.4;
}

.book-info {
    padding: 16px;
    background: #fff;
}

.book-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 4px;
    color: rgb(25, 25, 24);
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
}

.book-author {
    font-size: 14px;
    color: rgba(25, 25, 24, 0.6);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.add-book-card {
    cursor: pointer;
    border-radius: 8px;
    border: 1px dashed rgba(55, 53, 47, 0.16);
    height: 323px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    background: rgba(55, 53, 47, 0.03);
}

.add-book-card:hover {
    border-color: #2383E2;
    background: rgba(35, 131, 226, 0.05);
}

.add-book-icon {
    font-size: 48px;
    color: rgba(25, 25, 24, 0.4);
    margin-bottom: 8px;
}

.add-book-text {
    font-size: 16px;
    font-weight: 500;
    color: rgba(25, 25, 24, 0.6);
}

/* Empty State */
.empty-state {
    padding: 64px 24px;
    text-align: center;
}

.empty-state-content h2 {
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 12px;
    color: rgb(25, 25, 24);
}

.empty-state-content p {
    font-size: 18px;
    color: rgba(25, 25, 24, 0.6);
}

/* Login Prompt */
.login-prompt {
    max-width: 400px;
    margin: 0 auto;
}

.login-prompt h3 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 12px;
    color: rgb(25, 25, 24);
}

.login-prompt p {
    font-size: 16px;
    color: rgba(25, 25, 24, 0.6);
    margin-bottom: 24px;
    line-height: 1.5;
}

.login-button {
    display: inline-block;
    padding: 12px 24px;
    background: #2383E2;
    color: white;
    text-decoration: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    transition: background 0.2s;
}

.login-button:hover {
    background: #1B6DC1;
}

.login-button:active {
    background: #155A9E;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: #fff;
    border-radius: 8px;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow: auto;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-header {
    padding: 24px;
    border-bottom: 0.5px solid rgba(55, 53, 47, 0.16);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h2 {
    font-size: 22px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 32px;
    color: rgba(25, 25, 24, 0.4);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.2s;
}

.modal-close:hover {
    background: rgba(55, 53, 47, 0.08);
}

.upload-area {
    margin: 24px;
    border: 1px dashed rgba(55, 53, 47, 0.16);
    border-radius: 8px;
    padding: 48px 24px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
}

.upload-area:hover {
    border-color: #2383E2;
    background: rgba(35, 131, 226, 0.05);
}

.upload-area.drag-over {
    border-color: #2383E2;
    background: rgba(35, 131, 226, 0.1);
}

.upload-prompt svg {
    color: rgba(25, 25, 24, 0.4);
    margin-bottom: 16px;
}

.upload-title {
    font-size: 18px;
    font-weight: 500;
    margin-bottom: 8px;
    color: rgb(25, 25, 24);
}

.upload-subtitle {
    font-size: 16px;
    color: rgba(25, 25, 24, 0.6);
}

.upload-progress {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(35, 131, 226, 0.2);
    border-top-color: #2383E2;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.upload-error {
    margin: 0 24px 24px;
    padding: 12px 16px;
    background: rgba(235, 87, 87, 0.1);
    border: 0.5px solid rgba(55, 53, 47, 0.16);
    border-radius: 6px;
    color: rgb(200, 30, 30);
    font-size: 14px;
}

/* Reader View */
.reader-view {
    display: grid;
    grid-template-columns: 280px 1fr 360px;
    height: calc(100vh - 55px);
    overflow: hidden;
}

/* Table of Contents Sidebar */
.toc-sidebar {
    background: #f7f6f3;
    border-right: 0.5px solid rgba(55, 53, 47, 0.16);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.back-button {
    margin: 16px;
    padding: 10px 16px;
    background: #fff;
    border: 0.5px solid rgba(55, 53, 47, 0.16);
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    color: rgb(25, 25, 24);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s;
    font-family: "Inter", sans-serif;
}

.back-button:hover {
    background: rgba(55, 53, 47, 0.08);
}

.toc-header {
    padding: 16px 16px 12px;
}

.toc-header h3 {
    font-size: 14px;
    font-weight: 600;
    color: rgba(25, 25, 24, 0.6);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.toc-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px 16px;
}

.toc-item {
    padding: 8px 12px;
    margin-bottom: 2px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    color: rgba(25, 25, 24, 0.8);
    transition: background 0.2s;
    user-select: none;
}

.toc-item:hover {
    background: rgba(55, 53, 47, 0.08);
}

.toc-item.active {
    background: rgba(35, 131, 226, 0.1);
    color: #2383E2;
    font-weight: 500;
}

/* Reading Area */
.reading-area {
    overflow-y: auto;
    padding: 48px 80px;
    background: #fff;
}

.book-header {
    margin-bottom: 48px;
}

.book-header .book-title {
    font-size: 36px;
    font-weight: 600;
    line-height: 1.25;
    letter-spacing: -0.01em;
    margin-bottom: 8px;
}

.book-header .book-author {
    font-size: 18px;
    color: rgba(25, 25, 24, 0.6);
}

.book-content {
    max-width: 720px;
}

.chapter {
    margin-bottom: 48px;
}

.chapter-title {
    font-size: 28px;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    margin-bottom: 24px;
    padding-top: 24px;
    border-top: 0.5px solid rgba(55, 53, 47, 0.16);
}

.chapter:first-child .chapter-title {
    border-top: none;
    padding-top: 0;
}

.chapter-content {
    font-size: 18px;
    line-height: 1.55;
    letter-spacing: 0.2px;
    color: rgba(25, 25, 24, 0.9);
}

.chapter-content p {
    margin-bottom: 16px;
}

.chapter-content h1,
.chapter-content h2,
.chapter-content h3 {
    margin-top: 32px;
    margin-bottom: 16px;
    font-weight: 600;
}

.chapter-content h1 {
    font-size: 28px;
}

.chapter-content h2 {
    font-size: 22px;
}

.chapter-content h3 {
    font-size: 18px;
}

.chapter-content img {
    max-width: 100%;
    height: auto;
    margin: 24px 0;
    border-radius: 6px;
}

.chapter-content::selection {
    background: rgba(35, 131, 226, 0.28);
}

/* Chat Sidebar */
.chat-sidebar {
    background: #f7f6f3;
    border-left: 0.5px solid rgba(55, 53, 47, 0.16);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: calc(100vh - 55px);
    max-height: calc(100vh - 55px);
}

.chat-header {
    padding: 24px 20px 16px;
    border-bottom: 0.5px solid rgba(55, 53, 47, 0.16);
    flex-shrink: 0;
}

.chat-header h3 {
    font-size: 18px;
    font-weight: 600;
    color: rgb(25, 25, 24);
}

.api-key-section {
    padding: 16px 20px;
    border-bottom: 0.5px solid rgba(55, 53, 47, 0.16);
}

.api-key-section label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: rgba(25, 25, 24, 0.7);
    margin-bottom: 8px;
}

.api-key-input-group {
    display: flex;
    gap: 8px;
}

.api-key-input-group input {
    flex: 1;
    padding: 8px 12px;
    border: 0.5px solid rgba(55, 53, 47, 0.16);
    border-radius: 6px;
    font-size: 14px;
    font-family: "Inter", sans-serif;
    background: #fff;
    color: rgb(25, 25, 24);
}

.api-key-input-group input:focus {
    outline: none;
    border-color: #2383E2;
}

.api-key-input-group button {
    padding: 8px 16px;
    background: #2383E2;
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    font-family: "Inter", sans-serif;
}

.api-key-input-group button:hover {
    background: #1B6DC1;
}

.api-key-input-group button:active {
    background: #155A9E;
}

.api-key-status {
    margin-top: 8px;
    font-size: 13px;
    min-height: 18px;
}

.api-key-status.success {
    color: #0b6e4f;
}

.api-key-status.error {
    color: #c81e1e;
}

.api-key-help {
    margin-top: 8px;
    font-size: 12px;
    color: rgba(25, 25, 24, 0.5);
    line-height: 1.4;
}

.selected-context {
    padding: 16px 20px;
    border-bottom: 0.5px solid rgba(55, 53, 47, 0.16);
    background: rgba(35, 131, 226, 0.05);
    flex-shrink: 0;
}

.context-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.context-header span {
    font-size: 13px;
    font-weight: 500;
    color: rgba(25, 25, 24, 0.7);
}

.context-header button {
    padding: 4px 8px;
    background: none;
    border: none;
    color: #2383E2;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.2s;
}

.context-header button:hover {
    background: rgba(35, 131, 226, 0.1);
}

.selected-text {
    font-size: 13px;
    line-height: 1.5;
    color: rgba(25, 25, 24, 0.8);
    max-height: 80px;
    overflow-y: auto;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    min-height: 0;
}

.chat-empty-state {
    text-align: center;
    padding: 32px 16px;
}

.chat-empty-state p {
    font-size: 14px;
    color: rgba(25, 25, 24, 0.5);
    line-height: 1.5;
}

.message {
    margin-bottom: 20px;
}

.message-header {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: rgba(25, 25, 24, 0.7);
}

.message-content {
    padding: 8px 14px;
    border-radius: 20px;
    font-size: 15px;
    line-height: 1.45;
    word-wrap: break-word;
    box-shadow: 0 6px 18px rgba(15, 20, 25, 0.04);
    transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.message-content:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 28px rgba(15, 20, 25, 0.06);
}

.message.user .message-content {
    background: #0a84ff;
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.06);
    box-shadow: 0 8px 24px rgba(10, 132, 255, 0.15);
}

.message.user .message-content:hover {
    box-shadow: 0 12px 32px rgba(10, 132, 255, 0.2);
}

.message.assistant .message-content {
    background: rgba(55, 53, 47, 0.06);
    border: 0.5px solid rgba(55, 53, 47, 0.12);
    color: rgba(25, 25, 24, 0.9);
}

/* Markdown styling within message content */
.message-content p {
    margin: 0 0 8px;
}

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

.message-content ul,
.message-content ol {
    margin: 8px 0;
    padding-left: 20px;
}

.message-content li {
    margin: 4px 0;
}

.message-content code {
    background: rgba(55, 53, 47, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: ui-monospace, "SF Mono", Monaco, "Cascadia Code", "Roboto Mono",
        Consolas, "Courier New", monospace;
}

.message.user .message-content code {
    background: rgba(255, 255, 255, 0.2);
}

.message-content pre {
    background: rgba(55, 53, 47, 0.06);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.message.user .message-content pre {
    background: rgba(255, 255, 255, 0.15);
}

.message-content pre code {
    background: none;
    padding: 0;
}

.message-content strong {
    font-weight: 600;
}

.message-content h1,
.message-content h2,
.message-content h3 {
    margin: 12px 0 8px;
    font-weight: 600;
}

.message-content h1 {
    font-size: 18px;
}

.message-content h2 {
    font-size: 16px;
}

.message-content h3 {
    font-size: 15px;
}

.message-content a {
    color: #0a84ff;
    text-decoration: underline;
}

.message.user .message-content a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: underline;
}

/* Typing animation */
.typing-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #9aa3ad;
    margin-right: 6px;
    animation: blink 1s infinite;
}

.message.user .message-content .typing-dot {
    background: rgba(255, 255, 255, 0.7);
}

@keyframes blink {
    0% {
        opacity: 0.2;
    }
    50% {
        opacity: 1;
    }
    100% {
        opacity: 0.2;
    }
}

.chat-input-container {
    padding: 16px 20px;
    border-top: 0.5px solid rgba(55, 53, 47, 0.16);
    display: flex;
    gap: 8px;
    background: #f7f6f3;
    flex-shrink: 0;
}

.chat-input-container textarea {
    flex: 1;
    padding: 10px 12px;
    border: 0.5px solid rgba(55, 53, 47, 0.16);
    border-radius: 6px;
    font-size: 14px;
    font-family: "Inter", sans-serif;
    resize: none;
    background: #fff;
}

.chat-input-container textarea:focus {
    outline: none;
    border-color: #2383E2;
}

.chat-input-container button {
    width: 40px;
    height: 40px;
    background: #000000;
    color: #fff;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: 500;
    font-family: "Inter", sans-serif;
    white-space: nowrap;
}

.chat-input-container button:hover {
    background: #1a1a1a;
}

.chat-input-container button:active {
    background: #333333;
}

.chat-input-container button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .reader-view {
        grid-template-columns: 240px 1fr 300px;
        height: calc(100vh - 55px);
    }

    .reading-area {
        padding: 32px 40px;
    }
}

@media (max-width: 768px) {
    .container {
        padding: var(--space-24) var(--space-16);
    }

    .app-header {
        padding: var(--space-12) var(--space-16);
        margin: calc(-1 * var(--space-24)) calc(-1 * var(--space-16)) var(--space-20);
    }

    .app-header__title {
        font-size: 28px;
    }

    .reader-view {
        grid-template-columns: 1fr;
    }

    .toc-sidebar,
    .chat-sidebar {
        display: none;
    }

    .reading-area {
        padding: 24px 20px;
    }

    .library-view {
        height: calc(100vh - 55px);
        overflow-y: auto;
    }

    .library-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 16px;
    }
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

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

::-webkit-scrollbar-thumb {
    background: rgba(25, 25, 24, 0.2);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(25, 25, 24, 0.3);
}