:root {
  --bg: #0f1419;
  --panel: #1a222c;
  --panel-2: #222c38;
  --text: #e8eef4;
  --muted: #8b9aab;
  --accent: #3d9cf0;
  --ok: #3ecf8e;
  --warn: #e6b84d;
  --danger: #e85d5d;
  --line: rgba(255, 255, 255, 0.08);
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --display: "IBM Plex Serif", Georgia, serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(ellipse 80% 50% at 15% 0%, #1a3050 0%, transparent 55%),
    radial-gradient(ellipse 55% 40% at 95% 100%, #122018 0%, transparent 50%),
    var(--bg);
}

body.dash-body {
  height: 100vh;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap {
  width: min(1120px, calc(100% - 2.5rem));
  margin: 0 auto;
  padding: 2rem 0 3.5rem;
}

.dash-wrap {
  height: 100vh;
  padding: 1.25rem 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  overflow: hidden;
}

.dash-actions { margin: 0; flex-shrink: 0; }

.needs-section {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.ready-section {
  flex: 0 0 auto;
  max-height: 22vh;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.ready-section .section-sub { margin-bottom: 0.5rem; }

.table-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  overflow: hidden;
}

.table-sticky {
  flex-shrink: 0;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 2;
}

.table-sticky-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem 0.65rem;
}

.table-sticky-head .section-title { margin: 0 0 0.2rem; }
.table-sticky-head .section-sub { margin: 0; }

.table-sticky .needs-table {
  margin: 0;
}

.table-sticky thead th {
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  vertical-align: bottom;
  padding: 0.45rem 0.75rem 0.65rem;
}

.th-sort {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  width: 100%;
  margin: 0 0 0.4rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 560;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
  text-align: left;
}

.th-sort:hover { color: var(--text); }

.sort-ind {
  font-size: 0.7rem;
  opacity: 0.55;
  min-width: 0.85rem;
}

.th-sort[data-dir="asc"] .sort-ind,
.th-sort[data-dir="desc"] .sort-ind {
  opacity: 1;
  color: var(--accent);
}

.th-sort[data-dir="asc"] .sort-ind::before { content: "▲"; }
.th-sort[data-dir="desc"] .sort-ind::before { content: "▼"; }
.th-sort:not([data-dir]) .sort-ind::before { content: "↕"; }

.th-filter {
  width: 100%;
  margin: 0 !important;
  padding: 0.4rem 0.5rem !important;
  font-size: 0.82rem !important;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
}

#unmatched-scroll {
  flex: 1;
  min-height: 0;
  border: 0;
  border-radius: 0;
}

.ready-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
}


.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
}

.login-panel {
  width: min(26rem, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.75rem 1.5rem;
}

.brand {
  font-family: var(--display);
  font-size: 1.65rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 0.35rem;
}

.sub {
  color: var(--muted);
  margin: 0 0 1.5rem;
  line-height: 1.45;
  font-size: 0.95rem;
}

label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0 0 0.35rem;
}

input[type="text"],
input[type="password"],
input[type="search"] {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 5px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  margin-bottom: 1rem;
}

input:focus {
  outline: 1px solid var(--accent);
  border-color: transparent;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 0;
  border-radius: 5px;
  padding: 0.65rem 1rem;
  font: inherit;
  font-weight: 560;
  cursor: pointer;
  background: var(--accent);
  color: #061018;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
}

