/* RESET & BASE */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    background-color: #050505;
    color: #d0d0d0;
    font-family: 'Courier New', Courier, monospace;
    font-size: 19px;
    line-height: 1.7;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    position: relative;
}

/* BACKGROUND IMAGE LAYER */
body::before {
    content: '';
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background-image: url('https://picsum.photos/1920/1080.jpg?random=1&blur=2');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    opacity: 0.2;
    z-index: -1;
    filter: grayscale(30%);
}

/* --- NEW: TOGGLE BUTTON (The small button to reveal menu) --- */
#menu-toggle {
    position: absolute;
    top: 15px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid #444;
    border-radius: 50%;
    cursor: pointer;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.3s, transform 0.3s;
    font-size: 20px;
    color: #aaa;
}

#menu-toggle:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: translateX(-50%) scale(1.1);
}

#menu-toggle.active {
    background: rgba(0, 170, 255, 0.2);
    border-color: #0af;
    color: #0af;
}

/* --- NEW: DRAG AREA (Top 20px for mobile gesture) --- */
#drag-area {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 20px;
    z-index: 99;
    cursor: ns-resize; /* Indicates vertical drag */
}

/* --- UPDATED: TOOLBAR CONTAINER --- */
#toolbar {
    position: absolute;
    top: 65px; /* Below the toggle button */
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 10px;
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease, transform 0.3s ease;
    background: rgba(0, 0, 0, 0.8);
    padding: 10px 16px;
    border-radius: 25px;
    backdrop-filter: blur(8px);
    border: 1px solid #333;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
}

#toolbar.visible {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

/* --- UPDATED: TOOL BUTTONS --- */
.tool-btn {
    background: transparent;
    border: 1px solid #555;
    color: #ccc;
    padding: 8px 14px;
    cursor: pointer;
    border-radius: 20px;
    font-size: 14px;
    transition: all 0.3s;
    font-family: 'Courier New', Courier, monospace;
    white-space: nowrap;
}

.tool-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: #888;
    color: #fff;
}

.tool-btn.active {
    background: #0af;
    color: #000;
    border-color: #0af;
    font-weight: bold;
}

/* --- NEW: RIBBON BOOKMARK STYLE --- */
.bookmark-ribbon {
    position: absolute;
    top: 0;
    right: 72px;
    width: 32px;
    height: 48px;
    z-index: 50;
    cursor: pointer;
    background-image: url("bookmark.png");
}

.nav-item .bookmark-ribbon {
    position: relative;
    top: -3px;
    right: -27px;
    width: 24px;
    height: 32px;
    z-index: 50;
    cursor: pointer;
    background-image: url(bookmark.png);
    background-size: 24px 32px;
}

.bookmark-ribbon:hover {
    background-image: url("remove-bookmark.png");
}

/* --- THE BOOK CONTAINER --- */
#book-container {
    position: relative;
    width: 90%;
    max-width: 850px;
    height: 85vh;
    background: rgba(10, 10, 10, 0.85);
    box-shadow: 0 0 30px rgba(0,0,0,0.9);
    border-radius: 6px;
    overflow: hidden;
    transition: transform 0.8s cubic-bezier(0.25, 1, 0.5, 1);
    perspective: 1200px;
    backdrop-filter: blur(2px);
}

/* THE TEXT CONTENT */
.page-content {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 50px;
    overflow-y: auto;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.6s ease;
    font-weight: 300;
    scrollbar-width: none;
    z-index: 10;
    user-select: text;
}
.page-content::-webkit-scrollbar { display: none; }

.page-content.active {
    opacity: 1;
    pointer-events: auto;
    z-index: 10;
}

/* TYPOGRAPHY */
p { margin-bottom: 1.8em; text-align: justify; }

.rhythm {
    color: #666;
    font-style: italic;
    display: block;
    text-align: center;
    margin: 2.5em 0;
    letter-spacing: 3px;
    font-size: 0.9em;
}

.void {
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #222;
    font-size: 0.7em;
    letter-spacing: 5px;
}

.highlight {
    color: #fff;
    font-weight: bold;
    text-shadow: 0 0 5px rgba(255,255,255,0.3);
}

