:root {
  --ink: #edf3f7;
  --muted: #9aafbd;
  --panel: rgba(10, 16, 22, 0.72);
  --accent: #6ec8ff;
  --line: rgba(255, 255, 255, 0.14);
}

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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #071018;
  color: var(--ink);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  user-select: none;
  touch-action: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  position: relative;
  z-index: 1;
}

/* Login / menu must stay tappable on mobile (body has touch-action: none). */
#overlay,
#overlay * {
  touch-action: manipulation;
}

body:not(.playing) canvas {
  pointer-events: none;
}

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

#crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transition: opacity 0.15s ease;
}

body.playing #crosshair {
  opacity: 1;
}

#hint {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  max-width: calc(100vw - 24px);
  overflow: hidden;
  text-overflow: ellipsis;
}

#hint strong {
  color: var(--ink);
  font-weight: 700;
}

#player-label {
  position: absolute;
  left: 16px;
  top: 14px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  font-size: 13px;
  color: var(--ink);
  opacity: 0;
  transition: opacity 0.15s ease;
}

body.playing #player-label {
  opacity: 1;
}

#inv-btn {
  position: absolute;
  right: 16px;
  top: 14px;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(8px);
  color: var(--ink);
  font: inherit;
  display: grid;
  place-items: center;
  cursor: pointer;
  pointer-events: auto;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease, border-color 0.15s ease;
  z-index: 5;
  -webkit-tap-highlight-color: transparent;
}

body.playing #inv-btn {
  opacity: 1;
}

#inv-btn:hover,
#inv-btn[aria-expanded="true"] {
  border-color: var(--accent);
  background: rgba(110, 200, 255, 0.18);
}

.inv-btn-icon {
  font-size: 18px;
  line-height: 1;
  opacity: 0.92;
}

#stats {
  position: absolute;
  right: 16px;
  top: 66px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

body.playing #stats {
  opacity: 1;
}

#coords,
#fps {
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}

body.mobile.playing #inv-btn {
  top: max(12px, env(safe-area-inset-top));
  /* левее кнопок ☰ / 3л */
  right: max(116px, calc(env(safe-area-inset-right) + 104px));
}

body.mobile.playing #stats {
  top: max(64px, calc(env(safe-area-inset-top) + 52px));
  right: max(12px, env(safe-area-inset-right));
}

body.mobile.playing #coords,
body.mobile.playing #fps {
  font-size: 11px;
}

#hotbar {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

body.mobile.playing #hotbar {
  bottom: 28px;
  pointer-events: auto;
}

body.mobile.playing #hotbar .slot {
  pointer-events: auto;
}

#mobile-controls {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
}

body.mobile.playing #mobile-controls {
  display: block;
}

.stick {
  position: absolute;
  width: 132px;
  height: 132px;
  pointer-events: auto;
  touch-action: none;
}

#stick-move {
  left: max(12px, env(safe-area-inset-left));
  bottom: max(88px, calc(env(safe-area-inset-bottom) + 72px));
}

#stick-look {
  right: max(12px, env(safe-area-inset-right));
  bottom: max(88px, calc(env(safe-area-inset-bottom) + 72px));
}

.stick-base {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: rgba(8, 14, 20, 0.38);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: rgba(180, 220, 255, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.55);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  will-change: transform;
}

.mob-btn {
  position: absolute;
  pointer-events: auto;
  touch-action: manipulation;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  background: rgba(8, 14, 20, 0.55);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 12px 16px;
  min-width: 72px;
  -webkit-tap-highlight-color: transparent;
}

.mob-btn:active {
  background: rgba(110, 200, 255, 0.35);
}

#mob-jump {
  right: max(12px, env(safe-area-inset-right));
  bottom: max(236px, calc(env(safe-area-inset-bottom) + 220px));
}

#mob-place {
  right: max(100px, calc(env(safe-area-inset-right) + 88px));
  bottom: max(236px, calc(env(safe-area-inset-bottom) + 220px));
}

#mob-chat {
  left: max(12px, env(safe-area-inset-left));
  bottom: max(236px, calc(env(safe-area-inset-bottom) + 220px));
}

body.mobile.chatting #chat {
  left: 12px;
  right: 12px;
  bottom: max(12px, env(safe-area-inset-bottom));
  width: auto;
  opacity: 1;
  z-index: 4;
  pointer-events: auto;
}

body.mobile.chatting #chat-form {
  display: block;
}

body.mobile.chatting #chat-input {
  font-size: 16px; /* avoid iOS zoom on focus */
  padding: 12px 14px;
}

body.mobile.chatting #mobile-controls .stick,
body.mobile.chatting #mob-jump,
body.mobile.chatting #mob-place,
body.mobile.chatting #mob-chat {
  visibility: hidden;
  pointer-events: none;
}

