:root {
  --bg: #0d1117;
  --bg-panel: #131722;
  --bg-input: #1c2129;
  --border: #2a2e39;
  --border-strong: #3b4048;
  --text: #d1d4dc;
  --text-dim: #787b86;
  --up: #26a69a;
  --up-bg: rgba(38, 166, 154, 0.14);
  --down: #ef5350;
  --down-bg: rgba(239, 83, 80, 0.14);
  --accent-blue: #448aff;
  --radius: 8px;
  --radius-sm: 5px;
}

* {
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  /* Fill the viewport height as a column so the chart can absorb the leftover
     vertical space, leaving a uniform 16px gap top, between panels, and bottom.
     A definite height (not min-height) is required so flex actually distributes
     the space to the chart row rather than letting the body grow past the fold.
     (script tags are display:none and don't participate in the flex gaps.) */
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px 24px;
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
}

#chartRow {
  display: flex;
  align-items: stretch; /* chart + side panel both fill the row's height */
  gap: 16px;
  flex: 1 1 auto;       /* grow to take all the vertical space tf-bar/bottomRow don't */
  min-height: 0;        /* allow shrinking below the chart canvas's content height */
}

#chartWrap {
  position: relative;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  box-sizing: border-box;
  flex: 1 1 auto; /* take all row width the side panel doesn't need */
  min-width: 0;
  min-height: 0;      /* let the wrap shrink vertically with the chart row */
  overflow: hidden;   /* clip the canvas so it can't floor the flex height */
}

#sidePanel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-sizing: border-box;
  flex: 0 0 240px; /* fixed, compact — don't grow into the chart's space */
  width: 240px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  align-self: stretch;
}

.balance-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.balance-label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.balance-input {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-family: inherit;
  font-size: 13px;
  box-sizing: border-box;
  width: 100%;
  height: 38px;
  transition: border-color 0.15s ease;
}

.balance-input:focus {
  outline: none;
  border-color: var(--accent-blue);
}

.ls-row {
  display: flex;
  gap: 8px;
}

.ls-row .side-btn {
  flex: 1;
}

.side-btn {
  flex: 0 0 auto;
  width: 100%;
  box-sizing: border-box;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  font-size: 13px;
  height: 38px;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.side-btn:hover:not(:disabled):not(.active) {
  border-color: var(--border-strong);
}

.side-btn.active {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  color: #fff;
}

.side-btn.long-btn.active {
  background: var(--up);
  border-color: var(--up);
  color: #06201c;
}

.side-btn.short-btn.active {
  border-color: var(--down);
  background: var(--down);
  color: #2b0a09;
}

.side-btn:disabled {
  border-color: var(--border);
  color: #454952;
  cursor: not-allowed;
  background: transparent;
}

@media (max-width: 1000px) {
  /* Stacked layout: revert the fixed-viewport fill — let the page grow and
     scroll naturally, with the chart back to a fixed height. */
  body {
    height: auto;
  }

  #chartRow {
    flex-direction: column;
    flex: 0 0 auto;
  }

  #chart {
    height: 500px;
  }

  #chartWrap {
    width: 100%;
    flex: 0 0 auto;
  }

  #sidePanel {
    width: 100%;
    flex: 0 0 auto;
  }

  #bottomRow {
    flex-direction: column;
  }

  #bottomPanel {
    flex: 0 0 auto;
    width: 100%;
  }
}

.chart-legend {
  position: absolute;
  top: 10px;
  left: 14px;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  pointer-events: none;
  z-index: 2;
}

.candle-timer {
  position: absolute;
  bottom: 12px;
  right: 12px;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 8px;
  font-family: "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: 12px;
  pointer-events: none;
  z-index: 2;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

#chart {
  width: 100%;
  height: 100%;      /* fills #chartWrap, which stretches to the chart row height */
  min-height: 360px; /* stay usable if the viewport is very short */
}

.tf-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.tf-btn {
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 14px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.tf-btn:hover:not(.active):not(.static) {
  border-color: var(--border-strong);
}

.tf-btn.active {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  color: #fff;
  font-weight: 600;
}

.tf-btn.static {
  cursor: default;
  color: var(--text-dim);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
}

/* No-trade-zone warning: plain yellow text on the background (no box),
   shown only during the volatility/pause windows around the US open/close. */
.trade-warning {
  display: none;
  color: #ffeb3b;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0 8px;
  align-self: center;
}

.trade-warning.show {
  display: inline-block;
}

/* "Market Closed" is informational (not a volatility warning) — show it calmer. */
.trade-warning.market-closed {
  color: var(--text-dim);
  font-weight: 600;
}

#symbolSelect {
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-family: inherit;
  font-size: 13px;
}

