/* PDF Editor - Main Styles */
:root {
    --bg-dark: #1a1a2e;
    --bg-panel: #16213e;
    --bg-surface: #0f3460;
    --accent: #e94560;
    --accent-hover: #ff6b81;
    --text-primary: #eee;
    --text-secondary: #aab;
    --border: #2a2a4a;
    --success: #2ecc71;
    --warning: #f39c12;
    --danger: #e74c3c;
    --sidebar-width: 220px;
    --toolbar-height: 56px;
    --properties-width: 280px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ---- Toolbar ---- */
#toolbar {
    height: var(--toolbar-height);
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 12px;
    gap: 4px;
    flex-shrink: 0;
    z-index: 100;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 0 8px;
    border-right: 1px solid var(--border);
}

.toolbar-group:last-child { border-right: none; }

.toolbar-group label {
    font-size: 10px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-right: 4px;
}

.tool-btn {
    width: 38px;
    height: 38px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.15s;
    position: relative;
}

.tool-btn:hover {
    background: var(--bg-surface);
    border-color: var(--border);
}

.tool-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.tool-btn[title]::after {
    content: attr(title);
    position: absolute;
    bottom: -28px;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: #fff;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 200;
}

.tool-btn:hover[title]::after { opacity: 1; }

.tool-btn-text {
    width: auto;
    padding: 0 12px;
    font-size: 13px;
    gap: 4px;
}

/* ---- Main Layout ---- */
#main-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ---- Sidebar (Page Thumbnails) ---- */
#sidebar {
    width: var(--sidebar-width);
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 8px;
    flex-shrink: 0;
}

.sidebar-header {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 8px 4px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
}

.page-thumb {
    position: relative;
    margin-bottom: 8px;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 6px;
    overflow: hidden;
    transition: border-color 0.15s;
}

.page-thumb:hover { border-color: var(--text-secondary); }
.page-thumb.active { border-color: var(--accent); }

.page-thumb img {
    width: 100%;
    display: block;
    background: #fff;
}

.page-thumb .page-number {
    position: absolute;
    bottom: 4px;
    right: 4px;
    background: rgba(0,0,0,0.7);
    color: #fff;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 11px;
}

.page-thumb .page-actions {
    position: absolute;
    top: 4px;
    right: 4px;
    display: none;
    gap: 2px;
}

.page-thumb:hover .page-actions { display: flex; }

.page-action-btn {
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 4px;
    background: rgba(0,0,0,0.7);
    color: #fff;
    cursor: pointer;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.page-action-btn:hover { background: var(--accent); }

/* ---- Viewer Area ---- */
#viewer-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

#viewer-scroll {
    flex: 1;
    overflow: auto;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 20px;
    background: #2a2a3a;
}

#page-container {
    position: relative;
    display: inline-block;
    box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

#page-canvas {
    display: block;
    background: white;
}

#annotation-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#annotation-layer.active { pointer-events: auto; cursor: crosshair; }
#annotation-layer.text-mode { cursor: text; }

.annotation-overlay {
    position: absolute;
    pointer-events: auto;
}

.annotation-overlay.highlight-overlay {
    cursor: move;
}

.annotation-overlay.text-overlay {
    cursor: move;
    padding: 2px 4px;
    border: 1px dashed transparent;
    min-width: 20px;
}

.annotation-overlay.text-overlay:hover {
    border-color: var(--accent);
}

.annotation-overlay.comment-overlay {
    width: 24px;
    height: 24px;
    background: #FFD700;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.annotation-overlay.stamp-overlay {
    cursor: move;
    transform: rotate(30deg);
    font-weight: bold;
    opacity: 0.6;
    border: 3px solid;
    padding: 4px 12px;
    border-radius: 4px;
}

.comment-popup {
    position: absolute;
    background: #FFF9C4;
    color: #333;
    padding: 10px;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    min-width: 180px;
    max-width: 250px;
    z-index: 50;
    font-size: 13px;
}

.comment-popup .comment-author {
    font-weight: 600;
    font-size: 11px;
    color: #666;
    margin-bottom: 4px;
}

