:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #10b981;
    --background: #0f172a;
    --surface: #1e293b;
    --surface-light: #334155;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --border: #475569;
    --error: #ef4444;
    --warning: #f59e0b;
    --success: #10b981;
    --radius: 12px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--background);
    color: var(--text);
    overflow: hidden;
    user-select: none;
}

/* Header */
.header {
    background: var(--surface);
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: relative;
    z-index: 50;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

.header-controls {
    display: flex;
    gap: 1rem;
    align-items: center;
}

/* Toggle Switches */
.toggle-group {
    display: flex;
    background: var(--surface-light);
    border-radius: var(--radius);
    padding: 0.25rem;
}

.toggle-option {
    padding: 0.5rem 1rem;
    border-radius: calc(var(--radius) - 0.25rem);
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 0.875rem;
    font-weight: 500;
}

.toggle-option.active {
    background: var(--primary);
    color: white;
}

.toggle-option:not(.active) {
    color: var(--text-muted);
}

/* Buttons */
.btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.875rem;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    min-height: 44px; /* Touch target */
}

.btn:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: translateY(-1px);
}

.btn:disabled {
    background: var(--surface-light);
    color: var(--text-muted);
    cursor: not-allowed;
    transform: none;
}

.btn-secondary {
    background: var(--surface-light);
    color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--border);
}

.btn-icon {
    padding: 0.75rem 1rem;
    min-width: auto;
    justify-content: center;
    display: flex;
    align-items: center;
}

/* Main Layout */
.main-container {
    display: flex;
    height: calc(100vh - 81px);
    position: relative;
}

/* Sidebar */
.sidebar {
    width: 380px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    transition: transform 0.3s ease;
    position: relative;
    z-index: 40;
}

.sidebar.collapsed {
    transform: translateX(-100%);
}

.sidebar-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--background);
}

.sidebar-title {
    font-weight: 600;
    font-size: 1rem;
    color: var(--text-muted);
}

.section {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.section:last-child {
    border-bottom: none;
}

.section-title {
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
}

/* Form Elements */
.input {
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--background);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 0.875rem;
    transition: border-color 0.2s ease;
    min-height: 44px;
}

.input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.input-file {
    cursor: pointer;
}

.input-file::-webkit-file-upload-button {
    display: none;
}

/* Status Indicators */
.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    margin-bottom: 1rem;
}

.status-success {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success);
    border: 1px solid rgba(16, 185, 129, 0.2);
}

.status-error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error);
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.status-warning {
    background: rgba(245, 158, 11, 0.1);
    color: var(--warning);
    border: 1px solid rgba(245, 158, 11, 0.2);
}

.status-info {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    border: 1px solid rgba(99, 102, 241, 0.2);
}

/* Loading States */
.loading {
    position: relative;
    overflow: hidden;
}

.loading::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
    animation: loading 1.5s infinite;
}

@keyframes loading {
    0% { left: -100%; }
    100% { left: 100%; }
}

/* Viewer Container */
.viewer-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--background);
    position: relative;
}

.viewer-header {
    background: var(--surface);
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    gap: 0.5rem;
}

.viewer-controls {
    display: flex;
    gap: 0.5rem;
}

#rotate-btn.active {
    background: var(--primary);
    color: white;
}

.page-info {
    font-weight: 600;
    color: var(--text-muted);
    flex-grow: 1;
    text-align: center;
}

.viewer-content {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 1rem;
    overflow: auto;
    position: relative;
}

.pdf-container {
    position: relative;
    width: 100%;
    height: auto;
    max-width: 100%;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    display: flex;
    justify-content: center;
    overflow: hidden;
}

#pdf-canvas {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    object-fit: contain;
    border-radius: var(--radius);
    transform-origin: center center;
    transition: transform 0.1s ease-out;
    cursor: grab;
}

#pdf-canvas:active {
    cursor: grabbing;
}

#text-layer {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: auto;
    user-select: text;
    transform-origin: center center;
    overflow: hidden;
    opacity: 1; 
    line-height: 1.0;
}

#text-layer > span {
    color: transparent;
    background-color: transparent;
    position: absolute;
    white-space: pre;
    cursor: pointer;
    transform-origin: 0% 0%;
    border-radius: 4px;
    transition: background-color 0.2s ease;
}

#text-layer > span:hover {
    background-color: rgba(99, 102, 241, 0.2) !important;
}

#text-layer .highlighted {
    background-color: rgba(16, 185, 129, 0.3) !important;
    box-shadow: 0 0 0 2px var(--success);
}

