/* ===== CSS Variables for Theming ===== */
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --danger-color: #ef4444;
    --background: #f8fafc;
    --card-background: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --radius-sm: 0.375rem;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --transition: all 0.3s ease;
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --spacing-xl: 2rem;
}

/* ===== Reset & Base Styles ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    padding: clamp(0.5rem, 2vw, 1rem);
}

/* ===== Header ===== */
.header {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    padding: clamp(1rem, 3vw, 2rem);
    border-radius: var(--radius-lg);
    margin-bottom: clamp(1rem, 3vw, 1.5rem);
    box-shadow: var(--shadow-lg);
    text-align: center;
    flex-shrink: 0;
}

.header h1 {
    font-size: clamp(1.75rem, 5vw, 2.5rem);
    font-weight: 700;
    margin-bottom: clamp(0.5rem, 1vw, 0.75rem);
    line-height: 1.2;
    letter-spacing: -0.5px;
}

.header p {
    font-size: clamp(0.9rem, 2.5vw, 1.1rem);
    opacity: 0.95;
    line-height: 1.5;
    font-weight: 400;
    max-width: 600px;
    margin: 0 auto;
}

/* ===== Container & Layout ===== */
.container {
    max-width: 1600px;
    margin: 0 auto;
    width: 100%;
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 2rem);
}

/* ===== Main Content Layout ===== */
.main-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: clamp(1rem, 3vw, 1.5rem);
    flex: 1;
    overflow: hidden;
}

@media (max-width: 1023px) {
    .main-layout {
        grid-template-columns: 1fr;
    }
}

/* ===== Columns ===== */
.left-column,
.right-column {
    display: flex;
    flex-direction: column;
    gap: clamp(0.75rem, 2vw, 1rem);
    overflow-y: auto;
    padding-right: 0.5rem;
}

.left-column::-webkit-scrollbar,
.right-column::-webkit-scrollbar {
    width: 8px;
}

.left-column::-webkit-scrollbar-track,
.right-column::-webkit-scrollbar-track {
    background: var(--background);
}

.left-column::-webkit-scrollbar-thumb,
.right-column::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.left-column::-webkit-scrollbar-thumb:hover,
.right-column::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* ===== Card Component ===== */
.card {
    background: var(--card-background);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: clamp(1rem, 2.5vw, 1.5rem);
    transition: var(--transition);
    flex-shrink: 0;
    border: 1px solid var(--border-color);
}

.card:hover {
    box-shadow: var(--shadow-lg);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: clamp(0.75rem, 2vw, 1rem);
    padding-bottom: clamp(0.5rem, 2vw, 0.75rem);
    border-bottom: 2px solid var(--border-color);
    flex-wrap: wrap;
    gap: clamp(0.5rem, 1vw, 0.75rem);
}

.card-title {
    font-size: clamp(1.1rem, 2.5vw, 1.4rem);
    font-weight: 700;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: clamp(0.375rem, 1vw, 0.5rem);
    line-height: 1.2;
    flex: 1;
    min-width: 150px;
}

.badge {
    display: inline-block;
    padding: 0.35rem 0.85rem;
    border-radius: 9999px;
    font-size: clamp(0.75rem, 1.5vw, 0.85rem);
    font-weight: 700;
    background: var(--primary-color);
    color: white;
    white-space: nowrap;
    box-shadow: var(--shadow-sm);
}

/* ===== Form Controls ===== */
.form-group {
    margin-bottom: clamp(0.75rem, 2vw, 1rem);
}

label {
    display: block;
    font-weight: 600;
    margin-bottom: clamp(0.375rem, 1vw, 0.5rem);
    color: var(--text-primary);
    font-size: clamp(0.85rem, 2vw, 1rem);
}