.drawing-select {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  font-family: inherit;
  font-size: 12px;
  max-width: 100%;
}

#bottomRow {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  flex: 0 0 auto; /* natural height; the chart row absorbs the leftover space */
}

/* One consolidated bottom panel with tabs (Open Positions / Open Orders /
   Drawings) instead of two side-by-side panels. */
#bottomPanel {
  flex: 1 1 auto;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 16px 14px;
  box-sizing: border-box;
  min-width: 0;
}

.panel-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 10px;
}

.panel-tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  font-size: 13px;
  padding: 12px 12px 10px;
  margin-bottom: -1px;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.panel-tab:hover:not(.active) { color: var(--text); }

.panel-tab.active {
  color: var(--text);
  border-bottom-color: var(--accent-blue);
}

.tab-count {
  color: var(--text-dim);
  font-weight: 600;
  font-size: 12px;
  margin-left: 2px;
}

.panel-pane[hidden] { display: none; }

#positionsTable,
#ordersTable,
#alarmsTable,
#drawingsTable {
  width: 100%;
  border-collapse: collapse;
  font-family: inherit;
  table-layout: fixed;
}

#positionsTable th,
#ordersTable th,
#alarmsTable th,
#drawingsTable th {
  text-align: left;
  padding: 6px 8px;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}

#positionsTable td,
#ordersTable td,
#alarmsTable td,
#drawingsTable td {
  text-align: left;
  padding: 8px 8px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#positionsTable tbody tr:hover td,
#ordersTable tbody tr:hover td,
#alarmsTable tbody tr:hover td,
#drawingsTable tbody tr:hover td {
  background: rgba(255, 255, 255, 0.03);
}

#positionsTable th:last-child,
#positionsTable td:last-child,
#ordersTable th:last-child,
#ordersTable td:last-child,
#alarmsTable th:last-child,
#alarmsTable td:last-child,
#drawingsTable th:last-child,
#drawingsTable td:last-child {
  width: 78px;
}

.side-long {
  color: var(--up);
  font-weight: 600;
}

.side-short {
  color: var(--down);
  font-weight: 600;
}

.positions-empty {
  color: var(--text-dim);
  text-align: center;
  padding: 16px 0;
}

.pnl-positive {
  color: var(--up);
  font-weight: 600;
}

.pnl-negative {
  color: var(--down);
  font-weight: 600;
}

/* The live P&L is click-to-hide (header or any value), so it's easy to stop
   fixating on it while a trade is open. */
.pnl-cell,
.pnl-toggle {
  cursor: pointer;
  user-select: none;
}
.pnl-cell:hover,
.pnl-toggle:hover {
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.pnl-masked {
  color: var(--text-dim);
  font-weight: 600;
  letter-spacing: 2px;
}

.close-position-btn,
.remove-btn {
  background: transparent;
  color: var(--down);
  border: 1px solid var(--down);
  border-radius: var(--radius-sm);
  padding: 4px 12px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  font-size: 12px;
  transition: background 0.15s ease, color 0.15s ease;
}

.close-position-btn:hover:not(:disabled),
.remove-btn:hover:not(:disabled) {
  background: var(--down);
  color: #fff;
}

.close-position-btn:disabled {
  border-color: var(--border);
  color: #454952;
  cursor: not-allowed;
  background: transparent;
}

/* Trade confirmation modal (Limit / Stop Entry tool) */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--bg-panel);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 20px 22px;
  width: 340px;
  max-width: 92vw;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}
.modal-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 14px;
}
.modal-details { width: 100%; border-collapse: collapse; }
.modal-details td { padding: 5px 0; font-size: 13px; vertical-align: top; }
.modal-details .tc-label { color: var(--text-dim); white-space: nowrap; padding-right: 14px; }
.modal-details .tc-value { color: var(--text); text-align: right; width: 100%; }
.modal-details .tc-dim { color: var(--text-dim); font-size: 12px; }
.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}
.modal-btn {
  flex: 1;
  padding: 9px 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  background: var(--bg-input);
  color: var(--text);
}
.modal-btn.confirm { background: var(--up); border-color: var(--up); color: #06281f; }
.modal-btn.cancel:hover { border-color: var(--text-dim); }
.modal-btn.confirm:hover { filter: brightness(1.08); }

/* Clickable asset name in the panels — switches the chart to that asset. */
.asset-link {
  cursor: pointer;
  color: var(--accent-blue);
}
.asset-link:hover {
  text-decoration: underline;
}
