:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-border: #30363d;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --buy: #2ea043;
  --sell: #f85149;
  --accent: #58a6ff;
  --hot: #d29922;
  --row-alt: #1b2129;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", "Microsoft JhengHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--panel-border);
  background: var(--panel);
  flex-wrap: wrap;
  gap: 8px;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
}

.status {
  display: flex;
  align-items: center;
  gap: 12px;
}

.market-state,
.conn-state {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  letter-spacing: 0.5px;
}

.market-state--open { background: rgba(46, 160, 67, 0.2); color: var(--buy); }
.market-state--closed { background: rgba(139, 148, 158, 0.2); color: var(--text-dim); }
.market-state--unknown { background: rgba(139, 148, 158, 0.15); color: var(--text-dim); }
.market-state--afterhours { background: rgba(88, 166, 255, 0.2); color: var(--accent); }
.market-state--pre { background: rgba(210, 153, 34, 0.2); color: var(--hot); }

.clock {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.3px;
}

.feed-status {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}
.feed-status--ok { background: rgba(46, 160, 67, 0.18); color: var(--buy); }
.feed-status--warn { background: rgba(210, 153, 34, 0.2); color: var(--hot); }

.conn-state--up { background: rgba(46, 160, 67, 0.2); color: var(--buy); }
.conn-state--down { background: rgba(248, 81, 73, 0.2); color: var(--sell); }

.last-update {
  font-size: 12px;
  color: var(--text-dim);
}

.layout {
  display: grid;
  grid-template-columns: 2fr 1fr 1.2fr;
  gap: 16px;
  padding: 16px 20px;
  align-items: start;
}

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 14px 16px;
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 15px;
  color: var(--accent);
  border-bottom: 1px solid var(--panel-border);
  padding-bottom: 8px;
}

.table-wrap {
  overflow-x: auto;
}

.ranking-table,
.rotation-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.ranking-table th,
.ranking-table td,
.rotation-table th,
.rotation-table td {
  padding: 7px 8px;
  text-align: right;
  white-space: nowrap;
}

.ranking-table th,
.rotation-table th {
  color: var(--text-dim);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
}

.ranking-table th.col-rank,
.ranking-table th.col-symbol,
.ranking-table td.col-rank,
.ranking-table td.col-symbol,
.rotation-table th.col-rank,
.rotation-table th.col-symbol,
.rotation-table td.col-rank,
.rotation-table td.col-symbol {
  text-align: left;
}

.ranking-table tbody tr:nth-child(even),
.rotation-table tbody tr:nth-child(even) {
  background: var(--row-alt);
}

/* The 籌碼輪動 rotation panel sits alongside the ranking as the second "which stocks" table.
   Net buyers (買超) and net sellers (賣超) are both listed, tinted green / red respectively. */
.panel--rotation .col-netbuy,
.panel--themes .col-netbuy { font-weight: 600; }
.rotation-row--buy { border-left: 3px solid var(--buy); }
.rotation-row--sell { border-left: 3px solid var(--sell); }
.rotation-table td.col-rank,
.themes-table td.col-rank { border-left: inherit; }

/* Theme table shares the ranking/rotation table styling. */
.themes-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.themes-table th, .themes-table td { padding: 7px 8px; text-align: right; white-space: nowrap; }
.themes-table th { color: var(--text-dim); font-weight: 600; font-size: 12px; text-transform: uppercase; }
.themes-table th.col-rank, .themes-table th.col-theme,
.themes-table td.col-rank, .themes-table td.col-theme { text-align: left; }
.themes-table td.col-theme { font-weight: 600; }
.themes-table tbody tr:nth-child(even) { background: var(--row-alt); }
.themes-table .col-count { color: var(--text-dim); font-size: 12px; }

/* Dimmed stock name shown after the code, e.g. "2330 台積電". */
.sym-name { color: var(--text-dim); font-weight: 400; margin-left: 4px; font-size: 12px; }

/* Red/green buy-ratio bar in the ranking's buy-side column. The track is red (sell pressure),
   the fill is green (buy pressure) sized to the buy %. */
.buybar { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }
.buybar__track {
  position: relative;
  width: 56px;
  height: 8px;
  border-radius: 999px;
  background: var(--sell);
  overflow: hidden;
  flex: none;
}
.buybar__fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--buy); }
.buybar__pct { font-size: 12px; min-width: 40px; text-align: right; }
.buybar__pct--buy { color: var(--buy); }
.buybar__pct--sell { color: var(--sell); }

.col-symbol { font-weight: 600; }

.score-cell {
  font-weight: 700;
}

.score-cell--alert { color: var(--sell); }
.score-cell--reco { color: var(--hot); }

.num-pos { color: var(--buy); }
.num-neg { color: var(--sell); }

.empty-row,
.empty-row td {
  color: var(--text-dim);
  text-align: center;
  font-style: italic;
}

/* Alert feed */
.alert-feed,
.reco-board {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 70vh;
  overflow-y: auto;
}

.alert-item {
  border-left: 3px solid var(--sell);
  background: rgba(248, 81, 73, 0.08);
  border-radius: 4px;
  padding: 8px 10px;
}

.alert-item__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.alert-item__symbol { font-weight: 700; font-size: 15px; }
.alert-item__score { color: var(--sell); font-weight: 700; }
.alert-item__time { color: var(--text-dim); font-size: 11px; }

.alert-item__detail {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

/* Recommendation leaderboard */
.reco-item {
  border-left: 3px solid var(--hot);
  background: rgba(210, 153, 34, 0.08);
  border-radius: 4px;
  padding: 8px 10px;
}

.reco-item__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.reco-item__symbol { font-weight: 700; font-size: 15px; }
.reco-item__score { color: var(--hot); font-weight: 700; }

.reco-item__reasons {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.reason-tag {
  font-size: 11px;
  background: rgba(88, 166, 255, 0.15);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 999px;
}

/* AI 可買清單 */
.panel-note {
  font-size: 11px;
  font-weight: 400;
  color: var(--text-dim);
  margin-left: 8px;
}

.ai-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 70vh;
  overflow-y: auto;
}

.ai-item {
  border-left: 3px solid var(--text-dim);
  background: rgba(139, 148, 158, 0.08);
  border-radius: 4px;
  padding: 8px 10px;
}
.ai-item--buy { border-left-color: var(--buy); background: rgba(46, 160, 67, 0.1); }
.ai-item--avoid { border-left-color: var(--sell); background: rgba(248, 81, 73, 0.08); }
.ai-item--hold { border-left-color: var(--hot); background: rgba(210, 153, 34, 0.08); }

.ai-item__head {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.ai-item__symbol { font-weight: 700; font-size: 15px; }
.ai-item__conf { margin-left: auto; color: var(--text-dim); font-variant-numeric: tabular-nums; }

.ai-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.ai-badge--buy { background: rgba(46, 160, 67, 0.2); color: var(--buy); }
.ai-badge--avoid { background: rgba(248, 81, 73, 0.2); color: var(--sell); }
.ai-badge--hold { background: rgba(210, 153, 34, 0.2); color: var(--hot); }

.ai-item__reason {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 5px;
  line-height: 1.4;
}
