:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --panel: #f8fafc;
  --border: #e2e8f0;
  --border-hover: #cbd5e1;
  --accent: #3E3EF4;
  --accent-light: #eaebff;
  --accent-mid: #5850ec;
  --accent-dashed: #a3abf4;
  --text: #0f172a;
  --text-sec: #475569;
  --text-dim: #94a3b8;
  --danger: #ef4444;
  --success: #10b981;
  --success-bg: #ecfdf5;
  --font: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --radius: 12px;
  --toolbar-h: 64px;
}

.pdf-editor-container {
  position: relative;
  height: 85vh;
  min-height: 800px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  border-radius: 16px;
  margin-bottom: 40px;
  box-shadow: 0 20px 40px -15px rgba(0, 0, 0, 0.05);
  border: 1px solid var(--border);
  width: 100%;
}

.pdf-editor-container *,
.pdf-editor-container *::before,
.pdf-editor-container *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Toolbar ── */
#toolbar {
  height: var(--toolbar-h);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  z-index: 10;
  gap: 16px;
}

.toolbar-left,
.toolbar-center,
.toolbar-right {
  display: flex;
  align-items: center;
}

.toolbar-left {
  gap: 8px;
  flex-shrink: 0;
}

.toolbar-center {
  gap: 10px;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}

.toolbar-center::-webkit-scrollbar {
  display: none;
}

.toolbar-right {
  gap: 8px;
  flex-shrink: 0;
}

.brand {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.015em;
}

.tsep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 8px;
  flex-shrink: 0;
}

.tbtn {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-sec);
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.tbtn:hover {
  background: var(--accent-light);
  color: var(--accent);
  border-color: var(--accent-dashed);
}

.tbtn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: 0 4px 12px rgba(62, 62, 244, 0.2);
}

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

.tbtn svg {
  width: 15px;
  height: 15px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.tool-clear-btn {
  color: var(--danger);
  border-color: var(--danger);
}

.tool-clear-btn:hover {
  background: #fee2e2;
  color: var(--danger);
  border-color: var(--danger);
}

.tool-clear-btn svg {
  stroke: var(--danger);
}

.open-btn {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  border: none;
  height: 38px;
  padding: 0 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
  box-shadow: 0 4px 10px rgba(62, 62, 244, 0.15);
}

.open-btn:hover {
  background: #2b2be0;
  transform: translateY(-1px);
  box-shadow: 0 6px 14px rgba(62, 62, 244, 0.2);
}

.open-btn:active {
  transform: translateY(0);
}

.open-btn svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.2;
}

.finish-btn {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  border: none;
  height: 38px;
  padding: 0 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
  box-shadow: 0 4px 10px rgba(62, 62, 244, 0.15);
}

.finish-btn:hover {
  background: #2b2be0;
  transform: translateY(-1px);
  box-shadow: 0 6px 14px rgba(62, 62, 244, 0.2);
}

.finish-btn:active {
  transform: translateY(0);
}

.finish-btn svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.2;
}

/* ── Main ── */
#main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ── Viewer ── */
#viewer {
  flex: 1;
  overflow: auto;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 0 64px;
  gap: 24px;
  position: relative;
  width: 100%;
}

#viewer::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

#viewer::-webkit-scrollbar-thumb {
  background: var(--border-hover);
  border-radius: 4px;
}

.page-wrap {
  position: relative;
  flex-shrink: 0;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.04);
  user-select: none;
  transition: box-shadow 0.3s ease;
  max-width: 100% !important;
}

.page-wrap:hover {
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.08);
}

.page-wrap canvas {
  display: block;
  border-radius: 8px;
  max-width: 100% !important;
  height: auto !important;
}

.annotation-layer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  pointer-events: none;
}

.annotation-layer .placed-el,
.annotation-layer .placed-text {
  pointer-events: auto;
}

/* ── Placement mode ── */
#viewer.placing {
  cursor: crosshair;
}

