:root {
  --bg: #f3efe7;
  --bg-top-left: rgba(15, 118, 110, 0.10);
  --bg-right: rgba(180, 83, 9, 0.08);
  --bg-grad-top: #f7f4ed;
  --bg-grad-bottom: #ece7de;
  --panel: rgba(255, 255, 255, 0.78);
  --panel-strong: #ffffff;
  --ink: #18222c;
  --muted: #5d6a75;
  --line: rgba(24, 34, 44, 0.12);
  --accent: #0f766e;
  --warn: #b45309;
  --danger: #b91c1c;
  --good: #166534;
  --shadow: 0 18px 50px rgba(24, 34, 44, 0.10);
  --radius: 24px;
  --status-bg: #111827;
  --status-ink: #d1fae5;
  --chart-bg-top: rgba(15, 118, 110, 0.10);
  --chart-bg-bottom: rgba(255, 255, 255, 0.65);
}

body[data-theme="dark"] {
  --bg: #10161b;
  --bg-top-left: rgba(15, 118, 110, 0.18);
  --bg-right: rgba(180, 83, 9, 0.10);
  --bg-grad-top: #121a20;
  --bg-grad-bottom: #0b1116;
  --panel: rgba(19, 28, 35, 0.88);
  --panel-strong: #172028;
  --ink: #eef4f8;
  --muted: #98a6b3;
  --line: rgba(220, 234, 244, 0.10);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  --status-bg: #061017;
  --status-ink: #bbf7d0;
  --chart-bg-top: rgba(15, 118, 110, 0.18);
  --chart-bg-bottom: rgba(10, 16, 22, 0.72);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", "IBM Plex Sans", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, var(--bg-top-left), transparent 28%),
    radial-gradient(circle at right, var(--bg-right), transparent 20%),
    linear-gradient(180deg, var(--bg-grad-top) 0%, var(--bg-grad-bottom) 100%);
  /* prevent horizontal overflow */
  overflow-x: hidden;
}

.shell { min-height: 100vh; }
.main { padding: 30px; max-width: 100%; overflow-x: hidden; }

.brand h1, .hero h2, .card-head h3 {
  font-family: Georgia, "Times New Roman", serif;
  letter-spacing: -0.02em;
  margin: 0;
}
.eyebrow, .panel-title, .form-label, .section-label {
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-size: 11px;
  color: var(--muted);
}
.section-label {
  display: block;
  margin: 24px 0 12px;
}
.muted { color: var(--muted); }

.card, .inner-card {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.65);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
}
.inner-card {
  background: color-mix(in srgb, var(--panel-strong) 60%, transparent);
  box-shadow: none;
}
.clickable-card { cursor: pointer; }
.clickable-card:hover {
  border-color: rgba(15, 118, 110, 0.28);
  transform: translateY(-1px);
}

/* ── Portfolio ── */
.portfolio-banner { margin-bottom: 20px; }
.portfolio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 14px;
}

/* ── Bot grid (horizontal cards) ── */
.bot-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.bot-card {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.65);
  border-radius: 20px;
  box-shadow: var(--shadow);
  cursor: grab;
  user-select: none;
  will-change: transform;
  transition: transform 0.22s cubic-bezier(0.22, 1, 0.36, 1), border-color 0.16s ease, box-shadow 0.22s ease, opacity 0.16s ease;
  overflow: hidden;
}
.bot-card:hover { transform: translateY(-2px); border-color: rgba(15, 118, 110, 0.28); }
.bot-card.expanded { border-color: rgba(15, 118, 110, 0.50); }
.bot-card.dragging {
  cursor: grabbing;
  opacity: 0.55;
  box-shadow: 0 22px 46px rgba(24, 34, 44, 0.18);
}
.bot-card.drag-over {
  border-color: rgba(15, 118, 110, 0.65);
  box-shadow: 0 16px 34px rgba(15, 118, 110, 0.14);
}

.bot-card-top {
  padding: 14px 16px 10px;
}
.bot-card-name {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bot-card-symbol {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}
.bot-card-price {
  font-size: 12px;
  color: var(--muted);
  margin-top: 8px;
  font-family: Consolas, "SFMono-Regular", monospace;
  letter-spacing: 0.02em;
}
.bot-health-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.health-chip {
  padding: 10px 16px;
  border-radius: 16px;
  border: 1px solid var(--line);
  min-width: 90px;
}
.health-ok { background: rgba(22, 101, 52, 0.08); }
.health-bad { background: rgba(185, 28, 28, 0.10); border-color: rgba(185, 28, 28, 0.25); }
.health-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.health-value { font-size: 18px; font-weight: 700; margin-top: 4px; }
.health-ok .health-value { color: var(--good); }
.health-bad .health-value { color: var(--danger); }

.bot-card-stale { font-size: 10px; margin-left: 6px; }
.bot-card-stale.warn { color: var(--warn); }
.bot-card-stale.dead { color: var(--danger); font-weight: 700; }
.bot-card-stats {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 8px;
}
.stat-ok {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(22, 101, 52, 0.10);
  color: var(--good);
}
.stat-bad {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(185, 28, 28, 0.12);
  color: var(--danger);
  font-weight: 700;
}
.bot-card-pnl {
  font-size: 22px;
  font-weight: 700;
  margin-top: 10px;
  line-height: 1.1;
}
.bot-card-pnl.pos { color: var(--good); }
.bot-card-pnl.neg { color: var(--danger); }
.bot-card-pnl.neutral { color: var(--ink); }

.bot-card-footer {
  padding: 8px 16px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--line);
  margin-top: 10px;
}
.bot-card-expand {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
}

