:root {
  --bg-dark: rgba(18, 22, 28, 0.88);
  --panel: #252b34;
  --panel-border: #485466;
  --slot: #3b424d;
  --slot-border: #1e2229;
  --accent: #4caf50;
  --accent-hi: #66cc6a;
  --danger: #d94343;
  --gold: #ffd24d;
  --text: #f3f6fa;
  --muted: #a6b2c2;
}

* { box-sizing: border-box; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; background: #141920; color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  touch-action: none;
}

#game-container { position: fixed; inset: 0; z-index: 1; }
#game-container canvas { display: block; width: 100%; height: 100%; }
.hidden { display: none !important; }

/* SCREENS & MODALS */
.screen {
  position: fixed; inset: 0; z-index: 20;
  display: none; align-items: center; justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
.screen.active { display: flex; }
.screen.overlay { background: rgba(10, 14, 20, 0.72); backdrop-filter: blur(4px); }
.screen.danger-bg { background: rgba(95, 14, 14, 0.76); }

/* TITLE SCREEN */
.title-backdrop {
  position: fixed; inset: 0; z-index: -1;
  background: radial-gradient(circle at 50% 30%, rgba(44, 118, 179, 0.35), rgba(14, 20, 28, 0.82));
}
.title-panel {
  width: min(440px, 94vw); text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 18px;
}
.logo-wrap { position: relative; margin-bottom: 6px; }
.game-logo {
  margin: 0; font-size: clamp(2.2rem, 8vw, 3.8rem); font-weight: 900;
  letter-spacing: 0.08em; color: #f6f8fb;
  text-shadow: 0 4px 0 #3d4854, 0 7px 0 #1c2229, 0 12px 28px rgba(0,0,0,0.65);
}
.splash-text {
  position: absolute; right: -10px; bottom: -10px;
  color: var(--gold); font-weight: 800; font-size: clamp(0.8rem, 2.5vw, 1.05rem);
  text-shadow: 2px 2px 0 #3a2d00; transform: rotate(-10deg);
  animation: splashPulse 1.6s ease-in-out infinite;
}
@keyframes splashPulse { 0%,100%{transform:rotate(-10deg) scale(1)} 50%{transform:rotate(-10deg) scale(1.08)} }
.version-tag { margin: 4px 0 0; font-size: 0.78rem; color: rgba(255,255,255,0.72); }

.menu-stack { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.menu-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.mc-btn {
  appearance: none; border: 2px solid #161a20; border-radius: 6px;
  background: linear-gradient(180deg, #5e6a7a 0%, #46505e 100%);
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.22), inset 0 -3px 0 rgba(0,0,0,0.35);
  color: #fff; font-weight: 700; font-size: 0.96rem; padding: 11px 16px;
  cursor: pointer; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  transition: transform 0.08s, filter 0.12s; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.mc-btn:hover { filter: brightness(1.12); }
.mc-btn:active { transform: translateY(1px); filter: brightness(0.95); }
.mc-btn.primary { background: linear-gradient(180deg, #4eb857 0%, #348a3b 100%); }
.mc-btn.danger { background: linear-gradient(180deg, #d84b4b 0%, #9e2d2d 100%); }
.mc-btn.small { padding: 7px 11px; font-size: 0.82rem; }
.mc-btn.active { outline: 2px solid var(--gold); }

/* MODAL BOXES */
.modal-box {
  width: min(520px, 95vw); max-height: 90vh;
  background: var(--panel); border: 2px solid var(--panel-border); border-radius: 10px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.6); display: flex; flex-direction: column; overflow: hidden;
}
.modal-box.wide { width: min(680px, 95vw); }
.modal-box.compact { width: min(360px, 92vw); padding: 18px; }
.modal-box.inv-modal { width: min(740px, 96vw); }

.modal-header {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 16px; background: #1d222a; border-bottom: 1px solid #384252;
}
.modal-header h2 { margin: 0; font-size: 1.15rem; }
.modal-footer {
  display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap;
  padding: 12px 16px; background: #1d222a; border-top: 1px solid #384252;
}
.form-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }

.field, .range-field { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; }
.field input[type="text"], #creative-search {
  background: #161b22; border: 1px solid #455266; border-radius: 6px;
  color: #fff; padding: 10px 12px; font-size: 0.95rem; user-select: text; -webkit-user-select: text;
}
.field small { color: var(--muted); font-size: 0.8rem; }
.range-field input[type="range"] { width: 100%; accent-color: var(--accent); }

.segmented { display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); gap: 6px; }
.seg-btn {
  padding: 8px 10px; border-radius: 6px; border: 1px solid #445064;
  background: #1a2029; color: var(--muted); font-weight: 700; cursor: pointer;
}
.seg-btn.active { background: var(--accent); color: #fff; border-color: #7ee283; }

/* WORLDS LIST */
.worlds-list { padding: 12px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; min-height: 180px; }
.world-card {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 12px; background: #1c222b; border: 1px solid #384456; border-radius: 8px;
}
.world-info h3 { margin: 0 0 3px; font-size: 1rem; }
.world-meta { font-size: 0.78rem; color: var(--muted); }
.world-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.empty-worlds { text-align: center; color: var(--muted); padding: 36px 12px; }

.controls-grid {
  padding: 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px; overflow-y: auto; font-size: 0.85rem;
}
.controls-grid h4 { margin: 0 0 8px; color: var(--gold); }
.controls-grid ul { margin: 0; padding-left: 16px; display: flex; flex-direction: column; gap: 5px; }

/* IN-GAME HUD */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#underwater-tint {
  position: fixed; inset: 0; background: rgba(18, 78, 150, 0.42);
  opacity: 0; transition: opacity 0.2s; pointer-events: none;
}
#underwater-tint.active { opacity: 1; }
#damage-flash {
  position: fixed; inset: 0; background: rgba(220, 30, 30, 0.45);
  opacity: 0; transition: opacity 0.18s; pointer-events: none;
}
#damage-flash.active { opacity: 1; }

#crosshair {
  position: fixed; left: 50%; top: 50%; width: 20px; height: 20px;
  transform: translate(-50%, -50%); pointer-events: none;
}
#crosshair::before, #crosshair::after {
  content: ''; position: absolute; background: rgba(255,255,255,0.9);
  box-shadow: 0 0 2px rgba(0,0,0,0.9);
}
#crosshair::before { left: 9px; top: 2px; width: 2px; height: 16px; }
#crosshair::after { top: 9px; left: 2px; height: 2px; width: 16px; }

