:root {
    --bg-color: #F9F7F2;
    /* Warm off-white */
    --shelf-color: #EFEBE0;
    /* Light wood/stone tone */
    --text-color: #4A4A4A;
    --accent-color: #8E8070;
    --sunlight: rgba(255, 253, 240, 0.6);

    /* Pastel Book Colors */
    --book-blue: #D4E0E8;
    --book-pink: #F2D4D7;
    --book-green: #D9E4D6;
    --book-yellow: #F9EBC8;
    --book-beige: #E8E0D5;
}

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

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Noto Sans KR', sans-serif;
    overflow-x: hidden;
    min-height: 100vh;
}

/* Sunlight Effect */
.sunlight-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0) 40%);
    pointer-events: none;
    z-index: 10;
}

/* Navigation */
.top-nav {
    position: absolute;
    top: 2rem;
    left: 2rem;
    z-index: 10;
}

.home-link {
    text-decoration: none;
    color: var(--text-color);
    font-size: 1.1rem;
    padding: 0.5rem 1rem;
    border: 1px solid transparent;
    transition: all 0.3s ease;
    font-family: 'Gowun Batang', serif;
}

.home-link:hover {
    border-color: var(--text-color);
}

/* Header */
.library-header {
    text-align: center;
    padding: 2rem 1rem 1rem;
    animation: fadeIn 1.5s ease-out;
}

.library-title {
    font-family: 'Playfair Display', serif;
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
    letter-spacing: 1px;

    /* Gradient Text Style */
    background: linear-gradient(135deg, var(--text-color) 0%, var(--accent-color) 100%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.library-subtitle {
    font-family: 'Gowun Batang', serif;
    color: var(--accent-color);
    font-size: 1rem;
}

/* Bookshelf */
.bookshelf-container {
    max-width: 1000px;
    margin: 1rem auto;
    padding: 0 2rem;
}

.shelf {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    gap: 1.5rem;
    border-bottom: 12px solid var(--shelf-color);
    padding-bottom: 0;
    margin-bottom: 2rem;
    position: relative;
    height: 280px;
    /* Fixed height for consistent look */
}

/* Shadow under shelf */
.shelf::after {
    content: '';
    position: absolute;
    bottom: -20px;
    left: 2%;
    width: 96%;
    height: 10px;
    background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.05) 0%, rgba(0, 0, 0, 0) 70%);
}

/* Books */
.book {
    width: 50px;
    /* Spine width */
    height: 240px;
    background-color: #fff;
    position: relative;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
    transform: rotate(var(--random-rotate, 0deg));
    transform-origin: var(--transform-origin, bottom left);
    box-shadow: -2px 0 5px rgba(0, 0, 0, 0.05);
    border-radius: 2px 4px 4px 2px;
}

/* Randomize heights slightly for realism */
.book:nth-child(1) {
    height: 250px;
}

.book:nth-child(2) {
    height: 230px;
}

.book:nth-child(3) {
    height: 260px;
}

/* Book Colors */
.book[data-color="pastel-blue"] {
    background-color: var(--book-blue);
}

.book[data-color="pastel-pink"] {
    background-color: var(--book-pink);
}

.book[data-color="pastel-green"] {
    background-color: var(--book-green);
}

.book[data-color="pastel-yellow"] {
    background-color: var(--book-yellow);
}

.book[data-color="pastel-beige"] {
    background-color: var(--book-beige);
}

/* Spine Text */
.book-spine {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    writing-mode: vertical-rl;
    text-orientation: mixed;
    padding: 10px 0;
    color: rgba(0, 0, 0, 0.6);
    font-family: 'Gowun Batang', serif;
    font-size: 0.9rem;
    transition: opacity 0.3s;
}

.book-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 180px;
    /* Cover width */
    height: 100%;
    background-color: inherit;
    transform: rotateY(-90deg);
    transform-origin: left;
    transition: all 0.5s ease;
    border-radius: 0 4px 4px 0;
    box-shadow: inset 3px 0 10px rgba(0, 0, 0, 0.05);
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    /* Let clicks pass through to container */
}

