:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-soft: #f0f4f7;
  --text: #202733;
  --muted: #6b7280;
  --border: #d9dee6;
  --primary: #2563eb;
  --positive: #118a74;
  --negative: #c64949;
  --line: #2e6dd8;
  --shadow: 0 16px 32px rgba(31, 41, 55, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.trend-popout-open {
  overflow: hidden;
}

button,
select {
  font: inherit;
}

button {
  border: 1px solid var(--primary);
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  padding: 8px 12px;
  cursor: pointer;
}

button.secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

select {
  min-width: 180px;
  max-width: 280px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 8px 10px;
}

.app-shell {
  width: min(1760px, calc(100vw - 32px));
  margin: 22px auto 36px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  margin-bottom: 14px;
}

h1 {
  margin: 0 0 5px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0;
}

.site-brand {
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0;
}

h2 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0;
}

.meta-line,
.status-line {
  color: var(--muted);
}

.toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.summary-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
}

.summary-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.summary-item strong {
  display: block;
  font-size: 18px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.trend-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 12px 14px;
  position: sticky;
  top: 14px;
  max-height: calc(100vh - 28px);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 10px;
}

.trend-popout-btn {
  padding: 6px 10px;
  white-space: nowrap;
}

.trend-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(32, 39, 51, 0.34);
}

.trend-panel.is-expanded {
  position: fixed;
  inset: 24px;
  z-index: 30;
  max-height: none;
  width: auto;
  padding: 16px;
}

.trend-table-wrap {
  overflow-x: auto;
  overflow-y: auto;
  min-height: 0;
  flex: 1;
}

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

.trend-panel.is-expanded .trend-table {
  min-width: 0;
  table-layout: fixed;
  font-size: 13px;
}

.trend-panel.is-expanded .trend-table th,
.trend-panel.is-expanded .trend-table td {
  padding: 8px 9px;
  white-space: normal;
  overflow-wrap: anywhere;
}

.trend-panel.is-expanded .trend-table th:nth-child(1) {
  width: 18%;
}

.trend-panel.is-expanded .trend-table th:nth-child(2) {
  width: 10%;
}

.trend-panel.is-expanded .trend-table th:nth-child(3),
.trend-panel.is-expanded .trend-table th:nth-child(4),
.trend-panel.is-expanded .trend-table th:nth-child(5),
.trend-panel.is-expanded .trend-table th:nth-child(7),
.trend-panel.is-expanded .trend-table th:nth-child(9) {
  width: 9%;
}

.trend-panel.is-expanded .trend-table th:nth-child(6),
.trend-panel.is-expanded .trend-table th:nth-child(8) {
  width: 13.5%;
}

.trend-table th,
.trend-table td {
  border-bottom: 1px solid var(--border);
  padding: 6px 7px;
  text-align: right;
  white-space: nowrap;
}

.trend-table th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-soft);
}

.trend-table th:first-child,
.trend-table th:nth-child(2),
.trend-table td:first-child,
.trend-table td:nth-child(2) {
  text-align: left;
}

.trend-table tr.aggregate-row td {
  background: #eef4ff;
  font-weight: 600;
}

.trend-table tr.trend-group-start td {
  border-top: 2px solid var(--border);
}

.trend-table tr:hover td {
  background: #f7faff;
}

.trend-row-button {
  appearance: none;
  width: 100%;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  padding: 0;
  text-align: left;
  font: inherit;
  cursor: pointer;
}

.trend-row-button:hover {
  color: var(--primary);
}

.cell-positive {
  color: var(--positive);
}

.cell-negative {
  color: var(--negative);
}

.percentile-chip {
  display: inline-block;
  min-width: 48px;
  border-radius: 999px;
  padding: 2px 7px;
  background: #eef2f6;
  color: var(--text);
  text-align: center;
}

.percentile-chip.is-high {
  background: #dcf5ed;
  color: #0f6f5d;
}

.percentile-chip.is-low {
  background: #fbe7e7;
  color: #a23b3b;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: 270px minmax(540px, 1fr) minmax(420px, 520px);
  gap: 12px;
  align-items: start;
}

