:root {
  color-scheme: light dark;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --raised: #ffffff;
  --line: #e4e3df;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --up-line: #0ca30c;
  --down-line: #d03b3b;
  --up-text: #006300;
  --down-text: #c03030;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --live: #d03b3b;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 56px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #0d0d0d;
    --surface: #1a1a19;
    --raised: #242422;
    --line: #2e2e2b;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --ink-3: #8f8e86;
    --accent: #3987e5;
    --up-line: #0ca30c;
    --down-line: #d03b3b;
    --up-text: #2fbf2f;
    --down-text: #e66767;
    --warn-bg: #3a2e0a;
    --warn-ink: #fad67a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--page); color: var(--ink); }
body { overscroll-behavior-y: none; -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; }
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); }
.error { color: var(--down-text); min-height: 1.2em; font-size: 14px; }
.tnum { font-variant-numeric: tabular-nums; }
.up { color: var(--up-text); }
.down { color: var(--down-text); }
.spacer { width: 60px; }

/* ── Login ───────────────────────────────────────────── */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-logo { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 4px; }
.login-card p { margin: 0; }
.login-card input { text-align: center; letter-spacing: .2em; }

/* ── Inputs & buttons ────────────────────────────────── */
input, select {
  width: 100%; font-size: 17px; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--raised); outline: none;
}
input:focus, select:focus { border-color: var(--accent); }
.btn {
  border: 0; border-radius: 12px; padding: 14px 18px; font-size: 17px; font-weight: 600;
  background: var(--raised); color: var(--ink); border: 1px solid var(--line);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { background: var(--down-line); color: #fff; border-color: transparent; }
.btn.secondary { background: var(--ink); color: var(--page); border-color: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btn.small { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.icon-btn { background: none; border: 0; padding: 8px; color: var(--ink); display: grid; place-items: center; min-width: 44px; min-height: 44px; }
.link-btn { background: none; border: 0; color: var(--accent); font-size: 17px; padding: 10px 4px; min-height: 44px; }

.seg { display: flex; background: var(--raised); border: 1px solid var(--line); border-radius: 10px; padding: 2px; gap: 2px; }
.seg button { flex: 1; border: 0; background: none; padding: 7px 4px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--ink-2); min-height: 34px; }
.seg button.active { background: var(--accent); color: var(--accent-ink); }

/* ── App chrome ──────────────────────────────────────── */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  padding: calc(var(--safe-top) + 4px) 12px 4px 16px;
  display: flex; align-items: center; justify-content: space-between;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.pill { font-size: 11px; font-weight: 800; letter-spacing: .08em; padding: 3px 8px; border-radius: 999px; }
.pill.paper { background: var(--raised); color: var(--ink-2); border: 1px solid var(--line); }
.pill.live { background: var(--live); color: #fff; }
.market { font-size: 13px; color: var(--ink-2); }
.market .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; background: var(--ink-3); vertical-align: 1px; }
.market.open .dot { background: var(--up-line); }

main { padding: calc(var(--safe-top) + 60px) 16px calc(var(--safe-bottom) + var(--tabbar-h) + 24px); max-width: 640px; margin: 0 auto; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex;
  padding-bottom: var(--safe-bottom); border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--page) 90%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
}
.tabbar button { flex: 1; background: none; border: 0; height: var(--tabbar-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--ink-3); font-size: 10px; font-weight: 600; }
.tabbar svg { width: 24px; height: 24px; }
.tabbar button.active { color: var(--accent); }

/* ── Hero & chart ────────────────────────────────────── */
.hero { margin: 4px 0 8px; }
.hero-label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.hero-value { font-size: 38px; font-weight: 700; letter-spacing: -.02em; margin: 2px 0; }
.hero-change { font-size: 15px; font-weight: 600; }
.hero-change .when { color: var(--ink-2); font-weight: 500; }

.chart { position: relative; height: 200px; margin: 8px -16px 4px; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-3); font-size: 14px; }
.chart-time { position: absolute; top: -2px; font-size: 11px; color: var(--ink-2); transform: translateX(-50%); white-space: nowrap; pointer-events: none; }
.ranges { margin: 4px 0 16px; }

/* ── Lists & cards ───────────────────────────────────── */
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 24px 0 8px; }
.section-title h2 { font-size: 20px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--line); }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%; background: none; border: 0; text-align: left; min-height: 60px; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 700; font-size: 16px; }
.row-sub { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-end { text-align: right; flex-shrink: 0; }
.row-value { font-weight: 600; font-size: 16px; }
.chip { display: inline-block; font-size: 13px; font-weight: 700; padding: 5px 8px; border-radius: 8px; min-width: 76px; text-align: center; background: var(--raised); border: 1px solid var(--line); }
.empty-state { padding: 28px 16px; text-align: center; color: var(--ink-2); font-size: 15px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }
.stat { background: var(--surface); padding: 12px 14px; }
.stat-label { font-size: 12px; color: var(--ink-2); }
.stat-value { font-size: 17px; font-weight: 600; margin-top: 2px; }

.kv { display: flex; justify-content: space-between; gap: 12px; padding: 12px 14px; font-size: 15px; }
.kv + .kv { border-top: 1px solid var(--line); }
.kv .k { color: var(--ink-2); }
.kv .v { font-weight: 600; text-align: right; }

.status { font-size: 12px; font-weight: 700; text-transform: capitalize; }
.status.filled { color: var(--up-text); }
.status.canceled, .status.expired, .status.rejected { color: var(--ink-3); }

.banner { background: var(--warn-bg); color: var(--warn-ink); border-radius: 12px; padding: 10px 12px; font-size: 14px; margin: 8px 0; }
.banner.live { background: var(--live); color: #fff; font-weight: 600; }

/* ── Sheets ──────────────────────────────────────────── */
.sheet {
  position: fixed; inset: 0; z-index: 20; background: var(--page);
  display: flex; flex-direction: column; animation: slide-up .22s ease-out;
}
@keyframes slide-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: calc(var(--safe-top) + 4px) 12px 4px; }
.sheet-head input { flex: 1; }
.sheet-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px 24px; max-width: 640px; width: 100%; margin: 0 auto; }
.sheet-foot { display: flex; gap: 12px; padding: 12px 16px calc(var(--safe-bottom) + 12px); border-top: 1px solid var(--line); background: var(--page); }
.sheet-foot .btn { flex: 1; }

.detail-name { font-size: 15px; color: var(--ink-2); margin-top: 2px; }

/* ── Order ticket ────────────────────────────────────── */
.field { margin: 14px 0; }
.field > label, .field-label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 10px; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; }
.toggle input { width: 51px; height: 31px; appearance: none; -webkit-appearance: none; border-radius: 999px; background: var(--line); position: relative; padding: 0; border: 0; transition: background .15s; }
.toggle input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.toggle input:checked { background: var(--up-line); }
.toggle input:checked::after { transform: translateX(20px); }
.toggle input:disabled { opacity: .4; }
.hint { font-size: 13px; color: var(--ink-2); margin-top: 6px; }
.summary { margin: 18px 0; }
.review-title { font-size: 24px; font-weight: 700; margin: 8px 0 4px; }

/* ── Toast ───────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 16px);
  background: var(--ink); color: var(--page); padding: 12px 16px; border-radius: 12px;
  font-size: 15px; font-weight: 600; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.25);
}
.toast.error { background: var(--down-line); color: #fff; }

.spinner { width: 22px; height: 22px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 24px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