#hud-top-left {
  position: fixed; top: max(10px, env(safe-area-inset-top)); left: max(10px, env(safe-area-inset-left));
  display: flex; flex-direction: column; gap: 6px;
}
#hud-biome-pill {
  background: rgba(15, 20, 28, 0.62); border: 1px solid rgba(255,255,255,0.16);
  padding: 5px 10px; border-radius: 999px; font-size: 0.78rem; font-weight: 700;
  backdrop-filter: blur(4px); width: fit-content;
}
#debug-panel {
  background: rgba(12, 16, 22, 0.72); padding: 8px 10px; border-radius: 6px;
  font-family: monospace; font-size: 0.74rem; line-height: 1.45; white-space: pre;
}

#hud-top-right {
  position: fixed; top: max(10px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right));
  display: flex; gap: 6px; pointer-events: auto;
}
.hud-icon-btn {
  width: 38px; height: 38px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.22);
  background: rgba(18, 24, 32, 0.68); color: #fff; font-weight: 800; font-size: 0.88rem;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.hud-icon-btn:active { transform: scale(0.94); }

#toast-container {
  position: fixed; top: 56px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.toast {
  background: rgba(16, 22, 30, 0.85); border: 1px solid rgba(255,255,255,0.2);
  padding: 6px 14px; border-radius: 999px; font-size: 0.82rem; font-weight: 600;
  animation: toastIn 0.2s ease;
}
@keyframes toastIn { from{opacity:0;transform:translateY(-8px)} to{opacity:1;transform:none} }

