/* BTC Compass — tokens follow the validated dataviz reference palette */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --ink-1: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --bull: #2a78d6;          /* diverging cool pole = เอียงขึ้น */
  --bear: #e34948;          /* diverging warm pole = เอียงลง */
  --bull-soft: rgba(42, 120, 214, 0.32);
  --bear-soft: rgba(227, 73, 72, 0.32);
  --neutral-zone: #f0efec;
  --series-1: #2a78d6;      /* ราคา / เส้นหลัก */
  --series-2: #eb6834;      /* เส้นรอง (ไม่ค่อยใช้) */
  --ma-fast: #a19f97;       /* de-emphasis: SMA50 */
  --ma-slow: #6b6963;       /* de-emphasis: SMA200 */
  --delta-up: #006300;
  --delta-down: #d03b3b;
  --status-ok: #0ca30c;
  --status-warn: #fab219;
  --status-fail: #d03b3b;
  --focus: #2a78d6;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 4px 16px rgba(11, 11, 11, 0.05);
  --font-ui: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", "Thonburi", sans-serif;
  --font-data: system-ui, -apple-system, "Segoe UI", "Thonburi", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --ink-1: #ffffff;
  --ink-2: #c3c2b7;
  --ink-3: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --bull: #3987e5;
  --bear: #e66767;
  --bull-soft: rgba(57, 135, 229, 0.34);
  --bear-soft: rgba(230, 103, 103, 0.34);
  --neutral-zone: #383835;
  --series-1: #3987e5;
  --series-2: #d95926;
  --ma-fast: #757369;
  --ma-slow: #a3a196;
  --delta-up: #0ca30c;
  --delta-down: #e66767;
  --shadow: none;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink-1);
  font-family: var(--font-ui);
  font-size: 14.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 17px; }
h3 { font-size: 15px; margin-top: 18px; }
p { margin: 0; }
.mono { font-family: var(--font-mono); font-size: 12px; }
.sub { color: var(--ink-2); font-size: 13px; }
.eyebrow {
  font-size: 11.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.09em; color: var(--ink-3);
}

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

/* ── topbar ─────────────────────────────────────────── */

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  flex-wrap: wrap;
  padding: 14px 22px;
  border-bottom: 1px solid var(--grid);
  background: var(--surface-1);
  position: sticky; top: 0; z-index: 30;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 34px; height: 34px; color: var(--ink-1); }
.brand-needle { fill: var(--bull); }
.brand-hub { fill: var(--ink-1); }
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: 0.01em; }
.brand-sub { font-size: 12px; color: var(--ink-3); }

.topbar-right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.updated { color: var(--ink-3); }

