:root {
  --bg: #0b0e11;
  --bg-panel: #12161b;
  --border: #23303a;
  --cyan: #22d3ee;
  --green: #2ecc71;
  --red: #ef4444;
  --yellow: #eab308;
  --gray: #6b7785;
  --text: #e6edf3;
  --text-dim: #9aa7b2;

  /* Chart (Graafik) tab — matched to the iOS ChartView's dark-mode system colors */
  --chart-up: #34c759;
  --chart-down: #ff3b30;
  --chart-blue: #0a84ff;
  --chart-orange: #ff9f0a;
  --chart-purple: #bf5af2;
  --chart-min: #b32626;
  --chart-max: #1a7333;
  --chart-flat: #8e8e93;
}

* { 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;
}

body { display: flex; flex-direction: column; }

.screen { display: flex; flex-direction: column; min-height: 100vh; }
.screen[hidden] { display: none; }

/* --- Login --- */

#login-screen {
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-form {
  width: 100%;
  max-width: 320px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-form h1 {
  margin: 0 0 4px;
  font-size: 1.3rem;
  text-align: center;
  color: var(--cyan);
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.login-form input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 1rem;
}

.login-form input:focus {
  outline: none;
  border-color: var(--cyan);
}

.login-form button {
  margin-top: 4px;
  background: var(--cyan);
  color: #04222b;
  border: none;
  border-radius: 6px;
  padding: 11px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.login-form button:disabled { opacity: 0.6; cursor: default; }

.error { color: var(--red); font-size: 0.85rem; margin: 0; text-align: center; }

/* --- App shell --- */

.topbar {
  padding: 14px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  text-align: center;
}

.topbar-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 16px;
  font-size: 1.15rem;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
}

.topbar-row:first-child { font-size: 1.45rem; font-weight: 600; }
.topbar-row + .topbar-row { margin-top: 4px; }
.topbar-row b { color: var(--text-dim); font-weight: 500; margin-right: 4px; }

@media (max-width: 600px) {
  .topbar-row { gap: 4px 16px; font-size: 1rem; }
  .topbar-row:first-child { font-size: 1.2rem; }
}

.tabbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

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

.tabbar-end { margin-left: auto; display: flex; align-items: center; }

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}

.tab-btn.active { background: var(--cyan); color: #04222b; font-weight: 600; }

.spacer { flex: 1; }

.conn-indicator { color: var(--red); font-size: 0.8rem; margin-right: 10px; }
.conn-indicator.connected { color: var(--green); }

.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

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

.tab-panel { display: none; flex: 1; min-height: 0; padding: 10px; flex-direction: column; }
.tab-panel.active { display: flex; }

.split { display: flex; flex-direction: column; gap: 10px; flex: 1; min-height: 0; }

@media (min-width: 820px) {
  .split { flex-direction: row; }
  .list { width: 320px; flex-shrink: 0; }
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-y: auto;
  max-height: 40vh;
}

@media (min-width: 820px) {
  .list { max-height: none; }
}

.list.full { max-height: none; flex: 1; }

.list li {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.92rem;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.list li:last-child { border-bottom: none; }
.list li:hover { background: rgba(34, 211, 238, 0.06); }
.list li.selected { background: rgba(34, 211, 238, 0.14); }
.list li.empty { color: var(--text-dim); cursor: default; }
.list li.empty:hover { background: none; }

.detail {
  flex: 1;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  overflow-y: auto;
  font-size: 0.92rem;
  line-height: 1.5;
}

.detail .hint { color: var(--text-dim); text-align: center; margin-top: 20px; }

.detail .row {
  display: flex;
  gap: 14px;
  max-width: 420px;
  border-bottom: 1px solid var(--border);
  padding: 5px 0;
}

.detail .row > span:first-child { flex: 0 0 150px; color: var(--text-dim); }
.detail .row > span:last-child { flex: 1; text-align: right; }
.detail .section-gap { height: 10px; }
.detail .warning { color: var(--yellow); margin-bottom: 10px; }

/* --- Bots tab (cards, details in modal) --- */

.bot-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 340px));
  gap: 14px;
  align-content: start;
  justify-content: center;
  overflow-y: auto;
}

