:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #d9dee7;
  --text: #17202a;
  --muted: #667085;
  --primary: #216fed;
  --primary-dark: #1756bc;
  --success: #0f8a5f;
  --danger: #c9342f;
  --warning: #a15c00;
  --sidebar: #142033;
  --sidebar-active: #263a59;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  font-size: 14px;
}

a {
  color: var(--primary);
  text-decoration: none;
}

.merchant-tabs {
  display: flex;
  gap: 24px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 18px;
}

.merchant-tabs a {
  padding: 12px 0;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

.merchant-tabs a.selected {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.layout {
  display: grid;
  grid-template-columns: 230px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--sidebar);
  color: #fff;
  padding: 18px 14px;
}

.brand {
  font-size: 18px;
  font-weight: 700;
  padding: 10px 12px 20px;
}

.nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #d7ddea;
  padding: 11px 12px;
  border-radius: 8px;
  margin-bottom: 4px;
}

.nav a.active,
.nav a:hover {
  background: var(--sidebar-active);
  color: #fff;
}

.main {
  min-width: 0;
}

.topbar {
  min-height: 58px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.content {
  padding: 22px 24px 48px;
}

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

.status-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

h1,
h2,
h3 {
  margin: 0;
}

h1 {
  font-size: 24px;
}

h2 {
  font-size: 18px;
  margin-bottom: 12px;
}

.muted {
  color: var(--muted);
}

.grid {
  display: grid;
  gap: 14px;
}

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

.broadcast-grid {
  grid-template-columns: repeat(3, minmax(280px, 1fr));
  align-items: start;
}

.broadcast-grid form {
  display: grid;
  gap: 12px;
}

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

.stat-value {
  font-size: 28px;
  font-weight: 700;
  margin-top: 8px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}

.batch-toolbar {
  margin-top: -4px;
}

.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  width: auto;
  color: var(--text);
}

.region-picker {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 38px;
}

.region-picker .checkbox-inline {
  min-height: 34px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
}

input,
select,
textarea,
button {
  font: inherit;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 9px 10px;
  background: #fff;
  color: var(--text);
}

input[type="checkbox"] {
  width: auto;
  min-width: 16px;
  height: 16px;
  padding: 0;
}

textarea {
  min-height: 120px;
  resize: vertical;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

.form-hint {
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

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

.form-grid .full {
  grid-column: 1 / -1;
}

.btn {
  border: 0;
  border-radius: 7px;
  padding: 9px 13px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
}

.btn:hover {
  background: var(--primary-dark);
}

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

.btn[disabled]:hover {
  background: var(--primary);
}

.btn.secondary {
  background: #eef2f7;
  color: var(--text);
}

.btn.danger {
  background: var(--danger);
}

.btn.small {
  padding: 6px 9px;
  min-height: 30px;
  font-size: 12px;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 920px;
}

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

.check-cell {
  width: 42px;
  text-align: center;
}

th {
  background: #f7f9fc;
  color: #48556a;
  font-weight: 600;
}

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

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  color: #354052;
  background: #eef2f7;
  white-space: nowrap;
}

.badge.sent,
.badge.resent,
.badge.reserve_sent,
.badge.bound,
.badge.ok {
  background: #e7f7ef;
  color: var(--success);
}

.badge.failed,
.badge.invalid,
.badge.disabled {
  background: #fdecec;
  color: var(--danger);
}

.badge.recall_failed {
  background: #fdecec;
  color: var(--danger);
}

.recall-dialog {
  width: min(540px, calc(100vw - 32px));
  max-height: 85vh;
  overflow: auto;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
}

.recall-title > div {
  min-width: 0;
  overflow-wrap: anywhere;
}

.recall-title > a {
  flex-shrink: 0;
  white-space: nowrap;
}

.recall-dialog::backdrop {
  background: rgba(15, 23, 42, 0.42);
}

.recall-dialog .toolbar {
  margin-top: 20px;
  margin-bottom: 0;
}

.recall-progress {
  margin-bottom: 18px;
}

.recall-progress [hidden] {
  display: none;
}

.recall-progress progress {
  width: 100%;
  height: 10px;
  accent-color: var(--primary);
}

.badge.unbound,
.badge.pending,
.badge.reserve_available,
.badge.reserve_locked,
.badge.sending {
  background: #fff3df;
  color: var(--warning);
}

.alert {
  background: #eaf3ff;
  color: #12498f;
  border: 1px solid #bdd7ff;
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 14px;
}

.runner-alert {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
}

.runner-alert[hidden] {
  display: none;
}

.progress {
  width: 150px;
  max-width: 100%;
  height: 8px;
  border-radius: 999px;
  background: #e8edf5;
  overflow: hidden;
  margin-bottom: 6px;
}

.progress div {
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 0.2s ease;
}

.progress-text {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.queue-actions {
  white-space: nowrap;
}

.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
  gap: 16px;
}

.inline-form {
  display: inline;
}

.batch-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.pagination {
  margin-top: 12px;
  color: var(--muted);
  display: flex;
  gap: 10px;
  align-items: center;
}

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

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

.template-editor {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.message-cell {
  max-width: 360px;
  white-space: pre-wrap;
  word-break: break-word;
}

.manual-cell {
  min-width: 220px;
}

.manual-form {
  display: grid;
  gap: 6px;
}

body.modal-open {
  overflow: hidden;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.42);
  z-index: 40;
}

.modal-backdrop[hidden],
.modal-panel[hidden] {
  display: none;
}

.modal-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 41;
  width: min(960px, calc(100vw - 48px));
  max-height: min(78vh, 760px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 70px rgba(15, 23, 42, 0.28);
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.modal-header {
  min-height: 70px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.modal-body {
  overflow-y: auto;
  padding: 14px;
  display: grid;
  align-content: start;
  gap: 10px;
}

.modal-empty {
  padding: 18px 20px;
  border-top: 1px solid var(--line);
}

.modal-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  gap: 12px;
}

.modal-select {
  margin-bottom: 8px;
}

.modal-item-title {
  font-weight: 700;
  margin-bottom: 5px;
}

.modal-meta,
.modal-note {
  color: var(--muted);
  font-size: 12px;
}

.modal-content {
  margin-top: 8px;
  max-height: 150px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: #27364a;
  background: #f8fafc;
  border: 1px solid #e7ebf2;
  border-radius: 7px;
  padding: 8px;
}

.modal-form {
  align-self: start;
}

.modal-item-actions {
  align-self: start;
  display: grid;
  gap: 8px;
}

.modal-delete-form .btn {
  width: 100%;
}

.modal-panel textarea[name="orders_text"] {
  min-height: 260px;
}

.modal-panel textarea[name="order_link"] {
  min-height: 280px;
}

@media (max-width: 920px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidebar {
    position: static;
    min-width: 0;
  }

  .nav {
    display: flex;
    overflow-x: auto;
    gap: 6px;
  }

  .nav a {
    white-space: nowrap;
  }

  .stats,
  .broadcast-grid,
  .form-grid,
  .split {
    grid-template-columns: 1fr;
  }

  .form-grid .wide,
  .form-grid .full {
    grid-column: auto;
  }

  .topbar {
    padding: 0 14px;
  }

  .content {
    padding: 16px;
  }

  .modal-panel {
    width: calc(100vw - 24px);
    max-height: calc(100vh - 36px);
  }

  .modal-item {
    grid-template-columns: 1fr;
  }
}
