:root {
  color-scheme: light;
  --ink: #172126;
  --muted: #66747b;
  --line: #d8dfe2;
  --surface: #ffffff;
  --canvas: #f3f5f5;
  --green: #087f5b;
  --green-soft: #e8f5f0;
  --red: #c0392b;
  --red-soft: #fff0ed;
  --amber: #9a6700;
  --amber-soft: #fff7dc;
  --blue: #1769aa;
}

* { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
body { margin: 0; background: var(--canvas); color: var(--ink); font: 14px/1.45 "Segoe UI", "Microsoft YaHei", sans-serif; letter-spacing: 0; }
body:not([data-role="admin"]) .admin-only, body[data-role="viewer"] .operator-only { display: none !important; }
.top-link { color: #fff; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; }
.top-link svg { width: 17px; height: 17px; }
.auth-user { color: #d8e0e3; }
.auth-main { min-height: calc(100vh - 68px); display: grid; place-items: center; }
.auth-panel { padding: 20px; background: #fff; border: 1px solid var(--line); border-radius: 6px; }
.auth-panel h2 { margin: 0 0 18px; font-size: 18px; }
.auth-panel .field { margin: 0 0 15px; }
.auth-main .auth-panel { width: min(380px, 100%); }
.auth-main .command { width: 100%; }
.users-main { max-width: 1000px; display: grid; gap: 15px; }
.user-form { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) auto; align-items: end; gap: 12px; }
.user-form .field { margin: 0; }
.user-actions { display: flex; flex-wrap: wrap; gap: 5px; }
.reset-dialog { width: min(420px, calc(100vw - 24px)); }
@media (max-width: 700px) { .user-form { grid-template-columns: 1fr; } .top-actions { gap: 7px; } }
button, input, select { font: inherit; }
.topbar { min-height: 68px; padding: 12px 24px; display: flex; align-items: center; justify-content: space-between; gap: 16px; background: #152127; color: #fff; border-bottom: 3px solid #d3a62c; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { width: 38px; height: 38px; display: grid; place-items: center; flex: 0 0 auto; border: 1px solid #708087; color: #f3c955; font-weight: 800; }
h1 { margin: 0; font-size: 18px; line-height: 1.25; }
.brand p { margin: 3px 0 0; color: #aebbc0; font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 14px; flex: 0 0 auto; }
.auto-refresh { display: flex; align-items: center; gap: 7px; color: #d8e0e3; font-size: 13px; }
.auto-refresh input { accent-color: #d3a62c; }
.icon-button { width: 36px; height: 36px; display: grid; place-items: center; border: 1px solid #53636a; background: transparent; color: #fff; cursor: pointer; }
.icon-button:hover { background: #26363d; }
.icon-button svg { width: 17px; height: 17px; }
.fallback-icon { font-size: 21px; line-height: 1; }
.lucide + .fallback-icon { display: none; }
main { width: 100%; max-width: 1500px; min-width: 0; margin: 0 auto; padding: 18px 24px 28px; }
.tasks-panel { width: 100%; min-width: 0; margin-bottom: 12px; overflow: hidden; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.tasks-heading { padding: 14px 16px 11px; }
.task-table-wrap { width: 100%; max-width: 100%; overflow-x: auto; }
.task-table { min-width: 1040px; }
.task-table th { position: static; }
.task-table td { vertical-align: middle; }
.task-table td > strong, .task-table td > small { display: block; }
.task-table td > small { margin-top: 2px; color: var(--muted); font-size: 11px; }
.task-table .empty-row td { height: 82px; }
.task-table tr[data-config] { cursor: pointer; }
.task-table tr[data-config]:hover { background: #f5f8fa; }
.task-table tr.active-task { background: #f2faf7; }
.task-table tr.selected-task { background: #e8f2f7; box-shadow: inset 3px 0 0 var(--accent); }
.task-table tr.selected-task.active-task { background: #e5f5ef; }
.task-table tr[data-config]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.task-actions-heading { width: 104px; }
.task-row-actions { white-space: nowrap; }
.task-action { width: 34px; height: 34px; margin-right: 5px; display: inline-grid; place-items: center; border: 1px solid #b7c1c5; border-radius: 3px; background: #fff; color: #526168; cursor: pointer; }
.task-action svg { width: 15px; height: 15px; }
.task-action.primary { border-color: var(--green); color: var(--green); }
.task-action.danger { border-color: var(--red); color: var(--red); }
.task-action:hover { background: #eef2f3; }
.task-state { display: inline-block; padding: 3px 7px; border-radius: 3px; background: #edf0f1; color: var(--muted); font-size: 12px; font-weight: 700; white-space: nowrap; }
.task-state.running { background: var(--green-soft); color: var(--green); }
.tasks-panel > .control-message { margin: 6px 16px 8px; }
.control-panel { width: 100%; min-width: 0; margin-bottom: 12px; padding: 16px 18px 18px; overflow: hidden; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.control-heading { padding-bottom: 12px; }
.control-actions { display: flex; gap: 8px; }
.command { min-height: 36px; padding: 7px 14px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid #aab6ba; border-radius: 4px; background: #fff; color: var(--ink); font-weight: 600; cursor: pointer; }
.command svg { width: 16px; height: 16px; }
.command.primary { border-color: #087f5b; background: #087f5b; color: #fff; }
.command.danger { border-color: #b63529; background: #b63529; color: #fff; }
.command:disabled { border-color: #d3dadd; background: #e8ecee; color: #89969b; cursor: not-allowed; }
#control-form { padding-top: 14px; display: grid; grid-template-columns: repeat(6, minmax(120px, 1fr)); gap: 13px 14px; align-items: end; }
.field { min-width: 0; }
.field.wide { grid-column: span 2; }
.field > label, .field legend { display: block; margin-bottom: 5px; color: #526168; font-size: 12px; font-weight: 600; }
.field input, .field select { width: 100%; height: 36px; padding: 6px 9px; border: 1px solid #b9c4c8; border-radius: 3px; background: #fff; color: var(--ink); outline: none; }
.field input:focus, .field select:focus { border-color: var(--blue); box-shadow: 0 0 0 2px #dcecf8; }
.field input:disabled, .field select:disabled { background: #edf0f1; color: #69767c; }
.mode-field { margin: 0; padding: 0; border: 0; }
.segmented { height: 36px; display: grid; grid-template-columns: repeat(2, 1fr); border: 1px solid #b9c4c8; border-radius: 3px; overflow: hidden; }
.segmented label { position: relative; min-width: 0; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { height: 100%; display: grid; place-items: center; color: var(--muted); cursor: pointer; }
.segmented label + label span { border-left: 1px solid #b9c4c8; }
.segmented input:checked + span { background: #26363d; color: #fff; font-weight: 600; }
.segmented input:disabled + span { background: #edf0f1; color: #89969b; cursor: not-allowed; }
.contract-input { height: 36px; display: grid; grid-template-columns: auto minmax(52px, 1fr); border: 1px solid #b9c4c8; border-radius: 3px; overflow: hidden; }
.contract-input span { padding: 7px 8px; background: #edf0f1; color: #59686e; font-size: 12px; white-space: nowrap; }
.contract-input input { height: 34px; border: 0; border-left: 1px solid #cbd3d6; border-radius: 0; }
.limit-option.inactive { opacity: .48; }
.toggle-field { min-height: 51px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 7px 0; cursor: pointer; }
.toggle-field > span { min-width: 0; }
.toggle-field strong, .toggle-field small { display: block; }
.toggle-field strong { font-size: 12px; }
.toggle-field small { color: var(--muted); font-size: 11px; }
.toggle-field input { position: absolute; opacity: 0; }
.toggle-field i { width: 38px; height: 22px; padding: 2px; flex: 0 0 auto; border-radius: 11px; background: #aab5ba; transition: background .15s; }
.toggle-field i::after { content: ""; display: block; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle-field input:checked + i { background: var(--green); }
.toggle-field input:checked + i::after { transform: translateX(16px); }
.toggle-field input:disabled + i { opacity: .5; }
.control-message { min-height: 20px; margin: 9px 0 -6px; color: var(--muted); font-size: 12px; }
.control-message.success { color: var(--green); }
.control-message.error { color: var(--red); }
.account-summary, .positions-panel, .orders-panel, .history-panel { margin-top: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.account-heading, .positions-panel > .section-heading, .orders-heading, .history-heading { padding: 14px 16px 11px; }
.history-heading > span { color: var(--muted); font-size: 12px; }
.history-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border-top: 1px solid var(--line); }
.history-columns > section { min-width: 0; }
.history-columns > section + section { border-left: 1px solid var(--line); }
.snapshot-state { padding: 3px 8px; border-radius: 3px; font-size: 12px; font-weight: 700; }
.snapshot-state.fresh { color: var(--green); background: var(--green-soft); }
.snapshot-state.stale { color: var(--amber); background: var(--amber-soft); }
.fund-sides { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.fund-side { min-width: 0; padding: 14px 16px 16px; }
.fund-side + .fund-side { border-left: 1px solid var(--line); }
.side-title { display: flex; align-items: center; gap: 9px; }
.side-title h3 { margin: 0; font-size: 14px; }
.side-title small { color: var(--muted); }
.platform-mark { width: 30px; height: 30px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 3px; color: #fff; font-size: 11px; font-weight: 800; }
.platform-mark.mt5 { background: #1769aa; }
.platform-mark.es { background: #9a6700; }
.fund-totals { margin: 13px 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.fund-totals div { min-width: 0; padding-left: 9px; border-left: 2px solid #cbd4d7; }
.fund-totals dt { color: var(--muted); font-size: 11px; }
.fund-totals dd { margin: 2px 0 0; font-size: 16px; font-weight: 700; overflow-wrap: anywhere; }
.mini-table-wrap { overflow: auto; border: 1px solid #e0e5e7; }
.mini-table { min-width: 570px; }
.mini-table th { position: static; }
.mini-table th, .mini-table td { padding: 7px 9px; }
.mini-table .empty-row td { height: 64px; }
.account-errors { display: none; margin: 0; padding: 10px 16px; border-top: 1px solid #f0cbc5; background: var(--red-soft); color: var(--red); font-size: 12px; }
.account-errors.visible { display: block; }
.reconcile-strip { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); border-bottom: 1px solid var(--line); }
.reconcile-strip > div { min-width: 0; padding: 11px 15px; border-right: 1px solid var(--line); }
.reconcile-strip > div:last-child { border-right: 0; }
.reconcile-strip span, .reconcile-strip strong { display: block; }
.reconcile-strip span { color: var(--muted); font-size: 11px; }
.reconcile-strip strong { margin-top: 2px; font-size: 16px; overflow-wrap: anywhere; }
.match-result strong.matched { color: var(--green); }
.match-result strong.mismatch { color: var(--red); }
.position-sides { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.position-sides > section { min-width: 0; }
.position-sides > section + section { border-left: 1px solid var(--line); }
.table-title { min-height: 42px; padding: 11px 14px 8px; display: flex; align-items: center; justify-content: space-between; }
.table-title span { color: var(--muted); font-size: 12px; }
.table-wrap.compact { min-height: 126px; max-height: 320px; }
.table-wrap.compact table { min-width: 700px; }
.table-wrap.compact .empty-row td { height: 90px; }
.orders-heading { padding-bottom: 11px; }
.order-sides { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.order-sides > section { min-width: 0; }
.order-sides > section + section { border-left: 1px solid var(--line); }
.side-label { min-width: 28px; display: inline-block; font-weight: 700; }
.side-label.buy { color: var(--red); }
.side-label.sell { color: var(--green); }
.status-band { display: flex; align-items: center; gap: 30px; padding: 17px 20px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.primary-state { min-width: 190px; display: flex; align-items: center; gap: 12px; border-right: 1px solid var(--line); }
.primary-state div { display: flex; flex-direction: column; }
.primary-state strong { font-size: 17px; }
.primary-state span:last-child { color: var(--muted); font-size: 12px; }
.state-dot { width: 13px; height: 13px; border-radius: 50%; background: #89969b; box-shadow: 0 0 0 4px #edf0f1; }
.state-dot.online { background: var(--green); box-shadow: 0 0 0 4px var(--green-soft); }
.state-dot.error { background: var(--red); box-shadow: 0 0 0 4px var(--red-soft); }
.runtime-facts { margin: 0; flex: 1; display: grid; grid-template-columns: repeat(4, minmax(110px, 1fr)); gap: 16px; }
.runtime-facts div { min-width: 0; }
.runtime-facts dt { color: var(--muted); font-size: 12px; }
.runtime-facts dd { margin: 3px 0 0; font-weight: 700; overflow-wrap: anywhere; }
.connection-strip { margin: 12px 0; padding: 10px 18px; display: flex; align-items: center; background: #e9edef; border: 1px solid var(--line); }
.connection-strip > div { min-width: 205px; display: grid; grid-template-columns: 12px auto; column-gap: 9px; align-items: center; }
.connection-strip small { grid-column: 2; color: var(--muted); }
.connection-dot { width: 9px; height: 9px; border-radius: 50%; background: #89969b; }
.connection-dot.connected { background: var(--green); }
.flow-line { width: 72px; height: 1px; margin: 0 18px; background: #a9b4b8; position: relative; }
.flow-line::after { content: ""; position: absolute; right: 0; top: -3px; border-left: 6px solid #78878d; border-top: 3px solid transparent; border-bottom: 3px solid transparent; }
.metrics { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.metrics article { min-height: 108px; padding: 15px 17px; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--blue); border-radius: 6px; }
.metrics article:nth-child(2) { border-top-color: #6f42c1; }
.metrics article:nth-child(3) { border-top-color: var(--green); }
.metrics article.metric-alert { border-top-color: var(--red); }
.metrics span, .metrics small { display: block; color: var(--muted); }
.metrics strong { display: block; margin: 3px 0 1px; font-size: 27px; line-height: 1.2; }
.workspace { margin-top: 12px; display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 12px; align-items: start; }
.context-panel, .events-panel { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.context-panel { padding: 16px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-bottom: 11px; border-bottom: 1px solid var(--line); }
.section-heading h2 { margin: 0; font-size: 15px; }
.section-heading p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.context-group { margin-top: 16px; }
.context-group > span, .last-error > span { display: block; margin-bottom: 8px; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; }
.tag-list { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-list b { padding: 3px 7px; border: 1px solid #bec9cd; background: #f7f9f9; font-size: 12px; font-weight: 600; }
.route-list { display: grid; gap: 7px; }
.route-list div { padding: 8px 9px; background: #f5f7f7; border-left: 3px solid #d3a62c; }
.route-list strong, .route-list small { display: block; }
.route-list small { color: var(--muted); }
.last-error { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.last-error p { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.events-panel { min-width: 0; }
.events-heading { padding: 14px 16px 11px; }
.filters { display: flex; border: 1px solid var(--line); }
.filter { min-width: 52px; padding: 6px 10px; border: 0; border-right: 1px solid var(--line); background: #fff; cursor: pointer; color: var(--muted); }
.filter:last-child { border-right: 0; }
.filter.active { background: #26363d; color: #fff; }
.table-wrap { overflow: auto; min-height: 330px; max-height: 58vh; }
table { width: 100%; border-collapse: collapse; min-width: 850px; }
th { position: sticky; top: 0; z-index: 1; padding: 9px 11px; background: #eef1f2; color: #536168; text-align: left; font-size: 12px; font-weight: 600; }
td { padding: 10px 11px; border-top: 1px solid #e8ecee; vertical-align: top; }
tbody tr:hover { background: #fafbfb; }
.event-title { display: block; font-weight: 600; }
.event-message { display: block; max-width: 260px; color: var(--muted); font-size: 12px; }
.status-label { display: inline-block; padding: 2px 6px; border-radius: 3px; font-size: 12px; background: #edf0f1; }
.status-label.info { color: var(--blue); background: #eaf3fa; }
.status-label.warning { color: var(--amber); background: var(--amber-soft); }
.status-label.error { color: var(--red); background: var(--red-soft); }
.empty-row td { height: 220px; color: var(--muted); text-align: center; vertical-align: middle; }
em { color: var(--muted); font-style: normal; }
dialog { width: min(440px, calc(100vw - 28px)); padding: 0; border: 1px solid #9ca9ae; border-radius: 6px; box-shadow: 0 18px 55px #0d171c55; }
dialog::backdrop { background: #101a1f99; }
.confirm-dialog { padding: 22px; display: grid; grid-template-columns: 38px 1fr; gap: 15px 12px; }
.dialog-icon { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; background: var(--red-soft); color: var(--red); }
.dialog-icon svg { width: 20px; }
.confirm-dialog h2 { margin: 0; font-size: 17px; }
.confirm-dialog p { margin: 5px 0 0; color: var(--muted); }
.confirm-dialog > label, .confirm-dialog > input, .dialog-actions { grid-column: 1 / -1; }
.confirm-dialog > label { margin-bottom: -10px; color: #526168; font-size: 12px; font-weight: 600; }
.confirm-dialog > input { height: 38px; padding: 7px 9px; border: 1px solid #b9c4c8; border-radius: 3px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.task-dialog { width: min(1040px, calc(100vw - 30px)); max-height: calc(100vh - 30px); overflow: auto; }
.task-form { padding: 0 20px 20px; }
.task-dialog-heading { position: sticky; top: 0; z-index: 3; margin: 0 -20px; padding: 16px 20px 13px; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); background: #fff; }
.task-dialog-heading h2 { margin: 0; font-size: 18px; }
.task-dialog-heading p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.task-dialog-heading .icon-button { flex: 0 0 auto; border-color: #b9c4c8; color: #526168; }
.task-dialog-heading .icon-button:hover { background: #eef2f3; }
.form-section { padding: 15px 0 16px; border-bottom: 1px solid var(--line); }
.form-section h3 { margin: 0 0 11px; font-size: 13px; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; align-items: end; }
.settings-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.compact-toggle { min-height: 36px; padding: 0; }
.task-form > .dialog-actions { position: sticky; bottom: -20px; margin: 0 -20px -20px; padding: 14px 20px; border-top: 1px solid var(--line); background: #fff; }
.task-form > .control-message { margin: 8px 0 0; }
.positions-heading { padding-right: 16px; }
.inline-command { min-height: 30px; padding: 4px 9px; font-size: 12px; }
.manual-dialog { width: min(760px, calc(100vw - 30px)); }
.manual-dialog .task-form { padding-top: 0; }
.manual-confirm-field { opacity: .55; }
.manual-confirm-field.active { opacity: 1; }

@media (max-width: 900px) {
  #control-form { grid-template-columns: repeat(3, minmax(120px, 1fr)); }
  .runtime-facts { grid-template-columns: repeat(2, minmax(110px, 1fr)); }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .workspace { grid-template-columns: 1fr; }
  .fund-sides, .position-sides, .history-columns { grid-template-columns: 1fr; }
  .fund-side + .fund-side, .position-sides > section + section, .history-columns > section + section { border-left: 0; border-top: 1px solid var(--line); }
  .context-panel { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 18px; }
  .context-panel .section-heading, .last-error { grid-column: 1 / -1; }
  .settings-grid.three { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .order-sides { grid-template-columns: 1fr; }
  .order-sides > section + section { border-left: 0; border-top: 1px solid var(--line); }
}

@media (max-width: 600px) {
  .topbar { padding: 11px 14px; }
  .brand-mark { display: none; }
  h1 { font-size: 16px; }
  .auto-refresh { font-size: 0; gap: 0; }
  main { padding: 12px; }
  .tasks-heading { align-items: flex-start; }
  .tasks-heading .command { width: 38px; padding: 7px; font-size: 0; }
  .control-panel { padding: 14px; }
  .control-heading { align-items: flex-start; }
  .control-actions .command { width: 38px; padding: 7px; font-size: 0; }
  #control-form { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 9px; }
  .field.wide { grid-column: 1 / -1; }
  .field input, .field select { min-width: 0; }
  .contract-input { display: block; height: auto; border: 0; overflow: visible; }
  .contract-input span { display: block; padding: 0 0 3px; background: transparent; }
  .contract-input input { height: 36px; border: 1px solid #b9c4c8; border-radius: 3px; }
  .status-band { display: block; padding: 14px; }
  .primary-state { padding-bottom: 13px; border-right: 0; border-bottom: 1px solid var(--line); }
  .runtime-facts { margin-top: 12px; gap: 12px; }
  .connection-strip > div { min-width: 0; flex: 1; }
  .flow-line { width: 24px; margin: 0 9px; }
  .metrics { gap: 8px; }
  .metrics article { min-height: 96px; padding: 12px; }
  .metrics strong { font-size: 23px; }
  .fund-totals { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fund-totals dd { font-size: 15px; }
  .reconcile-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .reconcile-strip > div { border-bottom: 1px solid var(--line); }
  .reconcile-strip > div:nth-child(2n) { border-right: 0; }
  .reconcile-strip > div:last-child { grid-column: 1 / -1; border-bottom: 0; }
  .orders-heading { display: block; padding-top: 12px; }
  .context-panel { display: block; }
  .events-heading { display: block; }
  .filters { margin-top: 10px; width: max-content; }
  .task-dialog { width: calc(100vw - 16px); max-height: calc(100vh - 16px); }
  .task-form { padding: 0 14px 16px; }
  .task-dialog-heading { margin: 0 -14px; padding: 14px; }
  .settings-grid, .settings-grid.three { grid-template-columns: 1fr; }
  .settings-grid .field.wide { grid-column: auto; }
  .task-form > .dialog-actions { bottom: -16px; margin: 0 -14px -16px; padding: 12px 14px; }
}
