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

html, body {
  height: 100vh;
  height: 100dvh;
}

body {
  overflow: hidden;
  background: #000;
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: #fff;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

#game {
  display: block;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.hidden { display: none !important; }

/* --- Overlays --- */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  z-index: 100;
}

/* --- Join Screen --- */
.join-card {
  background: linear-gradient(135deg, #1a1a2e, #16213e);
  border: 2px solid #0f3460;
  border-radius: 16px;
  padding: 48px;
  text-align: center;
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  touch-action: pan-y;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.join-card h1 {
  font-size: 48px;
  font-weight: 800;
  background: linear-gradient(90deg, #4ade80, #22d3ee);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 8px;
  letter-spacing: -1px;
}
.subtitle {
  color: #888;
  margin-bottom: 32px;
  font-size: 14px;
}
.join-card input {
  display: block;
  width: 100%;
  padding: 14px 18px;
  border: 2px solid #333;
  border-radius: 10px;
  background: #0a0a1a;
  color: #fff;
  font-size: 16px;
  margin-bottom: 16px;
  outline: none;
  transition: border-color 0.2s;
}
.join-card input:focus { border-color: #4ade80; }
.join-card button {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 10px;
  background: linear-gradient(90deg, #4ade80, #22d3ee);
  color: #000;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s;
}
.join-card button:hover { opacity: 0.9; transform: scale(1.02); }
.controls-hint {
  margin-top: 24px;
  font-size: 12px;
  color: #666;
  line-height: 2;
}
.controls-hint span {
  background: #222;
  padding: 2px 8px;
  border-radius: 4px;
  font-family: monospace;
  color: #aaa;
}

/* --- HUD --- */
#crosshair {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  color: rgba(255,255,255,0.7);
  pointer-events: none;
  z-index: 10;
  text-shadow: 0 0 3px rgba(0,0,0,0.8);
  font-weight: 100;
}
#coords {
  position: fixed;
  top: 12px;
  left: 12px;
  font-family: monospace;
  font-size: 13px;
  color: rgba(255,255,255,0.6);
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  z-index: 10;
}
#player-count {
  position: fixed;
  top: 12px;
  right: 12px;
  font-size: 13px;
  color: rgba(255,255,255,0.6);
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  z-index: 10;
}

/* --- Hotbar --- */
#hotbar {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
  background: rgba(0,0,0,0.5);
  padding: 6px;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.1);
  z-index: 10;
}
.hotbar-slot {
  width: 48px;
  height: 48px;
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.15s;
}
.hotbar-slot.selected { border-color: #fff; background: rgba(255,255,255,0.1); }
.hotbar-slot:hover { border-color: rgba(255,255,255,0.5); }
.block-preview {
  width: 32px;
  height: 32px;
  border-radius: 4px;
}
.block-preview.grass { background: linear-gradient(to bottom, #4d9e0f 40%, #85603b 40%); }
.block-preview.stone { background: #808080; }
.block-preview.planks { background: #c29b62; }
.block-preview.brick { background: #a04030; }
.block-preview.sand { background: #dbd3a0; }
.block-preview.leaves { background: #2b7312; }
.block-preview.cobble { background: #6a6a6a; }
.block-preview.glass { background: rgba(170,210,240,0.5); border: 1px solid rgba(170,210,240,0.7); }
.block-preview.log { background: linear-gradient(to bottom, #734a26 0%, #5e3a1a 100%); }

/* --- Task Panel --- */
#task-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 360px;
  height: 100vh;
  height: 100dvh;
  background: rgba(10, 10, 30, 0.92);
  border-left: 2px solid #1a1a4a;
  z-index: 50;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(10px);
  overflow-y: auto;
  touch-action: pan-y;
}
.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #222;
}
.panel-header h2, .panel-header h3 { font-size: 18px; }
.panel-header button {
  background: none;
  border: none;
  color: #888;
  font-size: 24px;
  cursor: pointer;
}
.panel-header button:hover { color: #fff; }

#task-create-form {
  padding: 16px 20px;
  border-bottom: 1px solid #222;
}
#task-create-form input, #task-create-form textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #333;
  border-radius: 8px;
  background: #0a0a1a;
  color: #fff;
  font-size: 14px;
  margin-bottom: 10px;
  outline: none;
  font-family: inherit;
  resize: none;
}
#task-create-form input:focus, #task-create-form textarea:focus { border-color: #4ade80; }

.task-size-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.task-size-row label { color: #888; font-size: 14px; }
.size-btn {
  padding: 6px 14px;
  border: 1px solid #333;
  border-radius: 6px;
  background: #111;
  color: #ccc;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: all 0.15s;
}
.size-btn.selected { border-color: #4ade80; color: #4ade80; background: rgba(74,222,128,0.1); }
.size-btn:hover { border-color: #555; }

#create-task-btn {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: #4ade80;
  color: #000;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
#create-task-btn:hover { background: #3dca73; }

#task-list { padding: 8px 12px; flex: 1; overflow-y: auto; }

#jira-section {
  padding: 0 20px 12px;
  border-bottom: 1px solid #222;
}
#jira-toggle {
  width: 100%;
  padding: 8px;
  border: 1px solid #333;
  border-radius: 6px;
  background: #111;
  color: #aaa;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s;
}
#jira-toggle:hover { border-color: #555; color: #fff; }
#jira-form {
  margin-top: 10px;
}
#jira-form input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #333;
  border-radius: 6px;
  background: #0a0a1a;
  color: #fff;
  font-size: 13px;
  margin-bottom: 8px;
  outline: none;
  font-family: inherit;
}
#jira-form input:focus { border-color: #4ade80; }
#jira-import-btn {
  width: 100%;
  padding: 8px;
  border: none;
  border-radius: 6px;
  background: #2563eb;
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
#jira-import-btn:hover { background: #1d4ed8; }
#jira-import-btn:disabled { opacity: 0.5; cursor: default; }
#jira-status {
  font-size: 12px;
  color: #888;
  margin-top: 8px;
}

#task-panel-footer {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid #222;
}
#export-btn, #import-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid #333;
  border-radius: 6px;
  background: #111;
  color: #aaa;
  font-size: 13px;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: border-color 0.15s;
}
#export-btn:hover, #import-btn:hover { border-color: #555; color: #fff; }

.task-item {
  padding: 12px 16px;
  margin-bottom: 6px;
  border-radius: 8px;
  cursor: pointer;
  border-left: 4px solid;
  transition: background 0.15s;
}
.task-item:hover { background: rgba(255,255,255,0.05); }
.task-item.todo { border-left-color: #ef4444; background: rgba(239,68,68,0.05); }
.task-item.wip { border-left-color: #f59e0b; background: rgba(245,158,11,0.05); }
.task-item.done { border-left-color: #22c55e; background: rgba(34,197,94,0.05); }
.task-item.blocked { border-left-color: #a855f7; background: rgba(168,85,247,0.05); }
.task-item-name { font-weight: 600; font-size: 14px; }
.task-item-meta { font-size: 12px; color: #888; margin-top: 4px; }

/* --- Task Detail --- */
#task-detail {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(10, 10, 30, 0.95);
  border: 2px solid #333;
  border-radius: 16px;
  padding: 0 0 20px;
  width: 380px;
  z-index: 60;
  backdrop-filter: blur(10px);
}
#task-detail .panel-header { padding: 16px 20px; }
#detail-desc { padding: 0 20px; color: #aaa; font-size: 14px; margin-bottom: 12px; }
#detail-meta { padding: 0 20px; color: #666; font-size: 12px; margin-bottom: 16px; }
.status-buttons { display: flex; gap: 6px; padding: 0 20px; margin-bottom: 16px; }
.status-btn {
  flex: 1;
  padding: 8px 4px;
  border: 1px solid #333;
  border-radius: 6px;
  background: #111;
  color: #ccc;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s;
}
.status-btn:hover { border-color: #555; }
.status-btn.active { font-weight: 700; }
.status-btn[data-status="todo"].active { border-color: #ef4444; color: #ef4444; }
.status-btn[data-status="wip"].active { border-color: #f59e0b; color: #f59e0b; }
.status-btn[data-status="done"].active { border-color: #22c55e; color: #22c55e; }
.status-btn[data-status="blocked"].active { border-color: #a855f7; color: #a855f7; }

#delete-task-btn {
  margin: 0 20px;
  padding: 8px;
  width: calc(100% - 40px);
  border: 1px solid #442222;
  border-radius: 6px;
  background: rgba(200,50,50,0.1);
  color: #ef4444;
  cursor: pointer;
  font-size: 13px;
}
#delete-task-btn:hover { background: rgba(200,50,50,0.2); }

/* --- Chat --- */
#chat {
  position: fixed;
  bottom: 80px;
  left: 12px;
  width: 340px;
  z-index: 20;
}
#chat-messages {
  max-height: 200px;
  overflow-y: auto;
  margin-bottom: 8px;
  scrollbar-width: thin;
  scrollbar-color: #333 transparent;
}
#chat-messages::-webkit-scrollbar { width: 4px; }
#chat-messages::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }
.chat-msg {
  font-size: 13px;
  padding: 2px 0;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  word-break: break-word;
}
.chat-msg .chat-name { font-weight: 700; }
.chat-msg.system { color: #f59e0b; font-style: italic; }
#chat-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #333;
  border-radius: 8px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  font-size: 13px;
  outline: none;
  display: none;
}
#chat-input.active { display: block; }

/* --- Demo mode --- */
body.demo-mode #hud,
body.demo-mode #chat,
body.demo-mode #task-panel,
body.demo-mode #task-detail,
body.demo-mode #join-screen { display: none !important; }

#demo-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: #fff;
}
#demo-overlay > div { transition: opacity 0.4s ease; opacity: 0; }
body.demo-mode #demo-prestart {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  padding: 14px 28px;
  background: rgba(0,0,0,0.6);
  border-radius: 12px;
  font-size: 22px;
  letter-spacing: 0.5px;
  backdrop-filter: blur(8px);
}
#demo-prestart kbd {
  background: #fff;
  color: #000;
  padding: 2px 10px;
  border-radius: 6px;
  font-family: monospace;
  font-weight: 700;
  margin: 0 4px;
}
body.demo-mode #demo-title-wrap {
  position: absolute;
  bottom: 100px;
  left: 80px;
  text-shadow: 0 4px 24px rgba(0,0,0,0.9), 0 1px 3px rgba(0,0,0,1);
  max-width: 60%;
}
#demo-main {
  font-size: 56px;
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -1px;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #4ade80, #22d3ee);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
#demo-sub {
  font-size: 22px;
  font-weight: 400;
  color: rgba(255,255,255,0.92);
  letter-spacing: 0.2px;
}
body.demo-mode #demo-end {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  font-weight: 700;
  font-family: monospace;
  background: rgba(0,0,0,0.55);
  text-shadow: 0 4px 24px rgba(0,0,0,0.9);
}

/* --- Explore mode (static demo) --- */
#explore-hint {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  padding: 10px 14px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
  border-radius: 8px;
  max-width: 340px;
  z-index: 5;
  backdrop-filter: blur(4px);
}
#explore-hint .hint-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 4px;
}
#explore-hint-toggle {
  background: none;
  border: none;
  color: #fff;
  font-size: 18px;
  line-height: 1;
  padding: 0 0 0 8px;
  cursor: pointer;
  opacity: 0.6;
}
#explore-hint.collapsed .hint-body { display: none; }
#explore-hint.collapsed { padding: 6px 10px; max-width: 200px; }

#explore-menu {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  padding: 10px;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 13px;
  border-radius: 8px;
  z-index: 6;
  width: 260px;
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - 24px);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
}
#explore-menu .menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
#explore-menu .menu-title { font-weight: 700; }
#explore-menu .menu-hint { font-size: 11px; opacity: 0.7; }
#explore-menu-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 22px;
  line-height: 1;
  padding: 0 0 0 8px;
  cursor: pointer;
  opacity: 0.7;
  display: none;
}
#explore-menu-list {
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
#explore-menu button.build-btn {
  display: block;
  width: 100%;
  margin: 2px 0;
  padding: 8px 10px;
  background: rgba(255,255,255,0.08);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 4px;
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}
#explore-menu button.build-btn[data-active="1"] {
  background: rgba(125,211,252,0.35);
  border-color: #7dd3fc;
}

