/* ── Tokens ─────────────────────────────────────────── */
:root {
  color-scheme: dark;
  --bg: #0b0e14;
  --surface: #121722;
  --surface-2: #182030;
  --surface-3: #1f293b;
  --border: #253046;
  --text: #e6e9ef;
  --muted: #8b95a7;
  --accent: #4c8dff;
  --accent-text: #ffffff;
  --up: #16a394;
  --down: #ef5350;
  --warn: #f5b942;
  --vwap: #f5b942;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgb(0 0 0 / .35);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f3f5f9;
    --surface: #ffffff;
    --surface-2: #f2f4f8;
    --surface-3: #e8ecf3;
    --border: #dde3ec;
    --text: #111827;
    --muted: #5b6577;
    --accent: #2563eb;
    --up: #0d8f82;
    --down: #dc2626;
    --warn: #b7791f;
    --vwap: #d69e2e;
    --shadow: 0 8px 24px rgb(15 23 42 / .12);
  }
}

/* ── Base ───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; font-weight: 600; }
h3 { font-size: .92rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.up { color: var(--up); }
.down { color: var(--down); }
.warn { color: var(--warn); }
.small { font-size: .82rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ── App shell: web view ────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: 236px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "side top" "side main";
  min-height: 100dvh;
}
.sidebar {
  grid-area: side;
  position: sticky; top: 0; height: 100dvh;
  display: flex; flex-direction: column; gap: 8px;
  padding: 16px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 4px 8px 16px; }
.side-nav { display: flex; flex-direction: column; gap: 2px; }
.side-nav a, .tabbar a {
  display: flex; align-items: center; gap: 12px;
  color: var(--muted); text-decoration: none;
  border-radius: var(--radius-sm);
}
.side-nav a { padding: 10px 12px; font-weight: 500; }
.side-nav a:hover { background: var(--surface-2); color: var(--text); }
.side-nav a[aria-current="page"] { background: var(--surface-3); color: var(--text); }
.side-nav svg, .tabbar svg { width: 20px; height: 20px; flex: none; }
.side-foot { margin-top: auto; }
.user-chip { font-size: .85rem; color: var(--muted); padding: 8px 12px; overflow-wrap: anywhere; }

.topbar {
  grid-area: 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) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-logo { display: none; }
.topbar h1 { font-size: 1.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.view { grid-area: main; padding: 24px; min-width: 0; outline: none; }
.tabbar { display: none; }

/* Narrow desktop window in web view: icon-only sidebar */
@media (width < 900px) {
  html[data-view="web"] .app { grid-template-columns: 64px 1fr; }
  html[data-view="web"] .brand span, html[data-view="web"] .side-nav span, html[data-view="web"] .user-chip { display: none; }
  html[data-view="web"] .side-nav a { justify-content: center; }
  html[data-view="web"] .view { padding: 16px; }
}

/* ── App shell: mobile view ─────────────────────────── */
html[data-view="mobile"] body { background: var(--bg); }
html[data-view="mobile"] .app {
  display: block;
  max-width: 480px;
  margin-inline: auto;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b));
}
html[data-view="mobile"][data-phone="0"] .app {
  /* Mobile view on a desktop browser: show it framed like a phone */
  border-inline: 1px solid var(--border);
  box-shadow: var(--shadow);
}
html[data-view="mobile"] .sidebar { display: none; }
html[data-view="mobile"] .topbar { padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); }
html[data-view="mobile"] .topbar-logo { display: block; }
html[data-view="mobile"] .topbar h1 { font-size: 1.05rem; }
html[data-view="mobile"] .view { padding: 12px 12px 20px; }
html[data-view="mobile"] .tabbar {
  display: grid;
  grid-auto-flow: column; grid-auto-columns: 1fr;
  position: fixed; bottom: 0; z-index: 20;
  left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
html[data-view="mobile"] .tabbar a {
  flex-direction: column; justify-content: center; gap: 3px;
  font-size: .7rem; font-weight: 500; border-radius: 0;
  min-height: 44px;
}
html[data-view="mobile"] .tabbar a[aria-current="page"] { color: var(--accent); }
html[data-phone="1"] .view-switch { display: none; }

/* ── View switch / toggles ──────────────────────────── */
.view-switch { display: inline-flex; align-items: center; gap: 8px; font-size: .8rem; color: var(--muted); cursor: pointer; user-select: none; }
input[type="checkbox"][role="switch"] {
  appearance: none; -webkit-appearance: none;
  position: relative; flex: none;
  width: 40px; height: 24px; margin: 0;
  border-radius: 999px;
  background: var(--surface-3);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background .15s;
}
input[type="checkbox"][role="switch"]::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--text);
  transition: translate .15s;
}
input[type="checkbox"][role="switch"]:checked { background: var(--accent); border-color: var(--accent); }
input[type="checkbox"][role="switch"]:checked::after { translate: 16px 0; background: #fff; }
input[type="checkbox"][role="switch"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Building blocks ───────────────────────────────── */
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 360px), 1fr)); }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card > header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
html[data-view="mobile"] .card { padding: 14px; }
html[data-view="mobile"] .cols-2 { grid-template-columns: 1fr; }
html[data-view="mobile"] .stack { gap: 12px; }

