:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --line: #dfe5ec;
  --line-strong: #c8d0da;
  --ink: #22303f;
  --ink-soft: #6b7a8c;
  --ink-faint: #94a2b2;
  --brand: #1f5d92;
  --brand-soft: #e8f2fb;
  --banner: #dceefb;
  --header-yellow: #ffe6a3;
  --header-yellow-deep: #f6d37a;
  --orange: #f28c28;
  --pink: #fbe9ec;
  --pink-head: #f7dce1;
  --mint: #e2f4ec;
  --mint-head: #cfecdf;
  --cream: #fff6ce;
  --cream-head: #faedb2;
  --lavender: #eeeafb;
  --sky: #e9f2fb;
  --up: #c0392b;
  --down: #16836b;
  --danger: #d14343;
  --shadow: 0 1px 2px rgba(31, 60, 90, 0.05), 0 8px 24px rgba(31, 60, 90, 0.05);
  --radius: 10px;
  --mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }

.app-shell { max-width: 1680px; margin: 0 auto; padding: 18px 22px 28px; }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(145deg, #2a6ea8, #164b78); color: #fff; font-size: 16px; font-weight: 700;
  box-shadow: 0 4px 12px rgba(31, 93, 146, 0.22);
}
.brand-name { font-size: 15px; font-weight: 700; letter-spacing: 0.3px; }
.brand-sub { font-size: 10px; color: var(--ink-faint); letter-spacing: 1.1px; margin-top: 2px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line-strong);
  background: var(--surface); display: inline-flex; align-items: center; gap: 7px; font-weight: 600;
  transition: background .16s, border-color .16s, transform .16s;
}
.btn:hover { background: #f6f9fc; border-color: #aab9c9; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: #174b76; border-color: #174b76; }
.btn-ghost { background: #fff; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }

.spinner { width: 13px; height: 13px; border: 2px solid rgba(255,255,255,.45); border-top-color: #fff; border-radius: 50%; display: none; animation: spin .7s linear infinite; }
.btn.loading .spinner { display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

.switch { display: inline-flex; align-items: center; gap: 7px; color: var(--ink-soft); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track { width: 30px; height: 17px; background: #ccd6e0; border-radius: 10px; padding: 2px; transition: background .18s; }
.switch-thumb { display: block; width: 13px; height: 13px; background: #fff; border-radius: 50%; transition: transform .18s; box-shadow: 0 1px 2px rgba(0,0,0,.18); }
.switch input:checked + .switch-track { background: var(--brand); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(13px); }
.switch-label { font-size: 12px; }

/* ---------- banner ---------- */
.banner {
  background: var(--banner); border: 1px solid #cfe3f2; border-radius: var(--radius);
  display: grid; grid-template-columns: 168px 1fr 218px; min-height: 106px; overflow: hidden;
  box-shadow: var(--shadow);
}
.fx-panel { border-right: 1px solid #cfe3f2; background: rgba(255,255,255,.34); }
.fx-row { display: flex; height: 35px; border-bottom: 1px solid #d8e8f4; }
.fx-row:last-child { border-bottom: 0; }
.fx-label { width: 92px; background: var(--orange); color: #fff; font-weight: 700; display: flex; align-items: center; padding-left: 10px; font-size: 12px; }
.fx-value { flex: 1; display: flex; align-items: center; justify-content: center; color: #125fa8; font-family: var(--mono); font-weight: 700; background: rgba(255,255,255,.72); }
.banner-title { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 16px; text-align: center; }
.banner-title h1 { font-size: 25px; letter-spacing: 3px; font-weight: 600; color: #1c2b38; }
.banner-title p { font-size: 11px; color: #54738e; margin-top: 8px; line-height: 1.5; }
.status-panel { border-left: 1px solid #cfe3f2; background: rgba(255,255,255,.46); padding: 18px 16px; display: flex; flex-direction: column; justify-content: center; gap: 7px; }
.status-line { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 13px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ink-faint); box-shadow: 0 0 0 3px rgba(148,162,178,.16); }
.status-dot.ok { background: #1a9e78; box-shadow: 0 0 0 3px rgba(26,158,120,.16); }
.status-dot.warn { background: #e6a23c; box-shadow: 0 0 0 3px rgba(230,162,60,.16); }
.status-dot.error { background: var(--danger); box-shadow: 0 0 0 3px rgba(209,67,67,.16); }
.status-meta { font-size: 11px; color: #5d7690; font-family: var(--mono); }

/* ---------- platform nav ---------- */
.platform-nav { display: flex; gap: 6px; margin: 14px 0 12px; flex-wrap: wrap; }
.platform-tab {
  border: 1px solid var(--line-strong); background: #fff; border-radius: 8px; padding: 9px 16px;
  font-weight: 600; color: var(--ink-soft); display: flex; align-items: center; gap: 8px; transition: all .16s;
}
.platform-tab:hover { border-color: #9fb9d0; color: var(--brand); }
.platform-tab.active { background: var(--brand); border-color: var(--brand); color: #fff; box-shadow: 0 4px 12px rgba(31,93,146,.18); }
.platform-tab .tab-code { font-family: var(--mono); font-size: 10px; opacity: .72; }

/* ---------- kpi ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 14px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi-card::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent, var(--brand)); }
.kpi-label { color: var(--ink-soft); font-size: 11px; display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.kpi-value { font-family: var(--mono); font-size: 20px; font-weight: 700; margin-top: 8px; letter-spacing: -.4px; white-space: nowrap; }
.kpi-foot { font-size: 10px; color: var(--ink-faint); margin-top: 5px; }
.kpi-card.is-loading .kpi-value { color: var(--ink-faint); }

/* ---------- charts ---------- */
.charts-grid { display: grid; grid-template-columns: 1.55fr 1fr 1fr; gap: 12px; margin-bottom: 12px; align-items: stretch; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.chart-card { min-width: 0; display: flex; flex-direction: column; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; padding: 14px 16px 0; }
.card-head h2 { font-size: 13px; font-weight: 700; }
.card-sub { font-size: 10px; color: var(--ink-faint); margin-top: 4px; }
.chart-body { flex: 1; padding: 10px 12px 12px; min-height: 210px; }
.chart-body svg { display: block; width: 100%; height: 100%; }
.chart-empty { height: 100%; min-height: 180px; display: grid; place-items: center; color: var(--ink-faint); font-size: 12px; text-align: center; padding: 20px; }
.segmented { display: flex; border: 1px solid var(--line-strong); border-radius: 7px; overflow: hidden; flex-shrink: 0; }
.segmented button { border: 0; background: #fff; padding: 5px 9px; font-size: 11px; color: var(--ink-soft); }
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button.active { background: var(--brand-soft); color: var(--brand); font-weight: 700; }

/* ---------- table ---------- */
.table-card { overflow: hidden; }
.table-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 8px; }
.toolbar-left h2 { font-size: 14px; }
.chip { background: var(--brand-soft); color: var(--brand); border-radius: 20px; padding: 3px 9px; font-size: 10px; font-weight: 700; }
.chip-muted { background: #eef1f5; color: var(--ink-soft); }
.hint { font-size: 10px; color: var(--ink-faint); }
.table-scroll { overflow: auto; max-height: 560px; background: #fff; position: relative; }
.portfolio-table { border-collapse: collapse; width: max-content; min-width: 100%; font-size: 11px; table-layout: fixed; }
.portfolio-table th, .portfolio-table td { border: 1px solid #cbd2d9; padding: 0 7px; height: 29px; text-align: right; white-space: nowrap; }
.portfolio-table th {
  background: linear-gradient(#fff0c4, var(--header-yellow-deep)); color: #4a4028; font-weight: 700;
  text-align: center; height: 38px; position: sticky; top: 0; z-index: 3; line-height: 1.25;
}
.portfolio-table th.group-head { background: linear-gradient(#fff6d8, #f9e5a4); left: 34px; z-index: 4; }
.portfolio-table th.rownum-head { background: #f1f4f7; left: 0; z-index: 5; }
.portfolio-table td { background: #fff; }
.portfolio-table tbody tr:hover td { filter: brightness(.985); }
.portfolio-table .cell-num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.portfolio-table .cell-text { text-align: left; }
.portfolio-table .cell-center { text-align: center; }
.portfolio-table .cell-edit { cursor: text; border-bottom: 1px dashed transparent; }
.portfolio-table .cell-edit:hover { border-bottom-color: #8ba9c4; background: #fbfdff; }
.portfolio-table .cell-edit::after { content: ""; display: inline-block; width: 0; }
.portfolio-table .cell-merged { font-weight: 700; }
.portfolio-table .row-group-cell {
  background: var(--cream-head) !important; color: #7a6220; font-weight: 700; text-align: center;
  position: sticky; left: 34px; z-index: 2; min-width: 46px; writing-mode: vertical-rl; letter-spacing: 4px;
}
.portfolio-table .rownum { background: #f5f7f9; color: var(--ink-faint); text-align: center; position: sticky; left: 0; z-index: 3; min-width: 34px; }
.portfolio-table td.tone-pink { background: var(--pink); }
.portfolio-table td.tone-mint { background: var(--mint); }
.portfolio-table td.tone-cream { background: var(--cream); }
.portfolio-table td.tone-lavender { background: var(--lavender); }
.portfolio-table th.tone-pink { background: var(--pink-head); }
.portfolio-table th.tone-mint { background: var(--mint-head); }
.portfolio-table th.tone-cream { background: var(--cream-head); }
.portfolio-table th.tone-lavender { background: var(--lavender); }
.neg { color: var(--up); font-weight: 700; }
.pos { color: var(--down); font-weight: 700; }
.muted { color: var(--ink-faint); }
.stale-badge { display: inline-block; margin-left: 5px; font-size: 9px; color: #a86a00; background: #fff0cc; border-radius: 3px; padding: 0 3px; font-family: inherit; }
.cell-input { width: 100%; height: 27px; border: 1px solid var(--brand); outline: 2px solid rgba(31,93,146,.14); border-radius: 3px; padding: 0 4px; background: #fff; font-family: var(--mono); text-align: right; }
.cell-input.cell-input-text { text-align: left; font-family: inherit; }
.cell-select { width: 100%; height: 27px; border: 1px solid var(--brand); outline: 2px solid rgba(31,93,146,.14); border-radius: 3px; background: #fff; padding: 0 3px; }
.row-del { border: 0; background: transparent; color: #b3bdc8; font-size: 14px; line-height: 1; padding: 0 3px; }
.row-del:hover { color: var(--danger); }
.portfolio-table tr.summary-row td { background: #eef3f8; font-weight: 700; border-top: 2px solid var(--line-strong); }
.portfolio-table tr.summary-row td.sum-label { background: #fff; text-align: left; font-weight: 700; }
.portfolio-table tr.summary-row td.sum-green { background: var(--mint); }
.portfolio-table tr.summary-row td.sum-blue { background: var(--sky); }
.portfolio-table tr.summary-row td.sum-red { color: var(--up); }
.table-foot { border-top: 1px solid var(--line); padding: 9px 14px; color: var(--ink-faint); font-size: 10px; }
.table-loading { position: absolute; inset: 0; background: rgba(255,255,255,.72); display: grid; place-items: center; z-index: 8; color: var(--ink-soft); font-size: 12px; gap: 8px; }
.table-loading .spinner { display: inline-block; border-color: rgba(31,93,146,.25); border-top-color: var(--brand); width: 18px; height: 18px; }

.page-foot { display: flex; gap: 8px; justify-content: center; color: var(--ink-faint); font-size: 10px; margin-top: 16px; }
.foot-sep { color: var(--line-strong); }

/* ---------- toast ---------- */
.toast-stack { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: 360px; }
.toast { background: #fff; border: 1px solid var(--line-strong); border-left: 4px solid var(--brand); border-radius: 8px; padding: 10px 13px; box-shadow: 0 8px 24px rgba(28,50,72,.14); font-size: 12px; line-height: 1.5; animation: toastIn .2s ease-out; }
.toast.error { border-left-color: var(--danger); }
.toast.warn { border-left-color: #e6a23c; }
.toast.success { border-left-color: #1a9e78; }
.toast-title { font-weight: 700; margin-bottom: 2px; }
.toast-msg { color: var(--ink-soft); word-break: break-word; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- account layer ---------- */
.account-nav { display: flex; align-items: center; gap: 8px; margin: 14px 0 0; flex-wrap: wrap; }
.account-label { font-size: 11px; color: var(--ink-faint); margin-right: 2px; font-weight: 700; letter-spacing: .5px; }
.account-tab {
  border: 1px solid var(--line-strong); background: #fff; border-radius: 8px; padding: 9px 15px;
  font-weight: 700; color: var(--ink-soft); display: inline-flex; align-items: center; gap: 8px;
  box-shadow: var(--shadow); transition: all .16s;
}
.account-tab:hover { border-color: #9fb9d0; color: var(--brand); }
.account-tab.active { background: #fff; border-color: var(--brand); color: var(--brand); box-shadow: 0 0 0 2px rgba(31,93,146,.12); }
.account-tab .acct-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--acct-color, #94a2b2); }
.account-tab .acct-tag { font-size: 9px; font-weight: 600; background: #eef2f6; color: var(--ink-soft); border-radius: 4px; padding: 1px 5px; }
.account-tab.active .acct-tag { background: var(--brand-soft); color: var(--brand); }

/* ---------- category quick filter ---------- */
.category-nav { display: flex; align-items: center; gap: 7px; margin: 10px 0 0; flex-wrap: wrap; }
.category-nav .account-label { color: #b9762a; }
.category-tab {
  border: 1px solid #e3c79c; background: #fffaf2; border-radius: 8px; padding: 7px 13px;
  font-weight: 700; color: #9a6a2e; display: inline-flex; align-items: center; gap: 7px;
  box-shadow: var(--shadow); transition: all .16s;
}
.category-tab:hover { border-color: #d69a44; color: #b9762a; }
.category-tab.active { background: #b9762a; border-color: #b9762a; color: #fff; box-shadow: 0 4px 12px rgba(185,118,42,.22); }
.category-tab .cat-dot { width: 7px; height: 7px; border-radius: 50%; background: #d69a44; }
.category-tab.active .cat-dot { background: #fff; opacity: .85; }
.category-tab .tab-code { font-family: var(--mono); font-size: 9px; opacity: .72; }
.category-tab .acct-tag { font-size: 9px; font-weight: 600; background: #f3e5cf; color: #9a6a2e; border-radius: 4px; padding: 1px 5px; }
.category-tab.active .acct-tag { background: rgba(255,255,255,.2); color: #fff; }

.platform-nav { margin-top: 10px; }

.summary-card { margin-bottom: 12px; }
.summary-scroll { max-height: 260px; }
.summary-scroll .portfolio-table { min-width: 1120px; }
.summary-scroll th { position: sticky; top: 0; }
.summary-scroll td.account-cell { text-align: left; font-weight: 700; }
.summary-scroll .scope-head, .summary-scroll .scope-cell { background: var(--brand-soft); }

.filter-bar { display: flex; align-items: center; gap: 14px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: #f8fafc; flex-wrap: wrap; }
.filter-bar label { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; color: var(--ink-soft); font-weight: 700; }
.filter-bar select { border: 1px solid var(--line-strong); border-radius: 6px; height: 27px; padding: 0 7px; background: #fff; font-size: 11px; }

.account-detail { display: grid; grid-template-columns: 320px 1fr; gap: 12px; margin-top: 12px; align-items: start; }
.asset-body { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.asset-field { display: flex; flex-direction: column; gap: 5px; }
.asset-field label { font-size: 11px; color: var(--ink-soft); font-weight: 700; }
.asset-field input, .asset-field textarea { border: 1px solid var(--line-strong); border-radius: 7px; padding: 7px 9px; font-size: 12px; background: #fff; width: 100%; }
.asset-field textarea { min-height: 58px; resize: vertical; font-family: inherit; }
.asset-field input:focus, .asset-field textarea:focus { outline: 2px solid rgba(31,93,146,.14); border-color: var(--brand); }
.asset-total { border-top: 1px dashed var(--line-strong); padding-top: 10px; display: flex; justify-content: space-between; align-items: baseline; }
.asset-total span { font-size: 11px; color: var(--ink-soft); }
.asset-total strong { font-family: var(--mono); font-size: 16px; }
.asset-total .pl-pos { color: #16836b; }
.asset-total .pl-neg { color: #c0392b; }
.asset-total .pl-mark { font-size: 11px; margin-left: 3px; }
.asset-save { align-self: flex-start; }
.asset-flows { border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px; background: #fbfcfd; display: flex; flex-direction: column; gap: 6px; }
.flow-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.flow-head span { font-size: 10px; color: var(--ink-soft); }
.flow-head strong { font-family: var(--mono); font-size: 11px; white-space: nowrap; }
.flow-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 10px; }
.flow-table th { font-size: 9px; color: var(--ink-soft); text-align: left; padding: 3px 2px; border-bottom: 1px solid var(--line); }
.flow-table td { padding: 4px 2px; border-bottom: 1px solid var(--line); }
.flow-table th:nth-child(1), .flow-table td:nth-child(1) { width: 66px; }
.flow-table th:nth-child(2), .flow-table td:nth-child(2) { width: 30px; }
.flow-table th:nth-child(3), .flow-table td:nth-child(3) { width: 66px; }
.flow-table th:nth-child(5), .flow-table td:nth-child(5) { width: 18px; }
.flow-table .flow-in { color: var(--brand); }
.flow-table .flow-out { color: var(--ink-soft); }
.flow-table .flow-empty { text-align: center; color: var(--ink-soft); padding: 8px 0; }
.flow-form { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.flow-form input, .flow-form select { width: 100%; height: 26px; border: 1px solid var(--line-strong); border-radius: 5px; padding: 0 5px; font-size: 10px; background: #fff; min-width: 0; }
.flow-form button { grid-column: 1 / -1; }

.trade-card { min-width: 0; }
.trade-form { display: grid; grid-template-columns: 116px 104px 100px 88px minmax(90px, 1fr) 78px 78px 82px 82px auto; gap: 6px; padding: 10px 16px 12px; border-bottom: 1px solid var(--line); align-items: center; }
.trade-form input, .trade-form select { width: 100%; height: 27px; border: 1px solid var(--line-strong); border-radius: 5px; padding: 0 5px; font-size: 11px; background: #fff; min-width: 0; }
.trade-scroll { max-height: 300px; }
.trade-scroll .portfolio-table { min-width: 100%; }
.trade-scroll th { position: sticky; top: 0; }
.trade-scroll td.account-cell { text-align: left; font-weight: 700; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .charts-grid { grid-template-columns: 1fr 1fr; }
  .chart-wide { grid-column: 1 / -1; }
  .chart-body { min-height: 200px; }
}
@media (max-width: 760px) {
  .app-shell { padding: 12px; }
  .banner { grid-template-columns: 1fr; }
  .fx-panel { display: grid; grid-template-columns: repeat(3, 1fr); border-right: 0; border-bottom: 1px solid #cfe3f2; }
  .fx-row { border-bottom: 0; flex-direction: column; height: auto; }
  .fx-label { width: 100%; height: 22px; justify-content: center; padding: 0; }
  .fx-value { height: 26px; }
  .status-panel { border-left: 0; border-top: 1px solid #cfe3f2; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .charts-grid { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: flex-start; }
  .topbar-actions { justify-content: flex-start; }
  .banner-title h1 { font-size: 20px; }
  .account-detail { grid-template-columns: 1fr; }
  .trade-form { grid-template-columns: 1fr 1fr; }
  .trade-form .btn { grid-column: 1 / -1; }
}
