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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3250;
  --primary: #6366f1;
  --primary-hover: #4f52d4;
  --primary-light: rgba(99, 102, 241, 0.15);
  --text: #e8eaf0;
  --muted: #7b82a8;
  --success: #22c55e;
  --danger: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

html {
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  padding: 1.5rem 1rem 4rem;
  overflow-x: hidden;
}

/* ── App Shell ────────────────────────────────────────────── */
.app {
  width: 100%;
  max-width: 700px;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  min-width: 0;
}

/* ── Header ───────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  padding: 0.5rem 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
}

.logo svg {
  width: 28px;
  height: 28px;
  color: var(--primary);
}

/* ── Tab Nav ──────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  min-height: 44px;
}

.tab svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.tab:hover {
  color: var(--text);
  background: var(--surface2);
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

/* ── Panel ────────────────────────────────────────────────── */
.panel {
  display: none;
  flex-direction: column;
  gap: 1.25rem;
}

.panel.active {
  display: flex;
}

.panel-header h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.panel-header p {
  color: var(--muted);
  font-size: 0.875rem;
}

/* ── Drop Zone ────────────────────────────────────────────── */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface);
}

.drop-zone:hover,
.drop-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
}

.drop-zone svg {
  width: 40px;
  height: 40px;
  color: var(--primary);
  margin-bottom: 0.5rem;
}

.drop-zone p {
  font-size: 0.95rem;
  color: var(--text);
}

.drop-zone span {
  font-size: 0.8rem;
  color: var(--muted);
}

.link {
  color: var(--primary);
  cursor: pointer;
  text-decoration: underline;
}

/* ── File List (Merge) ────────────────────────────────────── */
.file-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: grab;
  transition: all 0.15s;
  user-select: none;
}

.file-item:hover {
  border-color: var(--primary);
}

.file-item.dragging {
  opacity: 0.4;
  cursor: grabbing;
}

.file-item.drag-target {
  border-color: var(--primary);
  background: var(--primary-light);
}

.drag-handle {
  color: var(--muted);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 8px 6px;
  touch-action: none;
}

.drag-handle span {
  display: block;
  width: 16px;
  height: 2px;
  background: currentColor;
  border-radius: 2px;
}

.file-item-icon {
  color: var(--primary);
  flex-shrink: 0;
}

.file-item-icon svg {
  width: 20px;
  height: 20px;
}

.file-item-info {
  flex: 1;
  min-width: 0;
}

.file-item-name {
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-item-size {
  font-size: 0.75rem;
  color: var(--muted);
}

.file-item-order {
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--surface2);
  border-radius: 4px;
  padding: 2px 6px;
}

/* ── File Info (Split/Compress) ───────────────────────────── */
.file-info {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
}

.file-info-inner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.file-info-inner svg {
  width: 20px;
  height: 20px;
  color: var(--primary);
  flex-shrink: 0;
}

.file-info-inner>div {
  flex: 1;
  min-width: 0;
}

