:root {
  --bg: var(--tg-theme-bg-color, #0e1217);
  --bg2: var(--tg-theme-secondary-bg-color, #151a21);
  --card: var(--tg-theme-section-bg-color, #1a2029);
  --text: var(--tg-theme-text-color, #e7ecf2);
  --hint: var(--tg-theme-hint-color, #8793a1);
  --accent: var(--tg-theme-button-color, #3b82f6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --sep: var(--tg-theme-section-separator-color, rgba(135, 147, 161, .16));
  --up: #1fbf75;
  --down: #f0464f;
  --warn: #f1a531;
  --tp: #22b8cf;
  --ema-fast: #4c8dff;
  --ema-slow: #e8b923;
  --radius: 14px;
  --tabs-h: 62px;
  --safe-t: 0px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
/* Toàn màn hình trong Telegram: nội dung tràn dưới thanh trạng thái + nút Đóng/⋯ của Telegram */
:root.fs {
  --safe-t: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-b: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
}
:root[data-theme="light"] {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f3f6);
  --card: var(--tg-theme-section-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #111418);
  --hint: var(--tg-theme-hint-color, #6b7684);
  --sep: var(--tg-theme-section-separator-color, rgba(17, 20, 24, .09));
  --up: #0d9458;
  --down: #d62f3a;
  --warn: #b86e00;
  --tp: #0b8599;
  --ema-fast: #2563eb;
  --ema-slow: #b58900;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg2);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num, code, .mono { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }
.hidden { display: none !important; }
.muted { color: var(--hint); }
.up { color: var(--up); }
.down { color: var(--down); }
.warn { color: var(--warn); }

/* ---------- top bar ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-t)) 14px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--sep);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; letter-spacing: -.01em; }
.brand svg { width: 22px; height: 22px; color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 6px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: 999px; border: 1px solid var(--sep);
  background: var(--bg2); font-size: 12px; color: var(--hint); white-space: nowrap;
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--hint); flex: none; }
.dot.on { background: var(--up); box-shadow: 0 0 0 3px color-mix(in srgb, var(--up) 22%, transparent); }
.dot.off { background: var(--down); }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; flex: none;
  border-radius: 10px; border: 1px solid var(--sep); background: var(--bg2); color: var(--text);
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.on { background: color-mix(in srgb, var(--warn) 18%, var(--bg2)); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
#fsBtn .i-collapse, #fsBtn.fs-on .i-expand { display: none; }
#fsBtn.fs-on .i-collapse { display: block; }
.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */
main { padding: 12px 12px calc(var(--tabs-h) + var(--safe-b) + 16px); max-width: 760px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }
.card {
  background: var(--card); border-radius: var(--radius); padding: 14px;
  border: 1px solid var(--sep); margin-bottom: 12px;
}
.card h3 { margin: 0 0 10px; font-size: 15px; }
.card h4 { margin: 14px 0 8px; font-size: 13px; color: var(--hint); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* ---------- segmented ---------- */
.seg {
  display: flex; gap: 2px; padding: 3px; border-radius: 11px;
  background: var(--bg); border: 1px solid var(--sep);
}
.seg button {
  flex: 1; border: 0; background: transparent; padding: 7px 8px; border-radius: 8px;
  font-weight: 600; font-size: 13px; color: var(--hint); white-space: nowrap;
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .18); }
:root[data-theme="light"] .seg button.on { background: var(--accent); color: var(--accent-text); box-shadow: none; }
.seg.small button { padding: 5px 8px; font-size: 12px; }
#trackSeg { overflow-x: auto; scrollbar-width: none; }
#trackSeg::-webkit-scrollbar { display: none; }
.seg .count { font-weight: 500; opacity: .7; margin-left: 3px; }

.filters { display: grid; gap: 8px; margin-bottom: 10px; }
.filters .row { display: flex; gap: 8px; align-items: center; }
.filters .row .seg { flex: 1; }
input[type="search"], input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%; min-width: 0; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--sep); background: var(--bg); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.filters select { width: auto; flex: none; }
.summary { font-size: 12px; color: var(--hint); margin: 2px 2px 10px; display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }

/* ---------- signal cards ---------- */
.list { display: grid; gap: 10px; }
.sig {
  background: var(--card); border: 1px solid var(--sep); border-radius: var(--radius);
  padding: 12px 12px 10px; cursor: pointer; position: relative; overflow: hidden;
}
.sig::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--up); }
.sig.short::before { background: var(--down); }
.sig:active { transform: scale(.995); }
.sig-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.sym { font-weight: 700; font-size: 15px; letter-spacing: -.01em; }
.star { color: var(--warn); font-size: 13px; }
.badge {
  display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 700;
  background: var(--bg2); color: var(--hint); border: 1px solid var(--sep);
}
.badge.long { color: var(--up); background: color-mix(in srgb, var(--up) 12%, transparent); border-color: transparent; }
.badge.short { color: var(--down); background: color-mix(in srgb, var(--down) 12%, transparent); border-color: transparent; }
.badge.phase-pullback { color: var(--warn); background: color-mix(in srgb, var(--warn) 13%, transparent); border-color: transparent; }
.badge.accent { color: var(--accent); background: color-mix(in srgb, var(--accent) 13%, transparent); border-color: transparent; }
.sig-head .right { margin-left: auto; font-size: 11px; color: var(--hint); }
.sig-price { display: flex; align-items: baseline; gap: 10px; margin: 6px 0 8px; flex-wrap: wrap; }
.sig-price .p { font-size: 18px; font-weight: 700; }
.sig-price .meta { font-size: 12px; color: var(--hint); }

.levels { display: grid; grid-template-columns: auto 1fr auto; gap: 3px 10px; font-size: 13px; align-items: center; }
.levels .n { color: var(--hint); white-space: nowrap; }
.levels .v { text-align: right; font-weight: 600; }
.levels .d { text-align: right; font-size: 12px; min-width: 62px; }
.levels .hit .n::after { content: " ✓"; color: var(--up); }
.levels .row-sl .n, .levels .row-sl .v { color: var(--down); }
.levels .row-tp .n, .levels .row-tp .v { color: var(--tp); }
.levels .sep { grid-column: 1 / -1; height: 1px; background: var(--sep); margin: 2px 0; }

.ladder { position: relative; height: 22px; margin: 10px 2px 2px; }
.ladder .rail { position: absolute; left: 0; right: 0; top: 10px; height: 3px; border-radius: 3px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--down) 55%, transparent), var(--sep) 40%, color-mix(in srgb, var(--tp) 55%, transparent)); }
.ladder .tick { position: absolute; top: 5px; width: 2px; height: 13px; border-radius: 1px; background: var(--up); transform: translateX(-1px); }
.ladder .tick.sl { background: var(--down); }
.ladder .tick.tp { background: var(--tp); }
.ladder .now { position: absolute; top: 3px; width: 10px; height: 16px; transform: translateX(-5px); }
.ladder .now::before { content: ""; position: absolute; left: 4px; top: 0; bottom: 0; width: 2px; background: var(--text); border-radius: 2px; }
.ladder .now::after { content: ""; position: absolute; left: 1px; top: -4px; border: 4px solid transparent; border-top-color: var(--text); }

.empty { text-align: center; padding: 40px 16px; color: var(--hint); }
.empty svg { width: 42px; height: 42px; opacity: .5; margin-bottom: 8px; }
.skeleton { height: 150px; border-radius: var(--radius); background: linear-gradient(90deg, var(--card), var(--bg2), var(--card)); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

/* ---------- tracks & alerts ---------- */
.trk-status { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 6px; }
.st-waiting { color: var(--hint); background: var(--bg2); }
.st-open { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.st-tp { color: var(--tp); background: color-mix(in srgb, var(--tp) 14%, transparent); }
.st-sl { color: var(--down); background: color-mix(in srgb, var(--down) 14%, transparent); }
.alert-item { background: var(--card); border: 1px solid var(--sep); border-radius: 12px; padding: 10px 12px; font-size: 13px; }
.alert-item .time { font-size: 11px; color: var(--hint); margin-bottom: 4px; }
.alert-item.k-tp { border-left: 3px solid var(--tp); }
.alert-item.k-sl { border-left: 3px solid var(--down); }
.alert-item.k-entry { border-left: 3px solid var(--up); }
.alert-item.k-trade { border-left: 3px solid var(--accent); }

/* ---------- auto trade ---------- */
.badge.auto { color: var(--accent-text); background: var(--accent); border-color: transparent; font-size: 10px; letter-spacing: .03em; }
.badge.testnet { color: var(--warn); background: color-mix(in srgb, var(--warn) 13%, transparent); border-color: transparent; }
.key-box { margin-bottom: 6px; }
.key-line { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 12px; background: var(--bg); border: 1px solid var(--sep); }
.key-line b { word-break: break-all; }
.key-status { font-size: 12px; margin-top: 3px; }
.key-actions { display: flex; gap: 12px; flex: none; }
.exec-line { font-size: 12px; color: var(--hint); margin: 6px 2px 0; }
.exec-line b { color: var(--text); font-weight: 600; }
.key-form { border: 1px solid var(--sep); border-radius: 12px; padding: 12px; margin: 8px 0; background: var(--bg2); }
.trade-sum { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 13px; padding: 10px 12px; background: var(--card); border: 1px solid var(--sep); border-radius: var(--radius); }
.trade-sum .muted { font-size: 12px; }
.list-head { font-size: 12px; font-weight: 600; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; margin: 6px 2px -2px; }
.trade-card .levels .d { min-width: 0; font-size: 11.5px; }
.levels .plan .n, .levels .plan .v { opacity: .75; }
.trade-actions { display: flex; justify-content: flex-end; }
.btn.small { width: auto; padding: 8px 12px; font-size: 13px; border-radius: 10px; }
.btn.ghost.danger { color: var(--down); border-color: color-mix(in srgb, var(--down) 40%, transparent); }
.trade-err { margin-top: 8px; font-size: 12px; color: var(--warn); }
.trade-foot { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-top: 8px; font-size: 12px; }
.trade-log summary { color: var(--hint); cursor: pointer; }
.trade-log div { margin-top: 3px; }
.sq-preview { margin-top: 10px; display: grid; gap: 8px; }
.sq-preview .lbl { font-size: 11px; font-weight: 600; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; }
.sq-preview pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: 13px/1.45 inherit; font-family: inherit; background: var(--bg); border: 1px solid var(--sep); border-radius: 10px; padding: 10px 12px; }
.sq-links { display: flex; gap: 12px; flex-wrap: wrap; }
.sq-links .err { color: var(--warn); }

/* ---------- forms ---------- */
.field { display: grid; gap: 5px; margin-bottom: 12px; }
.field > span { font-size: 12px; color: var(--hint); font-weight: 600; }
.field small { color: var(--hint); font-size: 11px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.checks { display: flex; gap: 6px; flex-wrap: wrap; }
.chk { position: relative; }
.chk input { position: absolute; opacity: 0; pointer-events: none; }
.chk span {
  display: inline-block; padding: 7px 12px; border-radius: 10px; border: 1px solid var(--sep);
  background: var(--bg); font-weight: 600; font-size: 13px; color: var(--hint);
}
.chk input:checked + span { color: var(--accent); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--bg)); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--sep); }
.switch-row:last-child { border-bottom: 0; }
.switch-row .txt { display: grid; }
.switch-row small { color: var(--hint); font-size: 12px; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch i { position: absolute; inset: 0; border-radius: 26px; background: var(--sep); transition: .2s; }
.switch i::before { content: ""; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + i { background: var(--up); }
.switch input:checked + i::before { transform: translateX(18px); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 12px 16px; border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 15px;
}
.btn:disabled { opacity: .55; }
.btn.ghost { background: var(--bg); color: var(--text); border: 1px solid var(--sep); }
.errors { color: var(--down); font-size: 13px; margin: 0 0 10px; padding-left: 18px; }
.note { font-size: 12px; color: var(--hint); background: var(--bg); border-radius: 10px; padding: 8px 10px; margin-bottom: 12px; border: 1px dashed var(--sep); }

/* ---------- backtest ---------- */
.progress { height: 8px; border-radius: 8px; background: var(--bg); overflow: hidden; border: 1px solid var(--sep); }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.kpi { background: var(--bg); border-radius: 12px; padding: 10px; border: 1px solid var(--sep); min-width: 0; }
.kpi .l { font-size: 11px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .v { font-size: 18px; font-weight: 700; margin-top: 2px; white-space: nowrap; }
.kpi .s { font-size: 11px; color: var(--hint); }
.tbl-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
th, td { padding: 7px 6px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--sep); }
th { color: var(--hint); font-weight: 600; font-size: 11.5px; cursor: default; }
th.sortable { cursor: pointer; }
th:first-child, td:first-child { text-align: left; }
tr:last-child td { border-bottom: 0; }
.eq-chart { position: relative; z-index: 0; height: 220px; margin: 4px -4px 0; }
.hist-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--sep); cursor: pointer; font-size: 13px; }
.hist-item:last-child { border-bottom: 0; }
.link { color: var(--tg-theme-link-color, var(--accent)); text-decoration: none; font-weight: 600; background: none; border: 0; padding: 0; }
.bt-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 10px; }
.bt-head .muted { font-size: 12px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; padding: 6px 6px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--sep);
}
.tabs button {
  flex: 1; display: grid; justify-items: center; gap: 2px; padding: 5px 0; border: 0; background: none;
  color: var(--hint); font-size: 11px; font-weight: 600; border-radius: 10px; position: relative;
}
.tabs button svg { width: 22px; height: 22px; }
.tabs button.on { color: var(--accent); }
.tabs .bubble { position: absolute; top: 1px; left: calc(50% + 6px); min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--down); color: #fff; font-size: 10px; line-height: 16px; }

