:root {
  --bg: #0e0f14;
  --panel: #161821;
  --panel2: #1c1f2b;
  --border: #262a38;
  --text: #d6dae3;
  --muted: #7c8598;
  --blue: #4c9aff;
  --accent: #e0b64d;
  --green: #26a69a;
  --red: #ef5350;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; height: 100vh; overflow: hidden;
}

/* ─── Top bar ─── */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand .logo { font-weight: 700; font-size: 16px; color: #fff; }
.brand .tag { color: var(--muted); font-size: 12px; }
.controls { display: flex; align-items: center; gap: 10px; }
.tf-group { display: flex; gap: 2px; }
.tf-group button {
  background: var(--panel2); border: 1px solid var(--border); color: var(--muted);
  padding: 5px 9px; border-radius: 5px; cursor: pointer; font-size: 12px;
}
.tf-group button.active { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }
.tf-group button:hover { color: #fff; }
.status { font-size: 16px; }
.status.dot-off { color: var(--red); }
.status.dot-on { color: var(--green); }
.status.dot-wait { color: var(--accent); }

/* ─── Grid ─── */
#grid { flex: 1; min-height: 0; display: grid; gap: 2px; background: var(--border); padding: 2px; }
#grid.layout-1 { grid-template-columns: 1fr; grid-template-rows: 1fr; }
#grid.layout-2 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
#grid.layout-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }

/* ─── Pane ─── */
.pane { display: flex; flex-direction: column; background: var(--bg); min-width: 0; min-height: 0; overflow: hidden; }
.pane-header {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px;
  background: var(--panel); border-bottom: 1px solid var(--border); flex-shrink: 0;
  font-size: 12px; overflow: hidden;
}
.p-symbol {
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  padding: 4px 8px; border-radius: 5px; width: 92px; font-size: 12px; text-transform: uppercase;
}
.p-tf button { padding: 4px 7px; font-size: 11px; }

/* ─── Timeframe "more" dropdown (1m / 4h / 1d) ─── */
.tf-more { position: relative; display: inline-flex; }
.tf-more-btn { padding: 4px 7px; font-size: 11px; white-space: nowrap; }
.tf-more .dd-menu {
  display: none; position: fixed; z-index: 1000;
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  padding: 4px; box-shadow: 0 6px 18px rgba(0,0,0,0.45);
}
.tf-more.open .dd-menu { display: flex; flex-direction: column; gap: 2px; }
.tf-more .dd-menu button { padding: 5px 14px; font-size: 11px; text-align: left; }

/* ─── Searchable pair picker ─── */
.p-symbol-wrap { position: relative; display: inline-flex; }
.sym-menu {
  display: none; position: fixed; z-index: 1000; max-height: 340px; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  padding: 4px; box-shadow: 0 6px 18px rgba(0,0,0,0.45); min-width: 150px;
}
.p-symbol-wrap.open .sym-menu { display: flex; flex-direction: column; gap: 1px; }
.sym-menu button {
  display: block; width: 100%; text-align: left; padding: 5px 10px; font-size: 11px;
  background: transparent; border: none; color: var(--text); cursor: pointer;
  border-radius: 4px; white-space: nowrap;
}
.sym-menu button:hover { background: var(--panel2); }
.sym-empty { padding: 8px 10px; font-size: 11px; color: var(--muted); max-width: 220px; }
.p-price { font-size: 14px; font-weight: 700; color: #fff; }
.p-change { font-size: 12px; }
.up { color: var(--green); }
.down { color: var(--red); }
.p-quant { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; background: var(--panel2); }
.p-quant.LONG { color: var(--green); }
.p-quant.SHORT { color: var(--red); }
.p-quant.NEUTRAL { color: var(--muted); }
.p-flex { flex: 1; }
.p-trade-pnl { font-size: 12px; font-weight: 700; }

.p-body { flex: 1; min-height: 0; position: relative; display: flex; }
.p-chart { flex: 1; min-height: 0; min-width: 0; position: relative; }
.subpane { height: 72px; border-top: 1px solid var(--border); flex-shrink: 0; }

/* ─── Trade monitor + analyze controls (pane header) ─── */
.p-trade-id {
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  padding: 4px 6px; border-radius: 5px; width: 62px; font-size: 11px;
}
.p-monitor, .p-analyze {
  background: var(--panel2); border: 1px solid var(--border); color: var(--muted);
  padding: 4px 9px; border-radius: 5px; cursor: pointer; font-size: 11px; white-space: nowrap;
}
.p-monitor:hover, .p-analyze:hover { color: #fff; }
.p-monitor.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.p-analyze.active { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }

/* ─── Chart legend (overlay, top-right of the chart) ─── */
.p-legend {
  position: absolute; top: 6px; right: 8px; z-index: 4;
  display: flex; flex-wrap: wrap; gap: 4px 10px; justify-content: flex-end;
  max-width: 60%; pointer-events: none;
}
.p-legend .lg-item {
  display: flex; align-items: center; gap: 4px; font-size: 10px; color: var(--muted);
  background: rgba(14,15,20,0.6); padding: 1px 5px; border-radius: 4px;
}
.p-legend .lg-item i { width: 10px; height: 2px; border-radius: 1px; display: inline-block; }

/* OHLC readout (top-left of chart, TradingView-style) */
.p-ohlc {
  position: absolute; top: 6px; left: 40px; z-index: 4;
  display: flex; flex-wrap: wrap; gap: 2px 10px; align-items: center;
  font-size: 11px; color: var(--muted); pointer-events: none;
  background: rgba(14,15,20,0.72); padding: 3px 8px; border-radius: 5px;
  max-width: 70%;
}
.p-ohlc b { color: var(--text); font-weight: 600; }
.p-ohlc .o-lbl { color: var(--muted); }
.p-ohlc .up { color: var(--green); }
.p-ohlc .down { color: var(--red); }
/* ─── Layers dropdown (chart overlay toggles: S/R, VP, Liq) ─── */
.p-layers { position: relative; }
.p-layers-btn {
  background: var(--panel2); border: 1px solid var(--border); color: var(--muted);
  padding: 4px 9px; border-radius: 5px; cursor: pointer; font-size: 11px; white-space: nowrap;
}
.p-layers-btn:hover { color: #fff; }
.p-layers-btn.active { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }
.p-layers .dd-menu {
  display: none; position: fixed; z-index: 1000;
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  padding: 4px; min-width: 172px; box-shadow: 0 6px 18px rgba(0,0,0,0.45);
}
.p-layers.open .dd-menu { display: block; }
.p-layers .dd-menu label {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 4px;
  font-size: 11px; color: var(--text); cursor: pointer; white-space: nowrap;
}
.p-layers .dd-menu label:hover { background: var(--panel2); }
.p-layers .dd-menu input { accent-color: var(--accent); cursor: pointer; margin: 0; width: 13px; height: 13px; }
/* In multi-pane layouts compact the trade-id input but keep it accessible */
#grid.layout-4 .p-trade-id, #grid.layout-2 .p-trade-id { width: 52px; }

/* ─── Analysis panel (floating over the page, not inside a pane) ─── */
.p-analysis {
  position: fixed; top: 96px; right: 10px;
  width: 320px; max-height: calc(100vh - 110px); z-index: 1000;
  overflow-y: auto; overflow-x: hidden;
  background: var(--panel); border: 1px solid var(--border);
  padding: 0; font-size: 12px;
  display: none; border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
}
.p-analysis.open { display: flex; flex-direction: column; }
.apanel-head {
  padding: 6px 10px; background: var(--panel2); border-bottom: 1px solid var(--border);
  cursor: move; font-weight: 700; font-size: 12px; color: var(--text);
  display: flex; justify-content: space-between; align-items: center;
  user-select: none; flex-shrink: 0; border-radius: 6px 6px 0 0;
}
.apanel-head .close-btn { cursor: pointer; color: var(--muted); font-size: 16px; line-height: 1; }
.apanel-head .close-btn:hover { color: #fff; }
.apanel-body { flex: 1; overflow-y: auto; padding: 8px 10px; }
.ah-updated { font-size: 10px; color: var(--muted); margin-bottom: 4px; text-align: right; }
.ahead { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.ahead .ah-sym { font-weight: 700; color: #fff; font-size: 13px; }
.ahead .ah-price { font-weight: 700; }
.asec { border-top: 1px solid var(--border); padding: 6px 0; }
.asec h4 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.ar { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; line-height: 1.35; }
.ar > span { color: var(--muted); flex-shrink: 0; }
.ar > b { text-align: right; font-weight: 600; word-break: break-word; }
.ar.note { color: var(--muted); font-style: italic; display: block; }
/* Plain-English "what's happening now" callout — a soft highlighted line so a
   new trader immediately sees the interpretation, without shouting. */
.ar.say {
  display: block; color: var(--text); line-height: 1.4;
  background: rgba(76,154,255,0.08); border-left: 2px solid var(--blue);
  padding: 5px 8px; margin-top: 5px; border-radius: 0 4px 4px 0; font-size: 11.5px;
}
/* Section info icon — invisible until hovered, explains the section's purpose. */
.asec h4 .asec-i {
  color: var(--muted); font-size: 11px; cursor: help; margin-left: 5px;
  opacity: .5; vertical-align: middle; font-style: normal;
}
.asec h4 .asec-i:hover { opacity: 1; color: var(--accent); }
.ar.col { flex-direction: column; gap: 2px; }
.ar.col > span { color: var(--muted); }
.ar.col p { margin: 0; color: var(--text); }
.ar.col ul { margin: 2px 0 0; padding-left: 16px; color: var(--text); }
.ar.col li { margin: 1px 0; }
.muted2 { color: var(--muted); font-weight: 400; font-size: 11px; }
.neutral { color: var(--muted); }

/* Quant factor bars */
.factors { margin-top: 4px; display: flex; flex-direction: column; gap: 2px; }
.fac { display: grid; grid-template-columns: 78px 1fr 46px; align-items: center; gap: 6px; }
.fac-n { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fac-bar { background: var(--panel2); height: 8px; border-radius: 4px; overflow: hidden; }
.fac-bar i { display: block; height: 100%; border-radius: 4px; }
.fac-bar i.up { background: var(--green); }
.fac-bar i.down { background: var(--red); }
.fac-v { text-align: right; font-size: 11px; }

/* Drawing toolbar (overlays top-left of the chart) */
.p-tools {
  position: absolute; top: 6px; left: 6px; z-index: 5;
  display: flex; flex-direction: column; gap: 2px;
  background: rgba(22,24,33,0.9); border: 1px solid var(--border); border-radius: 6px; padding: 3px;
}
.p-tools button {
  width: 26px; height: 26px; background: transparent; border: none; color: var(--muted);
  cursor: pointer; border-radius: 4px; font-size: 13px; line-height: 1;
}
.p-tools button:hover { background: var(--panel2); color: #fff; }
.p-tools button.active { background: var(--accent); color: #111; }

/* In multi-pane layouts, hide the sub-indicator panes to save space */
#grid.layout-2 .subpane, #grid.layout-4 .subpane { display: none; }

.muted { color: var(--muted); font-size: 12px; }

/* ─── Mobile responsive ─── */
@media (max-width: 768px) {
  /* Stack grid vertically */
  #grid.layout-2, #grid.layout-4 { grid-template-columns: 1fr; grid-template-rows: auto; }
  /* Pane header: wrap and show all controls */
  .pane-header { flex-wrap: wrap; gap: 4px 6px; padding: 4px 6px; }
  .p-symbol { width: 72px; font-size: 11px; }
  .p-tf button { padding: 3px 5px; font-size: 10px; }
  .p-trade-id { width: 50px; font-size: 10px; }
  .p-monitor, .p-analyze { padding: 3px 6px; font-size: 10px; }
  .p-price { font-size: 12px; }
  .p-change, .p-quant, .p-trade-pnl { font-size: 10px; }
  .p-flex { display: none; } /* remove spacer to let items wrap naturally */
  /* Analysis panel: full width on mobile */
  .p-analysis { width: 100%; right: 0; left: 0; border-radius: 0; max-height: 60vh; }
  /* Legend smaller */
  .p-legend { max-width: 90%; }
  .p-legend .lg-item { font-size: 9px; padding: 1px 3px; }
  /* Drawing tools: horizontal on mobile */
  .p-tools { flex-direction: row; top: auto; bottom: 6px; left: 6px; }
  .p-tools button { width: 22px; height: 22px; font-size: 11px; }
}

@media (max-width: 480px) {
  .pane-header { font-size: 10px; }
  .p-symbol { width: 60px; }
  .p-tf button { padding: 2px 4px; font-size: 9px; }
  .p-analysis { font-size: 11px; }
  .p-analysis .ar { padding: 1px 0; }
}
