/* AI 投資研究戰情室 — dark financial-terminal theme (dark by design; no external fonts or assets). */
:root {
  color-scheme: dark;
  --bg: #0a0e13;
  --panel: #111821;
  --panel-2: #16202b;
  --line: #223042;
  --line-soft: #1a2533;
  --text: #e7edf4;
  --text-2: #a8b4c3;
  --muted: #7b8898;
  --accent: #4cc3ff;
  --accent-soft: rgba(76, 195, 255, .12);
  /* polarity (validated: dark surface #121821, CVD ΔE 19.2, normal ΔE 29.0, both ≥3:1) */
  --in: #e66767;          /* 流入／利多：台股慣例紅色 */
  --in-wash: rgba(230, 103, 103, .14);
  --out: #3987e5;         /* 流出／利空 */
  --out-wash: rgba(57, 135, 229, .16);
  --neutral: #8a96a6;
  /* status (always with icon + label) */
  --good: #2fbf71;
  --warn: #fab219;
  --serious: #ec835a;
  --critical: #e5484d;
  --radius: 10px;
  --gap: 16px;
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Heiti TC", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, "Roboto Mono", monospace;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 112px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font-family: var(--font);
  font-size: 15px; line-height: 1.6; overflow-x: hidden;
}
a { color: var(--accent); text-decoration: none; }
a:hover, a:focus-visible { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: .01em; }
.num, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: rgba(10, 14, 19, .94); border-bottom: 1px solid var(--line); backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px; flex: none;
  background: linear-gradient(135deg, var(--accent) 0 45%, transparent 45% 55%, var(--in) 55% 100%);
  box-shadow: 0 0 0 1px var(--line);
}
.brand h1 { font-size: 18px; line-height: 1.2; }
.brand-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; flex: none; }
.clock { font-family: var(--mono); font-size: 14px; color: var(--text-2); }
.btn-refresh {
  font: inherit; font-size: 13px; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 12px; cursor: pointer;
}
.btn-refresh:hover { border-color: var(--accent); }
.btn-refresh[aria-busy="true"] { opacity: .6; }

.tabs {
  position: sticky; top: 57px; z-index: 19; display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  padding: 6px 16px; background: rgba(10, 14, 19, .94); border-bottom: 1px solid var(--line-soft);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none; padding: 5px 12px; border-radius: 999px; color: var(--text-2); font-size: 14px; white-space: nowrap;
}
.tabs a:hover { background: var(--panel-2); color: var(--text); text-decoration: none; }

.banner {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--warn);
  background: rgba(250, 178, 25, .08); color: var(--text); font-size: 14px;
}
.banner.critical { border-color: var(--critical); background: rgba(229, 72, 77, .1); }

/* ---------- layout ---------- */
main { max-width: 1440px; margin: 0 auto; padding: 8px 16px 32px; }
.section { padding-top: 22px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.section-head h2 { font-size: 20px; }
.section-head h2::before { content: ""; display: inline-block; width: 4px; height: 16px; margin-right: 10px; border-radius: 2px; background: var(--accent); vertical-align: -1px; }
.section-meta { font-size: 13px; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--gap); margin-top: var(--gap); }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0;
  margin-top: var(--gap);
}
.grid-2 > .panel { margin-top: 0; }
.panel h3 { font-size: 16px; margin-bottom: 6px; }
.panel h4 { font-size: 14px; margin: 14px 0 6px; color: var(--text-2); font-weight: 600; }
.panel-note { margin: 0 0 10px; font-size: 12.5px; color: var(--muted); }
.loading, .empty { color: var(--muted); font-size: 14px; padding: 8px 0; }
.notice {
  margin-top: 4px; padding: 10px 14px; border-left: 3px solid var(--warn); background: var(--panel); border-radius: 6px;
  font-size: 14px; color: var(--text-2);
}
.notice.ok { border-left-color: var(--good); }
.notice strong { color: var(--text); }
.notice p { margin: 2px 0; }

