:root {
    --reader-bg: #000000;
    --reader-text: #cccccc;
    --reader-accent: #38e1ff;
    --reader-font: 'Merriweather', serif;
    --reader-size: 18px;
    --reader-line-height: 1.6;
    --reader-max-width: 700px;
    --gold: #f2c14e;
}

/* Themes */
body.theme-oled { --reader-bg: #000000; --reader-text: #a0a0a0; --reader-accent: #38e1ff; }
body.theme-dark { --reader-bg: #1a1a1a; --reader-text: #d0d0d0; --reader-accent: #4fc3f7; }
body.theme-light { --reader-bg: #f5f5f5; --reader-text: #1a1a1a; --reader-accent: #007bb5; }
body.theme-sepia { --reader-bg: #f4ecd8; --reader-text: #5b4636; --reader-accent: #8b5e3c; }

body.high-contrast {
    --reader-bg: #000000 !important;
    --reader-text: #ffffff !important;
    --reader-accent: #ffff00 !important;
}

body {
    background-color: var(--reader-bg);
    color: var(--reader-text);
    font-family: var(--reader-font);
    font-size: var(--reader-size);
    line-height: var(--reader-line-height);
    margin: 0; padding: 0;
    transition: background-color 0.3s, color 0.3s;
    overflow-x: hidden; width: 100vw;
}

/* Header */
.reader-header {
    position: fixed;
    top: 0; left: 0; width: 100%;
    padding: 10px 15px;
    background: rgba(0,0,0,0.95);
    backdrop-filter: blur(15px);
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    box-sizing: border-box;
}

.reader-title {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.55rem;
    color: var(--reader-accent);
    text-decoration: none;
    white-space: nowrap;
}

.reader-controls { display: flex; gap: 5px; }

.icon-btn {
    background: none; border: none;
    color: var(--reader-text);
    font-size: 1.1rem; cursor: pointer;
    padding: 8px; border-radius: 50%;
    transition: all 0.2s;
    display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: rgba(255,255,255,0.1); color: var(--reader-accent); }
.icon-btn.active { background: var(--gold); color: #000; box-shadow: 0 0 15px rgba(242, 193, 78, 0.4); }

/* Donate button special styling */
.icon-btn.donate-btn:hover { 
    background: #ffc439; 
    color: #000; 
    box-shadow: 0 0 15px rgba(255, 196, 57, 0.4); 
    transform: scale(1.05);
}

.icon-btn.donate-btn:active {
    transform: scale(0.95);
}

/* PayPal button hover effect */
.help-panel a[href*="paypal"]:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(255, 196, 57, 0.4);
}

/* POPUP OVERLAYS - THE FIX */
.panel-overlay {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    background: rgba(0,0,0,0.85) !important;
    backdrop-filter: blur(8px);
    z-index: 2000 !important;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s;
}

.panel-overlay.active { display: flex !important; opacity: 1 !important; }

.settings-panel {
    background: var(--reader-bg);
    border: 2px solid var(--reader-accent);
    width: 95%; max-width: 550px;
    height: 80vh; max-height: 600px;
    border-radius: 16px;
    display: flex; flex-direction: column;
    position: relative;
    overflow: hidden;
    box-shadow: 0 30px 60px rgba(0,0,0,0.8);
}

.settings-header {
    background: rgba(56, 225, 255, 0.1);
    border-bottom: 1px solid var(--reader-accent);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    font-weight: 600;
    font-size: 14px;
    letter-spacing: 2px;
    color: var(--reader-accent);
    position: relative;
}

.settings-header i {
    font-size: 16px;
}

.settings-header-controls {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    gap: 8px;
}

.settings-close {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    background: rgba(0,0,0,0.6);
    border: 1px solid var(--reader-accent);
    color: var(--reader-accent);
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    padding: 0;
    transition: all 0.2s;
}

.settings-close:hover {
    background: rgba(56, 225, 255, 0.2);
}

.master-reset-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    background: rgba(220, 53, 69, 0.2);
    border: 1px solid #dc3545;
    color: #dc3545;
    cursor: pointer;
    font-size: 14px;
    padding: 0;
    transition: all 0.2s;
}

.master-reset-btn:hover {
    background: rgba(220, 53, 69, 0.4);
}

.text-preview {
    border: 1px solid var(--reader-accent);
    border-radius: 8px;
    padding: 15px;
    margin-top: 8px;
    background: var(--reader-bg);
    color: var(--reader-text);
    font-size: 0.9rem;
    line-height: 1.5;
    max-height: 120px;
    overflow-y: auto;
}

.download-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.download-option {
    display: flex;
    align-items: flex-start;
    gap: 16px;
    padding: 16px;
    border: 1px solid var(--reader-accent);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: rgba(255,255,255,0.02);
}

.download-option:hover {
    background: rgba(56, 225, 255, 0.1);
    border-color: rgba(56, 225, 255, 0.5);
    transform: translateY(-1px);
}

.download-icon {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(56, 225, 255, 0.1);
    border: 1px solid var(--reader-accent);
    border-radius: 8px;
    color: var(--reader-accent);
    font-size: 20px;
    flex-shrink: 0;
}

.download-info h4 {
    margin: 0 0 8px 0;
    color: var(--reader-accent);
    font-size: 1rem;
    font-weight: 600;
}

.download-info p {
    margin: 0;
    color: var(--reader-text);
    font-size: 0.85rem;
    opacity: 0.8;
    line-height: 1.4;
}

.color-preset {
    width: 32px;
    height: 32px;
    border: 2px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.color-preset:hover {
    border-color: var(--reader-accent);
    transform: scale(1.1);
}

.color-preset.active {
    border-color: var(--reader-accent);
    box-shadow: 0 0 0 2px rgba(56, 225, 255, 0.3);
}

#highlightColorPicker {
    cursor: crosshair;
}

body.highlight-mode-active {
    cursor: crosshair !important;
}

body.highlight-mode-active #readingArea {
    cursor: crosshair !important;
}

body.highlight-mode-active .read-span {
    cursor: pointer !important;
    transition: background-color 0.2s ease;
}

body.highlight-mode-active .read-span:hover {
    background-color: rgba(56, 225, 255, 0.1);
}

body.hide-highlights .user-highlight {
    opacity: 0 !important;
    pointer-events: none !important;
}

body.hide-highlights .note-tooltip {
    display: none !important;
}

@keyframes pulse {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.1); opacity: 0.8; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes highlightPulse {
    0% { transform: scale(1); opacity: 1; box-shadow: 0 3px 12px rgba(0,0,0,0.2); }
    50% { transform: scale(1.15); opacity: 0.8; box-shadow: 0 5px 20px rgba(0,0,0,0.3); }
    100% { transform: scale(1); opacity: 1; box-shadow: 0 3px 12px rgba(0,0,0,0.2); }
}

/* Professional highlight styles */
.user-highlight {
    background-color: inherit !important;
    padding: 2px 0;
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
    box-shadow: inset 0 -2px 0 0 rgba(0,0,0,0.1);
}

.user-highlight:hover {
    filter: brightness(1.2);
    transform: translateY(-1px);
    box-shadow: inset 0 -2px 0 0 rgba(0,0,0,0.1), 0 2px 8px rgba(0,0,0,0.15);
}

/* Highlight mode visual feedback */
body.highlight-mode-active #readingArea {
    cursor: text !important;
}

body.highlight-mode-active #readingArea * {
    cursor: text !important;
}

