:root {
  color-scheme: dark;
  --bg: #101314;
  --panel: #171b1d;
  --panel-2: #1d2325;
  --line: #30383b;
  --text: #edf2ef;
  --muted: #99a5a2;
  --green: #32d082;
  --red: #ff6b6b;
  --amber: #f4ba4e;
  --blue: #56a7ff;
  --cyan: #4bd3d0;
  --pink: #f277c6;
  --violet: #a88cff;
  --orange: #ff945f;
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.app-shell {
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.topbar-actions {
  display: flex;
  align-items: stretch;
  gap: 10px;
}

.instrument-picker {
  display: grid;
  gap: 5px;
  min-width: 210px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
}

.instrument-picker span {
  color: var(--muted);
  font-size: 12px;
}

.instrument-picker select {
  width: 100%;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 8px;
  font: inherit;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
  font-weight: 720;
}

h2 {
  font-size: 18px;
  line-height: 1.2;
  font-weight: 680;
}

p,
time {
  color: var(--muted);
  font-size: 13px;
}

.status-panel {
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 8px;
  align-items: center;
  justify-content: end;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  min-width: 228px;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: var(--amber);
  box-shadow: 0 0 0 4px rgba(244, 186, 78, 0.12);
}

.status-dot.ok {
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(50, 208, 130, 0.12);
}

#statusText {
  font-size: 13px;
  font-weight: 650;
}

#latestTs {
  grid-column: 1 / -1;
  text-align: right;
}

