/* ── Editor Layout ── */
#editor {
  flex: 1;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.toolbar-sep {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
  flex-shrink: 0;
}

.page-nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.page-input {
  width: 48px;
  padding: 6px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
  text-align: center;
}

.page-input:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

.page-total {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.toolbar-right {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

.zoom-display {
  color: var(--text-muted);
  font-size: 0.82rem;
  min-width: 44px;
  text-align: center;
}

/* ── Editor body (sidebar + canvas) ── */
#editor-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Thumbnail panel ── */
#thumb-panel {
  width: 150px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition:
    box-shadow 0.2s,
    background 0.2s;
}

#thumb-panel.selecting {
  background: rgba(245, 158, 11, 0.05);
  box-shadow:
    inset -3px 0 0 0 #f59e0b,
    0 0 24px -6px rgba(245, 158, 11, 0.4);
}

.thumb {
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 4px;
  padding: 4px;
  text-align: center;
  transition: border-color 0.15s;
  background: var(--surface2);
  user-select: none;
}

.thumb:hover {
  border-color: var(--border);
}
.thumb.active {
  border-color: var(--accent);
}
.thumb.selected {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
}
.thumb canvas {
  display: block;
  width: 100%;
  border-radius: 2px;
  background: #fff;
}
.thumb span {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 3px;
  display: block;
}

/* ── Canvas Area ── */
#canvas-area {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 32px 24px;
  background: var(--bg);
}

#canvas-wrapper {
  position: relative;
  box-shadow: var(--shadow);
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  display: inline-block;
}

#pdf-canvas {
  display: block;
}

#annotation-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* ── Text annotations ── */
.text-annotation {
  position: absolute;
  pointer-events: all;
  cursor: move;
  user-select: none;
  touch-action: none;
}

.text-annotation span {
  display: block;
  padding: 3px 6px;
  background: rgba(255, 255, 0, 0.82);
  color: #000;
  font-size: 14px;
  font-family: sans-serif;
  border-radius: 3px;
  white-space: nowrap;
  border: 1px solid transparent;
}

.text-annotation:hover span {
  border-color: var(--accent);
}

.text-annotation .ann-del {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  border: none;
  line-height: 1;
}

.text-annotation:hover .ann-del {
  display: flex;
}

/* ── Image annotations ── */
.img-annotation {
  position: absolute;
  pointer-events: all;
  cursor: move;
  user-select: none;
  touch-action: none;
}

.img-annotation img {
  display: block;
  border: 2px solid transparent;
  border-radius: 2px;
}

.img-annotation:hover img {
  border-color: var(--accent);
}

.img-annotation .ann-del {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  border: none;
  line-height: 1;
}

.img-annotation:hover .ann-del {
  display: flex;
}

.ann-resize {
  position: absolute;
  bottom: -5px;
  right: -5px;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 3px;
  cursor: se-resize;
  display: none;
  touch-action: none;
}

.img-annotation:hover .ann-resize {
  display: block;
}

/* ── Extract active button ── */
.btn-ghost.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ── Success button ── */
.btn-success {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  background: var(--success);
  color: #fff;
  transition: opacity 0.15s;
}
.btn-success:hover {
  opacity: 0.88;
}

/* ── Form fields popup ── */
#form-fields-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 360px;
  overflow-y: auto;
  padding-right: 4px;
}

.form-field-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.form-field-row label {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.form-field-row.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.form-field-row input[type="checkbox"] {
  width: auto;
}

.form-fields-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 12px 0;
}

.form-flatten-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.form-flatten-row input {
  width: auto;
  margin: 0;
}

/* ── Watermark popup inputs ── */
#watermark-popup input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  margin-bottom: 10px;
  box-sizing: border-box;
}

/* Touch devices can't hover, so annotation controls that only appear
   on :hover would otherwise be permanently invisible on mobile. */
@media (hover: none) {
  .text-annotation .ann-del,
  .img-annotation .ann-del {
    display: flex;
    width: 22px;
    height: 22px;
    font-size: 13px;
  }
  .img-annotation .ann-resize {
    display: block;
    width: 20px;
    height: 20px;
  }
}

@media (max-width: 700px) {
  /* Toolbar becomes a single horizontally-scrolling row instead of
     wrapping to several rows, which ate too much vertical space and
     pushed the canvas down on phones. */
  .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 4px;
    padding: 8px 10px;
  }
  .toolbar::-webkit-scrollbar {
    display: none;
  }
  .toolbar-sep,
  .page-nav,
  .zoom-display,
  .btn-icon,
  .btn-ghost,
  .btn-primary,
  .btn-danger,
  .btn-success {
    flex-shrink: 0;
  }
  .toolbar-right {
    margin-left: 0;
  }

  /* Bigger touch targets */
  .btn-icon {
    padding: 10px;
  }
  .page-input {
    width: 40px;
    padding: 8px 6px;
  }

  /* Stack the thumbnail rail above the page instead of beside it, so
     the page itself keeps the full screen width to edit on. */
  #editor-body {
    flex-direction: column;
  }
  #thumb-panel {
    width: auto;
    height: 96px;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 6px 8px;
  }
  .thumb {
    flex-shrink: 0;
    width: 60px;
  }
  #thumb-panel.selecting {
    box-shadow:
      inset 0 -3px 0 0 #f59e0b,
      0 0 24px -6px rgba(245, 158, 11, 0.4);
  }

  #canvas-area {
    padding: 16px 8px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y pinch-zoom;
  }

  /* Popup rows: stack the label above its control so sliders and
     inputs aren't squeezed into a couple dozen pixels. */
  .popup-box {
    padding: 20px;
  }
  .popup-row {
    flex-wrap: wrap;
  }
  .popup-row label {
    width: 100%;
  }
  #watermark-popup input[type="range"] {
    flex: 1;
    width: auto !important;
    min-width: 0;
  }
}
