/* ============ 色彩：淺色 / 深色兩組 token ============ */
:root {
  --bg: #EEF1F4;
  --surface: #FFFFFF;
  --ink: #18202B;
  --muted: #5B6675;
  --rule: #D5DBE3;
  --accent: #2B3F6B;
  --accent-ink: #FFFFFF;
  --up: #D92B2B;
  --down: #1A9E3F;
  --up-bar: rgba(217, 43, 43, .14);
  --down-bar: rgba(26, 158, 63, .14);
  --hl: #FFF3C4;
  --focus: #3D6BE0;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10151C; --surface: #171E27; --ink: #E4E8EE; --muted: #8E99A8; --rule: #27313D;
    --accent: #8FA8DB; --accent-ink: #10151C; --up: #FF5C5C; --down: #33C46A;
    --up-bar: rgba(255, 92, 92, .17); --down-bar: rgba(51, 196, 106, .16); --hl: #3A3420; --focus: #8FB0FF;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #10151C; --surface: #171E27; --ink: #E4E8EE; --muted: #8E99A8; --rule: #27313D;
  --accent: #8FA8DB; --accent-ink: #10151C; --up: #FF5C5C; --down: #33C46A;
  --up-bar: rgba(255, 92, 92, .17); --down-bar: rgba(51, 196, 106, .16); --hl: #3A3420; --focus: #8FB0FF;
  color-scheme: dark;
}

/* ============ 基礎 ============ */
:root {
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }
html { scroll-padding-top: env(safe-area-inset-top, 0px); -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 1240px;
  padding: 0 clamp(12px, 3vw, 32px) 32px;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
  transition: background-color .2s, color .2s;
}
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ============ 頁首 ============ */
.top {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 16px;
  padding: 24px 0 18px;
}
.brand h1 { margin: 0; font-size: clamp(22px, 3vw, 30px); font-weight: 900; letter-spacing: .02em; line-height: 1.2; }
.brand .sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.day { display: flex; align-items: center; gap: 4px; }
.day-pick select {
  appearance: none; border: 1px solid var(--rule); background: var(--surface);
  border-radius: 8px; padding: 7px 14px; font-weight: 700; font-size: 16px; cursor: pointer; text-align: center;
}
.day { flex-wrap: wrap; justify-content: center; gap: 8px; }
.day-single, .day-range { display: flex; align-items: center; gap: 4px; }
.day-range[hidden], .day-single[hidden], .presets[hidden] { display: none; }
.day-range .day-pick select { font-size: 15px; padding: 7px 10px; }
.range-sep { color: var(--muted); padding: 0 2px; }
.mode-seg button { padding: 5px 12px; }

.presets { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: -4px 0 12px; font-size: 13px; color: var(--muted); }
.presets span { margin-right: 4px; }
.presets button {
  border: 1px solid var(--rule); background: var(--surface); border-radius: 14px; padding: 3px 12px; cursor: pointer; font-size: 13px; color: var(--ink);
}
.presets button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.presets button:disabled { opacity: .35; cursor: default; }

.days-badge { display: inline-block; margin-left: 6px; font-size: 11px; padding: 0 6px; border-radius: 9px; vertical-align: 1px; color: var(--muted); border: 1px solid var(--rule); }
.days-badge.full { color: var(--ink); border-color: var(--ink); font-weight: 700; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; border: 1px solid transparent;
  background: transparent; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted);
}
.icon-btn:hover:not(:disabled) { border-color: var(--rule); color: var(--ink); }
.icon-btn:disabled { opacity: .3; cursor: default; }

.theme-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border-radius: 18px; border: 1px solid var(--rule); background: var(--surface); cursor: pointer; font-size: 13px;
}
.theme-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.theme-btn .i-moon { fill: currentColor; stroke: none; }
:root[data-theme="dark"] .i-moon, :root:not([data-theme="dark"]) .i-sun { display: none; }

/* ============ 篩選列 ============ */
.controls { border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); margin-bottom: 14px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding-top: 6px; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: none; border: 0; background: none; padding: 10px 14px 12px; cursor: pointer;
  font-size: 16px; font-weight: 500; color: var(--muted); border-bottom: 3px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--ink); font-weight: 700; border-bottom-color: var(--accent); }
.tabs button:disabled { opacity: .35; cursor: default; }

