:root {
  --bg: #f0f2f5;
  --surface: #fff;
  --surface2: #f8f9fa;
  --text: #1a1a2e;
  --text2: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --shadow-lg: 0 4px 16px rgba(0,0,0,.1);
  --radius: 12px;
  --transition: .25s ease;
}

[data-theme="dark"] {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #252540;
  --text: #e4e4ed;
  --text2: #9ca3af;
  --border: #2d2d44;
  --shadow: 0 1px 3px rgba(0,0,0,.3);
  --shadow-lg: 0 4px 16px rgba(0,0,0,.4);
}

* { margin:0; padding:0; box-sizing:border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), color var(--transition);
  min-height: 100vh;
}

.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
}
.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 44px 36px;
  width: 100%; max-width: 400px;
  box-shadow: var(--shadow-lg);
  animation: fadeUp .4s ease;
}
.login-header { text-align: center; margin-bottom: 28px; }
.login-header .logo { font-size: 48px; }
.login-header h1 { font-size: 22px; margin: 8px 0 4px; }
.login-header p { color: var(--text2); font-size: 14px; }
.login-hint { text-align: center; font-size: 12px; color: var(--text2); margin-top: 16px; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 100;
  box-shadow: var(--shadow);
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-left .logo { font-size: 26px; }
.topbar-left .brand { font-weight: 700; font-size: 17px; letter-spacing: -.3px; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.user-info { font-size: 13px; color: var(--text2); }

.container { max-width: 1340px; margin: 0 auto; padding: 24px 28px; animation: fadeUp .35s ease; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px;
  margin-bottom: 24px;
  box-shadow: var(--shadow);
  transition: background var(--transition), box-shadow var(--transition);
}
.card h3 { font-size: 16px; margin-bottom: 18px; font-weight: 600; }
.card code, .card tt {
  background: var(--surface2); padding: 3px 10px; border-radius: 6px;
  font-size: 13px; word-break: break-all; font-family: 'SFMono-Regular', Consolas, monospace;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px 16px;
  text-align: center;
  box-shadow: var(--shadow);
  transition: transform .25s, box-shadow .25s, background var(--transition);
  cursor: default;
  position: relative; overflow: hidden;
}
.stat-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--border);
}
.stat-card.alive::before { background: linear-gradient(90deg, var(--success), #4ade80); }
.stat-card.dead::before { background: linear-gradient(90deg, var(--danger), #f87171); }
.stat-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }
.stat-value { font-size: 30px; font-weight: 800; letter-spacing: -.5px; }
.stat-label { font-size: 12px; color: var(--text2); margin-top: 5px; text-transform: uppercase; letter-spacing: .5px; }
.stat-card.alive .stat-value { color: var(--success); }
.stat-card.dead .stat-value { color: var(--danger); }

.tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 0;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 10px 10px 0;
  box-shadow: var(--shadow);
  overflow-x: auto;
}
.tab {
  padding: 12px 24px;
  border: none;
  background: transparent;
  color: var(--text2);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px 8px 0 0;
  transition: all var(--transition);
  white-space: nowrap;
}
.tab:hover { color: var(--text); background: var(--surface2); }
.tab.active { color: var(--primary); background: var(--bg); font-weight: 600; }

.tab-content { display: none; background: var(--surface); border-radius: 0 0 var(--radius) var(--radius); padding: 24px; box-shadow: var(--shadow); transition: background var(--transition); }
.tab-content.active { display: block; animation: fadeUp .3s ease; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 20px;
}
.search-box { margin-left: auto; }
.search-box input {
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface2);
  color: var(--text);
  width: 220px;
  transition: all var(--transition);
}
.search-box input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
th, td { text-align: left; padding: 12px 10px; }
th {
  font-weight: 600; color: var(--text2); font-size: 12px;
  text-transform: uppercase; letter-spacing: .5px;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
td { border-bottom: 1px solid var(--border); }
tr:nth-child(even) td { background: var(--surface2); }
tr:hover td { background: rgba(79,70,229,.04); }
tr:last-child td { border-bottom: none; }
td tt {
  background: var(--surface2); padding: 2px 8px; border-radius: 5px;
  font-size: 12px; word-break: break-all; font-family: 'SFMono-Regular', Consolas, monospace;
}
td strong { font-weight: 600; }
.action-cell { white-space: nowrap; }

.status-dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.status-dot.alive { background: var(--success); box-shadow: 0 0 6px rgba(34,197,94,.5); }
.status-dot.dead { background: var(--danger); animation: pulse 1.5s infinite; }
.status-dot.unknown { background: var(--text2); }

.status-badge {
  display: inline-block; padding: 2px 8px; border-radius: 4px;
  font-size: 12px; font-weight: 600;
}
.status-badge.ok { background: rgba(34,197,94,.12); color: var(--success); }
.status-badge.warn { background: rgba(245,158,11,.12); color: var(--warn); }
.status-badge.err { background: rgba(239,68,68,.12); color: var(--danger); }

.btn {
  display: inline-block;
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all .2s;
  white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); transform: translateY(-1px); }
.btn.secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover:not(:disabled) { background: #dc2626; transform: translateY(-1px); }
.btn.small { padding: 5px 12px; font-size: 12px; }
.btn-block { width: 100%; }
.icon-btn {
  background: transparent; border: none; font-size: 20px;
  cursor: pointer; padding: 6px; border-radius: 8px; line-height: 1;
  transition: background .2s;
}
.icon-btn:hover { background: var(--surface2); }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; color: var(--text2); margin-bottom: 5px; font-weight: 500; }
.form-group input, .form-group select {
  width: 100%; padding: 10px 14px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 14px; background: var(--surface2); color: var(--text);
  transition: border-color .2s, box-shadow .2s;
}
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.12); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-error { color: var(--danger); font-size: 13px; margin-bottom: 10px; min-height: 20px; }
.form-hint { font-size: 12px; color: var(--text2); margin-top: 4px; }
.input-row { display: flex; gap: 10px; align-items: flex-start; }
.input-row input { flex: 1; }

