:root {
  --ink: #16202a;
  --muted: #64717d;
  --line: #d8dee4;
  --paper: #f4f6f8;
  --panel: #ffffff;
  --brand: #16324f;
  --brand-light: #2a5c86;
  --up: #b42318;
  --up-bg: #fee4e2;
  --down: #067647;
  --down-bg: #dcfae6;
  --pending: #8a5a00;
  --pending-bg: #fff3c4;
  --ok: #067647;
  --bad: #b42318;
  --radius: 12px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.05rem; }
p { margin: 0; }
button, input { font: inherit; }

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: env(safe-area-inset-top, 0) 16px 14px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  padding-right: 148px;
  background: var(--brand);
  color: #fff;
}

.topbar .muted { color: #c9d7e3; font-size: .8rem; margin-top: 4px; }

.lang-switch {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top, 0px));
  right: 10px;
  z-index: 70;
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid rgba(22, 32, 42, .18);
  border-radius: 999px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 3px 12px rgba(0, 0, 0, .18);
}

.lang-switch button {
  min-width: 40px;
  min-height: 30px;
  padding: 3px 8px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--brand);
  font-size: .76rem;
  font-weight: 700;
  cursor: pointer;
}

.lang-switch button.active { background: var(--brand); color: #fff; }
.lang-switch button:focus-visible { outline: 2px solid var(--brand-light); outline-offset: 1px; }

main {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 14px;
  display: grid;
  gap: 14px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.muted { color: var(--muted); }
.small { font-size: .8rem; }
.error { color: var(--bad); min-height: 1.4em; }

.upload-panel h2, .section-head h2 { margin-bottom: 4px; }
.upload-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ghost { background: transparent; color: inherit; }
.btn.small { min-height: 34px; padding: 5px 10px; font-size: .82rem; }
.topbar .btn.ghost { color: #fff; border-color: #6f8ca5; }
.file-btn { position: relative; overflow: hidden; }

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 24, 38, .78);
}

.auth-card {
  width: min(360px, 100%);
  background: #fff;
  border-radius: 14px;
  padding: 22px;
  display: grid;
  gap: 10px;
}

.auth-card h1 { color: var(--brand); }
.auth-card input { min-height: 46px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; }

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.job-list { display: grid; gap: 8px; }

.job-item {
  width: 100%;
  text-align: left;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  cursor: pointer;
}

.job-item:hover { border-color: var(--brand-light); background: #f8fbfd; }
.job-item strong { display: block; font-size: .92rem; word-break: break-all; }
.job-item small { color: var(--muted); }

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: .75rem;
  font-weight: 700;
  white-space: nowrap;
  background: #e8edf2;
  color: #334;
}

.badge.needs_review, .badge.extracted, .badge.matched { background: var(--pending-bg); color: var(--pending); }
.badge.approved, .badge.exported { background: var(--down-bg); color: var(--down); }
.badge.failed, .badge.skip_outgoing { background: var(--up-bg); color: var(--up); }
.badge.uploaded { background: #e8edf2; color: #445; }

.checks { display: grid; gap: 10px; margin: 11px 0; }

.check-card {
  border: 1px solid var(--line);
  border-left: 5px solid var(--muted);
  border-radius: 9px;
  padding: 10px 12px;
  background: #fbfcfd;
}

.check-card.ok { border-left-color: var(--ok); }
.check-card.bad { border-left-color: var(--bad); }
.check-card.warn { border-left-color: #e0a400; }
.check-card h3 { margin: 0 0 4px; font-size: .92rem; }
.check-card p { font-size: .85rem; }
.check-list { margin: 6px 0 0; padding-left: 18px; font-size: .8rem; color: var(--muted); }

.warnings { display: grid; gap: 6px; margin-bottom: 10px; }
.warning {
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--pending-bg);
  color: var(--pending);
  font-size: .85rem;
}

.review-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-items: center;
  margin: 12px 0;
}

.review-actions label { grid-column: 1 / -1; font-size: .82rem; color: var(--muted); }
.review-actions input {
  grid-column: 1 / -1;
  min-height: 44px;
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.review-actions #approve, .review-actions #download-xls { grid-column: span 1; }
.review-actions #show-payload { grid-column: 1 / -1; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 9px; }

.review-table { width: 100%; min-width: 760px; border-collapse: collapse; font-size: .84rem; }
.review-table th, .review-table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
.review-table th { background: #eef2f5; font-size: .76rem; color: #445; white-space: nowrap; }
.review-table tr:last-child td { border-bottom: none; }
.review-table td.desc { min-width: 180px; white-space: pre-wrap; }
.review-table input { width: 100%; min-width: 72px; padding: 6px; border: 1px solid #c6ced6; border-radius: 6px; background: #fff; }
.review-table input.code { min-width: 96px; font-family: ui-monospace, Consolas, monospace; }
.review-table input.qty { max-width: 80px; }
.review-table input.cost { max-width: 92px; }
.item-name { color: var(--muted); font-size: .76rem; display: block; margin-top: 3px; }

.status-pill {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: .72rem;
  font-weight: 700;
  background: #edf0f3;
}
.status-pill.up { background: var(--up-bg); color: var(--up); }
.status-pill.down { background: var(--down-bg); color: var(--down); }
.status-pill.pending, .status-pill.low { background: var(--pending-bg); color: var(--pending); }
.diff.up { color: var(--up); font-weight: 700; }
.diff.down { color: var(--down); font-weight: 700; }
tr.row-pending td, tr.row-low td { background: #fffbea; }

.status-line { min-height: 1.4em; margin-top: 8px; font-size: .82rem; color: var(--muted); }
.footer { max-width: 1100px; margin: 0 auto; padding: 0 16px 32px; font-size: .78rem; }
.payload-box {
  margin-top: 12px;
  max-height: 360px;
  overflow: auto;
  padding: 10px;
  border-radius: 8px;
  background: #101820;
  color: #d8e4ef;
  font-size: .75rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100% - 30px);
  padding: 10px 16px;
  border-radius: 999px;
  background: #102033;
  color: #fff;
  font-size: .85rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}

.tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.tab {
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 9px 9px 0 0;
  background: transparent;
  padding: 10px 18px;
  min-height: 42px;
  font: inherit;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
}

.tab.active {
  background: var(--panel);
  border-color: var(--line);
  color: var(--brand);
}

.memory-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  font-size: .84rem;
}

.memory-filters select,
.memory-filters input {
  min-height: 38px;
  padding: 6px 9px;
  border: 1px solid #c6ced6;
  border-radius: 7px;
  background: #fff;
  font: inherit;
}

.memory-table { width: 100%; min-width: 900px; border-collapse: collapse; font-size: .82rem; }
.memory-table th, .memory-table td { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.memory-table th { background: #eef2f5; font-size: .76rem; color: #445; white-space: nowrap; }
.memory-table tr:last-child td { border-bottom: none; }
.memory-table td.desc { min-width: 220px; white-space: pre-wrap; }
.memory-table code { font-family: ui-monospace, Consolas, monospace; font-size: .82rem; }

.candidate-options {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 6px;
}

.candidate-hint { color: var(--muted); font-size: .72rem; }

.candidate-option {
  text-align: left;
  border: 1px dashed #9aa9b7;
  border-radius: 6px;
  background: #f7f9fb;
  padding: 4px 6px;
  font: inherit;
  font-size: .72rem;
  cursor: pointer;
}

.candidate-option:hover { border-color: var(--brand); background: #eaf2f8; }

@media (min-width: 700px) {
  body { font-size: 16px; }
  .topbar { padding-left: 28px; padding-right: 170px; }
  h1 { font-size: 1.45rem; }
  main { padding: 20px; gap: 18px; }
  .panel { padding: 18px; }
  .checks { grid-template-columns: 1fr 1fr; }
  .review-actions { grid-template-columns: 220px auto auto auto; }
  .review-actions label { grid-column: 1 / -1; }
  .review-actions input { grid-column: auto; }
  .review-actions #approve, .review-actions #download-xls, .review-actions #show-payload { grid-column: auto; }
}
