:root {
  /* Indigo/blue is spent in exactly one place: the chart's alloy/item bars,
   * their legend swatches, and focus rings. Everything else (buttons,
   * active toggles, open-group affordance) uses ink or a neutral tint, so
   * "blue" only ever means one thing on the page. */
  --accent: #4f46e5;
  --accent-alloy: #4f46e5;
  --accent-item: #93c5fd;
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1e2233;
  --text-soft: #5b6178;
  --border: #e4e6ef;
  --danger: #c0362c;
  --danger-bg: #fdf3f2;
  --override: #b45309;
  --override-bg: #fffaf0;
  --override-border: #f0b429;
  /* Market-boost direction, matching the in-game Market dialog's own
   * red/green arrows. --market-down deliberately isn't --danger even
   * though the hue is close — --danger means "invalid input" elsewhere on
   * the page, and the two meanings should be free to diverge later. */
  --market-up: #15803d;
  --market-down: #c0362c;
  /* Tech tree "researched" border, matching the in-game tree's own green
   * highlight. Deliberately not --market-up — "researched" and "price
   * going up" are unrelated ideas and should be free to diverge later. */
  --tech-on: #15803d;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px rgba(18, 22, 45, 0.04), 0 6px 20px rgba(18, 22, 45, 0.06);

  /* Four-step type scale (was eight ad-hoc rem values). */
  --fs-sm: 0.8125rem;
  --fs-base: 0.9375rem;
  --fs-md: 1.0625rem;
  --fs-lg: 1.5rem;

  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  font-size: var(--fs-base);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* This page is mostly numbers (levels, multipliers, prices) — keep digits
 * fixed-width everywhere so they line up in columns instead of jittering. */
input,
select,
.control-hint,
.group-summary,
.stat-table {
  font-variant-numeric: tabular-nums;
}

/* ---- header ---- */
.site-header {
  max-width: 1280px;
  margin: 0 auto;
  padding: 32px 24px 0;
}

.site-header-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 16px 24px;
}

.site-header h1 {
  margin: 0;
  font-size: var(--fs-lg);
  letter-spacing: -0.01em;
}

.tagline {
  margin: 6px 0 0;
  color: var(--text-soft);
  max-width: 60ch;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.import-status {
  display: block;
  margin-top: 8px;
  font-size: var(--fs-sm);
  color: var(--text-soft);
}

main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 24px 64px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---- shared surfaces ----
 * The chart is the one elevated surface on the page (larger radius, the
 * only shadow) — everything else is a flat bordered panel, so the chart
 * reads as the primary thing without competing with six identical cards. */
.chart-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setup,
.market,
.stats {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 20px;
}

.setup h2,
.market h2,
.stats h2 {
  margin: 20px 0 4px;
  font-size: var(--fs-md);
}

.card-sub {
  margin: 0 0 14px;
  color: var(--text-soft);
  font-size: var(--fs-sm);
}

/* ---- market boosts ---- */
.boost-list {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  min-height: 36px;
}

.boost-empty {
  margin: 0;
  color: var(--text-soft);
  font-size: var(--fs-sm);
}

.boost-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 6px 5px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
}

.boost-name {
  font-size: var(--fs-sm);
  font-weight: 600;
  white-space: nowrap;
}

.boost-name-unknown {
  font-weight: 400;
  font-style: italic;
  color: var(--text-soft);
}