.cover-content h3 {
    font-family: 'Gowun Batang', serif;
    font-size: 1.2rem;
    margin-bottom: 0.5rem;
    color: rgba(0, 0, 0, 0.7);
}

.cover-content .author {
    font-size: 0.8rem;
    color: rgba(0, 0, 0, 0.5);
}

/* Hover Effect: Pull book out */
.book:hover,
.book.is-flipped {
    transform: translateY(-10px) translateX(10px);
    width: 180px;
    /* Expand to show cover */
    box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);
    z-index: 10;
}

.book:hover .book-spine,
.book.is-flipped .book-spine {
    opacity: 0;
    /* Hide spine text */
}

.book:hover .book-cover,
.book.is-flipped .book-cover {
    transform: rotateY(0deg);
    opacity: 1;
}

/* Decor Items */
.decor-plant {
    width: 60px;
    height: 100px;
    background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path d="M50 100 L50 60 Q30 30 10 50 Q30 70 50 60 Q70 30 90 50 Q70 70 50 60" fill="%238E8070" opacity="0.3"/></svg>') no-repeat bottom center;
    background-size: contain;
    margin-left: auto;
    /* Push to right */
}

/* Modal (Reading View) */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(249, 247, 242, 0.95);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    perspective: 1500px;
    /* 3D effect for page flip */
}

.modal:not([hidden]) {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background-color: #fff;
    width: 90%;
    max-width: 700px;
    height: 85vh;
    /* padding: 3rem; Moved to story-container */
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
    /* overflow: hidden; Removed to allow 3D page flip */
    position: relative;
    border: 1px solid #eee;
    display: flex;
    flex-direction: column;
    transform-style: preserve-3d;
}

/* Page Flip Animation Layers */
.flip-page {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    transform-origin: left center;
    z-index: 20;
    pointer-events: none;
    border-right: 1px solid rgba(0, 0, 0, 0.05);
    backface-visibility: hidden;
    opacity: 0;
    background: linear-gradient(to right, #fff 95%, #f0f0f0 100%);
}

.modal:not([hidden]) .flip-page {
    opacity: 1;
}

/* Staggered animations for 5 pages */
.modal:not([hidden]) .flip-page:nth-child(1) {
    animation: pageFlip 0.4s cubic-bezier(0.2, 0.1, 0.25, 1) forwards;
    z-index: 25;
}

.modal:not([hidden]) .flip-page:nth-child(2) {
    animation: pageFlip 0.5s cubic-bezier(0.2, 0.1, 0.25, 1) forwards;
    z-index: 24;
}

.modal:not([hidden]) .flip-page:nth-child(3) {
    animation: pageFlip 0.6s cubic-bezier(0.2, 0.1, 0.25, 1) forwards;
    z-index: 23;
}

.modal:not([hidden]) .flip-page:nth-child(4) {
    animation: pageFlip 0.7s cubic-bezier(0.2, 0.1, 0.25, 1) forwards;
    z-index: 22;
}

.modal:not([hidden]) .flip-page:nth-child(5) {
    animation: pageFlip 0.8s cubic-bezier(0.2, 0.1, 0.25, 1) forwards;
    z-index: 21;
}

@keyframes pageFlip {
    0% {
        transform: rotateY(0);
        opacity: 1;
    }

    60% {
        opacity: 1;
    }

    100% {
        transform: rotateY(-120deg);
        opacity: 0;
    }
}

.close-btn {
    position: absolute;
    top: 1rem;
    right: 0.5rem;
    font-size: 2rem;
    background: rgba(255, 255, 255, 0.8);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    color: var(--text-color);
    z-index: 10;
    backdrop-filter: blur(4px);
}

.story-container {
    font-family: 'Gowun Batang', serif;
    line-height: 1.8;
    /* Ebook Style Layout */
    overflow-y: hidden;
    overflow-x: hidden;
    /* Controlled by JS */
    height: 100%;
    padding: 4.5rem 3rem 5rem 3rem;
    /* Increased top padding to avoid close button */
    /* column-width set by JS to match container width */
    column-gap: 4rem;
    column-fill: auto;
    flex: 1;
    /* scroll-behavior: smooth; Removed for JS-controlled flip animation */
    transform-style: preserve-3d;
    backface-visibility: hidden;
}

/* Page Flip Animations */
.page-clone {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #fff;
    /* Ensure background is opaque */
    z-index: 10;
    pointer-events: none;
    transform-origin: left center;
    backface-visibility: hidden;
    /* Copy padding from container to match layout */
    padding: 4.5rem 3rem 5rem 3rem;
    box-sizing: border-box;
    overflow: hidden;
}

@media (max-width: 768px) {
    .page-clone {
        padding: 4.5rem 1.5rem 5rem 1.5rem;
    }
}

.page-clone.flip-out-left {
    animation: flipOutLeft 0.6s cubic-bezier(0.645, 0.045, 0.355, 1) forwards;
    transform-origin: left center;
}

.page-clone.flip-out-right {
    animation: flipOutRight 0.6s cubic-bezier(0.645, 0.045, 0.355, 1) forwards;
    transform-origin: right center;
}

@keyframes flipOutLeft {
    0% {
        transform: rotateY(0);
        opacity: 1;
    }

    100% {
        transform: rotateY(-120deg);
        opacity: 0;
    }
}

@keyframes flipOutRight {
    0% {
        transform: rotateY(0);
        opacity: 1;
    }

    100% {
        transform: rotateY(120deg);
        opacity: 0;
    }
}

/* Navigation Controls */
.book-nav {
    position: absolute;
    bottom: 1.5rem;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 2rem;
    z-index: 50;
}

.nav-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-color);
    cursor: pointer;
    padding: 0.5rem 1rem;
    opacity: 0.7;
    transition: opacity 0.3s;
}