.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  gap: 16px;
}
.setting-row:last-child { border-bottom: none; }
.input-group { display: flex; align-items: center; gap: 8px; }
.toggle { position: relative; display: inline-block; width: 46px; height: 26px; cursor: pointer; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--border); border-radius: 13px;
  transition: background .3s;
}
.toggle-slider::before {
  content: ''; position: absolute; left: 3px; bottom: 3px;
  width: 20px; height: 20px; background: #fff; border-radius: 50%;
  transition: transform .3s;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.toggle input:checked + .toggle-slider { background: var(--primary); }
.toggle input:checked + .toggle-slider::before { transform: translateX(20px); }

.model-view { padding: 8px 0; }
.model-group { margin-bottom: 24px; }
.model-group-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  background: var(--surface2);
  border-radius: 8px;
  margin-bottom: 12px;
}
.model-name { font-weight: 600; font-size: 15px; }
.model-count { font-size: 12px; color: var(--text2); margin-left: auto; }
.model-upstreams {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 12px;
}
.model-upstream-card {
  background: var(--surface2);
  border-radius: 10px;
  padding: 16px;
  border: 1px solid var(--border);
  transition: all .2s;
}
.model-upstream-card:hover { border-color: var(--primary); transform: translateY(-2px); box-shadow: var(--shadow); }
.model-upstream-card .mus-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.model-upstream-card .mus-name { font-weight: 600; font-size: 14px; flex: 1; }
.model-upstream-card .mus-status { font-size: 12px; color: var(--text2); }
.model-upstream-card .mus-url { margin-bottom: 12px; }
.model-upstream-card .mus-url tt { font-size: 11px; }
.model-upstream-card .mus-actions { display: flex; gap: 6px; }
.model-upstream-card.dead { border-left: 3px solid var(--danger); }
.model-upstream-card.alive { border-left: 3px solid var(--success); }
.model-upstream-card.unknown { border-left: 3px solid var(--text2); }

.token-section {
  background: var(--surface2);
  border-radius: 10px;
  padding: 18px;
  margin-bottom: 20px;
  display: none;
}
.token-title { font-weight: 600; font-size: 14px; margin-bottom: 14px; }
.token-row {
  display: flex; align-items: center; gap: 14px;
  padding: 7px 0;
  font-size: 13px;
}
.token-model { width: 200px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.token-bar { flex: 1; height: 18px; background: var(--border); border-radius: 9px; overflow: hidden; }
.token-fill { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), #818cf8); border-radius: 9px; transition: width .5s ease; }
.token-nums { width: 220px; text-align: right; font-size: 12px; color: var(--text2); white-space: nowrap; }

.modal {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,.5);
  z-index: 1000;
  animation: fadeIn .2s ease;
}
.modal-content {
  background: var(--surface);
  max-width: 520px;
  margin: 80px auto;
  padding: 32px;
  border-radius: 14px;
  position: relative;
  animation: fadeUp .25s ease;
  box-shadow: var(--shadow-lg);
}
.modal-lg { max-width: 740px; }
.modal-content .close {
  position: absolute; top: 16px; right: 20px;
  font-size: 26px; cursor: pointer; color: var(--text2);
  transition: color .2s; line-height: 1;
}
.modal-content .close:hover { color: var(--text); }
.modal-content h3 { margin-bottom: 20px; font-size: 18px; }

#toastContainer {
  position: fixed;
  top: 72px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.toast {
  padding: 14px 22px;
  border-radius: 10px;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  animation: slideIn .3s ease;
  box-shadow: 0 6px 20px rgba(0,0,0,.2);
  max-width: 400px;
}
.toast.error { background: linear-gradient(135deg, var(--danger), #f87171); }
.toast.success { background: linear-gradient(135deg, var(--success), #4ade80); }
.toast.warn { background: linear-gradient(135deg, var(--warn), #fbbf24); }

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text2);
  font-size: 14px;
}

.pagination {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-top: 20px;
}
.pagination button {
  padding: 7px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all .2s;
}
.pagination button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.pagination button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pagination button:disabled { opacity: .35; cursor: default; }

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .4; }
}

@media (max-width: 768px) {
  .container { padding: 14px; }
  .stats-row { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .stat-card { padding: 16px 10px; }
  .stat-value { font-size: 24px; }
  .search-box { margin-left: 0; width: 100%; }
  .search-box input { width: 100%; }
  .topbar { padding: 0 14px; }
  .topbar-left .brand { font-size: 14px; }
  .modal-content { margin: 40px 14px; padding: 24px; }
  .token-model { width: 120px; }
  .token-nums { width: 140px; }
}