#viewer.placing .page-wrap {
  outline: 2px dashed var(--accent);
  outline-offset: 4px;
}

#placementPreview {
  position: absolute;
  pointer-events: none;
  z-index: 500;
  opacity: 0.7;
  transform: translate(-10px, -10px);
  border: 2px dashed var(--accent);
  border-radius: 6px;
  background: var(--accent-light);
  padding: 6px 12px;
  display: none;
  white-space: nowrap;
  max-width: 220px;
}

#placementPreview img {
  display: block;
  max-width: 200px;
  max-height: 70px;
  object-fit: contain;
}

#placementPreview .preview-label {
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  padding: 2px 0;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#placementBanner {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 12px 24px;
  border-radius: 100px;
  font-size: 13px;
  font-weight: 600;
  z-index: 500;
  display: none;
  align-items: center;
  gap: 16px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  white-space: nowrap;
}

#placementBanner button {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  border-radius: 100px;
  padding: 6px 16px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font);
  transition: background 0.2s;
}

#placementBanner button:hover {
  background: rgba(255, 255, 255, 0.35);
}

/* ── Placed element ── */
.placed-el {
  position: absolute;
  cursor: move;
  border: 2px solid transparent;
  border-radius: 6px;
  transition: border-color 0.15s;
  user-select: none;
}

.placed-el:hover {
  border-color: var(--accent-mid);
}

.placed-el.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(62, 62, 244, 0.15);
}

.placed-el img {
  display: block;
  max-width: 100%;
  pointer-events: none;
}

.placed-el .del-handle {
  position: absolute;
  top: -12px;
  right: -12px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  border: 2px solid #fff;
  font-weight: 700;
  line-height: 1;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.placed-el.selected .del-handle {
  display: flex;
}

.placed-el .resize-handle {
  position: absolute;
  bottom: -6px;
  right: -6px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  cursor: se-resize;
  display: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.placed-el.selected .resize-handle {
  display: block;
}

.placed-text {
  position: absolute;
  cursor: move;
  border: 2px solid transparent;
  border-radius: 6px;
  transition: border-color 0.15s;
  user-select: none;
  font-family: var(--font);
  font-size: 16px;
  color: #1a1a1a;
  white-space: nowrap;
  padding: 4px 8px;
  min-width: 40px;
  min-height: 24px;
  display: flex;
  align-items: center;
}

.placed-text:hover {
  border-color: var(--accent-mid);
}

.placed-text.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(62, 62, 244, 0.15);
}

.placed-text .del-handle {
  position: absolute;
  top: -12px;
  right: -12px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  border: 2px solid #fff;
  font-weight: 700;
  line-height: 1;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.placed-text.selected .del-handle {
  display: flex;
}

/* ── Modal ── */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.esign-modal {
  background: var(--surface);
  border-radius: 20px;
  width: 540px;
  max-width: 95vw;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.25);
  transform: scale(0.95) translateY(10px);
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  border: 1px solid var(--border);
}

.modal-overlay.open .esign-modal {
  transform: scale(1) translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
  flex-shrink: 0;
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.01em;
}

.modal-close {
  background: var(--bg-gray);
  border: none;
  cursor: pointer;
  color: var(--text-sec);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-size: 18px;
  transition: all 0.2s ease;
}

.modal-close:hover {
  background: #fee2e2;
  color: var(--danger);
}

.modal-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  margin-top: 16px;
  flex-shrink: 0;
}

.mtab {
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-sec);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s ease;
}

.mtab:hover {
  color: var(--accent);
}

.mtab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

/* Draw tab */
#drawCanvas {
  width: 100%;
  height: 200px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  cursor: crosshair;
  display: block;
  background: #fff;
  touch-action: none;
}

.canvas-actions {
  display: flex;
  gap: 12px;
  margin-top: 14px;
  align-items: center;
}

.clr-btn {
  padding: 8px 16px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sec);
  cursor: pointer;
  transition: all 0.2s ease;
}