.file-info-inner strong {
  display: block;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-info-inner span {
  font-size: 0.75rem;
  color: var(--muted);
}

/* ── Buttons ──────────────────────────────────────────────── */
.actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.7rem 1.4rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn svg {
  width: 16px;
  height: 16px;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

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

.btn-ghost {
  background: var(--surface2);
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--muted);
}

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

.btn-icon {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 8px 10px;
  border-radius: 4px;
  font-size: 0.85rem;
  line-height: 1;
  transition: all 0.15s;
  flex-shrink: 0;
  min-width: 36px;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

/* ── Progress ─────────────────────────────────────────────── */
.progress-container {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.progress-bar {
  height: 6px;
  background: var(--surface2);
  border-radius: 99px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 99px;
  width: 0%;
  transition: width 0.3s ease;
}

.progress-container span {
  font-size: 0.8rem;
  color: var(--muted);
}

/* ── Split Options ────────────────────────────────────────── */
.split-options {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.option-group {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  cursor: pointer;
  font-size: 0.9rem;
  min-height: 32px;
}

.radio-label input[type="radio"] {
  accent-color: var(--primary);
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.range-input {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  background: var(--surface2);
  border-radius: var(--radius-sm);
}

.input-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.range-input label {
  font-size: 0.85rem;
  color: var(--muted);
}

.range-input small {
  font-size: 0.75rem;
  color: var(--muted);
}

input[type="number"],
input[type="text"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.45rem 0.75rem;
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.2s;
}

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

input[type="number"] {
  width: 75px;
}

input[type="text"] {
  width: 100%;
}

/* ── Page Picker ──────────────────────────────────────────── */
.page-picker {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.page-picker-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* Range row */
.picker-range-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  background: var(--surface2);
}

.picker-range-label {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.picker-range-sep {
  color: var(--muted);
  font-size: 0.9rem;
}

.picker-range-row input[type="number"] {
  width: 64px;
  padding: 0.35rem 0.6rem;
  font-size: 0.825rem;
}

.btn-range-apply {
  background: var(--primary);
  border: none;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.35rem 0.85rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

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

.range-error {
  font-size: 0.775rem;
  color: var(--danger);
}

.selection-count {
  font-size: 0.825rem;
  color: var(--muted);
  font-weight: 500;
}

.picker-actions {
  display: flex;
  gap: 0.5rem;
}

.btn-text {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.825rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  transition: background 0.15s;
}

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

/* Loading state */
.thumbnails-loading {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.5rem 1rem;
  color: var(--muted);
  font-size: 0.875rem;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

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

/* Thumbnail grid */
.thumbnail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.75rem;
  padding: 1rem;
  max-height: 420px;
  overflow-y: auto;
}

.thumbnail-grid::-webkit-scrollbar {
  width: 6px;
}

.thumbnail-grid::-webkit-scrollbar-track {
  background: transparent;
}

.thumbnail-grid::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 99px;
}

.thumb-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
}

.thumb-canvas-wrap {
  position: relative;
  width: 100%;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  transition: border-color 0.15s, box-shadow 0.15s;
  background: #fff;
  aspect-ratio: 3/4;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb-item:hover .thumb-canvas-wrap {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.thumb-item.selected .thumb-canvas-wrap {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

/* The rendered canvas fills the wrap */
.thumb-canvas-wrap canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* Skeleton placeholder while rendering */
.thumb-skeleton {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--surface2) 25%, var(--border) 50%, var(--surface2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer {
  from {
    background-position: 200% 0;
  }

  to {
    background-position: -200% 0;
  }
}

/* Checkmark overlay */
.thumb-check {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 20px;
  height: 20px;
  background: var(--primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 0.15s, transform 0.15s;
  color: #fff;
}

.thumb-check svg {
  width: 11px;
  height: 11px;
}

.thumb-item.selected .thumb-check {
  opacity: 1;
  transform: scale(1);
}

/* Dim unselected pages when something is selected */
.thumbnail-grid.has-selection .thumb-item:not(.selected) .thumb-canvas-wrap {
  opacity: 0.45;
}

.thumb-label {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1;
}

.thumb-item.selected .thumb-label {
  color: var(--primary);
  font-weight: 600;
}

/* ── Compress Options ─────────────────────────────────────── */
.compress-options {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.option-label {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

.level-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

.level-card {
  cursor: pointer;
  position: relative;
}

.level-card input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.card-inner {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding: 0.9rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  transition: all 0.2s;
  text-align: center;
}

.card-inner strong {
  font-size: 0.9rem;
}

.card-inner span {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.3;
}

.level-card input:checked+.card-inner,
.level-card.selected .card-inner {
  border-color: var(--primary);
  background: var(--primary-light);
}

.level-card:hover .card-inner {
  border-color: var(--primary);
}

/* ── Result Card ──────────────────────────────────────────── */
.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.25rem;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
}

.result-row:last-child {
  border-bottom: none;
}

.result-row.highlight {
  background: rgba(34, 197, 94, 0.08);
}

.result-row.highlight strong {
  color: var(--success);
}

.result-row span {
  color: var(--muted);
}

/* ── Toast ────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 999;
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.875rem;
  min-width: 260px;
  max-width: 360px;
  animation: slideIn 0.25s ease;
}

.toast.success {
  border-left: 3px solid var(--success);
}

.toast.error {
  border-left: 3px solid var(--danger);
}

.toast-icon {
  font-size: 1.1rem;
  flex-shrink: 0;
}

.toast-msg {
  flex: 1;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }

  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
    transform: translateY(4px);
  }
}

.toast.removing {
  animation: fadeOut 0.3s ease forwards;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 600px) {
  body {
    padding: 1rem 0.75rem 3rem;
  }

  .app {
    gap: 1rem;
  }

  .tabs {
    justify-content: flex-start;
  }

  .tab {
    flex: 0 0 auto;
    padding: 0.65rem 0.85rem;
  }

  .tab span {
    display: none;
  }

  .tab svg {
    width: 19px;
    height: 19px;
  }

  .panel-header h2 {
    font-size: 1.1rem;
  }

  .panel-header p {
    font-size: 0.825rem;
  }

  .drop-zone {
    padding: 1.75rem 1rem;
  }

  .drop-zone svg {
    width: 32px;
    height: 32px;
  }

  .actions {
    flex-wrap: wrap;
  }

  .btn {
    flex: 1 1 auto;
    justify-content: center;
    min-height: 44px;
    padding: 0.7rem 1rem;
  }

  .level-cards {
    grid-template-columns: 1fr;
  }

  /* File list rows: let name/size wrap instead of being crushed */
  .file-item {
    padding: 0.65rem 0.75rem;
    gap: 0.6rem;
  }

  .file-item-order {
    display: none;
  }

  /* Page picker toolbar / range row stack cleanly */
  .page-picker-toolbar {
    flex-direction: column;
    align-items: flex-start;
  }

  .picker-actions {
    width: 100%;
  }

  .btn-text {
    flex: 1;
    text-align: center;
    padding: 0.5rem;
  }

  .picker-range-row {
    gap: 0.4rem;
  }

  .picker-range-row input[type="number"] {
    width: 56px;
  }

  /* Smaller thumbnails so 2-3 fit comfortably per row */
  .thumbnail-grid {
    grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
    gap: 0.5rem;
    padding: 0.75rem;
    max-height: 360px;
  }

  .thumb-delete-btn {
    width: 24px;
    height: 24px;
    font-size: 13px;
  }

  .file-info-inner {
    flex-wrap: wrap;
  }

  /* Toasts: full-width banner anchored to top, easier to read/dismiss on mobile */
  .toast-container {
    left: 0.75rem;
    right: 0.75rem;
    bottom: auto;
    top: 1rem;
    align-items: stretch;
  }

  .toast {
    min-width: 0;
    max-width: none;
    width: 100%;
  }
}

@media (max-width: 380px) {
  .thumbnail-grid {
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  }

  .picker-range-row input[type="number"] {
    width: 48px;
  }
}

/* Styling additions for interactive structural page manipulation */
.rearrangement-grid .thumb-item {
  cursor: grab;
  position: relative;
  touch-action: none;
}

.rearrangement-grid .thumb-item.dragging {
  opacity: 0.4;
  border: 2px dashed #3b82f6;
}

.rearrangement-grid .thumb-item.drag-target {
  transform: scale(1.05);
  box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
}

.thumb-delete-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(239, 68, 68, 0.85);
  color: white;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: background 0.2s ease;
}

.thumb-delete-btn:hover {
  background: rgb(220, 38, 38);
}