.main-stage {
  min-width: 0;
}

.etf-rail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: calc(100vh - 28px);
  overflow: auto;
  position: sticky;
  top: 14px;
}

.etf-list {
  display: grid;
  gap: 1px;
}

.etf-row {
  width: 100%;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  padding: 10px 12px;
  text-align: left;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  border-bottom: 1px solid var(--border);
}

.etf-row:hover {
  background: var(--surface-soft);
}

.etf-row.is-active {
  background: #e8f0ff;
}

.etf-row:disabled {
  color: var(--muted);
  opacity: 1;
}

.etf-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.etf-code,
.etf-note {
  color: var(--muted);
  font-size: 12px;
}

.chart-stack {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 14px;
  position: relative;
}

.chart {
  height: 168px;
  position: relative;
}

.chart:last-of-type {
  height: 190px;
}

svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.axis text,
.chart-label,
.marker-label {
  fill: var(--muted);
  font-size: 11px;
}

.chart-title {
  fill: var(--text);
  font-size: 12px;
  font-weight: 600;
}

.grid-line,
.axis-line {
  stroke: var(--border);
  stroke-width: 1;
}

.series-line {
  fill: none;
  stroke: var(--line);
  stroke-width: 2;
}

.benchmark-line {
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.4;
  stroke-dasharray: 4 4;
  opacity: 0.6;
}

.bar-turnover {
  fill: var(--positive);
  opacity: 0.7;
}

.bar-positive {
  fill: var(--positive);
  opacity: 0.76;
}

.bar-negative {
  fill: var(--negative);
  opacity: 0.74;
}

.zero-line {
  stroke: var(--border);
  stroke-width: 1;
}

.marker-line {
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 3 4;
  opacity: 0.7;
}

.marker-dot {
  fill: var(--surface);
  stroke: var(--line);
  stroke-width: 2;
}

.hover-layer,
.axis-pan-layer {
  fill: transparent;
  touch-action: none;
}

.hover-layer {
  cursor: crosshair;
}

.axis-pan-layer {
  cursor: grab;
}

.axis-pan-layer.is-panning {
  cursor: grabbing;
}

.hover-line {
  stroke: var(--text);
  stroke-width: 1;
  opacity: 0.35;
  pointer-events: none;
}

.zoom-selection {
  fill: rgba(37, 99, 235, 0.12);
  stroke: var(--primary);
  stroke-width: 1;
  pointer-events: none;
}

.tooltip {
  position: absolute;
  visibility: hidden;
  z-index: 4;
  pointer-events: none;
  min-width: 230px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 9px 10px;
}

.tooltip-title {
  font-weight: 600;
  margin-bottom: 4px;
}

.tooltip-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
}

.tooltip-row strong {
  color: var(--text);
  font-weight: 500;
}

.status-line {
  min-height: 22px;
  margin-top: 10px;
}

@media (max-width: 980px) {
  .app-shell {
    width: min(100vw - 20px, 1760px);
    margin-top: 14px;
  }

  .topbar {
    display: block;
  }

  .toolbar {
    justify-content: flex-start;
    margin-top: 10px;
  }

  .section-head {
    display: block;
  }

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

  .etf-rail,
  .trend-panel {
    position: static;
    max-height: 240px;
  }

  .trend-panel.is-expanded {
    position: fixed;
    inset: 10px;
    max-height: none;
  }

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

  .chart-stack {
    padding: 10px;
  }

  .chart {
    height: 178px;
  }
}

@media (min-width: 981px) and (max-width: 1380px) {
  .dashboard-grid {
    grid-template-columns: 250px minmax(480px, 1fr);
  }

  .trend-panel {
    grid-column: 1 / -1;
    position: static;
    max-height: none;
  }
}

@media (max-width: 560px) {
  select {
    width: 100%;
    max-width: none;
  }

  .toolbar {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .toolbar select {
    grid-column: span 2;
  }

  .trend-panel.is-expanded .trend-table {
    font-size: 12px;
  }
}
