/* =====================================================================
   EXPLICIT BASE & HUD LAYOUT POSITIONING (GUARANTEED NON-OVERLAPPING)
===================================================================== */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  background: #0f172a;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  user-select: none;
}

#canvas-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 1;
  touch-action: none;
}

#canvas-container canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#labels-layer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 5;
  pointer-events: none;
  overflow: hidden;
}

#hud-top-left {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  left: calc(14px + env(safe-area-inset-left, 0px));
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: 255px;
}

#hud-top-right {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  right: calc(14px + env(safe-area-inset-right, 0px));
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
}

#hud-bottom-left {
  position: fixed;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  left: calc(14px + env(safe-area-inset-left, 0px));
  z-index: 20;
  width: 270px;
  max-width: 34vw;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#hud-bottom-center {
  position: fixed;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

/* Prevent bottom Action Dock from overlapping Chat Box on narrower tablet viewports */
@media (min-width: 768px) and (max-width: 1023px) and (min-height: 521px) {
  #hud-bottom-center {
    right: calc(14px + env(safe-area-inset-right, 0px));
    left: auto;
    transform: none;
    align-items: flex-end;
    max-width: 62vw;
  }
}

#hud-center-alerts {
  position: fixed;
  top: 78px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: min(92vw, 440px);
}

.hud-interactive {
  pointer-events: auto;
}

/* Explicit horizontal toolbar rows */
.hud-toolbar-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  pointer-events: auto;
}

.hud-dock-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 5px;
  flex-wrap: nowrap;
  overflow-x: auto;
  max-width: 100%;
  pointer-events: auto;
  touch-action: pan-x;
  -webkit-overflow-scrolling: touch;
}

/* High-Contrast GPU-Composited HUD Surfaces (zero per-frame WebGL backdrop-blur readback!) */
.hud-glass {
  background: rgba(15, 23, 42, 0.90);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 6px 20px rgba(2, 6, 23, 0.42);
  contain: layout style;
}

.hud-btn {
  cursor: pointer;
  outline: none;
  touch-action: manipulation;
  transition: transform 0.12s ease, background-color 0.15s ease;
  pointer-events: auto;
}
.hud-btn:focus,
.hud-btn:focus-visible {
  outline: none;
}
.hud-btn:hover {
  filter: brightness(1.08);
}
.hud-btn:active {
  transform: scale(0.93);
}

/* 3D Floating Labels */
.char-label-wrap {
  position: absolute;
  top: 0;
  left: 0;
  will-change: transform;
  contain: layout style;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}

.char-name-pill {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
  color: #ffffff;
  background: rgba(15, 23, 42, 0.84);
  border: 1.5px solid rgba(255, 255, 255, 0.25);
  white-space: nowrap;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.35);
}
.char-name-pill.is-boss {
  background: linear-gradient(135deg, #dc2626, #991b1b);
  border-color: #fca5a5;
}

.char-bubble {
  margin-bottom: 4px;
  max-width: 175px;
  padding: 4px 9px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.96);
  color: #0f172a;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(15, 23, 42, 0.12);
  animation: popIn 0.2s ease-out;
}

.char-emote {
  margin-bottom: 3px;
  font-size: 24px;
  text-shadow: 0 3px 6px rgba(0, 0, 0, 0.55);
  will-change: transform;
  animation: floatEmote 1s ease-in-out infinite;
}

@keyframes popIn {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes floatEmote {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

/* Floating Money Popup */
.float-money {
  position: fixed;
  pointer-events: none;
  z-index: 45;
  font-weight: 900;
  font-size: 14px;
  color: #4ade80;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
  animation: moneyUp 0.9s ease-out forwards;
}
@keyframes moneyUp {
  0% { transform: translate(-50%, 0) scale(0.85); opacity: 1; }
  100% { transform: translate(-50%, -44px) scale(1.1); opacity: 0; }
}

/* Virtual Joystick & Right-Thumb Action Pad */
#joystick-zone {
  position: fixed;
  left: calc(16px + env(safe-area-inset-left, 0px));
  bottom: calc(200px + env(safe-area-inset-bottom, 0px));
  width: 112px;
  height: 112px;
  z-index: 26;
  touch-action: none;
  pointer-events: auto;
}
#joystick-base {
  width: 100%;
  height: 100%;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.52);
  border: 2px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
}
#joystick-knob {
  width: 44px;
  height: 44px;
  border-radius: 999px;
  background: radial-gradient(circle at 35% 35%, #ffffff, #cbd5e1);
  border: 2px solid #38bdf8;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  transform: translate3d(0, 0, 0);
}
#joystick-knob.sprinting {
  background: radial-gradient(circle at 35% 35%, #fef08a, #f59e0b);
  border-color: #ef4444;
}

#mobile-thumb-pad {
  position: fixed;
  right: calc(14px + env(safe-area-inset-right, 0px));
  bottom: calc(84px + env(safe-area-inset-bottom, 0px));
  z-index: 26;
  touch-action: manipulation;
  pointer-events: auto;
}

