:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --line: #e3e6eb;
  --ink: #15181d;
  --ink-2: #4c535e;
  --ink-3: #7c8490;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --gain: #0f8a5f;
  --gain-soft: #e3f5ec;
  --loss: #cf3b3b;
  --loss-soft: #fbe9e9;
  --warn: #9a6400;
  --warn-soft: #fdf2dc;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --bar-gain: #1baf7a;
  --bar-loss: #e34948;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 2px 8px rgba(16, 24, 40, 0.05);
  --radius: 12px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111214;
    --surface: #1a1b1e;
    --surface-2: #212226;
    --line: #2e3035;
    --ink: #f1f2f4;
    --ink-2: #b9bec7;
    --ink-3: #868c96;
    --accent: #3987e5;
    --accent-soft: #1b2b40;
    --gain: #3cc48f;
    --gain-soft: #15291f;
    --loss: #ef6b6b;
    --loss-soft: #351a1a;
    --warn: #e9b44c;
    --warn-soft: #33290f;
    --series-1: #3987e5;
    --series-2: #d95926;
    --bar-gain: #199e70;
    --bar-loss: #e66767;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111214; --surface: #1a1b1e; --surface-2: #212226; --line: #2e3035;
  --ink: #f1f2f4; --ink-2: #b9bec7; --ink-3: #868c96; --accent: #3987e5; --accent-soft: #1b2b40;
  --gain: #3cc48f; --gain-soft: #15291f; --loss: #ef6b6b; --loss-soft: #351a1a; --warn: #e9b44c; --warn-soft: #33290f;
  --series-1: #3987e5; --series-2: #d95926; --bar-gain: #199e70; --bar-loss: #e66767; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--ink-3); }
.small { font-size: 0.84rem; }
.gain { color: var(--gain); }
.loss { color: var(--loss); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }

button { transition: transform 0.08s ease, background-color 0.15s ease; font: inherit; cursor: pointer; }
button:active { transform: scale(0.95); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }

