:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-muted: #f1f5f9;
  --border: #dbe4f0;
  --text: #172033;
  --muted: #64748b;
  --primary: #1e40af;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --success: #047857;
  --success-bg: #ecfdf5;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

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

.login-panel {
  width: min(100%, 400px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1 { margin: 0 0 4px; font-size: 22px; }
.login-sub { margin: 0 0 22px; font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 6px; margin: 0; }
.field span { color: var(--muted); font-size: 12px; font-weight: 600; }

input, textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

input { height: 38px; padding: 0 10px; }
textarea { min-height: 112px; padding: 10px; resize: vertical; font-family: Consolas, "Courier New", monospace; }

input:focus, textarea:focus, button:focus-visible {
  outline: 3px solid rgba(30, 64, 175, 0.22);
  outline-offset: 1px;
  border-color: var(--primary);
}

.btn {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease;
}
.btn:hover { background: var(--surface-muted); border-color: #cbd5e1; }
.btn:disabled { cursor: wait; opacity: 0.65; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { background: #fff; border-color: #fecaca; color: var(--danger); }
.btn.danger:hover { background: var(--danger-bg); }
.btn.block { width: 100%; min-height: 40px; }
.error-text { min-height: 18px; margin: 10px 0 0; color: var(--danger); font-size: 13px; }

.app {
  width: min(1440px, calc(100% - 40px));
  margin: 0 auto;
  padding: 18px 0 40px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 20px;
  min-height: 64px;
  margin-bottom: 18px;
  padding: 10px 0;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

.brand { font-size: 17px; font-weight: 700; letter-spacing: 0; white-space: nowrap; }
.brand .muted { font-weight: 500; }
.tabs { display: flex; justify-content: center; gap: 2px; min-width: 0; overflow-x: auto; padding: 4px; background: #eaf0f7; border-radius: 7px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { min-height: 32px; padding: 0 12px; border: 0; border-radius: 5px; background: transparent; color: var(--muted); font: inherit; font-size: 12px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.tab:hover { color: var(--text); }
.tab.active { background: #fff; color: var(--primary); box-shadow: var(--shadow); }
.tab-short { display: none; }
.top-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; white-space: nowrap; }
.top-actions .muted { font-size: 12px; }

.stats-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { min-height: 96px; padding: 15px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: 7px; box-shadow: var(--shadow); }
.stat-label { color: var(--muted); font-size: 12px; font-weight: 600; }
.stat-value { margin-top: 8px; font-size: 23px; line-height: 1; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: 0; }

.panel { margin-bottom: 16px; padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 7px; box-shadow: var(--shadow); }
.panel h2 { margin: 0; font-size: 15px; line-height: 1.2; }
.panel-subtitle { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel-head > div { min-width: 0; }

.pool-layout { display: grid; grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.2fr); gap: 16px; }
.add-key-form { display: grid; grid-template-columns: minmax(140px, 0.65fr) minmax(220px, 1.35fr) auto; align-items: end; gap: 12px; }
.add-key-submit { min-width: 108px; }
.batch-form { display: flex; flex-direction: column; gap: 10px; }
.batch-form .btn { align-self: flex-start; }
.upload-form { display: flex; align-items: center; gap: 12px; }
.upload-form input[type="file"] { flex: 1; height: 38px; padding: 6px 10px; }
.pricing-form { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(220px, 1fr) auto; align-items: end; gap: 12px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); }

.table-wrap { overflow-x: auto; margin: 0 -2px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 12px; border-bottom: 1px solid #e8eef5; text-align: left; vertical-align: middle; }
th { color: var(--muted); font-size: 12px; font-weight: 700; white-space: nowrap; }
tbody tr { transition: background-color 140ms ease; }
tbody tr:hover { background: #f8fbff; }
tbody tr:last-child td { border-bottom: 0; }
.usage-table th:nth-child(n+2), .usage-table td:nth-child(n+2) { text-align: right; font-variant-numeric: tabular-nums; }

.badge { display: inline-flex; align-items: center; min-height: 22px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; white-space: nowrap; }
.badge.active, .badge.ok { color: var(--success); background: var(--success-bg); }
.badge.cooldown { color: var(--warn); background: var(--warn-bg); }
.badge.disabled { color: var(--muted); background: #eef2f7; }
.badge.err { color: var(--danger); background: var(--danger-bg); }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.mono, code { font-family: Consolas, "Courier New", monospace; }
.mono { white-space: nowrap; }
.cell-limit { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.relay-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.relay-item { padding: 14px; background: var(--surface-muted); border: 1px solid var(--border); border-radius: 6px; }
.relay-label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 12px; font-weight: 600; }
code { font-size: 13px; overflow-wrap: anywhere; }
.code-block { margin: 0; padding: 14px; overflow-x: auto; border-radius: 6px; background: #0f172a; color: #e2e8f0; font-family: Consolas, "Courier New", monospace; font-size: 12px; line-height: 1.6; }
.kv-list { display: flex; flex-direction: column; }
.kv-list > div { display: flex; justify-content: space-between; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.kv-list > div:last-child { border-bottom: 0; }
.kv-list span { color: var(--muted); }

.trend-chart { min-height: 168px; padding: 10px 4px 0; }
.trend-inner { display: flex; align-items: flex-end; gap: 7px; height: 132px; padding: 0 6px 24px; border-bottom: 1px solid var(--border); }
.trend-bar { position: relative; flex: 1 1 24px; max-width: 54px; min-width: 10px; border-radius: 4px 4px 0 0; background: #3b82f6; cursor: help; transition: background-color 140ms ease; }
.trend-bar:hover { background: var(--primary); }
.trend-label { position: absolute; top: calc(100% + 6px); left: 50%; transform: translateX(-50%); color: var(--muted); font-size: 10px; white-space: nowrap; }

@media (max-width: 1100px) {
  .topbar { grid-template-columns: 1fr auto; }
  .tabs { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; }
  .stats-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .app { width: min(100% - 24px, 1440px); padding-top: 10px; }
  .topbar { gap: 10px; min-height: 56px; }
  .brand { font-size: 15px; }
  .top-actions .muted { display: none; }
  .tab { min-height: 40px; padding: 0 12px; }
  .tab-full { display: none; }
  .tab-short { display: inline; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { min-height: 86px; padding: 13px; }
  .stat-value { font-size: 20px; }
  .panel { padding: 14px; border-radius: 6px; }
  .pool-layout, .relay-grid { grid-template-columns: 1fr; }
  .add-key-form, .pricing-form { grid-template-columns: 1fr; align-items: stretch; }
  .add-key-submit, .pricing-form .btn { width: 100%; min-height: 44px; }
  .upload-form { align-items: stretch; flex-direction: column; }
  .upload-form .btn { min-height: 44px; }
  .responsive-table thead { display: none; }
  .responsive-table, .responsive-table tbody { display: block; width: 100%; }
  .responsive-table tbody tr { display: block; margin-bottom: 10px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
  .responsive-table tbody td { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; padding: 6px 0; border: 0; text-align: right; overflow-wrap: anywhere; }
  .responsive-table tbody td::before { content: attr(data-label); flex: 0 0 auto; color: var(--muted); font-size: 12px; font-weight: 600; text-align: left; }
  .responsive-table tbody td.mono, .responsive-table tbody td.cell-limit { max-width: none; white-space: normal; }
  .responsive-table tbody td:last-child .row-actions { width: 100%; }
  .row-actions .btn { flex: 1; min-height: 40px; }
  .kv-list > div { align-items: flex-start; flex-direction: column; gap: 4px; }
  .code-block { white-space: pre-wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
