@import url('https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@300;400;500;600;700&display=swap');

:root {
  --bg: #09090b;
  --surface: #18181b;
  --surface-elevated: rgba(255, 255, 255, 0.05);
  --accent: #4f46e5;
  --accent-hover: #6366f1;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --border: rgba(255, 255, 255, 0.1);
  --radius: 12px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --glow: 0 0 0 2px rgba(79, 70, 229, 0.55), 0 0 18px rgba(79, 70, 229, 0.35);
  --sidebar-width: 280px;
  --touch: 44px;
  --font: 'Hanken Grotesk', sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-weight: 400;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(ellipse 80% 50% at 20% -10%, rgba(79, 70, 229, 0.18), transparent 55%),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(79, 70, 229, 0.08), transparent 50%),
    var(--bg);
}

button,
input {
  font-family: inherit;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Custom scrollbars */
*::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 8px;
}

*::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.22);
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: 1fr;
  height: 100dvh;
  max-height: 100dvh;
  gap: 12px;
  padding: 12px;
}

/* Sidebar */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-height: 0;
  box-shadow: var(--shadow);
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

.brand {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.sidebar-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.status-text {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  overflow-y: auto;
  min-height: 0;
  padding-right: 4px;
  -webkit-overflow-scrolling: touch;
}

.thumb-item {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-elevated);
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s ease;
  touch-action: manipulation;
}

.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.thumb-item:hover,
.thumb-item:focus-visible {
  border-color: var(--accent);
  outline: none;
  transform: translateY(-1px);
}

.thumb-item.in-timeline {
  box-shadow: var(--glow);
  border-color: var(--accent);
}

.thumb-label {
  position: absolute;
  left: 6px;
  bottom: 6px;
  max-width: calc(100% - 12px);
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(9, 9, 11, 0.75);
  color: var(--muted);
  font-size: 0.65rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Main column */
.main {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 12px;
  min-width: 0;
  min-height: 0;
}

.preview-panel,
.timeline-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 0;
}

.preview-panel {
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.03), transparent 40%),
    #0c0c10;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

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

.preview-empty {
  position: absolute;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  pointer-events: none;
  text-align: center;
  padding: 0 16px;
}

.preview-empty.hidden {
  display: none;
}

.playback-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

.control-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.fps-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 500;
}

.fps-control input {
  width: 64px;
  min-height: var(--touch);
  padding: 0 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  transition: all 0.2s ease;
}

.fps-control input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--glow);
}

.scrub-control input[type="range"] {
  width: 100%;
  height: 28px;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.scrub-control input[type="range"]:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.scrub-control input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--accent) 0%,
    var(--accent) var(--scrub-pct, 0%),
    rgba(255, 255, 255, 0.12) var(--scrub-pct, 0%),
    rgba(255, 255, 255, 0.12) 100%
  );
}

.scrub-control input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 20px;
  height: 20px;
  margin-top: -7px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.5);
  transition: all 0.2s ease;
}

.scrub-control input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
}

.scrub-control input[type="range"]::-moz-range-progress {
  height: 6px;
  border-radius: 999px;
  background: var(--accent);
}

.scrub-control input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--touch);
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-elevated);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  touch-action: manipulation;
  user-select: none;
}

.btn:hover:not(:disabled) {
  opacity: 0.9;
  border-color: rgba(255, 255, 255, 0.2);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

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

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

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  opacity: 1;
}

.btn-secondary {
  width: 100%;
}

.btn-icon {
  width: var(--touch);
  min-width: var(--touch);
  padding: 0;
  font-size: 0.85rem;
}

.upload-btn {
  cursor: pointer;
}

/* Timeline */
.timeline-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px 16px;
}

.timeline-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.timeline-header h2 {
  margin: 0 0 4px;
  font-size: 1.05rem;
  font-weight: 600;
}

.export-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.timeline {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 148px;
  padding: 6px 2px 10px;
  -webkit-overflow-scrolling: touch;
}

.timeline-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.timeline-empty.hidden {
  display: none;
}

.timeline-item {
  position: relative;
  flex: 0 0 140px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: all 0.2s ease;
  touch-action: none;
  cursor: grab;
}

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

.timeline-item.active {
  border-color: var(--accent);
  box-shadow: var(--glow);
}

.timeline-item.sortable-ghost {
  opacity: 0.4;
}

.timeline-item.sortable-chosen {
  box-shadow: var(--shadow);
}

.timeline-thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 8px;
  overflow: hidden;
  background: #0c0c10;
}

.timeline-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.order-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.duration-row {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.75rem;
}

.duration-row input {
  width: 52px;
  min-height: 36px;
  padding: 0 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  transition: all 0.2s ease;
}

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

.btn-remove {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(9, 9, 11, 0.85);
  color: var(--text);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 2;
}

.btn-remove:hover {
  background: #dc2626;
  border-color: #dc2626;
}

/* iPad / tablet */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 240px;
  }

  .app {
    padding: 8px;
    gap: 8px;
  }

  .thumb-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    max-height: none;
    min-height: 100dvh;
  }

  .sidebar {
    max-height: 40dvh;
  }

  .main {
    min-height: 60dvh;
  }

  .fps-control {
    margin-left: 0;
  }
}
