:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-soft: #f8f9fd;
  --line: #e2e6ef;
  --line-strong: #d3d9e5;
  --text: #171b2c;
  --muted: #697386;
  --soft: #9199aa;
  --brand: #6550df;
  --brand-strong: #4f3ac7;
  --brand-soft: #f1efff;
  --green: #0f9f6e;
  --green-soft: #e9f9f2;
  --amber: #cf7a0a;
  --amber-soft: #fff5df;
  --red: #d94452;
  --red-soft: #fff0f2;
  --shadow: 0 14px 38px rgba(31, 38, 70, .08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", Arial, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Code", Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  background: var(--bg);
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 82% 8%, rgba(101, 80, 223, .065), transparent 30rem),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

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

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer;
}

a {
  color: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 12px clamp(18px, 4vw, 52px);
  border-bottom: 1px solid rgba(211, 217, 229, .9);
  background: rgba(255, 255, 255, .9);
  backdrop-filter: blur(16px);
}

.brand {
  min-width: 0;
  display: inline-flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
}

.brand img {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  border-radius: 15px;
  object-fit: cover;
  box-shadow: 0 8px 22px rgba(101, 80, 223, .2);
}

.brand span {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.brand strong {
  font-size: 18px;
  letter-spacing: -.02em;
}

.brand small {
  color: var(--muted);
  font-size: 12px;
}

.session-area {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.session-area > span {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.shell {
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 36px 0 52px;
}

.center-stage {
  min-height: calc(100vh - 166px);
  display: grid;
  place-items: center;
}

.gate-card {
  width: min(410px, 100%);
  display: grid;
  gap: 16px;
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: 0 26px 70px rgba(31, 38, 70, .14);
}

.gate-card h1,
.gate-card p {
  margin: 0;
}

.gate-card h1 {
  font-size: 24px;
  letter-spacing: -.03em;
}

.gate-card p {
  color: var(--muted);
}

.gate-avatar {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  object-fit: cover;
  box-shadow: 0 10px 28px rgba(101, 80, 223, .22);
}

.gate-symbol {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border-radius: 17px;
  background: var(--red-soft);
  color: var(--red);
  font-size: 26px;
  font-weight: 800;
}

.loading-line {
  height: 4px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--brand-soft);
}

.loading-line::after {
  content: "";
  width: 45%;
  height: 100%;
  display: block;
  border-radius: inherit;
  background: var(--brand);
  animation: loading 1.1s ease-in-out infinite alternate;
}

@keyframes loading {
  from { transform: translateX(-15%); }
  to { transform: translateX(145%); }
}

.dashboard {
  display: grid;
  gap: 22px;
}

.dashboard-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
}

.dashboard-head h1,
.dashboard-head p {
  margin: 0;
}

.dashboard-head h1 {
  font-size: clamp(28px, 4vw, 36px);
  line-height: 1.15;
  letter-spacing: -.045em;
}

.dashboard-head p {
  margin-top: 6px;
  color: var(--muted);
}

.dashboard-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  color: var(--soft);
  font-size: 12px;
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #eceff5;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  min-width: 112px;
  min-height: 40px;
  padding: 8px 18px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}

.tab:hover {
  color: var(--text);
}

.tab.active,
.tab[aria-selected="true"] {
  background: var(--surface);
  color: var(--brand-strong);
  box-shadow: 0 2px 8px rgba(31, 38, 70, .08);
}

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

.stat-card {
  position: relative;
  min-height: 142px;
  display: grid;
  align-content: center;
  gap: 5px;
  overflow: hidden;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(31, 38, 70, .045);
}

.stat-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: var(--brand);
}

