@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500&display=swap');

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

:root {
    --paper-color: #fdfaf0;
    --paper-edge: #e4dccb;
    --desk-color: #1a1512;
    --text-color: #2c2c2c;
    --accent-color: #d4af37;
    --sidebar-bg: rgba(0, 0, 0, 0.4);

    /* Updated Responsive Page Dimensions */
    --book-width: 1000px;
    --book-height: 700px;
    --page-padding-v: 60px;
    --page-padding-h: 50px;
    --writing-height: calc(var(--book-height) - (var(--page-padding-v) * 2));
    --sidebar-width: 280px;
}

html, body {
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* Editor Loader */
.editor-loader {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: #0a0908;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.4s ease, visibility 0.4s ease;
}

.editor-loader.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loader-spinner {
    width: 44px;
    height: 44px;
    border: 3px solid rgba(212, 175, 55, 0.15);
    border-top: 3px solid var(--accent-color);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}

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

body {
    background: var(--desk-color);
    background-image:
        radial-gradient(circle at 50% 50%, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%),
        url("https://www.transparenttextures.com/patterns/dark-leather.png");
    height: 100vh;
    overflow: hidden;
    font-family: 'Poppins', sans-serif;
    color: #fff;
}

.app-container {
    display: flex;
    width: 100vw;
    height: 100vh;
    padding-top: 92px; /* Space for fixed 90px navbar */
}

/* Sidebar */
.sidebar {
    width: 280px;
    background: var(--sidebar-bg);
    backdrop-filter: blur(15px);
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    z-index: 200;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-header {
    padding: 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: rgba(255, 255, 255, 0.6);
}

#add-chapter-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    transition: 0.3s;
}

#add-chapter-btn:hover {
    background: #fff;
    color: #000;
}

#chapters-list {
    flex: 1;
    overflow-y: auto;
    list-style: none;
}

.chapter-item {
    padding: 15px 30px;
    font-size: 14px;
    color: rgba(255, 255, 255, 0.4);
    cursor: pointer;
    transition: 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
}

.chapter-item:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.05);
}

.chapter-item.active {
    color: #fff;
    background: rgba(255, 255, 255, 0.1);
    border-left: 3px solid var(--accent-color);
}

.delete-chapter-btn {
    opacity: 0;
    color: rgba(255, 255, 255, 0.3);
    font-size: 18px;
    padding: 0 5px;
    transition: 0.3s;
    border: none;
    background: transparent;
    cursor: pointer;
}

.chapter-item:hover .delete-chapter-btn {
    opacity: 1;
}

.delete-chapter-btn:hover {
    color: #ff4444 !important;
}

/* Stats */
.stats-panel {
    padding: 20px 30px;
    background: rgba(0, 0, 0, 0.2);
}

.stat-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.stat-label {
    color: rgba(255, 255, 255, 0.3);
}

/* Main Stage */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 20px;
    position: relative;
    overflow-y: auto; /* ENABLES SCROLLING */
}

.top-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    margin-bottom: 20px;
    position: relative;
}

.page-nav-group {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.nav-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    padding: 8px 16px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 13px;
    transition: 0.3s;
}

.nav-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.15);
}

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

#page-label {
    font-size: 12px;
    letter-spacing: 2px;
    color: rgba(255, 255, 255, 0.5);
}

/* Book Rendering */
.book-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 2000px;
}

.book {
    display: flex;
    width: var(--book-width);
    height: var(--book-height);
    background: var(--paper-edge);
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
    border-radius: 4px 15px 15px 4px;
    position: relative;
    transform: rotateX(2deg);
    transform-style: preserve-3d;
    transition: width 0.3s, height 0.3s;
}

.page {
    flex: 1;
    background: var(--paper-color);
    padding: var(--page-padding-v) var(--page-padding-h);
    position: relative;
    display: flex;
    flex-direction: column;
}

.page-left {
    border-radius: 4px 0 0 4px;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    background: linear-gradient(to right, #efe9d9 0%, var(--paper-color) 4%, var(--paper-color) 96%, rgba(0, 0, 0, 0.1) 100%);
}

.page-right {
    border-radius: 0 15px 15px 0;
    background: linear-gradient(to left, #efe9d9 0%, var(--paper-color) 4%, var(--paper-color) 96%, rgba(0, 0, 0, 0.1) 100%);
}

.writing-area {
    width: 100%;
    height: var(--writing-height);
    outline: none;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-color);
    text-align: left;
    overflow: hidden;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
    -ms-hyphens: auto;
    position: relative;
    z-index: 100;
    caret-color: #000 !important;
    transform: translateZ(1px); 
    will-change: transform;
}

.writing-area:focus {
    outline: none;
    caret-color: #000 !important;
}

.writing-area p, .writing-area div {
    margin: 0;
    padding: 0;
    min-height: 1.6em; /* Matches line-height */
}

/* Page Numbers */
.page-footer {
    position: absolute;
    bottom: 25px;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 13px;
    color: rgba(0, 0, 0, 0.3);
    font-style: italic;
    pointer-events: none;
}

.spine-border {
    position: absolute;
    left: 50%;
    top: 5px;
    bottom: 5px;
    width: 40px;
    transform: translateX(-50%);
    background: linear-gradient(to right, rgba(0, 0, 0, 0.1), transparent 50%, rgba(0, 0, 0, 0.1));
    z-index: 10;
    pointer-events: none;
}

/* Save status */
.save-notification {
    position: fixed;
    top: 30px;
    right: 30px;
    font-size: 10px;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.4);
    opacity: 0;
    transition: 0.5s;
}

