/* ── Tokens ─────────────────────────────────────── */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #7a7873;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --focus: #2a78d6;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 4px 16px rgba(11, 11, 11, 0.04);

  /* 漲跌語意色（文字用） */
  --red-text: #c4302f;
  --green-text: #006300;
  /* 漲跌熱度 ramp：1 淺 → 4 深，0 為中性 */
  --neutral: #ecebe6;
  --red-1: #f7dcda; --red-2: #f0aeab; --red-3: #dd6461; --red-4: #b42d2b;
  --green-1: #d6ecd5; --green-2: #9fd29e; --green-3: #4ba552; --green-4: #1d7a2b;
  --on-strong: #ffffff;

  /* 分類色（歷史趨勢線），依固定順序使用 */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4;

  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #8f8d86;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --accent-wash: rgba(57, 135, 229, 0.16); --focus: #5598e7;
    --shadow: none;
    --red-text: #ef7a78; --green-text: #3fbf4a;
    --neutral: #2c2c2a;
    --red-1: #3d2322; --red-2: #6b2c2b; --red-3: #a33634; --red-4: #d9504e;
    --green-1: #1d3320; --green-2: #23502a; --green-3: #2b7d36; --green-4: #3fa84b;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #8f8d86;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-wash: rgba(57, 135, 229, 0.16); --focus: #5598e7;
  --shadow: none;
  --red-text: #ef7a78; --green-text: #3fbf4a;
  --neutral: #2c2c2a;
  --red-1: #3d2322; --red-2: #6b2c2b; --red-3: #a33634; --red-4: #d9504e;
  --green-1: #1d3320; --green-2: #23502a; --green-3: #2b7d36; --green-4: #3fa84b;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
}

/* 漲跌色對應：預設紅漲綠跌（台灣慣例），可切換 */
:root {
  --up-text: var(--red-text); --down-text: var(--green-text);
  --up-1: var(--red-1); --up-2: var(--red-2); --up-3: var(--red-3); --up-4: var(--red-4);
  --down-1: var(--green-1); --down-2: var(--green-2); --down-3: var(--green-3); --down-4: var(--green-4);
}
:root[data-updown="green-up"] {
  --up-text: var(--green-text); --down-text: var(--red-text);
  --up-1: var(--green-1); --up-2: var(--green-2); --up-3: var(--green-3); --up-4: var(--green-4);
  --down-1: var(--red-1); --down-2: var(--red-2); --down-3: var(--red-3); --down-4: var(--red-4);
}

/* ── Base ───────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
svg { display: block; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 16px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; }
.muted { color: var(--muted); font-weight: 400; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.up { color: var(--up-text); }
.down { color: var(--down-text); }
.flat { color: var(--ink-2); }

/* ── Top bar ────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px max(16px, calc((100vw - 1280px) / 2));
  background: color-mix(in srgb, var(--page) 85%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 16px; }
.brand svg { width: 22px; height: 22px; fill: var(--accent); }
.controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.field { display: flex; align-items: center; gap: 8px; }
.field-label { color: var(--muted); font-size: 13px; }
select {
  height: 34px; padding: 0 30px 0 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5 6 7.5 9 4.5' fill='none' stroke='%23898781' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 10px center / 12px;
  appearance: none; cursor: pointer; font-variant-numeric: tabular-nums;
}
.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border-radius: 9px; border: 1px solid var(--border);
}
.seg button {
  border: 0; background: none; padding: 4px 12px; height: 28px;
  border-radius: 6px; color: var(--ink-2); cursor: pointer; font-size: 13px; white-space: nowrap;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: var(--ink-2); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ── Layout ─────────────────────────────────────── */
