/* Tool pages (scan / merge) — loaded only on /scan and /merge */
.tool-page {
  min-height: 100vh;
  background:
    radial-gradient(900px 360px at 80% -5%, rgba(124, 92, 191, 0.12), transparent 55%),
    var(--paper);
}

.tool-page .site-header {
  position: sticky;
}

.scan-app {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.5rem 1.1rem 2.5rem;
}

.scan-hero {
  margin-bottom: 1.25rem;
}

.scan-hero h1 {
  margin: 0 0 0.55rem;
  font-size: clamp(1.55rem, 4.5vw, 2.1rem);
  letter-spacing: -0.035em;
  line-height: 1.15;
}

.scan-lede {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.scan-stage {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 1rem;
  box-shadow: 0 10px 30px rgba(42, 33, 64, 0.05);
}

.scan-step-hint {
  margin: 0 0 0.85rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.scan-drop {
  position: relative;
  min-height: 280px;
  border: 1.5px dashed rgba(109, 74, 173, 0.35);
  border-radius: var(--radius);
  background: linear-gradient(180deg, #fbf8ff, #f3eefb);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.scan-drop.is-dragover {
  border-color: var(--navy);
  background: #efe8fb;
}

.scan-drop-inner {
  display: grid;
  gap: 0.85rem;
  justify-items: center;
  text-align: center;
  padding: 1.5rem 1rem;
  max-width: 22rem;
  z-index: 1;
}

.scan-drop-icon {
  color: var(--navy);
  opacity: 0.85;
}

.scan-drop-inner p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.scan-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  justify-content: center;
}

.btn-touch {
  min-height: 48px;
  padding-inline: 1.15rem;
  font-size: 0.98rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

label.btn {
  margin: 0;
}

.scan-live,
.scan-live-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-live-overlay {
  pointer-events: none;
}

.crop-wrap,
.filter-preview-wrap {
  position: relative;
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
  background: #1a1524;
  touch-action: none;
  user-select: none;
}

#crop-canvas,
#filter-canvas {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  touch-action: none;
  cursor: crosshair;
}

#crop-canvas {
  /* Sized explicitly in JS to keep hit-testing aligned */
  width: auto;
  max-height: none;
}

.loupe {
  position: absolute;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  z-index: 5;
  image-rendering: auto;
}

.scan-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin-top: 0.95rem;
}

.scan-toolbar-stack {
  flex-direction: column;
}

.scan-toolbar-stack .btn {
  width: 100%;
}

.filter-toggles {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.45rem;
  margin-top: 0.85rem;
  overflow-x: auto;
  padding-bottom: 0.25rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.filter-chip {
  flex: 0 0 auto;
  min-height: 44px;
  min-width: max-content;
  padding: 0 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  font-weight: 650;
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.filter-chip.is-active {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
}

.edit-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.75rem;
}

.edit-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.7rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #f7f3ff;
  color: var(--ink);
  font-weight: 700;
  font-size: 0.84rem;
  cursor: pointer;
}

.edit-btn:hover,
.edit-btn:focus-visible {
  border-color: rgba(109, 74, 173, 0.45);
  outline: none;
}

.layout-orient {
  display: flex;
  gap: 0.45rem;
  margin-bottom: 0.75rem;
}

.layout-workspace {
  display: grid;
  grid-template-columns: 1fr 7.2rem;
  gap: 0.65rem;
  align-items: start;
}

.a4-stage {
  display: flex;
  justify-content: center;
  padding: 0.5rem;
  background: #2a2140;
  border-radius: 12px;
  overflow: auto;
  max-height: min(68vh, 720px);
}

.a4-sheet {
  position: relative;
  background: #fff;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
  touch-action: none;
  user-select: none;
  overflow: hidden;
}

.a4-sheet.is-landscape {
  /* sized in JS */
}

.layout-item {
  position: absolute;
  box-sizing: border-box;
  border: 2px solid transparent;
  cursor: grab;
  touch-action: none;
}

.layout-item img {
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  pointer-events: none;
  user-select: none;
}

