/* ==========================================================================
   即時運營台
   --------------------------------------------------------------------------
   設計前提：有人會盯著這個畫面好幾個小時。
     1. 不刺眼：底色不用純白，深色模式給夜班用
     2. 顏色只用來表意（成功 / 失敗 / 異常），不做裝飾
     3. 數字用等寬對齊，掃視時不會跳動
     4. 手機號／IP／投票碼都能一鍵複製，不用手抄
     5. 鍵盤可達：/ 搜尋、1/2 切頁、T 換主題、+/- 調字級
   ========================================================================== */

:root {
  --ink: #1b2430;
  --ink-2: #3d4a5a;
  --muted: #78828f;
  --line: #dfe4ea;
  --line-soft: #eceff3;

  --bg: #eceff3;
  --surface: #fbfcfd;
  --surface-2: #f3f5f8;
  --surface-3: #e9edf2;

  --accent: #2f6fdb;
  --accent-soft: #e7effc;

  --ok: #1a7f52;
  --ok-soft: #e2f3ea;
  --warn: #96620f;
  --warn-soft: #fbf0dd;
  --bad: #ac3a31;
  --bad-soft: #fbe8e6;

  --brand-a: #ff7a00;
  --brand-b: #ff9500;

  --row-h: 34px;
  --font-scale: 1;
  --radius: 8px;

  --shadow: 0 1px 2px rgba(20, 30, 45, 0.05);
}

/* 夜班模式：降低整體亮度，避免長時間盯螢幕的乾眼 */
[data-theme="dark"] {
  --ink: #dfe6ee;
  --ink-2: #b8c3d0;
  --muted: #8b98a8;
  --line: #2b333d;
  --line-soft: #242b34;

  --bg: #12161b;
  --surface: #191f26;
  --surface-2: #1f262f;
  --surface-3: #263039;

  --accent: #5b9bff;
  --accent-soft: #1b2a3f;

  --ok: #4fbf8b;
  --ok-soft: #14302a;
  --warn: #e0a94a;
  --warn-soft: #33280f;
  --bad: #ef7d73;
  --bad-soft: #3a1d1c;

  --shadow: none;
}

* { box-sizing: border-box; }

html { font-size: calc(13.5px * var(--font-scale)); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
    "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 1rem;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  /* 長時間閱讀：稍微放寬字距 */
  letter-spacing: 0.01em;
}

button, input, select { font-family: inherit; font-size: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --------------------------------------------------------------- 頂欄 */

.topbar {
  height: 52px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}

.station {
  font-size: 0.78rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(90deg, var(--brand-a), var(--brand-b));
  padding: 3px 10px;
  border-radius: 999px;
}

/* 分頁：大目標，掃視時一眼看得出在哪一頁 */
.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  padding: 3px;
  border-radius: 10px;
}

.tab {
  border: 0;
  background: transparent;
  color: var(--ink-2);
  font-weight: 600;
  padding: 7px 18px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}

.tab:hover { color: var(--ink); }

.tab.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.tab .dot {
  display: inline-block;
  min-width: 18px;
  padding: 1px 5px;
  margin-left: 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  line-height: 1.4;
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.conn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

.conn i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  display: inline-block;
}

.conn.on i { background: var(--ok); }
.conn.off i { background: var(--bad); }

.switch {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.8rem;
  color: var(--ink-2);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.switch input { width: 14px; height: 14px; accent-color: var(--accent); }

.seg { display: inline-flex; gap: 4px; }

/* --------------------------------------------------------------- 通用元件 */

.mini {
  height: 28px;
  padding: 0 10px;
  font-size: 0.8rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink-2);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.mini:hover { background: var(--surface-2); border-color: var(--muted); }
.mini[disabled] { opacity: 0.45; cursor: not-allowed; }

.btn {
  height: 30px;
  padding: 0 14px;
  font-size: 0.82rem;
  font-weight: 600;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-ghost {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink-2);
  font-weight: 500;
}

.btn-ghost:hover { background: var(--surface-2); }

.btn-danger {
  background: transparent;
  border: 1px solid var(--bad);
  color: var(--bad);
  font-weight: 600;
}

.btn-danger:hover { background: var(--bad-soft); }

.btn-block { width: 100%; height: 38px; }

/* --------------------------------------------------------------- 登入 */

#loginView { max-width: 360px; margin: 10vh auto 0; padding: 0 16px; }

.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
}

.login-card h1 { margin: 0 0 4px; font-size: 1.15rem; }
.login-card p { margin: 0 0 18px; color: var(--muted); font-size: 0.85rem; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 0.8rem; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }

.field input {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
}

.err { color: var(--bad); font-size: 0.82rem; min-height: 18px; margin: 8px 0 0; }

