:root {
  --bg: #1a1428;
  --panel: rgba(28, 22, 44, 0.94);
  --ink: #f3eefc;
  --muted: #b9add6;
  --accent: #a77bff;
  --accent2: #ffcf5a;
  --btn: rgba(20, 16, 32, 0.45);
  --btn-border: rgba(255, 255, 255, 0.35);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  touch-action: none; overscroll-behavior: none;
}

#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#crosshair {
  position: fixed; left: 50%; top: 50%; width: 18px; height: 18px; transform: translate(-50%, -50%); pointer-events: none;
  background:
    linear-gradient(#fff, #fff) center / 2px 100% no-repeat,
    linear-gradient(#fff, #fff) center / 100% 2px no-repeat;
  mix-blend-mode: difference; display: none;
}
body.mouse #crosshair { display: block; }

#joy {
  position: fixed; width: 130px; height: 130px; margin: -65px 0 0 -65px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.35); background: rgba(0, 0, 0, 0.18); display: none; pointer-events: none;
}
#joyknob {
  position: absolute; left: 50%; top: 50%; width: 58px; height: 58px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.55); transform: translate(-50%, -50%);
}

#hud { position: fixed; inset: 0; pointer-events: none; }
#hud button { pointer-events: auto; }

button {
  font: inherit; color: var(--ink); border: none; cursor: pointer; touch-action: manipulation;
}
.round {
  width: 64px; height: 64px; border-radius: 50%; background: var(--btn); border: 2px solid var(--btn-border);
  display: grid; place-items: center; padding: 0; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.round.big { width: 86px; height: 86px; }
.round.small { width: 48px; height: 48px; }
.round svg { width: 55%; height: 55%; fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.round.on { background: rgba(167, 123, 255, 0.6); border-color: #fff; }
.round:active { transform: scale(0.94); }
#btn-inv svg rect { fill: rgba(255,255,255,0.85); stroke: none; }
#btn-fly svg path { fill: rgba(255,255,255,0.9); stroke: none; }

#btn-menu { position: absolute; top: calc(12px + var(--safe-t)); left: calc(12px + var(--safe-l)); }
#fps { position: absolute; top: calc(20px + var(--safe-t)); left: calc(72px + var(--safe-l)); font-size: 13px; color: #fff; text-shadow: 0 1px 2px #000; }

#right-buttons {
  position: absolute; right: calc(18px + var(--safe-r)); bottom: calc(24px + var(--safe-b));
  display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 14px; align-items: end; justify-items: center;
}
#btn-fly { grid-column: 2; grid-row: 1; }
#btn-down { grid-column: 1; grid-row: 2; visibility: hidden; }
#btn-jump { grid-column: 2; grid-row: 2; }
body.flying #btn-down { visibility: visible; }
body.mouse #right-buttons { display: none; }

#hotbar-wrap {
  position: absolute; left: 50%; bottom: calc(12px + var(--safe-b)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
}
#hotbar { display: flex; gap: 4px; padding: 4px; background: rgba(0, 0, 0, 0.38); border-radius: 12px; pointer-events: auto; }
.slot {
  width: 50px; height: 50px; border-radius: 8px; border: 2px solid transparent; padding: 4px; background: rgba(255,255,255,0.08);
  display: grid; place-items: center;
}
.slot img { width: 100%; height: 100%; image-rendering: pixelated; border-radius: 3px; pointer-events: none; }
.slot.sel { border-color: var(--accent2); background: rgba(255, 207, 90, 0.18); }

/* Smala skärmar (iPhone stående eller liggande) */
@media (max-width: 760px), (max-height: 430px) {
  .slot { width: 40px; height: 40px; padding: 3px; }
  #hotbar-wrap { left: calc(50% - 30px); }
  .round { width: 56px; height: 56px; }
  .round.big { width: 72px; height: 72px; }
}
@media (max-width: 560px) {
  .slot { width: 32px; height: 32px; padding: 2px; }
  #hotbar { gap: 2px; padding: 3px; }
  #hotbar-wrap { left: 50%; bottom: calc(110px + var(--safe-b)); }
}

/* Overlays */
.overlay {
  position: fixed; inset: 0; display: grid; place-items: center; background: rgba(10, 6, 20, 0.55);
  padding: 16px; z-index: 10; pointer-events: auto;
}
.overlay.hidden { display: none; }
.panel {
  background: var(--panel); border: 1px solid rgba(255,255,255,0.12); border-radius: 18px; padding: 22px;
  width: min(560px, 100%); max-height: 100%; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  touch-action: pan-y;
}
.panel h2 { margin: 0 0 10px; font-size: 22px; }
.hint { color: var(--muted); font-size: 14px; margin: 6px 0 12px; }

.start { text-align: center; background: linear-gradient(180deg, rgba(40, 26, 70, 0.96), rgba(18, 12, 30, 0.96)); }
.start h1 {
  font-size: clamp(40px, 9vw, 68px); margin: 4px 0 2px; letter-spacing: 1px; color: #fff;
  text-shadow: 0 0 18px rgba(167, 123, 255, 0.9), 0 3px 0 #3b2560;
}
.start .sub { color: var(--muted); margin: 0 0 18px; }
.help { display: grid; gap: 6px; margin: 0 auto 18px; font-size: 16px; color: var(--muted); }
.help b { color: var(--ink); }
body.mouse .touch-only, body:not(.mouse) .mouse-only { display: none; }

.wide {
  display: block; width: 100%; padding: 14px; border-radius: 12px; background: var(--accent); font-weight: 700; font-size: 18px; margin-top: 12px;
}
.wide.big { font-size: 24px; padding: 18px; }
.wide:disabled { opacity: 0.5; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(255,255,255,0.08); font-size: 16px; }
.row input[type=range] { flex: 1; accent-color: var(--accent); }
.row input[type=checkbox] { width: 24px; height: 24px; accent-color: var(--accent); }
.row.buttons { justify-content: flex-start; flex-wrap: wrap; border: none; }
.row.buttons button { padding: 10px 14px; border-radius: 10px; background: rgba(255,255,255,0.12); }
.row.buttons button.danger { background: #8c2f3c; }
.seg { display: flex; border-radius: 10px; overflow: hidden; background: rgba(255,255,255,0.1); }
.seg button { padding: 8px 12px; background: transparent; font-size: 15px; }
.seg button.on { background: var(--accent); }

.wide.alt { background: #2f9e7a; }
.hidden { display: none !important; }
.link { background: none; color: var(--muted); margin-top: 10px; font-size: 15px; text-decoration: underline; }
#name-input {
  width: 100%; font: inherit; font-size: 22px; padding: 14px; border-radius: 12px; border: 2px solid rgba(255,255,255,0.2);
  background: rgba(0,0,0,0.3); color: var(--ink); text-align: center; -webkit-user-select: text; user-select: text;
}
#toast {
  position: fixed; top: calc(16px + var(--safe-t)); left: 50%; transform: translateX(-50%); padding: 10px 18px; border-radius: 12px;
  background: rgba(20, 14, 34, 0.85); color: #fff; font-size: 16px; opacity: 0; transition: opacity 0.3s; pointer-events: none; z-index: 20;
}
#toast.show { opacity: 1; }
#online {
  position: fixed; top: calc(22px + var(--safe-t)); right: calc(16px + var(--safe-r)); font-size: 13px; color: #fff; text-shadow: 0 1px 2px #000;
  pointer-events: none;
}

#inv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.inv-item {
  display: grid; justify-items: center; gap: 4px; padding: 6px 2px; border-radius: 10px; background: rgba(255,255,255,0.07); font-size: 11px; color: var(--muted);
}
.inv-item img { width: 44px; height: 44px; image-rendering: pixelated; border-radius: 4px; }
.inv-item:active { background: rgba(167, 123, 255, 0.4); }

