/* ---------- chat room ---------- */
#chatRoom {
  position: fixed; left: calc(8px + env(safe-area-inset-left)); top: calc(8px + env(safe-area-inset-top));
  width: min(420px, calc(100% - 210px)); display: flex; flex-direction: column; pointer-events: none; z-index: 4;
}
#chatHead, #chatForm { display: none; }
#chatLog {
  max-height: 34vh; overflow-y: auto; font-size: 14px; display: flex; flex-direction: column; gap: 2px;
  scrollbar-width: thin;
}
#chatLog .line {
  background: rgba(0,0,0,.4); padding: 2px 8px; border-radius: 5px; align-self: flex-start;
  overflow-wrap: anywhere; transition: opacity 1s;
}
#chatLog .line.old { opacity: 0; }
#chatLog .time { color: #8fa3ba; font-size: 12px; }
#chatLog .system { color: #ffe9a8; font-style: italic; }
#chatLog .who { color: #9fd0ff; font-weight: 600; }
#chatLog .who.me { color: #b6f5a3; }
#chatLog .who.gm { color: #ffd84a; }

/* Open: a real chat room with the whole history */
body.chatting #chatRoom {
  pointer-events: auto; background: rgba(12,20,32,.88); border-radius: 12px; padding: 8px;
  width: min(460px, calc(100% - 16px)); box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
body.chatting #chatHead { display: flex; justify-content: space-between; align-items: center; padding: 0 2px 6px; }
body.chatting #chatLog { height: min(46vh, 420px); max-height: none; padding-right: 4px; }
body.chatting #chatLog .line { background: none; padding: 1px 2px; }
body.chatting #chatLog .line.old { opacity: 1; }
body.chatting #chatForm { display: flex; gap: 6px; margin-top: 8px; }
#chatHead button { background: none; border: 0; color: #cfe0f2; font-size: 18px; padding: 2px 6px; }
#chatInput {
  flex: 1; min-width: 0; padding: 10px 12px; border-radius: 8px; border: 1px solid #567;
  background: rgba(10,20,30,.85); color: #fff; font-size: 16px; -webkit-user-select: text; user-select: text;
}
#chatForm button { padding: 0 14px; border: 0; border-radius: 8px; background: #4caf50; color: #fff; font-weight: 600; }

#btnChat {
  position: fixed; left: calc(12px + env(safe-area-inset-left)); bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 5;
  width: 46px; height: 46px; border-radius: 50%; border: 2px solid rgba(255,255,255,.35);
  background: rgba(0,0,0,.4); color: #fff; font-size: 20px; padding: 0;
}
#chatBadge {
  position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: #e5484d; color: #fff; font-size: 12px; font-weight: 700; line-height: 20px;
}

/* ---------- hotbar ---------- */
#hotbarWrap {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(10px + env(safe-area-inset-bottom));
  display: flex; gap: 6px; align-items: center; max-width: calc(100% - 130px);
}
#btnBag {
  flex: 0 0 auto; width: 52px; height: 52px; border-radius: 10px; border: 2px solid rgba(255,255,255,.3);
  background: rgba(0,0,0,.45); font-size: 24px; padding: 0;
}
#hotbar {
  display: flex; gap: 4px; padding: 5px; background: rgba(0,0,0,.4); border-radius: 10px; min-width: 0;
  overflow-x: auto; scrollbar-width: none;
}
#hotbar::-webkit-scrollbar { display: none; }
#hotbar .empty { font-size: 13px; color: #e8eef5; padding: 8px 10px; white-space: nowrap; }
.slot, .item {
  position: relative; flex: 0 0 auto; width: 46px; height: 46px; border-radius: 6px; padding: 0;
  border: 2px solid rgba(255,255,255,.15); background-size: cover; image-rendering: pixelated;
  box-shadow: inset 0 -6px 0 rgba(0,0,0,.18);
}
.slot.see-through { background-color: rgba(255,255,255,.08); }
.slot.active { border-color: #fff; box-shadow: 0 0 0 2px #000, inset 0 -6px 0 rgba(0,0,0,.18); }
.slot[data-key]::after {
  content: attr(data-key); position: absolute; left: 3px; top: 1px; font-size: 11px; color: #fff;
  text-shadow: 0 0 3px #000;
}
.slot .count, .item .count {
  position: absolute; right: 2px; bottom: 0; font-size: 12px; font-weight: 700; color: #fff;
  text-shadow: 0 0 3px #000, 0 0 2px #000;
}
#blockName {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(76px + env(safe-area-inset-bottom));
  font-weight: 600; text-shadow: 0 1px 3px #000; opacity: 0; pointer-events: none;
}
#blockName.fade { animation: fade 1.6s ease-out; }
@keyframes fade { 0%, 60% { opacity: 1; } 100% { opacity: 0; } }