#btn-mobile-chat-toggle {
  position: fixed;
  left: calc(12px + env(safe-area-inset-left, 0px));
  bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  z-index: 24;
}
body.joy-visible #btn-mobile-chat-toggle {
  bottom: calc(182px + env(safe-area-inset-bottom, 0px));
}

/* Hide Scrollbars */
.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }

/* Fake Excel Table */
.excel-cell {
  border: 1px solid #d4d4d4;
  padding: 3px 6px;
  font-size: 12px;
  white-space: nowrap;
  height: 24px;
}
.excel-header {
  background: #f3f4f6;
  font-weight: 600;
  color: #374151;
  text-align: center;
  border: 1px solid #cbd5e1;
  font-size: 11px;
}

/* 0.4s Instant Elevator Curtain Transition (0.2s in + 0.2s out) */
#elevator-fade-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: radial-gradient(circle at center, rgba(15, 23, 42, 0.94), #020617);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease-in-out;
}
#elevator-fade-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

/* Fullscreen Frosted-Glass Start Screen (3D Office visible behind) */
#start-screen {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: radial-gradient(circle at center, rgba(15, 23, 42, 0.48), rgba(2, 6, 23, 0.78));
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: calc(12px + env(safe-area-inset-top, 0px)) calc(12px + env(safe-area-inset-right, 0px)) calc(12px + env(safe-area-inset-bottom, 0px)) calc(12px + env(safe-area-inset-left, 0px));
  transition: opacity 0.32s ease, transform 0.32s ease;
}
#start-screen > div {
  margin: auto;
}
#start-screen.closing {
  opacity: 0;
  transform: scale(1.04);
  pointer-events: none;
}
#start-screen.hidden {
  display: none;
}

/* =====================================================================
   COMMERCE & ASSET SHOPPING CENTER UX/UI (HIGH-CONTRAST STOREFRONT)
===================================================================== */
.shop-modal-panel {
  animation: shopModalIn 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: 0 24px 64px -12px rgba(2, 6, 23, 0.65), 0 0 0 1px rgba(255, 255, 255, 0.14);
}
@supports (height: 100dvh) {
  .shop-modal-panel {
    max-height: 92dvh;
  }
}

@keyframes shopModalIn {
  0% { opacity: 0; transform: translateY(10px) scale(0.97); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

.shop-scroll {
  scrollbar-width: thin;
  scrollbar-color: #94a3b8 transparent;
  -webkit-overflow-scrolling: touch;
}
.shop-scroll::-webkit-scrollbar {
  width: 6px;
}
.shop-scroll::-webkit-scrollbar-track {
  background: transparent;
}
.shop-scroll::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 999px;
}
.shop-scroll::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.shop-card {
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
}
.shop-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 24px -8px rgba(15, 23, 42, 0.14);
}

.shop-progress-track {
  height: 6px;
  border-radius: 999px;
  background: #e2e8f0;
  overflow: hidden;
}
.shop-progress-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

.shop-toggle-pill {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  padding: 2px;
  transition: background-color 0.18s ease;
}
.shop-toggle-knob {
  width: 20px;
  height: 20px;
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.28);
  transition: transform 0.18s cubic-bezier(0.22, 1, 0.36, 1);
}
.shop-toggle-pill.is-on {
  background: #0d9488;
}
.shop-toggle-pill.is-on .shop-toggle-knob {
  transform: translateX(18px);
}
.shop-toggle-pill.is-off {
  background: #94a3b8;
}

/* =====================================================================
   MOBILE PORTRAIT, LANDSCAPE, TWO-THUMB HUD & IOS ANTI-ZOOM OPTIMIZATION
===================================================================== */
/* Desktop (mouse/trackpad primary pointer): ALWAYS hide Virtual Joystick & Mobile Thumb Pad */
@media (hover: hover) and (pointer: fine) {
  #joystick-zone,
  #mobile-thumb-pad,
  #btn-joystick,
  #start-btn-joystick {
    display: none !important;
  }
}

/* Prevent iOS Safari forced viewport auto-zoom when focusing inputs < 16px */
@media (max-width: 768px), (pointer: coarse) {
  input, select, textarea {
    font-size: 16px !important;
  }
}

