:root {
  --bg: #0d0f14;
  --bg2: #13161e;
  --bg3: #1a1e2a;
  --border: #2a2f3d;
  --accent: #f0a500;
  --accent2: #e06c00;
  --text: #d4dbe8;
  --text-muted: #6b748a;
  --text-dim: #9aa3b8;
  --green: #3ecf6e;
  --red: #e05252;
  --blue: #5b9cf6;
  --node-w: 220px;
  --radius: 6px;
}

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

body {
  font-family: 'Courier New', 'Consolas', monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── Header ─────────────────────────────────────────── */
header {
  background: var(--bg2);
  border-bottom: 2px solid var(--accent);
  padding: 0 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.logo {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-transform: uppercase;
}
.logo span { color: var(--text-dim); font-weight: 400; }

/* ── Layout ─────────────────────────────────────────── */
.app-layout {
  display: flex;
  height: calc(100vh - 60px);
}

.sidebar {
  width: 320px;
  min-width: 280px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}

.main-area {
  flex: 1;
  overflow: auto;
  padding: 2rem;
  position: relative;
}

/* ── Sidebar sections ───────────────────────────────── */
.sidebar-section {
  padding: 1.2rem 1.4rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-label {
  font-size: 0.65rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.7rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.sidebar-label::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

select, input[type="number"], input[type="text"] {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.15s;
  margin-bottom: 0.6rem;
}
select:focus, input:focus { border-color: var(--accent); }
select option { background: var(--bg3); }

.input-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}
.input-row select:first-child { flex: 0 0 110px; margin-bottom: 0; }
.input-row input { flex: 1; margin-bottom: 0; }
.input-row select:last-child { flex: 0 0 70px; margin-bottom: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: all 0.15s;
}
.btn-primary {
  background: var(--accent);
  color: #000;
  width: 100%;
  margin-top: 0.5rem;
}
.btn-primary:hover { background: var(--accent2); }
.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.72rem;
  background: var(--bg3);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }

/* ── Target list ────────────────────────────────────── */
.target-list { display: flex; flex-direction: column; gap: 0.4rem; }
.target-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.7rem;
  font-size: 0.82rem;
  gap: 0.5rem;
}
.target-item .target-name { flex: 1; color: var(--text); }
.target-item .target-rate { color: var(--accent); font-weight: 700; white-space: nowrap; }
.target-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.2rem;
  transition: color 0.15s;
}
.target-remove:hover { color: var(--red); }

/* ── Build checklist in sidebar ─────────────────────── */
.checklist-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.35rem 0;
  font-size: 0.8rem;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
}
.checklist-item:last-child { border-bottom: none; }
.checklist-item input[type="checkbox"] {
  width: 14px;
  min-width: 14px;
  height: 14px;
  margin: 0;
  accent-color: var(--green);
  cursor: pointer;
}
.checklist-item.done .item-label { color: var(--text-muted); text-decoration: line-through; }
.item-label { flex: 1; }
.item-count { color: var(--accent); font-weight: 700; }

/* ── Chain visualization ─────────────────────────────── */
#chain-container {
  min-height: 300px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  gap: 1rem;
  color: var(--text-muted);
}
.empty-icon { font-size: 3rem; opacity: 0.3; }
.empty-text { font-size: 0.9rem; letter-spacing: 0.1em; text-transform: uppercase; }

