:root {
  --bg: #0e1117;
  --panel: #151a23;
  --panel-border: #262d3a;
  --text: #e6e9ef;
  --text-dim: #8b93a7;
  --accent: #4f8cff;
  --green: #16c784;
  --red: #ea3943;
  --flash-green: rgba(22, 199, 132, 0.35);
  --flash-red: rgba(234, 57, 67, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--panel-border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.2px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-dim);
}

.controls select {
  background: #1c2330;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
}

.conn-status {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: 12px;
  background: #1c2330;
}
.conn-status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
}
.conn-status.ok .dot { background: var(--green); }
.conn-status.err .dot { background: var(--red); }

.trade-btn {
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 6px;
  transition: filter 0.15s ease;
}
.trade-btn:hover { filter: brightness(1.1); }

@media (max-width: 560px) {
  /* The dashboard's topbar has few enough controls to stay on one line;
   * the trade page's has more (source/symbol/timeframe/status), so let
   * it wrap onto a second row rather than overflow off-screen — a no-op
   * for the dashboard since nothing there needs to wrap. */
  .topbar { padding: 8px 10px; gap: 8px; flex-wrap: wrap; }
  .brand { font-size: 13px; white-space: nowrap; }
  .controls { gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
  .controls label { display: none; } /* the select's own options (1/2/4/6/8) are self-explanatory */
  .controls select { padding: 4px 5px; font-size: 12px; }
  .conn-status { padding: 3px 6px; }
  .conn-status .conn-label { display: none; } /* keep just the status dot */
  .trade-btn { padding: 6px 10px; font-size: 12px; }

  /* The symbol dropdown is absolutely positioned from its input's left
   * edge (needs position:relative on .symbol-picker to work at all), so
   * if that input sits narrow and off-center in the wrapped ticker bar,
   * a wide dropdown can run past the right edge of the screen. Simplest
   * reliable fix: give the picker its own full-width row so the
   * dropdown always has the whole pane's width to open into. */
  .ticker-bar .symbol-picker,
  .controls .symbol-picker { flex: 1 1 100%; max-width: none; }
  .ind-panel { max-width: min(260px, 92vw); }
}

.grid {
  display: grid;
  gap: 6px;
  padding: 6px;
  height: calc(100vh - 49px);
  width: 100%;
}

/* Layout presets, applied by layout.js via data-count on #grid */
.grid[data-count="1"] { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.grid[data-count="2"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
.grid[data-count="4"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.grid[data-count="6"] { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }
.grid[data-count="8"] { grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }

@media (max-width: 900px) {
  .grid[data-count="6"],
  .grid[data-count="8"] {
    grid-template-columns: 1fr 1fr;
  }
}

/* Below ~560px, a CSS grid stops being the right tool: squeezing several
 * fixed row-tracks into one column leaves later panes with almost no
 * height (an empty grid-template-rows track still constrains its row
 * even after grid-template-columns collapses to one column), and even
 * fixed correctly, N tiny unreadable charts crammed onto one small
 * screen isn't usable anyway. So instead: stack panes as a scrollable
 * flex column, each one tall enough to actually read a candlestick
 * chart on. `display: flex` here overrides the base `.grid`'s
 * `display: grid` (same specificity, later in source order); the
 * leftover grid-template-columns/rows from the `[data-count]` rules
 * above become inert once the container isn't a grid. */
@media (max-width: 560px) {
  .grid {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: calc(100vh - 49px);
  }
  .grid .pane {
    min-height: 420px;
    flex-shrink: 0;
  }
  .grid[data-count="1"] .pane {
    min-height: calc(100vh - 61px);
  }
}

.pane {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  overflow: hidden;
  min-height: 0;
  min-width: 0;
}

.ticker-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--panel-border);
  background: #10151e;
  flex-wrap: wrap;
  transition: background-color 0.15s ease;
}

.ticker-bar.flash-up { background-color: var(--flash-green); }
.ticker-bar.flash-down { background-color: var(--flash-red); }

.ticker-bar select {
  background: #1c2330;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  padding: 3px 6px;
  font-size: 12px;
  max-width: 40%;
}

/* Searchable symbol combobox — shared by the dashboard's per-pane picker
 * and the trade page's symbol field. See symbol_picker.js. */
.symbol-picker {
  position: relative;
  max-width: 40%;
}
.symbol-picker input.symbol-input {
  width: 100%;
  box-sizing: border-box;
  background: #1c2330;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  padding: 3px 6px;
  font-size: 12px;
}
.symbol-picker input.symbol-input:focus {
  outline: none;
  border-color: var(--accent);
}
.symbol-dropdown {
  position: absolute;
  z-index: 30;
  top: 100%;
  left: 0;
  margin-top: 2px;
  min-width: 220px;
  max-height: 260px;
  overflow-y: auto;
  background: #161c27;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.symbol-dropdown.hidden { display: none; }
.symbol-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.symbol-option:hover,
.symbol-option.active { background: #1c2330; }
.symbol-option .symbol-name { overflow: hidden; text-overflow: ellipsis; }
.symbol-option .symbol-code { color: var(--text-dim); font-size: 11px; flex-shrink: 0; }

.price-block {
  margin-left: auto;
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-variant-numeric: tabular-nums;
}

.price {
  font-size: 15px;
  font-weight: 700;
}

.change-pct {
  font-size: 12px;
  color: var(--text-dim);
}
.change-pct.up { color: var(--green); }
.change-pct.down { color: var(--red); }

.ind-toggle {
  background: #1c2330;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  padding: 3px 8px;
  font-size: 12px;
  cursor: pointer;
}
.ind-toggle:hover { background: #232b3b; }
.ind-toggle.active { border-color: var(--accent); color: var(--accent); }

.ind-panel {
  position: absolute;
  z-index: 20;
  margin-top: 2px;
  background: #161c27;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 220px;
}
.ind-panel.hidden { display: none; }
.ind-panel hr { border: none; border-top: 1px solid var(--panel-border); margin: 2px 0; width: 100%; }
.ind-row {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
}
.ind-period {
  width: 46px;
  background: #1c2330;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 12px;
}

.chart-container {
  flex: 1;
  min-height: 0;
  min-width: 0;
  position: relative;
}

.pane-status {
  font-size: 11px;
  color: var(--text-dim);
  padding: 2px 8px;
  min-height: 16px;
  border-top: 1px solid var(--panel-border);
}
.pane-status.err { color: var(--red); }