/* ---- Text Block Editor (Edit Existing Text) ---- */
.text-block-overlay {
    position: absolute;
    pointer-events: auto;
    cursor: pointer;
    border: 1px dashed rgba(0, 120, 255, 0.4);
    background: rgba(0, 120, 255, 0.05);
    border-radius: 2px;
    padding: 1px 3px;
    color: transparent; /* Hide text, it's below on the PDF */
    overflow: hidden;
    white-space: nowrap;
    transition: all 0.15s;
    z-index: 30;
}

.text-block-overlay:hover {
    border-color: rgba(0, 120, 255, 0.8);
    background: rgba(0, 120, 255, 0.12);
    color: rgba(0, 0, 0, 0.3);
}

.text-block-overlay.edited {
    border-color: rgba(233, 69, 96, 0.7);
    background: rgba(233, 69, 96, 0.1);
    color: rgba(233, 69, 96, 0.6);
}

.text-block-editor {
    position: absolute;
    z-index: 60;
}

.text-block-input {
    background: rgba(255, 255, 255, 0.97);
    border: 2px solid var(--accent);
    border-radius: 3px;
    padding: 2px 4px;
    font-family: Helvetica, Arial, sans-serif;
    color: #000;
    outline: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.text-block-input:focus {
    border-color: #0078ff;
}

/* ---- Properties Panel ---- */
#properties-panel {
    width: var(--properties-width);
    background: var(--bg-panel);
    border-left: 1px solid var(--border);
    padding: 16px;
    overflow-y: auto;
    flex-shrink: 0;
    display: none;
}

#properties-panel.visible { display: block; }

.prop-section {
    margin-bottom: 16px;
}

.prop-section h3 {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
}

.prop-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.prop-row label {
    font-size: 12px;
    color: var(--text-secondary);
    width: 70px;
    flex-shrink: 0;
}

.prop-input {
    flex: 1;
    background: var(--bg-dark);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-primary);
    padding: 6px 8px;
    font-size: 13px;
}

.prop-input:focus {
    outline: none;
    border-color: var(--accent);
}

select.prop-input {
    cursor: pointer;
}

/* ---- Status Bar ---- */
#status-bar {
    height: 28px;
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 12px;
    font-size: 12px;
    color: var(--text-secondary);
    gap: 16px;
    flex-shrink: 0;
}

.status-item { display: flex; align-items: center; gap: 4px; }

/* ---- Welcome Screen ---- */
#welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    text-align: center;
}

#welcome-screen h1 {
    font-size: 28px;
    margin-bottom: 12px;
    font-weight: 300;
}

#welcome-screen p {
    color: var(--text-secondary);
    margin-bottom: 24px;
    max-width: 400px;
}

#drop-zone {
    width: 320px;
    height: 200px;
    border: 2px dashed var(--border);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    cursor: pointer;
    transition: all 0.2s;
}

#drop-zone:hover, #drop-zone.drag-over {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.05);
}

#drop-zone .drop-icon { font-size: 48px; opacity: 0.5; }
#drop-zone .drop-text { color: var(--text-secondary); font-size: 14px; }

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 10px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.15s;
}

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

/* ---- Zoom Controls ---- */
#zoom-controls {
    position: absolute;
    bottom: 16px;
    right: 16px;
    display: flex;
    gap: 4px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px;
    z-index: 50;
}

#zoom-controls button {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#zoom-controls button:hover { background: var(--bg-surface); }

#zoom-level {
    width: 50px;
    text-align: center;
    font-size: 12px;
    line-height: 32px;
    color: var(--text-secondary);
}

/* ---- Nav Controls ---- */
#nav-controls {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px 12px;
    z-index: 50;
}

#nav-controls button {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 16px;
}

#nav-controls button:hover { background: var(--bg-surface); }

#page-indicator {
    font-size: 13px;
    color: var(--text-secondary);
    min-width: 80px;
    text-align: center;
}

/* ---- Modals ---- */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px;
    min-width: 360px;
    max-width: 500px;
}

.modal h2 {
    margin-bottom: 16px;
    font-size: 18px;
}

.modal-buttons {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
}

.btn-secondary {
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}

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

/* ---- Text Input Overlay ---- */
.text-input-overlay {
    position: absolute;
    z-index: 60;
}

.text-input-overlay textarea {
    background: rgba(255,255,255,0.95);
    border: 2px solid var(--accent);
    border-radius: 4px;
    padding: 4px 6px;
    font-family: Helvetica, Arial, sans-serif;
    color: #000;
    resize: both;
    outline: none;
    min-width: 100px;
    min-height: 28px;
}

