:root {
  --bg: #0b0d12;
  --surface: #141821;
  --surface-2: #1b2030;
  --border: #232a3a;
  --text: #e9ecf3;
  --muted: #8a93a6;
  --accent: #6d7cff;
  --up: #22c55e;
  --down: #f0506e;
  --up-bg: rgba(34, 197, 94, .12);
  --down-bg: rgba(240, 80, 110, .12);
  --radius: 16px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f5f6fa;
    --surface: #ffffff;
    --surface-2: #eef0f6;
    --border: #e1e4ec;
    --text: #0f1320;
    --muted: #5d6577;
    --accent: #4f5fe8;
    --up: #0e9f55;
    --down: #d93954;
    --up-bg: rgba(14, 159, 85, .1);
    --down-bg: rgba(217, 57, 84, .1);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
h2 { font-size: 16px; font-weight: 600; margin: 0 0 12px; }
.muted { color: var(--muted); font-weight: 400; }
.num-font, .num, .kpis b, .legend, .stat b { font-variant-numeric: tabular-nums; }
.up { color: var(--up); }
.down { color: var(--down); }

/* Buttons */
.btn {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; font-weight: 600; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--down); }
.icon-btn {
  background: transparent; border: 1px solid var(--border); border-radius: 10px;
  width: 36px; height: 36px; display: inline-grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Segmented control */
.seg { display: inline-flex; gap: 2px; background: var(--surface-2); border-radius: 10px; padding: 3px; flex-wrap: wrap; }
.seg button {
  background: transparent; border: 0; border-radius: 8px; padding: 6px 12px;
  font-weight: 600; font-size: 13px; color: var(--muted); cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.seg.small button { padding: 4px 9px; font-size: 12px; }

/* Header */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px; background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.mkt { display: flex; gap: 6px; }
.pill { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.open { background: var(--up-bg); color: var(--up); }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live i.on { background: var(--up); box-shadow: 0 0 0 0 var(--up); animation: pulse 2s infinite; }
.live i.err { background: var(--down); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(34,197,94,.6); } 70% { box-shadow: 0 0 0 8px rgba(34,197,94,0); } 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); } }

/* Layout */
.grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; padding: 20px 24px; max-width: 1280px; margin: 0 auto; }
.col-main, .col-side { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
select { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; }

/* Hero */
.filter-row { margin-bottom: 14px; }
.label { font-size: 13px; color: var(--muted); font-weight: 500; }
.big { font-size: 40px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; margin: 2px 0; }
.chg { font-weight: 600; font-size: 15px; min-height: 22px; }
.chg .when { color: var(--muted); font-weight: 400; margin-left: 6px; }
.chart-wrap { position: relative; margin: 16px -4px 10px; }
.chart { width: 100%; height: 220px; display: block; touch-action: pan-y; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; }
.chart .line.up, .chart .dot.up { stroke: var(--up); }
.chart .line.down, .chart .dot.down { stroke: var(--down); }
.chart .dot { fill: var(--surface); stroke-width: 2.5; }
.chart .stop-up { stop-color: var(--up); }
.chart .stop-down { stop-color: var(--down); }
.chart .baseline { stroke: var(--muted); stroke-dasharray: 3 4; stroke-width: 1; opacity: .6; }
.chart .xhair { stroke: var(--muted); stroke-width: 1; }
.chart .empty { fill: var(--muted); font-size: 13px; }
.ranges { margin-bottom: 16px; }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; border-top: 1px solid var(--border); padding-top: 16px; }
.kpis b { display: block; font-size: 16px; margin-top: 2px; }

/* Holdings */
.hold-head, .row {
  display: grid; grid-template-columns: minmax(0, 1.8fr) 90px minmax(0, 1fr) minmax(0, .8fr) minmax(0, 1.1fr);
  align-items: center; gap: 12px;
}
.hold-head { font-size: 12px; color: var(--muted); padding: 0 8px 8px; border-bottom: 1px solid var(--border); }
.hold-head span:not(:first-child) { text-align: right; }
.hold-head span:nth-child(2) { text-align: center; }
.row {
  width: 100%; background: transparent; border: 0; border-bottom: 1px solid var(--border);
  padding: 12px 8px; text-align: left; cursor: pointer; border-radius: 0;
}
.row:hover { background: var(--surface-2); }
.row:last-child { border-bottom: 0; }
.who { display: flex; align-items: center; gap: 12px; min-width: 0; }
.who > span { min-width: 0; }
.who b { display: block; font-weight: 700; }
.who small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.logo {
  flex: none; width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
  font-weight: 700; font-size: 12px; color: #fff; background: var(--c, var(--accent));
}
.chip { display: inline-block; font-size: 10px; font-weight: 600; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); margin-right: 4px; }
.spark { width: 90px; height: 32px; }
.spark path { fill: none; stroke-width: 1.5; }
.spark .up { stroke: var(--up); } .spark .down { stroke: var(--down); }
.num { text-align: right; min-width: 0; }
.num b { display: block; font-weight: 600; white-space: nowrap; }
.num small { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; }
.num small.up { color: var(--up); } .num small.down { color: var(--down); }
.badge { display: inline-block; padding: 1px 6px; border-radius: 6px; font-weight: 600; }
.badge.up { background: var(--up-bg); } .badge.down { background: var(--down-bg); }
.empty-state { padding: 28px 8px; text-align: center; color: var(--muted); }