/* ---------- status chips & tiles ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 9px; border-radius: 999px; font-size: 12.5px;
  border: 1px solid var(--line); color: var(--text-2); background: var(--panel-2); white-space: nowrap; line-height: 1.7;
}
.chip::before { content: attr(data-icon); font-size: 11px; }
.chip.good { color: var(--good); border-color: rgba(47, 191, 113, .45); }
.chip.warn { color: var(--warn); border-color: rgba(250, 178, 25, .45); }
.chip.serious { color: var(--serious); border-color: rgba(236, 131, 90, .5); }
.chip.critical { color: var(--critical); border-color: rgba(229, 72, 77, .5); }
.chip.off { color: var(--muted); }
.chip.in { color: var(--in); border-color: rgba(230, 103, 103, .45); }
.chip.out { color: var(--out); border-color: rgba(57, 135, 229, .5); }
.chip.plain::before { content: none; }

.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--gap); }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.tile-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tile-label { font-size: 13px; color: var(--text-2); }
.tile-value { margin-top: 8px; font-size: 22px; font-weight: 600; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.tile-sub { margin-top: 2px; font-size: 12.5px; color: var(--muted); }
.tile.warn { border-color: rgba(250, 178, 25, .35); }
.tile.critical { border-color: rgba(229, 72, 77, .45); }

/* ---------- summary & signals ---------- */
.summary, .signals { list-style: none; margin: 0; padding: 0; }
.summary li, .signals li { padding: 10px 0; border-top: 1px solid var(--line-soft); }
.summary li:first-child, .signals li:first-child { border-top: 0; padding-top: 2px; }
.summary .tag, .signals .tag {
  display: inline-block; margin-right: 8px; padding: 0 7px; border-radius: 4px; font-size: 12px;
  background: var(--accent-soft); color: var(--accent); vertical-align: 1px;
}
.summary .when, .signals .when { font-size: 12px; color: var(--muted); margin-left: 6px; white-space: nowrap; }
.signals .sig-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.signals .sig-text { margin-top: 3px; font-size: 14px; color: var(--text-2); }
.pos { color: var(--in); }
.neg { color: var(--out); }

/* ---------- controls ---------- */
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 10px 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button {
  font: inherit; font-size: 13px; background: transparent; color: var(--text-2); border: 0; padding: 5px 11px; cursor: pointer;
  border-left: 1px solid var(--line);
}
.seg button:first-child { border-left: 0; }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
select {
  font: inherit; font-size: 13px; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 8px; max-width: 100%;
}
.ctl-label { font-size: 13px; color: var(--muted); }

