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

:root {
    --color-bg: #f5f7fa;
    --color-surface: #ffffff;
    --color-border: #e1e5eb;
    --color-text: #2d3748;
    --color-text-muted: #718096;
    --color-primary: #3b82f6;
    --color-primary-hover: #2563eb;
    --color-primary-light: #dbeafe;
    --color-success: #10b981;
    --color-success-light: #d1fae5;
    --color-error: #ef4444;
    --color-error-light: #fee2e2;
    --color-warning: #f59e0b;
    --color-warning-light: #fef3c7;
    --color-info: #6366f1;
    --color-info-light: #e0e7ff;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.1);
    --transition: 150ms ease;
    --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    min-height: 100vh;
}

/* ===== Layout ===== */
.app-container {
    max-width: 960px;
    margin: 0 auto;
    padding: 24px 16px 64px;
}

.app-header {
    text-align: center;
    margin-bottom: 32px;
}

.app-header h1 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.app-header .subtitle {
    color: var(--color-text-muted);
    font-size: 1.05rem;
}

/* ===== Cards ===== */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-desc {
    color: var(--color-text-muted);
    font-size: 0.92rem;
    margin-bottom: 16px;
}

/* ===== Badges ===== */
.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    font-size: 0.8rem;
    font-weight: 700;
    color: #fff;
    background: #a0aec0;
    flex-shrink: 0;
    transition: background var(--transition);
}

.badge.pending { background: #a0aec0; }
.badge.running { background: var(--color-primary); animation: pulse 1.5s infinite; }
.badge.done { background: var(--color-success); }
.badge.error { background: var(--color-error); }

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    line-height: 1.4;
}

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

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

.btn-secondary {
    background: var(--color-surface);
    color: var(--color-text);
    border-color: var(--color-border);
}
.btn-secondary:hover:not(:disabled) {
    background: var(--color-bg);
}

.btn-danger {
    background: var(--color-error);
    color: #fff;
}
.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

.btn-lg {
    padding: 12px 28px;
    font-size: 1rem;
    font-weight: 600;
}

.btn-sm {
    padding: 5px 10px;
    font-size: 0.82rem;
}

.button-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

/* ===== Model Selectors ===== */
.model-selectors {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
}

.model-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--color-text-muted);
}

.model-group select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    background: var(--color-surface);
    color: var(--color-text);
}

/* ===== Upload Area ===== */
.upload-area {
    border: 2px dashed var(--color-border);
    border-radius: var(--radius-lg);
    padding: 40px 24px;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
}

.upload-area:hover,
.upload-area.drag-over {
    border-color: var(--color-primary);
    background: var(--color-primary-light);
}

.upload-area .upload-icon {
    font-size: 2.5rem;
    margin-bottom: 8px;
}

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

.upload-area input[type="file"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

/* ===== File List ===== */
.file-list-container {
    margin-top: 16px;
}

.file-list-container h3 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 8px;
}

.file-list {
    list-style: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    overflow: hidden;
}

.file-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    font-size: 0.88rem;
    border-bottom: 1px solid var(--color-border);
}

.file-list li:last-child {
    border-bottom: none;
}

.file-list li .file-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.file-list li .file-icon {
    font-size: 1.1rem;
}

.file-list li .file-name {
    font-weight: 500;
}

.file-list li .file-size {
    color: var(--color-text-muted);
    font-size: 0.8rem;
}
.file-list li .file-status-badge {
     font-size: 0.75rem;
     padding: 2px 8px;
     border-radius: 10px;
     font-weight: 600;
}
.file-list li .file-status-badge.status-done {
     background: var(--color-success-light);
     color: var(--color-success);
}


.file-list li .file-delete {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--color-error);
    font-size: 1rem;
    padding: 2px 6px;
    border-radius: 4px;
    transition: background var(--transition);
}

.file-list li .file-delete:hover {
    background: var(--color-error-light);
}

.file-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

/* ===== OCR Controls ===== */
.ocr-controls {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-bottom: 12px;
}

/* ===== Session Links ===== */
.session-links {
    margin-top: 8px;
}

.session-links a {
    color: var(--color-primary);
    text-decoration: none;
    font-size: 0.88rem;
}

.session-links a:hover {
    text-decoration: underline;
}

.session-link-container {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
    font-size: 0.88rem;
}

.session-link-container .pulse {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--color-primary);
    animation: pulse 1.5s infinite;
}

/* ===== Batch Log ===== */
.batch-log {
    background: #1a202c;
    color: #e2e8f0;
    border-radius: var(--radius);
    padding: 12px 16px;
    font-family: var(--font-mono);
    font-size: 0.82rem;
    max-height: 300px;
    overflow-y: auto;
    line-height: 1.7;
}

