/* #region zone:desk.style.stage-view — หน้าวิเคราะห์ช่วงตลาด: การ์ดสรุปเลือกช่วง เพิ่งเปลี่ยนช่วง board */
/* Spec docs/superpowers/specs/2026-09-27-desk-stage-analysis.md; readability follow-ups 2026-09-28
   and 2026-10-13 desk-stage-ux (owner: "ปรับหน้านี้หน่อยให้ดูง่ายขึ้น"): Thai-first headings, every
   text >=12px, plain stat labels, stage-coloured sparklines, grouped transitions, summary cards as
   the navigation, honest note up top. A real Desk VIEW (swaps with #app-grid inside #app-shell),
   not a dialog: no backdrop/scrim. The whole view scrolls as one page at every width (columns are
   paged with "แสดงเพิ่ม", never their own scrollers). >=1024px: 4 columns, or one stage when a
   summary card is picked; below that: the cards are tabs and one stage shows. Tokens only. */

/* One stage palette: a .tone-N ancestor sets --tone/--tone-soft, and every stage-coloured part
   (summary card, column head, stock card sparkline) reads from it. */
.tone-1 {
  --tone: var(--stage-1);
  --tone-soft: var(--stage-1-soft);
}
.tone-2 {
  --tone: var(--stage-2);
  --tone-soft: var(--stage-2-soft);
}
.tone-3 {
  --tone: var(--stage-3);
  --tone-soft: var(--stage-3-soft);
}
.tone-4 {
  --tone: var(--stage-4);
  --tone-soft: var(--stage-4-soft);
}

/* Same flex-child sizing as .app-grid (desk.css): #app-shell is display:flex/column, and this view
   takes over that one flex slot when shown (desk-stage-analysis.js toggles `hidden` on both). */
.stage-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  background: var(--bg);
  padding: var(--sp-4) var(--sp-5) var(--sp-6);
  overflow-y: auto;
}
.stage-view > * {
  flex-shrink: 0;
}

/* ---- Head: back, title + one-line explanation, market switch ---- */
.stage-view-head {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
}
#stage-back-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--panel);
}
.stage-view-title {
  flex: 1 1 auto;
  min-width: 0;
}
.stage-view-title h1 {
  margin: 0;
  font-size: var(--fs-4xl);
  font-weight: 700;
  line-height: 1.25;
  text-wrap: balance;
}
.stage-view-en {
  font-size: var(--fs-md);
  font-weight: 500;
  color: var(--muted);
}
.stage-view-sub {
  margin: var(--sp-1) 0 0;
  font-size: var(--fs-md);
  color: var(--muted);
  line-height: 1.5;
}
.stage-view-head .stage-market-seg {
  height: auto;
}
.stage-market-seg .seg-btn {
  height: 36px;
  font-size: var(--fs-md);
  padding: 0 var(--sp-3);
}

/* ---- Honest framing: data date + "not a recommendation", readable, up top ---- */
.stage-note {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-2);
  margin: 0;
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--panel);
  font-size: var(--fs-md);
  line-height: 1.5;
  color: var(--muted);
}
.stage-note svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  margin-top: 3px;
  color: var(--accent-text);
}
.stage-asof {
  color: var(--text);
  font-weight: 700;
}
.stage-asof:empty + .stage-note-sep {
  display: none;
}

/* ---- Stage icon chip (icon + word everywhere, never colour alone) ---- */
.stage-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: var(--r-control);
  background: var(--tone-soft);
  color: var(--tone);
}
.stage-icon svg {
  width: 17px;
  height: 17px;
}

/* ---- Summary cards = the navigation ---- */
.stage-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--sp-3);
}
.stage-summary-tile {
  display: grid; /* DUI-23: a button centres its content; grid + align-content keeps every tile on one top line */
  align-content: start;
  gap: var(--sp-2);
  min-width: 0;
  padding: var(--sp-3) var(--sp-4);
  border: 1px solid var(--line);
  border-radius: var(--r-inner);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition:
    background-color var(--dur-base) var(--ease-out),
    border-color var(--dur-base) var(--ease-out),
    box-shadow var(--dur-base) var(--ease-out);
}
.stage-summary-tile:hover {
  border-color: var(--tone);
}
.stage-summary-tile[aria-pressed='true'] {
  border-color: var(--tone);
  background: var(--tone-soft);
  box-shadow: inset 0 0 0 1px var(--tone);
}
.stage-summary-row {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-width: 0;
}
.stage-summary-name {
  min-width: 0;
  font-size: var(--fs-xl);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stage-summary-name small,
.stage-column-name small {
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--muted);
}
.stage-summary-count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-4xl);
  font-weight: 700;
  line-height: 1;
}
.stage-summary-bar {
  display: block;
  height: 6px;
  border-radius: var(--r-pill);
  background: var(--line);
  overflow: hidden;
}
.stage-summary-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: var(--r-pill);
  background: var(--tone);
  transition: width var(--dur-slow) var(--ease-out);
}
.stage-summary-blurb {
  font-size: var(--fs-md);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  color: var(--muted);
  line-height: 1.45;
}
.stage-summary-pct {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--text);
}

