:root {
  --bg: #3b3b3b;
  --bg-deep: #2a2a2a;
  --panel: #1f1f1f;
  --line: #e6e6e6;
  --text: #f2f2f2;
  --muted: #a8a8a8;
  --tile: #262626;
  --tile-lit: #777;
  --accent: #ffd500;
  --danger: #ff5a5f;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { height: 100dvh; }
body {
  background: var(--bg-deep);
  color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
  /* Never fake a bold: on a heavy fallback such as Impact it fills the letters in. */
  font-synthesis: style;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button {
  background: #333; border: 1px solid #555; border-radius: 4px; padding: 5px 10px; cursor: pointer;
}
button:hover { background: #444; }
button.primary { background: var(--accent); border-color: var(--accent); color: #111; font-weight: 700; }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger { border-color: var(--danger); color: var(--danger); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input { background: #151515; border: 1px solid #555; border-radius: 4px; padding: 7px 9px; width: 100%; }
label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 10px; }
label input { margin-top: 4px; color: var(--text); font-size: 14px; }
.error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; font-size: 13px; }
.logo {
  font: italic 900 44px/1 "Arial Narrow", "Roboto Condensed", Impact, sans-serif;
  text-transform: uppercase; letter-spacing: 1px; margin: 0;
}
.logo span { font-size: 18px; color: var(--accent); letter-spacing: 2px; }
.logo.small { font-size: 22px; color: var(--text); text-decoration: none; }

/* ---------- home ---------- */
#home { height: 100%; overflow: auto; padding: 0 max(16px, calc(50% - 560px)) 40px; }
.hero { padding: 7vh 0 8px; }
.lead { color: #cfcfcf; margin: 14px 0 22px; max-width: 62ch; font-size: 16px; }
.cta { display: flex; gap: 10px; flex-wrap: wrap; }
button.big { padding: 10px 18px; font-size: 15px; }
#recent { color: var(--muted); font-size: 13px; margin: 16px 0 0; }
a { color: var(--accent); }
.code { font-family: ui-monospace, Consolas, monospace; letter-spacing: 2px; margin-right: 10px; }
#home section { margin-top: 36px; }
#home h2 { font: italic 900 24px/1.1 "Arial Narrow", "Roboto Condensed", Impact, sans-serif; text-transform: uppercase; letter-spacing: .5px; margin: 0 0 6px; }
.sub { color: var(--muted); margin: 0 0 12px; font-size: 13px; }
#explore { height: min(72vh, 660px); min-height: 320px; border: 1px solid #555; border-radius: 6px; overflow: hidden; display: flex; }
.steps { margin: 10px 0 0; padding-left: 22px; color: #cfcfcf; max-width: 75ch; }
.steps li { margin-bottom: 6px; }
.steps b { color: var(--text); }
.branch h3 { font-size: 16px; margin: 22px 0 8px; }
.branch h3 small { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 6px; }
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 5px 10px 5px 0; border-bottom: 1px solid #3a3a3a; white-space: nowrap; }
thead th, #totals-table tr:first-child th { color: var(--muted); font-weight: 400; font-size: 12px; }
#catalogue td:last-child { white-space: normal; color: var(--muted); }
#catalogue td img { width: 24px; height: 24px; object-fit: contain; vertical-align: middle; margin-right: 6px; background: var(--tile); }
#catalogue td:nth-child(n+2):nth-child(-n+5), #totals-table td:nth-child(n+2), #totals-table th:nth-child(n+2) { text-align: right; padding-right: 18px; font-variant-numeric: tabular-nums; }
#catalogue thead th:nth-child(n+2):nth-child(-n+5) { text-align: right; padding-right: 18px; }
.fine { color: #777; font-size: 12px; margin-top: 36px; }

/* ---------- top bar ---------- */
#board { height: 100%; display: flex; flex-direction: column; }
#bar {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px; background: var(--panel);
  border-bottom: 1px solid #444; flex-wrap: wrap;
}
.group { display: flex; align-items: center; gap: 10px; }
#group-name { color: var(--muted); max-width: 22ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#group-id { font-family: ui-monospace, Consolas, monospace; letter-spacing: 2px; color: var(--accent); }
#members { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; align-items: center; min-width: 0; }
.chip {
  display: flex; align-items: center; gap: 7px; padding: 4px 10px 4px 8px; border-radius: 999px;
  background: #2b2b2b; border: 2px solid transparent; line-height: 1.15; text-align: left;
}
.chip .dot { width: 12px; height: 12px; border-radius: 3px; background: var(--c); flex: none; }
.chip small { display: block; color: var(--muted); font-size: 11px; }
.chip.me { border-color: var(--c); background: #111; }
.chip.add { border: 1px dashed #777; color: var(--muted); }
.who { color: var(--accent); font-size: 12px; }
.tools { display: flex; gap: 6px; flex-wrap: wrap; }
#shared.on { border-color: var(--accent); color: var(--accent); }
.tools b { margin-left: 2px; }
#totals b { color: var(--accent); }

/* ---------- tree ---------- */
#viewport {
  position: relative; flex: 1; overflow: hidden; touch-action: pan-y; cursor: grab;
  background: radial-gradient(ellipse at 50% 40%, #4a4a4a, #333 70%);
  user-select: none;
}
#viewport.free { touch-action: none; }
#viewport.dragging { cursor: grabbing; }
#canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#canvas svg { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#canvas svg path { fill: none; stroke: var(--line); stroke-width: calc(1px * var(--sw, 1)); opacity: .8; shape-rendering: crispEdges; }
#canvas svg path.lit { stroke-width: calc(2px * var(--sw, 1)); opacity: 1; }

.node {
  position: absolute; width: 40px; height: 40px; margin: -20px 0 0 -20px; padding: 0;
  background: var(--tile); border: 2px solid transparent; border-radius: 0; cursor: pointer;
}
.node:hover { background: #4a4a4a; z-index: 3; }
.node img { width: 100%; height: 100%; object-fit: contain; display: block; filter: grayscale(1) brightness(.6) contrast(.9); opacity: .75; pointer-events: none; }
.node .abbr { font-size: 9px; color: var(--muted); line-height: 1.05; display: block; padding: 2px; overflow: hidden; height: 100%; }
.node.claimed { background: var(--tile-lit); }
.node.claimed img { filter: none; opacity: 1; }
.node .tier {
  position: absolute; left: 1px; top: 0; font: 700 7px/1.2 "Arial Narrow", sans-serif; color: #ddd; letter-spacing: .5px;
  pointer-events: none;
}
.node.mine { border-color: var(--me); }
.node.shared-on { outline: 2px solid var(--accent); outline-offset: 2px; z-index: 2; }
#canvas.only-shared .node:not(.shared-on) { opacity: .35; }
.pips { position: absolute; left: -2px; right: -2px; bottom: -9px; display: flex; gap: 2px; justify-content: center; pointer-events: none; }
.pips i { width: 8px; height: 8px; border-radius: 2px; background: var(--c); border: 2px solid var(--c); box-shadow: 0 0 0 1px #111; }

.hub { position: absolute; width: 58px; transform: translate(-50%, -50%); pointer-events: none; text-align: center; }
.hub img { width: 100%; display: block; filter: drop-shadow(0 2px 5px rgba(0, 0, 0, .55)); }
.hub span {
  position: absolute; left: 50%; top: 100%; transform: translateX(-50%); margin-top: 2px; padding: 0 5px; background: #d9d9d9;
  font: italic 800 7px/11px "Arial Narrow", Arial, sans-serif; color: #222; text-transform: uppercase; white-space: nowrap;
}
.hub.lenny { width: 110px; }

#hint {
  position: absolute; left: 12px; bottom: 10px; color: var(--muted); font-size: 12px; pointer-events: none;
  background: rgba(20, 20, 20, .7); padding: 4px 8px; border-radius: 4px;
}

#tip {
  position: absolute; z-index: 10; width: 270px; background: #141414; border: 1px solid #666; border-radius: 6px;
  padding: 10px 12px; pointer-events: none; box-shadow: 0 6px 20px rgba(0, 0, 0, .6);
}
#tip h3 { margin: 0 0 2px; font-size: 15px; }
#tip .meta { color: var(--muted); font-size: 12px; }
#tip .rar { font-weight: 700; }
#tip p { margin: 6px 0 0; font-size: 12px; color: #cfcfcf; }
#tip dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 8px 0 0; font-size: 12px; }
#tip dt { color: var(--muted); }
#tip dd { margin: 0; }
#tip ul { list-style: none; margin: 8px 0 0; padding: 6px 0 0; border-top: 1px solid #333; font-size: 12px; }
#tip li { display: flex; align-items: center; gap: 6px; }
#tip li i { width: 9px; height: 9px; border-radius: 2px; border: 2px solid var(--c); background: var(--c); }
#tip .none { color: var(--muted); }

#toast {
  position: absolute; left: 50%; bottom: 44px; transform: translateX(-50%); background: #111; border: 1px solid #666;
  border-radius: 4px; padding: 6px 12px; font-size: 13px; pointer-events: none;
}
#toast.bad { border-color: var(--danger); color: var(--danger); }

dialog { background: var(--panel); color: var(--text); border: 1px solid #666; border-radius: 6px; padding: 18px; width: min(360px, 92vw); }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 14px; font-size: 16px; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.row.tight { margin-top: 0; }
#totals-dialog { width: min(820px, 94vw); }
#totals-table .dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; background: var(--c); margin-right: 7px; }
#totals-table tr.sum th { border-top: 1px solid #777; border-bottom: 0; color: var(--accent); }
.grow { flex: 1; }
#member-colors { display: flex; gap: 6px; flex-wrap: wrap; }
#member-colors button { width: 24px; height: 24px; padding: 0; background: var(--c); border: 2px solid #111; border-radius: 4px; }
#member-colors button.sel { outline: 2px solid #fff; }
#member-colors button:disabled { opacity: .2; cursor: not-allowed; }

@media (max-width: 700px) {
  #hint { display: none; }
  .logo { font-size: 32px; }
  .hero { padding-top: 4vh; }
  #explore { height: 56vh; min-height: 260px; }
  /* 16px inputs stop iOS from zooming the page when a field gets focus. */
  input { font-size: 16px; }
  label input { font-size: 16px; }
  #bar { gap: 6px; padding: 6px 8px; }
  .group { width: 100%; }
  #members, .tools { flex: none; width: 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  #members::-webkit-scrollbar, .tools::-webkit-scrollbar { display: none; }
  #members > *, .tools > * { flex: none; white-space: nowrap; }
  .chip small { font-size: 10px; }
  #tip { width: min(270px, calc(100% - 16px)); }
  #toast { bottom: 12px; max-width: calc(100% - 24px); text-align: center; }
  th, td { padding-right: 12px; }
}
/* Phone held sideways: one scrollable row, so the tree keeps the height. */
@media (max-height: 480px) {
  #hint { display: none; }
  #bar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 8px; padding: 5px 8px; }
  #bar::-webkit-scrollbar { display: none; }
  .group, #members, .tools { flex: none; width: auto; flex-wrap: nowrap; overflow: visible; }
  #members > *, .tools > *, .group > * { flex: none; white-space: nowrap; }
}