#text-layer .auto-reading {
    background: rgba(16, 185, 129, 0.5) !important;
    box-shadow: 0 0 0 3px var(--success), 0 0 10px rgba(16, 185, 129, 0.3);
    animation: pulse 2s ease-in-out infinite;
}

/* Text Panel */
.text-panel {
    background: var(--surface);
    border-top: 1px solid var(--border);
    max-height: 300px;
    overflow-y: auto;
}

.text-content {
    padding: 1.5rem;
}

.selected-text {
    background: var(--background);
    padding: 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    min-height: 80px;
    border: 1px solid var(--border);
    color: var(--text-muted);
    line-height: 1.6;
}

.text-controls {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    align-items: center;
}

.speed-control {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.speed-slider {
    width: 100px;
    height: 6px;
    border-radius: 3px;
    background: var(--surface-light);
    outline: none;
    -webkit-appearance: none;
}

.speed-slider::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
}

/* Bookmark Item Style */
.bookmark-item {
    background: var(--background);
    padding: 0.75rem;
    border-radius: 6px;
    margin-bottom: 0.5rem;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.2s ease;
}

.bookmark-item:hover {
    background: var(--surface-light);
}

/* Mobile Optimizations */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        max-width: 380px;
        z-index: 50;
    }

    .sidebar.collapsed {
        transform: translateX(-100%);
    }

    .header {
        padding: 1rem;
    }

    .logo {
        font-size: 1.25rem;
    }

    .btn {
        padding: 0.625rem 1rem;
        font-size: 0.8rem;
    }

    .section {
        padding: 1rem;
    }

    .viewer-header {
        padding: 1rem;
    }

    .text-controls {
        flex-direction: column;
        align-items: stretch;
    }

    .speed-control {
        justify-content: space-between;
    }

    .viewer-content {
        padding: 0.5rem;
    }

    #pdf-canvas {
        max-height: calc(100vh - 150px);
    }
}

@media (max-width: 480px) {
    .viewer-content {
        padding: 0.25rem;
    }

    #pdf-canvas {
        max-height: calc(100vh - 130px);
    }

    .viewer-header {
        padding: 0.75rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .page-info {
        order: 2;
        width: 100%;
        text-align: center;
        margin-top: 0.5rem;
    }
}

/* Fullscreen Mode */
.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 9999;
    background: var(--background);
}

.fullscreen .sidebar {
    display: none;
}

.fullscreen .main-container {
    height: 100vh;
}

/* Overlay */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 30;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.overlay.active {
    opacity: 1;
    visibility: visible;
}

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

.fade-in {
    animation: fadeIn 0.3s ease;
}

@keyframes pageTransition {
    0% { opacity: 1; transform: scale(1); }
    50% { opacity: 0; transform: scale(0.95); }
    100% { opacity: 1; transform: scale(1); }
}

.page-transition {
    animation: pageTransition 0.4s ease-in-out;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.pulse {
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes waveform {
    0%, 100% { height: 4px; }
    25% { height: 12px; }
    50% { height: 8px; }
    75% { height: 16px; }
}

.audio-indicator {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-left: 0.5rem;
}

.audio-bar {
    width: 3px;
    height: 4px;
    background: var(--success);
    border-radius: 2px;
    animation: waveform 1.2s ease-in-out infinite;
}

.audio-bar:nth-child(2) { animation-delay: 0.1s; }
.audio-bar:nth-child(3) { animation-delay: 0.2s; }
.audio-bar:nth-child(4) { animation-delay: 0.3s; }
.audio-bar:nth-child(5) { animation-delay: 0.4s; }

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

/* Progress Bar */
.progress-bar {
    position: absolute;
    top: 0;
    left: 0;
    height: 3px;
    background: var(--primary);
    transition: width 0.3s ease;
    z-index: 60;
}

/* Tooltip */
.tooltip {
    position: relative;
}

.tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface-light);
    color: var(--text);
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: all 0.2s ease;
    z-index: 70;
    margin-top: 8px;
}

.tooltip:hover::after {
    opacity: 1;
}

/* Help Modal */
.help-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 9999;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.help-modal.active {
    display: flex;
}

.help-content {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 2rem;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
}

.help-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
    transition: background 0.2s ease;
}

.help-close:hover {
    background: var(--surface-light);
}

.shortcut-list {
    display: grid;
    gap: 0.75rem;
    margin-top: 1rem;
}

.shortcut-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
}

.shortcut-key {
    background: var(--background);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.8rem;
    color: var(--primary);
}

#autoplay-btn.active {
    background-color: var(--error);
}
#autoplay-btn.active:hover {
    background-color: #c0392b;
}