:root {
  --bg: #0b0e14;
  --surface: #131720;
  --surface-hover: #171c27;
  --border: #232838;
  --border-strong: #2e3548;

  --text-primary: #e6e8ec;
  --text-secondary: #8b92a5;
  --text-muted: #5b6272;

  --accent: #5b8def;
  --accent-dim: rgba(91, 141, 239, 0.14);

  --buy: #2dd4a7;
  --buy-dim: rgba(45, 212, 167, 0.14);
  --sell: #ff6b6b;
  --sell-dim: rgba(255, 107, 107, 0.14);
  --hold: #f5a623;
  --hold-dim: rgba(245, 166, 35, 0.14);

  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'SFMono-Regular', Consolas, monospace;

  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
}

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

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 20px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--border);
}

.topbar-left h1 {
  margin: 0;
  font-size: clamp(20px, 3vw, 26px);
  font-weight: 600;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 13px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.last-updated {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-muted);
}

.btn {
  min-height: 44px;
  padding: 0 20px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
}

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

.btn-primary {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent);
  color: var(--bg);
}

.btn-small {
  min-height: 36px;
  padding: 0 14px;
  font-size: 13px;
  width: 100%;
}

.btn[disabled] {
  opacity: 0.5;
  cursor: default;
  pointer-events: none;
}

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

/* ---------- Forms (login/profile) ---------- */

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-field label {
  font-size: 13px;
  color: var(--text-secondary);
}

.form-field input {
  background: var(--bg);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 12px;
  min-height: 40px;
  font-family: var(--font-sans);
  font-size: 14px;
}

.form-error {
  color: var(--sell);
  font-size: 13px;
  min-height: 18px;
}

.form-success {
  color: var(--buy);
  font-size: 13px;
  min-height: 18px;
}

.form-switch {
  text-align: center;
  font-size: 13px;
  color: var(--text-secondary);
}

.form-switch a {
  color: var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.data-caveat {
  background: var(--hold-dim);
  color: var(--hold);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* ---------- Stats bar ---------- */

.stats-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 28px;
  padding: 18px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--border);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 70px;
}

.stat-value {
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-value-small {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

/* ---------- Grid ---------- */

main {
  padding: 24px clamp(16px, 4vw, 40px) 60px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.empty-state {
  color: var(--text-secondary);
  text-align: center;
  margin-top: 60px;
  font-size: 15px;
}

/* ---------- Card ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: transform 0.15s, border-color 0.15s;
  cursor: pointer;
}

.card:hover {
  transform: translateY(-2px);
  border-color: var(--border-strong);
}

.card.action-buy { border-left-color: var(--buy); }
.card.action-sell { border-left-color: var(--sell); }
.card.action-hold { border-left-color: var(--hold); }

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-symbol {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 4px 10px;
  border-radius: 20px;
  text-transform: uppercase;
}

.badge-buy { background: var(--buy-dim); color: var(--buy); }
.badge-sell { background: var(--sell-dim); color: var(--sell); }
.badge-hold { background: var(--hold-dim); color: var(--hold); }

.card-price {
  font-family: var(--font-mono);
  font-size: 24px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.card-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-secondary);
}

.card-row .mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.tp-value { color: var(--buy); }
.sl-value { color: var(--sell); }

.news-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.leverage-pill {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-dim);
  padding: 3px 9px;
  border-radius: 12px;
}

.ml-pill {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border-strong);
  padding: 2px 9px;
  border-radius: 12px;
}

.news-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}

.news-dot.bullish { background: var(--buy); }
.news-dot.bearish { background: var(--sell); }
.news-dot.neutral { background: var(--text-muted); }

.card-reasoning {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: auto;
}

.card-timestamp {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}

/* ---------- Detail overlay ---------- */

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

.detail-overlay[hidden] {
  display: none;
}

.detail-panel {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  max-width: 560px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 28px;
  position: relative;
}

.detail-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  min-width: 36px;
  min-height: 36px;
  border-radius: 8px;
}

.detail-close:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.detail-title {
  font-size: 22px;
  font-weight: 600;
  margin: 0 0 4px;
}

.detail-section {
  margin-top: 20px;
}

.detail-section h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0 0 10px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 20px;
}

.detail-kv {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.detail-kv .v {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.headline-list {
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.7;
}

.reasoning-text {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

/* ---------- Tabs ---------- */

.tabs {
  display: flex;
  gap: 4px;
  padding: 14px clamp(16px, 4vw, 40px) 0;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  padding: 10px 16px;
  min-height: 44px;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ---------- History table ---------- */

.history-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.history-controls label {
  font-size: 13px;
  color: var(--text-secondary);
}

.history-controls select,
.history-controls input {
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 12px;
  min-height: 40px;
  font-family: var(--font-sans);
  font-size: 14px;
}

.history-controls input[type="number"] {
  width: 90px;
}

.history-count {
  color: var(--text-muted);
  font-size: 12px;
  margin-left: auto;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  min-width: 640px;
}

.history-table th {
  text-align: left;
  padding: 12px 14px;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border-strong);
  background: var(--surface);
  position: sticky;
  top: 0;
}

.history-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.history-table tbody tr:hover {
  background: var(--surface-hover);
  cursor: pointer;
}

.outcome-win { color: var(--buy); font-weight: 700; }
.outcome-loss { color: var(--sell); font-weight: 700; }

.return-positive { color: var(--buy); }
.return-negative { color: var(--sell); }

.history-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 20px;
}

.history-pager .btn-small {
  width: auto;
  padding: 0 20px;
}

#history-page-label {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-secondary);
}

/* ---------- Analytics charts ---------- */

.analytics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
}

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chart-card-wide {
  grid-column: 1 / -1;
}

.chart-title {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
}

.chart-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 8px;
}

.chart-wrap {
  position: relative;
  height: 240px;
}

.chart-card-wide .chart-wrap {
  height: 280px;
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- Mobile ---------- */

@media (max-width: 640px) {
  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }
  .topbar-right {
    width: 100%;
    justify-content: space-between;
  }
  .btn-primary {
    flex: 1;
  }
  .grid {
    grid-template-columns: 1fr;
  }
  .detail-grid {
    grid-template-columns: 1fr;
  }
  .analytics-grid {
    grid-template-columns: 1fr;
  }
  .chart-wrap {
    height: 200px;
  }
}
