:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --border: #e2e5ec;
  --text: #1a1d23;
  --text-secondary: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --yellow: #ca8a04;
  --yellow-bg: #fef9c3;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --blue-bg: #dbeafe;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.12);
  --radius: 8px;
  --radius-lg: 12px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}
#app { max-width: 1480px; margin: 0 auto; padding: 0 24px; }

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 24px;
}
.header-inner { display: flex; align-items: baseline; gap: 12px; }
header h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.5px; }
.subtitle { font-size: 13px; color: var(--text-secondary); }
nav { display: flex; gap: 4px; background: var(--surface); padding: 4px; border-radius: var(--radius); border: 1px solid var(--border); }
.nav-btn {
  padding: 8px 16px;
  border: none;
  background: transparent;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  border-radius: 6px;
  cursor: pointer;
  transition: all .15s;
}
.nav-btn:hover { color: var(--text); background: var(--bg); }
.nav-btn.active { color: var(--primary); background: var(--blue-bg); }

/* Tabs */
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Panel header */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.panel-header h2 { font-size: 18px; font-weight: 600; }
.panel-actions { display: flex; gap: 10px; align-items: center; }
.search-input {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  width: 240px;
  outline: none;
  transition: border-color .15s;
}
.search-input:focus { border-color: var(--primary); }

/* Legend */
.legend {
  display: flex;
  gap: 16px;
  align-items: center;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.dot-active { background: var(--green); }
.dot-expiring { background: var(--yellow); }
.dot-expired { background: var(--red); }

/* Buttons */
.btn {
  padding: 8px 16px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
}
.btn:hover { background: var(--bg); }
.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-danger { color: var(--red); border-color: var(--red); }
.btn-danger:hover { background: var(--red-bg); }
.btn-renewal { color: var(--yellow); border-color: var(--yellow); }
.btn-renewal:hover { background: var(--yellow-bg); }
.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
}
.btn-close:hover { color: var(--text); }

/* Customer cards */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
}
.customer-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  transition: box-shadow .15s;
  position: relative;
}
.customer-card:hover { box-shadow: var(--shadow-lg); }
.customer-card.border-green { border-left: 4px solid var(--green); }
.customer-card.border-yellow { border-left: 4px solid var(--yellow); }
.customer-card.border-red { border-left: 4px solid var(--red); opacity: 0.75; }
.card-top { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; }
.card-email { font-weight: 600; font-size: 14px; word-break: break-all; }
.card-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 99px;
  white-space: nowrap;
}
.badge-active { background: var(--green-bg); color: var(--green); }
.badge-expiring { background: var(--yellow-bg); color: var(--yellow); }
.badge-expired { background: var(--red-bg); color: var(--red); }
.badge-disabled { background: #f3f4f6; color: #6b7280; }
.badge-per-episode { background: #dbeafe; color: #2563eb; }
.customer-card.border-blue { border-left: 4px solid #2563eb; }
.customer-card.border-purple { border-left: 4px solid #9333ea; }
.customer-card.border-orange { border-left: 4px solid #ea580c; }
.card-meta { font-size: 13px; color: var(--text-secondary); display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 10px; }
.card-meta span { display: flex; align-items: center; gap: 4px; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Split layout for episodes */
.split-layout { display: flex; gap: 20px; min-height: 500px; }
.split-left {
  width: 360px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  overflow-y: auto;
  max-height: 70vh;
}
.split-left h3 { font-size: 14px; font-weight: 600; margin-bottom: 12px; }
.split-right { flex: 1; }
.episode-list { display: flex; flex-direction: column; gap: 6px; }
.episode-item {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: all .15s;
}
.episode-item:hover { background: var(--bg); }
.episode-item.active { background: var(--blue-bg); border-color: var(--primary); }
.episode-date { font-size: 12px; color: var(--text-secondary); font-weight: 500; }
.episode-topic { font-size: 13px; font-weight: 500; margin-top: 2px; }
.episode-badges { font-size: 11px; color: var(--text-secondary); margin-top: 4px; }

.episode-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.placeholder-text { color: var(--text-secondary); text-align: center; padding: 80px 0; font-size: 14px; }
.detail-header { margin-bottom: 16px; }
.detail-header h3 { font-size: 16px; font-weight: 600; }
.detail-header .detail-meta { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }
.customer-checklist { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.check-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.check-item input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); }
.check-item .check-email { font-weight: 500; flex: 1; }
.check-item .check-xianyu { color: var(--text-secondary); font-size: 12px; }
.check-item .check-sent { font-size: 11px; color: var(--green); font-weight: 500; }
.send-actions { display: flex; gap: 10px; align-items: center; }
.send-actions .select-info { font-size: 13px; color: var(--text-secondary); }

/* Settings */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 16px;
}
.settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow);
}
.settings-card.wide { grid-column: 1 / -1; }
.settings-card h3 { font-size: 15px; font-weight: 600; margin-bottom: 16px; }
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
  font-family: inherit;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--primary); }
