:root {
  color-scheme: dark;
  --bg: #0a0d11;
  --surface: #12171e;
  --surface-2: #18202a;
  --surface-3: #202a36;
  --line: #303b49;
  --line-strong: #3d4c5d;
  --text: #e8edf3;
  --muted: #9ca8b6;
  --accent: #39c6ad;
  --accent-dark: #1d6f63;
  --blue: #4c8dff;
  --green: #65d6a9;
  --red: #ff7d89;
  --yellow: #f5bd48;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; overflow-x: hidden; background: var(--bg); color: var(--text); }
body { font-family: Inter, "Segoe UI", "PingFang SC", Arial, sans-serif; letter-spacing: 0; }
button, input, select { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; }
.topbar {
  height: 60px; position: sticky; top: 0; z-index: 50; display: grid; align-items: center;
  grid-template-columns: max-content minmax(250px, 1fr) max-content; gap: 18px; padding: 0 22px; background: #11161d;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { width: 30px; height: 30px; display: grid; place-items: center; background: var(--accent-dark); color: #fff; font-weight: 800; }
.brand h1 { font-size: 18px; margin: 0; white-space: nowrap; }
.top-actions { min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 8px; white-space: nowrap; }
.top-actions > * { flex: 0 0 auto; }
.top-actions .button { white-space: nowrap; }
.top-actions .badge { white-space: nowrap; }
.top-actions select { width: auto; }
.top-user { color: var(--muted); font-size: 13px; white-space: nowrap; }
.top-update { max-width: none; }
.nav { min-width: 0; display: flex; height: 100%; align-items: stretch; justify-content: center; }
.nav a { display: flex; align-items: center; padding: 0 16px; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; }
.nav a.active { color: var(--text); border-color: var(--accent); background: rgba(57, 198, 173, .06); }

.content { width: min(1880px, 100%); margin: 0 auto; padding: 18px 22px 40px; }
.filter-bar {
  position: sticky; top: 60px; z-index: 40; display: grid;
  grid-template-columns: minmax(200px, 1.3fr) minmax(180px, 1fr) repeat(4, minmax(140px, .75fr)) auto;
  gap: 9px; padding: 12px; background: rgba(18, 23, 30, .97); border: 1px solid var(--line);
  border-top: 3px solid var(--accent); box-shadow: 0 10px 24px rgba(0, 0, 0, .32);
}
input, select {
  width: 100%; height: 40px; padding: 0 12px; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line-strong); border-radius: 3px; outline: none;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(57, 198, 173, .13); }
.button { width: auto; min-height: 38px; padding: 0 15px; border: 1px solid var(--line-strong); border-radius: 3px; color: var(--text); background: var(--surface-3); }
.button:hover { border-color: var(--accent); }
.button.primary { border-color: var(--accent); background: var(--accent); color: #081311; font-weight: 700; }
.button.danger { color: var(--red); }
.button.small { min-height: 30px; padding: 0 10px; font-size: 12px; }
.icon-button { width: 38px; height: 38px; padding: 0; display: grid; place-items: center; }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin: 14px 0; }
.stat { min-height: 110px; padding: 16px; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--blue); }
.stat span { color: var(--muted); font-size: 13px; }
.stat strong { display: block; margin-top: 12px; font-size: 26px; overflow-wrap: anywhere; }
.stat small { display: block; margin-top: 6px; color: var(--muted); }
.positive { color: var(--green) !important; }
.negative { color: var(--red) !important; }
.trade-side-buy { color: var(--green); font-weight: 800; }
.trade-side-sell { color: var(--red); font-weight: 800; }
.trade-side-unknown { color: var(--yellow); font-weight: 800; }
.locale-select { width: 124px; min-width: 124px; }

.panel { margin-top: 12px; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--blue); }
.panel-header { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 14px; border-bottom: 1px solid var(--line); }
.panel-header h2 { margin: 0; font-size: 15px; }
.panel-header p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.panel-actions { display: flex; gap: 8px; align-items: center; }
.panel-actions select { width: auto; min-width: 130px; }
.panel-body { padding: 14px; }

