:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #1d2733;
  --muted: #6b7785;
  --line: #e3e8ef;
  --primary: #1f4e78;
  --primary-d: #163a5a;
  --accent: #2d7ff9;
  --hi: #e7f6ec; --hi-b: #2e9e54;
  --mid: #fff7e6; --mid-b: #d99311;
  --lo: #fdeceb; --lo-b: #d64541;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.muted { color: var(--muted); font-size: 13px; }
.hidden { display: none !important; }
.link { color: var(--accent); text-decoration: underline; cursor: pointer; }

/* top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--primary); color: #fff; padding: 12px 22px;
}
.brand { font-weight: 700; }
.topbar nav { display: flex; align-items: center; gap: 18px; }
.topbar nav a { color: #d7e3f1; text-decoration: none; font-size: 14px; }
.topbar nav a.active, .topbar nav a:hover { color: #fff; }
.topbar .user { color: #9fc0e0; font-size: 13px; }
.topbar .logout { color: #ffd7d2 !important; }

.container { max-width: 1320px; margin: 16px auto; padding: 0 20px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px 18px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(20,40,70,.05);
}
.card h2 { margin: 0 0 4px; font-size: 16px; }

/* dropzone */
.dropzone {
  margin-top: 12px; border: 2px dashed #c3cedd; border-radius: 10px;
  padding: 20px; text-align: center; color: var(--muted);
  cursor: pointer; transition: .15s; background: #fafcff;
}
.dropzone.drag { border-color: var(--accent); background: #eef5ff; color: var(--ink); }

.file-list { list-style: none; padding: 0; margin: 10px 0 0; }
.file-list li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 4px;
  font-size: 13px; background: #fafcff;
}
.file-list .rm { color: var(--lo-b); cursor: pointer; border: none; background: none; font-size: 16px; }

/* buttons */
.actions { display: flex; align-items: center; gap: 12px; margin-top: 16px; flex-wrap: wrap; }
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 9px 16px; border-radius: 8px; cursor: pointer; font-size: 14px; text-decoration: none;
}
.btn:hover { background: #f1f4f9; }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-d); }
.btn.primary:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.status { font-size: 13px; color: var(--muted); }
.split-opt { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink); cursor: pointer; }

