* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --panel: rgba(18, 22, 32, 0.94);
  --accent: #6cc24a;
  --accent-2: #4a9e2f;
  --ink: #eef3ea;
  --dim: #9fb0a8;
  /* Ten of these have to fit across the screen whatever the screen is — a hotbar whose
   * last two slots are off the edge is a hotbar you cannot reach two thirds of. The
   * divisor allows for the gaps and the padding either side. */
  --slot: clamp(22px, calc((100vw - 82px) / 10), 60px);
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #7ec0ee;
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

#game-root { position: fixed; inset: 0; overflow: hidden; }

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

.hidden { display: none !important; }
/* The `hidden` attribute is the obvious way to take a chip away, but it is a UA rule on
 * `display`, so any author `display` on the same element silently overrules it and the
 * chip stays on screen. Saying it here once is cheaper than remembering. */
[hidden] { display: none !important; }

/* ---------- crosshair ---------- */
#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 22px;
  height: 22px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 20;
  opacity: 0.85;
  mix-blend-mode: difference;
}
#crosshair i {
  position: absolute;
  background: #fff;
  border-radius: 1px;
}
#crosshair i:nth-child(1) { left: 50%; top: 0; width: 2px; height: 100%; margin-left: -1px; }
#crosshair i:nth-child(2) { top: 50%; left: 0; height: 2px; width: 100%; margin-top: -1px; }

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 12px 14px;
  z-index: 30;
  pointer-events: none;
  padding-top: calc(12px + env(safe-area-inset-top));
}

.hud-back {
  pointer-events: auto;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: rgba(20, 24, 34, 0.6);
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border: 1px solid rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(4px);
}

.hud-chips { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.hud-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(20, 24, 34, 0.6);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(4px);
  white-space: nowrap;
}
.hud-chip .chip-ico { font-size: 15px; }
.hud-chip.good { background: rgba(30, 90, 40, 0.75); }
.hud-chip.warn { background: rgba(120, 70, 20, 0.8); }

#debug {
  position: absolute;
  left: 14px;
  top: calc(60px + env(safe-area-inset-top));
  color: rgba(255, 255, 255, 0.5);
  font: 600 11px/1.5 ui-monospace, Menlo, Consolas, monospace;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
  pointer-events: none;
  white-space: pre;
}

/* ---------- dig progress ---------- */
#digwrap {
  position: absolute;
  left: 50%;
  top: calc(50% + 26px);
  transform: translateX(-50%);
  width: 86px;
  height: 6px;
  border-radius: 3px;
  background: rgba(0, 0, 0, 0.45);
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 21;
  pointer-events: none;
}
#digwrap.on { opacity: 1; }
#digbar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), #f5d766);
  border-radius: 3px;
}

/* ---------- hotbar ---------- */
#hotbar {
  position: absolute;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  padding: 6px;
  border-radius: 16px;
  background: rgba(16, 20, 28, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(6px);
  z-index: 25;
}
.slot {
  position: relative;
  width: var(--slot);
  height: var(--slot);
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.07);
  border: 2px solid rgba(255, 255, 255, 0.10);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.08s, border-color 0.12s;
}
.slot canvas { width: 82%; height: 82%; image-rendering: pixelated; }
.slot .key {
  position: absolute;
  top: 1px; left: 4px;
  font-size: 10px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.82);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}
.slot.on {
  border-color: var(--accent);
  transform: translateY(-5px);
  background: rgba(108, 194, 74, 0.18);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
}
.slot .ico { font-size: 26px; }

/* ---------- floating toast ---------- */
#toast {
  position: absolute;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  z-index: 40;
  pointer-events: none;
}
.toast-item {
  padding: 7px 14px;
  border-radius: 12px;
  background: rgba(18, 22, 32, 0.86);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.14);
  animation: toast-in 0.18s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- full-screen panels ---------- */
