:root {
  --sky-top: #6eb6e8;
  --sky-bottom: #d8f0ff;
  --moss: #2f6b3a;
  --moss-deep: #1e4a28;
  --gold: #c9a227;
  --gold-soft: #e8d48b;
  --ink: #1a2418;
  --parchment: #f3e7c7;
  --panel: rgba(22, 36, 24, 0.92);
  --panel-border: rgba(201, 162, 39, 0.45);
  --accent: #3d8f5a;
  --danger: #b33a3a;
  --crystal: #5ec8c0;
}

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

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: "Nunito", system-ui, sans-serif;
  background: #0d1a12;
  color: var(--parchment);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

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

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse 80% 60% at 50% 20%, rgba(94, 200, 192, 0.18), transparent 55%),
    linear-gradient(180deg, #1a3a2a 0%, #0d1a12 55%, #142218 100%);
}

.overlay.hidden {
  display: none !important;
  pointer-events: none;
}

.title-card {
  text-align: center;
  max-width: 34rem;
  padding: 2.5rem 2rem;
  animation: rise 0.9s ease-out both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(18px); }
  to { opacity: 1; transform: translateY(0); }
}

.eyebrow {
  font-size: 0.75rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--crystal);
  margin-bottom: 0.75rem;
}

.title-card h1 {
  font-family: "Cinzel", serif;
  font-size: clamp(2.6rem, 8vw, 4.2rem);
  font-weight: 700;
  color: var(--gold-soft);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.35);
  line-height: 1.05;
  margin-bottom: 1rem;
}

.tagline {
  font-size: 1.05rem;
  line-height: 1.55;
  color: rgba(243, 231, 199, 0.88);
  margin-bottom: 1.75rem;
}

#start-btn {
  font-family: "Cinzel", serif;
  font-size: 1.05rem;
  font-weight: 600;
  padding: 0.85rem 2rem;
  border: 2px solid var(--gold);
  background: linear-gradient(180deg, #3d8f5a, #2a6840);
  color: #fff8e7;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}

#start-btn:hover {
  transform: translateY(-2px);
  filter: brightness(1.08);
}

.controls-hint {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem 1rem;
  justify-content: center;
  margin-top: 1.75rem;
  font-size: 0.82rem;
  color: rgba(243, 231, 199, 0.7);
}

.controls-hint .chip,
.controls-hint kbd {
  display: inline-block;
  padding: 0.12rem 0.4rem;
  border: 1px solid rgba(201, 162, 39, 0.4);
  background: rgba(0, 0, 0, 0.25);
  font-family: inherit;
  font-size: 0.78rem;
  color: var(--gold-soft);
  margin-right: 0.25rem;
}

.hud {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
}

.hidden {
  display: none !important;
}

.crosshair {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(255, 248, 231, 0.85);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35);
}

.crosshair::before,
.crosshair::after {
  content: "";
  position: absolute;
  background: rgba(255, 248, 231, 0.85);
}

.crosshair::before {
  left: 50%;
  top: -6px;
  bottom: -6px;
  width: 2px;
  margin-left: -1px;
}

.crosshair::after {
  top: 50%;
  left: -6px;
  right: -6px;
  height: 2px;
  margin-top: -1px;
}

