:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --border-strong: #cfcec8;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #75746f;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e6f0fc;
  --series-1: #2a78d6;
  --track: #e6e5e1;
  --grid: #ebeae6;
  --good: #0ca30c;
  --good-ink: #006300;
  --good-soft: #e5f5e5;
  --warning: #fab219;
  --warning-ink: #8a5a00;
  --warning-soft: #fdf3dc;
  --critical: #d03b3b;
  --critical-soft: #fbe9e9;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --radius: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --border: #2e2e2b;
    --border-strong: #3d3d39;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #97968e;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-soft: #16263a;
    --series-1: #3987e5;
    --track: #2f2f2c;
    --grid: #2a2a27;
    --good-ink: #4fd14f;
    --good-soft: #13291a;
    --warning-ink: #fab219;
    --warning-soft: #2d2412;
    --critical-soft: #331a1a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --border: #2e2e2b;
  --border-strong: #3d3d39;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #97968e;
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-soft: #16263a;
  --series-1: #3987e5;
  --track: #2f2f2c;
  --grid: #2a2a27;
  --good-ink: #4fd14f;
  --good-soft: #13291a;
  --warning-ink: #fab219;
  --warning-soft: #2d2412;
  --critical-soft: #331a1a;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text-primary);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h2 { font-size: 15px; }
h3 { font-size: 13px; }
.num, .tile-value, .mono { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.grow { flex: 1; }
.min0 { min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.mt { margin-top: 20px; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface-1);
  cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
:root[data-theme="dark"] .btn-primary:hover { color: #0b0b0b; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-secondary); }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn-block { width: 100%; }
.btn-danger { color: var(--critical); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; border: 0; background: transparent;
  display: inline-grid; place-items: center; cursor: pointer; color: var(--text-secondary);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text-primary); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
button:focus-visible, input:focus-visible, a:focus-visible, summary:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--text-secondary); font-weight: 500; }
.field input {
  height: 38px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border-strong);
  background: var(--surface-1); color: var(--text-primary); font-size: 14px; min-width: 0;
}
.field.inline input { width: 80px; height: 36px; }
.form-error { color: var(--critical); font-size: 13px; min-height: 0; margin: 8px 0 0; }
.form-error:empty { display: none; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px 24px; display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow);
}
.api-details summary { cursor: pointer; color: var(--text-muted); font-size: 12px; }
.api-details .field { margin-top: 10px; }

/* ---------- top bar ---------- */
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 15px; white-space: nowrap; }
.brand-lg { font-size: 18px; justify-content: center; margin-bottom: 4px; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  -webkit-mask: none; position: relative; flex: none;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 0;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 21l5-6 4 3 7-8' stroke='white' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/100% no-repeat;
}
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; cursor: pointer;
  color: var(--text-secondary); font-weight: 500; white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); }
.tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent-ink); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.live-dot { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); margin-right: 6px; white-space: nowrap; }
.live-dot i { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.live-dot.ok i { background: var(--good); box-shadow: 0 0 0 3px var(--good-soft); }
.live-dot.warn i { background: var(--warning); }
.live-dot.bad i { background: var(--critical); }

.filterbar { padding: 12px 20px 0; max-width: 1400px; margin: 0 auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface-1);
  cursor: pointer; font-size: 13px; color: var(--text-secondary);
}
.chip img { width: 16px; height: 16px; border-radius: 50%; }
.chip[aria-checked="true"] { background: var(--text-primary); color: var(--surface-1); border-color: var(--text-primary); }
.chip .x { border: 0; background: none; cursor: pointer; color: inherit; padding: 0 0 0 2px; font-size: 14px; line-height: 1; }

main { max-width: 1400px; margin: 0 auto; padding: 16px 20px 40px; }

/* ---------- cards & tiles ---------- */
.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.subhead { margin: 20px 0 8px; color: var(--text-secondary); }
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.tile-label { font-size: 12px; color: var(--text-secondary); }
.tile-value { font-size: 24px; font-weight: 600; margin-top: 2px; letter-spacing: -.02em; }
.tile-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.tiles-sm { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.tiles-sm .tile { padding: 10px 12px; box-shadow: none; background: var(--surface-2); border-color: transparent; }
.tiles-sm .tile-value { font-size: 17px; }

.live-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }

