/* ============================================================
   Charts — 圖表元件樣式
   結構由 shared/js/charts.js 產生的 inline SVG，這裡只定義樣式。

   原則：
   - Dashboard 類圖表自己刻 inline SVG，不引外部套件
   - 例外是 K 線：使用者對 K 線的操作預期（拖曳、縮放、時間尺度）已被
     市面套件定型，自己刻只會做出四不像 → 用 lightweight-charts，
     檔案 vendor 進 shared/vendor/，file:// 離線照樣能開
   - 顏色一律走 tokens.css，同一資產在所有圖表是同一個顏色
   - 格線極淡、無外框，與整體弱框線的設計一致
   - 色塊圓角化、相鄰色塊之間留 --chart-seg-gap 的細縫，
     不靠白色描邊分隔（描邊在深色背景上會露餡）
   - 圖上不放任何 emoji；漲跌用顏色 + 正負號表示
   ============================================================ */

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }

/* ── 座標軸與格線 ──────────────────────────────── */
.c-grid { stroke: var(--chart-grid); stroke-width: 1; shape-rendering: crispEdges; }
.c-axis-line { stroke: var(--border); stroke-width: 1; shape-rendering: crispEdges; }
.c-axis-text {
  fill: var(--chart-axis);
  font-family: var(--font);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
}
.c-axis-y { text-anchor: end; }
.c-axis-x { text-anchor: middle; }

/* ── 折線 / 面積 ───────────────────────────────── */
.c-line { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.c-area { stroke: none; opacity: .14; }
.c-dot  { stroke: var(--bg-panel); stroke-width: 2; }

/* 游標十字線與最近點 */
.c-crosshair { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 3 3; }
.c-hover-dot { stroke: var(--bg-panel); stroke-width: 2; }
.c-hit { fill: transparent; cursor: crosshair; }

/* ── 長條 ──────────────────────────────────────── */
.c-bar { transition: opacity .12s; }   /* 圓角由 charts.js 的 path 產生 */
.chart:hover .c-bar { opacity: .75; }
.chart .c-bar:hover { opacity: 1; }

/* ── K 線 ──────────────────────────────────────────
   .c-wick / .c-body 是 charts.js 內建的簡版 K 線（fallback 用）。
   交易模組的主 K 線由 lightweight-charts 畫在 canvas 上，
   canvas 內部的顏色不吃 CSS，charts.js 會把 token 解析成實色再傳進去。 */
.c-wick { stroke-width: 1; shape-rendering: crispEdges; }
.c-body { shape-rendering: crispEdges; }

.kline { display: flex; flex-direction: column; gap: var(--sp-3); }

/* 工具列：時間尺度切換 + 重設縮放 */
.kline-bar { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.kline-tf {
  display: inline-flex;
  padding: 2px;
  border-radius: var(--r);
  background: var(--bg-sunken);
}
.kline-tf button {
  padding: 4px var(--sp-3);
  border: 0;
  border-radius: calc(var(--r) - 3px);
  background: transparent;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.kline-tf button:hover { color: var(--text); }
.kline-tf button.is-on {
  background: var(--bg-panel);
  color: var(--text);
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}

/* OHLC 讀數：跟著十字線走，市面上的 K 線套件都放在左上角 */
.kline-ohlc {
  display: flex;
  gap: var(--sp-3);
  flex-wrap: wrap;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-height: 20px;
}
.kline-ohlc b { font-weight: 500; color: var(--text); }
.kline-ohlc .is-up   { color: var(--chart-up); }
.kline-ohlc .is-down { color: var(--chart-down); }

.kline-canvas { position: relative; width: 100%; }
.kline-hint { margin-left: auto; font-size: var(--fs-xs); color: var(--text-subtle); }

/* ── 圓環 ──────────────────────────────────────── */
/* 每段是 charts.js 產生的圓角環狀扇形 path（四角倒圓、段間留縫），顏色走 inline style */
.c-arc { transition: opacity .12s; }
.chart:hover .c-arc { opacity: .72; }
.chart .c-arc:hover { opacity: 1; }
.c-center-label {
  fill: var(--text-subtle);
  font-family: var(--font);
  font-size: 11px;
  text-anchor: middle;
}
.c-center-value {
  fill: var(--text);
  font-family: var(--font);
  font-size: 18px;
  font-weight: 600;
  text-anchor: middle;
  font-variant-numeric: tabular-nums;
}

/* ── 量表（風險分、額度使用率、健康度）─────────── */
.c-gauge-track { fill: none; stroke: var(--chart-track); stroke-linecap: round; }
.c-gauge-value { fill: none; stroke-linecap: round; }

/* ── 圖例 ──────────────────────────────────────── */
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-4);
  margin-top: var(--sp-3);
  font-size: var(--fs-sm);
}
.chart-legend-item { display: flex; align-items: center; gap: var(--sp-2); }
.chart-legend-swatch { width: 8px; height: 8px; border-radius: 999px; flex: 0 0 8px; }
.chart-legend-label { color: var(--text-muted); }
.chart-legend-value { color: var(--text); font-variant-numeric: tabular-nums; }

/* 直式圖例（放在圓環旁邊時）*/
.chart-legend-stack { flex-direction: column; gap: var(--sp-2); margin-top: 0; }
.chart-legend-stack .chart-legend-item { width: 100%; }
.chart-legend-stack .chart-legend-value { margin-left: auto; }

/* 圓環 + 圖例並排 */
.chart-donut-row {
  display: flex;
  align-items: center;
  gap: var(--sp-6);
}
.chart-donut-row .chart { flex: 0 0 auto; width: auto; }
.chart-donut-row .chart-legend { flex: 1; min-width: 0; }

/* ── Tooltip ───────────────────────────────────── */
.chart-tooltip {
  position: fixed;
  z-index: var(--z-dropdown);
  pointer-events: none;
  padding: var(--sp-2) var(--sp-3);
  background: var(--bg-panel);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-md);
  font-size: var(--fs-sm);
  white-space: nowrap;
  opacity: 0;
  transition: opacity .1s;
}
.chart-tooltip.is-on { opacity: 1; }
.chart-tooltip-title { color: var(--text-subtle); font-size: var(--fs-xs); margin-bottom: 2px; }
.chart-tooltip-row { display: flex; align-items: center; gap: var(--sp-2); }
.chart-tooltip-row .chart-legend-swatch { width: 7px; height: 7px; flex: 0 0 7px; }
.chart-tooltip-val { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 500; }