body.highlight-mode-active #readingArea p:hover {
    background-color: rgba(56, 225, 255, 0.03);
    transition: background-color 0.2s ease;
}

/* CSS Custom Highlight API styles */
::highlight(user-highlight-1) {
    background-color: #f2c14e;
    color: inherit;
}

::highlight(user-highlight-2) {
    background-color: #38e1ff;
    color: inherit;
}

::highlight(user-highlight-3) {
    background-color: #ff6b6b;
    color: inherit;
}

::highlight(user-highlight-4) {
    background-color: #4ecdc4;
    color: inherit;
}

::highlight(user-highlight-5) {
    background-color: #45b7d1;
    color: inherit;
}

::highlight(user-highlight-6) {
    background-color: #96ceb4;
    color: inherit;
}

::highlight(user-highlight-7) {
    background-color: #ffeaa7;
    color: inherit;
}

::highlight(user-highlight-8) {
    background-color: #dfe6e9;
    color: inherit;
}

/* Fallback for browsers without Custom Highlight API */
.user-highlight {
    background-color: var(--highlight-color, #f2c14e);
    padding: 2px 0;
    border-radius: 3px;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
}

.user-highlight:hover {
    filter: brightness(1.2);
    transform: translateY(-1px);
}

.settings-tab-bar {
    display: flex;
    background: rgba(255,255,255,0.05);
    border-bottom: 1px solid rgba(255,255,255,0.1);
    width: 100%;
    position: relative;
}

.settings-tab-bar .close-btn {
    position: absolute !important;
    top: 0 !important;
    right: 0 !important;
    width: 50px !important;
    height: 100% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: rgba(255,255,255,0.2) !important;
    border: 1px solid var(--reader-accent) !important;
    color: var(--reader-accent) !important;
    cursor: pointer !important;
    font-size: 1.5rem !important;
    font-weight: bold !important;
    line-height: 1 !important;
    padding: 0 !important;
    z-index: 9999 !important;
}

.settings-tab-bar .close-btn:hover {
    background: var(--reader-accent) !important;
    color: #000 !important;
}

 .panel-header {
     position: relative;
     padding-right: 44px;
 }

 .close-btn {
     position: absolute !important;
     top: 0 !important;
     right: 0 !important;
     width: 44px;
     height: 44px;
     display: flex;
     align-items: center;
     justify-content: center;
     background: none;
     border: none;
     color: var(--reader-accent);
     cursor: pointer;
     font-size: 1.4rem;
     line-height: 1;
     padding: 0;
 }

 .close-btn:hover {
     background: rgba(255,255,255,0.06);
 }

.tab-btn {
    flex: 1; padding: 15px 5px;
    background: none; border: none;
    color: var(--reader-text);
    font-family: 'Press Start 2P', cursive;
    font-size: 0.45rem; cursor: pointer;
    opacity: 0.5; transition: 0.3s;
    border-bottom: 3px solid transparent;
}
.tab-btn.active { opacity: 1; color: var(--reader-accent); border-bottom-color: var(--reader-accent); background: rgba(255,255,255,0.02); }

.tab-content-area { padding: 25px; overflow-y: auto; flex-grow: 1; display: none; -webkit-overflow-scrolling: touch; }
.tab-content-area.active { display: block; }

 .reading-highlight {
     background: rgba(56, 225, 255, 0.18);
     box-shadow: 0 0 0 1px rgba(56, 225, 255, 0.35);
     border-radius: 4px;
 }

/* Main Container */
.reader-container {
    max-width: var(--reader-max-width);
    margin: 100px auto 120px auto;
    padding: 0 20px;
    box-sizing: border-box;
    width: 100%;
}

 .chapter-nav-wrap {
     position: sticky;
     bottom: 18px;
     display: flex;
     justify-content: center;
     margin: 60px 0 0 0;
     z-index: 50;
 }

 .chapter-nav-btn {
     display: inline-flex;
     align-items: center;
     justify-content: center;
     min-height: 44px;
     padding: 12px 16px;
     border-radius: 12px;
     border: 2px solid var(--reader-accent);
     background: rgba(0,0,0,0.75);
     color: var(--reader-accent);
     font-family: 'Press Start 2P', cursive;
     font-size: 0.55rem;
     letter-spacing: 1px;
     text-decoration: none;
     text-align: center;
     box-shadow: 0 16px 30px rgba(0,0,0,0.65);
     transition: transform 0.15s, background 0.15s, color 0.15s;
 }

 .chapter-nav-btn:hover {
     background: var(--reader-accent);
     color: #000;
     transform: translateY(-1px);
 }

/* Chapter Styling */
.chapter-title-wrap { text-align: center; margin: 120px 0 80px 0; padding-top: 50px; border-top: 1px solid rgba(255,255,255,0.1); }
.chapter-number { font-family: 'Press Start 2P'; font-size: 0.65rem; color: var(--reader-accent); opacity: 0.6; margin-bottom: 15px; display: block; letter-spacing: 2px; }
.chapter-title { font-size: 2.2rem; text-transform: uppercase; margin: 0; letter-spacing: 3px; }

/* Responsive */
@media (max-width: 600px) {
    .reader-container { padding: 0 18px; margin-top: 90px; }
    .chapter-title { font-size: 1.6rem; }
    .reader-title { font-size: 0.45rem; }
    .icon-btn { font-size: 1rem; padding: 7px; }
    .settings-panel { height: 90vh; max-height: none; width: 98%; border-radius: 0; }
    .tab-btn { font-size: 0.4rem; }
    .chapter-nav-wrap { bottom: 10px; }
}

#progressBar { position: fixed; top: 0; left: 0; height: 4px; background: var(--reader-accent); width: 0%; z-index: 1100; transition: width 0.1s; }

/* Focus Mode */
body.focus-mode::before {
    content: ""; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: radial-gradient(circle, transparent 15%, rgba(0,0,0,0.97) 85%);
    z-index: 500; pointer-events: none;
}

.lore-link { color: var(--reader-accent); border-bottom: 1px dashed var(--reader-accent); cursor: pointer; }
.lore-img { width: 100%; max-width: 128px; image-rendering: pixelated; border: 2px solid var(--reader-accent); margin: 10px 0; }

.btn-toggle {
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.2);
    color: var(--reader-text); padding: 10px;
    font-size: 0.7rem; border-radius: 4px; cursor: pointer;
}
.btn-toggle.active { background: var(--reader-accent); color: #000; }

.switch { position: relative; display: inline-block; width: 44px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #333; border-radius: 22px; }
.slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background-color: #fff; border-radius: 50%; }
input:checked + .slider { background-color: var(--reader-accent); }
input:checked + .slider:before { transform: translateX(22px); }

.toggle-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }
.setting-item { margin-bottom: 20px; }
.setting-item-label { display: block; font-size: 0.8rem; margin-bottom: 10px; }
.theme-options { display: flex; gap: 10px; }
.theme-btn { width: 35px; height: 35px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.2); cursor: pointer; }
.theme-btn.active { border-color: var(--reader-accent); transform: scale(1.1); }

