:root {
  --ink: #22272f;
  --muted: #667085;
  --line: #d9dee7;
  --paper: #f4f6f8;
  --surface: #ffffff;
  --green: #0f6b4f;
  --red: #b42318;
  --amber: #9a6700;
  --focus: #1f7a8c;
  --text-xs: 12px;
  --text-sm: 13px;
  --text-md: 14px;
  --text-lg: 16px;
  --control-h: 36px;
  --row-h: 38px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei", Inter, system-ui, sans-serif;
  font-size: var(--text-md);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

button,
input,
select,
textarea {
  font: inherit;
  font-size: var(--text-sm);
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--control-h);
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  padding: 0 12px;
  font-weight: 650;
  line-height: 1;
  white-space: nowrap;
}

button:hover {
  border-color: var(--focus);
}

button[type="submit"],
#saveConfigBtn {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

input,
select,
textarea {
  width: 100%;
  min-height: var(--control-h);
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 7px 9px;
  line-height: 1.35;
}

select {
  height: var(--control-h);
  padding-top: 0;
  padding-bottom: 0;
}

input:not([type="checkbox"]) {
  height: var(--control-h);
}

textarea {
  resize: vertical;
  height: auto;
}

label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 650;
  line-height: 1.35;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 21px;
  line-height: 1.25;
}

h2 {
  font-size: 15px;
  line-height: 1.3;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  min-height: 68px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
}

#health {
  margin-top: 3px;
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.35;
}

.topbar-actions,
.inline-actions {
  display: flex;
  gap: 8px;
  align-items: end;
}

.account-badge {
  align-self: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #eef7f4;
  color: var(--green);
  font-size: var(--text-xs);
  font-weight: 700;
  padding: 7px 10px;
  white-space: nowrap;
}

.topbar-actions label {
  min-width: 170px;
}

.app-shell {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: 12px;
  display: grid;
  gap: 12px;
}

.page-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 6px;
}

.page-tab {
  flex: 0 0 auto;
  min-width: 92px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-weight: 700;
  font-size: var(--text-sm);
}

.page-tab.active {
  background: var(--focus);
  border-color: var(--focus);
  color: #fff;
}

.tab-count {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  margin-left: 5px;
  padding: 0 6px;
  border-radius: 999px;
  background: #a36b00;
  color: #fff;
  font-size: var(--text-xs);
  line-height: 1;
}

.page-tab.active .tab-count {
  background: #fff;
  color: #8b5e00;
}

.page-section {
  display: none;
  gap: 12px;
}

.page-section.active {
  display: grid;
}

.auto-status-metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin: 10px 0;
}

.auto-status-metrics > div {
  min-height: 66px;
  display: grid;
  align-content: center;
  gap: 3px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--focus);
  border-radius: 6px;
  background: var(--surface);
}

.auto-status-metrics span {
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 700;
}

.auto-status-metrics strong {
  font-size: 18px;
}

.status-badge {
  display: inline-block;
  min-width: 76px;
  padding: 3px 7px;
  border-radius: 4px;
  background: #eef2f3;
  font-size: var(--text-xs);
  font-weight: 700;
  text-align: center;
}

