:root {
  --bg: #131722;
  --panel: #1c2030;
  --border: #2a2e39;
  --text: #d1d4dc;
  --muted: #787b86;
  --accent: #2962ff;
  --up-hist: #26a69a;
  --down-hist: #ef5350;
  --up-live: #42a5f5;
  --down-live: #ff7043;
  --sma: #e0e3eb;
  --sigma: #26a69a;
  --warn: #f0b429;
  --sep: #f0b429;
  --bid: #26a69a;
  --ask: #ef5350;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Trebuchet MS", Roboto, Ubuntu, sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 100vh;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 18px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.toolbar .title {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.01em;
  margin-right: 8px;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
}

.tab:hover { color: var(--text); }
.tab.active {
  color: var(--text);
  background: var(--bg);
  border-color: var(--border);
}

/* display:flex on these would otherwise override the hidden attribute */
.panel[hidden],
.sim-progress[hidden],
.tab[hidden],
.chart-only[hidden] {
  display: none !important;
}

.sim-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 180px;
  max-width: 280px;
  flex: 1;
}

.sim-progress-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.sim-progress-track {
  flex: 1;
  height: 6px;
  background: #222733;
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.sim-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.25s ease;
}

body.tab-trades .chart-only { display: none; }

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#panelTrades {
  overflow: auto;
  padding: 12px 16px 24px;
}

.trades-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 16px 24px;
  padding: 10px 12px;
  margin-bottom: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.trades-summary .kpi {
  color: var(--muted);
}
.trades-summary .kpi b {
  color: var(--text);
  font-weight: 600;
  margin-left: 6px;
}
.trades-summary .pnl-pos { color: var(--up-hist); }
.trades-summary .pnl-neg { color: var(--down-hist); }

