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

:root {
    --primary-color: #0066cc;
    --primary-hover: #0052a3;
    --background: #f5f5f5;
    --surface: #ffffff;
    --border: #e0e0e0;
    --text-primary: #333333;
    --text-secondary: #666666;
    --shadow: rgba(0, 0, 0, 0.1);
    --highlight-color: rgba(255, 235, 59, 0.4);
    --redaction-color: #000000;
    --search-highlight: rgba(255, 152, 0, 0.5);
}

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

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
.header {
    background-color: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 16px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 4px var(--shadow);
    z-index: 100;
}

.header h1 {
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
}

.header-actions {
    display: flex;
    gap: 12px;
}

/* Toolbar */
.toolbar {
    background-color: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    display: flex;
    gap: 24px;
    align-items: center;
    flex-wrap: wrap;
    box-shadow: 0 1px 3px var(--shadow);
    z-index: 99;
}

.toolbar-section {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* Buttons */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 8px;
    background-color: transparent;
    color: var(--text-primary);
}

.btn:hover:not(:disabled) {
    background-color: rgba(0, 0, 0, 0.05);
}

.btn:active:not(:disabled) {
    transform: scale(0.98);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
    background-color: var(--primary-hover);
}

.btn-secondary {
    background-color: var(--surface);
    color: var(--primary-color);
    border: 1px solid var(--primary-color);
}

.btn-secondary:hover:not(:disabled) {
    background-color: rgba(0, 102, 204, 0.08);
}

.btn-icon {
    padding: 8px;
    min-width: 36px;
    justify-content: center;
}

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

/* Zoom Controls */
.zoom-level {
    font-size: 14px;
    font-weight: 500;
    min-width: 50px;
    text-align: center;
}

.zoom-preset {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    background-color: var(--surface);
    color: var(--text-primary);
}

.zoom-preset:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.page-info {
    font-size: 14px;
    color: var(--text-secondary);
    min-width: 120px;
    text-align: center;
}

/* Search Panel */
.search-panel {
    background-color: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    box-shadow: 0 2px 4px var(--shadow);
    z-index: 98;
}

.search-input-container {
    display: flex;
    gap: 8px;
    align-items: center;
    max-width: 600px;
}

.search-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
}

.search-input:focus {
    border-color: var(--primary-color);
}

.search-results {
    margin-top: 8px;
    font-size: 13px;
    color: var(--text-secondary);
}

/* Content with Sidebar layout */
.content-with-sidebar {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* Main Content */
.main-content {
    flex: 1;
    overflow: auto;
    position: relative;
    background-color: var(--background);
    min-width: 0;
}

/* Upload Area */
.upload-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 48px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.upload-area:hover {
    background-color: rgba(0, 102, 204, 0.05);
}

.upload-area svg {
    color: var(--primary-color);
    margin-bottom: 24px;
}

.upload-area h2 {
    font-size: 24px;
    margin-bottom: 12px;
    color: var(--text-primary);
}

.upload-area p {
    color: var(--text-secondary);
    margin-bottom: 24px;
}

.upload-area.dragging {
    background-color: rgba(0, 102, 204, 0.1);
    border: 2px dashed var(--primary-color);
}

.load-by-id-divider {
    display: flex;
    align-items: center;
    width: 100%;
    max-width: 420px;
    margin: 20px 0 16px;
    gap: 12px;
    color: var(--text-secondary);
    font-size: 13px;
}

.load-by-id-divider::before,
.load-by-id-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background-color: var(--border);
}

.load-by-id-group {
    display: flex;
    gap: 8px;
    width: 100%;
    max-width: 420px;
}

.doc-id-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
    color: var(--text-primary);
    background-color: var(--surface);
}

.doc-id-input:focus {
    border-color: var(--primary-color);
}

/* Viewer Container */
.viewer-container {
    height: 100%;
    overflow: auto;
    padding: 24px;
}

