:root {
  --bg: #f3f6fa;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --line: #dce4ee;
  --line-strong: #c8d4e3;
  --ink: #172235;
  --ink-soft: #546278;
  --blue: #176b87;
  --blue-dark: #0e5068;
  --teal: #168b82;
  --green: #378b60;
  --amber: #b66b14;
  --red: #c74747;
  --purple: #7657a6;
  --shadow: 0 14px 34px rgba(28, 47, 75, .08);
  --radius: 16px;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); }
body {
  margin: 0;
  background:
    radial-gradient(circle at 85% -5%, rgba(23, 107, 135, .11), transparent 28rem),
    var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.topbar {
  max-width: 1600px;
  margin: 0 auto;
  padding: 28px 30px 22px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 28px;
}
.topbar h1 { margin: 2px 0 5px; font-size: clamp(26px, 2.5vw, 38px); letter-spacing: -.035em; }
.subtitle { margin: 0; color: var(--ink-soft); font-size: 15px; }
.eyebrow, .section-kicker {
  margin: 0;
  color: var(--blue);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: .14em;
  text-transform: uppercase;
}
.safety-note {
  max-width: 470px;
  border: 1px solid #bcd8df;
  border-left: 4px solid var(--teal);
  border-radius: 10px;
  background: rgba(255,255,255,.74);
  padding: 12px 15px;
  display: grid;
  gap: 2px;
}
.safety-note strong { color: var(--teal); font-size: 13px; }
.safety-note span { color: var(--ink-soft); font-size: 12px; }

.workspace {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 30px 50px;
  display: grid;
  grid-template-columns: minmax(370px, 450px) minmax(0, 1fr);
  align-items: start;
  gap: 22px;
}
.config-panel, .run-card, .population-card, .pool-section, .ratio-card, .protection-card, .result-card, .history-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.config-panel { max-height: calc(100vh - 34px); overflow: auto; position: sticky; top: 17px; }
.sim-panel { min-width: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.sim-panel > * { min-width: 0; }
.panel-heading {
  padding: 19px 20px 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}
.panel-heading h2 { margin: 1px 0 0; font-size: 20px; letter-spacing: -.02em; }
.compact-heading { padding-bottom: 13px; }
.sticky-heading { background: var(--surface); border-bottom: 1px solid var(--line); z-index: 2; }

.form-section { padding: 18px 20px; border-bottom: 1px solid var(--line); }
.form-section h3 { margin: 0 0 13px; font-size: 14px; }
.section-title-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.field-note { color: var(--ink-soft); font-size: 11px; font-weight: 450; }
.preset-help { margin: -3px 0 12px; color: var(--ink-soft); font-size: 12px; }
.source-badge { flex: 0 0 auto; }
.field-grid { display: grid; gap: 12px; }
.field-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field-grid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.field-grid .full, .field.full { grid-column: 1 / -1; }
.field { display: grid; gap: 5px; min-width: 0; color: var(--ink-soft); font-size: 12px; font-weight: 650; }
.field small { color: #788599; font-weight: 400; line-height: 1.3; }
.field input, .field select {
  width: 100%;
  min-height: 39px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  padding: 8px 10px;
  outline: none;
  font-weight: 540;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(23,107,135,.13); }
.field input:invalid { border-color: #d79a9a; }
.sum-badge, .status-badge {
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 11px;
  font-weight: 750;
  white-space: nowrap;
}
.sum-badge { color: var(--green); background: #e9f6ef; }
.sum-badge.bad { color: var(--red); background: #fdeeee; }
.status-badge.neutral { color: #65758a; background: #edf1f6; }
.status-badge.ok { color: var(--green); background: #e7f5ed; }
.status-badge.warn { color: var(--amber); background: #fff2df; }
.status-badge.error { color: var(--red); background: #fdeaea; }

.switch-row { margin-top: 14px; display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.switch-row.compact { margin: 0 0 13px; }
.switch-row > span:first-child { display: grid; color: var(--ink); }
.switch-row small { color: var(--ink-soft); font-size: 11px; font-weight: 400; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  width: 42px;
  height: 24px;
  flex: 0 0 auto;
  border-radius: 99px;
  background: #cbd5e1;
  position: relative;
  transition: .15s;
}
.switch::after { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.22); transition: .15s; }
.switch-row input:checked + .switch { background: var(--teal); }
.switch-row input:checked + .switch::after { transform: translateX(18px); }
.switch-row input:focus-visible + .switch { box-shadow: 0 0 0 3px rgba(23,107,135,.2); }

.button, .icon-button {
  border: 1px solid transparent;
  border-radius: 9px;
  min-height: 39px;
  padding: 8px 14px;
  font-weight: 700;
  transition: transform .08s, background .15s, border-color .15s;
}
.button:active, .icon-button:active { transform: translateY(1px); }
.button:focus-visible, .icon-button:focus-visible { outline: 3px solid rgba(23,107,135,.23); outline-offset: 2px; }
.button.primary { background: var(--blue); color: #fff; }
.button.primary:hover { background: var(--blue-dark); }
.button.secondary { background: #e7f2f5; color: var(--blue-dark); border-color: #c6e0e6; }
.button.secondary:hover { background: #d7ebef; }
.button.ghost, .icon-button { background: var(--surface); color: var(--ink-soft); border-color: var(--line-strong); }
.button.ghost:hover, .icon-button:hover { color: var(--ink); border-color: #aebdce; }
.button.small { min-height: 34px; padding: 6px 10px; font-size: 12px; }
.download-link { display: inline-flex; align-items: center; text-decoration: none; }
.button:disabled { opacity: .48; cursor: not-allowed; }
.full-width { width: 100%; margin-top: 11px; }
.form-actions { padding: 16px 20px; display: flex; gap: 10px; }
.assumptions { margin: 0 20px 20px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-soft); }
.assumptions summary { cursor: pointer; padding: 11px 13px; color: var(--ink-soft); font-weight: 700; }
.assumptions ul { margin: 0; padding: 0 30px 14px; color: var(--ink-soft); font-size: 12px; }
.error-box { margin: 14px 20px 0; border: 1px solid #f0b7b7; border-radius: 9px; background: #fff3f3; color: #a83939; padding: 10px 12px; font-size: 12px; white-space: pre-line; }

.run-card { padding-bottom: 18px; }
.dirty-notice {
  margin: 0 20px 15px;
  border: 1px solid #e8bd76;
  border-left: 4px solid var(--amber);
  border-radius: 10px;
  background: #fff8ea;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.dirty-notice[hidden] { display: none; }
.dirty-notice > div { display: grid; gap: 1px; }
.dirty-notice strong { color: #85500f; font-size: 13px; }
.dirty-notice span { color: #846b4c; font-size: 11px; }
.run-form { padding: 0 20px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.run-actions { padding: 16px 20px 0; display: flex; flex-wrap: wrap; gap: 9px; align-items: center; }
.action-separator { width: 1px; height: 26px; background: var(--line); margin: 0 3px; }

.population-card { padding-bottom: 19px; }
.population-intro {
  margin: 0 20px 15px;
  border: 1px solid #c6dde4;
  border-left: 4px solid var(--blue);
  border-radius: 10px;
  background: #f0f7f9;
  padding: 10px 12px;
  display: grid;
  gap: 3px;
}
.population-intro strong { color: var(--blue-dark); font-size: 12px; }
.population-intro span { color: var(--ink-soft); font-size: 11px; }
.population-form { padding: 0 20px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.population-actions { padding: 16px 20px 0; display: flex; flex-wrap: wrap; gap: 9px; align-items: center; }
.population-dirty { margin-top: 14px; margin-bottom: 0; }
.batch-limit-note { margin: 12px 20px 0; color: var(--ink-soft); font-size: 11px; }
.population-summary { padding: 15px 20px 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.population-summary article { min-width: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-soft); padding: 10px 12px; display: grid; gap: 2px; }
.population-summary span { color: var(--ink-soft); font-size: 11px; }
.population-summary strong { font-size: 15px; overflow-wrap: anywhere; }
.population-summary small { color: #7c899b; overflow-wrap: anywhere; }

.pool-section { padding-bottom: 18px; }
.metric-time { margin: 0; font-variant-numeric: tabular-nums; }
.pool-grid { padding: 0 20px; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.pool-card {
  min-height: 116px;
  border-radius: 13px;
  padding: 15px;
  display: grid;
  align-content: space-between;
  color: #fff;
  overflow: hidden;
  position: relative;
}
.pool-card::after { content: ""; width: 88px; height: 88px; border: 18px solid rgba(255,255,255,.09); border-radius: 50%; position: absolute; right: -30px; top: -38px; }
.pool-card span { font-size: 12px; font-weight: 700; opacity: .86; z-index: 1; }
.pool-card strong { font-size: 30px; line-height: 1; font-variant-numeric: tabular-nums; z-index: 1; }
.pool-card small { opacity: .76; z-index: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pool-card.blood { background: linear-gradient(135deg, #c34d5d, #9a334b); }
.pool-card.protect { background: linear-gradient(135deg, #328d82, #1e6f69); }
.pool-card.jp { background: linear-gradient(135deg, #8365b5, #604592); }
.pool-card.rake { background: linear-gradient(135deg, #ca7e26, #a45f13); }

.metrics-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.metric-card { border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.8); padding: 14px 16px; display: grid; gap: 3px; }
.metric-card span { color: var(--ink-soft); font-size: 11px; font-weight: 650; }
.metric-card strong { font-size: 23px; font-variant-numeric: tabular-nums; }
.metric-card small { color: #7c899b; }

.ratio-card, .protection-card, .result-card, .history-card { padding-bottom: 19px; }
.ratio-content { padding: 0 20px; display: grid; grid-template-columns: minmax(260px, 1.4fr) minmax(260px, 1fr); gap: 24px; align-items: center; }
.ratio-bar { height: 14px; background: #e4ebf2; border-radius: 99px; overflow: hidden; }
.ratio-bar span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--teal), #3eb4a2); border-radius: inherit; transition: width .3s ease; }
.ratio-labels { margin-top: 8px; display: flex; justify-content: space-between; color: var(--ink-soft); font-size: 12px; }
.ratio-labels strong { color: var(--ink); }
.ratio-facts { margin: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; }
.ratio-facts div { min-width: 0; }
.ratio-facts dt { color: var(--ink-soft); font-size: 11px; }
.ratio-facts dd { margin: 2px 0 0; font-weight: 700; overflow-wrap: anywhere; }
.info-callout { margin: 15px 20px 0; border-left: 3px solid var(--blue); background: #edf6f8; color: var(--ink-soft); padding: 8px 11px; font-size: 12px; }
.protection-facts { margin: 0; padding: 0 20px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.protection-facts div { min-width: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-soft); padding: 10px 12px; }
.protection-facts dt { color: var(--ink-soft); font-size: 11px; }
.protection-facts dd { margin: 3px 0 0; font-weight: 700; overflow-wrap: anywhere; }

.empty-state { margin: 0 20px; border: 1px dashed var(--line-strong); border-radius: 10px; background: var(--surface-soft); color: var(--ink-soft); padding: 28px 18px; text-align: center; }
.result-summary { padding: 0 20px 15px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.result-summary div { border-radius: 10px; background: var(--surface-soft); padding: 11px 12px; display: grid; gap: 2px; }
.result-summary span { color: var(--ink-soft); font-size: 11px; }
.result-summary strong { font-size: 14px; overflow-wrap: anywhere; }
.table-wrap { min-width: 0; max-width: calc(100% - 40px); margin: 0 20px; overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 10px 11px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
th { background: #f4f7fa; color: var(--ink-soft); font-size: 11px; font-weight: 750; }
th:first-child, td:first-child { text-align: left; }
tbody tr:last-child td { border-bottom: 0; }
.subheading { margin: 18px 20px 10px; font-size: 14px; }
.trace-list { list-style: none; margin: 0 20px; padding: 0; display: grid; gap: 8px; }
.trace-list li { border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; display: grid; grid-template-columns: 30px minmax(100px, .65fr) minmax(0, 2fr); gap: 10px; align-items: start; }
.trace-list .step { width: 25px; height: 25px; border-radius: 50%; display: grid; place-items: center; background: #edf2f7; color: var(--ink-soft); font-size: 11px; font-weight: 750; }
.trace-list strong { font-size: 12px; padding-top: 3px; }
.trace-list p { margin: 2px 0 0; color: var(--ink-soft); font-size: 12px; overflow-wrap: anywhere; }
.trace-list li.ok { border-left: 3px solid var(--green); }
.trace-list li.warn { border-left: 3px solid var(--amber); }
.trace-list li.error { border-left: 3px solid var(--red); }
.trace-list li.skip { opacity: .72; }

.export-actions { display: flex; flex-wrap: wrap; gap: 7px; justify-content: flex-end; }
.history-wrap { max-height: 380px; }
.history-table { min-width: 1180px; }
.history-table thead { position: sticky; top: 0; z-index: 1; }
.history-table td { font-size: 12px; }
.empty-cell { color: var(--ink-soft); text-align: center !important; padding: 24px; }
.positive { color: var(--green); }
.negative { color: var(--red); }
.muted { color: var(--ink-soft); }

.toast { position: fixed; right: 24px; bottom: 24px; max-width: min(390px, calc(100vw - 32px)); background: var(--ink); color: #fff; border-radius: 10px; padding: 11px 14px; box-shadow: 0 12px 35px rgba(15,24,39,.22); z-index: 20; }
.mobile-only { display: none; }

@media (max-width: 1180px) {
  .workspace { grid-template-columns: 360px minmax(0, 1fr); }
  .pool-grid, .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .run-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .population-form, .population-summary, .protection-facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field-grid.four { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .result-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ratio-content { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 860px) {
  .topbar { padding: 20px 16px 16px; align-items: flex-start; flex-direction: column; }
  .safety-note { max-width: none; width: 100%; }
  .workspace { padding: 0 16px 38px; grid-template-columns: 1fr; }
  .config-panel { position: static; max-height: none; }
  .mobile-only { display: inline-block; }
  .ratio-content { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .pool-grid, .metrics-grid, .run-form, .population-form, .population-summary, .protection-facts, .field-grid.two, .field-grid.three, .field-grid.four, .result-summary { grid-template-columns: 1fr; }
  .form-actions, .run-actions, .population-actions { align-items: stretch; flex-direction: column; }
  .dirty-notice { align-items: stretch; flex-direction: column; }
  .action-separator { display: none; }
  .form-actions .button, .run-actions .button, .population-actions .button { width: 100%; }
  .panel-heading { align-items: flex-start; }
  .export-actions { width: 100%; justify-content: flex-start; }
  .history-card > .panel-heading { flex-direction: column; }
  .ratio-facts { grid-template-columns: 1fr; }
  .trace-list li { grid-template-columns: 28px 1fr; }
  .trace-list p { grid-column: 2; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
