:root {
  --bg: #f4f6fb; --card: #ffffff; --ink: #1f2a44; --sub: #6b7590; --line: #e4e8f2;
  --accent: #3d6bf5; --accent-ink: #ffffff; --accent-soft: #e8eeff;
  --ok: #1f9d6b; --ok-soft: #e3f6ee; --warn: #c27c0e; --warn-soft: #fdf2dc; --bad: #d2453b; --bad-soft: #fde8e6;
  --shadow: 0 1px 2px rgba(31, 42, 68, .06), 0 4px 16px rgba(31, 42, 68, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151d; --card: #1b2030; --ink: #e8ecf6; --sub: #98a2bb; --line: #2a3145;
    --accent: #6b8cff; --accent-soft: #252f4d; --ok: #3cc58d; --ok-soft: #173a2c; --warn: #e3a43a; --warn-soft: #3a2f17;
    --bad: #f0675d; --bad-soft: #3d1f1d; --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.6 -apple-system, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.page { max-width: 640px; margin: 0 auto; padding: 16px 16px calc(28px + env(safe-area-inset-bottom)); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 22px; }
.brand.small { font-size: 18px; }
.logo { width: 40px; height: 40px; }
.brand.small .logo { width: 32px; height: 32px; }

.primary, .ghost, .danger {
  border: 0; border-radius: 12px; padding: 8px 16px; font-weight: 600; white-space: nowrap;
}
.primary { background: var(--accent); color: var(--accent-ink); }
.primary:disabled { opacity: .4; cursor: default; }
.ghost { background: var(--accent-soft); color: var(--accent); }
.danger { background: var(--bad-soft); color: var(--bad); }
.link { background: none; border: 0; padding: 0; color: var(--accent); }
input[type="text"], input[type="password"] {
  font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; width: 100%;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* 登入 */
#login { min-height: 100%; display: grid; place-items: center; }
.login-card { width: min(360px, 100%); background: var(--card); border-radius: 22px; padding: 28px 22px; box-shadow: var(--shadow); display: grid; gap: 18px; }
.login-card .brand { justify-content: center; }
#loginForm { display: grid; gap: 10px; }
#loginForm .primary { padding: 11px; font-size: 16px; }
.msg { margin: 0; min-height: 1.6em; text-align: center; color: var(--bad); font-size: 14px; }
#login { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.pub-links { display: flex; flex-wrap: wrap; justify-content: center; align-items: baseline; gap: 4px 14px; padding: 16px 0 4px; font-size: 14px; color: var(--sub); }
.dl-link { color: var(--accent); text-decoration: none; font-weight: 600; }
.dl-info { color: var(--sub); font-size: 12px; }

/* 使用說明（蓋在畫面上，不換頁）*/
#help { position: fixed; inset: 0; z-index: 30; background: rgba(15, 20, 32, .45); display: flex; justify-content: center; align-items: flex-end; }
.help-sheet {
  width: min(680px, 100%); max-height: calc(100% - 24px); background: var(--bg); border-radius: 22px 22px 0 0;
  display: flex; flex-direction: column; box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
}
@media (min-width: 720px) {
  #help { align-items: center; }
  .help-sheet { border-radius: 22px; max-height: calc(100% - 48px); }
}
.help-top { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px 20px; }
.help-top h1 { font-size: 19px; margin: 0; }
.help-close { width: 40px; height: 40px; border: 0; border-radius: 12px; background: var(--card); color: var(--sub); display: grid; place-items: center; }
.help-close svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.help-body { overflow-y: auto; padding: 6px 16px calc(20px + env(safe-area-inset-bottom)); display: grid; gap: 8px; -webkit-overflow-scrolling: touch; }
.help-body details { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 16px; }
.help-body summary { cursor: pointer; font-weight: 700; }
.help-body p, .help-body ol, .help-body ul { margin: 10px 0 0; }
.help-body ol, .help-body ul { padding-left: 22px; }
.help-body li + li { margin-top: 4px; }
.help-body .note { color: var(--sub); font-size: 14px; }
.gest { border-collapse: collapse; margin-top: 8px; width: 100%; font-size: 14px; }
.gest th, .gest td { text-align: left; padding: 7px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.gest th { font-weight: 600; width: 50%; }
.ico { width: 18px; height: 18px; vertical-align: -3px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* 清單 */
.top { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 14px; }
.hub-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--sub); }
.hub-dot.on { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-soft); }
.hub-dot.off { background: var(--bad); box-shadow: 0 0 0 4px var(--bad-soft); }
.group h2 { font-size: 13px; color: var(--sub); font-weight: 600; margin: 18px 4px 8px; }
.device {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.device-main { display: flex; align-items: center; gap: 12px; }
.pc { position: relative; width: 40px; height: 40px; flex: none; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.pc svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.pc i { position: absolute; right: -2px; bottom: -2px; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--card); background: var(--sub); }
.pc i.on { background: var(--ok); }
.device.offline .pc { background: var(--line); color: var(--sub); }
.info { flex: 1; min-width: 0; }
.info b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { color: var(--sub); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.more { border: 0; background: none; color: var(--sub); width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; flex: none; }
.more svg { width: 20px; height: 20px; fill: currentColor; }
.device-edit { display: flex; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.device-edit input { flex: 1 1 160px; }
.empty { color: var(--sub); font-size: 14px; padding: 18px 4px; }
.who { display: flex; flex-wrap: wrap; gap: 8px; }
.who button { border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 999px; padding: 7px 16px; font-weight: 600; }
.who button.none { color: var(--sub); font-weight: 400; }
.this-tag { flex: none; padding: 6px 14px; border-radius: 12px; background: var(--ok-soft); color: var(--ok); font-weight: 600; font-size: 14px; }

.foot { margin-top: 26px; color: var(--sub); font-size: 13px; display: grid; gap: 6px; }
.foot details { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px; }
.foot summary { cursor: pointer; color: var(--ink); font-weight: 600; }
.foot-row { display: flex; justify-content: space-between; align-items: center; padding: 4px 4px 0; }
.ver h3 { font-size: 14px; color: var(--ink); margin: 12px 0 2px; }
.ver ul { margin: 0; padding-left: 20px; }
.logrow { display: flex; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); }
.logrow:first-child { border-top: 0; }
.logrow time { flex: none; width: 88px; font-variant-numeric: tabular-nums; }
.logrow span { color: var(--ink); min-width: 0; }
.logrow .bad { color: var(--bad); }

/* 遠端畫面 */
#remote {
  position: fixed; left: 0; top: var(--vv-top, 0px); width: 100%; height: var(--vv-h, 100dvh);
  background: #0d0f14; display: flex; flex-direction: column; overflow: hidden; z-index: 10;
}
#stage { position: relative; flex: 1; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
#video { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #000; pointer-events: none; visibility: hidden; }
#status {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%); max-width: 82%;
  background: rgba(0, 0, 0, .72); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; text-align: center; pointer-events: none;
}
#status.err { background: rgba(160, 40, 32, .9); pointer-events: auto; }
#kb {
  position: fixed; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; resize: none;
  font-size: 16px; color: transparent; background: transparent; caret-color: transparent;
}
/* 特殊鍵列：開了手機鍵盤時貼在鍵盤上方 */
#keys { flex: none; background: #161a24; padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); }
body.kb-open #keys { padding-bottom: 6px; }
.keys-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.keys-row::-webkit-scrollbar { display: none; }
.keys-row button {
  flex: none; min-width: 44px; height: 40px; padding: 0 10px; border: 0; border-radius: 10px;
  background: #262c3b; color: #e8ecf6; font-size: 13px; font-weight: 600; display: flex; align-items: center; justify-content: center;
}
.keys-row button.on { background: #3d6bf5; color: #fff; }
.keys-row svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* 手機上放大顯示的游標 */
#cursor {
  position: absolute; left: 0; top: 0; width: 26px; height: 26px; margin: -3px 0 0 -5px; pointer-events: none;
  fill: #fff; stroke: #111; stroke-width: 1.6; stroke-linejoin: round; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, .5));
}

/* 浮動按鈕（可以拖到任何位置）與展開的選單 */
#fab {
  position: absolute; left: 0; top: 0; width: 52px; height: 52px; padding: 0; border: 0; border-radius: 16px; background: none;
  opacity: .78; touch-action: none; z-index: 3; filter: drop-shadow(0 3px 8px rgba(0, 0, 0, .45)); transition: opacity .15s;
}
#fab svg { width: 52px; height: 52px; display: block; }
#fab.open, #fab:active { opacity: 1; }
#pie { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
#pie button {
  position: absolute; left: 0; top: 0; width: 60px; height: 60px; border: 0; border-radius: 50%; pointer-events: auto;
  background: rgba(30, 35, 48, .94); color: #e8ecf6; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4); font-size: 10.5px; font-weight: 600; line-height: 1.15; padding: 0 2px; animation: pop .14s ease-out;
}
#pie button.on { background: #3d6bf5; color: #fff; }
#pie svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#pie span { white-space: nowrap; }
@keyframes pop { from { opacity: 0; } to { opacity: 1; } }

/* 切換螢幕：照實際擺放位置畫的小地圖 */
#scr { position: absolute; inset: 0; z-index: 4; background: rgba(0, 0, 0, .45); display: flex; align-items: center; justify-content: center; }
#scr[hidden] { display: none; }
.scr-card {
  background: #1e2330; color: #e8ecf6; border-radius: 18px; padding: 16px 18px 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
  max-width: calc(100% - 32px); animation: pop .14s ease-out;
}
.scr-title { font-size: 14px; font-weight: 700; margin-bottom: 14px; text-align: center; }
#scrMap { position: relative; margin: 0 auto; }
#scrMap button {
  position: absolute; border: 2px solid #3a4256; border-radius: 8px; background: #262c3b; color: #e8ecf6; padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
#scrMap button b { font-size: 22px; line-height: 1; }
#scrMap button small { font-size: 11px; opacity: .75; }
#scrMap button.on { background: #3d6bf5; border-color: #8aa6ff; color: #fff; }
@media (hover: hover) and (pointer: fine) {
  #scrMap button:hover { filter: brightness(1.2); }
}

@media (hover: hover) and (pointer: fine) {
  #stage { cursor: crosshair; }
  .keys-row button:hover, #pie button:hover { filter: brightness(1.15); }
}
