/* ── Reset & Base ── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0f1117;
    --bg-card: #1a1d27;
    --bg-surface: #22252f;
    --bg-hover: #2a2e3a;
    --text: #e4e6ec;
    --text-dim: #8b8fa3;
    --text-muted: #5a5e70;
    --accent: #6c5ce7;
    --accent-hover: #7f71ee;
    --accent-glow: rgba(108, 92, 231, 0.25);
    --danger: #e74c3c;
    --danger-hover: #c0392b;
    --success: #27ae60;
    --warning: #f39c12;
    --border: #2d3040;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --transition: 0.2s ease;
}

html, body {
    height: 100%;
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
    box-shadow: 0 0 20px var(--accent-glow);
}

.btn-sm {
    padding: 6px 14px;
    font-size: 13px;
}

.btn-done {
    background: var(--success);
    color: #fff;
}

.btn-done:hover {
    background: #2ecc71;
}

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
}

.btn-icon:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.btn-icon.active {
    background: var(--accent);
    color: #fff;
}

/* ── Library Page ── */
.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}

.app-header h1 {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 24px;
    font-weight: 600;
    color: var(--text);
}

.app-header h1 svg {
    color: var(--accent);
}

/* ── Upload Zone ── */
.upload-zone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 40px;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
    margin-bottom: 32px;
    position: relative;
    overflow: hidden;
}

.upload-zone:hover,
.upload-zone.drag-over {
    border-color: var(--accent);
    background: var(--accent-glow);
}

.upload-zone-content svg {
    color: var(--text-muted);
    margin-bottom: 12px;
}

.upload-zone-content p {
    font-size: 16px;
    color: var(--text-dim);
    margin-bottom: 4px;
}

.upload-zone-content span {
    font-size: 13px;
    color: var(--text-muted);
}

.upload-progress {
    display: none;
    margin-top: 20px;
}

.upload-progress.active {
    display: block;
}

.progress-bar {
    height: 6px;
    background: var(--bg-surface);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.3s ease;
    width: 0%;
}

.progress-text {
    font-size: 13px;
    color: var(--text-dim);
}

/* ── URL Import ── */
.url-import {
    margin-bottom: 24px;
}

.url-import-inner {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 16px;
}

.url-import-inner svg {
    color: var(--text-muted);
    flex-shrink: 0;
}

.url-import-inner input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text);
    font-size: 14px;
    min-width: 0;
}

.url-import-inner input::placeholder {
    color: var(--text-muted);
}

.url-import-inner .btn {
    flex-shrink: 0;
}

.url-import-inner .btn:disabled {
    opacity: 0.5;
    cursor: wait;
}

/* ── Render Queue ── */
.render-queue {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
}

.render-queue:empty {
    display: none;
}

.queue-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px 16px;
    transition: opacity var(--transition), border-color var(--transition);
}

.queue-item .queue-item-info {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.queue-item .queue-item-icon {
    color: var(--accent);
    flex-shrink: 0;
}

.queue-item .queue-item-details {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.queue-item .queue-item-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.queue-item .queue-item-status {
    font-size: 12px;
    color: var(--text-dim);
}

.queue-item .queue-item-bar {
    height: 4px;
    background: var(--bg-surface);
    border-radius: 2px;
    overflow: hidden;
}

.queue-item .queue-item-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.queue-item.queue-rendering .queue-item-icon {
    color: var(--warning);
    animation: pulse-icon 1.2s ease infinite;
}

.queue-item.queue-rendering .queue-item-fill {
    background: var(--warning);
}

.queue-item.queue-done {
    border-color: var(--success);
}

.queue-item.queue-done .queue-item-icon {
    color: var(--success);
}

.queue-item.queue-done .queue-item-fill {
    background: var(--success);
}

.queue-item.queue-done .queue-item-status {
    color: var(--success);
}

.queue-item.queue-error {
    border-color: var(--danger);
}

.queue-item.queue-error .queue-item-icon {
    color: var(--danger);
}

.queue-item.queue-error .queue-item-status {
    color: var(--danger);
}

@keyframes pulse-icon {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* ── Document Cards Grid ── */
.documents-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
}

.doc-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
}

.doc-card:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.card-thumb {
    width: 100%;
    height: 260px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-surface);
    overflow: hidden;
}

.card-thumb canvas,
.card-thumb img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.thumb-placeholder {
    color: var(--text-muted);
}

