/* ===== CSS Variables ===== */
:root {
  --sidebar-width: 240px;
  --sidebar-bg: #1a1f2e;
  --sidebar-hover: #252b3d;
  --sidebar-active: #2d3a5c;
  --sidebar-text: #a0aec0;
  --sidebar-text-active: #ffffff;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --bg-body: #f1f5f9;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.12);
  --radius: 10px;
  --radius-sm: 6px;
  --transition: all 0.2s ease;
}

/* ===== Reset ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 15px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-body);
  color: var(--text-main);
  line-height: 1.6;
  overflow-x: hidden;
}
a { text-decoration: none; color: inherit; }
button { font-family: inherit; cursor: pointer; }

/* ===== Layout ===== */
.app {
  display: flex;
  min-height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0; top: 0; bottom: 0;
  z-index: 100;
  transition: var(--transition);
}
.sidebar-header {
  padding: 20px 24px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}
.logo-icon {
  font-size: 24px;
}
.logo-text {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  letter-spacing: 0.5px;
}
.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  transition: var(--transition);
  color: var(--sidebar-text);
}
.nav-item:hover {
  background: var(--sidebar-hover);
  color: #fff;
}
.nav-item.active {
  background: var(--sidebar-active);
  color: var(--sidebar-text-active);
  font-weight: 500;
}
.nav-icon {
  font-size: 18px;
  width: 24px;
  text-align: center;
}
.sidebar-footer {
  padding: 16px 24px;
  border-top: 1px solid rgba(255,255,255,0.06);
  font-size: 13px;
}
.bot-status-mini {
  display: flex;
  align-items: center;
  gap: 8px;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-secondary);
  display: inline-block;
}
.status-dot.online { background: var(--success); box-shadow: 0 0 6px rgba(16,185,129,0.5); }
.status-dot.offline { background: var(--danger); box-shadow: 0 0 6px rgba(239,68,68,0.5); }

/* ===== Main Content ===== */
.main {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: var(--transition);
}