/* ---------- lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 10px 8px; border-radius: 10px;
  cursor: pointer; min-width: 0;
}
.row:hover { background: var(--surface-2); }
.list > li + li { border-top: 1px solid var(--border); }
.list > li { border-radius: 10px; }
.row .main { flex: 1; min-width: 0; }
.row .title { display: flex; align-items: baseline; gap: 6px; min-width: 0; font-weight: 600; }
.row .title .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .title .sym { color: var(--text-muted); font-weight: 500; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-shrink: 1000; min-width: 0; }
.row .title .name { flex-shrink: 1; min-width: 3ch; }
.row .meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 8px; flex-wrap: wrap; margin-top: 1px; }
.row .right { text-align: right; flex: none; }
.row .right .big { font-weight: 600; font-variant-numeric: tabular-nums; }
.rank { width: 20px; text-align: right; color: var(--text-muted); font-variant-numeric: tabular-nums; font-size: 12px; flex: none; }
.logo {
  width: 36px; height: 36px; border-radius: 50%; background: var(--surface-2); object-fit: cover; flex: none;
  display: inline-grid; place-items: center; font-size: 12px; font-weight: 600; color: var(--text-secondary);
}
.logo-lg { width: 48px; height: 48px; }
.fresh { animation: fresh 2.4s ease-out; }
@keyframes fresh { from { background: var(--accent-soft); } to { background: transparent; } }
.empty { padding: 28px 8px; text-align: center; color: var(--text-muted); }

.badge {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 6px;
  font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--text-secondary); white-space: nowrap;
}
.badge-good { background: var(--good-soft); color: var(--good-ink); }
.badge-warn { background: var(--warning-soft); color: var(--warning-ink); }
.badge-accent { background: var(--accent-soft); color: var(--accent-ink); }

/* progress */
.prog { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.prog-track { position: relative; flex: 1; height: 6px; border-radius: 3px; background: var(--track); overflow: visible; }
.prog-fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 3px; background: var(--series-1); }
.prog-tick { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--surface-1); }
.prog-pct { font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; width: 38px; text-align: right; }
.prog-lg .prog-track { height: 10px; border-radius: 5px; }
.prog-lg .prog-fill { border-radius: 5px; }
.prog-caption { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); margin-top: 6px; }
.row:hover .prog-tick { background: var(--surface-2); }

/* ---------- pairs tab ---------- */
.pair-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr)); gap: 16px; }
.pair-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.pair-head .logo { width: 32px; height: 32px; }
.pair-flags { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.stat-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
.stat-row .tile { padding: 10px 12px; box-shadow: none; background: var(--surface-2); border-color: transparent; }
.stat-row .tile-value { font-size: 18px; }

/* ---------- charts ---------- */
.chart { position: relative; width: 100%; min-height: 220px; }
.chart.chart-bars { min-height: 150px; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .axis text { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--border-strong); stroke-width: 1; }
.chart .ref { stroke: var(--text-secondary); stroke-width: 1; stroke-dasharray: 4 4; }
.chart .ref-label { fill: var(--text-secondary); font-size: 11px; font-weight: 600; }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--series-1); opacity: .08; }
.chart .bar { fill: var(--series-1); }
.chart .bar.dim { opacity: .45; }
.chart .hair { stroke: var(--text-muted); stroke-width: 1; }
.chart .dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.chart .empty { position: absolute; inset: 0; display: grid; place-items: center; }
.tip {
  position: absolute; pointer-events: none; z-index: 5; background: var(--surface-1); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 16px rgba(0,0,0,.12); white-space: nowrap;
  transform: translate(-50%, calc(-100% - 12px));
}
.tip b { font-variant-numeric: tabular-nums; }
.tip .t { color: var(--text-muted); margin-bottom: 2px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
.data-table th, .data-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: right; }
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table th { color: var(--text-secondary); font-weight: 600; position: sticky; top: 0; background: var(--surface-1); }
.table-wrap { overflow: auto; max-height: 320px; }