.top-bar {
  position: absolute;
  top: calc(0.65rem + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  justify-content: center;
  width: min(96vw, 420px);
  padding: 0 0.35rem;
}

.stat {
  pointer-events: none;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  padding: 0.35rem 0.55rem;
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.hotbar {
  position: absolute;
  bottom: calc(9.5rem + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 3px;
  pointer-events: auto;
  max-width: 96vw;
  overflow-x: auto;
  padding: 0.2rem;
  -webkit-overflow-scrolling: touch;
  z-index: 26;
}

.slot {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  background: rgba(18, 30, 20, 0.92);
  border: 2px solid rgba(201, 162, 39, 0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: 1.2rem;
  line-height: 1;
  touch-action: manipulation;
  cursor: pointer;
}

.slot.selected {
  border-color: var(--gold);
  box-shadow: 0 0 0 1px var(--gold-soft);
}

.slot .count {
  position: absolute;
  right: 3px;
  bottom: 1px;
  font-size: 0.65rem;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 1px 2px #000;
}

.slot .key {
  display: none;
}

.hint-bar {
  display: none;
}

.toast {
  position: absolute;
  top: calc(4.2rem + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  padding: 0.55rem 1rem;
  font-weight: 700;
  font-size: 0.9rem;
  max-width: min(92vw, 28rem);
  text-align: center;
  animation: toast-in 0.25s ease;
  pointer-events: none;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(-8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.dialogue {
  position: absolute;
  bottom: calc(11rem + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  width: min(92vw, 32rem);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  padding: 1rem 1.15rem 1.1rem;
  pointer-events: auto;
  z-index: 25;
}

.dialogue-name {
  font-family: "Cinzel", serif;
  color: var(--gold-soft);
  margin-bottom: 0.35rem;
  font-size: 1.05rem;
}

.dialogue p {
  line-height: 1.45;
  margin-bottom: 0.85rem;
  color: rgba(243, 231, 199, 0.92);
}

#dialogue-close {
  font-family: inherit;
  font-weight: 700;
  border: 1px solid var(--gold);
  background: var(--accent);
  color: #fff8e7;
  padding: 0.4rem 0.9rem;
  cursor: pointer;
}

.panel {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(8, 14, 10, 0.72);
  pointer-events: auto;
}

.panel-inner {
  width: min(94vw, 720px);
  max-height: 85vh;
  overflow: auto;
  background:
    linear-gradient(160deg, rgba(40, 62, 44, 0.98), rgba(18, 30, 22, 0.98));
  border: 1px solid var(--panel-border);
  padding: 1.25rem 1.35rem 1.5rem;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

.panel-inner.narrow {
  width: min(94vw, 520px);
}

.panel-inner header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.panel-inner h2 {
  font-family: "Cinzel", serif;
  color: var(--gold-soft);
  font-size: 1.45rem;
}

.panel-inner h3 {
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--crystal);
  margin-bottom: 0.65rem;
}

.close-btn {
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--panel-border);
  background: transparent;
  color: var(--parchment);
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
}

.panel-note {
  font-size: 0.9rem;
  color: rgba(243, 231, 199, 0.75);
  margin-bottom: 1rem;
  line-height: 1.4;
}

.craft-layout {
  display: grid;
  grid-template-columns: 1fr 1.6fr;
  gap: 1.25rem;
}

@media (max-width: 640px) {
  .craft-layout {
    grid-template-columns: 1fr;
  }
}

#inv-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

#inv-list li {
  display: flex;
  justify-content: space-between;
  padding: 0.4rem 0.55rem;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.06);
  font-size: 0.9rem;
}

.recipe-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.55rem;
}

.recipe-card {
  text-align: left;
  padding: 0.7rem 0.75rem;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(201, 162, 39, 0.28);
  color: inherit;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s, background 0.15s;
}

.recipe-card:hover:not(:disabled) {
  border-color: var(--gold);
  background: rgba(61, 143, 90, 0.25);
}

.recipe-card:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.recipe-card .icon {
  font-size: 1.5rem;
  display: block;
  margin-bottom: 0.25rem;
}

.recipe-card .name {
  font-weight: 800;
  font-size: 0.88rem;
  display: block;
  margin-bottom: 0.2rem;
}

.recipe-card .cost {
  font-size: 0.72rem;
  color: rgba(243, 231, 199, 0.65);
  line-height: 1.35;
}

.recipe-card.selected {
  border-color: var(--gold);
  box-shadow: inset 0 0 0 1px var(--gold);
}

/* ——— Touch / trackpad control layer ——— */
.touch-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 22;
}

.look-pad {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: calc(10.5rem + env(safe-area-inset-bottom, 0px));
  pointer-events: auto;
  touch-action: none;
  z-index: 1;
}

.move-stick {
  position: absolute;
  left: max(0.75rem, env(safe-area-inset-left));
  bottom: calc(1.1rem + env(safe-area-inset-bottom, 0px));
  width: 128px;
  height: 128px;
  pointer-events: auto;
  touch-action: none;
  z-index: 24;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
}

.stick-base {
  width: 110px;
  height: 110px;
  border-radius: 50%;
  background: rgba(12, 22, 14, 0.45);
  border: 2px solid rgba(232, 212, 139, 0.35);
  position: relative;
  touch-action: none;
}

.stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46px;
  height: 46px;
  margin: -23px 0 0 -23px;
  border-radius: 50%;
  background: rgba(61, 143, 90, 0.85);
  border: 2px solid rgba(232, 212, 139, 0.7);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  will-change: transform;
}

.stick-label {
  font-size: 0.65rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(243, 231, 199, 0.55);
}

.action-cluster {
  position: absolute;
  right: max(0.55rem, env(safe-area-inset-right));
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  width: 168px;
  height: 200px;
  pointer-events: auto;
  z-index: 24;
}

.pad-btn {
  position: absolute;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 2px solid rgba(232, 212, 139, 0.45);
  background: rgba(18, 30, 20, 0.82);
  color: #fff8e7;
  font-size: 1.35rem;
  display: grid;
  place-items: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
  user-select: none;
}

.pad-btn.pressed,
.pad-btn:active {
  transform: scale(0.94);
  filter: brightness(1.15);
  border-color: var(--gold);
}

.pad-btn.menu-btn {
  width: 46px;
  height: 46px;
  font-size: 1.05rem;
  background: rgba(22, 40, 28, 0.88);
}

#btn-craft { top: 0; right: 56px; }
#btn-build { top: 0; right: 0; }
#btn-talk { top: 52px; right: 28px; }
#btn-jump {
  top: 70px;
  left: 8px;
  background: rgba(61, 143, 90, 0.75);
}
#btn-mine {
  bottom: 0;
  right: 62px;
  width: 64px;
  height: 64px;
  background: rgba(160, 70, 50, 0.82);
}
#btn-place {
  bottom: 8px;
  right: 0;
  width: 64px;
  height: 64px;
  background: rgba(70, 110, 150, 0.82);
}