/* Highlight Styles */
.user-highlight {
    cursor: pointer;
    border-radius: 2px;
    transition: all 0.2s ease;
    position: relative;
}

.user-highlight:hover {
    filter: brightness(1.2);
    transform: translateY(-1px);
}

.user-highlight.active-note {
    filter: brightness(1.3);
    box-shadow: 0 0 8px rgba(255,255,255,0.3);
}

.note-tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.9);
    color: white;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.8rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 1000;
    border: 1px solid var(--reader-accent);
}

.user-highlight.active-note .note-tooltip {
    opacity: 1;
}

/* Hide highlights when toggle is off */
body.hide-highlights .user-highlight {
    background: transparent !important;
    cursor: default !important;
}

body.hide-highlights .user-highlight .note-tooltip {
    display: none !important;
}

.highlight-start-marker {
    background: var(--reader-accent) !important;
    border-radius: 3px;
    animation: pulse 1s infinite;
}

.tap-indicator {
    position: fixed;
    width: 20px;
    height: 20px;
    background: var(--reader-accent);
    border-radius: 50%;
    pointer-events: none;
    z-index: 9999;
    animation: tap-fade 0.5s ease-out forwards;
}

body.highlight-mode-active {
    cursor: crosshair;
}

body.highlight-mode-active .reader-container {
    user-select: none;
}

