:root {
  --bg: #0c1017;
  --bg-2: #131a24;
  --bg-3: #1a2330;
  --line: #33415a;
  --text: #dfe6f0;
  --muted: #8b9ab1;
  --accent: #4fd1c5;
  --refiner: #f0a04b;
  --craft: #6ea8fe;
  --cook: #9ae66e;
  --warn: #ff8a80;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Segoe UI", system-ui, sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 10px 16px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo { font-size: 26px; color: var(--accent); }
.brand h1 { margin: 0; font-size: 17px; letter-spacing: .3px; }
.sub { margin: 0; font-size: 11.5px; color: var(--muted); }

.controls { display: flex; align-items: flex-end; gap: 18px; flex-wrap: wrap; }
.ctl { display: flex; flex-direction: column; gap: 4px; }
.ctl > span { font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.ctl input[type=number] {
  width: 78px; padding: 5px 7px; background: var(--bg-3); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
}
.kindrow { display: flex; gap: 6px; align-items: center; }

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 9px; border-radius: 999px; cursor: pointer;
  background: var(--bg-3); border: 1px solid var(--line); font-size: 12px;
  user-select: none;
}
.chip input { accent-color: currentColor; margin: 0; }
.chip.k-refiner { color: var(--refiner); }
.chip.k-craft { color: var(--craft); }
.chip.k-cook { color: var(--cook); }
.chip.plain { color: var(--muted); }

.btn {
  padding: 4px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
  background: var(--bg-3); color: var(--text); border: 1px solid var(--line);
}
.btn:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- layout ---------- */

main {
  flex: 1;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr) 270px;
  min-height: 0;
}

@media (max-width: 1100px) {
  main { grid-template-columns: 200px minmax(0, 1fr) 220px; }
}

.panel {
  background: var(--bg-2);
  padding: 12px;
  overflow-y: auto;
  min-height: 0;
}
.search-panel { border-right: 1px solid var(--line); }
.totals-panel { border-left: 1px solid var(--line); }

.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .7px; color: var(--muted); margin: 0 0 6px; }
.panel h2.mt { margin-top: 20px; }
.hint { margin: 0 0 8px; font-size: 11px; color: var(--muted); }

#q {
  width: 100%; padding: 8px 10px; margin-bottom: 10px;
  background: var(--bg-3); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius);
}
#q:focus { outline: none; border-color: var(--accent); }

.results, .totals, .uses { list-style: none; margin: 0; padding: 0; }

.results li {
  display: flex; align-items: center; gap: 8px;
  padding: 6px; border-radius: 6px; cursor: pointer;
}
.results li:hover { background: var(--bg-3); }
.results li.active { background: var(--bg-3); outline: 1px solid var(--accent); }
.results .rname { flex: 1; min-width: 0; }
.results .rname b { display: block; font-weight: 500; }
.results .rname small { color: var(--muted); font-size: 11px; }
.results .count {
  font-size: 10.5px; color: var(--muted); background: var(--bg);
  padding: 1px 6px; border-radius: 999px; white-space: nowrap;
}
.results .empty { color: var(--muted); padding: 6px; cursor: default; }

.totals li, .uses li {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 4px; border-bottom: 1px solid #ffffff0d;
}
.totals .tqty { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 600; }
.totals .partial { color: var(--warn); }
.uses li { cursor: pointer; font-size: 12.5px; }
.uses li:hover { background: var(--bg-3); }
.uses .u-op { color: var(--muted); font-size: 11px; display: block; }

/* ---------- icons ---------- */

