:root {
  --bg: #fafafa;
  --surface: #fff;
  --surface-raised: #fff;
  --text: #111;
  --text-secondary: #555;
  --text-tertiary: #888;
  --border: #e2e2e2;
  --border-focus: #111;
  --input-bg: #fff;

  --btn-primary-bg: #111;
  --btn-primary-text: #fff;
  --btn-primary-hover: #333;
  --btn-secondary-bg: #f0f0f0;
  --btn-secondary-text: #333;
  --btn-secondary-hover: #e4e4e4;
  --btn-ghost-hover: rgba(0, 0, 0, 0.06);

  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --success-bg: #f0fdf4;
  --success-text: #15803d;

  --track-bg: #eee;
  --range-bg: rgba(0, 0, 0, 0.08);
  --range-border: #111;

  --lightbox-bg: rgba(0, 0, 0, 0.92);
  --lightbox-surface: rgba(255, 255, 255, 0.08);
  --lightbox-text: #fff;
  --lightbox-text-secondary: rgba(255, 255, 255, 0.6);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 9999px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0a;
    --surface: #161616;
    --surface-raised: #1c1c1c;
    --text: #ededed;
    --text-secondary: #a0a0a0;
    --text-tertiary: #666;
    --border: #282828;
    --border-focus: #ededed;
    --input-bg: #161616;

    --btn-primary-bg: #ededed;
    --btn-primary-text: #0a0a0a;
    --btn-primary-hover: #fff;
    --btn-secondary-bg: #1c1c1c;
    --btn-secondary-text: #a0a0a0;
    --btn-secondary-hover: #262626;
    --btn-ghost-hover: rgba(255, 255, 255, 0.06);

    --error-bg: #1c1111;
    --error-text: #f87171;
    --success-bg: #111c14;
    --success-text: #4ade80;

    --track-bg: #1c1c1c;
    --range-bg: rgba(255, 255, 255, 0.08);
    --range-border: #ededed;

    --lightbox-bg: rgba(0, 0, 0, 0.95);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 2rem 2rem 4rem;
  -webkit-font-smoothing: antialiased;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

/* ── Header ── */

header {
  max-width: 520px;
  margin: 0 auto 1.5rem;
  text-align: center;
}

h1 {
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.875rem;
  margin-top: 0.25rem;
  margin-bottom: 1.25rem;
}

/* ── Form ── */

form {
  display: flex;
  gap: 0.5rem;
}

input[type="url"] {
  flex: 1;
  padding: 0.625rem 0.875rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.875rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}

input[type="url"]::placeholder {
  color: var(--text-tertiary);
}

input[type="url"]:focus {
  border-color: var(--border-focus);
}

/* ── Buttons ── */

.btn {
  padding: 0.625rem 1rem;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  white-space: nowrap;
  transition: background 0.15s ease, opacity 0.15s ease;
  -webkit-user-select: none;
  user-select: none;
}

.btn:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

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

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

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

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

.btn-secondary:hover:not(:disabled) {
  background: var(--btn-secondary-hover);
}

.btn-block {
  width: 100%;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.5s linear infinite;
}

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

/* ── Status ── */

#status {
  margin-top: 0.75rem;
  padding: 0.625rem 0.875rem;
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  text-align: left;
}

#status.error {
  background: var(--error-bg);
  color: var(--error-text);
}

#status.success {
  background: var(--success-bg);
  color: var(--success-text);
}

/* ── Content grid (two-column) ── */

#content-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  align-items: start;
}

#col-video,
#col-thumb {
  min-width: 0;
}

#video-player {
  width: 100%;
  max-height: 50vh;
  border-radius: var(--radius-lg);
  background: #000;
  display: block;
  object-fit: contain;
}

#col-video .btn {
  margin-top: 0.375rem;
}

/* ── Thumbnail ── */

#thumbnail-card {
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: #000;
  border: 1px solid var(--border);
  max-height: 50vh;
}

#thumb-img {
  width: 100%;
  max-height: 50vh;
  display: block;
  object-fit: contain;
}

#col-thumb .btn {
  margin-top: 0.375rem;
}

/* ── Timeline section ── */

#timeline-section {
  margin-top: 1.25rem;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.875rem;
}

#timeline-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.625rem;
}

.section-label {
  font-weight: 600;
  font-size: 0.8125rem;
  letter-spacing: -0.01em;
}

#frame-count-wrap {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.8125rem;
}

#frame-count-wrap label {
  color: var(--text-secondary);
}

input[type="number"] {
  width: 48px;
  padding: 0.25rem 0.375rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.8125rem;
  font-family: inherit;
  text-align: center;
  outline: none;
  transition: border-color 0.15s ease;
}

input[type="number"]:focus {
  border-color: var(--border-focus);
}

#extract-btn {
  padding: 0.3rem 0.625rem;
  font-size: 0.75rem;
}

/* ── Timeline track ── */

