/* TradingView dark palette */
:root {
  --bg: #131722;
  --surface: #1e222d;
  --surface-2: #2a2e39;
  --border: #2a2e39;
  --hover: #2a2e39;
  --text: #d1d4dc;
  --text-strong: #f0f3fa;
  --muted: #787b86;
  --accent: #2962ff;
  --accent-hover: #1e53e5;
  --buy: #2962ff;
  --sell: #f23645;
  --up: #089981;
  --down: #f23645;
  --terminal-h: 260px;
  --font: -apple-system, BlinkMacSystemFont, "Trebuchet MS", Roboto, Ubuntu, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--font);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

.is-up { color: var(--up); }
.is-down { color: var(--down); }
.is-buy { color: var(--buy); }
.muted { color: var(--muted); }

/* ---------- layout ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app[hidden],
.setup[hidden] {
  display: none;
}

/* ---------- setup ---------- */

.setup {
  height: 100vh;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(41, 98, 255, 0.12), transparent 60%),
    var(--bg);
}

.setup-card {
  width: 480px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.setup-head h1,
.setup-head h2 {
  margin: 0 0 4px;
  font-size: 20px;
  font-weight: 600;
  color: var(--text-strong);
}

.setup-head p {
  margin: 0;
  color: var(--muted);
}

.setup-card .input,
.setup-card .select {
  height: 38px;
}

.setup-hint {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.setup-error:empty {
  display: none;
}

.setup-start {
  height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.setup-start:disabled {
  opacity: 0.8;
  cursor: default;
}

.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.seg-btn {
  min-width: 48px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: border-color 0.12s, background-color 0.12s, color 0.12s;
}

.seg-btn:hover:not(:disabled) {
  border-color: #50535e;
  color: var(--text-strong);
}

.seg-btn.is-active {
  border-color: var(--accent);
  background: rgba(41, 98, 255, 0.15);
  color: var(--text-strong);
}

.seg-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.seg-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
}

.spinner--lg {
  width: 28px;
  height: 28px;
  border-width: 3px;
  border-color: rgba(41, 98, 255, 0.25);
  border-top-color: var(--accent);
}

.spinner[hidden] {
  display: none;
}

.btn--sm {
  height: 30px;
  padding: 0 14px;
  border-radius: 4px;
  font-size: 12px;
}

a.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

/* ---------- brand / auth ---------- */

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-strong);
  font-weight: 700;
  letter-spacing: 0.02em;
}

.setup-head .brand {
  margin-bottom: 18px;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent) 0 50%, var(--up) 50% 100%);
}

.auth-card {
  width: 400px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border-radius: 6px;
  background: var(--bg);
}

.auth-tab {
  height: 32px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s;
}

.auth-tab:hover {
  color: var(--text-strong);
}

.auth-tab.is-active {
  background: var(--surface-2);
  color: var(--text-strong);
}

.auth-tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ---------- dashboard ---------- */

body.page {
  overflow: auto;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(19, 23, 34, 0.92);
  backdrop-filter: blur(8px);
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-strong);
  font-weight: 500;
}

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2962ff, #7b61ff);
}

.dash {
  display: grid;
  grid-template-columns: 440px minmax(0, 1fr);
  align-items: start;
  gap: 24px;
  max-width: 1320px;
  margin: 0 auto;
  padding: 28px 24px 48px;
}

.dash-new {
  position: sticky;
  top: 84px;
}

.dash-new .setup-card {
  width: 100%;
  box-shadow: none;
}

.dash-history-head {
  margin-bottom: 14px;
}

.dash-history-head h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-strong);
}

.dash-history-head p {
  margin: 0;
  color: var(--muted);
}

.bt-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bt-item {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  transition: border-color 0.12s;
}

.bt-item:hover {
  border-color: #3a3e4a;
}

.bt-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bt-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.bt-title h3 {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-strong);
}

.badge {
  flex: none;
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.badge--active {
  background: rgba(41, 98, 255, 0.16);
  color: #6b93ff;
}

.badge--done {
  background: rgba(8, 153, 129, 0.16);
  color: #22c39f;
}

.bt-meta,
.bt-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.bt-meta b {
  color: var(--text);
}

.bt-stats b {
  color: var(--text-strong);
  font-weight: 600;
}

.bt-stats b.is-up { color: var(--up); }
.bt-stats b.is-down { color: var(--down); }

.bt-progress {
  height: 4px;
  border-radius: 2px;
  background: var(--surface-2);
  overflow: hidden;
}

.bt-progress span {
  display: block;
  height: 100%;
  border-radius: 2px;
  background: var(--accent);
}

.bt-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.bt-actions .btn {
  min-width: 92px;
}

.bt-actions .icon-btn {
  color: var(--muted);
}

.bt-actions .icon-btn:hover {
  color: var(--down);
}

.bt-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 48px 24px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.bt-empty b {
  color: var(--text-strong);
}

@media (max-width: 980px) {
  .dash {
    grid-template-columns: 1fr;
  }
  .dash-new {
    position: static;
  }
}

@media (max-width: 560px) {
  .topbar,
  .dash {
    padding-left: 16px;
    padding-right: 16px;
  }
  .bt-item {
    flex-direction: column;
    align-items: stretch;
  }
  .bt-actions {
    justify-content: space-between;
  }
}

/* ---------- backtest loader ---------- */

.loader-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  text-align: center;
}

