:root {
    color-scheme: dark;   /* 默认暗色; 亮色见下方 [data-theme="light"] */
    --surface-0: #111110; --surface-1: #1a1a19; --surface-2: #232322; --line: #2e2e2c;
    --text-primary: #f3f2ee; --text-secondary: #c3c2b7; --text-muted: #8b8a82; --text-dim: #6b6a63;
    --s-wan: #3987e5;      /* slot 1 blue    : WAN / 公网 */
    --s-ovpn: #d95926;     /* slot 2 orange  : OpenVPN 服务端用户 */
    --s-wg: #199e70;       /* slot 3 aqua    : WireGuard peers */
    --s-proxy: #c98500;    /* slot 4 yellow  : trojan/anytls 入口 + Clash 出口 */
    --s-ts: #d55181;       /* slot 5 magenta : tailnet */
    --s-branch: #9085e9;   /* slot 7 violet  : 分支站点隧道 */
    --good: #2f9e63; --warn: #c98500; --bad: #e66767;
    font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, "Noto Sans Mono CJK SC", monospace;
  }

  /* 亮色主题 —— 语义色都重新取过, 保证在白底上对比度够 (暗色那套直接放白底会发飘) */
  :root[data-theme="light"] {
    color-scheme: light;
    --surface-0: #faf9f7; --surface-1: #ffffff; --surface-2: #f1efeb; --line: #e2dfd8;
    --text-primary: #1b1a17; --text-secondary: #4b4941; --text-muted: #7c7970; --text-dim: #a5a29a;
    --s-wan: #1f6fd0; --s-ovpn: #c2470f; --s-wg: #0d7a54; --s-proxy: #92620a;
    --s-ts: #b8386a; --s-branch: #6152cf;
    --good: #1d7a48; --warn: #92620a; --bad: #c23434;
  }

  /* 主题切换按钮 + 语言切换 */
  .topbtn { background: var(--surface-1); border: 1px solid var(--line); color: var(--text-secondary);
            border-radius: 6px; padding: 3px 9px; font: inherit; font-size: 11px; cursor: pointer; }
  .topbtn:hover { color: var(--text-primary); border-color: var(--text-muted); }
  .topright { margin-left: auto; display: flex; gap: 6px; align-items: center; }
  * { box-sizing: border-box; }
  html, body { margin: 0; background: var(--surface-0); color: var(--text-primary); }
  body { padding: 18px 22px 32px; font-size: 13px; line-height: 1.45; }
  header { display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
  header h1 { font-size: 18px; margin: 0; font-weight: 600; letter-spacing: .02em; }
  header .sub { color: var(--text-muted); font-size: 12px; }
  header .demo { margin-left: auto; color: var(--good) !important; border-color: var(--good) !important; font-size: 11px; color: var(--warn); border: 1px solid var(--warn); border-radius: 4px; padding: 2px 8px; }
  .grid { display: grid; gap: 14px; }
  .tiles { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
  .tile { background: var(--surface-1); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; }
  .tile .k { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
  .tile .v { font-size: 26px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
  .tile .v small { font-size: 12px; color: var(--text-secondary); font-weight: 400; margin-left: 4px; }
  .tile .d { color: var(--text-secondary); font-size: 11px; margin-top: 4px; }
  .main { grid-template-columns: minmax(0, 2.1fr) minmax(300px, 1fr); margin-top: 14px; }
  .panel { background: var(--surface-1); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; min-width: 0; }
  .panel h2 { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin: 0 0 8px; letter-spacing: .04em; text-transform: uppercase; display: flex; gap: 10px; align-items: center; }
  .panel h2 .hint { font-weight: 400; color: var(--text-muted); text-transform: none; letter-spacing: 0; }
  .legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 11px; color: var(--text-secondary); margin: 6px 0 2px; }
  .legend i { display: inline-block; width: 18px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
  svg.map { width: 100%; height: auto; display: block; overflow: visible; }
  svg.map text { font-family: inherit; fill: var(--text-primary); }
  svg.map .node rect { fill: var(--surface-2); stroke: var(--line); stroke-width: 1; rx: 6; }
  svg.map .node.core rect { stroke: var(--text-muted); }
  svg.map .node .name { font-size: 11.5px; font-weight: 600; }
  svg.map .node .sub { font-size: 9.5px; fill: var(--text-muted); }
  svg.map .edge-bg { stroke: var(--line); stroke-width: 1; fill: none; }
  svg.map .edge { fill: none; stroke-linecap: round; opacity: .95; pointer-events: none; }
  svg.map .edge-bg { pointer-events: none; }
  /* 悬停命中区: 不可见但比可见线宽得多, 空闲的细线也能停得住 */
  svg.map .edge-hit { stroke: transparent; stroke-width: 14; fill: none; pointer-events: stroke; }
  svg.map .edge.idle { opacity: .18; }
  svg.map .elabel { font-size: 10px; fill: var(--text-secondary); font-variant-numeric: tabular-nums; paint-order: stroke; stroke: var(--surface-1); stroke-width: 3px; }
  svg.map .peer rect { fill: var(--surface-2); stroke: var(--line); rx: 4; }
  svg.map .peer .name { font-size: 10px; }
  svg.map .peer .sub { font-size: 9px; fill: var(--text-muted); }
  svg.map .dot { r: 3.2; }
  /* 长列表面板: 列表自身不参与撑高(height:0), 只填满由同行更高面板决定的行高;
     滚动条隐藏但仍可滚 (Firefox: scrollbar-width, WebKit: ::-webkit-scrollbar) */
  #p-events, #p-lan { display: flex; flex-direction: column; }
  .feed { list-style: none; margin: 0; padding: 0; font-size: 12px;
          flex: 1 1 auto; height: 0; min-height: 320px; overflow-y: auto; scrollbar-width: none; }
  .feed::-webkit-scrollbar, .tscroll::-webkit-scrollbar { display: none; }
  .feed li { display: grid; grid-template-columns: 52px 10px 1fr; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); align-items: start; }
  .feed li .t { color: var(--text-muted); font-variant-numeric: tabular-nums; }
  .feed li .c { width: 10px; height: 10px; border-radius: 50%; margin-top: 4px; }
  .feed li .m b { font-weight: 600; }
  .feed li.new { animation: flash 1.6s ease-out; }
  @keyframes flash { from { background: var(--surface-2); } to { background: transparent; } }
  .bottom { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); margin-top: 14px; }
  table { width: 100%; border-collapse: collapse; font-size: 12px; }
  th { text-align: left; color: var(--text-muted); font-weight: 500; font-size: 11px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
  td { padding: 5px 6px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; white-space: nowrap; }
  td.num, th.num { text-align: right; }
  .st { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
  .spark { width: 90px; height: 18px; vertical-align: middle; }
  .spark path { fill: none; stroke-width: 1.5; }
  .bar { height: 6px; border-radius: 3px; background: var(--s-wan); display: inline-block; vertical-align: middle; }
  .tooltip { position: fixed; pointer-events: none; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; font-size: 11.5px; color: var(--text-primary); z-index: 10; display: none; min-width: 180px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
  .tooltip .tt { color: var(--text-muted); font-size: 10.5px; margin-bottom: 4px; }
  .tooltip b { font-weight: 600; }
  .history { grid-template-columns: 1fr; margin-top: 14px; }
  svg.hist { width: 100%; height: 170px; display: block; }
  svg.hist .grid line { stroke: var(--line); stroke-width: 1; }
  svg.hist .axis text { fill: var(--text-muted); font-size: 10px; font-family: inherit; }
  svg.hist .area { opacity: .18; }
  svg.hist .ln { fill: none; stroke-width: 2; }
  svg.hist .cross { stroke: var(--text-muted); stroke-dasharray: 3 3; }
  .hw { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); margin-top: 14px; }
  .kv { display: grid; grid-template-columns: 1fr auto auto; gap: 4px 10px; font-size: 12px; align-items: center; }
  .kv .k { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .kv .v { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
  .kv .g { width: 84px; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
  .kv .g i { display: block; height: 100%; border-radius: 3px; }
  .hero { display: flex; align-items: baseline; gap: 8px; margin: 2px 0 8px; }
  .hero .n { font-size: 30px; font-weight: 600; font-variant-numeric: tabular-nums; }
  .hero .u { color: var(--text-secondary); }
  .hero .tag { margin-left: auto; font-size: 11px; padding: 2px 8px; border-radius: 4px; border: 1px solid var(--line); color: var(--text-secondary); }
  .tag.ok { color: var(--good); border-color: var(--good); } .tag.warn { color: var(--warn); border-color: var(--warn); } .tag.bad { color: var(--bad); border-color: var(--bad); }
  .sel { list-style: none; margin: 8px 0 0; padding: 0; font-size: 11px; color: var(--text-secondary); }
  .sel li { padding: 3px 0; border-top: 1px solid var(--line); display: flex; gap: 8px; }
  .sel li .t { color: var(--text-muted); font-variant-numeric: tabular-nums; min-width: 92px; }
  .cores { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 6px; }
  .core { background: var(--surface-2); border-radius: 6px; padding: 6px 8px; font-size: 11px; }
  .core b { display: block; font-size: 16px; font-variant-numeric: tabular-nums; }
  .core .f { color: var(--text-muted); }
  .rssi { display: inline-block; width: 46px; height: 6px; background: var(--surface-2); border-radius: 3px; vertical-align: middle; margin-right: 6px; overflow: hidden; }
  .rssi i { display: block; height: 100%; }
  .band { display: inline-block; font-size: 10px; padding: 0 5px; border-radius: 3px; border: 1px solid var(--line); color: var(--text-secondary); }
  .util { display: flex; gap: 14px; margin: 4px 0 10px; }
  .util > div { flex: 1; }
  .util .lbl { font-size: 11px; color: var(--text-secondary); display: flex; justify-content: space-between; }
  .util .g { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-top: 3px; }
  .util .g i { display: block; height: 100%; border-radius: 4px; }
  #p-netflow { display: flex; flex-direction: column; }
  svg.sankey { width: 100%; height: 300px; display: block; flex: none; }
  #p-netflow .tscroll { min-height: 90px; }     /* 这个面板的表是次要内容, 兜底矮一点 */
  .subh { margin: 12px 0 4px; }
  svg.sankey text { font-family: inherit; font-size: 11px; fill: var(--text-primary); }
  svg.sankey .small { font-size: 10px; fill: var(--text-muted); }
  svg.sankey path.flow { fill-opacity: .45; stroke: none; transition: d .6s; }
  svg.sankey path.flow:hover { fill-opacity: .8; }
  footer { color: var(--text-muted); font-size: 11px; margin-top: 18px; }
  @media (max-width: 1100px) { .main { grid-template-columns: 1fr; } }

  /* --- 响应式: 表格独立横向滚动, 页面本身不横向滚动 --- */
  .tw { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -2px; }
  /* overflow-y:auto 会让 overflow-x 隐式变成 auto, 冒出一条没用的横向滚动条 */
  .tscroll { flex: 1 1 auto; height: 0; min-height: 300px; overflow-y: auto; overflow-x: hidden;
             scrollbar-width: none; }
  /* 设备名 + IP 分两行, 设备列吸收剩余宽度并省略超长部分, 否则数值列会被挤出面板 */
  #top-table { table-layout: fixed; width: 100%; }
  #top-table td:first-child { max-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #top-table td:first-child .ipl { display: block; font-size: 10px; color: var(--text-dim); }
  /* 固定布局下每列宽度要给够, 否则 nowrap 的数字会互相压上去 (0.65 和 207.46 MB 粘一起) */
  #top-table th:not(:first-child), #top-table td:not(:first-child) { white-space: nowrap; padding-left: 10px; }
  #top-table th:nth-child(2), #top-table td:nth-child(2) { width: 6.6em; }
  #top-table th:nth-child(3), #top-table td:nth-child(3),
  #top-table th:nth-child(4), #top-table td:nth-child(4) { width: 7.2em; }
  th.s { cursor: pointer; user-select: none; white-space: nowrap; }
  th.s:hover { color: var(--text-primary); }
  th.s .ar { opacity: .35; font-size: 9px; margin-left: 2px; }
  th.s.on .ar { opacity: 1; }
  .tw table { min-width: 100%; }
  svg#map { min-width: 720px; }
  .mapwrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  @media (max-width: 820px) {
    body { padding: 10px; }
    .panel { padding: 10px; }
    table { font-size: 11px; }
    th, td { padding: 4px 4px; }
    td:first-child { max-width: 42vw; overflow: hidden; text-overflow: ellipsis; }
    .util { flex-direction: column; gap: 10px; }
    .rssi { width: 34px; }
    .spark { width: 56px; }
    header h1 { font-size: 15px; }
    .tiles { grid-template-columns: repeat(2, 1fr) !important; }
  }
  @media (max-width: 520px) {
    #wifi-table th:nth-child(6), #wifi-table td:nth-child(6),
    #wg-table th:nth-child(6), #wg-table td:nth-child(6),
    #br-table th:nth-child(6), #br-table td:nth-child(6),
    #vm-table th:nth-child(4), #vm-table td:nth-child(4) { display: none; }
  }

  /* 首屏自检 */
  #health { background: var(--surface-1); border: 1px solid var(--warn); border-left-width: 3px;
            border-radius: 8px; padding: 12px 14px; margin-bottom: 14px; }
  #health.hide { display: none; }
  #health h3 { margin: 0 0 8px; font-size: 13px; color: var(--warn); display: flex; align-items: center; gap: 8px; }
  #health h3 button { margin-left: auto; background: none; border: 1px solid var(--line);
            color: var(--text-muted); border-radius: 5px; font: inherit; font-size: 11px;
            padding: 2px 8px; cursor: pointer; }
  #health ul { margin: 0; padding: 0; list-style: none; }
  #health li { padding: 6px 0; border-top: 1px solid var(--line); font-size: 12px; }
  #health li:first-child { border-top: 0; }
  #health .lbl { color: var(--text-primary); font-weight: 600; }
  #health .hint { color: var(--text-secondary); display: block; margin-top: 2px; line-height: 1.6; }
  #health a { color: var(--s-wan); }

  /* WAN 曲线的时间档位按钮 */
  .rangebtns { margin-left: auto; display: inline-flex; gap: 4px; }
  .rangebtns button { background: var(--surface-2); border: 1px solid var(--line);
        color: var(--text-muted); border-radius: 5px; font: inherit; font-size: 11px;
        padding: 1px 8px; cursor: pointer; }
  .rangebtns button:hover { color: var(--text-primary); }
  .rangebtns button.on { background: var(--s-wan); border-color: var(--s-wan); color: #fff; }
  #p-wan24 h2 { display: flex; align-items: center; gap: 8px; }
