:root {
  color-scheme: light;
  --bg: #f4f3ef; --surface: #fdfcfa; --surface-2: #efede8; --border: #e2e0d9; --border-2: #cfccc3;
  --text: #1b1a17; --text-2: #55534d; --muted: #8a877f; --grid: #ebe9e3;
  --accent: #2a78d6; --accent-ink: #1d5fae; --good: #0f7a3d; --bad: #c4332f;
  --accent-rgb: 42 120 214; --bad-rgb: 196 51 47;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --ref: #8a8984;
  --sh: 60 52 38; --sh-a: 1;   /* 陰影色相跟著暖灰底色 */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #131312; --surface: #1b1b19; --surface-2: #262623; --border: #302f2b; --border-2: #45443e;
    --text: #f1f0ea; --text-2: #c3c2b7; --muted: #8f8e86; --grid: #2a2a27;
    --accent: #3987e5; --accent-ink: #8ab8f0; --good: #3fb56f; --bad: #e66767; --accent-rgb: 57 135 229; --bad-rgb: 230 103 103;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --ref: #8f8e86;
    --sh: 0 0 0; --sh-a: 3;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #131312; --surface: #1b1b19; --surface-2: #262623; --border: #302f2b; --border-2: #45443e;
  --text: #f1f0ea; --text-2: #c3c2b7; --muted: #8f8e86; --grid: #2a2a27;
  --accent: #3987e5; --accent-ink: #8ab8f0; --good: #3fb56f; --bad: #e66767; --accent-rgb: 57 135 229; --bad-rgb: 230 103 103;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --ref: #8f8e86;
  --sh: 0 0 0; --sh-a: 3;
}
:root {
  /* 離線執行：只用系統字體。Segoe UI Variable（Win11）負責拉丁字與數字，中文落到微軟正黑體 */
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Microsoft JhengHei UI", "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
  --font-display: "Segoe UI Variable Display", "Segoe UI", system-ui, -apple-system, "Microsoft JhengHei UI", "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
  /* 半透明色用 RGB 三元組，不依賴 color-mix（含 var() 的宣告無法退回上一行） */
  --accent-soft: rgb(var(--accent-rgb) / .12);
  --ring: rgb(var(--accent-rgb) / .55);
  --shadow-1: 0 1px 2px rgb(var(--sh) / calc(.05 * var(--sh-a))), 0 1px 1px rgb(var(--sh) / calc(.03 * var(--sh-a)));
  --shadow-2: 0 10px 28px -6px rgb(var(--sh) / calc(.16 * var(--sh-a))), 0 2px 6px rgb(var(--sh) / calc(.06 * var(--sh-a)));
  --shadow-3: 0 28px 70px -12px rgb(var(--sh) / calc(.32 * var(--sh-a))), 0 6px 18px rgb(var(--sh) / calc(.10 * var(--sh-a)));
  --r-lg: 14px; --r-md: 10px; --r-sm: 7px;
  --ease: cubic-bezier(.2, .7, .2, 1);
  /* z-index 階層 */
  --z-tip: 3; --z-header: 10; --z-backdrop: 29; --z-max: 30; --z-toast: 40; --z-skip: 50;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body { margin: 0; min-height: 100dvh; background: var(--bg); color: var(--text);
  font: 14px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
::selection { background: rgb(var(--accent-rgb) / .24); }
:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
.skip { position: fixed; top: 8px; left: 8px; z-index: var(--z-skip); padding: 8px 14px; border-radius: var(--r-sm);
  background: var(--text); color: var(--surface); text-decoration: none; transform: translateY(-150%); transition: transform .2s var(--ease); }
.skip:focus { transform: none; }
header { position: sticky; top: 0; z-index: var(--z-header); border-bottom: 1px solid var(--border); background: var(--surface);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
.bar { max-width: 1280px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap; }
h1 { font: 650 15px/1.2 var(--font-display); letter-spacing: -.01em; margin: 0; display: flex; align-items: center; gap: 10px; }
h1 .mark { width: 22px; height: 22px; flex: none; border-radius: 6px; display: grid; place-items: center;
  background: var(--text); color: var(--surface); }
h1 .ver { font: 500 11px/1 var(--font); color: var(--text-2); letter-spacing: .02em; padding: 3px 6px; border-radius: 4px; background: var(--surface-2); }
nav { display: flex; gap: 2px; flex-wrap: wrap; }
nav button { background: none; border: 0; padding: 6px 12px; border-radius: var(--r-sm); color: var(--text-2);
  cursor: pointer; font: 500 13.5px/1.4 var(--font); transition: background .2s var(--ease), color .2s var(--ease); }
nav button:hover { background: var(--surface-2); color: var(--text); }
nav button:active { transform: translateY(1px); }
nav button.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.spacer { flex: 1; }
main { max-width: 1280px; margin: 0 auto; padding: 20px 20px 48px;
  padding: 20px max(20px, env(safe-area-inset-right)) calc(48px + env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left)); }
main:focus { outline: none; }
section.tab { display: none; }
section.tab.on { display: block; animation: tabin .28s var(--ease); }
/* 只動 opacity：transform 會讓 section 暫時成為 .card.max（position: fixed）的定位容器 */
@keyframes tabin { from { opacity: 0; } to { opacity: 1; } }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: 18px 20px 20px; margin-bottom: 16px;
  box-shadow: var(--shadow-1); }
.card h2 { font: 600 15px/1.35 var(--font-display); letter-spacing: -.005em; margin: 0 0 14px; text-wrap: balance; }
.card h2 small { font: 400 12px/1.4 var(--font); color: var(--muted); margin-left: 8px; letter-spacing: 0; }
.card h2.sub { font-size: 14px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.grid2 > .card { margin-bottom: 0; }
/* 格線子項預設 min-width:auto，會被寬表格撐開；設 0 讓表格在 .tablewrap 內捲動 */
.grid2 > *, .grid3 > * { min-width: 0; }
.grid3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.grid3 > .card { margin-bottom: 0; }
.mt { margin-top: 16px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: 14px 16px 15px; box-shadow: var(--shadow-1); }
.kpi .l { color: var(--text-2); font-size: 12px; font-weight: 500; letter-spacing: .02em; }
.kpi .v { font: 650 24px/1.2 var(--font-display); letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 4px; overflow-wrap: anywhere; }
.kpis > .msg { grid-column: 1 / -1; }
.kpi .s { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; margin-top: 3px; text-wrap: pretty; }
.pos { color: var(--good); } .neg { color: var(--bad); }
.tablewrap { overflow-x: auto; scrollbar-width: thin; scrollbar-color: var(--border-2) transparent; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-2); font-weight: 500; font-size: 12px; letter-spacing: .02em; text-align: right; border-bottom-color: var(--border-2); }
td { text-align: right; }
tbody tr:not(.editing):not(.total):hover td { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
th:first-child, td:first-child, td.l, th.l { text-align: left; }
tr.total td { font-weight: 600; border-top: 1px solid var(--border-2); background: var(--surface-2); }
tr.editing td { background: var(--accent-soft); }
.warnbox { position: relative; border-radius: var(--r-md); padding: 12px 16px 12px 18px; margin-bottom: 16px; overflow: hidden;
  --tone: var(--bad); background: var(--surface); border: 1px solid var(--tone); }
@supports (color: color-mix(in srgb, red, red)) {
  .warnbox { background: color-mix(in srgb, var(--tone) 7%, var(--surface)); border-color: color-mix(in srgb, var(--tone) 28%, var(--border)); }
}
.warnbox::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--tone); }
.warnbox b { color: var(--tone); font-weight: 600; }
.warnbox div { margin: 2px 0; }
.warnbox.okbox { --tone: var(--good); }
.warnbox.oppbox { --tone: var(--s7); }
tr.editing input, tr.editing select { padding: 3px 6px; }
.chart { position: relative; height: 340px; }
.chart.sm { height: 260px; }
.chart.zoomable canvas { cursor: crosshair; }
/* ---- 圖表卡片工具列 ---- */
.card h2 { display: flex; align-items: flex-start; gap: 8px; }
.card h2 .ttl { flex: 1 1 0; min-width: 0; line-height: 26px; }
.card h2 .ttl small { display: block; margin: -2px 0 0; line-height: 1.4; }
.grid3 > .card { display: flex; flex-direction: column; }
.grid3 > .card > .chart { margin-top: auto; }
.ctools { display: flex; flex: 0 0 auto; align-items: center; gap: 4px; margin-left: auto; font-weight: 400; }
.ctools .sep { width: 1px; height: 16px; background: var(--border); margin: 0 4px; }
.chip { font: 500 12px/1 var(--font); padding: 5px 9px; border-radius: 6px; border: 1px solid var(--border);
  background: none; color: var(--text-2); cursor: pointer; transition: background .15s, color .15s, border-color .15s, transform .1s; }