.bot-cards .hint { color: var(--text-dim); text-align: center; grid-column: 1 / -1; }

.bot-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.bot-card.grid-long { border-color: rgba(46, 204, 113, 0.35); }
.bot-card.grid-short { border-color: rgba(239, 68, 68, 0.35); }
.bot-card:hover { border-color: rgba(34, 211, 238, 0.5); }
.bot-card.selected { border-color: var(--cyan); background: rgba(34, 211, 238, 0.08); }

.bot-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.grid-type-badge {
  flex-shrink: 0;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 3px 9px;
  border: 1px solid currentColor;
  border-radius: 999px;
}

.bot-card-balance-parts { font-size: 0.85rem; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.bot-card-balance-parts.empty { visibility: hidden; }
.bot-card-balance { font-size: 1.9rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.bot-card-range {
  display: flex;
  justify-content: space-between;
  font-size: 0.88rem;
  font-variant-numeric: tabular-nums;
}

.range-bar {
  position: relative;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, var(--chart-min), var(--chart-max));
  opacity: 0.9;
}

.range-marker {
  position: absolute;
  top: 50%;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text);
  border: 2px solid var(--bg-panel);
  transform: translate(-50%, -50%);
}

.range-marker.out { background: var(--gray); }
.range-marker.profit { background: var(--green); }
.range-marker.loss { background: var(--red); }

/* Segment from the break-even to the current price, drawn over the gradient. */
.range-segment {
  position: absolute;
  top: 0;
  bottom: 0;
  border-radius: 3px;
}

.range-segment.profit { background: var(--green); }
.range-segment.loss { background: var(--red); }

/* Break-even tick: a vertical line across the bar, dashed when the price is outside the range. */
.range-break-even {
  position: absolute;
  top: 50%;
  width: 3px;
  height: 16px;
  border-radius: 1px;
  background: var(--yellow);
  transform: translate(-50%, -50%);
}

.range-break-even.out { background: repeating-linear-gradient(to bottom, var(--yellow) 0 3px, transparent 3px 5px); }

.be { color: var(--yellow); }

.bot-card-foot { display: flex; justify-content: space-between; color: var(--text-dim); font-size: 0.85rem; }
.bot-card-id, .bot-card-error { font-size: 0.78rem; word-break: break-all; }
.bot-card-error { color: var(--red); }

/* Bots view toggle + table view */

.bots-toolbar { display: flex; justify-content: flex-end; margin-bottom: 10px; }

.view-toggle {
  display: inline-flex;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}

