/* Document AI - Styles */

/* Upload Zone */
.upload-zone {
    border: 3px dashed #dee2e6;
    border-radius: 12px;
    padding: 4rem 2rem;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    background: #f8f9fa;
}

.upload-zone:hover,
.upload-zone.dragover {
    border-color: #0d6efd;
    background: #e8f0fe;
}

.upload-zone .upload-icon {
    font-size: 4rem;
    color: #6c757d;
    display: block;
    margin-bottom: 1rem;
}

/* PDF Viewer */
.pdf-viewer-container {
    max-height: 70vh;
    overflow: auto;
    background: #e9ecef;
    position: relative;
}

.pdf-canvas-wrapper {
    position: relative;
    display: inline-block;
    margin: auto;
}

.pdf-canvas-wrapper canvas {
    display: block;
}

.overlay-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.field-overlay {
    position: absolute;
    border: 2px solid;
    background: rgba(74, 144, 217, 0.15);
    border-color: #4A90D9;
    cursor: pointer;
    pointer-events: auto;
    transition: background-color 0.2s;
}

.field-overlay:hover,
.field-overlay.highlighted {
    background: rgba(74, 144, 217, 0.35);
    z-index: 100 !important;
    border-width: 3px;
    box-shadow: 0 0 8px rgba(74, 144, 217, 0.6);
}

.field-overlay.highlighted .field-overlay-label {
    font-size: 11px;
    padding: 2px 6px;
    z-index: 101;
}

.field-overlay.selected {
    background: rgba(74, 144, 217, 0.3);
    box-shadow: 0 0 0 2px rgba(74, 144, 217, 0.5);
}

.field-overlay.excluded {
    opacity: 0.3;
    border-style: dashed;
}

/* #399: drag-to-reposition affordances */
.field-overlay-draggable {
    cursor: move;
}

.field-overlay-dragging {
    opacity: 0.85;
    z-index: 200 !important;
    box-shadow: 0 0 12px rgba(0, 0, 0, 0.45);
}

.field-overlay-movepage {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 18px;
    height: 18px;
    padding: 0;
    line-height: 1;
    font-size: 9px;
    border: none;
    border-radius: 50%;
    background: #333;
    color: #fff;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
}

.field-overlay:hover .field-overlay-movepage,
.field-overlay.highlighted .field-overlay-movepage {
    display: inline-flex;
}

/* #420: resize handles on field overlays (builder mode) */
.field-resize-handle {
    position: absolute;
    width: 9px;
    height: 9px;
    background: #fff;
    border: 1.5px solid #333;
    border-radius: 2px;
    box-sizing: border-box;
    z-index: 150;
    display: none;
}

.field-overlay:hover .field-resize-handle,
.field-overlay.highlighted .field-resize-handle {
    display: block;
}

.field-resize-handle.rh-nw { top: -5px;    left: -5px;    cursor: nwse-resize; }
.field-resize-handle.rh-n  { top: -5px;    left: 50%;     margin-left: -4.5px; cursor: ns-resize; }
.field-resize-handle.rh-ne { top: -5px;    right: -5px;   cursor: nesw-resize; }
.field-resize-handle.rh-e  { top: 50%;     right: -5px;   margin-top: -4.5px;  cursor: ew-resize; }
.field-resize-handle.rh-se { bottom: -5px; right: -5px;   cursor: nwse-resize; }
.field-resize-handle.rh-s  { bottom: -5px; left: 50%;     margin-left: -4.5px; cursor: ns-resize; }
.field-resize-handle.rh-sw { bottom: -5px; left: -5px;    cursor: nesw-resize; }
.field-resize-handle.rh-w  { top: 50%;     left: -5px;    margin-top: -4.5px;  cursor: ew-resize; }

/* While actively resizing, keep handles visible even off-hover */
.field-overlay.field-overlay-resizing .field-resize-handle { display: block; }

.field-overlay-label {
    position: absolute;
    top: -18px;
    left: 0;
    font-size: 9px;
    font-weight: 600;
    padding: 1px 4px;
    background: #4A90D9;
    color: white;
    border-radius: 2px;
    white-space: nowrap;
    pointer-events: none;
}