/* --------------------------------------------------------------- 版面 */

main { padding: 16px; max-width: 1680px; margin: 0 auto; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
}

.stat span { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 4px; }

.stat strong {
  font-size: 1.3rem;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  letter-spacing: 0.02em;
}

.stat.alert { border-color: var(--warn); background: var(--warn-soft); }
.stat.alert strong { color: var(--warn); }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

/* --------------------------------------------------------------- 工具列 */

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line-soft);
}

.toolbar .spacer { flex: 1; }

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--ink-2);
  cursor: pointer;
  white-space: nowrap;
}

.check input { width: 15px; height: 15px; accent-color: var(--accent); }

.search {
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  width: 240px;
}

.search::placeholder { color: var(--muted); }

select, input[type="date"] {
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  max-width: 220px;
}

/* 狀態膠囊 */
.chips { display: inline-flex; gap: 6px; }

.chip {
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  font-size: 0.82rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.chip b {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-weight: 600;
}

.chip:hover { border-color: var(--muted); }

.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.chip.active b { color: var(--accent); }

/* --------------------------------------------------------------- 表格 */

.table-wrap { overflow: auto; max-height: calc(100vh - 300px); }

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.86rem;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

tbody td {
  padding: 0 12px;
  height: var(--row-h);
  border-bottom: 1px solid var(--line-soft);
  white-space: nowrap;
  vertical-align: middle;
}

tbody tr:nth-child(even) { background: var(--surface-2); }
tbody tr:hover { background: var(--surface-3); }

tbody tr.row-risk { background: var(--warn-soft); }
tbody tr.row-danger { background: var(--bad-soft); }
tbody tr.row-fail td { color: var(--muted); }
tbody tr.row-check { background: var(--accent-soft); }

/* 剛進來的資料：柔和淡入，不閃爍 */
tbody tr.fresh { animation: settle 1.1s ease-out; }

@keyframes settle {
  0% { background: var(--accent-soft); }
  100% { background: transparent; }
}

@media (prefers-reduced-motion: reduce) {
  tbody tr.fresh { animation: none; }
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  font-size: 0.84rem;
  letter-spacing: 0;
}

.dim { color: var(--muted); }

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 5px;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
}

.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.fail { background: var(--surface-3); color: var(--muted); }
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.dim { background: var(--surface-3); color: var(--muted); }
/* 人工标注 / 检测：用强调色，跟系统自动打的标签区分开 */
.tag.manual { background: var(--accent-soft); color: var(--accent); }
/* 请求验证码：既不是成功也不是失败，用中性蓝区分 */
.tag.info { background: var(--accent-soft); color: var(--accent); }
/* 待审核：要显眼，运营人员得先处理它 */
.tag.pending { background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn); }
.chip.pending-chip b { color: var(--warn); }
.chip.pending-chip.active { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }

/* 备注：平常像文字，hover 才看得出可以点 */
.note-edit {
  border: 1px dashed transparent;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  padding: 2px 6px;
  border-radius: 5px;
  cursor: text;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
}

.note-edit:hover { border-color: var(--line); background: var(--surface-2); }
.note-edit.is-empty { color: var(--muted); }

.note-input {
  width: 220px;
  height: 24px;
  padding: 0 6px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 5px;
}

/* 标注 / 检测：未选是空心，选了填色 */
.row-tag {
  height: 24px;
  padding: 0 9px;
  margin-right: 4px;
  font-size: 0.76rem;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}

.row-tag:hover { border-color: var(--accent); color: var(--accent); }

.row-tag.on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* 一鍵複製：平常低調，hover / focus 才亮起來 */
.copy {
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 2px 6px;
  border-radius: 5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.copy::after {
  content: "⧉";
  font-size: 0.78rem;
  opacity: 0;
  color: var(--accent);
  transition: opacity 0.12s ease;
}

.copy:hover { background: var(--accent-soft); border-color: var(--accent); }
.copy:hover::after { opacity: 1; }
.copy:focus-visible::after { opacity: 1; }
.copy.copied { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.copy.copied::after { content: "✓"; opacity: 1; color: var(--ok); }

p.empty {
  margin: 0;
  padding: 36px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

/* 欄寬 */
.c-time { width: 128px; }
.c-num { width: 150px; }
.c-state { width: 96px; }
.c-code { width: 176px; }
.c-phone { width: 168px; }
.c-ip { width: 136px; }
.c-ua { width: 148px; }
.c-note { width: 200px; }
.c-who { width: 92px; }
.c-risk { width: auto; }
.c-tags { width: 150px; }
.c-act { width: 268px; text-align: right; white-space: nowrap; }

/* 刪除：平常是低調的灰框，hover 才轉紅；避免盯盤時手滑 */
.row-del {
  height: 24px;
  padding: 0 9px;
  font-size: 0.76rem;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}

.row-del:hover {
  border-color: var(--bad);
  color: var(--bad);
  background: var(--bad-soft);
}

/* --------------------------------------------------------------- 表尾 */

.foot, .rec-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--line-soft);
  background: var(--surface);
}

.rec-foot { border-top: 0; border-bottom: 1px solid var(--line-soft); }
.rec-foot .spacer, .foot .spacer { flex: 1; }

.page-size { display: inline-flex; align-items: center; gap: 6px; }
.page-size select { height: 26px; }

/* --------------------------------------------------------------- 提示 */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--bg);
  padding: 9px 18px;
  border-radius: 999px;
  font-size: 0.83rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 50;
  max-width: 80vw;
}