.sources { display: flex; gap: 7px; align-items: center; }
.source-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--baseline); display: inline-block;
}
.source-dot.ok { background: var(--status-ok); }
.source-dot.fail { background: var(--status-fail); }
.source-dot.loading { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

.btn {
  font: inherit; font-size: 13px; font-weight: 500;
  color: var(--ink-1); background: var(--surface-1);
  border: 1px solid var(--baseline); border-radius: 8px;
  padding: 7px 14px; cursor: pointer;
}
.btn:hover { border-color: var(--ink-3); }
.btn.ghost { border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { border-color: var(--baseline); }
.btn.small { padding: 4px 10px; font-size: 12px; border-radius: 6px; }

/* ── layout ─────────────────────────────────────────── */

main {
  /* จอใหญ่ใช้พื้นที่ให้เต็ม — เพดาน 2400px กันจอ ultrawide ยืดจนกวาดตาไม่ไหว
     ระยะขอบโตตามจอด้วย clamp เพื่อไม่ให้เนื้อหาชิดขอบ */
  max-width: 2400px; margin: 0 auto;
  padding: 22px clamp(16px, 2vw, 44px) 40px;
  display: flex; flex-direction: column; gap: 18px;
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 22px;
  box-shadow: var(--shadow);
}

.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }

main.refetching .chart-card, main.refetching .hero, main.refetching .tiles {
  opacity: 0.55; transition: opacity 0.25s ease;
}

/* ── realized-price alert banner ────────────────────── */

/* [hidden] ต้องชนะ display:flex ด้านล่าง ไม่งั้นแบนเนอร์ที่ซ่อนยังกินที่เป็นกล่องว่าง */
.alert-banner[hidden] { display: none; }

.alert-banner {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 18px; border-radius: 12px;
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  font-size: 13.5px;
}
.alert-banner .alert-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.alert-banner.warn { border-color: color-mix(in srgb, var(--status-warn) 55%, transparent); }
.alert-banner.warn .alert-dot { background: var(--status-warn); }
.alert-banner.critical { border-color: color-mix(in srgb, var(--status-fail) 60%, transparent); }
.alert-banner.critical .alert-dot { background: var(--status-fail); }
.alert-banner .alert-text strong { font-weight: 700; }
.alert-banner .alert-nums { font-family: var(--font-data); font-variant-numeric: tabular-nums; }
.alert-dismiss { margin-left: auto; flex: none; }

/* ── hero ───────────────────────────────────────────── */

.hero { padding: 24px 26px 20px; }
.hero-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; flex-wrap: wrap; }
.verdict h1 { font-size: 30px; font-weight: 700; letter-spacing: -0.015em; margin-top: 6px; }
.verdict .sub { margin-top: 6px; max-width: 78ch; }

.hero-score { text-align: right; }
.hero-num-row { display: flex; align-items: baseline; gap: 6px; justify-content: flex-end; }
.hero-num {
  font-family: var(--font-data);
  font-size: 56px; font-weight: 600; line-height: 1; letter-spacing: -0.02em;
}
.hero-num.pos { color: var(--bull); }
.hero-num.neg { color: var(--bear); }
.hero-unit { color: var(--ink-3); font-size: 14px; }
.conf { font-size: 12.5px; color: var(--ink-2); margin-top: 6px; }

/* bearing tape — the signature */

.tape { margin-top: 22px; }
.tape-track {
  position: relative; height: 34px; border-radius: 8px; overflow: hidden;
  display: flex; border: 1px solid var(--border);
}
.tape-zone { height: 100%; }
.z-bear2 { width: 25%; background: var(--bear); opacity: 0.85; }
.z-bear1 { width: 17.5%; background: var(--bear-soft); }
.z-mid   { width: 15%;  background: var(--neutral-zone); }
.z-bull1 { width: 17.5%; background: var(--bull-soft); }
.z-bull2 { width: 25%; background: var(--bull); opacity: 0.85; }

.tape-ticks {
  position: absolute; inset: 0;
  background-image: repeating-linear-gradient(
    to right,
    transparent 0, transparent calc(5% - 1px),
    rgba(255, 255, 255, 0.55) calc(5% - 1px), rgba(255, 255, 255, 0.55) 5%
  );
  mix-blend-mode: soft-light;
}
:root[data-theme="dark"] .tape-ticks { mix-blend-mode: overlay; }

.tape-needle {
  position: absolute; top: 0; bottom: 0; left: 50%;
  width: 2px; background: var(--ink-1);
  transform: translateX(-1px);
  transition: left 0.6s cubic-bezier(0.22, 1, 0.36, 1);
}
.tape-needle-cap {
  position: absolute; top: -1px; left: 50%; transform: translateX(-50%);
  border-left: 6px solid transparent; border-right: 6px solid transparent;
  border-top: 7px solid var(--ink-1);
}
@media (prefers-reduced-motion: reduce) {
  .tape-needle { transition: none; }
  .source-dot.loading { animation: none; opacity: 0.5; }
}

.tape-scale, .tape-labels {
  display: flex; justify-content: space-between;
  margin-top: 6px; color: var(--ink-3);
}
.tape-scale span { font-size: 10.5px; }
.tape-labels { margin-top: 2px; font-size: 12px; }
.tape-labels span { flex: 1; text-align: center; }
.tape-labels span:first-child { text-align: left; }
.tape-labels span:last-child { text-align: right; }

