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

:root {
  --bg: #0a0a0b;
  --bg-surface: #121214;
  --bg-elevated: #1a1a1e;
  --bg-hover: #222226;
  --border: #1e1e22;
  --border-hover: #2a2a30;
  --text: #f4f4f5;
  --text-secondary: #8a8a92;
  --text-muted: #55555e;
  --accent: #a855f7;
  --accent-hover: #c084fc;
  --accent-muted: rgba(168, 85, 247, 0.15);
  --radius: 8px;
  --radius-sm: 6px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* App layout */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--bg);
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 52px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 50;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-logo {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  text-decoration: none;
}

.topbar-sep {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.topbar-title {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.topbar-center {
  position: relative;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  font-size: 0.8rem;
  font-weight: 500;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
  background: var(--bg-elevated);
  color: var(--text);
  white-space: nowrap;
}

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

.btn-sm {
  padding: 5px 12px;
  font-size: 0.75rem;
}

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

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-outline {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--accent);
}

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

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

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

/* Preset trigger/menu */
.preset-trigger {
  padding: 6px 14px;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

.preset-trigger:hover {
  border-color: var(--accent);
  background: var(--accent-muted);
}

.preset-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px;
  display: none;
  flex-direction: column;
  gap: 2px;
  min-width: 180px;
  z-index: 100;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.preset-menu.open {
  display: flex;
}

.preset-option {
  padding: 8px 12px;
  font-size: 0.8rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: background 0.1s;
}

.preset-option:hover {
  background: var(--bg-hover);
}

/* Editor layout */
.editor {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Preview */
.preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #050505;
  overflow: hidden;
}

.preview-empty {
  text-align: center;
  padding: 40px;
}

.preview-empty-icon {
  color: var(--text-muted);
  margin-bottom: 16px;
  opacity: 0.5;
}

.preview-empty h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.preview-empty p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

.upload-btn {
  display: inline-flex;
  padding: 10px 24px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  z-index: 8;
}

.upload-btn:hover {
  background: var(--accent-hover);
}

/* Small variant: shown in corner when video is loaded */
.upload-btn.small {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  font-size: 0.75rem;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255,255,255,0.1);
}

.upload-btn.small:hover {
  background: rgba(0,0,0,0.8);
}

.preview-canvas-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Viewport: wraps canvas + overlays, sizes to canvas */
.preview-viewport {
  position: relative;
  flex-shrink: 0;
  line-height: 0;
}

.text-overlay-wrap {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 5;
}

/* Allow clicks on text spans */
.text-overlay-wrap span {
  pointer-events: auto;
  cursor: move;
  user-select: none;
}

#previewCanvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
  object-fit: contain;
}

