/* === Theme === */
:root {
  --bg: #0a0e15;
  --bg-2: #0e131c;
  --card: #131826;
  --card-hover: #182033;
  --border: #1f2638;
  --border-2: #2a3349;
  --text: #e6e9ef;
  --text-dim: #9ba3b4;
  --text-muted: #6b7589;
  --accent: #4c8eff;
  --accent-2: #7aa9ff;
  --good: #22c55e;
  --good-bg: rgba(34, 197, 94, 0.15);
  --bad: #ef4444;
  --bad-bg: rgba(239, 68, 68, 0.15);
  --warn: #f59e0b;
  --warn-bg: rgba(245, 158, 11, 0.15);
  --neutral-bg: rgba(155, 163, 180, 0.1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "sidebar topbar" "sidebar main";
  min-height: 100vh;
}

.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.r { text-align: right; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3a4360; }

/* === Sidebar === */
#sidebar {
  grid-area: sidebar;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent) 0%, #2563eb 100%);
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 20px;
  box-shadow: 0 4px 12px rgba(76, 142, 255, 0.3);
}
.brand-name { font-weight: 600; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: 8px;
  color: var(--text-dim); cursor: pointer;
  transition: all 0.15s;
  font-size: 14px;
  user-select: none;
}
.nav-item:hover { background: var(--card); color: var(--text); }
.nav-item.active { background: var(--card-hover); color: var(--text); }
.nav-item .ic { font-size: 14px; width: 20px; text-align: center; opacity: 0.85; }

.sidebar-foot { display: flex; flex-direction: column; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.status-pill, .env-pill {
  font-size: 11px; padding: 4px 10px; border-radius: 100px;
  display: inline-flex; align-items: center; gap: 6px;
  width: fit-content;
}
.status-pill.open { background: var(--good-bg); color: var(--good); }
.status-pill.closed { background: var(--warn-bg); color: var(--warn); }
.status-pill::before { content: "●"; font-size: 8px; }
.env-pill.paper { background: var(--neutral-bg); color: var(--text-dim); }
.env-pill.live { background: var(--bad-bg); color: var(--bad); }
.env-pill.dryrun { background: var(--warn-bg); color: var(--warn); }
.last-update { font-size: 10px; color: var(--text-muted); }

/* === Topbar === */
#topbar {
  grid-area: topbar;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: blur(10px);
}
.title { font-size: 18px; font-weight: 600; }
.menu-btn { display: none; }
.refresh-btn, .menu-btn {
  background: var(--card); border: 1px solid var(--border);
  color: var(--text); cursor: pointer;
  width: 36px; height: 36px; border-radius: 8px;
  font-size: 16px;
  transition: all 0.15s;
}
.refresh-btn:hover, .menu-btn:hover { background: var(--card-hover); border-color: var(--border-2); }

/* === Main === */
#main {
  grid-area: main;
  padding: 24px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}
.tab { display: flex; flex-direction: column; gap: 16px; }

/* === Cards === */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  transition: border-color 0.2s;
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; flex-wrap: wrap; gap: 8px; }
.card-title { font-weight: 600; font-size: 15px; }
.card-sub { color: var(--text-muted); font-size: 12px; display: flex; gap: 8px; align-items: center; }

/* === KPIs === */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { display: flex; flex-direction: column; gap: 6px; }
.kpi-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.kpi-value { font-size: 22px; font-weight: 600; line-height: 1.2; }
.kpi-sub { font-size: 12px; color: var(--text-dim); }
.kpi-sub.good { color: var(--good); }
.kpi-sub.bad { color: var(--bad); }