.table-wrap { width: 100%; overflow: auto; }
.report-cutoff { display: flex; align-items: center; gap: 12px; margin: 0 0 14px; padding: 10px 13px; border: 1px solid var(--line-strong); background: #151d25; color: var(--muted); }
.report-cutoff span { color: var(--accent); font-weight: 700; }
.report-cutoff strong { color: var(--text); font-weight: 600; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { height: 42px; padding: 0 12px; text-align: left; color: #dce5ee; background: #22303c; white-space: nowrap; }
td { min-height: 45px; padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:hover { background: rgba(76, 141, 255, .05); }
.sort-button { display: inline-flex; gap: 7px; align-items: center; padding: 0; border: 0; background: transparent; color: inherit; font-weight: 700; }
.sort-button span { color: var(--accent); min-width: 10px; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.sub { margin-top: 4px; color: var(--muted); font-size: 11px; }
.exchange { font-weight: 800; }
.exchange-binance { color: #f3ba2f; } .exchange-bybit { color: #f5f7fb; }
.exchange-bitget { color: #28d7d8; } .exchange-gate { color: #3a85ff; }
.empty { padding: 42px 16px; text-align: center; color: var(--muted); }
.badge { display: inline-flex; align-items: center; min-height: 25px; padding: 0 8px; border: 1px solid var(--line-strong); font-size: 12px; }
.badge.ok { color: var(--green); border-color: rgba(101, 214, 169, .45); background: rgba(101, 214, 169, .08); }
.badge.off { color: var(--muted); }
.badge.warn { color: var(--yellow); }

.chart { height: 220px; position: relative; overflow: hidden; background: #111923; border: 1px solid var(--line); }
.chart { touch-action: pan-y; user-select: none; }
.chart svg { width: 100%; height: 100%; display: block; }
.chart .grid { stroke: #263443; stroke-width: 1; }
.chart .line { stroke: var(--blue); stroke-width: 2; fill: none; vector-effect: non-scaling-stroke; }
.chart .area { fill: rgba(76, 141, 255, .1); }
.chart-meta { display: flex; flex-wrap: wrap; gap: 10px 24px; margin-bottom: 10px; color: var(--muted); font-size: 12px; }

.login-layer { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #090c10; }
.login-card { width: min(430px, 100%); padding: 26px; background: var(--surface); border: 1px solid var(--line); border-top: 3px solid var(--accent); }
.login-card h1 { margin: 0 0 8px; font-size: 23px; }
.login-card p { margin: 0 0 22px; color: var(--muted); line-height: 1.6; }
.form-field { display: grid; gap: 7px; margin-top: 13px; }
.form-field label { color: var(--muted); font-size: 12px; }
.login-card .button { width: 100%; margin-top: 18px; }
.error-box { margin-top: 13px; padding: 10px; border: 1px solid rgba(255, 125, 137, .4); color: var(--red); background: rgba(255, 125, 137, .07); font-size: 13px; }

.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 24px; background: rgba(0, 0, 0, .68); }
.modal { width: min(840px, 100%); max-height: min(820px, calc(100vh - 48px)); overflow: auto; background: #121820; border: 1px solid var(--line-strong); }
.modal-header { position: sticky; top: 0; z-index: 2; display: flex; justify-content: space-between; align-items: center; padding: 16px 18px; background: #1a222c; border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 18px; }
.modal-footer { position: sticky; bottom: 0; display: flex; justify-content: flex-end; gap: 8px; padding: 13px 18px; background: #161d26; border-top: 1px solid var(--line); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 13px; }
.account-identity-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.account-filter-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.symbol-picker-field { align-content: start; }
.selected-symbols { min-height: 25px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.symbol-chip { min-height: 25px; display: inline-flex; align-items: center; gap: 7px; padding: 0 8px; border: 1px solid rgba(57, 198, 173, .5); background: rgba(57, 198, 173, .1); color: var(--accent); }
.symbol-chip span { font-size: 16px; line-height: 1; }
.scope-list { margin-top: 14px; border: 1px solid var(--line); max-height: 340px; overflow: auto; }
.scope-row { display: grid; grid-template-columns: 34px minmax(180px, 1fr) 130px 120px 170px; align-items: center; gap: 10px; min-height: 48px; padding: 7px 10px; border-bottom: 1px solid var(--line); }
.scope-row[hidden], .scope-list .empty[hidden] { display: none; }
.scope-row input { width: 18px; height: 18px; }

.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--line); }
.tabs button { width: auto; height: 42px; padding: 0 16px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); }
.tabs button.active { color: var(--text); border-color: var(--accent); }
.record-message { max-width: 480px; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); }
.account-detail-button { width: auto; height: auto; padding: 0; border: 0; background: transparent; color: var(--text); font-weight: 700; text-decoration: underline; text-decoration-color: var(--accent-dark); text-underline-offset: 4px; }
.account-detail-button:hover { color: var(--accent); }
.audit-modal { width: min(1480px, 100%); }
.audit-table { max-height: min(620px, calc(100vh - 220px)); }
.loading-line { height: 2px; position: fixed; top: 60px; left: 0; right: 0; z-index: 90; overflow: hidden; }
.loading-line::after { content: ""; display: block; width: 35%; height: 100%; background: var(--accent); animation: loading 1s infinite ease-in-out; }
.pwa-help { width: min(520px, 100%); }
.pwa-help p { margin: 0; color: var(--muted); line-height: 1.7; }
@keyframes loading { from { transform: translateX(-100%); } to { transform: translateX(390%); } }

@media (max-width: 1180px) {
  .topbar {
    height: auto;
    grid-template-columns: max-content minmax(0, 1fr);
    grid-template-rows: 58px 42px;
  }
  .brand { grid-column: 1; grid-row: 1; }
  .top-actions { grid-column: 2; grid-row: 1; overflow-x: auto; scrollbar-width: none; }
  .top-actions::-webkit-scrollbar { display: none; }
  .nav { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; border-top: 1px solid rgba(48, 59, 73, .55); overflow-x: auto; }
  .filter-bar { grid-template-columns: repeat(3, minmax(150px, 1fr)); }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .nav a { padding: 0 10px; }
}
@media (max-width: 720px) {
  .topbar {
    min-height: 0;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 48px 40px 52px;
    gap: 0;
    padding: 0 12px;
  }
  .brand { grid-column: 1; grid-row: 1; }
  .brand-mark { width: 28px; height: 28px; }
  .brand h1 { font-size: 16px; overflow: hidden; text-overflow: ellipsis; }
  .nav { grid-column: 1; grid-row: 2; width: 100%; height: 40px; }
  .nav a { flex: 1 0 auto; justify-content: center; padding: 0 12px; }
  .top-actions {
    grid-column: 1;
    grid-row: 3;
    width: 100%;
    justify-content: flex-start;
    overflow-x: auto;
    padding: 7px 0;
    border-top: 1px solid rgba(48, 59, 73, .55);
    scrollbar-width: none;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions .button { min-height: 34px; }
  .top-update { min-height: 34px; }
  .locale-select { width: 112px; min-width: 112px; height: 34px; }
  .content { padding: 10px 10px 28px; }
  .filter-bar { position: static; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding: 10px; }
  .filter-bar input { min-width: 0; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .stat { min-height: 118px; padding: 13px; }
  .stat strong { margin-top: 10px; font-size: 20px; }
  .panel { margin-top: 10px; }
  .panel-header { align-items: flex-start; padding: 11px; }
  .panel-header .panel-actions { max-width: 55%; flex-wrap: wrap; justify-content: flex-end; }
  .panel-actions select { min-width: 108px; }
  .panel-body { padding: 10px; }
  .table-wrap { overscroll-behavior-x: contain; scrollbar-width: thin; }
  .table-wrap > table:not(.mobile-card-table) { min-width: 820px; }
  .chart { height: 190px; }
  .chart-meta { gap: 7px 14px; }
  .tabs { overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
  .tabs button { flex: 0 0 auto; padding: 0 13px; }
  .mobile-card-table, .mobile-card-table tbody { display: block; width: 100%; }
  .mobile-card-table thead { display: none; }
  .mobile-card-table tbody { padding: 8px; }
  .mobile-card-table tr { display: grid; gap: 0; margin-bottom: 9px; padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--line); }
  .mobile-card-table td { min-height: 0; display: grid; grid-template-columns: minmax(92px, .7fr) minmax(0, 1.3fr); gap: 10px; padding: 8px 0; border-bottom: 1px solid rgba(48, 59, 73, .65); overflow-wrap: anywhere; }
  .mobile-card-table td:last-child { border-bottom: 0; }
  .mobile-card-table td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; }
  .mobile-card-table td.empty { display: block; text-align: center; }
  .mobile-card-table td.empty::before { content: none; }
  .record-message { max-width: none; }
  .form-grid, .account-identity-grid { grid-template-columns: 1fr; }
  .account-filter-grid { grid-template-columns: 1fr; }
  .scope-row { grid-template-columns: 28px 1fr; }
  .scope-row > :nth-child(n+3) { display: none; }
  .modal-backdrop { padding: 0; }
  .modal { width: 100%; max-height: 100svh; min-height: 100svh; border: 0; }
  .modal-header { padding: 14px; }
  .modal-body { padding: 14px; }
  .modal-footer { padding: 12px 14px max(12px, env(safe-area-inset-bottom)); }
}

@media (max-width: 420px) {
  .filter-bar { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat { min-height: 108px; }
  .stat span, .stat small { font-size: 11px; }
  .stat strong { font-size: 18px; }
  .panel-header { flex-direction: column; }
  .panel-header .panel-actions { width: 100%; max-width: none; justify-content: flex-start; }
}
.record-pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  color: var(--muted);
}

.record-pager > div {
  display: flex;
  gap: 8px;
}

.audit-modal-footer {
  justify-content: space-between;
}

@media (max-width: 680px) {
  .record-pager {
    align-items: stretch;
    flex-direction: column;
  }

  .record-pager > div .button {
    flex: 1;
  }
}
