/* ---------- tokens ---------- */
:root {
  --up: #e5484d;          /* 台股慣例：紅漲 */
  --down: #2fa36b;        /* 綠跌 */
  --flat: #8b95a1;
  --accent: #3b82f6;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
:root[data-theme="dark"] {
  --bg: #0b0f14;
  --panel: #121820;
  --panel-2: #18202a;
  --line: #243040;
  --line-soft: #1b2430;
  --text: #e6edf3;
  --muted: #8b98a8;
  --head: #1a2431;
  --head-2: #1f2b3a;
  --zebra: #0f151c;
  --hover: #1d2733;
  --chip: #1b2531;
  --up: #f0565b;
  --down: #35b778;
}
:root[data-theme="light"] {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel-2: #f7f9fb;
  --line: #d9e0e8;
  --line-soft: #e9eef3;
  --text: #17202a;
  --muted: #5e6b7a;
  --head: #e3eefb;        /* Goodinfo 風格的淺藍表頭 */
  --head-2: #d6e6f8;
  --zebra: #f6f8fa;
  --hover: #eef4fb;
  --chip: #eef2f6;
  --up: #d9262c;
  --down: #13834a;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  transition: background .2s, color .2s;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.wrap { max-width: 1440px; margin: 0 auto; padding: 0 20px; }

.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--text); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-in { display: flex; align-items: center; gap: 16px; height: 58px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; letter-spacing: .2px; }
.logo { display: inline-flex; align-items: flex-end; gap: 3px; height: 22px; }
.logo i { display: block; width: 5px; border-radius: 1px; }
.logo i:nth-child(1) { height: 14px; background: var(--up); }
.logo i:nth-child(2) { height: 22px; background: var(--down); }
.logo i:nth-child(3) { height: 17px; background: var(--up); }

.mkt-tabs { display: flex; gap: 4px; margin-left: auto; background: var(--chip); padding: 4px; border-radius: 10px; }
.mkt-tabs button {
  border: 0; background: transparent; padding: 6px 16px; border-radius: 7px; font-weight: 600;
  display: flex; align-items: baseline; gap: 6px; color: var(--muted);
}
.mkt-tabs button small { font-weight: 400; font-size: 12px; }
.mkt-tabs button[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .15); }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); display: grid; place-items: center;
}
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: var(--text); stroke-width: 1.8; stroke-linecap: round; }
:root[data-theme="dark"] .i-moon, :root[data-theme="light"] .i-sun { display: none; }

/* ---------- quote ---------- */
.quote {
  display: flex; align-items: center; gap: 28px; flex-wrap: wrap;
  margin: 18px 0 14px;
}
.q-name { color: var(--muted); font-size: 14px; display: flex; gap: 10px; align-items: baseline; }
.q-name #qName { color: var(--text); font-weight: 600; font-size: 16px; }
.q-date { font-size: 13px; }
.q-price { display: flex; align-items: baseline; gap: 12px; }
#qClose { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.5px; }
.q-chg { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.q-stats { display: grid; grid-template-columns: repeat(6, minmax(86px, auto)); gap: 4px 22px; margin: 0; margin-left: auto; }
.q-stats div { display: flex; flex-direction: column; }
.q-stats dt { font-size: 12px; color: var(--muted); }
.q-stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; font-size: 15px; }

/* ---------- cards ---------- */
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  margin-bottom: 16px; overflow: hidden;
}
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; padding: 12px 14px 4px; }
.seg { display: inline-flex; background: var(--chip); padding: 3px; border-radius: 9px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 14px; border-radius: 7px; color: var(--muted); font-weight: 500; white-space: nowrap; }
.seg button.on { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .18); font-weight: 600; }
.seg-sm button { padding: 4px 11px; font-size: 13px; }

.legend {
  padding: 6px 14px 0; font-size: 12.5px; color: var(--muted); min-height: 24px;
  display: flex; flex-wrap: wrap; gap: 2px 14px; font-variant-numeric: tabular-nums;
}
.legend b { color: var(--text); font-weight: 600; }
.legend .ma { font-weight: 600; }
.legend-sub { border-top: 1px solid var(--line-soft); margin-top: 2px; padding-top: 6px; }
.chart-main { height: 470px; position: relative; }
.chart-sub { height: 150px; position: relative; }