/* New Highlight System Styles */
.highlight {
    background-color: inherit !important;
    padding: 2px 0;
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
}

.highlight:hover {
    filter: brightness(1.2);
    transform: translateY(-1px);
}

.highlight.has-note::after {
    content: '📝';
    position: absolute;
    top: -8px;
    right: -8px;
    font-size: 10px;
    opacity: 0.7;
}

/* Floating Highlight Toolbar */
.highlight-toolbar {
    position: fixed;
    background: var(--reader-bg);
    border: 2px solid var(--reader-accent);
    border-radius: 8px;
    padding: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    z-index: 10000;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    backdrop-filter: blur(10px);
}

.toolbar-colors {
    display: flex;
    gap: 4px;
}

.toolbar-color-btn {
    width: 28px;
    height: 28px;
    border: 2px solid transparent;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s ease;
}

.toolbar-color-btn:hover {
    transform: scale(1.1);
    border-color: var(--reader-accent);
}

.toolbar-color-btn:active {
    transform: scale(0.95);
}

.toolbar-note-btn,
.toolbar-close-btn {
    background: none;
    border: 1px solid var(--reader-accent);
    color: var(--reader-text);
    width: 32px;
    height: 32px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.toolbar-note-btn:hover,
.toolbar-close-btn:hover {
    background: var(--reader-accent);
    color: var(--reader-bg);
}

.toolbar-note-btn {
    font-size: 14px;
}

.toolbar-close-btn {
    font-size: 12px;
}

/* Note Modal */
.note-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.8);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 10001;
    backdrop-filter: blur(5px);
}

