:root {
  --bg-light: #f2f4f7;
  --bg-dark: #0b0e11;
  --surface-light: #ffffff;
  --surface-dark: #141920;
  --field-light: #f0f2f5;
  --field-dark: #1c222b;
  --hover-light: #eceff3;
  --hover-dark: #212833;
  --line-light: #e5e8ed;
  --line-dark: #232a35;
  --text-light: #15181e;
  --text-dark: #edf0f5;
  --muted-light: #6e7787;
  --muted-dark: #848e9c;
  --accent-light: #b88200;
  --accent-dark: #f0b90b;
  --accent-ink-light: #ffffff;
  --accent-ink-dark: #11151b;
  --up-light: #029b60;
  --up-dark: #0ecb81;
  --up-bg-light: rgba(2, 155, 96, 0.1);
  --up-bg-dark: rgba(14, 203, 129, 0.14);
  --down-light: #d93048;
  --down-dark: #f6465d;
  --down-bg-light: rgba(217, 48, 72, 0.1);
  --down-bg-dark: rgba(246, 70, 93, 0.14);

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --field: var(--field-light);
  --hover: var(--hover-light);
  --line: var(--line-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --accent: var(--accent-light);
  --accent-ink: var(--accent-ink-light);
  --up: var(--up-light);
  --up-bg: var(--up-bg-light);
  --down: var(--down-light);
  --down-bg: var(--down-bg-light);
  --target-size: 32px;

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --field: var(--field-dark);
    --hover: var(--hover-dark);
    --line: var(--line-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --accent: var(--accent-dark);
    --accent-ink: var(--accent-ink-dark);
    --up: var(--up-dark);
    --up-bg: var(--up-bg-dark);
    --down: var(--down-dark);
    --down-bg: var(--down-bg-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --field: light-dark(var(--field-light), var(--field-dark));
    --hover: light-dark(var(--hover-light), var(--hover-dark));
    --line: light-dark(var(--line-light), var(--line-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --accent: light-dark(var(--accent-light), var(--accent-dark));
    --accent-ink: light-dark(var(--accent-ink-light), var(--accent-ink-dark));
    --up: light-dark(var(--up-light), var(--up-dark));
    --up-bg: light-dark(var(--up-bg-light), var(--up-bg-dark));
    --down: light-dark(var(--down-light), var(--down-dark));
    --down-bg: light-dark(var(--down-bg-light), var(--down-bg-dark));
  }

  color-scheme: light dark;
}

@media (pointer: coarse) {
  :root {
    --target-size: 44px;
  }
}

html {
  accent-color: var(--accent);
  scroll-padding-top: calc(3.25rem + env(safe-area-inset-top, 0px));
  scroll-padding-bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px));
}

*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  overflow-wrap: break-word;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  text-wrap: balance;
}

p {
  text-wrap: pretty;
}

:where(a, code) {
  overflow-wrap: anywhere;
}

:where(button, select, textarea, input:not([type="checkbox"], [type="radio"]), label:has(input[type="checkbox"])) {
  min-block-size: var(--target-size);
}

/* App Shell: mobile-first with fixed bottom bar; desktop becomes left icon rail + centered column */
.app-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
}

/* Top App Bar */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  min-block-size: 50px;
  padding: env(safe-area-inset-top, 0px) 16px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}

.live-dot.on {
  background: var(--up);
}

.mode-tag {
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  background: var(--field);
  color: var(--accent);
}

.mode-tag.live-danger {
  background: var(--down-bg);
  color: var(--down);
}

