/* ==========================================================================
   Design System & Dark Theme CSS
   ========================================================================== */

:root {
    --bg-base: #0B0F19;
    --bg-surface: #111827;
    --bg-card: #1F2937;
    --bg-glass: rgba(31, 41, 55, 0.7);
    --border-color: rgba(255, 255, 255, 0.1);
    --border-active: rgba(99, 102, 241, 0.5);

    --primary: #6366F1;
    --primary-hover: #4F46E5;
    --primary-glow: rgba(99, 102, 241, 0.3);

    --accent-emerald: #10B981;
    --accent-amber: #F59E0B;
    --accent-rose: #EF4444;
    --accent-cyan: #06B6D4;

    --text-main: #F9FAFB;
    --text-muted: #9CA3AF;
    --text-dim: #6B7280;

    --font-sans: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-heading: 'Space Grotesk', sans-serif;

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;

    --shadow-glow: 0 0 20px rgba(99, 102, 241, 0.25);
    --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.4);
}

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

body {
    font-family: var(--font-sans);
    background-color: var(--bg-base);
    color: var(--text-main);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

.hidden {
    display: none !important;
}

/* ==========================================================================
   Layout Containers
   ========================================================================== */

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

.app-header {
    height: 64px;
    background: rgba(17, 24, 39, 0.85);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    z-index: 50;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-icon {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #fff;
    box-shadow: var(--shadow-glow);
}

.brand-text h1 {
    font-family: var(--font-heading);
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.badge {
    font-size: 11px;
    padding: 2px 8px;
    background: rgba(99, 102, 241, 0.15);
    color: var(--primary);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 20px;
    font-weight: 500;
}

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

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.2s ease;
}

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

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

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
    box-shadow: var(--shadow-glow);
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-main);
    border-color: var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.08);
}

.btn-success {
    background: var(--accent-emerald);
    color: #fff;
}

.btn-success:hover:not(:disabled) {
    background: #059669;
}

.btn-danger {
    background: var(--accent-rose);
    color: #fff;
}

.btn-danger:hover:not(:disabled) {
    background: #DC2626;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 12px;
}

/* Workspace */
.workspace {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
}

/* Welcome / Empty State */
.welcome-screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at 50% 30%, rgba(99, 102, 241, 0.1), transparent 60%);
    padding: 24px;
}

.welcome-card {
    max-width: 560px;
    background: var(--bg-glass);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 40px;
    text-align: center;
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(16px);
}

.welcome-icon {
    font-size: 48px;
    color: var(--primary);
    margin-bottom: 16px;
}

.welcome-card h2 {
    font-family: var(--font-heading);
    font-size: 24px;
    margin-bottom: 12px;
}

.welcome-card p {
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 24px;
}

.upload-dropzone {
    border: 2px dashed var(--border-active);
    background: rgba(99, 102, 241, 0.05);
    border-radius: var(--radius-md);
    padding: 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.upload-dropzone:hover {
    background: rgba(99, 102, 241, 0.12);
    border-color: var(--primary);
}

.upload-dropzone i {
    font-size: 32px;
    color: var(--primary);
}

.feature-highlights {
    display: flex;
    justify-content: space-around;
    margin-top: 30px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
    gap: 12px;
}

.feature-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--text-muted);
}

.feature-item i {
    color: var(--accent-cyan);
}

/* Editor Layout */
.editor-layout {
    display: flex;
    width: 100%;
    height: 100%;
}

/* Left Sidebar (Pages) */
.sidebar-pages {
    width: 240px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 8px;
}

.page-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.page-card {
    background: var(--bg-card);
    border: 2px solid transparent;
    border-radius: var(--radius-md);
    padding: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.page-card:hover {
    border-color: rgba(255, 255, 255, 0.2);
}

.page-card.active {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.1);
}

.page-thumb-container {
    width: 100%;
    height: 140px;
    background: #000;
    border-radius: var(--radius-sm);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.page-thumb-container img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.page-card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
}