/* HIGHLIGHT STYLES */
.highlighted-text {
    background-color: rgba(255, 255, 0, 0.4); /* Yellow with 40% opacity */
    color: inherit; /* Keeps the text color the same as the surrounding text */
    cursor: pointer;
    transition: background-color 0.2s;
    border-radius: 2px;
    padding: 0 2px; /* Adds a tiny bit of breathing room */
}

.highlighted-text:hover {
    background-color: rgba(255, 255, 0, 0.6); /* Brighter yellow on hover */
}

/* Ensure text selection works correctly */
::selection {
    background-color: rgba(0, 170, 255, 0.3); /* Blue selection color */
    color: #fff;
}

.ritual-box {
    border: 1px dashed #444;
    padding: 1.5em;
    margin: 2em 0;
    background: rgba(255,255,255,0.02);
}

.angel-seal {
    border: 2px solid #333;
    padding: 2em;
    margin: 2em 0;
    text-align: center;
    background: rgba(0,0,0,0.3);
}

/* CORNER CLICK ZONES */
.corner {
    position: absolute;
    width: 70px;
    height: 70px;
    cursor: pointer;
    z-index: 20;
    transition: background 0.4s;
}

.corner:hover { background: rgba(255, 255, 255, 0.08); }

/* Top Right: Next Page */
#next-corner {
    top: 0;
    right: 0;
    border-bottom-left-radius: 25px;
}

/* Bottom Left: Previous Page */
#prev-corner {
    bottom: 0;
    left: 0;
    border-top-right-radius: 25px;
}

/* Triangle Indicators */
.corner::after {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-style: solid;
}

/* Top Right Triangle (Points Down-Left) */
#next-corner::after {
    top: 15px;
    right: 15px;
    border-width: 0 18px 18px 0;
    border-color: transparent #777 transparent transparent;
}

/* Bottom Left Triangle (Points Up-Right) - MATCHING STYLE */
#prev-corner::after {
    bottom: 15px;
    left: 15px;
    border-width: 18px 0 0 18px;
    border-color: transparent transparent transparent #777;
}

/* GLITCH EFFECT */
.glitch-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(255, 255, 255, 0.03);
    z-index: 5;
    opacity: 0;
    pointer-events: none;
    mix-blend-mode: overlay;
}

/* MOBILE TWEAKS */
@media (max-width: 600px) {
    #book-container { width: 100%; height: 100vh; border-radius: 0; }
    .page-content { padding: 25px; font-size: 17px; }
    .corner { display: none; }

    #menu-toggle { top: 10px; width: 35px; height: 35px; font-size: 18px; }
    #toolbar { top: 55px; padding: 8px 12px; gap: 8px; }
    .tool-btn { padding: 6px 10px; font-size: 12px; }

    /* Adjust ribbon for mobile */
    .bookmark-ribbon {
        border-width: 0 30px 30px 0;
        right: -5px; /* Slight overlap */
    }
}

/* --- TTS CONTROLS (FIXED POSITIONING) --- */
#tts-controls {
    display: none;
    position: fixed; /* CHANGED FROM ABSOLUTE TO FIXED */
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.9); /* Slightly darker for contrast */
    padding: 12px 16px;
    border-radius: 30px;
    border: 1px solid #555;
    backdrop-filter: blur(10px);
    z-index: 2147483647; /* MAX INT: Ensures it's on top of EVERYTHING */
    gap: 12px;
    align-items: center;
    box-shadow: 0 8px 25px rgba(0,0,0,0.6);
    white-space: nowrap;
    max-width: 95%; /* Prevent overflow on very small screens */
}