.hero-stats {
  display: flex; gap: 26px; flex-wrap: wrap;
  margin-top: 18px; padding-top: 16px;
  border-top: 1px solid var(--grid);
}
.hstat .hstat-label { font-size: 12px; color: var(--ink-3); }
.hstat .hstat-value { font-family: var(--font-data); font-size: 17px; font-weight: 600; }
.hstat .hstat-value .delta { font-size: 13px; font-weight: 500; margin-left: 6px; }
.delta.up { color: var(--delta-up); }
.delta.down { color: var(--delta-down); }

/* ── stat tiles ─────────────────────────────────────── */

.tiles {
  /* flex แทน grid: แถวสุดท้ายที่มีไม่ครบจะยืดเติมเต็มแทนที่จะเหลือช่องว่างโหว่
     max-width กันไม่ให้ยืดจนใหญ่ผิดสัดส่วนเมื่อเหลือใบเดียว */
  display: flex; flex-wrap: wrap; gap: 14px;
}
.tile { flex: 1 1 190px; max-width: 360px; }
.tile {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow);
}
.tile-label { font-size: 12.5px; color: var(--ink-2); }
.tile-value { font-family: var(--font-data); font-size: 24px; font-weight: 600; margin-top: 3px; }
.tile-detail { font-size: 12px; color: var(--ink-3); margin-top: 3px; }

/* ── category bars ──────────────────────────────────── */

.cat-bars { display: flex; flex-direction: column; gap: 10px; }
.cat-row {
  display: grid; grid-template-columns: 150px 1fr 64px; gap: 14px; align-items: center;
}
.cat-name { font-size: 13.5px; font-weight: 500; }
.cat-name .cat-weight { color: var(--ink-3); font-weight: 400; font-size: 12px; margin-left: 4px; }
.cat-track {
  position: relative; height: 16px; border-radius: 6px;
  background: transparent;
}
.cat-track::before {
  content: ""; position: absolute; left: 0; right: 0; top: 50%;
  height: 1px; background: var(--grid);
}
.cat-track::after {
  content: ""; position: absolute; left: 50%; top: 0; bottom: 0;
  width: 1px; background: var(--baseline);
}
.cat-fill {
  position: absolute; top: 1px; bottom: 1px; border-radius: 4px; min-width: 2px;
}
.cat-fill.pos { background: var(--bull); border-top-left-radius: 2px; border-bottom-left-radius: 2px; }
.cat-fill.neg { background: var(--bear); border-top-right-radius: 2px; border-bottom-right-radius: 2px; }
.cat-fill.na { background: var(--baseline); height: 4px; top: 6px; }
.cat-score { font-family: var(--font-data); font-size: 14px; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.cat-score.na { color: var(--ink-3); font-weight: 400; }

/* ── range bar ──────────────────────────────────────── */

.rangebar { display: flex; align-items: center; gap: 8px; padding: 0 2px; }
.rangebar-label { font-size: 13px; color: var(--ink-2); margin-right: 4px; }
.range-btn {
  font: inherit; font-size: 12.5px; color: var(--ink-2);
  background: transparent; border: 1px solid transparent;
  border-radius: 7px; padding: 5px 12px; cursor: pointer;
}
.range-btn:hover { background: var(--surface-1); border-color: var(--baseline); }
.range-btn[aria-pressed="true"] {
  background: var(--surface-1); border-color: var(--ink-3);
  color: var(--ink-1); font-weight: 600;
}

/* ── charts ─────────────────────────────────────────── */

.charts-grid {
  display: grid; gap: 18px;
  /* auto-fit: จอ ~1200px ได้ 2 คอลัมน์ · ~1500px ได้ 3 · ~2000px ได้ 4
     min() กันไม่ให้ล้นจอแคบ */
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr));
}
.span2 { grid-column: span 2; }

