:root {
  --bg: #ffffff;
  --surface: #fafafa;
  --surface2: #f4f4f5;
  --border: #e5e5e5;
  --border-strong: #d4d4d4;
  --text: #111111;
  --muted: #6b6b6b;
  --dim: #9a9a9a;
  --accent: #2a7c13;
  --accent-soft: #eaf2e7;
  --buy: #16a34a;
  --sell: #dc2626;
  --warn: #b45309;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sidebar-w: 220px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; font-size: inherit; cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

/* ---------------------------------------------------------------- login --- */
/* Split screen: brand panel on the left, form on the right. */
.login-wrap {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.login-brand {
  background: var(--accent);
  color: #fff;
  padding: 40px 44px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.login-brand .mark {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.login-brand .mark .glyph {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.22);
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
}
.login-brand .pitch h2 {
  font-size: 30px;
  line-height: 1.22;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 14px;
  max-width: 14em;
}
.login-brand .pitch p {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.82);
  max-width: 30em;
}
.login-brand .foot {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 11.5px;
  color: rgba(255, 255, 255, 0.72);
}

.login-panel {
  display: grid;
  place-items: center;
  padding: 40px 24px;
  background: var(--bg);
}
.login-card {
  width: min(360px, 100%);
}
.login-card h1 {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
}
.login-card .sub {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
  margin: 0 0 26px;
}

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 6px;
}
.field .control { position: relative; }
.field input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}
.field input:focus { outline: none; border-color: var(--accent); }
.field input.has-toggle { padding-right: 64px; }

.field .toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  padding: 6px 8px;
  border-radius: 5px;
}
.field .toggle:hover { background: var(--surface2); color: var(--text); }

.login-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0 18px;
}
.login-row label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--muted);
}
.login-row input[type="checkbox"] { accent-color: var(--accent); margin: 0; }
.login-row .hint { font-size: 12px; color: var(--dim); }

.btn-primary {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  font-size: 14px;
}
.btn-primary:hover { filter: brightness(0.94); }
.btn-primary:disabled { opacity: 0.55; cursor: default; filter: none; }

.error {
  color: var(--sell);
  font-size: 12.5px;
  min-height: 18px;
  margin-top: 12px;
}

@media (max-width: 820px) {
  .login-wrap { grid-template-columns: 1fr; }
  .login-brand { padding: 22px 20px; }
  .login-brand .pitch { margin-top: 18px; margin-bottom: 18px; }
  .login-brand .pitch h2 { font-size: 20px; margin-bottom: 8px; }
  .login-brand .pitch p { font-size: 12.5px; }
  .login-brand .foot { display: none; }
  .login-panel { padding: 28px 20px 40px; place-items: start center; }
}


/* ----------------------------------------------------------------- shell --- */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  padding: 16px 12px 12px;
  min-height: 0;
}
.sidebar .brand {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 0 8px 2px;
}
.sidebar .brand span { color: var(--accent); }
.sidebar .tagline {
  padding: 0 8px 16px;
  font-size: 11px;
  color: var(--dim);
}

.nav { display: flex; flex-direction: column; gap: 1px; }
.nav-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
}
.nav-btn:hover { background: var(--surface2); color: var(--text); }
.nav-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }

.spacer { flex: 1; min-height: 12px; }

.controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}
.controls-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--dim);
  margin-bottom: 2px;
  padding-left: 2px;
}
.btn {
  width: 100%;
  padding: 10px 12px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.15s ease-in-out;
}
.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}
.btn:active:not(:disabled) {
  transform: translateY(0);
}
.btn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
  box-shadow: none;
}
.btn-indicator {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.btn-start {
  border-color: #2a7c13;
  color: #2a7c13;
  background: rgba(42, 124, 19, 0.07);
}
.btn-start:hover:not(:disabled) {
  background: rgba(42, 124, 19, 0.16);
  border-color: #2a7c13;
}
.btn-pause {
  border-color: #c48000;
  color: #c48000;
  background: rgba(196, 128, 0, 0.07);
}
.btn-pause:hover:not(:disabled) {
  background: rgba(196, 128, 0, 0.16);
  border-color: #c48000;
}
.btn-stop {
  border-color: #c92a2a;
  color: #c92a2a;
  background: rgba(201, 42, 42, 0.07);
}
.btn-stop:hover:not(:disabled) {
  background: rgba(201, 42, 42, 0.16);
  border-color: #c92a2a;
}
.dot-pause {
  background: #c48000;
}
.btn-inline {
  width: auto;
  display: inline-flex;
  padding: 8px 16px;
}
.sheet-panel {
  margin-bottom: 24px;
  padding: 18px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  box-sizing: border-box;
}
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.sheet-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}
.sheet-link {
  font-size: 13px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.sheet-link:hover {
  text-decoration: underline;
}
.sheet-iframe {
  width: 100% !important;
  min-width: 100% !important;
  height: 850px !important;
  min-height: 800px !important;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  display: block;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.account {
  border-top: 1px solid var(--border);
  padding: 12px 8px 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.account .who { min-width: 0; }
.account .name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.account .role {
  font-size: 11px;
  color: var(--dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.link-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 11px;
}
.link-btn:hover { background: var(--surface2); color: var(--text); }

/* ------------------------------------------------------------------ main --- */
.main { padding: 22px 26px; overflow-y: auto; min-width: 0; }
.head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 18px; }
.head h1 { font-size: 16px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.head .clock { color: var(--dim); font-family: var(--mono); font-size: 12px; }

.view { display: none; }
.view.active { display: block; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}
.card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}
.card .k {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--dim);
  margin-bottom: 8px;
}
.card .v { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.card .v.small { font-size: 14px; font-weight: 500; }

.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 7px; vertical-align: 1px; }
.dot-run { background: var(--buy); }
.dot-stop { background: var(--dim); }

.section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--dim);
  margin: 0 0 10px;
}

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--dim);
  font-weight: 500;
  padding: 0 10px 8px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tr:hover td { background: var(--surface); }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.sym { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--muted); }
.dim { color: var(--dim); }

.buy { color: var(--buy); font-weight: 500; }
.sell { color: var(--sell); font-weight: 500; }

.chip {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
  background: var(--surface2);
  color: var(--muted);
}
.chip-matched { background: #eaf7ee; color: var(--buy); }
.chip-failed { background: #fdeeee; color: var(--sell); }
.chip-unmatched { background: #fdf6e9; color: var(--warn); }

.empty { color: var(--dim); padding: 26px 10px; text-align: center; }

.toolbar { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input, .toolbar select {
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
}
.toolbar button {
  padding: 7px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}
.toolbar button:hover { background: var(--surface2); }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

/* Positions: one column per broker. auto-fit collapses 3 -> 2 -> 1 on its own,
   so the three brokers never get squashed side by side on a narrow screen. */
.three-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 22px; }

/* Totals row at the foot of each broker's position table. */
tfoot td {
  border-bottom: 0;
  border-top: 1px solid var(--border-strong);
  padding-top: 10px;
  font-weight: 600;
}
tfoot tr:hover td { background: transparent; }

/* Per-child on/off switches. */
.toggle-row { display: flex; flex-wrap: wrap; gap: 10px 26px; }
.toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0;
  cursor: pointer;
  user-select: none;
}
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  width: 38px;
  height: 21px;
  border-radius: 11px;
  border: 1px solid var(--border-strong);
  background: var(--surface2);
  position: relative;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  flex: none;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  transition: transform 0.15s ease;
}
.toggle input:checked {
  background: var(--accent);
  border-color: var(--accent);
}
.toggle input:checked::after {
  transform: translateX(17px);
  border-color: var(--accent);
}
.toggle input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.toggle span { font-size: 12px; }
.toggle-help { font-size: 11px; line-height: 1.5; margin-top: 10px; }

/* Client accounts never see the development views. Set on <body> by app.js from
   the role in the session; the API refuses these routes as well, so this is
   presentation rather than the security boundary. !important because the nav
   buttons carry their own display rules. */
body.role-client [data-admin-only] { display: none !important; }

pre.log {
  margin: 0;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.65;
  color: var(--muted);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 68vh;
}

.feed { display: flex; flex-direction: column; }
.feed-row {
  display: grid;
  grid-template-columns: 62px 1fr auto;
  gap: 12px;
  padding: 9px 4px;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
}
.feed-row .t { font-family: var(--mono); font-size: 11px; color: var(--dim); }
.feed-row .m { min-width: 0; word-break: break-word; }
.feed-row .k { font-size: 11px; color: var(--dim); }
.sev-error { color: var(--sell); }
.sev-warn { color: var(--warn); }
.sev-success { color: var(--buy); }

/* --------------------------------------------------------- table scroll --- */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ------------------------------------------------------- account switch --- */
.panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px 16px;
}
.panel-spaced { margin-top: 18px; }

