@import url('theme.css');

/* 阅读模式 — 使用 theme.css 统一深色主题 */
:root {
    --reading-header-h: 56px;
    --reading-radius: 12px;
    --annotation-sidebar-w: 220px;
    --reading-chat-w: 320px;
    --reader-panel-pad-y: 20px;
    --reader-panel-pad-bottom: 28px;
    --reader-max-height: calc(
        100vh - var(--reading-header-h) - var(--reader-panel-pad-y) - var(--reader-panel-pad-bottom) - 8px
    );
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body.reading-page {
    font-family: var(--c-font-family);
    background: var(--c-bg-body);
    color: var(--c-text);
    min-height: 100vh;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.reading-header {
    height: var(--reading-header-h);
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid var(--c-border);
    background: var(--c-bg-main);
    backdrop-filter: blur(8px);
    position: sticky;
    top: 0;
    z-index: 20;
}

.reading-header__left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
}

.reading-header__title-row {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
    flex: 1;
    flex-wrap: nowrap;
}

.reading-header__title {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-shrink: 1;
    min-width: 0;
    max-width: 36%;
}

.reading-header__subtitle {
    font-size: 13px;
    color: var(--c-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-shrink: 0;
    max-width: 22%;
}

.reading-header__progress {
    font-size: 13px;
    color: var(--c-text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}

.reading-header__progress::before {
    content: '·';
    margin-right: 8px;
    color: var(--c-text-faint);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.header-btn {
    border: 1px solid var(--c-border);
    background: var(--c-bg-surface);
    color: var(--c-text);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.header-btn:hover {
    background: var(--c-bg-card-hover);
    border-color: var(--c-border-strong);
}

.header-btn--icon {
    width: 36px;
    height: 36px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

.header-btn--active {
    border-color: var(--c-accent-border);
    background: var(--c-accent-soft);
    color: var(--c-accent);
}

.reading-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.book-library[hidden],
.reader-panel[hidden] {
    display: none !important;
}

.book-library {
    padding: 24px;
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}

.book-library__intro h2 {
    margin: 0 0 8px;
    font-size: 22px;
}

.book-library__intro p {
    margin: 0 0 24px;
    color: var(--c-text-muted);
}

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

.book-grid__loading,
.book-grid__empty,
.book-grid__error {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--c-text-muted);
    padding: 48px 16px;
}

.book-card--unavailable {
    opacity: 0.55;
    cursor: not-allowed;
}

.book-card--unavailable:hover {
    transform: none;
    border-color: var(--c-border);
    box-shadow: none;
    background: var(--c-bg-card);
}

.book-card__status {
    margin-top: auto;
    font-size: 12px;
    color: var(--c-danger);
}

.book-card {
    border: 1px solid var(--c-border);
    background: var(--c-bg-card);
    border-radius: var(--reading-radius);
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
    display: flex;
    flex-direction: column;
    min-height: 280px;
}

.book-card:hover {
    transform: translateY(-2px);
    border-color: var(--c-accent-border);
    box-shadow: var(--c-shadow-card);
    background: var(--c-bg-card-hover);
}

.book-card__cover {
    aspect-ratio: 3 / 4;
    background: linear-gradient(135deg, var(--c-bg-raised), var(--c-bg-code));
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.book-card__cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.book-card__cover-placeholder {
    font-size: 48px;
    opacity: 0.5;
}

.book-card__body {
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

.book-card__title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.35;
}

.book-card__author {
    margin: 0;
    font-size: 13px;
    color: var(--c-text-muted);
}

.book-card__progress {
    margin-top: auto;
    font-size: 12px;
    color: var(--c-success);
}

.book-card__progress--new {
    color: var(--c-text-muted);
}

/* 阅读区：三栏布局（标注树 + EPUB + 对话面板） */
.reader-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: stretch;
    gap: 16px;
    padding: var(--reader-panel-pad-y) 16px var(--reader-panel-pad-bottom);
}

.annotation-sidebar {
    width: var(--annotation-sidebar-w);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--c-border);
    border-radius: var(--reading-radius);
    background: var(--c-bg-card);
    overflow: hidden;
    min-height: 0;
    max-height: var(--reader-max-height);
}

.annotation-sidebar__header {
    padding: 12px 14px;
    border-bottom: 1px solid var(--c-border);
}

.annotation-sidebar__title {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
}

.annotation-tree {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
    min-height: 120px;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.annotation-tree::-webkit-scrollbar {
    display: none;
}

.annotation-tree__empty,
.annotation-tree__error {
    padding: 16px 14px;
    font-size: 12px;
    color: var(--c-text-muted);
    line-height: 1.5;
}

.annotation-tree__error {
    color: var(--c-danger);
}

.annotation-chapter {
    margin-bottom: 8px;
}

.annotation-chapter__title {
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    color: var(--c-text-secondary);
}

.annotation-chapter__list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.annotation-item {
    padding: 0 8px;
    margin-bottom: 2px;
}

.annotation-item--active .annotation-item__btn {
    background: var(--c-accent-soft);
    border-color: var(--c-accent-border);
}

.annotation-item--has-chat .annotation-item__label {
    color: var(--c-text);
}

.annotation-item__btn {
    width: 100%;
    min-width: 0;
    border: 1px solid transparent;
    background: transparent;
    color: var(--c-text);
    border-radius: 6px;
    padding: 6px 8px;
    font-size: 12px;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.annotation-item__btn:hover {
    background: var(--c-bg-surface);
    border-color: var(--c-border);
}

.annotation-item__label {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.annotation-item__preview {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
    color: var(--c-text-muted);
    line-height: 1.35;
}

/* 阅读工作区：主阅读区 + 对话侧栏（对齐 chat-workspace） */
.reader-workspace {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    align-items: stretch;
    gap: 16px;
    max-height: var(--reader-max-height);
}

.reader-stage {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* 划词对话侧栏：复用 chat.css 消息/输入样式，收窄布局变量 */
.reading-chat-panel.main-area {
    flex: 0 0 var(--reading-chat-w);
    width: var(--reading-chat-w);
    height: 100%;
    border: 1px solid var(--c-border);
    border-radius: var(--reading-radius);
    background: var(--c-bg-main);
    overflow: hidden;
    min-height: 0;
    max-height: var(--reader-max-height);
    --msg-side-gutter: 0;
    --msg-h-padding: 10px;
    --msg-header-name-size: 13px;
    --msg-header-time-size: 10px;
    --msg-gap: 4px;
}

.reading-chat-panel[hidden] {
    display: none !important;
}

.reading-chat-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--c-border);
    flex-shrink: 0;
    background: var(--c-bg-main);
}

.reading-chat-panel__title {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.reading-chat-panel__close {
    border: none;
    background: transparent;
    color: var(--c-text-muted);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 0 4px;
}

.reading-chat-panel__close:hover {
    color: var(--c-text);
}

.reading-chat-panel .chat-container {
    padding: 12px var(--msg-h-padding);
    gap: 14px;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.reading-chat-panel .chat-container::-webkit-scrollbar {
    display: none;
}

.reading-chat-panel .input-area {
    padding: 6px var(--msg-h-padding) 8px;
}

.reading-chat-panel .composer-body textarea {
    font-size: 12px;
    min-height: 36px;
}

.reading-chat-panel .input-container {
    width: 100%;
    margin-left: 0;
    margin-right: 0;
}

.reading-chat-panel .msg-row {
    width: 100%;
}

.reading-chat-panel .msg-block {
    max-width: 100%;
}

.reading-chat-panel .msg-aside {
    display: none;
}

.reading-chat-panel .msg-header {
    display: none;
}

.reading-chat-panel .composer-toolbar--compact {
    justify-content: flex-end;
    padding: 6px 8px;
}

.reading-chat-panel .composer-toolbar--compact .toolbar-right {
    margin-left: 0;
}

.reading-chat-panel .message-content {
    font-size: 12px;
}

.reading-chat-panel .message-content h1,
.reading-chat-panel .message-content h2,
.reading-chat-panel .message-content h3 {
    font-size: 13px;
}

.reading-chat-panel .msg-bubble {
    padding: 8px 10px;
}

.reading-chat-panel .message-controls {
    display: none;
}

.selection-popover {
    position: fixed;
    z-index: 1000;
    transform: translateZ(0);
}

.selection-popover[hidden] {
    display: none !important;
}

.selection-popover__btn {
    border: 1px solid var(--c-accent-border);
    background: var(--c-bg-elevated);
    color: var(--c-accent);
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    box-shadow: var(--c-shadow-dropdown);
    white-space: nowrap;
}

.selection-popover__btn:hover {
    background: var(--c-accent-soft);
}

.reader-book-wrap {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.reader-content {
    position: relative;
    flex: 1;
    min-height: 0;
    border: 1px solid var(--c-border);
    border-radius: var(--reading-radius);
    background: var(--c-bg-main);
    box-shadow: var(--c-shadow-dropdown);
    overflow: hidden;
}

.epub-viewer {
    height: 100%;
    min-height: 460px;
    background: var(--c-bg-main);
    overflow: hidden;
}

.epub-viewer iframe {
    border: none;
    background: var(--c-bg-main);
}

/* 阅读区左右翻页：悬浮在内侧边缘，移入显示 */
.reader-nav-zone {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 72px;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
}

.reader-nav-zone--prev {
    left: 0;
    justify-content: flex-start;
    padding-left: 10px;
}

.reader-nav-zone--next {
    right: 0;
    justify-content: flex-end;
    padding-right: 10px;
}

.reader-side-nav {
    width: 40px;
    height: 72px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--c-text-secondary);
    font-size: 32px;
    font-weight: 300;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.92);
    transition:
        opacity 0.2s ease,
        visibility 0.2s ease,
        transform 0.2s ease,
        color 0.15s ease;
    user-select: none;
    box-shadow: none;
    text-shadow: 0 0 12px var(--c-bg-body), 0 0 4px var(--c-bg-body);
}

.reader-nav-zone:hover .reader-side-nav,
.reader-side-nav:hover,
.reader-side-nav:focus-visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}

.reader-side-nav span {
    display: block;
    margin-top: -2px;
}

.reader-side-nav:hover {
    background: transparent;
    border: none;
    color: var(--c-accent);
    box-shadow: none;
}

.reader-side-nav:active {
    transform: scale(0.96);
}

.reader-side-nav:focus-visible {
    outline: 2px solid var(--c-accent);
    outline-offset: 2px;
}

.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    border-radius: 8px;
    background: var(--c-bg-elevated);
    border: 1px solid var(--c-border);
    box-shadow: var(--c-shadow-dropdown);
    animation: toast-in 0.2s ease;
}

.toast.success { border-color: var(--c-success-border); }
.toast.error { border-color: var(--c-danger); }
.toast.info { border-color: var(--c-accent-border); }

.toast.hiding {
    opacity: 0;
    transform: translateX(12px);
    transition: opacity 0.2s, transform 0.2s;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 768px) {
    .reader-panel {
        flex-direction: column;
        padding: 16px 12px var(--reader-panel-pad-bottom);
    }

    .annotation-sidebar {
        width: 100%;
        max-height: 160px;
    }

    .reader-workspace {
        flex-direction: column;
        max-height: none;
    }

    .reading-chat-panel.main-area {
        width: 100%;
        flex: 0 0 auto;
        max-height: 50vh;
    }

    .reader-nav-zone {
        width: 56px;
    }

    .reader-nav-zone--prev {
        padding-left: 6px;
    }

    .reader-nav-zone--next {
        padding-right: 6px;
    }

    .reader-side-nav {
        width: 34px;
        height: 64px;
        font-size: 22px;
        border-radius: 8px;
    }

    .epub-viewer {
        min-height: 380px;
        max-height: calc(
            100vh - var(--reading-header-h) - var(--reader-panel-pad-y) - var(--reader-panel-pad-bottom) - 8px
        );
    }
}

@media (max-width: 640px) {
    :root {
        --reader-panel-pad-bottom: 20px;
    }

    .reading-header__subtitle {
        display: none;
    }

    .reading-header__progress {
        font-size: 12px;
    }

    .reading-header__title {
        max-width: 48%;
    }

    .header-btn {
        padding: 8px 10px;
        font-size: 12px;
    }

    .book-library {
        padding: 16px;
    }

    .reader-panel {
        padding: 16px 12px var(--reader-panel-pad-bottom);
    }

    .reader-nav-zone {
        width: 48px;
    }

    .reader-side-nav {
        width: 30px;
        height: 56px;
        font-size: 20px;
    }
}
