:root {
  --bg: #0d0e11;
  --panel: #15171c;
  --panel-2: #1a1d23;
  --text: #e6e4dd;
  --muted: #8a8d96;
  --muted-2: #5f626a;
  --accent: #d4a259;
  --accent-2: #ecc488;
  --error: #c97a7a;
  --border: #24272e;
  --border-soft: #1d2026;

  --sans: "IBM Plex Sans", ui-sans-serif, system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.container {
  max-width: 1060px;
  margin: 0 auto;
  padding: 56px 24px 80px;
}

/* Inputs on the left, reference tables on the right. */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 28px;
  align-items: start;
}

/* Keep the sources list at a comfortable reading width; the header rule spans full width. */

/* ── Masthead ──────────────────────────────────────────── */
.masthead {
  margin-bottom: 32px;
}

.masthead-row {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

/* Keep the title and subtitle at a comfortable reading width. */
.masthead h1,
.subtitle {
  max-width: 760px;
}

.issue-link {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto; /* push to the far right edge of the masthead */
  margin-top: 4px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--muted);
  text-decoration: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  transition:
    color 0.15s,
    border-color 0.15s;
}

.issue-link:hover {
  color: var(--accent-2);
  border-color: var(--accent);
}

h1 {
  font-size: 30px;
  font-weight: 500;
  letter-spacing: -0.015em;
  margin: 0 0 8px;
  line-height: 1.15;
}

.subtitle {
  color: var(--muted);
  margin: 0;
  font-size: 14px;
}

/* ── Form ──────────────────────────────────────────────── */
.form {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
}

fieldset {
  border: 0;
  padding: 14px 22px;
  margin: 0;
  /* <fieldset> defaults to min-inline-size: min-content, which stops it from
     shrinking below its content and overflows narrow (mobile) viewports. */
  min-width: 0;
}

fieldset:first-of-type {
  padding-top: 22px;
}

fieldset:last-of-type {
  padding-bottom: 20px;
}

/* Render each legend as an enclosed header bar matching the right-column panel
   headers (e.g. "OPTIMIZE PLAN"): amber title, #13151a background, and a divider
   beneath. Negative margins pull the bar out to the fieldset edges (cancelled by
   equal padding) so it spans the whole panel width like a panel-head does. */
.legend {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 -22px 16px;
  padding: 12px 22px;
  background: #13151a;
  border-bottom: 1px solid var(--border-soft);
}

/* The first fieldset's top padding would leave a band of panel colour above the
   first header bar; pull the bar up so it sits flush with the form's top edge. */
fieldset:first-of-type .legend {
  margin-top: -22px;
  border-radius: 4px 4px 0 0;
}

/* Later section headers (Star range, Modifiers) get a divider on top too, so
   each section reads as its own enclosed block. */
fieldset:not(:first-of-type) .legend {
  border-top: 1px solid var(--border-soft);
}

.row {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.row + .row {
  margin-top: 14px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 220px;
  /* Without this, a long <select> option (min-content auto) keeps the label
     from shrinking or wrapping, so it overflows on narrow screens. */
  min-width: 0;
}

.field-label {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.field-label .hint,
.check .hint {
  font-style: normal;
  color: var(--muted-2);
  letter-spacing: 0.04em;
}

.check .hint:not(:empty) {
  margin-left: 4px;
  font-size: 11px;
}

/* ── Enhancement Mode slider ───────────────────────────── */
.slider-field {
  display: flex;
  flex-direction: column;
  gap: 9px;
  flex: 1;
}

.slider {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  height: 4px;
  margin: 4px 0 0;
  border-radius: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -7px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: pointer;
  transition: background 0.12s ease;
}

.slider::-webkit-slider-thumb:hover {
  background: var(--accent-2);
}

.slider::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
}

.slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: pointer;
}

.slider::-moz-range-thumb:hover {
  background: var(--accent-2);
}

.slider::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: var(--panel-2);
}

.slider:focus-visible {
  outline: none;
}
.slider:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 3px rgba(212, 162, 89, 0.35);
}
.slider:focus-visible::-moz-range-thumb {
  box-shadow: 0 0 0 3px rgba(212, 162, 89, 0.35);
}

