:root {
  --bg: #0a0b0d;
  --panel: #111318;
  --panel-2: #16191f;
  --line: #23272f;
  --line-soft: #1b1f26;
  --text: #e8ebf0;
  --muted: #8b93a1;
  --accent: #6ea8fe;
  --up: #2fbf71;
  --down: #ff5d5d;
  --warn: #f2b544;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --sans: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: radial-gradient(1200px 600px at 20% -10%, #151922 0%, var(--bg) 60%) fixed;
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 7px;
  background: linear-gradient(145deg, #2a3342, #171b22);
  border: 1px solid #2d3441;
  font: 700 10px/1 var(--mono);
  color: var(--accent);
  letter-spacing: .04em;
}

.brand-name { font-weight: 700; letter-spacing: .01em; white-space: nowrap; }

.stats {
  display: flex;
  gap: 22px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1 1 auto;
}
.stats::-webkit-scrollbar { display: none; }

.stat { display: flex; flex-direction: column; gap: 1px; white-space: nowrap; }
.stat-label { font-size: 10.5px; color: var(--muted); letter-spacing: .02em; }
.stat-value { font: 600 13.5px/1.3 var(--mono); font-variant-numeric: tabular-nums; }

.live {
  display: flex; align-items: center; gap: 6px;
  font: 500 11.5px var(--mono);
  color: var(--muted);
  flex: 0 0 auto;
}
.live .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 0 transparent;
  transition: background .2s;
}
.live.on .dot { background: var(--up); box-shadow: 0 0 0 3px color-mix(in srgb, var(--up) 18%, transparent); }
.live.off .dot { background: var(--down); }

/* ---------- layout ---------- */

main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(400px, 470px);
  gap: 14px;
  padding: 14px 18px;
  flex: 1 1 auto;
  align-items: start;
}

@media (max-width: 1080px) {
  main { grid-template-columns: minmax(0, 1fr); }
  .topbar { flex-wrap: wrap; }
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-soft);
}

.card-title h2 { margin: 0; font-size: 14px; font-weight: 650; }
.card-title .subtle { margin: 3px 0 0; }
.count { color: var(--muted); font: 500 11px var(--mono); }

.subtle { color: var(--muted); font-size: 11.5px; }
.warn { color: var(--warn); font-size: 11.5px; }

.card-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

