body {
  margin: 0;
  overflow: hidden;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
canvas {
  display: block;
}

#controls {
  position: fixed;
  top: 0;
  left: -460px; /* Hidden off-screen initially - accounts for width + padding + border + shadow */
  width: 400px;
  height: 100vh;
  background: rgba(255, 255, 255, 0.98);
  color: #333;
  padding: 24px;
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  transition: left 0.3s ease;
  z-index: 10;
  overflow-y: auto;
}

#controls.open {
  left: 0; /* Slide in from left */
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f2f5;
}

#controls h3 {
  margin: 0;
  color: #1a365d;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
  flex: 1;
}

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

.icon-btn {
  background: #e2e8f0;
  color: #4a5568;
  border: none;
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: #cbd5e0;
  transform: scale(1.05);
}

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

.control-group {
  margin-bottom: 24px;
}

.control-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #374151;
  font-size: 14px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
}

label[for="functionInput"] {
  font-size: 14px;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 4px;
}

.input-buttons {
  display: flex;
  gap: 12px;
  width: 100%;
}

#functionInput {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.2s ease;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Ensure consistent font */
}

#functionInput:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.primary-btn {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0; /* Prevent button from shrinking */
  min-width: 100px;
  flex: 1;
}

.primary-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.primary-btn:active {
  transform: translateY(0);
}

.btn-icon {
  font-size: 16px;
}

#controls h4 {
  margin: 20px 0 12px 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
  border-bottom: 2px solid #f3f4f6;
  padding-bottom: 8px;
}

.range-inputs {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
}

.range-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
}

.range-group label {
  width: 70px;
  font-weight: 500;
  color: #6b7280;
  font-size: 14px;
  flex-shrink: 0;
}

.range-group input {
  width: 70px;
  padding: 8px 12px;
  border: 2px solid #e5e7eb;
  border-radius: 6px;
  text-align: center;
  font-size: 14px;
  transition: border-color 0.2s ease;
  flex-shrink: 0;
}

.range-group input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.range-group span {
  color: #9ca3af;
  font-weight: 500;
  flex-shrink: 0;
  margin: 0 4px;
}

.checkboxes {
  display: flex;
  gap: 12px;
  width: 100%;
}

.checkbox-label {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 500;
  padding: 12px 20px;
  border-radius: 8px;
  transition: background-color 0.2s ease;
  border: 1px solid #d1d5db;
  background: #f3f4f6;
  color: #374151;
  flex: 1;
  min-height: 40px;
  font-size: 14px;
}

.checkbox-label:hover {
  background: #e5e7eb;
  border-color: #9ca3af;
  transform: translateY(-1px);
}

.checkbox-label input[type="checkbox"] {
  margin-right: 8px;
  width: 18px;
  height: 18px;
  accent-color: #3b82f6;
}

.checkbox-label span {
  color: #374151;
  user-select: none;
  font-weight: 600;
}

.function-list-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.function-list-group .function-list {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: white;
  color: #374151;
  cursor: pointer;
  transition: border-color 0.2s ease;
  box-sizing: border-box;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6,9 12,15 18,9'%3e%3c/polyline%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 40px;
}

.function-list-group .function-list:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.function-list-group .function-list:hover {
  border-color: #d1d5db;
}

.danger-btn.small {
  padding: 8px;
  min-width: 36px;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.function-buttons {
  display: flex;
  gap: 12px;
  width: 100%;
}

.secondary-btn {
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #d1d5db;
  padding: 14px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0;
  min-width: 100px;
  flex: 1;
}

.secondary-btn:hover {
  background: #e5e7eb;
  border-color: #9ca3af;
  transform: translateY(-1px);
}

.secondary-btn:active {
  transform: translateY(0);
}

.danger-btn {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
  padding: 14px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0;
  min-width: 100px;
  flex: 1;
}

.danger-btn:hover {
  background: #fee2e2;
  border-color: #fca5a5;
  transform: translateY(-1px);
}

.danger-btn:active {
  transform: translateY(0);
}

/* Action buttons - always visible */
.action-buttons {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 5; /* Behind controls but in front of canvas */
}

/* Burger menu button */
.burger-btn {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(255, 255, 255, 0.95);
  color: #374151;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  width: 48px;
  height: 48px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  z-index: 15;
  backdrop-filter: blur(10px);
}

.burger-btn:hover {
  background: rgba(255, 255, 255, 0.98);
  border-color: #3b82f6;
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

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

.burger-icon {
  display: inline-block;
  width: 24px;
  height: 18px;
  position: relative;
  transition: 0.3s ease-in-out;
}

.burger-icon::before,
.burger-icon::after,
.burger-icon span {
  content: '';
  display: block;
  position: absolute;
  height: 3px;
  width: 100%;
  background: #374151;
  border-radius: 2px;
  opacity: 1;
  left: 0;
  transform: rotate(0deg);
  transition: 0.25s ease-in-out;
}

.burger-icon::before {
  top: 0px;
}

.burger-icon span {
  top: 7.5px;
}

.burger-icon::after {
  top: 15px;
}

.burger-btn.active .burger-icon::before {
  top: 7.5px;
  transform: rotate(135deg);
}

.burger-btn.active .burger-icon::after {
  top: 7.5px;
  transform: rotate(-135deg);
}

.burger-btn.active .burger-icon span {
  opacity: 0;
  left: -60px;
}

#controls.hidden {
  left: -460px; /* Slide out to the left */
}

/* Mobile responsiveness */
@media (max-width: 768px) {
  #controls {
    width: 320px;
    left: -380px; /* Hidden off-screen */
    padding: 20px;
  }
  
  #controls.open {
    left: 0;
  }
  
  #controls.hidden {
    left: -380px;
  }
  
  .action-buttons {
    right: 10px;
  }
  
  .burger-btn {
    width: 48px;
    height: 48px;
  }
  
  .burger-btn .burger-icon {
    width: 24px;
    height: 18px;
  }
  
  .burger-btn .burger-icon span {
    top: 7.5px;
  }
  
  .burger-btn .burger-icon::after {
    top: 15px;
  }
  
  .input-group {
    flex-direction: column;
    gap: 12px;
  }
  
  .input-buttons {
    gap: 12px;
    width: 100%;
  }
  
  .primary-btn {
    justify-content: center;
  }
  
  .secondary-btn {
    justify-content: center;
  }
  
  .range-inputs {
    gap: 10px;
  }
  
  .range-group {
    gap: 6px;
  }
  
  .range-group input {
    width: 70px;
    padding: 10px;
    font-size: 16px;
  }
  
  .function-list-group {
    flex-direction: column;
    gap: 12px;
  }
  
  .function-list-group .function-list {
    width: 100%;
    padding: 14px 16px;
    font-size: 16px;
    padding-right: 44px;
    background-size: 18px;
    background-position: right 14px center;
  }
  
  .function-buttons {
    gap: 12px;
    width: 100%;
  }
}

