:root {
  --color-primary: #15803d;
  --color-primary-light: #22c55e;
  --color-secondary: #166534;
  --color-accent: #d97706;
  --color-accent-light: #fbbf24;
  --color-background: #0f172a;
  --color-muted: #0f1f2b;
  --color-foreground: #ffffff;
  --color-dim: #cbd5e1;
  --color-border: rgba(255, 255, 255, 0.08);
  --color-border-strong: rgba(255, 255, 255, 0.18);
  --font-pixel: 'Press Start 2P', 'Courier New', monospace;
  --font-mono: 'VT323', 'Courier New', monospace;
  --font-ar: 'Cairo', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --glass: rgba(15, 23, 42, 0.72);
  --shadow-lift: 0 18px 40px rgba(0, 0, 0, 0.45);
  --radius: 4px;
  --radius-lg: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--color-background);
  color: var(--color-foreground);
  font-family: var(--font-ar);
  -webkit-tap-highlight-color: transparent;
}

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

canvas.playing { cursor: none; }

.hidden { display: none !important; }
.muted { color: var(--color-dim); font-size: 14px; line-height: 1.7; }

kbd {
  font-family: var(--font-mono);
  font-size: 15px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid var(--color-border-strong);
  border-bottom-width: 2px;
  border-radius: 3px;
  padding: 0 5px;
  margin: 0 1px;
  color: var(--color-accent-light);
}

@keyframes fadeUp {
  /* Animates the individual `translate` property, never `transform`, so an
     element can stay centred with transform: translate(-50%, -50%). */
  from { opacity: 0; translate: 0 14px; }
  to { opacity: 1; translate: 0 0; }
}

@keyframes pulseGlow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.35); }
  50% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); }
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 14px;
  left: 14px;
  padding: 10px 14px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  font-family: var(--font-mono);
  font-size: 17px;
  line-height: 1.45;
  letter-spacing: 0.5px;
  pointer-events: none;
  min-width: 148px;
  z-index: 20;
}
.hud-row { display: flex; justify-content: space-between; gap: 12px; }
.hud-key { color: var(--color-primary-light); }

#crosshair {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  transform: translate(-50%, -50%);
  z-index: 15;
  pointer-events: none;
  opacity: 0.9;
  background:
    linear-gradient(#fff, #fff) center/2px 22px no-repeat,
    linear-gradient(#fff, #fff) center/22px 2px no-repeat;
  mix-blend-mode: difference;
}

#slotName {
  position: fixed;
  bottom: 86px;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--font-ar);
  font-weight: 800;
  font-size: 15px;
  padding: 5px 14px;
  background: var(--glass);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  backdrop-filter: blur(8px);
  z-index: 20;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

#toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(-18px);
  padding: 10px 18px;
  background: rgba(6, 78, 59, 0.92);
  border: 1px solid var(--color-primary-light);
  border-radius: var(--radius-lg);
  font-weight: 600;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 40;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.warn { background: rgba(120, 53, 15, 0.94); border-color: var(--color-accent); }

#lockHint {
  position: fixed;
  bottom: 18px;
  right: 18px;
  font-size: 12px;
  color: var(--color-dim);
  background: var(--glass);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 6px 12px;
  z-index: 20;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
#lockHint.gone { opacity: 0; }

/* ---------- Hotbar ---------- */
#hotbar {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  padding: 7px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  z-index: 25;
}
.slot {
  position: relative;
  width: 54px;
  height: 54px;
  border: 2px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  display: grid;
  place-items: center;
}
.slot img { width: 40px; height: 40px; image-rendering: pixelated; }
.slot .num {
  position: absolute;
  top: 2px;
  left: 4px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--color-dim);
}
.slot:hover { transform: translateY(-3px); border-color: var(--color-primary-light); }
.slot.active {
  border-color: var(--color-accent-light);
  box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.25), 0 8px 20px rgba(0, 0, 0, 0.4);
  transform: translateY(-3px);
}

/* ---------- Panels ---------- */
.panel {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(560px, 92vw);
  height: max-content;
  max-height: 84vh;
  overflow: auto;
  padding: 22px;
  background: var(--glass);
  backdrop-filter: blur(16px);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lift);
  z-index: 45;
  direction: rtl;
  animation: fadeUp 0.25s ease both;
}
.panel h2 {
  font-family: var(--font-pixel);
  font-size: 16px;
  margin: 0 0 10px;
  color: var(--color-accent-light);
}
#invGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 8px;
  margin: 14px 0 18px;
}
.inv-item {
  border: 2px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.04);
  padding: 4px;
  cursor: pointer;
  display: grid;
  gap: 4px;
  justify-items: center;
  color: var(--color-dim);
  font-size: 10px;
  font-family: var(--font-ar);
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.inv-item img { width: 38px; height: 38px; image-rendering: pixelated; }
.inv-item:hover { transform: translateY(-2px); border-color: var(--color-primary-light); color: #fff; }
.inv-item.selected { border-color: var(--color-accent-light); }

table.keys { width: 100%; border-collapse: collapse; direction: rtl; }
table.keys td {
  padding: 7px 4px;
  border-bottom: 1px dashed var(--color-border);
  font-size: 14px;
}
table.keys td:last-child { color: var(--color-dim); text-align: left; }

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-ar);
  font-weight: 800;
  font-size: 15px;
  color: #fff;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  padding: 12px 22px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}