.boost-chevrons {
  width: 8px;
  height: 15px;
  flex: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

.boost-up .boost-chevrons {
  color: var(--market-up);
}

.boost-down .boost-chevrons {
  color: var(--market-down);
}

/* Borderless so it reads as part of the tag rather than a separate control. */
.boost-tag select.boost-amount {
  border: 0;
  background: none;
  padding: 0 2px;
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
}

.boost-remove {
  border: 0;
  background: none;
  padding: 0;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  color: var(--text-soft);
  cursor: pointer;
  font-size: 0.7rem;
  line-height: 1;
  flex: none;
}

.boost-remove:hover {
  background: var(--bg);
  color: var(--text);
}

@keyframes boost-enter {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.boost-enter {
  animation: boost-enter 120ms ease-out;
}

.market-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin: 12px 0 14px;
}

.resource-search-wrap {
  position: relative;
}

.resource-search-input {
  font: inherit;
  font-size: var(--fs-base);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  width: 220px;
  max-width: 100%;
}

.resource-search-listbox {
  position: absolute;
  z-index: 5;
  top: calc(100% + 4px);
  left: 0;
  width: max-content;
  min-width: 220px;
  max-width: min(360px, 90vw);
  max-height: 280px;
  overflow-y: auto;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.resource-option {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: var(--fs-sm);
}

.resource-option.active {
  background: var(--bg);
}

.resource-option-name {
  flex: 1;
}

.resource-option-cat {
  color: var(--text-soft);
  font-size: var(--fs-sm);
}

/* ---- disclosure groups ---- */
.group {
  border-top: 1px solid var(--border);
  padding-bottom: 0;
}

.group:first-of-type {
  border-top: 0;
}

.group[open] {
  padding-bottom: 18px;
}

.group > summary {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 16px;
  margin: 0 -20px;
  padding: 14px 20px;
  list-style: none;
}

.group > summary::-webkit-details-marker {
  display: none;
}

.group[open] > summary {
  background: var(--bg);
}

.group > summary::before {
  content: "";
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  margin-right: 2px;
  border-right: 2px solid var(--text-soft);
  border-bottom: 2px solid var(--text-soft);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}

.group[open] > summary::before {
  transform: rotate(45deg);
}

@media (prefers-reduced-motion: reduce) {
  .group > summary::before {
    transition: none;
  }

  .boost-enter {
    animation: none;
  }
}

.group-title {
  font-weight: 600;
}

.group-summary {
  margin-left: auto;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  text-align: right;
}

/* ---- controls (chart toolbar) ---- */
.chart-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 20px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-label {
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-soft);
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.segmented button {
  border: 0;
  background: var(--card);
  padding: 8px 14px;
  font: inherit;
  font-size: var(--fs-base);
  color: var(--text-soft);
  cursor: pointer;
}

.segmented button + button {
  border-left: 1px solid var(--border);
}

.segmented button.active {
  background: var(--text);
  color: #fff;
}

select {
  font: inherit;
  font-size: var(--fs-base);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
}

.btn-secondary {
  font: inherit;
  font-size: var(--fs-base);
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* A quieter action than .btn-secondary, for something that's rarely the
 * thing you came to do (clearing a whole list) sitting right next to
 * something that often is (the search field it wipes). */
.btn-text {
  font: inherit;
  font-size: var(--fs-sm);
  padding: 4px 2px;
  border: 0;
  background: none;
  color: var(--text-soft);
  cursor: pointer;
}

.btn-text:hover {
  color: var(--text);
  text-decoration: underline;
}

/* ---- researched tech tree ---- */
.tech-tree-scroll {
  overflow-x: auto;
}

.tech-tree {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--tech-cols, 8), minmax(0, 1fr));
  grid-template-rows: repeat(var(--tech-rows, 6), auto);
  gap: 6px;
  min-width: 560px;
  padding: 8px 2px;
}

.tech-node {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 2px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  font: inherit;
  cursor: pointer;
}

.tech-node.is-researched {
  border-color: var(--tech-on);
}

.tech-node:not(.is-researched):not(.is-context) {
  opacity: 0.5;
  filter: grayscale(1);
}

.tech-node.is-context {
  border-color: transparent;
  background: none;
  opacity: 0.2;
  cursor: default;
}

.tech-icon {
  width: 32px;
  height: 32px;
  object-fit: contain;
}

.tech-name {
  font-size: 0.6875rem;
  line-height: 1.15;
  text-align: center;
  color: var(--text-soft);
}

.control-hint {
  font-size: var(--fs-sm);
  color: var(--text-soft);
}

/* ---- mothership rooms ---- */
.rooms-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px 32px;
  margin-bottom: 16px;
}

.room-row {
  display: grid;
  grid-template-columns: 96px 60px 52px 1fr;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
}

.room-row input {
  font: inherit;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: right;
}

.room-label {
  font-weight: 600;
  font-size: var(--fs-sm);
}

.room-effect {
  color: var(--text-soft);
  font-size: var(--fs-sm);
}

/* ---- station nodes ---- */
.station-grid {
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-bottom: 16px;
}

.station-category-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 8px;
}

.station-name {
  font-weight: 600;
  font-size: var(--fs-sm);
}

.station-nodes {
  display: grid;
  grid-template-columns: repeat(var(--node-count, 5), minmax(52px, 1fr));
  gap: 10px;
}