/* Allocation */
.donut-wrap { position: relative; width: 190px; height: 190px; margin: 4px auto 16px; }
.donut-wrap svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.donut-wrap circle { fill: none; stroke-width: 14; transition: stroke-width .15s; }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.donut-center b { font-size: 18px; }
.donut-center small { color: var(--muted); font-size: 12px; }
.legend { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.legend div { display: grid; grid-template-columns: 10px 1fr auto auto; gap: 8px; align-items: center; }
.legend i { width: 10px; height: 10px; border-radius: 3px; }
.legend span:last-child { color: var(--muted); width: 48px; text-align: right; }

/* Accounts and movers */
.acct { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); gap: 10px; }
.acct:last-child { border-bottom: 0; }
.acct small { display: block; color: var(--muted); font-size: 12px; }
.mover { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; cursor: pointer; }
.mover .who b { font-size: 14px; }
.mover .logo { width: 30px; height: 30px; border-radius: 9px; font-size: 10px; }

.foot { color: var(--muted); font-size: 12px; text-align: center; padding: 0 24px 32px; max-width: 760px; margin: 0 auto; }

/* Sheet (detail + edit) */
.sheet { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
.sheet-panel {
  position: relative; width: min(760px, 100%); max-height: min(90vh, 900px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 24px;
}
.sheet-close { position: absolute; top: 14px; right: 14px; }
.detail-head { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; padding-right: 40px; }
.detail-head .logo { width: 48px; height: 48px; font-size: 14px; }
.detail-head h3 { margin: 0; font-size: 20px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin: 16px 0; }
.stat span { display: block; font-size: 12px; color: var(--muted); }
.stat b { font-weight: 600; }
.pos-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.pos-table th { text-align: right; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 4px; }
.pos-table td { text-align: right; padding: 8px 4px; border-top: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.pos-table th:first-child, .pos-table td:first-child { text-align: left; }
.section-title { font-size: 14px; font-weight: 600; margin: 20px 0 8px; }

/* Edit form */
.edit-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.edit-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 4px; }
.edit-table td { padding: 4px; }
.edit-table input { width: 100%; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 7px 8px; min-width: 0; }
.edit-table input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.edit-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 18px; }
.edit-actions .spacer { flex: 1; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0 12px; }

/* Lock screen */
.lock { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.lock-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px; display: flex; flex-direction: column; gap: 12px; }
.big-brand { font-size: 22px; }
.lock-card input { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.lock-msg { color: var(--down); min-height: 1em; margin: 0; font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 500; font-size: 14px;
  max-width: calc(100% - 32px);
}

/* Responsive */
@media (max-width: 980px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .top { padding: 10px 16px; }
  .top .brand { font-size: 0; gap: 0; }
  .grid { padding: 12px 16px; gap: 14px; }
  .col-main, .col-side { gap: 14px; }
  .card { padding: 16px; }
  .big { font-size: 32px; }
  .chart { height: 180px; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .hide-sm { display: none; }
  .hold-head, .row { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr) minmax(0, 1.1fr); gap: 8px; }
  .logo { width: 34px; height: 34px; }
  .sheet { place-items: end stretch; }
  .sheet-panel { border-radius: 20px 20px 0 0; max-height: 92vh; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .edit-table .col-cost { display: none; }
  .status { flex-wrap: nowrap; gap: 6px; }
  .status .btn { padding: 7px 12px; font-size: 13px; }
  .pill { font-size: 11px; padding: 3px 7px; }
  .live span { display: none; }
  .ranges { display: flex; flex-wrap: nowrap; width: 100%; }
  .ranges button { flex: 1; padding: 6px 0; }
}
