/* MXE operator console. Tokens first; dark is the default, light follows the OS. */
:root {
  --bg: #0f1216;
  --panel: #171b21;
  --panel-2: #1d222a;
  --line: #2a313b;
  --text: #e6e9ee;
  --muted: #8b95a3;
  --accent: #5b9dff;
  --ok: #3fb97a;
  --warn: #e0a83a;
  --bad: #e5534b;
  --idle: #6b7480;
  --mono: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", sans-serif;
  --radius: 8px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --panel: #ffffff;
    --panel-2: #f0f3f7;
    --line: #dde2e9;
    --text: #1b2028;
    --muted: #5e6875;
    --accent: #2563eb;
    --ok: #1f9a5c;
    --warn: #b7791f;
    --bad: #c93c35;
    --idle: #8a939f;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 var(--sans); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.mono, code { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ------------------------------------------------------------------ layout */
.shell { display: grid; grid-template-rows: auto auto 1fr; min-height: 100vh; }
header.top {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
header.top .brand { font-weight: 700; letter-spacing: .02em; }
header.top .spacer { flex: 1; }
header.top .meta { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
nav.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
nav.tabs a {
  padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap;
}
nav.tabs a:hover { color: var(--text); text-decoration: none; }
nav.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }
main { padding: 20px; max-width: 1400px; width: 100%; margin: 0 auto; }
@media (max-width: 640px) { main { padding: 16px; } header.top { padding: 10px 16px; } }

h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }
.page-head { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .spacer { flex: 1; }

.grid { display: grid; gap: 16px; }
.grid.tiles { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
@media (max-width: 640px) { .grid.tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; } .tile .value { font-size: 18px; } }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
@media (max-width: 640px) { .grid.two { grid-template-columns: 1fr; } }
.stack { display: grid; gap: 16px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
.panel.flush { padding: 0; }
.panel.flush > h2 { padding: 14px 16px 0; }

.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { color: var(--muted); font-size: 12px; }
.tile .value { font-size: 22px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tile.ok .value { color: var(--ok); }
.tile.warn .value { color: var(--warn); }
.tile.bad .value { color: var(--bad); }

/* ------------------------------------------------------------------ table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12px; white-space: nowrap; background: var(--panel-2); }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: var(--panel-2); }
td.actions { white-space: nowrap; text-align: right; }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* ------------------------------------------------------------------ badges */
.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 1px 8px; border-radius: 999px;
  font-size: 12px; border: 1px solid var(--line); background: var(--panel-2); white-space: nowrap;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--idle); }
.badge.ok::before { background: var(--ok); }
.badge.warn::before { background: var(--warn); }
.badge.bad::before { background: var(--bad); }
.badge.plain::before { display: none; }
.badge.mode { border-color: var(--ok); color: var(--ok); font-weight: 600; }
.badge.mode.live { border-color: var(--bad); color: var(--bad); }
/* Trading mode per strategy / account: real money is red and filled; the rest is neutral. */
.badge.tm { font-weight: 600; letter-spacing: .02em; border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); color: var(--accent); }
.badge.tm.live { background: var(--bad); border-color: var(--bad); color: #fff; }
.modes { display: inline-flex; gap: 4px; flex-wrap: wrap; }

/* Operator note: plain text, muted, two lines max until tapped. */
.note { font-size: 12.5px; white-space: pre-line; overflow-wrap: anywhere; min-width: 0; }
.note.clamp { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; cursor: pointer; }
.note.clamp.open { display: block; -webkit-line-clamp: unset; line-clamp: none; overflow: visible; }
.note.clamp:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
.sname .note { margin-top: 2px; max-width: 42ch; font-weight: 400; }
.note-panel { display: grid; gap: 8px; }
.note-panel .note { font-size: 14px; color: var(--text); }
.note-panel .note.empty-note { color: var(--muted); font-size: 13px; }
.note-panel h2 { margin: 0; }
.grow { flex: 1; min-width: 0; }
.note-meta { font-size: 12px; }
.head-badges { justify-content: flex-end; }
textarea.note-input { font-family: var(--sans); font-size: 14px; min-height: 120px; line-height: 1.5; }
.char-count { text-align: right; font-size: 12px; font-variant-numeric: tabular-nums; }
.char-count.over { color: var(--bad); }

/* ------------------------------------------------------------------ controls */
.btn {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 6px; padding: 5px 12px;
  cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 50%, var(--line)); }
.btn.small { padding: 2px 9px; font-size: 12.5px; }
.btn + .btn { margin-left: 6px; }

label.field { display: grid; gap: 4px; }
label.field span { color: var(--muted); font-size: 12px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 9px; width: 100%;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { font-family: var(--mono); font-size: 12.5px; min-height: 180px; resize: vertical; }
input.changed, select.changed { border-color: var(--warn); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 16px; align-items: start; }
label.field { align-content: start; }
.field .hint { color: var(--muted); font-size: 11.5px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

/* ------------------------------------------------------------------ notices */
.notice { border: 1px solid var(--line); border-left-width: 3px; border-radius: 6px; padding: 9px 12px; background: var(--panel); }
.notice.bad { border-left-color: var(--bad); }
.notice.warn { border-left-color: var(--warn); }
.notice.ok { border-left-color: var(--ok); }
.empty { color: var(--muted); padding: 16px; text-align: center; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 6px; padding: 10px 12px; box-shadow: 0 6px 24px rgb(0 0 0 / .25); }
.toast.bad { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }

/* ------------------------------------------------------------------ modal */
.modal-back { position: fixed; inset: 0; background: rgb(0 0 0 / .5); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 18px; width: min(560px, 100%); max-height: 90vh; overflow: auto; }
.modal h2 { font-size: 16px; }
.diff { font-family: var(--mono); font-size: 12.5px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; margin: 10px 0; }
.diff .from { color: var(--bad); text-decoration: line-through; }
.diff .to { color: var(--ok); }

/* ------------------------------------------------------------------ login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .panel { width: min(360px, 100%); display: grid; gap: 14px; }
.login .brand { font-size: 18px; font-weight: 700; }

/* ------------------------------------------------------------------ bars */
.bar { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; min-width: 60px; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bar.warn > i { background: var(--warn); }
.bar.bad > i { background: var(--bad); }

.stale { color: var(--warn); }

/* ------------------------------------------------------------------ signed values */
.pos { color: var(--ok); }
.neg { color: var(--bad); }
.bar.ok > i { background: var(--ok); }
.tile[title], th[title], .row[title] { cursor: help; }
.tile.click { cursor: pointer; }
.tile.click:hover { border-color: var(--accent); }

/* ------------------------------------------------------------------ host health */
.overall {
  display: flex; align-items: center; gap: 14px; padding: 14px 18px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--panel);
}
.overall .dot { width: 18px; height: 18px; border-radius: 50%; background: var(--idle); flex: none; }
.overall .big { font-size: 20px; font-weight: 700; }
.overall.ok { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 10%, var(--panel)); }
.overall.ok .dot { background: var(--ok); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 25%, transparent); }
.overall.ok .big { color: var(--ok); }
.overall.warn { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, var(--panel)); }
.overall.warn .dot { background: var(--warn); box-shadow: 0 0 0 4px color-mix(in srgb, var(--warn) 25%, transparent); }
.overall.warn .big { color: var(--warn); }
.overall.bad { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, var(--panel)); }
.overall.bad .dot { background: var(--bad); box-shadow: 0 0 0 4px color-mix(in srgb, var(--bad) 25%, transparent); }
.overall.bad .big { color: var(--bad); }