.mob-btn-icon {
  min-width: 44px;
  width: 44px;
  height: 44px;
  padding: 0;
  top: max(12px, env(safe-area-inset-top));
  display: grid;
  place-items: center;
}

#mob-menu {
  right: max(12px, env(safe-area-inset-right));
}

#mob-view {
  right: max(64px, calc(env(safe-area-inset-right) + 52px));
}

.controls-mobile {
  display: none;
}

body.mobile .controls-desktop {
  display: none;
}

body.mobile .controls-mobile {
  display: grid;
}

body.mobile #hint {
  display: none;
}

body.mobile #crosshair {
  opacity: 0 !important;
}

body.mobile.playing #chat {
  left: 12px;
  right: auto;
  bottom: max(240px, calc(env(safe-area-inset-bottom) + 220px));
  width: min(220px, 42vw);
  opacity: 0.85;
}

body.mobile.playing #player-label {
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  bottom: auto;
}

.slot {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: var(--panel);
  display: grid;
  place-items: center;
  position: relative;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.slot.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(110, 200, 255, 0.25);
}

.slot .swatch {
  width: 26px;
  height: 26px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.35);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.slot .key {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 10px;
  color: var(--muted);
}

.slot .count {
  position: absolute;
  left: 5px;
  bottom: 2px;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
}

.slot .swatch.empty {
  opacity: 0.2;
  border-style: dashed;
}

#break-bar {
  position: absolute;
  left: 50%;
  top: calc(50% + 22px);
  width: 72px;
  height: 6px;
  margin-left: -36px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  opacity: 1;
  pointer-events: none;
}

#break-bar.hidden {
  display: none;
}

#break-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #f0c14a, #fff3c4);
  box-shadow: 0 0 8px rgba(240, 193, 74, 0.45);
}

#inventory {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(4, 10, 16, 0.55);
  pointer-events: auto;
  z-index: 6;
  padding: 16px;
}

#inventory.hidden {
  display: none;
}

.inv-card {
  width: min(420px, 100%);
  padding: 16px 16px 18px;
  border-radius: 16px;
  background: rgba(12, 18, 26, 0.94);
  border: 1px solid var(--line);
  backdrop-filter: blur(12px);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.45);
}

.inv-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.inv-head h2 {
  font-size: 18px;
  font-weight: 700;
}

.inv-hint {
  font-size: 12px;
  color: var(--muted);
}

.inv-hint kbd {
  font: inherit;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
}

.inv-hotbar-label {
  margin: 14px 0 8px;
  font-size: 12px;
  color: var(--muted);
}

.inv-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.inv-slot {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  color: inherit;
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

.inv-slot:hover {
  border-color: rgba(110, 200, 255, 0.45);
}

.inv-slot.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(110, 200, 255, 0.22);
}

.inv-slot .swatch {
  width: 58%;
  height: 58%;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.35);
  background-size: 100% 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.inv-slot .count {
  position: absolute;
  right: 5px;
  bottom: 3px;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.75);
}

.inv-slot .inv-key {
  position: absolute;
  left: 5px;
  top: 3px;
  font-size: 10px;
  color: var(--muted);
}

body.inventory-open #crosshair,
body.inventory-open #break-bar {
  opacity: 0;
  visibility: hidden;
}

#chat {
  position: absolute;
  left: 16px;
  bottom: 78px;
  width: min(360px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

body.playing #chat {
  opacity: 1;
}

#chat-log {
  max-height: 180px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 4px;
  pointer-events: none;
}

.chat-line {
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(8, 14, 20, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 13px;
  line-height: 1.35;
  color: var(--ink);
  word-break: break-word;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.chat-line .who {
  color: var(--accent);
  font-weight: 700;
  margin-right: 6px;
}

#chat-form {
  display: none;
  pointer-events: auto;
}

body.chatting #chat-form {
  display: block;
}

#chat-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: rgba(8, 14, 20, 0.92);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  outline: none;
  pointer-events: auto;
  user-select: text;
}

#chat-input:focus {
  border-color: var(--accent);
}

#overlay {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: 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));
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(ellipse 70% 50% at 50% 0%, rgba(70, 130, 180, 0.28), transparent 55%),
    rgba(5, 10, 16, 0.82);
  backdrop-filter: blur(6px);
}

#overlay.hidden {
  display: none;
}