/* review grid: document viewer (left) + table (right) */
.review-grid { display: flex; gap: 16px; align-items: flex-start; margin-top: 14px; }
.doc-pane {
  flex: 0 0 40%; position: sticky; top: 12px;
  height: 80vh; border: 1px solid var(--line); border-radius: 10px;
  background: #f7f9fc; display: flex; flex-direction: column; overflow: hidden;
}
.doc-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 8px 6px 12px; background: #eef2f7; border-bottom: 1px solid var(--line);
}
.doc-name { font-size: 13px; font-weight: 600; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-tools { display: flex; gap: 4px; flex: 0 0 auto; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 6px;
  cursor: pointer; font-size: 15px; line-height: 1; padding: 4px 9px; }
.icon-btn:hover { background: #e7edf5; }
.doc-pane-body { flex: 1; overflow: auto; display: flex; }
.doc-pane-body iframe { width: 100%; height: 100%; border: none; transition: transform .15s; }
.doc-pane-body img { max-width: 100%; height: auto; margin: auto; transition: transform .15s; }
.doc-pane-body .doc-empty { margin: auto; color: var(--muted); font-size: 13px; padding: 20px; text-align: center; }

.splitter { flex: 0 0 12px; cursor: col-resize; position: relative; align-self: stretch; }
.splitter::before { content: "⋮"; position: absolute; left: 0; right: 0; top: 50%;
  transform: translateY(-50%); text-align: center; color: #9aa7b6; font-size: 18px;
  background: var(--line); border-radius: 6px; padding: 10px 0; line-height: .4; }
.splitter:hover::before { background: var(--accent); color: #fff; }

.review-grid .table-wrap { flex: 1; min-width: 0; margin-top: 0; }
.review-grid.no-doc .doc-pane, .review-grid.no-doc .splitter { display: none; }

.review-toolbar { display: flex; align-items: center; gap: 12px; margin: 6px 0 4px; flex-wrap: wrap; }

/* give each column room so text isn't truncated; table scrolls horizontally */
#resultsTable { min-width: 1240px; }
td input[data-k="supplier_name"], td input[data-k="buyer_name"] { min-width: 190px; }
td input[data-k="date"] { min-width: 92px; }
td input[data-k="ref_number"] { min-width: 150px; }
td.amount input { min-width: 92px; }
td textarea[data-k="description"] { min-width: 220px; }
td select[data-k="doc_type"] { min-width: 132px; }
td input[data-k="source_file"] { min-width: 180px; }
td input[data-k="account"] { min-width: 150px; }
td input[data-k="name"] { min-width: 220px; }
td input[data-k="page"] { min-width: 44px; max-width: 56px; text-align: center; }

#resultsTable tbody tr { cursor: pointer; }
#resultsTable tbody tr.selected td { box-shadow: inset 0 0 0 9999px rgba(45,127,249,.06); }
#resultsTable tbody tr.selected td.conf { box-shadow: none; }

@media (max-width: 900px) {
  .review-grid { flex-direction: column; }
  .doc-pane { position: static; flex: none !important; width: 100%; height: 60vh; }
  .splitter { display: none; }
}

/* table */
.table-wrap { overflow-x: auto; margin-top: 14px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { border: 1px solid var(--line); padding: 7px 9px; text-align: left; vertical-align: top; }
th { background: #f2f5fa; font-weight: 600; white-space: nowrap; }
td input, td textarea, td select {
  width: 100%; border: none; background: transparent; font: inherit; color: inherit; resize: vertical;
}
td textarea { overflow: hidden; line-height: 1.35; min-height: 22px; }
td textarea:focus { overflow: auto; }
td input:focus, td textarea:focus, td select:focus { outline: 2px solid var(--accent); border-radius: 4px; }
td.amount input { text-align: right; }
tr.conf-high   td.conf { background: var(--hi); color: var(--hi-b); }
tr.conf-medium td.conf { background: var(--mid); color: var(--mid-b); }
tr.conf-low    td.conf { background: var(--lo); color: var(--lo-b); }
td.conf { text-align: center; font-size: 12px; font-weight: 600; text-transform: uppercase; white-space: nowrap; }
.rm-row { color: var(--lo-b); border: none; background: none; cursor: pointer; font-size: 16px; }
.row-error td { background: #fdeceb; color: var(--lo-b); }

/* mode selector */
.mode-row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin: 8px 0; }
.mode-row label { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }

/* company selector on extract page */
.company-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 4px 0 14px; }
.company-row select { padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; min-width: 240px; }
.link.small { font-size: 13px; }

/* buyer/company mismatch */
.warn-banner { background: var(--mid); color: var(--mid-b); border: 1px solid #f0d8a0; padding: 9px 12px; border-radius: 8px; font-size: 13px; margin: 10px 0; }
td.buyer { position: relative; }
td.buyer .warn-flag { position: absolute; left: 2px; top: 6px; color: var(--lo-b); font-size: 12px; }
td.buyer input { padding-left: 14px; }
tr.row-warn td { background: #fff6f5; }
tr.row-warn td.conf { background: var(--lo); }

/* history filter */
.history-filter { display: flex; gap: 10px; flex-wrap: wrap; margin: 10px 0 16px; }
.history-filter select, .history-filter input { padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.history-filter input[name="q"] { flex: 1; min-width: 200px; }
.suppliers-cell { max-width: 360px; font-size: 13px; color: var(--muted); }

/* account + users forms */
.fld { display: block; margin: 14px 0 6px; font-size: 13px; font-weight: 600; }
.fld input { width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; margin-top: 4px; }
.user-form { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.user-form input, .user-form select { padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.user-form input[name="username"], .user-form input[name="name"], .user-form input[name="password"] { flex: 1; min-width: 150px; }
.inline-form { display: flex; gap: 6px; }
.inline-form input { padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: 13px; }
.btn.danger { color: var(--lo-b); border-color: #f0c4c2; }
.btn.danger:hover { background: var(--lo); }

/* login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 32px; width: 340px; box-shadow: 0 6px 24px rgba(20,40,70,.10);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card label { display: block; margin: 16px 0 6px; font-size: 13px; font-weight: 600; }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px;
}
.login-card .btn { width: 100%; margin-top: 20px; }
.alert { background: var(--lo); color: var(--lo-b); padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-top: 12px; }
