/* ════════════════════════════════════════════════════════════════════════
   TW Market — Sector Rotation (台股板塊輪動)
   Approved mockup: design_mockups/twstock_sector_rotation_C_hybrid.html
   (方案 C 混搭定稿). Focus 1 = rotation quadrant scatter (RRG-style, values
   in tooltip only); focus 2 = sector × period-return heatmap matrix
   (sortable, expandable constituents); col-side = auto-read + quadrant
   legend + breadth history + SEO explain.
   Shared shell (pagehead/segment/card/analysis/read/states) copied from the
   sibling market/*.css pages verbatim; only `.hm-*` (heatmap matrix),
   `.qhow` (quadrant how-to) and `.constituents` are new to this page.
   ALL color / radius / shadow / motion via main.css tokens; heatmap heat
   steps are rgba() alpha grades of --color-green / --color-red read from
   the tokens at runtime (JS RGBA helper) — no new hue.
   ════════════════════════════════════════════════════════════════════════ */

#box_main {
  flex: 1;
  min-width: 0;
  min-height: 800px;
  padding-top: 14px;
  padding-left: 20px;
  max-width: 1340px;
}

/* ── Page head ── */
.pagehead {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-16);
  flex-wrap: wrap;
  margin-bottom: var(--space-16);
}
.titlewrap { display: flex; flex-direction: column; gap: 2px; }
.titlewrap .eyebrow {
  font-size: 11px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: var(--color-greyMedium);
  font-family: "Roboto Mono", monospace;
}
.titlewrap h1 { margin: 0; color: var(--color-black); }
body.dark-mode .titlewrap h1 { color: var(--color-darkText); }
.titlewrap .meta { font-size: 12px; color: var(--color-greyMedium); }

.segment { display: inline-flex; border: 1px solid var(--color-greyLight); border-radius: var(--radius-sm); overflow: hidden; }
body.dark-mode .segment { border-color: var(--color-darkBorder); }
.segment .btn_range {
  height: 28px; padding: 0 10px; border: none; background: transparent; color: var(--color-greyMedium);
  font-size: 12px; font-family: inherit; cursor: pointer; border-right: 1px solid var(--color-greyLight);
  transition: background var(--motion-fast) var(--ease-standard);
}
body.dark-mode .segment .btn_range { border-right-color: var(--color-darkBorder); }
.segment .btn_range:last-child { border-right: none; }
.segment .btn_range:hover { background: var(--color-greyLight); }
body.dark-mode .segment .btn_range:hover { background: var(--color-darkBorder); }
.segment .btn_range.active { background: var(--color-pureWhite); color: var(--color-black); font-weight: 600; box-shadow: var(--shadow-sm); }
body.dark-mode .segment .btn_range.active { background: var(--color-darkBorder); color: var(--color-darkText); box-shadow: none; }

/* ── Card ── */
.card { background: var(--color-surface); border: none; border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: var(--space-16); }
body.dark-mode .card { background: var(--color-darkCard); box-shadow: none; }
.card-h { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: var(--space-8); margin-bottom: var(--space-12); }
.card-h h6 { margin: 0; font-size: 13px; font-weight: 600; color: var(--color-black); }
body.dark-mode .card-h h6 { color: var(--color-darkText); }
.card-h .meta { font-size: 12px; color: var(--color-greyMedium); }
.chart_box { position: relative; }
.chart_note { font-size: 12px; color: var(--color-greyMedium); margin-top: var(--space-8); }
body.dark-mode .chart_note { color: var(--color-greyLightMedium); }

.pos { color: var(--color-green); }
.neg { color: var(--color-red); }
.mut { color: var(--color-greyMedium); }

/* summary-stat sub line (streak / migration / percentile caption) — the
   shared studio/base.css .ssr-stat recipe defines lab/val/unit only */
.ssr-stat .sub { font-size: 11px; color: var(--color-greyMedium); margin-top: 2px; min-height: 16px; }

/* ── Legend row in card-h (group swatches + window segment for the quadrant
   chart; RWD rules at the 768 block follow the sibling market pages' toolbar
   recipe — full-width segment, flexed buttons) ── */
.quad-tools { display: inline-flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.quad-tools .segment { margin-left: 10px; }
.leg-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--color-greyMedium);
}
.swatch { width: 8px; height: 8px; border-radius: var(--radius-pill); flex-shrink: 0; display: inline-block; }

/* ── Analysis grid ── */
.analysis { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: var(--space-16); align-items: start; }
.col-main { display: flex; flex-direction: column; gap: var(--space-16); min-width: 0; }
/* min-width: 0 matters: col-side holds the breadth CANVAS (siblings only put
   text here). Without it, on window shrink the canvas's intrinsic width
   becomes the shared grid track's min-content and deadlocks the whole layout
   at its last size — Chart.js can't shrink the canvas because its container
   is propped open by the canvas itself. */
.col-side { display: flex; flex-direction: column; gap: var(--space-16); position: sticky; top: var(--space-16); min-width: 0; }

