:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --border: #e6e9f1;
  --text: #1c2333;
  --muted: #646d80;
  --blue: #2f5fe8;
  --blue-soft: #eef2fe;
  --green: #15803d;
  --green-soft: #e8f6ed;
  --red: #c62828;
  --red-soft: #fdecec;
  --orange: #b45309;
  --orange-soft: #fff4e2;
  --focus: #2f5fe8;
  --shadow: 0 1px 2px rgba(28, 35, 51, .04), 0 4px 16px rgba(28, 35, 51, .04);
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 14px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2 { margin: 0; font-weight: 700; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 15px; }
p { margin: 0; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.big { font-size: 20px; font-weight: 700; }
.small { font-size: 12.5px; }
.muted { color: var(--muted); }
.pos { color: var(--green); }
.neg { color: var(--red); }
.r { text-align: right; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  background: var(--card); border-right: 1px solid var(--border); padding: 22px 16px;
}
.logo { font-size: 26px; font-weight: 800; color: var(--text); letter-spacing: -.02em; padding: 0 12px 26px; }
.logo span { color: var(--blue); }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 12px; border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 600;
}
.nav-item svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item.active { background: var(--blue-soft); color: var(--blue); }
.nav-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--blue); margin-left: auto; }
.nav-dot.warn { background: var(--orange); }
.nav-dot.error { background: var(--red); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }

.mode-pill { border-radius: 10px; padding: 12px 14px; font-size: 12.5px; line-height: 1.4; }
.mode-pill strong { display: block; font-size: 13px; }
.mode-pill.observe { background: var(--orange-soft); color: var(--orange); }
.mode-pill.live { background: var(--red-soft); color: var(--red); }

.main { min-width: 0; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 16px 28px; background: rgba(255, 255, 255, .96); border-bottom: 1px solid var(--border);
  backdrop-filter: blur(6px);
}
.topbar-status { display: flex; align-items: center; gap: 18px; flex: 1; justify-content: flex-end; }
.topbar-price { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.25; }
.topbar-price #price { font-size: 18px; font-weight: 700; }
.topbar-actions { display: flex; gap: 8px; }
.btn-subtle-danger { background: transparent; border-color: transparent; color: var(--muted); font-weight: 600; }
.btn-subtle-danger span { color: var(--red); margin-right: 4px; }
.btn-subtle-danger:hover:not(:disabled) { background: var(--red-soft); border-color: transparent; color: var(--red); }

.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px;
  font-size: 12.5px; font-weight: 700;
}
.badge.running { background: var(--green-soft); color: var(--green); }
.badge.paused { background: var(--orange-soft); color: var(--orange); }
.badge.halted, .badge.no_contact { background: var(--red-soft); color: var(--red); }