.kpis { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
html[data-view="mobile"] .kpis { grid-template-columns: repeat(2, 1fr); }
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
}
.kpi .label { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font: 600 1.25rem/1.3 var(--mono); font-variant-numeric: tabular-nums; margin-top: 4px; overflow-wrap: anywhere; }
.kpi .sub { font-size: .78rem; color: var(--muted); margin-top: 2px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-size: .75rem; font-weight: 600; letter-spacing: .03em;
  background: var(--surface-3); color: var(--muted);
  white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.on { color: var(--up); background: color-mix(in srgb, var(--up) 15%, transparent); }
.pill.off { color: var(--muted); }
.pill.bad { color: var(--down); background: color-mix(in srgb, var(--down) 15%, transparent); }
.pill.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  font-size: .72rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
  background: var(--surface-3); color: var(--muted);
}
.badge.live { background: var(--down); color: #fff; }
.badge.testnet { background: color-mix(in srgb, var(--warn) 25%, transparent); color: var(--warn); }
.badge.paper { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.badge.long { background: color-mix(in srgb, var(--up) 18%, transparent); color: var(--up); }
.badge.short { background: color-mix(in srgb, var(--down) 18%, transparent); color: var(--down); }

/* ── Buttons & forms ───────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600; cursor: pointer;
  text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--surface-3); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.good { background: var(--up); border-color: var(--up); color: #fff; }
.btn.danger { background: var(--down); border-color: var(--down); color: #fff; }
.btn.ghost { background: transparent; }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn.block { width: 100%; }
html[data-view="mobile"] .btn { min-height: 44px; }
html[data-view="mobile"] .btn.sm { min-height: 36px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface-2); cursor: pointer;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: .85rem; font-weight: 500; }
.field .help { font-size: .78rem; color: var(--muted); }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; }
.field.inline > div { min-width: 0; }
input:not([type="checkbox"]), select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
}
html[data-view="mobile"] input:not([type="checkbox"]), html[data-view="mobile"] select { min-height: 44px; font-size: 16px; }
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input:user-invalid { border-color: var(--down); }
.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr)); }
html[data-view="mobile"] .form-grid { grid-template-columns: 1fr; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; }

.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border);
  overflow-x: auto; max-width: 100%;
  scrollbar-width: none;
}
.seg button {
  border: 0; background: transparent; padding: 6px 12px; border-radius: 6px;
  color: var(--muted); font-weight: 600; font-size: .85rem; cursor: pointer; white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--surface-3); color: var(--text); }
html[data-view="mobile"] .seg button { padding: 8px 12px; }

details.disclosure > summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
details.disclosure[open] > summary { margin-bottom: 12px; }

/* ── Tables (stack into cards when narrow) ─────────── */
.table-wrap {
  overflow: auto;
  max-block-size: min(640px, 75vh);
  container-type: inline-size;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
table.data { width: 100%; border-collapse: separate; border-spacing: 0; font-size: .87rem; }
table.data th, table.data td { padding: 9px 12px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border); }
table.data thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2); color: var(--muted);
  font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.r, table.data th.r { text-align: right; }
table.data tbody th { font-weight: 600; background: var(--surface); position: sticky; left: 0; }
table.data tbody tr:hover th { background: var(--surface-2); }
table.data tbody th > * + * { margin-left: 8px; }
.empty { padding: 24px; text-align: center; color: var(--muted); }

@container (width < 640px) {
  table.data, table.data thead, table.data tbody, table.data tr, table.data th, table.data td { display: block; }
  table.data thead { display: none; }
  table.data tr { padding: 4px 0; border-bottom: 1px solid var(--border); }
  table.data tbody th { position: static; border: 0; padding: 10px 14px 4px; display: flex; justify-content: space-between; gap: 8px; background: transparent; }
  table.data td {
    display: flex; justify-content: space-between; gap: 12px;
    border: 0; padding: 3px 14px; white-space: normal; text-align: right;
  }
  table.data td::before {
    content: attr(data-label);
    content: attr(data-label) / attr(data-label);
    color: var(--muted); text-align: left; font-family: var(--font);
  }
  table.data td.r { text-align: right; }
}

/* ── Charts ────────────────────────────────────────── */
.chart-box { position: relative; height: 420px; min-height: 240px; }
.chart-box.tall { height: calc(100dvh - 230px); min-height: 360px; }
html[data-view="mobile"] .chart-box.tall { height: calc(100dvh - 300px - var(--tabbar-h)); min-height: 320px; }
html[data-view="mobile"] .chart-box { height: 260px; }
.chart-legend {
  position: absolute; top: 8px; left: 10px; z-index: 3; pointer-events: none;
  font: .78rem/1.4 var(--mono); color: var(--muted);
  display: flex; gap: 10px; flex-wrap: wrap; max-width: calc(100% - 80px);
}
.chart-legend b { color: var(--text); font-weight: 600; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar select { width: auto; min-width: 110px; }
html[data-view="mobile"] .toolbar select { flex: 1 1 40%; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: .8rem; cursor: pointer; user-select: none;
}
.chip input { margin: 0; accent-color: var(--accent); }

/* ── Symbol cards ──────────────────────────────────── */
.sym-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); }
.sym-card .price { font: 600 1.6rem/1.2 var(--mono); }
.dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; font-size: .87rem; margin: 10px 0 0; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ── Events ────────────────────────────────────────── */
.events { list-style: none; margin: 0; padding: 0; font-size: .85rem; }
.events li { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.events li:last-child { border-bottom: 0; }
.events time { color: var(--muted); font-family: var(--mono); font-size: .78rem; white-space: nowrap; }
.events .msg { overflow-wrap: anywhere; }
.events .lvl-error .msg { color: var(--down); }
.events .lvl-warning .msg { color: var(--warn); }
.events .lvl-trade .msg { color: var(--text); font-weight: 600; }
html[data-view="mobile"] .events li { grid-template-columns: 1fr; }

/* ── Auth ──────────────────────────────────────────── */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(400px, 100%); }
.auth-card .brand { justify-content: center; font-size: 1.2rem; padding-bottom: 20px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.error-text { color: var(--down); font-size: .87rem; min-height: 1.2em; }

/* ── Dialog & toast ────────────────────────────────── */
.dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 0; width: min(440px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog.wide { width: min(760px, calc(100vw - 32px)); }
.dialog-body h3 { margin-top: 4px; }
.dialog { max-height: calc(100dvh - 32px); }
.dialog::backdrop { background: rgb(0 0 0 / .55); }
.dialog-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-body p { margin: 0; color: var(--muted); }
.toast-wrap {
  position: fixed; z-index: 100; left: 50%; translate: -50% 0;
  bottom: calc(24px + var(--safe-b));
  display: flex; flex-direction: column; gap: 8px; align-items: center;
  width: min(440px, calc(100vw - 32px));
}
html[data-view="mobile"] .toast-wrap { bottom: calc(var(--tabbar-h) + 12px + var(--safe-b)); }
.toast {
  background: var(--surface-3); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: var(--radius-sm); box-shadow: var(--shadow);
  font-size: .9rem; width: 100%;
}
.toast.error { border-color: var(--down); }
.toast.ok { border-color: var(--up); }
.copy-row { display: flex; gap: 8px; }
.copy-row input { font-family: var(--mono); font-size: .8rem; }

.banner {
  padding: 10px 14px; border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--warn) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
  font-size: .87rem;
}
.banner.danger {
  background: color-mix(in srgb, var(--down) 14%, transparent);
  border-color: color-mix(in srgb, var(--down) 45%, transparent);
}
.skeleton { color: var(--muted); padding: 24px; text-align: center; }