.grid.htiles { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
@media (max-width: 640px) { .grid.htiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; } }
.htile {
  border: 1px solid var(--line); border-left: 4px solid var(--idle); border-radius: var(--radius);
  padding: 12px 14px; background: var(--panel); display: grid; gap: 4px; min-width: 0;
}
.htile .head { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12.5px; }
.htile .head .state { margin-left: auto; font-weight: 600; }
.htile .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.htile .sub { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.htile.ok { border-left-color: var(--ok); background: color-mix(in srgb, var(--ok) 9%, var(--panel)); }
.htile.ok .value, .htile.ok .state { color: var(--ok); }
.htile.warn { border-left-color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, var(--panel)); }
.htile.warn .value, .htile.warn .state { color: var(--warn); }
.htile.bad { border-left-color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, var(--panel)); }
.htile.bad .value, .htile.bad .state { color: var(--bad); }
.htile.unknown .value { color: var(--muted); }
.trend { display: grid; grid-template-columns: max-content 1fr max-content; gap: 8px 14px; align-items: center; }
.trend svg { width: 100%; }
.tile.rot .value { font-size: 18px; }

/* ------------------------------------------------------------------ exposure (start / stop) */
h3 { font-size: 13.5px; margin: 4px 0 0; font-weight: 600; color: var(--muted); }
.xgrid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr)); }
.xcard { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); padding: 10px 12px; display: grid; gap: 8px; min-width: 0; align-content: start; }
.xhead { display: flex; gap: 10px; align-items: flex-start; }
.xname { flex: 1; min-width: 0; overflow-wrap: anywhere; font-weight: 600; }
.xname .muted { font-weight: 400; }
.xbody { display: grid; gap: 3px; font-size: 13px; }
.xline { overflow-wrap: anywhere; }
.xcard .notice { font-size: 12.5px; padding: 6px 10px; }

/* ------------------------------------------------------------------ charts */
.chart { position: relative; min-width: 0; }
.chart svg { display: block; touch-action: pan-y; }
.chart-tip { position: absolute; pointer-events: none; padding: 6px 9px; font-size: 12px; display: none; white-space: nowrap; z-index: 2; }
.empty.tight { padding: 8px; }
.grid.cards { grid-template-columns: repeat(auto-fill, minmax(min(320px, 100%), 1fr)); }
.panel.scard { display: grid; gap: 10px; cursor: pointer; }