.view-toggle button {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.view-toggle button svg { fill: currentColor; }
.view-toggle button:hover { color: var(--text); }
.view-toggle button.active { background: var(--cyan); color: #04222b; }

.bot-cards.table-view { display: block; }

.bot-table-wrap {
  overflow-x: auto;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.bot-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; white-space: nowrap; }

.bot-table th, .bot-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.bot-table th { color: var(--text-dim); font-weight: 500; font-size: 0.8rem; }
.bot-table tbody tr:last-child td { border-bottom: none; }
.bot-table .num { text-align: right; font-variant-numeric: tabular-nums; }

.bot-row { cursor: pointer; transition: background 0.12s; }
.bot-row:hover { background: rgba(34, 211, 238, 0.06); }
.bot-row.selected { background: rgba(34, 211, 238, 0.12); }
.bot-row.grid-long td:first-child { box-shadow: inset 3px 0 0 rgba(46, 204, 113, 0.6); }
.bot-row.grid-short td:first-child { box-shadow: inset 3px 0 0 rgba(239, 68, 68, 0.6); }
.bot-row-balance { font-weight: 600; }
.bot-row-balance-parts { color: var(--text-dim); }
.bot-row-bar { min-width: 120px; width: 20%; }
.bot-row-info { white-space: normal; word-break: break-all; font-size: 0.8rem; }

/* Bot detail modal */

.modal.modal-bot {
  max-width: 760px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  font-size: 0.92rem;
  line-height: 1.5;
}

.modal-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.modal-head .grid-type-badge { font-size: 0.85rem; padding: 4px 12px; }

.modal-bot.grid-long { border-color: rgba(46, 204, 113, 0.35); }
.modal-bot.grid-short { border-color: rgba(239, 68, 68, 0.35); }
.modal-head h2 { margin: 0; }

.modal-close {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
}

.modal-close:hover { color: var(--text); border-color: var(--cyan); }

.modal-bot .hint { color: var(--text-dim); text-align: center; }
.modal-bot .warning { color: var(--yellow); margin: 0 0 12px; }

.detail-sections {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 6px 28px;
}

.detail-section h3 {
  margin: 0 0 4px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--cyan);
}

.detail-section .row {
  display: flex;
  gap: 14px;
  border-bottom: 1px solid var(--border);
  padding: 5px 0;
}

.detail-section .row > span:first-child { color: var(--text-dim); }
.detail-section .row > span:last-child { flex: 1; text-align: right; word-break: break-word; }

@media (max-width: 600px) {
  .detail-sections { gap: 14px; }
}

.view-toggle.text button { font: inherit; font-size: 0.82rem; padding: 4px 12px; }

/* Bot detail: order chart */

.order-chart {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 0;
}

.order-chart svg { display: block; overflow: visible; }
.order-chart text { font-size: 11px; font-variant-numeric: tabular-nums; }
.oc-level { stroke: var(--border); stroke-width: 1; }
.oc-axis { fill: var(--text-dim); }
.oc-open { stroke: var(--chart-blue); stroke-width: 1.5; stroke-dasharray: 6 4; }
.oc-open-label { fill: var(--chart-blue); }
.oc-position { stroke-width: 3; stroke-linecap: round; }
.oc-tp { stroke: var(--chart-orange); stroke-width: 2; stroke-dasharray: 4 3; }
.oc-tp-label { fill: var(--chart-orange); font-weight: 600; }
.oc-link { stroke: var(--text-dim); stroke-width: 1; stroke-dasharray: 2 3; }
.oc-profit { fill: var(--chart-orange); font-weight: 600; paint-order: stroke; stroke: var(--bg); stroke-width: 3px; stroke-linejoin: round; }
.oc-price { stroke: var(--cyan); stroke-width: 1.5; }
.oc-price-label { fill: var(--cyan); font-weight: 600; }

.order-chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin-top: 10px;
  color: var(--text-dim);
  font-size: 0.82rem;
}

.order-chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.oc-swatch { display: inline-block; width: 18px; height: 3px; border-radius: 2px; }
.oc-swatch-tp { background: repeating-linear-gradient(90deg, var(--chart-orange) 0 4px, transparent 4px 7px); }
.oc-swatch-open { background: repeating-linear-gradient(90deg, var(--chart-blue) 0 6px, transparent 6px 10px); }
.oc-swatch-price { background: var(--cyan); }
.order-chart-summary { margin: 6px 0 0; color: var(--text-dim); font-size: 0.82rem; }

.g { color: var(--green); }
.r { color: var(--red); }
.y { color: var(--yellow); }
.gr { color: var(--gray); }
.cy { color: var(--cyan); }

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