.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 8px 14px; border-radius: 9px; font-weight: 600; font-size: 0.9rem;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.07); }
.btn-buy { background: var(--gain); color: #fff; border-color: var(--gain); }
.btn-sell { background: var(--loss); color: #fff; border-color: var(--loss); }
.btn-danger { color: var(--loss); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 4px 9px; font-size: 0.82rem; border-radius: 7px; }
.icon-btn { border: 0; background: transparent; color: var(--ink-3); padding: 4px 6px; border-radius: 6px; font-size: 1rem; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; min-height: 80px; }
label.field { display: grid; gap: 4px; font-size: 0.85rem; color: var(--ink-2); font-weight: 600; }
.check { display: inline-flex; gap: 8px; align-items: center; font-size: 0.9rem; cursor: pointer; }
.check input { width: auto; }

/* --- layout --- */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar-row { display: flex; align-items: center; gap: 12px; padding: 10px 16px; max-width: 1280px; margin: 0 auto; }
.brand { font-weight: 800; font-size: 1.05rem; white-space: nowrap; display: flex; align-items: center; gap: 8px; color: var(--ink); }
.brand svg { flex: none; }
.search-wrap { position: relative; flex: 1; max-width: 460px; margin-left: auto; }
.search-wrap input { padding-left: 34px; }
.search-wrap .search-ico { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--ink-3); pointer-events: none; }
.dropdown {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); max-height: 60vh; overflow: auto; z-index: 40;
}
.dd-item { display: flex; gap: 10px; align-items: center; padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.dd-item:last-child { border-bottom: 0; }
.dd-item.active, .dd-item:hover { background: var(--accent-soft); }
.dd-sym { font-weight: 700; min-width: 64px; }
.dd-name { flex: 1; color: var(--ink-2); font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dd-price { font-size: 0.85rem; white-space: nowrap; }

.tabs { display: flex; gap: 2px; padding: 0 12px; max-width: 1280px; margin: 0 auto; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 9px 12px 10px; color: var(--ink-2); font-weight: 600; font-size: 0.9rem; white-space: nowrap;
  border-bottom: 2px solid transparent; text-decoration: none !important;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 16px 16px 80px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.page-head h1 { font-size: 1.35rem; margin-right: auto; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.card-head h2 { margin-right: auto; }
.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat-label { color: var(--ink-3); font-size: 0.8rem; font-weight: 600; }
.stat-value { font-size: 1.35rem; font-weight: 750; margin-top: 2px; }
.stat-sub { font-size: 0.82rem; color: var(--ink-2); }

.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px 14px; }
.kv div { min-width: 0; }
.kv dt, .kv .k { color: var(--ink-3); font-size: 0.78rem; font-weight: 600; }
.kv dd, .kv .v { margin: 0; font-weight: 650; }

/* --- tables --- */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.tbl th { text-align: left; color: var(--ink-3); font-weight: 650; font-size: 0.78rem; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; user-select: none; }
.tbl th.sortable { cursor: pointer; }
.tbl th.sortable:hover { color: var(--ink); }
.tbl th.sorted { color: var(--accent); }
.tbl td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl tr.clickable { cursor: pointer; }
.tbl tr.clickable:hover td { background: var(--surface-2); }
.tbl .num { text-align: right; white-space: nowrap; }
.sym { font-weight: 750; }
.sym-name { color: var(--ink-3); font-size: 0.8rem; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 0.76rem; font-weight: 700; white-space: nowrap; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); }
.badge-strong { background: var(--gain); color: #fff; border-color: var(--gain); }
.badge-buy { background: var(--gain-soft); color: var(--gain); border-color: transparent; }
.badge-watch { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-skip { background: var(--surface-2); color: var(--ink-3); }
.badge-bad { background: var(--loss-soft); color: var(--loss); border-color: transparent; }
.badge-info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.score { display: inline-grid; place-items: center; min-width: 34px; height: 26px; padding: 0 6px; border-radius: 7px; font-weight: 800; font-size: 0.85rem; }

.advice { display: grid; gap: 4px; }
.advice div { font-size: 0.82rem; padding: 3px 8px; border-radius: 6px; }
.advice .good { background: var(--gain-soft); color: var(--gain); }
.advice .info { background: var(--accent-soft); color: var(--accent); }
.advice .warn { background: var(--warn-soft); color: var(--warn); }
.advice .bad { background: var(--loss-soft); color: var(--loss); }

.reasons { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.reasons li { display: flex; gap: 10px; font-size: 0.88rem; align-items: baseline; }
.reasons .pts { min-width: 36px; text-align: right; font-weight: 800; }

.filters { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; margin-bottom: 12px; }
.filters input[type="search"], .filters select { width: auto; min-width: 160px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 11px; font-size: 0.85rem; font-weight: 600; color: var(--ink-2); }
.seg button.on { background: var(--accent); color: #fff; }

.notice { padding: 10px 14px; border-radius: 10px; font-size: 0.9rem; margin-bottom: 14px; }
.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice-info { background: var(--accent-soft); color: var(--ink); }
.notice-bad { background: var(--loss-soft); color: var(--loss); }

.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width 0.4s; }

.empty { text-align: center; color: var(--ink-3); padding: 30px 10px; }

/* --- charts --- */
.chart-box { position: relative; width: 100%; }
.chart-svg { width: 100%; display: block; overflow: visible; }
.chart-svg text { fill: var(--ink-3); font-size: 11px; font-family: var(--font); }
.chart-svg .grid-line { stroke: var(--line); stroke-width: 1; }
.chart-svg .zero-line { stroke: var(--ink-3); stroke-width: 1; }
.chart-tip {
  position: absolute; pointer-events: none; background: var(--ink); color: var(--bg); font-size: 0.8rem;
  padding: 6px 9px; border-radius: 7px; white-space: nowrap; z-index: 5; transform: translate(-50%, -110%); box-shadow: var(--shadow);
}
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.82rem; color: var(--ink-2); margin-bottom: 6px; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 5px; }
#price-chart { height: 380px; }
.range-btns { display: inline-flex; gap: 4px; }

/* --- modal --- */
.modal-back { position: fixed; inset: 0; background: rgba(10, 12, 16, 0.45); z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; width: min(520px, 100%); max-height: calc(100vh - 32px); overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,0.25); }
.modal-head { display: flex; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); gap: 8px; }
.modal-head h2 { margin-right: auto; }
.modal-body { padding: 16px; display: grid; gap: 12px; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.confirm-box { background: var(--surface-2); border-radius: 10px; padding: 12px; font-size: 0.95rem; }

/* --- toast --- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(16px); max-width: calc(100vw - 48px);
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 10px;
  font-size: 0.88rem; font-weight: 600; line-height: 1.35; opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease; z-index: 100; box-shadow: 0 6px 24px rgba(0,0,0,0.2);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: #8a2f14; color: #fff; }
.toast.toast-success { background: #2f6b39; color: #fff; }

/* --- login --- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px; width: min(360px, 100%); display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 1.3rem; }
.login-card label { display: grid; gap: 4px; font-size: 0.85rem; font-weight: 600; color: var(--ink-2); }
.login-error { color: var(--loss); min-height: 1.2em; margin: 0; font-size: 0.88rem; }
.btn-primary.wide, .login-card .btn-primary { padding: 10px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; font-weight: 700; }

pre.prompt { white-space: pre-wrap; background: var(--surface-2); border-radius: 10px; padding: 12px; max-height: 360px; overflow: auto; font-size: 0.8rem; margin: 0; }

/* --- phone --- */
@media (max-width: 720px) {
  .topbar-row { flex-wrap: wrap; padding: 8px 16px; }
  .search-wrap { order: 3; flex-basis: 100%; max-width: none; }
  main { padding: 12px 16px 80px; }
  .stat-value { font-size: 1.15rem; }
  .hide-sm { display: none; }
  .row-2 { grid-template-columns: 1fr; }
  #price-chart { height: 300px; }
  .modal-back { place-items: end center; padding: 0; }
  .modal { width: 100%; border-radius: 14px 14px 0 0; max-height: 92vh; }
  /* positions/trades become cards */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; padding: 6px 10px; background: var(--surface); }
  table.cards td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; gap: 10px; text-align: right; }
  table.cards td::before { content: attr(data-label); color: var(--ink-3); font-size: 0.8rem; font-weight: 600; text-align: left; }
  table.cards td.cell-main { display: block; text-align: left; }
  table.cards td.cell-main::before { content: none; }
  table.cards .advice { text-align: left; }
}

/* ===== Mobile app layer ===== */
.desk-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.bottom-nav { display: none; }
.mobile-only { display: none; }
.scr-cards { display: none; }
.action-bar { display: none; }
.modal { display: flex; flex-direction: column; }
.modal-body { overflow: auto; flex: 1 1 auto; }
.modal-head, .modal-foot { flex: none; }
.chart-svg { touch-action: pan-y; }

@media (max-width: 720px) {
  body { font-size: 15px; }
  /* 16px inputs stop iOS from zooming the page on focus */
  input, select, textarea { font-size: 16px; padding: 10px 12px; }
  .btn { min-height: 42px; padding: 9px 14px; }
  .btn-sm { min-height: 36px; padding: 6px 11px; font-size: 0.86rem; }
  .icon-btn { min-width: 40px; min-height: 40px; display: inline-grid; place-items: center; font-size: 1.15rem; }
  .seg button { padding: 9px 12px; }
  .check { min-height: 36px; }
  .check input[type="checkbox"] { width: 20px; height: 20px; }

  .topbar-row { flex-wrap: nowrap; gap: 10px; padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .brand-text { display: none; }
  .search-wrap { order: 0; flex: 1; }
  .tabs { display: none; }
  .mobile-only { display: block; }
  .hide-mobile { display: none !important; }
  main { padding: 12px 16px calc(96px + env(safe-area-inset-bottom)); }
  .page-head h1 { font-size: 1.25rem; }
  .card { padding: 14px; border-radius: 14px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat-value { font-size: 1.12rem; }
  .kv { grid-template-columns: 1fr 1fr; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }

  .bottom-nav {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a, .bottom-nav button {
    display: grid; justify-items: center; gap: 2px; padding: 7px 2px 6px; color: var(--ink-3); font-size: 0.68rem; font-weight: 650;
    background: none; border: 0; text-decoration: none !important; min-height: 54px; -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav svg { width: 23px; height: 23px; }
  .bottom-nav .active { color: var(--accent); }
  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); }

  /* sheet menu for the secondary sections */
  .sheet-links { display: grid; gap: 4px; }
  .sheet-links a { display: flex; align-items: center; gap: 12px; padding: 14px 10px; border-radius: 10px; color: var(--ink); font-weight: 600; text-decoration: none !important; }
  .sheet-links a:active { background: var(--surface-2); }
  .sheet-links small { display: block; color: var(--ink-3); font-weight: 500; }

  /* screener as cards */
  .scr-table { display: none; }
  .scr-cards { display: grid; gap: 8px; }
  .scr-card { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
  .scr-card .sym { font-size: 1rem; }
  .scr-card .meta { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 0.82rem; color: var(--ink-2); }
  .scr-card .meta b { color: var(--ink); }
  .scr-card .right-col { display: grid; justify-items: end; gap: 4px; }
  .filters-toggle { width: 100%; }
  .filters.collapsed-sm { display: none; }
  .filters { gap: 10px; }
  .filters input[type="search"], .filters select { min-width: 0; width: 100%; }
  .filters .seg { overflow-x: auto; max-width: 100%; }

  /* sticky actions on the asset card */
  .action-bar {
    display: flex; gap: 8px; position: fixed; left: 0; right: 0; z-index: 25;
    bottom: calc(55px + env(safe-area-inset-bottom)); padding: 8px 16px; background: var(--surface); border-top: 1px solid var(--line);
  }
  .action-bar .btn { flex: 1; }
  .action-bar .icon-btn { flex: none; border: 1px solid var(--line); border-radius: 9px; }
  body.has-action-bar main { padding-bottom: calc(150px + env(safe-area-inset-bottom)); }
  body.has-action-bar .toast { bottom: calc(130px + env(safe-area-inset-bottom)); }
  .page-head .desk-actions { display: none; }

  .modal-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1; }
  .modal-body { padding: 14px 16px; }
  #price-chart { height: 320px; }
  .legend { font-size: 0.78rem; }
  .reasons li { font-size: 0.86rem; }
}