.save-notification.show {
    opacity: 1;
}

/* Flourishes */
.grain-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0.12;
    z-index: 1; /* Moved behind text layers */
    background-image: url("https://www.transparenttextures.com/patterns/paper-fibers.png");
}

/* Animation */
/* Menu Toggle Button */
.mobile-only-btn {
    display: none;
    background: var(--accent-color);
    color: #000;
    border: none;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    font-size: 18px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* Mobile Responsiveness */
@media (max-width: 1100px) {
    :root {
        --book-width: 90vw;
        --book-height: 80vh;
        --page-padding-h: 30px;
        --page-padding-v: 40px;
    }

    .book {
        transform: none;
    }
}

@media (max-width: 800px) {
    html, body {
        height: auto !important;
        min-height: 100vh !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
    }

    .app-container {
        padding-top: 80px;
        min-height: 100vh;
        height: auto;
        display: flex;
        flex-direction: column;
        overflow-y: visible;
        overflow-x: hidden;
    }

    .main-content {
        padding: 6px 12px 0 12px;
        display: flex;
        flex-direction: column;
        align-items: center;
        height: auto;
        min-height: auto;
        overflow: visible;
        flex: 1;
    }

    .mobile-only-btn {
        display: flex;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: var(--sidebar-width);
        transform: translateX(-100%);
        box-shadow: 20px 0 50px rgba(0,0,0,0.5);
        z-index: 500;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-header {
        padding-left: 30px;
    }

    /* Sidebar Backdrop */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 450;
        backdrop-filter: blur(3px);
    }

    .sidebar.open + .sidebar-overlay {
        display: block;
    }

    .top-bar {
        margin: 0 auto 10px auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        max-width: 480px;
        flex-wrap: nowrap;
        gap: 10px;
        background: rgba(22, 19, 16, 0.92);
        border: 1px solid rgba(212, 175, 55, 0.25);
        border-radius: 12px;
        padding: 7px 12px;
        backdrop-filter: blur(12px);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
        position: relative;
        z-index: 50;
        flex-shrink: 0;
    }

    .page-nav-group {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 8px;
        flex: 1;
    }

    .nav-btn {
        padding: 6px 14px;
        font-size: 12px;
        border-radius: 8px;
        background: rgba(255, 255, 255, 0.08);
        border: 1px solid rgba(212, 175, 55, 0.2);
        color: #f5f5f5;
        font-weight: 500;
        transition: 0.2s;
    }

    .nav-btn:hover:not(:disabled) {
        background: rgba(212, 175, 55, 0.2);
        border-color: var(--accent-color);
        color: var(--accent-color);
    }

    #page-label {
        order: unset;
        width: auto;
        margin-top: 0;
        font-size: 12px;
        font-weight: 600;
        color: var(--accent-color);
        white-space: nowrap;
        padding: 3px 8px;
        background: rgba(212, 175, 55, 0.08);
        border-radius: 6px;
    }

    .book-container {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        padding-bottom: 140px; /* Generous dark site background visible under the book! */
        flex: none;
    }

    .book {
        width: 100%;
        max-width: 480px;
        height: 500px; /* Fixed, compact book height - NOT STRETCHED */
        min-height: 500px;
        max-height: 500px;
        margin: 0 auto;
        flex-direction: column;
        transform: none;
        border-radius: 14px;
        box-shadow: 0 16px 40px rgba(0,0,0,0.7), 0 3px 10px rgba(0,0,0,0.4);
        border: 1px solid rgba(212, 175, 55, 0.25);
        border-bottom: 4px solid #d4cbb5; /* Physical book paper bottom edge */
        overflow: hidden;
        flex-shrink: 0;
    }

    .page-right {
        display: none !important; /* Hide right page on mobile */
    }

    .page-left {
        border-radius: 14px;
        border-right: none;
        background: var(--paper-color);
        padding: 22px 18px 38px;
        height: 100%;
        display: flex;
        flex-direction: column;
        position: relative;
        box-sizing: border-box;
    }

    .writing-area {
        flex: 1;
        height: calc(100% - 22px);
        width: 100%;
        font-size: 13.5px !important; /* Compact, crisp, neat font */
        line-height: 1.55 !important;
        overflow: hidden;
        box-sizing: border-box;
        -webkit-text-size-adjust: 100% !important;
        text-size-adjust: 100% !important;
    }

    .page-footer {
        position: absolute;
        bottom: 10px;
        left: 0;
        right: 0;
        font-size: 13px;
        color: rgba(44, 36, 30, 0.45);
        font-style: italic;
        letter-spacing: 2px;
    }

    .spine-border {
        display: none;
    }
}

/* Landscape Mobile Fix */
@media (max-height: 500px) {
    :root {
        --book-height: 90vh;
    }
    .top-bar {
        margin-bottom: 5px;
    }
}

/* Page Transition Overlay */
#page-transition {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;
    z-index: 10000;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
    transition: opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

#page-transition.active {
    opacity: 1;
    pointer-events: all;
}

#page-transition.hidden {
    opacity: 0;
}

.transition-logo {
    font-family: 'Poppins', sans-serif;
    font-size: 32px;
    color: var(--accent-color);
    transform: scale(0.8);
    opacity: 0;
    transition: 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

#page-transition.active .transition-logo {
    transform: scale(1);
    opacity: 1;
}

@keyframes contentFadeOut {
    to { opacity: 0; transform: scale(1.05); filter: blur(10px); }
}

.page-exit {
    animation: contentFadeOut 0.6s forwards cubic-bezier(0.4, 0, 0.2, 1);
}