.btn:hover { transform: translateY(-2px); border-color: var(--color-primary-light); }
.btn:active { transform: translateY(0); }
.btn:focus-visible { outline: 2px solid var(--color-accent-light); outline-offset: 2px; }
.btn.primary {
  background: linear-gradient(160deg, var(--color-primary-light), var(--color-primary));
  border-color: #14532d;
  box-shadow: 0 5px 0 #14532d, 0 12px 24px rgba(0, 0, 0, 0.4);
  animation: pulseGlow 2.6s ease-in-out infinite;
}
.btn.primary:hover { box-shadow: 0 6px 0 #14532d, 0 16px 28px rgba(0, 0, 0, 0.45); }
.btn.primary:disabled {
  opacity: 0.55;
  cursor: progress;
  animation: none;
  box-shadow: 0 5px 0 #14532d;
}
.btn.small { font-size: 13px; padding: 8px 16px; margin-top: 12px; }
.btn.floating {
  position: fixed;
  top: 14px;
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  background: var(--glass);
  backdrop-filter: blur(10px);
  z-index: 30;
}
#helpBtn { right: 14px; }
#flyBtn { right: 66px; }
#flyBtn.on { border-color: var(--color-accent-light); color: var(--color-accent-light); }
.btn.floating svg { width: 20px; height: 20px; }

/* ---------- Start overlay ---------- */
#overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 700px at 20% 10%, rgba(21, 128, 61, 0.28), transparent 60%),
    radial-gradient(900px 600px at 85% 85%, rgba(217, 119, 6, 0.22), transparent 60%),
    linear-gradient(rgba(15, 23, 42, 0.72), rgba(15, 23, 42, 0.92)),
    var(--color-background);
  transition: opacity 0.35s ease, visibility 0.35s ease;
  overflow: auto;
}
#overlay.hide { opacity: 0; visibility: hidden; pointer-events: none; }
.ov-card {
  width: min(760px, 100%);
  background: var(--glass);
  backdrop-filter: blur(18px);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-lg);
  padding: 30px 28px 26px;
  box-shadow: var(--shadow-lift);
  direction: rtl;
  text-align: right;
  animation: fadeUp 0.5s ease both;
}
.ov-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  color: var(--color-accent-light);
  border: 1px solid rgba(251, 191, 36, 0.4);
  background: rgba(251, 191, 36, 0.08);
  border-radius: 999px;
  padding: 4px 12px;
  margin-bottom: 14px;
}

.ov-card h1 {
  font-family: var(--font-pixel);
  font-size: clamp(22px, 4.6vw, 40px);
  line-height: 1.35;
  margin: 0 0 12px;
  letter-spacing: -0.5px;
}
.ov-card h1 span { color: var(--color-primary-light); text-shadow: 3px 3px 0 rgba(0, 0, 0, 0.5); }
.ov-sub { margin: 0 0 18px; color: var(--color-dim); font-size: 15px; line-height: 1.85; }
.ov-feats {
  list-style: none;
  margin: 0 0 20px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 8px 18px;
}
.ov-feats li {
  position: relative;
  padding-right: 20px;
  font-size: 14px;
  color: #e2e8f0;
  line-height: 1.7;
}
.ov-feats li::before {
  content: "";
  position: absolute;
  right: 0;
  top: 9px;
  width: 8px;
  height: 8px;
  background: var(--color-primary-light);
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.2);
}
.ov-progress {
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--color-border);
  overflow: hidden;
  margin-bottom: 8px;
}
#progressFill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--color-primary-light), var(--color-accent-light));
  transition: width 0.3s ease;
}
.ov-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0;
  flex-wrap: wrap;
}
.ov-row label { font-size: 14px; font-weight: 600; color: var(--color-dim); }
#seedInput {
  flex: 1 1 200px;
  min-width: 160px;
  font-family: var(--font-mono);
  font-size: 17px;
  padding: 10px 12px;
  color: #fff;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  direction: ltr;
  text-align: left;
}
#seedInput:focus { outline: none; border-color: var(--color-primary-light); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.15); }
.ov-actions { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.ov-help { font-size: 13px; color: var(--color-dim); line-height: 1.9; margin: 0 0 8px; }
.ov-help b { color: var(--color-accent-light); }
.ov-note { font-size: 11px; color: #64748b; margin: 0; }

/* ---------- Touch controls ---------- */
#touch { position: fixed; inset: 0; z-index: 22; pointer-events: none; }
#touch .pad {
  position: absolute;
  bottom: 22px;
  width: 132px;
  height: 132px;
  border-radius: 50%;
  border: 2px dashed rgba(255, 255, 255, 0.22);
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(6px);
  pointer-events: auto;
  touch-action: none;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.45);
  font-size: 12px;
  user-select: none;
}
#tMove { left: 20px; }
#tLook { right: 20px; }
.tbtns {
  position: absolute;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  pointer-events: auto;
}
.tbtn {
  width: 74px;
  height: 74px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border-strong);
  background: rgba(15, 23, 42, 0.72);
  backdrop-filter: blur(8px);
  color: #fff;
  font-family: var(--font-ar);
  font-size: 12px;
  font-weight: 700;
  display: grid;
  place-items: center;
  gap: 2px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.tbtn svg { width: 24px; height: 24px; }