.loader-box p {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.chart {
  flex: 1 1 auto;
  min-height: 220px;
  position: relative;
}

body.is-resizing,
body.is-resizing * {
  cursor: row-resize !important;
  user-select: none;
}

/* the chart iframe would swallow pointer events while dragging */
body.is-resizing .chart iframe {
  pointer-events: none;
}

.resizer {
  flex: 0 0 4px;
  cursor: row-resize;
  background: var(--bg);
  border-top: 1px solid var(--border);
  transition: background-color 0.15s;
}

.resizer:hover,
body.is-resizing .resizer {
  background: var(--accent);
}

/* ---------- shared controls ---------- */

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s;
}

.icon-btn:hover {
  background: var(--hover);
  color: var(--text-strong);
}

.icon-btn.is-active {
  color: var(--accent);
}

.icon-btn.sm {
  width: 26px;
  height: 26px;
  color: var(--muted);
}

.icon-btn.sm:hover {
  color: var(--text-strong);
}

.icon-btn:focus-visible,
.btn:focus-visible,
.select:focus-visible,
.term-tab:focus-visible,
.cb-trade:focus-visible,
.stepper-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.select,
.input {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  outline: none;
  transition: border-color 0.12s;
}

.select:hover,
.input:hover {
  border-color: #50535e;
}

.input:focus,
.select:focus {
  border-color: var(--accent);
}

.input:disabled,
.select:disabled {
  opacity: 0.6;
}

.select {
  padding-right: 6px;
  cursor: pointer;
}

.select option {
  background: var(--surface);
}

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 12px;
}

.link-btn:hover {
  text-decoration: underline;
}

.btn {
  height: 36px;
  padding: 0 18px;
  border: 0;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  transition: background-color 0.12s, transform 0.08s;
}

.btn:active {
  transform: scale(0.98);
}

