:root {
  --bg: #2a221c;
  --panel: rgba(30, 24, 20, 0.88);
  --line: rgba(255, 255, 255, 0.14);
  --text: #fbf6ee;
  --muted: #c9bba8;
  --accent: #f2cd37;
  --red: #c91a09;
  --xmas: #1f7a46;
  --hween: #e8730c;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body { -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 13px; }

#scene { position: fixed; inset: 0; }
#scene canvas { display: block; }

/* ── Screens ─────────────────────────────────────────────────────────────── */

.screen {
  position: fixed; inset: 0; z-index: 10; overflow-y: auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.screen.side { align-items: flex-start; padding-left: max(24px, 4vw); }
.screen.top { justify-content: flex-start; gap: 12px; background: rgba(20, 15, 12, 0.6); backdrop-filter: blur(4px); }
#setinfo, #pause, #done { background: rgba(20, 15, 12, 0.55); backdrop-filter: blur(3px); }

.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 18px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35); backdrop-filter: blur(6px);
}
.menu-panel { width: min(420px, 100%); display: flex; flex-direction: column; gap: 10px; text-align: center; }
.menu-panel.wide { width: min(560px, 100%); }
.menu-panel > button {
  padding: 11px 10px; border-radius: 10px; font-weight: 600;
  background: rgba(255, 255, 255, 0.07); border: 1px solid var(--line);
}
.menu-panel > button:hover { background: rgba(255, 255, 255, 0.14); }
button.primary { background: linear-gradient(180deg, #ffde5c, #e9b10f) !important; border: 0 !important; color: #2a1800 !important; font-weight: 800; }
button.big { font-size: 20px; padding: 14px; letter-spacing: 0.06em; }
button.theme { display: flex; justify-content: space-between; align-items: center; color: #fff; border: 0 !important; }
button.theme small { font-size: 13px; opacity: 0.85; }
button.xmas { background: linear-gradient(180deg, #2c9a5c, #17603a) !important; }
button.hween { background: linear-gradient(180deg, #f08a2a, #b4520a) !important; }
h2 { margin: 0; font-size: 26px; letter-spacing: 0.04em; }

/* The logo: each letter a little brick with a stud row on top. */
.logo { margin: 0 0 4px; font-size: clamp(34px, 8vw, 52px); font-weight: 900; letter-spacing: 0.02em; }
.logo .b {
  display: inline-block; min-width: 1.05em; padding: 0.1em 0.12em 0.04em; margin: 0 0.02em; border-radius: 0.12em; color: #fff;
  position: relative; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.35); box-shadow: inset 0 -0.08em 0 rgba(0, 0, 0, 0.25), inset 0 0.06em 0 rgba(255, 255, 255, 0.35);
}
.logo .b::before {
  content: ""; position: absolute; left: 22%; right: 22%; top: -0.16em; height: 0.16em; border-radius: 0.06em 0.06em 0 0;
  background: inherit; box-shadow: inset 0 0.04em 0 rgba(255, 255, 255, 0.35);
}
.logo .r { background: #c91a09; }
.logo .y { background: #f2cd37; color: #3a2a00; }
.logo .g { background: #4b9f4a; }
.logo .u { background: #0055bf; }
.logo .go { color: var(--accent); font-style: italic; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.5); }
.tag { margin: 0; color: var(--muted); }
.settings { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.chip { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.07); font-size: 14px; }
.chip.active { background: var(--accent); color: #2a1800; border-color: var(--accent); font-weight: 700; }
.help { text-align: left; color: var(--muted); font-size: 14px; }
.help summary { cursor: pointer; text-align: center; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 16px; margin-top: 10px; }
.help b { color: var(--text); }
.help p { margin: 8px 0 0; }
.touch-help { display: none; }
body.touch .touch-help { display: block; }
body.touch .help-grid { display: none; }
.stat-row { display: flex; justify-content: space-around; gap: 10px; flex-wrap: wrap; color: var(--muted); }
.stat-row b { color: var(--text); }

/* ── Set browser ─────────────────────────────────────────────────────────── */

.browse-top { width: min(1100px, 100%); display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 12px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06); font-weight: 600; }
.tab.active { background: var(--accent); color: #2a1800; border-color: var(--accent); }
.filters { display: flex; gap: 8px; flex: 1; min-width: 240px; align-items: center; flex-wrap: wrap; }
.filters input, .filters select {
  padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: rgba(0, 0, 0, 0.35); min-width: 0;
}
.filters input { flex: 1; min-width: 160px; }
.filters select option { background: #2a221c; }

.grid { width: min(1100px, 100%); display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.card {
  padding: 0; border: 0; border-radius: 12px; overflow: hidden; text-align: left; background: #f4efe6; color: #2a1d10;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.35); transition: transform 0.12s;
}
.card:hover { transform: translateY(-3px); }
.card.christmas { border-top: 6px solid var(--xmas); }
.card.halloween { border-top: 6px solid var(--hween); }
.card-art { position: relative; aspect-ratio: 4 / 3; background: #333; }
.card-art img, .box-art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-text { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; }
.card-text small { color: #7a6650; }
.box-num { position: absolute; left: 8px; top: 8px; background: rgba(0, 0, 0, 0.65); color: #fff; padding: 2px 8px; border-radius: 6px; font-weight: 700; font-size: 13px; }
.tick { position: absolute; right: 8px; bottom: 8px; background: #2c9a5c; color: #fff; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 6px; }
.tick.part { background: #b4520a; }
.star { position: absolute; right: 8px; top: 6px; color: var(--accent); font-size: 20px; text-shadow: 0 1px 3px #000; }
.more { padding: 16px 0 24px; font-size: 14px; }

/* ── The box ─────────────────────────────────────────────────────────────── */

.box-art { position: relative; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 3; background: #333; }
.box-age { position: absolute; right: 8px; top: 8px; background: #fff; color: #222; padding: 2px 8px; border-radius: 6px; font-weight: 800; }
.done-art { width: 100%; border-radius: 10px; }

/* ── Building HUD ────────────────────────────────────────────────────────── */

#set-hud, #free-hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
#set-hud > *, #free-hud > * { pointer-events: auto; }
.hud-head {
  position: absolute; left: 12px; top: max(12px, env(safe-area-inset-top)); padding: 10px 14px; width: min(320px, calc(100% - 24px));
  display: flex; flex-direction: column; gap: 4px;
}
.hud-head small { display: block; color: var(--muted); }
.step-label { font-size: 18px; }
.step-label b { color: var(--accent); font-size: 24px; }
.bar { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.12); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }

/* The parts list for the step, like the call-out box in a booklet. */
.callout {
  position: absolute; right: 12px; top: max(12px, env(safe-area-inset-top)); padding: 10px; width: min(260px, 46vw);
  max-height: calc(100% - 140px); overflow-y: auto; background: rgba(244, 239, 230, 0.95); color: #2a1d10;
}
.need {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 4px; margin: 0 0 4px; text-align: left; color: inherit;
  background: transparent; border: 2px solid transparent; border-bottom-color: rgba(0, 0, 0, 0.08); border-radius: 8px;
}
/* The part in your hand. */
.need.active { border-color: var(--accent); background: rgba(242, 205, 55, 0.22); }
.need img { width: 64px; height: 48px; object-fit: cover; border-radius: 6px; }
.need b { font-size: 18px; }
.need span { display: flex; flex-direction: column; font-size: 14px; }
.need small { color: #7a6650; }
.finished { margin: 0; font-weight: 700; }

.build-bar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: max(12px, env(safe-area-inset-bottom));
  display: flex; gap: 8px; padding: 8px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
}
.tb {
  min-width: 64px; padding: 8px 10px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08);
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 20px; line-height: 1;
}
.tb small { font-size: 11px; color: var(--muted); }
.tb.primary small { color: #2a1800; }
.tb.active { background: var(--red); border-color: var(--red); }

/* Free build: a drawer of parts and colours along the left. */
.drawer {
  position: absolute; left: 12px; top: max(12px, env(safe-area-inset-top)); bottom: 96px; width: min(300px, 70vw);
  display: flex; flex-direction: column; gap: 10px; padding: 10px; transition: transform 0.2s;
}
.drawer.closed { transform: translateX(calc(-100% - 20px)); }
.part-list { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; align-content: start; }
.part {
  padding: 2px; border-radius: 8px; border: 2px solid transparent; background: #f4efe6; color: #2a1d10;
  display: flex; flex-direction: column; align-items: center; font-size: 11px; position: relative;
}
.part img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 6px; }
.part i { position: absolute; left: 4px; top: 3px; font-style: normal; font-size: 9px; color: #7a6650; }
.part.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(242, 205, 55, 0.4); }
.color-list { display: grid; grid-template-columns: repeat(8, 1fr); gap: 5px; }
.swatch {
  aspect-ratio: 1; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.25); background: var(--c);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25), inset 0 2px 0 rgba(255, 255, 255, 0.35);
}
.swatch.trans { opacity: 0.6; }
.swatch.metal { background: linear-gradient(135deg, #fff 0%, var(--c) 40%, var(--c) 70%, #555 100%); }
.swatch.active { border-color: #fff; transform: scale(1.12); }
.hint {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(max(12px, env(safe-area-inset-bottom)) + 76px);
  background: rgba(0, 0, 0, 0.55); padding: 4px 12px; border-radius: 999px; font-size: 13px; white-space: nowrap; max-width: 94vw; overflow: hidden; text-overflow: ellipsis;
}

/* ── Phone ───────────────────────────────────────────────────────────────── */

#touch-zoom { position: fixed; right: 12px; bottom: calc(max(12px, env(safe-area-inset-bottom)) + 84px); z-index: 6; display: flex; flex-direction: column; gap: 8px; }
#touch-zoom .tb { width: 52px; height: 52px; min-width: 0; font-size: 26px; justify-content: center; background: var(--panel); }
body.touch .tb { min-width: 58px; min-height: 58px; font-size: 22px; }
body.touch .build-bar { gap: 6px; padding: 6px; max-width: calc(100% - 16px); }

@media (max-width: 720px), (max-aspect-ratio: 1/1) {
  .screen.side { align-items: center; justify-content: flex-end; padding-left: 16px; }
  .callout { top: auto; bottom: calc(max(12px, env(safe-area-inset-bottom)) + 86px); right: 8px; width: min(200px, 52vw); max-height: 34vh; }
  .need img { width: 44px; height: 33px; }
  .hud-head { left: 8px; top: max(8px, env(safe-area-inset-top)); }
  .drawer { left: 8px; right: 8px; width: auto; top: auto; bottom: calc(max(12px, env(safe-area-inset-bottom)) + 84px); height: 46vh; }
  .drawer.closed { transform: translateY(calc(100% + 120px)); }
  .part-list { grid-template-columns: repeat(5, 1fr); }
  .color-list { grid-template-columns: repeat(10, 1fr); }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .tb small { font-size: 10px; }
  .tb { min-width: 50px; padding: 6px; }
  body.touch .tb { min-width: 50px; min-height: 54px; }
  .hint { bottom: calc(max(12px, env(safe-area-inset-bottom)) + 78px); font-size: 12px; }
  /* Zoom buttons go up the right edge, clear of the drawer and parts list. */
  #touch-zoom { bottom: auto; top: calc(max(8px, env(safe-area-inset-top)) + 110px); right: 8px; }
  #touch-zoom .tb { width: 46px; height: 46px; min-height: 0; }
}
/* The drawer says what you're holding; the hint would only sit under it. */
.drawer:not(.closed) ~ .hint { display: none; }

/* ── Toast and loading ───────────────────────────────────────────────────── */

#toast {
  position: fixed; left: 50%; top: 18%; transform: translate(-50%, -10px); z-index: 30; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  background: rgba(0, 0, 0, 0.75); padding: 8px 16px; border-radius: 999px; font-weight: 600;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#loading { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; background: var(--bg); font-size: 20px; color: var(--muted); }
