:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1c2430;
  --muted: #5d6877;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --accent-soft: #e6eefc;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --warn: #a35f00;
  --warn-soft: #fdf1dc;
  --err: #c0352b;
  --err-soft: #fbe6e4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151a;
    --surface: #1b1f26;
    --surface-2: #232832;
    --border: #313844;
    --text: #e6e9ee;
    --muted: #9aa4b2;
    --accent: #6a9cf5;
    --accent-text: #0d1320;
    --accent-soft: #1f2c46;
    --ok: #5fcf8e;
    --ok-soft: #173325;
    --warn: #f0b45a;
    --warn-soft: #3a2c14;
    --err: #ff8a80;
    --err-soft: #3d1d1b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.15rem; }
h2 { font-size: 1.2rem; margin-bottom: 0.5rem; }
h3 { font-size: 0.95rem; margin: 1.1rem 0 0.4rem; color: var(--muted); }

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.75rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: baseline; gap: 0.6rem; min-width: 0; }
.version { color: var(--muted); font-size: 0.8rem; }
.topbar-actions { display: flex; align-items: center; gap: 0.75rem; }
.who { color: var(--muted); font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(260px, 340px) 1fr; gap: 1rem;
  max-width: 1200px; margin: 1rem auto; padding: 0 1rem;
}
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.side { padding: 0.75rem; align-self: start; position: sticky; top: 4.25rem; max-height: calc(100vh - 5.5rem); display: flex; flex-direction: column; }
.main { padding: 1.25rem; min-width: 0; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 0 16px; }
  .side { position: static; max-height: none; }
  .roster { max-height: 50vh; }
  .who { display: none; }
}

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px;
  padding: 0.5rem 0.95rem; cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.small { padding: 0.25rem 0.6rem; font-size: 0.85rem; }
