* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: system-ui, "Noto Sans TC", sans-serif; background: #111; color: #eee; }

/* 登入頁 */
.login-body { display: flex; align-items: center; justify-content: center; }
.login-card { background: #1c1c22; padding: 32px; border-radius: 14px; width: min(92vw, 380px); box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.login-card h1 { font-size: 1.5rem; margin-bottom: 14px; }
.login-card .hint { color: #aab; font-size: .9rem; line-height: 1.5; margin: 8px 0; }
.login-card code { background: #333; padding: 1px 6px; border-radius: 4px; }
.login-card hr { border: none; border-top: 1px solid #333; margin: 14px 0; }
.login-card input { width: 100%; margin: 6px 0; padding: 10px; border-radius: 8px; border: 1px solid #444; background: #26262e; color: #eee; }
.login-card button { width: 100%; margin-top: 10px; padding: 10px; border: none; border-radius: 8px; background: #3a7afe; color: #fff; font-size: 1rem; cursor: pointer; }
.err { color: #ff7070; font-size: .85rem; margin-top: 10px; min-height: 1.2em; }

/* 第二道門:計算機 */
.calc-body { display: flex; align-items: center; justify-content: center; background: #111; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.calc-frame {
  width: 100vw; height: 100dvh; max-width: 430px; max-height: 860px;
  display: flex; flex-direction: column; background: #000; padding: 18px 14px; gap: 16px;
}
.calc-bar {
  flex: none; display: flex; justify-content: space-between; align-items: center;
  color: #9a9aa2; font-size: .95rem; padding: 4px 10px 0;
}
.calc-screen { flex: none; display: flex; flex-direction: column; justify-content: flex-end; min-height: 110px; }
.calc-expr {
  color: #9a9aa2; font-size: 1.15rem; text-align: right; padding: 0 10px; min-height: 1.5em;
  overflow: hidden; white-space: nowrap; /* 靠右對齊:超長時左端被裁,尾端(最新輸入)保持可見 */
}
.calc-display {
  display: flex; align-items: flex-end; justify-content: flex-end;
  color: #fff; font-size: 3rem; font-weight: 300; padding: 0 8px; overflow: hidden;
  white-space: nowrap; text-overflow: ellipsis;
}
.calc-grid { flex: 1; display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(5, 1fr); gap: 10px; }
.calc-btn {
  border: none; border-radius: 14px; background: #333; color: #fff; font-size: 1.6rem;
  cursor: pointer; display: flex; align-items: center; justify-content: center; -webkit-tap-highlight-color: transparent;
}
.calc-btn:active { filter: brightness(1.35); }
.calc-fn { background: #a5a5a5; color: #000; }
.calc-op { background: #ff9f0a; color: #fff; }
.calc-eq { background: #ff9f0a; color: #fff; grid-row: span 2; }
.calc-zero { grid-column: span 2; }

/* 主介面 */
.app-body { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
#topbar, #navbar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; background: #1a1a20; flex: none; }
#topbar { position: relative; }
#navbar { justify-content: center; gap: 28px; }
/* 隱藏鍵盤承接框:視覺上不可見但可聚焦接收打字 */
#kbd { position: fixed; bottom: 0; left: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; background: transparent; color: transparent; caret-color: transparent; pointer-events: none; z-index: -1; }
.brand { font-weight: 600; margin-right: 4px; white-space: nowrap; }
.spacer { flex: 1; min-width: 0; }
.stat { color: #8a9; font-size: .72rem; white-space: nowrap; overflow: hidden; }
#topbarActions { display: flex; align-items: center; gap: 6px; flex: none; }
#btnMore { display: none; }
/* 寬度 < 900 時,把所有功能按鈕收進一個下拉選單,避免把狀態資訊擠到看不見 */
@media (max-width: 900px) {
  #btnMore { display: flex; }
  #topbarActions {
    display: none;
    position: absolute; top: calc(100% + 4px); right: 6px;
    flex-direction: column; align-items: stretch; gap: 4px;
    background: #1a1a20; border: 1px solid #3a3a46; border-radius: 10px;
    padding: 6px; z-index: 25; box-shadow: 0 10px 30px rgba(0,0,0,.6);
  }
  #topbarActions.open { display: flex; }
  #topbarActions button { width: 100%; justify-content: flex-start; text-align: left; }
}
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.dot.on { background: #35d07f; } .dot.off { background: #777; } .dot.busy { background: #f5a623; }
#topbar button, #navbar button { background: #2a2a33; border: 1px solid #3a3a46; color: #eee; border-radius: 8px; padding: 6px 14px; font-size: 1.1rem; cursor: pointer; flex: none; }
#topbar button:active, #navbar button:active { background: #3a7afe; }

#stage { flex: 1; position: relative; display: flex; align-items: center; justify-content: center; background: #000; min-height: 0; }
#screen { max-width: 100%; max-height: 100%; touch-action: none; cursor: crosshair; }
#overlay { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 14px; align-items: center; justify-content: center; background: rgba(0,0,0,.72); z-index: 5; text-align: center; padding: 20px; }
#overlay[hidden] { display: none; }
#overlayBtn { padding: 10px 26px; font-size: 1rem; border: none; border-radius: 10px; background: #3a7afe; color: #fff; cursor: pointer; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 20; }
.modal[hidden] { display: none; }
.modal-box { background: #1c1c22; border-radius: 12px; padding: 20px; width: min(92vw, 420px); max-height: 80vh; overflow: auto; }
.modal-box h3 { margin: 0 0 12px; }
.modal-box .hint { color: #aab; font-size: .85rem; line-height: 1.5; margin: 0 0 14px; }
.modal-hr { border: none; border-top: 1px solid #333; margin: 22px 0 16px; }
.settings-grid { display: flex; flex-direction: column; gap: 12px; margin: 0 0 14px; }
.settings-grid label { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: .9rem; color: #ccd; }
.settings-grid input { width: 90px; padding: 6px 8px; border-radius: 8px; border: 1px solid #3a3a46; background: #26262e; color: #eee; text-align: right; }
.app-row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid #2a2a33; font-size: .85rem; }
.app-row .pkg { flex: 1; word-break: break-all; }
.app-row button { padding: 4px 10px; font-size: .8rem; border-radius: 6px; border: none; cursor: pointer; }
.app-row .remove { background: #6b2e2e; color: #fff; }
.modal-actions { margin-top: 14px; text-align: right; }
.modal-actions button { padding: 7px 18px; border-radius: 8px; border: none; background: #3a3a46; color: #eee; cursor: pointer; }
.modal-actions.clip-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-start; text-align: left; }
.modal-actions.clip-actions button { padding: 6px 12px; font-size: .8rem; }
#clipText { width: 100%; resize: vertical; padding: 8px 10px; border-radius: 8px; border: 1px solid #3a3a46; background: #26262e; color: #eee; font-family: inherit; }

/* 設定頁:頁籤 */
.tabs { display: flex; gap: 4px; margin: 0 0 16px; border-bottom: 1px solid #333; }
.tabs .tab { flex: 1; padding: 9px 8px; border: none; background: none; color: #9a9aa2;
  font-size: .9rem; font-family: inherit; cursor: pointer; border-bottom: 2px solid transparent; }
.tabs .tab.active { color: #fff; border-bottom-color: #3a7afe; }
.tab-panel[hidden] { display: none; }

#ruleSteps, #resetSteps { max-height: 50vh; overflow-y: auto; }
.rule-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.modal-box .hint.danger { color: #ff9b9b; background: #3a1f24; border: 1px solid #7a3a42;
  border-radius: 6px; padding: 10px 12px; }
.modal-box .hint.danger strong { color: #ffd0d0; }

/* 通知設置:三欄規則 */
#notifyRows { max-height: 50vh; overflow-y: auto; }
.notify-row { display: flex; align-items: center; gap: 6px; padding: 6px 0; }
.notify-row select { flex: 1; min-width: 0; padding: 7px 6px; border-radius: 8px; border: 1px solid #3a3a46;
  background: #26262e; color: #eee; font-size: .85rem; font-family: inherit; }
.notify-row .remove { flex: 0 0 auto; padding: 6px 10px; font-size: .8rem; border-radius: 6px;
  border: none; background: #6b2e2e; color: #fff; cursor: pointer; }
.notify-row .notify-freq { flex: 0 0 auto; display: flex; align-items: center; gap: 3px; font-size: .78rem; color: #aaa; }
.notify-row .notify-freq input { width: 3.4em; padding: 6px 4px; text-align: right; border-radius: 8px;
  border: 1px solid #3a3a46; background: #26262e; color: #eee; font-size: .85rem; font-family: inherit; }
.notify-row .notify-freq input:disabled { opacity: .4; cursor: not-allowed; }
.rule-row input, .rule-row select { flex: 1; padding: 7px 8px; border-radius: 8px; border: 1px solid #3a3a46; background: #26262e; color: #eee; font-size: .9rem; font-family: inherit; }
.rule-row select { flex: 0 0 auto; min-width: 4.5em; }

.notify-tmpl { margin: 16px 0 0; border-top: 1px solid #2c2c34; padding-top: 14px; }
.notify-tmpl-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.notify-tmpl-head label { font-size: .9rem; color: #eee; }
.notify-tmpl .hint code { background: #26262e; border: 1px solid #3a3a46; border-radius: 4px; padding: 0 4px; font-size: .8em; color: #bcd; }
.notify-tmpl textarea { width: 100%; box-sizing: border-box; padding: 9px 10px; border-radius: 8px; border: 1px solid #3a3a46;
  background: #26262e; color: #eee; font-size: .85rem; line-height: 1.5; font-family: inherit; resize: vertical; }
.notify-tmpl input[type="text"] { width: 100%; box-sizing: border-box; padding: 9px 10px; border-radius: 8px;
  border: 1px solid #3a3a46; background: #26262e; color: #eee; font-size: .9rem; font-family: inherit; }
.linklike { background: none; border: none; color: #6aa9ff; font-size: .8rem; cursor: pointer; padding: 2px 4px; }
.linklike:hover { text-decoration: underline; }


#toast { position: fixed; bottom: 70px; left: 50%; transform: translateX(-50%); background: #333a; color: #fff; padding: 10px 18px; border-radius: 10px; font-size: .85rem; z-index: 30; backdrop-filter: blur(6px); max-width: 90vw; }

/* ── 計算機桌面(登入後):綠色主題 + 首頁/串流視圖切換 ── */
.calc-frame.green .calc-op, .calc-frame.green .calc-eq { background: #2e9e6b; }
.calc-frame.green .calc-display { color: #d7ffe9; }
.calc-frame.green .calc-bar { color: #7fcfa6; }
.calc-frame.green .calc-eq:active, .calc-frame.green .calc-op:active { filter: brightness(1.25); }

/* 綠色計算機首頁:填滿 app-body、置中;串流時隱藏 */
#calcHome { flex: 1; display: flex; align-items: center; justify-content: center; background: #0a140f; min-height: 0; }
body.streaming #calcHome { display: none; }
/* 頂列/舞台/導覽列預設隱藏(計算機桌面純淨),僅串流操作 App 時顯示 */
#topbar, #stage, #navbar { display: none; }
body.streaming #topbar { display: flex; }
body.streaming #navbar { display: flex; }
body.streaming #stage { display: flex; }

/* 功能碼設定卡片 */
.func-card { border: 1px solid #3a3a46; border-radius: 10px; padding: 10px; margin-bottom: 12px; background: #15151b; }
.func-card .rule-row { margin-bottom: 8px; }
.func-card .linklike { margin-top: 6px; }

/* ───────── iOS / Apple 手機規範 ───────── */
* { -webkit-tap-highlight-color: transparent; }               /* 關點擊藍框高亮 */
html, body { max-width: 100%; overflow-x: hidden; }            /* 防橫向溢出 */
/* 表單字級 ≥16px:避免 iOS Safari 聚焦時自動放大畫面;且不破格 */
input, select, textarea { font-size: 16px; box-sizing: border-box; max-width: 100%; }
textarea { -webkit-overflow-scrolling: touch; }
/* 點擊目標 ≥44px(Apple HIG) */
#topbar button, #navbar button, .modal-actions button, .login-card button,
.tabs .tab, .app-row button, #overlayBtn, .linklike { min-height: 44px; }
#topbar button, #navbar button { min-width: 44px; }
/* 安全區留白(瀏海 / Home Indicator):viewport-fit=cover 搭配 env() */
.login-body { box-sizing: border-box;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
#topbar { padding-top: calc(6px + env(safe-area-inset-top)); }
#navbar { padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
.calc-frame { box-sizing: border-box;
  padding-top: calc(18px + env(safe-area-inset-top));
  padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
/* modal:安全區 + 捲動順暢 + 不溢出 */
.modal { box-sizing: border-box;
  padding: calc(8px + env(safe-area-inset-top)) 10px calc(8px + env(safe-area-inset-bottom)); }
.modal-box { -webkit-overflow-scrolling: touch; overflow-y: auto; max-height: 90dvh; max-width: 100%; box-sizing: border-box; }

/* 訊息頁說明段收合鈕 */
.hint-toggle { background: none; border: none; color: #6fa8ff; cursor: pointer; font-size: .85rem; padding: 0 2px; }
.hint-toggle:active { color: #9ac2ff; }

/* SystemUI 若殘留顯示列時的保險:裁切串流上/下邊(狀態列約 3%、導覽列約 6%)→顯黑,蓋掉列 */
#stage.crop-bars #screen { clip-path: inset(3% 0 6% 0); }

/* 自訂通知訊息列 */
.custom-row { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.custom-row input { flex: 0 0 34%; }
.custom-row textarea { flex: 1; resize: vertical; }