/* ── Tree nodes ─────────────────────────────────────── */
.tree-root {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.tree-target-block {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-top: 3px solid var(--accent);
  border-radius: var(--radius);
  overflow: hidden;
}

.tree-target-header {
  padding: 0.8rem 1.2rem;
  background: linear-gradient(135deg, var(--bg3), var(--bg2));
  display: flex;
  align-items: center;
  gap: 0.8rem;
  border-bottom: 1px solid var(--border);
}
.tree-target-header .item-icon { font-size: 1.4rem; }
.tree-target-header .item-title { font-size: 1rem; font-weight: 700; }
.tree-target-header .item-rate { margin-left: auto; color: var(--accent); font-size: 0.85rem; }

.tree-body { padding: 1.2rem; }

/* The actual node */
.node {
  position: relative;
  display: flex;
  flex-direction: column;
}

.node-box {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  transition: border-color 0.2s, opacity 0.2s;
  cursor: pointer;
  position: relative;
  min-width: 220px;
  max-width: 320px;
}
.node-box:hover { border-color: var(--accent); }
.node-box.done {
  opacity: 0.45;
  border-color: var(--green);
  background: color-mix(in srgb, var(--green) 6%, var(--bg3));
}
.node-box.done .node-name { text-decoration: line-through; color: var(--text-muted); }

.node-icon { font-size: 1.2rem; flex-shrink: 0; }
.node-info { flex: 1; min-width: 0; }
.node-name { font-size: 0.83rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.node-machine { font-size: 0.7rem; color: var(--text-muted); }
.node-right { text-align: right; flex-shrink: 0; }
.node-qty { font-size: 0.9rem; font-weight: 700; color: var(--accent); }
.node-machines { font-size: 0.7rem; color: var(--blue); }

.done-badge {
  position: absolute;
  top: 4px; right: 4px;
  background: var(--green);
  color: #000;
  font-size: 0.6rem;
  padding: 1px 5px;
  border-radius: 3px;
  font-weight: 700;
  letter-spacing: 0.05em;
}

/* ── Tree connectors ─────────────────────────────────── */
.node-children {
  margin-left: 28px;
  padding-left: 1.5rem;
  border-left: 2px solid var(--border);
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  position: relative;
}
.node-children::before {
  content: '';
  position: absolute;
  left: 0; top: 0;
  width: 1.5rem; height: 1px;
  background: var(--border);
}

.child-connector {
  position: relative;
  padding-left: 0; /* handled by children container */
}
.child-connector::before {
  content: '';
  position: absolute;
  left: -1.5rem;
  top: 50%;
  width: 1.5rem;
  height: 1px;
  background: var(--border);
}

/* Circular dependency — shown as raw but with a warning tint */
.node-box.circular {
  border-color: color-mix(in srgb, var(--red) 50%, var(--border));
  background: color-mix(in srgb, var(--red) 5%, var(--bg3));
}
.node-box.circular .node-name::after {
  content: ' ↺';
  color: var(--red);
  font-size: 0.7em;
  opacity: 0.7;
}
.node-box.raw {
  border-color: color-mix(in srgb, var(--accent2) 40%, var(--border));
  background: color-mix(in srgb, var(--accent2) 5%, var(--bg3));
}
.node-box.raw .node-qty { color: var(--accent2); }

/* ── Summary strip ───────────────────────────────────── */
.summary-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.2rem;
  border-bottom: 1px solid var(--border);
}
.summary-chip {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.3rem 0.7rem;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.summary-chip.raw-chip { border-color: color-mix(in srgb, var(--accent2) 50%, var(--border)); }
.summary-chip.raw-chip .chip-qty { color: var(--accent2); }
.chip-qty { font-weight: 700; color: var(--accent); }

/* ── Progress bar ────────────────────────────────────── */
.progress-wrap {
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}
.progress-bar-bg {
  flex: 1;
  height: 6px;
  background: var(--bg3);
  border-radius: 3px;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--green), #59e88a);
  border-radius: 3px;
  transition: width 0.3s;
}
.progress-label { font-size: 0.75rem; color: var(--text-muted); white-space: nowrap; }

/* ── Icon rendering ──────────────────────────────────── */
.item-icon-img {
  display: inline-block;
  vertical-align: middle;
  image-rendering: pixelated; /* crisp for pixel art icons */
  object-fit: contain;
  flex-shrink: 0;
}
.icon-emoji { font-size: inherit; }
.icon-fallback { color: var(--text-muted); }

/* ── Byproduct chips ─────────────────────────────────── */
.node-byproducts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.25rem;
}
.byproduct-chip {
  background: color-mix(in srgb, var(--green) 10%, var(--bg));
  border: 1px solid color-mix(in srgb, var(--green) 30%, var(--border));
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 0.65rem;
  color: var(--green);
  display: flex;
  align-items: center;
  gap: 0.2rem;
  white-space: nowrap;
}

/* ── Byproduct offset badge (tree nodes) ─────────────── */
.bp-offset-badge {
  font-size: 0.65rem;
  color: var(--green);
  margin-top: 2px;
}
.node-box.fully-offset {
  opacity: 0.5;
  border-color: var(--green);
  background: color-mix(in srgb, var(--green) 8%, var(--bg3));
}
.node-box.fully-offset .node-name { text-decoration: line-through; color: var(--text-muted); }

