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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Courier New', Courier, monospace;
  background: #1a1a2e;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ── Top bar ── */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: #1a1a2e;
  color: #fff;
  z-index: 1000;
  flex-shrink: 0;
  gap: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

#topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

#logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 3px;
  color: #e94560;
  white-space: nowrap;
  user-select: none;
}

.logo-icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.logo-icon-lg {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  margin-bottom: 4px;
}

/* ── Color palette ── */
#palette {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  justify-content: flex-end;
  max-width: calc(100vw - 120px);
}

.swatch {
  width: 26px;
  height: 26px;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.1s ease, border-color 0.1s ease, box-shadow 0.1s ease;
  flex-shrink: 0;
}

.swatch:hover {
  transform: scale(1.25);
  z-index: 1;
}

.swatch.selected {
  box-shadow: inset 0 0 0 2px #fff, inset 0 0 0 4px #000;
  transform: scale(1.1);
}

.swatch[data-color="#000000"]:not(.selected) {
  box-shadow: 0 0 0 1px #444;
}

/* ── Tool swatches (undo, erase) ── */
.swatch-tool {
  background: #2a2a40;
  border: 1px solid rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
}

.swatch-tool:hover {
  border-color: rgba(255,255,255,0.4);
  transform: none;
}

.swatch-tool.selected {
  border-color: #fff;
  box-shadow: inset 0 0 0 2px #fff, inset 0 0 0 4px #000;
  transform: none;
}

.swatch-undo {
  user-select: none;
}

.swatch-undo svg {
  width: 75%;
  height: 75%;
}

.swatch-erase {
  background-image:
    linear-gradient(45deg, #555 25%, transparent 25%),
    linear-gradient(-45deg, #555 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #555 75%),
    linear-gradient(-45deg, transparent 75%, #555 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0px;
}

/* ── Map ── */
#map {
  flex: 1;
  cursor: crosshair;
}

#map.no-paint {
  cursor: grab;
}

/* ── Pixel-art position pin ── */
.pixel-pin-wrapper {
  background: none !important;
  border: none !important;
}

.pixel-pin {
  position: relative;
  width: 20px;
  height: 20px;
}

.pixel-pin-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  background: #e94560;
  transform: translate(-50%, -50%);
  image-rendering: pixelated;
}

.pixel-pin-dot::before,
.pixel-pin-dot::after {
  content: '';
  position: absolute;
  background: #e94560;
}

.pixel-pin-dot::before {
  width: 4px;
  height: 4px;
  top: -4px;
  left: 2px;
}

.pixel-pin-dot::after {
  width: 4px;
  height: 4px;
  bottom: -4px;
  left: 2px;
}

.pixel-pin-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 18px;
  border: 2px solid #e94560;
  transform: translate(-50%, -50%);
  opacity: 0;
  animation: ping 2s cubic-bezier(0, 0, 0.2, 1) infinite;
}

@keyframes ping {
  0% {
    transform: translate(-50%, -50%) scale(0.8);
    opacity: 0.6;
  }
  75%, 100% {
    transform: translate(-50%, -50%) scale(2);
    opacity: 0;
  }
}

/* ── Locate button ── */
#locate-btn {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  bottom: 120px;
  z-index: 1001;
  width: 36px;
  height: 36px;
  background: #1a1a2e;
  border: 2px solid rgba(255,255,255,0.2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s ease, background 0.2s ease;
}

#locate-btn:hover {
  border-color: #e94560;
  background: #22223a;
}

#locate-btn.locating {
  pointer-events: none;
  opacity: 0.5;
}

.locate-icon {
  width: 16px;
  height: 16px;
  position: relative;
}

.locate-icon::before,
.locate-icon::after {
  content: '';
  position: absolute;
  background: #fff;
}

.locate-icon::before {
  width: 2px;
  height: 100%;
  left: 50%;
  transform: translateX(-50%);
}

.locate-icon::after {
  width: 100%;
  height: 2px;
  top: 50%;
  transform: translateY(-50%);
}

.locate-icon-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 4px;
  height: 4px;
  background: #e94560;
  transform: translate(-50%, -50%);
}

/* ── Loading overlay ── */
#loading {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #1a1a2e;
  z-index: 3000;
  transition: opacity 0.4s ease;
}

#loading.hidden {
  opacity: 0;
  pointer-events: none;
}

/* ── Welcome screen ── */
#welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  max-width: 20rem;
  padding: 0 1.5rem;
}

#welcome.hidden {
  display: none;
}

#welcome-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.4rem;
  color: #e94560;
  margin-bottom: 1.5rem;
}

#welcome-text {
  font-size: 0.875rem;
  line-height: 1.6;
  letter-spacing: 0.3px;
  margin-bottom: 1.25rem;
}

#welcome-text p {
  margin: 0 0 0.75rem 0;
  color: rgba(255,255,255,0.65);
}

#welcome-text p:last-child {
  margin-bottom: 0;
}

#welcome-text .location-cta {
  color: rgba(233, 69, 96, 0.85);
}

#welcome-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 100%;
}

