:root {
    --wood-primary: #5d4037;
    --wood-dark: #3e2723;
    --wood-light: #8d6e63;
    --shelf-height: 20px;
    --shelf-gap: 280px; /* 书的高度加上间隙 */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--wood-primary);
    background-image: repeating-linear-gradient(
        45deg,
        rgba(0,0,0,0.05) 0px,
        rgba(0,0,0,0.05) 2px,
        transparent 2px,
        transparent 4px
    );
    color: #fff;
    min-height: 100vh;
    overflow-x: hidden;
}

.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

header {
    text-align: center;
    margin-bottom: 40px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
    background: rgba(0, 0, 0, 0.3);
    padding: 15px;
    border-radius: 8px;
    border: 2px solid #8d6e63;
}

h1 {
    font-size: 2.5rem;
    margin-bottom: 10px;
    color: #efebe9;
}

.art-title {
    font-family: "STKaiti", "KaiTi", "楷体", "STSong", "SongTi", "宋体", serif;
    font-size: 3.5rem;
    color: #ffffff;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
    letter-spacing: 5px;
    margin-bottom: 0;
}

/* 移除 instruction 样式，因为 HTML 中已移除 */

/* 书架样式 */
.bookshelf {
    display: flex;
    flex-direction: column;
    gap: 40px;
    padding-bottom: 50px;
}

.shelf-row {
    position: relative;
    min-height: 200px; /* 书的高度 - 减小以适应横版 */
    border-bottom: 25px solid #3e2723; /* 架子板 */
    box-shadow: 0 10px 20px rgba(0,0,0,0.5), inset 0 -2px 5px rgba(255,255,255,0.1);
    display: flex;
    align-items: flex-end; /* 书籍底部对齐 */
    padding: 0 40px 10px 40px;
    flex-wrap: wrap;
    gap: 30px;
    background: linear-gradient(to bottom, transparent 95%, rgba(0,0,0,0.3) 100%);
}

/* 伪元素制造架子的深度感 */
.shelf-row::after {
    content: '';
    position: absolute;
    bottom: -45px; /* 阴影位置 */
    left: 0;
    right: 0;
    height: 20px;
    background: rgba(0,0,0,0.3);
    z-index: -1;
    filter: blur(10px);
}

/* 书籍样式 */
.book {
    width: 220px; /* 增加宽度 */
    height: 165px; /* 减小高度，比例约 4:3 */
    position: relative;
    cursor: pointer;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    transform-origin: bottom center;
    perspective: 1000px;
}

.book:hover {
    transform: scale(1.05) translateY(-5px);
    z-index: 10;
}

.book:active {
    transform: scale(0.98) translateY(0);
    transition: transform 0.1s ease;
}

.book-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 2px 4px 4px 2px;
    box-shadow: 
        -2px 0 5px rgba(0,0,0,0.3), /* 书脊阴影 */
        5px 5px 15px rgba(0,0,0,0.5); /* 整体阴影 */
    background-color: #fff;
    pointer-events: none; /* 防止图片拦截鼠标事件 */
    user-select: none;
}

/* 书脊效果 */
.book::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 10px;
    background: linear-gradient(to right, rgba(255,255,255,0.3), transparent, rgba(0,0,0,0.3));
    z-index: 2;
    border-radius: 2px 0 0 2px;
}