.slider-ticks {
  display: flex;
  justify-content: space-between;
  padding: 0 8px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.06em;
  color: var(--muted-2);
}

.slider-value {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
}

input[type="number"],
select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 9px 11px;
  font: inherit;
  font-feature-settings: "tnum" 1;
  transition:
    border-color 120ms ease,
    background 120ms ease;
}

input[type="number"]:hover,
select:hover {
  border-color: #2f333c;
}

input[type="number"]:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  background: #1c1f25;
}

select {
  appearance: none;
  -webkit-appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 16px) 50%,
    calc(100% - 11px) 50%;
  background-size:
    5px 5px,
    5px 5px;
  background-repeat: no-repeat;
  padding-right: 30px;
}

/* Checkboxes */
.checks {
  gap: 22px;
}

.check {
  flex: 0 1 auto;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: var(--text);
  font-size: 13.5px;
}

.check em {
  font-style: normal;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11.5px;
  margin-left: 2px;
}

.check input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 2px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition:
    border-color 120ms ease,
    background 120ms ease;
}

.check input[type="checkbox"]:hover {
  border-color: #3a3e48;
}

.check.is-disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.check.is-disabled input[type="checkbox"] {
  cursor: not-allowed;
}

.check input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.check input[type="checkbox"]:checked::after {
  content: "";
  width: 7px;
  height: 4px;
  border-left: 1.5px solid #1c1304;
  border-bottom: 1.5px solid #1c1304;
  transform: translateY(-1px) rotate(-45deg);
}

/* Actions */
.actions {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 22px;
  border-top: 1px solid var(--border-soft);
  background: #131519;
  border-radius: 0 0 4px 4px;
}

button {
  background: var(--accent);
  color: #1c1304;
  border: 0;
  border-radius: 3px;
  padding: 9px 18px;
  font: 500 13px var(--sans);
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background 120ms ease;
}

button:hover {
  background: var(--accent-2);
}
button:active {
  transform: translateY(0.5px);
}
button:disabled,
button.is-running {
  cursor: progress;
  background: var(--panel-2);
  color: var(--muted);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.error {
  color: var(--error);
  font-size: 13px;
}

.event-note {
  margin: -4px 0 0;
  padding: 9px 12px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 2px solid var(--accent);
  border-radius: 4px;
}

.event-note p {
  margin: 0;
}

.event-note p + p {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.event-note strong {
  color: var(--accent-2);
  font-weight: 600;
}

.event-note.hidden {
  display: none;
}

/* Keep the gap before the next row constant whether or not the note is shown —
the note's presence in the DOM breaks the .row + .row spacing chain. */
.event-note + .row {
  margin-top: 14px;
}

/* ── Results ───────────────────────────────────────────── */
.results {
  margin-top: 40px;
}

.results.hidden {
  display: none;
}

.section-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.section-head .rule {
  flex: 1;
  height: 1px;
  background: var(--border);
}

.section-title {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted-2);
}

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 14px 16px;
}

.card-label {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.card-value {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 500;
  color: var(--text);
  margin-top: 6px;
  font-feature-settings: "tnum" 1;
  letter-spacing: -0.01em;
}

/* Stat panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 3px;
  margin-bottom: 18px;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
  background: #13151a;
}

.panel-title {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
}

.panel-unit {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.08em;
  color: var(--muted-2);
}

.panel-body {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(0, 2fr);
  gap: 0;
}

/* ── Enhancement Mode cost reference ───────────────────── */
.reference {
  margin-top: 0;
}

.reference .panel:last-child {
  margin-bottom: 0;
}

.reference .panel-body {
  display: block;
}

.mode-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 13px;
}

.mode-table th,
.mode-table td {
  text-align: right;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-soft);
  white-space: nowrap;
}

.mode-table th:first-child,
.mode-table td:first-child {
  text-align: left;
}

.mode-table thead th {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted-2);
  font-weight: 500;
}

.mode-table tbody td {
  color: var(--text);
}

.mode-table tbody td:first-child {
  color: var(--muted);
}

.mode-table tbody tr:last-child td {
  border-bottom: 0;
}

.mode-table .num {
  color: var(--accent);
}

.mode-table .num.zero {
  color: var(--muted-2);
}