.chip:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-2); }
.chip:active { transform: translateY(1px); }
.chip.on { background: var(--text); border-color: var(--text); color: var(--surface); }
.ibtn { width: 28px; height: 26px; display: inline-grid; place-items: center; border-radius: 6px; border: 1px solid transparent;
  background: none; color: var(--text-2); cursor: pointer; font-size: 14px; line-height: 1; transition: background .15s, color .15s, transform .1s; }
.ibtn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.ibtn:active { transform: translateY(1px); }
.ibtn[hidden] { display: none; }
.chint { color: var(--muted); font-size: 11px; margin-top: 6px; text-wrap: pretty; }
/* 自訂提示框：數值在前、標籤在後，以短線標示序列 */
.ctip { position: absolute; top: 0; left: 0; z-index: var(--z-tip); pointer-events: none; opacity: 0; min-width: 150px; max-width: 280px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--r-md); padding: 8px 10px;
  box-shadow: var(--shadow-2); font-size: 12px;
  transition: opacity .12s, transform .12s ease-out; }
.ctip .tt { color: var(--text-2); font-weight: 500; margin-bottom: 4px; white-space: nowrap; }
.ctip .rw { display: grid; grid-template-columns: 14px auto 1fr; align-items: center; gap: 0 8px; padding: 1px 0; }
.ctip .rw .vv { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ctip .rw .ll { color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ctip .k { width: 14px; height: 3px; border-radius: 2px; }
.ctip .k.box { width: 10px; height: 10px; border-radius: 3px; justify-self: center; }
.ctip .k.none { visibility: hidden; }
.ctip .ft { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 5px; color: var(--text-2); }
.ctip .ft b { color: var(--text); font-variant-numeric: tabular-nums; }
/* 放大檢視 */
.card.max { position: fixed; inset: 24px; z-index: var(--z-max); margin: 0; display: flex; flex-direction: column;
  box-shadow: var(--shadow-3); animation: pop .22s var(--ease); }
.card.max .chart { flex: 1; height: auto; }
#backdrop { position: fixed; inset: 0; z-index: var(--z-backdrop); background: rgb(20 18 14 / .45);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); display: none; }
#backdrop.on { display: block; }
@keyframes pop { from { transform: scale(.98); opacity: .6; } to { transform: none; opacity: 1; } }
form.row, .row { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: flex-end; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 500; color: var(--text-2); }
label.chk { flex-direction: row; align-items: center; gap: 7px; font-size: 13px; font-weight: 400; color: var(--text); cursor: pointer; }
input, select { font: inherit; font-weight: 400; color: var(--text); background: var(--surface); border: 1px solid var(--border-2);
  border-radius: var(--r-sm); padding: 6px 9px; min-width: 0; font-variant-numeric: tabular-nums;
  transition: border-color .15s, box-shadow .15s, background .15s; }
input:hover:not(:disabled), select:hover:not(:disabled) { border-color: var(--muted); }
input:focus, select:focus { outline: 2px solid transparent; outline-offset: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input:disabled, select:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
input[type=checkbox] { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; padding: 0; cursor: pointer; }
input[type=checkbox]:focus { box-shadow: none; outline: none; }
input[type=checkbox]:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
input[type=file] { padding: 4px; }
input[type=file]::file-selector-button { font: inherit; margin-right: 8px; padding: 3px 10px; border-radius: 5px; border: 1px solid var(--border-2);
  background: var(--surface-2); color: var(--text); cursor: pointer; }
input::placeholder { color: var(--muted); opacity: .8; }
input[type=number] { width: 120px; } input.w80 { width: 80px; } input.w100 { width: 100px; }
input[type=date] { width: 150px; }
button.btn { position: relative; font: 500 13.5px/1.4 var(--font); padding: 6px 14px; border-radius: var(--r-sm); border: 1px solid var(--border-2);
  background: var(--surface); color: var(--text); cursor: pointer; box-shadow: var(--shadow-1);
  transition: background .18s var(--ease), border-color .18s var(--ease), color .18s, transform .1s, box-shadow .18s, filter .18s; }
button.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
button.btn:active:not(:disabled) { transform: translateY(1px); box-shadow: none; }
button.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff;
  box-shadow: 0 1px 2px rgb(var(--accent-rgb) / .4), inset 0 1px 0 rgb(255 255 255 / .14); }
button.btn.primary:hover:not(:disabled) { filter: brightness(.9); }
button.btn:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }
/* 執行中：保留文字，底部一道細進度光帶 */
button.btn[aria-busy="true"] { opacity: .8; cursor: progress; overflow: hidden; }
button.btn[aria-busy="true"]::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 2px;
  background: linear-gradient(90deg, transparent, currentColor, transparent) no-repeat; background-size: 50% 100%;
  animation: busy 1s linear infinite; }
