/* ===== 后台管理（与主站同一套浅色配色） ===== */
:root {
  --page-bg: #f6f7f9;
  --card-bg: #ffffff;
  --card-bg-soft: #fafafa;
  --border: #e6e8eb;
  --border-soft: #eef0f2;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --accent: #111827;
  --primary: #2563eb;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--page-bg);
  color: var(--text-main);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--text-muted);
}

a {
  color: var(--primary);
  text-decoration: none;
}

/* ===== 按钮与表单 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card-bg);
  color: var(--text-main);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  background: var(--card-bg-soft);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

.btn.primary:hover {
  opacity: 0.9;
}

.btn.danger {
  color: var(--danger);
  border-color: rgba(220, 38, 38, 0.3);
}

.btn.danger:hover {
  background: rgba(220, 38, 38, 0.06);
}

.btn.small {
  height: 26px;
  padding: 0 8px;
  font-size: 12px;
}

.btn.icon {
  width: 32px;
  padding: 0;
  font-size: 18px;
}

.btn.block {
  width: 100%;
}

input,
select,
textarea {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text-main);
  font: inherit;
  font-size: 13px;
  outline: none;
  min-width: 0;
}

textarea {
  height: auto;
  min-height: 90px;
  padding: 8px 10px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

input[type="checkbox"] {
  height: auto;
  width: auto;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
}

label.inline {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  color: var(--text-main);
  font-size: 13px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.form-error {
  min-height: 18px;
  color: var(--danger);
  font-size: 12px;
}

.row-gap {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* ===== 登录门 ===== */
.gate {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.gate-card {
  width: 100%;
  max-width: 360px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
}

.gate-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 18px;
}

#gate-login {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ===== 布局 ===== */
.layout {
  display: flex;
  min-height: 100%;
}

.sidebar {
  width: 200px;
  flex-shrink: 0;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  padding: 18px 20px;
  font-weight: 700;
  font-size: 16px;
}

.brand span {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 12px;
}

#nav {
  display: flex;
  flex-direction: column;
  padding: 0 10px;
  gap: 2px;
}

#nav a {
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-main);
  cursor: pointer;
}

#nav a:hover {
  background: var(--card-bg-soft);
}

#nav a.active {
  background: var(--accent);
  color: #fff;
}

.sidebar-foot {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
}

.sidebar-foot .me {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 20px 24px 40px;
}

.main-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.main-head h1 {
  font-size: 20px;
  margin: 0;
  flex: 1;
}

.head-actions {
  display: flex;
  gap: 8px;
}

.nav-toggle {
  display: none;
}

/* ===== 卡片 / 统计 ===== */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

