:root {
  --accent: #d97757;
  --accent-dark: #b85d3f;
  --panel: rgba(20, 18, 16, 0.82);
  --text: #f4efe8;
  --muted: #b9b0a5;
  --slot: rgba(0, 0, 0, 0.45);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #87b9e8; color: var(--text); font-family: var(--font); }
canvas#game { display: block; width: 100vw; height: 100vh; image-rendering: pixelated; }
[hidden] { display: none !important; }

.panel {
  background: var(--panel);
  border: 2px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

#menu, #paused, #inventory, #death {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(0, 0, 0, 0.25); z-index: 10;
}
#menu { background: radial-gradient(circle at 50% 30%, rgba(0,0,0,0.05), rgba(0,0,0,0.55)); }

.menu-panel { width: min(440px, 100%); text-align: center; }
.logo { font-size: 48px; margin: 0; letter-spacing: 1px; text-shadow: 3px 3px 0 #000; font-weight: 900; }
.logo span { color: var(--accent); }
.tagline { color: var(--muted); margin: 6px 0 20px; }

form { display: flex; flex-direction: column; gap: 10px; text-align: left; }
label { font-size: 14px; color: var(--muted); }
input {
  font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 6px;
  border: 2px solid rgba(255, 255, 255, 0.2); background: rgba(0, 0, 0, 0.4); color: var(--text);
  outline: none;
}
input:focus { border-color: var(--accent); }
button {
  font: inherit; font-size: 18px; font-weight: 700; padding: 12px; border: 0; border-radius: 6px;
  background: var(--accent); color: #fff; cursor: pointer; box-shadow: 0 4px 0 var(--accent-dark);
}
button:hover { filter: brightness(1.08); }
button:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--accent-dark); }
button:disabled { opacity: 0.6; cursor: wait; }
#status { min-height: 1.4em; color: #ffd27a; margin: 12px 0 0; }

.controls { margin-top: 16px; text-align: left; color: var(--muted); font-size: 14px; }
.controls summary { cursor: pointer; }
.controls ul { padding-left: 18px; line-height: 1.8; }
kbd {
  background: rgba(255, 255, 255, 0.12); border-radius: 4px; padding: 1px 6px;
  font-family: inherit; font-size: 12px; color: var(--text);
}

#paused .panel { text-align: center; min-width: 260px; }
#paused h2 { margin-top: 0; }

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; }
#crosshair { position: absolute; left: 50%; top: 50%; width: 22px; height: 22px; transform: translate(-50%, -50%); mix-blend-mode: difference; }
#crosshair::before, #crosshair::after { content: ""; position: absolute; background: #fff; }
#crosshair::before { left: 10px; top: 0; width: 2px; height: 22px; }
#crosshair::after { left: 0; top: 10px; width: 22px; height: 2px; }

#debug {
  position: absolute; left: 8px; top: 8px; font: 13px/1.5 Consolas, monospace;
  background: rgba(0, 0, 0, 0.45); padding: 6px 10px; border-radius: 4px; white-space: pre;
}
#online {
  position: absolute; right: 8px; top: 8px; font-size: 13px; background: rgba(0, 0, 0, 0.4);
  padding: 4px 10px; border-radius: 4px;
}

#hotbar {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 4px; padding: 4px; background: rgba(0, 0, 0, 0.35); border-radius: 6px;
}
.slot {
  width: 52px; height: 52px; background: var(--slot); border: 3px solid rgba(255, 255, 255, 0.15);
  border-radius: 4px; display: flex; align-items: center; justify-content: center; position: relative;
}
.slot.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
.slot canvas { width: 38px; height: 38px; image-rendering: pixelated; }
.slot .num { position: absolute; left: 3px; top: 1px; font-size: 10px; color: rgba(255,255,255,0.6); }
#block-name {
  position: absolute; bottom: 80px; left: 50%; transform: translateX(-50%);
  font-weight: 700; text-shadow: 2px 2px 0 #000; transition: opacity 0.4s; opacity: 0;
}

#chat { position: absolute; left: 8px; bottom: 104px; width: min(480px, calc(100vw - 16px)); }
#chat-log { display: flex; flex-direction: column; gap: 2px; max-height: 40vh; overflow: hidden; justify-content: flex-end; }
.chat-line {
  background: rgba(0, 0, 0, 0.45); padding: 3px 8px; border-radius: 3px; font-size: 14px;
  text-shadow: 1px 1px 0 #000; transition: opacity 1s; word-wrap: break-word;
}
.chat-line.system { color: #ffd27a; }
.chat-line.faded { opacity: 0; }
#chat.open .chat-line.faded { opacity: 1; }
#chat-input { pointer-events: auto; width: 100%; margin-top: 4px; }

#player-list {
  position: absolute; top: 40px; left: 50%; transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.6); padding: 10px 16px; border-radius: 6px; min-width: 220px;
}
#player-list h3 { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
#player-list div.p { padding: 2px 0; }