.card-progress {
    height: 3px;
    background: var(--bg-surface);
}

.card-progress-fill {
    height: 100%;
    background: var(--accent);
    transition: width 0.3s;
}

.card-info {
    padding: 12px 14px;
}

.card-title {
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 4px;
}

.card-meta {
    display: flex;
    gap: 12px;
    font-size: 12px;
    color: var(--text-muted);
}

.card-resume {
    margin-top: 6px;
    font-size: 12px;
    color: var(--accent);
    font-weight: 500;
}

.card-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    opacity: 0;
    transition: opacity var(--transition);
}

.doc-card:hover .card-actions {
    opacity: 1;
}

.btn-delete {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    color: var(--danger) !important;
}

.btn-delete:hover {
    background: var(--danger);
    color: #fff !important;
}

/* ── Empty State ── */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    color: var(--text-muted);
    text-align: center;
}

.empty-state svg {
    margin-bottom: 16px;
    opacity: 0.4;
}

.empty-state h3 {
    font-size: 18px;
    color: var(--text-dim);
    margin-bottom: 6px;
}

.empty-state p {
    font-size: 14px;
}

/* ── Toast ── */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    padding: 12px 24px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    z-index: 10000;
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast-success {
    background: var(--success);
    color: #fff;
}

.toast-error {
    background: var(--danger);
    color: #fff;
}

/* ══════════════════════════════════════════════
   READER PAGE
   ══════════════════════════════════════════════ */

.reader-body {
    overflow: hidden;
    height: 100vh;
    background: #0a0c10;
}

.reader-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* ── Reader Toolbar ── */
.reader-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    z-index: 100;
    min-height: 48px;
    flex-shrink: 0;
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
}

.toolbar-title {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-dim);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toolbar-center {
    flex-shrink: 0;
    padding: 0 16px;
}

.page-indicator {
    font-size: 13px;
    color: var(--text-dim);
    font-weight: 500;
    white-space: nowrap;
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.toolbar-sep {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 4px;
}

/* ── Zoom Layer ── */
.zoom-layer {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 20;
    background: rgba(10, 12, 16, 0.96);
}

.zoom-layer.active {
    display: block;
}

.zoom-layer-scroll {
    width: 100%;
    height: 100%;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: none;
    cursor: grab;
}

.zoom-layer-scroll.dragging {
    cursor: grabbing;
}

.zoom-layer-canvas {
    min-width: 100%;
    min-height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.zoom-layer-stage {
    position: relative;
    flex-shrink: 0;
    background: #fff;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    overflow: hidden;
}

.zoom-layer-stage img {
    display: block;
    width: 100%;
    height: 100%;
    user-select: none;
    -webkit-user-drag: none;
    pointer-events: none;
}

.zoom-layer-anno {
    position: absolute;
    inset: 0;
}

.zoom-layer-close {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 30;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
}

.zoom-layer-badge {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 30;
    padding: 6px 10px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    backdrop-filter: blur(4px);
}

.gesture-layer {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 25;
    background: transparent;
    touch-action: none;
}

/* ── Anno Page Navigation ── */
.anno-page-info {
    font-size: 12px;
    color: var(--text-dim);
    min-width: 80px;
    text-align: center;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.anno-page-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 30px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: all var(--transition);
    flex-shrink: 0;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}

.anno-page-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
    border-color: var(--text-muted);
}

.anno-page-btn.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* ── Loading Overlay ── */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(10, 12, 16, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    gap: 16px;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-overlay p {
    font-size: 14px;
    color: var(--text-dim);
}

/* ── Annotation Toolbar ── */
.annotation-toolbar {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    z-index: 100;
    flex-shrink: 0;
    overflow-x: auto;
}

.annotation-toolbar.active {
    display: flex;
}

.anno-tools {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: nowrap;
}

.anno-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: all var(--transition);
    flex-shrink: 0;
}

.anno-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.anno-btn.active {
    background: var(--accent);
    color: #fff;
}

.anno-separator {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 6px;
    flex-shrink: 0;
}

.color-picker-group {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.color-picker-group input[type="color"] {
    width: 28px;
    height: 28px;
    border: 2px solid var(--border);
    border-radius: 6px;
    background: transparent;
    cursor: pointer;
    padding: 0;
}

.color-picker-group input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 2px;
}

.color-picker-group input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 3px;
}