/* BOTTOM HUD: BARS + HOTBAR */
#hud-bottom {
  position: fixed; left: 50%; bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 5px;
  pointer-events: none; z-index: 12;
}
#item-label {
  font-size: 0.82rem; font-weight: 700; text-shadow: 0 1px 3px #000;
  min-height: 1.1em; opacity: 0; transition: opacity 0.25s;
}
#item-label.show { opacity: 1; }
#survival-bars { width: 100%; display: flex; justify-content: space-between; gap: 14px; padding: 0 4px; }
.stat-bar { display: flex; gap: 2px; font-size: 0.86rem; filter: drop-shadow(0 1px 1px #000); }
.stat-bar.right { justify-content: flex-end; }
#bar-oxygen { display: flex; gap: 2px; font-size: 0.78rem; align-self: flex-end; padding-right: 4px; }

#hotbar-row { display: flex; align-items: center; gap: 5px; pointer-events: auto; }
#hotbar {
  display: grid; grid-template-columns: repeat(9, 1fr); gap: 3px;
  background: rgba(18, 22, 28, 0.78); padding: 4px; border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.18);
}
.slot, .inv-slot-btn {
  width: 44px; height: 44px; border-radius: 5px; position: relative;
  background: rgba(58, 66, 78, 0.82); border: 2px solid rgba(20, 24, 30, 0.85);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.slot.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--gold); background: rgba(95, 110, 128, 0.92); }
.slot.picked { border-color: #4ef57b; box-shadow: 0 0 0 2px #4ef57b; }
.slot img { width: 32px; height: 32px; image-rendering: pixelated; pointer-events: none; }
.slot-count {
  position: absolute; right: 2px; bottom: 1px; font-size: 0.72rem; font-weight: 800;
  text-shadow: 1px 1px 0 #000, -1px -1px 0 #000; pointer-events: none;
}
.slot-key {
  position: absolute; left: 3px; top: 1px; font-size: 0.58rem; color: rgba(255,255,255,0.6); pointer-events: none;
}
.inv-slot-btn { background: rgba(34, 42, 54, 0.85); border-color: rgba(255,255,255,0.22); font-size: 1.25rem; }

/* INVENTORY MODAL */
.inv-tabs { display: flex; gap: 5px; flex-wrap: wrap; }
.inv-body { padding: 12px 16px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.crafting-panel {
  display: grid; grid-template-columns: auto 1fr; gap: 14px; align-items: start;
  background: #1c222b; padding: 10px; border-radius: 8px; border: 1px solid #364152;
}
.craft-label, .inv-section-label { font-size: 0.78rem; font-weight: 700; color: var(--muted); }
.craft-row { display: flex; align-items: center; gap: 10px; margin: 6px 0; }
.craft-arrow { font-size: 1.3rem; color: var(--gold); }
.craft-hint { font-size: 0.72rem; color: var(--muted); }
.slot-grid { display: grid; gap: 4px; width: fit-content; }
.slot-grid.cols-2 { grid-template-columns: repeat(2, 44px); }
.slot-grid.cols-3 { grid-template-columns: repeat(3, 44px); }
.slot-grid.cols-9 { grid-template-columns: repeat(9, minmax(32px, 1fr)); width: 100%; }
.slot-grid.cols-9 .slot { width: 100%; height: auto; aspect-ratio: 1; }
.slot-grid.cols-9 .slot img { width: 72%; height: 72%; }
.creative-scroll { max-height: 44vh; overflow-y: auto; padding-right: 4px; }

.recipe-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; max-height: 112px; overflow-y: auto; }
.recipe-chip {
  display: flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 6px;
  background: #2a3340; border: 1px solid #48586e; color: #fff; font-size: 0.78rem; cursor: pointer;
}
.recipe-chip:hover { border-color: var(--accent-hi); background: #324052; }
.recipe-chip img { width: 22px; height: 22px; image-rendering: pixelated; }

.recipes-catalog { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; max-height: 55vh; overflow-y: auto; }
.recipe-card {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: #1c222b; border: 1px solid #364152; border-radius: 7px; padding: 8px;
}
.recipe-card-info { display: flex; align-items: center; gap: 8px; }
.recipe-card-info img { width: 28px; height: 28px; image-rendering: pixelated; }
.recipe-card-info small { display: block; color: var(--muted); font-size: 0.72rem; }

/* MOBILE TOUCH CONTROLS */
#touch-controls { position: fixed; inset: 0; pointer-events: none; z-index: 11; }
#joy-zone {
  position: fixed; left: max(14px, env(safe-area-inset-left)); bottom: max(92px, calc(env(safe-area-inset-bottom) + 84px));
  width: 126px; height: 126px; pointer-events: auto; display: flex; align-items: center; justify-content: center;
}
#joy-base {
  width: 116px; height: 116px; border-radius: 50%;
  background: rgba(20, 26, 34, 0.42); border: 2px solid rgba(255,255,255,0.28);
  position: relative; display: flex; align-items: center; justify-content: center;
}
#joy-knob {
  width: 48px; height: 48px; border-radius: 50%;
  background: rgba(255,255,255,0.68); box-shadow: 0 2px 10px rgba(0,0,0,0.4);
  transform: translate(0, 0);
}

