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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #1a1612;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: #f2ebe3;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#game-stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #1a1612;
}

#game-stage canvas {
  display: block;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  z-index: 0;
}

/* Phone held upright: force landscape playfield via CSS rotate */
html.css-landscape #game-stage {
  inset: auto;
  width: 100vh;
  height: 100vw;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(90deg);
  transform-origin: center center;
}

#blocker {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 16, 12, 0.35);
  backdrop-filter: blur(1.5px);
}

#blocker.hidden {
  display: none;
}

#panel {
  text-align: center;
  padding: 2.5rem 2rem;
  max-width: 28rem;
}

#panel h1 {
  font-size: 2rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  margin-bottom: 0.75rem;
  color: #f5efe6;
}

#panel p {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #c4b5a4;
  margin-bottom: 1.75rem;
}

#start-btn {
  appearance: none;
  border: 1px solid #c4a882;
  background: #3d3228;
  color: #f5efe6;
  font-size: 1rem;
  padding: 0.85rem 1.8rem;
  cursor: pointer;
  letter-spacing: 0.06em;
  transition: background 0.15s ease, border-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

#start-btn:hover {
  background: #524536;
  border-color: #e0c9a8;
}

#hud {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#hud.visible {
  opacity: 1;
}

#hint {
  position: absolute;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  font-size: 0.8rem;
  color: rgba(245, 239, 230, 0.65);
  letter-spacing: 0.04em;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
}

#hp-bar {
  position: absolute;
  left: max(1rem, env(safe-area-inset-left));
  top: max(1rem, env(safe-area-inset-top));
  width: min(11rem, 42vw);
  height: 1.15rem;
  border: 1px solid rgba(245, 239, 230, 0.45);
  background: rgba(20, 14, 10, 0.65);
  overflow: hidden;
}

#hp-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #6aab4a, #9ed06a);
  transition: width 0.15s ease;
}

#hp-fill.low {
  background: linear-gradient(90deg, #a83828, #e07050);
}

#hp-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  color: #f5efe6;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
  pointer-events: none;
}

#combat-toast {
  position: absolute;
  left: 50%;
  top: 22%;
  transform: translateX(-50%);
  padding: 0.4rem 0.8rem;
  background: rgba(20, 14, 10, 0.75);
  border: 1px solid rgba(220, 160, 100, 0.5);
  color: #f5efe6;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#combat-toast.show {
  opacity: 1;
}

#interact-prompt {
  position: absolute;
  left: 50%;
  top: 14%;
  transform: translateX(-50%);
  text-align: center;
  padding: 1rem 1.35rem 1.1rem;
  background: rgba(28, 22, 16, 0.9);
  border: 2px solid rgba(255, 180, 90, 0.85);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  pointer-events: auto;
  max-width: 18rem;
  z-index: 8;
}

#interact-prompt.hidden {
  display: none;
}

#interact-prompt p {
  font-size: 0.95rem;
  color: #f0e6d8;
  margin-bottom: 0.7rem;
}

#open-letter-btn,
#interact-yes {
  appearance: none;
  border: 1px solid #c4a882;
  background: #4a3b2e;
  color: #f5efe6;
  font-size: 0.95rem;
  padding: 0.55rem 1.2rem;
  cursor: pointer;
  letter-spacing: 0.06em;
  -webkit-tap-highlight-color: transparent;
}

#open-letter-btn:hover,
#open-letter-btn:active,
#interact-yes:hover,
#interact-yes:active {
  background: #5c4a3a;
}

.interact-key {
  display: block;
  margin-top: 0.45rem;
  font-size: 0.75rem;
  color: rgba(196, 168, 130, 0.85);
}

#letter-modal {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 10, 8, 0.55);
  pointer-events: auto;
  padding: 1.5rem;
}

#letter-modal.hidden {
  display: none;
}

#letter-sheet {
  width: min(22rem, 100%);
  background:
    linear-gradient(180deg, #f7f0e4 0%, #efe4d2 100%);
  color: #3a3028;
  padding: 2rem 1.6rem 1.4rem;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  border: 1px solid #d8c4a8;
  text-align: center;
}

#letter-body {
  font-family: "Songti SC", "SimSun", "STSong", serif;
  font-size: 1.35rem;
  line-height: 1.8;
  letter-spacing: 0.08em;
  margin-bottom: 1.5rem;
  min-height: 4rem;
}