select, input, button {
  font-family: inherit;
  color: inherit;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 9px;
  font-size: 12px;
  outline: none;
}
select:focus-visible, input:focus-visible, button:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 22%, transparent);
}
input[type="search"] { width: 210px; }
button { cursor: pointer; }
button:hover { border-color: #333a46; }

.segmented { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented button {
  border: 0; border-radius: 0; background: transparent;
  padding: 6px 10px; font-size: 11.5px; color: var(--muted);
}
.segmented button + button { border-left: 1px solid var(--line-soft); }
.segmented button.active { background: #1d2430; color: var(--text); }

/* ---------- chart ---------- */

.chart-wrap { position: relative; height: 340px; padding: 6px 4px 0; }
/* 캔버스 레이아웃은 CSS 가 owns 한다 — canvas.width 를 attribute 로 키면 ResizeObserver 가 무한 증식한다 */
.chart-wrap canvas { display: block; width: 100%; height: 100%; }
#chart { width: 100%; height: 100%; display: block; }

.chart-tip {
  position: absolute;
  pointer-events: none;
  min-width: 132px;
  padding: 7px 9px;
  border-radius: 8px;
  background: #0e1116f2;
  border: 1px solid #2a3140;
  font: 11.5px/1.5 var(--mono);
  transform: translate(-50%, -112%);
  white-space: nowrap;
  z-index: 5;
}
.chart-tip b { color: var(--text); }
.chart-tip span { color: var(--muted); }

.chart-empty {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--muted); font-size: 12.5px;
  background: linear-gradient(#0b0d1000, #0b0d10aa);
}
.chart-empty[hidden] { display: none; }

.chart-foot {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 8px 14px 11px;
  border-top: 1px solid var(--line-soft);
}

/* 레전드: 복제본은 김프 선만 그린다. 레그 스와치는 아래 OHLC 줄의 태그 색과 짝을 맞춘다. */
.chart-legend {
  display: flex; flex-wrap: wrap; gap: 14px;
  padding: 6px 14px 0;
  color: var(--muted);
  font-size: 11px;
}
.legend { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.swatch.line-premium { background: var(--accent); }
.swatch.leg-domestic { background: var(--accent); }
.swatch.leg-overseas { background: #c9a2ff; }

/* 레그 OHLC 줄: 호버(또는 마지막) 봉의 시·고·저·종 */
.ohlc-strip {
  display: flex; flex-wrap: wrap; gap: 10px 18px;
  padding: 7px 14px;
  border-top: 1px solid var(--line-soft);
  font: 11px/1.6 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.ohlc-row { display: inline-flex; align-items: center; gap: 6px; }
.ohlc-time { color: #6b7484; }
.ohlc-tag { color: var(--accent); font-weight: 600; }
.ohlc-row[data-leg="overseas"] .ohlc-tag { color: #c9a2ff; }

/* ---------- 표시 설정 다이얼로그 ---------- */

dialog.settings {
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
}
dialog.settings::backdrop { background: #05070ac6; }
dialog.settings form { padding: 16px 18px 14px; display: flex; flex-direction: column; gap: 14px; }
dialog.settings h3 { margin: 0; font-size: 15px; font-weight: 700; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.setting-label { font-size: 12px; color: var(--muted); }
.setting-actions { display: flex; gap: 8px; margin-top: 2px; }

/* ---------- table ---------- */

.tabs { display: flex; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--line-soft); }
.tabs button {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  padding: 4px 9px; border-radius: 999px; font-size: 11.5px;
}
.tabs button.active { color: var(--text); background: var(--panel-2); border-color: var(--line); }

.table-scroll { max-height: 430px; overflow: auto; }

/* 좁은 화면에서는 보조 열(거래대금)을 숨겨 김프 열까지 보이게 한다 */
@media (max-width: 820px) {
  #board th:last-child,
  #board td:last-child { display: none; }
}

table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 12.5px; }

thead th {
  position: sticky; top: 0; z-index: 2;
  background: #0d0f13;
  border-bottom: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  font-weight: 550;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}
th.num, td.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sortable.active { color: var(--accent); }
th.sortable.active::after { content: " ▾"; font-size: 9px; }
th.sortable.active.asc::after { content: " ▴"; }

tbody td { padding: 7px 10px; border-bottom: 1px solid var(--line-soft); }
tbody tr:hover td { background: #14171d; }
tbody tr.fav td { background: #12161d; }

.col-star { width: 30px; }
.star {
  background: none; border: 0; padding: 0; cursor: pointer;
  color: #3d4553; font-size: 13px; line-height: 1;
}
.star.on { color: var(--warn); }

.sym { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.sym-name { font-weight: 600; font: 600 12.5px var(--mono); }
.sym-kr { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.premium { font-weight: 650; }
.flag { color: var(--warn); font-size: 10px; margin-left: 3px; vertical-align: super; }
.pos { color: var(--up); }
.neg { color: var(--down); }
.nil { color: #4c5563; }

/* 값 변동 플래시 */
@keyframes flash-up { from { background: color-mix(in srgb, var(--up) 26%, transparent); } to { background: transparent; } }
@keyframes flash-down { from { background: color-mix(in srgb, var(--down) 26%, transparent); } to { background: transparent; } }
.flash-up { animation: flash-up .7s ease-out; }
.flash-down { animation: flash-down .7s ease-out; }

/* ---------- status bar ---------- */

.statusbar {
  display: flex; gap: 16px; align-items: center;
  padding: 8px 18px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  flex-wrap: wrap;
}

/* ---------- nav ---------- */

.nav { display: flex; gap: 2px; flex: 0 0 auto; }
.nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap;
}
.nav a:hover { color: var(--text); border-color: var(--line); }
.nav a.active { color: var(--text); background: var(--panel-2); border-color: var(--line); }

/* ---------- single column pages ---------- */

main.single {
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
}

.page-title { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: .01em; }

.control-label { font-size: 11.5px; color: var(--muted); }

.table-scroll.tall { max-height: calc(100vh - 300px); min-height: 320px; }

td.empty, .empty {
  padding: 28px 12px;
  text-align: center;
  color: var(--muted);
}

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* 두 거래소 값을 한 셀에 쌓아 보여준다 (타겟 거래소는 밝게) */
.dual { display: flex; flex-direction: column; gap: 1px; line-height: 1.35; }
.dual > span { font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--muted); }
.dual > span.dual-main { color: var(--text); font-weight: 600; }
.dual-labels { display: flex; flex-direction: column; gap: 1px; line-height: 1.2; }
.dual-labels > span { font-size: 9.5px; color: #5a6273; }

.col-stack { display: flex; flex-direction: column; gap: 1px; line-height: 1.35; }
.col-stack .subtle { font-size: 10.5px; }

/* ---------- 아비트라지 ---------- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
}

.kpi {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 11px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.kpi-label { font-size: 11px; color: var(--muted); }
.kpi-value { font: 650 21px/1.1 var(--mono); font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 10.5px; color: var(--muted); }
.kpi.timer .segmented { margin-top: 4px; align-self: flex-start; }
.segmented.tiny button { padding: 3px 8px; font-size: 10.5px; }

.badge {
  display: inline-block;
  font-size: 9.5px;
  line-height: 1.5;
  padding: 0 5px;
  border-radius: 4px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.badge.perp { color: #c9a2ff; border-color: #3a2f52; background: #1b1628; }
.badge.spot { color: #7fd7c0; border-color: #24463d; background: #13211d; }

.legs { display: flex; flex-direction: column; gap: 2px; white-space: nowrap; }
.leg { display: flex; align-items: center; gap: 6px; }
.leg.muted-leg { color: var(--muted); font: 11px var(--mono); }
.leg b { color: var(--text); font-weight: 600; }
.venue { font-size: 11.5px; }

.gap { font: 650 12.5px var(--mono); font-variant-numeric: tabular-nums; }
.muted-leg, .nil { color: #6b7383; }

@media (max-width: 820px) {
  #board .col-optional { display: none; }
  .nav { order: 3; width: 100%; overflow-x: auto; scrollbar-width: none; }
  .nav::-webkit-scrollbar { display: none; }
}

/* ---------- 거래소 커버리지 ---------- */

.page-title-sm { margin: 0; font-size: 14px; font-weight: 650; }

.coverage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 8px;
  padding: 12px;
}

.venue-card {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  min-width: 0;
}
.venue-head { display: flex; align-items: center; gap: 6px; }
.venue-name { font-weight: 600; font-size: 12.5px; }
.venue-status { margin-left: auto; font: 500 10px var(--mono); color: var(--muted); }
.venue-meta { font-size: 10.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.venue-err { font-size: 10px; color: var(--warn); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.venue-card .dot { width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto; }
.dot.ok { background: var(--up); }
.dot.err { background: var(--down); }
.dot.warn { background: var(--warn); }

.tag {
  display: inline-block;
  font: 500 9.5px var(--mono);
  padding: 0 4px;
  margin-right: 3px;
  border-radius: 4px;
  border: 1px solid var(--line);
  color: var(--muted);
}

.helper-note {
  display: none;
  margin: 0 12px 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-left: 2px solid var(--warn);
  border-radius: 10px;
  background: #14161c;
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.6;
}
.helper-note.active { display: block; }
.helper-note b { color: var(--text); }
.helper-note code { color: var(--accent); font-family: var(--mono); font-size: 11px; }
.snippet {
  margin: 8px 0 0;
  padding: 9px 10px;
  border-radius: 8px;
  background: #0c0e12;
  border: 1px solid var(--line);
  color: #b9c2d0;
  font: 11px/1.55 var(--mono);
  overflow-x: auto;
  white-space: pre;
}

button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}
button.ghost:hover { color: var(--text); border-color: #333a46; }
a.ghost.link { text-decoration: none; white-space: nowrap; }

button.accent {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
}
button.accent:hover { border-color: var(--accent); }
