:root {
  --bg: #2b2140;
  --panel: #3b2f57;
  --panel2: #4a3b6b;
  --text: #fff8ee;
  --muted: #cbbfe0;
  --accent: #ffcf4a;
  --good: #2ecc71;
  --bad: #e74c3c;
  --slot: #5b4a80;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

body { display: flex; flex-direction: column; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

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

#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 3px solid #1d1530;
  flex-wrap: wrap;
}
.brand { font-weight: 800; font-size: 17px; }
.stat { font-weight: 700; font-size: 17px; background: var(--panel2); padding: 4px 10px; border-radius: 10px; }
.stat.money { color: var(--accent); font-size: 20px; }
.goal { flex: 1; min-width: 180px; font-size: 14px; }
.goal-track { height: 8px; background: #1d1530; border-radius: 4px; margin-top: 4px; overflow: hidden; }
#goal-bar { height: 100%; width: 0; background: linear-gradient(90deg, #ffcf4a, #ff7b54); transition: width .3s; }
.icon-btn {
  background: var(--panel2);
  border: 0;
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 18px;
}
.icon-btn:hover { background: #5d4b86; }

/* ---------- stage ---------- */

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#view {
  position: relative;
  width: 960px;
  height: 600px;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 6px 30px rgba(0,0,0,.5);
  cursor: crosshair;
  touch-action: none;
}
#view canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#overlay { pointer-events: none; }
body[data-mode="workshop"] #view { cursor: pointer; }

#toasts {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  pointer-events: none;
  z-index: 30;
  width: min(640px, 92%);
}
.toast {
  background: rgba(30, 22, 48, .92);
  border: 2px solid #6d5a99;
  padding: 8px 14px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  text-align: center;
  transition: opacity .5s, transform .5s;
  animation: pop .25s ease-out;
}
.toast.good { border-color: var(--good); }
.toast.bad { border-color: var(--bad); }
.toast.fade { opacity: 0; transform: translateY(-8px); }
@keyframes pop { from { transform: scale(.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- modal ---------- */

#modal {
  position: absolute;
  inset: 0;
  background: rgba(15, 10, 25, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  padding: 16px;
}
#modal[hidden], #craft[hidden] { display: none; }
#modal .card {
  background: var(--panel);
  border: 3px solid #6d5a99;
  border-radius: 18px;
  padding: 22px 26px;
  max-width: 460px;
  width: 100%;
  max-height: 100%;
  overflow: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
  animation: pop .25s ease-out;
}
#modal .card.big { max-width: 640px; }
#modal h2 { margin: 0 0 10px; font-size: 28px; }
#modal .body { font-size: 16px; line-height: 1.45; }
#modal .body p { margin: 8px 0; }
#modal .body ul { padding-left: 20px; margin: 8px 0; }
#modal .body li { margin: 6px 0; }
#modal .small { color: var(--muted); font-size: 14px; }
#modal .confetti { font-size: 34px; text-align: center; letter-spacing: 6px; }
#modal .buttons { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
#modal .buttons button {
  padding: 10px 18px;
  border-radius: 12px;
  border: 0;
  background: var(--panel2);
  font-weight: 700;
  font-size: 16px;
}
#modal .buttons button.primary { background: var(--accent); color: #3b2610; }
#modal .buttons button:hover { filter: brightness(1.1); }

/* ---------- crafting ---------- */

#craft {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 10, 25, .35);
  z-index: 20;
  padding: 12px;
}
.craft-card {
  background: #c6a47a;
  color: #3b2616;
  border: 4px solid #6b4226;
  border-radius: 16px;
  padding: 14px 18px;
  width: min(860px, 100%);
  max-height: 100%;
  overflow: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
.craft-head { display: flex; justify-content: space-between; align-items: center; }
.craft-head h2 { margin: 0; font-size: 22px; }
.craft-head .icon-btn { background: #8b5a33; color: #fff; }
.craft-body { display: flex; gap: 18px; margin-top: 8px; flex-wrap: wrap; }
.table-side { flex: 1 1 320px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.book-side { flex: 1 1 300px; min-width: 260px; }
.book-side h3 { margin: 4px 0 8px; }
.tip { margin: 0; font-size: 14px; }
.craft-row { display: flex; align-items: center; gap: 14px; }
#grid { display: grid; grid-template-columns: repeat(3, 60px); gap: 6px; background: #8b5a33; padding: 8px; border-radius: 10px; }
.cell, #result, .slot {
  background: var(--slot);
  border: 3px solid #2e2345;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.cell { width: 60px; height: 60px; background: #a3774b; border-color: #5c3519; cursor: pointer; }
.cell:hover, .cell.target { background: #b98a5a; }
.cell.target { border-color: #ffe066; }
.arrow { font-size: 34px; }
#result { width: 80px; height: 80px; background: #a3774b; border-color: #5c3519; }
#result.ready { background: #f9e79f; border-color: #d4ac0d; cursor: pointer; animation: glow 1s infinite alternate; }
@keyframes glow { from { box-shadow: 0 0 4px #ffe066; } to { box-shadow: 0 0 18px #ffe066; } }
#result-name { font-weight: 700; min-height: 20px; text-align: center; }
#clear-grid { background: #8b5a33; color: #fff; border: 0; border-radius: 8px; padding: 6px 12px; }

#book { display: flex; flex-direction: column; gap: 6px; max-height: 330px; overflow: auto; padding-right: 4px; }
.recipe {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #e0c9a6;
  border: 2px solid #8b5a33;
  border-radius: 10px;
  padding: 6px 8px;
  text-align: left;
  color: #3b2616;
  opacity: .65;
}
.recipe.ok { opacity: 1; background: #f3e3c3; border-color: #2e8b57; }
.recipe:hover { filter: brightness(1.06); }
.rinfo { flex: 1; }
.rname { font-weight: 800; }
.rprice { font-size: 13px; }
.mini { display: grid; gap: 2px; }
.mc { width: 18px; height: 18px; background: rgba(0,0,0,.12); border-radius: 3px; display: flex; align-items: center; justify-content: center; }
.hint { font-size: 13px; padding: 4px 2px; }

/* ---------- bottom bar ---------- */

#bottombar {
  background: var(--panel);
  border-top: 3px solid #1d1530;
  padding: 8px 12px;
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}
#hotbar { display: flex; gap: 4px; }
.slot { width: 50px; height: 50px; cursor: grab; touch-action: none; }
.slot.selected { border-color: var(--accent); box-shadow: 0 0 10px var(--accent); }
.count {
  position: absolute;
  right: 3px;
  bottom: 0;
  font-weight: 800;
  font-size: 14px;
  text-shadow: 0 0 3px #000, 0 0 3px #000;
  color: #fff;
}
.key { position: absolute; left: 3px; top: 0; font-size: 10px; color: var(--muted); }
.drag-ghost { position: fixed; pointer-events: none; transform: translate(-50%, -50%); z-index: 100; }

#actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; align-items: center; }
#actions button {
  background: var(--panel2);
  border: 2px solid #6d5a99;
  border-radius: 10px;
  padding: 8px 10px;
  font-weight: 700;
  font-size: 14px;
  position: relative;
  overflow: hidden;
}
#actions button small { color: var(--muted); font-weight: 600; }
#actions button:hover:not(:disabled) { background: #5d4b86; }
#actions button:disabled { opacity: .45; cursor: not-allowed; }
#actions button.big:not(:disabled) { background: #1e8449; border-color: var(--good); }
#actions button.active { background: #8e6c1f; border-color: var(--accent); }
#actions button.cant { opacity: .6; }
#actions button.glow:not(:disabled) { background: #a93226; border-color: #ff7b54; animation: glow 0.8s infinite alternate; }
#actions button.cd::after {
  content: '';
  position: absolute;
  left: 0; bottom: 0;
  height: 100%;
  width: calc(var(--cd, 0) * 100%);
  background: rgba(0,0,0,.35);
  pointer-events: none;
}
.cdtext { margin-left: 4px; color: var(--accent); }
#actions .hint { color: var(--muted); font-size: 14px; }

.boss-only, .workshop-only { display: none !important; }
body[data-mode="boss"] .boss-only { display: inline-block !important; }
body[data-mode="workshop"] .workshop-only { display: inline-block !important; }
body[data-mode="boss"] .shop-only { display: none !important; }
body[data-mode="boss"] #hotbar { display: none; }
body[data-mode="title"] #actions { visibility: hidden; }
body.paused #actions, body.paused #hotbar { pointer-events: none; opacity: .5; }

@media (max-width: 760px) {
  .slot { width: 38px; height: 38px; }
  .slot img { width: 26px; height: 26px; }
  #actions button { padding: 6px 8px; font-size: 12px; }
  #actions button small { display: none; }
  .brand { font-size: 14px; }
}
