/* ── CSS Variables ── */
:root {
  --bg:         #0d1117;
  --bg-card:    #161b22;
  --bg-input:   #21262d;
  --border:     #30363d;
  --text:       #e6edf3;
  --text-muted: #8b949e;
  --green:      #3fb950;
  --red:        #f85149;
  --blue:       #58a6ff;
  --yellow:     #d29922;

  --radius:     8px;
  --radius-sm:  4px;
  --gap:        16px;
  --font:       -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

/* ── Reset / Base ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

/* ── Utility ── */
[hidden] {
  display: none !important;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  white-space: nowrap;
}

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

.btn:hover:not(:disabled) {
  background: var(--bg-input);
}

.btn-primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: #4493e0;
  border-color: #4493e0;
}

.btn-ghost {
  border-color: transparent;
  color: var(--text-muted);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--border);
}

.btn-danger {
  border-color: var(--red);
  color: var(--red);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(248, 81, 73, 0.15);
}

.btn-sm {
  padding: 3px 9px;
  font-size: 12px;
}

.btn-full {
  width: 100%;
}

/* ── Login Screen ── */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-box {
  width: 100%;
  max-width: 380px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 32px;
}

.login-title {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 4px;
}

.login-subtitle {
  color: var(--text-muted);
  margin-bottom: 24px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text-muted);
}

.form-group input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus {
  border-color: var(--blue);
}

.login-error {
  background: rgba(248, 81, 73, 0.15);
  border: 1px solid var(--red);
  border-radius: var(--radius-sm);
  color: var(--red);
  padding: 8px 12px;
  margin-bottom: 16px;
  font-size: 13px;
}

/* ── Header ── */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* WebSocket status indicator */
.ws-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

.ws-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  transition: background 0.3s;
  flex-shrink: 0;
}

.ws-dot.connected {
  background: var(--green);
  box-shadow: 0 0 6px rgba(63, 185, 80, 0.6);
}

.ws-label {
  /* label text set by JS */
}

/* ── Main Content ── */
.main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

/* ── Summary Row ── */
.summary-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.summary-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.summary-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 500;
}

.summary-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.summary-value.positive { color: var(--green); }
.summary-value.negative { color: var(--red); }

/* ── Section ── */
.section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.section-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.subsection-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 8px;
}

/* ── Agent Cards Grid ── */
.agent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--gap);
}

.agent-placeholder {
  color: var(--text-muted);
  font-size: 14px;
  padding: 20px 0;
}

.agent-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.agent-card:hover {
  border-color: var(--blue);
  box-shadow: 0 0 0 1px var(--blue);
}

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

.agent-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Status Badges ── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}

.badge-running {
  background: rgba(63, 185, 80, 0.15);
  color: var(--green);
  border: 1px solid rgba(63, 185, 80, 0.4);
}

.badge-paused {
  background: rgba(210, 153, 34, 0.15);
  color: var(--yellow);
  border: 1px solid rgba(210, 153, 34, 0.4);
}

.badge-halted {
  background: rgba(248, 81, 73, 0.15);
  color: var(--red);
  border: 1px solid rgba(248, 81, 73, 0.4);
}

.badge-unreachable {
  background: rgba(139, 148, 158, 0.15);
  color: var(--text-muted);
  border: 1px solid rgba(139, 148, 158, 0.4);
}

/* ── Agent Card Stats ── */
.agent-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 16px;
}

.agent-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

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

.agent-stat-value {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.positive { color: var(--green); }
.negative { color: var(--red); }

/* IBKR status pill */
.ibkr-status {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 4px;
}

.ibkr-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.ibkr-dot.ok { background: var(--green); }
.ibkr-dot.err { background: var(--red); }

/* Agent card actions */
.agent-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ── Detail Panel ── */
.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.detail-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
}

.metric-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 120px;
}

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

.metric-value {
  font-size: 17px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ── Tables ── */
.table-wrapper {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.data-table thead {
  background: var(--bg-input);
}

.data-table th {
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

.data-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.data-table tbody tr:hover td {
  background: var(--bg-input);
}

.data-table .empty-row td {
  text-align: center;
  color: var(--text-muted);
  padding: 24px;
}

/* ── Live Trade Feed ── */
.feed-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.feed-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
}

.feed-header:hover {
  background: var(--bg-input);
}

.feed-header .section-title {
  margin: 0;
}

.feed-chevron {
  color: var(--text-muted);
  font-size: 12px;
  transition: transform 0.2s;
}

.feed-chevron.collapsed {
  transform: rotate(180deg);
}

.feed-body {
  max-height: 320px;
  overflow-y: auto;
  transition: max-height 0.25s ease;
}

.feed-body.collapsed {
  max-height: 0;
  overflow: hidden;
}

.feed-list {
  list-style: none;
  padding: 8px 0;
}

.feed-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.feed-item:last-child {
  border-bottom: none;
}

.feed-item-time {
  color: var(--text-muted);
  font-size: 12px;
  min-width: 60px;
  flex-shrink: 0;
}

.feed-item-agent {
  color: var(--blue);
  min-width: 100px;
  font-weight: 500;
  flex-shrink: 0;
}

.feed-item-symbol {
  font-weight: 600;
  min-width: 70px;
  flex-shrink: 0;
}

.feed-item-side {
  min-width: 40px;
  font-weight: 600;
  flex-shrink: 0;
}

.feed-item-side.buy  { color: var(--green); }
.feed-item-side.sell { color: var(--red); }

.feed-item-detail {
  color: var(--text-muted);
}

.feed-empty {
  padding: 16px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

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

/* ── Responsive ── */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
  }

  .main {
    padding: 16px;
    gap: 20px;
  }

  .summary-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .agent-grid {
    grid-template-columns: 1fr;
  }

  .header-title {
    font-size: 14px;
  }

  .ws-label {
    display: none;
  }

  .data-table th,
  .data-table td {
    padding: 8px 10px;
  }

  .detail-metrics {
    gap: 10px 16px;
  }
}

@media (max-width: 480px) {
  .summary-row {
    grid-template-columns: 1fr;
  }

  .login-box {
    padding: 28px 20px;
  }

  .summary-value {
    font-size: 22px;
  }
}
