:root {
  --bg: #07090d;
  --panel: rgba(12, 14, 20, 0.92);
  --line: rgba(180, 200, 220, 0.18);
  --text: #e8eef7;
  --muted: #9aa8b8;
  --accent: #7dffb3;
  --danger: #ff5d6c;
  --hunger: #ffb454;
  --slot: rgba(20, 24, 32, 0.88);
  --slot-on: rgba(125, 255, 179, 0.28);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

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

#app, #viewport {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #05070b;
}

#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(255,255,255,0.75);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.4);
  pointer-events: none;
  z-index: 5;
}
#crosshair::before, #crosshair::after {
  content: "";
  position: absolute;
  background: rgba(255,255,255,0.8);
}
#crosshair::before {
  left: 50%; top: -6px; width: 2px; height: 26px; margin-left: -1px;
}
#crosshair::after {
  top: 50%; left: -6px; height: 2px; width: 26px; margin-top: -1px;
}

#hurtVignette, #scareFlash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  opacity: 0;
  transition: opacity 80ms linear;
}
#hurtVignette {
  background: radial-gradient(circle at center, transparent 35%, rgba(120,0,20,0.55) 100%);
}
#scareFlash {
  background: rgba(230, 240, 255, 0.55);
}

#hud {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
}
#hud .bars, #hud button, #hotbar, #dialogue, #notePopup { pointer-events: auto; }

.hud-top {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.bars { width: min(320px, 42vw); display: grid; gap: 8px; }
.meter span {
  display: block;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
}
.bar {
  height: 12px;
  background: rgba(0,0,0,0.45);
  border: 1px solid var(--line);
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  width: 100%;
  transform-origin: left center;
  background: linear-gradient(90deg, #6bff9a, #2fd66c);
}
.meter.hunger .bar i {
  background: linear-gradient(90deg, #ffd27a, #ff9d2f);
}

.hud-meta {
  text-align: right;
  max-width: min(420px, 48vw);
  text-shadow: 0 2px 10px rgba(0,0,0,0.8);
}
#objectiveLabel {
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
}
#regionLabel {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

#toast {
  position: absolute;
  left: 50%;
  top: 18%;
  transform: translateX(-50%);
  background: rgba(8,10,14,0.82);
  border: 1px solid var(--line);
  padding: 10px 16px;
  font-size: 14px;
  max-width: min(560px, 88vw);
  text-align: center;
  z-index: 8;
}

#notePopup, #dialogue {
  position: absolute;
  left: 50%;
  bottom: 18%;
  transform: translateX(-50%);
  width: min(560px, 90vw);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 16px 18px;
  z-index: 9;
  pointer-events: auto;
}
#notePopup h3, #dialogue .dialogue-name {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 15px;
  letter-spacing: 0.04em;
}
#notePopup p, #dialogue p {
  margin: 0 0 12px;
  line-height: 1.45;
  color: var(--text);
}
#notePopup button, #dialogue button {
  pointer-events: auto;
}

#hotbar {
  position: absolute;
  left: 50%;
  bottom: calc(14px + var(--safe-bottom));
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  padding: 8px;
  background: rgba(8,10,14,0.55);
  border: 1px solid var(--line);
  z-index: 7;
}
.slot {
  width: 52px;
  height: 52px;
  background: var(--slot);
  border: 2px solid rgba(255,255,255,0.12);
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.slot.active { border-color: var(--accent); background: var(--slot-on); box-shadow: 0 0 0 1px rgba(125,255,179,0.35); }
.slot img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  image-rendering: pixelated;
  pointer-events: none;
}
.slot .count {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 11px;
  font-weight: 700;
  text-shadow: 0 1px 2px #000;
}
.slot .keycap {
  position: absolute;
  left: 3px;
  top: 1px;
  font-size: 9px;
  color: var(--muted);
}

#interactHint {
  position: absolute;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.55);
  border: 1px solid var(--line);
  padding: 6px 10px;
  font-size: 12px;
  color: var(--muted);
}

.overlay-panel {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background:
    linear-gradient(180deg, rgba(4,6,10,0.55), rgba(4,6,10,0.78)),
    center/cover no-repeat var(--title-art, none);
  overflow: auto;
}
.overlay-panel.hidden, .hidden { display: none !important; }

.panel {
  width: min(640px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 28px 28px 24px;
  box-shadow: 0 20px 80px rgba(0,0,0,0.45);
}
.panel.compact { width: min(420px, 100%); text-align: center; }
.title-panel h1 {
  margin: 6px 0 12px;
  font-size: clamp(28px, 5vw, 42px);
  line-height: 1.1;
  letter-spacing: 0.02em;
}
.eyebrow {
  margin: 0;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 12px;
  font-weight: 700;
}
.blurb, .help, .muted, .stats {
  color: var(--muted);
  line-height: 1.5;
}
.help {
  margin: 16px 0 20px;
  padding-left: 18px;
}
.help li { margin: 6px 0; }
.help strong { color: var(--text); }

button {
  appearance: none;
  border: 1px solid rgba(125,255,179,0.45);
  background: linear-gradient(180deg, rgba(60,120,90,0.55), rgba(24,54,42,0.9));
  color: var(--text);
  font: inherit;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 12px 18px;
  cursor: pointer;
}
button:hover { filter: brightness(1.08); }
button:active { transform: translateY(1px); }

#loadStatus { margin: 12px 0 0; font-size: 13px; }

.inventory-panel { width: min(720px, 100%); }
#invGrid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 18px;
}
#invGrid .slot { width: 100%; aspect-ratio: 1; height: auto; }
.row-actions { display: flex; gap: 10px; justify-content: flex-end; }

#touchLayer {
  position: absolute;
  inset: 0;
  z-index: 12;
  pointer-events: none;
}
#touchLayer > * { pointer-events: auto; }
#moveStick {
  position: absolute;
  left: calc(18px + var(--safe-left));
  bottom: calc(18px + var(--safe-bottom));
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.18);
  background: rgba(0,0,0,0.28);
}
#moveStick .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: rgba(125,255,179,0.35);
  border: 1px solid rgba(125,255,179,0.5);
}
#lookPad {
  position: absolute;
  right: 0;
  top: 18%;
  width: 48%;
  height: 55%;
}
.touch-actions {
  position: absolute;
  right: calc(12px + var(--safe-right));
  bottom: calc(14px + var(--safe-bottom));
  display: grid;
  grid-template-columns: repeat(3, minmax(64px, 1fr));
  gap: 8px;
  width: min(240px, 46vw);
}
.touch-actions button {
  min-height: 48px;
  padding: 8px;
  font-size: 12px;
  background: rgba(10,14,18,0.72);
}

@media (max-width: 900px) {
  .slot { width: 42px; height: 42px; }
  .slot img { width: 30px; height: 30px; }
  #hotbar { gap: 4px; padding: 6px; }
  .hud-top { flex-direction: column; }
  .hud-meta { text-align: left; max-width: none; }
  .bars { width: min(100%, 360px); }
}

@media (orientation: portrait) and (max-width: 900px) {
  #rotateHint:not(.hidden) { display: grid !important; }
}