/* ---- "เพิ่งเปลี่ยนช่วง": one group per transition type ---- */
.stage-section-title {
  margin: 0 0 var(--sp-2);
  font-size: var(--fs-xl);
  font-weight: 700;
}
.stage-section-title span {
  font-size: var(--fs-md);
  font-weight: 500;
  color: var(--muted);
}
.stage-recent-groups {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  gap: var(--sp-3);
}
.stage-recent-group {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  min-width: 0;
  padding: var(--sp-2) var(--sp-3) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-inner);
  background: var(--panel);
}
.stage-recent-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
}
.stage-recent-dir {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  height: 26px;
  padding: 0 var(--sp-2);
  border-radius: var(--r-pill);
  font-size: var(--fs-sm);
  font-weight: 700;
  background: var(--panel2);
  color: var(--muted);
}
.dir-up .stage-recent-dir {
  background: var(--up-soft);
  color: var(--up);
}
.dir-down .stage-recent-dir {
  background: var(--down-soft);
  color: var(--down);
}
.stage-recent-label {
  font-size: var(--fs-lg);
  font-weight: 700;
}
.stage-recent-count {
  margin-left: auto;
  font-size: var(--fs-md);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.stage-recent-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}
.stage-recent-chip,
.stage-recent-more {
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 var(--sp-3);
  border-radius: var(--r-pill);
  font-size: var(--fs-md);
  cursor: pointer;
}
.stage-recent-chip {
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  font-weight: 700;
}
.stage-recent-chip:hover {
  background: var(--hover);
}
.stage-recent-more {
  border: 0;
  background: transparent;
  color: var(--accent-text);
  font-weight: 600;
}
.stage-recent-more:hover {
  background: var(--acc-soft);
}

/* ---- Toolbar: search, sort, "ดูทุกช่วง" ---- */
.stage-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-2) var(--sp-3);
}
.stage-filter {
  flex: 1 1 220px;
  min-width: 160px;
  height: 40px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-input);
  background: var(--panel);
  color: var(--text);
  font-size: var(--fs-lg);
}
.stage-filter::placeholder {
  color: var(--faint);
}
.stage-sort {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-md);
  color: var(--muted);
  flex-shrink: 0;
}
.stage-sort-btn {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  height: 40px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--panel);
  color: var(--text);
  font-size: var(--fs-md);
  text-align: left;
}
.stage-sort-btn:hover {
  background: var(--hover);
}
.stage-sort-btn svg {
  width: 14px;
  height: 14px;
  flex: none;
  color: var(--muted);
}
#stage-sort-dir {
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--panel);
}
.stage-show-all {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  height: 40px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--panel);
  color: var(--accent-text);
  font-size: var(--fs-md);
  font-weight: 600;
  cursor: pointer;
}
.stage-show-all svg {
  width: 16px;
  height: 16px;
}
.stage-show-all:hover {
  background: var(--acc-soft);
}

/* ---- Board: 4 columns, or one focused stage ---- */
.stage-board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--sp-3);
  align-items: start;
}
.stage-board:not([data-focus='all']) {
  grid-template-columns: minmax(0, 1fr);
}
.stage-board[data-focus='1'] .stage-column:not([data-stage='1']),
.stage-board[data-focus='2'] .stage-column:not([data-stage='2']),
.stage-board[data-focus='3'] .stage-column:not([data-stage='3']),
.stage-board[data-focus='4'] .stage-column:not([data-stage='4']) {
  display: none;
}
.stage-column {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  background: var(--panel);
}
.stage-column-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-1) var(--sp-2);
  padding: var(--sp-3) var(--sp-3) var(--sp-3) var(--sp-3);
  border-bottom: 1px solid var(--line);
}
.stage-column-name {
  flex: 1 1 auto;
  min-width: 0;
  font-size: var(--fs-xl);
  font-weight: 700;
}
.stage-count {
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-md);
  font-weight: 700;
  color: var(--muted);
}
.stage-column-meaning {
  flex-basis: 100%;
  font-size: var(--fs-md);
  color: var(--muted);
  line-height: 1.45;
}
.stage-board[data-focus='all'] .stage-column-meaning {
  display: none;
}
.stage-column-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--sp-2);
  padding: var(--sp-2);
  container-type: inline-size;
}
.stage-board:not([data-focus='all']) .stage-column-list {
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
  gap: var(--sp-3);
  padding: var(--sp-3);
}