#touch-actions {
  position: fixed; right: max(14px, env(safe-area-inset-right)); bottom: max(92px, calc(env(safe-area-inset-bottom) + 84px));
  display: flex; gap: 10px; pointer-events: auto;
}
.touch-col { display: flex; flex-direction: column; gap: 10px; }
.touch-btn {
  width: 56px; height: 56px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.34);
  background: rgba(22, 28, 36, 0.56); color: #fff; font-size: 1.25rem; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.touch-btn.primary { background: rgba(65, 165, 78, 0.65); }
.touch-btn.danger { background: rgba(205, 65, 65, 0.65); }
.touch-btn.accent { background: rgba(52, 130, 210, 0.65); }
.touch-btn.active { transform: scale(0.92); border-color: var(--gold); filter: brightness(1.2); }

/* LOADING */
.loading-box {
  background: var(--panel); border: 2px solid var(--panel-border); border-radius: 12px;
  padding: 24px 28px; text-align: center; width: min(340px, 90vw);
}
.loading-cube {
  width: 40px; height: 40px; margin: 0 auto 12px; border-radius: 6px;
  background: linear-gradient(135deg, #5cc458 0%, #5cc458 42%, #7d5333 42%, #7d5333 100%);
  animation: spinCube 1.2s ease-in-out infinite;
}
@keyframes spinCube { 0%{transform:rotate(0) scale(1)} 50%{transform:rotate(180deg) scale(1.12)} 100%{transform:rotate(360deg) scale(1)} }
.progress-track { height: 10px; background: #14181f; border-radius: 99px; overflow: hidden; margin: 10px 0; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); transition: width 0.15s; }
.death-title { color: #ff6b6b; text-align: center; margin: 0 0 8px; font-size: 1.8rem; }
#death-reason { text-align: center; color: var(--muted); margin: 0 0 16px; }

/* RESPONSIVE: MOBILE PORTRAIT & LANDSCAPE */
@media (max-width: 720px) {
  .slot, .inv-slot-btn { width: 34px; height: 34px; }
  .slot img { width: 25px; height: 25px; }
  .slot-key { display: none; }
  .crafting-panel { grid-template-columns: 1fr; }
  .stat-bar { font-size: 0.72rem; gap: 1px; }
}
@media (max-height: 460px) and (orientation: landscape) {
  .slot, .inv-slot-btn { width: 34px; height: 34px; }
  .slot img { width: 24px; height: 24px; }
  #joy-zone { bottom: max(16px, env(safe-area-inset-bottom)); width: 108px; height: 108px; }
  #joy-base { width: 100px; height: 100px; }
  #touch-actions { bottom: max(16px, env(safe-area-inset-bottom)); }
  .touch-btn { width: 48px; height: 48px; font-size: 1.05rem; }
  #hud-bottom { bottom: max(6px, env(safe-area-inset-bottom)); gap: 2px; }
}
@media (orientation: portrait) and (max-width: 540px) {
  #hotbar { gap: 2px; padding: 3px; }
  .slot, .inv-slot-btn { width: 30px; height: 30px; }
  .slot img { width: 22px; height: 22px; }
  #joy-zone { bottom: max(106px, calc(env(safe-area-inset-bottom) + 96px)); }
  #touch-actions { bottom: max(106px, calc(env(safe-area-inset-bottom) + 96px)); }
}
