/* ============ Design tokens ============ */
:root {
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e2e5ef;
  --text: #1c2033;
  --text-2: #5b6178;
  --text-3: #9297ad;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --shadow-sm: 0 1px 2px rgba(20, 24, 44, 0.06);
  --shadow-md: 0 4px 16px rgba(20, 24, 44, 0.10);
  --shadow-lg: 0 12px 40px rgba(20, 24, 44, 0.16);
  --radius: 10px;
  --radius-lg: 16px;
  --header-h: 58px;
  --checker: repeating-conic-gradient(#e8eaf2 0% 25%, #f8f9fc 0% 50%) 0 0 / 20px 20px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #818cf8;
    --accent-hover: #a5b0fc;
    --accent-soft: #27273f;
    --bg: #12141d;
    --surface: #1a1d29;
    --surface-2: #222634;
    --border: #2e3345;
    --text: #e8eaf2;
    --text-2: #a6abc0;
    --text-3: #6c7189;
    --danger: #f87171;
    --danger-soft: #2c1a1c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.55);
    --checker: repeating-conic-gradient(#232635 0% 25%, #1a1d29 0% 50%) 0 0 / 20px 20px;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ============ Header ============ */
.app-header {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 26px;
  height: 26px;
  color: var(--accent);
}

.brand h1 {
  font-size: 19px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.brand h1 span { color: var(--accent); font-weight: 500; }

.header-meta {
  display: flex;
  align-items: center;
  gap: 14px;
}

.privacy-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-2);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 999px;
  cursor: default;
}

.privacy-badge svg { width: 14px; height: 14px; color: var(--success); }

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s, transform 0.05s;
  user-select: none;
  white-space: nowrap;
}

.btn svg { width: 16px; height: 16px; flex: 0 0 auto; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
@media (prefers-color-scheme: dark) {
  .btn-primary { color: #12141d; }
}

.btn-secondary {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent);
}
.btn-secondary:hover:not(:disabled) { background: var(--accent-soft); filter: brightness(0.96); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover:not(:disabled) { color: var(--text); background: var(--surface-2); }

.btn-icon { padding: 8px; }
.btn-text { padding: 8px 10px; min-width: 56px; font-variant-numeric: tabular-nums; }

/* ============ Upload screen ============ */
.screen-upload {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  overflow: auto;
}

.dropzone {
  width: min(640px, 100%);
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  padding: 56px 32px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, transform 0.2s, box-shadow 0.2s;
  outline: none;
}

.dropzone:hover, .dropzone:focus-visible {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}

.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: scale(1.015);
  box-shadow: var(--shadow-lg);
}

.dz-icon {
  width: 68px;
  height: 68px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s;
}

.dropzone.dragover .dz-icon { transform: translateY(-4px) scale(1.08); }
.dz-icon svg { width: 32px; height: 32px; }

.dropzone h2 {
  margin: 0 0 6px;
  font-size: 21px;
  letter-spacing: -0.02em;
}

.dropzone p { margin: 0; color: var(--text-2); font-size: 14.5px; }
.dropzone code { background: var(--surface-2); padding: 1px 6px; border-radius: 5px; font-size: 13px; }
.dz-browse { color: var(--accent); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }

.dz-features {
  list-style: none;
  margin: 26px auto 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 10px;
}

.dz-features li {
  font-size: 12.5px;
  color: var(--text-2);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
}

.upload-error {
  margin-top: 18px;
  width: min(640px, 100%);
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 14px;
  line-height: 1.45;
}

/* ============ Loading overlay ============ */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.loading-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 30px 40px;
  text-align: center;
  min-width: 300px;
}

.loading-card p { margin: 16px 0 14px; color: var(--text-2); font-size: 14px; }

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

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

.progress-track {
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.2s ease;
}

/* ============ Viewer layout ============ */
.screen-viewer {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* --- Sidebar --- */
.sidebar {
  width: 232px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  min-height: 0;
}

.sidebar-head {
  padding: 12px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.file-name {
  font-size: 13px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-info { font-size: 11.5px; color: var(--text-3); }

.sidebar-select {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-2);
}

.checkbox-label input { accent-color: var(--accent); cursor: pointer; }

.selection-count { font-size: 11.5px; color: var(--text-3); font-variant-numeric: tabular-nums; }

.thumb-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.thumb-item {
  position: relative;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.15s, box-shadow 0.15s;
  flex: 0 0 auto;
}

.thumb-item:hover { border-color: var(--text-3); }
.thumb-item.active { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }

.thumb-canvas-wrap {
  height: 118px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--checker);
}

.thumb-canvas-wrap canvas { max-width: 100%; max-height: 100%; display: block; }

.thumb-placeholder {
  width: 22px; height: 22px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.thumb-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 9px;
  font-size: 11.5px;
  color: var(--text-2);
  background: var(--surface);
}

.thumb-label .thumb-page { font-weight: 650; color: var(--text); }
.thumb-label .thumb-size { color: var(--text-3); font-size: 10.5px; }

.thumb-check {
  position: absolute;
  top: 7px;
  left: 7px;
  z-index: 2;
  width: 17px;
  height: 17px;
  accent-color: var(--accent);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s;
}

.thumb-item:hover .thumb-check,
.thumb-check:checked,
.thumb-item.active .thumb-check { opacity: 1; }

/* --- Viewer column --- */
.viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 9px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.toolbar-group { display: flex; align-items: center; gap: 5px; }

.page-indicator {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 13.5px;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
}

.page-indicator input {
  width: 52px;
  font: inherit;
  font-weight: 600;
  text-align: center;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 4px;
  -moz-appearance: textfield;
  appearance: textfield;
}

.page-indicator input::-webkit-outer-spin-button,
.page-indicator input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.page-indicator input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.page-meta {
  margin-left: auto;
  font-size: 12.5px;
  color: var(--text-3);
  font-variant-numeric: tabular-nums;
}

.viewport {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  background: var(--checker);
  cursor: grab;
  touch-action: none;
}

.viewport.panning { cursor: grabbing; }

#page-canvas {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  box-shadow: var(--shadow-md);
  background: #fff;
  image-rendering: auto;
}

#page-canvas.pixelated { image-rendering: pixelated; }

/* --- Export bar --- */
.export-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.export-group { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.export-group > label { font-size: 12.5px; font-weight: 600; color: var(--text-2); }

.export-group select {
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
}

.export-group select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.quality-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-2);
}

.quality-wrap label { font-weight: 600; }
.quality-wrap input[type="range"] { width: 110px; accent-color: var(--accent); }
.quality-wrap #quality-value { min-width: 22px; font-variant-numeric: tabular-nums; text-align: right; }

/* ============ Toasts ============ */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 11px 16px;
  border-radius: var(--radius);
  font-size: 13.5px;
  font-weight: 550;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.25s ease;
  max-width: 340px;
}

.toast.error { background: var(--danger); color: #fff; }
.toast.leaving { animation: toast-out 0.25s ease forwards; }

@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* ============ Responsive ============ */
@media (max-width: 760px) {
  .screen-viewer { flex-direction: column-reverse; }

  .sidebar {
    width: 100%;
    height: 168px;
    flex-direction: column;
    border-right: none;
    border-top: 1px solid var(--border);
  }

  .sidebar-head { display: none; }

  .thumb-list {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    align-items: stretch;
  }

  .thumb-item { width: 108px; flex: 0 0 auto; }
  .thumb-canvas-wrap { height: 74px; }

  .privacy-badge { display: none; }
  .page-meta { display: none; }
  .toolbar { gap: 8px; }
  .export-bar { justify-content: center; }
}