/* ---------------- Start: användare, kod, världar ---------------- */
#lobby { margin-top: 6px; }
.users { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 12px; margin-top: 10px; }
.user { display: grid; gap: 6px; justify-items: center; padding: 16px 8px; border-radius: 16px; background: rgba(255,255,255,0.08); font-size: 18px; font-weight: 600; }
.user:active { background: rgba(167,123,255,0.4); }
.av { font-size: 44px; line-height: 1; }
.who { font-size: 22px; font-weight: 700; display: flex; align-items: center; justify-content: center; gap: 10px; }
.who .av { font-size: 34px; }
.who.small { font-size: 18px; }
.who .link { margin: 0 0 0 8px; }
.dots { display: flex; gap: 14px; justify-content: center; margin: 12px 0 16px; }
.dots i { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--muted); }
.dots i.on { background: var(--accent2); border-color: var(--accent2); }
.dots.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
.pad { display: grid; grid-template-columns: repeat(3, 76px); gap: 10px; justify-content: center; }
.pad button { height: 64px; border-radius: 14px; background: rgba(255,255,255,0.1); font-size: 26px; font-weight: 600; }
.pad button:active { background: var(--accent); }
.tabs { display: flex; gap: 6px; margin: 14px 0 4px; background: rgba(255,255,255,0.07); border-radius: 12px; padding: 4px; }
.tabs button { flex: 1; padding: 10px; border-radius: 9px; background: transparent; font-size: 16px; font-weight: 600; }
.tabs button.on { background: var(--accent); }
.worlds { display: grid; gap: 8px; max-height: 38vh; overflow: auto; text-align: left; }
.world { display: flex; gap: 8px; }
.wbtn { flex: 1; display: grid; gap: 2px; padding: 12px 14px; border-radius: 12px; background: rgba(255,255,255,0.08); text-align: left; }
.wbtn:active { background: rgba(167,123,255,0.4); }
.wn { font-size: 18px; font-weight: 700; }
.wi { font-size: 13px; color: var(--muted); }
.wi b { color: #7be0a8; }
.del { padding: 0 14px; border-radius: 12px; background: rgba(140, 47, 60, 0.6); font-size: 14px; }
.newworld { display: flex; gap: 8px; margin-top: 12px; }
.newworld input { flex: 1; font: inherit; font-size: 17px; padding: 12px; border-radius: 12px; border: 2px solid rgba(255,255,255,0.2); background: rgba(0,0,0,0.3); color: var(--ink); -webkit-user-select: text; user-select: text; }
.newworld button { padding: 0 18px; border-radius: 12px; font-weight: 700; font-size: 17px; background: #2f9e7a; }
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 16px 0 8px; }
.mode { display: grid; gap: 6px; padding: 20px 12px; border-radius: 16px; background: var(--accent); text-align: center; }
.mode.alt { background: #2f9e7a; }
.mode b { font-size: 22px; }
.mode span { font-size: 13px; opacity: 0.9; }

/* ---------------- Survival-HUD ---------------- */
#hotbar-wrap { flex-direction: column; align-items: center; gap: 4px; }
.hotrow { display: flex; align-items: center; gap: 10px; }
#bars { display: flex; gap: 14px; align-self: stretch; justify-content: space-between; pointer-events: none; flex-wrap: wrap; }
#bars > div { display: flex; gap: 1px; }
#bars i { width: 15px; height: 15px; display: inline-block; background-size: contain; background-repeat: no-repeat; filter: drop-shadow(0 1px 1px rgba(0,0,0,0.7)); }
.h-full { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M1 1h2v1h2V1h2v1h1v2H7v1H6v1H5v1H3V6H2V5H1V4H0V2h1z' fill='%23e8323c'/%3E%3C/svg%3E"); }
.h-half { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M1 1h2v1h2V1h2v1h1v2H7v1H6v1H5v1H3V6H2V5H1V4H0V2h1z' fill='%23331a1e'/%3E%3Cpath d='M1 1h2v1h1v5H3V6H2V5H1V4H0V2h1z' fill='%23e8323c'/%3E%3C/svg%3E"); }
.h-empty { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M1 1h2v1h2V1h2v1h1v2H7v1H6v1H5v1H3V6H2V5H1V4H0V2h1z' fill='%23331a1e'/%3E%3C/svg%3E"); }
.f-full { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M3 0h3v1h1v3H6v1H4v1H3v1H1V6H0V5h1V4h1V1h1z' fill='%23b8743a'/%3E%3Cpath d='M0 5h1v1h1v1H1V6H0z' fill='%23eee'/%3E%3C/svg%3E"); }
.f-half { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M3 0h3v1h1v3H6v1H4v1H3v1H1V6H0V5h1V4h1V1h1z' fill='%23332418'/%3E%3Cpath d='M4 0h2v1h1v3H6v1H4z' fill='%23b8743a'/%3E%3C/svg%3E"); }
.f-empty { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M3 0h3v1h1v3H6v1H4v1H3v1H1V6H0V5h1V4h1V1h1z' fill='%23332418'/%3E%3C/svg%3E"); }
.a-full { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M0 1h3v1h2V1h3v4H7v1H6v1H2V6H1V5H0z' fill='%23d6d6d6'/%3E%3C/svg%3E"); }
.a-half { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M0 1h3v1h2V1h3v4H7v1H6v1H2V6H1V5H0z' fill='%23444'/%3E%3Cpath d='M0 1h3v1h1v5H2V6H1V5H0z' fill='%23d6d6d6'/%3E%3C/svg%3E"); }
.a-empty { display: none !important; }
#bars .food { flex-direction: row-reverse; }
#bars .armor { order: -1; width: 100%; }

.slot { position: relative; }
.slot .cnt { position: absolute; right: 3px; bottom: 1px; font-size: 13px; font-weight: 800; color: #fff; text-shadow: 1px 1px 0 #000, -1px 0 0 #000; pointer-events: none; }
.slot .dur { position: absolute; left: 5px; right: 5px; bottom: 3px; height: 3px; background: #000; pointer-events: none; }
.slot .dur span { display: block; height: 100%; }

#hurtflash { position: fixed; inset: 0; pointer-events: none; background: radial-gradient(ellipse at center, transparent 40%, rgba(220, 20, 30, 0.55)); opacity: 0; transition: opacity 0.4s; z-index: 2; }
#hurtflash.on { opacity: 1; transition: none; }

/* ---------------- Förrådsfönster ---------------- */
.panel.sinv { width: min(620px, 100%); padding: 14px 16px; }
.phead { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.phead h2 { margin: 0; }
.phead [data-close] { margin-left: auto; font-size: 20px; color: #fff; }
.cursor { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); min-height: 52px; }
.cursor .hint { margin: 0; font-size: 12px; max-width: 260px; }
.sgrid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; margin-top: 6px; }
.sgrid .slot, .cursor .slot, .ptop .slot { width: auto; height: auto; aspect-ratio: 1; min-width: 0; background: rgba(255,255,255,0.1); border: 2px solid rgba(255,255,255,0.12); border-radius: 6px; padding: 4px; display: grid; place-items: center; }
.cursor .slot { width: 48px; }
.sgrid .slot img, .ptop .slot img, .cursor .slot img { width: 100%; height: 100%; image-rendering: pixelated; pointer-events: none; }
.phot { margin-top: 10px; }
.phot .slot { background: rgba(255, 207, 90, 0.12); }
.craftrow { display: flex; align-items: center; gap: 14px; justify-content: center; }
.armorcol { display: grid; gap: 4px; width: 52px; }
.armorcol .slot { background: rgba(120, 140, 180, 0.2); }
.sgrid.g2, .sgrid.g3 { margin: 0; width: auto; }
.sgrid.g2 { grid-template-columns: repeat(2, 54px); }
.sgrid.g3 { grid-template-columns: repeat(3, 54px); }
.arrow { font-size: 30px; color: var(--muted); }
.ptop .slot.out { width: 64px; background: rgba(167, 123, 255, 0.25); }
.sgrid.chest { grid-template-columns: repeat(9, 1fr); }
.furnace { display: flex; align-items: center; gap: 18px; justify-content: center; }
.fcol { display: grid; gap: 6px; justify-items: center; width: 56px; }
.fcol .slot { width: 56px; }
.fire { width: 22px; height: 22px; border-radius: 50% 50% 40% 40%; background: #333; }
.fire.on { background: radial-gradient(circle at 50% 70%, #ffe070, #ff7a20 60%, transparent 70%); }
.fprog { width: 90px; height: 12px; border-radius: 6px; background: rgba(255,255,255,0.12); overflow: hidden; }
.fprog span { display: block; height: 100%; width: 0; background: var(--accent2); }

/* ---------------- Chatt ---------------- */
#btn-chat { position: absolute; top: calc(12px + var(--safe-t)); left: calc(70px + var(--safe-l)); }
#btn-chat svg path { fill: rgba(255,255,255,0.85); stroke: none; }
#fps { left: calc(130px + var(--safe-l)); }
#chatfeed { position: absolute; left: calc(14px + var(--safe-l)); top: calc(72px + var(--safe-t)); display: grid; gap: 6px; max-width: min(420px, 60vw); pointer-events: auto; }
.bubble { background: rgba(10, 6, 20, 0.6); padding: 7px 11px; border-radius: 12px; font-size: 15px; transition: opacity 1s; }
.bubble.fade { opacity: 0; }
.play { background: rgba(167,123,255,0.5); padding: 4px 10px; border-radius: 10px; font-size: 14px; }
.chatpanel { width: min(560px, 100%); }
.clog { height: 34vh; overflow: auto; display: grid; align-content: start; gap: 6px; padding: 8px; background: rgba(0,0,0,0.25); border-radius: 12px; font-size: 16px; touch-action: pan-y; }
.emojis { display: flex; flex-wrap: wrap; gap: 4px; margin: 10px 0; }
.emojis button { font-size: 26px; background: rgba(255,255,255,0.07); border-radius: 10px; padding: 4px 6px; }
.cinput { display: flex; gap: 8px; }
.cinput input { flex: 1; min-width: 0; font: inherit; font-size: 17px; padding: 12px; border-radius: 12px; border: 2px solid rgba(255,255,255,0.2); background: rgba(0,0,0,0.3); color: var(--ink); -webkit-user-select: text; user-select: text; }
.cinput .send { padding: 0 16px; border-radius: 12px; background: var(--accent); font-weight: 700; }
.cinput .mic { width: 56px; border-radius: 12px; background: #2f9e7a; font-size: 24px; touch-action: none; }
.cinput .mic.rec { background: #d83a3a; animation: pulse 0.8s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
#dead .start h1 { color: #ff6b6b; text-shadow: 0 0 18px rgba(255, 60, 60, 0.7); }
body:has(#start:not(.hidden)) #hud, body:has(#start:not(.hidden)) #joy { display: none; }
.bookbtn { display: block; width: 100%; padding: 9px; border-radius: 10px; background: rgba(255, 207, 90, 0.18); color: var(--accent2); font-weight: 700; margin-bottom: 8px; }
.recipes { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; max-height: 32vh; overflow: auto; margin-bottom: 8px; touch-action: pan-y; }
.recipe { display: grid; justify-items: center; gap: 3px; padding: 6px 2px; border-radius: 10px; background: rgba(255,255,255,0.05); font-size: 11px; color: var(--muted); opacity: 0.45; }
.recipe.can { opacity: 1; background: rgba(47, 158, 122, 0.3); color: var(--ink); }
.recipe img { width: 36px; height: 36px; image-rendering: pixelated; }
.trade { display: grid; gap: 8px; }
.offer { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 12px; background: rgba(255,255,255,0.07); font-size: 15px; opacity: 0.5; }
.offer.can { opacity: 1; background: rgba(47, 158, 122, 0.3); }
.offer img { width: 34px; height: 34px; image-rendering: pixelated; }
.offer .arrow { font-size: 22px; }
.tips ol { text-align: left; font-size: 17px; line-height: 1.5; padding-left: 22px; }