.card-title {
  font-weight: 600;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.stat .label {
  color: var(--text-muted);
  font-size: 12px;
}

.stat .value {
  font-size: 22px;
  font-weight: 600;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

.stat .sub {
  color: var(--text-muted);
  font-size: 12px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.grid-2 .card {
  margin-bottom: 0;
}

/* 柱状图（每日请求量，按类型堆叠） */
.chart {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 180px;
  padding-top: 8px;
}

.chart-col {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}

.chart-bar {
  width: 100%;
  max-width: 28px;
  display: flex;
  flex-direction: column-reverse;
  border-radius: 4px 4px 0 0;
  overflow: hidden;
  min-height: 1px;
}

.chart-bar span {
  display: block;
}

.chart-col .day {
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
}

.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

.legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: -1px;
}

.k-chat { background: #2563eb; }
.k-vision { background: #7c3aed; }
.k-agent { background: #0891b2; }
.k-image { background: #f59e0b; }

/* ===== 表格 ===== */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}

.toolbar input[type="search"] {
  width: 220px;
}

.table-wrap {
  overflow-x: auto;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-soft);
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--card-bg-soft);
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
}

tr:last-child td {
  border-bottom: none;
}

td.num,
th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

td.ellipsis {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

td.nowrap {
  white-space: nowrap;
}

tbody tr:hover {
  background: #fcfcfd;
}

.empty {
  padding: 32px;
  text-align: center;
  color: var(--text-muted);
}

.pager {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-muted);
}

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--border-soft);
  color: var(--text-muted);
  white-space: nowrap;
}

.tag.admin { background: #eef2ff; color: #4338ca; }
.tag.ok { background: #f0fdf4; color: var(--success); }
.tag.bad { background: #fef2f2; color: var(--danger); }
.tag.warn { background: #fffbeb; color: var(--warning); }

.quota-bar {
  height: 6px;
  border-radius: 3px;
  background: var(--border-soft);
  overflow: hidden;
  margin-top: 4px;
}

.quota-bar span {
  display: block;
  height: 100%;
  background: var(--primary);
}

.quota-bar span.full {
  background: var(--danger);
}

/* ===== 弹窗 ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.35);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 48px 16px;
  overflow-y: auto;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 760px;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.15);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-soft);
}

.modal-title {
  font-weight: 600;
  font-size: 15px;
}

.modal-body {
  padding: 18px;
}

.section-title {
  font-weight: 600;
  margin: 18px 0 10px;
}

.section-title:first-child {
  margin-top: 0;
}

.kv {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 6px 12px;
  font-size: 13px;
}

.kv dt {
  color: var(--text-muted);
}

.kv dd {
  margin: 0;
  word-break: break-all;
}

.pre {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--card-bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  max-height: 320px;
  overflow-y: auto;
  font-size: 13px;
}

.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
}

.model-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 6px 16px;
}

/* ===== 提示 ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fff;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  z-index: 60;
  max-width: calc(100% - 32px);
}

.toast.error {
  background: var(--danger);
}

/* ===== 窄屏 ===== */
@media (max-width: 860px) {
  .grid-2 {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    left: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .layout.nav-open .sidebar {
    transform: none;
  }

  .nav-toggle {
    display: inline-flex;
  }

  .main {
    padding: 14px 16px 32px;
  }

  .toolbar input[type="search"] {
    width: 100%;
  }

  .chart-col .day {
    display: none;
  }
}

/* ===== 完整对话 ===== */
.modal-card.wide {
  max-width: 960px;
}

.thread {
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-top: 12px;
}

.turn {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.msg.user {
  background: #f1f3f5;
  margin-right: 10%;
}

.msg.bot {
  background: var(--card-bg);
  margin-left: 4%;
}

.msg.bot.failed {
  border-color: rgba(220, 38, 38, 0.3);
}

.msg-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.msg-head b {
  color: var(--text-main);
}

.msg-body {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 13px;
  line-height: 1.65;
  max-height: 360px;
  overflow-y: auto;
}

.modal-body.expanded .msg-body {
  max-height: none;
}

.msg-error {
  color: var(--danger);
  font-size: 12px;
  white-space: pre-wrap;
  margin-bottom: 6px;
}

@media (max-width: 860px) {
  .msg.user,
  .msg.bot {
    margin: 0;
  }
}

/* Markdown 渲染：后台固定浅色，覆盖 github-markdown 的暗色模式变量 */
.msg-body.markdown-body {
  white-space: normal;
  background: transparent;
  color: var(--text-main);
  font-family: inherit;
  font-size: 13px;
  line-height: 1.65;
  color-scheme: light;
  --fgColor-default: var(--text-main);
  --fgColor-muted: var(--text-muted);
  --fgColor-accent: var(--primary);
  --bgColor-default: transparent;
  --bgColor-muted: #f6f8fa;
  --bgColor-neutral-muted: rgba(175, 184, 193, 0.2);
  --borderColor-default: var(--border);
  --borderColor-muted: var(--border-soft);
}

.msg-body.markdown-body > :first-child {
  margin-top: 0 !important;
}

.msg-body.markdown-body > :last-child {
  margin-bottom: 0 !important;
}

.msg-body.markdown-body pre {
  background: #f6f8fa;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  white-space: pre;
  overflow: auto;
}

.msg-body.markdown-body h1,
.msg-body.markdown-body h2 {
  font-size: 1.25em;
  border-bottom: 1px solid var(--border-soft);
}

.msg-body.markdown-body h3,
.msg-body.markdown-body h4 {
  font-size: 1.1em;
}

.msg-body.markdown-body img {
  max-width: 100%;
  border-radius: var(--radius-sm);
}

.msg-body.markdown-body table {
  display: block;
  overflow-x: auto;
  width: max-content;
  max-width: 100%;
}