/* ---------- filters ---------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px;
  padding: 12px 14px; background: var(--head); border-bottom: 1px solid var(--line);
}
.f-item { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.range { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; }
.range input {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; min-width: 0;
  color-scheme: light dark;
}
.quick { display: inline-flex; gap: 4px; }
.quick button, .btn-ghost {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 11px; font-size: 13px;
}
.quick button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.btn-primary { border: 0; background: var(--accent); color: #fff; border-radius: 8px; padding: 6px 18px; font-weight: 600; }
#csvBtn { margin-left: auto; }

.colseg { display: none; gap: 6px; padding: 10px 12px 0; }
.colseg button { border: 1px solid var(--line); background: var(--panel); padding: 4px 14px; border-radius: 999px; font-size: 13px; color: var(--muted); }
.colseg button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Goodinfo-style table ---------- */
.tbl-wrap { overflow: auto; max-height: 72vh; -webkit-overflow-scrolling: touch; }
.gtbl { border-collapse: separate; border-spacing: 0; width: 100%; font-variant-numeric: tabular-nums; font-size: 14px; }
.gtbl th, .gtbl td { padding: 7px 9px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line-soft); }
.gtbl thead th {
  position: sticky; z-index: 3; background: var(--head); text-align: center; font-weight: 700; line-height: 1.25;
  border-bottom: 1px solid var(--line); color: var(--text);
}
.gtbl thead tr:first-child th { top: 0; }
.gtbl thead tr:nth-child(2) th { top: 31px; background: var(--head-2); }
.gtbl thead tr:first-child th[rowspan] { vertical-align: middle; }
.gtbl th.grp { border-bottom: 1px solid var(--line); }
.gtbl th.inst:first-of-type, .gtbl td.c-fi, .gtbl th.c-fi,
.gtbl td.c-mb, .gtbl td.c-ss { border-left: 1px solid var(--line); }
.gtbl thead tr:first-child th.grp { border-left: 1px solid var(--line); }
.gtbl thead tr:nth-child(2) th:nth-child(1),
.gtbl thead tr:nth-child(2) th:nth-child(5),
.gtbl thead tr:nth-child(2) th:nth-child(7) { border-left: 1px solid var(--line); }
.gtbl tbody tr:nth-child(even) td { background: var(--zebra); }
.gtbl tbody tr td { background: var(--panel); }
.gtbl tbody tr:hover td { background: var(--hover); }
.gtbl tbody tr.sel td { background: color-mix(in srgb, var(--accent) 16%, var(--panel)); }
.gtbl .sticky, .gtbl td.c-date { position: sticky; left: 0; z-index: 2; text-align: left; }
.gtbl thead th.sticky { z-index: 4; text-align: center; }
.gtbl td.c-date { font-weight: 500; border-right: 1px solid var(--line-soft); }
.gtbl td.na { color: var(--muted); }

.tbl-foot { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; color: var(--muted); font-size: 13px; border-top: 1px solid var(--line-soft); }
.note { color: var(--muted); font-size: 12.5px; line-height: 1.7; margin: 6px 2px 24px; }
.foot { border-top: 1px solid var(--line); color: var(--muted); font-size: 12.5px; padding: 16px 0 28px; }

.loading::after {
  content: "載入中…"; position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted);
}

/* ---------- tablet ---------- */
@media (max-width: 1100px) {
  .q-stats { grid-template-columns: repeat(3, minmax(90px, auto)); margin-left: 0; }
  .colseg { display: flex; }
  .gtbl:not(.g-all) .px, .gtbl:not(.g-all) .inst, .gtbl:not(.g-all) .mg { display: none; }
  .gtbl.g-px .px, .gtbl.g-inst .inst, .gtbl.g-mg .mg { display: table-cell; }
  .chart-main { height: 400px; }
}

/* ---------- phone ---------- */
@media (max-width: 640px) {
  .wrap { padding: 0 12px; }
  .topbar-in { gap: 10px; height: 54px; }
  .brand span:last-child { display: none; }
  .mkt-tabs button { padding: 6px 12px; }
  .mkt-tabs button small { display: none; }
  .quote { gap: 10px; margin: 12px 0 10px; }
  #qClose { font-size: 28px; }
  .q-stats { grid-template-columns: repeat(3, 1fr); width: 100%; gap: 6px 10px; }
  .q-stats dd { font-size: 14px; }
  .toolbar { padding: 10px 10px 2px; }
  .seg { width: 100%; }
  .seg button { flex: 1; padding: 6px 4px; }
  .legend { padding: 6px 10px 0; font-size: 11.5px; gap: 0 10px; }
  .chart-main { height: 300px; }
  .chart-sub { height: 110px; }
  .filters { padding: 10px; }
  .f-item { width: 100%; justify-content: space-between; }
  .range input { width: 128px; font-size: 13px; padding: 4px 6px; }
  .quick { flex: 1 1 100%; }
  .quick button { flex: 1; white-space: nowrap; padding: 5px 4px; }
  .btn-primary { flex: 1; }
  #csvBtn { margin-left: 0; flex: 1; }
  .gtbl { font-size: 13px; }
  .gtbl th, .gtbl td { padding: 7px 7px; }
  .gtbl thead tr:nth-child(2) th { top: 30px; }
  .tbl-wrap { max-height: 70vh; }
}