#timeline {
  margin-bottom: 0.375rem;
}

#timeline-track {
  position: relative;
  height: 40px;
  background: var(--track-bg);
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  touch-action: none;
}

#timeline-filmstrip {
  display: flex;
  height: 100%;
  pointer-events: none;
}

.filmstrip-thumb {
  height: 100%;
  flex: 1;
  object-fit: cover;
}

/* Dim overlays outside the selection */

.timeline-dim {
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 1;
  pointer-events: none;
}

#dim-left {
  left: 0;
}

#dim-right {
  right: 0;
}

/* Selected range tint */

#range-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(59, 130, 246, 0.15);
  border-top: 2px solid rgba(59, 130, 246, 0.8);
  border-bottom: 2px solid rgba(59, 130, 246, 0.8);
  z-index: 2;
  pointer-events: none;
}

/* Handles */

.timeline-handle {
  position: absolute;
  top: -1px;
  bottom: -1px;
  width: 8px;
  margin-left: -4px;
  background: rgb(59, 130, 246);
  border-radius: 3px;
  z-index: 3;
  cursor: ew-resize;
  touch-action: none;
  transition: transform 0.1s ease;
}

.timeline-handle:hover,
.timeline-handle:active {
  transform: scaleX(1.4);
}

.timeline-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 10px;
  background: #fff;
  border-radius: 1px;
  opacity: 0.7;
}

#timeline-times {
  display: flex;
  justify-content: space-between;
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-tertiary);
  margin-top: 0.25rem;
}

/* ── Frames grid ── */

#frames-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.375rem;
  margin-top: 0.625rem;
}

#frames-grid:empty {
  display: none;
}

.frame-item {
  position: relative;
  cursor: pointer;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #000;
}

.frame-item::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border: 1px solid rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

.frame-item:hover img {
  opacity: 0.85;
}

.frame-item.active {
  outline: 2px solid var(--range-border);
  outline-offset: 1px;
}

.frame-item img {
  width: 100%;
  display: block;
  transition: opacity 0.15s ease;
}

.frame-time {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 2px 0;
  font-size: 0.5625rem;
  font-variant-numeric: tabular-nums;
  color: rgba(255, 255, 255, 0.85);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
  text-align: center;
  pointer-events: none;
}

/* ── Lightbox ── */

#frame-panel {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
}

#frame-panel.open {
  display: flex;
}

#frame-panel-backdrop {
  position: absolute;
  inset: 0;
  background: var(--lightbox-bg);
}

#frame-panel-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: min(90vw, 800px);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 3.5rem;
}

#frame-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.75rem 0;
  color: var(--lightbox-text);
  font-size: 0.8125rem;
  gap: 0.75rem;
}

#frame-panel-counter {
  color: var(--lightbox-text-secondary);
  font-variant-numeric: tabular-nums;
  font-size: 0.75rem;
}

#frame-panel-actions {
  display: flex;
  gap: 0.375rem;
  margin-left: auto;
}

#frame-panel-actions .btn {
  font-size: 0.75rem;
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius-full);
}

#frame-panel-download {
  background: #fff;
  color: #000;
}

#frame-panel-download:hover {
  background: #e4e4e4;
}

#frame-panel-close {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

#frame-panel-close:hover {
  background: rgba(255, 255, 255, 0.16);
}

#frame-panel-img {
  max-width: 100%;
  max-height: calc(90vh - 60px);
  border-radius: var(--radius-md);
  object-fit: contain;
  display: block;
}

/* Lightbox arrows */

.lightbox-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-full);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 0;
  cursor: pointer;
  transition: background 0.15s ease;
  -webkit-user-select: none;
  user-select: none;
}

.lightbox-arrow:hover {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-arrow:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.lightbox-arrow:disabled {
  opacity: 0.2;
  cursor: default;
}

.lightbox-arrow:disabled:hover {
  background: rgba(255, 255, 255, 0.1);
}

.lightbox-arrow svg {
  width: 20px;
  height: 20px;
}

#arrow-prev {
  left: max(0.75rem, calc(50% - min(45vw, 400px) - 3rem));
}

#arrow-next {
  right: max(0.75rem, calc(50% - min(45vw, 400px) - 3rem));
}

/* ── Responsive ── */

@media (max-width: 700px) {
  body {
    padding: 1.5rem 0.75rem 3rem;
  }

  #content-grid {
    grid-template-columns: 1fr;
    gap: 0.625rem;
  }

  #video-player,
  #thumbnail-card,
  #thumb-img {
    max-height: 40vh;
  }

  #frames-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  #timeline-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.375rem;
  }

  #frame-panel-content {
    padding: 0 0.5rem;
  }

  #arrow-prev { left: 0.5rem; }
  #arrow-next { right: 0.5rem; }

  .lightbox-arrow {
    width: 36px;
    height: 36px;
  }

  .lightbox-arrow svg {
    width: 16px;
    height: 16px;
  }
}