.btn--buy,
.btn--primary { background: var(--buy); }
.btn--buy:hover,
.btn--primary:hover { background: var(--accent-hover); }
.btn--sell { background: var(--sell); }
.btn--sell:hover { background: #f7525f; }
.btn--ghost {
  background: var(--surface-2);
  color: var(--text);
}
.btn--ghost:hover { background: #363a45; }
.btn--wide { flex: 1; }

/* ---------- control bar ---------- */

.controls {
  flex: 0 0 48px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  border-top: 1px solid var(--border);
}

.cb-left {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  white-space: nowrap;
}

.cb-name {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 10px;
  margin-right: 4px;
  border-right: 1px solid var(--border);
  color: var(--muted);
  font-weight: 500;
}

.cb-time {
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.cb-tz {
  color: var(--muted);
  font-size: 11px;
}

.cb-spread {
  margin-left: 10px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  cursor: help;
}

.cb-spread b {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.cb-save {
  margin-left: 10px;
  color: var(--muted);
  font-size: 11px;
}

.cb-save[data-kind="ok"]::before {
  content: "✓ ";
  color: var(--up);
}

.cb-save[data-kind="error"] {
  color: var(--down);
}

.cb-center {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cb-trade {
  height: 32px;
  min-width: 76px;
  padding: 0 16px;
  border: 0;
  border-radius: 4px;
  color: #fff;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: background-color 0.12s, transform 0.08s;
}

.cb-trade {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.cb-trade-price {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
  opacity: 0.92;
}

.cb-trade:active { transform: scale(0.96); }
.cb-sell { background: var(--sell); }
.cb-sell:hover { background: #f7525f; }
.cb-buy { background: var(--buy); }
.cb-buy:hover { background: var(--accent-hover); }

.stepper {
  display: flex;
  align-items: stretch;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  transition: border-color 0.12s;
}

.stepper:focus-within {
  border-color: var(--accent);
}

.stepper-btn {
  width: 28px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stepper-btn:hover {
  background: var(--hover);
  color: var(--text-strong);
}

.stepper-input {
  width: 64px;
  border: 0;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: transparent;
  text-align: center;
  font-variant-numeric: tabular-nums;
  outline: none;
}

.cb-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}

.cb-speed {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: 8px;
}

.cb-speed-value {
  min-width: 34px;
  text-align: right;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.cb-speed input[type="range"] {
  width: 110px;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
}

.cb-step-size {
  margin-left: 4px;
}

.controls.is-ended .cb-play,
.controls.is-ended .cb-step {
  opacity: 0.4;
  pointer-events: none;
}

/* ---------- terminal ---------- */

.terminal {
  flex: 0 0 var(--terminal-h);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

.terminal.is-collapsed {
  flex-basis: auto;
}

.terminal.is-collapsed .term-body {
  display: none;
}

.term-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 8px 0 4px;
  border-bottom: 1px solid var(--border);
}

.term-tabs {
  display: flex;
  align-self: stretch;
}

.term-tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border: 0;
  background: none;
  color: var(--muted);
  font-weight: 500;
  cursor: pointer;
  transition: color 0.12s;
}

.term-tab:hover {
  color: var(--text-strong);
}

.term-tab.is-active {
  color: var(--text-strong);
}

.term-tab.is-active::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: -1px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
}

.term-badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 11px;
  line-height: 18px;
  text-align: center;
}

.term-summary {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px 18px;
  flex: 1;
  margin: 0;
  font-size: 12px;
}

.term-summary div {
  display: flex;
  gap: 5px;
  white-space: nowrap;
}

.term-summary dt {
  color: var(--muted);
}

.term-summary dd {
  margin: 0;
  color: var(--text-strong);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.term-summary dd.is-up { color: var(--up); }
.term-summary dd.is-down { color: var(--down); }

.term-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.term-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.term-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 7px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-weight: 500;
  text-align: left;
  white-space: nowrap;
}

.term-table td {
  padding: 5px 10px;
  border-bottom: 1px solid rgba(42, 46, 57, 0.6);
  white-space: nowrap;
}

.term-table .num {
  text-align: right;
}

.term-table .actions {
  width: 1%;
  text-align: right;
  padding-right: 8px;
}

.term-table tbody tr[data-id] {
  cursor: default;
}

.term-table tbody tr:not(.empty):hover td {
  background: var(--surface);
}

.term-table tr.empty td {
  padding: 28px;
  text-align: center;
  color: var(--muted);
  border-bottom: 0;
}

.term-table tfoot td {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 0;
  padding: 7px 10px;
}

.term-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 22px;
  color: var(--muted);
}

.term-stats b {
  color: var(--text-strong);
  font-weight: 600;
}

.term-stats b.is-up { color: var(--up); }
.term-stats b.is-down { color: var(--down); }

.side {
  font-weight: 600;
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.03em;
}

.side--buy { color: var(--buy); }
.side--sell { color: var(--sell); }

/* ---------- dialog ---------- */

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

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

.dlg {
  width: 380px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  animation: dlg-in 0.14s ease-out;
}

@keyframes dlg-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
}

.dlg-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 8px 18px;
}

.dlg-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-strong);
}

.dlg-quote {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin: 0 18px;
  padding: 10px 14px;
  border-radius: 6px;
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}

.dlg-quote > div:last-child {
  text-align: right;
}

.dlg-quote span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.dlg-quote b {
  font-size: 18px;
}

.dlg-symbol {
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.05em;
}

.dlg-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 18px 4px;
}

.fld {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.fld[hidden] {
  display: none;
}

.fld > span {
  color: var(--muted);
  font-size: 12px;
}

.fld-optional {
  margin-left: 4px;
  font-style: normal;
  font-size: 11px;
  opacity: 0.7;
}

.fld-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.dlg-error {
  min-height: 18px;
  margin: 0;
  color: var(--down);
  font-size: 12px;
}

.dlg-foot {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 10px;
  padding: 8px 18px 18px;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--terminal-h) + 64px);
  z-index: 60;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-strong);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 6px);
  transition: opacity 0.15s, transform 0.15s;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast[data-kind="buy"] { border-left-color: var(--buy); }
.toast[data-kind="sell"],
.toast[data-kind="error"] { border-left-color: var(--sell); }

/* ---------- small screens ---------- */

@media (max-width: 1100px) {
  .term-summary div:nth-child(5) { display: none; }
}

@media (max-width: 860px) {
  .controls {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    padding: 6px 12px;
    flex-basis: auto;
  }
  .cb-left { grid-column: 1; }
  .cb-right { grid-column: 2; grid-row: 1; }
  .cb-center { grid-column: 1 / -1; justify-content: center; }
  .cb-speed input[type="range"] { width: 70px; }
  .term-summary { display: none; }
}
