/* U6 主题：默认浅色（不跟随系统）。<html data-theme="light|dark|system"> 由 index.html 内联脚本在首次绘制前设置；
   “跟随系统”才使用 prefers-color-scheme。 */
:root, :root[data-theme="light"] {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1c2330;
  --muted: #616161;
  --border: #d5dae1;
  --accent: #f38020;
  --accent-ink: #b45309;
  --link: #2563eb;
  --ok: #13773a;
  --ok-bg: #e7f6ec;
  --warn: #c2410c;
  --warn-bg: #fff4e0;
  --crit: #c0262d;
  --crit-bg: #fdecec;
  --unknown: #5b6472;
  --unknown-bg: #eef0f3;
  --info: #1d4ed8;
  --info-bg: #e8efff;
  --demo: #7c3aed;
  --demo-bg: #f1eaff;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --chart-axis: #5d6679;
  --chart-grid: #e3e6eb;
  --chart-gap: rgba(28, 35, 48, 0.07);
  --c-requests: #2563eb;
  --c-errors: #c0262d;
  --c-ratio: #e11d48;
  --c-gbs: #e8710a;
  --c-cpu: #9333ea;
  --c-rows-read: #0e7490;
  --c-rows-written: #0f766e;
  --c-stored: #4b5563;
  --c-ws: #4d7c0f;
  --c-kv-read: #a16207;
  --c-kv-write: #b45309;
  color-scheme: light;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
:root[data-theme="dark"] {
  --bg: #1c222d;
  --panel: #262d3a;
  --panel-2: #303848;
  --text: #f0f3f8;
  --muted: #b8b8b8;
  --border: #4a5467;
  --accent-ink: #fdba74;
  --link: #8fb5ff;
  --ok: #86efac;
  --ok-bg: #1d4431;
  --warn: #fb923c;
  --warn-bg: #4a2614;
  --crit: #fca5a5;
  --crit-bg: #5a2327;
  --unknown: #cbd1da;
  --unknown-bg: #3a4250;
  --info: #b4cbff;
  --info-bg: #233a63;
  --demo: #d8bfff;
  --demo-bg: #3f2d66;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  --chart-axis: #b8c0cc;
  --chart-grid: #3a4353;
  --chart-gap: rgba(240, 243, 248, 0.08);
  --c-requests: #7aa7ff;
  --c-errors: #f87171;
  --c-ratio: #fb7185;
  --c-gbs: #fb923c;
  --c-cpu: #c084fc;
  --c-rows-read: #22d3ee;
  --c-rows-written: #2dd4bf;
  --c-stored: #cbd5e1;
  --c-ws: #a3e635;
  --c-kv-read: #facc15;
  --c-kv-write: #fdba74;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #1c222d;
    --panel: #262d3a;
    --panel-2: #303848;
    --text: #f0f3f8;
    --muted: #b8b8b8;
    --border: #4a5467;
    --accent-ink: #fdba74;
    --link: #8fb5ff;
    --ok: #86efac;
    --ok-bg: #1d4431;
    --warn: #fb923c;
    --warn-bg: #4a2614;
    --crit: #fca5a5;
    --crit-bg: #5a2327;
    --unknown: #cbd1da;
    --unknown-bg: #3a4250;
    --info: #b4cbff;
    --info-bg: #233a63;
    --demo: #d8bfff;
    --demo-bg: #3f2d66;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    --chart-axis: #b8c0cc;
    --chart-grid: #3a4353;
    --chart-gap: rgba(240, 243, 248, 0.08);
    --c-requests: #7aa7ff;
    --c-errors: #f87171;
    --c-ratio: #fb7185;
    --c-gbs: #fb923c;
    --c-cpu: #c084fc;
    --c-rows-read: #22d3ee;
    --c-rows-written: #2dd4bf;
    --c-stored: #cbd5e1;
    --c-ws: #a3e635;
    --c-kv-read: #facc15;
    --c-kv-write: #fdba74;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }

.layout { display: grid; grid-template-columns: 216px 1fr; min-height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; padding: 0 8px 18px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg, #f6821f, #fbad41); display: grid; place-items: center; color: #fff; font-size: 15px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); margin-bottom: 2px; }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.active { background: var(--warn-bg); color: var(--accent-ink); font-weight: 600; }
.nav .ico { width: 18px; text-align: center; opacity: .8; }
.nav-foot { margin-top: 24px; padding: 10px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); }

.main { padding: 0 28px 48px; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 0 12px; }
.topbar h1 { font-size: 20px; margin: 0; }
.topbar .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.who { font-size: 12px; color: var(--muted); text-align: right; }

.banner { border-radius: var(--radius); padding: 10px 14px; margin: 12px 0 0; display: flex; gap: 10px; align-items: flex-start; font-size: 13px; border: 1px solid transparent; }
.banner.demo { background: var(--demo-bg); color: var(--demo); border-color: color-mix(in srgb, var(--demo) 30%, transparent); }
.banner.error { background: var(--crit-bg); color: var(--crit); border-color: color-mix(in srgb, var(--crit) 45%, transparent); }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.banner.info { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.banner b { font-weight: 700; }

.grid { display: grid; gap: 14px; }
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin: 16px 0; }
/* 费用卡片内容多：占两列，避免在 13–14 寸笔记本（主区约 1300px）上被压成一长条 */
.grid.kpi .kpi-wide { grid-column: span 2; }
/* 总览 6 张指标卡（费用占两格 = 7 格）：主区够宽时固定排成一行，不换行（1512px 屏幕主区约 1240px） */
@media (min-width: 1400px) {
  .grid.kpi.ov-kpi { grid-template-columns: repeat(7, minmax(0, 1fr)); }
}
.grid.two { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
/* 总览：当前告警在前（窄屏在上、宽屏在左）；消耗排名表列多，宽屏占较宽的右栏；主区不够宽时上下排列，表格占满整行 */
.grid.two.ov-split { grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); }
@media (max-width: 1800px) {
  .grid.two.ov-split { grid-template-columns: 1fr; }
}
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card h2 { font-size: 15px; margin: 0 0 12px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
/* h2 是两端对齐的 flex：标题文字（含 term() 名词）整体放进 .h2-title，避免被拆成几段拉开 */
.card h2 .h2-title { display: inline; }
.card h3 { font-size: 13.5px; margin: 16px 0 8px; }
.card .hint { color: var(--muted); font-size: 12.5px; }
.kpi .card { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12.5px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -.01em; }
.kpi .foot { color: var(--muted); font-size: 12px; margin-top: 2px; }
.section { margin-top: 18px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text); font-weight: 600; font-size: 12px; background: var(--panel-2); position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
td.nowrap, th.nowrap { white-space: nowrap; }
/* 规则列表：指标多的规则（账户费用模板有 11 项）最多显示 3 行，完整内容悬停查看；时长列不被挤成一字一行 */
td.rule-metrics { min-width: 220px; max-width: 420px; }
.clamp3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
td.rule-duration { min-width: 140px; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; line-height: 20px; }
.badge.critical, .badge.failed, .badge.dead, .badge.error { background: var(--crit-bg); color: var(--crit); }
.badge.warning, .badge.pending, .badge.partial, .badge.retryable, .badge.timeout { background: var(--warn-bg); color: var(--warn); }
.badge.ok, .badge.sent, .badge.success, .badge.valid, .badge.resolved, .badge.supported, .badge.active { background: var(--ok-bg); color: var(--ok); }
.badge.unknown, .badge.missing, .badge.cancelled, .badge.disabled, .badge.draft, .badge.unsupported, .badge.unverified, .badge.skipped, .badge.closed_config, .badge.stale_claim { background: var(--unknown-bg); color: var(--unknown); }
/* 说明性标签不可点，不用蓝色（蓝色只给真正能点的链接和按钮） */
.badge.info, .badge.observe, .badge.claimed, .badge.recovering, .badge.provisional, .badge.firing-ok { background: var(--panel-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }
.badge.simulated, .badge.demo { background: var(--panel-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }
.badge.firing { background: var(--crit-bg); color: var(--crit); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 6px 12px; border-radius: 8px; font: inherit; font-size: 13px; cursor: pointer; }
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(.95); }
.btn.danger { color: var(--crit); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

input, select, textarea { font: inherit; font-size: 13px; color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 7px; padding: 6px 8px; }
textarea { width: 100%; min-height: 60px; }
input[type=number] { width: 110px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
label.field > span { font-weight: 600; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 12px; margin: 12px 0; }
legend { font-weight: 600; font-size: 13px; padding: 0 6px; }
.cond { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 6px 0; }
.cond select { max-width: 260px; }

.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; font-size: 13px; }
.kv > div:nth-child(odd) { color: var(--muted); }
.timeline { list-style: none; padding: 0; margin: 0; border-left: 2px solid var(--border); }
.timeline li { position: relative; padding: 4px 0 12px 16px; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: 10px; width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.timeline li.open::before, .timeline li.escalate::before { background: var(--crit); }
.timeline li.resolve::before { background: var(--ok); }
.timeline li.ack::before, .timeline li.silence::before { background: var(--info); }
.timeline .t { color: var(--muted); font-size: 12px; }

.chart { width: 100%; height: 150px; display: block; }
.chart-card { padding: 12px 14px; }
.chart-card .title { display: flex; justify-content: space-between; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.chart-card .title span { color: var(--muted); font-weight: 400; font-size: 12px; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 12px; }
.legend { display: flex; gap: 12px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

.empty { color: var(--muted); padding: 18px; text-align: center; font-size: 13px; }
.explain { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.explain .reason { font-weight: 600; margin-bottom: 6px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--crit); }
.okc { color: var(--ok); }
.toast { position: fixed; right: 20px; bottom: 20px; background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; font-size: 13px; max-width: 420px; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
pre { background: var(--panel-2); padding: 10px; border-radius: 8px; overflow: auto; font-size: 12px; max-height: 320px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.tabs a { padding: 6px 12px; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--text); border-color: var(--accent); font-weight: 600; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .main { padding: 0 14px 40px; }
  .grid.two { grid-template-columns: 1fr; }
  .grid.kpi .kpi-wide { grid-column: auto; }
}
.warnc { color: var(--warn); }

/* 命名空间多选组件（规则范围：指定 / 排除） */
.nsp { max-width: 640px; }
.nsp-anchor { position: relative; }
.nsp-label { font-weight: 600; margin-bottom: 4px; }
.nsp-trigger { display: flex; width: 100%; justify-content: space-between; align-items: center; gap: 8px; font: inherit; font-size: 13px; color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 7px; padding: 6px 10px; cursor: pointer; text-align: left; }
.nsp.open .nsp-trigger { border-color: var(--accent); }
.nsp-panel { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; margin-top: 4px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12); padding: 8px; }
.nsp-search { width: 100%; margin-bottom: 6px; }
.nsp-list { max-height: 300px; overflow-y: auto; overscroll-behavior: contain; }
.nsp-ghead { display: flex; align-items: center; gap: 8px; padding: 5px 6px; font-weight: 600; font-size: 12.5px; background: var(--panel-2); border-radius: 6px; margin-top: 4px; cursor: pointer; position: sticky; top: 0; }
.nsp-ghead[aria-disabled="true"] { cursor: default; opacity: .6; }
.nsp-gname { overflow-wrap: anywhere; }
.nsp-opt { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 4px 6px 4px 14px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.nsp-opt:hover, .nsp-ghead:hover { background: var(--info-bg); }
.nsp-opt.hl, .nsp-ghead.hl { outline: 2px solid var(--link); outline-offset: -2px; }
.nsp-opt[aria-disabled="true"] { opacity: .45; cursor: not-allowed; background: none; }
.nsp-box { width: 15px; height: 15px; border: 1px solid var(--muted); border-radius: 3px; display: inline-grid; place-items: center; font-size: 11px; line-height: 1; flex: none; }
.nsp-opt[aria-selected="true"] .nsp-box { background: var(--accent); border-color: var(--accent); color: #fff; }
.nsp-name { overflow-wrap: anywhere; }
.nsp-sel { margin-top: 6px; }
.nsp-tags { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.nsp-tag { display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 4px 2px 10px; font-size: 12px; max-width: 100%; }
.nsp-x { border: 0; background: none; color: var(--muted); font-size: 15px; line-height: 1; cursor: pointer; padding: 0 4px; border-radius: 50%; }
.nsp-x:hover { color: var(--crit); background: var(--crit-bg); }
@media (max-width: 480px) {
  .nsp { max-width: none; }
  .nsp-list { max-height: 55vh; }
}

/* U2：环境标签、排名折叠与搜索 */
.badge.env-prod { background: transparent; color: var(--text); box-shadow: inset 0 0 0 1px var(--muted); } /* 生产：中性色，靠文字和边框区分（绿色只用于“正常”） */
.badge.env-test { background: var(--panel-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); } /* 橙 / 红只留给告警 */
.badge.env-none { background: var(--unknown-bg); color: var(--unknown); font-weight: 500; }
.rank-tools { display: flex; gap: 8px; align-items: center; margin: 4px 0 8px; flex-wrap: wrap; }
.rank-tools input[type="search"] { min-width: 220px; flex: 1 1 220px; max-width: 360px; }
.rank-more { margin-top: 6px; }
.rank-more a { cursor: pointer; }
.cost-month { font-weight: 600; }
.cost-window { display: block; font-size: 11px; }
.env-edit { margin-left: 4px; padding: 0 6px; font-size: 11px; line-height: 18px; }
.env-select { font-size: 12px; padding: 1px 4px; }
th[title] { cursor: help; }
/* U3：规则分组 */
.rule-group { margin-bottom: 12px; }
.rule-group > summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
.card-small { font-size: 12px; color: var(--muted, #6b7280); margin-top: 2px; }
/* U3：系统状态（运维）放在侧栏最下方，与业务页面分隔 */
.nav a.nav-ops { margin-top: 14px; border-top: 1px solid var(--border, #e5e7eb); padding-top: 12px; border-radius: 0 0 8px 8px; color: var(--muted); }
/* U6：图表轴线/网格/缺口使用主题变量（覆盖 SVG 属性里的 currentColor + 透明度） */
.chart text { fill: var(--chart-axis); opacity: 1; }
.chart line { stroke: var(--chart-grid); opacity: 1; }
.chart rect { fill: var(--chart-gap); opacity: 1; }
/* U6：侧栏底部主题切换（浅色 / 深色 / 跟随系统） */
.theme-switch { margin-top: 10px; padding: 10px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.theme-switch .seg { display: flex; margin-top: 6px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.theme-switch button { flex: 1; border: 0; background: var(--panel); color: var(--text); font: inherit; font-size: 12px; padding: 4px 0; cursor: pointer; }
.theme-switch button + button { border-left: 1px solid var(--border); }
.theme-switch button:hover { background: var(--panel-2); }
.theme-switch button[aria-pressed="true"] { background: var(--warn-bg); color: var(--accent-ink); font-weight: 600; }

/* M1 多账号：顶栏账号切换器、账号管理页 */
.acct-bar:empty { display: none; }
.acct-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 8px 12px; margin-top: 12px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; }
.acct-switch { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.acct-switch select { min-width: 200px; max-width: 100%; font-weight: 400; }
td.perm { text-align: center; }
.perm-ok { color: var(--ok); font-weight: 700; }
.perm-miss { color: var(--crit); font-weight: 700; }
.perm-err { color: var(--warn); font-weight: 700; }
.acct-actions { white-space: nowrap; }
.acct-actions .btn { margin: 2px 0; }
.acct-panel { margin-top: 12px; background: var(--panel-2); }
@media (max-width: 640px) {
  .acct-switch select { min-width: 0; width: 100%; }
  .acct-actions { white-space: normal; }
}
.warn-text { color: var(--warn); }
/* M2c：排行“最新窗口”列——窄屏时时间与质量标签换行，再窄时只显示时间（质量见悬停），不出现整页横向滚动 */
/* 不能排序的列头用普通深色文字，只有能点的排序列头是蓝色链接 */
#rank-table th.th-plain { color: var(--text); }
/* 能排序的列头：不用蓝色，悬停显示手型和下划线；当前排序列加粗，其他列浅灰 ↕ */
#rank-table th a.rank-sort { color: var(--text); text-decoration: none; cursor: pointer; white-space: nowrap; }
#rank-table th a.rank-sort:hover { text-decoration: underline; }
#rank-table th a.rank-sort.sorted { font-weight: 700; }
#rank-table th .sort-idle { color: var(--muted); opacity: .55; font-weight: 400; }
#rank-table td.latest-cell { white-space: normal; min-width: 0; }
#rank-table td.latest-cell .latest-time { white-space: nowrap; }
@media (max-width: 860px) {
  #rank-table td.latest-cell .latest-q { display: none; }
}
/* M2c：新建规则向导 */
.wiz-grid { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.wiz-card { display: flex; flex-direction: column; }
.wiz-th input { max-width: 160px; }
#wiz-adv summary { cursor: pointer; margin-top: 12px; font-weight: 600; }

/* 专业名词：虚线下划线 + 帮助光标，悬停显示解释（GLOSSARY / term()） */
.term { border-bottom: 1px dashed currentColor; cursor: help; text-decoration: none; }
.badge[title] { cursor: help; }

/* 本账号已由专属版本替代的“所有账号”规则：整行变灰（仍可点进去查看） */
tr.rule-overridden td { opacity: .55; }