/* ── Sankey view ─────────────────────────────────────── */
.sankey-wrap {
  overflow: auto;
  padding-bottom: 1rem;
}
.sankey-svg {
  display: block;
  min-width: 100%;
}
.sankey-edge {
  pointer-events: stroke;
  cursor: default;
  transition: stroke-opacity 0.15s;
}
.sankey-edge:hover { stroke-opacity: 0.75 !important; }
.sankey-node { cursor: default; }
.sankey-rect {
  fill: #1a1e2a;
  stroke: #2a2f3d;
  stroke-width: 1.5;
}
.sankey-rect.raw {
  fill: color-mix(in srgb, #e06c00 6%, #1a1e2a);
  stroke: color-mix(in srgb, #e06c00 35%, #2a2f3d);
}
.sankey-rect.has-offset {
  stroke: color-mix(in srgb, #3ecf6e 40%, #2a2f3d);
}
.sankey-name {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  font-weight: 700;
  fill: #d4dbe8;
}
.sankey-rate {
  font-family: 'Courier New', monospace;
  font-size: 10px;
  fill: #f0a500;
  font-weight: 700;
}
.sankey-machine {
  font-family: 'Courier New', monospace;
  font-size: 9px;
  fill: #5b9cf6;
}
.sankey-bp-offset {
  font-family: 'Courier New', monospace;
  font-size: 9px;
  fill: #3ecf6e;
}

/* ── Recipe switcher ─────────────────────────────────── */
.recipe-switcher {
  display: flex;
  gap: 3px;
  margin-top: 4px;
  flex-wrap: wrap;
  padding: 0 0 2px 36px; /* indent past icon */
}
.recipe-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 3px;
  padding: 1px 6px;
  font-family: inherit;
  font-size: 0.62rem;
  cursor: pointer;
  transition: all 0.12s;
  white-space: nowrap;
}
.recipe-btn:hover { border-color: var(--blue); color: var(--blue); }
.recipe-btn.active {
  background: color-mix(in srgb, var(--blue) 15%, var(--bg));
  border-color: var(--blue);
  color: var(--blue);
  font-weight: 700;
}

/* ── View tabs ───────────────────────────────────────── */
.view-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.2rem;
}
.view-tab {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius);
  padding: 0.4rem 1rem;
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: pointer;
  transition: all 0.15s;
}
.view-tab:hover { border-color: var(--accent); color: var(--text); }
.view-tab.active {
  background: color-mix(in srgb, var(--accent) 15%, var(--bg3));
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Machine building selector (settings) ────────────── */
.machine-grid {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.machine-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.machine-row-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  width: 68px;
  flex-shrink: 0;
}
.machine-tiers {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}
.machine-tier-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 5px 3px;
  background: var(--bg3);
  transition: border-color 0.15s, background 0.15s;
  opacity: 0.55;
  position: relative;
}
.machine-tier-label input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
  padding: 0;
}
.machine-tier-label:hover { opacity: 0.85; border-color: var(--text-muted); }
.machine-tier-label.is-selected {
  opacity: 1;
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg3));
  box-shadow: 0 0 0 1px var(--accent) inset;
}
.machine-tier-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
}
.machine-tier-speed {
  font-size: 0.6rem;
  color: var(--accent);
  font-weight: 700;
  white-space: nowrap;
}
.machine-tier-label.is-selected .machine-tier-speed { color: var(--accent); }

/* ── Grouped view ────────────────────────────────────── */
.grouped-view {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}
.grouped-section {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-top: 2px solid color-mix(in srgb, var(--accent) 50%, var(--border));
  border-radius: var(--radius);
  overflow: hidden;
}
.grouped-section-head {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.7rem 1.1rem;
  background: linear-gradient(135deg, var(--bg3), var(--bg2));
  border-bottom: 1px solid var(--border);
}
.grouped-building-icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
.grouped-building-name {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text);
  flex: 1;
}
.grouped-machine-total {
  font-size: 0.8rem;
  color: var(--blue);
  font-weight: 700;
}
.grouped-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1px;
  background: var(--border);
}
.grouped-card {
  background: var(--bg3);
  padding: 0.8rem 1rem;
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  transition: background 0.15s;
}
.grouped-card:hover { background: color-mix(in srgb, var(--accent) 5%, var(--bg3)); }
.grouped-card.raw { background: color-mix(in srgb, var(--accent2) 5%, var(--bg3)); }
.grouped-card.done { opacity: 0.4; }
.grouped-card-icon { flex-shrink: 0; }
.grouped-card-info { flex: 1; min-width: 0; }
.grouped-card-name {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 0.2rem;
}
.grouped-card-rate {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent);
}
.grouped-card-machines {
  font-size: 0.75rem;
  color: var(--blue);
  margin-top: 0.1rem;
}

/* ── Settings section ────────────────────────────────── */
.settings-row { margin-bottom: 0.5rem; }
.settings-label {
  font-size: 0.72rem;
  color: var(--text-dim);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  display: block;
  margin-bottom: 0.4rem;
}

.boost-btns { display: flex; gap: 0.35rem; flex-wrap: wrap; }
.boost-btn {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 0.3rem 0.6rem;
  font-family: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
}
.boost-btn:hover { border-color: var(--accent); color: var(--accent); }
.boost-btn.active {
  background: color-mix(in srgb, var(--accent) 15%, var(--bg3));
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}

.machine-speed-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
}
.machine-speed-row:last-child { border-bottom: none; }
.machine-speed-label { flex: 1; color: var(--text-dim); }
.machine-speed-row input { width: 64px !important; text-align: right; }

/* ── Scrollbar ───────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Utility ─────────────────────────────────────────── */
.gap { margin-top: 0.5rem; }
.text-muted { color: var(--text-muted); font-size: 0.78rem; }

/* ── Toast ───────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--bg3);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 0.7rem 1.2rem;
  font-size: 0.82rem;
  color: var(--accent);
  transform: translateY(60px);
  opacity: 0;
  transition: all 0.25s;
  z-index: 999;
  pointer-events: none;
}
#toast.show { transform: translateY(0); opacity: 1; }

@media (max-width: 700px) {
  .app-layout { flex-direction: column; height: auto; }
  .sidebar { width: 100%; max-height: 50vh; }
}
