:root {
  --bg: #0a0f1a;
  --bg-elevated: #111827;
  --bg-card: #151e30;
  --bg-hover: #1b273e;
  --border: #23304a;
  --text: #e6edf7;
  --text-muted: #8b9bb4;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --up: #ef4444;
  --up-bg: rgba(239, 68, 68, 0.14);
  --down: #10b981;
  --down-bg: rgba(16, 185, 129, 0.14);
  --warn: #f59e0b;
  --warn-bg: rgba(245, 158, 11, 0.14);
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 13px;
  line-height: 1.45;
}

.tabular { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); font-weight: 400; }
.up { color: var(--up); }
.down { color: var(--down); }
.hidden { display: none !important; }

.app-shell {
  max-width: 1520px;
  margin: 0 auto;
  padding: 16px 20px 30px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Topbar */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: linear-gradient(135deg, #2563eb, #06b6d4);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 14px;
  color: #fff;
}

.brand-title { font-size: 15px; font-weight: 700; }
.brand-sub { font-size: 11px; color: var(--text-muted); }

.badge-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.readonly-badge {
  background: var(--down-bg);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.35);
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.env-badge {
  background: rgba(59, 130, 246, 0.14);
  color: #93c5fd;
  border: 1px solid rgba(59, 130, 246, 0.35);
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.env-badge.real {
  background: rgba(168, 85, 247, 0.16);
  color: #d8b4fe;
  border-color: rgba(168, 85, 247, 0.4);
}

.market-tabs {
  display: inline-flex;
  background: #0b1120;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  gap: 4px;
}

.market-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
  transition: all 0.15s ease;
}

.market-tab.active {
  background: var(--accent);
  color: #fff;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.conn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid var(--border);
}

.conn-pill .dot { width: 8px; height: 8px; border-radius: 50%; }
.conn-pill.opend { background: var(--down-bg); color: #34d399; border-color: rgba(16, 185, 129, 0.35); }
.conn-pill.opend .dot { background: #10b981; }
.conn-pill.demo { background: var(--warn-bg); color: #fbbf24; border-color: rgba(245, 158, 11, 0.35); }
.conn-pill.demo .dot { background: #f59e0b; }
.conn-pill.error { background: var(--up-bg); color: #f87171; border-color: rgba(239, 68, 68, 0.35); }
.conn-pill.error .dot { background: #ef4444; }

.diagnostic-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: #fcd34d;
  border-radius: var(--radius);
  padding: 8px 14px;
  font-size: 12px;
}

/* Buttons */
.btn {
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  color: var(--text);
  background: transparent;
}
.btn-xs { padding: 3px 8px; font-size: 11px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: #1b263b; border-color: var(--border); }
.btn-secondary:hover { background: #23324d; }
.btn-ghost { border-color: var(--border); color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); background: var(--bg-hover); }

/* KPI Strip (5 cards) */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
}

.kpi-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.badge-ccy {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  background: #1b263b;
  color: #93c5fd;
}

.kpi-value {
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.kpi-sub {
  margin-top: 5px;
  font-size: 11px;
  color: var(--text-muted);
}

/* Per-market breakdown cards */
.market-cards-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.m-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.m-card:hover {
  border-color: var(--accent);
  background: var(--bg-card);
}

.m-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.m-card-title {
  font-weight: 700;
  font-size: 13px;
}

.m-card-metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.m-metric-label { font-size: 11px; color: var(--text-muted); }
.m-metric-val { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Charts Row */
.charts-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  gap: 14px;
}

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.panel-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.panel-sub {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.donut-layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  align-items: center;
  gap: 18px;
  min-height: 240px;
}

.donut-canvas-wrap {
  position: relative;
  width: 220px;
  height: 220px;
  margin: 0 auto;
}

#donut-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.donut-center-title { font-size: 11px; color: var(--text-muted); }
.donut-center-val { font-size: 18px; font-weight: 700; margin-top: 2px; }

.donut-legend {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.legend-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  background: #0d1422;
  font-size: 12px;
  transition: background 0.12s ease;
}

.legend-item.active {
  background: #1b2842;
}

.legend-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
}

.legend-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-canvas-wrap {
  width: 100%;
  height: 255px;
}

#pnl-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* Bottom Tables */
.bottom-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.bottom-tab {
  border: 1px solid var(--border);
  background: #0d1422;
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 7px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.bottom-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.sort-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.sort-btn {
  border: 1px solid var(--border);
  background: #0d1422;
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  cursor: pointer;
}

.sort-btn.active {
  border-color: var(--accent);
  color: #93c5fd;
  background: rgba(59, 130, 246, 0.14);
}

.tab-pane { display: none; }
.tab-pane.active { display: block; }

.table-scroll { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.data-table th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 9px 8px;
  border-bottom: 1px solid rgba(35, 48, 74, 0.55);
  white-space: nowrap;
}

.data-table th.num,
.data-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

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

.market-pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
}

.market-pill.HK { background: rgba(59, 130, 246, 0.16); color: #60a5fa; }
.market-pill.US { background: rgba(168, 85, 247, 0.16); color: #c084fc; }
.market-pill.CN { background: rgba(239, 68, 68, 0.16); color: #f87171; }

.sym-code { font-weight: 700; }
.sym-name { font-size: 11px; color: var(--text-muted); margin-left: 6px; }

.weight-cell {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
}

.weight-bar {
  width: 56px;
  height: 6px;
  border-radius: 999px;
  background: #1b263b;
  overflow: hidden;
}

.weight-fill {
  height: 100%;
  background: #3b82f6;
  border-radius: 999px;
}

.status-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.status-filled { background: rgba(16, 185, 129, 0.16); color: #34d399; }
.status-open { background: rgba(59, 130, 246, 0.16); color: #60a5fa; }
.status-cancelled { background: rgba(148, 163, 184, 0.16); color: #94a3b8; }

.empty-cell {
  text-align: center;
  color: var(--text-muted);
  padding: 22px !important;
}

.panel-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid rgba(35, 48, 74, 0.55);
  font-size: 11px;
}

/* Modal */
input, select {
  background: #0c1320;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  padding: 7px 10px;
  font-size: 12px;
  outline: none;
}

input:focus, select:focus { border-color: var(--accent); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 14, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.modal-card {
  width: 100%;
  max-width: 470px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h3 { margin: 0; font-size: 15px; }

.modal-body { display: flex; flex-direction: column; gap: 12px; }
.form-row { display: flex; flex-direction: column; gap: 5px; }
.form-row label { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.field-hint { font-size: 11px; color: var(--text-muted); }

.form-grid-2 {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 10px;
}

.cfg-status-box {
  background: #0c1320;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 12px;
  color: var(--text-muted);
}

.modal-footer { display: flex; justify-content: flex-end; gap: 8px; }

/* Toast */
.toast-container {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
}

.toast {
  background: #172135;
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 8px;
  min-width: 240px;
  max-width: 380px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.45);
  font-size: 12px;
}

.toast.success { border-left-color: #10b981; }
.toast.error { border-left-color: #ef4444; }

@media (max-width: 1180px) {
  .kpi-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 780px) {
  .kpi-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .market-cards-strip { grid-template-columns: 1fr; }
  .donut-layout { grid-template-columns: 1fr; }
}