#btn-enable-geo {
  background: #e94560;
  color: #fff;
  border: none;
  padding: 0.75rem 1.5rem;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: background 0.2s ease;
}

#btn-enable-geo:hover {
  background: #d63851;
}

#btn-skip-geo {
  background: transparent;
  color: rgba(255,255,255,0.45);
  border: 1px solid rgba(255,255,255,0.15);
  padding: 0.75rem 1.5rem;
  font-family: inherit;
  font-size: 0.8rem;
  letter-spacing: 0.3px;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease;
}

#btn-skip-geo:hover {
  border-color: rgba(255,255,255,0.35);
  color: rgba(255,255,255,0.7);
}

#privacy-link {
  display: inline-block;
  margin-top: 1rem;
  font-size: 0.75rem;
  color: rgba(255,255,255,0.3);
  text-decoration: none;
  letter-spacing: 0.3px;
}

#privacy-link:hover {
  color: rgba(255,255,255,0.6);
}

/* ── Space buttons (welcome screen) ── */
#welcome-divider {
  width: 100%;
  text-align: center;
  margin: 1.25rem 0 0.75rem;
  position: relative;
}
#welcome-divider::before,
#welcome-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: calc(50% - 5rem);
  height: 1px;
  background: rgba(255,255,255,0.1);
}
#welcome-divider::before { left: 0; }
#welcome-divider::after { right: 0; }
#welcome-divider span {
  font-size: 0.7rem;
  color: rgba(255,255,255,0.3);
  letter-spacing: 0.5px;
}

#welcome-space-buttons {
  display: flex;
  gap: 0.5rem;
  width: 100%;
}

#welcome-space-buttons.hidden {
  display: none;
}

#btn-create-space,
#btn-join-space {
  flex: 1;
  background: transparent;
  color: rgba(255,255,255,0.5);
  border: 1px solid rgba(255,255,255,0.12);
  padding: 0.6rem 1rem;
  font-family: inherit;
  font-size: 0.8rem;
  letter-spacing: 0.3px;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease;
}
#btn-create-space:hover,
#btn-join-space:hover {
  border-color: rgba(255,255,255,0.3);
  color: rgba(255,255,255,0.75);
}

#join-space-form {
  display: flex;
  gap: 0.5rem;
  width: 100%;
}
#join-space-form.hidden { display: none; }
#join-space-input {
  flex: 1;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  padding: 0.6rem 0.75rem;
  font-family: inherit;
  font-size: 0.8rem;
  outline: none;
  transition: border-color 0.2s ease;
}
#join-space-input:focus {
  border-color: rgba(255,255,255,0.3);
}
#btn-join-go {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.7);
  padding: 0.6rem 1rem;
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.2s ease;
}
#btn-join-go:hover {
  background: rgba(255,255,255,0.15);
}
#btn-join-back {
  background: transparent;
  border: none;
  color: rgba(255,255,255,0.4);
  padding: 0.6rem 0.5rem;
  font-family: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  letter-spacing: 0.3px;
  transition: color 0.2s ease;
}
#btn-join-back:hover {
  color: rgba(255,255,255,0.7);
}

/* ── Spinner screen ── */
#loading-spinner-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#loading-spinner-screen.hidden {
  display: none;
}

#loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255,255,255,0.15);
  border-top-color: #e94560;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#loading-text {
  margin-top: 16px;
  color: rgba(255,255,255,0.6);
  font-size: 14px;
  letter-spacing: 1px;
}

#loading-slow-hint {
  margin-top: 10px;
  color: rgba(255,255,255,0.3);
  font-size: 12px;
  letter-spacing: 0.3px;
  transition: opacity 0.3s ease;
}

/* ── Location banner ── */
#location-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: rgba(233, 69, 96, 0.15);
  border-top: 1px solid rgba(233, 69, 96, 0.3);
  color: rgba(255,255,255,0.85);
  font-size: 12px;
  letter-spacing: 0.3px;
  line-height: 1.4;
  z-index: 1500;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

#location-banner.visible {
  transform: translateY(0);
}

.banner-text {
  flex: 1;
  padding-right: 12px;
}

.banner-close {
  background: none;
  border: none;
  color: rgba(255,255,255,0.5);
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  flex-shrink: 0;
  line-height: 1;
}

.banner-close:hover {
  color: #fff;
}

/* ── Toast notification ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(30,30,50,0.92);
  color: #fff;
  padding: 8px 18px;
  border-radius: 20px;
  font-size: 13px;
  letter-spacing: 0.5px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  z-index: 2000;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 10px;
}

#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#toast.has-action {
  pointer-events: auto;
  white-space: normal;
  max-width: min(92vw, 520px);
  border-radius: 14px;
  padding: 10px 12px;
}

.toast-message {
  flex: 1;
  min-width: 0;
}

.toast-action,
.toast-dismiss {
  border: 1px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.1);
  color: #fff;
  font: inherit;
  font-size: 12px;
  letter-spacing: 0.3px;
  padding: 6px 10px;
  border-radius: 9px;
  cursor: pointer;
}

.toast-action {
  background: rgba(233, 69, 96, 0.9);
  border-color: rgba(233, 69, 96, 1);
}

.toast-action:hover {
  background: rgba(233, 69, 96, 1);
}

.toast-dismiss:hover {
  border-color: rgba(255,255,255,0.5);
}

/* ── Hamburger menu ── */
#menu-btn {
  width: 24px;
  height: 18px;
  position: relative;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}