.muted { color: var(--muted); font-size: 12px; }
button.ghost { background: transparent; box-shadow: none; padding: 4px 10px; font-size: 18px; }

/* status */
#stats {
  position: absolute; bottom: 76px; left: 50%; transform: translateX(-50%);
  width: 492px; font-size: 18px; line-height: 1; text-shadow: 1px 1px 0 #000;
}
.stats-row { display: flex; justify-content: space-between; }
#armor-bar { margin-bottom: 3px; }
#armor-bar span { display: inline-block; width: 19px; text-align: center; }
.shield { color: #cfe3ea; } .shield.half { color: #8aa3ab; } .shield.empty { color: #3b3b3b; }
.inv-top { display: flex; gap: 8px; align-items: flex-start; }
#inv-armor { display: grid; grid-template-columns: 44px; gap: 4px; }
#inv-armor .slot { width: 44px; height: 44px; cursor: pointer; }
#inv-armor .slot canvas { width: 32px; height: 32px; }
.armor-slot:empty::after, .armor-slot:not(:has(canvas))::after {
  content: attr(data-hint); color: rgba(255, 255, 255, 0.25); font-weight: 700;
}
#chest-section h3 { margin: 6px 0; }
#chest-grid { display: grid; grid-template-columns: repeat(9, 44px); gap: 4px; margin-bottom: 8px; }
#chest-grid .slot { width: 44px; height: 44px; cursor: pointer; background: rgba(120, 80, 30, 0.45); }
#chest-grid .slot canvas { width: 32px; height: 32px; }
#hearts span, #hunger span { display: inline-block; width: 19px; text-align: center; }
.heart { color: #e23b3b; } .heart.half { color: #e88a8a; } .heart.empty { color: #3b3b3b; }
.food { color: #d6923f; } .food.half { color: #a87a4a; } .food.empty { color: #3b3b3b; }
#stats.shake { animation: shake 0.3s; }
@keyframes shake { 25% { transform: translate(calc(-50% - 3px), 0); } 75% { transform: translate(calc(-50% + 3px), 0); } }

#break-bar {
  position: absolute; left: 50%; top: calc(50% + 22px); transform: translateX(-50%);
  width: 60px; height: 5px; background: rgba(0, 0, 0, 0.5); border-radius: 3px; display: none;
}
#break-fill { height: 100%; width: 0; background: #fff; border-radius: 3px; }

#hurt-flash {
  position: fixed; inset: 0; pointer-events: none; opacity: 0; transition: opacity 0.4s;
  background: radial-gradient(circle, transparent 40%, rgba(200, 0, 0, 0.55));
}
#hurt-flash.on { opacity: 1; transition: none; }

#mode-badge {
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
  background: var(--accent); padding: 3px 10px; border-radius: 4px; font-size: 12px; font-weight: 700;
}

