:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7382;
  --border: #e2e5ea;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --accent-soft: #e8eefc;
  --ok: #1f7a4d;     --ok-bg: #e3f4ea;
  --warn: #8a5a00;   --warn-bg: #fdf1d8;
  --bad: #b3261e;    --bad-bg: #fbe5e3;
  --idle: #4b5565;   --idle-bg: #eceef2;
  --row-hover: #f2f5fb;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --text: #e6e8ec;
    --muted: #98a0ad;
    --border: #2e333c;
    --accent: #7a9cf5;
    --accent-text: #0f1420;
    --accent-soft: #232d45;
    --ok: #7fd3a5;     --ok-bg: #1d3328;
    --warn: #f0c46b;   --warn-bg: #3a2f17;
    --bad: #f39a92;    --bad-bg: #3d1f1d;
    --idle: #b3bac6;   --idle-bg: #2a2f38;
    --row-hover: #232833;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 4px 0 14px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 15px; margin: 0 0 10px; }
.card h2 + * { margin-top: 0; }
.card h2:not(:first-child) { margin-top: 18px; }

/* ---------- 版面 ---------- */
.topbar {
  display: flex; align-items: center; gap: 20px; padding: 0 20px; height: 52px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.topbar nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.topbar nav a { padding: 6px 10px; border-radius: 6px; color: var(--muted); white-space: nowrap; }
.topbar nav a:hover { background: var(--row-hover); text-decoration: none; color: var(--text); }
.topbar nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.me { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.me form { margin: 0; }
main { max-width: 1400px; margin: 0 auto; padding: 20px; }
main.center { min-height: 100vh; display: flex; align-items: center; justify-content: center; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.card.narrow { max-width: 520px; }
.card.login { width: 100%; max-width: 360px; }
.card.login h1 { margin-bottom: 2px; }
.warnbox { border-color: var(--warn); background: var(--warn-bg); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; align-items: start; }
.pagehead { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.pagehead p { margin: -8px 0 14px; }
.actions { display: flex; gap: 8px; align-items: center; }
.actions form { margin: 0; }
.crumb { margin: 0 0 6px; }

/* ---------- 文字 ---------- */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.count { font-size: 14px; font-weight: 400; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.pre { white-space: pre-line; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- 表單 ---------- */
input, select, textarea, button, .btn { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; width: 100%; }
button, .btn {
  display: inline-flex; align-items: center; gap: 4px; padding: 6px 14px; border-radius: 6px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); white-space: nowrap; color: var(--text);
}
button:hover, .btn:hover { background: var(--row-hover); text-decoration: none; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.primary:hover { background: var(--accent); filter: brightness(1.08); }
.danger { color: var(--bad); border-color: var(--bad); }
.ghost { border-color: transparent; background: transparent; }
.btn.small, button.small { padding: 2px 10px; font-size: 12px; }
button.link { background: none; border: 0; padding: 0; color: var(--accent); }
button.link:hover { text-decoration: underline; background: none; }
form.inline { display: inline; margin: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack label, .formgrid label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12.5px; }
.stack label input, .stack label select, .formgrid label input, .formgrid label select { color: var(--text); font-size: 14px; }
label.check { flex-direction: row !important; align-items: center; gap: 6px; color: var(--text) !important; font-size: 13px !important; white-space: nowrap; }
.formgrid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px 16px; margin-bottom: 8px; }
.formgrid .span2 { grid-column: span 2; }
.formgrid .span4 { grid-column: 1 / -1; }
.formactions { display: flex; gap: 8px; margin-top: 16px; }
.filters, .rowform { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.rowform { margin-bottom: 12px; }
.filters .grow, .rowform .grow { flex: 1 1 200px; }
.alert { padding: 8px 12px; border-radius: 6px; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.bad { background: var(--bad-bg); color: var(--bad); }

/* ---------- 表格 ---------- */
.tablewrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
th a { color: inherit; }
tbody tr:last-child td { border-bottom: 0; }
table.list tbody tr { cursor: pointer; }
table.list td { white-space: nowrap; }
table.list td.wrap { white-space: normal; min-width: 180px; }
table.list tbody tr:hover, table tbody tr:hover { background: var(--row-hover); }
table.compact td, table.compact th { padding: 5px 6px; }
td.empty { text-align: center; color: var(--muted); padding: 24px; }
tr.muted td { color: var(--muted); }
th.num { text-align: right; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.idle { background: var(--idle-bg); color: var(--idle); }
.badge.gone { background: var(--idle-bg); color: var(--muted); text-decoration: line-through; }
.tag { display: inline-block; padding: 0 6px; border-radius: 4px; font-size: 11.5px; font-weight: 600; border: 1px solid var(--border); }
.tag.it { color: var(--accent); }
.tag.fx { color: var(--warn); }

/* ---------- 總覽 ---------- */
.tiles { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-bottom: 16px; }
.tile {
  display: flex; flex-direction: column; gap: 2px; padding: 14px 16px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); color: var(--text);
}
.tile:hover { text-decoration: none; border-color: var(--accent); }
.tile .label { font-size: 12.5px; color: var(--muted); }
.tile .value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .value.warn { color: var(--warn); }
.tile .sub { font-size: 12px; color: var(--muted); }
.sums { display: flex; gap: 24px; flex-wrap: wrap; margin-bottom: 12px; color: var(--muted); }
.sums b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- 詳細頁 ---------- */
dl.fields { display: grid; grid-template-columns: 1fr; margin: 0; }
dl.fields > div { display: grid; grid-template-columns: 110px 1fr; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
dl.fields > div:last-child { border-bottom: 0; }
dt { color: var(--muted); font-size: 12.5px; }
dd { margin: 0; overflow-wrap: anywhere; }
ul.issues { margin: 0; padding-left: 18px; }
ul.issues li { margin: 4px 0; }
ul.issues li.done { color: var(--muted); text-decoration: line-through; }
.match { display: flex; flex-direction: column; gap: 4px; }
.match h2 { display: flex; gap: 8px; align-items: center; margin: 0; }
.match p { margin: 0 0 8px; }

.pager { display: flex; gap: 16px; justify-content: center; align-items: center; margin: 16px 0; }

@media (max-width: 1000px) {
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .formgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  main { padding: 12px 16px; }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; gap: 6px 12px; }
  .topbar nav { order: 3; flex-basis: 100%; }
  .me { margin-left: auto; }
  .formgrid { grid-template-columns: 1fr; }
  .formgrid .span2 { grid-column: auto; }
  dl.fields > div { grid-template-columns: 90px 1fr; }
}

/* ---------- 篩選標籤、組織管理 ---------- */
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 13px; white-space: nowrap; }
.chip:hover { text-decoration: none; filter: brightness(0.97); }
.checklist { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; }
.checklist label.check { font-size: 13px !important; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px 14px; margin: 0 0 14px; }
legend { padding: 0 6px; color: var(--muted); font-size: 12.5px; }
.radios { display: flex; flex-direction: column; gap: 6px; }
.radios label.check { white-space: normal; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