.pages-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* PDF Page */
.pdf-page {
    background-color: var(--surface);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px var(--shadow);
    margin-bottom: 24px;
    margin-right: 24px;
    position: relative;
    border-radius: 4px;
    -webkit-user-drag: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Placeholder state - fixed dimensions for instant layout */
.pdf-page.placeholder {
    background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%);
    border: 2px dashed var(--border);
}

.pdf-page.placeholder .page-number {
    font-size: 18px;
    color: var(--text-secondary);
    font-weight: 500;
}

.pdf-page.placeholder .loading-spinner {
    display: none;
}

/* Loading state */
.pdf-page.loading {
    background: linear-gradient(135deg, #f0f4ff 0%, #e8ecf4 100%);
    border: 2px solid var(--primary-color);
}

.pdf-page.loading .loading-spinner {
    display: block;
    width: 40px;
    height: 40px;
    border: 3px solid var(--border);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-top: 16px;
}

/* Loaded state */
.pdf-page.loaded {
    border: none;
    justify-content: flex-start;
    align-items: flex-start;
}

.pdf-page.loaded .page-image {
    display: block;
    max-width: 100%;
    height: auto;
}

/* Error state */
.pdf-page.error {
    background-color: #fff5f5;
    border: 2px solid #ff6b6b;
}

.pdf-page.error .error-content {
    text-align: center;
}

.pdf-page.error .error-content span {
    display: block;
    color: #e03131;
    margin-bottom: 12px;
    font-weight: 500;
}

.pdf-page.error .error-content button {
    padding: 8px 20px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 500;
}

.pdf-page.error .error-content button:hover {
    background-color: var(--primary-hover);
}

.page-container {
    position: relative;
    display: inline-block;
}

/* Page loader */
.page-loader {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 40px;
    min-height: 400px;
}

.loader-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.loader-text {
    margin-top: 16px;
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}

/* Page error */
.page-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 40px;
    min-height: 400px;
}

.error-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.error-text {
    font-size: 16px;
    color: #e03131;
    margin-bottom: 16px;
    font-weight: 500;
}