.ico {
  width: 34px; height: 34px; flex: 0 0 34px;
  display: grid; place-items: center;
  border-radius: 6px; background: #ffffff0a; object-fit: contain;
}
.ico.letter { font-weight: 700; font-size: 13px; color: #0b0f16; }
.ico.sm { width: 24px; height: 24px; flex-basis: 24px; font-size: 11px; }

/* ---------- tree ---------- */

.tree-panel { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tree-head {
  padding: 8px 16px; border-bottom: 1px solid var(--line);
  color: var(--muted); font-size: 12px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap;
}
.tree-head b { color: var(--text); }
.tree { flex: 1; overflow: auto; padding: 20px 28px; min-height: 0; }

.node { display: flex; align-items: center; }

.branch {
  display: flex; flex-direction: column; justify-content: center;
  position: relative; margin-left: 26px;
}
.branch::before {
  content: ''; position: absolute; left: -26px; top: 50%;
  width: 26px; border-top: 2px solid var(--line);
}
.branch > .node { position: relative; padding: 5px 0 5px 26px; }
.branch > .node::before {
  content: ''; position: absolute; left: 0; top: 50%;
  width: 26px; border-top: 2px solid var(--line);
}
.branch > .node::after {
  content: ''; position: absolute; left: 0; top: 0; bottom: 0;
  border-left: 2px solid var(--line);
}
.branch > .node:first-child::after { top: 50%; }
.branch > .node:last-child::after { bottom: 50%; }
.branch > .node:only-child::after { display: none; }

/* Mirrored layout: ingredients on the left, flowing right towards the target.
   The card/branch order is swapped in the DOM rather than with row-reverse, so
   the tree still grows rightwards and stays reachable by the horizontal scrollbar. */
body.flip .branch { margin-left: 0; margin-right: 26px; }
body.flip .branch::before { left: auto; right: -26px; }
body.flip .branch > .node { padding-left: 0; padding-right: 26px; }
body.flip .branch > .node::before { left: auto; right: 0; }
body.flip .branch > .node::after {
  left: auto; right: 0;
  border-left: none; border-right: 2px solid var(--line);
}
/* Siblings stretch to the widest one's width (needed so the spine stays a
   straight line); pack each node's own content against the connector edge
   so the stub always touches the card instead of floating in the slack. */
body.flip .node { justify-content: flex-end; }
/* Keep the accent stripe and the expand button on the side facing the children. */
body.flip .icard {
  border-left: 1px solid var(--line);
  border-right: 4px solid var(--item-colour, var(--muted));
}
body.flip .icard.root { border-left-color: var(--accent); }
body.flip .rcard {
  border-left: 1px solid var(--line);
  border-right: 4px solid var(--kind, var(--line));
}
body.flip .icard .toggle { order: -1; }

/* item card */

.icard {
  display: flex; align-items: center; gap: 9px;
  background: var(--bg-2); border: 1px solid var(--line);
  border-left: 4px solid var(--item-colour, var(--muted));
  border-radius: var(--radius); padding: 6px 10px 6px 8px;
  white-space: nowrap; position: relative; z-index: 1;
}
.icard.root { background: var(--bg-3); border-color: var(--accent); }
.icard .iname { font-weight: 500; cursor: pointer; }
.icard .iname:hover { color: var(--accent); text-decoration: underline; }
.icard .igrp { display: block; font-size: 10.5px; color: var(--muted); font-weight: 400; }
.icard .qty {
  font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent);
  background: #4fd1c518; padding: 2px 7px; border-radius: 999px; font-size: 12.5px;
}
.icard .tag {
  font-size: 10px; text-transform: uppercase; letter-spacing: .5px;
  padding: 2px 6px; border-radius: 4px; background: var(--bg); color: var(--muted);
}
.icard .tag.cycle { color: var(--warn); background: #ff8a8018; }
.icard .tag.leaf { color: var(--muted); }

.toggle {
  border: 1px solid var(--line); background: var(--bg);
  color: var(--muted); border-radius: 999px; cursor: pointer;
  font-size: 11px; padding: 2px 8px; white-space: nowrap;
}
.toggle:hover { border-color: var(--accent); color: var(--accent); }
.toggle.open { color: var(--text); }

/* recipe card */

.rcard {
  display: flex; align-items: center; gap: 9px;
  background: var(--bg-2); border: 1px solid var(--line);
  border-left: 4px solid var(--kind, var(--line));
  border-radius: var(--radius); padding: 5px 10px 5px 8px;
  white-space: nowrap; cursor: pointer; position: relative; z-index: 1;
}
.rcard:hover { border-color: var(--kind); }
.rcard.chosen { background: #4fd1c510; box-shadow: 0 0 0 1px var(--accent) inset; }
.rcard .opt {
  font-size: 10px; color: var(--muted); background: var(--bg);
  border-radius: 4px; padding: 2px 5px; font-variant-numeric: tabular-nums;
}
.rcard .kind {
  font-size: 10px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--kind); font-weight: 700;
}
.rcard .op { font-size: 12.5px; }
.rcard .yield { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.rcard .loop { font-size: 10px; color: var(--warn); border: 1px solid #ff8a8055; border-radius: 4px; padding: 1px 5px; }

.k-line-refiner { --kind: var(--refiner); }
.k-line-craft   { --kind: var(--craft); }
.k-line-cook    { --kind: var(--cook); }

.placeholder { color: var(--muted); padding: 40px 8px; }
.placeholder kbd {
  background: var(--bg-3); border: 1px solid var(--line);
  border-radius: 4px; padding: 1px 6px; font-size: 12px;
}
