: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;
}

* {
  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;
}

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

.topbar,
.section-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.topbar {
  margin-bottom: 16px;
}

.page-tabs {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 0 0 16px;
  overflow-x: auto;
}

.page-tabs a {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}

.page-tabs a:hover {
  border-color: rgba(50, 208, 130, 0.55);
  background: rgba(50, 208, 130, 0.12);
}

.anchor-panel {
  scroll-margin-top: 18px;
}

.funding-panel {
  border-color: rgba(244, 186, 78, 0.42);
}

h1,
h2,
p {
  margin: 0;
}

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

h2 {
  font-size: 18px;
}

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

.status-box,
.panel,
.grid article {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
}

.instrument-picker {
  display: grid;
  gap: 5px;
  min-width: 150px;
}

.instrument-picker select {
  min-height: 38px;
  padding: 8px 34px 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
  color: var(--text);
  font: inherit;
}

.funding-controls {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.funding-controls label {
  display: grid;
  gap: 5px;
  min-width: 180px;
}

.funding-controls select {
  min-height: 34px;
  padding: 7px 30px 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
  color: var(--text);
  font: inherit;
}

.status-box {
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 8px;
  align-items: center;
  justify-content: end;
  min-width: 220px;
  padding: 10px 12px;
}

.status-box time {
  grid-column: 1 / -1;
  text-align: right;
}

.bot-control {
  display: grid;
  gap: 6px;
  min-width: 230px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.bot-control > div {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.bot-control button {
  min-height: 34px;
  padding: 7px 10px;
  border: 1px solid rgba(50, 208, 130, 0.45);
  border-radius: 8px;
  background: rgba(50, 208, 130, 0.12);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.bot-control button.danger {
  border-color: rgba(255, 107, 107, 0.45);
  background: rgba(255, 107, 107, 0.12);
}

.bot-control button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.bot-control small {
  min-height: 16px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

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

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

.dot.bad {
  background: var(--red);
  box-shadow: 0 0 0 4px rgba(255, 107, 107, 0.12);
}

.dot.idle {
  background: var(--blue);
  box-shadow: 0 0 0 4px rgba(86, 167, 255, 0.12);
}

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

.primary-grid {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  margin-bottom: 14px;
}

.grid article {
  min-width: 0;
  padding: 12px;
}

.grid strong {
  display: block;
  margin-top: 8px;
  overflow-wrap: anywhere;
  font-size: 20px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.metric-detail {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.connection-text.ok {
  color: var(--green);
}

.connection-text.bad {
  color: var(--red);
}

.connection-text.idle {
  color: var(--blue);
}

.panel {
  padding: 16px;
  margin-top: 14px;
}

.sub-heading {
  margin-top: 14px;
}

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

.imbalance-actions button {
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid rgba(244, 186, 78, 0.48);
  border-radius: 8px;
  background: rgba(244, 186, 78, 0.12);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.imbalance-actions button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.imbalance-grid {
  margin-top: 10px;
}

.imbalance-grid strong {
  font-size: 18px;
}

.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;
}

.history-pager {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin: 12px 0 10px;
}

.pager-buttons,
.history-pager label {
  display: flex;
  gap: 8px;
  align-items: center;
}

.history-pager button,
.history-pager select {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
  color: var(--text);
  font: inherit;
}

.history-pager button {
  padding: 7px 12px;
  cursor: pointer;
}

.history-pager button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.history-pager select {
  padding: 7px 28px 7px 10px;
}

#historyPageInfo {
  min-width: 150px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.threshold-editor {
  display: grid;
  grid-template-columns: repeat(3, minmax(160px, 1fr)) auto;
  gap: 10px;
  align-items: end;
  margin: 12px 0;
}

.threshold-editor label {
  display: grid;
  gap: 6px;
}

.threshold-editor input {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}

.threshold-editor button {
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid rgba(50, 208, 130, 0.45);
  border-radius: 8px;
  background: rgba(50, 208, 130, 0.12);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.threshold-editor button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.form-message {
  grid-column: 1 / -1;
  min-height: 18px;
}

.signal-distance-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0;
}

.signal-distance {
  display: grid;
  gap: 8px;
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
}

.signal-distance strong {
  display: block;
  margin-top: 6px;
  color: var(--text);
  font-size: 19px;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.signal-distance p {
  overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}

.signal-distance.signal-ready {
  border-color: rgba(50, 208, 130, 0.58);
  background: rgba(50, 208, 130, 0.08);
}

.signal-distance.signal-ready strong {
  color: var(--green);
}

.signal-distance.signal-wait strong {
  color: var(--amber);
}

.signal-distance.signal-missing {
  opacity: 0.72;
}

pre {
  margin: 12px 0 0;
  max-height: 360px;
  overflow: auto;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.05);
  background: #0c0f10;
  color: var(--text);
  font-size: 12px;
  line-height: 1.5;
}

.table-wrap {
  margin-top: 12px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
}

.compact-panel .table-wrap {
  margin-top: 10px;
}

.compact-table-wrap table {
  min-width: 720px;
}

.lighter-risk-table-wrap table {
  min-width: 900px;
  font-size: 11.5px;
  line-height: 1.22;
}

.lighter-risk-table-wrap th,
.lighter-risk-table-wrap td {
  padding: 5px 8px;
  vertical-align: middle;
  white-space: nowrap;
}

.lighter-risk-table-wrap td strong {
  display: inline;
  color: var(--text);
}

.lighter-risk-table-wrap td strong.profit-text {
  color: var(--green);
}

.lighter-risk-table-wrap td strong.loss-text {
  color: var(--red);
}

.lighter-risk-table-wrap td strong.warn-text {
  color: var(--amber);
}

.lighter-risk-table-wrap td small {
  display: inline;
  margin-left: 6px;
  color: var(--muted);
  font-size: 11px;
}

.profit-chart {
  position: relative;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0c0f10;
}

.chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.chart-head strong {
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

.chart-canvas {
  position: relative;
  min-height: 300px;
  overflow: hidden;
}

.chart-canvas svg {
  display: block;
  width: 100%;
  height: 300px;
}

.profit-direction-legend {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  max-width: calc(100% - 80px);
  color: var(--muted);
  font-size: 11px;
  pointer-events: none;
}

.direction-legend-item {
  padding: 2px 6px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  background: rgba(12, 15, 16, 0.82);
  font-variant-numeric: tabular-nums;
}

.direction-legend-item.dir-buy-lighter {
  color: var(--blue);
}

.direction-legend-item.dir-buy-spot {
  color: var(--amber);
}

.grid-line {
  stroke: rgba(255, 255, 255, 0.08);
  stroke-width: 1;
}

.axis-line {
  stroke: rgba(244, 186, 78, 0.45);
  stroke-width: 1.2;
  stroke-dasharray: 5 5;
}

.axis-text {
  fill: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.profit-line {
  fill: none;
  stroke: var(--green);
  stroke-width: 2.5;
}

.funding-line {
  fill: none;
  stroke: var(--amber);
  stroke-width: 2.5;
}

.split-line {
  fill: none;
  stroke-width: 2.4;
}

.split-line.binance {
  stroke: var(--blue);
}

.split-line.lighter {
  stroke: var(--amber);
}

.profit-point {
  cursor: crosshair;
  stroke: #0c0f10;
  stroke-width: 2;
}

.profit-point.closed {
  stroke-dasharray: none;
}

.profit-point.open {
  stroke: rgba(255, 255, 255, 0.88);
  stroke-dasharray: 3 2;
}

.profit-point.dir-buy-lighter {
  fill: var(--blue);
}

.profit-point.dir-buy-spot {
  fill: var(--amber);
}

.profit-point.dir-unknown {
  fill: var(--green);
}

.split-point {
  cursor: crosshair;
  stroke: #0c0f10;
  stroke-width: 2;
}

.split-point.binance {
  fill: var(--blue);
}

.split-point.lighter {
  fill: var(--amber);
}

.funding-point {
  cursor: crosshair;
  fill: var(--amber);
  stroke: #0c0f10;
  stroke-width: 2;
}

.chart-legend {
  display: flex;
  gap: 12px;
  align-items: center;
}

.legend-item {
  color: var(--muted);
  font-size: 12px;
}

.legend-item::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 3px;
  margin-right: 6px;
  vertical-align: middle;
  border-radius: 999px;
  background: var(--blue);
}

.legend-item.lighter::before {
  background: var(--amber);
}

.audit-marker {
  cursor: crosshair;
}

.audit-marker line {
  stroke-width: 1.4;
  stroke-dasharray: 4 4;
}

.audit-marker path {
  stroke: #0c0f10;
  stroke-width: 1.5;
}

.audit-marker.ok line {
  stroke: rgba(86, 167, 255, 0.7);
}

.audit-marker.ok path {
  fill: var(--blue);
}

.audit-marker.bad line {
  stroke: rgba(255, 107, 107, 0.8);
}

.audit-marker.bad path {
  fill: var(--red);
}

.last-label {
  fill: var(--text);
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.chart-tooltip {
  position: absolute;
  z-index: 4;
  min-width: 260px;
  max-width: 380px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(12, 15, 16, 0.96);
  color: var(--text);
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-line;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.chart-empty {
  padding: 120px 0;
}

table {
  width: 100%;
  min-width: 1320px;
  border-collapse: collapse;
  font-size: 12px;
  line-height: 1.45;
}

th,
td {
  padding: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  text-align: left;
  vertical-align: top;
  white-space: pre-line;
  font-variant-numeric: tabular-nums;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: var(--muted);
  background: var(--panel-2);
  font-weight: 650;
}

tbody tr:hover {
  background: rgba(255, 255, 255, 0.035);
}

.checkpoint-row td {
  background: rgba(86, 167, 255, 0.08);
  color: var(--text);
}

.checkpoint-row.checkpoint-ok td {
  border-left: 3px solid var(--blue);
}

.checkpoint-row.checkpoint-bad td {
  border-left: 3px solid var(--red);
  background: rgba(255, 107, 107, 0.08);
}

.empty {
  color: var(--muted);
  text-align: center;
}

.profit-text {
  color: var(--green);
}

.loss-text {
  color: var(--red);
}

.warn-text {
  color: var(--amber);
}

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

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

  .instrument-picker {
    width: 100%;
  }

  .status-box time {
    text-align: left;
  }

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

  .threshold-editor {
    grid-template-columns: 1fr;
  }

  .signal-distance-grid {
    grid-template-columns: 1fr;
  }

  .imbalance-actions {
    width: 100%;
    justify-content: stretch;
  }

  .imbalance-actions > * {
    flex: 1 1 100%;
  }
}