/* ---- Loading Spinner ---- */
.spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid var(--text-secondary);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-dark); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ---- Dragging state ---- */
.page-thumb.dragging { opacity: 0.5; }
.page-thumb.drag-target { border-color: var(--accent); border-style: dashed; }

/* ---- Save As Dialog ---- */
.dialog-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.dialog-box {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px 32px;
    min-width: 400px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.dialog-box h3 {
    margin-bottom: 16px;
    font-size: 18px;
    color: var(--text-primary);
}

.dialog-field {
    margin-bottom: 20px;
}

.dialog-field label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--text-secondary);
}

.dialog-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--bg-dark);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
}

.dialog-input:focus {
    border-color: var(--accent);
}

.dialog-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* ---- OCR Prompt ---- */
#ocr-prompt {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    background: var(--bg-panel);
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 12px 20px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from { opacity: 0; transform: translateX(-50%) translateY(-20px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.ocr-prompt-content {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
    color: var(--text-primary);
    white-space: nowrap;
}

.ocr-prompt-content .btn-primary {
    padding: 6px 16px;
    font-size: 13px;
}

.ocr-prompt-content .btn-secondary {
    padding: 6px 12px;
    font-size: 13px;
}

/* ---- OCR Overlays ---- */
.ocr-line-overlay {
    position: absolute;
    border: 1px solid rgba(0, 180, 0, 0.5);
    background: rgba(0, 180, 0, 0.08);
    pointer-events: none;
    z-index: 35;
    border-radius: 2px;
}

.ocr-dialog {
    min-width: 500px;
    max-width: 700px;
}

.ocr-stats {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.ocr-text-output {
    width: 100%;
    height: 300px;
    padding: 12px;
    background: var(--bg-dark);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 13px;
    font-family: 'Courier New', monospace;
    line-height: 1.5;
    resize: vertical;
    outline: none;
    margin-bottom: 16px;
}

.ocr-text-output:focus {
    border-color: var(--accent);
}

/* ═══════════════ Form Field Inserter ═══════════════ */

.form-insert-dialog {
    max-width: 700px;
    width: 90vw;
}

.form-insert-desc {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
    line-height: 1.5;
}

.form-insert-status {
    font-size: 13px;
    color: var(--accent);
    margin-bottom: 12px;
    padding: 8px 12px;
    background: rgba(233, 69, 96, 0.1);
    border-radius: 6px;
}

.form-insert-list {
    max-height: 320px;
    overflow-y: auto;
    margin-bottom: 16px;
}

.form-insert-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.form-insert-table th {
    text-align: left;
    padding: 8px 10px;
    background: var(--bg-surface);
    border-bottom: 2px solid var(--border);
    color: var(--text-secondary);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 1;
}

.form-insert-table td {
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
}

.form-field-row {
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
}

.form-field-row:hover {
    background: rgba(233, 69, 96, 0.1);
}

.form-field-row.deselected {
    opacity: 0.35;
    text-decoration: line-through;
}

.form-field-row code {
    font-size: 12px;
    background: var(--bg-dark);
    padding: 2px 6px;
    border-radius: 3px;
    color: var(--accent-hover);
}

/* Preview overlays on the page */
.form-field-preview {
    position: absolute;
    border: 2px dashed rgba(0, 180, 255, 0.7);
    background: rgba(0, 180, 255, 0.12);
    border-radius: 3px;
    cursor: pointer;
    transition: opacity 0.2s, border-color 0.2s;
    z-index: 50;
    display: flex;
    align-items: center;
    overflow: hidden;
}

.form-field-preview.confidence-high {
    border-color: rgba(46, 204, 113, 0.7);
    background: rgba(46, 204, 113, 0.12);
}

.form-field-preview.confidence-medium {
    border-color: rgba(243, 156, 18, 0.7);
    background: rgba(243, 156, 18, 0.12);
}

.form-field-preview.deselected {
    opacity: 0.2;
    border-style: dotted;
}

.form-field-preview:hover {
    border-width: 3px;
}

.field-preview-label {
    font-size: 9px;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    padding: 1px 4px;
    border-radius: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

/* Hidden file input */
.hidden { display: none; }
