:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --ink: #1a2129;
  --muted: #5c6774;
  --line: #dde2e8;
  --accent: #0e7490;
  --accent-ink: #ffffff;
  --pos: #15803d;
  --neg: #b42318;
  --warn-bg: #fffaeb;
  --warn-border: #f5d998;
  --warn-ink: #7a4d08;
  --radius: 8px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --mono: "SF Mono", "Cascadia Mono", ui-monospace, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; }

[hidden] { display: none !important; }

html { font-size: 13px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  -webkit-font-smoothing: antialiased;
}

.app-head {
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--ink);
  color: #fff;
  padding: 8px 12px;
}
.logo { font-weight: 700; letter-spacing: 0.08em; font-size: 1rem; }
.logo em { color: var(--accent); font-style: normal; }
.topbar-note { flex: 1; font-size: 0.8rem; color: #9aa6b2; }
.topbar button {
  background: none;
  border: 1px solid #3a4450;
  color: #fff;
  border-radius: 6px;
  width: 30px;
  height: 30px;
  font-size: 1rem;
  cursor: pointer;
}

main { padding: 10px; max-width: 560px; margin: 0 auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 10px;
}
.card h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--muted);
  margin-bottom: 8px;
}
.card h3 { font-size: 0.95rem; margin-bottom: 6px; }

.eyebrow {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.hint { font-size: 0.78rem; color: var(--muted); line-height: 1.4; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 8px; }

.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field-label {
  font-size: 0.72rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 4px;
}
input[type="text"], input[type="number"], select {
  font: inherit;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  color: var(--ink);
  min-height: 34px;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="number"] { font-family: var(--mono); }

.help {
  border: 1px solid var(--line);
  background: #f3f5f7;
  color: var(--muted);
  border-radius: 50%;
  width: 16px;
  height: 16px;
  font-size: 0.68rem;
  line-height: 1;
  cursor: pointer;
  flex: none;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 0.85rem;
  cursor: pointer;
}
.toggle-row input { width: 17px; height: 17px; accent-color: var(--accent); }

.scenario-name { font-weight: 600; }

.item-card {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  margin-bottom: 8px;
  background: #fafbfc;
}
.item-head { display: flex; gap: 6px; margin-bottom: 6px; }
.item-head input { font-weight: 600; }

.split-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin-top: 8px;
  padding-top: 7px;
  border-top: 1px dotted var(--line);
  font-size: 0.76rem;
  color: var(--muted);
}
.split-preview b { color: var(--ink); font-weight: 600; }

.icon-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  width: 34px;
  flex: none;
  cursor: pointer;
  font-size: 1rem;
}
.icon-btn.danger { color: var(--neg); }

.add-btn {
  width: 100%;
  padding: 8px;
  border: 1px dashed var(--line);
  background: none;
  color: var(--accent);
  border-radius: 6px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  cursor: pointer;
  margin-bottom: 10px;
}
.primary-btn.small { width: auto; padding: 7px 14px; margin: 0; font-size: 0.82rem; }

.num {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.num.pos { color: var(--pos); }
.num.neg { color: var(--neg); }

.kpi-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px dotted var(--line);
  font-size: 0.85rem;
}
.kpi-row:last-child { border-bottom: none; }
.kpi-row.strong { font-weight: 700; border-bottom-style: solid; }
.kpi-row .field-label { font-size: inherit; }

.result-head { margin-bottom: 8px; }
.result-head h2 {
  font-size: 1.05rem;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
  margin: 2px 0;
}

.waterfall { font-size: 0.85rem; }
.wf-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0 4px 10px;
  border-left: 2px solid var(--line);
}
.wf-row.wf-start { border-left-color: var(--accent); font-weight: 700; }
.wf-row.wf-minus span:last-child { color: var(--neg); }
.wf-row.wf-subtotal { border-top: 1px solid var(--line); font-weight: 600; }
.wf-row.wf-total {
  border-top: 2px solid var(--ink);
  border-left-color: var(--pos);
  font-weight: 700;
}

.explain-links { display: flex; flex-wrap: wrap; gap: 6px; }
.link-btn {
  border: 1px solid var(--line);
  background: #f3f5f7;
  border-radius: 999px;
  padding: 5px 10px;
  font: inherit;
  font-size: 0.78rem;
  color: var(--accent);
  cursor: pointer;
}

.warn-card { background: var(--warn-bg); border-color: var(--warn-border); }
.warn-card h2 { color: var(--warn-ink); }
.warn-list { padding-left: 16px; color: var(--warn-ink); font-size: 0.8rem; line-height: 1.45; }
.warn-list li { margin-bottom: 5px; }
.err-list { padding-left: 16px; color: var(--neg); font-size: 0.85rem; line-height: 1.5; }

.scenario-tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.scenario-tools button, .file-btn {
  font: inherit;
  font-size: 0.8rem;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
.scenario-tools .danger { color: var(--neg); }
.file-btn { position: relative; overflow: hidden; display: inline-block; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.scenario-card { border-left: 3px solid var(--accent); }
.scenario-head { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 6px; }
.cmp-check input { width: 18px; height: 18px; accent-color: var(--accent); margin-top: 3px; }
.scenario-kpis { border-top: 1px solid var(--line); padding-top: 4px; }
.scenario-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.scenario-actions button {
  font: inherit;
  font-size: 0.78rem;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
.scenario-actions .danger { color: var(--neg); }

.table-wrap { overflow-x: auto; }
.cmp-table { border-collapse: collapse; width: 100%; font-size: 0.8rem; }
.cmp-table th, .cmp-table td {
  border-bottom: 1px solid var(--line);
  padding: 5px 8px;
  text-align: right;
}
.cmp-table th:first-child, .cmp-table td:first-child { text-align: left; }
.cmp-table th { font-size: 0.72rem; color: var(--muted); }
.cmp-max { background: #ecfdf3; color: var(--pos); font-weight: 600; }
.cmp-min { background: #fef3f2; color: var(--neg); }

.calc-steps { font-size: 0.88rem; }
.calc-step {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px dotted var(--line);
}
.calc-step:last-child { border-bottom: 2px solid var(--ink); font-weight: 700; }

.admin-actions { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
#admin-body h3 { margin: 12px 0 6px; font-size: 0.85rem; }

.tabbar {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.tabbar button {
  flex: 1;
  padding: 9px 2px;
  border: none;
  background: none;
  font: inherit;
  font-size: 0.74rem;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tabbar button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 700; }

#modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 25, 32, 0.55);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  max-height: 75vh;
  overflow-y: auto;
  padding: 14px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.modal-head h3 { font-size: 1rem; }
#modal-close {
  border: none;
  background: #f3f5f7;
  border-radius: 6px;
  width: 30px;
  height: 30px;
  font-size: 1.1rem;
  cursor: pointer;
}
#modal-body p { font-size: 0.88rem; line-height: 1.5; }

#toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 0.82rem;
  z-index: 50;
  max-width: 90vw;
}

@media (prefers-reduced-motion: no-preference) {
  .tab { animation: fade 0.15s ease; }
  @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } }
}