input[type="file"],
input[type="text"],
textarea,
select {
    width: 100%;
    padding: clamp(0.65rem, 1.5vw, 0.85rem);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: clamp(0.85rem, 2vw, 1rem);
    transition: var(--transition);
    background: white;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

input[type="file"]:focus,
input[type="text"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

textarea {
    resize: vertical;
    min-height: 100px;
    font-family: inherit;
}

#pastedText {
    min-height: 120px;
    font-family: 'Courier New', monospace;
    font-size: clamp(0.8rem, 1.8vw, 0.95rem);
    line-height: 1.6;
}

.character-count {
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
    margin-top: 0.35rem;
    text-align: right;
}

.file-help {
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
    margin-top: 0.35rem;
    line-height: 1.5;
}

/* ===== Checkboxes ===== */
.checkbox-group {
    display: flex;
    align-items: center;
    gap: clamp(0.625rem, 1.5vw, 0.85rem);
    padding: clamp(0.625rem, 1.5vw, 0.85rem);
    background: var(--background);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
}

.checkbox-group:hover {
    background: var(--border-color);
}

.checkbox-group label {
    margin: 0 !important;
    font-size: clamp(0.8rem, 1.8vw, 0.95rem);
    cursor: pointer;
    flex: 1;
}

input[type="checkbox"] {
    width: clamp(1.1rem, 2.5vw, 1.35rem);
    height: clamp(1.1rem, 2.5vw, 1.35rem);
    cursor: pointer;
    accent-color: var(--primary-color);
    flex-shrink: 0;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: clamp(0.35rem, 1vw, 0.6rem);
    padding: clamp(0.75rem, 1.5vw, 0.9rem) clamp(1.1rem, 2vw, 1.4rem);
    font-size: clamp(0.85rem, 1.8vw, 1rem);
    font-weight: 700;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    white-space: nowrap;
    min-height: 44px;
    line-height: 1.2;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-primary:hover:not(:disabled) {
    background: linear-gradient(135deg, var(--primary-hover), #1536b8);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--text-secondary);
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--text-primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-success {
    background: linear-gradient(135deg, var(--success-color), #059669);
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-success:hover:not(:disabled) {
    background: linear-gradient(135deg, #059669, #047857);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-warning {
    background: linear-gradient(135deg, var(--warning-color), #d97706);
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-warning:hover:not(:disabled) {
    background: linear-gradient(135deg, #d97706, #b45309);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

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

.btn-group {
    display: flex;
    flex-wrap: wrap;
    gap: clamp(0.625rem, 1.5vw, 0.85rem);
}

.btn-group .btn {
    flex: 1;
    min-width: 130px;
}

/* ===== File List ===== */
#fileList {
    display: flex;
    flex-direction: column;
    gap: clamp(0.625rem, 1.5vw, 0.85rem);
    max-height: 300px;
    overflow-y: auto;
}

.file-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: clamp(0.625rem, 1.5vw, 0.85rem);
    background: var(--background);
    border-radius: var(--radius-md);
    border: 2px solid var(--border-color);
    gap: clamp(0.625rem, 1vw, 0.85rem);
    flex-wrap: wrap;
}

.file-info {
    flex: 1;
    min-width: 120px;
}

.file-name {
    font-weight: 700;
    color: var(--text-primary);
    word-break: break-word;
    font-size: clamp(0.85rem, 1.8vw, 0.95rem);
    margin-bottom: 0.35rem;
    line-height: 1.2;
}

.file-size {
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
}

.file-status {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.35rem 0.6rem;
    border-radius: 9999px;
    font-size: clamp(0.7rem, 1.5vw, 0.8rem);
    font-weight: 700;
    white-space: nowrap;
}

.status-live {
    background: #dbeafe;
    color: #1e40af;
}

.status-ocr {
    background: #fef3c7;
    color: #92400e;
}

.status-ready {
    background: #d1fae5;
    color: #065f46;
}

.status-error {
    background: #fee2e2;
    color: #991b1b;
}

/* ===== Progress Bar ===== */
.progress-container {
    margin: clamp(0.75rem, 1.5vw, 1rem) 0;
}

.progress-bar {
    width: 100%;
    height: clamp(0.5rem, 1vw, 0.75rem);
    background: var(--border-color);
    border-radius: 9999px;
    overflow: hidden;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.06);
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--success-color));
    border-radius: 9999px;
    transition: width 0.3s ease;
    width: 0%;
}

.progress-text {
    margin-top: 0.35rem;
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
    text-align: center;
    font-weight: 600;
}

/* ===== Crop Tool Styles ===== */
.crop-container {
    position: relative;
    display: block;
    max-width: 100%;
    margin: clamp(0.625rem, 1.5vw, 0.85rem) 0;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--background);
    padding: clamp(0.625rem, 1.5vw, 0.85rem);
    overflow: auto;
    max-height: 420px;
}

.crop-info {
    padding: clamp(0.625rem, 1vw, 0.85rem);
    background: #dbeafe;
    border-left: 4px solid var(--primary-color);
    border-radius: var(--radius-sm);
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: #1e40af;
    margin-bottom: clamp(0.625rem, 1vw, 0.85rem);
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-weight: 600;
}

.crop-preview {
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    max-width: 100%;
    max-height: 280px;
    margin: clamp(0.625rem, 1vw, 0.85rem) 0;
    display: none;
    background: var(--background);
    padding: clamp(0.625rem, 1vw, 0.85rem);
    overflow: auto;
}

.crop-preview img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
}

.crop-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: clamp(0.625rem, 1.5vw, 0.85rem);
    margin: clamp(0.625rem, 1.5vw, 0.85rem) 0;
}

.crop-controls .btn {
    min-width: auto;
    font-size: clamp(0.8rem, 1.6vw, 0.9rem);
}

.crop-page-nav {
    display: flex;
    gap: 0.625rem;
    margin-bottom: clamp(0.625rem, 1vw, 0.85rem);
    justify-content: center;
}

.crop-page-nav button {
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
}

/* ===== Results ===== */
.results-container {
    display: flex;
    flex-direction: column;
    gap: clamp(0.85rem, 2vw, 1.1rem);
    overflow-y: auto;
}

.result-item {
    display: block;
    margin-bottom: 1rem;
    background: var(--card-background);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-lg);
    overflow: hidden;
    flex-shrink: 0;
    box-shadow: var(--shadow-md);
    transition: var(--transition);
}

.result-item:hover {
    box-shadow: var(--shadow-lg);
    border-color: var(--primary-color);
}

.result-header {
    padding: clamp(0.85rem, 1.5vw, 1.1rem);
    background: linear-gradient(135deg, var(--background), var(--border-color));
    border-bottom: 2px solid var(--border-color);
}

.result-title {
    font-weight: 700;
    font-size: clamp(0.95rem, 2vw, 1.1rem);
    margin-bottom: 0.35rem;
    word-break: break-word;
    line-height: 1.3;
    color: var(--text-primary);
}

.result-stats {
    display: flex;
    gap: clamp(0.625rem, 1vw, 0.85rem);
    flex-wrap: wrap;
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
    font-weight: 600;
}

.result-body {
    padding: clamp(0.85rem, 1.5vw, 1.1rem);
    max-height: 550px;
    overflow-y: auto;
}

.extracted-text {
    background: var(--background);
    padding: clamp(0.625rem, 1.5vw, 0.85rem);
    border-radius: var(--radius-md);
    max-height: 280px;
    overflow-y: auto;
    font-family: "Courier New", monospace;
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    margin-bottom: clamp(0.625rem, 1vw, 0.85rem);
    border: 1px solid var(--border-color);
}

.misspelling {
    background-color: #fef2f2;
    color: #991b1b;
    padding: 0.15rem 0.3rem;
    border-radius: 0.25rem;
    font-weight: 700;
    cursor: help;
    text-decoration: underline wavy #ef4444;
}

.error-list {
    display: flex;
    flex-direction: column;
    gap: clamp(0.5rem, 1vw, 0.625rem);
    max-height: 320px;
    overflow-y: auto;
}

.error-item {
    padding: clamp(0.625rem, 1vw, 0.85rem);
    background: #fef2f2;
    border-left: 4px solid var(--danger-color);
    border-radius: var(--radius-md);
}

.error-word {
    font-weight: 700;
    color: var(--danger-color);
    font-size: clamp(0.9rem, 1.8vw, 1rem);
    word-break: break-word;
}

.suggestions {
    margin-top: 0.35rem;
    font-size: clamp(0.75rem, 1.6vw, 0.85rem);
    color: var(--text-secondary);
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.suggestion-item {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: clamp(0.75rem, 1.5vw, 0.85rem);
    white-space: nowrap;
    cursor: pointer;
    transition: var(--transition);
}

.suggestion-item:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

/* ===== Alerts ===== */
.alert {
    padding: clamp(0.625rem, 1.5vw, 0.85rem) clamp(0.85rem, 2vw, 1.1rem);
    border-radius: var(--radius-md);
    margin-bottom: clamp(0.625rem, 1vw, 0.85rem);
    display: flex;
    align-items: flex-start;
    gap: clamp(0.625rem, 1vw, 0.85rem);
    font-size: clamp(0.8rem, 1.6vw, 0.9rem);
    line-height: 1.5;
    font-weight: 600;
}

.alert-info {
    background: #dbeafe;
    color: #1e40af;
    border-left: 4px solid #3b82f6;
}

.alert-success {
    background: #d1fae5;
    color: #065f46;
    border-left: 4px solid #10b981;
}

.alert-warning {
    background: #fef3c7;
    color: #92400e;
    border-left: 4px solid #f59e0b;
}

.alert-error {
    background: #fee2e2;
    color: #991b1b;
    border-left: 4px solid #ef4444;
}

/* ===== Responsive Design ===== */
@media (max-width: 1023px) {
    .container {
        min-height: auto;
    }

    .main-layout {
        grid-template-columns: 1fr;
        overflow: visible;
    }

    .left-column,
    .right-column {
        overflow: visible;
        padding-right: 0;
    }

    .card {
        flex-shrink: 1;
    }

    #fileList {
        max-height: 250px;
    }

    .crop-container {
        max-height: 320px;
    }

    .result-body {
        max-height: auto;
    }

    .extracted-text {
        max-height: 200px;
    }

    .error-list {
        max-height: 250px;
    }

    .crop-controls {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 640px) {
    body {
        padding: 0.5rem;
    }

    .header {
        padding: 1rem 0.75rem;
        margin-bottom: 0.75rem;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .header p {
        font-size: 0.9rem;
    }

    .container {
        min-height: auto;
    }

    .main-layout {
        gap: 1rem;
    }

    .card {
        padding: 0.85rem;
    }

    .card-title {
        font-size: 1rem;
    }

    .btn {
        padding: 0.65rem 0.85rem;
        min-height: 40px;
        font-size: 0.85rem;
    }

    .btn-group {
        flex-direction: column;
    }

    .btn-group .btn {
        width: 100%;
    }

    #fileList {
        max-height: 200px;
    }

    .crop-container {
        max-height: 250px;
    }

    .crop-controls {
        grid-template-columns: 1fr;
    }
}

/* ===== Print Styles ===== */
@media print {
    body {
        background: white;
        padding: 0;
    }

    .no-print {
        display: none !important;
    }

    .card {
        box-shadow: none;
        page-break-inside: avoid;
    }

    .result-item {
        page-break-inside: avoid;
    }
}

/* ===== Focus visible for accessibility ===== */
:focus-visible {
    outline: 3px solid var(--primary-color);
    outline-offset: 2px;
}

button:focus-visible {
    outline-offset: 2px;
}

/* ===== Dark mode ready ===== */
@media (prefers-color-scheme: dark) {
    :root {
        --background: #0f172a;
        --card-background: #1e293b;
        --text-primary: #f1f5f9;
        --text-secondary: #94a3b8;
        --border-color: #334155;
    }
}

/* ===== Search Functionality ===== */
#searchInput {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid var(--border-color, #e0e0e0);
    border-radius: 8px;
    font-size: 1rem;
    transition: all 0.3s ease;
    background: white;
}

#searchInput:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}

.btn-icon {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.25rem;
    font-size: 0.9rem;
    opacity: 0.6;
    transition: opacity 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-icon:hover {
    opacity: 1;
}

/* Highlight matched text */
.search-highlight {
    background-color: #ffeb3b;
    padding: 2px 4px;
    border-radius: 3px;
    font-weight: 600;
    box-shadow: 0 0 0 2px rgba(255, 235, 59, 0.3);
}

/* Hidden result items */
.result-item.hidden {
    display: none !important;
}

/* No results message */
.no-search-results {
    padding: 3rem 2rem;
    text-align: center;
    color: #64748b;
    background: #f8fafc;
    border-radius: 8px;
    margin: 1rem 0;
}

.no-search-results > div:first-child {
    font-size: 3rem;
    margin-bottom: 0.5rem;
    opacity: 0.5;
}

/* Navigation buttons */
.nav-btn {
    background: #f1f5f9;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    font-size: 0.9rem;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
}

.nav-btn:hover {
    background: #e2e8f0;
    border-color: #94a3b8;
}

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

/* Current match highlight */
.search-highlight.current-match {
    background-color: #fb923c;
    box-shadow: 0 0 0 3px rgba(251, 146, 60, 0.3);
}

#pageRangeInput:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

#pageRangeInput::placeholder {
    color: #94a3b8;
}

#pageRangeSection code {
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}