@keyframes busy { from { background-position: -100% 0; } to { background-position: 200% 0; } }
button.link { background: none; border: 0; color: var(--accent-ink); cursor: pointer; font: inherit; font-weight: 500; padding: 2px 4px; border-radius: 4px;
  transition: background .15s, color .15s; }
button.link:hover { background: var(--accent-soft); }
button.link.del { color: var(--bad); }
button.link.del:hover { background: rgb(var(--bad-rgb) / .12); }
.muted { color: var(--muted); font-size: 12px; }
p.muted { max-width: 72ch; text-wrap: pretty; line-height: 1.6; }
.msg { margin-top: 8px; font-size: 13px; color: var(--text-2); }
.msg.err { color: var(--bad); }
.assetrows { display: flex; flex-direction: column; gap: 6px; }
.assetrows .ar { display: flex; gap: 8px; align-items: center; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: middle; }
#toast { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%; z-index: var(--z-toast); pointer-events: none;
  background: var(--text); color: var(--surface); padding: 9px 18px; border-radius: var(--r-md); box-shadow: var(--shadow-2);
  font-weight: 500; opacity: 0; visibility: hidden; transform: translate(-50%, 8px);
  transition: opacity .2s var(--ease), transform .2s var(--ease), visibility 0s linear .2s; }
