:root {
  color-scheme: light dark;
  --bg: #f7f7f2;
  --panel: rgba(255, 255, 255, 0.78);
  --panel-strong: rgba(255, 255, 255, 0.92);
  --text: #171817;
  --muted: #767872;
  --line: rgba(36, 38, 34, 0.12);
  --accent: #0a84ff;
  --green: #2ea44f;
  --red: #d93644;
  --amber: #b36b00;
  --shadow: 0 18px 42px rgba(28, 31, 25, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111310;
    --panel: rgba(31, 33, 30, 0.82);
    --panel-strong: rgba(39, 41, 37, 0.94);
    --text: #f2f3ef;
    --muted: #a5a79f;
    --line: rgba(255, 255, 255, 0.13);
    --shadow: 0 18px 42px rgba(0, 0, 0, 0.35);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at 12% 0%, rgba(10, 132, 255, 0.13), transparent 28rem),
    radial-gradient(circle at 85% 8%, rgba(46, 164, 79, 0.12), transparent 28rem),
    var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

button,
label.primary-action,
input,
select {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 18px) 14px calc(env(safe-area-inset-bottom) + 28px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.topbar h1 {
  margin: 2px 0 0;
  font-size: clamp(2rem, 11vw, 3.8rem);
  line-height: 0.96;
  letter-spacing: 0;
}

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel-strong);
  color: var(--text);
  font-weight: 800;
}

main {
  display: grid;
  gap: 14px;
}

.hero-card,
.panel,
.metric-card {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
}

.hero-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px;
}

.hero-card h2,
.panel h2 {
  margin: 4px 0 0;
  font-size: 1.18rem;
  line-height: 1.18;
}

.hero-card p:not(.eyebrow),
.compact-panel p {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.35;
}

.primary-action,
.secondary-action,
.danger-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border: 0;
  border-radius: 14px;
  padding: 0 16px;
  cursor: pointer;
  font-weight: 750;
  text-decoration: none;
  white-space: nowrap;
}

.primary-action {
  background: var(--accent);
  color: white;
}

.secondary-action {
  background: rgba(10, 132, 255, 0.12);
  color: var(--accent);
}

.danger-action {
  background: rgba(217, 54, 68, 0.13);
  color: var(--red);
}