#explore-status {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 14px;
  padding: 8px 14px;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 5;
  max-width: calc(100vw - 32px);
  text-align: center;
}

#explore-crosshair {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 20px;
  opacity: 0.5;
  pointer-events: none;
  z-index: 4;
  text-shadow: 0 0 2px rgba(0,0,0,0.5);
}

#explore-chip {
  position: fixed;
  left: 50%;
  top: calc(50% + 22px);
  transform: translateX(-50%);
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 12px;
  pointer-events: none;
  z-index: 4;
  white-space: nowrap;
  max-width: calc(100vw - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- Touch controls (mobile) --- */
#touch-controls { display: none; }
body.touch #touch-controls { display: block; }

#touch-joystick {
  position: fixed;
  left: max(20px, env(safe-area-inset-left));
  bottom: max(24px, env(safe-area-inset-bottom));
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,255,255,0.18);
  z-index: 7;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#touch-joystick .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  margin-left: -28px;
  margin-top: -28px;
  border-radius: 50%;
  background: rgba(255,255,255,0.4);
  border: 2px solid rgba(255,255,255,0.6);
  pointer-events: none;
  transition: background 0.1s;
}
#touch-joystick.active .knob { background: rgba(125,211,252,0.6); }

.touch-btn {
  position: fixed;
  right: max(20px, env(safe-area-inset-right));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: rgba(0,0,0,0.45);
  border: 2px solid rgba(255,255,255,0.25);
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  z-index: 7;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.touch-btn:active, .touch-btn.active {
  background: rgba(125,211,252,0.4);
  border-color: #7dd3fc;
}
#touch-up   { bottom: calc(max(24px, env(safe-area-inset-bottom)) + 150px); }
#touch-down { bottom: calc(max(24px, env(safe-area-inset-bottom)) + 80px); }
#touch-build {
  bottom: max(24px, env(safe-area-inset-bottom));
  background: rgba(74,222,128,0.5);
  border-color: #4ade80;
  color: #000;
  font-size: 14px;
  font-weight: 700;
}
#touch-menu {
  right: calc(max(20px, env(safe-area-inset-right)) + 70px);
  bottom: max(24px, env(safe-area-inset-bottom));
  font-size: 14px;
  font-weight: 700;
}

