/* ============================================================
   APEX dashboard — mobile-first, auto light/dark mode
   ============================================================ */

:root {
  --bg: #fafaf7;
  --surface: #ffffff;
  --surface-2: #f4f3ee;
  --border: rgba(0,0,0,0.08);
  --border-strong: rgba(0,0,0,0.15);
  --text: #1a1a19;
  --text-muted: #6a6a64;
  --text-dim: #9a9a94;
  --brand: #534ab7;
  --brand-dark: #3c3489;
  --long: #3b6d11;
  --long-bg: #eaf3de;
  --short: #a32d2d;
  --short-bg: #fceb eb;
  --warn: #854f0b;
  --warn-bg: #faeeda;
  --info: #185fa5;
  --info-bg: #e6f1fb;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,0.04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Monaco, "Cascadia Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a19;
    --surface: #232322;
    --surface-2: #2c2c2a;
    --border: rgba(255,255,255,0.08);
    --border-strong: rgba(255,255,255,0.16);
    --text: #e6e4da;
    --text-muted: #a5a39a;
    --text-dim: #7a7872;
    --brand: #7f77dd;
    --brand-dark: #afa9ec;
    --long: #97c459;
    --long-bg: rgba(151,196,89,0.12);
    --short: #e24b4a;
    --short-bg: rgba(226,75,74,0.12);
    --warn: #ef9f27;
    --warn-bg: rgba(239,159,39,0.12);
    --info: #378add;
    --info-bg: rgba(55,138,221,0.12);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: var(--mono); font-size: 0.9em; }
h1, h2, h3 { margin: 0 0 0.5em; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }
.muted { color: var(--text-muted); font-size: 0.92em; }

/* ------------------ Nav ------------------ */
.topnav {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  flex-wrap: wrap;
}
.brand {
  font-weight: 700;
  font-size: 18px;
  color: var(--brand);
  letter-spacing: 0.05em;
}
.navlinks { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; min-width: 0; }
.navlinks a {
  color: var(--text-muted);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}
.navlinks a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.navlinks a.active { color: var(--text); background: var(--surface-2); }
.userchip {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-muted); font-size: 13px;
}
.userchip img { width: 24px; height: 24px; border-radius: 50%; }
.logout { color: var(--text-dim); font-size: 12px; padding-left: 8px; border-left: 1px solid var(--border); }

.container { max-width: 1200px; margin: 0 auto; padding: 20px 16px; }
.page-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 20px; gap: 12px; flex-wrap: wrap;
}