.table-overlay {
    position: absolute;
    border: 2px solid #17a2b8;
    background: rgba(23, 162, 184, 0.1);
    pointer-events: auto;
    cursor: pointer;
}

.table-overlay:hover {
    background: rgba(23, 162, 184, 0.25);
}

/* Mode Tabs */
#modeTabs .nav-link {
    color: #6c757d;
    border-radius: 8px;
    margin-right: 0.5rem;
    padding: 0.5rem 1rem;
}

#modeTabs .nav-link.active {
    background: #0d6efd;
    color: white;
}

#modeTabs .nav-link:hover:not(.active) {
    background: #e9ecef;
}

/* Fields List */
.fields-list {
    max-height: 400px;
    overflow-y: auto;
}

.field-item {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    transition: background-color 0.15s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.field-item:hover {
    background: #f8f9fa;
}

.field-item.highlighted {
    background: #d6e8ff;
    border-left: 3px solid #0d6efd;
}

.field-item.active {
    background: #e8f0fe;
}

.field-item-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    flex-shrink: 0;
}

.field-item-content {
    flex: 1;
    min-width: 0;
}

.field-item-label {
    font-weight: 600;
    font-size: 0.85rem;
}

.field-item-value {
    font-size: 0.75rem;
    color: #6c757d;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.field-item-confidence {
    font-size: 0.7rem;
    flex-shrink: 0;
}

