:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2330;
  --border: #2a3140;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --green: #2ecc71;
  --red: #e74c3c;
  --amber: #f1c40f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 20px; font-weight: 700; letter-spacing: .3px; }
.tabs { display: flex; gap: 6px; }
.tabs button {
  padding: 8px 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  transition: all .15s;
}
.tabs button:hover { color: var(--text); }
.tabs button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #0d1117;
  font-weight: 600;
}

/* ---------- Layout ---------- */
.container { max-width: 1200px; margin: 24px auto; padding: 0 20px; display: flex; flex-direction: column; gap: 20px; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
}
.card h2 { font-size: 16px; margin-bottom: 12px; color: var(--text); }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.card-head h2 { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }

/* ---------- Filters ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.filters label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.filters input,
.filters select,
.cfg-form input {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.filters input:focus,
.filters select:focus,
.cfg-form input:focus { border-color: var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  padding: 9px 18px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #0d1117;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: filter .15s;
}
.btn:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger.ghost { background: transparent; color: var(--red); }
.btn.active { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Playback / Live source toggle ---------- */
.filter-actions { width: 100%; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* ---------- Stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
}
.stat { text-align: center; padding: 16px 12px; }
.stat-label { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.stat-value { font-size: 22px; font-weight: 700; }
.stat-unit { font-size: 13px; font-weight: 400; color: var(--muted); }

.pos { color: var(--green); }
.neg { color: var(--red); }
.zero { color: var(--muted); }

/* ---------- Live status (ราคาปัจจุบัน + PnL สด) ---------- */
.live-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.live-actions .muted { display: flex; flex-wrap: wrap; gap: 4px; }
.live-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
}
.live-item {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.live-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.live-price { font-size: 26px; font-weight: 700; font-family: Consolas, monospace; }
.live-value { font-size: 20px; font-weight: 700; }
.live-value .side { font-size: 13px; }


/* ---------- Chart ---------- */
.chart-toggle { display: flex; gap: 6px; }
.chart-toggle button {
  padding: 6px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
}
.chart-toggle button.active { background: var(--accent); border-color: var(--accent); color: #0d1117; font-weight: 600; }
.chart-wrap { position: relative; height: 340px; }
.chart-wrap .empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tr:hover td { background: rgba(88, 166, 255, .05); }
td.right, th.right { text-align: right; }
.empty-row { text-align: center; color: var(--muted); padding: 24px; }

/* ---------- Tags & pills ---------- */
.product-tag {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 6px;
  background: rgba(88, 166, 255, .12);
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  font-family: Consolas, monospace;
}
.side {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 12px;
}
.side.long { background: rgba(46, 204, 113, .15); color: var(--green); }
.side.short { background: rgba(231, 76, 60, .15); color: var(--red); }
.pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}
.pill.open { background: rgba(241, 196, 15, .15); color: var(--amber); }
.pill.closed { background: rgba(139, 148, 158, .15); color: var(--muted); }

/* ---------- Reason cell (ellipsis) ---------- */
.reason-cell {
  display: block;
  max-width: 180px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--accent);
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 13px;
  text-align: left;
}
.reason-cell:hover { background: rgba(88, 166, 255, .1); }

/* ---------- Modal (เหตุผลการเทรด) ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, .6);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%; max-width: 640px;
  max-height: 85vh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.reason-section {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; margin-bottom: 12px;
  background: var(--panel-2);
}
.reason-title { font-weight: 700; font-size: 14px; margin-bottom: 8px; color: var(--accent); }
.reason-line { font-size: 13px; line-height: 1.6; margin: 4px 0; word-break: break-word; }

/* ---------- Config ---------- */
.cfg-form { display: flex; flex-wrap: wrap; gap: 12px; }
.cfg-form input { flex: 1; min-width: 140px; }
.msg { margin-top: 12px; font-size: 13px; color: var(--accent); }
.error {
  background: rgba(231, 76, 60, .12);
  border: 1px solid var(--red);
  color: var(--red);
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
}

@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; }
}