main { max-width: 1280px; margin: 0 auto; padding: 20px 16px 40px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.card, .stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card { padding: 20px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px 16px; flex-wrap: wrap; margin-bottom: 16px; }
.card-head h2 { font-size: 16px; font-weight: 650; }
.card-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.card-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.foot { color: var(--muted); font-size: 12px; text-align: center; padding-top: 8px; }

.search {
  display: flex; align-items: center; gap: 8px; height: 34px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  width: 240px; max-width: 100%;
}
.search:focus-within { border-color: var(--focus); box-shadow: 0 0 0 3px var(--accent-wash); }
.search svg { width: 16px; height: 16px; flex: none; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.search input { border: 0; outline: 0; background: none; width: 100%; min-width: 0; font-size: 13px; }
.search input::placeholder { color: var(--muted); }

/* ── Alert banner（狀態色只用在圖示與邊條，文字維持墨色） ── */
.alert {
  --tone: var(--accent);
  display: flex; gap: 12px; align-items: flex-start;
  padding: 14px 16px 14px 14px;
  background: color-mix(in srgb, var(--tone) 8%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--tone) 35%, transparent);
  border-left: 4px solid var(--tone);
  border-radius: var(--radius);
}
.alert[data-level="warning"] { --tone: #fab219; }
.alert[data-level="critical"] { --tone: #d03b3b; }
.alert-icon {
  flex: none; width: 22px; height: 22px; border-radius: 50%; margin-top: 1px;
  display: grid; place-items: center;
  background: var(--tone); color: #fff; font-weight: 700; font-size: 14px; line-height: 1;
}
.alert[data-level="warning"] .alert-icon { color: #0b0b0b; }
.alert[data-level="info"] .alert-icon {
  background: none; border: 2px solid color-mix(in srgb, var(--tone) 30%, transparent);
  border-top-color: var(--tone); animation: spin .8s linear infinite;
}
.alert-title { font-weight: 600; font-size: 14px; }
.alert-detail { color: var(--ink-2); font-size: 13px; margin-top: 2px; }
.alert-detail:empty { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Stat tiles ─────────────────────────────────── */
.stats { display: grid; grid-template-columns: minmax(0, 1.3fr) repeat(3, minmax(0, 1fr)); gap: 16px; }
.stat { padding: 18px 20px; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.stat-label { font-size: 13px; font-weight: 500; color: var(--ink-2); }
.hero { font-size: 48px; font-weight: 650; line-height: 1.1; letter-spacing: -0.02em; }
.hero-of { font-size: 20px; color: var(--muted); font-weight: 500; letter-spacing: 0; }
.stat-value { font-size: 22px; font-weight: 650; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-delta { font-size: 15px; font-weight: 600; }
.stat-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.breadth { display: flex; gap: 2px; height: 8px; margin: 10px 0 4px; }
.breadth span { border-radius: 4px; min-width: 4px; }

/* ── Heat grid ──────────────────────────────────── */
.heat { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 4px; }
.tile {
  position: relative; display: flex; flex-direction: column; justify-content: space-between;
  min-height: 76px; padding: 8px 10px; border: 0; border-radius: 8px;
  text-align: left; cursor: pointer; color: var(--ink);
  transition: transform .12s ease, box-shadow .12s ease;
}
.tile:hover { transform: translateY(-1px); box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--ink-2); z-index: 1; }
.tile .t-name { font-size: 12px; font-weight: 600; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tile .t-chg { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .t-lead { font-size: 11px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile.strong { color: var(--on-strong); }
.h-0 { background: var(--neutral); }
.h-u1 { background: var(--up-1); } .h-u2 { background: var(--up-2); } .h-u3 { background: var(--up-3); } .h-u4 { background: var(--up-4); }
.h-d1 { background: var(--down-1); } .h-d2 { background: var(--down-2); } .h-d3 { background: var(--down-3); } .h-d4 { background: var(--down-4); }
.legend-scale { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.legend-scale .sw { display: flex; gap: 2px; }
.legend-scale .sw i { width: 22px; height: 10px; border-radius: 3px; display: block; }

/* ── Table ──────────────────────────────────────── */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th {
  position: sticky; top: 0; background: var(--surface);
  text-align: left; font-weight: 500; color: var(--muted); font-size: 12px;
  border-bottom: 1px solid var(--grid); padding: 0; white-space: nowrap;
}
.tbl th button {
  all: unset; box-sizing: border-box; display: flex; align-items: center; gap: 4px;
  width: 100%; padding: 8px 10px; cursor: pointer;
}
.tbl th.num button { justify-content: flex-end; }
.tbl th button:hover { color: var(--ink); }
.tbl th button:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.tbl th[aria-sort] button { color: var(--ink); font-weight: 600; }
.tbl th[aria-sort="ascending"] button::after { content: "↑"; }
.tbl th[aria-sort="descending"] button::after { content: "↓"; }
.tbl td { padding: 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; white-space: nowrap; }
.tbl tbody tr { cursor: pointer; }
.tbl tbody tr:hover td { background: var(--accent-wash); }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl .rank { color: var(--muted); font-size: 12px; }
.tbl .rank-move { font-size: 11px; margin-left: 4px; }
.tbl .sector-name { font-weight: 600; color: var(--ink); }
.tbl .chg { font-weight: 600; }
.tbl-compact td { padding: 8px 8px; }
.tbl-compact .coin-name { display: inline-block; max-width: 96px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; margin-left: 6px; font-size: 12px; color: var(--muted); }
.tbl-compact tbody tr { cursor: default; }
.tbl tr.dim td { color: var(--muted); }
.tbl tr.dim .chg { opacity: .7; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

.updown { display: flex; align-items: center; gap: 8px; }
.updown-bar { display: flex; gap: 2px; width: 72px; height: 6px; }
.updown-bar span { border-radius: 3px; }
.updown-txt { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.leads { display: flex; gap: 6px; }
.lead {
  display: inline-flex; align-items: baseline; gap: 4px;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2);
  font-size: 12px;
}
.lead b { font-weight: 600; color: var(--ink); }
.lead span { color: var(--muted); font-variant-numeric: tabular-nums; }
.no-lead { color: var(--muted); font-size: 12px; }

/* ── History chart ──────────────────────────────── */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px 0 8px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
  font-size: 12px; cursor: pointer; color: var(--ink-2);
}
.chip i { width: 10px; height: 3px; border-radius: 2px; background: var(--grid); }
.chip[aria-pressed="true"] { color: var(--ink); border-color: var(--axis); font-weight: 600; }
.chip:hover { background: var(--surface-2); }
.chip-add { color: var(--accent); }
.chart { position: relative; min-height: 280px; }
.chart-sm { min-height: 160px; }
.chart svg { width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .series path { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .series circle { stroke: var(--surface); stroke-width: 2; }
.chart .end-label { fill: var(--ink-2); font-size: 11px; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; }
.chart-empty {
  display: grid; place-items: center; text-align: center; gap: 4px;
  min-height: inherit; border: 1px dashed var(--grid); border-radius: 10px; padding: 24px;
  color: var(--muted); font-size: 13px;
}
.chart-empty strong { color: var(--ink-2); font-size: 14px; display: block; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend i { width: 14px; height: 2px; border-radius: 1px; }

/* ── Drawer ─────────────────────────────────────── */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .32); z-index: 40; animation: fade .15s ease; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 50;
  width: min(560px, 100vw); background: var(--surface);
  border-left: 1px solid var(--border); box-shadow: -8px 0 32px rgba(0, 0, 0, .12);
  display: flex; flex-direction: column; animation: slide .2s ease;
  overscroll-behavior: contain;
}
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 20px 20px 12px; border-bottom: 1px solid var(--grid); }
.drawer-head h2 { font-size: 20px; font-weight: 650; }
.drawer-kicker { font-size: 12px; color: var(--muted); }
.drawer-body { padding: 16px 20px 32px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.drawer-body > * { flex-shrink: 0; }
.drawer-body h3 { font-size: 13px; font-weight: 600; color: var(--ink-2); margin-top: 8px; }
.mini-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.mini { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; min-width: 0; }
.mini p:first-child { font-size: 11px; color: var(--muted); }
.mini p:last-child { font-size: 15px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.share { display: flex; gap: 2px; height: 10px; }
.share span { border-radius: 4px; min-width: 4px; }
.leaders { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.leader { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.leader-top { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
.leader-top i { width: 8px; height: 8px; border-radius: 2px; }
.leader-sym { font-size: 17px; font-weight: 700; margin-top: 2px; }
.leader-meta { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* ── Tooltip ────────────────────────────────────── */
.tooltip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, .14);
  padding: 8px 10px; font-size: 12px; min-width: 140px; max-width: 260px;
}
.tooltip .tt-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.tooltip .tt-row span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.tooltip .tt-row i { width: 10px; height: 2px; border-radius: 1px; }
.tooltip .tt-row b { font-variant-numeric: tabular-nums; }

@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateX(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ── Responsive ─────────────────────────────────── */
@media (max-width: 960px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; }
  .controls { width: 100%; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .stat { padding: 14px 16px; }
  .stat-value { font-size: 18px; }
  .tbl-compact .coin-name { display: none; }
  .stat-hero { grid-column: 1 / -1; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .heat { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
  .hide-sm { display: none; }
  .mini-stats { grid-template-columns: 1fr 1fr; }
  .leaders { grid-template-columns: 1fr; }
}