.actions-bar button {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.actions-bar button:hover { border-color: var(--cyan); }
#notif-save-btn { background: var(--cyan); color: #04222b; border-color: var(--cyan); font-weight: 600; }

.dirty-flag { color: var(--yellow); font-size: 0.8rem; }
.push-status { margin-left: auto; font-size: 0.8rem; color: var(--text-dim); }

.list li .rule-toggle,
.list li .rule-delete {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.78rem;
  cursor: pointer;
}

.list li .rule-delete:hover { color: var(--red); border-color: var(--red); }
.list li .rule-toggle:hover { color: var(--cyan); border-color: var(--cyan); }

/* --- Markets (Turud) tab --- */

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

.market-clock { font-size: 1rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.markets-hint { color: var(--text-dim); font-size: 0.8rem; }

.market-table-wrap {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
}

.market-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.market-table th {
  text-align: left;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.market-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
}

.market-table tbody tr:last-child td { border-bottom: none; }
.market-table tr.open td:first-child { box-shadow: inset 3px 0 0 rgba(46, 204, 113, 0.5); }
.market-table .num { font-variant-numeric: tabular-nums; }

.market-name { font-weight: 700; }
.market-place { color: var(--text-dim); font-size: 0.78rem; }

.market-status {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 8px;
  border: 1px solid currentColor;
  border-radius: 999px;
}

.market-status.open { color: var(--green); }
.market-status.closed { color: var(--gray); }

.market-countdown { font-variant-numeric: tabular-nums; }
.market-table tr.open .market-countdown b { color: var(--green); }
.market-table tr.closed .market-countdown b { color: var(--yellow); }
.market-session { color: var(--text-dim); }

/* --- Modal --- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}

.modal-backdrop[hidden] { display: none; }

.modal {
  width: 100%;
  max-width: 360px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
}

.modal h2 { margin: 0 0 12px; font-size: 1.05rem; color: var(--cyan); }

.modal .option {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 8px;
  cursor: pointer;
  font-size: 0.88rem;
}

.modal .option:hover { border-color: var(--cyan); }

.modal input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 12px;
}

.modal-buttons { display: flex; gap: 8px; justify-content: flex-end; }

.modal-buttons button {
  background: var(--cyan);
  color: #04222b;
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
}

.modal-buttons button.secondary {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  font-weight: 400;
}

/* --- Toast --- */

.toast {
  position: fixed;
  top: 14px;
  right: 14px;
  left: 14px;
  max-width: 340px;
  margin-left: auto;
  background: var(--bg-panel);
  border: 1px solid var(--cyan);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.88rem;
  z-index: 30;
}

.toast[hidden] { display: none; }

/* --- Chart (Graafik) tab --- */

.chart-panel { gap: 8px; }

.chart-toolbar { display: flex; justify-content: center; }

.timeframe-selector {
  display: inline-flex;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}

.timeframe-selector button {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.timeframe-selector button.active { background: var(--cyan); color: #04222b; font-weight: 600; }
.timeframe-selector button.overbought { color: var(--green); font-weight: 600; }
.timeframe-selector button.oversold { color: var(--red); font-weight: 600; }
.timeframe-selector button.active.overbought { background: var(--green); color: #04220f; }
.timeframe-selector button.active.oversold { background: var(--red); color: #2b0404; }

.chart-status {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
  margin: 4px 0;
}

.chart-status.error { color: var(--red); }

.chart-area {
  position: relative;
  display: flex;
  flex: 1;
  min-height: 320px;
  height: min(560px, 62vh);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

/* Panning/zooming is handled in chart.js (virtual scroll), not by native overflow scrolling.
   pan-y leaves vertical page scrolling to the browser on touch; horizontal drags and pinch come to us. */
.chart-scroll {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  touch-action: pan-y;
  user-select: none;
  cursor: crosshair;
}

.chart-scroll.over-entry-label { cursor: pointer; }
.chart-scroll.dragging { cursor: grabbing; }

.chart-scroll-inner {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: 100%;
}

.chart-scroll-inner canvas { display: block; }

.chart-axis {
  flex: 0 0 76px;
  width: 76px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-left: 1px solid var(--border);
}

.chart-axis canvas { display: block; }

#chart-price-axis-canvas {
  cursor: ns-resize;
  touch-action: none;
  user-select: none;
}

.chart-controls {
  position: absolute;
  top: 6px;
  right: 82px;
  display: flex;
  gap: 6px;
}

.chart-ctrl-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: rgba(18, 22, 27, 0.85);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-ctrl-btn:disabled { opacity: 0.4; cursor: default; }
.chart-ctrl-btn[hidden] { display: none; }

.chart-badge {
  position: absolute;
  left: 6px;
  background: rgba(18, 22, 27, 0.72);
  border-radius: 4px;
  padding: 3px 6px;
  font-size: 10px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
}

.chart-badge[hidden] { display: none; }
.chart-badge-bb { top: 6px; color: var(--chart-orange); }
.chart-badge-stoch { color: var(--chart-purple); }