.filters { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; padding: 12px 0; }
.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--rule); border-radius: 9px; padding: 3px; }
.seg button {
  border: 0; background: none; padding: 5px 14px; border-radius: 6px; cursor: pointer; font-size: 14px; color: var(--muted);
}
.seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.seg button:disabled { opacity: .35; cursor: default; }
.field { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.field select, .field input {
  border: 1px solid var(--rule); background: var(--surface); border-radius: 8px; padding: 6px 10px; font-size: 14px; color: var(--ink);
}
.search { margin-left: auto; }
.search input { width: 200px; }

.notice {
  border-left: 4px solid var(--accent); background: var(--surface); padding: 10px 14px; border-radius: 0 8px 8px 0;
  margin-bottom: 12px; font-size: 14px;
}
.notice.err { border-left-color: var(--up); }
.tally { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.tally b.up { color: var(--up); }
.tally b.down { color: var(--down); }

/* ============ 對拉排行板 ============ */
.board {
  display: grid;
  grid-template-columns: 1fr 2px 1fr;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 14px;
  overflow: hidden;
}
.spine { background: var(--ink); opacity: .85; }
.col h2 {
  margin: 0; padding: 14px 18px 10px; font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 8px;
  border-bottom: 1px solid var(--rule);
}
.col-buy h2 { justify-content: flex-end; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.up { background: var(--up); }
.dot.down { background: var(--down); }

.rows { list-style: none; margin: 0; padding: 0; }
.row {
  position: relative;
  display: grid;
  grid-template-columns: 2.4em 4.4em minmax(0, 1fr) 6.2em 5em 4.4em;
  align-items: center;
  gap: 10px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--rule);
  isolation: isolate;
}
.row:last-child { border-bottom: 0; }
/* 量能條：從中央脊線往外長 */
.row::before {
  content: ""; position: absolute; top: 4px; bottom: 4px; width: var(--w, 0%); z-index: -1; border-radius: 3px;
}
.col-buy .row::before { right: 0; background: var(--up-bar); border-radius: 3px 0 0 3px; }
.col-sell .row::before { left: 0; background: var(--down-bar); border-radius: 0 3px 3px 0; }
.row.hit { background: var(--hl); }

.rk { color: var(--muted); font-size: 13px; text-align: right; }
.rk.rk-top { color: var(--ink); font-weight: 900; font-size: 15px; }
.nm { min-width: 0; }
.nm a { color: inherit; text-decoration: none; font-weight: 500; }
.nm a:hover { text-decoration: underline; }
.row > .cd { color: var(--muted); font-size: 13px; }
.nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.streak {
  display: inline-block; margin-left: 6px; font-size: 11px; padding: 0 6px; border-radius: 9px; vertical-align: 1px;
  border: 1px solid currentColor;
}
.col-buy .streak { color: var(--up); }
.col-sell .streak { color: var(--down); }
.px { text-align: right; font-size: 14px; }
.row > .chg { text-align: right; font-size: 13px; }
/* 表頭 */
.row.thead { padding-top: 6px; padding-bottom: 6px; color: var(--muted); background: var(--bg); }
.row.thead::before { display: none; }
.row.thead > span { color: var(--muted); font-size: 12px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: clip; }
.row.thead .nm { overflow: visible; }
.net { text-align: right; font-weight: 700; font-size: 16px; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.empty { padding: 36px 18px; color: var(--muted); text-align: center; }

.side-switch { display: none; margin-bottom: 12px; }

/* ============ 綜合分析 ============ */
.combo[hidden] { display: none; }
.combo-bar { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--rule); background: var(--surface); border-radius: 14px; padding: 4px 12px; cursor: pointer; font-size: 13px;
}
.chips button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.combo-opts { display: flex; align-items: center; gap: 12px; }
.toggle { border: 1px solid var(--rule); background: var(--surface); border-radius: 8px; padding: 5px 12px; cursor: pointer; font-size: 13px; }
.toggle[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); font-weight: 700; }
.toggle[aria-pressed="true"]::before { content: "✓ "; }
.chips button .n { margin-left: 4px; font-size: 11px; opacity: .75; }
.combo-wrap { background: var(--surface); border: 1px solid var(--rule); border-radius: 14px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.combo-table { width: 100%; border-collapse: collapse; font-size: 14px; white-space: nowrap; }
.combo-table th {
  position: sticky; top: 0; z-index: 1; background: var(--bg); color: var(--muted); font-size: 12px; font-weight: 500;
  padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--rule);
}
.combo-table td { padding: 8px 10px; border-bottom: 1px solid var(--rule); }
.combo-table tbody tr:last-child td { border-bottom: 0; }
.combo-table tbody tr.hit td { background: var(--hl); }
.combo-table .c-num { text-align: right; }
.combo-table .c-rk { text-align: right; color: var(--muted); width: 3em; }
.combo-table .c-cd { color: var(--muted); font-size: 13px; }
.combo-table td.c-nm a { color: inherit; text-decoration: none; font-weight: 500; }
.combo-table td.c-nm a:hover { text-decoration: underline; }
.combo-table td.c-inst { font-weight: 700; }
.combo-table td.na { color: var(--muted); opacity: .6; }
.combo-table td.c-num.up { color: var(--up); }
.combo-table td.c-num.down { color: var(--down); }
.sig { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 9px; margin-right: 4px; border: 1px solid currentColor; }
.sig.up { color: var(--up); }
.sig.down { color: var(--down); }
.sig.mid { color: var(--muted); }
.score { display: inline-block; min-width: 2.2em; text-align: center; font-weight: 900; margin-right: 6px; border-radius: 6px; padding: 0 4px; }
.score.up { background: var(--up-bar); color: var(--up); }
.score.down { background: var(--down-bar); color: var(--down); }
.score.flat { color: var(--muted); }

.foot { color: var(--muted); font-size: 12px; margin-top: 22px; line-height: 1.7; }
.foot a { color: var(--accent); }

/* ============ 平板 ============ */
@media (max-width: 980px) {
  .row { grid-template-columns: 2em 3.9em minmax(0, 1fr) 5.4em 4.4em 3.8em; gap: 6px; padding: 8px 10px; }
  .net { font-size: 15px; }
  .px, .row > .chg { font-size: 12px; }
  .streak, .days-badge { display: none; }
  .search { margin-left: 0; flex: 1 1 180px; }
  .search input { width: 100%; }
}

/* ============ 手機：單欄 + 買/賣切換 ============ */
@media (max-width: 640px) {
  .top { grid-template-columns: 1fr auto; gap: 10px; padding-top: 16px; }
  .day { grid-column: 1 / -1; grid-row: 2; justify-content: center; }
  .theme-btn span { display: none; }
  .theme-btn { width: 36px; padding: 0; justify-content: center; }
  .filters { gap: 10px; }
  .side-switch { display: flex; }
  .side-switch button { flex: 1; padding: 8px; font-size: 15px; }
  .side-switch button[data-side="sell"][aria-checked="true"] { background: var(--down); color: #fff; }
  .side-switch button[data-side="buy"][aria-checked="true"] { background: var(--up); color: #fff; }
  .board { grid-template-columns: 1fr; }
  .spine { display: none; }
  .board[data-side="buy"] .col-sell, .board[data-side="sell"] .col-buy { display: none; }
  .col h2 { display: none; }
  .col-buy h2 { justify-content: flex-start; }
  .row { grid-template-columns: 1.5em 3.3em minmax(0, 1fr) 4.9em 4.1em 3.3em; gap: 4px; padding: 10px 8px; }
  .row > .cd { font-size: 12px; }
  .net { font-size: 14px; }
  .row.thead > span { font-size: 11px; letter-spacing: -.02em; }
  .col-buy .row::before { right: auto; left: 0; border-radius: 0 3px 3px 0; }
}

@media (max-width: 640px) {
  .combo-table { font-size: 13px; }
  .combo-table th, .combo-table td { padding: 8px 7px; }
  .combo-table .c-rk { display: none; }
  .combo-table .c-cd, .combo-table .c-nm { position: sticky; background: var(--surface); z-index: 2; }
  .combo-table th.c-cd, .combo-table th.c-nm { background: var(--bg); z-index: 3; }
  .combo-table .c-cd { left: 0; min-width: 4.6em; }
  .combo-table .c-nm { left: 4.6em; box-shadow: 1px 0 0 var(--rule); }
  .combo-bar { align-items: stretch; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .chips button { flex: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

.board[hidden] { display: none; }