/* "แสดงเพิ่ม": spans the whole list row. */
.stage-more-btn {
  grid-column: 1 / -1;
  min-height: 40px;
  padding: var(--sp-2);
  border: 1px dashed var(--line);
  border-radius: var(--r-control);
  background: transparent;
  color: var(--accent-text);
  font-size: var(--fs-md);
  font-weight: 600;
  cursor: pointer;
}
.stage-more-btn:hover {
  background: var(--acc-soft);
}

/* ---- Stock card ---- */
.stage-row {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-width: 0;
  padding: var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-inner);
  background: var(--panel2);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition:
    background-color var(--dur-base) var(--ease-out),
    border-color var(--dur-base) var(--ease-out);
}
.stage-row:hover {
  background: var(--hover);
  border-color: var(--tone);
}
.stage-row-head {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  align-items: center;
  column-gap: var(--sp-2);
  min-width: 0;
}
.stage-row .stage-row-logo {
  grid-area: auto;
  --logo-size: 28px;
  --k: 0.7;
}
.stage-row-id {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-width: 0;
}
.stage-row-line1 {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-2);
  min-width: 0;
}
.stage-row-ticker {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--fs-2xl);
  font-weight: 700;
  letter-spacing: -0.01em;
}
.stage-row-age {
  flex-shrink: 0;
  font-size: var(--fs-sm);
  color: var(--faint);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.stage-row-name {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--fs-sm);
  color: var(--muted);
}
.stage-row-spark {
  display: block;
  width: 100%;
  height: 40px;
  margin: var(--sp-1) 0;
}
.stage-spark-area {
  fill: var(--tone-soft);
  stroke: none;
}
.stage-spark-price {
  fill: none;
  stroke: var(--tone);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.stage-spark-ma {
  fill: none;
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  opacity: 0.7;
}
.stage-row-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--sp-2);
}
.stage-row-stat {
  display: flex;
  flex-direction: column;
  justify-content: space-between; /* a label that wraps to 2 lines keeps its value on the shared bottom line */
  gap: 2px;
  min-width: 0;
  cursor: help;
}
.stage-row-stat-label {
  font-size: var(--fs-sm);
  color: var(--muted);
  overflow-wrap: anywhere; /* wraps instead of being cut with an ellipsis ("Stronger than market") */
  line-height: 1.25;
}
.stage-row-stat b {
  font-size: var(--fs-lg);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.stage-row-stat b.up {
  color: var(--up);
}
.stage-row-stat b.down {
  color: var(--down);
}
/* A narrow card (a 4-column board at ~1024px): stats stack as label/value rows so the Thai labels
   never truncate into nonsense. */
@container (max-width: 250px) {
  .stage-row-stats {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--sp-1);
  }
  .stage-row-stat {
    flex-direction: row;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--sp-2);
  }
}

.stage-empty {
  margin: var(--sp-5) auto;
  text-align: center;
  color: var(--muted);
  font-size: var(--fs-md);
  max-width: 280px;
}
.stage-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-4) 0;
}
/* One message slot visible at every width (loading/empty/error/package-required). Scoped to
   :not([hidden]) so JS's plain `.hidden = true` still hides it. */
.stage-message:not([hidden]) {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 200px;
  text-align: center;
}
.stage-message p {
  margin: 0;
}
.stage-message-why {
  max-width: 60ch;
  color: var(--muted);
  font-size: var(--fs-sm);
}
.stage-message > svg {
  color: var(--muted);
}
/* While a message shows, the summary/board stay off whatever the breakpoint rules say. */
.stage-summary.is-suppressed,
.stage-board.is-suppressed {
  display: none !important;
}
.stage-footnote {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--muted);
  line-height: 1.6;
  max-width: 75ch;
}

@media (prefers-reduced-motion: reduce) {
  .stage-summary-fill,
  .stage-summary-tile,
  .stage-row {
    transition: none;
  }
}