/* ── Detail panel ── */
.bot-detail { margin-bottom: 32px; }
.bot-detail > .card { padding: 28px; }

.overview-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 20px; }
.content-grid { display: grid; gap: 20px; }
.content-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.content-grid.single { grid-template-columns: 1fr; }
.content-grid.thirds { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.hero { display: flex; justify-content: space-between; gap: 24px; align-items: center; margin-bottom: 22px; }
.hero-actions { display: flex; gap: 10px; }

.metric, .summary-chip {
  padding: 18px;
  border-radius: 20px;
  background: var(--panel-strong);
  border: 1px solid var(--line);
}
.metric-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.14em; color: var(--muted); }
.metric-value { font-size: 22px; font-weight: 700; margin-top: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.summary-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin: 18px 0; }
.summary-chip .value { font-size: 20px; font-weight: 700; word-break: break-word; }
#alerts-list, #run-metadata { display: grid; gap: 14px; }
.status-line, .log-output {
  font-family: Consolas, "SFMono-Regular", monospace;
  white-space: pre-wrap;
  background: var(--status-bg);
  color: var(--status-ink);
  border-radius: 18px;
  padding: 16px;
  min-height: 86px;
  overflow: auto;
}
.log-output { max-height: 420px; overflow-x: auto; word-break: break-all; }

/* Alerts modal */
.alerts-modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 999;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.alerts-modal {
  background: var(--card-bg); border-radius: 18px; padding: 24px;
  max-width: 540px; width: 100%; max-height: 80vh; overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0,0,0,0.35);
}
.alerts-modal h3 { margin: 0 0 16px; }
.alerts-modal .alert-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.alerts-modal .alert-row:last-child { border-bottom: none; }
.alerts-modal .alert-bot { font-size: 11px; color: var(--muted-ink); margin-bottom: 2px; }
.alerts-modal .alert-msg { font-size: 14px; }
.alerts-modal .alert-sev { font-size: 11px; font-weight: 600; text-transform: uppercase; }
.alerts-modal .alert-sev.warn { color: #f59e0b; }
.alerts-modal .alert-sev.error { color: #ef4444; }
.alerts-modal .modal-close { float: right; cursor: pointer; font-size: 20px; line-height: 1; background: none; border: none; color: var(--muted-ink); }
.metric.clickable { cursor: pointer; }
.metric.clickable:hover .metric-value { text-decoration: underline; }

.card-head { display: flex; justify-content: space-between; gap: 16px; align-items: center; margin-bottom: 18px; }

.pill {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}
.pill.active { background: rgba(22, 101, 52, 0.14); color: var(--good); }
.pill.failed { background: rgba(185, 28, 28, 0.12); color: var(--danger); }
.pill.neutral { background: rgba(24, 34, 44, 0.10); color: var(--muted); }

.actions { display: flex; flex-wrap: wrap; gap: 10px; }
.btn {
  border: 0;
  border-radius: 14px;
  padding: 10px 16px;
  font-weight: 700;
  cursor: pointer;
  font-size: 13px;
}
.btn-primary { background: var(--accent); color: white; }
.btn-ghost { background: rgba(24, 34, 44, 0.08); color: var(--ink); }
.btn-warn { background: rgba(180, 83, 9, 0.14); color: var(--warn); }
.btn-danger { background: rgba(185, 28, 28, 0.12); color: var(--danger); }
.btn-good { background: rgba(22, 101, 52, 0.14); color: var(--good); }

.stack-form { display: grid; gap: 10px; }
.stack-form input, .stack-form textarea {
  width: 100%;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel-strong) 95%, transparent);
  color: var(--ink);
  padding: 12px 14px;
  font: inherit;
}
.stack-form input::placeholder,
.stack-form textarea::placeholder { color: var(--muted); }
.stack-form textarea { min-height: 90px; resize: vertical; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; }
.col-tp { color: var(--good); }
.col-sl { color: var(--danger); }
.table-scroll { overflow: auto; }

#equity-chart {
  width: 100%;
  height: 280px;
  border-radius: 18px;
  background: linear-gradient(180deg, var(--chart-bg-top), var(--chart-bg-bottom));
  border: 1px solid var(--line);
}

.back-link { color: var(--ink); text-decoration: none; font-weight: 700; }

.telemetry-kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}
.mono { font-family: Consolas, "SFMono-Regular", monospace; }

/* ── Dark mode overrides ── */
body[data-theme="dark"] .btn-ghost { background: rgba(220, 234, 244, 0.08); color: var(--ink); }
body[data-theme="dark"] .btn-primary { background: #14b8a6; color: #041216; }
body[data-theme="dark"] .btn-warn { background: rgba(245, 158, 11, 0.18); color: #fcd34d; }
body[data-theme="dark"] .btn-danger { background: rgba(239, 68, 68, 0.18); color: #fca5a5; }
body[data-theme="dark"] .btn-good { background: rgba(34, 197, 94, 0.18); color: #86efac; }
body[data-theme="dark"] .stack-form input,
body[data-theme="dark"] .stack-form textarea {
  background: #0f171d;
  color: #eef4f8;
  border-color: rgba(220, 234, 244, 0.14);
}

/* ── Responsive ── */
@media (max-width: 1180px) {
  .overview-grid, .content-grid, .summary-grid, .content-grid.thirds, .telemetry-kpis {
    grid-template-columns: 1fr;
  }
  .portfolio-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 600px) {
  .main { padding: 16px; }
  .portfolio-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .bot-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