.screen {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: radial-gradient(ellipse at 50% 30%, rgba(12, 18, 28, 0.72), rgba(8, 12, 20, 0.92));
  backdrop-filter: blur(3px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.panel {
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 22px;
  padding: 26px 24px;
  text-align: center;
  color: var(--ink);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}
.panel.thin { width: min(400px, 100%); }

.title-badge {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(108, 194, 74, 0.16);
  color: #a8e08a;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  margin-bottom: 12px;
}

.game-title {
  font-size: clamp(30px, 9vw, 44px);
  letter-spacing: -0.02em;
  margin-bottom: 8px;
  background: linear-gradient(180deg, #fff, #9fd08a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.game-sub { color: var(--dim); font-size: 14px; line-height: 1.55; margin-bottom: 18px; }

.over-title { font-size: 24px; margin-bottom: 16px; }

.how {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin-bottom: 20px;
  background: rgba(255, 255, 255, 0.045);
  border-radius: 14px;
  padding: 14px;
}
.how-row { display: flex; align-items: baseline; gap: 10px; font-size: 13px; color: var(--dim); line-height: 1.45; }
.how-row span {
  flex: 0 0 auto;
  min-width: 108px;
  font-weight: 700;
  color: #d8e6d0;
}
.how-row b { color: #fff; }

.big-btn {
  width: 100%;
  padding: 15px 20px;
  border: none;
  border-radius: 14px;
  /* The flat colour is not decoration: it is what is left if the gradient cannot be
   * drawn, and it is what anything reading the colour of this button will find. */
  background: var(--accent);
  background-image: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: #08210a;
  font-size: 17px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 6px 0 #2f6b1e, 0 12px 24px rgba(0, 0, 0, 0.35);
  transition: transform 0.08s, box-shadow 0.08s;
  font-family: inherit;
}
.big-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 #2f6b1e; }

.ghost-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 13px 20px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #e8efe4;
  font-size: 15px;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  font-family: inherit;
}
.ghost-btn:hover { background: rgba(255, 255, 255, 0.12); }

.over-actions { display: flex; flex-direction: column; }

.mobile-tip { margin-top: 14px; font-size: 12px; color: var(--dim); line-height: 1.5; }

/* ---------- loading ---------- */
.loadbar {
  height: 10px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
  margin: 6px 0 4px;
}
#loadfill {
  height: 100%;
  width: 8%;
  border-radius: 5px;
  background: linear-gradient(90deg, var(--accent), #f5d766);
  transition: width 0.3s;
}

/* ---------- block shelf ---------- */
.craft-panel { width: min(620px, 100%); position: relative; }
.craft-close {
  position: absolute;
  top: 14px; right: 14px;
  width: 34px; height: 34px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.07);
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
}
.craft-title { font-size: 20px; margin-bottom: 6px; }
.craft-sub { color: var(--dim); font-size: 13px; line-height: 1.5; margin-bottom: 14px; }

.inv-search {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(0, 0, 0, 0.28);
  color: #fff;
  font-size: 15px;
  font-family: inherit;
  margin-bottom: 14px;
}
.inv-search::placeholder { color: #7f8f88; }

.craft-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
  max-height: min(52vh, 420px);
  overflow-y: auto;
  padding: 4px;
  -webkit-overflow-scrolling: touch;
}
.cell {
  aspect-ratio: 1;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(255, 255, 255, 0.09);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  position: relative;
}
.cell canvas { width: 84%; height: 84%; image-rendering: pixelated; }
.cell:hover { border-color: rgba(255, 255, 255, 0.3); }
.cell.on { border-color: var(--accent); background: rgba(108, 194, 74, 0.18); }
.cell .cap {
  position: absolute;
  inset: auto 0 0 0;
  font-size: 9px;
  font-weight: 700;
  color: #dfe9da;
  background: rgba(0, 0, 0, 0.55);
  padding: 2px 3px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- touch controls ---------- */
#touch { position: absolute; inset: 0; z-index: 35; pointer-events: none; }
#touch > * { pointer-events: auto; }

#joystick {
  position: absolute;
  left: calc(20px + env(safe-area-inset-left));
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 124px;
  height: 124px;
  border-radius: 50%;
  background: rgba(16, 20, 28, 0.38);
  border: 2px solid rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(4px);
}
#joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 52px;
  height: 52px;
  margin: -26px 0 0 -26px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.32);
  border: 2px solid rgba(255, 255, 255, 0.5);
  transition: transform 0.05s;
}

.touch-btns {
  position: absolute;
  right: calc(16px + env(safe-area-inset-right));
  bottom: calc(24px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 9px;
}

.touch-dig {
  position: absolute;
  right: calc(16px + env(safe-area-inset-right));
  bottom: calc(150px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
}

/* The platform puts its own Me and friends launchers in the bottom-right corner of every
 * page, on top of whatever the page has there. On a touch layout the action buttons were
 * headed for exactly that corner, so step the whole cluster left of it — every button
 * then sits where a thumb can actually reach it. */
@media (max-width: 900px), (pointer: coarse) {
  .touch-btns,
  .touch-dig { right: calc(82px + env(safe-area-inset-right)); }
}

.tbtn {
  min-width: 62px;
  height: 62px;
  padding: 0 12px;
  border-radius: 18px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: rgba(16, 20, 28, 0.55);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  font-family: inherit;
  backdrop-filter: blur(4px);
  cursor: pointer;
  touch-action: none;
}
.tbtn:active, .tbtn.on { background: rgba(108, 194, 74, 0.5); }
/* The view switch lives above the joystick rather than in the action grid:
 * it belongs to looking, and a third row there would sit on the Use button.
 * It hangs straight off the touch layer so these offsets are measured from the
 * screen edge, not from the corner of the action cluster. */
.tbtn-view {
  position: absolute;
  left: calc(20px + env(safe-area-inset-left));
  bottom: calc(160px + env(safe-area-inset-bottom));
  min-width: 76px;
  height: 46px;
  font-size: 12px;
}
.tbtn-core { min-width: 82px; height: 74px; font-size: 15px; }
.tbtn-dig { background: rgba(150, 80, 30, 0.55); }
.tbtn-dig:active { background: rgba(210, 120, 50, 0.7); }

/* ---------- narrow screens ---------- */
/* On a phone the thumbs are the problem: the hotbar belongs along the very bottom edge,
 * with the joystick and the action buttons stacked above it rather than on top of it. */
@media (max-width: 720px) {
  .panel { padding: 20px 16px; border-radius: 18px; }
  .how-row { font-size: 12px; }
  .how-row span { min-width: 92px; }
  #hotbar {
    bottom: calc(8px + env(safe-area-inset-bottom));
    gap: 3px;
    padding: 4px;
    border-radius: 12px;
  }
  .slot { border-radius: 8px; }

  #joystick {
    bottom: calc(74px + env(safe-area-inset-bottom));
    width: 108px;
    height: 108px;
  }
  #joystick-knob { width: 46px; height: 46px; margin: -23px 0 0 -23px; }

  .touch-btns { bottom: calc(74px + env(safe-area-inset-bottom)); gap: 7px; }
  .touch-dig { bottom: calc(206px + env(safe-area-inset-bottom)); }

  .tbtn { min-width: 56px; height: 56px; padding: 0 9px; font-size: 12px; }
  .tbtn-view { bottom: calc(196px + env(safe-area-inset-bottom)); height: 46px; }
  .tbtn-core { min-width: 74px; height: 66px; font-size: 14px; }
}

@media (max-height: 460px) {
  #hotbar { bottom: calc(8px + env(safe-area-inset-bottom)); }
  .how { display: none; }
}

/* ---------- hearts, drumsticks, breath ---------- */
/* Bottom-left, above the hotbar, out of the way of the crosshair. Two rows of ten: a
 * child can see how many are left without reading a number, which is the whole point of
 * drawing them as pips rather than as a bar. */
#vitals {
  position: absolute;
  left: 50%;
  bottom: calc(var(--slot) + 46px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: flex-start;
}
.vrow { display: flex; gap: 1px; line-height: 1; }
.vrow .pip {
  font-size: clamp(12px, 3.4vw, 17px);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.55));
}
.vrow .pip.gone { opacity: 0.22; filter: grayscale(1) drop-shadow(0 1px 1px rgba(0, 0, 0, 0.5)); }
.vrow .pip.half { opacity: 0.62; }
/* Air only exists while you are under it, and it is red the moment it starts to matter. */
#row-air { color: #8fd8ff; }
#row-air.low { animation: airpanic 0.7s steps(2, end) infinite; }
@keyframes airpanic { 50% { opacity: 0.35; } }

