* {
  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 ── */
.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;
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.1) transparent;
}
.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); }

.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 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,
.control-group .text-input,
.control-group input[type="number"],
.control-group input[type="text"] {
  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;
  outline: none;
  font-family: inherit;
}
.control-group input[type="number"],
.control-group input[type="text"] { cursor: text; }
.control-group select:focus,
.control-group input:focus { border-color: #999; }

.control-group label:has(select):not(:has(input[type="range"])) {
  grid-template-columns: 100px 1fr;
}
.control-group label:has(.text-input):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; }

/* 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;
}
.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); }

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

/* Range slider */
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;
}

/* 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;
}
.upload-btn-sm {
  padding: 6px 10px;
  font-size: 12px;
  justify-content: center;
}

/* 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;
  font-family: inherit;
}
.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); }

/* 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);
}

/* Small / ghost buttons */
.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;
  font-family: inherit;
}
.small-btn:hover {
  border-color: var(--ui-btn);
  color: var(--text);
}

/* ── Clip list (sidebar) ── */
.clip-list {
  margin: 12px 0 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.clip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: 8px;
  cursor: grab;
  transition: background .12s;
}
.clip-row:hover { background: var(--bg); }
.clip-row.active { background: var(--bg); outline: 1px solid var(--ui-btn); outline-offset: -1px; }
.clip-row.dragging { opacity: 0.4; }
.clip-drag-handle {
  width: 14px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  cursor: grab;
  flex-shrink: 0;
  opacity: 0.5;
  transition: opacity 0.12s, color 0.12s;
}
.clip-row:hover .clip-drag-handle { opacity: 1; }
.clip-drag-handle:active { cursor: grabbing; }
.clip-drag-handle svg { display: block; pointer-events: none; }
.clip-row.drop-target-above { box-shadow: inset 0 2px 0 0 var(--ui-btn); }
.clip-row.drop-target-below { box-shadow: inset 0 -2px 0 0 var(--ui-btn); }

.clip-thumb {
  width: 40px; height: 40px;
  border-radius: 6px;
  background: var(--bg) center/cover no-repeat;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.clip-name {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.clip-dur-input {
  width: 56px;
  font-size: 12px;
  padding: 4px 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  height: 26px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  outline: none;
  font-family: inherit;
  color: var(--text);
}
.clip-delete {
  width: 22px; height: 22px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 16px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-family: inherit;
}
.clip-delete:hover { background: var(--bg); color: var(--text); }
.total-row {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 4px;
  padding: 4px 6px;
}
.tabular { font-variant-numeric: tabular-nums; }

/* ── 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;
  font-family: inherit;
}
.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;
}

/* ── Canvas area ── */
.canvas-area {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* bottom padding reserves room for the seq-timeline (80px tall, 70px from bottom) + playbar */
  padding: 24px 24px 175px 24px;
  overflow: hidden;
  position: relative;
}
.canvas-wrapper {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  /* overflow:visible so selection handles can extend past canvas bounds */
  overflow: visible;
  box-shadow: var(--shadow-md);
  flex-shrink: 0;
}
.canvas-wrapper canvas {
  width: 100%;
  height: 100%;
  display: block;
  /* Apply rounded clipping at canvas level since wrapper no longer hides overflow */
  border-radius: var(--radius);
}
.selection-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.sel-box {
  position: absolute;
  border: 1px solid var(--ui-btn);
  pointer-events: none;
  box-sizing: border-box;
}
.snap-guide {
  position: absolute;
  background: #e91e63;
  pointer-events: none;
  opacity: 0.85;
}
.snap-guide.vertical { width: 1px; top: 0; bottom: 0; }
.snap-guide.horizontal { height: 1px; left: 0; right: 0; }
.sel-handle {
  position: absolute;
  width: 8px; height: 8px;
  background: white;
  border: 1px solid var(--ui-btn);
  pointer-events: auto;
  box-sizing: border-box;
  border-radius: 1px;
}

.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;
}

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

/* ── 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;
  font-family: inherit;
}
.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);
}
.play-btn {
  width: 30px;
  height: 30px;
  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); }
.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;
}

/* ── Sequence timeline (above playbar) ── */
.seq-timeline-wrapper {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 70px;
  height: 80px;
  pointer-events: none;
}
.seq-timeline-ticks {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 14px;
  width: 100%;
  pointer-events: none;
}
.seq-timeline-track {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 60px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  pointer-events: auto;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.seq-timeline-clips {
  display: flex;
  height: 100%;
  padding: 6px;
  gap: 4px;
}
.seq-clip {
  background: var(--bg);
  border-radius: 8px;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px 0 4px;
  position: relative;
  min-width: 60px;
  cursor: grab;
  user-select: none;
  overflow: hidden;
  border: 1px solid transparent;
  transition: border-color 0.12s;
}
.seq-clip.active { border-color: var(--ui-btn); }
.seq-clip.dragging { opacity: 0.5; }
.seq-clip-thumb {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background: var(--surface) center/cover no-repeat;
  flex-shrink: 0;
  border: 1px solid var(--border);
}
.seq-clip-name {
  font-size: 11px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--text);
}
.seq-clip-dur {
  font-size: 11px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.seq-clip-resize-handle {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 8px;
  cursor: ew-resize;
  background: transparent;
}
.seq-clip-resize-handle:hover { background: rgba(0,0,0,0.06); }

.seq-playhead {
  position: absolute;
  top: 0; bottom: 0;
  width: 3px;
  background: var(--ui-btn);
  pointer-events: auto;
  cursor: ew-resize;
  z-index: 10;
  border-radius: 2px;
}
.seq-playhead:hover { box-shadow: 0 0 0 4px rgba(130, 130, 130, 0.2); }

/* ── Color Picker ── */
.cp-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.05);
  display: flex;
  align-items: center;
  justify-content: center;
}
.cp-popup {
  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;
  outline: none;
}
.cp-hex:focus { border-color: #999; }
.cp-ok {
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  background: var(--text);
  color: white;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.cp-ok:hover { background: #333; }

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

/* ── 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; }
}