.source-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.source-card,
.chart-section,
.funding-section,
.spread-section {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.source-card {
  padding: 16px;
}

.source-card.lagging {
  border-color: rgba(244, 186, 78, 0.58);
}

.source-card.stale {
  border-color: rgba(255, 107, 107, 0.72);
  background: linear-gradient(180deg, rgba(255, 107, 107, 0.08), rgba(23, 27, 29, 0.98));
}

.source-card header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.source-name {
  font-size: 15px;
  font-weight: 720;
}

.symbol {
  color: var(--muted);
  font-size: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.funding {
  color: var(--cyan);
  font-size: 12px;
  white-space: nowrap;
}

.category-badge {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  padding: 2px 6px;
  border: 1px solid rgba(86, 167, 255, 0.4);
  border-radius: 999px;
  color: #b9d9ff;
  background: rgba(86, 167, 255, 0.1);
  font-size: 11px;
  line-height: 1;
}

.category-badge.new-listing {
  border-color: rgba(244, 186, 78, 0.48);
  color: #f8d78a;
  background: rgba(244, 186, 78, 0.1);
}

.freshness-badge {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 11px;
  line-height: 1;
}

.freshness-badge.lagging {
  border: 1px solid rgba(244, 186, 78, 0.55);
  color: #f8d78a;
  background: rgba(244, 186, 78, 0.12);
}

.freshness-badge.stale {
  border: 1px solid rgba(255, 107, 107, 0.62);
  color: #ffb2b2;
  background: rgba(255, 107, 107, 0.14);
}

.source-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin: -6px 0 12px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.source-meta.lagging {
  color: #f8d78a;
}

.source-meta.stale {
  color: #ffb2b2;
}

.quote-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.quote-cell {
  padding: 12px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.quote-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.price {
  display: block;
  font-size: 26px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  font-weight: 760;
}

.bid .price {
  color: var(--green);
}

.ask .price {
  color: var(--red);
}

.qty {
  display: block;
  color: var(--muted);
  margin-top: 7px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.chart-section,
.funding-section,
.spread-section {
  padding: 16px;
  margin-top: 14px;
}

.funding-summary {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(460px, 1.2fr);
  gap: 16px;
  align-items: start;
  margin-bottom: 14px;
}

.funding-metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.funding-metrics > div {
  padding: 12px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.funding-metrics strong {
  display: block;
  font-size: 22px;
  line-height: 1;
  font-weight: 760;
  font-variant-numeric: tabular-nums;
}

#fundingRate {
  color: var(--cyan);
}

#fundingAnnualized {
  color: var(--amber);
}

#fundingUpdated {
  color: var(--text);
  font-size: 14px;
  line-height: 1.2;
}

.funding-chart-heading {
  margin-top: 4px;
}

.funding-chart-wrap {
  position: relative;
  width: 100%;
  height: 260px;
  border-radius: 8px;
  background: #0c0f10;
  border: 1px solid rgba(255, 255, 255, 0.05);
  overflow: hidden;
}

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

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

.range-picker {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  gap: 8px;
  min-height: 33px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
}

.range-picker span {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.range-picker select {
  min-width: 104px;
  color: var(--text);
  background: #0f1314;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 4px 7px;
  font: inherit;
  font-size: 12px;
}

.scale-chip {
  color: var(--muted);
  font-size: 12px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  white-space: nowrap;
}

.chart-wrap {
  position: relative;
  width: 100%;
  height: 420px;
  border-radius: 8px;
  background: #0c0f10;
  border: 1px solid rgba(255, 255, 255, 0.05);
  overflow: hidden;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.chart-tooltip {
  position: absolute;
  z-index: 5;
  min-width: 260px;
  max-width: min(360px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(17, 21, 22, 0.96);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  font-size: 12px;
  color: var(--text);
}

.tooltip-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-weight: 700;
}

.tooltip-swatch {
  width: 18px;
  height: 3px;
  border-radius: 999px;
  flex: 0 0 auto;
}

.tooltip-time {
  color: var(--muted);
  margin-bottom: 8px;
}

.tooltip-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 5px 12px;
}

.tooltip-grid span:nth-child(odd) {
  color: var(--muted);
}

.tooltip-grid span:nth-child(even) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.legend {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px 12px;
  margin-top: 12px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  padding: 5px 6px;
  text-align: left;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.legend-item:hover {
  color: var(--text);
  border-color: var(--line);
  background: var(--panel-2);
}

.legend-item.hidden {
  opacity: 0.45;
  text-decoration: line-through;
}

.legend-item.hidden .legend-swatch {
  background: var(--muted) !important;
}

.legend-swatch {
  width: 18px;
  height: 3px;
  border-radius: 999px;
  flex: 0 0 auto;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

th,
td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

th:first-child,
td:first-child {
  text-align: left;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

tr:last-child td {
  border-bottom: 0;
}

tr.lagging-spread td {
  background: rgba(244, 186, 78, 0.05);
}

tr.stale-spread td {
  background: rgba(255, 107, 107, 0.06);
}

.spread-warning {
  display: block;
  margin-top: 4px;
  color: var(--amber);
  font-size: 11px;
  font-weight: 600;
}

tr.stale-spread .spread-warning {
  color: #ffb2b2;
}

.positive {
  color: var(--green);
}

.negative {
  color: var(--red);
}

.empty {
  color: var(--muted);
  padding: 16px;
}

@media (max-width: 980px) {
  .topbar {
    flex-direction: column;
  }

  .topbar-actions {
    display: grid;
    width: 100%;
  }

  .status-panel {
    width: 100%;
    justify-content: start;
  }

  #latestTs {
    text-align: left;
  }

  .source-grid,
  .legend {
    grid-template-columns: 1fr;
  }

  .funding-summary,
  .funding-metrics {
    grid-template-columns: 1fr;
  }

  .section-heading {
    flex-direction: column;
  }

  .chart-controls {
    width: 100%;
    justify-content: flex-start;
  }

  .chart-wrap {
    height: 340px;
  }
}

@media (max-width: 520px) {
  .app-shell {
    width: min(100% - 20px, 1480px);
    padding-top: 14px;
  }

  h1 {
    font-size: 23px;
  }

  .quote-grid {
    grid-template-columns: 1fr;
  }

  .price {
    font-size: 22px;
  }
}