.batch-log .log-entry {
    padding: 2px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.batch-log .log-entry:last-child {
    border-bottom: none;
}

.batch-log .log-entry.info { color: #90cdf4; }
.batch-log .log-entry.success { color: #68d391; }
.batch-log .log-entry.error { color: #fc8181; }
.batch-log .log-entry.warning { color: #fbd38d; }

/* ===== Results ===== */
.results-toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.results-tabs {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--color-border);
    margin-bottom: 16px;
    overflow-x: auto;
}

.results-tab {
    padding: 8px 16px;
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
    border: none;
    background: none;
    color: var(--color-text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all var(--transition);
    white-space: nowrap;
}

.results-tab:hover {
    color: var(--color-text);
}

.results-tab.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
     font-weight: 600;
}

.results-rendered {
    padding: 16px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    min-height: 200px;
    max-height: 600px;
    overflow-y: auto;
    line-height: 1.7;
}

.results-rendered h1, .results-rendered h2, .results-rendered h3 {
    margin-top: 1em;
    margin-bottom: 0.5em;
}

.results-rendered p {
    margin-bottom: 0.8em;
}

.results-rendered pre {
    background: var(--color-bg);
    padding: 12px;
    border-radius: var(--radius);
    overflow-x: auto;
    font-size: 0.88rem;
}

.results-rendered code {
    font-family: var(--font-mono);
    font-size: 0.88em;
}

.results-rendered img {
    max-width: 100%;
    border-radius: var(--radius);
}
.results-rendered table {
     width: 100%;
     border-collapse: collapse;
     margin: 1em 0;
     font-size: 0.9rem;
}
.results-rendered th,
.results-rendered td {
     padding: 8px 12px;
     text-align: left;
     border: 1px solid var(--color-border);
}
.results-rendered th {
     background: var(--color-bg);
     font-weight: 600;
}
.results-rendered blockquote {
     border-left: 4px solid var(--color-primary);
     margin: 1em 0;
     padding: 8px 16px;
     background: var(--color-primary-light);
     border-radius: 0 var(--radius) var(--radius) 0;
     color: var(--color-text);
}
.results-rendered ul,
.results-rendered ol {
     margin: 0.5em 0 1em 1.5em;
}
.results-rendered li {
     margin-bottom: 0.3em;
}
.results-rendered hr {
     border: none;
     border-top: 2px solid var(--color-border);
     margin: 1.5em 0;
}
/* Result file header */
.result-file-header {
     display: flex;
     align-items: center;
     gap: 12px;
     padding: 10px 14px;
     margin-bottom: 16px;
     background: var(--color-bg);
     border-radius: var(--radius);
     font-size: 0.88rem;
     border: 1px solid var(--color-border);
}
.result-source-label {
     color: var(--color-text);
}
.result-output-label {
     color: var(--color-text-muted);
     font-size: 0.82rem;
}


.results-raw {
    width: 100%;
    min-height: 300px;
    max-height: 600px;
    padding: 12px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-family: var(--font-mono);
    font-size: 0.88rem;
    line-height: 1.6;
    resize: vertical;
    color: var(--color-text);
    background: var(--color-surface);
}

/* ===== Save / Git ===== */
.save-controls {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.input-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--color-text-muted);
}

.input-group input[type="text"] {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
}

.git-status {
    margin-top: 12px;
    font-size: 0.85rem;
    color: var(--color-text-muted);
}

.git-status code {
    font-family: var(--font-mono);
    background: var(--color-bg);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 0.85em;
}

/* ===== Usage ===== */
.usage-summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.usage-stat {
    text-align: center;
    padding: 12px;
    background: var(--color-bg);
    border-radius: var(--radius);
}

.usage-label {
    display: block;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.usage-value {
    display: block;
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--color-text);
}

#usage-table-container table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

#usage-table-container th,
#usage-table-container td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
}

#usage-table-container th {
    font-weight: 600;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

#usage-table-container tr:last-child td {
    font-weight: 600;
}

/* ===== Status Messages ===== */
.status-message {
    font-size: 0.88rem;
    padding: 0;
    margin-top: 8px;
    border-radius: var(--radius);
    transition: all var(--transition);
}

.status-message:empty {
    display: none;
}

.status-message.success {
    color: var(--color-success);
    background: var(--color-success-light);
    padding: 8px 12px;
}

.status-message.error {
    color: var(--color-error);
    background: var(--color-error-light);
    padding: 8px 12px;
}

.status-message.info {
    color: var(--color-info);
    background: var(--color-info-light);
    padding: 8px 12px;
}

.status-message.warning {
    color: var(--color-warning);
    background: var(--color-warning-light);
    padding: 8px 12px;
}

/* ===== Toast ===== */
#toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 20px;
    border-radius: var(--radius);
    color: #fff;
    font-size: 0.9rem;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    animation: slideIn 0.3s ease;
    max-width: 360px;
}

.toast.success { background: var(--color-success); }
.toast.error { background: var(--color-error); }
.toast.info { background: var(--color-info); }
.toast.warning { background: var(--color-warning); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ===== Responsive ===== */
@media (max-width: 640px) {
    .app-container {
        padding: 16px 10px 48px;
    }

    .card {
        padding: 16px;
    }

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

    .model-selectors {
        grid-template-columns: 1fr;
    }

    .upload-area {
        padding: 24px 16px;
    }

    .results-toolbar {
        flex-direction: column;
    }

    .ocr-controls {
        flex-direction: column;
        align-items: stretch;
    }
}