.confidence-high { color: #198754; }
.confidence-medium { color: #ffc107; }
.confidence-low { color: #dc3545; }

/* Builder Field Editor */
.builder-field-item {
    padding: 0.75rem;
    border-bottom: 1px solid #f0f0f0;
}

.builder-field-item .form-control-sm,
.builder-field-item .form-select-sm {
    font-size: 0.8rem;
}

.builder-field-actions {
    display: flex;
    gap: 0.25rem;
    margin-top: 0.25rem;
}

/* Tables List */
.tables-list {
    max-height: 300px;
    overflow-y: auto;
}

.table-item {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #f0f0f0;
}

.table-item-header {
    font-weight: 600;
    font-size: 0.85rem;
    margin-bottom: 0.25rem;
}

.table-preview {
    font-size: 0.75rem;
    max-height: 100px;
    overflow: auto;
}

.table-preview table {
    width: 100%;
}

.table-preview th, .table-preview td {
    padding: 2px 4px;
    border: 1px solid #dee2e6;
    font-size: 0.7rem;
}

.table-preview th {
    background: #f8f9fa;
    font-weight: 600;
}

/* Extraction Results */
.extraction-results {
    max-height: 400px;
    overflow-y: auto;
}

.extraction-field {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.extraction-field-label {
    font-weight: 600;
    font-size: 0.85rem;
    color: #495057;
}

.extraction-field-value {
    font-size: 0.85rem;
    text-align: right;
    max-width: 60%;
    word-break: break-word;
}

.extraction-field-empty {
    color: #adb5bd;
    font-style: italic;
}

/* Process Template Select */
#processRedactFields .form-check {
    margin-bottom: 0.25rem;
}

#processRedactFields .form-check-label {
    font-size: 0.85rem;
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}

.loading-content {
    text-align: center;
}

/* Color palette for field overlays */
.field-color-0 { border-color: #4A90D9; background-color: rgba(74, 144, 217, 0.15); }
.field-color-1 { border-color: #E74C3C; background-color: rgba(231, 76, 60, 0.15); }
.field-color-2 { border-color: #2ECC71; background-color: rgba(46, 204, 113, 0.15); }
.field-color-3 { border-color: #F39C12; background-color: rgba(243, 156, 18, 0.15); }
.field-color-4 { border-color: #9B59B6; background-color: rgba(155, 89, 182, 0.15); }
.field-color-5 { border-color: #1ABC9C; background-color: rgba(26, 188, 156, 0.15); }
.field-color-6 { border-color: #E67E22; background-color: rgba(230, 126, 34, 0.15); }
.field-color-7 { border-color: #3498DB; background-color: rgba(52, 152, 219, 0.15); }
.field-color-8 { border-color: #E91E63; background-color: rgba(233, 30, 99, 0.15); }
.field-color-9 { border-color: #00BCD4; background-color: rgba(0, 188, 212, 0.15); }

/* Custom field drawing */
.overlay-container.drawing-mode {
    cursor: crosshair;
    pointer-events: auto;
}

.draw-preview {
    position: absolute;
    border: 2px dashed #0d6efd;
    background: rgba(13, 110, 253, 0.1);
    pointer-events: none;
    z-index: 200;
}

.custom-field-form {
    position: absolute;
    z-index: 300;
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    padding: 0.75rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    width: 220px;
    pointer-events: auto;
}

.custom-field-form input,
.custom-field-form select {
    font-size: 0.8rem;
    margin-bottom: 0.4rem;
}

/* Builder field highlighted */
.builder-field-item.highlighted {
    background: #d6e8ff;
    border-left: 3px solid #0d6efd;
}

/* Draw mode button active state */
.btn-draw-active {
    background-color: #0d6efd !important;
    color: white !important;
    border-color: #0d6efd !important;
}

/* Field type badge */
.field-type-badge {
    font-size: 0.65rem;
    padding: 1px 4px;
    border-radius: 3px;
    background: #e9ecef;
    color: #495057;
    margin-left: 4px;
}

/* Address components sub-display */
.address-components {
    font-size: 0.7rem;
    color: #6c757d;
    padding-left: 1rem;
    margin-top: 2px;
}

.address-components span {
    display: inline-block;
    margin-right: 6px;
}

/* Per-field redaction dropdown */
.field-redact-select {
    font-size: 0.7rem;
    padding: 1px 4px;
    max-width: 120px;
    height: auto;
    line-height: 1.3;
}

/* Template JSON preview */
.template-json-preview {
    font-size: 0.75rem;
    max-height: 300px;
    overflow: auto;
    padding: 0.75rem;
    background: #1e1e1e;
    color: #d4d4d4;
    border-radius: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}

/* Process field list items with redact dropdown */
.process-field-item {
    padding: 0.35rem 0.75rem;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
}

.process-field-item .field-label {
    font-size: 0.8rem;
    font-weight: 500;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* #400: AI Field Assistant — floating chat panel */
.docai-ai-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #4A90D9, #9B59B6);
    color: #fff;
    font-size: 1.4rem;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    z-index: 1050;
    display: flex;
    align-items: center;
    justify-content: center;
}
.docai-ai-fab:hover { transform: scale(1.05); }

.docai-ai-panel {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 340px;
    max-width: calc(100vw - 48px);
    height: 460px;
    max-height: calc(100vh - 140px);
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
    z-index: 1050;
    flex-direction: column;
    overflow: hidden;
}

.docai-ai-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: linear-gradient(135deg, #4A90D9, #9B59B6);
    color: #fff;
    font-weight: 600;
}
.docai-ai-close {
    background: none;
    border: none;
    color: #fff;
    font-size: 1.3rem;
    line-height: 1;
    cursor: pointer;
}

.docai-ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    background: #f8f9fa;
}

.docai-ai-msg { display: flex; margin-bottom: 8px; }
.docai-ai-msg.user { justify-content: flex-end; }
.docai-ai-bubble {
    max-width: 85%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    line-height: 1.35;
    white-space: pre-wrap;
    word-break: break-word;
}
.docai-ai-msg.assistant .docai-ai-bubble { background: #fff; border: 1px solid #e3e6ea; color: #212529; }
.docai-ai-msg.user .docai-ai-bubble { background: #4A90D9; color: #fff; }

.docai-ai-input {
    display: flex;
    gap: 6px;
    padding: 10px;
    border-top: 1px solid #dee2e6;
    background: #fff;
}
.docai-ai-input input {
    flex: 1;
    border: 1px solid #ced4da;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.85rem;
}
.docai-ai-input button {
    border: none;
    border-radius: 8px;
    background: #4A90D9;
    color: #fff;
    padding: 0 12px;
    cursor: pointer;
}
.docai-ai-input button:disabled { opacity: 0.5; cursor: default; }