#pickups {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(100px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column-reverse; align-items: center; gap: 2px; pointer-events: none;
}
.pickup { font-weight: 700; color: #b6f5a3; text-shadow: 0 1px 3px #000; animation: rise 1.3s ease-out forwards; }
@keyframes rise { 0% { opacity: 0; transform: translateY(8px); } 15% { opacity: 1; } 100% { opacity: 0; transform: translateY(-24px); } }

/* ---------- bag, crafting and settings panels ---------- */
.card.wide { width: min(560px, 100%); text-align: left; max-height: calc(100vh - 32px); overflow-y: auto; }
.panelHead { display: flex; justify-content: space-between; align-items: center; }
.panelHead h2 { margin: 0; }
.card h3 { margin: 18px 0 8px; }
.card button.x { width: auto; margin: 0; padding: 4px 10px; background: none; font-size: 20px; color: #cfe0f2; }
.card .muted { color: #9fb4cc; font-size: 14px; }
#bagGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; margin-top: 12px; }
.card button.bagItem {
  margin: 0; padding: 8px 4px; background: rgba(255,255,255,.06); border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; font-weight: normal;
}
.card button.bagItem:hover { background: rgba(255,255,255,.14); }
.bagItem .label { font-size: 12px; color: #cfe0f2; text-align: center; }
.item { display: inline-block; width: 36px; height: 36px; vertical-align: middle; }
.bagItem .item { width: 44px; height: 44px; }
.item.short { outline: 2px solid #e5484d; }
#craftList { display: flex; flex-direction: column; gap: 6px; }
.recipe { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; background: rgba(255,255,255,.05); }
.recipe.missing { opacity: .6; }
.recipe .ins { display: flex; align-items: center; gap: 4px; }
.recipe .plus, .recipe .arrow { color: #9fb4cc; }
.recipe .rname { flex: 1; font-size: 14px; }
.card button.craftBtn { width: auto; margin: 0; padding: 8px 14px; background: #4caf50; font-size: 14px; }
fieldset { border: 1px solid #34465c; border-radius: 10px; margin: 14px 0 0; padding: 8px 12px 12px; text-align: left; }
legend { color: #aac0db; font-size: 13px; padding: 0 4px; }
.card label.choice { display: flex; align-items: center; gap: 10px; margin: 8px 0 0; font-size: 15px; color: #e8eef5; cursor: pointer; }
.card label.choice input { width: auto; margin: 0; accent-color: #4caf50; transform: scale(1.2); }
.gm-cap-only { display: none; }
body.gmcap #accountMenu .gm-cap-only { display: block; }

/* ---------- touch controls ---------- */
#touchControls { display: none; }
body.touch #touchControls { display: block; }
body.touch #crosshair { display: none; }

#joystick {
  position: fixed; left: calc(24px + env(safe-area-inset-left)); bottom: calc(28px + env(safe-area-inset-bottom));
  width: 130px; height: 130px; border-radius: 50%;
  background: rgba(255,255,255,.14); border: 2px solid rgba(255,255,255,.35);
}
#knob {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px;
  border-radius: 50%; background: rgba(255,255,255,.55); pointer-events: none;
}
.tbtn {
  position: fixed; width: 58px; height: 58px; border-radius: 50%; border: 2px solid rgba(255,255,255,.4);
  background: rgba(0,0,0,.3); color: #fff; font-size: 22px; padding: 0;
}
.tbtn.pressed { background: rgba(255,255,255,.35); }
.tbtn.big { width: 76px; height: 76px; font-size: 28px; right: calc(24px + env(safe-area-inset-right)); bottom: calc(40px + env(safe-area-inset-bottom)); }
.tbtn.wide { width: auto; border-radius: 29px; padding: 0 16px; font-size: 16px; font-weight: 600; height: 48px;
  right: calc(110px + env(safe-area-inset-right)); bottom: calc(52px + env(safe-area-inset-bottom)); background: rgba(76,175,80,.55); }
.tbtn.wide.dig { background: rgba(214,120,40,.65); }
#btnFly { right: calc(24px + env(safe-area-inset-right)); bottom: calc(130px + env(safe-area-inset-bottom)); width: 50px; height: 50px; }
#btnDown { right: calc(110px + env(safe-area-inset-right)); bottom: calc(112px + env(safe-area-inset-bottom)); width: 50px; height: 50px; }
.gm-only, .fly-only { display: none; }
body.gm .gm-only { display: inline; }
body.touch.gm .tbtn.gm-only { display: block; }
body.touch.flying .tbtn.fly-only { display: block; }

/* On phones the hotbar moves to the top so the thumbs keep the bottom corners */
body.touch #hotbarWrap { bottom: auto; top: calc(108px + env(safe-area-inset-top)); max-width: calc(100% - 16px); }
body.touch #btnBag { width: 44px; height: 44px; font-size: 20px; }
body.touch .slot { width: 40px; height: 40px; }
body.touch .slot[data-key]::after { display: none; }
body.touch #blockName { bottom: auto; top: calc(162px + env(safe-area-inset-top)); }
body.touch #pickups { bottom: auto; top: calc(186px + env(safe-area-inset-top)); flex-direction: column; }
body.touch #chatRoom { width: calc(100% - 200px); }
body.touch #chatLog { max-height: 90px; }
body.touch #btnChat { left: auto; bottom: auto; right: calc(8px + env(safe-area-inset-right)); top: calc(50px + env(safe-area-inset-top)); }
body.touch.chatting #chatRoom { width: calc(100% - 16px); z-index: 7; }
body.touch.chatting #chatLog { height: 34vh; }
body.touch #topRight { top: auto; bottom: calc(170px + env(safe-area-inset-bottom)); left: 8px; right: auto; flex-direction: column; align-items: flex-start; gap: 4px; }

/* ---------- ad (draggable, collapsible) ---------- */
:root:not([data-game-ads-enabled]) .game-ad { display: none; }
.game-ad {
  position: fixed; left: calc(12px + env(safe-area-inset-left)); bottom: calc(72px + env(safe-area-inset-bottom)); z-index: 6;
  width: 302px; color: #1d2733; background: rgba(250,248,237,.95); border-radius: 12px; overflow: hidden;
  box-shadow: 0 4px 18px rgba(0,0,0,.25);
}
.game-ad summary {
  display: flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 12px; cursor: grab;
  font-size: 14px; list-style: none; touch-action: none;
}
.game-ad summary::-webkit-details-marker { display: none; }
.game-ad.dragging summary { cursor: grabbing; }
.game-ad .ad-collapse, .game-ad .ad-expand { margin-left: auto; color: #4b6075; font-size: 13px; }
.game-ad:not([open]) .ad-collapse, .game-ad[open] .ad-expand { display: none; }
.game-ad:not([open]) { width: 150px; }
.ad-grip { font-size: 20px; color: #6b7c8c; }
.game-ad summary:focus-visible { outline: 3px solid #4caf50; outline-offset: -3px; }
body.touch .game-ad { left: calc(8px + env(safe-area-inset-left)); bottom: auto; top: calc(220px + env(safe-area-inset-top)); }
body.touch .game-ad:not([open]) { width: 76px; }
body.touch .game-ad:not([open]) summary { padding: 0 8px; }
body.touch .game-ad:not([open]) .ad-expand { display: none; }

.card label.choice.slider { justify-content: space-between; }
.card label.choice.slider input { transform: none; width: 50%; }
#btnView { right: calc(110px + env(safe-area-inset-right)); bottom: calc(176px + env(safe-area-inset-bottom)); width: 50px; height: 50px; font-size: 20px; }
body.touch.gm #btnView { bottom: calc(176px + env(safe-area-inset-bottom)); }