a.btn { text-decoration: none; display: inline-block; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- tabs & roster ---------- */
.tabs { display: flex; gap: 0.25rem; border-bottom: 1px solid var(--border); margin-bottom: 0.6rem; }
.tab { background: none; border: 0; padding: 0.45rem 0.75rem; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabpane { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.side-tools { display: flex; gap: 0.5rem; align-items: center; justify-content: space-between; margin-bottom: 0.4rem; }
.side-tools input { flex: 1; min-width: 0; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0.2rem 0; }

input[type="search"], input:not([type]), input[type="email"], input[type="password"], input[type="number"], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 0.45rem 0.6rem; width: 100%;
}
textarea { resize: vertical; min-height: 5.5rem; }

.roster { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.roster .group-head { font-size: 0.78rem; color: var(--muted); padding: 0.6rem 0.4rem 0.2rem; font-weight: 600; }
.roster button {
  width: 100%; display: flex; align-items: center; gap: 0.5rem; text-align: left;
  border: 0; background: none; padding: 0.4rem 0.5rem; border-radius: 6px; cursor: pointer;
}
.roster button:hover { background: var(--surface-2); }
.roster button[aria-current="true"] { background: var(--accent-soft); }
.roster .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.roster .sid { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }

.badge { font-size: 0.72rem; padding: 0.05rem 0.4rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.me { background: var(--accent-soft); color: var(--accent); }

/* ---------- report ---------- */
#report { overflow-y: auto; flex: 1; }
.rgroup { border: 1px solid var(--border); border-radius: 8px; padding: 0.5rem 0.6rem; margin-bottom: 0.5rem; }
.rgroup-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; margin-bottom: 0.25rem; }
.rgroup button { display: flex; width: 100%; gap: 0.5rem; border: 0; background: none; padding: 0.2rem 0.25rem; border-radius: 4px; cursor: pointer; text-align: left; }
.rgroup button:hover { background: var(--surface-2); }
.rgroup .ord { color: var(--muted); width: 1.6rem; font-variant-numeric: tabular-nums; }

/* ---------- main form ---------- */
.label { font-weight: 600; font-size: 0.9rem; }
.target-bar { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 1rem; }
.target-bar select { flex: 1; min-width: 200px; width: auto; }
.target-flags { display: flex; gap: 0.35rem; flex-wrap: wrap; }

.dims { display: grid; gap: 0.35rem; }
.dim {
  display: grid; grid-template-columns: 7.5rem 1fr 4.5rem 6.5rem; gap: 0.75rem; align-items: center;
  padding: 0.45rem 0.25rem; border-bottom: 1px solid var(--border);
}
.dim .dim-name b { display: block; }
.dim .dim-name small { color: var(--muted); }
.dim input[type="range"] { width: 100%; accent-color: var(--accent); }
.dim input[type="number"] { text-align: right; font-variant-numeric: tabular-nums; }
.dim .conv { font-size: 0.82rem; color: var(--muted); line-height: 1.35; font-variant-numeric: tabular-nums; }
.dim .conv b { color: var(--text); }

@media (max-width: 560px) {
  .dim { grid-template-columns: 1fr 4.5rem; }
  .dim input[type="range"] { grid-column: 1 / -1; grid-row: 2; }
  .dim .conv { grid-column: 1 / -1; }
}

.summary { display: flex; gap: 1.5rem; flex-wrap: wrap; margin: 0.9rem 0 1.1rem; }
.summary .k { color: var(--muted); font-size: 0.85rem; margin-right: 0.4rem; }
.summary .v { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.field { margin-bottom: 1rem; }
.field-head { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.35rem; }
.field-head .label { margin-right: auto; }

.sites { border: 0; padding: 0; margin: 0 0 1rem; display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0.5rem; }
.sites legend { margin-bottom: 0.35rem; }
.site { display: flex; gap: 0.55rem; align-items: flex-start; border: 1px solid var(--border); border-radius: 8px; padding: 0.55rem 0.7rem; cursor: pointer; }
.site:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.site input { margin-top: 0.3rem; accent-color: var(--accent); }
.site b { display: block; font-size: 0.92rem; }
.site small { color: var(--muted); font-size: 0.8rem; }

.actions { display: flex; gap: 0.6rem; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- results ---------- */
.results { margin-top: 1.25rem; border-top: 1px solid var(--border); padding-top: 1rem; }
.results h3 { margin-top: 0; color: var(--text); font-size: 1rem; }
.result { display: flex; gap: 0.75rem; align-items: flex-start; padding: 0.6rem 0.75rem; border-radius: 8px; margin-bottom: 0.5rem; background: var(--surface-2); }
.result.ok { background: var(--ok-soft); }
.result.err { background: var(--err-soft); }
.result.act { background: var(--warn-soft); }
.result .rname { font-weight: 600; min-width: 9.5rem; }
.result .rbody { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.result pre { margin: 0.3rem 0 0; font-size: 0.8rem; white-space: pre-wrap; color: var(--muted); }
@media (max-width: 560px) { .result { flex-direction: column; gap: 0.25rem; } }

/* ---------- dialogs ---------- */
.dialog { border: 1px solid var(--border); border-radius: 12px; padding: 1.25rem; width: min(560px, calc(100vw - 32px)); background: var(--surface); color: var(--text); }
.dialog::backdrop { background: rgba(10, 14, 20, 0.45); }
.dialog label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.55rem; }
.dialog label input { margin-top: 0.2rem; color: var(--text); }
.dialog label.check { display: flex; gap: 0.5rem; align-items: center; color: var(--text); margin-top: 0.75rem; }
.dialog label.check input { width: auto; margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 0.75rem; }
@media (max-width: 480px) { .grid2 { grid-template-columns: 1fr; } }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }
.checks:empty { display: none; }
.checks { margin-top: 0.75rem; display: grid; gap: 0.35rem; }
.check-row { display: flex; gap: 0.5rem; font-size: 0.88rem; align-items: baseline; }
.check-row .badge { flex-shrink: 0; }

table.confirm { width: 100%; border-collapse: collapse; font-size: 0.88rem; font-variant-numeric: tabular-nums; }
table.confirm th, table.confirm td { border-bottom: 1px solid var(--border); padding: 0.35rem 0.4rem; text-align: right; }
table.confirm th:first-child, table.confirm td:first-child { text-align: left; }
.confirm-comment { background: var(--surface-2); border-radius: 8px; padding: 0.5rem 0.65rem; margin: 0.75rem 0 0; white-space: pre-wrap; font-size: 0.9rem; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 0.55rem 1rem; border-radius: 8px; z-index: 20;
  max-width: calc(100vw - 32px); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}
.toast.err { background: var(--err); color: #fff; }
