/* ===================== RESET & BASE ===================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --bg3: #0f3460;
  --surface: #1e2a4a;
  --surface2: #243354;
  --border: #2d4270;
  --accent: #4a90d9;
  --accent2: #5ba3f5;
  --success: #27ae60;
  --danger: #e74c3c;
  --warn: #f39c12;
  --text: #e8eaf6;
  --text2: #9bb5d6;
  --text3: #6a8ab5;
  --shadow: rgba(0,0,0,0.4);
  --topbar-h: 52px;
  --sidebar-w: 200px;
  --radius: 6px;
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  display: flex;
  flex-direction: column;
}

/* ===================== TOPBAR ===================== */
.topbar {
  height: var(--topbar-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 6px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px var(--shadow);
  z-index: 100;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  color: var(--accent2);
  margin-right: 12px;
  white-space: nowrap;
}

.topbar-brand i { font-size: 20px; color: var(--danger); }

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  flex-wrap: wrap;
}

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

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

.page-info {
  font-size: 12px;
  color: var(--text2);
  white-space: nowrap;
}

/* ===================== BUTTONS ===================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  transition: all .15s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface2); border-color: var(--accent); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn:disabled:hover { background: var(--surface); border-color: var(--border); }

.btn.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.btn-primary:hover { background: var(--accent2); border-color: var(--accent2); }

.btn.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn.btn-success:hover { filter: brightness(1.1); }

.btn.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.btn-danger:hover { filter: brightness(1.1); }

.btn.btn-sm { padding: 3px 7px; font-size: 11px; }
.btn.btn-lg { padding: 10px 20px; font-size: 14px; }
.btn.icon-btn { padding: 5px 8px; }

.btn-x {
  background: none;
  border: none;
  color: var(--text2);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}
.btn-x:hover { color: var(--danger); }

.full-w { width: 100%; justify-content: center; }
.mt-4 { margin-top: 4px; }

/* ===================== MAIN LAYOUT ===================== */
.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===================== SIDEBAR ===================== */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.tool-section {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

.section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--text3);
  margin-bottom: 8px;
  padding-left: 2px;
}

/* ===================== TOOL BUTTONS ===================== */
.tool-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 10px;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text2);
  cursor: pointer;
  font-size: 12px;
  transition: all .15s;
  margin-bottom: 2px;
}
.tool-btn:hover { background: var(--surface); color: var(--text); }
.tool-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.tool-btn i { width: 16px; text-align: center; font-size: 13px; }

/* ===================== PROPERTIES ===================== */
.prop-row {
  margin-bottom: 10px;
}
.prop-row label {
  display: block;
  font-size: 11px;
  color: var(--text3);
  margin-bottom: 4px;
}
.prop-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}
.prop-row select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 6px;
  border-radius: var(--radius);
  font-size: 12px;
  cursor: pointer;
}

.color-row { display: flex; align-items: center; gap: 6px; }
input[type="color"] {
  width: 32px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  padding: 1px;
  background: var(--surface);
  flex-shrink: 0;
}

.color-presets {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.color-preset {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color .15s;
}
.color-preset:hover, .color-preset.selected { border-color: #fff; }

.style-row { display: flex; gap: 4px; }
.style-btn {
  flex: 1;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text2);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  text-align: center;
  transition: all .15s;
}
.style-btn:hover { background: var(--surface2); color: var(--text); }
.style-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Signature preview */
#sig-preview-box {
  margin-top: 8px;
}
#sig-preview-img {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: white;
  margin-bottom: 4px;
}

.hint-text {
  font-size: 10px;
  color: var(--text3);
  line-height: 1.4;
  margin-top: 6px;
}
.hint-text.center { text-align: center; }

/* ===================== WORKSPACE ===================== */
.workspace {
  flex: 1;
  overflow: auto;
  background: var(--bg);
  position: relative;
}

.workspace::-webkit-scrollbar { width: 8px; height: 8px; }
.workspace::-webkit-scrollbar-track { background: var(--bg2); }
.workspace::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ===================== DROP ZONE ===================== */
.drop-zone {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - var(--topbar-h));
}
.drop-zone.drag-over { background: rgba(74,144,217,.1); outline: 2px dashed var(--accent); }
.drop-zone-inner {
  text-align: center;
  padding: 60px 40px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  max-width: 400px;
}
.drop-zone-inner i {
  font-size: 64px;
  color: var(--danger);
  margin-bottom: 16px;
}
.drop-zone-inner h2 { font-size: 22px; margin-bottom: 8px; color: var(--text); }
.drop-zone-inner p { color: var(--text3); margin-bottom: 20px; }

