body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background-color: #fafafb;
  /* Improve mobile touch handling */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#map-container {
  position: relative;
  width: 100%;
  height: 100%;
  touch-action: manipulation; /* Allow pan and zoom gestures */
  /* Additional mobile optimizations */
  -webkit-overflow-scrolling: touch;
}

#map {
  width: 100%;
  height: 100%;
  z-index: 1;
  touch-action: manipulation; /* Allow pan and zoom gestures */
}

#canvas {
  position: absolute;
  z-index: 2;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* Ensure smooth touch interactions */
  touch-action: none;
}

/* Fixed Properties Panel */
.properties-panel {
  position: fixed;
  top: 80px; /* Below the toolbar (16px + ~64px toolbar height) */
  left: 16px;
  width: 220px;
  background: #ffffff;
  border: 1px solid #e1e5e9;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  z-index: 5;
  transition: opacity 0.3s ease, transform 0.3s ease;
  overflow: hidden;
}

.properties-panel.hidden {
  opacity: 0;
  transform: translate(16px, 70px) scale(0.95);
  pointer-events: none;
}

/* Compact Panel Layout */
.compact-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid #f0f0f0;
}

.compact-row:last-child {
  border-bottom: none;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.compact-label {
  font-size: 11px;
  font-weight: 500;
  color: #6b7280;
  min-width: 35px;
}

.color-btn-compact {
  width: 28px;
  height: 28px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  transition: all 0.2s ease;
}

.color-btn-compact:hover {
  border-color: #1976d2;
  transform: scale(1.05);
}

.color-preview-compact {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 1px solid #e1e5e9;
}

.toggle-btn-compact {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  background: #ffffff;
  color: #6b7280;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.toggle-btn-compact:hover {
  border-color: #1976d2;
  color: #1976d2;
}

.toggle-btn-compact.active {
  border-color: #1976d2;
  background-color: #e3f2fd;
  color: #1976d2;
}

/* Mobile/Desktop Visibility */
.desktop-only {
  display: block;
}

.mobile-only {
  display: none;
}

#stroke-width-dropdown {
  padding: 4px 6px;
  border: 1px solid #e1e5e9;
  border-radius: 4px;
  font-size: 11px;
  background: white;
  cursor: pointer;
}

/* Mobile positioning: under toolbar */
@media (max-width: 768px) {
  .properties-panel {
    top: 70px; /* Under the toolbar */
    left: 50%;
    transform: translateX(-50%);
    width: calc(100vw - 32px);
    max-width: 350px;
  }
  
  .properties-panel.hidden {
    opacity: 0;
    transform: translate(-50%, -10px) scale(0.95);
    pointer-events: none;
  }
  
  .desktop-only {
    display: none;
  }
  
  .mobile-only {
    display: block;
  }
  
  .compact-row {
    padding: 6px 10px;
    gap: 8px;
  }
  
  .compact-label {
    min-width: 30px;
    font-size: 10px;
  }
  
  .control-group {
    gap: 4px;
  }
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #e1e5e9;
  background: #f8f9fa;
  user-select: none;
}

.delete-btn {
  width: 24px;
  height: 24px;
  border: none;
  background: none;
  color: #dc3545;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.delete-btn:hover {
  background: #f8d7da;
  color: #721c24;
  transform: scale(1.1);
}

.delete-btn:active {
  transform: scale(0.95);
}

.delete-btn.hidden {
  display: none;
}

.panel-header h3 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: #374151;
}

.close-btn {
  width: 20px;
  height: 20px;
  border: none;
  background: none;
  color: #6b7280;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #e5e7eb;
  color: #374151;
}

.panel-content {
  padding: 0;
}

/* Legacy styles - kept for compatibility */
.panel-section {
  margin-bottom: 8px;
}

.panel-section:last-child {
  margin-bottom: 0;
}

.section-label {
  display: block;
  font-size: 11px;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 4px;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.color-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  transition: all 0.2s ease;
}

.color-btn:hover {
  border-color: #1976d2;
  transform: scale(1.05);
}

.color-preview {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid #e1e5e9;
}

.fill-preview {
  background-image: linear-gradient(45deg, #ccc 25%, transparent 25%), 
                    linear-gradient(-45deg, #ccc 25%, transparent 25%), 
                    linear-gradient(45deg, transparent 75%, #ccc 75%), 
                    linear-gradient(-45deg, transparent 75%, #ccc 75%);
  background-size: 6px 6px;
  background-position: 0 0, 0 3px, 3px -3px, -3px 0px;
}

.stroke-width-mini {
  display: flex;
  gap: 4px;
  flex: 1;
}

.stroke-btn-mini {
  flex: 1;
  height: 24px;
  border: 1px solid #e1e5e9;
  border-radius: 4px;
  background: #ffffff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.stroke-btn-mini:hover {
  border-color: #1976d2;
}

.stroke-btn-mini.active {
  border-color: #1976d2;
  background-color: #e3f2fd;
}

.stroke-line {
  width: 16px;
  background-color: #6b7280;
  border-radius: 1px;
}

.stroke-btn-mini.active .stroke-line {
  background-color: #1976d2;
}

.toggle-btn {
  height: 32px;
  padding: 6px 12px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #6b7280;
  transition: all 0.2s ease;
}

.toggle-btn:hover {
  border-color: #1976d2;
  color: #1976d2;
}

.toggle-btn.active {
  border-color: #1976d2;
  background-color: #e3f2fd;
  color: #1976d2;
}

.toolbox {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  background: #ffffff;
  padding: 8px;
  border-radius: 12px;
  border: 1px solid #e1e5e9;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 4px;
  user-select: none;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.tool-separator {
  width: 1px;
  height: 24px;
  background-color: #e1e5e9;
  margin: 0 4px;
}

.tool-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #6c757d;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.2s ease;
  position: relative;
  /* Mobile touch optimizations */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Mobile responsive adjustments */
@media (max-width: 768px) {
  .toolbox {
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    padding: 6px;
    gap: 2px;
    max-width: calc(100vw - 16px);
    overflow-x: auto;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
  }
  
  .toolbox::-webkit-scrollbar {
    display: none; /* Chrome/Safari/Opera */
  }
  
  .tool-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
    min-width: 40px; /* Prevent shrinking on mobile */
  }
  
  .tool-group {
    gap: 1px;
  }
}

.tool-btn:hover {
  background-color: #f8f9fa;
  color: #495057;
}

.tool-btn.active {
  background-color: #e3f2fd;
  color: #1976d2;
}

.tool-btn:active {
  transform: scale(0.95);
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  max-width: 300px;
}

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

.color-option {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 2px solid #e1e5e9;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #6c757d;
}

.color-option:hover {
  transform: scale(1.1);
  border-color: #1976d2;
}

.color-option.transparent {
  background: linear-gradient(45deg, #ccc 25%, transparent 25%), 
              linear-gradient(-45deg, #ccc 25%, transparent 25%), 
              linear-gradient(45deg, transparent 75%, #ccc 75%), 
              linear-gradient(-45deg, transparent 75%, #ccc 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0px;
}

/* Selection handles */
.selection-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #1976d2;
  border: 2px solid white;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  z-index: 10;
}

.selection-handle.corner {
  cursor: nw-resize;
}

.selection-handle.edge-n,
.selection-handle.edge-s {
  cursor: ns-resize;
}

.selection-handle.edge-e,
.selection-handle.edge-w {
  cursor: ew-resize;
}

.selection-handle.rotate {
  background: #4caf50;
  cursor: crosshair;
}
