/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:        #2563eb;
  --primary-hover:  #1d4ed8;
  --success:        #16a34a;
  --warning:        #d97706;
  --error:          #dc2626;
  --dead:           #7f1d1d;
  --bg:             #f1f5f9;
  --card:           #ffffff;
  --border:         #e2e8f0;
  --text:           #1e293b;
  --muted:          #64748b;
  --radius:         8px;
  --shadow:         0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md:      0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.04);
}

html { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 14px;
  min-height: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout ───────────────────────────────────────────────────────────────── */
.nav {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.nav-brand {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-brand svg { color: var(--primary); }

.nav-actions { display: flex; align-items: center; gap: 12px; }

.page { max-width: 960px; margin: 0 auto; padding: 32px 24px; }

.page-wide { max-width: 1200px; margin: 0 auto; padding: 32px 24px; }

/* ── Cards ────────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.card-body { padding: 20px; }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background .15s, box-shadow .15s, opacity .15s;
  line-height: 1.4;
  white-space: nowrap;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }

.btn-secondary { background: var(--card); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--bg); }

.btn-danger { background: #fee2e2; color: var(--error); border-color: #fca5a5; }
.btn-danger:hover:not(:disabled) { background: #fecaca; }

.btn-success { background: #dcfce7; color: var(--success); border-color: #86efac; }
.btn-success:hover:not(:disabled) { background: #bbf7d0; }

.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-lg { padding: 10px 20px; font-size: 15px; }

.btn-icon { padding: 6px; border-radius: 6px; }

/* ── Forms ────────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text); }
.form-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  color: var(--text);
  background: var(--card);
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.form-select {
  width: 100%;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  background: var(--card);
  cursor: pointer;
  outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}
.form-select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }

.form-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.form-error { font-size: 12px; color: var(--error); margin-top: 4px; }

/* ── Upload zone ──────────────────────────────────────────────────────────── */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 40px 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  background: var(--bg);
  position: relative;
}

.upload-zone.dragover {
  border-color: var(--primary);
  background: #eff6ff;
}

.upload-zone input[type=file] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.upload-zone-icon { color: var(--muted); margin-bottom: 12px; }
.upload-zone-text { font-size: 15px; font-weight: 500; color: var(--text); margin-bottom: 4px; }
.upload-zone-hint { font-size: 12px; color: var(--muted); }

/* ── File list ────────────────────────────────────────────────────────────── */
.file-list { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }

.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.file-item-icon { color: var(--muted); flex-shrink: 0; }
.file-item-name { flex: 1; font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item-size { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.file-item-error { font-size: 11px; color: var(--error); flex-shrink: 0; }

/* ── Progress ─────────────────────────────────────────────────────────────── */
.progress-bar-wrap {
  height: 6px;
  background: var(--border);
  border-radius: 99px;
  overflow: hidden;
  margin-top: 10px;
}

.progress-bar {
  height: 100%;
  background: var(--primary);
  border-radius: 99px;
  transition: width .3s ease;
}

/* ── Badges / status ──────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .3px;
  text-transform: uppercase;
  white-space: nowrap;
}

.badge-queued    { background: #fef9c3; color: #854d0e; }
.badge-processing{ background: #dbeafe; color: #1e40af; }
.badge-done      { background: #dcfce7; color: #14532d; }
.badge-failed    { background: #fee2e2; color: var(--error); }
.badge-dead      { background: #fee2e2; color: var(--dead); }
.badge-skipped   { background: #f1f5f9; color: var(--muted); }
.badge-pending   { background: #f1f5f9; color: var(--muted); }

/* category badges */
.badge-category  { background: #ede9fe; color: #5b21b6; text-transform: none; letter-spacing: 0; font-size: 12px; }
.badge-insurance     { background: #ffe4e6; color: #9f1239; }
.badge-bill-of-entry { background: #ffedd5; color: #9a3412; }
.badge-bill          { background: #cffafe; color: #155e75; }
.badge-invoice       { background: #bdb8f991; color: #0f1176; }

/* OCR quality */
.badge-good        { background: #dcfce7; color: #14532d; }
.badge-poor        { background: #fef9c3; color: #854d0e; }
.badge-unreadable  { background: #fee2e2; color: var(--error); }

/* ── Pipeline stepper ─────────────────────────────────────────────────────── */
.pipeline {
  display: flex;
  align-items: center;
  gap: 0;
  margin: 12px 0;
  overflow-x: auto;
  padding-bottom: 2px;
}

.pipeline-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 72px;
  position: relative;
}

.pipeline-step + .pipeline-step::before {
  content: "";
  position: absolute;
  left: -50%;
  top: 13px;
  width: 100%;
  height: 2px;
  background: var(--border);
  z-index: 0;
}

.pipeline-step.step-done + .pipeline-step::before  { background: var(--success); }

.pipeline-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  position: relative;
  z-index: 1;
  border: 2px solid var(--border);
  background: var(--card);
  color: var(--muted);
  transition: background .2s, border-color .2s;
}

.step-done    .pipeline-dot { background: var(--success);   border-color: var(--success);   color: #fff; }
.step-processing .pipeline-dot { background: var(--primary); border-color: var(--primary); color: #fff; }
.step-failed  .pipeline-dot { background: var(--error);   border-color: var(--error);   color: #fff; }
.step-skipped .pipeline-dot { background: var(--bg);      border-color: var(--border);  color: var(--muted); }

.pipeline-label {
  font-size: 10px;
  margin-top: 5px;
  color: var(--muted);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .4px;
}

.step-done    .pipeline-label { color: var(--success); }
.step-processing .pipeline-label { color: var(--primary); font-weight: 600; }
.step-failed  .pipeline-label { color: var(--error); }

/* ── Tables ───────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }

thead th {
  padding: 10px 16px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  border-bottom: 2px solid var(--border);
  background: #f8fafc;
  white-space: nowrap;
}

tbody tr { transition: background .1s; }
tbody tr:hover { background: #f8fafc; }

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

.mono { font-family: "SF Mono", "Fira Code", monospace; font-size: 12px; }

/* ── Pagination ───────────────────────────────────────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  gap: 12px;
  flex-wrap: wrap;
}

.pagination-info { font-size: 13px; color: var(--muted); }
.pagination-controls { display: flex; gap: 4px; }

/* ── Alerts ───────────────────────────────────────────────────────────────── */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  border: 1px solid transparent;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.alert-success { background: #dcfce7; border-color: #86efac; color: #14532d; }
.alert-warning { background: #fef9c3; border-color: #fde68a; color: #78350f; }
.alert-error   { background: #fee2e2; border-color: #fca5a5; color: #7f1d1d; }
.alert-info    { background: #dbeafe; border-color: #93c5fd; color: #1e3a8a; }

.alert-body { flex: 1; }
.alert-title { font-weight: 600; margin-bottom: 2px; }

/* ── Doc card (task detail) ───────────────────────────────────────────────── */
.doc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(440px, 1fr));
  gap: 16px;
  margin-top: 20px;
}

.doc-card { border-radius: var(--radius); }

.doc-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.doc-icon { color: var(--muted); flex-shrink: 0; margin-top: 2px; }
.doc-name { font-size: 14px; font-weight: 600; word-break: break-word; }
.doc-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }

.doc-results {
  padding: 12px 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.doc-result-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.doc-result-label { color: var(--muted); min-width: 110px; font-size: 12px; }

.doc-actions { padding: 0 20px 16px; display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Modal ────────────────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 24px;
}

.modal {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 760px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title { font-size: 15px; font-weight: 600; }

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-body pre {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  background: #f8fafc;
  padding: 16px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* ── Misc helpers ─────────────────────────────────────────────────────────── */
.text-muted { color: var(--muted); }
.text-sm    { font-size: 12px; }
.text-xs    { font-size: 11px; }
.text-right { text-align: right; }
.flex       { display: flex; }
.items-center { align-items: center; }
.gap-2      { gap: 8px; }
.gap-3      { gap: 12px; }
.mt-1       { margin-top: 4px; }
.mt-2       { margin-top: 8px; }
.mt-3       { margin-top: 12px; }
.mt-4       { margin-top: 16px; }
.mb-3       { margin-bottom: 12px; }
.mb-4       { margin-bottom: 16px; }
.w-full     { width: 100%; }

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
  flex-shrink: 0;
}

.spinner-dark {
  border-color: rgba(37,99,235,.2);
  border-top-color: var(--primary);
}

@keyframes spin { to { transform: rotate(360deg); } }

[x-cloak] { display: none !important; }

/* ── Login page ───────────────────────────────────────────────────────────── */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 420px;
}

.login-logo {
  text-align: center;
  margin-bottom: 24px;
}

.login-logo-title {
  font-size: 22px;
  font-weight: 700;
  margin-top: 12px;
}

.login-logo-sub { font-size: 13px; color: var(--muted); margin-top: 4px; }

/* ── Section headers ──────────────────────────────────────────────────────── */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 12px;
}

.section-title { font-size: 16px; font-weight: 600; }

/* ── Filter bar ───────────────────────────────────────────────────────────── */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.filter-bar .form-select { width: auto; }

/* ── Task header ──────────────────────────────────────────────────────────── */
.task-header {
  margin-bottom: 24px;
  padding: 20px 24px;
}

.task-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.task-id { font-size: 18px; font-weight: 700; margin-bottom: 6px; }
.task-id .mono { font-size: 15px; }
.task-created { font-size: 13px; color: var(--muted); margin-top: 6px; }

.task-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Empty state ──────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--muted);
}

.empty-state-icon { margin-bottom: 12px; }
.empty-state-text { font-size: 14px; }

/* ── Auto-refresh indicator ───────────────────────────────────────────────── */
.refresh-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.refresh-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: .4; transform: scale(.8); }
}

/* (responsive rules consolidated at bottom of file) */

/* ══════════════════════════════════════════════════════════════════════════ */
/* ── Theme variables ─────────────────────────────────────────────────────── */

[data-theme="dark"] {
  --bg:           #0f172a;
  --card:         #1e293b;
  --border:       #334155;
  --text:         #e2e8f0;
  --muted:        #94a3b8;
  --primary:      #3b82f6;
  --primary-hover:#2563eb;
  --shadow:       0 1px 3px rgba(0,0,0,.3), 0 1px 2px rgba(0,0,0,.2);
  --shadow-md:    0 4px 6px -1px rgba(0,0,0,.3), 0 2px 4px rgba(0,0,0,.2);
}

[data-theme="dark"] thead th             { background: #1e293b; }
[data-theme="dark"] tbody tr:hover       { background: rgba(255,255,255,.03); }
[data-theme="dark"] .modal-body pre      { background: #0f172a; }
[data-theme="dark"] .upload-zone         { background: #0f172a; }
[data-theme="dark"] .upload-zone.dragover{ background: #1e3a5f; }

[data-theme="sepia"] {
  --bg:     #f4ede3;
  --card:   #fdf8f1;
  --border: #dfd3c0;
  --text:   #3b2a1a;
  --muted:  #8b7355;
}

[data-theme="sepia"] thead th       { background: #f4ede3; }
[data-theme="sepia"] .upload-zone   { background: #f4ede3; }

/* ── Sidebar layout ──────────────────────────────────────────────────────── */
html, body { height: 100%; }

.layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width .2s ease, min-width .2s ease;
  overflow: hidden;
  box-shadow: var(--shadow);
  z-index: 50;
}

.sidebar.sidebar-collapsed {
  width: 52px;
  min-width: 52px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 56px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  overflow: hidden;
}

.sidebar-brand-text {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  opacity: 1;
  transition: opacity .15s;
}

.sidebar-collapsed .sidebar-brand-text { opacity: 0; pointer-events: none; }

.sidebar-toggle {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  padding: 6px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  transition: background .15s, color .15s;
}

.sidebar-toggle:hover { background: var(--bg); color: var(--text); }

.sidebar-nav {
  flex: 1;
  padding: 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-footer {
  padding: 8px 8px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
  text-align: left;
  transition: background .12s, color .12s;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-item:hover { background: var(--bg); color: var(--text); }

.sidebar-item.active {
  background: #eff6ff;
  color: var(--primary);
}

[data-theme="dark"] .sidebar-item.active { background: rgba(59,130,246,.15); }
[data-theme="sepia"] .sidebar-item.active { background: #ede8e0; color: #7c5c35; }

.sidebar-item-danger:hover { background: #fee2e2; color: var(--error); }
[data-theme="dark"] .sidebar-item-danger:hover { background: rgba(220,38,38,.15); }

.sidebar-item-icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  width: 20px;
}

.sidebar-item-label {
  opacity: 1;
  transition: opacity .15s;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-collapsed .sidebar-item-label { opacity: 0; pointer-events: none; }

/* ── Main content area ───────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.main-content > div { flex: 1; }

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 28px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
  gap: 16px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.view-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.view-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px;
}

/* inner content padding for each view */
.view-body {
  padding: 24px 28px;
}

/* ── Dashboard stat cards ────────────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  margin-top: 24px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
  margin-bottom: 6px;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: .4px;
}

.stat-card-blue  { border-left: 3px solid var(--primary); }
.stat-card-green { border-left: 3px solid var(--success); }
.stat-card-yellow{ border-left: 3px solid var(--warning); }
.stat-card-red   { border-left: 3px solid var(--error); }
.stat-card-purple{ border-left: 3px solid #8b5cf6; }

.stat-card-blue  .stat-value { color: var(--primary); }
.stat-card-green .stat-value { color: var(--success); }
.stat-card-yellow .stat-value{ color: var(--warning); }
.stat-card-red   .stat-value { color: var(--error); }
.stat-card-purple .stat-value{ color: #8b5cf6; }

/* ── Files two-column layout ─────────────────────────────────────────────── */
.files-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  border-top: 1px solid var(--border);
  height: calc(100vh - 117px); /* viewport minus view-header */
}

.files-list-panel {
  width: 300px;
  min-width: 260px;
  max-width: 340px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--card);
  overflow: hidden;
  flex-shrink: 0;
}

.files-detail-panel {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  background: var(--bg);
}

.files-search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.files-search-icon { color: var(--muted); flex-shrink: 0; }

.files-search-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 13px;
  color: var(--text);
  background: transparent;
}

.files-search-input::placeholder { color: var(--muted); }

.files-search-clear {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  padding: 2px 4px;
  border-radius: 4px;
  flex-shrink: 0;
}
.files-search-clear:hover { color: var(--text); background: var(--border); }

.files-list-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 4px;
}

.file-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: 6px;
  background: none;
  cursor: pointer;
  text-align: left;
  transition: background .1s;
  color: var(--text);
}

.file-list-item:hover   { background: var(--bg); }
.file-list-item.selected { background: #eff6ff; }

[data-theme="dark"] .file-list-item.selected { background: rgba(59,130,246,.15); }
[data-theme="sepia"] .file-list-item.selected { background: #ede8e0; }

.file-list-item-icon { color: var(--muted); flex-shrink: 0; margin-top: 2px; }

.file-list-item-body { min-width: 0; flex: 1; }

.file-list-item-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 3px;
}

.file-list-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
}

/* ── File detail panel ───────────────────────────────────────────────────── */
.detail-content {
  padding: 24px;
}

.detail-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

.detail-file-icon { color: var(--muted); flex-shrink: 0; margin-top: 2px; }

.detail-file-name {
  font-size: 15px;
  font-weight: 600;
  word-break: break-word;
  margin-bottom: 6px;
}

.detail-file-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
  flex-wrap: wrap;
}

.detail-section { margin-bottom: 20px; }

.detail-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  margin-bottom: 10px;
}

.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; padding-top: 4px; }

/* ── Settings ────────────────────────────────────────────────────────────── */
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.settings-row-label { font-size: 14px; font-weight: 500; color: var(--text); }
.settings-row-hint  { font-size: 12px; color: var(--muted); margin-top: 2px; }

.theme-picker { display: flex; gap: 6px; flex-wrap: wrap; }

.theme-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
}

.theme-btn:hover { border-color: var(--primary); color: var(--primary); }

.theme-btn.active {
  border-color: var(--primary);
  background: #eff6ff;
  color: var(--primary);
  font-weight: 600;
}

[data-theme="dark"] .theme-btn.active { background: rgba(59,130,246,.15); }

/* ── Model picker ─────────────────────────────────────────────────────────── */
.model-picker { position: relative; }

.model-picker-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: border-color .15s;
}
.model-picker-trigger:hover:not(:disabled) { border-color: var(--primary); }
.model-picker-trigger:disabled { opacity: 0.55; cursor: not-allowed; }
.model-picker-trigger-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-picker-placeholder { color: var(--muted); }

.model-picker-panel {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.14);
  z-index: 200;
  overflow: hidden;
}

.model-picker-search {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 13px;
  outline: none;
}

.model-picker-list { max-height: 260px; overflow-y: auto; }

.model-picker-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.model-picker-item:last-child { border-bottom: none; }
.model-picker-item:hover { background: var(--bg); }
.model-picker-item.active { background: #eff6ff; }
[data-theme="dark"] .model-picker-item.active { background: rgba(59,130,246,.12); }
.model-picker-item-name { font-size: 13px; color: var(--text); font-weight: 500; }
.model-picker-item-id { font-size: 11px; color: var(--muted); margin-top: 1px; font-family: monospace; }
.model-picker-none {
  padding: 8px 12px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-style: italic;
}
.model-picker-none:hover { background: var(--bg); color: var(--text); }
.model-picker-empty { padding: 16px 12px; color: var(--muted); font-size: 13px; text-align: center; }

/* ── Dashboard grid layout ─────────────────────────────────────────────────── */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

.dashboard-charts {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dashboard-charts-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  align-items: start;
}

.chart-card {
  padding: 16px 20px;
}

.chart-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--muted);
  margin-bottom: 14px;
}

.chart-body {
  display: flex;
  align-items: center;
  gap: 20px;
}

.chart-body svg {
  flex-shrink: 0;
}

.chart-empty {
  text-align: center;
  padding: 20px;
  color: var(--muted);
  font-size: 13px;
}

.chart-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.legend-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.legend-value {
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.dashboard-recent .section-header {
  margin-bottom: 12px;
}

.recent-list {
  display: flex;
  flex-direction: column;
}

.recent-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}

.recent-item:last-child {
  border-bottom: none;
}

.recent-item:hover {
  background: var(--bg);
}

.recent-item-icon {
  color: var(--muted);
  flex-shrink: 0;
}

.recent-item-body {
  flex: 1;
  min-width: 0;
}

.recent-item-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recent-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

/* ── View spacing helpers ────────────────────────────────────────────────── */
.view-body {
  padding: 24px 28px;
  flex: 1;
}

/* ── Upload progress ─────────────────────────────────────────────────────── */
.upload-progress-wrap {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}

.upload-progress-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 13px;
}

.upload-progress-label {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-weight: 500;
}

.upload-progress-pct {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  min-width: 36px;
  text-align: right;
}

.upload-progress-bar { transition: width .2s ease; }

/* ── File item done state ────────────────────────────────────────────────── */
.file-item-done {
  border-color: var(--success) !important;
  background: color-mix(in srgb, var(--success) 6%, transparent);
}

[data-theme="dark"] .file-item-done {
  background: rgba(22,163,74,.1);
}

/* ── Category tags editor ────────────────────────────────────────────────── */
.category-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
  min-height: 32px;
  align-items: center;
}

.category-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: #eff6ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
  border-radius: 20px;
  padding: 3px 10px 3px 12px;
  font-size: 12px;
  font-weight: 500;
}

[data-theme="dark"] .category-tag {
  background: rgba(37,99,235,.2);
  color: #93c5fd;
  border-color: rgba(59,130,246,.3);
}

.category-tag-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: currentColor;
  opacity: .5;
  padding: 0;
  font-size: 11px;
  line-height: 1;
  display: flex;
  align-items: center;
}
.category-tag-remove:hover { opacity: 1; }

.category-add-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ── File type chips ─────────────────────────────────────────────────────── */
.file-type-chip {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .04em;
  margin-top: 1px;
}
.ft-pdf  { background: #fee2e2; color: #b91c1c; }
.ft-doc  { background: #dbeafe; color: #1d4ed8; }
.ft-xls  { background: #dcfce7; color: #15803d; }
.ft-img  { background: #f3e8ff; color: #7e22ce; }
.ft-file { background: var(--border); color: var(--muted); }
[data-theme="dark"] .ft-pdf  { background: rgba(185,28,28,.25);  color: #fca5a5; }
[data-theme="dark"] .ft-doc  { background: rgba(29,78,216,.25);  color: #93c5fd; }
[data-theme="dark"] .ft-xls  { background: rgba(21,128,61,.25);  color: #86efac; }
[data-theme="dark"] .ft-img  { background: rgba(126,34,206,.25); color: #d8b4fe; }

.file-list-category {
  font-size: 11px;
  color: var(--primary);
  font-weight: 500;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-list-date { font-size: 11px; color: var(--muted); margin-left: auto; }

/* ── Detail hero ─────────────────────────────────────────────────────────── */
.detail-hero {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.detail-hero-ft {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .05em;
}

.detail-hero-info { min-width: 0; flex: 1; }

.detail-hero-badges {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

/* ── Detail info grid ────────────────────────────────────────────────────── */
.detail-info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.detail-info-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}

.detail-info-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 3px;
}

.detail-info-value {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  word-break: break-all;
}

/* ── Detail result card ──────────────────────────────────────────────────── */
.detail-result-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.detail-result-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  font-size: 13px;
}

.detail-result-row + .detail-result-row {
  border-top: 1px solid var(--border);
}

.detail-result-label {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  min-width: 100px;
  flex-shrink: 0;
}

.detail-result-val {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  flex-wrap: wrap;
}

.detail-result-via {
  font-size: 11px;
  color: var(--muted);
}

/* ── Quality bar ─────────────────────────────────────────────────────────── */
.quality-bar-wrap {
  height: 5px;
  background: var(--border);
  border-radius: 3px;
  width: 72px;
  overflow: hidden;
  flex-shrink: 0;
}

.quality-bar {
  height: 100%;
  border-radius: 3px;
  background: var(--success);
  transition: width .4s ease;
}

.quality-bar.badge-warning { background: var(--warning); }
.quality-bar.badge-error   { background: var(--error); }

/* ── Detail status states ────────────────────────────────────────────────── */
.detail-status-state { margin-bottom: 16px; }

.detail-status-queued,
.detail-status-processing,
.detail-status-failed {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.detail-status-queued    { color: var(--muted); background: var(--bg); }
.detail-status-processing { color: var(--primary); background: #eff6ff; }
.detail-status-failed    { color: var(--error); background: #fef2f2; }
[data-theme="dark"] .detail-status-processing { background: rgba(37,99,235,.12); }
[data-theme="dark"] .detail-status-failed     { background: rgba(220,38,38,.1); }

/* ── Bottom navigation (mobile only) ────────────────────────────────────── */
.bottom-nav {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: 58px;
  background: var(--card);
  border-top: 1px solid var(--border);
  z-index: 200;
  align-items: stretch;
}

.bottom-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 10px;
  font-weight: 500;
  padding: 6px 4px 8px;
  transition: color .12s;
  -webkit-tap-highlight-color: transparent;
}
.bottom-nav-item.active { color: var(--primary); }
.bottom-nav-item svg { flex-shrink: 0; }

/* ── Mobile back button (files detail) ──────────────────────────────────── */
.mobile-back-btn { display: none; }

/* ── Tablet: icon-only sidebar ───────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { width: 52px; min-width: 52px; }
  .sidebar .sidebar-brand-text,
  .sidebar .sidebar-item-label { opacity: 0; pointer-events: none; }
  .files-layout { flex-direction: column; height: auto; }
  .files-list-panel { width: 100%; max-width: 100%; border-right: none; border-bottom: 1px solid var(--border); height: 280px; }
  .files-detail-panel { height: auto; overflow-y: auto; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .dashboard-charts-row { grid-template-columns: 1fr; }
  .view-header { padding: 16px 16px 14px; }
  .view-body { padding: 16px; }
}

/* ── Mobile: full responsive ────────────────────────────────────────────── */
@media (max-width: 640px) {
  /* Layout: hide sidebar, show bottom nav, pad for it */
  .sidebar { display: none !important; }
  .bottom-nav { display: flex; }
  .main-content { padding-bottom: 58px; }

  /* View spacing */
  .view-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    padding: 14px 14px 12px;
  }
  .view-header .view-title { font-size: 18px; }
  .filter-bar { width: 100%; }
  .filter-bar .form-select { flex: 1; min-width: 0; }
  .view-body { padding: 12px 14px 20px; }

  /* Stats */
  .stat-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card { padding: 14px 12px; }
  .stat-value { font-size: 22px; }

  /* Dashboard charts */
  .dashboard-charts-row { grid-template-columns: 1fr; gap: 10px; }
  .chart-body { flex-direction: column; align-items: center; }
  .chart-legend { margin-left: 0; margin-top: 10px; width: 100%; }

  /* Files view: list/detail toggle */
  .files-layout { flex-direction: column; height: auto; }
  .files-list-panel {
    width: 100%; max-width: 100%;
    border-right: none; border-bottom: 1px solid var(--border);
    height: auto; max-height: 50vh;
  }
  .files-detail-panel { display: none; }
  .files-layout.has-selection .files-list-panel { display: none; }
  .files-layout.has-selection .files-detail-panel {
    display: block;
    min-height: calc(100vh - 140px);
    overflow-y: auto;
  }
  .mobile-back-btn { display: inline-flex; margin-bottom: 12px; }

  /* Detail content */
  .detail-content { padding: 14px; }
  .detail-hero { gap: 10px; }
  .detail-hero-ft { width: 44px; height: 44px; font-size: 10px; border-radius: 10px; }
  .detail-file-name { font-size: 13px; }
  .detail-info-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .detail-info-item { padding: 8px 10px; }
  .detail-actions { flex-wrap: wrap; }

  /* Pipeline: scrollable horizontally */
  .pipeline { overflow-x: auto; padding-bottom: 6px; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .pipeline-step { min-width: 54px; flex-shrink: 0; }
  .pipeline-dot { width: 24px; height: 24px; font-size: 10px; }
  .pipeline-label { font-size: 9px; }

  /* Tables: horizontal scroll */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 480px; }

  /* Upload */
  .upload-zone { padding: 22px 14px; }
  .upload-zone-text { font-size: 14px; }
  .upload-zone-hint { font-size: 11px; }
  .upload-progress-wrap { padding: 10px 12px; }
  .file-list { gap: 6px; }
  .file-item { gap: 8px; padding: 8px 10px; }

  /* Section header */
  .section-header { align-items: flex-start; flex-wrap: wrap; gap: 8px; }

  /* Cards */
  .card-body { padding: 14px; }
  .card-header { padding: 12px 14px; }

  /* Settings */
  .settings-row { flex-direction: column; align-items: flex-start; gap: 10px; }
  .theme-picker { flex-wrap: wrap; gap: 6px; }
  .model-picker-panel { left: 0; right: 0; width: auto; }
  .category-add-row { flex-direction: column; }
  .category-add-row .form-input { width: 100%; }

  /* Pagination */
  .pagination { flex-direction: column; align-items: center; gap: 8px; }

  /* Modal → bottom sheet */
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal { width: 100%; max-width: 100%; border-radius: 16px 16px 0 0; max-height: 88vh; }

  /* task.html */
  .page, .page-wide { padding: 14px 12px; }
  .nav { padding: 0 14px; height: 52px; }
  .nav-brand { font-size: 14px; }
  .nav-actions { gap: 6px; }
  .nav-actions .btn { padding: 5px 10px; font-size: 12px; }
  .task-header { padding: 14px; }
  .task-header-row { flex-direction: column; gap: 12px; }
  .task-id { font-size: 13px; word-break: break-all; }
  .task-actions { flex-wrap: wrap; gap: 6px; }
  .doc-grid { grid-template-columns: 1fr; gap: 10px; }

  /* Alerts */
  .alert { padding: 10px 12px; font-size: 13px; }
}

/* ── ERP invoice field mapping ─────────────────────────────────────────────── */
.erp-panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  background: var(--card);
  margin-top: 12px;
}
.erp-panel-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.erp-panel-title span { font-weight: 500; color: var(--muted); }
.erp-panel-head {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: #0f766e;
  border-left: 3px solid #0f766e;
  padding-left: 8px;
  margin-bottom: 10px;
}
.erp-fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 10px 16px;
}
.erp-field { min-width: 0; }
.erp-field-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--muted);
  margin-bottom: 2px;
}
.erp-field-value {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
  word-break: break-word;
}
.erp-table { font-size: 12px; }
.erp-table thead th { font-size: 10.5px; white-space: nowrap; }
.erp-table tbody td { white-space: nowrap; }
.erp-note {
  margin-top: 12px;
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
}
[data-theme="dark"] .erp-panel-head { color: #2dd4bf; border-left-color: #2dd4bf; }
[data-theme="sepia"] .erp-panel-head { color: #8a5a2b; border-left-color: #8a5a2b; }
