/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #111;
  --bg2:       #1a1a1a;
  --bg3:       #222;
  --border:    #333;
  --accent:    #ff6b35;
  --accent2:   #7a5ebe;
  --text:      #ddd;
  --text-dim:  #888;
  --panel-h:   44px;
  --timeline-h: 84px;
  --export-h:   62px;
  --status-h:   24px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font: 12px/1.4 system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  user-select: none;
}

/* ── Toolbar ─────────────────────────────────────────────────────────────── */
#toolbar {
  height: var(--panel-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 10px;
  flex-shrink: 0;
  z-index: 10;
}

.toolbar-left { display: flex; align-items: center; gap: 4px; flex: 1; }
.toolbar-right { display: flex; align-items: center; gap: 8px; }

.tool-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  transition: background 0.1s, border-color 0.1s;
}
.tool-btn svg { width: 14px; height: 14px; flex-shrink: 0; }
.tool-btn:hover { background: #2a2a2a; border-color: #555; }
.tool-btn.active { background: #2d2440; border-color: var(--accent2); color: #c8b8f0; }
.tool-btn:active { transform: translateY(1px); }

.separator { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }

.toolbar-label { color: var(--text-dim); font-size: 11px; }

/* ── Work area ───────────────────────────────────────────────────────────── */
#work-area {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  background: #0a0a0a;
  position: relative;
  overflow: hidden;
  min-height: 0;
  height: calc(100% - var(--panel-h) - var(--timeline-h) - var(--export-h) - var(--status-h));
}

/* ── Image panel ─────────────────────────────────────────────────────────── */
.image-panel {
  position: relative;
  overflow: hidden;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel-label {
  position: absolute;
  top: 8px;
  left: 10px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  pointer-events: none;
  z-index: 3;
}

/* Drop overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 5;
  cursor: pointer;
  transition: background 0.15s;
}
.drop-overlay.hidden { display: none; }
.drop-overlay.drag-over { background: #1e1826; }

.drop-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--text-dim);
  text-align: center;
  pointer-events: none;
}
.drop-message svg { opacity: 0.4; }
.drop-message small { font-size: 10px; opacity: 0.6; }

/* Canvases inside panels — centered, aspect-ratio preserved, no stretch */
.image-panel canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  display: block;
}

#overlay-src, #overlay-dst {
  z-index: 2;
  cursor: crosshair;
  pointer-events: none; /* enabled via JS when images loaded */
}

/* ── Morph preview overlay ───────────────────────────────────────────────── */
#morph-canvas-wrapper {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 20;
  grid-column: 1 / -1;
}
#morph-canvas-wrapper.active { display: flex; }

#morph-canvas {
  position: static !important;
  width: auto !important;
  height: 100% !important;
  max-width: 100%;
  object-fit: contain;
}

#btn-close-preview {
  position: absolute;
  top: 8px; right: 8px;
  width: 28px; height: 28px;
  background: rgba(0,0,0,0.6);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  z-index: 21;
}
#btn-close-preview svg { width: 14px; height: 14px; }
#btn-close-preview:hover { background: rgba(255,80,0,0.3); }

/* ── Timeline ────────────────────────────────────────────────────────────── */
#timeline-panel {
  height: var(--timeline-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  flex-shrink: 0;
  position: relative;
}

#timeline-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0 8px;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  width: 90px;
}

#timeline-controls button {
  width: 28px; height: 28px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#timeline-controls button svg { width: 12px; height: 12px; }
#timeline-controls button:hover { border-color: #555; background: #2a2a2a; }

#time-display {
  font-size: 10px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#timeline-track {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#timeline-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#playhead {
  position: absolute;
  top: 0; bottom: 0;
  width: 2px;
  background: var(--accent);
  pointer-events: none;
  left: 0;
  z-index: 5;
  box-shadow: 0 0 6px rgba(255,107,53,0.5);
}
#playhead::before {
  content: '';
  position: absolute;
  top: 0;
  left: -4px;
  width: 10px; height: 10px;
  background: var(--accent);
  clip-path: polygon(50% 100%, 0 0, 100% 0);
}