.status-broadcasted { color: var(--green); background: #eaf6f1; }
.status-ambiguous { color: var(--amber); background: #fff7df; }
.status-failed { color: var(--red); background: #fff0ee; }
.status-matching { color: var(--focus); background: #eaf5f7; }

.overview-panel {
  padding-bottom: 12px;
}

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

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

.stack {
  display: grid;
  gap: 9px;
  margin-top: 10px;
}

.order-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 9px;
  align-items: end;
  margin-top: 10px;
}

.config-grid,
.role-grid,
.query-bar {
  display: grid;
  gap: 9px;
  align-items: end;
  margin-top: 10px;
}

.config-grid {
  grid-template-columns: repeat(3, minmax(150px, 1fr));
}

.role-grid,
.query-bar {
  grid-template-columns: minmax(240px, 1fr) minmax(150px, 220px) auto;
}

.whitelist-box {
  display: grid;
  gap: 8px;
  margin: 8px 0 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.whitelist-box p {
  margin: 0;
  color: var(--muted);
  font-size: var(--text-sm);
}

.merge-box {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.danger-box {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  padding: 12px;
  border: 1px solid rgba(180, 35, 24, 0.28);
  border-radius: 8px;
  background: #fff8f7;
}

.merge-box h3,
.danger-box h3 {
  margin: 0;
  font-size: 15px;
  line-height: 1.3;
}

.danger-box button.danger {
  justify-self: start;
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check-row input {
  width: auto;
}

.broadcast-setting .check-row {
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
  color: var(--text);
}

.role-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

button.ghost {
  padding: 6px 8px;
  background: transparent;
}

.management-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(300px, 1fr);
  gap: 12px;
  align-items: start;
}

.span-2 {
  grid-column: span 2;
}

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

.sub-section-head {
  margin-top: 14px;
}

.hint {
  color: var(--muted);
  font-size: var(--text-xs);
}

.muted {
  color: var(--muted);
  font-size: var(--text-xs);
}

.field-help {
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.45;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(15, 23, 42, 0.42);
}

.modal[hidden] {
  display: none;
}

.modal-card {
  width: min(920px, 100%);
  max-height: min(86vh, 780px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 16px;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.24);
}

.edit-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(150px, 1fr));
  gap: 9px;
  align-items: end;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.hash-review-modal {
  width: min(620px, 100%);
}

.order-note-modal {
  width: min(600px, 100%);
}

.order-note-content {
  min-height: 110px;
  max-height: 52vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafb;
  padding: 14px;
  color: var(--text);
  font-size: var(--text-sm);
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-view-button {
  width: 44px;
}

.hash-review-grid {
  display: grid;
  gap: 12px;
}

.hash-check-row {
  display: flex;
  min-height: 32px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7f9f8;
}

.hash-check-row .field-help {
  margin: 0;
}

.hash-short {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: var(--text-xs);
}

.segmented {
  display: inline-grid;
  grid-template-columns: repeat(3, 40px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.segmented button {
  border: 0;
  border-radius: 0;
}

.segmented .active {
  background: var(--focus);
  color: #fff;
}

.report-date-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(132px, 1fr)) auto;
  gap: 8px;
  align-items: end;
}

.report-date-controls label {
  min-width: 132px;
}

.report-date-controls button {
  background: var(--focus);
  border-color: var(--focus);
  color: #fff;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(7, minmax(120px, 1fr));
  gap: 8px;
}

.metrics div,
.mini-metrics div {
  border: 1px solid var(--line);
  border-left: 3px solid var(--green);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 8px;
}

.metric-primary {
  border-left-color: var(--focus) !important;
}

.metric-alert {
  border-left-color: var(--amber) !important;
}

.metrics span,
.mini-metrics span {
  display: block;
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.3;
}

.metrics strong,
.mini-metrics strong {
  display: block;
  margin-top: 2px;
  font-size: var(--text-lg);
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
}

.mini-metrics {
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

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

table {
  width: 100%;
  min-width: 900px;
  border-collapse: collapse;
  background: #fff;
  table-layout: auto;
  font-variant-numeric: tabular-nums;
}

.compact-table table {
  min-width: 680px;
}

.summary-table-wrap {
  width: fit-content;
  max-width: 100%;
}

.ledger-table {
  min-width: 1280px;
}

.order-detail-table,
.member-history-table {
  min-width: 1040px;
}

.channel-table {
  table-layout: fixed;
}

.staff-performance-table {
  width: 940px;
  min-width: 940px;
  table-layout: fixed;
}

.agent-performance-table {
  width: 880px;
  min-width: 880px;
  table-layout: fixed;
}

.channel-table th:first-child,
.channel-table td:first-child {
  width: 28%;
}

.staff-performance-table th:nth-child(1),
.staff-performance-table td:nth-child(1) {
  width: 72px;
}

.staff-performance-table th:nth-child(2),
.staff-performance-table td:nth-child(2) {
  width: 116px;
}

.staff-performance-table th:nth-child(3),
.staff-performance-table td:nth-child(3),
.agent-performance-table th:nth-child(2),
.agent-performance-table td:nth-child(2) {
  width: 64px;
  text-align: right;
}

.staff-performance-table th:nth-child(n+4),
.staff-performance-table td:nth-child(n+4),
.agent-performance-table th:nth-child(n+3),
.agent-performance-table td:nth-child(n+3) {
  width: 112px;
}

.agent-performance-table th:first-child,
.agent-performance-table td:first-child {
  width: 140px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  height: var(--row-h);
  padding: 8px 10px;
  text-align: left;
  white-space: nowrap;
  vertical-align: middle;
  font-size: var(--text-sm);
  line-height: 1.35;
}

th {
  background: #eef2f3;
  color: #344054;
  font-weight: 750;
  letter-spacing: 0;
}

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

tbody tr:nth-child(even) td {
  background: #fcfdfe;
}

tbody tr:hover td {
  background: #f6faf9;
}

table button {
  min-height: 28px;
  height: 28px;
  padding: 0 8px;
  font-size: var(--text-xs);
  line-height: 1;
}

.ledger-table th:nth-child(9),
.ledger-table th:nth-child(10),
.ledger-table td:nth-child(9),
.ledger-table td:nth-child(10),
.channel-table th:nth-child(n+2),
.channel-table td:nth-child(n+2),
.staff-performance-table th:nth-child(n+3),
.staff-performance-table td:nth-child(n+3),
.agent-performance-table th:nth-child(n+2),
.agent-performance-table td:nth-child(n+2),
.order-detail-table th:nth-child(9),
.order-detail-table th:nth-child(10),
.order-detail-table td:nth-child(9),
.order-detail-table td:nth-child(10),
.member-history-table th:nth-child(7),
.member-history-table th:nth-child(8),
.member-history-table td:nth-child(7),
.member-history-table td:nth-child(8),
#orderRows td:nth-child(9),
#orderRows td:nth-child(10),
#channelRows td:nth-child(n+2),
#staffPerformanceRows td:nth-child(n+3),
#agentPerformanceRows td:nth-child(n+2),
#staffPerformanceOrderRows td:nth-child(9),
#staffPerformanceOrderRows td:nth-child(10),
#memberOrderSearchRows td:nth-child(9),
#memberOrderSearchRows td:nth-child(10),
#memberHistoryRows td:nth-child(7),
#memberHistoryRows td:nth-child(8) {
  text-align: right;
}

.ledger-table th:nth-child(1),
.ledger-table td:nth-child(1),
.order-detail-table th:nth-child(1),
.order-detail-table td:nth-child(1),
.member-history-table th:nth-child(1),
.member-history-table td:nth-child(1),
#orderRows td:nth-child(1),
#staffPerformanceOrderRows td:nth-child(1),
#memberOrderSearchRows td:nth-child(1),
#memberHistoryRows td:nth-child(1) {
  font-family: Consolas, "SFMono-Regular", "Microsoft YaHei UI", monospace;
  font-size: 12px;
  color: #344054;
}

.list {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  color: var(--muted);
  font-size: var(--text-sm);
  line-height: 1.4;
}

.list button {
  text-align: left;
}

.member-list {
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  align-items: stretch;
}

.selected-member-line {
  display: flex;
  align-items: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--green);
  border-radius: 6px;
  background: #fbfcfd;
  color: var(--ink);
  padding: 0 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.member-list button {
  justify-content: flex-start;
  width: 100%;
  min-height: 38px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.status-active {
  color: var(--green);
  font-weight: 700;
}

.status-cancelled,
.danger,
.login-error {
  color: var(--red);
}

.pending-methods {
  margin-top: 8px;
  color: var(--amber);
  font-size: var(--text-sm);
}

.member-history {
  margin-top: 10px;
}

.performance-summary {
  margin-top: 10px;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-left: 4px solid var(--green);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 12px 30px rgba(32, 33, 36, 0.12);
  padding: 12px 14px;
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-shell {
  width: min(420px, 100%);
}

.login-card {
  display: grid;
  gap: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 24px;
  box-shadow: 0 18px 42px rgba(32, 33, 36, 0.08);
}

.login-card p {
  margin-top: 6px;
  color: var(--muted);
  font-size: var(--text-sm);
}

.totp-setup {
  display: grid;
  gap: 16px;
}

.totp-setup[hidden] {
  display: none;
}

.totp-setup h2 {
  margin: 0 0 6px;
  font-size: 18px;
}

.totp-setup p {
  margin: 0;
  line-height: 1.6;
}

.totp-setup img {
  width: 220px;
  height: 220px;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.totp-secret {
  display: grid;
  gap: 6px;
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7f9f8;
}

.totp-secret span {
  color: var(--muted);
  font-size: var(--text-sm);
}

.totp-secret strong {
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 14px;
}

@media (max-width: 1080px) {
  .order-grid,
  .config-grid,
  .edit-grid,
  .metrics,
  .mini-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .management-grid {
    grid-template-columns: 1fr;
  }

  .role-grid,
  .query-bar {
    grid-template-columns: minmax(0, 1fr) minmax(150px, 220px) auto;
  }
}

@media (max-width: 680px) {
  .topbar,
  .topbar-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar-actions label,
  .order-grid,
  .config-grid,
  .edit-grid,
  .role-grid,
  .query-bar,
  .report-date-controls,
  .metrics,
  .mini-metrics {
    grid-template-columns: 1fr;
  }

  .order-grid {
    display: grid;
  }

  .span-2 {
    grid-column: auto;
  }

  .overview-head,
  .section-head,
  .inline-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .page-tab {
    min-width: 84px;
  }
}

@media (max-width: 760px) {
  body {
    background: #eef2f3;
  }

  .topbar {
    position: static;
    padding: 10px;
  }

  .topbar h1 {
    font-size: 18px;
    line-height: 1.25;
  }

  #health {
    line-height: 1.45;
  }

  .app-shell {
    padding: 8px;
    gap: 8px;
  }

  .panel {
    border-radius: 8px;
    padding: 10px;
  }

  .page-tabs {
    position: sticky;
    top: 0;
    z-index: 3;
    border-radius: 0;
    margin: 0 -8px;
    border-left: 0;
    border-right: 0;
  }

  .page-tab {
    min-width: 92px;
    min-height: 38px;
  }

  .metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .auto-status-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .metrics div,
  .mini-metrics div {
    padding: 9px;
  }

  .metrics strong,
  .mini-metrics strong {
    font-size: 16px;
    word-break: break-word;
  }

  .table-wrap {
    border: 0;
    overflow: visible;
    background: transparent;
  }

  .summary-table-wrap {
    width: 100%;
  }

  table,
  .compact-table table {
    min-width: 0;
    background: transparent;
  }

  thead {
    display: none;
  }

  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  tr {
    margin: 0 0 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    overflow: hidden;
  }

  td {
    display: grid;
    grid-template-columns: minmax(78px, 34%) minmax(0, 1fr);
    gap: 8px;
    align-items: start;
    min-height: 40px;
    border-bottom: 1px solid #eef2f3;
    white-space: normal;
    word-break: break-word;
    line-height: 1.45;
    text-align: left !important;
  }

  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: 700;
  }

  td[colspan] {
    display: block;
    text-align: center;
    color: var(--muted);
  }

  td[colspan]::before {
    content: "";
  }

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

  .inline-actions,
  .topbar-actions {
    gap: 6px;
  }
}

.evidence-line {
  margin-top: 5px;
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.45;
  word-break: break-all;
  white-space: normal;
}

.review-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.cell-stack {
  min-width: 0;
  white-space: normal;
}

.mini-action {
  min-height: 30px;
  padding: 5px 9px;
  font-size: var(--text-xs);
}

.mini-action.secondary {
  background: #eef3f1;
  color: var(--ink);
}

.mini-action.force-approve {
  background: #a36b00;
  color: #fff;
}

.auto-recharge-table {
  min-width: 1320px;
}

.status-manual_review {
  color: #8b5e00;
  background: #fff3cd;
}

@media (max-width: 760px) {
  .auto-recharge-table {
    min-width: 0;
  }

  .auto-recharge-table td {
    height: auto;
    white-space: normal;
  }
}