.layout-item.is-selected {
  border-color: #6d4aad;
  box-shadow: 0 0 0 2px rgba(109, 74, 173, 0.25);
  z-index: 3;
}

.layout-item .resize-handle {
  position: absolute;
  right: -10px;
  bottom: -10px;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: #6d4aad;
  border: 2px solid #fff;
  cursor: nwse-resize;
  touch-action: none;
  z-index: 4;
}

.layout-tray {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  max-height: min(68vh, 720px);
  overflow: auto;
  padding: 0.15rem;
}

.tray-item {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 0.2rem;
  cursor: pointer;
  min-height: 48px;
}

.tray-item.is-on-sheet {
  opacity: 0.55;
}

.tray-item.is-active {
  border-color: var(--navy);
}

.tray-item img {
  width: 100%;
  display: block;
  border-radius: 4px;
}

.tray-item span {
  display: block;
  font-size: 0.68rem;
  font-weight: 700;
  text-align: center;
  margin-top: 0.2rem;
  color: var(--muted);
}

.layout-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  margin: 0.75rem 0 0.25rem;
  font-weight: 700;
  color: var(--ink);
}

.layout-pager .btn {
  min-width: 48px;
}

@media (max-width: 640px) {
  .layout-workspace {
    grid-template-columns: 1fr;
  }

  .layout-tray {
    flex-direction: row;
    max-height: none;
    overflow-x: auto;
  }

  .tray-item {
    flex: 0 0 4.8rem;
  }
}

.sr-file {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
  opacity: 0 !important;
}
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "label label"
    "input ext";
  gap: 0.35rem 0.4rem;
  align-items: center;
  margin: 0.85rem 0 0.35rem;
}

.filename-field > span:first-child {
  grid-area: label;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
}

.filename-field input {
  grid-area: input;
  min-height: 48px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem 0.75rem;
  font: inherit;
  font-weight: 650;
  color: var(--ink);
  background: #fff;
}

.filename-field input:focus {
  outline: 2px solid rgba(109, 74, 173, 0.35);
  border-color: var(--navy);
}

.filename-ext {
  grid-area: ext;
  font-weight: 750;
  color: var(--muted);
  font-size: 0.95rem;
}

.page-card {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f9f7fc;
  overflow: hidden;
  display: grid;
  grid-template-rows: 1fr auto;
}

.page-card img {
  width: 100%;
  aspect-ratio: 210 / 297;
  object-fit: cover;
  background: #fff;
  display: block;
}

.page-card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.25rem;
  padding: 0.4rem 0.45rem;
  font-size: 0.78rem;
  font-weight: 650;
}

.page-card-actions {
  display: flex;
  gap: 0.2rem;
}

.page-card-actions button {
  min-width: 36px;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
}

.page-card-actions button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.scan-privacy-note {
  margin: 1rem 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}

.scan-status {
  min-height: 1.4rem;
  margin: 0.85rem 0 0;
  text-align: center;
  color: var(--navy);
  font-weight: 650;
  font-size: 0.9rem;
}

.tool-footer {
  margin-top: 1rem;
}

.merge-list {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: grid;
  gap: 0.55rem;
}

.merge-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbf8ff;
  min-height: 52px;
}

.merge-item-index {
  flex: none;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 999px;
  background: rgba(109, 74, 173, 0.12);
  color: var(--navy);
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  font-weight: 750;
}

.merge-item-name {
  flex: 1;
  min-width: 0;
  font-weight: 650;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.merge-item-size {
  flex: none;
  color: var(--muted);
  font-size: 0.78rem;
}

.merge-item-actions {
  display: flex;
  gap: 0.25rem;
}

.merge-item-actions button {
  min-width: 40px;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

@media (max-width: 640px) {
  .scan-app {
    padding: 1rem 0.75rem 2rem;
  }

  .scan-stage {
    padding: 0.75rem;
    border-radius: 12px;
  }

  .filter-toggles {
    grid-template-columns: 1fr;
  }

  .tool-page .nav a:not(.btn) {
    display: none;
  }

  .tool-page .nav a[aria-current="page"],
  .tool-page .nav .btn-primary {
    display: inline-flex;
  }
}