/* ---------- alerts ---------- */
.seg { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.seg label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.ev-icon { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); flex: none; font-size: 16px; }

/* ---------- settings ---------- */
.settings-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.add-pair { position: relative; display: flex; gap: 8px; align-items: flex-end; margin: 12px 0 4px; }
.suggest {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 10; list-style: none; margin: 4px 0 0; padding: 4px;
  background: var(--surface-1); border: 1px solid var(--border-strong); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
  max-height: 280px; overflow: auto;
}
.suggest li { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer; }
.suggest li[aria-selected="true"], .suggest li:hover { background: var(--surface-2); }
.suggest img, .suggest .logo { width: 24px; height: 24px; }
.pairs-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.pairs-table th { font-size: 12px; color: var(--text-secondary); font-weight: 500; text-align: left; padding: 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.pairs-table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.pair-cell { display: flex; align-items: center; gap: 10px; min-width: 160px; }
.pair-cell .logo { width: 28px; height: 28px; }
.pair-cell input { width: 100%; max-width: 140px; border: 1px solid transparent; background: transparent; border-radius: 6px; padding: 4px 6px; font-weight: 600; }
.pair-cell input:hover, .pair-cell input:focus { border-color: var(--border-strong); background: var(--surface-1); }
.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--border-strong); transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.ms-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.sys { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; font-size: 13px; }
.sys dt { color: var(--text-secondary); }
.sys dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- drawer ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); z-index: 50;
  background: var(--surface-1); border-left: 1px solid var(--border); display: flex; flex-direction: column;
  box-shadow: -8px 0 32px rgba(0,0,0,.12);
}
.drawer-head { display: flex; align-items: center; gap: 12px; padding: 16px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { font-size: 18px; }
.drawer-body { padding: 16px; overflow-y: auto; flex: 1; }
.drawer-body > * + * { margin-top: 16px; }
.drawer-body .card-head { margin-bottom: 0; }
.link-row { display: flex; gap: 6px; flex-wrap: wrap; }
.link-row a, .link-row button {
  display: inline-flex; align-items: center; height: 30px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface-1); color: var(--text-primary);
  text-decoration: none; font-size: 12px; font-weight: 500; cursor: pointer;
}
.link-row a:hover, .link-row button:hover { background: var(--surface-2); }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60;
  background: var(--text-primary); color: var(--surface-1); padding: 10px 16px; border-radius: 10px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: calc(100vw - 32px);
}

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .live-grid, .settings-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  .tabs { order: 3; width: 100%; }
  .live-dot span { display: none; }
  .filterbar { padding: 12px 16px 0; }
  main { padding: 12px 16px 32px; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile-value { font-size: 20px; }
  .stat-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .card { padding: 12px; }
  .pairs-table thead { display: none; }
  .pairs-table, .pairs-table tbody, .pairs-table tr, .pairs-table td { display: block; }
  .pairs-table tr { border-bottom: 1px solid var(--border); padding: 8px 0; }
  .pairs-table td { border: 0; padding: 6px 4px; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
  .pairs-table td[data-label]::before { content: attr(data-label); color: var(--text-secondary); font-size: 13px; }
  .drawer { width: 100vw; border-left: 0; }
  .tiles-sm { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------- peaks ---------- */
.nomargin { margin: 2px 0 0; }
.peaks-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.pk-pair { position: relative; flex: 1 1 240px; display: flex; }
.field select {
  height: 38px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border-strong);
  background: var(--surface-1); color: var(--text-primary); font-size: 14px;
}
.pk-chips { margin: 12px 0 4px; }
.pk-bar { position: relative; height: 6px; border-radius: 3px; background: var(--track); margin-top: 6px; }
.pk-bar i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 3px; background: var(--series-1); min-width: 2px; }
.pk-drop { font-weight: 600; font-variant-numeric: tabular-nums; }
.pk-now { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
@media (max-width: 720px) {
  .peaks-controls .field:not(.grow) { flex: 1 1 calc(50% - 6px); }
  .peaks-controls .field select { width: 100%; }
}
