:root {
  --bg: #f5f4f0;
  --card: #ffffff;
  --card-soft: #f7f6f3;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e4e2dc;
  --brand: #e8730c;
  --brand-soft: #fdf1e6;
  --bar: #1b1d21;
  --bar-text: #f3f4f6;
  --ok: #2e7d32;
  --ok-soft: #e7f3e8;
  --danger: #c62828;
  --danger-soft: #fdecec;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

h2, h3 { margin: 0; font-weight: 700; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }
h3 .muted { font-weight: 400; font-size: 13px; margin-left: 8px; }

.muted { color: var(--muted); font-size: 13px; }
.mono { font-family: var(--mono); }
.small { font-size: 12px; }

code {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--card-soft);
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 6px;
}

/* ---------- 頂欄 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 24px;
  height: 64px;
  background: var(--bar);
  color: var(--bar-text);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-name { font-size: 18px; font-weight: 700; }

.logo {
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 4px solid var(--brand);
  position: relative;
}
.logo::after {
  content: "";
  position: absolute;
  inset: 5px;
  border-radius: 50%;
  background: var(--brand);
}

.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent;
  color: var(--bar-text);
  border: 0;
  padding: 10px 18px;
  border-radius: 10px;
  font: inherit;
  font-size: 15px;
  cursor: pointer;
  opacity: 0.85;
}
.tab.active { background: rgba(255, 255, 255, 0.12); font-weight: 700; opacity: 1; }
.tab:disabled { opacity: 0.4; cursor: default; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 20px; }

.hdr-tool { display: flex; align-items: center; gap: 8px; font-size: 15px; }

.dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: #9aa1ab;
}
.dot.on { background: #3fbf5a; }
.dot.off { background: #9aa1ab; }
.dot.err { background: #ef5350; }

.empid-form { display: flex; gap: 6px; }
.topbar .empid-form input {
  font: inherit;
  width: 150px;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: #fff;
  color: var(--text);
}
.topbar .empid-form input::placeholder { color: #9aa1ab; }
.topbar .empid-form button {
  font: inherit;
  padding: 7px 14px;
  border-radius: 8px;
  border: 0;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
}

.empid-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: #3a3d44;
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
}
.who { font-size: 15px; }
.link {
  background: none;
  border: 0;
  color: inherit;
  opacity: 0.7;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}
.card .link { color: var(--brand); opacity: 1; text-decoration: none; }

/* ---------- 版面 ---------- */
main {
  display: grid;
  grid-template-columns: 350px minmax(0, 1fr);
  gap: 24px;
  padding: 28px 32px;
  max-width: 1400px;
  margin: 0 auto;
}

@media (max-width: 860px) {
  main { grid-template-columns: 1fr; padding: 16px; }
  .topbar { flex-wrap: wrap; height: auto; padding: 10px 16px; gap: 10px 16px; }
  .topbar-right { margin-left: 0; width: 100%; justify-content: space-between; }
}

.col-left { min-width: 0; }
.col-right { min-width: 0; display: flex; flex-direction: column; gap: 15px; }
/* 右欄裡再包一層的容器（連線中、紀錄詳細）也要維持卡片間距 */
#session, #histDetail { display: flex; flex-direction: column; gap: 15px; }

/* ---------- 遠端連線頁的兩種版面 ---------- */
/* 未連線：只有工具列表，佔滿整頁，項目排成卡片格 */
#viewLive:not(.has-session) { grid-template-columns: 1fr; }
#viewLive:not(.has-session) .tool-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
  background: transparent;
  border: 0;
  border-radius: 0;
  overflow: visible;
}
#viewLive:not(.has-session) .tool-list li {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
}
#viewLive:not(.has-session) .tool-list li.mine { background: var(--brand-soft); }

