/* 台股即時 — iPhone 14 Pro Max (430×932pt) 深色行動介面 */
:root {
  --bg: #0b0f14;
  --surface: #141b22;
  --surface2: #1c2530;
  --line: #25303b;
  --text: #e9eef5;
  --sub: #8b97a5;
  --dim: #5d6b78;
  --up: #ff4d4f;   /* 台股：紅=漲 */
  --down: #27c281; /* 綠=跌 */
  --flat: #8b97a5;
  --accent: #2d9cf0;
  --warn: #ff9f0a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: #000;
  font-family: -apple-system, "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--text); -webkit-font-smoothing: antialiased;
}
body {
  display: flex; align-items: center; justify-content: center;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
input { font: inherit; }
ul { list-style: none; margin: 0; padding: 0; }

/* 桌面預覽：固定為 iPhone 14 Pro Max 尺寸 430×932，不加手機外框；真機(<480px)則全螢幕 */
/* 容器：手機全螢幕；大螢幕（桌面/平板）置中顯示手機尺寸框架並隨視窗高度伸縮 */
#phone {
  width: 100%; max-width: 480px;
  height: 100vh; height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  display: flex; flex-direction: column;
  position: relative; overflow: hidden;
}
@media (min-width: 520px) {
  body { padding: max(16px, 3vh) 16px; background: #05080c; align-items: stretch; }
  #phone {
    flex: 0 0 auto; align-self: center;
    max-width: 460px;
    height: 100%; max-height: 100dvh;
    border-radius: 18px;
    box-shadow: 0 16px 50px rgba(0, 0, 0, .55);
    border: 1px solid #1a2230;
  }
}
@media (min-width: 520px) and (max-height: 760px) {
  body { padding: 10px; }
  #phone { max-width: 420px; }
}

/* ───── 狀態列 ───── */
#statusbar {
  height: 44px; padding: 0 24px 0 24px;
  padding-top: var(--safe-top);
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600;
  flex: 0 0 auto;
}
#statusbar .sb-title { margin: 0 auto; letter-spacing: .5px; }
#statusbar .sb-clock { font-variant-numeric: tabular-nums; color: var(--sub); min-width: 44px; }
#statusbar .sb-badge {
  font-size: 11px; padding: 3px 8px; border-radius: 10px; font-weight: 700;
  background: var(--surface2); color: var(--sub); min-width: 44px; text-align: center;
}
#statusbar .sb-badge.live { color: var(--up); }
#statusbar .sb-badge.live::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--up); margin-right: 5px; vertical-align: 1px;
  animation: pulse 1.4s infinite;
}
#statusbar .sb-badge.pre { color: var(--accent); }
#statusbar .sb-badge.closed { color: var(--dim); }
#statusbar .sb-badge.warn { color: var(--warn); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.35} }

/* ───── 個股切換列 ───── */
.stockbar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px 10px; text-align: left; width: 100%;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.stockbar .sb-stock { font-size: 16px; font-weight: 700; }
.stockbar .sb-code { font-size: 12px; color: var(--dim); }
.stockbar .sb-price { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; }
.stockbar .sb-chg { font-size: 12px; font-weight: 600; }
.stockbar .sb-switch { font-size: 11px; color: var(--accent); margin-left: 6px; }
.up { color: var(--up); } .down { color: var(--down); } .flat { color: var(--flat); }

