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

:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e5e5ea;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --ui-btn: #868686;
  --ui-btn-hover: #6a6a6a;
  --radius: 12px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --header-h: 52px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

/* ── Header ── */
.app-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
}

.logo {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.logo-byline {
  font-size: 12px;
  font-weight: 500;
  color: #6e6e73;
  background: #f5f5f7;
  border: 1px solid #d2d2d7;
  border-radius: 999px;
  padding: 2px 10px;
  letter-spacing: 0.01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.header-btn {
  padding: 6px 14px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.header-btn:hover {
  border-color: var(--ui-btn);
  color: var(--text);
}

/* ── App layout: fills viewport below header ── */
.app-layout {
  display: flex;
  height: calc(100vh - var(--header-h));
}

/* ── Toolbar ── */
.controls-panel {
  width: 380px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px 24px;
}

/* Subtle scrollbar */
.controls-panel::-webkit-scrollbar {
  width: 6px;
}

.controls-panel::-webkit-scrollbar-track {
  background: transparent;
}

.controls-panel::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
}

.controls-panel::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.18);
}

/* Firefox */
.controls-panel {
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.1) transparent;
}

.control-group {
  padding-top: 22px;
  padding-bottom: 22px;
  border-bottom: 1px solid var(--border);
}

.control-group:first-child {
  padding-top: 0;
}

.control-group:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.control-group h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-secondary);
  margin-bottom: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 20px;
}

.control-group h3:has(+ .hidden),
.control-group h3:last-child {
  margin-bottom: 0;
}

/* ── Toggle Switch ── */
.toggle-switch {
  position: relative;
  display: inline-block !important;
  width: 36px;
  height: 20px;
  min-height: 20px !important;
  flex-shrink: 0;
  margin: 0;
  cursor: pointer;
}

.text-input {
  font-size: 12px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  width: 100%;
  outline: none;
}

.text-input:focus {
  border-color: #999;
}

/* Nested toggle options */
.toggle-options {
  margin-left: 10px;
  padding-left: 12px;
  border-left: 2px solid var(--border);
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.toggle-slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 36px;
  height: 20px;
  background: var(--border);
  border-radius: 10px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  left: 2px;
  top: 2px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.toggle-switch input:checked + .toggle-slider {
  background: #999;
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(16px);
}

.control-group label {
  display: grid;
  grid-template-columns: 100px 1fr auto;
  align-items: center;
  font-size: 13px;
  margin-bottom: 10px;
  gap: 6px;
  white-space: nowrap;
  min-height: 30px;
}

.control-group label:last-child {
  margin-bottom: 0;
}

.control-group select {
  font-size: 12px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  width: 100%;
  min-width: 0;
  height: 30px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  min-width: 50px;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, #999 0%, #999 50%, #e6e6e6 50%, #e6e6e6 100%);
  cursor: pointer;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ffffff;
  border: 1px solid #ccc;
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ffffff;
  border: 1px solid #ccc;
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
  cursor: pointer;
}

input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 3px;
  background: #e6e6e6;
}

input[type="range"]::-moz-range-progress {
  height: 6px;
  border-radius: 3px;
  background: #999;
}

.range-val {
  font-size: 11px;
  color: var(--text-secondary);
  min-width: 30px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

/* Labels with only text + select or swatch (no slider) use 2 cols */
.control-group label:has(select):not(:has(input[type="range"])) {
  grid-template-columns: 100px 1fr;
}

.control-group label:has(.color-swatch):not(:has(input[type="range"])) {
  grid-template-columns: 100px 1fr;
}

.control-group label:has(.color-swatch) .color-swatch {
  justify-self: end;
}

/* Upload — override grid layout */
.upload-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border: 2px dashed var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all 0.2s;
  width: 100%;
  grid-column: 1 / -1;
}

.upload-btn:hover {
  border-color: var(--ui-btn);
  color: var(--text);
}

.upload-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 300;
}

/* Background tabs */
.bg-type-tabs {
  display: flex;
  gap: 3px;
  margin-bottom: 14px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.bg-tab {
  flex: 1;
  padding: 5px 2px;
  font-size: 11px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  font-weight: 500;
  transition: all 0.2s;
}

.bg-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.bg-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
}

.bg-presets {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}

.bg-preset-swatch {
  aspect-ratio: 1;
  border-radius: 6px;
  background-size: cover;
  background-position: center;
  cursor: pointer;
  border: none;
  outline: 2px solid transparent;
  outline-offset: -2px;
  transition: outline-color 0.15s, transform 0.15s;
  overflow: hidden;
}

.bg-preset-swatch:hover {
  transform: scale(1.08);
}

.bg-preset-swatch.active {
  outline-color: var(--ui-btn);
}

.sub-section {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.upload-btn-sm {
  padding: 6px 10px;
  font-size: 12px;
  justify-content: center;
}

/* ── Timeline in toolbar ── */
.toolbar-timeline {
  margin-bottom: 12px;
}

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

.play-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--ui-btn);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}

.play-btn:hover {
  background: var(--ui-btn-hover);
}