.metrics-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.metric { padding: 12px 14px; }
.metric-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.metric-value { font-size: 18px; font-weight: 600; margin-top: 4px; }
.metric-value.good { color: var(--good); }
.metric-value.bad { color: var(--bad); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.chart-wrap { height: 240px; }
.candle-chart { height: 380px; }
.rsi-chart { height: 100px; margin-top: 8px; }

/* === Heatmap === */
.heatmap { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 6px; min-height: 200px; }
.heat-tile {
  border-radius: 8px;
  padding: 14px 10px;
  display: flex; flex-direction: column; gap: 4px;
  cursor: default;
  transition: transform 0.15s;
  border: 1px solid var(--border);
}
.heat-tile:hover { transform: translateY(-1px); }
.heat-tile .sym { font-weight: 600; font-size: 14px; }
.heat-tile .val { font-size: 12px; opacity: 0.85; }
.heat-tile.empty { display: flex; align-items: center; justify-content: center; color: var(--text-muted); grid-column: 1 / -1; padding: 30px; }

/* === Tables === */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th { text-align: left; padding: 8px 10px; color: var(--text-muted); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid var(--border); }
.data-table th.r { text-align: right; }
.data-table td { padding: 10px; border-bottom: 1px solid var(--border); }
.data-table td.r { text-align: right; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover { background: rgba(255,255,255,0.02); }
.data-table .empty { text-align: center; color: var(--text-muted); padding: 24px; }
.data-table .sym { font-weight: 600; }
.data-table .good { color: var(--good); }
.data-table .bad { color: var(--bad); }
.data-table .badge { padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 500; text-transform: uppercase; }
.data-table .badge.buy { background: var(--good-bg); color: var(--good); }
.data-table .badge.sell { background: var(--bad-bg); color: var(--bad); }
.data-table .badge.filled { background: var(--good-bg); color: var(--good); }
.data-table .badge.canceled { background: var(--neutral-bg); color: var(--text-dim); }
.data-table .badge.partial { background: var(--warn-bg); color: var(--warn); }
.data-table .badge.new { background: var(--neutral-bg); color: var(--accent-2); }

/* === Form === */
.select {
  background: var(--card-hover); border: 1px solid var(--border);
  color: var(--text); padding: 6px 10px; border-radius: 6px;
  font-size: 13px; cursor: pointer;
  font-family: inherit;
}
.select:focus { outline: none; border-color: var(--accent); }

/* === Decisions === */
.decisions-list { display: flex; flex-direction: column; gap: 8px; }
.decision-card {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 14px;
  cursor: pointer;
  transition: all 0.15s;
}
.decision-card:hover { background: var(--card-hover); border-color: var(--border-2); }
.decision-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.decision-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.decision-time { font-size: 11px; color: var(--text-muted); }
.decision-conf { font-size: 11px; color: var(--text-dim); padding: 2px 6px; background: var(--neutral-bg); border-radius: 4px; }
.decision-reasoning { font-size: 12px; color: var(--text-dim); margin-top: 6px; line-height: 1.5; }
.action-pill { padding: 3px 10px; border-radius: 100px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.action-pill.buy { background: var(--good-bg); color: var(--good); }
.action-pill.sell { background: var(--bad-bg); color: var(--bad); }
.action-pill.hold { background: var(--neutral-bg); color: var(--text-dim); }

/* === Logs === */
.logs-list { display: flex; flex-direction: column; gap: 4px; font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 12px; max-height: 600px; overflow-y: auto; }
.log-row { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border-radius: 4px; }
.log-row:hover { background: var(--card-hover); }
.log-time { color: var(--text-muted); flex-shrink: 0; width: 80px; }
.log-tag { padding: 2px 6px; border-radius: 3px; font-size: 10px; text-transform: uppercase; flex-shrink: 0; }
.log-tag.decision { background: var(--neutral-bg); color: var(--accent-2); }
.log-tag.error { background: var(--bad-bg); color: var(--bad); }
.log-tag.market_closed { background: var(--warn-bg); color: var(--warn); }
.log-tag.circuit_breaker { background: var(--bad-bg); color: var(--bad); }
.log-tag.log { background: var(--neutral-bg); color: var(--text-dim); }
.log-sym { color: var(--accent); font-weight: 500; }
.log-msg { color: var(--text-dim); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { text-align: center; color: var(--text-muted); padding: 32px; font-size: 13px; }

/* === Drawer (detail overlay) === */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex; justify-content: flex-end;
}
.drawer {
  background: var(--card);
  width: min(640px, 100%);
  height: 100%;
  padding: 24px;
  overflow-y: auto;
  border-left: 1px solid var(--border);
  position: relative;
  animation: slide 0.2s ease;
}
@keyframes slide { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.drawer-close {
  position: absolute; top: 16px; right: 16px;
  background: var(--card-hover); border: 1px solid var(--border);
  color: var(--text); cursor: pointer;
  width: 32px; height: 32px; border-radius: 6px;
  font-size: 14px;
}
.drawer h3 { font-size: 16px; margin-bottom: 12px; }
.drawer .kv { display: grid; grid-template-columns: 140px 1fr; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.drawer .kv .k { color: var(--text-muted); }
.drawer .kv .v { color: var(--text); word-break: break-word; }
.drawer pre {
  background: var(--bg); padding: 12px; border-radius: 6px;
  font-size: 11px; overflow-x: auto; font-family: "SF Mono", Menlo, monospace;
  border: 1px solid var(--border);
  white-space: pre-wrap;
}

/* === Mobile === */
@media (max-width: 900px) {
  body { grid-template-columns: 1fr; grid-template-areas: "topbar" "main"; }
  #sidebar {
    position: fixed; left: -260px; top: 0; bottom: 0;
    width: 240px; z-index: 50;
    transition: left 0.25s;
  }
  #sidebar.open { left: 0; box-shadow: 0 0 40px rgba(0,0,0,0.5); }
  .menu-btn { display: flex; align-items: center; justify-content: center; }
  #main { padding: 16px; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .metrics-grid { grid-template-columns: repeat(3, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .kpi-value { font-size: 18px; }
  .candle-chart { height: 280px; }
}
@media (max-width: 480px) {
  .metrics-grid { grid-template-columns: repeat(2, 1fr); }
  .data-table { font-size: 12px; }
  .data-table td, .data-table th { padding: 6px 8px; }
}