.chart-card { padding: 16px 18px 12px; }
.chart-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; flex-wrap: wrap; }
/* ให้ชื่อ/คำอธิบายยุบและตัดบรรทัดเองได้ ค่า "ล่าสุด" จะได้อยู่บรรทัดเดียวกับชื่อเสมอ
   (ไม่ใส่ min-width:0 ข้อความยาวจะดันบล็อกขวาตกบรรทัด) */
.chart-head > div:first-child { min-width: 0; flex: 1 1 auto; }
.chart-title { font-size: 14.5px; font-weight: 600; }
.chart-sub { font-size: 12px; color: var(--ink-3); margin-top: 1px; }

.chart-head-right { display: flex; align-items: center; gap: 12px; margin-left: auto; }
.chart-now { display: flex; align-items: baseline; gap: 6px; white-space: nowrap; }
.chart-now-label { font-size: 11px; color: var(--ink-3); }
.chart-now-value { font-family: var(--font-data); font-size: 17px; font-weight: 600; }

.chart-legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 8px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); }
.legend-val { font-family: var(--font-data); font-weight: 600; color: var(--ink-1); font-size: 12px; }
.legend-key { width: 14px; height: 2px; border-radius: 1px; display: inline-block; }
.legend-key.dashed {
  background: none !important;
  border-top: 2px dashed currentColor; height: 0;
}
.legend-key.band { height: 9px; border-radius: 2px; opacity: 0.28; }

.fc-controls {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding-bottom: 10px; margin-bottom: 6px;
  border-bottom: 1px solid var(--grid);
  font-size: 13px;
}
.fc-controls label { color: var(--ink-2); }
.fc-controls select {
  font: inherit; font-size: 12.5px; color: var(--ink-1);
  background: var(--surface-1); border: 1px solid var(--baseline);
  border-radius: 7px; padding: 5px 8px; max-width: 190px;
}
#fc-note { margin-left: auto; text-align: right; }

.chart-body { position: relative; margin-top: 8px; }
.chart-body canvas { display: block; width: 100%; }
.chart-empty {
  display: flex; align-items: center; justify-content: center;
  height: 180px; color: var(--ink-3); font-size: 13px;
  border: 1px dashed var(--grid); border-radius: 10px; text-align: center; padding: 12px;
}

.chart-tip {
  position: absolute; pointer-events: none; z-index: 10;
  background: var(--surface-1); border: 1px solid var(--baseline);
  border-radius: 9px; padding: 8px 11px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.13);
  min-width: 130px; display: none;
}
.chart-tip .tip-date { font-size: 11.5px; color: var(--ink-3); margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 7px; font-size: 12.5px; padding: 1.5px 0; }
.tip-key { width: 12px; height: 2px; border-radius: 1px; flex: none; }
.tip-name { color: var(--ink-2); }
.tip-val { margin-left: auto; font-family: var(--font-data); font-weight: 600; color: var(--ink-1); padding-left: 10px; }

.chart-table-wrap { max-height: 260px; overflow: auto; margin-top: 8px; border: 1px solid var(--grid); border-radius: 10px; }
.chart-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.chart-table th, .chart-table td { padding: 6px 12px; text-align: right; font-variant-numeric: tabular-nums; }
.chart-table th:first-child, .chart-table td:first-child { text-align: left; }
.chart-table thead th {
  position: sticky; top: 0; background: var(--surface-1);
  color: var(--ink-2); font-weight: 500; border-bottom: 1px solid var(--grid);
}
.chart-table tbody tr:nth-child(even) { background: color-mix(in srgb, var(--grid) 28%, transparent); }

/* ── signal table ───────────────────────────────────── */