#hotbar .slot { pointer-events: auto; }
.slot .count {
  position: absolute; right: 3px; bottom: 1px; font-size: 13px; font-weight: 700; text-shadow: 1px 1px 0 #000;
}
.slot .dur { position: absolute; left: 5px; right: 5px; bottom: 3px; height: 3px; background: #222; }
.slot .dur i { display: block; height: 100%; background: #5fd35f; }

#death { background: rgba(120, 0, 0, 0.45); z-index: 12; }
#death .panel { text-align: center; min-width: 280px; }
#death h2 { margin-top: 0; font-size: 32px; }
#death-text { color: var(--muted); }

/* inventário */
.inv-panel { width: min(900px, 100%); max-height: calc(100vh - 32px); overflow: auto; }
.inv-head { display: flex; justify-content: space-between; align-items: center; }
.inv-head h2 { margin: 0; }
.inv-body { display: flex; gap: 24px; flex-wrap: wrap; }
.inv-body section { flex: 1 1 280px; min-width: 0; }
.inv-body section:first-child { flex: 0 0 auto; }
#inventory .hint { color: var(--muted); margin: 8px 0 12px; font-size: 13px; }
#inv-grid, #inv-hotbar { display: grid; grid-template-columns: repeat(9, 44px); gap: 4px; }
#inv-grid .slot, #inv-hotbar .slot { width: 44px; height: 44px; cursor: pointer; }
#inv-grid .slot canvas, #inv-hotbar .slot canvas { width: 32px; height: 32px; }
.slot.picked { border-color: var(--accent); background: rgba(217, 119, 87, 0.35); }
.inv-row-label { color: var(--muted); font-size: 12px; margin: 10px 0 4px; }
#trash {
  margin-top: 10px; width: 44px; height: 44px; border-radius: 4px; background: rgba(120, 30, 30, 0.6);
  display: flex; align-items: center; justify-content: center; cursor: pointer; font-size: 20px;
}
.craft-section h3 { margin: 0 0 4px; }
#station-info { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
#recipes { display: flex; flex-direction: column; gap: 4px; max-height: 420px; overflow-y: auto; padding-right: 4px; }
.recipe {
  display: flex; align-items: center; gap: 8px; padding: 4px 8px; border-radius: 6px; cursor: pointer;
  background: rgba(255, 255, 255, 0.05); border: 1px solid transparent; font-size: 13px;
}
.recipe:hover { border-color: var(--accent); }
.recipe.no { opacity: 0.4; cursor: default; }
.recipe.no:hover { border-color: transparent; }
.recipe canvas { width: 28px; height: 28px; image-rendering: pixelated; flex: none; }
.recipe .rname { font-weight: 700; }
.recipe .rin { color: var(--muted); font-size: 12px; }
.recipe .rst { margin-left: auto; font-size: 11px; color: #ffd27a; white-space: nowrap; }

/* toque */
body.touch #chat { top: 8px; bottom: auto; width: min(46vw, 420px); }
body.touch .chat-line { font-size: 12px; padding: 2px 6px; }
body.touch #chat-input { pointer-events: auto; }
#touch-ui { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#joy-zone { position: absolute; left: 0; bottom: 0; width: 45%; height: 60%; pointer-events: auto; }
#look-zone { position: absolute; right: 0; top: 0; width: 55%; height: 100%; pointer-events: auto; }
#joy-base {
  position: absolute; left: 40px; bottom: 60px; width: 100px; height: 100px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.12); border: 2px solid rgba(255, 255, 255, 0.3); opacity: 0.6;
}
#joy-base.active { opacity: 1; }
#joy-knob {
  position: absolute; left: 30px; top: 30px; width: 40px; height: 40px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
}
.tbtn {
  position: absolute; width: 64px; height: 64px; border-radius: 50%; pointer-events: auto;
  background: rgba(0, 0, 0, 0.35); border: 2px solid rgba(255, 255, 255, 0.35);
  display: flex; align-items: center; justify-content: center; font-size: 26px; user-select: none;
}
.tbtn.down { background: rgba(217, 119, 87, 0.6); }
.tbtn.small { width: 46px; height: 46px; font-size: 20px; }
#t-jump { right: 24px; bottom: 150px; }
#t-break { right: 100px; bottom: 200px; }
#t-place { right: 24px; bottom: 230px; }
#t-inv { right: 12px; top: 50px; }
#t-chat { right: 66px; top: 50px; }
#t-fly { right: 120px; top: 50px; }

@media (max-width: 700px) {
  .slot { width: 36px; height: 36px; }
  .slot canvas { width: 26px; height: 26px; }
  .logo { font-size: 36px; }
  #stats { width: 340px; font-size: 14px; bottom: 58px; }
  #hearts span, #hunger span { width: 14px; }
  #armor-bar span { width: 14px; }
  .inv-panel { padding: 12px; }
  .inv-top { flex-direction: column; }
  #inv-grid, #inv-hotbar, #chest-grid { grid-template-columns: repeat(9, 32px); gap: 3px; }
  #inv-armor { grid-template-columns: repeat(4, 32px); }
  #inv-grid .slot, #inv-hotbar .slot, #chest-grid .slot, #inv-armor .slot { width: 32px; height: 32px; }
  #inv-grid .slot canvas, #inv-hotbar .slot canvas, #chest-grid .slot canvas, #inv-armor .slot canvas { width: 24px; height: 24px; }
  #chat { bottom: 110px; }
}

/* celular em pé: botões acima da barra, joystick mais alto */
@media (orientation: portrait) {
  body.touch #joy-zone { height: 50%; bottom: 90px; }
  body.touch #joy-base { bottom: 40px; }
  body.touch #t-jump { right: 16px; bottom: 130px; }
  body.touch #t-break { right: 92px; bottom: 175px; }
  body.touch #t-place { right: 16px; bottom: 210px; }
  body.touch #t-inv { right: 12px; top: 56px; }
  body.touch #t-chat { right: 64px; top: 56px; }
  body.touch #t-fly { right: 116px; top: 56px; }
  body.touch #stats { width: min(340px, calc(100vw - 16px)); }
  body.touch #hotbar { bottom: 8px; }
}