.card {
  width: min(420px, calc(100vw - 24px));
  max-height: min(100%, 100dvh, 100svh);
  padding: 28px 26px 24px;
  border-radius: 18px;
  background: rgba(12, 18, 26, 0.9);
  border: 1px solid var(--line);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.card h1 {
  font-size: clamp(2rem, 6vw, 2.6rem);
  letter-spacing: -0.04em;
  margin-bottom: 8px;
}

.card p,
.card .card-lead {
  color: var(--muted);
  line-height: 1.5;
  margin-bottom: 18px;
}

.card p.status {
  margin-bottom: 14px;
  font-size: 0.9rem;
  color: var(--accent);
}

.login-label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

#login-name {
  width: 100%;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  outline: none;
  pointer-events: auto;
  user-select: text;
}

#login-name:focus {
  border-color: var(--accent);
}

.card button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
}

.card button {
  pointer-events: auto;
  width: 100%;
  border: 0;
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 1rem;
  font-weight: 700;
  color: #061018;
  background: linear-gradient(180deg, #9ad8ff, #5eb6ef);
  cursor: pointer;
}

.card button:hover {
  filter: brightness(1.05);
}

.card ul {
  list-style: none;
  margin-top: 18px;
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}

kbd {
  display: inline-block;
  min-width: 1.4em;
  padding: 1px 6px;
  margin-right: 2px;
  border-radius: 5px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  font-size: 0.85em;
  text-align: center;
}

#rotate-gate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  place-items: center;
  background: #071018;
  color: var(--ink);
  text-align: center;
  padding: 24px;
}

.rotate-card {
  max-width: 280px;
}

.rotate-card p {
  font-size: 1.15rem;
  line-height: 1.4;
  margin-top: 18px;
}

.rotate-card .rotate-sub {
  margin-top: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

.rotate-phone {
  width: 52px;
  height: 88px;
  margin: 0 auto;
  border: 3px solid var(--accent);
  border-radius: 10px;
  position: relative;
  animation: rotate-hint 1.4s ease-in-out infinite;
}

.rotate-phone::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 8px;
  width: 16px;
  height: 3px;
  margin-left: -8px;
  border-radius: 2px;
  background: var(--accent);
}

@keyframes rotate-hint {
  0%,
  25% {
    transform: rotate(0deg);
  }
  55%,
  100% {
    transform: rotate(90deg);
  }
}

/* Mobile: force landscape only while in-game (login stays usable in any orientation). */
body.mobile.playing.portrait #rotate-gate {
  display: grid;
}

body.mobile.playing.portrait #hud,
body.mobile.playing.portrait canvas {
  visibility: hidden !important;
  pointer-events: none !important;
}

/* Compact login card for phones (browser chrome eats height). */
body.mobile #overlay {
  align-items: start;
  justify-items: center;
  padding:
    max(8px, env(safe-area-inset-top))
    max(10px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
}

body.mobile .card {
  width: min(400px, calc(100vw - 20px));
  max-height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  max-height: calc(100svh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  margin: auto 0;
  padding: 14px 14px 12px;
  border-radius: 14px;
  font-size: 13px;
}

body.mobile .card h1 {
  font-size: 1.45rem;
  margin-bottom: 4px;
}

body.mobile .card-lead {
  display: none;
}

body.mobile .login-label {
  font-size: 0.75rem;
  margin-bottom: 4px;
}

body.mobile #login-name {
  margin-bottom: 8px;
  padding: 10px 12px;
  font-size: 0.95rem;
  border-radius: 10px;
}

body.mobile .card p.status {
  margin-bottom: 8px;
  font-size: 0.78rem;
}

body.mobile .card button {
  padding: 12px 14px;
  font-size: 0.95rem;
  border-radius: 10px;
  /* Keep CTA visible while scrolling hints */
  position: sticky;
  bottom: 0;
  z-index: 1;
}

body.mobile .card ul {
  margin-top: 10px;
  gap: 4px;
  font-size: 0.72rem;
  line-height: 1.3;
}

body.mobile .card kbd {
  font-size: 0.7em;
  padding: 0 4px;
}

body.mobile .install-hint {
  color: var(--accent);
  opacity: 0.9;
}

/* Installed PWA / fullscreen: no need for install tip */
@media (display-mode: standalone), (display-mode: fullscreen) {
  .install-hint {
    display: none;
  }
}

@media (max-width: 640px) {
  #hint {
    font-size: 11px;
    top: 10px;
  }

  #player-label {
    top: auto;
    bottom: 78px;
    left: 12px;
  }

  #chat {
    bottom: 120px;
  }

  .slot {
    width: 42px;
    height: 42px;
  }
}

/* Very short landscape (phone + browser bar): hide help list entirely */
@media (max-height: 420px) {
  body.mobile .card ul {
    display: none;
  }

  body.mobile .card {
    padding: 10px 12px;
  }

  body.mobile .card h1 {
    font-size: 1.25rem;
  }
}
