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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --border: #2a2a4a;
  --text: #e0e0f0;
  --text-dim: #8888aa;
  --accent: #44dd88;
  --accent-dim: #2a8855;
  --rapid: #4488ff;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  touch-action: none;
}

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

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

header h1 {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}

/* Controls bar */
.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
  z-index: 10;
}

.btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s;
}

.btn:active {
  background: var(--border);
}

.btn-primary {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

/* Style the file input to look like a button */
.file-input-btn {
  padding: 6px 14px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent-dim);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  width: 105px;
}

.file-input-btn::file-selector-button {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
  margin: 0;
}

/* Paste area */
.paste-section {
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.paste-section.collapsed {
  display: none;
}

#paste-area {
  width: 100%;
  height: 80px;
  padding: 8px;
  margin: 8px 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: 'Courier New', monospace;
  font-size: 12px;
  resize: vertical;
}

/* Viewer */
#viewer-container {
  flex: 1;
  position: relative;
  min-height: 0;
}

#drop-zone {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
}

#drop-zone.dragover::after {
  content: 'Drop G-code file here';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(68, 221, 136, 0.1);
  border: 2px dashed var(--accent);
  font-size: 18px;
  color: var(--accent);
  pointer-events: none;
}

/* Stats panel */
#stats-panel {
  display: flex;
  gap: 16px;
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

#stats-panel.hidden {
  display: none;
}

#file-name {
  color: var(--accent);
  font-weight: 600;
  margin-right: auto;
}

.stat {
  color: var(--text-dim);
}

.stat span {
  color: var(--text);
}

/* Legend */
.legend {
  display: flex;
  gap: 12px;
  font-size: 11px;
  color: var(--text-dim);
}

.legend-item::before {
  content: '';
  display: inline-block;
  width: 12px;
  height: 3px;
  margin-right: 4px;
  vertical-align: middle;
  border-radius: 1px;
}

.legend-rapid::before {
  background: var(--rapid);
  opacity: 0.5;
}

.legend-shallow::before {
  background: var(--accent);
}

.legend-deep::before {
  background: #ff5533;
}

/* Status message overlay */
.status-msg {
  position: absolute;
  bottom: 12px;
  left: 12px;
  right: 12px;
  padding: 10px 14px;
  background: rgba(15, 15, 26, 0.9);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  z-index: 8;
  display: none;
  text-align: center;
}

/* Empty state */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
  pointer-events: none;
  z-index: 1;
}

.empty-state p {
  margin: 4px 0;
}

/* Toggle button */
.toggle-paste {
  font-size: 12px;
  padding: 4px 10px;
}

/* Playback bar */
#playback-bar {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
  flex-shrink: 0;
  z-index: 10;
}

#playback-bar.hidden {
  display: none;
}

.playback-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.btn-play {
  min-width: 64px;
  font-weight: 600;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 11px;
}

.speed-control {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  font-size: 12px;
  color: var(--text-dim);
}

.speed-control label {
  white-space: nowrap;
}

#speed-slider {
  width: 90px;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

#speed-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

#speed-label {
  min-width: 32px;
  text-align: right;
  color: var(--text);
  font-weight: 600;
  font-size: 12px;
}

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

.progress-bar {
  flex: 1;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  cursor: pointer;
  overflow: hidden;
  touch-action: none;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.05s linear;
}

.progress-text {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  min-width: 80px;
  text-align: right;
}

/* Info panel */
#info-panel {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
  flex-shrink: 0;
  z-index: 10;
  max-height: 180px;
  overflow-y: auto;
}

#info-panel.hidden {
  display: none;
}

.info-section {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 3px 0;
  font-size: 12px;
  border-bottom: 1px solid rgba(42, 42, 74, 0.5);
}

.info-section:last-child {
  border-bottom: none;
}

.info-label {
  color: var(--accent);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  min-width: 70px;
  flex-shrink: 0;
}

.tool-item {
  padding: 2px 0;
  font-size: 12px;
  color: var(--text-dim);
}

.toggle-info {
  margin-left: auto;
  font-size: 11px;
  padding: 2px 8px;
}

/* G-code panel — overlaid on the 3D viewer */
.gcode-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 280px;
  max-height: 100%;
  background: rgba(15, 15, 26, 0.92);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  z-index: 6;
  font-family: 'Courier New', monospace;
  font-size: 11px;
  line-height: 1.5;
}

.gcode-panel.hidden {
  display: none;
}

.gcode-line {
  display: flex;
  padding: 0 8px;
  border-left: 3px solid transparent;
}

.gcode-line.active {
  background: rgba(68, 221, 136, 0.15);
  border-left-color: var(--accent);
}

.gcode-num {
  color: var(--text-dim);
  min-width: 36px;
  text-align: right;
  margin-right: 8px;
  user-select: none;
  opacity: 0.6;
}

.gcode-text {
  color: var(--text);
  white-space: pre;
}

.gcode-line.active .gcode-text {
  color: #fff;
  font-weight: 600;
}

.toggle-gcode {
  font-size: 12px;
  padding: 4px 10px;
}

/* Z-depth legend — small vertical bar in viewer */
.z-legend {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 6px;
  align-items: stretch;
  z-index: 6;
  pointer-events: none;
}

.z-legend.hidden {
  display: none;
}

.z-legend-bar {
  width: 8px;
  height: 60px;
  border-radius: 4px;
  background: linear-gradient(to bottom, #44dd88, #dddd44, #ff8833, #ff5533);
}

.z-legend-labels {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-dim);
}

/* Responsive */
@media (max-width: 480px) {
  header h1 { font-size: 14px; }
  .controls { gap: 4px; padding: 6px 8px; }
  .btn { padding: 6px 10px; font-size: 12px; }
  #stats-panel { gap: 8px; font-size: 11px; padding: 6px 8px; }
  #playback-bar { padding: 6px 8px; }
  .speed-control { margin-left: 0; }
  #speed-slider { width: 70px; }
  .gcode-panel { width: 220px; font-size: 10px; }
}
