:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e4e7ec;
  --text: #1d2433;
  --muted: #6b7385;
  --accent: #2f6fed;
  --accent-soft: #e8effd;
  /* 国内习惯：红涨绿跌 */
  --up: #e5383b;
  --down: #12a150;
  --warn-bg: #fff4e0;
  --warn-text: #9a5b00;
  --grid: #eef0f3;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #171b23;
    --border: #262c38;
    --text: #e6e9ef;
    --muted: #8a93a6;
    --accent: #6c9cff;
    --accent-soft: #1c2840;
    --up: #ff5a5f;
    --down: #2fc26b;
    --warn-bg: #3a2c12;
    --warn-text: #f3c26b;
    --grid: #1f242e;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); font-weight: 400; }
.up { color: var(--up); }
.down { color: var(--down); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-size: 17px; font-weight: 650; }
.logo { color: var(--accent); }
.meta { display: flex; gap: 12px; align-items: center; font-size: 13px; }
.badge { padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }

.layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 300px;
  gap: 16px; padding: 16px 20px; max-width: 1600px; margin: 0 auto;
}

.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 0; align-self: start; position: sticky; top: 70px;
  max-height: calc(100vh - 86px); overflow: auto;
}
.side-title { padding: 4px 14px 8px; font-weight: 600; display: flex; justify-content: space-between; font-size: 13px; }
#cat-tree { list-style: none; margin: 0; padding: 0; }
#cat-tree li button {
  width: 100%; display: flex; justify-content: space-between; gap: 8px;
  padding: 6px 14px; border: 0; background: none; text-align: left;
}
#cat-tree li.root button { font-weight: 650; margin-top: 6px; }
#cat-tree li.child button { padding-left: 26px; }
#cat-tree li button:hover { background: var(--accent-soft); }
#cat-tree li.on button { background: var(--accent-soft); color: var(--accent); }
#cat-tree .chg { font-size: 12px; font-variant-numeric: tabular-nums; }

.main { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; gap: 8px; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0; }

.insights { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.insight {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--muted);
  border-radius: 8px; padding: 10px 12px; cursor: pointer; font-size: 13px;
}
.insight.lv-strong_up, .insight.lv-up { border-left-color: var(--up); }
.insight.lv-down { border-left-color: var(--down); }
.insight b { display: block; font-size: 14px; margin-bottom: 2px; }
.insight:hover { border-color: var(--accent); }

.chart-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.crumb { font-size: 12px; color: var(--muted); }
.crumb a { color: var(--accent); cursor: pointer; text-decoration: none; }
.range, .tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.range button, .tabs button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 6px; padding: 3px 10px; font-size: 13px;
}
.range button.on, .tabs button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

.stats { display: flex; flex-wrap: wrap; gap: 6px 22px; margin: 12px 0; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }

.chart-wrap { position: relative; }
#chart { height: 380px; }
.legend {
  position: absolute; top: 6px; left: 8px; right: 64px; z-index: 2; font-size: 12px; pointer-events: none;
  font-variant-numeric: tabular-nums; color: var(--muted);
}
.legend b { color: var(--text); font-weight: 600; }
.note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 500; text-align: left; cursor: pointer; user-select: none; }
th.num { text-align: right; }
th.sorted::after { content: " ▾"; }
th.sorted.asc::after { content: " ▴"; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--accent-soft); }
td .sub { font-size: 12px; color: var(--muted); }

.rank { align-self: start; position: sticky; top: 70px; }
.rank-list { list-style: none; padding: 0; margin: 12px 0 0; counter-reset: r; }
.rank-list li {
  display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; align-items: center;
  padding: 6px 4px; border-radius: 6px; cursor: pointer; counter-increment: r;
}
.rank-list li::before { content: counter(r); color: var(--muted); font-size: 12px; text-align: center; }
.rank-list li:hover { background: var(--accent-soft); }
.rank-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.rank-list .nm .sub { display: block; font-size: 11px; color: var(--muted); }
.rank-list .val { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 13px; }

.logout { border: 1px solid var(--border); background: var(--surface); border-radius: 6px; padding: 2px 10px; font-size: 13px; }
.logout:hover { border-color: var(--accent); color: var(--accent); }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px 24px; display: flex; flex-direction: column; gap: 14px;
}
.login-card .brand { font-size: 20px; text-align: center; margin-bottom: 4px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  font: inherit; font-size: 15px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px;
}
.login-card input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.login-card button[type=submit] {
  margin-top: 4px; border: 0; border-radius: 8px; padding: 10px; font-size: 15px; font-weight: 600;
  background: var(--accent); color: #fff;
}
.login-card button[disabled] { opacity: 0.6; cursor: wait; }
.code-row { display: flex; gap: 8px; }
.code-row input { flex: 1; min-width: 0; }
.login-card .send-code {
  flex: none; border: 1px solid var(--accent); color: var(--accent); background: var(--surface);
  border-radius: 8px; padding: 0 12px; font-size: 14px; white-space: nowrap;
}
.login-card .send-code[disabled] { border-color: var(--border); color: var(--muted); cursor: default; }
.login-msg { color: var(--up); font-size: 13px; min-height: 1em; margin: 0; }
.login-msg.ok { color: var(--down); }

@media (max-width: 1200px) {
  .layout { grid-template-columns: 200px minmax(0, 1fr); }
  .rank { grid-column: 2; position: static; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; gap: 12px; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .sidebar { position: static; max-height: none; padding: 8px; }
  .side-title { display: none; }
  #cat-tree { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
  #cat-tree li button { white-space: nowrap; border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; margin: 0; }
  #cat-tree li.child button { padding-left: 10px; }
  #cat-tree li.root button { margin-top: 0; }
  .rank { grid-column: auto; }
  #chart { height: 300px; }
}