.stat-card.pending::before { background: var(--amber); }
.stat-card.balance::before { background: var(--green); }
.stat-card.spent::before { background: #3d82e7; }

.stat-card span,
.stat-card small {
  color: var(--muted);
}

.stat-card strong {
  overflow: hidden;
  font-size: clamp(25px, 3vw, 34px);
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.035em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.section-gap {
  margin-top: 18px;
}

.overview-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(280px, .7fr);
  gap: 18px;
}

.panel {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-head {
  min-height: 76px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 19px 20px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2,
.panel-head p {
  margin: 0;
}

.panel-head h2 {
  font-size: 17px;
  letter-spacing: -.02em;
}

.panel-head p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

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

.status-list {
  margin: 0;
  padding: 6px 20px 12px;
}

.status-list div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 15px 0;
  border-bottom: 1px solid var(--line);
}

.status-list div:last-child {
  border-bottom: 0;
}

.status-list dt {
  color: var(--muted);
}

.status-list dd {
  margin: 0;
  font-weight: 750;
  text-align: right;
}

.service-summary > .btn {
  width: calc(100% - 40px);
  margin: 0 20px 20px;
}

.filters {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) 180px 180px;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
}

.usage-filters {
  grid-template-columns: minmax(260px, 1fr) 200px;
}

.field {
  display: grid;
  gap: 7px;
}

.field label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.input {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  outline: none;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}

.input:focus {
  border-color: #a99cf4;
  box-shadow: 0 0 0 3px rgba(101, 80, 223, .13);
}

.input:disabled {
  cursor: not-allowed;
  background: #f1f3f7;
  color: var(--soft);
}

.btn {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
  transition: border-color .15s, background .15s, color .15s, transform .15s;
}

.btn:hover {
  border-color: #b8aef4;
  background: var(--brand-soft);
  color: var(--brand-strong);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: .54;
  transform: none;
}

.btn.primary {
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}

.btn.primary:hover {
  border-color: var(--brand-strong);
  background: var(--brand-strong);
  color: #fff;
}

.btn.danger {
  border-color: #f4c7cc;
  background: var(--red-soft);
  color: var(--red);
}

.btn.danger-text {
  color: var(--red);
}

.btn.compact {
  min-height: 34px;
  padding: 6px 11px;
  font-size: 12px;
}

.btn.block {
  width: 100%;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 13px;
}

.data-table th {
  background: #fafbfe;
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: .02em;
  white-space: nowrap;
}

.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.data-table tbody tr:hover {
  background: #fcfcff;
}

.data-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.accounts-table {
  min-width: 1020px;
}

.usage-table {
  min-width: 980px;
}

.identity {
  min-width: 150px;
  display: grid;
  gap: 2px;
}

.identity strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.identity small {
  color: var(--soft);
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 25px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #eef1f6;
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
  white-space: nowrap;
}

.badge.success {
  background: var(--green-soft);
  color: var(--green);
}

.badge.pending {
  background: var(--amber-soft);
  color: var(--amber);
}

.badge.danger {
  background: var(--red-soft);
  color: var(--red);
}

.badge.brand-tone {
  background: var(--brand-soft);
  color: var(--brand-strong);
}

.actions {
  display: flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
}

.role-select {
  width: 122px;
  min-height: 34px;
  padding-block: 6px;
  font-size: 12px;
}

.request-id {
  max-width: 170px;
  overflow: hidden;
  display: block;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty {
  padding: 40px 20px;
  color: var(--soft);
  text-align: center;
}

.settings-grid {
  display: grid;
  grid-template-columns: minmax(300px, .85fr) minmax(300px, .85fr);
  gap: 18px;
  align-items: start;
}

.settings-card {
  display: grid;
  gap: 18px;
  padding: 20px;
}

.settings-card > .panel-head {
  min-height: auto;
  margin: -20px -20px 0;
}

.prompts-card {
  grid-column: 1 / -1;
}

.input-unit {
  position: relative;
}

.input-unit > span {
  position: absolute;
  top: 50%;
  left: 13px;
  z-index: 1;
  color: var(--muted);
  transform: translateY(-50%);
}

.input-unit .input {
  padding-left: 32px;
}

.input-unit-right > span {
  right: 13px;
  left: auto;
}

.input-unit-right .input {
  padding-right: 54px;
  padding-left: 12px;
}

.field-help {
  color: var(--soft);
  font-size: 11px;
  line-height: 1.55;
}

.toggle-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-soft);
  cursor: pointer;
}

.toggle-copy {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.toggle-copy strong {
  font-size: 13px;
}

.toggle-copy small {
  color: var(--muted);
  font-size: 11px;
}

.toggle-control {
  position: relative;
  width: 44px;
  height: 24px;
  flex: 0 0 44px;
}

.toggle-control input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #cbd1dc;
  transition: background .16s;
}

.toggle-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(31, 38, 70, .22);
  transition: transform .16s;
}

.toggle-control input:checked + .toggle-track {
  background: var(--brand);
}

.toggle-control input:checked + .toggle-track::after {
  transform: translateX(20px);
}

.toggle-control input:focus-visible + .toggle-track {
  outline: 3px solid rgba(101, 80, 223, .2);
  outline-offset: 2px;
}

.toggle-control input:disabled + .toggle-track {
  cursor: not-allowed;
  opacity: .55;
}

.route-list {
  display: grid;
  gap: 0;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-soft);
}