/* ── 單行比例條（餘額組成）───────────────────────
   用 gap 分隔色塊而不是白色描邊：描邊會被誤讀成資料的一部分。 */
.chart-meter {
  display: flex;
  gap: var(--chart-seg-gap);
  height: 10px;
}
.chart-meter-seg {
  height: 100%;
  border-radius: var(--chart-seg-r);   /* 不用 999px：小占比的段會圓成一顆藥丸 */
  min-width: 3px;                      /* 極小占比也要看得到 */
}
.chart-meter-lg { height: 14px; }

/* ── 橫條排行（案件分布、子帳戶比較）───────────── */
.chart-hbars { display: flex; flex-direction: column; gap: var(--sp-3); }
.chart-hbar { display: flex; align-items: center; gap: var(--sp-3); }
.chart-hbar-label {
  flex: 0 0 40%;
  min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}
/* track 與 fill 都是 <span>，必須 blockify，否則 fill 的 width 不生效 */
.chart-hbar-track {
  display: block;
  flex: 1;
  height: 10px;
  border-radius: var(--chart-seg-r);
  background: var(--chart-track);
}
.chart-hbar-fill { display: block; height: 100%; border-radius: var(--chart-seg-r); min-width: 4px; }
.chart-hbar-value {
  flex: 0 0 auto;
  min-width: 42px;
  text-align: right;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}

/* ── Sparkline（表格／Stat 卡內嵌）─────────────── */
.sparkline { display: inline-block; vertical-align: middle; }
.sparkline svg { display: block; overflow: visible; }

/* 漲跌文字：用顏色與正負號，不用箭頭圖示 */
.trend-up   { color: var(--chart-up); font-variant-numeric: tabular-nums; }
.trend-down { color: var(--chart-down); font-variant-numeric: tabular-nums; }
.trend-flat { color: var(--text-subtle); font-variant-numeric: tabular-nums; }

/* ── 空狀態 ────────────────────────────────────── */
.chart-empty {
  display: grid;
  place-items: center;
  min-height: 120px;
  color: var(--text-subtle);
  font-size: var(--fs-sm);
  background: var(--bg-sunken);
  border-radius: var(--r);
}
