:root {
  --bg: #f4f6f5; --surface: #ffffff; --surface-2: #eef2f0; --border: #dde3e0;
  --text: #17201d; --muted: #63706b; --accent: #176b5b; --accent-ink: #ffffff; --accent-soft: #d9efe9;
  --up: #0b6e4f; --down: #c23b3b; --warn: #955800;
  --l-net: #176b5b; --l-cash: #3a6fd8; --l-loan: #d0573a;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413; --surface: #171e1c; --surface-2: #1f2825; --border: #2c3733;
    --text: #e6ece9; --muted: #93a19b; --accent: #3fb59a; --accent-ink: #06130f; --accent-soft: #173a32;
    --up: #3ccf95; --down: #ff7a7a; --warn: #f0b04a;
    --l-net: #3fb59a; --l-cash: #7aa2ff; --l-loan: #ff8a66;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 Inter, system-ui, sans-serif; }
.num, td.r, .mono { font-variant-numeric: tabular-nums; }
.mono { font-family: 'JetBrains Mono', ui-monospace, monospace; }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; }
#nav { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#nav select { max-width: 220px; }
main { max-width: 1200px; margin: 0 auto; padding: 24px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); min-width: 0; }
.grid { display: grid; gap: 16px; }
.cols-main { grid-template-columns: minmax(0, 2fr) minmax(300px, 1fr); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: .8rem; font-weight: 500; }
.stat .value { font-size: 1.35rem; font-weight: 650; margin-top: 2px; }
.stat .sub { color: var(--muted); font-size: .8rem; }
.stat.hero { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.stat.hero .label, .stat.hero .sub { color: color-mix(in srgb, var(--accent-ink) 82%, transparent); }

.head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.muted { color: var(--muted); }
.up { color: var(--up); } .down { color: var(--down); }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px; font-size: .8rem; font-weight: 600; background: var(--surface-2); }
.pill.open { background: var(--accent-soft); color: var(--up); }
.pill.closed { color: var(--warn); }
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.tag { font-size: .72rem; font-weight: 600; padding: 2px 7px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
th.r, td.r { text-align: right; }
.table-wrap { overflow-x: auto; margin: 0 -10px; position: relative; }
tr.me td { background: var(--accent-soft); }
.empty { color: var(--muted); padding: 20px 0; text-align: center; }

button, .btn { font: inherit; font-weight: 600; font-size: .9rem; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; cursor: pointer; }
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--down); }
button.sm { padding: 4px 10px; font-size: .8rem; }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: 12px; }
label > input, label > select { margin-top: 4px; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-weight: 500; }
label.check input { width: auto; }
.row { display: flex; gap: 10px; align-items: flex-end; }
.row > * { flex: 1; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0 14px; }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

.auth { max-width: 400px; margin: 48px auto; }
.auth h1 { margin-bottom: 4px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; margin: 20px 0; }
.tabs button { flex: 1; border: 0; background: transparent; }
.tabs button.on { background: var(--surface); box-shadow: var(--shadow); }
.error { color: var(--down); font-size: .88rem; min-height: 1.3em; }

.trade .search { position: relative; }
.suggest { position: absolute; left: 0; right: 0; top: 100%; margin-top: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.12); z-index: 3; overflow: hidden; }
.suggest div { padding: 8px 12px; cursor: pointer; display: flex; gap: 10px; align-items: baseline; }
.suggest div:hover, .suggest div.active { background: var(--surface-2); }
.suggest b { min-width: 56px; }
.suggest span { color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quote { background: var(--surface-2); border-radius: 10px; padding: 14px; margin: 12px 0; }
.quote .price { font-size: 1.6rem; font-weight: 700; }
.summary-line { display: flex; justify-content: space-between; font-size: .88rem; padding: 3px 0; }
.summary-line.total { font-weight: 700; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 8px; }

.chart svg { width: 100%; height: 240px; display: block; }
.chart .legend { display: flex; gap: 16px; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.chart .legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: .78rem; padding: 6px 9px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -110%); }

dl.settings { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 0; font-size: .9rem; }
dl.settings dt { color: var(--muted); }
dl.settings dd { margin: 0; text-align: right; font-weight: 600; }
dl.settings .gap { border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }

.banner { background: var(--accent-soft); border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }

#toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-weight: 500; opacity: 0; transition: .2s; pointer-events: none; z-index: 20; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.err { background: var(--down); color: #fff; }

dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 24px; width: min(560px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.4); }