.route-list > div {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.route-list > div:last-child {
  border-bottom: 0;
}

.route-list dt {
  color: var(--muted);
  font-size: 12px;
}

.route-list dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  text-align: right;
}

.model-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.model-chip {
  display: inline-flex;
  padding: 3px 7px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand-strong);
  font-family: var(--mono);
  font-size: 10px;
}

.billing-fields-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.api-settings-card {
  grid-column: 1 / -1;
}

.api-config-actions {
  display: grid;
  gap: 8px;
}

.api-config-actions .btn {
  width: 100%;
}

.api-config-actions small {
  color: var(--soft);
  font-size: 11px;
  line-height: 1.55;
}

.service-identity {
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.service-identity > strong {
  font-size: 12px;
}

.service-copy {
  min-height: 22px;
  margin: 5px 0 0;
  color: var(--muted);
}

#serviceBindForm {
  display: grid;
  gap: 14px;
}

.prompt-input {
  min-height: 160px;
  resize: vertical;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.65;
}

.prompt-preset-list {
  display: grid;
  gap: 14px;
}

.prompt-preset-card {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-soft);
}

.prompt-preset-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.prompt-preset-card .prompt-input { min-height: 130px; }

.message {
  min-height: 20px;
  color: var(--muted);
  font-size: 12px;
}

.message.error {
  color: var(--red);
}

.message.success {
  color: var(--green);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(19, 24, 42, .42);
  backdrop-filter: blur(5px);
}

.modal-card {
  width: min(480px, 100%);
  display: grid;
  gap: 16px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: var(--surface);
  box-shadow: 0 26px 80px rgba(12, 17, 38, .24);
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
}

.modal-head h2,
.modal-head p {
  margin: 0;
}

.modal-head h2 {
  font-size: 19px;
}

.modal-head p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 9px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 18px;
}

.toast-region {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 100;
  display: grid;
  gap: 8px;
}

.toast {
  max-width: min(360px, calc(100vw - 36px));
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.toast.success {
  border-color: #b8ead8;
  background: var(--green-soft);
  color: var(--green);
}

.toast.error {
  border-color: #f4c7cc;
  background: var(--red-soft);
  color: var(--red);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 1020px) {
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .overview-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .prompts-card {
    grid-column: auto;
  }
}

@media (max-width: 760px) {
  .topbar {
    position: static;
    align-items: flex-start;
    padding-inline: 16px;
  }

  .brand small,
  .session-area > span {
    display: none;
  }

  .session-area {
    flex-wrap: wrap;
  }

  .shell {
    width: min(100% - 24px, 1480px);
    padding-top: 22px;
  }

  .dashboard-head {
    align-items: stretch;
    flex-direction: column;
  }

  .dashboard-actions {
    justify-content: space-between;
  }

  .tabs {
    margin-inline: -2px;
  }

  .tab {
    min-width: 104px;
    padding-inline: 13px;
  }

  .filters,
  .usage-filters {
    grid-template-columns: 1fr;
  }

  .billing-fields-grid {
    grid-template-columns: 1fr;
  }

  .compact-table,
  .accounts-table,
  .usage-table {
    min-width: 0;
  }

  .data-table,
  .data-table thead,
  .data-table tbody,
  .data-table tr,
  .data-table th,
  .data-table td {
    display: block;
  }

  .data-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .data-table tbody {
    display: grid;
    gap: 10px;
    padding: 12px;
  }

  .data-table tbody tr {
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
  }

  .data-table td {
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 8px 0;
    border-bottom: 1px solid var(--line);
    text-align: right;
  }

  .data-table td::before {
    content: attr(data-label);
    flex: 0 0 90px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    text-align: left;
  }

  .data-table td:last-child {
    border-bottom: 0;
  }

  .data-table td.num {
    text-align: right;
  }

  .identity {
    min-width: 0;
    justify-items: end;
  }

  .actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    white-space: normal;
  }
}

@media (max-width: 520px) {
  .topbar {
    gap: 10px;
  }

  .brand img {
    width: 42px;
    height: 42px;
    flex-basis: 42px;
    border-radius: 13px;
  }

  .brand strong {
    font-size: 16px;
  }

  .session-area .btn {
    min-height: 36px;
    padding-inline: 10px;
    font-size: 12px;
  }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .stat-card {
    min-height: 124px;
    padding: 16px;
  }

  .stat-card strong {
    font-size: 24px;
  }

  .panel-head {
    min-height: auto;
    align-items: stretch;
    flex-direction: column;
  }

  .settings-card > .panel-head {
    flex-direction: column;
  }

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

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