:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --line: #dfe4e8;
  --line-strong: #c8d0d6;
  --ink: #172027;
  --muted: #66737d;
  --accent: #176b5b;
  --accent-soft: #e8f3f0;
  --blue: #245e91;
  --blue-soft: #eaf2f8;
  --warn: #9a6200;
  --warn-soft: #fff5dd;
  --bad: #ad3434;
  --bad-soft: #fff0ef;
  --good: #2a6f44;
  --good-soft: #eaf5ed;
  --sidebar: #1d252b;
  --sidebar-muted: #9da9b1;
  font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink); }
body { font-size: 14px; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 232px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; background: var(--sidebar); color: white; padding: 22px 14px 16px; display: flex; flex-direction: column; }
.brand { padding: 0 10px 22px; border-bottom: 1px solid #344047; }
.brand strong { display: block; font-size: 17px; line-height: 1.4; }
.brand span { display: block; color: var(--sidebar-muted); margin-top: 5px; font-size: 12px; }
nav { padding: 16px 0; display: grid; gap: 4px; }
nav button { width: 100%; min-height: 42px; border: 0; border-radius: 6px; background: transparent; color: #ced6db; text-align: left; padding: 0 12px; }
nav button span { display: inline-block; width: 28px; color: #81909a; font-variant-numeric: tabular-nums; }
nav button:hover { background: #29343b; color: white; }
nav button.active { background: #e9f3f0; color: #174f45; font-weight: 700; }
nav button.active span { color: var(--accent); }
.sidebar-foot { margin-top: auto; border-top: 1px solid #344047; padding: 15px 10px 0; color: var(--sidebar-muted); font-size: 12px; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #67c58b; margin-right: 7px; }

main { min-width: 0; }
.topbar { min-height: 88px; padding: 18px 28px; display: flex; align-items: center; justify-content: space-between; gap: 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10; }
.topbar h1 { margin: 0; font-size: 22px; line-height: 1.25; }
.topbar p { margin: 5px 0 0; color: var(--muted); }
.toolbar { display: flex; align-items: center; gap: 8px; }
.toolbar button, .link-button, .danger-button, .dialog-actions button { min-height: 34px; border: 1px solid var(--line-strong); border-radius: 6px; background: white; color: var(--ink); padding: 0 12px; }
.toolbar button:hover, .link-button:hover { border-color: var(--accent); color: var(--accent); }
.save-status { color: var(--muted); font-size: 12px; margin-right: 3px; min-width: 58px; }

.view { display: none; padding: 24px 28px 44px; max-width: 1680px; margin: 0 auto; }
.view.active { display: block; }
.control-strip { display: flex; align-items: end; gap: 16px; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; padding: 14px 16px; margin-bottom: 18px; }
.control-strip label { min-width: 170px; }
.control-strip .context-note { margin-left: auto; color: var(--muted); max-width: 420px; line-height: 1.5; }

.metrics { display: grid; gap: 12px; margin-bottom: 18px; }
.metrics.six { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.metrics.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.metric { min-width: 0; min-height: 104px; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; padding: 15px; }
.metric .label { color: var(--muted); font-size: 12px; line-height: 1.35; }
.metric .value { margin-top: 10px; font-size: 24px; font-weight: 750; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric .unit { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: 4px; }
.metric .hint { margin-top: 7px; color: var(--muted); font-size: 12px; min-height: 16px; }
.metric.good { border-top: 3px solid var(--good); }
.metric.bad { border-top: 3px solid var(--bad); }
.metric.warn { border-top: 3px solid var(--warn); }

.section-block { background: var(--surface); border: 1px solid var(--line); border-radius: 7px; margin-top: 18px; overflow: hidden; }
.section-head { min-height: 66px; padding: 14px 16px; display: flex; justify-content: space-between; align-items: center; gap: 16px; border-bottom: 1px solid var(--line); }
.section-head h2 { margin: 0; font-size: 16px; }
.section-head p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.link-button { color: var(--accent); border-color: #b8d5ce; }

.form-band { padding: 18px 16px; display: grid; grid-template-columns: repeat(4, minmax(170px, 1fr)); gap: 16px; }
.enterprise-columns { padding: 18px 16px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px; }
.enterprise-block { min-width: 0; }
.enterprise-block h3 { margin: 0 0 13px; font-size: 15px; }
.field-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; }
input, select, textarea { width: 100%; min-height: 36px; border: 1px solid var(--line-strong); border-radius: 5px; background: white; color: var(--ink); padding: 7px 9px; outline: none; }
textarea { min-height: 58px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(23, 107, 91, .12); }
input[readonly] { background: var(--surface-2); color: var(--muted); }
.input-unit { position: relative; }
.input-unit input { padding-right: 70px; }
.input-unit span { position: absolute; right: 9px; top: 10px; color: var(--muted); font-size: 12px; }
.helper { color: var(--muted); font-size: 11px; line-height: 1.5; }

.boundary-note, .formula-banner, .notice { border-radius: 6px; padding: 12px 14px; line-height: 1.6; }
.boundary-note { background: var(--blue-soft); border: 1px solid #c9ddeb; color: #244a68; }
.formula-banner { display: flex; gap: 12px; flex-wrap: wrap; background: var(--accent-soft); border: 1px solid #c7ded8; color: #20584e; }
.formula-banner span { padding-right: 12px; border-right: 1px solid #b8d1ca; }
.formula-banner span:last-child { border-right: 0; }
.notice { margin: 10px 0; background: var(--warn-soft); border: 1px solid #f0d59e; color: #78510a; }
.notice.bad { background: var(--bad-soft); border-color: #efc4c0; color: #8e2d2d; }
.notice.good { background: var(--good-soft); border-color: #c4dfca; color: #275e39; }

.table-wrap { width: 100%; overflow: auto; }
.table-wrap.wide table { min-width: 1320px; }
table { width: 100%; border-collapse: collapse; }
th, td { border-bottom: 1px solid var(--line); padding: 10px 11px; text-align: left; vertical-align: middle; }
th { position: sticky; top: 0; z-index: 1; background: var(--surface-2); color: #52606a; font-size: 12px; font-weight: 700; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafcfc; }
td.numeric { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td .cell-input { min-width: 98px; }
td .name-input { min-width: 180px; }
.narrow-table table { max-width: 760px; }
.row-actions { display: flex; gap: 5px; }
.icon-action { width: 30px; min-width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 5px; background: white; color: var(--muted); padding: 0; }
.icon-action:hover { color: var(--bad); border-color: #e0aaaa; }
.check { width: 16px; height: 16px; min-height: 0; }
.badge { display: inline-flex; align-items: center; min-height: 24px; border-radius: 12px; padding: 2px 8px; background: #eef1f3; color: #59656e; font-size: 11px; white-space: nowrap; }
.badge.confirmed { background: var(--good-soft); color: var(--good); }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.missing { background: var(--bad-soft); color: var(--bad); }

.trace-grid { display: grid; grid-template-columns: repeat(4, 1fr); }
.trace-step { min-height: 115px; padding: 16px; border-right: 1px solid var(--line); }
.trace-step:last-child { border-right: 0; }
.trace-step .step { color: var(--accent); font-weight: 750; }
.trace-step strong { display: block; margin-top: 11px; font-size: 15px; }
.trace-step p { color: var(--muted); font-size: 12px; line-height: 1.55; }
.split-panels { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }

.matrix-wrap { width: 100%; overflow: auto; padding: 0 0 1px; }
.matrix-wrap table { min-width: 980px; }
.matrix-wrap.compact { max-height: 430px; }
.matrix-cell { min-width: 136px; height: 74px; padding: 8px 10px; border-left: 3px solid transparent; }
.matrix-cell strong { display: block; font-size: 14px; font-variant-numeric: tabular-nums; }
.matrix-cell span { display: block; margin-top: 5px; color: var(--muted); font-size: 11px; }
.matrix-cell.profit { background: var(--good-soft); border-left-color: var(--good); }
.matrix-cell.loss { background: var(--bad-soft); border-left-color: var(--bad); }
.matrix-cell.neutral { background: var(--warn-soft); border-left-color: var(--warn); }
.matrix-cell.empty { background: var(--surface-2); color: var(--muted); }

.snapshot-list { padding: 0 16px; }
.snapshot-row { min-height: 58px; display: grid; grid-template-columns: 1fr 180px auto; align-items: center; gap: 16px; border-bottom: 1px solid var(--line); }
.snapshot-row:last-child { border-bottom: 0; }
.snapshot-row strong { display: block; }
.snapshot-row span { color: var(--muted); font-size: 12px; }
.snapshot-row .actions { display: flex; gap: 6px; }
.formula-docs { display: grid; grid-template-columns: repeat(2, 1fr); }
.formula-docs article { min-height: 90px; padding: 16px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.formula-docs article:nth-child(2n) { border-right: 0; }
.formula-docs article:nth-last-child(-n+2) { border-bottom: 0; }
.formula-docs strong { color: var(--accent); display: block; margin-bottom: 8px; }
.formula-docs span { color: var(--muted); line-height: 1.6; }
.danger-zone { border-color: #e5c2c0; }
.danger-button { color: var(--bad); border-color: #dbafab; }

dialog { border: 0; border-radius: 8px; box-shadow: 0 20px 60px rgba(0,0,0,.22); width: min(480px, calc(100vw - 32px)); padding: 0; }
dialog::backdrop { background: rgba(14, 22, 27, .46); }
dialog form { padding: 22px; }
dialog h2 { margin: 0 0 18px; font-size: 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.dialog-actions button:last-child { background: var(--accent); color: white; border-color: var(--accent); }

@media (max-width: 1200px) {
  .metrics.six { grid-template-columns: repeat(3, 1fr); }
  .metrics.four { grid-template-columns: repeat(2, 1fr); }
  .form-band { grid-template-columns: repeat(2, 1fr); }
  .trace-grid { grid-template-columns: repeat(2, 1fr); }
  .trace-step:nth-child(2) { border-right: 0; }
  .trace-step:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
}

@media (max-width: 800px) {
  .app-shell { display: block; }
  .sidebar { position: static; width: 100%; height: auto; padding: 12px; }
  .brand { padding: 0 4px 10px; }
  nav { display: flex; overflow-x: auto; padding: 10px 0 0; }
  nav button { min-width: 138px; }
  .sidebar-foot { display: none; }
  .topbar { position: static; align-items: flex-start; padding: 16px; }
  .toolbar { flex-wrap: wrap; justify-content: flex-end; }
  .toolbar button, .link-button, .danger-button, .dialog-actions button, input, select { min-height: 44px; }
  .view { padding: 16px; }
  .metrics.six, .metrics.four, .enterprise-columns, .split-panels, .formula-docs, .field-grid { grid-template-columns: 1fr; }
  .form-band { grid-template-columns: 1fr; }
  .trace-grid { grid-template-columns: 1fr; }
  .trace-step { border-right: 0; border-bottom: 1px solid var(--line); }
  .trace-step:last-child { border-bottom: 0; }
  .formula-docs article { border-right: 0; border-bottom: 1px solid var(--line); }
  .snapshot-row { grid-template-columns: 1fr; padding: 12px 0; gap: 7px; }
  .table-wrap, .matrix-wrap { overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; }
}
