:root {
  font-family: "Segoe UI", "Microsoft YaHei UI", sans-serif;
  color: var(--text);
  background: var(--bg);
  letter-spacing: 0;
  --bg: #0e1117;
  --rail: #111722;
  --top: #101721;
  --panel: #151c27;
  --panel2: #1b2431;
  --border: #2a3443;
  --text: #eef2f7;
  --muted: #8c98aa;
  --green: #18a77d;
  --red: #db4b55;
  --amber: #d89b33;
  --blue: #2d7dd2;
}

body[data-theme="light"] {
  --bg: #f3f5f7;
  --rail: #ffffff;
  --top: #ffffff;
  --panel: #ffffff;
  --panel2: #f6f8fa;
  --border: #d8dee6;
  --text: #17202c;
  --muted: #657185;
  --green: #098465;
  --red: #c23b45;
  --amber: #b67812;
  --blue: #2267ba;
}

body[data-theme="compact"] {
  --bg: #101114;
  --rail: #151515;
  --top: #151515;
  --panel: #1b1b1e;
  --panel2: #222226;
  --border: #333339;
  --text: #f1f1f1;
  --muted: #9a9aa3;
  --green: #39b77f;
  --red: #ff5c68;
  --amber: #e0a13a;
  --blue: #5d8bd7;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; letter-spacing: 0; }
button { -webkit-tap-highlight-color: transparent; }

#app { display: flex; height: 100vh; }
.sidebar {
  width: 252px;
  background: var(--rail);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 24px; }
.brand-mark {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  background: var(--text);
  color: var(--bg);
  font-weight: 800;
  border-radius: 6px;
  font-size: 13px;
}
.brand strong, .brand small { display: block; }
.brand small { font-size: 10px; color: var(--muted); margin-top: 3px; }
.sidebar nav { display: grid; gap: 4px; }
.nav-btn {
  height: 42px;
  border: 0;
  background: transparent;
  color: var(--muted);
  text-align: left;
  border-radius: 6px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}
.nav-btn:hover { background: var(--panel2); }
.nav-btn.active { background: var(--panel2); color: var(--text); }
.nav-btn svg, .icon-btn svg, .live-pill svg, .status-strip svg { width: 17px; height: 17px; }
.side-foot {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding: 16px 9px 2px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.side-foot strong, .side-foot small { display: block; font-size: 12px; }
.side-foot small { color: var(--muted); margin-top: 3px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); }

main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  min-height: 78px;
  border-bottom: 1px solid var(--border);
  padding: 0 26px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--top);
}
.topbar h1 { font-size: 20px; margin: 0 0 4px; font-weight: 650; }
.topbar p { margin: 0; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 9px; }
.segment { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.segment button {
  width: 38px;
  height: 34px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.segment button.active { background: var(--text); color: var(--bg); }
.live-pill {
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--amber);
  background: color-mix(in srgb, var(--amber) 11%, transparent);
  font-size: 12px;
  white-space: nowrap;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel2);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn.danger { color: var(--red); }

#content { padding: 20px 26px 28px; overflow: auto; flex: 1; }
.status-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.status-strip span {
  height: 30px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 10px;
  color: var(--muted);
  background: var(--panel);
  font-size: 12px;
  min-width: 0;
  overflow-wrap: anywhere;
}
.status-strip .warn { color: var(--amber); }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.metric, .panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.metric { padding: 16px; min-width: 0; }
.metric label { font-size: 12px; color: var(--muted); }
.metric strong { display: block; font-size: 22px; margin: 8px 0 4px; font-weight: 650; overflow-wrap: anywhere; }
.metric small { font-size: 11px; color: var(--muted); line-height: 1.45; }
.positive { color: var(--green) !important; }
.negative { color: var(--red) !important; }
.grid-main { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(320px, .8fr); gap: 14px; }
.panel { padding: 16px; min-width: 0; }
.panel + .panel { margin-top: 14px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.panel-head h2 { font-size: 14px; margin: 0; font-weight: 650; }
.panel-head small { color: var(--muted); font-size: 11px; }
.chart { height: 315px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 11px 9px; border-bottom: 1px solid var(--border); font-size: 12px; white-space: nowrap; }
.table th { color: var(--muted); font-weight: 500; }
.table tr:last-child td { border-bottom: 0; }
.table small { color: var(--muted); }
.empty-cell { color: var(--muted); white-space: normal !important; line-height: 1.6; padding: 28px 10px !important; text-align: center !important; }
.badge {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 5px;
  background: var(--panel2);
  color: var(--muted);
  font-size: 11px;
}
.strategy-list { display: grid; gap: 10px; }
.strategy {
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  background: var(--panel2);
}
.strategy h3 { font-size: 13px; margin: 0 0 5px; }
.strategy p { font-size: 11px; color: var(--muted); margin: 0; line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chips span {
  height: 23px;
  display: inline-flex;
  align-items: center;
  border-radius: 5px;
  padding: 0 7px;
  background: var(--panel);
  color: var(--muted);
  font-size: 10px;
}
.chips .danger-chip { color: var(--red); }
.toggle {
  width: 38px;
  height: 22px;
  border: 0;
  border-radius: 11px;
  background: #707785;
  position: relative;
  cursor: pointer;
  align-self: center;
}
.toggle:after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  left: 3px;
  top: 3px;
  border-radius: 50%;
  background: #fff;
}
.toggle.on { background: var(--green); }
.toggle.on:after { left: 19px; }
.toolbar { display: flex; gap: 9px; align-items: end; flex-wrap: wrap; margin-bottom: 14px; }
.field { display: grid; gap: 5px; }
.field.grow { flex: 1; min-width: 260px; }
.field label { font-size: 11px; color: var(--muted); }
input, select {
  height: 36px;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  border-radius: 6px;
  padding: 0 10px;
  min-width: 0;
}
.btn {
  height: 36px;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  border-radius: 6px;
  padding: 0 14px;
  cursor: pointer;
}
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.danger { background: color-mix(in srgb, var(--red) 16%, transparent); color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, var(--border)); }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.spacer { height: 14px; }
.risk-row { display: grid; grid-template-columns: 1fr 120px 48px; gap: 12px; align-items: center; padding: 13px 0; border-bottom: 1px solid var(--border); }
.risk-row span { font-size: 12px; }
.risk-row small { color: var(--muted); display: block; margin-top: 4px; line-height: 1.45; }
.risk-row input { width: 100%; }
.save-row { margin-top: 16px; margin-bottom: 0; }
.muted-text { color: var(--muted); font-size: 12px; line-height: 1.7; margin: 0 0 14px; }
.nested-metrics { grid-template-columns: 1fr 1fr; margin: 0; }
.api-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.api-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel2);
  padding: 13px;
  min-width: 0;
}
.api-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.api-card-head strong, .api-card-head small { display: block; }
.api-card-head strong { font-size: 13px; }
.api-card-head small { color: var(--muted); font-size: 11px; margin-top: 4px; line-height: 1.45; }
.api-card-actions { display: inline-flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.api-card-actions .btn { height: 32px; padding: 0 10px; display: inline-flex; align-items: center; gap: 5px; }
.api-card-actions .btn svg { width: 15px; height: 15px; }
.api-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.api-form label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
  min-width: 0;
}
.api-form input { width: 100%; }
.log {
  font-family: Consolas, "Cascadia Mono", monospace;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.9;
  max-height: 300px;
  overflow: auto;
}
#toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  padding: 11px 14px;
  background: var(--text);
  color: var(--bg);
  border-radius: 6px;
  opacity: 0;
  transform: translateY(8px);
  transition: .2s;
  pointer-events: none;
  font-size: 12px;
  z-index: 50;
}
#toast.show { opacity: 1; transform: none; }
#modal:empty { display: none; }
#modal {
  position: fixed;
  inset: 0;
  background: #0009;
  display: grid;
  align-items: start;
  justify-items: center;
  overflow-y: auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  z-index: 60;
}
.modal-card {
  width: 430px;
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}
.modal-card.wide { width: min(760px, calc(100vw - 32px)); }
.modal-card h2 { margin: 0 0 10px; font-size: 17px; }
.modal-card p { color: var(--muted); font-size: 13px; line-height: 1.6; }
.modal-actions {
  position: sticky;
  bottom: -20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 18px -20px -20px;
  padding: 14px 20px 20px;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  border-top: 1px solid var(--border);
}

