:root {
  color-scheme: light dark;
  --bg: #0f1419;
  --panel: #1a2230;
  --text: #e7ecf3;
  --muted: #9aa7b5;
  --accent: #4da3ff;
  --border: #2a3545;
  --up: #f85149;
  --down: #3fb950;
  --error: #ff7b72;
  --row-alt: rgba(255, 255, 255, 0.03);
  --active: rgba(77, 163, 255, 0.15);
  --sidebar-w: 220px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --panel: #ffffff;
    --text: #1b2430;
    --muted: #5c6b7a;
    --accent: #0b69ff;
    --border: #e1e6ed;
    --up: #cf222e;
    --down: #1a7f37;
    --error: #cf222e;
    --row-alt: rgba(0, 0, 0, 0.02);
    --active: rgba(11, 105, 255, 0.1);
  }
}

* {
  box-sizing: border-box;
}

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

a {
  color: var(--accent);
  text-decoration: none;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.sidebar-brand {
  font-weight: 700;
  font-size: 0.95rem;
  padding: 0.25rem 0.5rem 0.75rem;
  color: var(--text);
}

.sidebar a {
  display: block;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  color: var(--text);
  font-size: 0.875rem;
}

.sidebar a:hover {
  background: var(--row-alt);
}

.sidebar a.active {
  background: var(--active);
  font-weight: 600;
}

.sidebar a.nav-muted {
  color: var(--muted);
  margin-top: auto;
}

.app-body {
  min-width: 0;
}

.app-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.app-header h1 {
  margin: 0;
  font-size: 1.25rem;
}

.sync-hint {
  margin: 0.35rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.app-main {
  padding: 1rem 1.5rem 2rem;
  max-width: 1200px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  margin-bottom: 1rem;
}

.toolbar label span {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.toolbar select,
.toolbar input[type="text"],
.toolbar input[type="search"] {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.45rem 0.65rem;
  font-size: 0.875rem;
}

.toolbar button,
.btn {
  border: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(77, 163, 255, 0.18), rgba(77, 163, 255, 0.08));
  color: var(--text);
  border-radius: 8px;
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.875rem;
}

.meta {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 1rem;
}

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

@media (max-width: 800px) {
  .app-shell {
    grid-template-columns: 1fr;
  }
  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .sidebar a.nav-muted {
    margin-top: 0;
  }
  .grid-2 {
    grid-template-columns: 1fr;
  }
}

section.panel {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  padding: 0.75rem;
  margin-bottom: 1rem;
}

section.panel h2 {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  table-layout: fixed;
}

table.data th,
table.data td {
  padding: 0.45rem 0.5rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
}

table.data th {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
}

/* Header and body must share alignment for numeric columns */
table.data th.num,
table.data td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Side-by-side flow tables: identical column widths */
table.data.cols-flow-3 col:nth-child(1) {
  width: 42%;
}
table.data.cols-flow-3 col:nth-child(2) {
  width: 33%;
}
table.data.cols-flow-3 col:nth-child(3) {
  width: 25%;
}

table.data.cols-sector-5 col:nth-child(1) {
  width: 22%;
}
table.data.cols-sector-5 col:nth-child(2),
table.data.cols-sector-5 col:nth-child(3),
table.data.cols-sector-5 col:nth-child(4) {
  width: 14%;
}
table.data.cols-sector-5 col:nth-child(5) {
  width: 36%;
}

table.data.cols-funds-8 col:nth-child(1) {
  width: 10%;
}
table.data.cols-funds-8 col:nth-child(2) {
  width: 18%;
}
table.data.cols-funds-8 col:nth-child(3),
table.data.cols-funds-8 col:nth-child(4),
table.data.cols-funds-8 col:nth-child(5),
table.data.cols-funds-8 col:nth-child(6),
table.data.cols-funds-8 col:nth-child(7) {
  width: 11%;
}
table.data.cols-funds-8 col:nth-child(8) {
  width: 16%;
}

table.data tr.clickable {
  cursor: pointer;
}

table.data tr.clickable:hover {
  background: var(--row-alt);
}

table.data tr.active-row {
  background: var(--active);
}

table.data tr.row-live {
  background: color-mix(in srgb, var(--positive) 6%, transparent);
}

.live-badge {
  display: inline-block;
  margin-right: 0.35rem;
  padding: 0.05rem 0.35rem;
  border-radius: 3px;
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--positive);
  background: color-mix(in srgb, var(--positive) 14%, transparent);
}

table.pe-table {
  table-layout: fixed;
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

table.pe-table th,
table.pe-table td {
  padding: 0.45rem 0.5rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table.pe-table th {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

table.pe-table td.name {
  font-weight: 600;
}

table.pe-table td.empty {
  text-align: center;
  color: var(--muted);
}

.up {
  color: var(--up);
}

.down {
  color: var(--down);
}

.banner-error {
  background: rgba(255, 123, 114, 0.12);
  border: 1px solid var(--error);
  color: var(--error);
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.875rem;
}

.muted {
  color: var(--muted);
}

.loading {
  color: var(--muted);
  padding: 2rem;
  text-align: center;
}

.drawer-root {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  justify-content: flex-end;
}

.drawer-root.hidden {
  display: none;
}

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.drawer-panel {
  position: relative;
  width: min(480px, 92vw);
  height: 100%;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.2);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
}

.drawer-header h2 {
  margin: 0;
  font-size: 1rem;
}

.drawer-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.25rem;
}

.drawer-panel.drawer-wide {
  width: min(720px, 96vw);
}

.drawer-body {
  padding: 1rem;
  overflow: auto;
  flex: 1;
}

.drawer-body section.panel {
  margin-bottom: 1rem;
}

.drawer-body dl.fund-dl {
  margin: 0;
  display: grid;
  grid-template-columns: 7rem 1fr;
  gap: 0.35rem 0.75rem;
  font-size: 0.85rem;
}

.drawer-body dl.fund-dl dt {
  color: var(--muted);
  margin: 0;
}

.drawer-body dl.fund-dl dd {
  margin: 0;
}

.drawer-scroll {
  max-height: 220px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.fund-holdings-table-wrap {
  max-height: 320px;
}

.fund-holdings-table code {
  font-size: 0.85em;
}

.fund-holdings-period {
  font-weight: 500;
  color: var(--text);
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  font-size: 0.875rem;
  margin-bottom: 0.75rem;
}

.footnote {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 1.5rem;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
}

.region-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.region-tabs a {
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.85rem;
  color: var(--text);
}

.region-tabs a.active {
  background: var(--active);
  border-color: var(--accent);
  font-weight: 600;
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.chip {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.82rem;
  cursor: pointer;
}

.chip.active {
  border-color: var(--accent);
  background: var(--active);
}

.badge {
  display: inline-block;
  padding: 0.12rem 0.45rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-success {
  background: rgba(63, 185, 80, 0.2);
  color: var(--down);
}

.badge-failed {
  background: rgba(248, 81, 73, 0.2);
  color: var(--up);
}

.badge-running {
  background: rgba(210, 153, 34, 0.25);
  color: #d29922;
}

.badge-skipped,
.badge-never {
  background: var(--row-alt);
  color: var(--muted);
}

.chart-wrap {
  position: relative;
  min-height: 280px;
}

.chart-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  margin-bottom: 0.5rem;
}

.chart-toolbar-ranges {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
}

.chart-toolbar-label {
  margin: 0;
  margin-right: 0.25rem;
}

.chart-toolbar button {
  padding: 0.2rem 0.55rem;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--row-alt);
  color: var(--text);
  cursor: pointer;
}

.chart-toolbar button:hover {
  border-color: var(--accent);
}

.chart-toolbar button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chart-toolbar-meta {
  margin: 0;
}

.market-kline-wrap {
  min-height: 360px;
  height: 360px;
}

.market-kline-chart {
  width: 100%;
  height: 100%;
}

.fund-nav-compare-wrap {
  min-height: 300px;
  height: 300px;
}

.fund-nav-compare-chart {
  width: 100%;
  height: 100%;
}

.fund-return-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
  gap: 0.5rem 1rem;
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.75rem;
  background: var(--row-alt);
  border-radius: 6px;
  border: 1px solid var(--border);
}

.fund-return-stats > div {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.fund-return-stats dt {
  font-size: 0.75rem;
  color: var(--muted);
}

.fund-return-stats dd {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.fund-holdings-sub {
  margin: 1rem 0 0.35rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
}

.fund-holdings-refresh {
  margin-left: 0.35rem;
}

.holdings-search-label input[type="search"] {
  min-width: 14rem;
}

.holdings-popular-block {
  margin-top: 0.75rem;
}

.holdings-popular-block:first-of-type {
  margin-top: 0.35rem;
}

.holdings-popular-chips {
  margin-top: 0.35rem;
}

.holdings-popular-chips .chip {
  margin-bottom: 0.35rem;
}

.view-subheading {
  margin: 0 0 0.5rem;
  font-size: 1rem;
}

.view-heading {
  margin: 0.5rem 0;
  font-size: 1.25rem;
}

.detail-dl {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
  gap: 0.5rem 1rem;
  margin: 1rem 0;
  font-size: 0.9rem;
}

.detail-dl dt {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.detail-dl dd {
  margin: 0.15rem 0 0;
  font-weight: 600;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.funds-filters {
  margin-bottom: 1rem;
}

.funds-filters .dim {
  margin: 0.75rem 0 0.35rem;
}

.funds-filters .dim:first-child {
  margin-top: 0;
}

.funds-filter-hint {
  margin: 0.65rem 0 0;
}

.stocks-table th.sortable {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.stocks-table th.sortable:hover {
  color: var(--accent);
}

.stocks-table th.sort-active {
  color: var(--accent);
}

.toolbar a.btn.secondary {
  display: inline-flex;
  align-items: center;
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  text-decoration: none;
  color: var(--text);
}

.toolbar a.btn.secondary:hover {
  border-color: var(--accent);
}

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

.dim {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0.5rem 0 0.25rem;
}

.backtest-layout {
  display: grid;
  gap: 1rem;
}

.backtest-form {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
  gap: 0.75rem 1rem;
  align-items: end;
}

.backtest-form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.backtest-form input,
.backtest-form select {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--row-alt);
  color: var(--text);
}

.backtest-params {
  display: contents;
}

.backtest-actions {
  grid-column: 1 / -1;
}

.metric-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.metric-card {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--row-alt);
}

.metric-card dt {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.metric-card dd {
  margin: 0.2rem 0 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.equity-chart-wrap {
  height: 320px;
  min-height: 280px;
}

.equity-chart {
  width: 100%;
  height: 100%;
}
