:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --edit-bg: #dbeafe;
  --edit-color: #2563eb;
  --edit-bg-hover: #bfdbfe;
  --delete-bg: #fee2e2;
  --delete-color: #dc2626;
  --delete-bg-hover: #fecaca;
  --success: #10b981;
  --warning: #f59e0b;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --font: 'Inter', system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  width: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  width: 100%;
  overflow-x: hidden;
}

.app-shell {
  min-height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
}

/* ── Topbar ── */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-inner {
  width: 100%;
  padding: 0.75rem 1.75rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
  color: inherit;
  justify-self: start;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  letter-spacing: -0.02em;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text strong { font-size: 0.9375rem; font-weight: 700; }
.brand-text small { font-size: 0.6875rem; color: var(--text-muted); font-weight: 500; }

.topbar-selects {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: min(100%, 520px);
}

.topbar-field {
  flex: 1 1 0;
  min-width: 0;
}

.tenant-select,
.record-file-select {
  width: 100%;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5L6 7.5L9 4.5' stroke='%2394a3b8' stroke-width='1.5' fill='none'/%3E%3C/svg%3E") no-repeat right 0.65rem center;
  appearance: none;
  cursor: pointer;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.tenant-select:focus,
.record-file-select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

.record-file-select.is-loading {
  opacity: 0.65;
  cursor: wait;
}

.record-file-select:disabled:not(.is-loading) {
  opacity: 0.85;
  cursor: default;
}

.topbar-status {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 0.35rem 0.75rem;
  background: var(--bg);
  border-radius: 999px;
  border: 1px solid var(--border-light);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}

.topbar-status.is-processing .status-dot {
  background: var(--warning);
  animation: pulse 1.2s ease infinite;
}

@keyframes pulse { 50% { opacity: 0.4; } }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); border: 0;
}

/* ── Main ── */
.main-content {
  flex: 1;
  width: 100%;
  padding: 1.25rem 1.75rem 2rem;
}

/* ── Tabs ── */
.tabs {
  display: flex;
  gap: 0;
  width: 100%;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.25rem;
  background: var(--surface);
  padding: 0 0.25rem;
  border-radius: var(--radius) var(--radius) 0 0;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.75rem 1.25rem;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover { color: var(--text-secondary); }

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-icon { width: 18px; height: 18px; opacity: 0.85; }

.tab-panel[hidden] { display: none !important; }

.tab-panel {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  width: 100%;
}

/* ── Cards ── */
.card {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-head { padding: 1.25rem 1.5rem 0; }
.card-head h2 { font-size: 1rem; font-weight: 700; margin-bottom: 0.2rem; }
.card-head p { font-size: 0.8125rem; color: var(--text-muted); margin: 0; }

.row-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.upload-card .dropzone { margin: 1rem 1.5rem 0; }
.upload-card .file-queue { margin: 0 1.5rem; }
.card-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 1rem 1.5rem 1.25rem;
  border-top: 1px solid var(--border-light);
  margin-top: 1rem;
}

/* ── Dropzone ── */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem 1.5rem;
  text-align: center;
  cursor: pointer;
  background: var(--bg);
  transition: border-color 0.2s, background 0.2s;
}

.dropzone:hover,
.dropzone.is-dragover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.dropzone-visual { color: var(--primary); margin-bottom: 0.75rem; opacity: 0.9; }
.dropzone-title { font-weight: 600; font-size: 0.9375rem; margin-bottom: 0.25rem; }
.dropzone-hint { font-size: 0.875rem; color: var(--text-secondary); }
.dropzone-meta { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.5rem; }

.link-btn {
  background: none; border: none; color: var(--primary);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: underline;
}

.file-queue {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  padding: 0.75rem 0;
}

.file-queue li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border-radius: 8px;
  font-size: 0.8125rem;
}

.file-queue .file-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.5rem 1.125rem;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-secondary {
  background: var(--surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--bg); }

.btn.is-loading .btn-label { opacity: 0; }
.btn.is-loading .btn-spinner { display: inline-block !important; }

.spinner, .btn-spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(79, 70, 229, 0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
}

.btn-spinner { display: none; position: absolute; }

