:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #1b1f2a;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #14213d;
  --accent: #2f6fde;
  --up: #d1293d;      /* 台股慣例：漲紅 */
  --down: #12875a;    /* 跌綠 */
  --warn: #b7791f;
  --chip: #eef2ff;
  --radius: 12px;
  --nav-w: 200px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: var(--bg);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1218; --card: #181c25; --ink: #e8eaf0; --muted: #9aa3b2; --line: #2a303c; --chip: #222a3d; --brand: #0b1220; }
}
* { box-sizing: border-box; }
body { margin: 0; -webkit-text-size-adjust: 100%; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.up { color: var(--up); }
.down { color: var(--down); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.app { display: flex; min-height: 100vh; }
.nav {
  position: fixed; inset: 0 auto 0 0; width: var(--nav-w); background: var(--brand); color: #fff;
  display: flex; flex-direction: column; padding: 16px 10px; gap: 4px; z-index: 10;
}
.nav .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 6px 10px 18px; }
.nav .brand img { width: 28px; height: 28px; }
.nav a { color: #c9d3ea; text-decoration: none; display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; }
.nav a.active, .nav a:hover { background: rgba(255,255,255,.12); color: #fff; }
.nav .ico { width: 20px; text-align: center; }
.main { flex: 1; margin-left: var(--nav-w); min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg); display: flex; align-items: center;
  justify-content: space-between; padding: 14px 24px 10px; gap: 12px; border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; margin: 0; }
.quote-status { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.view { padding: 18px 24px 40px; max-width: 1500px; }
.loading { padding: 60px; text-align: center; color: var(--muted); }

/* 手機：底部導覽列 */
@media (max-width: 820px) {
  .nav {
    inset: auto 0 0 0; width: 100%; height: calc(60px + env(safe-area-inset-bottom)); flex-direction: row;
    padding: 0 0 env(safe-area-inset-bottom); gap: 0; justify-content: space-around;
  }
  .nav .brand { display: none; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 8px 2px; flex: 1; justify-content: center; border-radius: 0; }
  .nav .ico { font-size: 18px; }
  .main { margin-left: 0; padding-bottom: calc(70px + env(safe-area-inset-bottom)); }
  .topbar { padding: calc(10px + env(safe-area-inset-top)) 14px 10px; }
  .topbar h1 { font-size: 18px; }
  .view { padding: 12px 12px 30px; }
  #qstatus { display: none; }
}

/* 元件 */
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: #fff; border-radius: 8px;
  padding: 8px 14px; font-size: 14px; cursor: pointer; font-family: inherit; white-space: nowrap;
}
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { background: transparent; color: var(--up); border-color: var(--up); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--ink); background: var(--card); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; min-width: 0;
}
.toolbar input, .toolbar select { font-size: 14px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--card); color: var(--ink); padding: 7px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.seg button.on { background: var(--accent); color: #fff; }

.grid { display: grid; gap: 12px; }
.kpis { grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); margin-bottom: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.kpi .label { font-size: 13px; color: var(--muted); }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 13px; margin-top: 4px; }
.two { grid-template-columns: 1fr 1fr; align-items: start; }
@media (max-width: 1100px) { .two { grid-template-columns: 1fr; } }
@media (max-width: 820px) { .kpis { grid-template-columns: 1fr 1fr; } .kpi .value { font-size: 18px; } }

table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; position: sticky; top: 0; background: var(--card); }
th.sortable { cursor: pointer; }
tbody tr:hover { background: rgba(47,111,222,.06); }
tbody tr.click { cursor: pointer; }
.table-wrap { overflow: auto; max-height: calc(100vh - 190px); background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.table-wrap.short { max-height: none; }
.sticky-col { position: sticky; left: 0; background: var(--card); z-index: 1; }
th.sticky-col { z-index: 2; }

.badge { display: inline-block; font-size: 11px; padding: 2px 6px; border-radius: 6px; background: var(--chip); color: var(--accent); margin-left: 4px; white-space: nowrap; }
.badge.buy { background: #fde8eb; color: var(--up); }
.badge.add { background: #e3f5ec; color: var(--down); }
.badge.sell { background: #fff4de; color: var(--warn); }

/* 手機卡片列表 */
.cards { display: none; }
.hcard { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
.hcard .row1 { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.hcard .nm { font-weight: 700; font-size: 16px; }
.hcard .px { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hcard .meta { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px 10px; margin-top: 8px; font-size: 13px; }
.hcard .meta div span { display: block; color: var(--muted); font-size: 11px; }
@media (max-width: 820px) {
  .wide-only { display: none !important; }
  .cards { display: block; }
}

.bars .bar { margin: 8px 0; }
.bars .bar .t { display: flex; justify-content: space-between; font-size: 13px; }
.bars .track { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin-top: 4px; }
.bars .fill { height: 100%; background: var(--accent); }
.list-plain { list-style: none; margin: 0; padding: 0; }
.list-plain li { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14px; gap: 8px; }
.list-plain li:last-child { border-bottom: 0; }

/* 對話框 */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(560px, 96vw); max-height: 92vh; background: var(--card); color: var(--ink); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--card); z-index: 1; }
dialog h2 { margin: 0; font-size: 17px; }
dialog .x { border: 0; background: none; font-size: 24px; color: var(--muted); cursor: pointer; }
.modal-body { padding: 14px 18px; overflow: auto; max-height: calc(92vh - 130px); }
dialog footer { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px; border-top: 1px solid var(--line); }
dialog footer .left { margin-right: auto; }
@media (max-width: 820px) {
  dialog { width: 100vw; max-width: 100vw; max-height: 94vh; margin: auto 0 0; border-radius: 16px 16px 0 0; }
  dialog footer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form label.full { grid-column: 1 / -1; }
.form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
.form .hint { font-size: 11px; color: var(--muted); }
@media (max-width: 520px) { .form { grid-template-columns: 1fr; } }
.detail { display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px 18px; font-size: 14px; }
.detail > div { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); gap: 8px; }
.detail > div > span:first-child { color: var(--muted); }
@media (max-width: 520px) { .detail { grid-template-columns: 1fr; } }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; font-size: 14px; max-width: 90vw; }
.toast.err { background: #b42335; }
.chart { width: 100%; height: auto; display: block; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin: 22px 0 10px; }
.section-title h2 { font-size: 16px; margin: 0; }
.note { font-size: 13px; color: var(--muted); line-height: 1.6; }