/* ---------- news ---------- */
.news-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 12px; }
.news-item {
  display: grid; grid-template-columns: 44px minmax(0, 1fr); gap: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
}
.rank { font-family: var(--mono); font-size: 24px; font-weight: 700; color: var(--accent); line-height: 1.1; text-align: center; }
.rank small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); margin-top: 4px; }
.news-title { font-size: 16.5px; font-weight: 600; line-height: 1.45; }
.news-badges { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.news-summary { margin: 4px 0 0; color: var(--text-2); font-size: 14.5px; }
.impact { margin-top: 10px; padding: 10px 12px; background: var(--panel-2); border-radius: 8px; border: 1px solid var(--line-soft); }
.impact-title { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.impact p { margin: 2px 0; font-size: 14px; }
.channels { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.news-meta { margin-top: 8px; font-size: 12.5px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 14px; }
.sources { margin: 6px 0 0; padding: 0; list-style: none; font-size: 13.5px; }
.sources li { padding: 2px 0; overflow-wrap: anywhere; }
.sources .src { color: var(--muted); margin-right: 6px; }
details summary { cursor: pointer; color: var(--text-2); font-size: 13px; margin-top: 8px; }
details[open] summary { margin-bottom: 4px; }
.companies { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 7px 8px; border-bottom: 1px solid var(--line-soft); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12.5px; white-space: nowrap; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.unusual td:first-child { box-shadow: inset 3px 0 0 var(--warn); }
.sub { display: block; font-size: 12px; color: var(--muted); }

/* ---------- charts ---------- */
.rank-chart { margin-top: 6px; }
.rank-group-title { font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
.bar-row {
  display: grid; grid-template-columns: minmax(84px, 34%) minmax(0, 1fr) 116px; align-items: center; gap: 8px;
  padding: 3px 0; border-radius: 4px;
}
.bar-row:hover, .bar-row:focus-visible { background: var(--panel-2); }
.bar-label { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { height: 14px; position: relative; }
.bar { position: absolute; left: 0; top: 0; height: 14px; border-radius: 0 4px 4px 0; min-width: 2px; }
.bar.in { background: var(--in); }
.bar.out { background: var(--out); }
.bar-value { font-family: var(--mono); font-size: 13px; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.legend { font-size: 12.5px; color: var(--muted); margin: 10px 0 0; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin: 0 4px 0 10px; vertical-align: -1px; }
.sw:first-child { margin-left: 0; }
.sw-in { background: var(--in); }
.sw-out { background: var(--out); }

.hist-chart { position: relative; width: 100%; min-height: 240px; }
.hist-chart svg { display: block; width: 100%; height: 260px; }
.hist-chart .grid { stroke: var(--line); stroke-width: 1; }
.hist-chart .zero { stroke: var(--muted); stroke-width: 1; }
.hist-chart .axis-label { fill: var(--muted); font-size: 11px; font-family: var(--mono); }
.hist-chart .col-in { fill: var(--in); }
.hist-chart .col-out { fill: var(--out); }
.hist-chart .col-partial { opacity: .45; }
.hist-chart .col-hit { fill: transparent; }
.hist-chart .cross { stroke: var(--text-2); stroke-width: 1; }
.hist-caption { font-size: 12.5px; color: var(--muted); margin-top: 6px; }

.tooltip {
  position: fixed; z-index: 50; max-width: 280px; pointer-events: none; padding: 8px 10px; border-radius: 8px;
  background: #1d2835; border: 1px solid var(--line); color: var(--text); font-size: 12.5px; line-height: 1.5;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .45);
}
.tooltip .tt-title { font-weight: 600; margin-bottom: 2px; }

/* ---------- card (v1.1) ---------- */
.card-lines { margin: 6px 0 0; padding-left: 18px; color: var(--text-2); font-size: 14px; }
.card-lines li { margin: 4px 0; }
.mini-table td, .mini-table th { padding: 5px 6px; }

/* ---------- coverage ---------- */
.cov-stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.cov-stat { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; }
.cov-stat .big { font-family: var(--mono); font-size: 20px; font-weight: 600; }
.meter { height: 8px; border-radius: 4px; background: var(--line-soft); margin-top: 6px; overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.limits { margin: 8px 0 0; padding-left: 18px; font-size: 13.5px; color: var(--text-2); }
.limits li { margin: 4px 0; }

/* ---------- models ---------- */
.models { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: var(--gap); }
.model { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.model.pending { border-style: dashed; background: transparent; }
.model h3 { font-size: 15.5px; margin: 8px 0 4px; }
.model p { margin: 4px 0; font-size: 13.5px; color: var(--text-2); }

/* ---------- status ---------- */
.feed { padding: 10px 0; border-top: 1px solid var(--line-soft); }
.feed:first-child { border-top: 0; padding-top: 0; }
.feed-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-wrap: wrap; }
.feed p { margin: 3px 0 0; font-size: 13.5px; color: var(--text-2); }
.feed .err { color: var(--serious); overflow-wrap: anywhere; }

.footer { max-width: 1440px; margin: 0 auto; padding: 20px 16px 40px; color: var(--muted); font-size: 12.5px; border-top: 1px solid var(--line-soft); }
.footer p { margin: 4px 0; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .grid-2 > .panel + .panel { margin-top: 0; }
}
@media (max-width: 560px) {
  body { font-size: 14.5px; }
  .brand h1 { font-size: 16px; }
  .brand-sub { display: none; }
  .tabs { top: 49px; }
  html { scroll-padding-top: 100px; }
  .tiles { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .tile-value { font-size: 19px; }
  .panel, .news-item { padding: 12px; }
  .news-item { grid-template-columns: 30px minmax(0, 1fr); gap: 8px; }
  .rank { font-size: 19px; }
  .bar-row { grid-template-columns: minmax(70px, 30%) minmax(0, 1fr) 96px; gap: 6px; }
  .bar-value { font-size: 12px; }
  .btn-refresh { padding: 4px 9px; }
  .cov-stats { grid-template-columns: minmax(0, 1fr); }
  /* tables become stacked rows */
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tr { padding: 8px 0; border-bottom: 1px solid var(--line-soft); }
  table.stack td { border: 0; padding: 2px 0; text-align: left; }
  table.stack td.num { text-align: left; }
  table.stack td[data-label]::before { content: attr(data-label) "："; color: var(--muted); font-size: 12px; }
  tr.unusual td:first-child { box-shadow: none; }
  tr.unusual { box-shadow: inset 3px 0 0 var(--warn); padding-left: 8px; }
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
@media (forced-colors: active) { .bar, .hist-chart rect { forced-color-adjust: none; } }
.linklike {
  font: inherit; color: var(--text); background: none; border: 0; padding: 0; cursor: pointer; text-align: left;
  border-bottom: 1px dashed var(--muted);
}
.linklike:hover, .linklike:focus-visible { color: var(--accent); border-bottom-color: var(--accent); }
tr.group-row td { font-size: 12px; color: var(--muted); padding-top: 14px; background: transparent; }
@media (max-width: 560px) { tr.group-row td::before { content: none; } tr.group-row { border-bottom: 0; padding-bottom: 0; } }
@media (max-width: 560px) {
  table.pool-table tr:not(.group-row) { display: flex; flex-wrap: wrap; gap: 0 14px; align-items: baseline; }
  table.pool-table td { width: auto; }
  table.pool-table td[data-label="市場"] { width: 100%; }
  table.pool-table td[data-label="類別"], table.pool-table td[data-label="期間"] { display: none; }
  table.pool-table td[data-label="淨流量"]::before, table.pool-table td[data-label="狀態"]::before { content: none; }
  table.pool-table td[data-label="淨流量"] { font-weight: 600; }
  table.pool-table td .sub { display: block; }
}
@media (max-width: 560px) {
  table.pool-table tr.unusual { padding-left: 10px; }
  table.mini-table tr { display: flex; flex-wrap: wrap; gap: 0 12px; align-items: baseline; }
  table.mini-table td { width: auto; }
  table.mini-table td[data-label="標的"] { width: 100%; font-weight: 600; }
  table.mini-table td[data-label="標的"]::before, table.mini-table td[data-label="狀態"]::before { content: none; }
}
/* public preview */
.preview-ribbon {
  padding: 8px 16px; background: repeating-linear-gradient(135deg, rgba(250, 178, 25, .16) 0 12px, rgba(250, 178, 25, .08) 12px 24px);
  border-bottom: 1px solid rgba(250, 178, 25, .5); color: var(--text); font-size: 13px; line-height: 1.5;
}
.preview-ribbon strong { color: var(--warn); }
.preview-tag {
  display: inline-block; margin-left: 6px; padding: 0 7px; border-radius: 4px; font-size: 12px; font-weight: 600;
  vertical-align: 2px; color: #1a1200; background: var(--warn);
}
/* long source URLs and attribution texts must wrap instead of widening the page on phones */
.limits li, .hist-caption, .notice p, .feed p, .sig-text, .summary li { overflow-wrap: anywhere; }