/* Selection box for interactive text */
.selection-box {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

/* Bounding box border drawn around selected text */
.selection-box.visible::before {
  content: '';
  position: absolute;
  inset: 0;
  border: 2px solid #a855f7;
  border-radius: 4px;
  pointer-events: none;
}

.sel-handle {
  position: absolute;
  pointer-events: auto;
  z-index: 12;
}

/* Visual square (small) + invisible hit area (large) */
.sel-handle::before {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: white;
  border: 2px solid #a855f7;
  border-radius: 2px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Hit area: 16x16 */
.sel-handle {
  width: 16px;
  height: 16px;
}
.sel-handle:hover::before { background: #a855f7; }

.sel-h-tl { top: -8px; left: -8px; cursor: nw-resize; }
.sel-h-tr { top: -8px; right: -8px; cursor: ne-resize; }
.sel-h-bl { bottom: -8px; left: -8px; cursor: sw-resize; }
.sel-h-br { bottom: -8px; right: -8px; cursor: se-resize; }
.sel-h-tc { top: -8px; left: 50%; margin-left: -8px; cursor: n-resize; }
.sel-h-bc { bottom: -8px; left: 50%; margin-left: -8px; cursor: s-resize; }
.sel-h-cl { top: 50%; left: -8px; margin-top: -8px; cursor: w-resize; }
.sel-h-cr { top: 50%; right: -8px; margin-top: -8px; cursor: e-resize; }

/* Rotation handle */
.sel-h-rotate {
  top: -32px;
  left: 50%;
  margin-left: -8px;
  width: 16px;
  height: 16px;
  cursor: grab;
}
.sel-h-rotate::before {
  width: 12px;
  height: 12px;
  background: #a855f7;
  border: none;
  border-radius: 50%;
}
.sel-h-rotate::after {
  content: '';
  position: absolute;
  top: -18px;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 18px;
  background: #a855f7;
  pointer-events: none;
}

/* Snap guidelines */
.snap-guide {
  position: absolute;
  z-index: 15;
  pointer-events: none;
}
.snap-guide-v {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
  background: #a855f7;
  box-shadow: 0 0 4px rgba(168,85,247,0.5);
}
.snap-guide-h {
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
  background: #a855f7;
  box-shadow: 0 0 4px rgba(168,85,247,0.5);
}

/* Safe zones overlay — 1080x1920 base */
.safe-zones {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  overflow: hidden;
}

/* Base zone style */
.sz {
  position: absolute;
  pointer-events: none;
  box-sizing: border-box;
}

.sz-label {
  position: absolute;
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  line-height: 1.3;
  background: rgba(0,0,0,0.65);
  padding: 2px 8px;
  border-radius: 4px;
  letter-spacing: 0.01em;
}

/* Danger: red */
.sz-danger {
  background: rgba(255, 50, 50, 0.08);
  border: 1px solid rgba(255, 80, 80, 0.25);
}
.sz-danger .sz-label {
  color: rgba(255, 120, 120, 0.6);
}

/* Safe: green */
.sz-safe {
  border: 1px dashed rgba(80, 200, 80, 0.2);
  background: rgba(80, 200, 80, 0.03);
}
.sz-safe .sz-label {
  color: rgba(80, 200, 80, 0.4);
}

/* Premium: yellow/gold */
.sz-premium {
  border: 2px dashed rgba(255, 200, 50, 0.3);
  background: rgba(255, 200, 50, 0.04);
}
.sz-premium .sz-label {
  color: rgba(255, 200, 50, 0.6);
  white-space: nowrap;
}

.preview-duration {
  position: absolute;
  bottom: 16px;
  right: 16px;
  padding: 4px 10px;
  background: rgba(0,0,0,0.7);
  border-radius: 100px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* Play/Pause button */
.play-btn {
  position: absolute;
  bottom: 16px;
  left: 16px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.2s, background 0.15s;
}

.preview-canvas-wrap:hover .play-btn,
.play-btn:focus-visible {
  opacity: 1;
}

.play-btn:hover {
  background: rgba(0,0,0,0.8);
}

.play-btn.playing {
  opacity: 1;
}

/* Volume control */
.volume-control {
  position: absolute;
  bottom: 16px;
  left: 58px;
  display: flex;
  align-items: center;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 10;
}
.preview-canvas-wrap:hover .volume-control,
.volume-control:hover {
  opacity: 1;
}

.vol-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.vol-btn:hover { background: rgba(0,0,0,0.8); }
.vol-btn.muted { opacity: 0.5; }

.vol-slider {
  width: 60px;
  height: 3px;
  -webkit-appearance: none;
  appearance: none;
  background: rgba(255,255,255,0.3);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
.vol-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  border: none;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  cursor: pointer;
}

/* Export overlay */
.export-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

/* Ensure hidden attribute works despite display:flex on overlays */
[hidden] {
  display: none !important;
}

.export-modal {
  text-align: center;
  max-width: 320px;
}

.export-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.export-modal h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 16px;
}

.export-progress {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}

.export-progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 0.3s;
}

.export-status {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

/* Export complete */
.export-complete {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.export-complete-inner {
  text-align: center;
  max-width: 360px;
  padding: 24px;
}

.export-complete-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.export-complete-inner h3 {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.export-complete-inner p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 20px;
  line-height: 1.6;
}

.export-complete-inner .btn {
  width: 100%;
  justify-content: center;
  margin-bottom: 8px;
}

/* Sidebar */
.sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
  padding-bottom: 16px;
}

.sidebar-row {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-row:last-of-type {
  border-bottom: none;
}

.sidebar-row-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sidebar-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Timeline — professional redesign */
.timeline-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.timeline-field {
  flex: 1;
}

.timeline-field-label {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.timeline-input {
  width: 100%;
  padding: 5px 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
}

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

.timeline-input-static {
  display: block;
  width: 100%;
  padding: 5px 8px;
  background: var(--bg-surface);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
}

/* Timeline container */
.timeline-container {
  margin: 8px 0 6px;
  user-select: none;
}

/* Ruler with time markers */
.timeline-ruler {
  position: relative;
  height: 20px;
  margin-bottom: 2px;
  overflow: hidden;
}

.timeline-ruler-mark {
  position: absolute;
  top: 0;
  width: 1px;
  height: 8px;
  background: var(--text-muted);
  opacity: 0.4;
}

.timeline-ruler-mark.major {
  height: 12px;
  opacity: 0.7;
}

.timeline-ruler-label {
  position: absolute;
  top: 12px;
  font-size: 0.6rem;
  color: var(--text-muted);
  transform: translateX(-50%);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* Rail / track */
.timeline-rail {
  position: relative;
  height: 28px;
  background: var(--bg-elevated);
  border-radius: 4px;
  cursor: pointer;
  overflow: hidden;
}

/* Selection window */
.timeline-selection {
  position: absolute;
  top: 0;
  height: 100%;
  background: rgba(168, 85, 247, 0.2);
  border: 1px solid var(--accent);
  border-radius: 3px;
  cursor: grab;
  z-index: 1;
  min-width: 4px;
  touch-action: none;
}

.timeline-selection:active {
  cursor: grabbing;
}

/* Selection handles */
.timeline-handle {
  position: absolute;
  top: 0;
  width: 12px;
  height: 100%;
  background: var(--accent);
  border: none;
  border-radius: 2px;
  cursor: ew-resize;
  z-index: 3;
  touch-action: none;
}

.timeline-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 14px;
  background: rgba(255,255,255,0.6);
  border-radius: 1px;
}

.timeline-handle-start {
  left: 0;
  transform: translateX(-50%);
}

.timeline-handle-end {
  right: 0;
  transform: translateX(50%);
}

/* Time indicators below the rail */
.timeline-time-indicators {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 0.65rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.timeline-duration {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  text-align: center;
  margin-top: 4px;
}

/* Text items */
.text-item {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  margin-bottom: 8px;
}

.text-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.text-item-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.text-item-remove {
  padding: 2px 6px;
  font-size: 0.7rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
}

.text-item-remove:hover {
  background: rgba(239, 68, 68, 0.15);
  color: #ef4444;
}

.text-item-input {
  width: 100%;
  padding: 5px 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  margin-bottom: 6px;
  resize: none;
  overflow-y: auto;
  min-height: 32px;
}

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

.text-item-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.text-item-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.text-item-row label {
  font-size: 0.7rem;
  color: var(--text-muted);
  min-width: 30px;
}

.text-item-row input[type="number"],
.text-item-row select {
  flex: 1;
  padding: 3px 6px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.75rem;
  font-family: inherit;
}

.text-item-row input[type="color"] {
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: none;
  cursor: pointer;
}

.text-item-row input:focus,
.text-item-row select:focus {
  outline: none;
  border-color: var(--accent);
}

/* Sliders */
.slider-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.slider-value {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

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

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  cursor: grab;
}

.slider::-webkit-slider-thumb:active {
  cursor: grabbing;
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Sidebar footer */
.sidebar-footer {
  margin-top: auto;
  padding: 16px;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  text-align: center;
}

.sidebar-footer p {
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.sidebar-footer-desc {
  font-size: 0.7rem !important;
  font-weight: 400 !important;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 12px !important;
}

.sidebar-footer .btn {
  width: 100%;
  justify-content: center;
}

/* Scrollbar */
.sidebar::-webkit-scrollbar {
  width: 4px;
}
.sidebar::-webkit-scrollbar-track {
  background: transparent;
}
.sidebar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* Responsive */
@media (max-width: 900px) {
  .sidebar {
    width: 260px;
  }
}

@media (max-width: 768px) {
  .editor {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    max-height: 50vh;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