/* ---- Below 1024px: the summary cards become the tabs, one stage shows ---- */
@media (max-width: 1023px) {
  .stage-view {
    padding: var(--sp-4);
  }
  .stage-board[data-focus='all'] {
    grid-template-columns: minmax(0, 1fr);
  }
  .stage-board[data-focus='all'] .stage-column:not([data-stage='1']) {
    display: none;
  }
  .stage-board[data-focus='all'] .stage-column-meaning {
    display: block;
  }
  .stage-board .stage-column-list {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr));
  }
  .stage-summary {
    gap: var(--sp-2);
  }
  .stage-summary-tile {
    padding: var(--sp-2) var(--sp-3);
    gap: var(--sp-1);
  }
  .stage-summary-blurb,
  .stage-summary-name small,
  .stage-summary-tile .stage-icon {
    display: none;
  }
  .stage-summary-tile {
    align-items: center;
    text-align: center;
  }
  .stage-summary-row {
    flex-direction: column;
    gap: var(--sp-1);
  }
  .stage-summary-count {
    margin-left: 0;
    font-size: var(--fs-3xl);
  }
  .stage-summary-bar {
    align-self: stretch;
  }
}
@media (max-width: 819px) {
  .stage-view-head {
    flex-wrap: wrap;
  }
  .stage-view-head .stage-market-seg {
    order: 3;
    flex-basis: 100%;
  }
  .stage-market-seg .seg-btn {
    flex: 1 1 0;
  }
}
/* Touch targets apply on tablets too, without the phone's stacked layout. */
@media (max-width: 600px), (pointer: coarse) {
  #stage-back-btn,
  #stage-sort-dir {
    width: var(--tap-min);
    height: var(--tap-min);
  }
  .stage-market-seg .seg-btn,
  .stage-filter,
  .stage-sort-btn,
  .stage-show-all,
  .stage-more-btn,
  .stage-recent-chip,
  .stage-recent-more {
    min-height: var(--tap-min);
  }
}
/* Phone: compact 4-up tabs (icon + count over the Thai name), 44px tap targets everywhere. */
@media (max-width: 599px) {
  .stage-view {
    padding: var(--sp-3) var(--sp-4) var(--sp-5);
    gap: var(--sp-3);
  }
  .stage-view-title h1 {
    font-size: var(--fs-2xl);
  }
  .stage-view-en {
    display: none;
  }
  /* ids: the Board's own phone .icon-btn sizing is more specific than the class pair */
  #stage-back-btn,
  #stage-sort-dir {
    width: 44px;
    height: 44px;
  }
  .stage-market-seg .seg-btn {
    height: 44px;
  }
  .stage-summary-tile {
    padding: var(--sp-2) var(--sp-1);
    min-height: 44px;
  }
  .stage-summary-count {
    font-size: var(--fs-2xl);
  }
  .stage-summary-name {
    font-size: var(--fs-md);
  }
  .stage-filter {
    flex: 1 1 100%;
    height: 44px;
    font-size: var(--fs-xl); /* 16px: no iOS zoom-on-focus */
  }
  .stage-sort {
    flex: 1 1 auto;
  }
  .stage-sort-btn {
    flex: 1 1 auto;
    height: 44px;
  }
  .stage-show-all,
  .stage-more-btn,
  .stage-recent-chip,
  .stage-recent-more {
    min-height: 44px;
    height: auto;
  }
  .stage-recent-group {
    padding: var(--sp-3);
  }
}
/* #endregion */

/* #region zone:desk.style.stage-nav — ปุ่ม "ช่วงตลาด" บนหัวเว็บ (เปิด/ปิดหน้าวิเคราะห์ช่วงตลาด) */
/* desk-stage-ux 2026-10-13 (owner: "มีปุ่มบน navigation bar ด้านบนกดเข้าถึงง่ายขึ้น"): a labelled
   .qt-btn (desk-quick-tools.css) in the header quick-tools group, Desk and Board header alike.
   aria-pressed mirrors the open view. Icon-only below 600px (its aria-label still names it). */
.qt-btn.qt-stage-btn {
  width: auto;
  gap: var(--sp-2);
  padding: 0 var(--sp-3);
  font-size: var(--fs-md);
  font-weight: 600;
  white-space: nowrap;
}
.qt-btn.qt-stage-btn svg {
  width: 16px;
  height: 16px;
}
.qt-btn.qt-stage-btn[aria-pressed='true'] {
  background: var(--acc-soft);
  border-color: var(--acc-ring);
  color: var(--accent-text);
}
:root[data-board] .app-header .qt-stage-btn {
  height: 36px;
}
/* Desktop widths where the search box needs the room (and the Board's Luna button shows): icon
   only, still named by aria-label + tooltip. */
@media (min-width: 900px) and (max-width: 1199px) {
  .qt-btn.qt-stage-btn {
    width: 36px;
    padding: 0;
  }
  .qt-stage-btn .qt-stage-label {
    display: none;
  }
}
@media (max-width: 599px) {
  .qt-btn.qt-stage-btn,
  :root[data-board] .app-header .qt-stage-btn {
    width: 44px;
    height: 44px;
    padding: 0;
  }
  .qt-stage-btn .qt-stage-label {
    display: none;
  }
}
/* #endregion */
