:root {
  --bg: var(--tg-theme-bg-color, #0f1419);
  --text: var(--tg-theme-text-color, #f4f7fb);
  --hint: var(--tg-theme-hint-color, #8b9bb4);
  --btn: var(--tg-theme-button-color, #2ea6ff);
  --btn-text: var(--tg-theme-button-text-color, #fff);
  --sec: var(--tg-theme-secondary-bg-color, #1b232d);
  --nav-h: 48px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Vazirmatn", "Segoe UI", Tahoma, sans-serif;
}
#app {
  position: relative;
  max-width: 520px;
  margin: 0 auto;
  height: 100dvh;
  padding: 6px 12px calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; flex: 0 0 auto; }
.head-side { display: flex; align-items: center; gap: 8px; }
#logoutBtn { width: auto; margin: 0; padding: 6px 10px; border: 0; border-radius: 12px; background: var(--sec); color: var(--text); font: inherit; }
#logoutBtn[hidden] { display: none; }
h1 { font-size: 1.05rem; margin: 0; }
#sub { margin: 2px 0 0; color: var(--hint); font-size: .75rem; }
.pill { font-size: .7rem; padding: 3px 8px; border-radius: 999px; background: #3a2a2a; }
.pill.on { background: #1f3d2f; }
nav {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: 100%;
  max-width: 520px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  margin: 0;
  padding: 3px 6px calc(3px + env(safe-area-inset-bottom, 0px));
  min-height: var(--nav-h);
  background: rgba(15, 20, 25, .96);
  border-top: 1px solid rgba(255, 255, 255, .08);
  z-index: 15;
}
nav button, .kinds button, .toggle, .row button, .grid2 button, form button, #pushBtn {
  border: 0;
  border-radius: 12px;
  padding: 10px 8px;
  background: var(--sec);
  color: var(--text);
  font: inherit;
}
nav button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px 2px;
  border-radius: 10px;
  background: transparent;
  color: var(--hint);
  font-size: .62rem;
  line-height: 1;
  min-width: 0;
}
nav button svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
nav button.on, .kinds button.on { background: var(--btn); color: var(--btn-text); }
nav button.on {
  background: transparent;
  color: var(--btn);
}
.page { display: none; }
.page.on { display: block; }
main {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
}
#strategies.page.on {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  overflow: auto;
  overflow-anchor: none;
  overscroll-behavior: contain;
  container-type: size;
}
.card {
  background: var(--sec);
  border-radius: 16px;
  padding: 14px;
  margin: 10px 0;
  line-height: 1.6;
}
details.fold > summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 1rem;
}
details.fold > summary::-webkit-details-marker { display: none; }
.price strong { display: block; font-size: 1.8rem; margin-top: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.toggle.on { background: #1f3d2f; }
.toggle.danger.on { background: #5a1f1f; }
.danger { background: #3a2222; }
.acc-intro { margin: 0 2px 10px; line-height: 1.6; }
.acc-card { display: grid; gap: 10px; }
.acc-card .acc-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: flex-start;
}
.auto-form h3 { margin: 8px 0 4px; font-size: .95rem; }
.auto-form label { display: block; font-size: .85rem; color: var(--hint); margin-top: 8px; }
.auto-form input, .auto-form select {
  width: 100%;
  margin-top: 4px;
  border: 0;
  border-radius: 12px;
  padding: 10px;
  background: var(--sec);
  color: var(--text);
  font: inherit;
}
.auto-form .row.kinds { margin-top: 8px; }
.inst-list { display: grid; gap: 8px; margin-top: 8px; }
.inst-row {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 8px;
  align-items: center;
}
.inst-row select { margin: 0; }
.inst-row .sym { font-weight: 600; }
.ohlcv-pct {
  font-size: .78rem;
  font-variant-numeric: tabular-nums;
  min-width: 2.8rem;
  color: var(--muted, #8b98a5);
  text-align: center;
}
.ohlcv-pct.hot { color: #ffd54f; }
.ohlcv-pct.done { color: #3ddc97; }
#stgInst .inst-row { grid-template-columns: auto auto 1fr auto; }
#stgSymbols .ohlcv-pct { min-width: 0; margin-inline-start: 4px; }
#lvlSymbols, #stgSymbols { display: flex; flex-wrap: wrap; gap: 6px; }
#lvlSymbols button, #stgSymbols button { padding: 8px 10px; font-size: .78rem; }
#stgList { display: grid; gap: 8px; margin: 8px 0; }
.stg-card { margin: 0; cursor: pointer; }
.stg-card.on { outline: 2px solid var(--btn); }
.stg-card .stg-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.stg-editor.is-open {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  margin: 0;
  border-radius: 0;
  max-width: none;
  background: var(--bg);
  padding: 8px 12px calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 12px);
  overflow: hidden;
}
.stg-editor-top {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  flex: 0 0 auto;
}
.stg-editor-top h2 { margin: 0; font-size: 1rem; text-align: center; }
.stg-editor-top button[type="submit"] { background: var(--btn); color: var(--btn-text); }
.stg-editor-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  flex: 0 0 auto;
  padding: 8px 0;
}
.stg-editor-tabs button { flex: 0 0 auto; white-space: nowrap; padding: 8px 12px; }
#stgEdPvApply, #stgEdPvAdd { width: 100%; margin-top: 8px; }
.stg-editor-body { overflow: auto; flex: 1 1 auto; min-height: 0; }
.stg-pane { display: none; }
.stg-editor.is-open .stg-pane.on { display: block; }
.stg-editor.is-open #stgInds .ind-row { display: none; }
.stg-editor.is-open #stgInds .ind-row.on { display: block; }
.ind-row { margin-top: 10px; padding-top: 8px; border-top: 1px solid rgba(255,255,255,.06); }
.ind-row.locked { opacity: 1; }
.ind-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.ind-head b { flex: 1; min-width: 8rem; }
.ind-ord {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--sec);
  font-size: .78rem;
  font-weight: 700;
}
.ind-tools { display: flex; gap: 4px; }
#stgAddIndBtn { width: 100%; margin-top: 10px; }
.wait-min { display: block; margin-top: 8px; }
.wait-min[hidden] { display: none !important; }
.ind-picker { display: grid; gap: 6px; margin-top: 8px; }
.ind-picker[hidden] { display: none; }
.ind-picker button { text-align: right; padding: 10px 12px; }
.ind-picker button small { display: block; color: var(--hint); font-size: .75rem; margin-top: 2px; }
.ind-tools button { padding: 6px 8px; min-width: 0; }
.ind-params { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ind-row.off .ind-params { opacity: .4; }
#stgAccs { display: grid; gap: 6px; margin-top: 6px; }
#stgForm input, #stgForm select {
  background: var(--bg);
}
.bt-span { align-items: center; }
.bt-span > input { flex: 0 0 88px; }
#stgBtUnit { margin: 0; flex: 1; }
#stgBtRun { width: 100%; margin-top: 8px; }
.bt-bar {
  height: 8px;
  margin-top: 10px;
  border-radius: 99px;
  background: rgba(255, 255, 255, .1);
  overflow: hidden;
}
.bt-bar[hidden] { display: none; }
.bt-bar > div {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--btn);
  transition: width .25s linear;
}
#stgBtOut { margin-top: 10px; }
.bt-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 8px 0;
}
.bt-grid div {
  background: var(--bg);
  border-radius: 12px;
  padding: 8px 10px;
}
.bt-grid.wide { grid-template-columns: 1fr 1fr; }
.tpl-list { display: grid; gap: 6px; margin-top: 8px; }
.tpl-list button { text-align: right; padding: 10px 12px; }
.tpl-list button small { display: block; color: var(--hint); font-size: .75rem; margin-top: 2px; }
.bt-eq { width: 100%; height: 90px; margin: 8px 0; }
.bt-table { width: 100%; border-collapse: collapse; font-size: .78rem; }
.bt-table th, .bt-table td { text-align: right; padding: 4px 6px; border-bottom: 1px solid rgba(255,255,255,.06); }
.bt-table .win { color: #7dffa3; }
.bt-table .loss { color: #ff8a8a; }
.acc b { display: block; }
.small { color: var(--hint); font-size: .8rem; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chips span {
  background: var(--sec);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: .9rem;
}
.chips span.on { outline: 1px solid var(--btn); }
.chips span button { background: transparent; color: #ff8a8a; padding: 0 4px; }
.step-tf { margin: 8px 0 12px; }
.step-tfs { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.step-tfs button { padding: 8px 2px; font-size: .75rem; }
#lvlTfs, #stgTfs, #stgFormTfs { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
#lvlTfs button, #stgTfs button, #stgFormTfs button { padding: 8px 4px; font-size: .78rem; }
.chart-wrap {
  position: relative;
  height: min(58vh, 360px);
  border-radius: 12px;
  overflow: hidden;
  background: #131722;
  touch-action: none;
  overscroll-behavior: none;
  margin: 8px 0;
  user-select: none;
  cursor: grab;
}
.chart-wrap.is-panning,
body.chart-panning .chart-wrap { cursor: grabbing; }
.chart-wrap.stg-chart {
  height: min(52dvh, 420px);
  min-height: 240px;
  max-height: none;
  margin: 0 0 8px;
  flex: 0 0 auto;
}
.chart-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
.chart-hud {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(19, 23, 34, .88);
  color: #d1d4dc;
  padding: 4px 8px;
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
  direction: ltr;
  font-variant-numeric: tabular-nums;
}
.chart-wrap.trade-chart { height: min(68vh, 520px); }
.trade-draw h2 { margin-bottom: 4px; }
#tdActive:empty { display: none; }
#tdInds .ind-row { border-top: 0; margin-top: 8px; }
.chart-ui {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 0;
  overflow: visible;
  pointer-events: none;
  z-index: 5;
}
.chart-fs-toggle,
.chart-fs-draw button {
  pointer-events: auto;
  border: 0;
  font: inherit;
  color: var(--text);
}
.chart-fs-toggle {
  position: absolute;
  top: 8px;
  right: 78px;
  left: auto;
  z-index: 6;
  background: rgba(19, 23, 34, .88);
  color: #d1d4dc;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: .78rem;
}
.chart-fs-draw {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  left: 8px;
  z-index: 6;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  pointer-events: none;
}
.chart-wrap.chart-fs .chart-fs-draw { display: grid; }
.chart-fs-draw button {
  border-radius: 12px;
  padding: 10px 8px;
  background: rgba(27, 35, 45, .94);
}
.chart-fs-draw .trade-side-btn[data-side="buy"].on { background: #26a69a; color: #0b1210; }
.chart-fs-draw .trade-side-btn[data-side="sell"].on { background: #ef5350; color: #140808; }
.chart-fs-draw .buy { background: #1f3d2f; }
.chart-fs-draw .sell { background: #5a1f1f; }
.chart-fs-draw .buy:disabled,
.chart-fs-draw .sell:disabled { opacity: .45; }
.chart-fs-exit { grid-column: 1 / -1; background: #3a2222 !important; }
@media (min-width: 700px) {
  .chart-fs-draw { grid-template-columns: 1fr 1fr 1fr 1fr 0.8fr; }
  .chart-fs-exit { grid-column: auto; }
  .chart-fs-tfs { grid-template-columns: repeat(6, 1fr) 0.8fr; }
  .chart-fs-tfs .chart-fs-exit { grid-column: auto; }
}
.chart-wrap.chart-fs {
  position: fixed;
  inset: 0;
  z-index: 50;
  width: 100%;
  height: 100dvh !important;
  max-height: none;
  margin: 0;
  border-radius: 0;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.chart-wrap.chart-fs.trade-chart .chart-fs-toggle,
.chart-wrap.chart-fs.stg-chart .chart-fs-toggle { display: none; }
.chart-fs-tfs { grid-template-columns: repeat(6, 1fr); }
.chart-fs-tfs .chart-fs-exit { grid-column: 1 / -1; }
.chart-fs-tfs button.on { background: var(--btn); color: var(--btn-text); }
body.chart-fs-on { overflow: hidden; }
body.chart-fs-on nav { visibility: hidden; pointer-events: none; }
#tradeTfs, #tradeSide { display: grid; gap: 6px; }
#tradeTfs { grid-template-columns: repeat(6, 1fr); }
#tradeSide { grid-template-columns: 1fr 1fr; }
#tradeTfs button, #tradeSide button, .trade-side-btn { padding: 8px 4px; font-size: .78rem; }
#tradeSide .trade-side-btn[data-side="buy"].on,
.chart-fs-draw .trade-side-btn[data-side="buy"].on { background: #26a69a; color: #0b1210; }
#tradeSide .trade-side-btn[data-side="sell"].on,
.chart-fs-draw .trade-side-btn[data-side="sell"].on { background: #ef5350; color: #140808; }
.chart-hint { color: var(--hint); font-size: .75rem; margin: 0 2px 8px; line-height: 1.5; }
.row { display: flex; gap: 8px; margin: 8px 0; }
.row input, .form input {
  flex: 1;
  border: 0;
  border-radius: 12px;
  padding: 10px;
  background: var(--sec);
  color: var(--text);
  font: inherit;
}
.form { display: grid; gap: 8px; }
.form h2, .trade-box h2 { margin: 0; font-size: 1rem; }
.trade-box { display: grid; gap: 8px; }
.trade-box label { display: grid; gap: 4px; font-size: .85rem; color: var(--hint); }
.trade-box input[type="text"], .trade-box input:not([type="checkbox"]) {
  border: 0;
  border-radius: 12px;
  padding: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.lock-row {
  display: flex !important;
  align-items: flex-start;
  gap: 8px;
  color: var(--text) !important;
  font-size: .9rem !important;
  line-height: 1.45;
}
.lock-row input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--btn); }
.stg-editor .lock-row input { flex: 0 0 18px; padding: 0; }
.buy { background: #1f3d2f; }
.sell { background: #5a1f1f; }
.buy:disabled, .sell:disabled { opacity: .45; }
img { width: 100%; border-radius: 12px; margin-top: 8px; background: #111; min-height: 80px; }
.report { white-space: pre-wrap; font-size: .85rem; overflow-x: auto; }
#err {
  color: #ff8a8a;
  position: absolute;
  top: 8px;
  left: 12px;
  right: 12px;
  margin: 0;
  z-index: 16;
  text-align: center;
  pointer-events: none;
  font-size: .8rem;
}
#err[hidden] { display: none !important; }
#gate {
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 18, .92);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 20;
}
#gate[hidden] { display: none; }
#gate .card { width: min(420px, 100%); display: grid; gap: 10px; }
#gate h2 { margin: 0; font-size: 1.1rem; }
#gate input, #gate button {
  border: 0;
  border-radius: 12px;
  padding: 10px;
  font: inherit;
}
#gate input { background: var(--bg); color: var(--text); }
#gate button { background: var(--btn); color: var(--btn-text); }
.risk-card h2 { margin: 0 0 4px; font-size: 1rem; }
.risk-card .row { margin: 8px 0 4px; }
.risk-card .row input, .risk-line .row input { background: var(--bg); }
.risk-card button, .risk-line button { background: var(--btn); color: var(--btn-text); }
.risk-line { margin-top: 10px; }
.risk-line label { display: block; font-size: .85rem; color: var(--hint); }
.risk-line .row { margin: 6px 0 0; }
.pack-preview { white-space: pre-wrap; margin: 8px 0; }
#packList { display: grid; gap: 8px; margin-top: 8px; }
#packList .acc { margin: 0; }
#packList .grid2 { gap: 6px; }
#pvAdd { width: 100%; }
.desk-split, .desk-main, .desk-side { display: contents; }
button { cursor: pointer; }
body.chart-panning {
  overscroll-behavior: none;
  user-select: none;
}
body.chart-panning main,
body.chart-panning #strategies.page.on {
  overscroll-behavior: none;
}

@media (min-width: 900px) {
  html, body { height: 100%; }
  #app {
    max-width: none;
    width: 100%;
    height: 100dvh;
    min-height: 0;
    margin: 0;
    padding: 6px 16px calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
  }
  header { margin-bottom: 4px; }
  h1 { font-size: 1.2rem; }
  .price strong { font-size: 2.4rem; }
  nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    transform: none;
    width: 100%;
    max-width: none;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 140px));
    justify-content: center;
    gap: 8px;
    margin: 0;
    padding: 3px 16px calc(3px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid rgba(255, 255, 255, .08);
    border-bottom: 0;
    background: rgba(15, 20, 25, .96);
  }
  nav button { max-width: none; font-size: .68rem; }
  nav button svg { width: 22px; height: 22px; }
  main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
  .page.on { flex: 1; min-height: 0; }
  #strategies.page.on,
  #trade.page.on,
  #accounts.page.on {
    display: flex;
    flex-direction: column;
  }
  #home.page.on {
    display: grid;
    grid-template-columns: minmax(280px, 1fr) minmax(320px, 1.1fr);
    gap: 12px 20px;
    align-items: start;
  }
  #home .price { grid-column: 1 / -1; }
  #home #riskCard { grid-column: 1; }
  #home #statusCard { grid-column: 2; grid-row: 2 / span 5; margin: 0; }
  .desk-split {
    display: grid;
    grid-template-columns: minmax(0, 1.7fr) minmax(300px, 420px);
    gap: 16px 20px;
    align-items: stretch;
    min-height: 0;
    height: auto;
  }
  .desk-main, .desk-side {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
  .desk-side { overflow: auto; }
  #trade.page.on .chart-wrap {
    flex: 1;
    height: auto;
    min-height: 520px;
  }
  #strategies.page.on {
    overflow: auto;
    height: 100%;
    container-type: size;
  }
  #strategies.page.on .desk-split {
    grid-template-columns: minmax(0, 1.7fr) minmax(300px, 420px);
    grid-template-rows: minmax(280px, 1fr) auto;
    height: 100%;
    min-height: 0;
    align-items: stretch;
  }
  #strategies.page.on #stgChartWrap {
    grid-column: 1;
    grid-row: 1;
    flex: none;
    height: 100%;
    min-height: 280px;
    max-height: none;
    margin: 0;
  }
  #strategies.page.on .desk-main {
    grid-column: 1;
    grid-row: 2;
  }
  #strategies.page.on .desk-side {
    grid-column: 2;
    grid-row: 1 / span 2;
    overflow: auto;
    max-height: 100%;
  }
  #strategies.page.on .desk-main,
  #strategies.page.on .desk-side {
    overflow: visible;
  }
  #strategies.page.on .desk-main .chart-hint,
  #strategies.page.on .desk-main .grid2,
  #strategies.page.on .desk-main .row,
  #strategies.page.on .desk-main .chips,
  #trade.page.on .desk-main .chart-hint,
  #trade.page.on .desk-main #tradeCard {
    flex: 0 0 auto;
  }
  .trade-box {
    grid-template-columns: 1fr 1fr;
  }
  .trade-box h2,
  .trade-box > .small,
  .trade-box .lock-row,
  .trade-box .risk-line,
  .trade-box .guardRiskNote,
  .trade-box .riskNote,
  .trade-box .grid2,
  #stopPreview,
  #orderOut {
    grid-column: 1 / -1;
  }
  #accList { display: grid; grid-template-columns: 1fr; gap: 8px; align-content: start; }
  img { min-height: 160px; }
}