/* Default / Custom segmented control */
.mode-row { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.mode-btn {
  padding: 8px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
}
.mode-btn:hover { background: var(--surface2); }
.mode-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}

/* In Default mode the ratio controls are visible but clearly not editable. */
.ratio-locked {
  filter: blur(1.6px);
  opacity: 0.45;
  pointer-events: none;
  user-select: none;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-grid-3 { grid-template-columns: repeat(3, 1fr); }

.form-grid label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.form-grid input,
.form-grid select {
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
}
.form-grid input:focus,
.form-grid select:focus { outline: none; border-color: var(--accent); }

/* A field that already has a stored value (never pre-filled, only hinted). */
.form-grid input.is-set { border-color: var(--border-strong); background: var(--surface); }
.form-grid input.is-set::placeholder { color: var(--accent); }

.notice {
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  background: var(--surface);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  margin-bottom: 16px;
}
.notice-ok { border-left-color: var(--buy); }
.notice-ok strong { color: var(--buy); }
.notice-bad { border-left-color: var(--sell); }
.notice-bad strong { color: var(--sell); }

.actions-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}
.btn-inline { width: auto; padding: 9px 16px; }

.monitor-note {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin-bottom: 12px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 11px;
  line-height: 1.45;
  color: var(--muted);
}
/* An author `display` rule beats the browser's [hidden] rule, so the note stayed
   visible even when JS hid it - showing "monitor only" next to working Start/Stop
   buttons. This restores the hidden attribute. */
.monitor-note[hidden] { display: none; }
.monitor-note .dot { margin-top: 4px; flex: none; }

/* When control is disabled (production monitor), remove the controls entirely. */
body.no-control .controls,
body.no-control .mobile-actions {
  display: none !important;
}

/* -------------------------------------------------- mobile-only elements --- */
.topbar,
.mobile-actions,
.overlay {
  display: none;
}

/* ------------------------------------------------------------ responsive --- */
@media (max-width: 820px) {
  /* Whole page scrolls naturally instead of an inner scroll area. */
  .shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }
  .main {
    padding: 14px 14px 88px;
    overflow: visible;
  }

  /* Top bar with the menu button. */
  .topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 25;
    height: 52px;
    padding: 0 12px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
  }
  .topbar-brand { font-size: 14px; font-weight: 600; }
  .topbar-brand span { color: var(--accent); }
  .topbar-status { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
  .icon-btn {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    line-height: 1;
  }
  .icon-btn:active { background: var(--surface2); }

  /* Sidebar becomes an off-canvas drawer. */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 264px;
    max-width: 84vw;
    z-index: 40;
    background: var(--bg);
    border-right: 1px solid var(--border);
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }

  .overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }
  .overlay.show { opacity: 1; pointer-events: auto; }

  /* Start/Stop live in a sticky bottom bar so they are always one tap away. */
  .sidebar .controls { display: none; }
  .mobile-actions {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    background: var(--bg);
    border-top: 1px solid var(--border);
  }

  /* Bigger touch targets. */
  .nav-btn { padding: 12px 12px; font-size: 14px; }
  .btn { padding: 12px 10px; font-size: 14px; }
  .link-btn { padding: 10px 8px; font-size: 12px; }

  .cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .card .v { font-size: 17px; }
  .head { margin-bottom: 14px; }
  .head h1 { font-size: 15px; }
  .head .clock { font-size: 11px; }

  /* Tables scroll sideways instead of squashing. */
  .table-wrap { border: 1px solid var(--border); border-radius: 8px; }
  .table-wrap table { min-width: 620px; }
  th, td { padding: 8px 10px; }
  .two-col { gap: 18px; }
  .form-grid,
  .form-grid-3 { grid-template-columns: 1fr; }
  .panel { padding: 12px 12px 14px; }
}