.tbtn:active, .tbtn.on { border-color: var(--color-accent-light); transform: scale(0.94); }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  #hud { font-size: 15px; min-width: 128px; padding: 8px 10px; }
  .slot { width: 44px; height: 44px; }
  .slot img { width: 32px; height: 32px; }
  #slotName { bottom: 74px; font-size: 13px; }
  #lockHint { display: none; }
  .ov-card { padding: 22px 18px; }
  .ov-actions .btn { flex: 1 1 auto; }
  #tools { display: none; }
}
@media (max-width: 420px) {
  .tbtn { width: 64px; height: 64px; font-size: 11px; }
  #touch .pad { width: 112px; height: 112px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001s !important; transition-duration: 0.001s !important; }
}

/* ---------- Inline icon system (SVG sprite, no external icon font) ---------- */
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.ic {
  width: 22px;
  height: 22px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
.btn .ic { width: 19px; height: 19px; }

/* ---------- Floating tool rail ---------- */
#tools {
  position: fixed;
  top: 14px;
  left: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 24;
}
.tool {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: var(--color-dim);
  background: var(--glass);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: transform 0.16s ease, color 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}
.tool:hover { transform: translateY(-2px); color: #fff; border-color: var(--color-border-strong); box-shadow: var(--shadow-lift); }
.tool.on { color: var(--color-accent-light); border-color: rgba(251, 191, 36, 0.45); }

/* ---------- HUD clock ---------- */
#timeRow { opacity: 0.9; }
#timeRow.night #timeVal { color: #a5b4fc; }

/* ---------- Fatal panel (WebGL unavailable / lost) ---------- */
#fatal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(2, 6, 23, 0.86);
  backdrop-filter: blur(6px);
}
.fatal-card { max-width: 460px; text-align: center; }
.fatal-icon {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  margin: 0 auto 14px;
  color: var(--color-accent-light);
  background: rgba(217, 119, 6, 0.16);
  border: 1px solid rgba(251, 191, 36, 0.35);
  border-radius: 50%;
}
.fatal-icon .ic { width: 28px; height: 28px; }
.fatal-card h2 { margin: 0 0 10px; font-size: 20px; }
.diag {
  margin: 12px 0 18px;
  padding: 10px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  color: var(--color-dim);
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  direction: ltr;
  text-align: left;
}

/* ---------- Settings panel (pause) ---------- */
.set-row {
  display: grid;
  grid-template-columns: 150px 1fr 46px;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--color-border);
}
.set-row:last-of-type { border-bottom: 0; }
.set-row label { font-size: 14px; font-weight: 600; color: var(--color-dim); }
.set-row output {
  font-family: var(--font-mono);
  font-size: 16px;
  color: var(--color-accent-light);
  text-align: center;
}
.set-row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.14);
  outline: none;
  cursor: pointer;
}
.set-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary-light);
  border: 2px solid #0b1220;
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.35);
  cursor: pointer;
}
.set-row input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--color-primary-light); border: 2px solid #0b1220; cursor: pointer;
}
.switch-row { grid-template-columns: 1fr auto; }
.switch-row input[type="checkbox"] {
  width: 20px; height: 20px; accent-color: var(--color-primary-light); cursor: pointer;
}
.stats-line { margin: 12px 0 0; font-family: var(--font-mono); font-size: 15px; }
.pause-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.pause-actions .btn.small { margin-top: 0; }
@media (max-width: 560px) {
  .set-row { grid-template-columns: 1fr 60px; }
  .set-row label { grid-column: 1 / -1; }
}

/* The tool rail belongs to mouse play: on touch screens the on-screen pads take
   over, so the rail is hidden (this must stay last in the file to win the cascade). */
@media (max-width: 760px), (pointer: coarse) {
  #tools { display: none; }
}