/* Boom cells with an active reduction show the old value (struck through)
   beside the new one. Inline on desktop; stacked on mobile (see media query). */
.boom-old {
  text-decoration: line-through;
  color: var(--muted-2);
}

.boom-new {
  margin-left: 5px;
}

[data-mode-col].active-mode-col {
  background: rgba(212, 162, 89, 0.08);
}

th[data-mode-col].active-mode-col {
  color: var(--accent);
}

.mode-table .t-note {
  margin-left: 6px;
  font-size: 11px;
  color: var(--muted-2);
}

.table-foot {
  margin: 0;
  padding: 12px 16px 14px;
  border-top: 1px solid var(--border-soft);
  font-size: 12px;
  line-height: 1.55;
  color: var(--muted);
}

.table-sub {
  display: block;
  font-size: 10.5px;
  color: var(--muted);
  margin-top: 2px;
}

.stat-list {
  margin: 0;
  padding: 14px 0;
  border-right: 1px solid var(--border-soft);
}

.stat-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 5px 18px;
  gap: 12px;
}

.stat-line dt {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0;
}

.stat-line dd {
  font-family: var(--mono);
  font-size: 13.5px;
  font-feature-settings: "tnum" 1;
  color: var(--text);
  margin: 0;
  text-align: right;
}

.stat-line--accent dt {
  color: var(--accent);
}

.stat-line--accent dd {
  color: var(--accent-2);
  font-weight: 500;
}

.stat-line--divider {
  border-bottom: 1px solid var(--border-soft);
  padding-bottom: 11px;
  margin-bottom: 6px;
}

/* Chart */
.chart {
  position: relative;
  padding: 12px 16px 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.hist-tooltip {
  position: absolute;
  top: 8px;
  left: 8px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  font-family: var(--mono);
  font-size: 11px;
  pointer-events: none;
  white-space: nowrap;
  z-index: 10;
  display: none;
}

#histogram,
#histogram-booms {
  width: 100%;
  max-width: 100%;
  height: 180px;
  display: block;
}

@media (max-width: 880px) {
  .layout {
    /* minmax(0, 1fr) — not bare 1fr — so the wide reference tables can't
       inflate the single shared track and push the form off-screen. */
    grid-template-columns: minmax(0, 1fr);
    gap: 22px;
  }
  .reference {
    margin-top: 4px;
  }
  /* Let the 5-column reference tables scroll within their panel instead of
     overflowing the page when they can't shrink to fit. */
  .reference .panel-body {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 600px) {
  .panel-body {
    grid-template-columns: 1fr;
  }
  .stat-list {
    border-right: 0;
    border-bottom: 1px solid var(--border-soft);
    padding: 10px 0;
  }
}

/* ── Colophon ──────────────────────────────────────────── */
.colophon {
  margin-top: 56px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.sources {
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
  max-width: 760px;
}

.source {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 16px;
  align-items: baseline;
}

.source dt {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted-2);
  margin: 0;
}

.source dd {
  margin: 0;
  color: var(--text);
  font-size: 13px;
}

.colophon a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition:
    border-color 120ms ease,
    color 120ms ease;
}

.colophon a:hover {
  color: var(--accent-2);
  border-bottom-color: var(--accent-2);
}

.src-sep {
  color: var(--muted-2);
  margin: 0 6px;
}

@media (max-width: 540px) {
  .source {
    grid-template-columns: 1fr;
    gap: 2px;
  }
}

/* ── Small screens ─────────────────────────────────────── */
@media (max-width: 540px) {
  .container {
    padding: 36px 18px 56px;
  }
  h1 {
    font-size: 24px;
  }
  fieldset {
    padding: 16px 16px;
  }
  /* Keep the legend bar flush with the narrower mobile fieldset padding. */
  .legend {
    margin-left: -16px;
    margin-right: -16px;
    padding-left: 16px;
    padding-right: 16px;
  }
  fieldset:first-of-type .legend {
    margin-top: -16px;
  }
  .actions {
    padding: 14px 16px;
    flex-wrap: wrap;
  }
  /* 16px keeps iOS Safari from auto-zooming when a field gets focus. */
  input[type="number"],
  select {
    font-size: 16px;
  }
  /* Tighten the reference tables so the 5 columns fit without needing to
     scroll on a typical phone (the panel keeps overflow-x as a fallback). */
  .mode-table {
    font-size: 11.5px;
  }
  .mode-table th,
  .mode-table td {
    padding: 8px 7px;
  }
  .table-sub {
    font-size: 9.5px;
  }
  /* Stack the old/new boom values so the destroy table stays narrow enough to
     fit without the horizontal scroll that the inline pair forced. */
  .boom-old,
  .boom-new {
    display: block;
    margin-left: 0;
    line-height: 1.35;
  }
  .panel-head {
    padding: 11px 12px;
  }
  .table-foot {
    padding: 11px 12px 12px;
  }
}