.pill-btn {
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.icon-btn {
  width: 34px;
  min-inline-size: var(--target-size);
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  font-size: 16px;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
}

.icon-btn:hover {
  color: var(--text);
  background: var(--hover);
}

/* Views & Tab Panels (no padding/border on tabpanel itself per tabs guidance) */
.views {
  flex: 1;
  display: grid;
}

.views > [role="tabpanel"] {
  grid-area: 1 / 1;
  align-self: start;
  min-width: 0;
  position: relative;
  z-index: 1;
}

.views > [role="tabpanel"][hidden] {
  z-index: 0;
  pointer-events: none;
}

.view-inner {
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 12px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Surface Cards */
.surface {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.label-sm {
  font-size: 12px;
  color: var(--muted);
}

.label-xs {
  font-size: 11px;
  color: var(--muted);
}

.num-sm {
  font-size: 13.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Hero Equity Card */
.hero-card {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hero-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hero-value {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.hero-sub {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.sub-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.quick-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.quick-chip {
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.quick-chip.danger {
  color: var(--down);
  border-color: var(--down);
  background: transparent;
}

/* Trader Watch Card */
.trader-card {
  overflow: hidden;
}

.trader-head {
  padding: 14px 16px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.trader-id-block {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.trader-name-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.trader-name-line strong {
  font-size: 15px;
}

.quiet-link {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
}

.watch-toggle {
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.watch-toggle.running {
  background: var(--up-bg);
  color: var(--up);
  border-color: var(--up);
}

.stats-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 0 16px 12px;
}

.stat-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.alert-line {
  margin: 0 16px 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--down-bg);
  color: var(--down);
  font-size: 12px;
}

.trader-foot {
  display: flex;
  border-top: 1px solid var(--line);
}

.foot-btn {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
}

.foot-btn:hover {
  color: var(--text);
  background: var(--hover);
}

.foot-sep {
  width: 1px;
  background: var(--line);
}

/* Native App Feed List (Zero Horizontal Scroll) */
.feed-section {
  overflow: hidden;
}

.section-bar,
.view-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-bar {
  padding: 12px 16px 10px;
  border-bottom: 1px solid var(--line);
}

.section-bar h2,
.view-bar h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.feed-list {
  display: flex;
  flex-direction: column;
}

.feed-row {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.feed-row:last-child {
  border-bottom: 0;
}

.feed-top,
.feed-bottom {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.feed-sym {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: 14px;
}

.side-chip {
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.side-chip.up { background: var(--up-bg); color: var(--up); }
.side-chip.down { background: var(--down-bg); color: var(--down); }

.feed-notional {
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.feed-meta {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.up { color: var(--up); }
.down { color: var(--down); }

.empty-msg {
  margin: 0;
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* Position Cards (Binance App Style) */
.card-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty-card {
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.empty-card .empty-msg {
  padding: 0;
}

.pos-card {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pos-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.pos-title {
  display: flex;
  align-items: center;
  gap: 6px;
}

.pos-title strong {
  font-size: 15px;
}

.pos-pnl {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.pos-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.pos-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding-top: 4px;
}

/* Grouped iOS / Exchange Settings List */
.settings-stack {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.group-label {
  padding: 0 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.group-box {
  overflow: hidden;
}

.group-pad {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.setting-row {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.setting-row:last-child {
  border-bottom: 0;
}

.setting-row > label,
.toggle-row > span {
  font-size: 13.5px;
  flex: 1;
}

.toggle-row {
  cursor: pointer;
}

.setting-ctrl {
  width: 150px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 3px;
}

.token-ctrl {
  width: auto;
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

.token-ctrl input {
  width: 150px;
}

/* Segmented Control */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border-radius: 8px;
  background: var(--field);
}

.seg-btn {
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Inputs & Validation */
.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  font-size: 12px;
  color: var(--muted);
}

input[type="text"],
input[type="password"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}

@media (pointer: coarse) {
  input[type="text"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px; /* Prevents iOS Safari auto-zoom on focus */
  }
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  resize: vertical;
}

.error-msg {
  display: none;
  font-size: 11px;
  color: var(--down);
}

:is(input, select, textarea):is(:user-invalid, .user-invalid-fallback) {
  border-color: var(--down);
  background: var(--down-bg);
}

:is(input, select, textarea):is(:user-invalid, .user-invalid-fallback) + .error-msg {
  display: block;
}

/* Buttons */
.btn {
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-ghost {
  background: var(--field);
  color: var(--text);
  border-color: var(--line);
}

.btn-danger {
  background: var(--down);
  color: #ffffff;
}

.btn-sm {
  padding: 0 11px;
  font-size: 12.5px;
}

.btn-block {
  width: 100%;
  min-block-size: 44px;
}

.flex-1 {
  flex: 1;
}

/* Native Details Fold */
.fold {
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.fold summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}

.fold summary::-webkit-details-marker {
  display: none;
}

.fold summary::before {
  content: "▸";
  display: inline-block;
  transition: transform 0.15s ease;
}

.fold[open] > summary::before {
  transform: rotate(90deg);
}

.fold::details-content {
  interpolate-size: allow-keywords;
  overflow: clip;
  contain: layout;
}

.fold:not([open])::details-content {
  block-size: 0;
}

@media (prefers-reduced-motion: no-preference) {
  .fold::details-content {
    transition: block-size 0.2s ease, content-visibility 0.2s ease allow-discrete;
  }
}

.fold-body {
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.switch-inline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

/* Fixed Bottom Tab Bar (Mobile App Pattern) */
.app-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 4px max(8px, env(safe-area-inset-right, 0px)) max(6px, env(safe-area-inset-bottom, 0px)) max(8px, env(safe-area-inset-left, 0px));
}

.app-nav-brand {
  display: none;
}

.nav-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  max-width: 680px;
  margin: 0 auto;
}

.nav-item {
  position: relative;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 5px 4px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font: inherit;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
}

.nav-icon {
  width: 21px;
  height: 21px;
}

.nav-item[aria-selected="true"] {
  color: var(--accent);
  font-weight: 600;
}

.nav-item[aria-selected="true"]::after {
  content: "";
  position: absolute;
  top: 0;
  left: 28%;
  right: 28%;
  border-block-start: 2px solid var(--accent);
}

.nav-badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 20px);
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 10px;
  font-weight: 700;
  display: inline-grid;
  place-items: center;
}

/* Desktop Adaptation: slim left icon rail + centered App column */
@media (min-width: 840px) {
  .app-shell {
    flex-direction: row;
  }

  .app-nav {
    position: sticky;
    top: 0;
    left: auto;
    right: auto;
    bottom: auto;
    height: 100dvh;
    width: 78px;
    border-top: 0;
    border-right: 1px solid var(--line);
    padding: 14px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 18px;
  }

  .app-nav-brand {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 800;
    font-size: 15px;
  }

  .nav-tabs {
    grid-template-columns: 1fr;
    width: 100%;
    gap: 8px;
  }

  .nav-item {
    padding: 9px 4px;
  }

  .nav-item[aria-selected="true"]::after {
    top: 22%;
    bottom: 22%;
    left: 0;
    right: auto;
    border-block-start: 0;
    border-inline-start: 3px solid var(--accent);
  }

  .app-main {
    padding-bottom: 24px;
  }
}

/* Native Dialog & Bottom Sheets */
dialog[open] {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(440px, calc(100vw - 24px));
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
}

dialog.sheet[open] {
  margin-block-end: 0;
  margin-block-start: auto;
  width: min(520px, 100vw);
  max-width: 100vw;
  max-height: 86dvh;
  overflow: auto;
  border-radius: 16px 16px 0 0;
  border-bottom: 0;
  padding-bottom: max(18px, env(safe-area-inset-bottom, 0px));
}

@media (min-width: 640px) {
  dialog.sheet[open] {
    margin: auto;
    border-radius: 14px;
    border-bottom: 1px solid var(--line);
    padding-bottom: 18px;
  }
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  overflow: hidden;
  overscroll-behavior: contain;
}

dialog h1 {
  margin: 0;
  font-size: 16px;
}

dialog p {
  margin: 0;
  font-size: 13.5px;
  color: var(--muted);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sheet-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sheet-actions,
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 4px;
}

.preview-summary {
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--field);
  font-size: 12.5px;
  white-space: pre-line;
  font-variant-numeric: tabular-nums;
}

/* Floating Toast above bottom nav */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(74px + env(safe-area-inset-bottom, 0px));
  z-index: 40;
  pointer-events: none;
  max-width: min(360px, calc(100vw - 24px));
  padding: 9px 14px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.toast.error {
  background: var(--down);
  color: #ffffff;
}

@media (min-width: 840px) {
  .toast {
    bottom: 20px;
  }
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}
