:root {
  --bg: #0f0f12;
  --bg-elevated: #18181c;
  --bg-card: #1e1e24;
  --border: #2a2a32;
  --text: #e8e8ed;
  --text-muted: #8b8b96;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-muted: rgba(99, 102, 241, 0.2);
  --success: #22c55e;
  --error: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: 'DM Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.header {
  text-align: center;
  margin-bottom: 3rem;
}

.logo {
  font-size: 5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--text) 0%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.tagline {
  color: var(--text-muted);
  font-size: 1.1rem;
  margin-top: 0.5rem;
}

.code-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  max-width: 420px;
  margin: 0 auto 2rem;
}

.code-box h2 {
  font-size: 1.25rem;
  margin-bottom: 0.5rem;
}

.code-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.code-box input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 0.02em;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  margin-bottom: 1rem;
}

.code-box input:focus {
  outline: none;
  border-color: var(--accent);
}

.code-actions {
  display: flex;
  gap: 0.75rem;
}

.code-actions .generate-btn {
  flex: 1;
  margin-top: 0;
}

.btn-outline {
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-outline:hover {
  color: var(--text);
  border-color: var(--accent);
}

.code-error {
  color: var(--error);
  font-size: 0.9rem;
  margin-top: 1rem;
}

.step-instruction {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  margin: 1rem 0;
}

.step-instruction-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.step-instruction-text {
  font-size: 1.25rem;
  font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
}

.instruction-nav {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.instruction-nav .step-btn {
  flex: 1;
}

.jump-to-step {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.jump-to-step label {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.jump-to-step input {
  width: 100px;
  padding: 0.4rem 0.75rem;
  font-size: 0.95rem;
  font-family: 'JetBrains Mono', monospace;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
}

.jump-to-step input:focus {
  outline: none;
  border-color: var(--accent);
}

.byline {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.25rem;
  opacity: 0.8;
}

/* Upload section */
.upload-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
}

.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 3rem 2rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
  background: var(--bg-elevated);
}

.upload-zone:hover,
.upload-zone.dragover {
  border-color: var(--accent);
  background: var(--accent-muted);
}

.upload-icon {
  width: 48px;
  height: 48px;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.upload-text {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
}

.upload-hint {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.board-info {
  margin-top: 1rem;
}

.board-spec {
  font-size: 0.9rem;
  color: var(--text-muted);
  font-weight: 500;
}

.trace-control {
  margin: 1rem 0;
}

.trace-label {
  display: block;
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.trace-slider {
  width: 100%;
  height: 8px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--bg);
  border-radius: 4px;
  outline: none;
}

.trace-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.2s;
}

.trace-slider::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}

.trace-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

.options-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1.5rem;
}

.option-label {
  font-weight: 500;
  color: var(--text-muted);
}

.pin-toggle {
  display: flex;
  gap: 0;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 4px;
  border: 1px solid var(--border);
}

.pin-btn {
  padding: 0.5rem 1.25rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

.pin-btn:hover {
  color: var(--text);
}

.pin-btn.active {
  background: var(--accent);
  color: white;
}

.generate-btn {
  width: 100%;
  margin-top: 1.5rem;
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  font-family: inherit;
  color: white;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s;
}

.generate-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.generate-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Result section */
.result-section {
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.result-section.hidden {
  display: none;
}

.upload-section.hidden {
  display: none;
}

.align-section.hidden {
  display: none;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.result-header h2 {
  font-size: 1.5rem;
  font-weight: 600;
}

.back-btn {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-family: inherit;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.back-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.preview-area {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.preview-scale {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.canvas-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 600px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 1.5rem;
}

/* 50cm board at 20px/cm = 1000px display for 1:1 scale */
.canvas-50cm .preview-img,
.canvas-50cm canvas.preview-img {
  max-width: 1000px;
  max-height: 1000px;
  width: auto;
  height: auto;
  border-radius: var(--radius-sm);
}

.preview-img {
  max-width: 100%;
  max-height: 700px;
  width: auto;
  height: auto;
  border-radius: var(--radius-sm);
}

.step-badge {
  position: absolute;
  bottom: 1rem;
  right: 1rem;
  padding: 0.35rem 0.75rem;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
}

.slider-container {
  padding: 0 0.5rem;
}

.slider-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.slider-controls .step-slider {
  flex: 1;
}

.step-btn {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  font-family: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
}

.step-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.slider-label {
  display: block;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.step-slider {
  width: 100%;
  height: 8px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--bg);
  border-radius: 4px;
  outline: none;
}

.step-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.2s;
}

.step-slider::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}

.step-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

/* Instructions panel */
.instructions-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.instructions-panel h3 {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.instructions-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.instructions-content {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
  max-height: 200px;
  overflow-y: auto;
}

.instructions-text {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text);
  word-break: break-all;
}

.copy-btn {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-family: inherit;
  color: var(--accent);
  background: var(--accent-muted);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.copy-btn:hover {
  background: rgba(99, 102, 241, 0.3);
}

/* Loading overlay */
.align-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
}

.align-header {
  margin-bottom: 1rem;
}

.align-header h2 {
  font-size: 1.25rem;
  margin-bottom: 0.25rem;
}

.align-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.align-canvas-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 0.75rem;
}

.align-toolbar {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.align-tool-btn {
  width: 40px;
  height: 40px;
  font-size: 1.25rem;
  font-family: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.align-tool-btn:hover {
  background: var(--accent-muted);
  border-color: var(--accent);
}

.align-viewport {
  position: relative;
  width: 480px;
  height: 480px;
  cursor: grab;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid var(--accent);
  box-shadow: 0 0 0 1px var(--border);
}

.align-viewport:active {
  cursor: grabbing;
}

.align-canvas {
  display: block;
  border-radius: 50%;
}

.circle-overlay {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  border: 2px solid rgba(99, 102, 241, 0.6);
  box-sizing: border-box;
}

.align-controls {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.back-align-btn {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-family: inherit;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.back-align-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

/* Loading overlay with progress */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 18, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  z-index: 1000;
  animation: scaleIn 0.3s ease-out;
}

.loading-overlay.hidden {
  display: none;
}

.progress-container {
  width: 320px;
  text-align: center;
}

.progress-bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 1rem;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.2s ease;
}

.progress-text {
  font-weight: 500;
  margin-bottom: 0.25rem;
}

.loading-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Error toast */
.error-toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 1rem 1.5rem;
  background: var(--error);
  color: white;
  border-radius: var(--radius-sm);
  font-weight: 500;
  box-shadow: 0 4px 20px rgba(239, 68, 68, 0.3);
  z-index: 1001;
  animation: slideUp 0.3s ease-out;
}

.error-toast.hidden {
  display: none;
}

/* Animations */
@keyframes float {
  0%, 100% { transform: translateY(0) translateX(0) scale(1); }
  50% { transform: translateY(-18px) translateX(0) scale(1.04); }
}

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4); }
  50% { box-shadow: 0 0 20px 4px rgba(99, 102, 241, 0.2); }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.logo {
  animation: float 5s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

.upload-zone {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.upload-zone:hover {
  transform: translateY(-2px);
}

.generate-btn {
  transition: all 0.3s ease;
}

.generate-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.progress-fill {
  background: linear-gradient(90deg, var(--accent), #818cf8, var(--accent));
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
}

.preview-img {
  animation: scaleIn 0.5s ease-out;
}

.step-btn:hover {
  transform: scale(1.05);
}

.step-btn {
  transition: transform 0.2s ease;
}

.copy-btn:hover {
  transform: scale(1.02);
}

.copy-btn {
  transition: transform 0.2s ease;
}

.instructions-panel {
  animation: slideUp 0.4s ease-out;
}

/* ========== Mobile responsive ========== */
@media (max-width: 768px) {
  .app {
    padding: 1rem 1rem;
  }

  .header {
    margin-bottom: 1.5rem;
  }

  .logo {
    font-size: 2.75rem;
  }

  .tagline {
    font-size: 0.95rem;
  }

  .byline {
    font-size: 0.8rem;
  }

  .code-section {
    padding: 1.25rem;
    max-width: none;
    margin-bottom: 1rem;
  }

  .code-box input {
    padding: 0.875rem 1rem;
    font-size: 16px; /* prevents zoom on iOS focus */
  }

  .code-actions {
    flex-direction: column;
    gap: 0.5rem;
  }

  .code-actions .generate-btn,
  .code-actions .btn-outline {
    width: 100%;
    padding: 0.875rem 1rem;
    min-height: 48px;
    touch-action: manipulation;
  }

  .upload-section {
    padding: 1.25rem;
  }

  .upload-zone {
    padding: 2rem 1.25rem;
  }

  .upload-icon {
    width: 40px;
    height: 40px;
  }

  .upload-text {
    font-size: 1rem;
  }

  .align-section {
    padding: 1.25rem;
  }

  .align-header h2 {
    font-size: 1.1rem;
  }

  .align-hint {
    font-size: 0.85rem;
  }

  .align-canvas-wrap {
    margin-bottom: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .align-viewport {
    width: min(100vw - 2rem, 360px);
    height: min(100vw - 2rem, 360px);
    min-width: 280px;
    min-height: 280px;
    touch-action: none;
  }

  .align-toolbar {
    gap: 0.75rem;
    margin-bottom: 1rem;
  }

  .align-tool-btn {
    width: 48px;
    height: 48px;
    font-size: 1.35rem;
    flex: 1;
    min-width: 48px;
  }

  .align-controls .generate-btn,
  .back-align-btn {
    min-height: 48px;
    padding: 1rem;
  }

  .result-header {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    margin-bottom: 1rem;
  }

  .result-header h2 {
    font-size: 1.25rem;
    text-align: center;
  }

  .back-btn {
    align-self: center;
    min-height: 44px;
    padding: 0.75rem 1.25rem;
  }

  .preview-area {
    padding: 1rem;
    margin-bottom: 1rem;
  }

  .preview-scale {
    font-size: 0.8rem;
  }

  .canvas-container {
    min-height: 280px;
    margin-bottom: 1rem;
    padding: 0.5rem;
  }

  .canvas-50cm .preview-img,
  .canvas-50cm canvas.preview-img {
    max-width: 100%;
    max-height: min(80vh, 500px);
  }

  .step-badge {
    font-size: 0.75rem;
    padding: 0.3rem 0.6rem;
    bottom: 0.75rem;
    right: 0.75rem;
  }

  .slider-container {
    padding: 0;
  }

  .slider-controls {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .slider-controls .step-btn {
    flex: 1;
    min-width: 80px;
    min-height: 44px;
    padding: 0.6rem 0.75rem;
  }

  .slider-controls .step-slider {
    flex: 1 1 100%;
    min-width: 0;
  }

  .step-slider::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
  }

  .step-slider::-moz-range-thumb {
    width: 24px;
    height: 24px;
  }

  .instructions-panel {
    padding: 1.25rem;
  }

  .instructions-hint {
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
  }

  .step-instruction {
    padding: 1rem;
    margin: 0.75rem 0;
  }

  .step-instruction-text {
    font-size: 1.1rem;
  }

  .instruction-nav {
    gap: 0.5rem;
    margin-bottom: 0.75rem;
  }

  .instruction-nav .step-btn {
    min-height: 48px;
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
  }

  .jump-to-step {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .jump-to-step input {
    width: 100%;
    min-height: 44px;
    padding: 0.6rem 1rem;
  }

  .jump-to-step .step-btn {
    min-height: 48px;
    padding: 0.75rem 1rem;
  }

  .error-toast {
    left: 1rem;
    right: 1rem;
    bottom: max(1rem, env(safe-area-inset-bottom));
    transform: none;
    max-width: calc(100% - 2rem);
    padding: 1rem;
  }

  .progress-container {
    width: min(90vw, 320px);
  }
}

@media (max-width: 480px) {
  .logo {
    font-size: 2.25rem;
  }

  .align-viewport {
    width: min(100vw - 2rem, 320px);
    height: min(100vw - 2rem, 320px);
  }
}
