/* ── Variables ─────────────────────────────────────────────── */
:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --text2: #8b949e;
  --green: #3fb950;
  --red: #f85149;
  --blue: #58a6ff;
  --yellow: #d29922;
  --orange: #db6d28;
  --purple: #b48eff;
  --glow-green: rgba(63, 185, 80, 0.4);
  --glow-red: rgba(248, 81, 73, 0.4);
  --glow-blue: rgba(88, 166, 255, 0.3);
  --glow-purple: rgba(180, 142, 255, 0.3);
  --accent-gradient: linear-gradient(90deg, #58a6ff, #b48eff, #58a6ff);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

/* ── Global transitions ──────────────────────────────────── */
a, button, .badge, .stat, .bottom-stat, .trade-item, .trade-filter, .chart-tab {
  transition: all 0.2s ease;
}

/* ── Header ───────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: none;
  position: relative;
}
.header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--accent-gradient);
  background-size: 200% 100%;
  animation: gradient-shift 3s ease infinite;
}
@keyframes gradient-shift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.header h1 { font-size: 16px; font-weight: 600; white-space: nowrap; }

/* ── Analysis Controls (right panel) ───────────────────── */
.analysis-controls {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--border);
}
.analysis-controls.hidden { display: none; }
.analysis-controls-header {
  padding: 10px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.analysis-controls-body {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bg);
}
.analysis-date-row {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.analysis-date-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.analysis-date-field label {
  font-size: 12px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.analysis-date {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
  color-scheme: dark;
}
.analysis-date:hover {
  border-color: var(--text2);
}
.analysis-date:focus {
  outline: 1px solid var(--blue);
  border-color: var(--blue);
}
.btn-run {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  color: #fff;
  border: none;
  font-family: inherit;
  white-space: nowrap;
  width: 100%;
}
.btn-run:hover:not(:disabled) {
  filter: brightness(1.15);
}
.btn-run:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.btn-run.running {
  background: var(--bg3);
  color: var(--text2);
}
.analysis-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}
.analysis-progress.hidden { display: none; }
.progress-bar {
  flex: 1;
  height: 6px;
  background: var(--bg3);
  border-radius: 3px;
  overflow: hidden;
  min-width: 80px;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--blue), var(--purple));
  border-radius: 3px;
  transition: width 0.3s ease;
}
.progress-text {
  font-size: 11px;
  color: var(--text2);
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.logo-text {
  background: linear-gradient(135deg, var(--blue), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.badge {
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.badge-exchange { background: var(--bg3); color: var(--blue); }
.badge-live {
  background: #1a3a1a;
  color: var(--green);
  box-shadow: 0 0 8px var(--glow-green), inset 0 0 4px rgba(63, 185, 80, 0.15);
}
.badge-offline {
  background: #3a1a1a;
  color: var(--red);
  box-shadow: 0 0 8px var(--glow-red);
}

/* ── Auth area ────────────────────────────────────────────── */
.auth-area {
  display: flex;
  align-items: center;
}
.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}
.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border);
}
.user-name {
  font-size: 12px;
  color: var(--text);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rate-limit-badge {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  background: #1a3a1a;
  color: var(--green);
  white-space: nowrap;
}
.rate-limit-badge.exhausted {
  background: #3a1a1a;
  color: var(--red);
}
.btn-signout {
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  background: var(--bg3);
  color: var(--text2);
  border: 1px solid var(--border);
  font-family: inherit;
}
.btn-signout:hover {
  color: var(--text);
  border-color: var(--text2);
}

/* ── Analysis auth gate ──────────────────────────────────── */
.analysis-auth-gate {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(13, 17, 23, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  border-radius: 0 0 6px 6px;
}
.analysis-auth-gate span {
  font-size: 13px;
  color: var(--text2);
  padding: 12px 16px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* ── Refresh spinner ──────────────────────────────────────── */
.refresh-spinner {
  display: none;
  width: 14px;
  height: 14px;
  border: 2px solid var(--bg3);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.refresh-spinner.active { display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  animation: toast-in 0.3s ease;
  max-width: 360px;
}
.toast.error { border-color: var(--red); background: #2d1214; }
.toast.success { border-color: var(--green); background: #122d14; }
@keyframes toast-in {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}

/* ── Stats Bar ────────────────────────────────────────────── */
.stats-bar {
  display: flex;
  gap: 0;
  padding: 0 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-wrap: wrap;
}
.stat {
  padding: 10px 16px;
  border-right: 1px solid var(--border);
  white-space: nowrap;
  position: relative;
}
.stat:hover {
  background: rgba(88, 166, 255, 0.04);
}
.stat:last-child { border-right: none; }
.stat-label { font-size: 11px; color: var(--text2); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 15px; font-weight: 600; margin-top: 2px; transition: color 0.3s ease; }
.positive { color: var(--green); }
.negative { color: var(--red); }
.neutral { color: var(--yellow); }

/* ── Main Layout ──────────────────────────────────────────── */
.main {
  display: grid;
  grid-template-columns: 1fr 340px;
  height: calc(100vh - 140px);
  min-height: 500px;
}

/* ── Chart Area ───────────────────────────────────────────── */
.chart-area {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}
.chart-tabs {
  display: flex;
  gap: 0;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.chart-tab {
  padding: 8px 20px;
  cursor: pointer;
  color: var(--text2);
  border-bottom: 2px solid transparent;
  font-size: 13px;
  background: none;
  border-top: none;
  border-left: none;
  border-right: none;
  font-family: inherit;
  border-radius: 4px 4px 0 0;
}
.chart-tab:hover {
  color: var(--text);
  background: rgba(88, 166, 255, 0.06);
}
.chart-tab:focus-visible {
  outline: 1px solid var(--blue);
  outline-offset: -1px;
}
.chart-tab.active {
  color: var(--blue);
  border-bottom-color: var(--blue);
  background: rgba(88, 166, 255, 0.08);
}
.chart-container {
  flex: 1;
  position: relative;
}
.chart-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.chart-wrapper.hidden { display: none; }

/* ── Trade Panel ──────────────────────────────────────────── */
.trade-panel {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.trade-panel-header {
  padding: 10px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.trade-filters {
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.trade-filter {
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  background: var(--bg3);
  color: var(--text2);
  border: 1px solid var(--border);
  font-family: inherit;
}
.trade-filter:hover { color: var(--text); background: rgba(88, 166, 255, 0.1); }
.trade-filter.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.trade-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}
.trade-item {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  cursor: default;
  border-left: 4px solid transparent;
}
.trade-item:hover { background: var(--bg2); }

/* ── Trade strategy left border ─────────────────────────── */
.trade-item.border-sq { border-left-color: var(--blue); }
.trade-item.border-mr { border-left-color: var(--purple); }
.trade-item.border-sent { border-left-color: var(--green); }
.trade-item.border-arb { border-left-color: var(--orange); }

/* ── Trade win/loss background ──────────────────────────── */
.trade-item.trade-win { background: rgba(63, 185, 80, 0.04); }
.trade-item.trade-win:hover { background: rgba(63, 185, 80, 0.08); }
.trade-item.trade-loss { background: rgba(248, 81, 73, 0.04); }
.trade-item.trade-loss:hover { background: rgba(248, 81, 73, 0.08); }

.trade-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.trade-id { color: var(--text2); font-size: 12px; }
.trade-strategy {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 500;
}
.strat-sq { background: #1a2a3a; color: var(--blue); box-shadow: 0 0 6px rgba(88, 166, 255, 0.15); }
.strat-mr { background: #2a1a3a; color: var(--purple); box-shadow: 0 0 6px rgba(180, 142, 255, 0.15); }
.strat-sent { background: #1a3a2a; color: var(--green); box-shadow: 0 0 6px rgba(63, 185, 80, 0.15); }
.strat-arb { background: #3a2a1a; color: var(--orange); box-shadow: 0 0 6px rgba(219, 109, 40, 0.15); }
.trade-side { font-size: 12px; font-weight: 500; }
.trade-pnl { font-weight: 600; }
.trade-detail { font-size: 11px; color: var(--text2); margin-top: 3px; }
.trade-signal { font-size: 11px; color: var(--blue); margin-top: 2px; opacity: 0.85; }

/* ── Signal condition detail ─────────────────────────────── */
.signal-detail { margin-top: 3px; }
.signal-section {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  align-items: center;
  margin-top: 2px;
}
.signal-section-label {
  font-size: 10px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-right: 2px;
  flex-shrink: 0;
}
.signal-cond {
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 3px;
  white-space: nowrap;
}
.signal-cond.cond-ok {
  background: rgba(63, 185, 80, 0.12);
  color: var(--green);
}
.signal-cond.cond-no {
  background: rgba(248, 81, 73, 0.12);
  color: var(--red);
}
.cond-icon {
  font-size: 10px;
  font-weight: 700;
}
.cond-icon.cond-ok { color: var(--green); }
.cond-icon.cond-no { color: var(--red); }
.trade-summary {
  padding: 12px 16px;
  background: var(--bg2);
  border-top: 2px solid transparent;
  border-image: var(--accent-gradient) 1;
}
.trade-summary-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  margin-bottom: 2px;
}

/* ── Bottom Bars ──────────────────────────────────────────── */
.bottom-bar {
  display: flex;
  gap: 0;
  padding: 0 20px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
  align-items: center;
}
.section-title {
  padding: 6px 12px 6px 0;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text2);
  border-right: 1px solid var(--border);
  opacity: 0.7;
}
.bottom-stat {
  padding: 8px 16px;
  border-right: 1px solid var(--border);
  font-size: 12px;
  white-space: nowrap;
}
.bottom-stat:hover {
  background: rgba(88, 166, 255, 0.04);
}
.bottom-stat:last-child { border-right: none; }
.bottom-label { color: var(--text2); }

/* ── PnL glow ────────────────────────────────────────────── */
.bottom-stat .positive {
  text-shadow: 0 0 8px var(--glow-green);
}
.bottom-stat .negative {
  text-shadow: 0 0 8px var(--glow-red);
}

/* ── Total PnL highlight ─────────────────────────────────── */
.total-pnl {
  background: rgba(88, 166, 255, 0.06);
  font-weight: 600;
}
.total-pnl .positive {
  text-shadow: 0 0 10px var(--glow-green);
}
.total-pnl .negative {
  text-shadow: 0 0 10px var(--glow-red);
}

/* ── Performance Table ────────────────────────────────────── */
.perf-table-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  padding: 16px;
}
.perf-table-wrapper.hidden { display: none; }
.perf-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.perf-table th {
  text-align: left;
  padding: 8px 12px;
  background: linear-gradient(135deg, var(--bg2), var(--bg3));
  border-bottom: 1px solid var(--border);
  color: var(--text2);
  font-weight: 500;
  text-transform: uppercase;
  font-size: 11px;
  position: sticky;
  top: 0;
}
.perf-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.perf-table tr:hover td { background: rgba(88, 166, 255, 0.06); }
.perf-table td.positive { background: rgba(63, 185, 80, 0.06); }
.perf-table td.negative { background: rgba(248, 81, 73, 0.06); }

/* ── Scrollbar ────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb {
  background: var(--bg3);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--border);
}

/* ── Empty state ──────────────────────────────────────────── */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text2);
  font-size: 14px;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .trade-panel { max-height: 300px; }
}

@media (max-width: 600px) {
  .header {
    flex-direction: column;
    gap: 8px;
    padding: 10px 12px;
  }
  .stats-bar {
    padding: 0 8px;
  }
  .stat {
    padding: 8px 10px;
    min-width: 80px;
  }
  .bottom-bar {
    flex-direction: column;
    padding: 0;
  }
  .section-title {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 4px 16px;
  }
  .bottom-stat {
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 6px 16px;
  }
  .bottom-stat:last-child { border-bottom: none; }
  .main {
    height: auto;
    min-height: auto;
  }
  .chart-area {
    height: 350px;
  }
}
