:root {
  --ink: #152326;
  --muted: #6a7778;
  --line: #dbe3e1;
  --surface: #ffffff;
  --canvas: #f2f5f3;
  --teal: #0f6b68;
  --teal-dark: #095652;
  --teal-soft: #e2f0ed;
  --orange: #c56b2c;
  --green: #157a4c;
  --red: #b84245;
  --blue: #4f7fa4;
  --purple: #7a5f98;
  --shadow: 0 8px 24px rgba(25, 52, 50, 0.06);
  --rail: 190px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
  font: 14px/1.5 "Segoe UI", "Microsoft YaHei", sans-serif;
}

button, input, select { font: inherit; }
button { cursor: pointer; }

.app-frame { max-width: 1600px; min-height: 100vh; margin: 0 auto; padding: 22px 28px 34px; }
.topbar, .brand, .topbar-actions, .view-heading, .panel-heading, .footer { display: flex; align-items: center; }
.topbar { justify-content: space-between; gap: 24px; margin-bottom: 22px; }
.brand { gap: 12px; }
.brand-mark { display: grid; width: 38px; height: 38px; place-items: center; border-radius: 6px; background: var(--teal); color: #fff; font-weight: 800; letter-spacing: 0; }
.topbar-actions { justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
.eyebrow { margin: 0 0 5px; color: var(--teal); font-size: 11px; font-weight: 800; letter-spacing: 1.2px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 22px; letter-spacing: 0; }
h2 { margin-bottom: 4px; font-size: 22px; letter-spacing: 0; }
h3 { margin: 0; font-size: 16px; letter-spacing: 0; }
.muted, .section-note, .metric-label, .metric small { color: var(--muted); }
.muted { margin-bottom: 0; }

.status-pill { border-radius: 999px; padding: 5px 10px; font-size: 12px; font-weight: 700; }
.status-idle { background: #e8eceb; color: #566362; }
.status-good { background: #d9f1e3; color: var(--green); }
.status-bad { background: #f8e0e1; color: var(--red); }
.button, .text-button {
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 8px 14px;
  background: var(--surface);
  color: var(--ink);
}
.button:hover, .text-button:hover { border-color: var(--teal); color: var(--teal); }
.button-primary { border-color: var(--teal); background: var(--teal); color: #fff; }
.button-primary:hover { background: var(--teal-dark); color: #fff; }
.button-quiet { padding: 6px 10px; }
.text-button { padding: 0; border: 0; background: transparent; color: var(--teal); }

.login-panel {
  display: grid;
  grid-template-columns: 1fr minmax(280px, 440px);
  gap: 28px;
  align-items: end;
  margin-bottom: 24px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.login-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; align-items: end; }
label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; }
input, select { width: 100%; border: 1px solid var(--line); border-radius: 5px; padding: 9px 10px; color: var(--ink); background: #fbfcfb; }
input:focus, select:focus { outline: 2px solid rgba(15, 107, 104, 0.2); border-color: var(--teal); }
.error-text { grid-column: 1 / -1; min-height: 20px; margin: 0; color: var(--red); }

.app-shell { display: grid; grid-template-columns: var(--rail) minmax(0, 1fr); gap: 24px; align-items: start; }
#desktop-nav, .desktop-nav { position: sticky; top: 18px; display: grid; gap: 5px; padding: 8px 0; }
.nav-heading { margin: 0 10px 7px; color: var(--muted); font-size: 12px; font-weight: 700; }
.nav-item { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; border-radius: 5px; padding: 11px 10px; background: transparent; color: var(--muted); text-align: left; }
.nav-item:hover { background: var(--teal-soft); color: var(--teal); }
.nav-item.active { background: var(--teal); color: #fff; font-weight: 700; }
.nav-index { display: grid; width: 22px; height: 22px; place-items: center; border: 1px solid currentColor; border-radius: 4px; font-size: 11px; }
.app-content { min-width: 0; }
.view-heading { justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.view-heading .muted { font-size: 13px; }
.stale-banner { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; padding: 10px 12px; border-left: 3px solid var(--orange); background: #fff6ed; color: #754619; }
.stale-banner span { flex: 1; }

.metrics { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 24px; }
.metric { min-height: 108px; padding: 17px 18px; background: var(--surface); border-top: 3px solid var(--teal); box-shadow: var(--shadow); }
.metric-orange { border-top-color: var(--orange); }
.metric-blue { border-top-color: var(--blue); }
.metric-red { border-top-color: var(--red); }
.metric-purple { border-top-color: var(--purple); }
.metric-label { display: block; margin-bottom: 8px; font-size: 12px; }
.metric strong { display: block; min-height: 34px; font-size: 23px; line-height: 1.2; }
.metric small { font-size: 12px; }
.positive { color: var(--green) !important; }
.negative { color: var(--red) !important; }

.charts-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; margin-bottom: 18px; }
.chart-wide { grid-column: 1 / -1; }
.panel { margin-bottom: 18px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.panel-heading { justify-content: space-between; gap: 16px; padding: 17px 18px; }
.chart-box { min-height: 280px; height: 280px; padding: 0 14px 14px; }
svg { display: block; width: 100%; height: 100%; }
.chart-grid-line { stroke: #e5ece9; stroke-width: 1; }
.chart-axis-label, .chart-empty { fill: #7b8987; font-size: 11px; }
.chart-line { fill: none; stroke: var(--teal); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.chart-zero { stroke: #aab8b5; stroke-width: 1; stroke-dasharray: 4 4; }
.bar-positive { fill: #55a982; }
.bar-negative { fill: #d97973; }

.bar-list, .reason-list, .retention-list, .activity-list { padding: 3px 18px 18px; }
.bar-row { display: grid; grid-template-columns: 105px 1fr 84px; gap: 10px; align-items: center; margin-top: 15px; }
.bar-label, .bar-value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; }
.bar-track, .retention-track { height: 9px; overflow: hidden; background: #edf1ef; }
.bar-fill, .retention-fill { display: block; height: 100%; background: var(--teal); }
.bar-fill.negative, .retention-fill.negative { background: var(--red); }
.reason-row { display: flex; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.reason-row:last-child { border-bottom: 0; }
.retention-row { display: grid; grid-template-columns: minmax(130px, 1fr) minmax(160px, 2fr) 110px 100px 90px; gap: 12px; align-items: center; padding: 11px 0; border-bottom: 1px solid var(--line); }
.retention-row:last-child { border-bottom: 0; }
.retention-row small, .activity-row small { display: block; color: var(--muted); }
.retention-number { text-align: right; font-variant-numeric: tabular-nums; }

.position-list, .trade-list, .message-list, .status-grid { display: grid; gap: 12px; }
.position-card { display: grid; gap: 14px; width: 100%; padding: 18px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); text-align: left; box-shadow: var(--shadow); }
.position-card:hover { border-color: var(--teal); }
.position-top, .position-bottom, .activity-row { display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.position-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.position-grid span { display: grid; gap: 3px; }
.position-grid small { color: var(--muted); }
.position-bottom { padding-top: 12px; border-top: 1px solid var(--line); color: var(--muted); }
.direction-long { color: var(--green); font-weight: 700; }
.direction-short { color: var(--red); font-weight: 700; }

.filter-row { display: flex; gap: 12px; margin-bottom: 14px; }
.filter-row label { min-width: 150px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; min-width: 900px; border-collapse: collapse; }
th, td { padding: 11px 13px; border-top: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { background: #f7f9f8; color: var(--muted); font-size: 12px; font-weight: 700; }
td { font-size: 13px; }
td small { color: var(--muted); }
.activity-row { width: 100%; border: 0; border-bottom: 1px solid var(--line); padding: 12px 0; background: transparent; color: var(--ink); text-align: left; }
.activity-row:last-child { border-bottom: 0; }
.activity-row:hover { color: var(--teal); }

.message-card { display: grid; grid-template-columns: 70px 1fr; gap: 15px; padding: 17px; border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow); }
.message-level { align-self: start; padding: 4px 7px; border-radius: 4px; background: var(--teal-soft); color: var(--teal); font-size: 12px; font-weight: 700; text-align: center; }
.message-warning { border-left: 3px solid var(--orange); }
.message-warning .message-level { background: #fff0de; color: #9a5a1f; }
.message-card h3 { margin-bottom: 4px; }
.message-card p { margin: 0; color: var(--muted); }
.status-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.status-card { display: grid; gap: 8px; padding: 17px; border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow); }
.status-card span { color: var(--muted); font-size: 12px; }
.status-card strong { font-size: 18px; }
.empty, .empty-state { color: var(--muted); text-align: center; }
.empty { padding: 28px 16px; }
.empty-state { display: grid; gap: 7px; padding: 46px 18px; border: 1px dashed var(--line); background: var(--surface); }
.empty-state strong { color: var(--ink); }

.detail-drawer { position: fixed; z-index: 20; top: 0; right: 0; bottom: 0; width: min(420px, 100vw); overflow-y: auto; padding: 24px; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(25, 52, 50, 0.15); }
.drawer-heading { display: flex; justify-content: space-between; gap: 16px; align-items: start; margin-bottom: 18px; }
.detail-direction { margin-bottom: 20px; }
.detail-list { display: grid; gap: 0; margin: 0 0 22px; }
.detail-list div { display: flex; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.detail-list dt { color: var(--muted); }
.detail-list dd { margin: 0; font-weight: 700; }
.footer { justify-content: space-between; gap: 16px; margin-top: 22px; color: var(--muted); font-size: 12px; }
#mobile-nav, .mobile-nav { display: none; }
.loading-state, .stale-state, .unavailable-state { padding: 18px; border: 1px dashed var(--line); color: var(--muted); background: var(--surface); }
.stale-state { border-left: 3px solid var(--orange); background: #fff6ed; color: #754619; }
.unavailable-state { border-left: 3px solid var(--red); background: #fff4f4; color: var(--red); }

@media (max-width: 1180px) {
  :root { --rail: 165px; }
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .app-frame { padding: 16px 14px calc(84px + env(safe-area-inset-bottom)); }
  .app-shell { display: block; }
  #desktop-nav, .desktop-nav { display: none; }
  #mobile-nav, .mobile-nav { position: fixed; z-index: 15; right: 0; bottom: 0; left: 0; display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px; padding: 7px 7px calc(7px + env(safe-area-inset-bottom)); background: rgba(255, 255, 255, 0.96); border-top: 1px solid var(--line); box-shadow: 0 -5px 18px rgba(25, 52, 50, 0.09); }
  .mobile-nav .nav-item { display: grid; justify-items: center; gap: 3px; min-width: 0; padding: 5px 2px; font-size: 11px; text-align: center; }
  .mobile-nav .nav-index { width: 20px; height: 20px; }
  .topbar { align-items: flex-start; flex-direction: column; gap: 14px; }
  .topbar-actions { justify-content: flex-start; }
  .view-heading { align-items: flex-start; flex-direction: column; }
  .charts-grid { grid-template-columns: 1fr; }
  .chart-wide { grid-column: auto; }
  .position-grid { grid-template-columns: repeat(2, 1fr); }
  .status-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .retention-row { grid-template-columns: 1fr 1.5fr 90px; }
  .retention-row .retention-giveback { display: none; }
}

@media (max-width: 680px) {
  .login-panel { grid-template-columns: 1fr; }
  .login-form { grid-template-columns: 1fr; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .metric { min-height: 100px; padding: 13px; }
  .metric strong { font-size: 18px; }
  .panel-heading { padding: 14px; }
  .chart-box { min-height: 220px; height: 220px; padding: 0 7px 8px; }
  .filter-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
  .filter-row label { min-width: 0; }
  .filter-row select { padding: 8px 5px; font-size: 12px; }
  .retention-row { grid-template-columns: 1fr 78px; }
  .retention-track, .retention-row .retention-number { display: none; }
  .message-card { grid-template-columns: 52px 1fr; gap: 10px; padding: 14px; }
  .status-grid { grid-template-columns: 1fr; }
  .footer { align-items: flex-start; flex-direction: column; }
}