@media (max-width: 1150px) {
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .grid-main, .split { grid-template-columns: 1fr; }
  .sidebar { width: 218px; }
}

@media (max-width: 1400px) {
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-main { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  body { overflow: auto; }
  #app { display: block; height: auto; min-height: 100vh; padding-bottom: calc(68px + env(safe-area-inset-bottom)); }
  .sidebar {
    position: fixed;
    z-index: 20;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: calc(64px + env(safe-area-inset-bottom));
    border: 0;
    border-top: 1px solid var(--border);
    padding: 5px 6px calc(5px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--rail) 94%, transparent);
    backdrop-filter: blur(12px);
  }
  .brand, .side-foot { display: none; }
  .sidebar nav { display: grid; grid-template-columns: repeat(6, 1fr); gap: 1px; }
  .nav-btn {
    height: 54px;
    padding: 4px 2px;
    justify-content: center;
    flex-direction: column;
    gap: 3px;
    font-size: 10px;
    text-align: center;
  }
  .nav-btn svg { width: 18px; height: 18px; }
  main { display: block; }
  .topbar {
    min-height: 72px;
    padding: 10px 12px;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 10;
  }
  .title-block { min-width: 0; }
  .topbar h1 { font-size: 17px; }
  .topbar p { display: none; }
  .top-actions { gap: 5px; }
  .segment button { width: 32px; height: 32px; }
  .live-pill { width: 34px; height: 34px; padding: 0; justify-content: center; font-size: 0; }
  .live-pill svg { width: 17px; height: 17px; }
  .icon-btn { width: 34px; height: 34px; }
  #content { padding: 12px 12px 20px; overflow: visible; }
  .status-strip { gap: 6px; margin-bottom: 10px; }
  .status-strip span { width: 100%; max-width: 100%; height: auto; min-height: 30px; font-size: 11px; white-space: normal; word-break: break-all; align-items: flex-start; padding-top: 7px; padding-bottom: 7px; }
  .status-strip svg { flex: 0 0 auto; margin-top: 1px; }
  .metrics { grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; min-width: 0; }
  .metric { padding: 12px; }
  .metric strong { font-size: 16px; }
  .grid-main, .split { display: block; }
  .panel { margin-bottom: 10px; padding: 12px; }
  .panel + .panel { margin-top: 10px; }
  .panel-head { align-items: flex-start; margin-bottom: 10px; }
  .panel-head h2 { font-size: 13px; }
  .panel-head small { text-align: right; }
  .chart { height: 245px; }
  .chart canvas { max-width: 100%; }
  .table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table th, .table td { padding: 9px 8px; font-size: 11px; }
  .toolbar { align-items: stretch; gap: 8px; }
  .toolbar .field { min-width: calc(50% - 6px); flex: 1; }
  .field.grow { min-width: 100%; }
  .toolbar input, .toolbar select { width: 100%; }
  .btn { display: inline-flex; align-items: center; justify-content: center; gap: 5px; }
  .risk-row { grid-template-columns: 1fr 88px 36px; gap: 8px; }
  .risk-row input { min-width: 0; }
  .api-grid { grid-template-columns: 1fr; }
  .api-form { grid-template-columns: 1fr; }
  .api-card-head { align-items: flex-start; }
  .api-card-actions { flex-direction: column-reverse; align-items: flex-end; }
  .modal-card { width: calc(100% - 28px); padding: 17px; }
  #toast { left: 12px; right: 12px; bottom: calc(78px + env(safe-area-inset-bottom)); text-align: center; }
  .strategy { padding: 11px; }
  .chips { gap: 5px; }
}

@media (max-width: 430px) {
  .metrics { grid-template-columns: 1fr; }
  .topbar { align-items: flex-start; }
  .top-actions { flex-wrap: wrap; justify-content: flex-end; }
  .risk-row { grid-template-columns: 1fr; }
  .risk-row .toggle { justify-self: start; }
}
