:root {
  --bg: #0f1419;
  --panel: #1a2332;
  --panel-border: #2d3a4d;
  --text: #e8eef5;
  --muted: #8a9bb0;
  --accent: #3d9cf5;
  --accent-dim: #2563a8;
  --success: #4ade80;
  --danger: #f87171;
  --slot: #0d1218;
  --slot-border: #3d4f66;
  --slot-active: #3d9cf5;
  --font: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  max-height: 100dvh;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #1e2a1e;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #1a2838 0%, #0a0e12 100%);
  z-index: 30;
}

.overlay.hidden {
  display: none;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 1.5rem 1.75rem;
  max-width: min(420px, 92vw);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.panel-wide {
  max-width: min(720px, 96vw);
}

.title-panel {
  text-align: center;
}

.title-panel h1 {
  margin: 0 0 0.35rem;
  font-size: clamp(1.75rem, 5vw, 2.25rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0 0 1.5rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.btn-row {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.btn {
  font: inherit;
  font-size: 1rem;
  padding: 0.75rem 1.25rem;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #243044;
  color: var(--text);
  cursor: pointer;
  min-height: 48px;
}

.btn:hover {
  background: #2c3a52;
}

.btn.primary {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.btn.primary:hover {
  background: var(--accent);
}

.btn.ghost {
  background: transparent;
  color: var(--muted);
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.setting-row input[type="range"] {
  width: 140px;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  margin: 0 0 1rem;
}

kbd {
  font-size: 0.75em;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  border: 1px solid var(--panel-border);
  background: var(--slot);
}

.hidden {
  display: none !important;
}

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

#hud > * {
  pointer-events: auto;
}

.hud-left-stack {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  z-index: 12;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  pointer-events: none;
}

.hud-left-stack .minimap-wrap {
  pointer-events: auto;
  cursor: pointer;
}

.hud-coins {
  font-size: 0.92rem;
  font-weight: 600;
  padding: 0.35rem 0.65rem;
  background: rgba(12, 16, 22, 0.9);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  color: #f5d76e;
}

.minimap-wrap {
  padding: 4px;
  background: rgba(12, 16, 22, 0.82);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}

#minimap {
  display: block;
  width: 132px;
  height: 132px;
  border-radius: 6px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.hud-prompt {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 20, 25, 0.85);
  border: 1px solid var(--panel-border);
  padding: 0.5rem 1rem;
  border-radius: 10px;
  font-size: 0.9rem;
  max-width: min(520px, calc(100vw - 175px));
  text-align: center;
  min-height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.hud-prompt:empty {
  display: none;
}

.hotbar {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  background: rgba(15, 20, 25, 0.88);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  pointer-events: auto;
}

.hotbar .slot {
  width: 52px;
  height: 52px;
}

@media (max-width: 600px), (pointer: coarse) {
  .hotbar .slot {
    width: 56px;
    height: 56px;
    min-width: 48px;
    min-height: 48px;
  }
}

.slot {
  position: relative;
  background: var(--slot);
  border: 2px solid var(--slot-border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  text-align: center;
  padding: 2px;
  cursor: pointer;
  user-select: none;
  overflow: hidden;
}

.slot .slot-icon {
  font-size: 1.35rem;
  line-height: 1;
}

.slot .slot-count {
  position: absolute;
  right: 3px;
  bottom: 1px;
  font-size: 0.7rem;
  font-weight: 700;
  text-shadow: 0 0 2px #000;
}

.slot.selected {
  border-color: var(--slot-active);
  box-shadow: 0 0 0 2px rgba(61, 156, 245, 0.35);
}

.slot-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.inv-grid {
  grid-template-columns: repeat(5, 1fr);
}

@media (min-width: 480px) {
  .inv-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

.slot-grid .slot {
  aspect-ratio: 1;
  width: 100%;
  height: auto;
  min-height: 48px;
}

.icon-btn {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: max(16px, env(safe-area-inset-right));
  min-width: 56px;
  min-height: 48px;
  padding: 0.5rem 0.75rem;
  border-radius: 10px;
  z-index: 11;
}

@media (pointer: coarse) {
  .icon-btn {
    top: max(12px, env(safe-area-inset-top));
    bottom: auto;
    right: max(12px, env(safe-area-inset-right));
  }
}

.modal {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.modal-content {
  position: relative;
  z-index: 1;
  max-height: min(88vh, 100dvh - 24px);
  overflow: auto;
  width: 100%;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.modal-head h2 {
  margin: 0;
  font-size: 1.25rem;
}

.btn-close {
  font-size: 1.5rem;
  line-height: 1;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
}

.btn-close:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
}

.modal-foot {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 0.75rem;
}

.tab {
  padding: 0.45rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: var(--slot);
  color: var(--muted);
  font: inherit;
  cursor: pointer;
  min-height: 40px;
}

.tab.active {
  border-color: var(--accent);
  color: var(--text);
  background: #243044;
}

.craft-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.craft-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--slot);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
}

.craft-row .meta {
  flex: 1;
  min-width: 140px;
}

.craft-row .name {
  font-weight: 600;
  margin-bottom: 4px;
}

.craft-row .cost {
  font-size: 0.8rem;
  color: var(--muted);
}

.craft-row .craft-btn {
  min-height: 44px;
  min-width: 88px;
}

.craft-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.chest-split {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.25rem;
}

@media (min-width: 560px) {
  .chest-split {
    grid-template-columns: 1fr 1fr;
  }
}

.chest-split h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  color: var(--muted);
}

.mobile-controls {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 15;
}

.mobile-controls .joystick-zone,
.mobile-controls .action-fab {
  pointer-events: auto;
}

.joystick-zone {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(100px, calc(env(safe-area-inset-bottom) + 88px));
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: rgba(30, 40, 55, 0.45);
  border: 2px solid rgba(255, 255, 255, 0.12);
  touch-action: none;
}

.joystick-knob {
  position: absolute;
  left: 0;
  top: 0;
  width: 48px;
  height: 48px;
  margin: 0;
  border-radius: 50%;
  background: rgba(61, 156, 245, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.25);
  pointer-events: none;
}

.action-fab {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(100px, calc(env(safe-area-inset-bottom) + 88px));
  width: 72px;
  height: 72px;
  border-radius: 50%;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  border: 2px solid var(--accent);
  background: rgba(37, 99, 168, 0.92);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.gather-bar-wrap {
  position: absolute;
  left: 50%;
  bottom: 120px;
  transform: translateX(-50%);
  width: min(200px, 60vw);
  height: 8px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  overflow: hidden;
  z-index: 12;
}

.gather-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-dim), var(--accent));
  border-radius: 4px;
  transition: width 0.05s linear;
}

.map-modal-panel {
  max-width: min(480px, 96vw);
}

.map-big-canvas {
  display: block;
  width: 100%;
  max-width: 400px;
  height: auto;
  margin: 0 auto;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  cursor: crosshair;
  touch-action: none;
}

.shop-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(55vh, 420px);
  overflow: auto;
}

.shop-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--slot);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
}

.shop-row .meta {
  flex: 1;
  min-width: 160px;
}

.shop-row .sell-btn {
  min-height: 44px;
  min-width: 88px;
}

body.game-running #screen-start.hidden ~ #hud,
body.game-running #hud:not(.hidden) {
  display: block;
}

body:not(.game-running) #hud {
  display: none !important;
}

body:not(.game-running) .mobile-controls {
  display: none !important;
}