.page-number-tag {
    font-weight: 600;
}

.page-fields-badge {
    font-size: 10px;
    padding: 2px 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.btn-icon {
    border: none;
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-secondary, #aaa);
    width: 24px;
    height: 24px;
    border-radius: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.btn-icon:hover {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
}

.btn-restore-page:hover {
    background: rgba(34, 197, 94, 0.2);
    color: #22c55e;
}

.page-card.page-deleted {
    opacity: 0.55;
}

.page-deleted-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-weight: 600;
    font-size: 13px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

/* Center Canvas Viewport */
.canvas-viewport {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #07090E;
    position: relative;
}

.viewport-toolbar {
    height: 48px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
}

.toolbar-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.active-page-title {
    font-weight: 700;
    font-size: 15px;
}

.fields-count-tag {
    font-size: 12px;
    color: var(--accent-emerald);
    background: rgba(16, 185, 129, 0.1);
    padding: 2px 8px;
    border-radius: 12px;
}

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

.btn-tool {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    height: 32px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
    padding: 0 10px;
    font-size: 12px;
}

.btn-tool:hover {
    background: rgba(255, 255, 255, 0.1);
}

.btn-tool-accent {
    background: var(--primary);
    border-color: var(--primary);
    font-weight: 600;
}

.btn-tool-success {
    background: var(--accent-emerald);
    border-color: var(--accent-emerald);
    font-weight: 600;
}

.zoom-text {
    font-size: 12px;
    color: var(--text-muted);
    min-width: 45px;
    text-align: center;
}

.canvas-scroll-container {
    flex: 1;
    overflow: auto;
    padding: 30px;
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

.pdf-canvas-wrapper {
    position: relative;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
    border-radius: 4px;
    background: #fff;
    user-select: none;
}

.pdf-canvas-wrapper img {
    display: block;
    max-width: 100%;
    pointer-events: none;
}

.fields-overlay-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* Text Box Overlay Element (Draggable & Resizable) */
.field-box {
    position: absolute;
    border: 2px dashed var(--primary);
    background: rgba(99, 102, 241, 0.2);
    border-radius: 3px;
    cursor: move;
    transition: border-color 0.15s, background 0.15s;
}

.field-box:hover {
    background: rgba(99, 102, 241, 0.35);
    border-color: var(--accent-cyan);
}

.field-box.selected {
    border: 2px solid var(--accent-emerald);
    background: rgba(16, 185, 129, 0.3);
    box-shadow: 0 0 12px rgba(16, 185, 129, 0.5);
    z-index: 10;
}

.field-tag {
    position: absolute;
    top: -20px;
    left: -2px;
    background: var(--primary);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 3px 3px 0 0;
    white-space: nowrap;
    pointer-events: none;
}

.field-box.selected .field-tag {
    background: var(--accent-emerald);
}

/* Resize Handle (Bottom-Right corner) */
.resize-handle {
    position: absolute;
    right: -4px;
    bottom: -4px;
    width: 10px;
    height: 10px;
    background: var(--accent-emerald);
    border: 1px solid #fff;
    border-radius: 2px;
    cursor: se-resize;
    display: none;
}

.field-box.selected .resize-handle {
    display: block;
}

/* Existing (static) PDF text overlay + editor */
.text-spans-overlay-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.text-span-box {
    position: absolute;
    border: 1px dashed rgba(56, 189, 248, 0.6);
    background: rgba(56, 189, 248, 0.08);
    border-radius: 2px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.text-span-box:hover {
    background: rgba(56, 189, 248, 0.25);
    border-color: var(--accent-cyan);
}

.text-span-box.selected {
    border: 2px solid var(--accent-emerald);
    background: rgba(16, 185, 129, 0.25);
    z-index: 10;
}

.text-edit-panel {
    position: absolute;
    right: 16px;
    bottom: 16px;
    width: 300px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 14px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    z-index: 20;
}

.text-edit-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.text-edit-panel-header h4 {
    font-size: 13px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.text-edit-original {
    font-size: 12px;
    color: var(--text-dim);
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
    padding: 6px 8px;
    max-height: 60px;
    overflow-y: auto;
    word-break: break-word;
}

.pdf-canvas-wrapper.add-text-active {
    cursor: crosshair;
}

.move-marker {
    position: absolute;
    z-index: 15;
    display: flex;
    align-items: center;
    gap: 6px;
}

.move-marker-dot {
    width: 14px;
    height: 14px;
    margin-left: -7px;
    margin-top: -7px;
    border-radius: 50%;
    background: var(--accent-emerald);
    border: 2px solid #fff;
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.7);
    cursor: grab;
    flex-shrink: 0;
}

.move-marker-dot:active {
    cursor: grabbing;
}

.move-marker.dragging .move-marker-dot {
    box-shadow: 0 0 12px rgba(16, 185, 129, 1);
}

.move-marker-label {
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    font-size: 12px;
    padding: 3px 8px;
    border-radius: 4px;
    white-space: nowrap;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}

.edit-target-marker .move-marker-dot {
    background: var(--accent-cyan, #38bdf8);
}

/* Right Sidebar (Inspector) */
.sidebar-inspector {
    width: 300px;
    background: var(--bg-surface);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.inspector-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.fields-tree-header {
    display: grid;
    grid-template-columns: 50px 1fr 40px;
    padding: 10px 16px;
    background: var(--bg-card);
    font-size: 11px;
    font-weight: 700;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-color);
}

.fields-index-list {
    flex: 1;
    overflow-y: auto;
}

.field-row {
    display: grid;
    grid-template-columns: 50px 1fr 40px;
    padding: 10px 16px;
    font-size: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    align-items: center;
    transition: background 0.15s;
}

.field-row:hover {
    background: rgba(255, 255, 255, 0.05);
}

.field-row.active {
    background: rgba(99, 102, 241, 0.15);
    color: var(--accent-cyan);
}

.field-idx {
    font-weight: 700;
    color: var(--primary);
}

.field-name-cell {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.field-page-cell {
    color: var(--text-dim);
    text-align: right;
}

/* Property Editor Panel */
.field-properties-panel {
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    padding: 16px;
}

.field-properties-panel h4 {
    font-size: 13px;
    margin-bottom: 12px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.prop-group {
    margin-bottom: 10px;
}

.prop-group label {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.form-input, .form-select {
    width: 100%;
    padding: 8px 10px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-main);
    font-family: var(--font-sans);
    font-size: 12px;
}

.form-input:focus, .form-select:focus {
    border-color: var(--primary);
    outline: none;
}

.prop-row {
    display: flex;
    gap: 8px;
}

/* Modals */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-card {
    width: 520px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    overflow: hidden;
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h2 {
    font-size: 16px;
    font-family: var(--font-heading);
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
}

.modal-body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.step-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.replacement-dropzone {
    border: 2px dashed var(--border-active);
    background: rgba(99, 102, 241, 0.05);
    border-radius: var(--radius-md);
    padding: 20px;
    text-align: center;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    font-size: 13px;
}

.strategy-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.radio-card {
    display: flex;
    gap: 12px;
    padding: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
}

.radio-card input {
    margin-top: 4px;
}

.radio-content strong {
    display: block;
    font-size: 13px;
    margin-bottom: 2px;
}

.radio-content p {
    font-size: 11px;
    color: var(--text-muted);
}

.modal-footer {
    padding: 16px 20px;
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toast {
    padding: 12px 18px;
    background: var(--bg-card);
    border-left: 4px solid var(--primary);
    border-radius: var(--radius-sm);
    color: var(--text-main);
    font-size: 13px;
    box-shadow: var(--shadow-card);
    animation: fadeIn 0.3s ease;
}

.toast-success {
    border-left-color: var(--accent-emerald);
}

.toast-error {
    border-left-color: var(--accent-rose);
}

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