/* 連線中：左欄貼左 10px、列表 90vh 內捲動；紀錄像聊天室浮在左下角（寬同左欄、高 70vh）內捲動，可收合 */
#viewLive.has-session { grid-template-columns: 420px minmax(0, 1fr); padding-left: 10px; }
#viewLive.has-session .tool-list { height: 80vh; overflow-y: auto; }
#viewLive.has-session .log-panel {
  position: fixed;
  left: 10px;
  bottom: 10px;
  width: 420px;
  height: 70vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  z-index: 5;
}
#viewLive.has-session .log-panel .card-head { flex: none; }
#viewLive.has-session .log-panel .log { flex: 1; max-height: none; min-height: 0; }
/* 聊天室樣式：標題列一律橘底白字、較矮較小；收合時只剩標題列 */
.card.log-panel { padding: 0; overflow: hidden; }
.card.log-panel .log-head {
  background: var(--brand);
  color: #fff;
  padding: 8px 14px;
  margin: 0;
  border-bottom: 0;
  border-radius: 0;
}
.log-panel .log-head h3 { font-size: 14px; color: #fff; }
.log-panel .log-head h3 .muted { color: rgba(255, 255, 255, 0.8); font-size: 12px; }
.log-panel .log-head .link { color: #fff; font-size: 12px; }
.log-panel .log-head .toggle { font-size: 14px; }
.log-panel .log { padding: 6px 14px; }
#viewLive.has-session .log-panel.collapsed { height: auto; }
#viewLive.has-session .log-panel.collapsed .log { display: none; }
.log-panel.collapsed { border-color: var(--brand); }
.log-head { cursor: pointer; user-select: none; }
.log-head-actions { display: flex; gap: 12px; align-items: center; }
.log-head-actions .toggle { font-size: 16px; line-height: 1; text-decoration: none; padding: 0 4px; }

@media (max-width: 860px) {
  /* 單欄時紀錄不固定，照流排在工具列表下面 */
  #viewLive.has-session { grid-template-columns: 1fr; padding-left: 16px; }
  #viewLive.has-session .tool-list { height: auto; max-height: 45vh; }
  #viewLive.has-session .log-panel { position: static; width: auto; height: auto; box-shadow: none; display: block; }
  #viewLive.has-session .log-panel .log { max-height: 240px; }
  #viewLive.has-session .log-panel.collapsed .card-head { border-bottom: 0; }
}
.col-title { margin-bottom: 2px; }
.col-sub { margin: 0 0 12px; }
.empty { margin: 8px 0; }

/* ---------- 卡片 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 22px;
}
.card.placeholder { text-align: center; padding: 40px 22px; }
.card.placeholder p { margin: 6px 0 0; }
.card > p.muted { margin: 4px 0 12px; }

/* 卡片標題列：淺灰底、貼齊卡片邊緣（用負邊距吃掉卡片內距） */
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: -18px -22px 12px;
  padding: 12px 22px;
  background: var(--card-soft);
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
}

/* ---------- 工具列表 ---------- */
.tool-list { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tool-list:empty { display: none; }
.tool-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.tool-list button { padding: 7px 12px; font-size: 14px; }
.tool-list li:first-child { border-top: 0; }
.tool-list li.mine { background: var(--brand-soft); }
.tool-list li.selectable { cursor: pointer; }
.tool-list li.selectable:hover { background: var(--card-soft); }
.tool-list li.offline .tool-icon { background: #c9c7c1; }
.tool-list li.offline .tool-serial { color: var(--muted); }
.tool-list .chevron { color: var(--brand); font-size: 18px; flex: none; }
.tool-icon {
  flex: none;
  width: 44px; height: 44px;
  border-radius: 10px;
  background: var(--brand);
  position: relative;
}
.tool-icon::after {
  content: "";
  position: absolute;
  inset: 13px 11px;
  border: 2px solid #fff;
  border-radius: 4px;
}
.tool-list li.busy .tool-icon { background: #c9c7c1; }
.tool-info { flex: 1; min-width: 0; }
.tool-serial { font-family: var(--mono); font-weight: 700; font-size: 15px; letter-spacing: -0.3px; word-break: break-all; }
.tool-status { font-size: 13px; color: var(--muted); margin-top: 2px; }
.tool-status.on { color: var(--ok); }
.tool-list button { flex: none; }

/* ---------- 連線卡 ---------- */
.session-head {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin: -18px -22px 0;
  padding: 14px 22px;
  background: var(--card-soft);
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
}
.session-head .serial { font-size: 24px; flex: 1; min-width: 0; word-break: break-all; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}
.pill.ok { background: var(--ok-soft); color: var(--ok); }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-top: 18px;
}
.tile {
  background: var(--card-soft);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.tile-label { font-size: 12px; color: var(--muted); }
.tile-value { font-weight: 700; font-size: 16px; word-break: break-all; }
.tile-value.small { font-weight: 400; }

/* ---------- 設定／指令列 ---------- */
.rows { display: flex; flex-direction: column; gap: 10px; }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0 4px;
  border-top: 1px solid var(--border);
}
.rows .row:first-child { border-top: 0; padding-top: 6px; }
.row-info { flex: 1; min-width: 0; }
.row-title { font-weight: 700; display: flex; align-items: center; flex-wrap: wrap; gap: 4px 0; }
.row-desc { color: var(--muted); font-size: 13px; margin-top: 2px; }
/* 群組內文字欄位的必填提示：欄位框線變紅，提示排在欄位正下方（格線第二欄） */
.group input.invalid { border-color: var(--danger); box-shadow: 0 0 0 2px rgba(198, 40, 40, 0.15); }
.group .field-hint { grid-column: 2; color: var(--danger); font-size: 12px; font-weight: 600; margin-top: -4px; }
.row-actions { display: flex; align-items: center; gap: 10px; flex: none; }

@media (max-width: 600px) {
  .row { flex-direction: column; align-items: stretch; }
  .row-actions { justify-content: flex-end; flex-wrap: wrap; }
  .session-head .serial { font-size: 19px; }
  .topbar .empid-form input { width: 120px; }
}

.tag {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  padding: 1px 8px;
  border-radius: 6px;
  margin-left: 8px;
}
.tag.danger { background: var(--danger-soft); color: var(--danger); }

.control { display: flex; align-items: center; gap: 8px; min-width: 0; }
.control select, .control input[type="text"] { min-width: 160px; }
.control input[type="range"] { width: 150px; accent-color: var(--brand); }
.control output { min-width: 3ch; text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }

.group {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 8px 12px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-soft);
}
.group label { display: contents; }
.group label > span { color: var(--muted); font-size: 13px; }

/* ---------- 控制項 ---------- */
button, select, input[type="text"] {
  font: inherit;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}
select, input[type="text"] { padding: 8px 10px; }
button { cursor: pointer; background: var(--brand); color: #fff; border-color: var(--brand); font-weight: 600; }
button.outline { background: var(--card); color: var(--text); border: 1.5px solid var(--text); }
button.outline.danger { color: var(--danger); border-color: var(--danger); }
button.outline:hover:not(:disabled) { background: var(--card-soft); }
button:disabled { opacity: 0.45; cursor: default; }
button.run::before { content: "▶"; font-size: 10px; margin-right: 8px; }

/* 等工具 ack：右欄蓋遮罩 + 轉圈，觸發的按鈕前面也轉圈 */
.col-right { position: relative; }
.overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  background: rgba(245, 244, 240, 0.72);
  backdrop-filter: blur(1px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 120px;
  gap: 12px;
  border-radius: var(--radius);
  cursor: progress;
}
.overlay .spinner {
  width: 36px;
  height: 36px;
  border: 4px solid rgba(232, 115, 12, 0.25);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  position: sticky;
  top: 120px;
}
.overlay-text { font-weight: 700; color: var(--text); position: sticky; top: 170px; }

/* 一次只能改一項：其他列鎖住 */
.row.locked { opacity: 0.45; }
.lock-hint {
  background: var(--brand-soft);
  color: var(--brand);
  border: 1px solid #f5d9bd;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  margin: 0 0 10px;
}
.row-actions .revert { color: var(--brand); font-size: 13px; text-decoration: none; opacity: 1; }
.row-actions .revert:hover { text-decoration: underline; }

#session.busy { cursor: progress; }
#session.busy * { cursor: progress; }
button.loading { opacity: 1 !important; }
button.loading::before {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 8px;
  vertical-align: -2px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 紀錄 ---------- */
.log {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 320px;
  overflow-y: auto;
  font-size: 13px;
}
.log li {
  display: grid;
  grid-template-columns: 70px 44px 1fr;
  gap: 12px;
  align-items: start;
  padding: 8px 4px;
  border-top: 1px solid var(--border);
}
.log li:first-child { border-top: 0; }
.log .time { font-family: var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.log .kind {
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--card-soft);
  color: var(--muted);
}
.log li.ok .kind { background: var(--ok-soft); color: var(--ok); }
.log li.err .kind { background: var(--danger-soft); color: var(--danger); }
.log li.out .kind { background: var(--brand-soft); color: var(--brand); }
.log .text { word-break: break-word; }

.log .kind { padding: 1px 4px; }
@media (max-width: 600px) {
  .log li { grid-template-columns: 70px 1fr; }
  .log .kind { display: none; }
}

/* ---------- 歷史紀錄 ---------- */
.sessions { display: flex; flex-direction: column; gap: 12px; margin-top: 10px; }
.sess {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card-soft);
  overflow: hidden;
}
.sess-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  user-select: none;
}
.sess-head:hover { background: #f0eeea; }
.sess-range { font-weight: 700; }
.sess-meta { color: var(--muted); font-size: 13px; }
.sess-toggle { margin-left: auto; color: var(--brand); font-weight: 700; font-size: 13px; white-space: nowrap; }
.pill.live { background: var(--brand-soft); color: var(--brand); }
.pill.gray { background: #ecebe7; color: var(--muted); }

.ev-list { list-style: none; margin: 0; padding: 0 16px 10px; background: var(--card); border-top: 1px solid var(--border); }
.ev-list li {
  display: grid;
  grid-template-columns: 110px 70px minmax(140px, 220px) 1fr;
  gap: 12px;
  align-items: start;
  padding: 8px 0;
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.ev-list li:first-child { border-top: 0; }
.ev-time { font-family: var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.ev-dir {
  font-size: 12px; font-weight: 700; text-align: center; padding: 1px 6px; border-radius: 6px;
  background: var(--card-soft); color: var(--muted);
}
.ev-dir.out { background: var(--brand-soft); color: var(--brand); }
.ev-dir.in { background: var(--ok-soft); color: var(--ok); }
.ev-dir.fail { background: var(--danger-soft); color: var(--danger); }
.ev-name { font-weight: 700; }
.ev-name code { margin-left: 0; display: block; margin-top: 2px; }
/* 每筆收發：預設只顯示摘要，點整列向下展開 JSON，再點收回 */
.ev-list li.clickable { cursor: pointer; }
.ev-list li.clickable:hover { background: var(--card-soft); }
.ev-list li.open { background: var(--card-soft); }
.ev-brief { font-size: 12px; color: var(--brand); font-weight: 600; text-align: right; white-space: nowrap; }
.ev-brief.err { color: var(--danger); font-family: var(--mono); font-weight: 400; text-align: left; white-space: normal; word-break: break-all; }
.ev-list li .json {
  grid-column: 1 / -1;
  margin: 4px 0 0;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 60vh;
  overflow: auto;
  cursor: text;
}
.ev-loading, .ev-empty { padding: 12px 16px; color: var(--muted); font-size: 13px; background: var(--card); border-top: 1px solid var(--border); }

@media (max-width: 760px) {
  .ev-list li { grid-template-columns: 90px 1fr; }
  .ev-list .ev-dir { display: none; }
  .ev-brief { grid-column: 1 / -1; text-align: left; }
}

/* ---------- 總管頁 ---------- */
main.admin { display: flex; flex-direction: column; gap: 15px; }
/* 總管頁的卡片標題用淺橘底、深色字 */
main.admin .card-head { background: var(--brand-soft); border-bottom-color: #f5d9bd; }
main.admin .card-head h3 { color: var(--text); }
main.admin .card-head .filters input { background: #fff; }
main.admin #dash { display: flex; flex-direction: column; gap: 15px; }
.tabs a.tab { text-decoration: none; display: inline-block; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 15px; }
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value.small { font-size: 18px; font-family: var(--mono); }

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 4px; }
table.data th, table.data td { text-align: left; padding: 8px 10px; border-top: 1px solid var(--border); white-space: nowrap; }
table.data th { color: var(--muted); font-weight: 600; font-size: 12px; border-top: 0; }
table.data td.mono { font-family: var(--mono); }
table.data td.small { font-size: 11px; color: var(--muted); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input { font: inherit; padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); color: var(--text); width: 140px; }
.topbar .empid-form input[type="password"] { width: 220px; }

/* ---------- 閒置鎖定 ---------- */
.idle-dialog {
  border: 0;
  border-radius: 14px;
  padding: 28px 32px;
  max-width: 420px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.idle-dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.idle-dialog h3 { font-size: 18px; margin-bottom: 8px; }
.idle-dialog p { margin: 0 0 20px; color: var(--muted); }
.idle-dialog button { min-width: 140px; }