.toast.show { opacity: 0.94; transform: translate(-50%, 0); }

/* --------------------------------------------------------------- 窄螢幕 */

@media (max-width: 900px) {
  .topbar { height: auto; flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .top-actions { margin-left: 0; width: 100%; justify-content: flex-start; }
  .search { width: 160px; }
  .table-wrap { max-height: none; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}

/* --------------------------------------------------------------- 合併列 */

/* 同一支手機的舊紀錄：縮排、淡一點，預設收起來 */
tbody tr.rec-child { background: var(--surface-2); }
tbody tr.rec-child td { color: var(--ink-2); }
tbody tr.rec-child td:first-child { padding-left: 26px; }
tbody tr.rec-child .child-mark { color: var(--muted); margin-right: 6px; }

[hidden] { display: none !important; }

/* 「3 条」展開鈕 */
.grp-toggle {
  margin-left: 6px;
  height: 20px;
  padding: 0 7px;
  font-size: 0.72rem;
  font-weight: 600;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  cursor: pointer;
  vertical-align: 1px;
}

.grp-toggle:hover { border-color: var(--accent); color: var(--accent); }

.grp-toggle.open {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.grp-toggle.open::after { content: " ▴"; }
.grp-toggle:not(.open)::after { content: " ▾"; }
/* --------------------------------------------------------------- 版本提示 */

/* 頁面是舊版時才會出現：釘在頂部，不擋表格 */
.stale-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 60;
  padding: 8px 14px;
  text-align: center;
  font-size: 0.84rem;
  background: var(--warn-soft);
  color: var(--warn);
  border-bottom: 1px solid var(--warn);
}

.stale-bar button {
  border: 1px solid var(--warn);
  background: var(--surface);
  color: var(--warn);
  font: inherit;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 5px;
  cursor: pointer;
}

.stale-bar button:hover { background: var(--warn); color: #fff; }
/* 「有人正在填」的燈 */
.typing {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 2px 10px;
  white-space: nowrap;
}

.typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: typing-pulse 1.1s ease-in-out infinite;
}

@keyframes typing-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .typing i { animation: none; opacity: 1; }
}
/* 行內的「輸入中」小燈 */
.typing-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  white-space: nowrap;
  vertical-align: 1px;
}

.typing-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: typing-pulse 1.1s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
  .typing-badge::before { animation: none; }
}
/* --------------------------------------------------------------- 正在填写 */

.live-sessions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding: 8px 12px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
}

.ls-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

.ls-items { display: flex; flex-wrap: wrap; gap: 8px; }

.ls-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 0.78rem;
  color: var(--ink-2);
  white-space: nowrap;
}

.ls-chip b {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--accent);
}

.ls-chip::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: typing-pulse 1.1s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
  .ls-chip::before { animation: none; }
}
/* 使用者行為標籤（自動帶出，不是人工標的） */
.tag.act {
  color: var(--ink-2);
  background: var(--bg-2, #eef1f6);
  border: 1px solid var(--line);
}

.tag.act::before {
  content: "›";
  margin-right: 3px;
  opacity: 0.55;
}

/* 「輸入中」直接寫在欄位裡（不另外做成方塊） */
button.typing-inline {
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  cursor: pointer;
}

.typing-inline:empty {
  min-width: 6px;
  min-height: 6px;
}

.typing-inline {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.typing-inline::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.55;
}

/* 只有「還有回報」的才跳動；超時的就靜靜留著數字 */
.typing-inline.is-live {
  color: var(--accent);
}

.typing-inline.is-live::before {
  background: var(--accent);
  opacity: 1;
  animation: typing-pulse 1.1s ease-in-out infinite;
}

.typing-inline.is-stale {
  color: var(--ink-2);
}

@media (prefers-reduced-motion: reduce) {
  .typing-inline::before { animation: none; }
}