/* ---------- sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); animation: fade .2s; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; top: calc(24px + var(--safe-t)); max-width: 760px; margin: 0 auto;
  background: var(--bg); border-radius: 18px 18px 0 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 0 14px calc(20px + var(--safe-b)); animation: up .25s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }
.sheet-head { position: sticky; top: 0; z-index: 5; background: var(--bg); display: flex; align-items: center; gap: 8px; margin: 0 -14px; padding: 12px 14px 10px; }
.sheet-head .title { flex: 1; min-width: 0; }
.sheet-head .title .sym { font-size: 17px; }
.sheet-head .title .sub { font-size: 12px; color: var(--hint); }
.grab { width: 36px; height: 4px; border-radius: 2px; background: var(--sep); margin: 8px auto 0; }
#chart { position: relative; z-index: 0; height: 340px; margin: 8px -6px 4px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 11px; color: var(--hint); margin-bottom: 12px; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 12.5px; }
.info-grid div { background: var(--card); border: 1px solid var(--sep); border-radius: 10px; padding: 8px 10px; }
.info-grid b { display: block; font-size: 13.5px; margin-top: 2px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + var(--safe-b) + 14px); z-index: 60;
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-size: 13px; font-weight: 600;
  transition: .25s; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 380px) {
  .grid3 { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 480px) {
  .kpis { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 560px) {
  #chart { height: 420px; }
}
