:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #667085;
  --line: #d8dee6;
  --accent: #176b87;
  --accent-strong: #0f5269;
  --danger: #b42318;
  --danger-bg: #fee4e2;
  --ok: #067647;
  --warn: #b54708;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Segoe UI, Arial, sans-serif;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
}

h2 {
  font-size: 17px;
}

h3 {
  font-size: 13px;
  margin-bottom: 8px;
}

button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 8px 11px;
  font-weight: 650;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  background: var(--accent-strong);
}

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

button.secondary {
  background: #fff;
  color: var(--accent);
}

button.danger {
  background: var(--danger);
  border-color: var(--danger);
}

input,
select {
  width: 100%;
  min-width: 80px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  font: inherit;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

label.checkbox {
  grid-auto-flow: column;
  justify-content: start;
  align-items: center;
}

label.checkbox input {
  width: auto;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-box {
  width: min(360px, 100%);
  display: grid;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 22px;
}

.login-hint {
  color: var(--muted);
  font-size: 13px;
}

.bod-footer {
  position: sticky;
  bottom: 0;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  color: var(--muted);
  font-size: 12px;
  padding: 7px 22px;
  z-index: 5;
}

.bod-footer.ok {
  color: var(--ok);
}

.bod-footer.error {
  color: var(--danger);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 16px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.topbar p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.top-actions,
.settings-actions,
.card-actions,
.order-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.card-actions button,
.expand {
  min-width: 34px;
  padding-left: 10px;
  padding-right: 10px;
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 12px 22px 0;
}

.tab,
.order-tab {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}

.tab.active,
.order-tab.active {
  background: var(--panel);
  color: var(--text);
  border-color: var(--line);
}

main {
  padding: 14px 22px 30px;
}

.page {
  display: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.page.active {
  display: block;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.strategy-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.strategy-card {
  display: grid;
  grid-template-columns: 36px minmax(180px, 1.6fr) repeat(7, minmax(78px, 1fr)) minmax(100px, auto) auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.strategy-card small,
.trade-card small,
.mini-table th,
.muted-line {
  color: var(--muted);
  font-size: 11px;
}

.strategy-card > div:not(.strategy-main):not(.card-actions):not(.strategy-expanded) {
  display: grid;
  gap: 2px;
}

.expand {
  width: 30px;
  height: 30px;
  padding: 0;
}

.status {
  display: inline-block;
  min-width: 108px;
  padding: 5px 8px;
  border-radius: 6px;
  background: #eef2f6;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.status.running {
  background: #dcfae6;
  color: var(--ok);
}

.status.error {
  background: var(--danger-bg);
  color: var(--danger);
}

.status.waiting {
  background: #fef0c7;
  color: var(--warn);
}

.pnl-pos {
  color: var(--ok);
  font-weight: 750;
}

.pnl-neg {
  color: var(--danger);
  font-weight: 750;
}

.strategy-expanded {
  grid-column: 1 / -1;
  display: grid;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.leg-builder {
  margin-top: 14px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.leg-builder-rows {
  display: grid;
  gap: 8px;
}

.leg-row {
  display: grid;
  grid-template-columns: minmax(110px, 1fr) minmax(90px, 0.8fr) minmax(120px, 1fr) minmax(80px, 0.7fr) minmax(120px, 1fr) minmax(80px, 0.7fr) minmax(80px, 0.7fr) auto;
  gap: 8px;
  align-items: end;
}

.leg-row label {
  font-size: 11px;
  color: var(--muted);
}

.leg-hidden {
  display: none;
}

.qty-preview {
  background: #f6f8fa;
  color: var(--muted);
}

.leg-contract {
  grid-column: 1 / -1;
  color: var(--muted);
}

.feed-pill {
  display: inline-block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
}

.mini-table {
  width: 100%;
  border-collapse: collapse;
}

.mini-table th,
.mini-table td {
  border-bottom: 1px solid var(--line);
  padding: 7px;
  text-align: left;
  font-size: 12px;
}

.empty-state,
.log-item,
.metric {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 10px;
}

.settings-grid,
.form-grid,
.backtest-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.history-tools {
  display: grid;
  gap: 10px;
  margin-bottom: 12px;
}

.metrics,
.logs,
.trade-history {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.trade-history,
.logs {
  display: grid;
}

.trade-card {
  display: grid;
  grid-template-columns: minmax(180px, 1.5fr) repeat(5, minmax(80px, 1fr)) minmax(160px, auto);
  gap: 10px;
  align-items: center;
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  text-align: left;
}

.backtest-row {
  cursor: default;
}

.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.icon-btn {
  min-width: 34px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--text);
  font-size: 12px;
}

.icon-btn.danger {
  color: #b42318;
  border-color: #f5c2bc;
  background: #fff7f5;
}

dialog {
  width: min(980px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
}

dialog::backdrop {
  background: rgba(16, 24, 40, 0.35);
}

#strategyForm {
  padding: 16px;
}

.modal-head,
.modal-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.modal-actions {
  justify-content: flex-end;
  margin: 0;
}

.dialog-live-status {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  min-height: 30px;
  margin: -4px 0 12px;
  color: var(--muted);
  font-size: 12px;
}

.dialog-live-status span {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 5px 8px;
}

.symbol-field {
  position: relative;
}

.suggestions {
  position: absolute;
  z-index: 10;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 230px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(16, 24, 40, 0.14);
}

.suggestions button {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--text);
  text-align: left;
  white-space: normal;
}

#toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: 420px;
  padding: 12px 14px;
  border-radius: 8px;
  background: #17202a;
  color: #fff;
  opacity: 0;
  transform: translateY(10px);
  transition: 160ms ease;
  pointer-events: none;
}

#toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1180px) {
  .strategy-card {
    grid-template-columns: 34px minmax(170px, 1fr) repeat(3, minmax(86px, 1fr));
  }

  .strategy-card .card-actions,
  .strategy-card .status {
    grid-column: span 2;
  }
}

@media (max-width: 760px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .tabs {
    overflow-x: auto;
  }

  main {
    padding: 12px;
  }

  .strategy-card,
  .trade-card {
    grid-template-columns: 1fr 1fr;
  }

  .expand,
  .strategy-main,
  .strategy-expanded {
    grid-column: 1 / -1;
  }
}