/* ------------------------------------------------------------------ modal actions */
/* Title and buttons stay visible; only the body scrolls. */
.modal { display: flex; flex-direction: column; overflow: hidden; }
.modal > h2 { flex: none; }
.modal-body { min-width: 0; min-height: 0; flex: 1 1 auto; overflow: auto; overscroll-behavior: contain; }
.modal-actions { flex: none; display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.modal-actions .btn + .btn { margin-left: 0; }

/* ------------------------------------------------------------------ responsive */
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); }
.form-grid { grid-template-columns: repeat(auto-fill, minmax(min(230px, 100%), 1fr)); }
h1, .tile .value, .tile .sub, .xcard .mono { overflow-wrap: anywhere; }
td, .mono { overflow-wrap: break-word; }
.tile { min-width: 0; }
.diff > div { overflow-wrap: anywhere; }
nav.tabs { scrollbar-width: none; -webkit-overflow-scrolling: touch; }
nav.tabs::-webkit-scrollbar { display: none; }

/* Stacked "label: value" cards instead of tiny scrolling tables. */
@media (max-width: 720px) {
  table.rt thead { display: none; }
  table.rt td { overflow-wrap: anywhere; }
  table.rt, table.rt tbody, table.rt tr { display: block; width: 100%; }
  table.rt tr { padding: 10px 16px; border-bottom: 1px solid var(--line); }
  table.rt tr:last-child { border-bottom: 0; }
  table.rt td { display: grid; grid-template-columns: minmax(88px, 38%) minmax(0, 1fr); gap: 10px; align-items: baseline; justify-items: start; padding: 3px 0; border: 0; text-align: left; white-space: normal; }
  table.rt td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  table.rt td[data-label=""], table.rt td:first-child { grid-template-columns: minmax(0, 1fr); }
  table.rt td[data-label=""]::before, table.rt td:first-child::before { display: none; }
  table.rt td:first-child { font-weight: 600; padding-bottom: 6px; }
  table.rt td.actions { text-align: left; padding-top: 8px; }
  table.rt td[colspan] { display: block; }
  tbody tr.click:hover td { background: transparent; }
}
@media (min-width: 721px) and (max-width: 1100px) {
  table.rt-wide thead { display: none; }
  table.rt-wide td { overflow-wrap: anywhere; }
  table.rt-wide, table.rt-wide tr { display: block; width: 100%; }
  table.rt-wide tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  table.rt-wide tr { padding: 10px 16px; border-bottom: 1px solid var(--line); }
  table.rt-wide tr:nth-child(odd) { border-right: 1px solid var(--line); }
  table.rt-wide td { display: grid; grid-template-columns: minmax(110px, 40%) minmax(0, 1fr); gap: 10px; justify-items: start; padding: 3px 0; border: 0; text-align: left; white-space: normal; }
  table.rt-wide td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  table.rt-wide td[data-label=""], table.rt-wide td:first-child { grid-template-columns: minmax(0, 1fr); }
  table.rt-wide td[data-label=""]::before, table.rt-wide td:first-child::before { display: none; }
  table.rt-wide td:first-child { font-weight: 600; padding-bottom: 6px; }
  table.rt-wide td.actions { text-align: left; }
}

/* Phones: header wraps, tap targets >= 40 px, full-width modal buttons, toasts across the top. */
@media (max-width: 640px) {
  header.top { gap: 8px 12px; }
  header.top .meta { flex-basis: 100%; gap: 6px 12px; font-size: 12.5px; }
  header.top .meta .btn { margin-left: auto; }
  main { padding: 12px; }
  .panel { padding: 12px; }
  .panel.flush > h2 { padding: 12px 12px 0; }
  .page-head { margin-bottom: 12px; }
  .modal-back { padding: 8px; align-items: end; }
  .modal { padding: 14px; max-height: calc(100dvh - 16px); width: 100%; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }
  /* Top on phones so a toast never covers the bottom-sheet modal buttons. */
  .toasts { left: 8px; right: 8px; top: 8px; bottom: auto; max-width: none; }
  .row.end .btn { flex: 1 1 auto; }
  .trend { gap: 6px 10px; grid-template-columns: max-content minmax(0, 1fr) max-content; }
  .overall { padding: 12px; }
  .htile .value { font-size: 22px; }
}
@media (max-width: 720px), (pointer: coarse) {
  .btn, .btn.small { min-height: 40px; padding: 8px 14px; font-size: 14px; }
  nav.tabs a { padding: 12px 14px; min-height: 44px; display: inline-flex; align-items: center; }
  input[type=text], input[type=password], input[type=number], select { min-height: 40px; }
  textarea.note-input { font-size: 16px; } /* no iOS zoom on focus */
}

/* ------------------------------------------------------------------ latency (延遲) */
td.lv-warn { color: var(--warn); font-weight: 600; }
td.lv-bad { color: var(--bad); font-weight: 600; }
tr.group td { background: var(--panel-2); color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: .02em; padding-top: 6px; padding-bottom: 6px; }
.lat-hint { font-size: 11.5px; font-weight: 400; margin-top: 1px; }
.lat-note { padding: 4px 16px 0; font-size: 12px; }
@media (max-width: 720px) {
  tr.group td { padding: 6px 0; background: transparent; }
  table.rt tr.group { padding: 8px 16px 0; border-bottom: 0; }
}