#tts-controls.active {
    display: flex;
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from { transform: translateX(-50%) translateY(20px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* Mobile adjustments */
@media (max-width: 600px) {
    #tts-controls {
        bottom: 15px;
        padding: 10px 14px;
        width: auto;
        max-width: 90%;
        flex-wrap: wrap; /* Allow wrapping if screen is tiny */
        justify-content: center;
    }

    .tts-btn {
        padding: 8px 12px;
        font-size: 13px;
    }

    #voice-select {
        max-width: 110px;
        font-size: 12px;
        padding: 4px;
    }

    #speed-range {
        width: 70px;
    }

    #speed-label {
        font-size: 11px;
    }

    .bookmark-ribbon {
        position: absolute;
        top: 0;
        right: 22px;
        width: 24px;
        height: 32px;
        z-index: 50;
        cursor: pointer;
        background-image: url("bookmark.png");
        background-size: 24px 32px;
    }

    .bookmark-ribbon:hover {
        background-image: url("remove-bookmark.png");
    }

    .nav-item .bookmark-ribbon {
        top: -15px;
        right: -27px;
        width: 12px;
        height: 16px;
        background-image: url(bookmark.png);
        background-size: 12px 16px;
    }
    small {
        font-size: 3.5vw;
    }
}

.tts-btn {
    background: transparent;
    border: 1px solid #555;
    color: #ccc;
    padding: 6px 12px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s;
}

.tts-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.tts-btn.playing {
    background: #0af;
    color: #000;
    border-color: #0af;
    font-weight: bold;
}

#voice-select {
    background: #222;
    color: #ddd;
    border: 1px solid #444;
    border-radius: 15px;
    padding: 4px 8px;
    font-size: 12px;
    max-width: 150px;
}

#speed-range {
    width: 80px;
    accent-color: #0af;
}

/* Highlight current sentence during playback (optional visual cue) */
.speaking-sentence {
    background-color: rgba(0, 170, 255, 0.2);
    border-radius: 2px;
    transition: background-color 0.3s;
}
/* --- NAVIGATION MODAL FIXES --- */
#nav-modal {
    display: none; /* Hidden by default */
    position: fixed; /* Changed from absolute to fixed to cover the whole viewport */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85); /* Darker overlay */
    z-index: 9999; /* Ensure it's above everything (toolbar is 100) */
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
}

.nav-content {
    background: #111;
    padding: 20px;
    border-radius: 8px;
    border: 1px solid #444;
    max-width: 400px;
    width: 90%;
    max-height: 80vh; /* Constrain height to 80% of viewport */
    overflow-y: auto; /* Enable scrolling if content is too long */
    position: relative;
    box-shadow: 0 10px 30px rgba(0,0,0,0.8);
}

.nav-content h3 {
    text-align: center;
}

/* Custom scrollbar for the modal list */
.nav-content::-webkit-scrollbar {
    width: 8px;
}
.nav-content::-webkit-scrollbar-track {
    background: #111;
}
.nav-content::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 4px;
}
.nav-content::-webkit-scrollbar-thumb:hover {
    background: #666;
}

.nav-item {
    padding: 12px 15px;
    border-bottom: 1px solid #333;
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background 0.2s;
}
.nav-item p {
    margin-bottom: 0px;
}

.nav-item:hover {
    background: #222;
}

.nav-item.bookmarked {
    color: #0af;
    font-weight: bold;
}

.close-nav {
    position: absolute;
    top: 10px;
    right: 15px;
    cursor: pointer;
    font-size: 24px;
    color: #888;
    line-height: 1;
    z-index: 10;
}

.close-nav:hover {
    color: #fff;
}

.page-content {
    /* ... existing styles ... */
    user-select: text !important;
    -webkit-user-select: text !important;
    -moz-user-select: text !important;
    -ms-user-select: text !important;
    -webkit-touch-callout: default !important; /* Allow context menu */
}

/* Ensure toolbar doesn't block touches on the text */
#toolbar {
    pointer-events: none;
}

#toolbar.active, #toolbar.visible {
    pointer-events: auto;
}

/* Hide empty voice dropdown */
#voice-select:empty {
    display: none !important;
}

/* Also hide if it has no options */
#voice-select option:first-child:only-child {
    display: none;
}

/* When the class 'show' is added, force it visible */
#nav-modal.show {
    display: flex !important;
}

.nav-content {
    background: #111 !important;
    padding: 20px !important;
    border-radius: 8px !important;
    border: 1px solid #444 !important;
    max-width: 400px !important;
    width: 90% !important;
    max-height: 80vh !important;
    overflow-y: auto !important;
    position: relative !important;
    box-shadow: 0 10px 30px rgba(0,0,0,0.8) !important;
}