#close-letter-btn {
  appearance: none;
  border: 1px solid #8a7058;
  background: #ebe0ce;
  color: #3a3028;
  font-size: 0.9rem;
  padding: 0.5rem 1.1rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#close-letter-btn:hover {
  background: #e0d2bc;
}

#touch-ui {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#joystick {
  position: absolute;
  left: max(1rem, env(safe-area-inset-left));
  bottom: max(1.4rem, env(safe-area-inset-bottom));
  width: 148px;
  height: 148px;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}

#joystick-base {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 2px solid rgba(245, 239, 230, 0.35);
  background: rgba(30, 24, 18, 0.35);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.25);
}

#joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 58px;
  margin: -29px 0 0 -29px;
  border-radius: 50%;
  background: rgba(245, 239, 230, 0.55);
  border: 2px solid rgba(245, 239, 230, 0.75);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  transition: background 0.1s ease;
}

#joystick-knob.active {
  background: rgba(245, 239, 230, 0.8);
}

#jump-btn {
  position: absolute;
  right: max(1.4rem, env(safe-area-inset-right));
  bottom: max(2rem, env(safe-area-inset-bottom));
  width: 84px;
  height: 84px;
  border-radius: 50%;
  border: 2px solid rgba(245, 239, 230, 0.45);
  background: rgba(61, 50, 40, 0.55);
  color: #f5efe6;
  font-size: 1.15rem;
  letter-spacing: 0.12em;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

#attack-btn {
  position: absolute;
  right: max(1.4rem, env(safe-area-inset-right));
  bottom: calc(max(2rem, env(safe-area-inset-bottom)) + 96px);
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 2px solid rgba(220, 120, 90, 0.65);
  background: rgba(90, 40, 30, 0.6);
  color: #f5efe6;
  font-size: 1.1rem;
  letter-spacing: 0.1em;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

#jump-btn:active,
#jump-btn.pressed,
#attack-btn:active,
#attack-btn.pressed {
  background: rgba(120, 95, 70, 0.75);
  transform: scale(0.94);
}

#attack-btn:active,
#attack-btn.pressed {
  background: rgba(140, 55, 40, 0.8);
}

.mobile-only {
  display: none;
}

.desktop-only {
  display: block;
}

@media (hover: none), (pointer: coarse) {
  .mobile-only {
    display: block;
  }

  .desktop-only {
    display: none;
  }

  #hud.visible #touch-ui {
    display: block;
  }

  #hint {
    display: none;
  }

  /* Landscape-first mobile HUD */
  #joystick {
    width: 118px;
    height: 118px;
    left: max(1rem, env(safe-area-inset-left));
    bottom: max(0.7rem, env(safe-area-inset-bottom));
  }

  #joystick-knob {
    width: 48px;
    height: 48px;
    margin: -24px 0 0 -24px;
  }

  #jump-btn {
    width: 64px;
    height: 64px;
    font-size: 1rem;
    right: max(1rem, env(safe-area-inset-right));
    bottom: max(0.75rem, env(safe-area-inset-bottom));
  }

  #attack-btn {
    width: 60px;
    height: 60px;
    font-size: 0.95rem;
    right: calc(max(1rem, env(safe-area-inset-right)) + 74px);
    bottom: max(0.85rem, env(safe-area-inset-bottom));
  }

  #hp-bar {
    top: max(0.5rem, env(safe-area-inset-top));
    left: max(0.7rem, env(safe-area-inset-left));
    width: min(9.5rem, 28%);
    height: 1rem;
  }

  #interact-prompt {
    top: 8%;
    padding: 0.75rem 1rem 0.85rem;
    max-width: 16rem;
  }

  #panel {
    padding: 1.1rem 1.4rem;
    max-width: 22rem;
  }

  #panel h1 {
    font-size: 1.4rem;
    margin-bottom: 0.45rem;
  }

  #panel p {
    font-size: 0.85rem;
    margin-bottom: 1rem;
  }

  #letter-sheet {
    max-width: min(22rem, 70%);
    padding: 1.4rem 1.3rem 1.1rem;
  }

  #letter-body {
    font-size: 1.15rem;
    min-height: 3rem;
    margin-bottom: 1rem;
  }
}