/* The name of whatever the crosshair is on, and whatever you just picked up. */
#itemname {
  position: absolute;
  left: 50%;
  bottom: calc(var(--slot) + 78px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  pointer-events: none;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  opacity: 0;
  transition: opacity 0.35s;
}
#itemname.on { opacity: 1; }

/* The edge of the world goes red when something takes a bite out of you. */
#hurt {
  position: absolute;
  inset: 0;
  z-index: 25;
  pointer-events: none;
  opacity: 0;
  box-shadow: inset 0 0 120px 24px rgba(170, 20, 20, 0.85);
  transition: opacity 0.25s;
}

/* ---------- the bag ---------- */
.inv-top {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.inv-col { display: flex; flex-direction: column; gap: 6px; }
.col-cap {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--dim);
  font-weight: 700;
}
.bag-cap { margin: 10px 0 6px; }
.armour-col { flex: 0 0 auto; }
.craft-col { flex: 1 1 220px; }
.book-col { flex: 1 1 180px; min-width: 150px; }

/* A square of slots that is exactly as wide as it needs to be, whatever size the
 * squares end up. */
.fit-box {
  display: grid;
  gap: 4px;
  flex: 0 0 auto;
}
.fit-box.one { grid-template-columns: repeat(1, 46px); }
#craft-grid { grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(3, 46px); }
#armour-slots { display: grid; grid-template-columns: repeat(2, 46px); gap: 4px; }