.panel-inner {
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  max-height: min(85vh, calc(100dvh - 2rem));
}

.recipe-card,
.close-btn,
#dialogue-close,
#start-btn {
  touch-action: manipulation;
  min-height: 44px;
}

@media (min-width: 900px) and (orientation: landscape) {
  .hotbar {
    bottom: calc(2.2rem + env(safe-area-inset-bottom, 0px));
  }
  .look-pad {
    bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  }
  .move-stick {
    bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
  }
  .action-cluster {
    bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
  }
  .dialogue {
    bottom: calc(8rem + env(safe-area-inset-bottom, 0px));
  }
}

/* Portrait phones: slightly tighter */
@media (max-width: 430px) and (orientation: portrait) {
  .title-card {
    padding: 1.5rem 1.1rem;
  }
  .title-card h1 {
    font-size: 2.4rem;
  }
  .move-stick {
    width: 118px;
    height: 118px;
  }
  .stick-base {
    width: 100px;
    height: 100px;
  }
  .action-cluster {
    width: 156px;
  }
  .pad-btn {
    width: 54px;
    height: 54px;
  }
  #btn-mine,
  #btn-place {
    width: 60px;
    height: 60px;
  }
  .slot {
    width: 42px;
    height: 42px;
    font-size: 1.05rem;
  }
}

/* Hide touch chrome only if we ever need — keep always visible per request */
.hud.hidden .touch-layer {
  display: none;
}