.table-wrap { overflow-x: auto; }
.signal-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.signal-table th, .signal-table td { padding: 9px 12px; text-align: left; vertical-align: top; }
.signal-table thead th {
  color: var(--ink-2); font-weight: 500; font-size: 12.5px;
  border-bottom: 1px solid var(--baseline); white-space: nowrap;
}
.signal-table tbody tr { border-bottom: 1px solid var(--grid); }
.signal-table tbody tr:last-child { border-bottom: none; }
.signal-table td.num { font-family: var(--font-data); font-variant-numeric: tabular-nums; text-align: right; font-weight: 600; }
.signal-table th.num { text-align: right; }
.signal-table .sig-value { font-family: var(--font-data); font-variant-numeric: tabular-nums; white-space: nowrap; }
.signal-table .sig-reason { color: var(--ink-2); max-width: 60ch; }
.signal-table .sig-cat { color: var(--ink-3); font-size: 12px; white-space: nowrap; }

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
  padding: 2px 9px; border-radius: 999px; border: 1px solid var(--border);
}
.chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; }
.chip.bull { color: var(--bull); }  .chip.bull::before { background: var(--bull); }
.chip.bear { color: var(--bear); }  .chip.bear::before { background: var(--bear); }
.chip.flat { color: var(--ink-2); } .chip.flat::before { background: var(--baseline); }

/* ── exit card (โซนขายทำกำไร) ───────────────────────── */

.exit-grid {
  display: grid; grid-template-columns: minmax(260px, 1fr) minmax(320px, 1.6fr);
  gap: 22px; align-items: start;
}
.trigger-row { padding: 10px 0; border-bottom: 1px solid var(--grid); }
.trigger-row:last-child { border-bottom: none; }
.trigger-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.trigger-label { font-weight: 600; font-size: 13.5px; }
.trigger-row.hit .trigger-label { color: var(--bear); }
.trigger-val { font-size: 12.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.trigger-track {
  height: 6px; border-radius: 999px; margin-top: 7px;
  background: color-mix(in srgb, var(--grid) 60%, transparent);
  overflow: hidden;
}
.trigger-fill { height: 100%; border-radius: 999px; background: var(--series-1); transition: width 0.4s; }
.trigger-fill.near { background: var(--ma-fast); }
.trigger-fill.hit { background: var(--bear); }
.trigger-hint { font-size: 12px; color: var(--ink-3); margin-top: 6px; }
#ladder-note { margin-top: 10px; }

/* slider เลื่อนเส้น M2 (การ์ด Global M2) */
.fc-controls input[type="range"] { accent-color: var(--series-2); width: 150px; }
#gm2-note { margin-left: auto; text-align: right; }

@media (max-width: 960px) {
  .exit-grid { grid-template-columns: 1fr; }
}

/* ── method / footer ────────────────────────────────── */

.method p { margin-top: 8px; color: var(--ink-2); max-width: 96ch; }
.weight-list, .source-list { margin: 10px 0 0; padding-left: 20px; color: var(--ink-2); max-width: 110ch; }
.weight-list li, .source-list li { margin-top: 4px; }
.source-list strong { color: var(--ink-1); font-weight: 600; }
.disclaimer {
  margin-top: 16px !important; padding: 12px 14px;
  border: 1px solid var(--grid); border-radius: 10px;
  font-size: 12.5px; color: var(--ink-3) !important;
}

.foot {
  max-width: 2400px; margin: 0 auto; padding: 0 clamp(16px, 2vw, 44px) 34px;
  display: flex; justify-content: space-between; gap: 12px;
  color: var(--ink-3); font-size: 12.5px;
}

/* ── responsive ─────────────────────────────────────── */

/* จอแคบกว่า 2 คอลัมน์: การ์ด span2 ต้องไม่ดันให้เกิดคอลัมน์ผี */
@media (max-width: 960px) {
  .charts-grid { grid-template-columns: 1fr; }
  .span2 { grid-column: span 1; }
}

@media (max-width: 900px) {
  .hero-num { font-size: 44px; }
  .verdict h1 { font-size: 24px; }
  .cat-row { grid-template-columns: 112px 1fr 52px; gap: 10px; }
  .topbar { padding: 12px 16px; }
  main { padding: 16px 16px 32px; }
  .tape-labels { font-size: 10.5px; }
}