/* ===== Top Header ===== */
.top-header {
  background: var(--bg-card);
  padding: 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow);
}
.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}
.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-main);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
}
.menu-toggle:hover { background: var(--bg-body); }
.page-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-main);
}
.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}
.bot-info {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
}
.status-indicator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 20px;
  background: #f1f5f9;
  font-weight: 500;
  font-size: 12px;
}
.status-indicator.online { background: #ecfdf5; color: var(--success); }
.status-indicator.offline { background: #fef2f2; color: var(--danger); }
.status-pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  position: relative;
}
.status-indicator.online .status-pulse::after {
  content: '';
  position: absolute;
  left: 0; top: 0;
  width: 100%; height: 100%;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% { transform: scale(1); opacity: 0.8; }
  70% { transform: scale(2.5); opacity: 0; }
  100% { transform: scale(1); opacity: 0; }
}
.bot-user {
  color: var(--text-secondary);
  font-weight: 500;
}
.bot-messages {
  color: var(--text-secondary);
  background: var(--bg-body);
  padding: 4px 10px;
  border-radius: 20px;
}

.switch-wechat-btn {
  margin-left: 8px;
  padding: 4px 12px;
  border: 1px solid var(--border-color);
  background: var(--bg-body);
  color: var(--text-secondary);
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}
.switch-wechat-btn:hover {
  background: var(--accent-color);
  color: white;
  border-color: var(--accent-color);
}

/* ===== Content ===== */
.content {
  flex: 1;
  padding: 24px 28px;
}
.page { display: none; }
.page.active { display: block; }

/* ===== Stats Grid ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow);
  transition: var(--transition);
}
.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.stat-icon {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
}
.stat-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
}
.stat-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== Card ===== */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
  overflow: hidden;
}
.card-header {
  padding: 18px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  gap: 12px;
}
.card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ===== Table ===== */
.table-responsive {
  overflow-x: auto;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.data-table thead th {
  background: #f8fafc;
  color: var(--text-secondary);
  font-weight: 600;
  text-align: left;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table tbody td {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  color: var(--text-main);
  vertical-align: middle;
}
.data-table tbody tr:hover {
  background: #f8fafc;
}
.data-table tbody tr:last-child td {
  border-bottom: none;
}
.data-table .cell-actions {
  display: flex;
  gap: 8px;
  white-space: nowrap;
}
.data-table .cell-truncate {
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.data-table .cell-small {
  max-width: 120px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary {
  background: #f1f5f9;
  color: var(--text-main);
}
.btn-secondary:hover { background: #e2e8f0; }
.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: #dc2626; }
.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}
.btn-icon {
  padding: 6px;
  font-size: 14px;
}

/* ===== Form ===== */
.form-group {
  margin-bottom: 18px;
}
.form-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}
.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text-main);
  background: #fff;
  transition: var(--transition);
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}
.form-textarea {
  min-height: 100px;
  resize: vertical;
  line-height: 1.5;
}
.form-textarea.json-editor {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 13px;
  min-height: 120px;
}
.form-textarea.content-editor {
  min-height: 160px;
}
.form-hint {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}
.form-inline {
  display: flex;
  align-items: center;
}
.form-check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}
.form-check input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}
.search-input {
  width: 160px;
  padding: 7px 12px;
  font-size: 13px;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.25s ease;
}
.modal-overlay.show {
  display: flex;
  opacity: 1;
}
.modal {
  background: var(--bg-card);
  border-radius: var(--radius);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  transform: translateY(20px) scale(0.97);
  transition: transform 0.25s ease, opacity 0.25s ease;
  opacity: 0;
}
.modal-overlay.show .modal {
  transform: translateY(0) scale(1);
  opacity: 1;
}
.modal-lg { max-width: 680px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-title {
  font-size: 17px;
  font-weight: 600;
}
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
}
.modal-close:hover {
  background: #f1f5f9;
  color: var(--text-main);
}
.modal-body {
  padding: 20px 24px;
}
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}
.toast {
  background: var(--bg-card);
  color: var(--text-main);
  padding: 14px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 500;
  min-width: 240px;
  max-width: 360px;
  pointer-events: auto;
  border-left: 4px solid var(--primary);
  animation: toastSlideIn 0.3s ease;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
@keyframes toastSlideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
.toast-exit {
  animation: toastSlideOut 0.3s ease forwards;
}
@keyframes toastSlideOut {
  from { transform: translateX(0); opacity: 1; }
  to { transform: translateX(100%); opacity: 0; }
}

/* ===== Pagination ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.pagination button {
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-main);
  cursor: pointer;
  transition: var(--transition);
}
.pagination button:hover:not(:disabled) {
  background: #f8fafc;
  border-color: var(--primary);
  color: var(--primary);
}
.pagination button.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.pagination button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.pagination-info {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0 8px;
}

/* ===== Badges ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}
.badge-green {
  background: #ecfdf5;
  color: #059669;
}
.badge-red {
  background: #fef2f2;
  color: #dc2626;
}
.badge-gray {
  background: #f1f5f9;
  color: var(--text-secondary);
}
.badge-blue {
  background: #eff6ff;
  color: #2563eb;
}

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
}
.empty-state-icon {
  font-size: 48px;
  margin-bottom: 12px;
  opacity: 0.5;
}

/* ===== Help Content ===== */
.help-content {
  padding: 20px 24px;
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 2;
}
.help-content p {
  margin: 0;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .main {
    margin-left: 0;
  }
  .menu-toggle {
    display: block;
  }
  .stats-grid {
    grid-template-columns: 1fr;
  }
  .search-input {
    width: 100%;
  }
  .header-actions {
    width: 100%;
  }
  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }
  .data-table {
    font-size: 13px;
  }
  .data-table thead th,
  .data-table tbody td {
    padding: 10px 14px;
  }
  .content {
    padding: 16px;
  }
  .bot-info {
    flex-wrap: wrap;
    gap: 8px;
  }
  .modal-overlay {
    padding: 10px;
  }
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 99;
}
@media (max-width: 768px) {
  .sidebar-overlay.show {
    display: block;
  }
}

/* ===== AI Toggle Switch ===== */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #ccc;
  border-radius: 26px;
  transition: 0.3s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.3s;
}
.toggle-switch input:checked + .toggle-slider {
  background: var(--primary);
}
.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(22px);
}