.btn-primary { position: relative; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Icon buttons ── */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.icon-btn:active { transform: scale(0.96); }

.icon-btn-edit {
  background: var(--edit-bg);
  color: var(--edit-color);
}

.icon-btn-edit:hover {
  background: var(--edit-bg-hover);
  box-shadow: 0 1px 3px rgba(37, 99, 235, 0.2);
}

.icon-btn-delete {
  background: var(--delete-bg);
  color: var(--delete-color);
}

.icon-btn-delete:hover {
  background: var(--delete-bg-hover);
  box-shadow: 0 1px 3px rgba(220, 38, 38, 0.2);
}

.th-actions { width: 96px; }

/* ── Table ── */
.table-card {
  border-top: 1px solid var(--border-light);
  width: 100%;
}

.table-scroll {
  overflow-x: auto;
  width: 100%;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
  table-layout: auto;
}

.data-table th {
  padding: 0.75rem 1.125rem;
  text-align: left;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 0.8125rem 1.125rem;
  border-bottom: 1px solid var(--border-light);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  max-width: 220px;
  background: var(--surface);
}

.data-table tbody tr:nth-child(even) td {
  background: #fafbfc;
}

.data-table tbody tr:hover td {
  background: #f0f4ff !important;
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.data-table .muted { color: var(--text-muted); }

.td-actions {
  white-space: nowrap;
  width: 96px;
  position: sticky;
  right: 0;
  box-shadow: -6px 0 10px rgba(15, 23, 42, 0.04);
}

.td-actions .icon-btn + .icon-btn { margin-left: 0.375rem; }

.data-table th.th-actions {
  position: sticky;
  right: 0;
  background: var(--bg);
  z-index: 2;
  box-shadow: -6px 0 10px rgba(15, 23, 42, 0.04);
}

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 0.875rem 1rem;
  border-top: 1px solid var(--border-light);
  background: var(--bg);
}

.page-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.page-btn:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.page-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.page-info {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
  min-width: 140px;
  text-align: center;
}

/* ── States ── */
.state-msg {
  padding: 2.5rem 1.5rem;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.state-msg[hidden] { display: none !important; }
.state-error { color: var(--danger); }

/* ── Badge & download ── */
.badge {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  white-space: nowrap;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.btn-download svg { flex-shrink: 0; }

/* ── Processing overlay ── */
.processing-overlay {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.processing-overlay[hidden] { display: none !important; }

.processing-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 2rem 2.5rem;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-md);
}

.processing-card h3 {
  font-size: 1.125rem;
  font-weight: 700;
  margin: 1rem 0 0.35rem;
}

.processing-step {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 1.25rem;
}

.processing-rings {
  position: relative;
  width: 72px;
  height: 72px;
  margin: 0 auto;
}

.processing-rings .ring,
.loader-rings .ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid transparent;
}

.processing-rings .ring-1,
.loader-rings .ring-1 {
  border-top-color: var(--primary);
  animation: spin 0.9s linear infinite;
}

.processing-rings .ring-2 {
  inset: 8px;
  border-right-color: #7c3aed;
  animation: spin 1.2s linear infinite reverse;
}

.processing-rings .ring-3 {
  inset: 16px;
  border-bottom-color: #a5b4fc;
  animation: spin 1.5s linear infinite;
}

.progress-track {
  height: 6px;
  background: var(--border-light);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 1rem;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), #7c3aed);
  border-radius: 999px;
  transition: width 0.6s ease;
}

.processing-files {
  list-style: none;
  text-align: left;
  max-height: 120px;
  overflow-y: auto;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  background: var(--bg);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
}

.processing-files li {
  padding: 0.25rem 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Loader panels ── */
.loader-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem 1.5rem;
  gap: 1rem;
}

.loader-panel[hidden] { display: none !important; }

.loader-panel p,
.loader-caption {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin: 0;
}

.loader-rings {
  position: relative;
  width: 48px;
  height: 48px;
}

.loader-rings .ring-2 {
  inset: 6px;
  border-right-color: #7c3aed;
  animation: spin 1.1s linear infinite reverse;
}

.loader-panel-table {
  padding: 1.5rem;
  align-items: stretch;
}

.skeleton-table {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.skeleton-row {
  height: 40px;
  border-radius: 8px;
  background: linear-gradient(
    90deg,
    var(--border-light) 0%,
    #e8ecf4 50%,
    var(--border-light) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s ease infinite;
}

.skeleton-head { height: 36px; opacity: 0.7; }

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.batch-card[hidden] { display: none !important; }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal-overlay[hidden] { display: none !important; }

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-md);
}

.modal-edit {
  max-width: 720px;
  max-height: 90vh;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-light);
}

.modal-header h3 { font-size: 1rem; font-weight: 700; }

.modal-body {
  padding: 1rem 1.25rem;
  overflow-y: auto;
  display: grid;
  gap: 0.75rem;
}

.modal-body-edit {
  padding: 1.25rem 1.5rem;
  gap: 1.25rem;
}

.form-section {
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-light);
}

.form-section:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.form-section-title {
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0 0 0.75rem;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 1rem;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-width: 0;
}

.form-field-wide {
  grid-column: 1 / -1;
}

.form-field span {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.form-field input {
  font-family: inherit;
  font-size: 0.875rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  transition: border-color 0.15s, background 0.15s;
  width: 100%;
}

.form-field input:focus {
  outline: none;
  border-color: var(--primary);
  background: var(--surface);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
  .form-field-wide { grid-column: auto; }
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-light);
  background: var(--bg);
}

.modal-footer .btn-primary {
  position: relative;
  min-width: 130px;
}

/* ── Toasts ── */
.toast-container {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.8125rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: slideUp 0.25s ease;
  max-width: 360px;
}

.toast-success { border-left: 3px solid var(--success); }
.toast-error { border-left: 3px solid var(--danger); }
.toast-info { border-left: 3px solid var(--primary); }

@keyframes slideUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  justify-self: end;
}

.logout-form { margin: 0; }

.btn-sm {
  padding: 0.4rem 0.75rem;
  font-size: 0.8125rem;
}

.btn-block { width: 100%; }

/* ── Login ── */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #eef2ff 0%, var(--bg) 45%, #f8fafc 100%);
}

.login-shell {
  width: 100%;
  max-width: 420px;
  padding: 1.5rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 2rem;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  margin-bottom: 1.75rem;
}

.login-brand strong {
  display: block;
  font-size: 1.125rem;
}

.login-brand p {
  margin: 0.15rem 0 0;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.login-error {
  margin-bottom: 1rem;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.8125rem;
  font-weight: 500;
}

.login-form .form-field {
  margin-bottom: 1rem;
}

.login-form label {
  display: block;
  margin-bottom: 0.35rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.login-form input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  color: var(--text);
  background: var(--surface);
}

.login-form input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.login-form .btn-primary {
  margin-top: 0.5rem;
}

@media (max-width: 640px) {
  .topbar-inner { grid-template-columns: 1fr; gap: 0.75rem; }
  .topbar-selects, .topbar-actions { justify-self: stretch; max-width: none; }
  .topbar-selects { flex-direction: column; }
  .topbar-actions { justify-content: space-between; }
  .tabs { overflow-x: auto; }
}