.time-display {
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.timeline {
  position: relative;
}

.timeline-track {
  position: relative;
  height: 28px;
  background: var(--bg);
  border-radius: 6px;
  cursor: pointer;
  overflow: visible;
}

.timeline-progress {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: rgba(150, 150, 150, 0.15);
  border-radius: 6px 0 0 6px;
  pointer-events: none;
}

.scrubber {
  position: absolute;
  top: -2px;
  width: 3px;
  height: 32px;
  background: var(--ui-btn);
  border-radius: 2px;
  cursor: ew-resize;
  transform: translateX(-1.5px);
  z-index: 5;
  transition: box-shadow 0.15s;
}

.scrubber:hover,
.scrubber.dragging {
  box-shadow: 0 0 0 4px rgba(130, 130, 130, 0.2);
}

.keyframe-marker {
  position: absolute;
  top: 50%;
  width: 12px;
  height: 12px;
  background: var(--ui-btn);
  border: 2px solid var(--surface);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: grab;
  z-index: 3;
  transition: transform 0.1s, box-shadow 0.15s;
}

.keyframe-marker:hover {
  transform: translate(-50%, -50%) scale(1.2);
}

.keyframe-marker.selected {
  box-shadow: 0 0 0 3px rgba(130, 130, 130, 0.3);
  transform: translate(-50%, -50%) scale(1.2);
}

.keyframe-marker.edge {
  background: var(--text-secondary);
}

.timeline-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.small-btn {
  padding: 4px 10px;
  font-size: 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.small-btn:hover {
  border-color: var(--ui-btn);
  color: var(--text);
}

/* Keyframe editor in toolbar */
.keyframe-editor {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.keyframe-editor h4 {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 8px;
}

.keyframe-editor label {
  display: grid;
  grid-template-columns: 100px 1fr auto;
  align-items: center;
  font-size: 13px;
  margin-bottom: 10px;
  gap: 6px;
  min-height: 30px;
}

.keyframe-editor label:last-child {
  margin-bottom: 0;
}

.keyframe-editor label:has(select):not(:has(input[type="range"])) {
  grid-template-columns: 100px 1fr;
}

.keyframe-editor label:has(select) select {
  justify-self: end;
}

.keyframe-editor select {
  font-size: 12px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  width: 100%;
  min-width: 0;
}

.keyframe-editor label:has(select):not(:has(input[type="range"])) {
  grid-template-columns: 100px 1fr;
}

/* ── Canvas area: fills remaining space ── */
.canvas-area {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;
  position: relative;
}

.canvas-wrapper {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  box-shadow: var(--shadow-md);
  flex-shrink: 0;
}

.canvas-wrapper canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  pointer-events: none;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
  opacity: 0.4;
}

.empty-state p {
  font-size: 14px;
  text-align: center;
  line-height: 1.5;
}

/* ── Zoom controls ── */
.zoom-controls {
  position: absolute;
  bottom: 16px;
  right: 16px;
  display: flex;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 4px;
  box-shadow: var(--shadow-sm);
}

.zoom-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.zoom-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.zoom-btn.zoom-fit {
  font-size: 11px;
  font-weight: 600;
  width: auto;
  padding: 0 10px;
  border-radius: 100px;
}

/* ── Bottom playbar ── */
.bottom-playbar {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 140px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 4px 14px 4px 4px;
  box-shadow: var(--shadow-sm);
}

.bottom-playbar .play-btn {
  width: 30px;
  height: 30px;
}

.playbar-track {
  flex: 1;
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  cursor: pointer;
  position: relative;
}

.playbar-progress {
  height: 100%;
  background: var(--ui-btn);
  border-radius: 3px;
  width: 0%;
  pointer-events: none;
}

.playbar-time {
  font-size: 11px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Export ── */
.export-section .export-btn,
.export-btn {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--text);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 8px;
}

.export-section .export-btn:hover:not(:disabled),
.export-btn:hover:not(:disabled) {
  background: #333;
}

.export-section .export-btn:disabled,
.export-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.export-progress {
  margin-top: 12px;
}

.progress-bar {
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--ui-btn);
  border-radius: 3px;
  width: 0%;
  transition: width 0.3s;
}

.progress-text {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 6px;
  display: block;
}

/* ── Color swatches ── */
.color-swatch {
  width: 32px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--border);
  cursor: pointer;
  display: inline-block;
  flex-shrink: 0;
  transition: box-shadow 0.15s;
}

.color-swatch:hover {
  box-shadow: 0 0 0 2px rgba(130, 130, 130, 0.3);
}

/* ── Timeline Tooltip ── */
.timeline-tooltip {
  position: fixed;
  padding: 3px 8px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  background: var(--text);
  color: white;
  border-radius: 4px;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
  transform: translateX(-50%);
}

/* ── Dropdown Panels ── */
.dropdown-panel {
  position: fixed;
  z-index: 900;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
  padding: 6px;
  width: 240px;
  max-height: 400px;
  overflow-y: auto;
}

.dropdown-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.12s;
}

.dropdown-item:hover {
  background: var(--bg);
}

.dropdown-item strong {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.dropdown-item span {
  font-size: 11px;
  color: var(--text-secondary);
}

/* ── Color Picker ── */
.cp-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
}

.cp-popup {
  position: fixed;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  padding: 16px;
  width: 260px;
  z-index: 1001;
}

.cp-body {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.cp-sv {
  border-radius: 6px;
  cursor: crosshair;
  display: block;
}

.cp-hue {
  border-radius: 6px;
  cursor: pointer;
  display: block;
}

.cp-footer {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cp-preview {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.cp-hex {
  flex: 1;
  font-size: 13px;
  font-family: monospace;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  min-width: 0;
}

.cp-ok {
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  background: var(--text);
  color: white;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.cp-ok:hover {
  background: #333;
}

/* ── Utilities ── */
.hidden {
  display: none !important;
}

body.dragover .canvas-wrapper {
  outline: 3px dashed var(--ui-btn);
  outline-offset: -3px;
}

/* ── Responsive ── */
@media (max-width: 768px) {
  body {
    height: auto;
    overflow: auto;
  }
  .app-layout {
    flex-direction: column;
    height: auto;
  }
  .controls-panel {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: none;
  }
  .canvas-area {
    padding: 16px;
    min-height: 50vh;
  }
  .canvas-wrapper {
    max-height: 60vh;
  }
}