.nav-btn:hover {
    opacity: 1;
}

.nav-btn:disabled {
    opacity: 0.2;
    cursor: not-allowed;
}

#page-indicator {
    font-family: 'Gowun Batang', serif;
    font-size: 0.9rem;
    color: #888;
}

#modal-title {
    font-size: 2rem;
    margin-bottom: 2rem;
    text-align: center;
    color: var(--text-color);
    /* Ensure title stays on first page */
    break-after: avoid;
}

.story-text p {
    margin-bottom: 1.5rem;
    font-size: 1.05rem;
    color: #333;
    text-align: justify;
}

/* Mobile adjustments for ebook reader */
@media (max-width: 768px) {
    .story-container {
        padding: 4.5rem 1.5rem 5rem 1.5rem;
        column-gap: 2rem;
    }

    .modal-content {
        width: 95%;
        height: 90vh;
    }
}

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

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

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .top-nav {
        position: relative;
        top: auto;
        left: auto;
        margin-bottom: 0.5rem;
        text-align: left;
    }

    .library-header {
        padding: 1rem 1rem 0.5rem;
    }

    .library-title {
        font-size: 2rem;
    }

    .bookshelf-container {
        margin: 3rem auto 1rem;
        /* Increased top margin */
        padding: 0 1rem;
        /* Reduce padding to give more space */
    }

    .shelf {
        flex-wrap: nowrap;
        /* Prevent wrapping */
        /* overflow-x: auto; Removed to prevent accidental scrolling */
        height: 220px;
        /* Reduced height for smaller books */
        padding-bottom: 0;
        margin-bottom: 1.5rem;
        align-items: flex-end;
        justify-content: center;
    }

    /* Removed scrollbar hiding styles since overflow is gone */

    .book {
        width: 32px;
        /* Reduced spine width */
        flex-shrink: 0;
        /* Prevent books from squishing */
    }

    /* Scale down book heights for mobile */
    .book:nth-child(1) {
        height: 190px;
    }

    .book:nth-child(2) {
        height: 170px;
    }

    .book:nth-child(3) {
        height: 200px;
    }

    .book:hover,
    .book.is-flipped {
        width: 120px;
        /* Reduced cover width */
        transform: translateY(-10px);
        /* Lift slightly to create space at bottom */
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        z-index: 10;
    }

    .book-cover {
        width: 120px;
        /* Match flipped width */
    }
}