:root {
    --bg-color: #f6f8fa;
    --surface-bg: #ffffff;
    --surface-hover: #f9fbfd;
    --surface-muted: #f1f5f9;
    --text-primary: #111827;
    --text-secondary: #4b5563;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --border-strong: #cbd5e1;
    --accent: #0f4c81;
    --accent-hover: #0b3a63;
    --accent-subtle: #eff6ff;
    --accent-text: #1e40af;
    --warn-bg: #fffbeb;
    --warn-text: #b45309;
    --warn-border: #fde68a;
    --success: #15803d;
    --success-bg: #dcfce7;
    --danger: #dc2626;
    --radius: 8px;
    --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%234b5563' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #0d1117;
        --surface-bg: #161b22;
        --surface-hover: #1c2128;
        --surface-muted: #21262d;
        --text-primary: #f0f6fc;
        --text-secondary: #8b949e;
        --text-muted: #6e7681;
        --border-color: #30363d;
        --border-strong: #484f58;
        --accent: #388bfd;
        --accent-hover: #58a6ff;
        --accent-subtle: rgba(56, 139, 253, 0.14);
        --accent-text: #58a6ff;
        --warn-bg: rgba(245, 158, 11, 0.14);
        --warn-text: #fbbf24;
        --warn-border: rgba(245, 158, 11, 0.35);
        --success: #4ade80;
        --success-bg: rgba(34, 197, 94, 0.15);
        --danger: #f87171;
        --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238b949e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    }
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
}

/* Regla global obligatoria GEMINI.md: espaciado amplio del icono chevron en todos los <select> */
select,
.jesant-select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-color: var(--surface-bg);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.48rem 2.35rem 0.48rem 0.75rem;
    font-size: 0.85rem;
    line-height: 1.4;
    background-image: var(--select-chevron);
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 15px 15px;
    cursor: pointer;
}

select:focus,
.jesant-select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}

.select-compact {
    padding: 0.38rem 2.15rem 0.38rem 0.65rem;
    font-size: 0.8rem;
    background-position: right 0.65rem center;
}

header {
    background: var(--surface-bg);
    border-bottom: 1px solid var(--border-color);
    padding: 1.15rem 2rem 0.95rem;
    position: sticky;
    top: 0;
    z-index: 40;
}

.header-inner {
    max-width: 1520px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0.95rem;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    flex-wrap: wrap;
}