/* ── Heatmap matrix (focus 2 — the detail layer) ──
   CSS-grid rows, not <table>: each row = sector cell + 5 period-return cells
   + 2 flow cells; the constituents drilldown spans the full row. Cell heat
   backgrounds are inline rgba() of --color-green/--color-red set by JS. */
/* Scroll container is unconditional (sibling .tbl-wrap recipe): the site-wide
   ≤1300px layout caps .container at 588px, which is narrower than the matrix's
   min content — the matrix scrolls horizontally whenever it doesn't fit, at
   any width. The sticky name column below keeps row identity while swiping;
   sticky is inert when nothing scrolls. */
.hm { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.hm-row {
  display: grid;
  /* returns 2fr vs flows 1fr keeps the mockup's wide-heat proportions on
     desktop; the minmax floors stop either group collapsing when the 588px
     tablet column squeezes the matrix (everything shrinks toward its floor
     together instead of the 1fr group alone absorbing the deficit) */
  grid-template-columns: 170px repeat(3, minmax(64px, 2fr)) repeat(3, minmax(64px, 1fr));
  gap: 4px; align-items: stretch; min-width: 578px;
}
.hm .sec, .hm-head .hc:first-child {
  position: sticky; left: 0; z-index: 1;
  background: var(--color-surface);
  box-shadow: 4px 0 0 0 var(--color-surface); /* covers the 4px grid gap */
}
body.dark-mode .hm .sec, body.dark-mode .hm-head .hc:first-child {
  background: var(--color-darkCard);
  box-shadow: 4px 0 0 0 var(--color-darkCard);
}
.hm-head { margin-bottom: var(--space-4); }
.hm-head .hc {
  font-size: 11px; color: var(--color-greyMedium); text-align: center; padding: 4px 2px;
  border-radius: var(--radius-micro); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; /* header cells never widen scrollWidth past the data rows */
  transition: background var(--motion-fast) var(--ease-standard);
}
.hm-head .hc[data-sort]:hover { background: var(--color-greyLight); }
body.dark-mode .hm-head .hc[data-sort]:hover { background: var(--color-darkBorder); }
.hm-head .hc:first-child { text-align: left; padding-left: 8px; }
/* Sort arrows come from the shared main.css recipe (`.box_header [data-sort]`
   — the header row carries the `box_header` class); no arrow CSS here.
   ⚠ canon-conflict note (per approved mockup header comment): canon says the
   active-sort arrow is --color-primaryText (AA on light), but on the dark
   ramp its contrast fails; the approved mockup uses --color-primary on dark.
   Scoped dark-mode override below — pending designer/canon alignment. */
body.dark-mode .hm-head [aria-sort]::after { background-color: var(--color-primary); }

.hm-row.data { margin-bottom: 4px; border-radius: var(--radius-xs); cursor: pointer; }
.hm-row.data:hover .sec { background: var(--color-greyLight); }
body.dark-mode .hm-row.data:hover .sec { background: var(--color-darkBorder); }
.hm .sec {
  display: flex; align-items: center; gap: 8px; padding: 0 8px; border-radius: var(--radius-micro);
  min-height: 30px; transition: background var(--motion-fast) var(--ease-standard);
}
.hm .sec .nm { color: var(--color-greyDark); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.dark-mode .hm .sec .nm { color: var(--color-greyLightMedium); }
.hm .sec .w { font-family: "Roboto Mono", monospace; font-size: 10px; color: var(--color-greyMedium); margin-left: auto; flex: none; }
.hm .cell {
  border-radius: var(--radius-micro); min-height: 30px; display: flex; align-items: center; justify-content: center;
  font-family: "Roboto Mono", monospace; font-variant-numeric: tabular-nums; font-size: 12px; color: var(--color-black);
}
body.dark-mode .hm .cell { color: var(--color-darkText); }
.hm .cell.flow { font-size: 11.5px; }

/* Constituents drilldown row (expanded on sector-row click) */
.constituents {
  grid-column: 1 / -1; background: transparent; border: 1px solid var(--color-greyLight);
  border-radius: var(--radius-xs); margin: 2px 0 6px; padding: var(--space-8) var(--space-12);
}
body.dark-mode .constituents { border-color: var(--color-darkBorder); }
.constituents .ttl { font-size: 11px; color: var(--color-greyMedium); margin-bottom: 6px; }
.constituents table { width: 100%; border-collapse: collapse; font-size: 12px; }
.constituents th {
  text-align: right; font-weight: 600; font-size: 11px; color: var(--color-greyMedium);
  padding: 3px 8px; border-bottom: 1px solid var(--color-greyLight);
}
body.dark-mode .constituents th { border-bottom-color: var(--color-darkBorder); }
.constituents th:first-child { text-align: left; }
.constituents td { text-align: right; padding: 4px 8px; font-family: "Roboto Mono", monospace; font-variant-numeric: tabular-nums; }
.constituents td:first-child { text-align: left; font-family: inherit; color: var(--color-greyDark); }
body.dark-mode .constituents td:first-child { color: var(--color-greyLightMedium); }
.constituents td:first-child .code { font-family: "Roboto Mono", monospace; color: var(--color-greyMedium); margin-right: 6px; }
.constituents .cons-msg { font-size: 12px; color: var(--color-greyMedium); padding: 4px 0; }
/* inline small-wait spinner (canon › Loader: button/inline waits = CSS spin, neutral) */
.constituents .spin {
  display: inline-block; width: 12px; height: 12px; margin-right: 6px; vertical-align: -2px;
  border: 2px solid var(--color-greyLightMedium); border-top-color: var(--color-greyDark);
  border-radius: var(--radius-pill); animation: spin 1s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
  .constituents .spin { animation: none; }
}

.hm-foot { display: flex; justify-content: space-between; align-items: center; gap: var(--space-8); flex-wrap: wrap; margin-top: var(--space-12); }
.hm-legend { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--color-greyMedium); }
.hm-legend .sw { width: 16px; height: 10px; border-radius: var(--radius-micro); }

/* ── 自動解讀 read card (col-side) — same recipe as the sibling market/chips
   pages (tagged signal lines). ── */
.read { display: flex; flex-direction: column; gap: var(--space-8); }
.read .line {
  border: 1px solid var(--color-greyLight); border-radius: var(--radius-sm); padding: 9px 11px;
  font-size: 13px; line-height: 1.6; display: flex; gap: 8px; align-items: flex-start; color: var(--color-greyDark);
}
body.dark-mode .read .line { border-color: var(--color-darkBorder); color: var(--color-greyLightMedium); }
.read .tag {
  flex: none; font-size: 10px; font-weight: 600; letter-spacing: .04em;
  border-radius: var(--radius-micro); padding: 2px 6px; margin-top: 3px;
}
.tag-pos { background: var(--color-greenLight); color: var(--color-greenBlack); }
.tag-neg { background: var(--color-redLight); color: var(--color-redBlack); }
.tag-neu { background: var(--color-badgeNeutralBg); color: var(--color-badgeNeutralText); }
.read-note { font-size: 11px; color: var(--color-greyMedium); margin-top: var(--space-8); }

/* ── 象限怎麼讀 (descriptive quadrant legend) ──
   row class is `.qrow`, NOT `.row` — Bootstrap's global `.row` grid class
   would win the layout fight and wrap the swatch onto its own line. */
.qhow { display: flex; flex-direction: column; gap: var(--space-8); font-size: 12px; color: var(--color-greyMedium); line-height: 1.6; }
.qhow .qrow { display: flex; gap: 8px; align-items: flex-start; }
.qhow .qd { flex: none; width: 10px; height: 10px; border-radius: var(--radius-micro); margin-top: 4px; }
.qhow b { color: var(--color-greyDark); }
body.dark-mode .qhow b { color: var(--color-greyLightMedium); }
/* quadrant tint swatches: alpha grades of the trading tokens (same technique
   as the heatmap heat steps — no new hue). .lead/.lag backgrounds are set by
   the page JS via RGBA(--color-green/--color-red, .45) token reads (CSS hex
   tokens can't take an alpha), same as the heatmap footer legend swatches. */
.qhow .qd.mid { background: var(--color-badgeNeutralBg); }

/* ── SEO explain block (shared main.css .seo-explain recipe is used; only the
   col-side stacking margin matches the sibling market/*.css pages) ── */
.seo-explain { margin-top: var(--space-16); }

/* ── States ── */
.loader_wrap { display: flex; flex-direction: column; gap: var(--space-12); justify-content: center; align-items: center; padding: 32px 0; color: var(--color-greyMedium); font-size: 13px; }
.loader_wrap .msg.error { color: var(--color-red); }
.retry { height: 30px; padding: 0 14px; border: 1px solid var(--color-greyLight); background: transparent; border-radius: var(--radius-sm); font: 500 12px "Roboto Mono", monospace; color: var(--color-greyDark); cursor: pointer; transition: border-color var(--motion-fast) var(--ease-standard); }
body.dark-mode .retry { border-color: var(--color-darkBorder); color: var(--color-darkText); }
.retry:hover { border-color: var(--color-primary); color: var(--color-primary); }

/* ── RWD ── */
@media (max-width: 1300px) {
  .analysis { grid-template-columns: 1fr; }
  .col-side { position: static; }
  #box_container { padding-top: 60px !important; }
  #box_main { padding-left: 0; }
  /* RWD offcanvas button names the section — drop the page title, keep .meta */
  .titlewrap .eyebrow,
  .titlewrap h1 { display: none; }
}
@media (max-width: 768px) {
  .pagehead { flex-direction: column; align-items: stretch; }
  /* sibling market pages' mobile toolbar recipe: the window segment goes
     full-width with evenly flexed buttons; legend wraps above it */
  .quad-tools { width: 100%; }
  .quad-tools .segment { margin-left: 0; }
  .segment { width: 100%; display: flex; }
  .segment .btn_range { flex: 1; }
  /* phone: tighter matrix columns (scroll container + sticky name column are
     unconditional base rules above) */
  .hm-row { grid-template-columns: 88px repeat(3, minmax(56px, 1fr)) repeat(3, minmax(56px, 1fr)); min-width: 500px; }
  .hm .cell { font-size: 10.5px; }
  .hm .cell.flow { font-size: 10px; }
}