/* 添加书籍按钮样式 */
.add-book-btn {
    background: linear-gradient(135deg, #d4af37 0%, #a88d2d 50%, #d4af37 100%); /* 金色渐变 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 2px 4px 4px 2px;
    box-shadow: 
        -2px 0 5px rgba(0,0,0,0.3), 
        5px 5px 15px rgba(0,0,0,0.5),
        inset 0 0 20px rgba(255,255,255,0.2);
    border: 1px solid #8e7425;
}

.add-book-btn:hover {
    transform: scale(1.05) translateY(-5px);
    z-index: 10;
    box-shadow: 
        -2px 0 5px rgba(0,0,0,0.3), 
        5px 10px 20px rgba(0,0,0,0.6),
        inset 0 0 30px rgba(255,255,255,0.4);
}

.add-icon {
    font-size: 5rem;
    color: #5d4037;
    font-weight: bold;
    text-shadow: 1px 1px 2px rgba(255,255,255,0.5);
    margin-bottom: 10px;
}

.add-text {
    font-size: 1.2rem;
    color: #fff;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
    font-weight: bold;
    letter-spacing: 2px;
}

/* 覆盖书脊效果，让添加按钮看起来更平整或保持统一，这里保持统一 */
.add-book-btn::before {
    background: linear-gradient(to right, rgba(255,255,255,0.4), transparent, rgba(0,0,0,0.2));
}

.book-title {
    position: absolute;
    bottom: -30px;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 0.9rem;
    color: #d7ccc8;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 1px 1px 2px black;
    opacity: 0;
    transition: opacity 0.3s;
}

.book:hover .book-title {
    opacity: 1;
}

/* 拖拽激活状态 */
.drag-active {
    border: 4px dashed #8d6e63;
    background-color: rgba(255,255,255,0.1);
}

/* 阅读器模态框 */
.reader-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* 使用深色木纹风格背景，代替纯黑 */
    background-color: #3e2723;
    background-image: 
        radial-gradient(circle at center, rgba(93, 64, 55, 0.8) 0%, rgba(62, 39, 35, 0.95) 80%),
        repeating-linear-gradient(
            45deg,
            rgba(0,0,0,0.05) 0px,
            rgba(0,0,0,0.05) 2px,
            transparent 2px,
            transparent 4px
        );
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(10px);
    opacity: 1;
    visibility: visible;
    transition: opacity 0.3s ease;
}

.reader-modal.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}

.book-content {
    height: 90vh;
    width: 100%; /* 确保宽度占满，以便居中 */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    /* 增加 3D 透视 */
    perspective: 2000px;
    z-index: 10; /* 低于控制按钮 */
    pointer-events: none; /* 让点击穿透空白区域，防止遮挡按钮 */
}

.page-container {
    max-height: 85vh;
    max-width: 90vw;
    /* 移除固定宽高限制，改由内容撑开，但为了防止溢出，需要限制内部图片的最大尺寸 */
    /* background-color: transparent; */ /* 背景透明，由图片决定 */
    display: flex;
    justify-content: center;
    align-items: center;
    transform-style: preserve-3d;
    position: relative; /* 为伪元素定位 */
    padding-left: 25px; /* 给书脊腾出空间 */
    pointer-events: auto; /* 恢复内容区域的点击 */
}

/* 书脊装饰元素 */
.book-spine-decor {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 25px;
    background-color: #3e2723;
    background-image: 
        /* 皮革纹理噪声 */
        url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)' opacity='0.15'/%3E%3C/svg%3E"),
        linear-gradient(to right, rgba(0,0,0,0.4) 0%, rgba(255,255,255,0.1) 20%, rgba(0,0,0,0.3) 100%);
    box-shadow: 2px 0 5px rgba(0,0,0,0.5);
    z-index: 20;
    border-radius: 4px 0 0 4px;
}

/* 缝线效果 */
.book-spine-decor::after {
    content: '';
    position: absolute;
    top: 10px;
    bottom: 10px;
    right: 5px;
    width: 0;
    border-right: 2px dashed #d4af37; /* 金色缝线 */
    filter: drop-shadow(1px 1px 0 rgba(0,0,0,0.5));
    opacity: 0.7;
}

.pages-wrapper {
    position: relative;
    /* 关键修改：pages-wrapper 需要有明确的尺寸限制，或者由内部内容撑开 */
    /* 但因为内部是 absolute 定位，外部无法被撑开，所以需要 JS 动态计算，或者 CSS 限制最大值 */
    /* 为了简单，我们让 img 撑开一个占位容器，或者限制 wrapper 的最大尺寸 */
    /* 这里我们限制 max-height/width，并让 wrapper 自身居中 */
    width: auto;
    height: auto;
    /* 确保 wrapper 不会超过视口，同时让内部图片适应这个限制 */
    max-height: 85vh; 
    max-width: calc(90vw - 25px); /* 减去书脊宽度 */
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 2000px;
    transform-style: preserve-3d;
}