.trades-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.trade-row {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.trade-row summary {
  display: grid;
  grid-template-columns: 16px minmax(100px, 1.1fr) minmax(140px, 1.4fr) minmax(120px, 1.2fr) minmax(120px, 1.2fr) 110px 110px;
  gap: 8px 12px;
  align-items: center;
  padding: 10px 12px;
  cursor: pointer;
  list-style: none;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.trade-row summary::-webkit-details-marker,
.trade-row summary::marker {
  display: none;
  content: "";
}

.trade-row .chev {
  color: var(--muted);
  font-size: 11px;
}
.trade-row[open] .chev::before { content: "▾"; }
.trade-row:not([open]) .chev::before { content: "▸"; }

.trade-row .pair { font-weight: 600; }
.trade-row .side { color: var(--muted); font-size: 12px; }
.trade-row .when { color: var(--muted); font-size: 12px; }
.trade-row .when-open { color: var(--warn); font-size: 12px; font-weight: 600; }
.trade-row .pnl { text-align: right; font-weight: 600; }
.trade-row .exit { color: var(--muted); font-size: 12px; text-align: right; }

.trade-detail {
  padding: 0 12px 12px 28px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

.trade-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  align-items: center;
  margin: 4px 0 10px;
  color: var(--text);
}

.trade-phase {
  margin: 0 0 14px;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.phase-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 12px;
  margin-bottom: 6px;
}

.phase-title {
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
}

.phase-times {
  color: var(--muted);
  font-size: 11px;
}

.phase-verdict {
  font-size: 12px;
  line-height: 1.35;
  margin-bottom: 8px;
  color: var(--text);
}

.phase-verdict.z-bad { color: #ef9a9a; }
.phase-verdict.z-warn { color: var(--warn); }
.phase-verdict.z-ok { color: #80cbc4; }
.phase-verdict.z-neutral { color: var(--text); }

.phase-z {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin-bottom: 8px;
}

.z-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 120px;
}

.z-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.z-val {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
}

.z-arrow {
  color: var(--muted);
  font-size: 14px;
  padding-top: 12px;
}

.legs-table {
  margin-top: 4px !important;
}

.legs-table .leg-side {
  display: inline-block;
  min-width: 2.6em;
  color: var(--muted);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.03em;
}

.legs-table .slip-bad { color: #ef9a9a; font-weight: 600; }
.legs-table .slip-good { color: #80cbc4; font-weight: 600; }
.legs-table .slip-flat { color: var(--muted); }

.tag-chase {
  display: inline-block;
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 3px;
  border: 1px solid var(--border);
  color: var(--warn);
  font-size: 10px;
  vertical-align: middle;
}

.trade-actions {
  margin: 10px 0 8px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

button.go-trade {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}

button.go-trade:hover {
  border-color: var(--accent);
  color: #fff;
}

a.trade-link {
  color: var(--muted);
  font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  word-break: break-all;
  text-decoration: none;
}

a.trade-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

.trade-detail table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
}

.trade-detail th,
.trade-detail td {
  text-align: left;
  padding: 3px 8px 3px 0;
  border-bottom: 1px solid var(--border);
}
.trade-detail th { color: var(--muted); font-weight: 500; }
.trade-detail td { color: var(--text); }
.trade-detail .muted { color: var(--muted); font-weight: 400; font-size: 12px; }

.trades-empty {
  color: var(--muted);
  padding: 24px 8px;
  font-size: 13px;
}

.toolbar label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 8px;
  font-size: 13px;
}

.legend {
  display: flex;
  gap: 14px;
  margin-left: auto;
  font-size: 12px;
  color: var(--muted);
}

.legend span i {
  display: inline-block;
  width: 14px;
  height: 3px;
  margin-right: 5px;
  vertical-align: middle;
  border-radius: 1px;
}

.legend .sma i { background: var(--sma); }
.legend .sigma i { background: var(--sigma); }
.legend .sep i {
  width: 3px;
  height: 12px;
  background: var(--sep);
}
.legend .night i {
  width: 3px;
  height: 12px;
  background: #7aa2ff;
}
.legend .hist i {
  height: 10px;
  width: 8px;
  background: linear-gradient(180deg, var(--up-hist) 50%, var(--down-hist) 50%);
}
.legend .live i {
  height: 10px;
  width: 8px;
  background: linear-gradient(180deg, var(--up-live) 50%, var(--down-live) 50%);
}

.stale {
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 4px;
  background: #222733;
  border: 1px solid var(--border);
  color: var(--text);
}

.stale.fresh { color: var(--up-hist); border-color: #1e4; }
.stale.ok { color: var(--warn); }
.stale.bad { color: var(--down-hist); border-color: #a33; }

.main {
  flex: 1;
  display: flex;
  min-height: 0;
  height: 100%;
}

#chartWrap {
  flex: 1;
  position: relative;
  min-width: 0;
  min-height: 420px;
}

#chart {
  width: 100%;
  height: 100%;
}

.break-seps {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
}

.session-sep {
  position: absolute;
  top: 8px;
  bottom: 28px;
  width: 0;
  border-left: 2px solid #7aa2ff;
  transform: translateX(-1px);
  opacity: 0.85;
}

.session-sep span {
  position: absolute;
  top: 22px;
  left: 6px;
  font-size: 10px;
  font-weight: 600;
  color: #9db7ff;
  background: rgba(19, 23, 34, 0.85);
  padding: 1px 5px;
  border-radius: 3px;
  white-space: nowrap;
}

.live-sep {
  position: absolute;
  top: 8px;
  bottom: 28px;
  width: 0;
  border-left: 2px dashed var(--sep);
  pointer-events: none;
  z-index: 5;
  transform: translateX(-1px);
}

.live-sep span {
  position: absolute;
  top: 4px;
  left: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--sep);
  background: rgba(19, 23, 34, 0.85);
  padding: 1px 5px;
  border-radius: 3px;
  white-space: nowrap;
}

#books {
  width: 320px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.books-head {
  padding: 10px 12px 4px;
  font-size: 13px;
  font-weight: 600;
}

.books-grid {
  flex: 1;
  min-height: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.book {
  flex: 1 1 50%;
  min-height: 0;
  overflow: hidden;
  padding: 8px 10px 0;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.book:first-child {
  border-top: none;
}

.book-head {
  flex-shrink: 0;
  padding-bottom: 4px;
}

.book-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding-bottom: 10px;
  /* Avoid macOS overlay scrollbar flashing on 1Hz re-renders */
  scrollbar-width: thin;
  scrollbar-color: #3a3f4b transparent;
}

.book-body::-webkit-scrollbar {
  width: 6px;
}
.book-body::-webkit-scrollbar-thumb {
  background: #3a3f4b;
  border-radius: 3px;
}
.book-body::-webkit-scrollbar-track {
  background: transparent;
}

.book h3 {
  margin: 0 0 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}

.book .meta {
  color: var(--muted);
  font-size: 10px;
  margin-bottom: 4px;
}

.book table {
  width: 100%;
  border-collapse: collapse;
}

.book th {
  text-align: right;
  color: var(--muted);
  font-weight: 500;
  padding: 2px 4px;
  border-bottom: 1px solid var(--border);
}

.book td {
  padding: 1px 4px;
  text-align: right;
}

.book tr.ask td { color: var(--ask); }
.book tr.bid td { color: var(--bid); }
.book tr.spread td {
  color: var(--muted);
  text-align: center;
  padding: 4px;
  font-size: 10px;
}

.book .empty {
  color: var(--muted);
  padding: 8px 0;
}

.footer {
  padding: 6px 16px 10px;
  font-size: 11px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

@media (max-width: 900px) {
  .main { flex-direction: column; }
  #books {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    height: 320px;
    max-height: 40vh;
  }
}