.alarm-bar { margin: 16px 28px 0; padding: 12px 16px; border-radius: 10px; background: var(--red); color: #fff; font-weight: 600; }

/* ---------- content ---------- */
.content { padding: 22px 28px 48px; display: grid; gap: 20px; max-width: 1400px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 14px; }
.halt-panel { display: flex; justify-content: space-between; align-items: center; gap: 20px; border-color: #f3b7b7; background: #fff8f8; }
.halt-panel h2 { color: var(--red); margin-bottom: 4px; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; }
.kpi { display: flex; flex-direction: column; justify-content: flex-start; padding: 18px 20px; min-width: 0; }
.kpi-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.kpi-value { font-size: 22px; font-weight: 800; margin-top: 6px; letter-spacing: -.01em; }
.kpi-sub { font-size: 12.5px; font-weight: 600; margin-top: 2px; color: var(--muted); }
.kpi-sub.pos { color: var(--green); }
.kpi-sub.neg { color: var(--red); }
.kpi-value.pos { color: var(--green); }
.kpi-value.neg { color: var(--red); }

.row-main { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; align-items: stretch; }
.pos-card { display: flex; flex-direction: column; }
.pos-card h2 { margin-bottom: 14px; }
.pos-card .actions { margin-top: auto; padding-top: 16px; }

@media (max-width: 1200px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 980px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; flex-wrap: wrap; gap: 12px; }
  .logo { padding: 0 12px; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar-foot { margin: 0 0 0 auto; flex-direction: row; }
  .row-main { grid-template-columns: 1fr; }
}

/* ---------- buttons ---------- */
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font: inherit; font-weight: 700; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover:not(:disabled) { border-color: #c9cfdd; background: #fafbfd; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #2550cc; border-color: #2550cc; }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #a91f1f; border-color: #a91f1f; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-outline-danger { color: var(--red); border-color: #f0b4b4; }
.btn-outline-danger:hover:not(:disabled) { background: var(--red-soft); border-color: var(--red); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.actions-end { justify-content: flex-end; margin-top: 18px; }
.msg { min-height: 1.5em; margin-top: 10px; color: var(--muted); }
.msg.error { color: var(--red); }
.msg.ok { color: var(--green); }

/* ---------- forms ---------- */
label { display: block; margin: 0 0 6px; color: var(--muted); font-size: 12.5px; font-weight: 600; }
input, select {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: #fbfcfe; color: var(--text); font: inherit;
  font-variant-numeric: tabular-nums;
}
input:focus { border-color: var(--blue); background: #fff; outline: 3px solid rgba(47, 95, 232, .15); }
input[aria-invalid="true"] { border-color: var(--red); background: #fffafa; }
select:disabled { color: var(--muted); }
.strategy-select { min-width: 240px; }
.fields { display: grid; gap: 4px 16px; }
.fields-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.input-unit { position: relative; }
.input-unit input { padding-right: 52px; }
.input-unit span {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-size: 13px; font-weight: 600; pointer-events: none;
}
.input-unit input:disabled { background: var(--bg); color: #9aa1b1; }
.opt {
  display: grid; grid-template-columns: 240px 160px 1fr; align-items: center; gap: 4px 16px;
  padding: 12px 0; border-top: 1px solid var(--border);
}
.opt .field-error { grid-column: 2 / -1; min-height: 0; margin: 0; }
.opt-hint { color: var(--muted); font-size: 13px; }
.switch { display: flex; align-items: center; gap: 10px; margin: 0; min-height: 44px; cursor: pointer; color: var(--text); font-size: 14px; }
.switch input {
  appearance: none; width: 40px; height: 24px; min-height: 0; padding: 0; flex: none; border-radius: 999px;
  background: #cfd5e1; border: 0; position: relative; cursor: pointer; transition: background .15s;
}
.switch input::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); transition: transform .15s;
}
.switch input:checked { background: var(--blue); }
.switch input:checked::after { transform: translateX(16px); }
@media (max-width: 900px) { .opt { grid-template-columns: 1fr 140px; } .opt-hint, .opt .field-error { grid-column: 1 / -1; } }
.save-box { display: flex; align-items: center; gap: 12px; }
.dirty { color: var(--orange); font-weight: 700; font-size: 13px; }
.field-error { min-height: 1.3em; margin-top: 4px; color: var(--red); font-size: 12.5px; }
.hint { margin-top: 4px; color: var(--muted); font-size: 12.5px; }
.warning { margin-top: 4px; color: var(--orange); font-weight: 600; }
.extra { border-top: 1px solid var(--border); padding-top: 4px; }
.extra summary {
  cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 700; color: var(--blue);
  margin-bottom: 8px;
}
.settings-foot { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-top: 16px; flex-wrap: wrap; }
.settings-foot .preview { flex: 1; min-width: 280px; }
.preview { padding: 12px 14px; border-radius: 10px; background: var(--blue-soft); color: #1e3a8a; font-weight: 500; }

/* ---------- position ---------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 10px 16px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-weight: 600; }
.wait { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.wait-label { color: var(--muted); font-weight: 600; font-size: 13px; }
.wait-value { font-size: 28px; font-weight: 800; letter-spacing: -.01em; }
.wait-sub { color: var(--muted); }
.wait-meter { height: 8px; border-radius: 999px; background: var(--bg); margin: 14px 0 6px; overflow: hidden; }
.wait-meter span { display: block; height: 100%; background: var(--blue); border-radius: 999px; width: 0; transition: width .4s; }
.wait.blocked .wait-value { color: var(--muted); }
.wait.blocked .wait-meter span { background: #c3cad8; }
.wait-note { margin-top: 8px; padding: 10px 12px; border-radius: 10px; background: var(--bg); font-size: 13px; }
.wait-note:empty { display: none; }
.wait-note.warn { background: var(--orange-soft); color: var(--orange); }

/* ---------- table ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th { padding: 10px 12px; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; text-align: left; background: var(--bg); }
.table th:first-child { border-radius: 8px 0 0 8px; }
.table th:last-child { border-radius: 0 8px 8px 0; }
.table td { padding: 12px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: 0; }
.table td.r, .table th.r { text-align: right; }
.tag { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 700; }
.tag.buy { background: var(--green-soft); color: var(--green); }
.tag.sell { background: var(--red-soft); color: var(--red); }

/* ---------- chart ---------- */
.chart { width: 100%; height: 320px; }
.chart svg { display: block; width: 100%; height: 100%; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .grid-line { stroke: #eef0f5; stroke-width: 1; }
.chart .price-line { fill: none; stroke: var(--blue); stroke-width: 2; stroke-linejoin: round; }
.chart .price-area { fill: url(#area-fill); }
.chart .lvl { stroke-width: 1.4; stroke-dasharray: 6 5; }
.chart .lvl-label { font-size: 11px; font-weight: 700; }
.chart .lp-link { stroke-width: 2; stroke-dasharray: 6 4; }
.chart .buy-level { stroke: var(--blue); stroke-width: 1.4; stroke-dasharray: 2 4; }
.chart .buy-label { fill: var(--blue); font-size: 11px; font-weight: 700; }
.chart .lp-label { font-size: 12px; font-weight: 700; paint-order: stroke; stroke: #fff; stroke-width: 4px; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; margin-right: 6px; }
.dot.buy { background: #16a34a; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.dot.sell { background: #dc2626; clip-path: polygon(0 0, 100% 0, 50% 100%); }

body.stale .num { opacity: .45; }

/* ---------- system status ---------- */
.sys-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.sys-item { display: flex; gap: 12px; align-items: flex-start; padding: 14px; border: 1px solid var(--border); border-radius: 10px; background: #fbfcfe; }
.sys-dot { width: 26px; height: 26px; flex: none; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 800; }
.sys-dot.ok { background: var(--green-soft); color: var(--green); }
.sys-dot.warn { background: var(--orange-soft); color: var(--orange); }
.sys-dot.error { background: var(--red-soft); color: var(--red); }
.sys-dot.off { background: var(--bg); color: var(--muted); }
.sys-name { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.sys-value { font-weight: 700; }
.sys-detail { font-size: 12.5px; color: var(--muted); overflow-wrap: anywhere; }

/* ---------- login + dialog ---------- */
.login { min-height: 100vh; display: grid; place-items: center; }
.login-card { width: 360px; padding: 32px; }
.login-card .logo { padding: 0 0 6px; }
.login-sub { margin-bottom: 20px; }
dialog {
  width: min(480px, 92vw); padding: 0; border: 0; border-radius: 16px;
  background: var(--card); color: var(--text); box-shadow: 0 20px 60px rgba(28, 35, 51, .25);
}
dialog::backdrop { background: rgba(28, 35, 51, .45); }
.dialog-body { padding: 26px; }
.dialog-body h2 { font-size: 18px; margin-bottom: 12px; }
.dialog-body .kv { margin: 12px 0 4px; padding: 14px; border-radius: 10px; background: var(--bg); }
.dialog-body p { margin: 0 0 10px; }

/* ---------- views ---------- */
.view { display: grid; gap: 20px; min-width: 0; }