.retry-btn {
    padding: 8px 24px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

.retry-btn:hover {
    background-color: var(--primary-hover);
}

.retry-btn:active {
    transform: scale(0.98);
}

.page-image {
    display: block;
    max-width: none;
    height: auto;
    user-select: none;
    -webkit-user-drag: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* OCR Text Overlay */
.ocr-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    pointer-events: none;
}

/* Line-level text for selection/copy */
.ocr-line {
    position: absolute;
    pointer-events: auto;
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    color: transparent;
    white-space: pre;
    overflow: visible;
    cursor: text;
    z-index: 2;
}

/* Word-level elements for interaction */
.ocr-word {
    position: absolute;
    pointer-events: none;
    user-select: none;
    transition: background-color 0.15s ease;
    z-index: 1;
}

/* Enable word interaction only in selection mode */
body.selection-mode .ocr-word {
    pointer-events: auto;
    cursor: pointer;
    z-index: 3;
}

body.selection-mode .ocr-line {
    pointer-events: none;
    user-select: none;
}

/* Highlight OCR word on hover when selection mode is active */
body.selection-mode .ocr-word:hover {
    background-color: rgba(33, 150, 243, 0.15);
}

.ocr-word.search-highlight {
    background-color: rgba(255, 235, 59, 0.4);
}

.ocr-word.search-highlight.current {
    background-color: rgba(255, 152, 0, 0.5);
}

/* Unified selection box that encompasses all selected words */
.unified-selection-box {
    position: absolute;
    background-color: rgba(33, 150, 243, 0.1);
    border: 2px solid #2196F3;
    pointer-events: none;
    border-radius: 2px;
    box-sizing: border-box;
    z-index: 2;
}

/* Drawing selection box during rectangle drag */
.drawing-selection-box {
    position: absolute;
    background-color: rgba(33, 150, 243, 0.1);
    border: 2px dashed #2196F3;
    pointer-events: none;
    border-radius: 2px;
    box-sizing: border-box;
    z-index: 10;
}

/* Drawing box style during rectangle drag */
.annotation-box.drawing {
    border: 2px dashed #2196F3;
    background-color: rgba(33, 150, 243, 0.1);
    pointer-events: none;
}

.ocr-line::selection {
    background: rgba(0, 102, 204, 0.3);
    color: transparent;
}

.ocr-line span::selection {
    background: rgba(0, 102, 204, 0.3);
    color: transparent;
}

/* Redaction Box */
.redaction-box {
    position: absolute;
    background-color: var(--redaction-color);
    cursor: move;
    pointer-events: auto;
    border: 2px solid transparent;
    transition: border-color 0.2s ease;
}

.redaction-box:hover,
.redaction-box.selected {
    border-color: var(--primary-color);
}

.redaction-box.drawing {
    border: 2px dashed var(--primary-color);
    background-color: rgba(0, 0, 0, 0.7);
}

/* Resize Handles — all 8 handles are uniform 6×6 circles (matches editor-v1) */
.resize-handle {
    position: absolute;
    width: calc(6px * var(--annotation-scale, 1));
    height: calc(6px * var(--annotation-scale, 1));
    background-color: #FBFBFB;
    border: 1px solid #0288d1;
    border-radius: 50%;
    opacity: 0;
    transition: opacity 0.2s ease;
    z-index: 10;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.annotation-box.selected .resize-handle {
    opacity: 1;
}

/* Corner handles */
.resize-nw { top: calc(-3px * var(--annotation-scale, 1)); left: calc(-3px * var(--annotation-scale, 1)); cursor: nw-resize; }
.resize-ne { top: calc(-3px * var(--annotation-scale, 1)); right: calc(-3px * var(--annotation-scale, 1)); cursor: ne-resize; }
.resize-sw { bottom: calc(-3px * var(--annotation-scale, 1)); left: calc(-3px * var(--annotation-scale, 1)); cursor: sw-resize; }
.resize-se { bottom: calc(-3px * var(--annotation-scale, 1)); right: calc(-3px * var(--annotation-scale, 1)); cursor: se-resize; }

/* Edge handles — circles at midpoints */
.resize-n { top: calc(-3px * var(--annotation-scale, 1)); left: 50%; transform: translateX(-50%); cursor: n-resize; }
.resize-s { bottom: calc(-3px * var(--annotation-scale, 1)); left: 50%; transform: translateX(-50%); cursor: s-resize; }
.resize-e { right: calc(-3px * var(--annotation-scale, 1)); top: 50%; transform: translateY(-50%); cursor: e-resize; }
.resize-w { left: calc(-3px * var(--annotation-scale, 1)); top: 50%; transform: translateY(-50%); cursor: w-resize; }

/* Highlight */
.highlight-box {
    position: absolute;
    background-color: var(--highlight-color);
    pointer-events: auto;
    border-radius: 2px;
    border: 2px solid transparent;
    transition: border-color 0.2s ease;
}

.highlight-box:hover,
.highlight-box.selected {
    border-color: rgba(255, 193, 7, 0.8);
}

/* Annotation Box */
.annotation-box {
    --annotation-scale: 1;
    position: absolute;
    background-color: rgba(33, 150, 243, 0.15);
    border: 1px solid #2196F3;
    pointer-events: auto;
    cursor: move;
    border-radius: 2px;
    transition: all 0.15s ease;
    box-sizing: border-box;
    z-index: 4;
}

.annotation-box:hover {
    background-color: rgba(33, 150, 243, 0.2);
}

.annotation-box.selected {
    background-color: rgba(33, 150, 243, 0.25);
    border-color: #1976D2;
    box-shadow: 0 0 0 1px rgba(33, 150, 243, 0.2), 0 2px 6px rgba(33, 150, 243, 0.25);
}

.annotation-box.drawing {
    border: 1px dashed #2196F3;
    background-color: rgba(33, 150, 243, 0.1);
}

/* Server-returned annotation (LLM-detected entities) */
.annotation-box.server-annotation {
    background-color: rgba(233, 30, 99, 0.15);
    border: 1px solid #E91E63;
}

.annotation-box.server-annotation:hover {
    background-color: rgba(233, 30, 99, 0.18);
}

.annotation-box.server-annotation.selected {
    background-color: rgba(233, 30, 99, 0.22);
    border-color: #C2185B;
    box-shadow: 0 0 0 1px rgba(233, 30, 99, 0.2), 0 2px 6px rgba(233, 30, 99, 0.25);
}

/* Search Highlight */
.search-highlight {
    background-color: var(--search-highlight) !important;
}

.search-highlight.current {
    background-color: rgba(255, 87, 34, 0.6) !important;
    outline: 2px solid #ff5722;
}

/* Loading Overlay */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

/* Note: @keyframes spin is defined earlier in this file */

.loading-overlay p {
    margin-top: 16px;
    font-size: 16px;
    color: var(--text-secondary);
}

/* Annotation Sidebar */
.annotation-sidebar {
    width: 320px;
    min-width: 320px;
    background-color: var(--surface);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.sidebar-inner {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

/* Empty state */
.sidebar-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 60px 24px;
    gap: 8px;
    flex: 1;
}

.sidebar-empty-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    margin-top: 8px;
}

.sidebar-empty-subtitle {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* Sidebar when annotation is selected */
.sidebar-details {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.sidebar-selection-section {
    padding: 20px 20px 0;
}

.sidebar-heading {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 14px;
}

.sidebar-category-display {
    width: 100%;
    padding: 8px 12px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background-color: var(--background);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    box-sizing: border-box;
    margin-bottom: 8px;
}

.sidebar-category-display.server-category {
    border-color: rgba(233, 30, 99, 0.4);
    background-color: rgba(233, 30, 99, 0.06);
    color: #C2185B;
}

.sidebar-category-display.user-category {
    border-color: rgba(33, 150, 243, 0.4);
    background-color: rgba(33, 150, 243, 0.06);
    color: #1565C0;
}

.sidebar-description {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin-bottom: 14px;
    word-break: break-word;
}

.sidebar-apply-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
}

.sidebar-delete-link {
    font-size: 12px;
    color: #f44336;
    cursor: pointer;
    background: none;
    border: none;
    padding: 0;
    text-decoration: underline;
}

.sidebar-delete-link:hover {
    color: #d32f2f;
}

.sidebar-divider {
    height: 1px;
    background-color: var(--border);
    margin: 0 0;
}

/* Comments section */
.sidebar-comments-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    padding: 16px 20px 20px;
    gap: 12px;
    overflow: hidden;
}

.sidebar-comments-heading {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
}

.sidebar-comments-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 80px;
}

.sidebar-no-comments {
    font-size: 13px;
    color: var(--text-secondary);
    text-align: center;
}

.sidebar-comment-entry {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: auto;
}

.sidebar-comment-input {
    width: 100%;
    min-height: 72px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: inherit;
    font-size: 13px;
    color: var(--text-primary);
    background-color: var(--background);
    resize: vertical;
    box-sizing: border-box;
    outline: none;
    transition: border-color 0.15s ease;
}

.sidebar-comment-input:focus {
    border-color: var(--primary-color);
}

.sidebar-comment-input::placeholder {
    color: var(--border);
}

.sidebar-add-btn {
    align-self: flex-end;
    padding: 8px 20px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.sidebar-add-btn:hover {
    background-color: var(--primary-hover);
}

/* Responsive Design */
@media (max-width: 768px) {
    .header {
        padding: 12px 16px;
    }

    .header h1 {
        font-size: 18px;
    }

    .toolbar {
        padding: 8px 16px;
        gap: 12px;
    }

    .toolbar-section {
        gap: 4px;
    }

    .viewer-container {
        padding: 16px;
    }

    .zoom-level {
        display: none;
    }
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 12px;
    height: 12px;
}

::-webkit-scrollbar-track {
    background: var(--background);
}

::-webkit-scrollbar-thumb {
    background: #c0c0c0;
    border-radius: 6px;
}

::-webkit-scrollbar-thumb:hover {
    background: #a0a0a0;
}