.primary-action input {
  display: none;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.metric-card {
  min-height: 120px;
  padding: 15px;
}

.metric-card.prominent {
  grid-column: 1 / -1;
  min-height: 142px;
  background: linear-gradient(135deg, rgba(10, 132, 255, 0.17), rgba(46, 164, 79, 0.11)), var(--panel-strong);
}

.metric-card span,
.metric-card small {
  display: block;
  color: var(--muted);
}

.metric-card span {
  font-size: 0.86rem;
  font-weight: 700;
}

.metric-card strong {
  display: block;
  margin: 12px 0 8px;
  font-size: clamp(1.28rem, 7vw, 2.5rem);
  line-height: 1.02;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.metric-card:not(.prominent) strong {
  font-size: clamp(1.08rem, 5.8vw, 1.65rem);
}

.panel {
  padding: 16px;
}

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

.segmented {
  display: inline-flex;
  flex: 0 0 auto;
  padding: 3px;
  border-radius: 13px;
  background: rgba(128, 128, 128, 0.17);
}

.segmented button {
  min-height: 34px;
  border: 0;
  border-radius: 10px;
  padding: 0 12px;
  background: transparent;
  color: var(--text);
  font-weight: 700;
}

.segmented button.active {
  background: var(--panel-strong);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.segmented.compact button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 0.86rem;
}

.header-actions,
.filters {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border-radius: 999px;
  padding: 0 10px;
  background: rgba(128, 128, 128, 0.15);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

.allocation-layout {
  display: grid;
  grid-template-columns: minmax(180px, 280px) minmax(0, 1fr);
  gap: 16px;
  align-items: center;
}

.chart-frame {
  width: 100%;
  overflow: visible;
}

.square-chart {
  width: min(280px, 100%);
  aspect-ratio: 1;
  justify-self: center;
}

.line-chart {
  height: clamp(230px, 56vw, 320px);
  min-height: 230px;
  margin-top: 12px;
  border-radius: 14px;
  background: rgba(128, 128, 128, 0.08);
}

#allocationChart,
#historyChart {
  display: block;
  width: 100%;
  height: 100%;
}

.legend {
  display: grid;
  gap: 8px;
}

.legend-row,
.position-row,
.transaction-row {
  display: grid;
  gap: 6px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}

.legend-row:first-child,
.position-row:first-child,
.transaction-row:first-child {
  border-top: 0;
}

.legend-row {
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  padding: 8px 0;
}

.dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
}

.row-title {
  min-width: 0;
  font-weight: 760;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-meta {
  color: var(--muted);
  font-size: 0.88rem;
}

.compact-extra {
  display: none;
}

.amount {
  font-weight: 760;
  font-variant-numeric: tabular-nums;
}

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

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

.position-row {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
}

.positions-list.list-mode .position-row {
  grid-template-columns: minmax(0, 1.2fr) auto;
  padding: 11px 0;
}

.positions-list.list-mode .position-details {
  display: none;
}

.positions-list.list-mode .row-meta.compact-extra {
  display: block;
  margin-top: 3px;
}

.position-details {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.detail-chip {
  padding: 9px;
  border-radius: 12px;
  background: rgba(128, 128, 128, 0.12);
}

.detail-chip span {
  display: block;
  color: var(--muted);
  font-size: 0.73rem;
  font-weight: 700;
}

.detail-chip strong {
  display: block;
  margin-top: 3px;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

.transaction-row {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
}

input[type="search"],
select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0 10px;
  background: var(--panel-strong);
  color: var(--text);
}

input[type="search"] {
  width: min(220px, 100%);
}

.history-metrics,
.data-grid {
  display: grid;
  gap: 10px;
}

.history-metrics {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.history-metric,
.mini-card {
  padding: 10px;
  border-radius: 13px;
  background: rgba(128, 128, 128, 0.1);
}

.history-metric span,
.mini-card span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 750;
}

.history-metric strong,
.mini-card strong {
  display: block;
  margin-top: 4px;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.line-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
}

.legend-line {
  display: inline-block;
  width: 22px;
  height: 3px;
  margin-right: 6px;
  border-radius: 999px;
  vertical-align: middle;
}

.accent-line {
  background: var(--accent);
}

.muted-line {
  background: var(--muted);
}

.data-grid {
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.2fr);
}

.data-grid h3 {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

.mini-list {
  display: grid;
  gap: 7px;
}

.mini-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 9px 10px;
  border-radius: 12px;
  background: rgba(128, 128, 128, 0.1);
}

.mini-row span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mini-row strong {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.compact-panel {
  display: flex;
  justify-content: space-between;
  gap: 14px;
}

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

.empty-state {
  padding: 22px 8px;
  text-align: center;
  color: var(--muted);
}

.empty-state strong,
.empty-state span {
  display: block;
}

dialog {
  width: min(360px, calc(100% - 28px));
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  background: var(--panel-strong);
  color: var(--text);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.25);
}

.dialog-close {
  width: 100%;
  margin-top: 12px;
}

@media (max-width: 720px) {
  .hero-card,
  .compact-panel {
    align-items: stretch;
    flex-direction: column;
  }

  .primary-action,
  .secondary-action,
  .danger-action {
    width: 100%;
  }

  .allocation-layout {
    grid-template-columns: 1fr;
  }

  .section-header {
    align-items: stretch;
    flex-direction: column;
  }

  .position-details {
    grid-template-columns: 1fr;
  }

  .history-metrics,
  .data-grid {
    grid-template-columns: 1fr;
  }

  .filters {
    justify-content: stretch;
  }

  .filters input,
  .filters select {
    width: 100%;
  }

  .action-row {
    justify-content: stretch;
  }
}

@media (min-width: 820px) {
  .summary-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .metric-card.prominent {
    grid-column: span 2;
  }
}
