:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #16a34a;
  --ok-bg: #dcfce7;
  --ok-text: #166534;
  --fuzzy-bg: #fef3c7;
  --fuzzy-text: #92400e;
  --missing-bg: #fee2e2;
  --missing-text: #991b1b;
  --unknown-bg: #e2e8f0;
  --unknown-text: #475569;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.5;
}

h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.15rem; }

/* Topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.85rem 1.75rem;
  background: #101828;
  color: white;
}
.topbar nav a {
  color: #cbd5e1;
  margin-right: 1.5rem;
  text-decoration: none;
  font-weight: 500;
  padding-bottom: 2px;
  border-bottom: 2px solid transparent;
  transition: color 0.15s;
}
.topbar nav a:hover { color: white; border-bottom-color: var(--primary); }
.user-info { display: flex; align-items: center; gap: 1rem; color: #cbd5e1; font-size: 0.85rem; }
.user-info a { color: #cbd5e1; text-decoration: none; }
.user-info a:hover { color: white; }

main { padding: 1.75rem; max-width: 1280px; margin: 0 auto; }

/* Page head */
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1.5rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}
.page-head-title { display: flex; align-items: center; gap: 1rem; }
.type-icon {
  font-size: 1.6rem;
  width: 52px;
  height: 52px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.page-subtitle { margin: 0.15rem 0 0; color: var(--text-muted); font-size: 0.88rem; }

/* Compact import status pills, top-right */
.import-status-compact { display: flex; flex-direction: column; align-items: flex-end; gap: 0.4rem; }
.import-pill {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.3rem 0.4rem 0.3rem 0.85rem;
  font-size: 0.8rem;
  box-shadow: var(--shadow);
}
.import-pill-label { font-weight: 600; color: var(--text-muted); }
.import-auto-note { margin: 0; font-size: 0.72rem; color: var(--text-muted); }

/* Stats */
.stats-bar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.stat-value { font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.stat-label { font-size: 0.78rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.stat-ok .stat-value { color: var(--ok-text); }
.stat-fuzzy .stat-value { color: var(--fuzzy-text); }
.stat-missing .stat-value { color: var(--missing-text); }
.stat-unknown .stat-value { color: var(--unknown-text); }

/* Filters */
.filters {
  display: flex;
  gap: 1rem;
  align-items: end;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.filters label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.filters select, .filters input {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  background: white;
  color: var(--text);
}
.inline-form { display: inline; }

.import-status-text { display: inline-flex; gap: 0.4rem; }

.row-type-icon { margin-right: 0.4rem; font-size: 0.95rem; }

/* Buttons */
.btn {
  cursor: pointer;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  background: white;
  border-radius: 7px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: #f1f5f9; }
.btn:disabled, .icon-btn:disabled { opacity: 0.5; cursor: default; }
.btn:disabled:hover, .icon-btn:disabled:hover { background: white; }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-busy,
.btn-busy:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  color: white;
}
.btn-small { padding: 0.32rem 0.7rem; font-size: 0.78rem; }
.btn-muted { background: var(--unknown-bg); color: var(--unknown-text); border-color: var(--unknown-bg); }
.btn-muted:hover { background: #cbd5e1; border-color: #cbd5e1; }
.btn-danger { background: white; color: var(--missing-text); border-color: #fca5a5; }
.btn-danger:hover { background: var(--missing-bg); border-color: var(--missing-text); }
.icon-btn {
  cursor: pointer;
  border: 1px solid var(--border);
  background: white;
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font-size: 0.9rem;
  line-height: 1;
}
.icon-btn:hover { background: #f1f5f9; }
a.btn-primary { text-decoration: none; display: inline-block; }

/* Table */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  overflow-x: auto;
}
.documents-table { width: 100%; border-collapse: collapse; }
.documents-table th, .documents-table td { padding: 0.65rem 0.85rem; border-bottom: 1px solid var(--border); text-align: left; font-size: 0.85rem; white-space: nowrap; }
.documents-table thead th {
  background: #f8fafc;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  position: sticky;
  top: 0;
}
.documents-table tbody tr:hover { background: #f8fafc; }
.documents-table tbody tr:last-child td { border-bottom: none; }
.documents-table a { color: var(--primary); text-decoration: none; font-weight: 500; }
.documents-table a:hover { text-decoration: underline; }
.documents-table thead th a.sort-link {
  color: inherit;
  text-decoration: none;
  font-weight: 600;
  font-size: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  cursor: pointer;
}
.documents-table thead th a.sort-link:hover { color: var(--primary); text-decoration: none; }
.documents-table thead th a.sort-link.sort-active { color: var(--primary); }
.documents-table thead th a.sort-active .sort-arrow { margin-left: 0.25em; font-size: 0.7em; }
.col-checkbox { width: 2rem; }
.col-amount { text-align: right; font-variant-numeric: tabular-nums; }
.col-problem { min-width: 8rem; }
.col-supplier { max-width: 14rem; overflow: hidden; text-overflow: ellipsis; }
.col-note { max-width: 14rem; overflow: hidden; text-overflow: ellipsis; cursor: help; }
.col-account { max-width: 7rem; overflow: hidden; text-overflow: ellipsis; }
.payment-icon {
  width: 1.4rem;
  height: 1.4rem;
  padding: 0;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: help;
}
.col-actions { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.row-error { background: #fff2f2; }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-ok { background: var(--ok-bg); color: var(--ok-text); }
.badge-fuzzy { background: var(--fuzzy-bg); color: var(--fuzzy-text); cursor: help; }
.badge-missing { background: var(--missing-bg); color: var(--missing-text); }
.badge-unknown { background: var(--unknown-bg); color: var(--unknown-text); }
.badge-problem { background: #fff7ed; color: #9a3412; }

.bulk-bar { margin-bottom: 0.75rem; }

.vs-info { font-size: 0.9rem; margin: 0 0 1rem; }
.muted { color: var(--text-muted); }

.bank-candidates {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.bank-candidate {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.4rem 0.75rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.82rem;
}
.bank-candidate-info { display: flex; flex-direction: column; gap: 0.2rem; min-width: 0; }
.bank-candidate-headline { display: flex; align-items: baseline; gap: 0.6rem; flex-wrap: wrap; }
.bank-candidate-number { font-weight: 600; }
.bank-candidate-amount { font-weight: 700; font-size: 1.1rem; color: var(--primary-dark); }
.bank-candidate-meta { color: var(--text-muted); font-size: 0.78rem; overflow-wrap: anywhere; }
.bank-candidate-warning { color: #92400e; font-size: 0.78rem; font-weight: 600; }
.bank-candidate-actions { display: flex; gap: 0.3rem; flex-wrap: wrap; }

.bank-match-dialog { width: min(760px, 95vw); max-width: none; padding: 0; }
.bank-match-context { margin: 0.9rem 1.1rem 0; color: var(--text-muted); font-size: 0.85rem; }
#bank-match-search-form { display: flex; gap: 0.3rem; margin: 0.75rem 1.1rem; position: relative; }
#bank-match-search-form input[type="search"] {
  flex: 1;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  background: white;
  color: var(--text);
}
#bank-match-candidates { padding: 0 1.1rem 1.1rem; max-height: 60vh; overflow-y: auto; }
#bank-match-candidates .bank-candidate { font-size: 0.88rem; padding: 0.6rem 0.75rem; }

.statement-upload { margin-bottom: 1.25rem; }
.statement-upload summary { cursor: pointer; list-style: none; }
.statement-upload summary::-webkit-details-marker { display: none; }
.statement-upload-form { margin-top: 0.6rem; margin-bottom: 0.4rem; }

.admin-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}
.admin-section h2 { margin-top: 0; }
.admin-section .table-wrap { margin: 0.75rem 0; }
.admin-section .filters { box-shadow: none; border: none; padding: 0; margin-bottom: 0.75rem; }
.admin-cache-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.checkbox-inline { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.6rem; font-size: 0.9rem; }
.logs-box {
  margin-top: 0.6rem;
  max-height: 22rem;
  overflow-y: auto;
  background: #0f172a;
  color: #e2e8f0;
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}
.admin-toggle { display: flex; align-items: center; gap: 0.5rem; font-weight: 500; margin: 0.6rem 0; cursor: pointer; }

.warning { color: #92400e; background: #fffbeb; border: 1px solid #fde68a; padding: 0.65rem 0.9rem; border-radius: var(--radius); font-size: 0.85rem; }
.error { color: #991b1b; background: #fef2f2; border: 1px solid #fecaca; padding: 0.65rem 0.9rem; border-radius: var(--radius); font-size: 0.85rem; }
.ok { color: var(--ok-text); }
.err { color: var(--missing-text); }
.blocked { color: var(--fuzzy-text); }

#upload-results ul { list-style: none; padding: 0; margin: 1rem 0 0; display: flex; flex-direction: column; gap: 0.3rem; }
#upload-results li {
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  white-space: normal;
  overflow-wrap: anywhere;
}
.upload-results-notice {
  margin-top: 1rem;
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.85rem;
}
.upload-results-notice-busy {
  background: #eff6ff;
  border-color: #bfdbfe;
  color: #1d4ed8;
}
.upload-results-notice-error {
  background: var(--missing-bg);
  border-color: #fca5a5;
  color: var(--missing-text);
}
.toast-host {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  max-width: min(28rem, calc(100vw - 2rem));
  pointer-events: none;
}
.toast {
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: 0 14px 32px rgba(15, 23, 42, 0.18);
  padding: 0.8rem 0.95rem;
  font-size: 0.88rem;
  line-height: 1.4;
  white-space: normal;
  overflow-wrap: anywhere;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}
.toast-success {
  background: var(--ok-bg);
  border-color: #86efac;
  color: var(--ok-text);
}
.toast-info {
  background: #eff6ff;
  border-color: #bfdbfe;
  color: #1d4ed8;
}
.toast-error {
  background: var(--missing-bg);
  border-color: #fca5a5;
  color: var(--missing-text);
}
.toast-blocked {
  background: var(--fuzzy-bg);
  border-color: #fcd34d;
  color: var(--fuzzy-text);
}

.login-box { max-width: 400px; margin: 5rem auto; text-align: center; }
.login-box h1 { margin-bottom: 1.5rem; }
.landing-hero {
  min-height: calc(100vh - 8rem);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem 0;
}
.landing-card {
  width: min(560px, 100%);
  background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.12);
  padding: 2.5rem;
  text-align: center;
}
.landing-eyebrow {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  background: #dbeafe;
  color: #1d4ed8;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.landing-card h1 {
  margin: 1rem 0 0.75rem;
  font-size: clamp(2.2rem, 5vw, 3.4rem);
}
.landing-copy {
  margin: 0 auto;
  max-width: 34rem;
  color: var(--text-muted);
  font-size: 1rem;
}
.landing-actions {
  margin-top: 1.5rem;
}

/* Orphans */
.orphans-box { margin-top: 1.5rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }
.orphans-box summary { cursor: pointer; font-weight: 600; font-size: 0.9rem; }
.orphans-box table { margin-top: 0.85rem; }
.overmatch-payment-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.15rem 0; }

.back-link { color: var(--text-muted); text-decoration: none; font-size: 0.85rem; }
.back-link:hover { color: var(--primary); }

/* Detail page */
.detail-layout { display: flex; gap: 2rem; }
.detail-preview, .detail-form { flex: 1; min-width: 0; }
.pdf-frame { width: 100%; height: 70vh; border: 1px solid var(--border); border-radius: var(--radius); background: #525659; }
.attachment-list {
  list-style: none;
  padding: 0;
  margin: 0 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.attachment-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--primary);
  cursor: pointer;
  font: inherit;
  text-align: left;
}
.attachment-link:hover {
  text-decoration: underline;
}
.attachment-link.is-active {
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
}
.detail-form form { display: flex; flex-direction: column; gap: 0.85rem; max-width: 420px; }
.detail-form form label { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; display: flex; flex-direction: column; gap: 0.3rem; }
.detail-form form select, .detail-form form input {
  padding: 0.5rem 0.65rem; border: 1px solid var(--border); border-radius: 7px; font-size: 0.85rem;
}
.upload-status {
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--text);
  font-size: 0.88rem;
}
.upload-status[data-state="busy"] {
  background: #eff6ff;
  border-color: #bfdbfe;
  color: #1d4ed8;
}
.upload-status[data-state="success"] {
  background: var(--ok-bg);
  border-color: #86efac;
  color: var(--ok-text);
}
.upload-status[data-state="error"] {
  background: var(--missing-bg);
  border-color: #fca5a5;
  color: var(--missing-text);
}
.upload-status[data-state="blocked"] {
  background: var(--fuzzy-bg);
  border-color: #fcd34d;
  color: var(--fuzzy-text);
}
.upload-status-link {
  color: inherit;
  font-weight: 700;
}
.upload-status-link:hover {
  color: inherit;
}
.upload-inline-status {
  display: none;
  font-size: 0.78rem;
  font-weight: 600;
}
.upload-inline-status[data-state="busy"] { color: #1d4ed8; }
.upload-inline-status[data-state="success"] { color: var(--ok-text); }
.upload-inline-status[data-state="error"] { color: var(--missing-text); }
.upload-inline-status[data-state="blocked"] { color: var(--fuzzy-text); }
.detail-form table { width: 100%; border-collapse: collapse; margin-bottom: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.detail-form th, .detail-form td { padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--border); font-size: 0.83rem; }
.highlight { background: #eef7ee; font-weight: 600; }

/* Dialogs */
dialog { border-radius: 12px; border: none; padding: 1.5rem; box-shadow: 0 10px 40px rgba(15, 23, 42, 0.25); max-width: 480px; }
dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
.modal-actions { display: flex; gap: 0.6rem; justify-content: end; margin-top: 1.25rem; }
#confirm-list { max-height: 40vh; overflow-y: auto; padding-left: 1.2rem; font-size: 0.85rem; }

.preview-dialog { width: min(900px, 92vw); max-width: none; padding: 0; overflow: hidden; }
.preview-dialog-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.85rem 1.1rem; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 0.9rem;
}
.preview-dialog-titlebar,
.preview-dialog-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.preview-position {
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 500;
}
.preview-dialog .pdf-frame { width: 100%; height: 82vh; border: none; border-radius: 0; display: block; }
.logout-form { display: inline; margin: 0; }
.link-button { background: none; border: 0; color: inherit; cursor: pointer; font: inherit; padding: 0; text-decoration: underline; }