/* ───── 主內容 ───── */
#app {
  flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px 14px 16px;
  display: flex; flex-direction: column;
}
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 14px; margin-bottom: 12px;
}
.card-title { font-size: 12px; color: var(--sub); letter-spacing: .5px; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.card-title .dot { width: 4px; height: 14px; background: var(--accent); border-radius: 2px; }

/* 行情：現價 */
.quote-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.quote-name { font-size: 20px; font-weight: 800; }
.quote-code { font-size: 13px; color: var(--dim); }
.quote-fullname { font-size: 12px; color: var(--sub); margin-left: auto; text-align: right; }
.price-row { display: flex; align-items: baseline; gap: 12px; margin: 6px 0 14px; }
.price-big { font-size: 42px; font-weight: 800; letter-spacing: -1px; font-variant-numeric: tabular-nums; }
.chg-box { display: flex; flex-direction: column; font-variant-numeric: tabular-nums; }
.chg-box .chg { font-size: 18px; font-weight: 700; }
.chg-box .pct { font-size: 13px; font-weight: 600; }
.badge { font-size: 11px; padding: 3px 8px; border-radius: 8px; background: var(--surface2); color: var(--sub); font-weight: 700; }
.badge.live { color: var(--up); } .badge.warn { color: var(--warn); } .badge.closed, .badge.pre { color: var(--accent); }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: var(--line); border-radius: 12px; overflow: hidden; }
.stat { background: var(--surface2); padding: 10px 8px; text-align: center; }
.stat .k { font-size: 11px; color: var(--sub); margin-bottom: 3px; }
.stat .v { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* 最佳五檔 */
.fivecol { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fivecol h4 { font-size: 11px; color: var(--sub); margin: 0 0 6px; }
.ladder { font-size: 12px; }
.ladder .row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dashed var(--line); font-variant-numeric: tabular-nums; }
.ladder .row .vol { color: var(--dim); }
.ask .p { color: var(--up); } .bid .p { color: var(--down); }

/* 圖表 */
.chart-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.seg { display: inline-flex; background: var(--surface2); border-radius: 9px; padding: 2px; }
.seg button { font-size: 12px; padding: 5px 12px; border-radius: 7px; color: var(--sub); font-weight: 600; }
.seg button.active { background: var(--accent); color: #fff; }
#chart svg { width: 100%; height: 190px; display: block; }
.chart-empty { height: 190px; display: grid; place-items: center; color: var(--dim); font-size: 13px; }

/* 動作按鈕 */
.actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 2px; }
.btn { background: var(--surface2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 8px; border-radius: 12px; font-size: 13px; font-weight: 700; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--up); }
.btn:disabled { opacity: .45; }

/* 自選 */
.list-row { display: flex; align-items: center; gap: 10px; padding: 12px; border-bottom: 1px solid var(--line); }
.list-row .nm { font-weight: 700; } .list-row .cd { font-size: 12px; color: var(--dim); }
.list-row .px { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.list-row .cg { font-size: 12px; }
.list-row .del { color: var(--dim); font-size: 18px; padding: 6px 10px; }
.list-row .del:active { color: var(--up); }
.empty { text-align: center; color: var(--dim); padding: 40px 20px; font-size: 13px; line-height: 1.7; }

/* 自選：新增輸入 + 即時建議 */
.watch-add { position: relative; display: flex; gap: 8px; }
.watch-add input { flex: 1; }
.watch-add .btn { padding: 11px 16px; font-size: 16px; }
.suggest {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--surface2); border: 1px solid var(--line); border-radius: 12px;
  overflow: hidden; box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.suggest.hidden { display: none; }
.suggest .sg { display: flex; align-items: center; gap: 10px; padding: 11px 13px; border-bottom: 1px solid var(--line); }
.suggest .sg:last-child { border-bottom: 0; }
.suggest .sg:active { background: var(--surface); }
.suggest .sgc { font-variant-numeric: tabular-nums; font-weight: 800; min-width: 50px; }
.suggest .sgn { color: var(--text); }
.suggest .sg-empty { padding: 12px 13px; color: var(--dim); font-size: 13px; }

/* 提醒表單 */
.form { display: grid; gap: 10px; }
.form label { font-size: 12px; color: var(--sub); }
.field, select, input.text { width: 100%; background: var(--surface2); border: 1px solid var(--line);
  color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.field:focus, select:focus, input.text:focus { outline: none; border-color: var(--accent); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.alert-row { background: var(--surface2); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.alert-row .top { display: flex; align-items: center; gap: 8px; }
.alert-row .cond { font-size: 13px; }
.alert-row .stat { margin-left: auto; font-size: 11px; padding: 3px 8px; border-radius: 8px; background: var(--surface); color: var(--sub); font-weight: 700; }
.alert-row .stat.fired { color: var(--warn); }
.alert-row .stat.off { color: var(--dim); }
.alert-row .meta { font-size: 11px; color: var(--dim); margin-top: 6px; }
.alert-row .acts { margin-top: 8px; display: flex; gap: 8px; }
.mini-btn { font-size: 12px; padding: 5px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); color: var(--sub); }

/* AI 分析 */
.ai-banner { font-size: 12px; padding: 8px 12px; border-radius: 10px; margin-bottom: 12px; }
.ai-banner.ok { background: rgba(45,156,240,.12); color: var(--accent); }
.ai-banner.off { background: rgba(255,79,79,.1); color: var(--up); }
.ctx-box { white-space: pre-wrap; font-size: 12.5px; line-height: 1.7; color: var(--sub);
  background: var(--surface2); border-radius: 10px; padding: 12px; max-height: 200px; overflow: auto; }
.ai-out { white-space: pre-wrap; font-size: 14px; line-height: 1.8; }
.ai-out .err { color: var(--up); font-size: 13px; }
.typing::after { content: "▋"; animation: pulse 1s infinite; color: var(--accent); }

/* Markdown（AI 回覆） */
.md { white-space: normal; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: .55em 0 .25em; font-weight: 800; line-height: 1.3; }
.md h1 { font-size: 16px; } .md h2 { font-size: 15px; } .md h3 { font-size: 14px; } .md h4 { font-size: 13px; color: var(--sub); }
.md p { margin: 0 0 7px; }
.md ul, .md ol { margin: 0 0 7px; padding-left: 20px; }
.md li { margin: 3px 0; }
.md strong { font-weight: 800; }
.md em { font-style: italic; color: var(--sub); }
.md code { background: var(--surface); padding: 1px 6px; border-radius: 5px; font-size: .92em; }

/* 聊天 */
.chat-wrap { display: flex; flex-direction: column; height: calc(100dvh - 360px); min-height: 280px; }
#chatLog { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 2px; }
.msg { margin-bottom: 10px; max-width: 88%; white-space: pre-wrap; line-height: 1.6; font-size: 14px; }
.msg.user { margin-left: auto; background: var(--accent); color: #fff; border-radius: 14px 14px 4px 14px; padding: 9px 13px; }
.msg.bot { margin-right: auto; background: var(--surface2); border-radius: 14px 14px 14px 4px; padding: 9px 13px; }
.msg.bot .err { color: var(--up); }
.msg.bot.md { white-space: normal; line-height: 1.7; }
.quick { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.quick button { font-size: 12px; padding: 6px 11px; border-radius: 14px; background: var(--surface2); color: var(--sub); border: 1px solid var(--line); }
.chat-input { display: flex; gap: 8px; padding-top: 8px; border-top: 1px solid var(--line); }
.chat-input input { flex: 1; }

/* 分頁列 */
#tabbar {
  flex: 0 0 auto; display: grid; grid-template-columns: repeat(7, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tab {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 8px 0 7px; color: var(--dim); font-weight: 600;
}
.tab .ti { font-size: 18px; line-height: 1; opacity: .55; transition: opacity .15s, transform .15s; }
.tab .tl { font-size: 10px; letter-spacing: .3px; }
.tab.active { color: var(--accent); }
.tab.active .ti { opacity: 1; transform: scale(1.1); }
.tab.active::before {
  content: ""; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  width: 22px; height: 3px; background: var(--accent); border-radius: 0 0 3px 3px;
}

/* 搜尋覆蓋 */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 100; display: flex; flex-direction: column; align-items: center; }
.overlay.hidden { display: none; }
.overlay-card { width: 100%; max-width: 430px; background: var(--bg); margin-top: calc(var(--safe-top) + 44px);
  border-radius: 18px 18px 0 0; padding: 14px; flex: 1; overflow-y: auto; }
.overlay-head { display: flex; gap: 10px; margin-bottom: 14px; position: sticky; top: 0; background: var(--bg); padding-bottom: 10px; }
.overlay-head input { flex: 1; background: var(--surface2); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 11px 14px; font-size: 15px; }
.overlay-head input:focus { outline: none; border-color: var(--accent); }
.overlay-head button { color: var(--accent); font-size: 15px; font-weight: 600; }
.hint { font-size: 12px; color: var(--dim); margin: 6px 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chips .chip { font-size: 13px; padding: 7px 12px; border-radius: 16px; background: var(--surface2); color: var(--text); border: 1px solid var(--line); }
.chips .chip:active { border-color: var(--accent); color: var(--accent); }
.results .r { display: flex; align-items: center; gap: 10px; padding: 12px 8px; border-bottom: 1px solid var(--line); }
.results .r .cd { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 56px; }
.results .r .nm { color: var(--text); }
.results .r .ex { margin-left: auto; font-size: 11px; color: var(--dim); }

/* toast */
.toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); transform: translateX(-50%);
  background: #222; color: #fff; padding: 12px 18px; border-radius: 14px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4); z-index: 200; max-width: 90%; text-align: center; }
.toast.hidden { display: none; }

.loading { color: var(--dim); font-size: 13px; padding: 20px; text-align: center; }
.skeleton { color: var(--dim); }

/* 大盤加權指數列 */
.index-strip { display: flex; align-items: baseline; gap: 10px; }
.index-strip .idx-name { font-size: 12px; color: var(--sub); }
.index-strip .idx-val { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.index-strip .idx-chg { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.idx-date { margin-left: auto; font-size: 11px; color: var(--dim); }
.refresh-btn { font-size: 18px; padding: 4px 8px; color: var(--sub); line-height: 1; }
.refresh-btn.spinning { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* AI 子分頁（分析 / 聊天）*/
.aisub { display: flex; gap: 8px; margin-bottom: 12px; }
.aisub button { flex: 1; padding: 9px 0; border-radius: 10px; background: var(--surface2); color: var(--sub); font-weight: 700; font-size: 13px; border: 1px solid var(--line); }
.aisub button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.aipanel { display: none; }
.aipanel.active { display: block; }

/* 持倉 */
.hold-form { display: grid; gap: 10px; }
.hold-total { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; }
.hold-total .k { font-size: 12px; color: var(--sub); }
.hold-total .v { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; }
.hold-row { display: flex; align-items: center; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.hold-row .hnm { font-weight: 700; } .hold-row .hcd { font-size: 12px; color: var(--dim); }
.hold-row .hmid { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
.hold-row .hpx { font-weight: 700; }
.hold-row .hpnl { font-size: 12px; }
.hold-row .hmeta { font-size: 11px; color: var(--dim); }
.hold-row .hacts { display: flex; flex-direction: column; gap: 4px; margin-left: 10px; }
.hold-row .hacts .mini-btn { font-size: 11px; padding: 4px 8px; }

/* 榜單 */
.board-seg { display: flex; gap: 8px; margin-bottom: 12px; }
.board-seg button { flex: 1; padding: 8px 0; border-radius: 10px; background: var(--surface2); color: var(--sub); font-weight: 700; font-size: 13px; border: 1px solid var(--line); }
.board-seg button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.brow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.brow .brk { font-size: 11px; color: var(--dim); width: 18px; text-align: center; font-weight: 700; }
.brow .bcode { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 46px; }
.brow .bname { color: var(--text); }
.brow .bpx { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.brow .bpct { font-size: 12px; min-width: 64px; text-align: right; font-variant-numeric: tabular-nums; }
.brow .bvol { font-size: 11px; color: var(--dim); min-width: 64px; text-align: right; }

/* 彈性聊天（填滿 AI 卡片剩餘高度，取代寫死的 calc）*/
.card.chat-card { display: flex; flex-direction: column; padding-bottom: 12px; flex: 1 1 auto; min-height: 0; }
.chat-wrap { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 180px; }
#chatLog { flex: 1 1 auto; min-height: 0; }

/* 聊天：停止按鈕、清除對話 */
.chat-input .btn.stop { background: var(--surface2); border-color: var(--warn); color: var(--warn); }
.chat-input .btn.stop:disabled { opacity: .4; }
.chat-tools { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.chat-tools .ct-hint { font-size: 11px; color: var(--dim); }
.chat-tools .ct-clear { margin-left: auto; font-size: 11px; padding: 4px 10px; border-radius: 8px; background: var(--surface2); color: var(--sub); border: 1px solid var(--line); }

/* 設定頁 */
.set-sec { margin-bottom: 14px; }
.set-sec:last-child { margin-bottom: 0; }
.set-row { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.set-row:last-child { margin-bottom: 0; }
.set-row label { font-size: 12px; color: var(--sub); display: flex; align-items: center; gap: 6px; }
.set-row label .hint { font-size: 10px; color: var(--dim); }
.set-row input.text, .set-row select, .set-row textarea { width: 100%; }
.set-row textarea { resize: vertical; min-height: 64px; line-height: 1.5; }
.set-inline { display: flex; gap: 8px; align-items: flex-end; }
.set-inline > * { flex: 1; }
.set-inline .btn { flex: 0 0 auto; padding: 11px 14px; }
.set-seg2 { display: flex; gap: 8px; }
.set-seg2 button { flex: 1; padding: 9px 0; border-radius: 10px; background: var(--surface2); color: var(--sub); font-weight: 700; font-size: 13px; border: 1px solid var(--line); }
.set-seg2 button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.set-note { font-size: 11px; color: var(--dim); line-height: 1.6; }
.set-status { font-size: 12px; padding: 8px 12px; border-radius: 10px; margin-top: 10px; }
.set-status.ok { background: rgba(39,194,129,.12); color: var(--down); }
.set-status.err { background: rgba(255,79,79,.1); color: var(--up); }
.set-status.busy { background: rgba(45,156,240,.12); color: var(--accent); }
.set-ro { font-size: 12px; color: var(--dim); padding: 10px 12px; background: var(--surface2); border-radius: 10px; border: 1px solid var(--line); }
.set-actions { display: flex; gap: 10px; margin-top: 14px; position: sticky; bottom: 0; background: var(--surface); padding-top: 10px; }
.set-actions .btn { flex: 1; }