:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f7fafb;
  --ink: #1b2530;
  --muted: #6b7684;
  --line: #dde3ea;
  --accent: #0e7c8b;
  --accent-d: #0a5c68;
  --danger: #c0503c;
  --good: #2e7d51;
  --warn: #b5761a;
  --up: #2f6fd0;    /* 上行(藍) */
  --down: #2e9e5b;  /* 下行(綠) */
  --accent-wash: #e6f2f4;
  /* 結構性底色(暗色主題會覆寫) */
  --field-bg: #ffffff;
  --th-bg: #f0f3f6;
  --row-hover: #f7fafb;
  --ghost-bg: #ffffff;
  --ghost-hover: #eef6f7;
  --overlay: rgba(20,30,40,.5);
  --shadow: 0 20px 60px rgba(0,0,0,.3);
  --bad-bg: #fbeae6;
  --ok-bg: #e2f0e7;
  --sb-thumb: rgba(27,37,48,.22);
  --sb-thumb-hover: rgba(27,37,48,.42);
}

/* ── 暗色主題:覆寫 tokens + 少數寫死色 ─────────────────── */
:root[data-theme="dark"] {
  --bg: #0f151b;
  --surface: #182029;
  --surface-2: #1e2833;
  --ink: #e5eaf0;
  --muted: #9aa7b3;
  --line: #2a3742;
  --accent: #35b4c6;
  --accent-d: #7fd6e2;
  --danger: #e0705c;
  --good: #57b37f;
  --warn: #d6a24a;
  --up: #5a97e0;
  --down: #57b37f;
  --accent-wash: #123038;
  --field-bg: #10171e;
  --th-bg: #1e2833;
  --row-hover: #1e2833;
  --ghost-bg: #182029;
  --ghost-hover: #22303b;
  --overlay: rgba(0,0,0,.6);
  --shadow: 0 20px 60px rgba(0,0,0,.6);
  --bad-bg: #3a211b;
  --ok-bg: #16301f;
  --sb-thumb: rgba(229,234,240,.20);
  --sb-thumb-hover: rgba(229,234,240,.40);
}
:root[data-theme="dark"] button.danger { background: var(--surface); }
:root[data-theme="dark"] button.danger:hover { background: #2a1e1c; }
:root[data-theme="dark"] .banner.err { background: #3a211b; }
:root[data-theme="dark"] .banner.ok  { background: #16301f; }
:root[data-theme="dark"] .warn-tag  { color: #ef9a8c; background: #3a211b; border-color: #5a3128; }
:root[data-theme="dark"] .warn-text { color: #ef9a8c; }
:root[data-theme="dark"] .pill.gateway { background: #123038; color: var(--accent-d); }
:root[data-theme="dark"] .pill.manual  { background: #2a2333; color: #c3a6e0; }
:root[data-theme="dark"] .pill.on  { background: #16301f; color: var(--good); }
:root[data-theme="dark"] .pill.off { background: #222c35; color: var(--muted); }
:root[data-theme="dark"] .tag.ro-tag { background: #3a4650; }
:root[data-theme="dark"] .mini.ro { border-color: #3a4650; }
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", sans-serif;
  font-size: 15px; line-height: 1.6;
}
a { color: var(--accent-d); }

/* nav */
.nav {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  padding: 0 18px; position: sticky; top: 0; z-index: 5;
}
.nav .brand { font-weight: 700; margin-right: 18px; color: var(--accent-d); }
.nav a {
  padding: 14px 14px; text-decoration: none; color: var(--muted);
  border-bottom: 2px solid transparent; font-weight: 600;
}
.nav a.active { color: var(--accent-d); border-bottom-color: var(--accent); }

.wrap { max-width: 1080px; margin: 22px auto; padding: 0 18px; }
h2 { margin: 0 0 4px; font-size: 1.3rem; }
.sub { color: var(--muted); margin: 0 0 18px; font-size: .9rem; }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 18px; margin-bottom: 18px;
}
.row { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: .8rem; color: var(--muted); font-weight: 600; }
input, select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--field-bg); color: var(--ink); min-width: 160px;
}
textarea { width: 100%; resize: vertical; }
.warn-tag { display: inline-block; font-size: .72rem; font-weight: 700; color: #b4232a; background: #fdecec; border: 1px solid #f3c2c4; border-radius: 6px; padding: 1px 7px; }
.warn-text { color: #b4232a; font-size: .82rem; font-weight: 600; padding: 6px 0; }
.note-line { font-size: .85rem; color: var(--muted); margin: 4px 0 12px; }
.perm-group { border: 1px solid var(--line, #dcdcdc); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; }
.perm-group-title { font-size: .72rem; font-weight: 700; color: var(--muted, #667); letter-spacing: .04em; margin-bottom: 4px; }
.perm-group .checks { margin: 0; }
.perm-group .checks label .muted { font-weight: 400; font-size: .8rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  font: inherit; font-weight: 600; cursor: pointer;
  padding: 8px 16px; border-radius: 8px; border: 1px solid var(--accent);
  background: var(--accent); color: #fff;
}
button:hover { background: var(--accent-d); }
button.ghost { background: var(--ghost-bg); color: var(--accent-d); }
button.ghost:hover { background: var(--ghost-hover); }
button.danger { background: #fff; color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: #fbeeeb; }
button.sm { padding: 4px 10px; font-size: .85rem; }

.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.checks label { display: flex; align-items: center; gap: 6px; font-weight: 500; cursor: pointer; }
.checks input { min-width: 0; }

table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.tbl-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
thead th { background: var(--th-bg); color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--row-hover); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent-d); }
tr.row-consumed td { opacity: .5; }
tr.row-consumed input[type=checkbox] { cursor: not-allowed; }

.pill { font-size: .74rem; padding: 2px 9px; border-radius: 20px; font-weight: 700; }
.pill.gateway { background: #e1f0f2; color: var(--accent-d); }
.pill.manual { background: #eee9f3; color: #6a4b8c; }
.pill.on { background: #e2f0e7; color: var(--good); }
.pill.off { background: #eef1f4; color: var(--muted); }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.green { background: var(--good); }
.dot.red { background: var(--danger); }

.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-weight: 600; }
.banner.err { background: #f6e3dd; color: var(--danger); }
.banner.ok { background: #e2f0e7; color: var(--good); }
.muted { color: var(--muted); }
.right { margin-left: auto; }
.spacer { flex: 1; }

/* autocomplete */
.ac { position: relative; }
.ac ul {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 10;
  margin: 4px 0 0; padding: 4px; list-style: none; max-height: 260px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.1);
}
.ac li { padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.ac li:hover, .ac li.hl { background: var(--ghost-hover); }
.ac li .s { font-weight: 600; }
.ac li .n { color: var(--muted); font-size: .85rem; margin-left: 6px; }

.empty { padding: 30px; text-align: center; color: var(--muted); }
.formgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.nowrap { white-space: nowrap; }
.nowrap button + button { margin-left: 6px; }

/* 分頁 */
.pager { display: flex; align-items: center; gap: 14px; justify-content: center; margin-top: 14px; }

/* 日期切換 */
.date-nav { display: flex; align-items: center; gap: 4px; }
.date-nav input { min-width: 140px; }
.date-nav button.sm { padding: 6px 9px; min-width: 0; }

/* lightbox */
.lightbox { position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50; overflow-y: auto; }
.lb-card { background: var(--surface); border-radius: 14px; width: min(720px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.lb-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.lb-body { padding: 20px; }

/* 傳輸量 bar */
.usage { margin-bottom: 18px; }
.usage-lbl { font-weight: 600; margin-bottom: 6px; display: flex; flex-wrap: wrap; gap: 14px; align-items: baseline; }
.usage-lbl .up-txt { color: var(--up); font-weight: 600; font-size: .88rem; }
.usage-lbl .down-txt { color: var(--down); font-weight: 600; font-size: .88rem; }
.bar { height: 14px; display: flex; background: var(--th-bg); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.bar .seg { height: 100%; transition: width .4s; }
.bar .seg.up { background: var(--up); }
.bar .seg.down { background: var(--down); }
.usage-scale { display: flex; justify-content: space-between; font-size: .82rem; margin-top: 4px; }

/* 詳細資訊:三張卡 + key-value 網格 */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.mini { border: 1px solid var(--line); border-radius: 10px; padding: 14px; background: var(--surface-2, #f7fafb); }
.mini-h { font-weight: 700; color: var(--accent-d); margin-bottom: 10px; display: flex; align-items: center; }
.kv-grid { display: grid; grid-template-columns: max-content 1fr; gap: 8px 14px; font-size: .9rem; }
.kv-grid .k { color: var(--muted); font-weight: 600; }
.kv-grid .v { word-break: break-all; }

/* Block 分組(記錄查詢)*/
.block-group { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.bg-head { font-weight: 700; margin-bottom: 6px; display: flex; align-items: center; gap: 10px; }

/* Block 清單 chips(欄位定義)*/
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 6px 14px; border: 1px solid var(--line); border-radius: 20px; background: var(--surface); color: var(--ink); font-weight: 600; cursor: pointer; }
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* 詳細:客戶(醒目)/ read-only(雙框)/ 雙欄版面 */
.mini.editable { border: 2px solid var(--accent); background: var(--accent-wash); }
.mini.ro { border: 3px double #9aa7b3; background: var(--surface); }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.detail-grid .col { display: flex; flex-direction: column; gap: 14px; }
@media (max-width: 640px) { .detail-grid { grid-template-columns: 1fr; } }
.kv-grid.cols2 { grid-template-columns: max-content 1fr max-content 1fr; }
@media (max-width: 520px) { .kv-grid.cols2 { grid-template-columns: max-content 1fr; } }
.tag { font-size: .68rem; font-weight: 700; padding: 2px 8px; border-radius: 10px; margin-left: 8px; background: var(--accent); color: #fff; }
.tag.ro-tag { background: #9aa7b3; }

/* 登入畫面 */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px 26px; width: min(360px, 100%); box-shadow: var(--shadow); }
.login-brand { font-size: 1.4rem; font-weight: 800; text-align: center; color: var(--accent-d); margin-bottom: 2px; }
.login-card .field { width: 100%; }
.login-card input { width: 100%; min-width: 0; }

/* nav 右側(使用者/帳號管理/登出)*/
.nav-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.nav-user { font-weight: 600; color: var(--ink); font-size: .9rem; }

/* 詳細:整體上次更新時間(置底)*/
.update-time { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); text-align: right; color: var(--muted); font-size: .9rem; }
.update-time b { color: var(--ink); margin-left: 4px; }

/* 主題切換(太陽/月亮)*/
.theme-toggle { display: inline-flex; gap: 2px; }
.theme-toggle .ticon {
  padding: 4px 8px; min-width: 0; font-size: .95rem; line-height: 1;
  border: 1px solid var(--line); background: var(--ghost-bg); color: var(--muted); border-radius: 8px;
}
.theme-toggle .ticon:hover { background: var(--ghost-hover); }
.theme-toggle .ticon.on { color: var(--accent-d); border-color: var(--accent); background: var(--accent-wash); }

/* ── 儀錶板 ─────────────────────────────────────── */
.sel-chips { flex: 1; min-width: 200px; align-items: center; }
.sel-chips .chip { padding: 4px 10px; font-size: .85rem; display: inline-flex; align-items: center; }
.sel-chips .chip .x { margin-left: 8px; opacity: .6; font-weight: 700; }
.sel-chips .chip .x:hover { opacity: 1; }
.ok-tag { display: inline-block; font-size: .72rem; font-weight: 700; color: var(--good); background: var(--ok-bg); border-radius: 6px; padding: 1px 7px; }

.dash-cards { display: flex; gap: 14px; overflow-x: auto; padding: 2px 2px 10px; margin-bottom: 18px; scroll-snap-type: x proximity; }
.dash-card {
  flex: 0 0 300px; max-height: 540px; overflow-y: auto; scroll-snap-align: start; cursor: pointer;
  scrollbar-gutter: stable;   /* 預留捲軸寬度:展開詳細數值後不會因捲軸出現而跳動 */
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px;
}
.dash-card:hover { border-color: var(--accent); }
.dash-card.active { border: 2px solid var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); padding: 13px; }
.dc-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 1.05rem; }
.dc-meta { font-size: .8rem; margin-top: 2px; }
.dc-status { margin-top: 10px; display: flex; flex-direction: column; gap: 3px; }
.st-row { display: flex; justify-content: space-between; gap: 10px; padding: 5px 8px; border-radius: 6px; font-size: .88rem; }
.st-row .k { color: var(--muted); white-space: nowrap; }
.st-row .v { font-weight: 600; text-align: right; }
.st-row.bad { background: var(--bad-bg); }
.st-row.bad .v { color: var(--danger); }
.dc-sub { margin: 12px 0 4px; font-size: .75rem; font-weight: 700; color: var(--muted); letter-spacing: .04em; }
.mini-tbl { font-size: .82rem; }
.mini-tbl td { padding: 3px 6px; border-bottom: 1px solid var(--line); white-space: normal; }
.mini-tbl tr.bad td { background: var(--bad-bg); color: var(--danger); font-weight: 600; }
.mini-tbl tbody tr:hover { background: transparent; }

.pick-list { max-height: 52vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.pick-item { display: grid; grid-template-columns: 26px 1.3fr 1fr 1fr; gap: 8px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); cursor: pointer; }
.pick-item:last-child { border-bottom: 0; }
.pick-item:hover { background: var(--row-hover); }
.pick-item.on { background: var(--accent-wash); }
.pick-item.pick-head { cursor: default; position: sticky; top: 0; background: var(--th-bg); font-size: .76rem; color: var(--muted); font-weight: 700; }
.pk-check { color: var(--accent-d); font-weight: 800; text-align: center; }

.dash-chart { margin-top: 14px; }
.chart-info { font-size: .85rem; color: var(--muted); min-height: 1.4em; margin-bottom: 4px; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-svg .dot-pt { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart-svg .dot-pt.bad { fill: var(--danger); }
.chart-svg .hover-line { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; opacity: .6; }
.chart-svg .grid line { stroke: var(--line); }
.chart-svg .axis text { fill: var(--muted); font-size: 11px; }
.chart-svg .thr line { stroke: var(--warn); stroke-dasharray: 5 4; }
.chart-svg .thr text { fill: var(--warn); font-size: 11px; }
@media (max-width: 640px) { .dash-card { flex-basis: 260px; } }
.expand-bar { display: flex; align-items: center; gap: 16px; margin: -8px 0 18px; }
.expand-btn { display: inline-flex; align-items: center; gap: 6px; background: var(--ghost-bg); color: var(--accent-d); border: 1px solid var(--line); padding: 6px 14px; }
.expand-btn:hover { background: var(--ghost-hover); }
.expand-btn svg { transition: transform .2s; }
.expand-btn svg.open { transform: rotate(180deg); }

/* ── 捲軸:與網頁同風格、跟隨主題、不顯示上下箭頭 ───────────── */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track, ::-webkit-scrollbar-corner { background: transparent; }
::-webkit-scrollbar-thumb { background-color: var(--sb-thumb); border-radius: 10px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background-color: var(--sb-thumb-hover); }
::-webkit-scrollbar-button { display: none; width: 0; height: 0; }
/* Firefox 不支援 ::-webkit-scrollbar → 用標準屬性(不可對 Chrome 套用,否則會覆蓋上面的自訂樣式) */
@supports not selector(::-webkit-scrollbar) {
  * { scrollbar-width: thin; scrollbar-color: var(--sb-thumb) transparent; }
}

/* ── 儀錶板 idx tooltip ─────────────────────────── */
.idx-row { cursor: default; }
.tip {
  position: fixed; z-index: 90; pointer-events: none; max-width: 300px; padding: 8px 11px; border-radius: 8px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.2);
  font-size: .82rem; line-height: 1.55;
}
.tip-h { font-weight: 700; margin-bottom: 3px; }
.tip .muted { display: inline-block; min-width: 2.8em; }
.tip-bad { color: var(--danger); }
.tip-cur { margin-top: 5px; padding-top: 5px; border-top: 1px solid var(--line); }
.tip-cur b.bad { color: var(--danger); }

/* ── 儀錶板圖表:縮放/捲動 ─────────────────────── */
.chart-svg { user-select: none; }
.chart-svg.pannable { cursor: grab; }
.chart-svg.dragging { cursor: grabbing; }
.chart-nav { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-top: 6px; font-size: .82rem; }
.chart-nav .hint { margin-left: auto; }
.pan-range { flex: 1; min-width: 160px; padding: 0; border: 0; background: transparent; accent-color: var(--accent); }

/* ── 儀表板(異常總覽)─────────────────────────── */
.ov-grid { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 18px; align-items: start; }
@media (max-width: 820px) { .ov-grid { grid-template-columns: minmax(0, 1fr); } }
.ov-grid > .card { margin-bottom: 0; }
.ov-title { font-weight: 700; margin-bottom: 10px; }
.ov-donut-wrap { position: relative; width: 200px; max-width: 100%; margin: 0 auto; }
.ov-donut { display: block; width: 100%; height: auto; }
.ov-track { fill: none; stroke: var(--line); stroke-width: 22; opacity: .35; }
.ov-seg { fill: none; stroke-width: 22; transition: opacity .15s; }
.ov-seg.bad { stroke: var(--danger); }
.ov-seg.ok { stroke: var(--good); }
.ov-seg.none { stroke: var(--muted); opacity: .45; }
.ov-seg.dim { opacity: .25; }
.ov-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; text-align: center; font-size: .82rem; }
.ov-big { font-size: 1.9rem; font-weight: 800; line-height: 1.1; color: var(--ink); }
.ov-big small { font-size: 1rem; font-weight: 600; color: var(--muted); }
.ov-big .bad { color: var(--danger); }
.ov-legend { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; margin-top: 12px; font-size: .88rem; }
.ov-lg { display: inline-flex; align-items: center; gap: 6px; transition: opacity .15s; }
.ov-lg.dim { opacity: .4; }
.ov-lg b { color: var(--ink); }
.ov-lg .sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.ov-lg .sw.bad { background: var(--danger); }
.ov-lg .sw.ok { background: var(--good); }
.ov-lg .sw.none { background: var(--muted); opacity: .45; }
.ov-foot { text-align: center; font-size: .78rem; margin-top: 10px; min-height: 1em; }
.ov-item { border: 1px solid var(--line); border-left: 4px solid var(--danger); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; cursor: pointer; }
.ov-item:hover, .ov-item:focus-visible { border-color: var(--accent); border-left-color: var(--danger); background: var(--row-hover); outline: none; }
.ov-item-h { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.ov-item-h .muted { font-size: .85rem; }
.ov-item-h .right { margin-left: auto; }
.ov-reasons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.ov-reason { font-size: .8rem; color: var(--danger); background: var(--bad-bg); border-radius: 6px; padding: 2px 8px; }
.ov-reason b { font-weight: 700; }
.ov-reason.more { color: var(--muted); background: var(--ghost-bg); }
.ov-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.ov-tag { min-width: 0; padding: 3px 10px; font-size: .8rem; font-weight: 600; border-radius: 14px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; }
.ov-tag b { margin-left: 4px; color: var(--muted); font-weight: 700; }
.ov-tag:hover { border-color: var(--danger); }
.ov-tag.on { background: var(--danger); border-color: var(--danger); color: #fff; }
.ov-tag.on b { color: #fff; opacity: .85; }
.ov-reason.hit { outline: 1.5px solid var(--danger); }