.note-modal-content {
    background: var(--reader-bg);
    border: 2px solid var(--reader-accent);
    border-radius: 12px;
    padding: 24px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.4);
}

.note-modal-content h3 {
    margin: 0 0 16px 0;
    color: var(--reader-text);
    font-size: 1.2rem;
}

.note-modal-content textarea {
    width: 100%;
    background: rgba(255,255,255,0.05);
    color: var(--reader-text);
    border: 1px solid var(--reader-accent);
    border-radius: 6px;
    padding: 12px;
    font-family: inherit;
    font-size: 14px;
    resize: vertical;
    min-height: 80px;
    box-sizing: border-box;
}

.note-modal-buttons {
    display: flex;
    gap: 12px;
    margin-top: 16px;
    justify-content: flex-end;
}

/* Note Tooltip */
.note-tooltip {
    position: fixed;
    background: rgba(0,0,0,0.9);
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 14px;
    max-width: 300px;
    z-index: 10002;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    pointer-events: auto;
    cursor: pointer;
    line-height: 1.4;
}

/* Mobile-friendly adjustments */
@media (max-width: 768px) {
    .reader-header {
        padding: 8px 10px;
        flex-wrap: wrap;
        gap: 5px;
    }
    
    .reader-title {
        font-size: 0.45rem;
        order: 1;
        flex: 1;
        min-width: 120px;
    }
    
    .reader-controls {
        order: 2;
        gap: 3px;
    }
    
    .icon-btn {
        font-size: 0.9rem;
        padding: 6px;
        min-width: 36px;
        min-height: 36px;
    }
    
    .highlight-toolbar {
        padding: 6px;
        gap: 6px;
    }
    
    .toolbar-color-btn {
        width: 24px;
        height: 24px;
    }
    
    .toolbar-note-btn,
    .toolbar-close-btn {
        width: 28px;
        height: 28px;
        font-size: 12px;
    }
    
    .note-modal-content {
        padding: 20px;
        margin: 20px;
    }
    
    /* Ensure settings button is visible */
    #settingsBtn {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
    }
}

@keyframes pulse {
    0%, 100% { opacity: 0.6; }
    50% { opacity: 1; }
}

@keyframes tap-fade {
    0% { transform: scale(0); opacity: 1; }
    100% { transform: scale(2); opacity: 0; }
}
