:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --up: #006300;
  --down: #d03b3b;
  --accent: #2a78d6;
  --accent-text: #ffffff;
  --danger: #d03b3b;
  --banner: #fff4e0;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --up: #0ca30c;
    --down: #ef6b6b;
    --accent: #3987e5;
    --danger: #ef6b6b;
    --banner: #3a2c12;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --up: #0ca30c;
  --down: #ef6b6b;
  --accent: #3987e5;
  --danger: #ef6b6b;
  --banner: #3a2c12;
}

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

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
}

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }

button, input { font: inherit; color: inherit; }

.muted { color: var(--muted); }
.up { color: var(--up); }
.down { color: var(--down); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* --- Top bar --- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px max(16px, calc((100% - 1200px) / 2));
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.status { color: var(--muted); font-size: 13px; margin-right: auto; }

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 16px;
}

/* --- Buttons & controls --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  color: inherit;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .seg button:focus-visible, .tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn.icon { width: 36px; padding: 0; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border-radius: 999px;
  background: var(--surface-2);
}
.seg button {
  white-space: nowrap;
  border: 0;
  background: transparent;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-2);
  cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }
.seg-wide { display: flex; }
.seg-wide button { flex: 1; }

.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0;
  background: transparent;
  padding: 6px 2px;
  margin-right: 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); cursor: pointer; }

/* --- Banner --- */
.banner {
  max-width: 1200px;
  margin: 12px auto 0;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--banner);
  border-radius: var(--radius);
  width: calc(100% - 32px);
}
.banner[hidden] { display: none; }

/* --- Cards --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/* --- KPI tiles --- */
.kpis {
  display: grid;
  grid-template-columns: 1.4fr repeat(3, 1fr);
  gap: 16px;
}
.kpi { display: grid; gap: 2px; align-content: start; }
.kpi-label { font-size: 13px; color: var(--text-2); }
.kpi-value { font-size: 24px; font-weight: 600; line-height: 1.2; }
.kpi-hero .kpi-value { font-size: 40px; letter-spacing: -0.01em; }
.kpi-sub { font-size: 13px; color: var(--muted); }
.kpi-delta { font-size: 14px; font-weight: 600; }

/* --- Chart --- */
.chart-card .seg-wide { margin-bottom: 12px; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); min-height: 20px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 14px; height: 2px; border-radius: 1px; }
.chart { position: relative; height: 260px; margin-top: 8px; touch-action: pan-y; }
.chart.loading svg { opacity: 0.45; transition: opacity 0.2s; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .overlay { fill: transparent; cursor: crosshair; outline: none; }
.chart .empty { fill: var(--muted); font-size: 14px; }
.tooltip {
  position: absolute;
  top: 0;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  padding: 8px 10px;
  font-size: 13px;
  min-width: 170px;
  z-index: 2;
}
.tooltip .tt-date { color: var(--muted); margin-bottom: 4px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 8px; }
.tooltip .tt-row strong { font-variant-numeric: tabular-nums; }
.tooltip .tt-row span { color: var(--text-2); }
.tooltip i { display: inline-block; width: 12px; height: 2px; border-radius: 1px; flex: none; }

.hint { font-size: 13px; color: var(--text-2); margin: 8px 0 0; }
.hint .btn { margin-left: 4px; }
.chart-table { margin-top: 12px; font-size: 13px; }
.chart-table summary { cursor: pointer; color: var(--text-2); }

/* --- Platforms --- */
.platform { padding: 10px 0; border-top: 1px solid var(--grid); }
.platform:first-child { border-top: 0; padding-top: 0; }
.platform-row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.platform-name { font-weight: 600; }
.platform-meta { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-2); margin-top: 2px; }

.card-sub-head { font-size: 13px; margin: 18px 0 6px; padding-top: 12px; border-top: 1px solid var(--grid); color: var(--text-2); font-weight: 600; }
.cash-list { display: grid; gap: 8px; margin-bottom: 10px; }
.cash-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.cash-name { flex: 1 1 140px; min-width: 0; font-weight: 600; overflow-wrap: anywhere; }
.cash-amount { flex: none; display: inline-flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text-2); }
.cash-row .cash-amount input { width: 110px; text-align: right; font-variant-numeric: tabular-nums; }
.cash-row .btn.icon { flex: none; }
.cash-add { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin-bottom: 8px; }
.cash-add input { min-width: 0; }
.cash-add button { justify-self: start; align-self: center; }
.bar-track { height: 8px; margin-top: 6px; }
.bar { height: 8px; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }

/* --- Tables --- */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
  text-align: right;
  font-weight: 500;
  font-size: 12px;
  color: var(--muted);
  padding: 6px 8px;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}
td { text-align: right; padding: 10px 8px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th:first-child, td:first-child { text-align: left; padding-left: 0; }
th button.sort { all: unset; cursor: pointer; display: inline-flex; gap: 3px; align-items: center; border-radius: 4px; }
th button.sort:hover, th button.sort.active { color: var(--text); }
th button.sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sort-arrow { font-size: 9px; min-width: 9px; }
th:last-child, td:last-child { padding-right: 0; }
tbody tr.holding { cursor: pointer; }
tbody tr.holding:hover td, tbody tr.tx:hover td { background: var(--surface-2); }
tbody tr.tx { cursor: pointer; }
.name { font-weight: 600; display: flex; gap: 6px; align-items: baseline; }
.sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
tr.holding .sub { padding-left: 18px; }
.cell-sub { display: block; font-size: 12px; }
.chev { display: inline-block; flex: none; width: 12px; color: var(--muted); transition: transform 0.15s; }
tr.open .chev { transform: rotate(90deg); }
tr.closed-pos td { color: var(--text-2); }
tr.detail > td { background: var(--surface-2); padding: 12px 16px; text-align: left; }
.detail-grid { display: grid; gap: 12px; }
.detail-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.detail table { font-size: 13px; }
.detail td, .detail th { padding: 6px 8px; }
.detail h3 { font-size: 13px; margin: 0 0 4px; color: var(--text-2); font-weight: 600; }
.manual-price { display: inline-flex; gap: 6px; align-items: center; }
.manual-price input { width: 110px; }
.empty-state { text-align: center; padding: 32px 16px; color: var(--text-2); }
.empty-state p { margin: 0 0 12px; }
.warn-dot { color: var(--down); font-weight: 700; }

/* --- Footer --- */
.footer { font-size: 13px; color: var(--muted); padding: 8px 0 32px; }
.footer p { margin: 0 0 8px; }
.footer-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
#whoami { margin-right: auto; }

/* --- Dialog --- */
dialog {
  width: min(520px, calc(100% - 24px));
  max-height: calc(100% - 24px);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
#tx-form { padding: 16px; display: grid; gap: 12px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { font-size: 18px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; padding-top: 4px; }
.spacer { flex: 1; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field[hidden] { display: none; }
.label { font-size: 13px; color: var(--text-2); font-weight: 500; }
.field-note { font-size: 12px; color: var(--muted); min-height: 0; }
input[type="text"], input[type="search"], input[type="date"] {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input.invalid { border-color: var(--danger); }
.search { position: relative; }
.results {
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 260px;
  overflow-y: auto;
  background: var(--surface);
}
.results:empty { display: none; }
.results li { border-top: 1px solid var(--grid); }
.results li:first-child { border-top: 0; }
.results button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.results button:hover, .results button:focus-visible { background: var(--surface-2); outline: none; }
.results .r-name { font-weight: 600; }
.results .r-meta { font-size: 12px; color: var(--muted); display: block; font-weight: 400; }
.results .r-price { font-size: 13px; color: var(--text-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.results .r-group { padding: 6px 10px; font-size: 12px; color: var(--muted); background: var(--surface-2); }
.picked {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
}
.picked[hidden] { display: none; }
.picked .muted { display: block; font-size: 12px; }
.summary { margin: 0; font-size: 14px; color: var(--text-2); min-height: 1em; }
.error { margin: 0; color: var(--danger); font-size: 14px; }
.error:empty, .summary:empty { display: none; }
[data-tx-type="dividend"] .trade-only { display: none; }
[data-tx-type="buy"] .dividend-only, [data-tx-type="sell"] .dividend-only { display: none; }
[data-tx-type="sell"] .buy-only, [data-tx-type="dividend"] .buy-only { display: none; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; cursor: pointer; }
.check input { margin-top: 3px; }
.check .field-note { display: block; }

/* --- Narrow screens --- */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-hero { grid-column: 1 / -1; }
}

@media (max-width: 640px) {
  .topbar { gap: 8px; }
  .topbar h1 { font-size: 18px; }
  .kpis { gap: 12px; }
  .kpi-hero .kpi-value { font-size: 34px; }
  .kpi-value { font-size: 19px; }
  .chart { height: 210px; }
  .col-opt { display: none; }
  table { font-size: 13px; }
  .row { grid-template-columns: 1fr; }
  .seg button { padding: 5px 8px; }
}