.brand-title-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-icon {
    width: 36px;
    height: 36px;
    border-radius: 7px;
    background: var(--accent-subtle);
    color: var(--accent-text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

h1 {
    font-size: 1.4rem;
    font-weight: 700;
}

.subtitle {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-top: 0.2rem;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
    padding-top: 0.65rem;
    border-top: 1px solid var(--border-color);
}

.toolbar-links,
.context-selectors {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-wrap: wrap;
}

.ctx-field {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.ctx-field label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.tab-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.42rem 0.8rem;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background: var(--surface-bg);
    color: var(--text-secondary);
    font-size: 0.82rem;
    font-weight: 500;
    text-decoration: none;
}

.tab-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

.content-area {
    max-width: 1520px;
    width: 100%;
    margin: 1.25rem auto;
    padding: 0 2rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1.15rem;
}

/* KPIs */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.9rem;
}

.kpi-card {
    background: var(--surface-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 0.9rem 1.1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.kpi-card.highlight-warn {
    border-color: var(--warn-border);
    background: var(--warn-bg);
}

.kpi-label {
    font-size: 0.76rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.kpi-value {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 1.3rem;
    font-weight: 700;
}

.kpi-sub {
    font-size: 0.76rem;
    color: var(--text-muted);
}

/* Dashboard Card & Tabs */
.dashboard-card {
    background: var(--surface-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    overflow: hidden;
}

.card-header {
    padding: 0.85rem 1.2rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.type-tabs {
    display: flex;
    gap: 0.35rem;
    flex-wrap: wrap;
}

.type-tab {
    background: transparent;
    border: 1px solid transparent;
    padding: 0.45rem 0.85rem;
    border-radius: 6px;
    font-size: 0.84rem;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
}

.type-tab:hover {
    background: var(--surface-hover);
}

.type-tab.active {
    background: var(--accent-subtle);
    color: var(--accent-text);
    border-color: rgba(30, 64, 175, 0.2);
}

.action-buttons {
    display: flex;
    gap: 0.6rem;
    align-items: center;
}

.btn-primary,
.btn-secondary,
.btn-scan,
.btn-validate,
.btn-merge,
.btn-danger-outline {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.5rem 0.95rem;
    border-radius: 6px;
    font-size: 0.84rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.15s;
}

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

.btn-scan {
    background: #0d9488;
    color: #fff;
}

.btn-validate {
    background: #15803d;
    color: #fff;
}

.btn-merge {
    background: #7c3aed;
    color: #fff;
    padding: 0.36rem 0.75rem;
    font-size: 0.78rem;
}

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

.btn-danger-outline {
    background: transparent;
    color: var(--danger);
    border-color: var(--danger);
}

.btn-xs {
    padding: 0.36rem 0.7rem;
    font-size: 0.78rem;
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.subfilters-bar {
    padding: 0.65rem 1.2rem;
    background: var(--surface-hover);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    gap: 0.65rem;
    flex-wrap: wrap;
}

.subfilter-input {
    flex: 1;
    min-width: 240px;
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--surface-bg);
    color: var(--text-primary);
    font-size: 0.83rem;
}

/* Main Table */
.table-container {
    overflow-x: auto;
}

.jesant-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.83rem;
}

.jesant-table th,
.jesant-table td {
    padding: 0.68rem 0.8rem;
    border-bottom: 1px solid var(--border-color);
    text-align: left;
}

.jesant-table thead th {
    background: var(--surface-muted);
    font-size: 0.73rem;
    font-weight: 600;
    color: var(--text-secondary);
    letter-spacing: 0.03em;
}

.jesant-table tbody tr:hover {
    background: var(--surface-hover);
}

.mono {
    font-family: 'IBM Plex Mono', monospace;
}

.empty-cell {
    text-align: center !important;
    padding: 2.5rem 1rem !important;
    color: var(--text-muted);
}

.pill-tipo,
.val-badge {
    display: inline-block;
    padding: 0.16rem 0.5rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 700;
}

.tipo-EMITIDA { background: #dbeafe; color: #1e40af; }
.tipo-RECIBIDA { background: #fce7f3; color: #9d174d; }
.tipo-TICKET { background: #fef3c7; color: #92400e; }

.val-revision { background: var(--warn-bg); color: var(--warn-text); border: 1px solid var(--warn-border); }
.val-validada { background: var(--success-bg); color: var(--success); }

.period-tag {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.76rem;
    padding: 0.15rem 0.45rem;
    border-radius: 5px;
    background: var(--surface-muted);
    border: 1px solid var(--border-color);
}

.icon-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    border-radius: 6px;
    padding: 0.3rem 0.45rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Modals & Split-Screen Editor */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 1rem;
}

.hidden {
    display: none !important;
}

.modal-card {
    background: var(--surface-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    width: 100%;
    box-shadow: 0 24px 48px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.modal-sm {
    max-width: 620px;
}

.modal-split {
    max-width: 1460px;
    height: 92vh;
    display: flex;
    flex-direction: column;
}

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

.editor-header-left,
.editor-header-actions {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.ocr-pill {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.75rem;
    padding: 0.2rem 0.55rem;
    border-radius: 6px;
}

.ocr-ok { background: var(--success-bg); color: var(--success); }
.ocr-diff { background: var(--warn-bg); color: var(--warn-text); border: 1px solid var(--warn-border); }

.modal-body {
    padding: 1.25rem;
}

.dropzone {
    margin-top: 1rem;
    border: 2px dashed var(--border-strong);
    border-radius: 10px;
    padding: 2rem 1.25rem;
    text-align: center;
    position: relative;
    cursor: pointer;
    background: var(--surface-hover);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.45rem;
}

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

.dropzone-hint {
    font-size: 0.76rem;
    color: var(--text-muted);
}

.selected-file-pill {
    margin-top: 0.4rem;
    padding: 0.25rem 0.7rem;
    background: var(--accent-subtle);
    color: var(--accent-text);
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
}

/* Split Workspace */
.split-workspace {
    display: grid;
    grid-template-columns: 42% 58%;
    flex: 1;
    min-height: 0;
}

.viewer-pane {
    border-right: 1px solid var(--border-color);
    background: var(--surface-muted);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.viewer-toolbar {
    padding: 0.55rem 0.9rem;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.8rem;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.viewer-link {
    color: var(--accent-text);
    text-decoration: none;
    font-size: 0.78rem;
}

.viewer-frame-box {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    min-height: 0;
}

.viewer-frame-box iframe {
    width: 100%;
    height: 100%;
    border: none;
}

.viewer-frame-box img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.viewer-placeholder {
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 2rem;
    text-align: center;
}

.viewer-footer-meta {
    padding: 0.45rem 0.9rem;
    border-top: 1px solid var(--border-color);
    font-size: 0.74rem;
    color: var(--text-muted);
    font-family: 'IBM Plex Mono', monospace;
}

.form-pane {
    padding: 1rem 1.25rem;
    overflow-y: auto;
    min-height: 0;
}

.section-label {
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--accent-text);
    margin: 0.65rem 0 0.55rem;
}

.form-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.8rem;
}

.form-grid-4 {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.7rem;
    margin-bottom: 0.75rem;
}

.span-2 {
    grid-column: span 2;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-group label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.inline-link-btn {
    background: none;
    border: none;
    color: var(--accent-text);
    font-size: 0.73rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
}

.form-group input {
    padding: 0.46rem 0.65rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--surface-bg);
    color: var(--text-primary);
    font-size: 0.84rem;
}

.lines-header-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin: 0.9rem 0 0.5rem;
}

.lines-toolbar {
    display: flex;
    gap: 0.45rem;
    flex-wrap: wrap;
}

.lines-table-wrap {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow-x: auto;
}

.lines-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.lines-table th,
.lines-table td {
    padding: 0.45rem 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.lines-table thead th {
    background: var(--surface-muted);
    font-size: 0.72rem;
    color: var(--text-secondary);
    text-align: left;
}

.lines-table input {
    width: 100%;
    padding: 0.36rem 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    background: var(--surface-bg);
    color: var(--text-primary);
    font-size: 0.81rem;
}

.lines-table select {
    width: 100%;
    padding: 0.36rem 1.85rem 0.36rem 0.45rem;
    background-position: right 0.45rem center;
    font-size: 0.79rem;
}

.merged-indicator {
    display: inline-block;
    margin-top: 2px;
    font-size: 0.69rem;
    color: #7c3aed;
    font-weight: 600;
}

.totals-and-notes {
    margin-top: 0.9rem;
    display: flex;
    gap: 1.2rem;
    align-items: flex-start;
    flex-wrap: wrap;
}

.totals-box {
    min-width: 260px;
    background: var(--surface-hover);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.65rem 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.tot-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.82rem;
}

.tot-grand {
    margin-top: 0.25rem;
    padding-top: 0.35rem;
    border-top: 1px solid var(--border-strong);
    font-size: 0.95rem;
    color: var(--accent-text);
}

.modal-footer {
    margin-top: 1rem;
    padding-top: 0.85rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

footer {
    max-width: 1520px;
    width: 100%;
    margin: 0 auto;
    padding: 1rem 2rem 1.5rem;
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-muted);
}

@media (max-width: 1100px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .split-workspace { grid-template-columns: 1fr; }
    .viewer-pane { height: 280px; border-right: none; border-bottom: 1px solid var(--border-color); }
}