@media (max-width: 1024px) {
  .cols-main { grid-template-columns: 1fr; }
  main { padding: 16px; }
  .topbar { padding: 10px 16px; }
}

/* ---------- markets, tooltips, rules ---------- */
:root { --us: #2556b8; --tsx: #b3262f; --crypto: #84560b; }
@media (prefers-color-scheme: dark) { :root { --us: #7aa2ff; --tsx: #ff7a85; --crypto: #f0b04a; } }
.tag.ex-US { color: var(--us); background: color-mix(in srgb, var(--us) 12%, transparent); }
.tag.ex-TSX { color: var(--tsx); background: color-mix(in srgb, var(--tsx) 12%, transparent); }
.tag.ex-Crypto { color: var(--crypto); background: color-mix(in srgb, var(--crypto) 14%, transparent); }
.tag.type-buy { color: var(--up); } .tag.type-sell { color: var(--down); }
.suggest .tag { margin-left: auto; flex-shrink: 0; }
.ellip { max-width: 180px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.holdings td { white-space: nowrap; }
.holdings td:first-child { white-space: normal; }
.holdings .sub { font-size: .78rem; color: var(--muted); }
.pills { display: flex; gap: 6px; flex-wrap: wrap; }

.seg { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: 9px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 6px 4px; font-size: .82rem; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow), 0 0 0 1px var(--border); }
.quick { display: flex; gap: 6px; margin: -4px 0 10px; }
.quick button { flex: 1; }

.help { display: inline-grid; place-items: center; width: 15px; height: 15px; border-radius: 50%; font-size: 10px; font-weight: 700; background: var(--surface-2); color: var(--muted); cursor: help; position: relative; vertical-align: 1px; margin-left: 2px; }
.stat.hero .help { background: rgba(255,255,255,.25); color: inherit; }
.help:hover::after, .help:focus::after { content: attr(data-tip); position: absolute; left: 50%; top: calc(100% + 6px); transform: translateX(-50%); width: max-content; max-width: 240px; background: var(--text); color: var(--bg); font-size: .78rem; font-weight: 500; line-height: 1.4; padding: 8px 10px; border-radius: 8px; z-index: 10; white-space: normal; text-align: left; }
.help:focus { outline: 2px solid var(--accent); }

details.rules { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 12px; }
details.rules summary { cursor: pointer; font-weight: 600; font-size: .9rem; }
details.rules ul { margin: 10px 0 0; padding-left: 18px; font-size: .86rem; color: var(--muted); }
details.rules li { margin-bottom: 6px; }

.form-h { grid-column: 1 / -1; font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; margin: 10px 0 8px; padding-top: 10px; border-top: 1px solid var(--border); }
.form-h .muted { text-transform: none; letter-spacing: 0; font-weight: 500; }
dialog { max-height: calc(100vh - 32px); overflow-y: auto; }
.intro { margin: 0 0 12px; font-size: .9rem; }
.confirm { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; color: var(--text); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.grid > * { min-width: 0; }
/* Phones: each holding becomes a two-line row (asset over position | value | sell); price is in the trade panel. */
@media (max-width: 600px) {
  .holdings thead { display: none; }
  .holdings tr { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; column-gap: 12px; align-items: center; padding: 10px; border-bottom: 1px solid var(--border); }
  .holdings tbody tr:last-child { border-bottom: 0; }
  .holdings td { display: block; padding: 0; border: 0; white-space: normal; }
  .holdings td:nth-child(1) { grid-column: 1; grid-row: 1; }
  .holdings td:nth-child(2) { grid-column: 1; grid-row: 2; text-align: left; font-size: .85rem; margin-top: 2px; }
  .holdings td:nth-child(2) .sub { display: inline; margin-left: 6px; }
  .holdings td:nth-child(3) { display: none; }
  .holdings td:nth-child(4) { grid-column: 2; grid-row: 1 / 3; }
  .holdings td:nth-child(5) { grid-column: 3; grid-row: 1 / 3; }
  .holdings tfoot tr { border-top: 1px solid var(--border); }
  .holdings tfoot td:nth-child(2) { grid-column: 2; grid-row: 1; text-align: right; font-size: inherit; margin: 0; }
  .holdings tfoot td:nth-child(3) { display: none; }
  .ellip { max-width: 100%; }
}

.foot { max-width: 1200px; margin: 0 auto; padding: 8px 24px 32px; font-size: .85rem; }
.foot a { color: var(--muted); }