.btn-ok { background: var(--ok); color: #062016; }
.btn-warn { background: var(--warn); color: #1a1405; }
.btn-block { width: 100%; }

.error {
  background: rgba(232, 93, 93, 0.12);
  border: 1px solid rgba(232, 93, 93, 0.35);
  color: #ffb4b4;
  padding: 0.65rem 0.75rem;
  border-radius: 5px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.flash {
  display: none;
  margin: 0;
  padding: 0.7rem 0.85rem;
  border-radius: 5px;
  font-size: 0.9rem;
  flex-shrink: 0;
}
.flash.show { display: block; }
.flash.ok {
  background: rgba(62, 207, 142, 0.12);
  border: 1px solid rgba(62, 207, 142, 0.35);
  color: #9affee;
}
.flash.err {
  background: rgba(232, 93, 93, 0.12);
  border: 1px solid rgba(232, 93, 93, 0.35);
  color: #ffb4b4;
}

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0;
  flex-shrink: 0;
}

.top .user {
  color: var(--muted);
  font-size: 0.9rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 0;
  flex-shrink: 0;
}

@media (max-width: 800px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.9rem 1rem;
}

.stat .label {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat .value {
  font-family: var(--display);
  font-size: 1.7rem;
  margin-top: 0.2rem;
}

.section-title {
  font-family: var(--display);
  font-size: 1.2rem;
  margin: 0 0 0.35rem;
}

.section-sub {
  color: var(--muted);
  margin: 0 0 1rem;
  font-size: 0.92rem;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  table-layout: fixed;
}

.needs-table th:nth-child(1),
.needs-table td:nth-child(1),
.needs-table .col-order { width: 9%; }
.needs-table th:nth-child(2),
.needs-table td:nth-child(2),
.needs-table .col-customer { width: 18%; }
.needs-table th:nth-child(3),
.needs-table td:nth-child(3),
.needs-table .col-email { width: 22%; }
.needs-table th:nth-child(4),
.needs-table td:nth-child(4),
.needs-table .col-date { width: 12%; }
.needs-table th:nth-child(5),
.needs-table td:nth-child(5),
.needs-table .col-total { width: 11%; }
.needs-table th:nth-child(6),
.needs-table td:nth-child(6),
.needs-table .col-match { width: 7.5rem; }
.needs-table th:nth-child(7),
.needs-table td:nth-child(7),
.needs-table .col-push { width: 6.5rem; }

.needs-orders-table .order-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent, #6ea8fe);
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}
.needs-orders-table .order-link:hover {
  text-decoration: underline;
}
.needs-orders-table .cell-match,
.needs-orders-table .cell-push {
  white-space: nowrap;
  vertical-align: middle;
  text-align: center;
}
.needs-orders-table .cell-match .btn,
.needs-orders-table .cell-push .btn {
  width: 100%;
  max-width: 6.5rem;
}

.needs-table th.th-match,
.needs-table th.th-push {
  text-align: center;
  vertical-align: bottom;
  padding-bottom: 0.9rem;
}

.needs-table th.th-match .th-label,
.needs-table th.th-push .th-label {
  display: block;
}

.needs-table tr.needs-orders-row > td {
  width: auto;
}

th, td {
  text-align: left;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

th {
  color: var(--muted);
  font-weight: 560;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

tr:last-child td { border-bottom: 0; }

.muted { color: var(--muted); }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: 0.85em; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 12, 0.72);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 40;
}
.modal-backdrop.open { display: flex; }

.modal {
  width: min(560px, 100%);
  max-height: min(90vh, 720px);
  overflow: auto;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.25rem;
}

.modal h2 {
  font-family: var(--display);
  font-size: 1.25rem;
  margin: 0 0 0.35rem;
}

.modal-match {
  width: min(920px, 100%);
  max-height: min(92vh, 860px);
}

.match-seed {
  margin: 0 0 0.85rem;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  font-size: 0.88rem;
}

.match-results-wrap {
  margin: 0 0 1rem;
  max-height: 320px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 5px;
}

.match-results-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 0.82rem;
}

.match-results-table th,
.match-results-table td {
  padding: 0.45rem 0.5rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  word-break: break-word;
}

.match-results-table th {
  position: sticky;
  top: 0;
  background: var(--panel-2);
  z-index: 1;
}

.match-results-table th:nth-child(1),
.match-results-table td:nth-child(1) { width: 10%; }
.match-results-table th:nth-child(2),
.match-results-table td:nth-child(2) { width: 22%; }
.match-results-table th:nth-child(3),
.match-results-table td:nth-child(3) { width: 26%; }
.match-results-table th:nth-child(4),
.match-results-table td:nth-child(4) { width: 16%; }
.match-results-table th:nth-child(5),
.match-results-table td:nth-child(5) { width: 26%; }

.match-filter-row th {
  padding-top: 0.25rem;
  vertical-align: middle;
}

.match-col-filter {
  width: 100%;
  min-width: 0;
  font-size: 0.75rem;
  padding: 0.28rem 0.35rem;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}

.match-fm { font-weight: 560; }
.match-shopify {
  margin-top: 0.12rem;
  font-size: 0.72rem;
  line-height: 1.25;
}

.match-results-table tr.match-row {
  cursor: pointer;
}

.match-results-table tr.match-row:hover,
.match-results-table tr.match-row.selected {
  background: rgba(61, 156, 240, 0.12);
}

.match-score { font-weight: 650; }

.match-tag {
  margin-top: 0.15rem;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.match-exact .match-tag { color: #6dcf8a; }
.match-fuzzy .match-tag { color: #e0b35a; }
.match-no .match-tag { color: #d08080; }

.search-results {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  max-height: 220px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 5px;
}

.search-results li {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.search-results li:last-child { border-bottom: 0; }
.search-results li:hover,
.search-results li.selected {
  background: rgba(61, 156, 240, 0.12);
}

.empty {
  color: var(--muted);
  padding: 1.25rem;
  text-align: center;
}

.customer-name {
  font-weight: 560;
}

.last-order-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
}

.last-order-date {
  min-width: 0;
}

.order-badge {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 0.75rem;
  line-height: 1.2;
  white-space: nowrap;
}

.orders-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  margin-top: 0.4rem;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 0.82rem;
  padding: 0;
  cursor: pointer;
  user-select: none;
}

.orders-toggle::before {
  content: "▸ ";
  color: var(--muted);
}

.orders-toggle[aria-expanded="true"]::before {
  content: "▾ ";
}

.needs-orders-row > td.needs-orders-cell {
  padding: 0.35rem 0.9rem 0.9rem;
  background: rgba(0, 0, 0, 0.12);
  border-bottom: 1px solid var(--line);
}

.needs-main-row > td {
  border-bottom: 0;
}

.order-grid {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 0.85rem;
  background: rgba(0, 0, 0, 0.18);
}

.order-grid th,
.order-grid td {
  padding: 0.45rem 0.65rem;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.order-grid th {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.03);
}

.order-grid th:nth-child(1),
.order-grid td:nth-child(1) { width: 28%; }
.order-grid th:nth-child(2),
.order-grid td:nth-child(2) { width: 28%; }
.order-grid th:nth-child(3),
.order-grid td:nth-child(3) { width: 28%; }
.order-grid th:nth-child(4),
.order-grid td:nth-child(4) { width: 16%; }

.order-grid tr:last-child td { border-bottom: 0; }

.order-grid .order-link {
  display: inline;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-family: ui-monospace, Menlo, monospace;
  padding: 0;
  cursor: pointer;
  text-align: left;
}

.order-grid .order-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

.order-grid-actions {
  text-align: right;
}

.row-actions {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
}

.row-actions .btn-match {
  white-space: nowrap;
  min-width: 5.5rem;
}

.btn-push-one {
  padding: 0.35rem 0.75rem !important;
  font-size: 0.78rem !important;
  min-width: 4.5rem;
  white-space: nowrap;
}

.th-date-filter {
  display: grid;
  gap: 0.35rem;
}

.th-filter-select {
  width: 100%;
  appearance: auto;
}

.th-date-custom {
  display: none;
  align-items: center;
  gap: 0.25rem;
}

.th-date-custom.is-visible,
.th-date-custom:not([hidden]) {
  display: flex;
}

.th-date-custom[hidden] {
  display: none !important;
}

.th-filter-date {
  flex: 1;
  min-width: 0;
  font-size: 0.75rem;
  padding: 0.25rem 0.35rem;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}

.modal-invoice {
  max-width: 720px;
  width: min(720px, 96vw);
}

.invoice {
  margin: 0 0 1.1rem;
}

.invoice-meta {
  display: grid;
  grid-template-columns: 1fr 1fr 1.1fr;
  gap: 0.85rem 1rem;
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
}

@media (max-width: 700px) {
  .invoice-meta { grid-template-columns: 1fr; }
}

.invoice-label {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.2rem;
}

.invoice-addr {
  margin: 0;
  white-space: pre-wrap;
  font-family: inherit;
  font-size: 0.88rem;
  line-height: 1.35;
}

.invoice-facts {
  display: grid;
  gap: 0.35rem;
  font-size: 0.88rem;
}

.invoice-facts .invoice-label {
  display: inline;
  margin: 0 0.35rem 0 0;
}

.invoice-lines {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
  font-size: 0.88rem;
}

.invoice-lines th,
.invoice-lines td {
  padding: 0.5rem 0.55rem;
  border-bottom: 1px solid var(--line);
}

.invoice-lines th {
  color: var(--muted);
  font-size: 0.72rem;
}

.invoice-lines .num,
.invoice-totals .mono {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.invoice-totals {
  margin-left: auto;
  width: min(260px, 100%);
  display: grid;
  gap: 0.35rem;
}

.invoice-total-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.invoice-total-row.grand {
  color: var(--text);
  font-weight: 650;
  padding-top: 0.4rem;
  border-top: 1px solid var(--line);
  font-size: 1rem;
}

.invoice-loading {
  margin: 0.5rem 0 0;
  font-size: 0.82rem;
}

.detail-list {
  margin: 0 0 1.25rem;
  padding: 0.85rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.45rem 1rem;
  font-size: 0.9rem;
}

.detail-list dt { color: var(--muted); }
.detail-list dd { margin: 0; font-family: ui-monospace, Menlo, monospace; }
