:root {
  --ink: #15202b;
  --muted: #5c6672;
  --line: #d5dbe3;
  --panel: #f3f6f8;
  --accent: #1f4e5f;
  --air: #0b6e4f;
  --low: #9b3a2f;
  --rail-w: 340px;
  --font: "IBM Plex Sans", "Helvetica Neue", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: #e4eaef;
}

a { color: var(--accent); }

.shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-shrink: 0;
  padding: 0.55rem 1rem;
  background: #fff;
  border-bottom: 1px solid var(--line);
  z-index: 6;
}

.top__brand {
  display: flex;
  flex-direction: column;
  gap: 0.02rem;
}

.top__brand a {
  font-size: 0.72rem;
  font-weight: 600;
  text-decoration: none;
  color: var(--muted);
}

.top__brand strong {
  font-size: 1.05rem;
  letter-spacing: -0.03em;
}

.tabs { display: flex; flex-wrap: wrap; gap: 0.35rem; }

.tabs button {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 0.38rem 0.7rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.tabs button.is-on {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  flex: 1;
  min-height: 0;
}

.rail {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  z-index: 5;
}

.rail__section {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
}

.rail__section--detail {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.rail__label {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.rail__hint {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
}

select {
  width: 100%;
  font: inherit;
  font-size: 0.88rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.stat {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.stat__value {
  margin: 0.15rem 0 0;
  font-family: var(--mono);
  font-size: 1.55rem;
  letter-spacing: -0.04em;
  color: var(--accent);
}

.stat__value.is-low { color: var(--low); }
.stat__value.is-high { color: var(--air); }

.water {
  margin: 0.7rem 0 0;
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.water th { display: none; }

.water td {
  padding: 0.18rem 0;
  border-bottom: 1px solid var(--line);
}

.water td.num {
  text-align: right;
  font-family: var(--mono);
}

.water tr.total td {
  font-weight: 700;
  border-bottom: 0;
  padding-top: 0.4rem;
}

.ends {
  margin: 0 0 0.25rem;
  padding: 0;
  list-style: none;
}

.rail__section--detail > .rail__label {
  margin: 0.65rem 0 0.15rem;
}

.rail__section--detail > .rail__label:first-child,
.rail__section--detail > #picked + .rail__label {
  margin-top: 0.15rem;
}

.ends li {
  border-top: 1px solid var(--line);
}

.ends__hit {
  display: grid;
  grid-template-columns: 1.5rem minmax(0, 1fr) auto;
  gap: 0.35rem;
  align-items: baseline;
  padding: 0.28rem 0;
  cursor: pointer;
  font-size: 0.8rem;
}

.ends .rank {
  font-family: var(--mono);
  color: var(--muted);
  font-size: 0.78rem;
}

.ends .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ends li.is-on .name { font-weight: 600; }

.ends .amt {
  font-family: var(--mono);
  font-size: 0.78rem;
}

.ends__detail,
#picked {
  padding: 0 0 0.35rem;
}

#picked .stat__value {
  font-size: 1.45rem;
}

.rail__foot {
  padding: 0.7rem 1rem 0.9rem;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.map-pane { position: relative; min-width: 0; min-height: 0; }

.map {
  width: 100%;
  height: 100%;
  background: #dce5ec;
}

.map-legend {
  position: absolute;
  right: 12px;
  bottom: 18px;
  z-index: 500;
  margin: 0;
  padding: 0.45rem 0.55rem;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.72rem;
  color: var(--muted);
}

.legend-ramp {
  display: flex;
  height: 8px;
  border-radius: 99px;
  overflow: hidden;
  margin-bottom: 0.25rem;
}

.legend-ramp span { flex: 1; }

.rail-close { display: none; }

.rail-toggle {
  display: none;
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 1000;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
}

.app[hidden],
.insights[hidden] {
  display: none !important;
}

.insights {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: #eef2f5;
}

.insights__inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 1.25rem 1.1rem 3rem;
}

.insights__head h1,
.sources h2 {
  margin: 0;
  letter-spacing: -0.03em;
}

.insights__head p,
.caveat,
.sources p {
  margin: 0.4rem 0 0.85rem;
  color: var(--muted);
  line-height: 1.45;
  max-width: 46rem;
}

.table-wrap {
  overflow-x: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.data-table th,
.data-table td {
  padding: 0.45rem 0.65rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
}

.data-table th {
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.data-table td.num,
.data-table th.num {
  text-align: right;
  font-family: var(--mono);
}

.data-table tr.is-on td { background: #eef6f2; }

.data-table td.is-low { color: var(--low); font-weight: 600; }

.sources { margin-top: 2.1rem; }

.source-files {
  margin: 0.4rem 0 0;
  padding-left: 1.1rem;
  color: var(--muted);
  line-height: 1.5;
}

code {
  font-family: var(--mono);
  font-size: 0.84em;
}

.modes {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0.35rem 0 0.6rem;
}

.modes button {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 0.35rem 0.65rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.modes button.is-on {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.insights__inner .step { margin-top: 0.9rem; }
.insights__inner .step:first-of-type { margin-top: 0; }
.insights__inner .table-wrap { margin-top: 0.65rem; }

.pie-panel {
  display: grid;
  grid-template-columns: minmax(220px, 280px) 1fr;
  gap: 1.1rem 1.5rem;
  align-items: center;
  margin-top: 0.85rem;
}

.pie {
  width: 100%;
  max-width: 280px;
}

.pie svg { width: 100%; height: auto; display: block; }

.pie-legend {
  list-style: none;
  margin: 0;
  padding: 0;
}

.pie-legend li {
  display: grid;
  grid-template-columns: 0.7rem 1fr auto auto;
  gap: 0.45rem 0.55rem;
  align-items: baseline;
  padding: 0.28rem 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.84rem;
}

.pie-legend li:last-child { border-bottom: 0; }

.pie-legend .swatch {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 2px;
  align-self: center;
}

.pie-legend .name { color: var(--ink); }

.pie-legend li.is-out .name { color: var(--muted); }

.pie-legend .pct,
.pie-legend .amt {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
}

.pie-legend .tag {
  grid-column: 2 / -1;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--air);
  margin-top: -0.15rem;
}

.pie-legend li.is-out .tag { color: var(--muted); }

.whatif-controls {
  display: grid;
  grid-template-columns: minmax(12rem, 1.5fr) minmax(8rem, 0.7fr) minmax(12rem, 1.3fr);
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}

.whatif-controls .field input {
  width: 100%;
  font: inherit;
  font-size: 0.88rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.me-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-top: 0.65rem;
}

.me-grid--costs {
  grid-template-columns: repeat(3, 1fr);
}

.me-grid .field input,
.me-grid .field select {
  width: 100%;
  font: inherit;
  font-size: 0.88rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.ghost {
  appearance: none;
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.me-move {
  max-width: 28rem;
  margin: 0 0 0.75rem;
}

.me-sliders {
  display: grid;
  gap: 1rem;
  margin-top: 0.35rem;
}

.me-slider__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  font-size: 0.88rem;
  font-weight: 600;
}

.me-slider__nums {
  display: flex;
  gap: 0.75rem;
  align-items: baseline;
  font-family: var(--mono);
  font-weight: 500;
}

.me-slider input[type="range"] {
  width: 100%;
  margin: 0.35rem 0 0;
  accent-color: var(--air);
}

.me-slider__hint {
  margin: 0.2rem 0 0;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
}

.delta-up { color: var(--air); font-weight: 600; }
.delta-down { color: var(--low); font-weight: 600; }

.moves {
  margin: 0.55rem 0 0;
  padding-left: 1.25rem;
  color: var(--ink);
  line-height: 1.5;
}

.method-list strong {
  color: var(--ink);
}

.method-list em {
  font-style: italic;
}

.step {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem 1.05rem 1.1rem;
  margin-top: 0.75rem;
}

.step h2 {
  margin: 0.15rem 0 0;
  font-size: 1.15rem;
  letter-spacing: -0.03em;
}

.step__kicker {
  margin: 0;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.step__note {
  margin: 0.4rem 0 0.7rem;
  color: var(--muted);
  line-height: 1.45;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
}

.field[hidden] {
  display: none !important;
}

.field input,
.field select {
  font-weight: 500;
  color: var(--ink);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0 0 0.65rem;
}

.step .chips { margin-top: 0.7rem; }

.chips button {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 99px;
  padding: 0.28rem 0.65rem;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
}

.chips button.is-on {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

@media (max-width: 820px) {
  .top { flex-wrap: wrap; }
  .tabs { width: 100%; }
  .whatif-controls { grid-template-columns: 1fr; }
  .me-grid,
  .me-grid--costs { grid-template-columns: 1fr; }
  .pie-panel { grid-template-columns: 1fr; justify-items: center; }
  .app { grid-template-columns: 1fr; }
  .rail {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(340px, 92vw);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.08);
  }
  .rail.is-open { transform: none; }
  .rail-toggle, .rail-close {
    display: block;
  }
  .rail-close {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 2;
    width: 36px;
    height: 36px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
  }
}
