  :root {
    --bg: #0d0d0d;
    --surface: #161616;
    --surface2: #1e1e1e;
    --border: #2a2a2a;
    --text: #e0e0e0;
    --muted: #888;
    --green: #22c55e;
    --yellow: #eab308;
    --blue: #3b82f6;
    --red: #ef4444;
    --orange: #f97316;
    --accent: #6366f1;
    --profit: #22c55e;
    --loss: #ef4444;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body {
    background: var(--bg);
    color: var(--text);
    font-family: 'Segoe UI', system-ui, sans-serif;
    font-size: 14px;
    min-height: 100vh;
  }

  /* Engine state stripe */
  #engine-stripe {
    position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 200;
    background: #2a2a2a;
    transition: background 0.4s;
  }
  #engine-stripe.running { background: var(--green); }
  #engine-stripe.stopped { background: var(--red); }

  /* Layout */
  .topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 20px;
    margin-top: 3px;
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
    position: sticky;
    top: 3px;
    z-index: 100;
  }
  .topbar h1 { font-size: 16px; font-weight: 600; letter-spacing: 0.5px; }
  .app-version { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; margin-left: -10px; }
  .topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

  /* Engine chip in topbar */
  .engine-chip {
    display: inline-flex; align-items: center; gap: 7px;
    padding: 4px 10px; border-radius: 20px; font-size: 11px; font-weight: 600;
    letter-spacing: 0.4px; text-transform: uppercase;
    border: 1px solid var(--border); background: var(--surface2); color: var(--muted);
    transition: background 0.3s, border-color 0.3s, color 0.3s;
  }
  .engine-chip .ec-dot {
    width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex-shrink: 0;
  }
  .engine-chip.running { color: var(--green); border-color: rgba(34,197,94,0.35); background: rgba(34,197,94,0.08); }
  .engine-chip.stopped { color: var(--red);   border-color: rgba(239,68,68,0.35);  background: rgba(239,68,68,0.07);  }

  /* Tabs */
  .tabs {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 20px;
    display: flex;
    gap: 4px;
    position: sticky;
    top: 60px;
    z-index: 90;
    overflow-x: auto;
  }
  .tab {
    padding: 10px 18px;
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
    letter-spacing: 0.3px;
  }
  .tab:hover { color: var(--text); }
  .tab.active { color: var(--accent); border-bottom-color: var(--accent); }

  .main { padding: 20px; display: grid; gap: 16px; max-width: 1400px; margin: 0 auto; }
  .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
  .grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
  @media (max-width: 900px) {
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
  }

  .tab-pane { display: none; }
  .tab-pane.active { display: grid; gap: 16px; }

  /* Grid/flex items default to min-width:auto, which refuses to shrink below
     their content — so a wide table propagates its width up the tree and
     overflows the viewport, breaking the sticky header on horizontal scroll.
     min-width:0 lets each level shrink so the inner overflow-x:auto wrappers
     actually contain their tables. */
  .tab-pane, .grid-2, .grid-3 { min-width: 0; }
  .tab-pane > *, .grid-2 > *, .grid-3 > * { min-width: 0; }

  /* Cards */
  .card {
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
  }
  .card-title {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 1px;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 12px;
  }
  .card-subtitle {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 12px;
  }

  /* Status badge */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  .badge::before {
    content: '';
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: currentColor;
  }
  .badge-idle     { background: #1a2a1a; color: var(--green); }
  .badge-scanning { background: #1a2a1a; color: var(--green); }
  .badge-executing { background: #1a2a1a; color: var(--green); }
  .badge-transferring { background: #1a1a2a; color: var(--blue); }
  .badge-halted   { background: #2a1a1a; color: var(--red); }
  .badge-paused   { background: #2a2a1a; color: var(--yellow); }
  .badge-unknown  { background: #2a2a2a; color: var(--muted); }
  .badge-dry { background: #2a1a2a; color: var(--orange); font-size: 11px; }
  .badge-live { background: #1a2a1a; color: var(--green); font-size: 11px; }

  /* Exchange columns */
  .exchanges { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
  .exch-card { border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; background: rgba(255,255,255,0.015); }
  .exch-name { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 10px; }
  .exch-row { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 5px; font-size: 13px; }
  .exch-row span:first-child { color: var(--muted); min-width: 38px; }
  .exch-row span:last-child { font-variant-numeric: tabular-nums; font-weight: 500; }
  .exch-row-zero span { color: var(--muted); font-weight: 400; }
  .holdings-summary { display: flex; gap: 30px; flex-wrap: wrap; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }
  .holdings-summary > div { display: flex; flex-direction: column; gap: 3px; }
  .hs-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
  .hs-val { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }

  /* Spread panel */
  .spread-big { font-size: 36px; font-weight: 700; font-variant-numeric: tabular-nums; text-align: center; margin: 8px 0; }
  .spread-direction { text-align: center; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
  .depth-table { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
  .depth-table th { color: var(--muted); text-align: right; padding: 2px 6px; font-weight: 500; border-bottom: 1px solid var(--border); }
  .depth-table th:first-child { text-align: left; }
  .depth-table td { padding: 3px 6px; text-align: right; }
  .depth-table td:first-child { text-align: left; color: var(--muted); font-size: 11px; }
  .depth-bid td:nth-child(2) { color: var(--green); }
  .depth-ask td:nth-child(2) { color: var(--red); }
  .depth-bar-cell { position: relative; }
  .depth-bar { position: absolute; top: 2px; bottom: 2px; right: 0; border-radius: 2px; opacity: 0.18; pointer-events: none; }
  .depth-bar-bid { background: var(--green); }
  .depth-bar-ask { background: var(--red); }

  /* Engine status display */
  .engine-status-block {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 14px;
    background: var(--surface2);
    border: 1px solid var(--border);
    transition: border-color 0.3s, background 0.3s;
  }
  .engine-status-block.running {
    background: rgba(34,197,94,0.06);
    border-color: rgba(34,197,94,0.3);
  }
  .engine-status-block.stopped {
    background: rgba(239,68,68,0.05);
    border-color: rgba(239,68,68,0.2);
  }
  .engine-pulse {
    position: relative;
    width: 12px; height: 12px; flex-shrink: 0;
  }
  .engine-pulse-dot {
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--muted);
    transition: background 0.3s;
  }
  .engine-pulse-ring {
    position: absolute; inset: 0;
    border-radius: 50%;
    border: 2px solid transparent;
    opacity: 0;
  }
  .engine-status-block.running .engine-pulse-dot { background: var(--green); }
  .engine-status-block.running .engine-pulse-ring {
    border-color: var(--green);
    opacity: 1;
    animation: pulse-ring 1.8s ease-out infinite;
  }
  .engine-status-block.stopped .engine-pulse-dot { background: var(--red); }
  @keyframes pulse-ring {
    0%   { transform: scale(1);   opacity: 0.7; }
    100% { transform: scale(2.4); opacity: 0; }
  }
  .engine-status-text { flex: 1; min-width: 0; }
  .engine-status-label { font-size: 14px; font-weight: 600; }
  .engine-status-meta  { font-size: 12px; color: var(--muted); margin-top: 2px; }
  .btn-engine {
    padding: 8px 20px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: opacity 0.15s, transform 0.1s;
    white-space: nowrap;
    flex-shrink: 0;
  }
  .btn-engine:hover { opacity: 0.85; }
  .btn-engine:active { transform: scale(0.97); }
  .btn-engine:disabled { opacity: 0.4; cursor: not-allowed; }
  .btn-engine.start { background: var(--green); color: #0a0a0a; }
  .btn-engine.stop  { background: var(--red);   color: #fff; }

  /* Controls */
  .btn {
    padding: 7px 16px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface2);
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s;
  }
  .btn:hover { background: #2a2a2a; }
  .btn-green { border-color: var(--green); color: var(--green); }
  .btn-green:hover { background: rgba(34,197,94,0.1); }
  .btn-yellow { border-color: var(--yellow); color: var(--yellow); }
  .btn-yellow:hover { background: rgba(234,179,8,0.1); }
  .btn-red { border-color: var(--red); color: var(--red); }
  .btn-red:hover { background: rgba(239,68,68,0.1); }
  .btn:disabled { opacity: 0.4; cursor: not-allowed; }
  .controls-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }

  /* Toggle switch */
  .switch { position: relative; display: inline-block; width: 46px; height: 24px; }
  .switch input { opacity: 0; width: 0; height: 0; }
  .switch-slider {
    position: absolute; cursor: pointer; inset: 0;
    background: var(--surface2); border: 1px solid var(--border); border-radius: 24px;
    transition: 0.2s;
  }
  .switch-slider::before {
    position: absolute; content: "";
    height: 16px; width: 16px; left: 3px; top: 3px;
    background: var(--muted); border-radius: 50%; transition: 0.2s;
  }
  .switch input:checked + .switch-slider { border-color: var(--orange); background: rgba(249,115,22,0.15); }
  .switch input:checked + .switch-slider::before { transform: translateX(22px); background: var(--orange); }
  .switch-row { display: flex; align-items: center; gap: 12px; }

  /* Config rows */
  .config-row {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid #1a1a1a;
  }
  .config-row:last-child { border-bottom: none; }
  .config-row.modified { background: rgba(234,179,8,0.04); border-radius: 6px; padding: 10px 8px; margin: 0 -8px; }
  .config-label-col { flex: 1; min-width: 0; }
  .config-label { font-size: 13px; font-weight: 500; color: var(--text); }
  .config-desc  { font-size: 11px; color: var(--muted); margin-top: 2px; }
  .config-input-col { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
  .config-input {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 6px 10px;
    font-size: 13px;
    width: 110px;
    text-align: right;
    font-variant-numeric: tabular-nums;
  }
  .config-input:focus { outline: none; border-color: var(--accent); }
  .config-input.modified { border-color: var(--yellow); box-shadow: 0 0 0 2px rgba(234,179,8,0.15); }
  .config-meta { font-size: 11px; color: var(--muted); text-align: right; line-height: 1.5; min-width: 80px; }
  .config-save-btn {
    padding: 5px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--muted);
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    transition: border-color 0.15s, color 0.15s;
  }
  .config-save-btn:hover { border-color: var(--accent); color: var(--accent); }
  .config-save-btn.saved { border-color: var(--green); color: var(--green); }
  .config-group-sep { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); padding: 8px 0 4px; margin-top: 4px; border-top: 1px solid var(--border); }
  .config-group-sep:first-child { border-top: none; margin-top: 0; padding-top: 0; }

  /* Trade table */
  .trade-table { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
  .trade-table th {
    color: var(--muted); text-align: right; padding: 6px 8px; border-bottom: 1px solid var(--border);
    font-weight: 500; font-size: 11px; text-transform: uppercase; cursor: pointer; white-space: nowrap; user-select: none;
  }
  .trade-table th:first-child { text-align: left; }
  .trade-table th:hover { color: var(--text); }
  .trade-table td { padding: 6px 8px; text-align: right; border-bottom: 1px solid #1a1a1a; white-space: nowrap; }
  .trade-table td:first-child { text-align: left; color: var(--muted); }
  .trade-table tbody tr:hover { background: rgba(255,255,255,0.02); }
  .trade-table tbody tr:last-child td { border-bottom: none; }
  .sort-icon { font-size: 10px; margin-left: 2px; }

  /* PnL summary boxes */
  .pnl-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
  @media (max-width: 700px) { .pnl-grid { grid-template-columns: 1fr 1fr; } }
  .pnl-box { background: var(--surface2); border-radius: 6px; padding: 12px; text-align: center; }
  .pnl-box-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
  .pnl-box-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

  /* Misc */
  .refresh-indicator { font-size: 11px; color: var(--muted); }
  .auth-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 20px; font-size: 11px; background: var(--surface2); border: 1px solid var(--border); color: var(--muted); }
  .auth-chip.ok { color: var(--green); border-color: rgba(34,197,94,0.3); }
  .auth-chip.bad { color: var(--red); border-color: rgba(239,68,68,0.3); }
  .error-banner { background: rgba(239,68,68,0.1); border: 1px solid var(--red); border-radius: 6px; padding: 10px 14px; color: var(--red); margin-bottom: 12px; font-size: 13px; display: none; }
  .section-sep { border: none; border-top: 1px solid var(--border); margin: 4px 0 12px; }
  .imbalance-warn { color: var(--orange); font-size: 12px; margin-top: 6px; }
  .dir-box { background: rgba(255,255,255,0.03); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; flex: 1; min-width: 0; }
  .dir-box.opp { border-color: rgba(34,197,94,0.4); background: rgba(34,197,94,0.04); }
  .dir-box.nofill { opacity: 0.55; }
  .dir-box-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin-bottom: 8px; }
  .dir-row { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 3px; }
  .dir-row span:first-child { color: var(--muted); }
  .formula-block { background: rgba(0,0,0,0.25); border: 1px solid rgba(255,255,255,0.07); border-radius: 4px; padding: 8px 10px; margin-top: 10px; font-size: 11px; font-family: monospace; line-height: 1.7; }
  .formula-step { color: var(--muted); }
  .formula-step b { color: var(--text); font-weight: 500; }
  .formula-result { color: var(--text); font-weight: 600; }
  .pair-bal-row { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 3px; }
  .tag-dry { display: inline-block; background: rgba(249,115,22,0.15); color: var(--orange); border-radius: 3px; font-size: 10px; padding: 1px 5px; margin-left: 4px; vertical-align: middle; }
  .val-pos { color: var(--green); }
  .val-neg { color: var(--red); }
  .val-warn { color: var(--yellow); }
  .intent-fill { width: 92px; }
  .cycle-time { font-size: 12px; color: var(--muted); }
  .no-data { color: var(--muted); font-size: 12px; text-align: center; padding: 24px 0; }
  .halt-reason { font-size: 12px; color: var(--red); margin-top: 4px; }
  .spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
  .env-table { width: 100%; border-collapse: collapse; font-size: 13px; }
  .env-table td { padding: 4px 6px; vertical-align: middle; }
  .env-input {
    width: 100%; background: var(--surface2); color: var(--text);
    border: 1px solid var(--border); border-radius: 4px; padding: 4px 6px;
    font-family: Consolas, monospace; font-size: 12px;
  }
  /* Mask secret fields visually WITHOUT type="password", so Chromium never
     offers to save them as a login. Applied to text inputs. */
  .masked { -webkit-text-security: disc; text-security: disc; }
  .key-cell { color: var(--muted); white-space: nowrap; font-family: Consolas, monospace; font-size: 12px; }
  .hint { font-size: 12px; color: var(--muted); }
  .hint.warn { color: var(--orange); }
  .section-group { display: grid; gap: 16px; }

  /* Portal adapter test pane */
  .ad-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
  @media (max-width: 640px) { .ad-grid { grid-template-columns: 1fr; } }
  .ad-field-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
  .ad-row { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 10px; overflow: hidden; }
  .ad-row:last-child { margin-bottom: 0; }
  .ad-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--surface2); flex-wrap: wrap; }
  .ad-method { font: 700 11px Consolas, monospace; padding: 2px 8px; border-radius: 4px; }
  .ad-method.get { background: rgba(59,130,246,0.15); color: var(--blue); }
  .ad-method.post { background: rgba(249,115,22,0.15); color: var(--orange); }
  .ad-path { font-size: 12px; color: var(--text); flex: 1; min-width: 150px; }
  .ad-pill { font: 700 11px Consolas, monospace; padding: 2px 9px; border-radius: 20px; border: 1px solid var(--border); color: var(--muted); }
  .ad-pill.ok  { background: rgba(34,197,94,0.12); color: var(--green); border-color: transparent; }
  .ad-pill.err { background: rgba(239,68,68,0.12); color: var(--red);   border-color: transparent; }
  .ad-body { border-top: 1px solid var(--border); }
  .ad-pre { margin: 0; padding: 12px; background: var(--bg); color: var(--text); font: 12px/1.5 Consolas, monospace; overflow-x: auto; white-space: pre; max-height: 340px; }
  .ad-pre.bad { color: var(--red); }
  .ad-metrics { width: 100%; border-collapse: collapse; font-size: 12px; }
  .ad-metrics td { padding: 6px 12px; border-bottom: 1px solid var(--border); font-family: Consolas, monospace; }
  .ad-metrics tr:last-child td { border-bottom: 0; }
  .ad-metrics td:first-child { color: var(--muted); width: 46%; }
  .ad-good { color: var(--green); } .ad-bad { color: var(--red); } .ad-flag { color: var(--orange); }

  /* ── Login gate ────────────────────────────────────────────────────────── */
  .login-overlay {
    position: fixed; inset: 0; z-index: 500;
    display: flex; align-items: center; justify-content: center;
    background: var(--bg);
    padding: 20px;
  }
  .login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 28px;
    width: 100%; max-width: 360px;
  }
  .login-card h2 { font-size: 20px; margin-bottom: 4px; }
  .login-sub { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
  .login-card label {
    display: block; color: var(--muted); font-size: 12px; margin-bottom: 5px;
  }
  .login-card input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-size: 14px;
    padding: 9px 10px;
    margin-bottom: 10px;
  }
  .login-card input:focus { outline: none; border-color: var(--accent); }
  .login-btn {
    width: 100%;
    background: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 4px;
    color: #fff;
    cursor: pointer;
    font-size: 14px;
    padding: 9px;
  }
  .login-btn:disabled { opacity: 0.6; cursor: default; }
  .login-error {
    background: rgba(239,68,68,0.12);
    border-radius: 4px;
    color: var(--red);
    font-size: 12px;
    margin-top: 14px;
    padding: 9px 10px;
  }

  /* Viewer mode — presentation only. The server refuses a viewer's writes
     regardless of what is hidden here; this keeps the UI honest, not safe. */
  body.viewer-mode .operator-only { display: none !important; }