/* ── Utility ───────────────────────────────────────────── */
.hidden {
  display: none !important;
}

/* ── Tabs (Quick / Per-star) ───────────────────────────── */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 22px;
  border-bottom: 1px solid var(--border);
}

.tab {
  appearance: none;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 9px 16px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  cursor: pointer;
  transition:
    color 120ms ease,
    border-color 120ms ease;
}

.tab:hover {
  /* Override the global button:hover amber fill — tabs are an underline style,
     not filled buttons; hover only shifts the text colour. */
  background: none;
  color: var(--text);
}

.tab:active {
  transform: none;
}

.tab.is-active,
.tab.is-active:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

/* ── Per-star strategy matrix ──────────────────────────── */
.plan-table td,
.plan-table th {
  text-align: center;
}

.plan-table th:first-child,
.plan-table td:first-child {
  text-align: left;
}

.plan-table tbody td:first-child {
  color: var(--muted);
  white-space: nowrap;
}

.plan-mode {
  font-family: var(--mono);
  font-size: 12px;
  padding: 4px 6px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  cursor: pointer;
}

.plan-sg {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.plan-sg:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* Rows whose star sits outside the current → target range take no part in the
   run; greyed and inert so the active range reads clearly. */
.plan-row--off td {
  opacity: 0.4;
}

.plan-boom {
  color: var(--accent);
}

.plan-boom.zero {
  color: var(--muted-2);
}

.plan-note {
  margin: 0;
  padding: 11px 16px 13px;
  border-top: 1px solid var(--border-soft);
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}

/* ── Optimize tab ──────────────────────────────────────── */
.opt-controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 16px 0;
}

/* Override the global label flex-grow so stacked fields take natural height
   instead of stretching to fill the column. */
.opt-controls label {
  flex: 0 0 auto;
}

.opt-intro {
  margin: 14px 0 0;
  padding: 0 16px;
  font-size: 12px;
  line-height: 1.55;
  color: var(--muted);
}

.opt-actions {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
}

.opt-result {
  border-top: 1px solid var(--border-soft);
}

.opt-result.hidden {
  display: none;
}

/* Headline metrics for the recommended plan. */
.opt-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 16px 16px 4px;
}

.opt-summary > div {
  flex: 1 1 120px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 10px 12px;
}

.opt-k {
  display: block;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.opt-v {
  display: block;
  font-family: var(--mono);
  font-size: 17px;
  font-weight: 500;
  color: var(--accent-2);
  margin-top: 5px;
  font-feature-settings: "tnum" 1;
}

.opt-note {
  margin: 0;
  padding: 10px 16px 0;
  font-size: 12px;
  line-height: 1.55;
  color: var(--muted);
}

.opt-note.opt-warn {
  color: var(--error);
}

/* The recommended-plan preview reuses the matrix look. */
.opt-table {
  margin-top: 12px;
}

.opt-table th,
.opt-table td {
  text-align: center;
}

.opt-table th:first-child,
.opt-table td:first-child {
  text-align: left;
}

.opt-table tbody td:first-child {
  color: var(--muted);
  white-space: nowrap;
}

.opt-apply {
  padding: 14px 16px 16px;
}

@media (max-width: 540px) {
  .opt-controls {
    padding: 14px 12px 0;
  }
  .opt-intro {
    padding: 0 12px;
  }
  .opt-actions,
  .opt-apply {
    padding: 14px 12px;
  }
  .opt-summary {
    padding: 14px 12px 4px;
  }
  .opt-note {
    padding: 10px 12px 0;
  }
}
