/* Town & Castle reward builder (js/build.js) — v5 full-screen editor.
   Reuses the global .btn / .btn.primary / .tut-kicker / .row / .hidden from after-hours.css. */
#build-overlay {
  position: fixed !important; top: 0; right: 0; bottom: 0; left: 0; inset: 0; z-index: 95;
  display: flex; align-items: stretch; justify-content: stretch;
  background: #0b0d12; padding: 0;
}
#build-overlay:not(.hidden) { display: flex !important; pointer-events: auto !important; visibility: visible !important; opacity: 1 !important; }
#build-overlay.hidden { display: none !important; pointer-events: none !important; visibility: hidden !important; opacity: 0 !important; }

/* The card fills the screen: header, an optional grid (offers / styles / shop), the scene + palette, notes, buttons */
.build-card {
  background: #141822; border: 0; border-radius: 0;
  padding: 10px 14px 10px; width: 100vw; height: 100vh; max-height: none; box-sizing: border-box;
  display: flex; flex-direction: column; gap: 8px;
}
.build-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex: 0 0 auto; }
.build-head .tut-kicker { margin-bottom: 2px; }
.build-head h2 { margin: 0; font-size: 21px; color: #fff; line-height: 1.15; }
.build-badge {
  font: 700 11px/1 "Trebuchet MS", sans-serif; letter-spacing: .14em; color: #ffe08a;
  border: 1px solid rgba(255,224,138,.5); border-radius: 999px; padding: 6px 10px; white-space: nowrap;
}
.build-badge:empty { display: none; }
.build-sub { margin: 0; color: var(--dim); font-size: 13px; line-height: 1.35; flex: 0 0 auto; }

/* Theme choice: two big cards, each with a painted preview of its scene */
.build-themes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; flex: 1 1 auto; min-height: 0; overflow: auto; align-content: start; }
.build-theme, .build-opt {
  appearance: none; border: 2px solid #3a4150; background: #0f121a; color: var(--ink);
  border-radius: 14px; padding: 10px; min-height: 48px; cursor: pointer; font: inherit; text-align: left;
  display: flex; flex-direction: column; gap: 6px; touch-action: manipulation;
}
.build-theme canvas { width: 100%; height: auto; max-height: 40vh; object-fit: cover; display: block; border-radius: 8px; }
.build-theme .name, .build-opt .name { font-size: 17px; font-weight: 800; color: #fff; }
.build-theme .meta, .build-opt .meta { color: var(--dim); font-size: 12px; line-height: 1.3; }
.build-theme.selected, .build-opt.selected { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(245,200,66,.25); background: #1b1f2c; }

/* Option cards (reward offers, styles, shop) — the shop scrolls */
.build-options { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: max-content; gap: 10px; flex: 0 1 auto; min-height: 0; overflow: auto; }
.build-opt { min-height: 120px; }
.build-opt .pic {
  height: 88px; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at 50% 80%, rgba(245,200,66,.14), transparent 65%), #10141c;
  border-radius: 8px; overflow: hidden;
}
.build-opt .pic img, .build-opt .pic canvas { max-width: 100%; max-height: 100%; display: block; }
.build-opt .pic .ph {
  font-size: 12px; font-weight: 700; color: var(--gold); border: 1px dashed rgba(245,200,66,.5);
  border-radius: 6px; padding: 6px 10px; text-align: center;
}

/* Main area: the scene fills whatever is left; the palette sits on the right */
.build-main { display: flex; gap: 10px; flex: 1 1 auto; min-height: 160px; }
.build-card.step-shop .build-main { flex: 0 0 150px; min-height: 150px; }      /* the shop grid gets the room; the scene is a preview */
.build-card.step-pick .build-main, .build-card.step-style .build-main { flex: 1 1 220px; }
.build-scene { position: relative; flex: 1 1 auto; min-width: 0; min-height: 0; border-radius: 10px; overflow: hidden; border: 1px solid rgba(255,255,255,.08); background: #0f121a; }
.build-scene canvas { display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; }
.build-tools { position: absolute; top: 8px; left: 8px; display: flex; gap: 6px; z-index: 2; }
.build-tool {
  appearance: none; width: 40px; height: 40px; border-radius: 10px; border: 1px solid rgba(255,255,255,.25);
  background: rgba(8,10,14,.78); color: #fff; font: 700 20px/1 "Trebuchet MS", sans-serif; cursor: pointer; touch-action: manipulation;
}
.build-tool:hover, .build-tool:focus-visible { border-color: var(--gold); color: var(--gold); outline: none; }
.build-pbar {
  position: absolute; left: 8px; bottom: 8px; z-index: 2; display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  background: rgba(8,10,14,.85); border: 1px solid rgba(255,255,255,.2); border-radius: 12px; padding: 6px 8px; max-width: calc(100% - 16px);
}
.build-pbar .name { font-weight: 800; color: #fff; font-size: 14px; margin-right: 4px; }
.build-pbar .btn.small { min-height: 34px; padding: 4px 12px; font-size: 13px; }
.build-pbar .btn.danger { border-color: #c0504d; color: #ff9d9a; }
.build-chip {
  appearance: none; width: 30px; height: 30px; border-radius: 50%; border: 2px solid rgba(255,255,255,.35); background: #0f121a;
  padding: 3px; cursor: pointer; touch-action: manipulation;
}
.build-chip .sw { display: block; width: 100%; height: 100%; border-radius: 50%; }
.build-chip.selected { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(245,200,66,.3); }

/* Palette: tabs on top, a scrolling grid of unlocked pieces (locked ones greyed with their price) */
.build-side { width: 260px; flex: 0 0 260px; display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.build-ptabs { display: flex; flex-wrap: wrap; gap: 4px; flex: 0 0 auto; }
.build-ptabs .build-tab { padding: 5px 9px; font-size: 12px; }
.build-plist { flex: 1 1 auto; min-height: 0; overflow: auto; display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: max-content; gap: 6px; align-content: start; padding-right: 2px; }
.build-pitem {
  appearance: none; border: 2px solid #3a4150; background: #0f121a; color: var(--ink); border-radius: 10px; padding: 6px;
  display: flex; flex-direction: column; gap: 3px; cursor: pointer; font: inherit; text-align: left; touch-action: manipulation;
}
.build-pitem .pic { height: 64px; display: flex; align-items: center; justify-content: center; background: #10141c; border-radius: 6px; overflow: hidden; }
.build-pitem .pic img, .build-pitem .pic canvas { max-width: 100%; max-height: 100%; display: block; }
.build-pitem .name { font-size: 12px; font-weight: 800; color: #fff; line-height: 1.2; }
.build-pitem .tag { font: 700 10px/1.2 "Trebuchet MS", sans-serif; letter-spacing: .04em; color: var(--gold); }
.build-pitem:hover, .build-pitem:focus-visible { border-color: var(--gold); outline: none; }
.build-pitem.locked { opacity: .55; }
.build-pitem.locked .tag { color: var(--dim); }
/* v5.6: castle perks */
.build-pitem .perkmark { font: 700 10px/1.2 "Trebuchet MS", sans-serif; color: #9aefc0; }
.build-pitem.has-perk { border-color: #2f5a48; }
.build-pbar .perk { font-size: 12px; color: #9aefc0; max-width: 420px; line-height: 1.25; }
.build-perks { margin: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; font-size: 12px; line-height: 1.3; color: var(--dim); flex: 0 0 auto; }
.build-perks .lab { font-weight: 800; color: #9aefc0; }
.build-perks .chip { padding: 2px 8px; border-radius: 999px; border: 1px solid #2f5a48; background: #0f1d18; color: #c8f5dc; font-weight: 700; cursor: help; }
.build-perks .more { color: var(--dim); }
.build-pempty { grid-column: 1 / -1; color: var(--dim); font-size: 13px; margin: 6px; }

.build-note { margin: 0; font-size: 14px; font-weight: 700; color: var(--gold); min-height: 1.3em; text-align: center; flex: 0 0 auto; }

/* Gallery: build code (copy) and load code rows */
.build-code {
  display: grid; grid-template-columns: auto 1fr auto auto 1fr auto; gap: 6px 8px; align-items: center;
  font-size: 12px; color: var(--dim); flex: 0 0 auto;
}
.build-code .lab { font-weight: 700; color: var(--ink); white-space: nowrap; }
.build-code input {
  width: 100%; min-height: 36px; border-radius: 8px; border: 1px solid #3a4150; background: #0f121a;
  color: var(--ink); padding: 4px 10px; font: 12px ui-monospace, Consolas, "Courier New", monospace; box-sizing: border-box;
  user-select: text; -webkit-user-select: text;
}
.build-code input:focus { outline: none; border-color: var(--gold); }
.build-code .btn { min-height: 36px; padding: 4px 12px; white-space: nowrap; }
.build-msg { grid-column: 1 / -1; margin: 0; min-height: 1.2em; font-size: 12px; color: var(--dim); }
.build-msg.ok { color: #9aefc0; }
.build-msg.bad { color: #ff8b8b; }
.build-msg:empty { display: none; }

.build-row { margin: 0; flex: 0 0 auto; }
#build-overlay .btn:disabled { opacity: .45; cursor: default; }
#build-overlay .btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* Style choice cards */
.build-styles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); grid-auto-rows: max-content; gap: 10px; flex: 0 1 auto; min-height: 0; overflow: auto; }
.build-styles .build-opt .pic { height: 72px; }
.build-opt .match {
  font: 700 11px/1.2 "Trebuchet MS", sans-serif; letter-spacing: .04em; color: #ffe08a;
  border: 1px solid rgba(255,224,138,.5); border-radius: 999px; padding: 4px 8px; align-self: flex-start;
}

/* Coin shop: tabs, a scrolling grid with category headings, price tags */
.build-tabs { display: flex; gap: 8px; flex-wrap: wrap; flex: 0 0 auto; }
.build-tab {
  appearance: none; border: 1px solid #3a4150; background: #0f121a; color: var(--dim); border-radius: 999px;
  padding: 8px 14px; font: 700 13px "Trebuchet MS", sans-serif; cursor: pointer; touch-action: manipulation;
}
.build-tab.selected { border-color: var(--gold); color: var(--gold); background: #1b1f2c; }
.build-shop { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); flex: 1 1 auto; align-content: start; }
.build-shop .build-opt { position: relative; }
.build-shop .build-opt .pic { height: 70px; }
.build-shop-h { grid-column: 1 / -1; margin: 6px 0 0; font: 700 12px/1 "Trebuchet MS", sans-serif; letter-spacing: .12em; text-transform: uppercase; color: var(--gold); }
.build-opt .price {
  font: 700 12px/1 "Trebuchet MS", sans-serif; color: #10141c; background: var(--gold); border-radius: 999px; padding: 5px 9px; align-self: flex-start;
}
.build-opt .price.short { background: #3a4150; color: var(--dim); }
.build-opt .price.own { background: #2f6f4a; color: #d8ffe6; }

@media (max-width: 720px) {
  .build-card { padding: 8px 10px; }
  .build-themes, .build-options { grid-template-columns: 1fr; }
  .build-main { flex-direction: column; }
  .build-side { width: auto; flex: 0 0 38%; }
  .build-plist { grid-template-columns: repeat(3, 1fr); }
  .build-code { grid-template-columns: 1fr auto; }
  .build-code .lab { grid-column: 1 / -1; }
  .build-styles { grid-template-columns: 1fr 1fr; }
}
@media (max-height: 640px) {
  .build-head h2 { font-size: 17px; }
  .build-sub { font-size: 12px; }
  .build-opt .pic { height: 60px; }
}
/* v5.17.2: the 500-pixel Canvas frame: small cards several to a row, a one-line note, a smaller preview */
@media (max-height: 620px) {
  .build-card { padding: 6px 10px; gap: 6px; }
  .build-sub { display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
  .build-themes { grid-template-columns: 1fr 1fr; }
  .build-theme canvas { max-height: 26vh; }
  .build-options, .build-shop { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; }
  .build-opt { min-height: 0; padding: 6px; gap: 3px; }
  .build-opt .pic, .build-shop .build-opt .pic, .build-styles .build-opt .pic { height: 44px; }
  .build-opt .name, .build-theme .name { font-size: 14px; }
  .build-opt .meta { display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
  .build-styles { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 6px; }
  .build-card.step-shop .build-main { flex: 0 0 104px; min-height: 104px; }
  .build-main { min-height: 120px; }
  .build-tab { padding: 5px 12px; }
  #build-overlay .btn { min-height: 36px; padding: 6px 12px; }
}
/* v5.7.6: Fenrir's monuments in the palette */
.build-pitem.trophy .tag { color: #ffd84a; font-weight: 700; }
.build-pitem.trophy.locked { outline: 1px dashed #ffd84a; }
