:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2128;
  --muted: #5d6673;
  --border: #d9dde3;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --over: #b45309;
  --over-bg: #fef3c7;
  --under: #1d4ed8;
  --under-bg: #dbeafe;
  --ok: #3f6212;
  --ok-bg: #ecfccb;
  --danger: #b42318;
  --danger-bg: #fee4e2;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f252d;
    --text: #e6e9ee;
    --muted: #9aa4b2;
    --border: #2d343e;
    --accent: #4c8dff;
    --accent-text: #0b1220;
    --over: #fbbf24;
    --over-bg: #3a2a0a;
    --under: #7aa7ff;
    --under-bg: #13254a;
    --ok: #a3d65c;
    --ok-bg: #1f2e0d;
    --danger: #ff8a80;
    --danger-bg: #3d1512;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.05rem; }
.nav { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.nav a, .nav .link { padding: 6px 10px; border-radius: 8px; color: var(--muted); text-decoration: none; }
.nav a.active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.badge { display: inline-block; min-width: 1.4em; margin-left: 4px; padding: 0 .4em; border-radius: 999px; background: var(--danger); color: #fff; font-size: .75rem; text-align: center; }

.main { width: 100%; max-width: 1180px; margin: 0 auto; padding: 20px 16px 40px; flex: 1; }
.footer { padding: 16px; color: var(--muted); font-size: .8rem; text-align: center; border-top: 1px solid var(--border); }
.footer p { margin: .25em auto; max-width: 900px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.narrow { max-width: 420px; margin: 24px auto; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.spread { justify-content: space-between; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.35rem; font-weight: 650; font-variant-numeric: tabular-nums; }

form .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
form .fields { display: grid; gap: 0 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
label { font-size: .9rem; font-weight: 600; }
.hint { font-size: .8rem; color: var(--muted); font-weight: 400; }
input, select {
  font: inherit; color: var(--text); background: var(--surface); width: 100%;
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-height: 40px;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"] { width: auto; min-height: 0; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }

button, .button {
  font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px; min-height: 40px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; padding: 6px 10px; min-height: 0; color: var(--muted); }
button.small { padding: 4px 10px; min-height: 32px; font-size: .85rem; }
button:disabled { opacity: .6; cursor: progress; }

.alert { border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; border: 1px solid transparent; }
.alert.error { background: var(--danger-bg); color: var(--danger); }
.alert.info { background: var(--under-bg); color: var(--under); }
.alert.warn { background: var(--over-bg); color: var(--over); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }

.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.chip.over { background: var(--over-bg); color: var(--over); }
.chip.under { background: var(--under-bg); color: var(--under); }
.chip.ok { background: var(--ok-bg); color: var(--ok); }
.chip.neutral { background: var(--surface-2); color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; text-align: left; white-space: nowrap; }
th.num { text-align: right; }
tr.cash td { background: var(--surface-2); }
.code { font-weight: 650; }
.dev { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.devbar { position: relative; width: 96px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.devbar .mid { position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: var(--muted); }
.devbar .fill { position: absolute; top: 0; bottom: 0; border-radius: 3px; background: var(--muted); }
.devbar .fill.over { background: var(--over); }
.devbar .fill.under { background: var(--under); }
.devbar .fill.within { background: var(--ok); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 12px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.notif.unread .title { font-weight: 700; }
.notif .body { white-space: pre-line; margin: 4px 0; }
.portfolio-link { display: flex; justify-content: space-between; gap: 12px; align-items: center; color: var(--text); text-decoration: none; }
.portfolio-link:hover .title { color: var(--accent); }

.dialog { border: 1px solid var(--border); border-radius: var(--radius); padding: 0; width: min(560px, calc(100vw - 32px)); background: var(--surface); color: var(--text); }
.dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog .inner { padding: 18px; max-height: 85vh; overflow-y: auto; }
.dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

.toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 20; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }

/* 手機：持股表改為卡片 */
@media (max-width: 760px) {
  h1 { font-size: 1.3rem; }
  table.holdings thead { display: none; }
  table.holdings, table.holdings tbody, table.holdings tr, table.holdings td { display: block; width: 100%; }
  table.holdings tr { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; padding: 6px 10px; }
  table.holdings td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  table.holdings td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; }
  table.holdings td.name-cell { font-size: 1rem; }
  table.holdings td.name-cell::before { content: none; }
  .dev { align-items: flex-end; }
}
.alert-text { color: var(--danger); }
.row > h1, .row > h2 { margin: 0; }
.main > .row.spread { margin-bottom: 14px; }
.card > .row.spread { margin-bottom: 8px; }
@media (max-width: 760px) {
  table.holdings td > .v { text-align: right; }
  table.holdings td.name-cell > .v { text-align: left; }
  .grid { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
  .grid > .card { margin-bottom: 0; padding: 12px; }
  .grid > :last-child:nth-child(odd) { grid-column: 1 / -1; }
  .stat .value { font-size: 1.15rem; }
}
