:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #4d6bfe;
  --accent-hover: #3b58e6;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --warning-bg: #fdf6ec;
  --warning-border: #f0d9b5;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------------- 顶栏 ---------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .logo { font-weight: 700; font-size: 16px; white-space: nowrap; }
.topbar .logo span { color: var(--accent); }
.topbar .spacer { flex: 1; }
.search-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  user-select: none;
}
.search-toggle input { accent-color: var(--accent); cursor: pointer; margin: 0; }
.search-toggle:has(input:checked) { border-color: var(--accent); color: var(--accent); }

/* ---------------- 按钮 ---------------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
  background: var(--accent);
  color: #fff;
}
.btn:hover:not(:disabled) { background: var(--accent-hover); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover:not(:disabled) { background: #f3f4f6; }
.btn.danger { background: transparent; color: var(--danger); border-color: #fecaca; }
.btn.danger:hover:not(:disabled) { background: var(--danger-bg); }
.btn.small { padding: 5px 10px; font-size: 13px; border-radius: 7px; }

/* ---------------- 输入 ---------------- */
input[type="text"], input[type="password"], select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  outline: none;
  background: var(--panel);
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: none; line-height: 1.5; }

/* ---------------- 登录卡片 ---------------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .06);
}
.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.auth-card .btn { width: 100%; justify-content: center; margin-top: 18px; }
.tabs { display: flex; gap: 6px; margin-bottom: 18px; border-bottom: 1px solid var(--border); }
.tabs button {
  border: none; background: none; padding: 8px 4px; margin-right: 12px;
  font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.auth-msg { font-size: 13px; margin-top: 12px; min-height: 18px; }
.auth-msg.err { color: var(--danger); }
.auth-msg.ok { color: var(--success); }

/* ---------------- 聊天 ---------------- */
.chat-main { flex: 1; overflow-y: auto; padding: 24px 20px; }
.messages { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; flex-direction: column; gap: 6px; }
.msg .role { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.msg.user .role { align-self: flex-end; }
.bubble {
  padding: 12px 16px; border-radius: 14px; line-height: 1.65; font-size: 15px;
  white-space: pre-wrap; word-break: break-word; max-width: 100%;
}
.msg.user .bubble { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: #eef0f4; align-self: flex-start; border-bottom-left-radius: 4px; }
.reasoning {
  background: var(--warning-bg); border: 1px solid var(--warning-border);
  border-radius: 10px; padding: 10px 14px; font-size: 13px; color: #7a5c2e;
  line-height: 1.6; white-space: pre-wrap; word-break: break-word; align-self: flex-start; max-width: 100%;
}
.reasoning .label { font-weight: 700; font-size: 12px; display: block; margin-bottom: 4px; }
.cursor { display: inline-block; width: 8px; height: 16px; background: var(--accent); margin-left: 2px; vertical-align: -2px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.error-box { color: var(--danger); background: var(--danger-bg); border: 1px solid #fecaca; border-radius: 10px; padding: 10px 14px; font-size: 13px; white-space: pre-wrap; word-break: break-word; }
.chat-footer { padding: 14px 20px 18px; background: var(--panel); border-top: 1px solid var(--border); }
.composer { max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; }
.composer textarea { flex: 1; max-height: 200px; }
.hint { max-width: 820px; margin: 8px auto 0; font-size: 12px; color: var(--muted); }

/* ---------------- 后台 / 面板 ---------------- */
.page { max-width: 1080px; margin: 24px auto; padding: 0 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 20px; margin-bottom: 20px; }
.card h2 { font-size: 16px; margin-bottom: 14px; }
.card h3 { font-size: 14px; margin-bottom: 8px; }
.form-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.form-row > * { flex: 1; min-width: 120px; }
.form-row .btn { flex: 0 0 auto; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tr:last-child td { border-bottom: none; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.admin { background: #ede9fe; color: #6d28d9; }
.badge.sysadmin { background: #fee2e2; color: #b91c1c; }
.badge.user { background: #e0f2fe; color: #0369a1; }
.badge.on { background: var(--success-bg); color: var(--success); }
.badge.off { background: #f3f4f6; color: var(--muted); }
.badge.revoked { background: var(--danger-bg); color: var(--danger); }

/* 操作下拉菜单 */
.act-menu { position: relative; display: inline-block; }
.act-menu summary { list-style: none; cursor: pointer; user-select: none; }
.act-menu summary::-webkit-details-marker { display: none; }
.act-items {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 60;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .14);
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 158px;
}
.act-items .btn { width: 100%; justify-content: flex-start; }

/* 限额弹窗 */
.quota-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 12px 0; }
.quota-item { display: flex; flex-direction: column; gap: 6px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.quota-item label { font-weight: 600; font-size: 13px; color: var(--muted); }
.quota-item input { font-size: 15px; padding: 10px 12px; }
.quota-item .muted { font-size: 12px; }

.muted { color: var(--muted); font-size: 13px; }

/* ---------------- 弹窗 ---------------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .4);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  background: var(--panel); border-radius: 14px; padding: 22px;
  width: 100%; max-width: 480px; max-height: 90vh; overflow: auto;
}
.modal h3 { font-size: 16px; margin-bottom: 12px; }
.modal .actions { margin-top: 16px; justify-content: flex-end; }
.key-box {
  background: var(--warning-bg); border: 1px solid var(--warning-border);
  border-radius: 9px; padding: 12px; font-family: ui-monospace, Consolas, monospace;
  font-size: 13px; word-break: break-all; margin: 10px 0;
}

/* ---------------- 提示 ---------------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 9px;
  font-size: 14px; z-index: 200; box-shadow: 0 8px 20px rgba(0,0,0,.2);
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }
.toast.err { background: var(--danger); }