.sq {
  width: 46px;
  height: 46px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(255, 255, 255, 0.09);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
}
.sq canvas, .sq .ico { width: 78%; height: 78%; image-rendering: pixelated; font-size: 22px; line-height: 1; display: flex; align-items: center; justify-content: center; }
.sq:hover { border-color: rgba(255, 255, 255, 0.32); }
.sq.on { border-color: var(--accent); }
.sq .n {
  position: absolute;
  right: 2px;
  bottom: 0;
  font-size: 11px;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 1px 2px #000, 0 0 3px #000;
}
/* A tool with a little left in it shows it — the alternative is a pickaxe that simply
 * vanishes one day. */
.sq .dura {
  position: absolute;
  left: 3px; right: 3px; bottom: 3px;
  height: 3px;
  border-radius: 2px;
  background: rgba(0, 0, 0, 0.55);
  overflow: hidden;
}
.sq .dura i { display: block; height: 100%; background: var(--accent); }
.sq.empty-slot { cursor: default; opacity: 0.5; }
.sq.locked { border-style: dashed; cursor: default; }

.craft-row { display: flex; align-items: center; gap: 10px; }
.craft-arrow { color: var(--dim); font-size: 18px; }
.out-box { background: rgba(108, 194, 74, 0.10); border-color: rgba(108, 194, 74, 0.3); }
.craft-hint { font-size: 11.5px; color: var(--dim); line-height: 1.45; max-width: 240px; margin-top: 6px; }

.book-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 4px;
  max-height: 190px;
  overflow-y: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}
.book-list .sq { width: 100%; height: auto; aspect-ratio: 1; }
.book-list .sq.no { opacity: 0.34; filter: grayscale(0.6); }

.bag-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 5px;
}
.bag-grid .sq { width: 100%; height: auto; aspect-ratio: 1; }
.bag-grid .sq.big { grid-column: span 1; }
/* The hotbar row is separated from the pack because it is the row that stays on screen
 * once this window is shut. */
.bag-grid .sep { grid-column: 1 / -1; height: 8px; }
.chest-grid { margin-bottom: 6px; }

/* What the pointer is holding, following it about. */
#inv-cursor {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  width: 42px;
  height: 42px;
  display: none;
  align-items: center;
  justify-content: center;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.6));
}
#inv-cursor.on { display: flex; }
#inv-cursor canvas, #inv-cursor .ico { width: 34px; height: 34px; image-rendering: pixelated; font-size: 26px; line-height: 1; display: flex; align-items: center; justify-content: center; }
#inv-cursor .n {
  position: absolute; right: 0; bottom: 0;
  font-size: 12px; font-weight: 800; color: #fff;
  text-shadow: 0 1px 2px #000, 0 0 3px #000;
}

/* ---------- furnace ---------- */
.furnace-row { display: flex; align-items: center; gap: 16px; justify-content: center; padding: 6px 0 14px; }
.furnace-stack { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.flame { width: 46px; height: 22px; display: flex; align-items: flex-end; justify-content: center; }
.flame i { width: 16px; height: 0%; background: linear-gradient(#ffd25e, #e2621f); border-radius: 8px 8px 3px 3px; transition: height 0.2s linear; }
.furnace-bar { width: 46px; height: 6px; border-radius: 3px; background: rgba(0, 0, 0, 0.4); overflow: hidden; }
.furnace-bar i { display: block; height: 100%; width: 0%; background: var(--accent); }

@media (max-width: 560px) {
  .inv-top { gap: 10px; }
  .book-col { flex: 1 1 100%; order: 3; }
  .book-list { max-height: 96px; }
  #craft-grid { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); }
  #armour-slots { grid-template-columns: repeat(4, 40px); }
  .sq, .fit-box.one { width: 40px; height: 40px; }
  .fit-box.one { grid-template-columns: repeat(1, 40px); }
}