/* Mobile Portrait & Narrow Touch Screens (< 768px) */
@media (max-width: 767px) {
  #hud-top-left {
    top: calc(8px + env(safe-area-inset-top, 0px));
    left: calc(8px + env(safe-area-inset-left, 0px));
    gap: 5px;
    max-width: calc(44vw - 10px);
  }
  #hud-top-left:has(#hud-office-card:not(.mobile-collapsed)) {
    max-width: min(240px, 66vw);
    z-index: 34;
  }
  #hud-office-card.mobile-collapsed .hud-extra-row {
    display: none !important;
  }

  #hud-top-right {
    top: calc(8px + env(safe-area-inset-top, 0px));
    right: calc(8px + env(safe-area-inset-right, 0px));
    gap: 4px;
    max-width: calc(56vw - 10px);
  }
  #hud-top-right > .hud-toolbar-row:first-child {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 4px;
  }
  #hud-top-right > .hud-toolbar-row:first-child > button {
    height: 32px;
    padding-left: 8px;
    padding-right: 8px;
  }
  #hud-top-right > .hud-toolbar-row:nth-child(2) {
    gap: 5px;
    padding: 3px 8px;
  }
  #hud-top-right > .hud-toolbar-row:nth-child(3) {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 4px;
  }

  #hud-center-alerts {
    top: calc(116px + env(safe-area-inset-top, 0px));
    width: min(94vw, 420px);
    gap: 6px;
  }

  /* Full-width touch-scrollable bottom Action Dock */
  #hud-bottom-center {
    left: calc(8px + env(safe-area-inset-left, 0px));
    right: calc(8px + env(safe-area-inset-right, 0px));
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    max-width: none;
    transform: none;
    align-items: center;
  }
  #dock-social-drawer {
    max-width: 100%;
    overflow-x: auto;
    touch-action: pan-x;
    -webkit-overflow-scrolling: touch;
  }

  /* Left-Thumb Virtual Joystick cleanly above Action Dock */
  #joystick-zone {
    left: calc(10px + env(safe-area-inset-left, 0px));
    bottom: calc(74px + env(safe-area-inset-bottom, 0px));
    width: 96px;
    height: 96px;
  }
  #joystick-knob {
    width: 38px;
    height: 38px;
  }

  /* Right-Thumb Contextual Action Pad cleanly above Action Dock */
  #mobile-thumb-pad {
    right: calc(10px + env(safe-area-inset-right, 0px));
    bottom: calc(78px + env(safe-area-inset-bottom, 0px));
  }

  /* Collapsible Floating Chat Drawer on Mobile */
  #btn-mobile-chat-toggle {
    left: calc(10px + env(safe-area-inset-left, 0px));
    bottom: calc(74px + env(safe-area-inset-bottom, 0px));
  }
  body.joy-visible #btn-mobile-chat-toggle {
    bottom: calc(178px + env(safe-area-inset-bottom, 0px));
  }
  #hud-bottom-left.mobile-chat-closed {
    display: none !important;
  }
  #hud-bottom-left:not(.mobile-chat-closed) {
    left: calc(10px + env(safe-area-inset-left, 0px));
    right: calc(10px + env(safe-area-inset-right, 0px));
    bottom: calc(74px + env(safe-area-inset-bottom, 0px));
    width: auto;
    max-width: none;
    z-index: 33;
    background: rgba(15, 23, 42, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 18px;
    padding: 8px;
    box-shadow: 0 14px 36px rgba(2, 6, 23, 0.65);
  }
}

/* Mobile Landscape Phones (tight vertical height <= 520px, even when width >= 768px) */
@media (max-height: 520px) and (orientation: landscape) {
  #hud-top-left {
    top: calc(6px + env(safe-area-inset-top, 0px));
    left: calc(10px + env(safe-area-inset-left, 0px));
    gap: 4px;
    max-width: 220px;
  }
  #hud-info-chevron {
    display: inline !important;
  }
  #hud-office-card.mobile-collapsed .hud-extra-row {
    display: none !important;
  }

  #hud-top-right {
    top: calc(6px + env(safe-area-inset-top, 0px));
    right: calc(10px + env(safe-area-inset-right, 0px));
    gap: 4px;
  }

  #hud-center-alerts {
    top: calc(46px + env(safe-area-inset-top, 0px));
    width: min(68vw, 400px);
    gap: 5px;
  }

  /* Center Action Dock between Left Joystick & Right Thumb Pad in Landscape */
  #hud-bottom-center {
    left: 50%;
    right: auto;
    bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%);
    max-width: calc(100vw - 220px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
    align-items: center;
  }

  #joystick-zone {
    left: calc(12px + env(safe-area-inset-left, 0px));
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    width: 88px;
    height: 88px;
  }
  #joystick-knob {
    width: 34px;
    height: 34px;
  }

  #mobile-thumb-pad {
    right: calc(12px + env(safe-area-inset-right, 0px));
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }

  #btn-mobile-chat-toggle {
    display: flex !important;
    left: calc(12px + env(safe-area-inset-left, 0px));
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }
  body.joy-visible #btn-mobile-chat-toggle {
    bottom: calc(106px + env(safe-area-inset-bottom, 0px));
  }
  #btn-mobile-chat-close {
    display: flex !important;
  }
  #hud-bottom-left.mobile-chat-closed {
    display: none !important;
  }
  #hud-bottom-left:not(.mobile-chat-closed) {
    left: calc(12px + env(safe-area-inset-left, 0px));
    right: auto;
    bottom: calc(64px + env(safe-area-inset-bottom, 0px));
    width: min(340px, 62vw);
    max-width: none;
    z-index: 33;
    background: rgba(15, 23, 42, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 18px;
    padding: 8px;
    box-shadow: 0 14px 36px rgba(2, 6, 23, 0.65);
  }

  /* Extra breathing room inside modals on short landscape phones */
  .shop-modal-panel {
    max-height: 94vh;
    max-height: 94dvh;
  }
  .shop-modal-panel > div:first-child {
    padding-top: 8px;
    padding-bottom: 6px;
  }
}