.full-width { width: 100%; }
.mono { font-family: "SF Mono", "Menlo", "Consolas", monospace; font-size: 12px; line-height: 1.6; }
.template-vars { font-size: 12px; color: var(--text-secondary); margin-top: 8px; }
.template-vars code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  margin: 0 2px;
}

/* Template list in settings */
.tpl-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 6px;
}
.tpl-item .tpl-name { font-weight: 500; font-size: 14px; }
.tpl-item .tpl-subject { font-size: 12px; color: var(--text-secondary); }

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.show { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.modal-wide { width: 600px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-body { padding: 20px; }
.modal-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  z-index: 200;
  transition: transform .3s ease;
  pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--red); }

/* Result list */
.result-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.result-icon { font-size: 16px; }
.result-item:last-child { border-bottom: none; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* Responsive */
@media (max-width: 768px) {
  header { flex-direction: column; gap: 12px; align-items: flex-start; }
  .split-layout { flex-direction: column; }
  .split-left { width: 100%; max-height: 300px; }
  .card-grid { grid-template-columns: 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
  .modal, .modal-wide { width: 95%; }
}

/* History */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.history-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  box-shadow: var(--shadow);
}
.history-date {
  font-size: 14px;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 6px;
}
.history-topic {
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 10px;
}
.history-recipients {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-left: 12px;
  border-left: 2px solid var(--border);
}
.history-recipient {
  font-size: 13px;
  padding: 4px 0;
}

/* Operation Logs */
.logs-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.log-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.log-time {
  color: var(--text-secondary);
  font-size: 12px;
  white-space: nowrap;
  min-width: 150px;
}
.log-action {
  font-weight: 500;
  min-width: 100px;
  color: var(--primary);
}
.log-detail {
  color: var(--text-secondary);
  flex: 1;
}

/* Redemption Code Status Badges */
.status-used { background: #f3f4f6; color: #6b7280; }
.status-unused { background: #f0fdf4; color: #16a34a; }

/* ── Data Table (codes tab) ── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.data-table th,
.data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.data-table th {
  background: var(--bg);
  font-weight: 600;
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
}
.status-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 600;
}
.status-used { background: #f3f4f6; color: #6b7280; }
.status-unused { background: #f0fdf4; color: #16a34a; }

/* ── Form Control (inline selects/inputs in panel-header) ── */
.form-control {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  background: var(--surface);
  transition: border-color .15s;
}
.form-control:focus { border-color: var(--primary); }

/* ── Missing border colors for disabled ── */
.customer-card.border-gray { border-left: 4px solid #9ca3af; }

/* ── Codes table enhancements ── */
.copy-icon {
  display: inline-flex;
  align-items: center;
  margin-left: 6px;
  cursor: pointer;
  font-size: 14px;
  opacity: 0.45;
  transition: opacity 0.15s;
  vertical-align: middle;
}
.copy-icon:hover { opacity: 1; }
.link-action {
  font-size: 12px;
  color: var(--primary);
  cursor: pointer;
  white-space: nowrap;
}
.link-action:hover { text-decoration: underline; }

/* Ensure data-table thead has consistent background */
.data-table thead tr {
  background: var(--bg);
}

/* ── Login Page ── */
#login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

/* Checkbox group for multi-select */
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 0;
}
.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.15s;
}
.checkbox-group label:has(input:checked) {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.checkbox-group input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: var(--primary);
}

/* Redeem-sourced institution labels */
.checkbox-group label:has(input:disabled) {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text-secondary);
}
.checkbox-group label:has(input:disabled):hover {
  background: var(--bg);
}

/* ── Customer Table ── */
.cust-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.cust-table thead th {
  background: var(--bg);
  padding: 8px 8px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 1;
  white-space: nowrap;
}
.cust-table tbody td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.cust-table tbody tr:hover {
  background: #f9fafb;
}
.cust-table .td-email {
  font-weight: 500;
  color: var(--text);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cust-table .td-date {
  font-size: 12px;
  white-space: nowrap;
  color: var(--text-secondary);
}
.cust-table .td-notes {
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-secondary);
  font-size: 12px;
}
.cust-table .td-remaining {
  font-weight: 600;
  white-space: nowrap;
}
.cust-table .td-actions {
  white-space: nowrap;
}
.row-expired { opacity: 0.55; }
.row-disabled { opacity: 0.5; }
.row-expiring .td-remaining { color: #d97706; }

/* Badge dot */
.badge-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.badge-dot.badge-active { background: var(--green); }
.badge-dot.badge-expiring { background: var(--yellow); }
.badge-dot.badge-expired { background: var(--red); }
.badge-dot.badge-disabled { background: #9ca3af; }
.badge-dot.badge-per-episode { background: #2563eb; }

/* Service type tags */
.svc-tag {
  display: inline-block;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 3px;
  white-space: nowrap;
}
.svc-1month { background: #dcfce7; color: #166534; }
.svc-3month { background: #f3e8ff; color: #6b21a8; }
.svc-1year { background: #fff7ed; color: #9a3412; }
.svc-per_episode { background: #dbeafe; color: #1d4ed8; }

/* Pagination */
.cust-pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  font-size: 13px;
  color: var(--text-secondary);
}
.pag-btns {
  display: flex;
  gap: 4px;
}
.pag-btns button:disabled {
  opacity: 0.4;
  cursor: default;
}

/* Smaller buttons for table */
.btn-xs {
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  border-radius: 3px;
}
.btn-xs:hover { background: var(--bg); }
.btn-xs.btn-danger { color: var(--red); border-color: var(--red); }
.btn-xs.btn-primary { background: #000; color: #fff; border-color: #000; }
.btn-xs.btn-renewal { color: #d97706; border-color: #d97706; }

/* ── Customer Stats ── */
.cust-stats {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.stat-box {
  flex: 1;
  min-width: 80px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  text-align: center;
  transition: all 0.15s ease;
  user-select: none;
}
.stat-box:hover {
  border-color: var(--text-primary);
}
.stat-active {
  background: var(--yellow-bg) !important;
  border-color: var(--yellow) !important;
  box-shadow: 0 0 0 1px var(--yellow);
}
.stat-active .stat-num {
  color: var(--yellow) !important;
}
.stat-active .stat-label {
  color: var(--yellow) !important;
}
.stat-num {
  font-size: 22px;
  font-weight: 700;
  color: #000;
  line-height: 1.2;
}
.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Compact table refinements */
.cust-table .td-inst {
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cust-table th, .cust-table td {
  white-space: nowrap;
}

/* ── Subscription History ── */
.sub-history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.sub-history-table th {
  text-align: left;
  padding: 8px 10px;
  background: var(--bg-secondary);
  font-weight: 600;
  font-size: 12px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.sub-history-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.sub-history-table tr:last-child td {
  border-bottom: none;
}
.sub-history-table tr.row-current {
  background: var(--green-bg);
  font-weight: 500;
}
.tag-current {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: var(--green-bg);
  color: var(--green);
}
.tag-history {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: #dbeafe;
  color: #2563eb;
}
.renew-info-row {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}
.renew-info-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.renew-info-label {
  font-size: 11px;
  color: var(--text-muted);
}
.renew-info-value {
  font-size: 14px;
  font-weight: 500;
}

/* ── Customer Detail Modal ── */
.cust-detail-header {
  padding: 20px 0 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.cust-detail-info {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 14px;
  color: var(--text-secondary);
}
.cust-detail-info strong {
  font-size: 16px;
  color: var(--text-primary);
}
.svc-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}
.svc-1month { background: #dbeafe; color: #2563eb; }
.svc-3month { background: #d1fae5; color: #16a34a; }
.svc-1year { background: #fef3c7; color: #92400e; }
.svc-per_episode { background: #e5e7eb; color: #374151; }

.detail-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
.detail-stat {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px 16px;
  text-align: center;
}
.detail-stat-num {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
}
.detail-stat-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}
.detail-section {
  margin-bottom: 20px;
}
.detail-section-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.tag-done {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: #d1fae5;
  color: #16a34a;
}

/* ── Extra-wide modal for customer detail ── */
.modal-xl {
  width: 1200px;
  max-width: 90vw;
}

/* User Records sub-tabs */
.ur-tab { background: var(--surface); border: 1px solid var(--border); color: var(--text-secondary); }
.ur-tab.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.ur-tab:hover:not(.active) { background: var(--bg); }