#menu-btn span,
#menu-btn::before,
#menu-btn::after {
  display: block;
  position: absolute;
  left: 0;
  width: 100%;
  height: 2px;
  background: rgba(255,255,255,0.8);
  border-radius: 1px;
  transition: all 0.25s ease;
}

#menu-btn span {
  top: 8px;
}

#menu-btn::before {
  content: '';
  top: 0;
}

#menu-btn::after {
  content: '';
  bottom: 0;
}

body.menu-open #menu-btn span {
  opacity: 0;
}

body.menu-open #menu-btn::before {
  top: 8px;
  transform: rotate(45deg);
}

body.menu-open #menu-btn::after {
  bottom: 8px;
  transform: rotate(-45deg);
}

#menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

body.menu-open #topbar {
  z-index: 2500;
}

body.menu-open #menu-backdrop {
  opacity: 1;
  pointer-events: auto;
}



#menu-panel {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 80vw;
  background: #1a1a2e;
  z-index: 2000;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 58px 0 env(safe-area-inset-bottom);
  border-right: 1px solid rgba(255,255,255,0.06);
}

body.menu-open #menu-panel {
  transform: translateX(0);
}

.menu-item {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: rgba(255,255,255,0.7);
  font: inherit;
  font-size: 0.85rem;
  letter-spacing: 0.3px;
  padding: 12px 20px;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease;
}

.menu-item:hover {
  background: rgba(255,255,255,0.06);
  color: #fff;
}

.menu-item.hidden {
  display: none;
}

.menu-section-label {
  padding: 16px 20px 4px;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: rgba(255,255,255,0.25);
}

#menu-space-slug {
  padding: 4px 20px 8px;
  font-size: 0.8rem;
  color: rgba(255,255,255,0.5);
  letter-spacing: 0.5px;
  word-break: break-all;
}

#menu-btn-leave-space {
  color: rgba(255,255,255,0.35);
}

#menu-btn-leave-space:hover {
  color: #e94560;
}

.menu-sep {
  border: none;
  border-top: 1px solid rgba(255,255,255,0.06);
  margin: 4px 16px;
}

#menu-join-form {
  padding: 4px 16px 12px;
}

#menu-space-section.hidden,
#menu-join-form.hidden {
  display: none;
}

#menu-join-input {
  width: 100%;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.8rem;
  outline: none;
  margin-bottom: 8px;
}

#menu-join-input:focus {
  border-color: rgba(255,255,255,0.3);
}

.menu-join-actions {
  display: flex;
  gap: 8px;
}

.menu-join-actions button {
  flex: 1;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.7);
  padding: 6px;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  transition: background 0.15s ease;
}

.menu-join-actions button:hover {
  background: rgba(255,255,255,0.14);
}

#menu-join-go {
  background: rgba(233, 69, 96, 0.2) !important;
  border-color: rgba(233, 69, 96, 0.4) !important;
  color: #e94560 !important;
}

/* ── Safe area insets ── */
.leaflet-bottom.leaflet-right {
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}
#topbar {
  padding-left: max(10px, env(safe-area-inset-left));
  padding-right: max(10px, env(safe-area-inset-right));
}


/* ── Mobile adjustments ── */
@media (max-width: 480px) {
  #logo {
    gap: 0;
  }
  .logo-text {
    display: none;
  }
  .logo-icon {
    width: 40px;
    height: 40px;
  }
  #palette {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    flex: 1;
    max-width: none;
    gap: 4px;
  }
  .swatch {
    width: 100%;
    height: auto;
    aspect-ratio: 1;
  }
  #locate-btn {
    bottom: 100px;
    width: 32px;
    height: 32px;
  }
  .swatch-erase {
    order: -1;
  }
  .swatch-undo {
    order: 1;
  }
  #palette .swatch:nth-child(18) { order: 2; }
  #palette .swatch:nth-child(17) { order: 3; }
  #palette .swatch:nth-child(16) { order: 4; }
  #palette .swatch:nth-child(15) { order: 5; }
  #palette .swatch:nth-child(14) { order: 6; }
  #palette .swatch:nth-child(13) { order: 7; }
  #palette .swatch:nth-child(12) { order: 8; }
  #palette .swatch:nth-child(11) { order: 9; }
  #menu-btn {
    padding: 10px 10px 10px 0;
    margin: -10px 0;
  }
  #menu-panel {
    padding-top: 80px;
    width: 300px;
  }
  #location-banner {
    font-size: 11px;
    padding: 8px 10px;
  }
  #toast.has-action {
    width: calc(100vw - 20px);
    left: 10px;
    right: 10px;
    transform: translateY(20px);
    border-radius: 12px;
    justify-content: space-between;
  }
  #toast.has-action.visible {
    transform: translateY(0);
  }
}