#toast.on { opacity: 1; visibility: visible; transform: translate(-50%, 0); transition-delay: 0s; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (max-width: 640px) {
  .bar { padding: 8px 16px; }
  main { padding: 16px max(16px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  h1 { flex: 1; min-width: 0; } h1 .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } h1 .pre { display: none; }
  .spacer { display: none; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; } .kpi { padding: 12px 13px 13px; }
  nav { order: 3; flex-basis: calc(100% + 32px); flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
  nav::-webkit-scrollbar { display: none; }
  nav button { flex: none; }
  .grid2, .grid3 { grid-template-columns: minmax(0, 1fr); } .kpi .v { font-size: clamp(15px, 4.6vw, 19px); }
  .chart { height: 280px; } .chart.sm { height: 230px; }
  /* 寬表格：縮小間距，第一欄（代號／日期）固定在左側，橫向捲動時仍看得到 */
  th, td { padding: 7px 8px; }
  .tablewrap { margin: 0 -16px; padding: 0 16px; }
  .tablewrap th:first-child, .tablewrap td:first-child { position: sticky; left: -16px; z-index: 1; background: var(--surface);
    box-shadow: 1px 0 0 var(--border); }
  .tablewrap tr.total td:first-child, .tablewrap tbody tr:hover td:first-child { background: var(--surface-2); }
  .tablewrap tr.editing td:first-child { background: var(--surface); background-image: linear-gradient(var(--accent-soft), var(--accent-soft)); }
  /* 第一欄的名稱（如「富邦NASDAQ」）移到代號下一行並截斷，第一欄才不會佔掉半個螢幕 */
  .tablewrap td:first-child .muted { display: block; max-width: 8.5em; overflow: hidden; text-overflow: ellipsis; font-size: 11px; line-height: 1.3; }
  /* 投資組合輸入列（代號／角色／權重／年化調整）：平分寬度，不用固定寬 */
  .assetrows .ar > input, .assetrows .ar > select { flex: 1 1 0; width: auto; min-width: 0; } .assetrows .ar > select { flex-grow: 1.4; }
  .row > * { max-width: 100%; min-width: 0; }
  /* 文字說明型表格：內容欄換行，不橫向捲動 */
  #tSmRule td:last-child { white-space: normal; }
  /* 分頁列右側淡出，提示還有分頁可往右滑 */
  nav { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); }
  .card { padding: 16px 16px 18px; }
  .card h2 { flex-wrap: wrap; } .card h2 .ttl { flex-basis: 100%; } .ctools { flex-wrap: wrap; } .card.max { inset: 8px; } }
/* 觸控裝置：iOS 在輸入框字級小於 16px 時聚焦會放大整頁；按鈕與連結至少約 40px 高以便點按 */
@media (pointer: coarse) {
  input:not([type=checkbox]), select, textarea { font-size: 16px; }
  input:not([type=checkbox]):not([type=file]), select { min-height: 40px; }
  input[type=checkbox] { width: 20px; height: 20px; }
  label.chk { min-height: 40px; }
  button.btn { min-height: 40px; padding: 8px 14px; }
  nav button { padding: 10px 14px; }
  button.link { padding: 9px 8px; margin: -6px 0; }
  .ibtn { width: 40px; height: 40px; font-size: 16px; }
  .chip { padding: 10px 12px; }
  input[type=number] { width: 130px; } input.w80 { width: 90px; } input.w100 { width: 110px; }
  input[type=date] { width: 160px; }
}
/* 程式已結束的遮罩 */
#ended { position: fixed; inset: 0; z-index: var(--z-skip); display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--bg, #000) 70%, transparent); backdrop-filter: blur(3px); }
@supports not (background: color-mix(in srgb, red, blue)) { #ended { background: rgba(0, 0, 0, .55); } }
#ended[hidden] { display: none; }
#ended .card { max-width: 420px; text-align: center; }

/* 離線 PWA（html.pwa）沒有伺服器：隱藏結束（抓價經 /yahoo/ 代理，兩邊都有）；匯入／匯出資料庫只在 PWA 顯示 */
.pwa .server-only { display: none !important; }
:root:not(.pwa) .pwa-only { display: none !important; }