/* ── Export panel ────────────────────────────────────────────────────────── */
#export-panel {
  height: var(--export-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 12px;
  gap: 6px;
  flex-shrink: 0;
}

.export-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.export-row label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

.export-row input[type="number"] {
  width: 56px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  padding: 3px 6px;
  font-size: 12px;
}
.export-row input[type="number"]:focus { outline: none; border-color: var(--accent2); }

.export-row input[type="range"] {
  width: 80px;
  accent-color: var(--accent2);
}

.export-btn {
  margin-left: auto;
  padding: 6px 16px;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.1s;
}
.export-btn:hover { background: #ff8c5a; }
.export-btn:disabled { background: #554; cursor: default; opacity: 0.6; }

#export-progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
#export-progress { flex: 1; accent-color: var(--accent); height: 6px; }
#export-progress-label { font-size: 11px; color: var(--text-dim); width: 34px; }
#export-progress-row button {
  padding: 3px 10px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font-size: 11px;
  cursor: pointer;
}

/* ── Loupe ───────────────────────────────────────────────────────────────── */
#loupe {
  position: fixed;
  width: 192px;
  height: 192px;
  border-radius: 6px;
  border: 1.5px solid rgba(255,255,255,0.18);
  box-shadow: 0 4px 24px rgba(0,0,0,0.7), 0 0 0 1px rgba(0,0,0,0.5);
  pointer-events: none;
  z-index: 1000;
  display: none;
  overflow: hidden;
  background: #111;
}
#loupe.visible { display: block; }

#loupe canvas {
  position: static !important;
  width: 192px !important;
  height: 192px !important;
  transform: none !important;
  display: block;
}

/* ── Autosave indicator ──────────────────────────────────────────────────── */
#autosave-indicator {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #444;
  flex-shrink: 0;
  transition: background 0.3s;
}
#autosave-indicator.saving  { background: #f0a500; }
#autosave-indicator.saved   { background: #4caf50; }
#autosave-indicator.nosave  { background: #e05; }

/* ── Status bar ──────────────────────────────────────────────────────────── */
#status-bar {
  height: var(--status-h);
  background: #0d0d0d;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 12px;
  flex-shrink: 0;
}
#status-msg { font-size: 11px; color: var(--text-dim); flex: 1; }
#status-hint { font-size: 10px; color: #555; }

/* Body layout: toolbar + work area + timeline + export + status */
body {
  display: flex;
  flex-direction: column;
}

/* ── Control point dots ──────────────────────────────────────────────────── */
/* These are absolutely positioned inside .image-panel via JS */
.cp-dot {
  position: absolute;
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.8);
  background: var(--cp-color, #7a5ebe);
  transform: translate(-50%, -50%);
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
  font-weight: 700;
  color: rgba(255,255,255,0.9);
  pointer-events: all;
  z-index: 10;
  transition: box-shadow 0.1s;
}
.cp-dot:active { cursor: grabbing; }
.cp-dot.pending {
  border-color: #ffcc00;
  box-shadow: 0 0 8px rgba(255,204,0,0.7);
  animation: pulse 0.8s ease-in-out infinite alternate;
}
.cp-dot.selected {
  box-shadow: 0 0 0 3px rgba(255,255,255,0.4);
}

@keyframes pulse {
  from { box-shadow: 0 0 4px rgba(255,204,0,0.4); }
  to   { box-shadow: 0 0 12px rgba(255,204,0,0.9); }
}

/* ── Context menu ────────────────────────────────────────────────────────── */
.context-menu {
  position: fixed;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 0;
  min-width: 150px;
  z-index: 1000;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}
.context-item {
  padding: 6px 14px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text);
}
.context-item:hover { background: var(--bg3); }
#kf-ctx-easing-label { color: var(--text-dim); cursor: default; font-size: 10px; letter-spacing: 0.05em; }
#kf-ctx-easing-label:hover { background: none; }
.context-divider { height: 1px; background: var(--border); margin: 3px 0; }
.context-danger { color: #e05; }
.context-danger:hover { background: rgba(220,0,50,0.15); }