@media (max-width: 480px) {
  #controls {
    width: 300px;
    left: -350px;
    padding: 16px;
  }
  
  #controls.open {
    left: 0;
  }
  
  #controls.hidden {
    left: -350px;
  }
  
  .action-buttons {
    right: 10px;
  }
  
  .burger-btn {
    width: 44px;
    height: 44px;
  }
}

/* Touch-friendly improvements for mobile devices */
@media (hover: none) and (pointer: coarse) {
  .burger-btn {
    width: 56px;
    height: 56px;
  }
  
  .burger-btn .burger-icon {
    width: 28px;
    height: 20px;
  }
  
  .burger-btn .burger-icon span {
    top: 8.5px;
  }
  
  .burger-btn .burger-icon::after {
    top: 16.5px;
  }
  
  .primary-btn, .secondary-btn, .danger-btn {
    min-height: 48px;
    padding: 14px 16px;
    font-size: 16px;
  }
  
  .range-group input,
  #functionInput {
    min-height: 48px;
    font-size: 16px;
  }
  
  .checkbox-label {
    min-height: 48px;
    padding: 14px 16px;
  }
  
  .icon-btn {
    width: 44px;
    height: 44px;
  }
  
  .function-list {
    min-height: 48px;
    font-size: 16px;
  }
}

/* Popup mobile adjustments */
@media (max-width: 480px) {
  .popup-content {
    width: 90vw;
    max-width: none;
    padding: 15px 20px;
    font-size: 15px;
  }
  
  .popup-close {
    top: 8px;
    right: 12px;
    font-size: 20px;
  }
}

/* Modal styles */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2000;
  animation: fadeIn 0.3s ease-out;
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-header h3 {
  margin: 0;
  color: #1f2937;
  font-size: 18px;
  font-weight: 600;
}

.modal-close {
  font-size: 24px;
  cursor: pointer;
  color: #9ca3af;
  transition: color 0.2s ease;
}

.modal-close:hover {
  color: #6b7280;
}

.modal-body {
  padding: 20px 24px;
}

.modal-body h4 {
  margin: 20px 0 8px 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.modal-body p {
  margin: 0 0 12px 0;
  color: #4b5563;
  line-height: 1.6;
}

.modal-body ul {
  margin: 8px 0 16px 0;
  padding-left: 20px;
}

.modal-body li {
  margin-bottom: 4px;
  color: #4b5563;
}

.modal-body code {
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: 13px;
}

.modal-body kbd {
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 2px 6px;
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Loading indicator */
.loading-indicator {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.95);
  padding: 20px 24px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  display: none;
  align-items: center;
  gap: 12px;
  z-index: 1500;
  backdrop-filter: blur(10px);
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid #e5e7eb;
  border-top: 2px solid #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Enhanced popup styles */
.popup {
  display: none;
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  animation: slideUp 0.3s ease-out;
}

.popup-content {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  color: white;
  padding: 16px 24px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(16, 185, 129, 0.3);
  max-width: 400px;
  text-align: center;
  position: relative;
  font-size: 14px;
  font-weight: 500;
  backdrop-filter: blur(10px);
}

.popup-content.error {
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  box-shadow: 0 8px 32px rgba(220, 38, 38, 0.3);
}

.popup-content.info {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  box-shadow: 0 8px 32px rgba(59, 130, 246, 0.3);
}

.popup-content.warning {
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
  box-shadow: 0 8px 32px rgba(245, 158, 11, 0.3);
}

.popup-content.success {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  box-shadow: 0 8px 32px rgba(16, 185, 129, 0.3);
}

.popup-close {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 20px;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.8);
  transition: color 0.3s;
}

.popup-close:hover {
  color: white;
}

#popup-message {
  margin: 0;
  padding-right: 24px;
}

@keyframes slideUp {
  from { transform: translateX(-50%) translateY(100%); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* Popup mobile adjustments */
@media (max-width: 480px) {
  .popup-content {
    width: 90vw;
    max-width: none;
    padding: 16px 20px;
    font-size: 15px;
  }
  
  .popup-close {
    top: 10px;
    right: 12px;
    font-size: 20px;
  }
}