.station-node {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.station-node input {
  font: inherit;
  width: 100%;
  padding: 6px 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: center;
}

/* ---- managers / modules ---- */
.manager-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.manager-row input[type="text"] {
  flex: 1;
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.manager-row input[type="number"] {
  width: 90px;
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.manager-remove {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text-soft);
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.manager-remove:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- chart ---- */
.chart-legend {
  display: flex;
  gap: 18px;
  font-size: var(--fs-sm);
  color: var(--text-soft);
}

.chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chart-legend i {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

/* Caps the panel to roughly one viewport with its own internal scroll —
 * with every alloy/item unlocked the canvas grows to ~2800px tall (26px per
 * row), which would otherwise push the settings below a full screen-height
 * of chart. Rows are sorted profit-first by default, so the top of the
 * scroll already answers the question the chart exists to answer. */
.chart-scroll {
  overflow: auto;
  max-height: min(640px, calc(100vh - 220px));
}

.chart-canvas-wrap {
  position: relative;
  min-width: 320px;
}

.chart-note {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--text-soft);
  min-height: 1.2em;
}

/* ---- stat tables ---- */
.table-scroll {
  overflow-x: auto;
}

.stat-table {
  width: 100%;
  min-width: 810px;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}

.stat-table th:nth-child(1),
.stat-table td:nth-child(1) {
  width: 44px;
  text-align: center;
}

.stat-table th:nth-child(2),
.stat-table td:nth-child(2) {
  width: 190px;
}

.stat-table th:nth-child(3),
.stat-table td:nth-child(3) {
  width: 124px;
  text-align: center;
}

.stat-table th:nth-child(4),
.stat-table td:nth-child(4) {
  width: 118px;
}

.stat-table th:nth-child(5),
.stat-table td:nth-child(5) {
  width: 210px;
}

.stat-table th:nth-child(6),
.stat-table td:nth-child(6) {
  width: 120px;
}

.stat-table th,
.stat-table td {
  padding: 6px 8px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.stat-table th.col-name,
.stat-table td.col-name,
.stat-table th.col-ingredients,
.stat-table td.col-ingredients {
  text-align: left;
}

.stat-table th {
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-soft);
}

.stat-table input:not([type="checkbox"]) {
  font: inherit;
  width: 68px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: right;
}

/* Stars is edited primarily via the +/- buttons, so the number itself is
 * borderless at rest (reads as a display value, not a field) and only gains
 * a border on focus, when typing becomes the active path. */
.stars-stepper {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* "input.stars-input", not just ".stars-input", to outrank the generic
 * ".stat-table input:not([type=checkbox])" rule above (same specificity,
 * later in the file) — that rule's plain-class form loses the tie. */
.stat-table input.stars-input {
  width: 36px;
  text-align: center;
  border-color: transparent;
  background: transparent;
}

.stat-table input.stars-input:focus {
  border-color: var(--border);
}

/* Native spinner arrows would be redundant next to .stars-step and don't
 * fit the narrow field; keyboard up/down arrows still work without them. */
.stars-input {
  appearance: textfield;
}

.stars-input::-webkit-inner-spin-button,
.stars-input::-webkit-outer-spin-button {
  appearance: none;
  margin: 0;
}

.stars-step {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text-soft);
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  touch-action: manipulation;
  user-select: none;
}

.stars-step:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.stars-step:disabled {
  opacity: 0.35;
  cursor: default;
}

/* The whole "On" cell is the click target — a bare 13px checkbox is an
 * awkward thing to hit in a dense table. */
.stat-table .unlock-cell {
  padding: 0;
}

.stat-table .unlock-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 8px;
  cursor: pointer;
}

.stat-table .unlock-toggle input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  cursor: pointer;
}

/* Wraps a stat field and its revert button — display:flex on the <td> itself
 * would opt it out of the table's column-width sharing with its header cell,
 * so this flexes an inner wrapper instead. */
.stat-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* A value the player changed away from what the model predicts. */
.stat-table input.overridden {
  color: var(--override);
  border-color: var(--override-border);
  background: var(--override-bg);
}

/* Revert button. Its slot is always reserved (visibility, not display) so it
 * appearing never shifts the row or misaligns stacked ingredient rows. */
.stat-reset {
  flex: none;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--override);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  visibility: hidden;
}

.stat-reset.shown {
  visibility: visible;
}

.stat-reset:hover {
  background: var(--override-bg);
}

/* Effective sell price: computed, not directly editable. */
.stat-table td.price-readonly[data-eff-for] {
  color: var(--text);
  font-weight: 600;
}

input.invalid {
  border-color: var(--danger);
  background: var(--danger-bg);
}

/* Invalid must beat the orange override styling above (higher specificity). */
.stat-table input.overridden.invalid {
  border-color: var(--danger);
  background: var(--danger-bg);
  color: inherit;
}

/* Restated as "input.stars-input.invalid" to match the specificity of (and,
 * by appearing later, win the tie against) ".stat-table input.stars-input:focus"
 * above — otherwise an invalid value in a focused field loses its red border. */
.stat-table input.stars-input.invalid {
  border-color: var(--danger);
  background: var(--danger-bg);
}

.stat-table tr.locked td:not(:first-child) {
  opacity: 0.45;
}

.stat-table td.col-name {
  font-weight: 600;
}

.entity-icon {
  height: 1.1em;
  width: 1.1em;
  object-fit: contain;
  vertical-align: -0.2em;
}

.col-ingredients .ing-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.col-ingredients .ing-row + .ing-row {
  margin-top: 4px;
}

.col-ingredients .ing-name {
  color: var(--text-soft);
}

.col-ingredients .ing-x {
  color: var(--text-soft);
}

.col-ingredients input {
  width: 60px;
}

/* ---- focus ---- */
button:focus-visible,
select:focus-visible,
input:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- responsive ---- */
@media (max-width: 900px) {
  .rooms-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .site-header {
    padding: 24px 16px 0;
  }

  main {
    padding: 16px 16px 48px;
  }

  .chart-panel,
  .setup,
  .market,
  .stats {
    padding-left: 14px;
    padding-right: 14px;
  }

  .group > summary {
    margin: 0 -14px;
    padding: 14px;
  }

  .room-row {
    grid-template-columns: 84px 56px 48px 1fr;
    font-size: var(--fs-sm);
  }

  .station-nodes {
    grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  }
}