.page-layer {
    /* 移除固定宽高，改为继承 wrapper */
    width: 100%;
    height: 100%;
    display: flex; /* 让内部图片居中 */
    justify-content: center;
    align-items: center;
    background-color: #fff;
    box-shadow: 5px 0 15px rgba(0,0,0,0.2);
    border-left: 1px solid rgba(0,0,0,0.1); /* 内边缘 */
    position: absolute; /* 改为绝对定位，让 layer-top 覆盖 layer-bottom */
    top: 0;
    left: 0;
}

/* 图片加载占位符和样式 */
.page-img {
    /* 关键修改：图片限制最大尺寸，且保持比例 */
    max-height: 85vh;
    max-width: calc(90vw - 25px);
    width: auto; /* 允许自动宽度 */
    height: auto; /* 允许自动高度 */
    object-fit: contain;
    display: block;
    pointer-events: none; /* 图片不响应事件，让 canvas 响应 */
    transition: opacity 0.3s ease;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
}

.page-img:not([src]), .page-img[src=""] {
    opacity: 0.3;
    animation: loading-shimmer 1.5s infinite;
}

.page-img[src]:not([src=""]) {
    opacity: 1;
    animation: none;
}

@keyframes loading-shimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.page-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 5; /* 在图片之上 */
    cursor: default;
    pointer-events: none; /* 默认不接受点击，开启画笔模式后改为 auto */
}

/* 标注工具栏 */
.annotation-toolbar {
    margin-top: 15px;
    display: flex;
    gap: 15px;
    z-index: 2001; /* 确保可点击 */
    pointer-events: auto;
}

.tool-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: #fff;
    padding: 8px 15px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s;
}

.tool-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: translateY(-2px);
}

.tool-btn.active {
    background: #d4af37;
    color: #3e2723;
    border-color: #d4af37;
    font-weight: bold;
    box-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
}

/* 定位层级 */
#layer-bottom {
    position: relative; /* 撑开容器 (实际上容器由 pages-wrapper 决定，这里 relative 只是为了层级) */
    z-index: 1;
}

#layer-top {
    z-index: 2;
    transform-origin: left center;
    backface-visibility: hidden;
}

/* 翻页动画类 - 仅应用于 Top 层 */
.flip-next {
    animation: flipNext 0.6s ease-in-out forwards;
}

.flip-prev {
    animation: flipPrev 0.6s ease-in-out forwards;
}

/* Next: 顶层页从 0 翻到 -180 (或 -90 视效果而定，为了自然，通常翻到不可见即可) */
/* 这里我们只需要翻到 -90deg 以上，让它看起来翻过去了，然后瞬间切换内容 */
@keyframes flipNext {
    0% { transform: rotateY(0); }
    100% { transform: rotateY(-120deg); opacity: 0; }
}

/* Prev: 顶层页从左侧 (-120deg) 翻回来 (0) */
@keyframes flipPrev {
    0% { transform: rotateY(-120deg); opacity: 0; }
    100% { transform: rotateY(0); opacity: 1; }
}

.page-indicator {
    margin-top: 15px;
    color: #aaa;
    font-size: 1rem;
    font-family: monospace;
}

.control-btn {
    position: absolute;
    background: none;
    border: none;
    color: rgba(255,255,255,0.6);
    cursor: pointer !important; /* 强制手型 */
    transition: all 0.2s ease;
    outline: none;
    z-index: 2000; /* 确保在最上层，高于 book-spine-decor (z-index: 20) */
    pointer-events: auto !important; /* 强制可点击 */
}

.control-btn:hover {
    color: #fff;
    transform: scale(1.1);
    background-color: rgba(255,255,255,0.1); /* 增加背景反馈 */
    border-radius: 50%; /* 圆形背景 */
    box-shadow: 0 0 15px rgba(255,255,255,0.2);
}

.control-btn:active {
    transform: scale(0.95);
    background-color: rgba(255,255,255,0.3);
    transition: transform 0.05s ease;
}

.close-btn {
    top: 20px;
    right: 30px;
    font-size: 3rem;
    line-height: 1;
    width: 60px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.nav-btn {
    font-size: 4rem;
    width: 80px;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 50%;
}

.nav-btn:hover {
    transform: translateY(-50%) scale(1.1); /* 保持垂直居中 */
}

.prev-btn {
    left: 20px;
}

.next-btn {
    right: 20px;
}