/* ===================== PAGES CONTAINER ===================== */
#pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  gap: 24px;
}

/* ===================== PAGE WRAPPER ===================== */
.page-wrapper {
  position: relative;
  box-shadow: 0 4px 20px var(--shadow);
  background: white;
  cursor: crosshair;
}

.page-wrapper.tool-select { cursor: default; }
.page-wrapper.tool-text { cursor: text; }
.page-wrapper.tool-draw { cursor: crosshair; }
.page-wrapper.tool-highlight { cursor: crosshair; }
.page-wrapper.tool-signature { cursor: copy; }
.page-wrapper.tool-eraser { cursor: none; }

.pdf-canvas {
  display: block;
  position: relative;
}

.annot-canvas {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
}

.html-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
}

/* page number label */
.page-label {
  position: absolute;
  top: -22px;
  left: 0;
  font-size: 11px;
  color: var(--text3);
  background: var(--bg2);
  padding: 2px 8px;
  border-radius: var(--radius) var(--radius) 0 0;
}

/* ===================== TEXT ANNOTATION ===================== */
.text-annot {
  position: absolute;
  background: transparent;
  border: 1px dashed rgba(74,144,217,.6);
  border-radius: 2px;
  cursor: move;
  min-width: 60px;
  min-height: 20px;
  user-select: none;
}
.text-annot:hover { border-color: var(--accent); }
.text-annot.selected { border: 1.5px solid var(--accent); }

.text-annot-input {
  width: 100%;
  min-width: 80px;
  background: transparent;
  border: none;
  outline: none;
  cursor: text;
  resize: none;
  font-family: inherit;
  padding: 2px 4px;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
}

.annot-del-btn {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 18px;
  height: 18px;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 11px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
  z-index: 10;
}
.text-annot:hover .annot-del-btn,
.text-annot.selected .annot-del-btn {
  display: flex;
}

/* ===================== SIGNATURE ANNOTATION ===================== */
.sig-annot {
  position: absolute;
  cursor: move;
  border: 1px dashed rgba(74,144,217,.5);
  user-select: none;
}
.sig-annot:hover { border-color: var(--accent); }
.sig-annot.selected { border: 1.5px solid var(--accent); }

.sig-annot img {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.sig-resize-handle {
  position: absolute;
  bottom: -5px;
  right: -5px;
  width: 12px;
  height: 12px;
  background: var(--accent);
  border-radius: 50%;
  cursor: se-resize;
  z-index: 5;
}

/* ===================== ERASER CURSOR ===================== */
.eraser-cursor {
  position: fixed;
  border: 2px solid rgba(255,255,255,.8);
  border-radius: 50%;
  pointer-events: none;
  z-index: 9999;
  transform: translate(-50%, -50%);
  display: none;
}

/* ===================== LOADING ===================== */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(26,26,46,.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 200;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay p { color: var(--text2); font-size: 14px; }

/* ===================== MODALS ===================== */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 620px;
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}
.modal.modal-wide { max-width: 700px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.modal-header h3 i { color: var(--accent); }

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

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.modal-footer > div { display: flex; gap: 8px; }

/* ===================== SIGNATURE CANVAS ===================== */
#sig-canvas {
  display: block;
  width: 100%;
  background: white;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: crosshair;
  touch-action: none;
}

/* ===================== MERGE ===================== */
.merge-drop {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  cursor: pointer;
  color: var(--text3);
  margin-bottom: 12px;
  transition: all .15s;
}
.merge-drop:hover, .merge-drop.drag-over {
  border-color: var(--accent);
  background: rgba(74,144,217,.05);
  color: var(--text2);
}
.merge-drop i { margin-bottom: 8px; color: var(--accent); }
.merge-drop p { font-size: 13px; }

.merge-list { display: flex; flex-direction: column; gap: 6px; }

.merge-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.merge-item-name {
  flex: 1;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.merge-item-pages {
  font-size: 11px;
  color: var(--text3);
  white-space: nowrap;
}
.merge-item-btns { display: flex; gap: 3px; }
.merge-item-btn {
  width: 24px;
  height: 24px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text2);
  cursor: pointer;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.merge-item-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.merge-item-btn.del:hover { background: var(--danger); border-color: var(--danger); }

/* ===================== TOAST ===================== */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  box-shadow: 0 4px 12px var(--shadow);
  animation: slideIn .2s ease;
  max-width: 280px;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ===================== SCROLLBAR STYLING ===================== */
.modal-body::-webkit-scrollbar { width: 4px; }
.modal-body::-webkit-scrollbar-track { background: transparent; }
.modal-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#pages-container::-webkit-scrollbar { width: 8px; }