.preset-colors {
    display: flex;
    gap: 4px;
}

.preset-color {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color var(--transition);
}

.preset-color:hover {
    border-color: var(--text-dim);
}

.size-control {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    margin-left: 8px;
}

.size-control label {
    font-size: 12px;
    color: var(--text-muted);
    white-space: nowrap;
}

.size-control input[type="range"] {
    width: 80px;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border);
    border-radius: 2px;
    outline: none;
}

.size-control input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

/* ── Flipbook Area ── */
.flipbook-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    background: #0a0c10;
    min-height: 0;
}

.flipbook-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.flip-page {
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}

.flip-page .page-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    user-select: none;
    -webkit-user-drag: none;
}

.flip-page .anno-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    user-select: none;
    -webkit-user-drag: none;
}

.page-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
    height: 100%;
    font-size: 13px;
    color: #999;
    background: #f5f5f5;
}

.page-loading-spinner {
    width: 24px;
    height: 24px;
    border: 2px solid #ddd;
    border-top-color: #999;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* ── Annotation View (dedicated single-page) ── */
.annotation-view {
    display: none;
    flex: 1;
    align-items: center;
    justify-content: center;
    background: #0a0c10;
    min-height: 0;
    overflow: auto;
    padding: 12px;
}

.annotation-view-inner {
    position: relative;
    flex-shrink: 0;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    overflow: hidden;
}

.annotation-view-inner img {
    display: block;
    user-select: none;
    -webkit-user-drag: none;
}

.annotation-view-inner .canvas-container {
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
}

.annotation-view-inner canvas {
    position: absolute;
    top: 0;
    left: 0;
}

/* ── Bottom Navigation ── */
.reader-nav {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    z-index: 100;
    flex-shrink: 0;
}

.page-slider-container {
    flex: 1;
}

.page-slider {
    width: 100%;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border);
    border-radius: 2px;
    outline: none;
    cursor: pointer;
}

.page-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 0 8px var(--accent-glow);
}

.page-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

/* ── Bookmark Panel ── */
.bookmark-panel {
    position: fixed;
    top: 0;
    right: -320px;
    width: 300px;
    height: 100vh;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    z-index: 200;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.3);
}

.bookmark-panel.open {
    right: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.panel-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.bookmark-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition);
}

.bookmark-item:hover {
    background: var(--bg-hover);
}

.bookmark-info {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-dim);
    font-size: 14px;
}

.bookmark-info svg {
    color: var(--accent);
    flex-shrink: 0;
}

.empty-bookmarks {
    padding: 40px 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
}

/* ── StPageFlip overrides ── */
.stf__parent {
    margin: 0 auto;
}

.stf__wrapper {
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
}


/* ── Responsive ── */
@media (max-width: 768px) {
    .app-container {
        padding: 16px;
    }

    .app-header h1 {
        font-size: 18px;
    }

    .upload-zone {
        padding: 24px;
    }

    .documents-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 12px;
    }

    .card-thumb {
        height: 200px;
    }

    /* Reader */
    .toolbar-title {
        max-width: 100px;
    }

    .toolbar-center {
        padding: 0 4px;
    }

    .annotation-toolbar {
        padding: 6px 8px;
    }

    .anno-tools {
        gap: 2px;
    }

    .anno-page-info {
        min-width: 60px;
        font-size: 11px;
    }

    .preset-colors {
        display: none;
    }

    .size-control {
        display: none;
    }

    .reader-nav {
        gap: 8px;
        padding: 6px 12px;
    }

    .bookmark-panel {
        width: 260px;
        right: -280px;
    }

    .annotation-view {
        padding: 8px;
    }

    .gesture-layer {
        display: block;
    }

    .zoom-layer-close {
        top: 8px;
        right: 8px;
    }

    .zoom-layer-badge {
        top: 8px;
        left: 8px;
    }
}

@media (max-width: 480px) {
    .documents-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .card-thumb {
        height: 160px;
    }

    .card-info {
        padding: 8px 10px;
    }

    .card-title {
        font-size: 12px;
    }

    .card-meta {
        font-size: 11px;
    }
}

/* ── Scrollbar ── */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ── Selection ── */
::selection {
    background: var(--accent);
    color: #fff;
}

/* ── Fabric.js canvas overrides ── */
.upper-canvas {
    cursor: crosshair !important;
}