.clr-btn:hover {
  background: var(--panel);
  border-color: var(--border-hover);
}

.pen-size-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-sec);
  margin-left: auto;
}

input[type="range"] {
  accent-color: var(--accent);
}

/* Type tab */
.type-input {
  width: 100%;
  padding: 12px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  font-family: var(--font);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s ease;
  margin-bottom: 16px;
}

.type-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(62, 62, 244, 0.15);
}

.font-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.font-opt {
  padding: 10px 18px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 18px;
  color: var(--text);
  background: var(--panel);
}

.font-opt.selected {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.type-preview {
  width: 100%;
  height: 90px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #1a1a1a;
  overflow: hidden;
  padding: 12px;
}

/* Upload tab */
.upload-sig-area {
  border: 2px dashed var(--accent-dashed);
  border-radius: var(--radius);
  padding: 48px 24px;
  text-align: center;
  cursor: pointer;
  background: var(--panel);
  transition: all 0.2s ease;
  margin-bottom: 16px;
}

.upload-sig-area:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.upload-sig-text {
  font-size: 14px;
  color: var(--text-sec);
  line-height: 1.5;
}

#sigPreviewImg {
  width: 100%;
  max-height: 110px;
  object-fit: contain;
  display: none;
  border-radius: 8px;
  margin-top: 12px;
}

/* Color picker */
.color-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 12px;
}

.color-dot {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  border: 2.5px solid #fff;
  box-shadow: 0 0 0 1px var(--border);
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.color-dot:hover {
  transform: scale(1.1);
}

.color-dot.selected {
  box-shadow: 0 0 0 2px var(--accent);
}

/* Modal footer */
.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-cancel {
  padding: 10px 20px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sec);
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-cancel:hover {
  background: var(--panel);
  border-color: var(--border-hover);
}

.btn-apply {
  padding: 10px 22px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 10px rgba(62, 62, 244, 0.15);
}

.btn-apply:hover {
  background: #2b2be0;
  box-shadow: 0 6px 14px rgba(62, 62, 244, 0.2);
}

/* Text modal */
#textModal .type-input {
  margin-bottom: 0;
}

.placed-date {
  font-size: 14px;
  color: #1a1a1a;
}

/* ── Toast ── */
#toast {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px 24px;
  border-radius: 100px;
  font-size: 13px;
  color: var(--text-sec);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 300;
  white-space: nowrap;
}

#toast.show {
  opacity: 1;
}

/* ── Loading bar ── */
#loadingBar {
  position: absolute;
  top: 0;
  left: 0;
  height: 4px;
  background: var(--accent);
  width: 0%;
  z-index: 400;
  transition: width 0.3s ease;
  opacity: 0;
  border-radius: 0 2px 2px 0;
}

/* Page number badge */
.page-badge {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 8px;
  pointer-events: none;
  font-weight: 600;
}

.pdf-editor-container * {
  scrollbar-width: thin;
  scrollbar-color: var(--border-hover) transparent;
}

/* ── Responsive Styling ── */
@media (max-width: 768px) {
  #toolbar {
    flex-direction: column;
    height: auto;
    padding: 8px 12px;
    gap: 8px;
  }

  .toolbar-left {
    width: 100%;
    justify-content: space-between;
  }

  .toolbar-center {
    width: 100%;
    overflow-x: auto;
    justify-content: flex-start;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }

  .toolbar-right {
    width: 100%;
    justify-content: flex-end;
  }

  .tbtn {
    padding: 0 10px;
    height: 34px;
  }
}

@media (max-width: 576px) {
  #toolbar {
    padding: 8px 8px;
  }

  .brand {
    font-size: 14px;
  }

  .tbtn {
    font-size: 12px;
    padding: 0 8px;
  }

  .open-btn,
  .finish-btn {
    padding: 0 12px;
    font-size: 12px;
    height: 34px;
  }
}

@media (max-width: 1300px) {
  .appscms-left-sidebar,
  .appscms-right-sidebar {
    display: none !important;
  }
}