/* On touch devices, hide pointer-lock-specific UI bits */
body.touch #explore-hint { display: none; }
body.touch #explore-crosshair { display: none; }
body.touch #explore-chip { display: none; }

/* Small screens: compact join card, narrow side panels */
@media (max-width: 600px) {
  .join-card {
    padding: 28px 20px;
    border-radius: 12px;
  }
  .join-card h1 { font-size: 36px; }
  .controls-hint { font-size: 11px; line-height: 1.8; }

  #task-panel { width: 100vw; border-left: none; }
  #task-detail {
    width: calc(100vw - 24px);
    max-width: 380px;
  }
  #chat { width: calc(100vw - 24px); left: 12px; bottom: 90px; }
  #hotbar { gap: 2px; padding: 4px; }
  .hotbar-slot { width: 38px; height: 38px; }
  .block-preview { width: 26px; height: 26px; }

  #explore-menu {
    width: calc(100vw - 24px);
    max-width: 320px;
    transform: translateY(calc(-100% + 44px));
    transition: transform 0.2s ease;
  }
  #explore-menu.open { transform: translateY(0); }
  #explore-menu-close { display: block; }
  body.touch #explore-menu:not(.open) #explore-menu-list { display: none; }
  body.touch #explore-menu:not(.open) .menu-hint { display: none; }
  body.touch #explore-menu:not(.open) { padding: 10px 14px; cursor: pointer; }
}