/* ------------------ Forms ------------------ */
input[type=text], input[type=number], input[type=email], input[type=password], select {
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -2px; }
button, .btn {
  padding: 8px 14px;
  background: var(--brand);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.12s;
}
button:hover, .btn:hover { background: var(--brand-dark); text-decoration: none; color: white; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.danger-btn { background: var(--short); }
button.danger-btn:hover { background: #7c1f1f; }

.add-form, .ticker-form { display: flex; gap: 8px; }
.add-form input, .ticker-form input { text-transform: uppercase; }

/* ------------------ Login ------------------ */
.login-wrap {
  min-height: 70vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
  max-width: 420px; width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 40px; margin-bottom: 8px; color: var(--brand); letter-spacing: 0.08em; }
.tg-login { margin: 24px 0; min-height: 48px; }
.warn {
  background: var(--warn-bg); color: var(--warn);
  padding: 12px; border-radius: var(--radius);
  font-size: 13px;
}
.footer-note { font-size: 12px; color: var(--text-muted); margin-top: 20px; line-height: 1.6; }
.footer-note code { background: var(--surface-2); padding: 1px 6px; border-radius: 4px; }

/* ------------------ Grid of cards ------------------ */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.signal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  transition: border-color 0.15s, transform 0.12s;
  box-shadow: var(--shadow);
}
.signal-card:hover { border-color: var(--border-strong); }
.card-head {
  display: flex; justify-content: space-between; align-items: start;
  margin-bottom: 10px;
}
.card-head .ticker { font-weight: 700; font-size: 18px; letter-spacing: 0.02em; }
.card-head .timeframe { color: var(--text-dim); font-size: 11px; margin-left: 6px; }
.card-head .rm {
  background: transparent; color: var(--text-dim); font-size: 20px;
  padding: 0 6px; line-height: 1; border-radius: 4px;
}
.card-head .rm:hover { background: var(--short-bg); color: var(--short); }
.score-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.dir-pill {
  padding: 3px 8px; font-size: 11px; font-weight: 600;
  border-radius: 4px; letter-spacing: 0.04em;
}
.dir-pill.LONG { background: var(--long-bg); color: var(--long); }
.dir-pill.SHORT { background: var(--short-bg); color: var(--short); }
.dir-pill.NEUTRAL { background: var(--surface-2); color: var(--text-muted); }
.score { font-weight: 600; font-size: 18px; }
.meta {
  font-size: 12px; color: var(--text-muted);
  display: flex; gap: 10px; margin-bottom: 10px;
}
.prices {
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  gap: 8px; font-size: 12px;
  border-top: 1px solid var(--border); padding-top: 10px;
}
.prices .lbl { display: block; color: var(--text-dim); font-size: 10px; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 2px; }
.prices > div span:last-child { font-weight: 500; font-family: var(--mono); }
.card-foot { margin-top: 12px; }
.card-foot .view { font-size: 12px; color: var(--brand); }
.empty { text-align: center; padding: 60px 20px; color: var(--text-muted); }

/* ------------------ Tables ------------------ */
.table-wrap { overflow-x: auto; background: var(--surface); border-radius: var(--radius-lg); border: 1px solid var(--border); }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table th { font-weight: 600; color: var(--text-muted); background: var(--surface-2); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.data-table tr:last-child td { border-bottom: none; }
.data-table .loading { text-align: center; color: var(--text-muted); padding: 30px; }
.r-pos { color: var(--long); font-weight: 500; }
.r-neg { color: var(--short); font-weight: 500; }
.filter-bar { display: flex; gap: 8px; }
.filter-bar select { min-width: 130px; }

/* ------------------ KPIs ------------------ */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 14px 16px;
}
.kpi-label { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.kpi-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 20px;
  margin-bottom: 20px;
}
.panel.danger { border-color: rgba(162,45,45,0.25); }

/* ------------------ Chart page ------------------ */
.chart-layout {
  display: grid; grid-template-columns: 1fr; gap: 16px;
}
@media (min-width: 900px) {
  .chart-layout { grid-template-columns: 1fr 340px; }
}
.chart-main {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 12px; min-height: 500px;
}
#chart-container { width: 100%; height: 500px; }
.chart-hint { font-size: 11px; color: var(--text-dim); margin-top: 8px; }
.chart-side {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 16px;
  font-size: 13px;
}
.sig-head { display: flex; justify-content: space-between; align-items: start; margin-bottom: 14px; }
.sig-score { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.sig-section { margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.sig-section:last-child { border-bottom: none; }
.sig-section h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin-bottom: 8px; }
.contributor {
  padding: 8px 10px; margin-bottom: 6px;
  background: var(--surface-2); border-radius: var(--radius);
  font-size: 12px;
}
.contributor-head { display: flex; justify-content: space-between; margin-bottom: 3px; }
.contributor-name { font-weight: 600; }
.contributor-meta { color: var(--text-muted); font-size: 11px; }
.contributor-reason { color: var(--text-muted); line-height: 1.4; }

/* ------------------ Forms (backtest/settings) ------------------ */
.bt-form, .settings-form {
  display: grid; grid-template-columns: 1fr; gap: 12px;
}
@media (min-width: 600px) {
  .bt-form { grid-template-columns: repeat(2, 1fr) auto; align-items: end; }
  .settings-form { grid-template-columns: repeat(2, 1fr); align-items: end; }
  .settings-form button { grid-column: 1 / -1; justify-self: start; }
}
.bt-form label, .settings-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--text-muted); font-weight: 500;
}
.bt-form input, .bt-form select, .settings-form input { font-size: 14px; }
.summary-pre {
  background: var(--surface-2); padding: 14px;
  border-radius: var(--radius); font-family: var(--mono);
  font-size: 12px; overflow-x: auto; margin: 0;
}
.msg { padding: 10px 12px; border-radius: var(--radius); font-size: 13px; }
.msg.ok { background: var(--long-bg); color: var(--long); }
.msg.err { background: var(--short-bg); color: var(--short); }

.tg-info { display: flex; align-items: center; gap: 12px; }
.tg-info img { width: 48px; height: 48px; border-radius: 50%; }
.tg-name { font-weight: 600; margin-bottom: 2px; }

/* ------------------ Error page ------------------ */
.error-wrap { min-height: 60vh; display: flex; align-items: center; justify-content: center; }
.error-card { text-align: center; padding: 40px; }
.error-card h1 { font-size: 72px; color: var(--text-dim); margin-bottom: 8px; }
