:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c2129;
  --muted: #6b7380;
  --primary: #2f6fde;
  --primary-text: #ffffff;
  --ok: #1a9a5b;
  --info: #2f6fde;
  --warn: #c98a06;
  --bad: #d23f3f;
  --off: #8a919c;
  --buy: #1a9a5b;
  --sell: #d23f3f;
  --log-bg: #12161c;
  --log-text: #d6dbe3;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1e232b;
    --border: #2a3039;
    --text: #e4e8ee;
    --muted: #8d96a3;
    --primary: #4f8cf0;
    --ok: #34c17e;
    --info: #4f8cf0;
    --warn: #e0a826;
    --bad: #ef5d5d;
    --off: #6f7782;
    --buy: #34c17e;
    --sell: #ef5d5d;
    --log-bg: #0a0d11;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 18px; }
h3 { font-size: 14px; margin: 20px 0 8px; }
code { font-family: var(--mono); font-size: 12px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.muted { color: var(--muted); }
.error { color: var(--bad); }
.spacer { flex: 1; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-size: 12px; }

/* ─── controls ─────────────────────────────────────────────────────────── */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font: inherit; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.danger-ghost { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 3px 8px; font-size: 12px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
label.check { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); white-space: nowrap; }

/* ─── login ────────────────────────────────────────────────────────────── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px; display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { font-size: 22px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
.login-card input { color: var(--text); }
.login-card .btn { margin-top: 6px; padding: 9px; }

/* ─── layout ───────────────────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.infra { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.user { color: var(--muted); display: flex; align-items: center; gap: 6px; }
.tabs {
  display: flex; gap: 2px; padding: 0 20px; background: var(--surface);
  border-bottom: 1px solid var(--border); overflow-x: auto;
}
.tabs button {
  border: 0; background: none; color: var(--muted); font: inherit; padding: 10px 14px;
  border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }
main { padding: 20px; max-width: 1400px; margin: 0 auto; }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.log-toolbar { margin-top: 24px; }

/* ─── badges / chips ───────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 500;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: none; }
.badge.dot::before { display: inline-block; }
.badge.ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.badge.info { color: var(--info); background: color-mix(in srgb, var(--info) 12%, transparent); }
.badge.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.badge.bad { color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, transparent); }
.badge.off { color: var(--off); }
.side-BUY { color: var(--buy); font-weight: 600; }
.side-SELL { color: var(--sell); font-weight: 600; }
.pos { color: var(--buy); }
.neg { color: var(--sell); }

/* ─── tables ───────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
.table th.num, .table td.num { text-align: right; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
.table td.actions { white-space: nowrap; text-align: right; }
.table td.empty { text-align: center; color: var(--muted); padding: 24px; }
.svc-name { font-weight: 600; }
.svc-desc { color: var(--muted); font-size: 12px; }
.reason { color: var(--muted); font-size: 12px; display: block; margin-top: 2px; }
.activity { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─── log ──────────────────────────────────────────────────────────────── */
.log {
  background: var(--log-bg); color: var(--log-text); font-family: var(--mono); font-size: 12px;
  line-height: 1.5; padding: 12px; border-radius: var(--radius); height: 420px; overflow: auto;
  margin: 0; white-space: pre-wrap; word-break: break-all;
}
.log .err { color: #ff8a8a; }
.log .svc { color: #7fb2ff; }

/* ─── misc ─────────────────────────────────────────────────────────────── */
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 12px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); color: var(--muted); padding: 6px 12px; font: inherit; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--primary); color: var(--primary-text); }
.switch { position: relative; display: inline-block; width: 34px; height: 20px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--off); border-radius: 999px; cursor: pointer; transition: background .15s; }
.switch span::after { content: ""; position: absolute; width: 14px; height: 14px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(14px); }
.alert { padding: 8px 12px; border-radius: 6px; background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad); margin-bottom: 8px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.chart { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; position: relative; min-height: 300px; }
.chart svg { display: block; width: 100%; height: 300px; }
.chart .grid line { stroke: var(--border); }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .l-equity { stroke: var(--primary); fill: none; stroke-width: 2; }
.chart .l-balance { stroke: var(--muted); fill: none; stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart .hover-line { stroke: var(--muted); stroke-width: 1; }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px; font-size: 12px; box-shadow: 0 4px 12px rgb(0 0 0 / .12); white-space: nowrap;
}
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.legend i { display: inline-block; width: 14px; height: 2px; vertical-align: middle; margin-right: 4px; background: var(--primary); }
.legend i.dash { background: repeating-linear-gradient(90deg, var(--muted) 0 4px, transparent 4px 7px); }

/* ─── modal / toast ────────────────────────────────────────────────────── */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text);
  width: min(520px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
dialog h3 { margin: 0 0 4px; font-size: 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 13px; color: var(--muted); }
.field small { color: var(--muted); font-size: 12px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--ok);
  padding: 10px 14px; border-radius: 6px; box-shadow: 0 6px 20px rgb(0 0 0 / .15); max-width: 380px;
}
.toast.bad { border-left-color: var(--bad); }

@media (max-width: 640px) {
  main { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .tabs { padding: 0 8px; }
  .field-row { grid-template-columns: 1fr; }
  .log { height: 320px; }
}
