:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --line: #e1e6ee;
  --brand: #d6202a;
  --primary: #1f6feb;
  --online: #1a9b4b;
  --busy: #d98a00;
  --offline: #9aa3b0;
  --pending: #c2410c;
  --danger: #c62828;
  --top: #17202c;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 14px;
  color: var(--text);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b; --panel: #171e28; --text: #e3e8ef; --muted: #8b97a8; --line: #273140;
    --top: #0b1016; --offline: #5d6878;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }
.boot, .fatal { padding: 40px; text-align: center; color: var(--muted); }

input, select { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
input[type=checkbox] { padding: 0; }
label { display: block; margin: 10px 0; }
label > input, label > select { display: block; width: 100%; margin-top: 4px; }
label.inline { display: inline-flex; align-items: center; gap: 4px; margin: 0; white-space: nowrap; }
label.inline > input, label.inline > select { display: inline-block; width: auto; margin: 0; }

.btn { font: inherit; display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  border-radius: 6px; padding: 7px 14px; cursor: pointer; }
.btn:hover:not(:disabled) { filter: brightness(0.96); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.warn { background: var(--busy); border-color: var(--busy); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.wide { width: 100%; justify-content: center; margin-top: 8px; }

.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; color: inherit; font-size: 16px; }
.brand.big { font-size: 24px; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--brand); box-shadow: 0 0 0 3px rgba(214,32,42,.25); }

/* 登入 */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; }
/* Powered by SportsArt（docs/01：所有程式都要顯示） */
.powered { font-size: 11px; color: var(--muted); text-align: center; letter-spacing: .02em; }
.login-card .powered { margin-top: 18px; }
.page-foot { padding: 8px 0 10px; border-top: 1px solid var(--line); }
.sess-bottom .powered { color: #7d8ca0; white-space: nowrap; }
.form-err, .dlg-err { color: var(--danger); min-height: 1.2em; margin-top: 6px; }

/* 版面 */
.layout { display: flex; flex-direction: column; min-height: 100vh; }
.topbar { display: flex; align-items: center; gap: 12px; background: var(--top); color: #fff; padding: 10px 18px; }
.topbar .muted { color: #9fb0c5; }
.who { display: flex; align-items: center; gap: 10px; }
.who .btn.ghost { color: #fff; }
.role { font-size: 12px; padding: 1px 6px; border-radius: 4px; background: rgba(255,255,255,.12); }
.body { flex: 1; display: flex; min-height: 0; }
.side { width: 190px; flex: none; border-right: 1px solid var(--line); padding: 14px 10px; background: var(--panel); }
.side-title { font-size: 12px; color: var(--muted); margin: 4px 8px 8px; }
.side-link { display: flex; align-items: center; justify-content: space-between; padding: 7px 10px; border-radius: 6px; color: var(--text); }
.side-link:hover { background: var(--bg); }
.side-link.active { background: rgba(31,111,235,.12); color: var(--primary); font-weight: 600; }
.side-link .count { color: var(--muted); font-size: 12px; }
.side-add { background: none; border: none; color: var(--muted); padding: 7px 10px; cursor: pointer; font: inherit; }
.side-sep { border-top: 1px solid var(--line); margin: 10px 4px; }
.badge { font-size: 11px; padding: 0 6px; border-radius: 9px; background: var(--line); }
.badge.warn { background: var(--pending); color: #fff; }
.main { flex: 1; padding: 18px 22px; min-width: 0; }

.toolbar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar h2 { margin: 0; font-size: 20px; }
.stats { display: flex; gap: 18px; margin-bottom: 14px; flex-wrap: wrap; }
.st.online { color: var(--online); } .st.busy { color: var(--busy); } .st.offline { color: var(--offline); } .st.pending { color: var(--pending); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 6px; border-left: 4px solid var(--offline); }
.card.online { border-left-color: var(--online); }
.card.busy { border-left-color: var(--busy); }
.card.pending { border-left-color: var(--pending); }
.card.offline { opacity: .8; }
.card-head { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.card-title b { font-weight: 700; }
.dot.online { color: var(--online); } .dot.busy { color: var(--busy); } .dot.offline { color: var(--offline); } .dot.pending { color: var(--pending); }
.card-line { font-size: 13px; min-height: 18px; }
.busy-line { color: var(--busy); }
.warn-text { color: var(--pending); font-size: 12px; }
.card-actions { display: flex; justify-content: space-between; margin-top: 4px; }
.empty { color: var(--muted); padding: 30px; text-align: center; }

.menu { position: absolute; z-index: 20; width: 180px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 4px; }
.menu-item { display: block; width: 100%; text-align: left; border: none; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; font: inherit; color: var(--text); }
.menu-item:hover { background: var(--bg); }

/* 對話框 */
.dlg { border: 1px solid var(--line); border-radius: 12px; padding: 0; width: min(440px, calc(100% - 32px)); background: var(--panel); color: var(--text); }
.dlg::backdrop { background: rgba(10,15,25,.45); }
.dlg form { padding: 20px 22px; }
.dlg h3 { margin: 0 0 10px; }
.dlg p { margin: 6px 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 14px; margin-top: 6px; }
.field { margin: 10px 0; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1c2430; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 50; }
.toast.err { background: var(--danger); }

/* 稽核、帳號表格 */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--bg); }
tr.disabled td { opacity: .5; }
.tag { font-size: 12px; padding: 1px 6px; border-radius: 4px; background: var(--line); font-family: ui-monospace, Menlo, monospace; }
.tag.connect { background: rgba(26,155,75,.15); color: var(--online); }
.tag.disconnect { background: rgba(154,163,176,.2); }
.tag.takeover { background: rgba(217,138,0,.18); color: var(--busy); }
.tag.group_move, .tag.register { background: rgba(31,111,235,.12); color: var(--primary); }

/* 連線畫面 */
.sess { display: flex; flex-direction: column; height: 100vh; background: #0b0f15; color: #e3e8ef; }
.sess-top, .sess-bottom { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--top); flex-wrap: wrap; }
.sess-top .muted, .sess-bottom .muted { color: #9fb0c5; }
.sess-title { font-weight: 700; font-size: 15px; }
.path { font-size: 13px; padding: 2px 8px; border-radius: 10px; background: rgba(255,255,255,.08); }
.path.direct { color: #4ade80; } .path.relay { color: #fbbf24; } .path.ended { color: #9aa3b0; }
.sess-main { flex: 1; display: flex; min-height: 0; }
.stage { position: relative; flex: 1; min-width: 0; background: #000; display: flex; }
.screen { width: 100%; height: 100%; object-fit: contain; outline: none; cursor: crosshair; touch-action: none; }
.overlay { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; background: rgba(5,8,12,.82); text-align: center; padding: 20px; }
.overlay-text { font-size: 16px; max-width: 520px; }
.overlay-actions { display: flex; gap: 10px; }
.spinner { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.2); border-top-color: #fff; border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.tools { width: 200px; flex: none; background: #121924; padding: 12px; overflow-y: auto; border-left: 1px solid #1f2936; }
.tools-title { font-weight: 600; margin-bottom: 8px; }
.tool { display: block; width: 100%; text-align: left; padding: 8px 10px; margin-bottom: 4px; background: #1a2330; border: 1px solid #263243; color: #e3e8ef; border-radius: 6px; cursor: pointer; font: inherit; }
.tool:disabled { opacity: .45; cursor: default; }
.sysinfo { margin: 14px 0 0; font-size: 13px; }
.sysinfo dt { font-weight: 600; margin-bottom: 4px; }
.sysinfo dd { margin: 0 0 3px; }
.sess-bottom .btn { background: #1a2330; border-color: #263243; color: #e3e8ef; }
.sess-top .btn.ghost { color: #e3e8ef; }
.sess-bottom select { background: #1a2330; border-color: #263243; }
.sep { width: 1px; height: 20px; background: #2b3746; }
.brand.small .brand-dot { width: 10px; height: 10px; }

textarea { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%; }
textarea.json { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.small { font-size: 12px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.menu-note { font-size: 12px; color: var(--muted); padding: 6px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.who-btn { max-width: 60vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-btn { display: none; color: #fff !important; font-size: 18px; }

/* 管理：總覽 */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; color: var(--text); border-top: 4px solid var(--offline); }
.tile.online { border-top-color: var(--online); } .tile.busy { border-top-color: var(--busy); } .tile.pending { border-top-color: var(--pending); }
.tile-n { font-size: 28px; font-weight: 700; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: 14px; background: var(--bg); border: 1px solid var(--line); font-size: 13px; }
.chip-x { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 15px; padding: 0 2px; }
.chip-x:hover { color: var(--danger); }
ul.log { margin: 6px 0; padding-left: 18px; font-size: 13px; }
ul.log li { margin: 3px 0; }
.filters.upload { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.dlg-table { max-height: 50vh; overflow: auto; }
.dlg.wide { width: min(720px, calc(100% - 32px)); }

/* 連線畫面：鍵盤接收框（看不到但可取得焦點，手機可叫出虛擬鍵盤） */
.kbd-sink { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; resize: none; font-size: 16px; /* 16px 以上 iOS 不會自動放大 */ }
.quality-now { white-space: nowrap; }
.quality-now.warn { color: #fbbf24; }
.quality-now.hot { color: #f87171; font-weight: 600; }
.clip-text { white-space: pre-wrap; word-break: break-all; background: #1a2330; border-radius: 4px; padding: 4px 6px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }

@media (max-width: 720px) {
  .nav-btn { display: inline-flex; }
  .side { display: none; }
  .layout.nav-open .side { display: block; position: fixed; top: 48px; left: 0; bottom: 0; z-index: 30; width: 240px; overflow-y: auto; box-shadow: 4px 0 20px rgba(0,0,0,.25); }
  .topbar { padding: 8px 10px; }
  .main { padding: 12px; }
  .grid { grid-template-columns: 1fr; }
  .toolbar h2 { font-size: 18px; }
  .btn { padding: 9px 14px; }        /* 觸控好按 */
  .btn.small { padding: 7px 10px; }
  .menu-item { padding: 12px 10px; }
  input, select, textarea { font-size: 16px; } /* iOS 聚焦時不放大 */
  th, td { padding: 8px; }

  /* 連線畫面 */
  .sess { height: 100dvh; }
  .sess-top, .sess-bottom { padding: 6px 8px; gap: 6px; }
  .sess-bottom { overflow-x: auto; flex-wrap: nowrap; }
  .sess-bottom > * { flex: none; }
  .sess-bottom .kbd-btn { order: -1; }
  .sess-top .muted { display: none; }
  .tools { display: none; }
  .tools.open { display: block; position: absolute; right: 0; top: 0; bottom: 0; z-index: 5; width: min(280px, 85vw); box-shadow: -4px 0 20px rgba(0,0,0,.4); }
  .sess-main { position: relative; }
  .who .muted { display: none; }
}
@media (pointer: coarse) {
  .screen { cursor: default; }
}
