*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  color: #1a1a1a;
  background: #f0f2f5;
  min-height: 100vh;
}

/* ── Header ── */
.app-header {
  background: #1a1a2e;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 20px;
  height: 52px;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.app-header .logo { font-size: 16px; font-weight: 700; letter-spacing: .3px; }
.app-header .logo span { color: #6ec6ff; }
.header-spacer { flex: 1; }
.header-model {
  font-size: 11px; color: #a0c8ff; background: rgba(110,198,255,.12);
  border: 1px solid rgba(110,198,255,.3); border-radius: 10px;
  padding: 2px 9px; letter-spacing: .3px; white-space: nowrap;
}
.model-short { display: none; }
.header-user {
  font-size: 13px; color: #ccc;
  max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.header-link {
  color: #6ec6ff; text-decoration: none; font-size: 13px; padding: 4px 10px;
  border: 1px solid #6ec6ff; border-radius: 4px;
}
.header-link:hover { background: rgba(110,198,255,.15); }
.header-link-disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }
.btn-logout {
  background: transparent; border: 1px solid #888; color: #ccc;
  padding: 4px 12px; border-radius: 4px; cursor: pointer; font-size: 13px;
}
.btn-logout:hover { border-color: #fff; color: #fff; }
.btn-guide {
  background: transparent; border: 1px solid #888; color: #ccc;
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
  font-size: 14px; font-weight: 700; line-height: 1; flex-shrink: 0;
}
.btn-guide:hover { border-color: #fff; color: #fff; }

/* ── User Guide Modal ── */
.guide-modal-box {
  width: clamp(320px, calc(100vw - 32px), 680px);
  max-height: 88vh;
  display: flex; flex-direction: column;
  padding: 0; overflow: hidden;
}
.guide-modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid #e5e7eb; flex-shrink: 0;
}
.guide-modal-title { font-size: 16px; font-weight: 700; color: #111; }
.guide-modal-body {
  padding: 20px 22px; overflow-y: auto; flex: 1; font-size: 14px; color: #333; line-height: 1.6;
}
.lang-toggle { display: flex; gap: 4px; }
.lang-btn {
  background: transparent; border: 1px solid #d1d5db; color: #555;
  padding: 3px 10px; border-radius: 4px; cursor: pointer; font-size: 12px; font-weight: 500;
}
.lang-btn.active { background: #1d4ed8; border-color: #1d4ed8; color: #fff; }
.modal-x-btn {
  background: transparent; border: none; font-size: 20px; line-height: 1;
  cursor: pointer; color: #888; padding: 0 4px;
}
.modal-x-btn:hover { color: #111; }
.guide-intro { margin-bottom: 14px; color: #444; }
.guide-divider { border: none; border-top: 1px solid #e5e7eb; margin: 18px 0; }
.guide-section-title { font-weight: 700; font-size: 13px; color: #111; margin-bottom: 12px; text-transform: uppercase; letter-spacing: .04em; }
.guide-case {
  background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px;
  padding: 14px 16px; margin-bottom: 14px;
}
.guide-case strong { display: block; margin-bottom: 8px; font-size: 14px; color: #111; }
.guide-tag {
  display: inline-block; background: #1d4ed8; color: #fff;
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px;
  margin-bottom: 6px; letter-spacing: .03em;
}
.guide-steps { margin: 6px 0 0 18px; padding: 0; }
.guide-steps li { margin-bottom: 7px; }
.guide-steps code { background: #e9edf2; padding: 1px 5px; border-radius: 3px; font-size: 12px; }
.guide-tips { margin: 6px 0 0 18px; padding: 0; }
.guide-tips li { margin-bottom: 9px; }
.guide-tips a { color: #1d4ed8; }
.guide-tips code { background: #e9edf2; padding: 1px 5px; border-radius: 3px; font-size: 12px; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 5px; border: none;
  font-size: 13px; font-weight: 500; cursor: pointer; transition: opacity .15s;
}
.btn:hover { opacity: .85; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: #0078d4; color: #fff; }
.btn-secondary { background: #e0e0e0; color: #333; }
.btn-success { background: #107c10; color: #fff; }
.btn-danger { background: #d13438; color: #fff; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { background: none; border: none; cursor: pointer; font-size: 18px; line-height: 1; padding: 2px 6px; }
.btn-icon-label { padding: 5px 10px; min-width: 32px; justify-content: center; }

/* ── Forms ── */
input[type="text"], input[type="email"], input[type="number"], select, textarea {
  border: 1px solid #ccc; border-radius: 4px; padding: 6px 10px;
  font-size: 13px; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: #0078d4; }
label { font-weight: 500; display: block; margin-bottom: 4px; }

/* ── Cards ── */
.card {
  background: #fff; border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08); padding: 20px;
}

/* ── Login page ── */
.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 60%, #0f3460 100%);
}
.login-box {
  background: #fff; border-radius: 12px; padding: 40px;
  width: 360px; text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.3);
}
.login-box .logo-big { font-size: 28px; font-weight: 800; color: #1a1a2e; margin-bottom: 4px; }
.login-box .logo-big span { color: #0078d4; }
.login-box .subtitle { color: #666; font-size: 13px; margin-bottom: 30px; }
.btn-ms {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 12px; border-radius: 6px; font-size: 15px; font-weight: 600;
  background: #0078d4; color: #fff; border: none; cursor: pointer;
  text-decoration: none; transition: background .15s;
}
.btn-ms:hover { background: #005fa3; }
.login-footer { margin-top: 24px; font-size: 12px; color: #999; }

/* ── Admin page ── */
.admin-tabs-bar {
  max-width: 900px;
  margin: 28px auto 0;
  padding: 0 20px;
  border-bottom: 2px solid #e0e0e0;
  display: flex;
}
.admin-tab-btn {
  padding: 10px 22px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  border-radius: 4px 4px 0 0;
  transition: color 0.15s, border-color 0.15s;
  font-family: inherit;
}
.admin-tab-btn:hover { color: #1a1a2e; }
.admin-tab-btn.admin-tab-active { color: #0078d4; border-bottom-color: #0078d4; font-weight: 600; }
.admin-layout { max-width: 900px; margin: 0 auto; padding: 24px 20px 40px; display: flex; flex-direction: column; gap: 24px; }
.section-title { font-size: 16px; font-weight: 700; margin-bottom: 14px; color: #1a1a2e; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; padding: 8px 12px; background: #f5f5f5; font-size: 12px; color: #666; text-transform: uppercase; border-bottom: 2px solid #e0e0e0; }
td { padding: 10px 12px; border-bottom: 1px solid #f0f0f0; vertical-align: middle; }
tr:last-child td { border-bottom: none; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge-green { background: #dff6dd; color: #107c10; }
.badge-gray { background: #f0f0f0; color: #666; }
.badge-blue { background: #dce9f9; color: #0078d4; }
.add-user-form { display: flex; gap: 10px; align-items: flex-end; }
.add-user-form > div { flex: 1; }
.chop-section { display: flex; gap: 20px; align-items: flex-start; }
.chop-preview {
  border: 1px dashed #ccc; border-radius: 6px; padding: 10px;
  /* checkerboard so transparent PNG areas are visible */
  background-color: #fff;
  background-image: repeating-conic-gradient(#e0e0e0 0% 25%, #fff 0% 50%);
  background-size: 16px 16px;
  min-width: 160px; min-height: 100px; display: flex; align-items: center; justify-content: center;
}
.chop-preview img { max-width: 140px; max-height: 100px; object-fit: contain; }
.chop-preview .no-chop { color: #999; font-size: 12px; }
.chop-upload { display: flex; flex-direction: column; gap: 8px; }

/* ── Editor page ── */
.editor-layout { display: flex; flex-direction: column; height: calc(100vh - 52px); overflow: hidden; }

.toolbar {
  background: #fff; border-bottom: 1px solid #e0e0e0;
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  flex-wrap: nowrap; overflow: hidden; min-height: 52px;
}
.toolbar-sep { width: 1px; height: 28px; background: #e0e0e0; margin: 0 4px; }

/* Signature thumbnail button — replaces text label with the saved signature image */
.sig-thumb-mode { padding: 2px 8px !important; min-height: 30px; }
/* Gear settings button shown alongside the sig thumbnail */
.sig-settings-btn {
  padding: 4px 7px !important;
  font-size: 15px !important;
  line-height: 1 !important;
  min-width: 0 !important;
  color: #666;
}
.sig-btn-thumb {
  height: 26px; max-width: 100px; object-fit: contain; display: block;
  /* checkerboard so transparent strokes show */
  background-image: repeating-conic-gradient(#eee 0% 25%, #fff 0% 50%);
  background-size: 8px 8px;
}

/* ── Signature slot cards (in the sig modal) ── */
.sig-slot-card {
  flex: 1;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  padding: 8px 6px 6px;
  cursor: pointer;
  text-align: center;
  min-height: 72px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: border-color 0.15s, background 0.15s;
  background: #fafafa;
  user-select: none;
}
.sig-slot-card:hover { border-color: #aaa; background: #f5f5f5; }
.sig-slot-card.sig-slot-selected { border-color: #0078d4; background: #eef5ff; }
.sig-slot-thumb {
  max-width: 100%;
  max-height: 44px;
  object-fit: contain;
  background-image: repeating-conic-gradient(#eee 0% 25%, #fff 0% 50%);
  background-size: 8px 8px;
  border-radius: 3px;
}
.sig-slot-empty { font-size: 12px; color: #bbb; }
.sig-slot-label { font-size: 11px; color: #999; margin-top: 2px; }

/* Company chop thumbnail button */
.chop-thumb-mode {
  position: relative !important;
  padding: 2px 4px !important;
  overflow: hidden;
}
.chop-btn-thumb {
  height: 26px; width: auto; max-width: 90px;
  object-fit: contain; display: block; border-radius: 3px;
  background-image: repeating-conic-gradient(#eee 0% 25%, #fff 0% 50%);
  background-size: 8px 8px;
}
/* Badge overlays the bottom of the image in thumb mode */
.chop-thumb-mode .chop-admin-badge {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  text-align: center;
  font-size: 7px; padding: 1px 0;
  border-radius: 0 0 4px 4px;
  letter-spacing: 0.05em;
}
/* No "Chop" text needed when thumbnail is visible */
.chop-thumb-mode .btn-label { display: none !important; }

.chop-admin-badge {
  display: inline-block;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #fff;
  background: #ca5010;
  border-radius: 3px;
  padding: 1px 5px;
  line-height: 1.4;
  pointer-events: none;
}

/* Lasso selection rectangle */
.lasso-rect {
  position: absolute; pointer-events: none; z-index: 50;
  border: 1.5px dashed #0078d4; background: rgba(0,120,212,0.08);
}

.editor-body { display: flex; flex: 1; overflow: hidden; }

.pages-scroll {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  background: #e8eaf0; padding: 24px;
  display: flex; flex-direction: column; align-items: center; gap: 24px;
}

.upload-zone {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; border: 3px dashed #b0b8cc; border-radius: 12px;
  padding: 60px 40px; color: #666; cursor: pointer; transition: all .2s;
  background: #fff; width: 100%; max-width: 600px;
}
.upload-zone:hover, .upload-zone.drag-over { border-color: #0078d4; color: #0078d4; background: #f0f7ff; }
.upload-zone .upload-icon { font-size: 48px; }
.upload-zone p { font-size: 15px; font-weight: 500; }
.upload-zone small { font-size: 12px; color: #999; }

.page-wrapper { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.page-label {
  font-size: 12px; color: #888; align-self: stretch; margin-left: 2px;
  display: flex; align-items: center; gap: 6px;
}
.page-label-text { flex: 1; }
.page-rot-controls { display: flex; gap: 4px; }
.page-rot-btn {
  padding: 2px 7px; font-size: 14px; line-height: 1;
  background: #e8eaf0; border: 1px solid #c8cad0; border-radius: 4px;
  cursor: pointer; color: #555;
}
.page-rot-btn:hover { background: #d0d4dc; }
.page-container {
  position: relative; background: #fff;
  box-shadow: 0 3px 12px rgba(0,0,0,0.15);
  border-radius: 2px;
}
.page-overlay { position: absolute; top: 0; left: 0; pointer-events: all; }

/* ── PDF items ── */
.pdf-item {
  position: absolute;
  border: 2px dashed transparent;
  border-radius: 2px;
  cursor: move;
  user-select: none;
}
.pdf-item:hover { border-color: rgba(0,120,212,0.4); }
.pdf-item.selected { border-color: #0078d4; border-style: solid; }

.pdf-item img { display: block; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }

.text-content {
  outline: none; white-space: pre; min-height: 1em; padding: 2px 4px;
  line-height: 1.3; font-family: Arial, sans-serif;
  cursor: move;            /* drag mode by default */
  caret-color: transparent;
}
.pdf-item.selected .text-content { background: rgba(0,120,212,0.05); }
/* AI-placed text: wrap within the blank-line bounds instead of growing indefinitely */
.text-item.ai-constrained .text-content { white-space: pre-wrap; word-break: break-word; overflow-wrap: break-word; }
/* Active alignment / format toggle buttons */
.align-btn.active, .format-btn.active { background: #0078d4 !important; color: #fff !important; border-color: #0078d4 !important; }
.pdf-item.editing .text-content {
  cursor: text;
  caret-color: auto;
  background: rgba(0,120,212,0.1) !important;
}
.pdf-item.editing { border-style: solid !important; border-color: #0078d4 !important; }

.item-delete-btn {
  display: none; position: absolute; top: -10px; right: -10px;
  width: 20px; height: 20px; border-radius: 50%; background: #d13438; color: #fff;
  border: none; cursor: pointer; font-size: 14px; line-height: 1; z-index: 10;
  align-items: center; justify-content: center;
}
.pdf-item.selected .item-delete-btn { display: flex; }

.resize-handle {
  display: none; position: absolute; width: 10px; height: 10px;
  background: #0078d4; border: 2px solid #fff; border-radius: 2px; z-index: 10;
}
.pdf-item.selected .resize-handle { display: block; }
.corner-nw { top: -5px; left: -5px; cursor: nw-resize; }
.corner-ne { top: -5px; right: -5px; cursor: ne-resize; }
.corner-sw { bottom: -5px; left: -5px; cursor: sw-resize; }
.corner-se { bottom: -5px; right: -5px; cursor: se-resize; }
.edge-ml { top: 50%; left: -4px; width: 8px; height: 24px; cursor: ew-resize; transform: translateY(-50%); border-radius: 4px; }
.edge-mr { top: 50%; right: -4px; width: 8px; height: 24px; cursor: ew-resize; transform: translateY(-50%); border-radius: 4px; }
/* ── Colour swatches (text toolbar) ── */
.color-swatch {
  width: 20px; height: 20px; border-radius: 50%; flex-shrink: 0;
  border: 2px solid rgba(0,0,0,0.18); cursor: pointer; padding: 0;
  transition: transform .12s; display: inline-block; vertical-align: middle;
}
.color-swatch:hover { transform: scale(1.25); }
.color-swatch.active { outline: 2.5px solid #0078d4; outline-offset: 2px; }
.color-swatch-more {
  background: conic-gradient(red 0deg,yellow 60deg,lime 120deg,cyan 180deg,blue 240deg,magenta 300deg,red 360deg);
  position: relative; overflow: hidden; cursor: pointer;
}
.color-swatch-more input[type="color"] {
  position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%;
  cursor: pointer; padding: 0; border: 0;
}

/* ── Text controls bar (appears when text item selected) ── */
.text-controls-bar {
  display: none; background: #fff; border-top: 1px solid #e0e0e0;
  padding: 6px 16px; align-items: center; gap: 10px; flex-wrap: wrap;
}
.text-controls-bar.visible { display: flex; }
.text-controls-bar label { font-size: 12px; color: #666; margin-bottom: 0; }
.text-controls-bar select, .text-controls-bar input[type="color"] { width: auto; }

/* ── AI panel ── */
.ai-panel {
  display: none; width: min(340px, calc(100vw - 32px)); background: #fff;
  border-left: 1px solid #e0e0e0; overflow-y: auto;
  flex-direction: column;
}
.ai-panel.visible { display: flex; }
.ai-panel-header {
  display: flex; align-items: center; padding: 12px 16px;
  border-bottom: 1px solid #e0e0e0; gap: 8px;
}
.ai-panel-header h3 { flex: 1; font-size: 14px; font-weight: 700; }
.ai-panel-body { padding: 12px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.ai-suggestion-row {
  border: 1px solid #e8e8e8; border-radius: 6px; padding: 10px; display: flex; flex-direction: column; gap: 6px;
}
.ai-field-name { font-weight: 600; font-size: 13px; }
.ai-field-meta { font-size: 11px; color: #888; }
.ai-value-input { font-size: 13px; padding: 5px 8px; }
.ai-panel-footer { padding: 12px; border-top: 1px solid #e0e0e0; display: flex; gap: 8px; }

/* ── Status bar ── */
.status-bar {
  display: none; position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  padding: 9px 20px; border-radius: 6px; font-size: 13px; font-weight: 500;
  z-index: 200; box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  animation: fadeIn .2s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }
.status-info { background: #1a1a2e; color: #fff; }
.status-success { background: #107c10; color: #fff; }
.status-error { background: #d13438; color: #fff; }
.status-action-btn {
  margin-left: 10px; padding: 2px 10px; font-size: 12px; font-weight: 600;
  border: 1px solid rgba(255,255,255,0.6); border-radius: 4px;
  background: rgba(255,255,255,0.15); color: #fff; cursor: pointer;
}
.status-action-btn:hover { background: rgba(255,255,255,0.3); }

/* ── Spinner ── */
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: spin .6s linear infinite; }
/* Inline spinner inside a .btn (dark border for secondary buttons) */
.btn-spinner {
  display: inline-block; width: 12px; height: 12px; margin-right: 6px;
  border: 2px solid rgba(0,0,0,0.2); border-top-color: currentColor;
  border-radius: 50%; animation: spin .6s linear infinite; vertical-align: middle;
}
.btn-primary .btn-spinner { border-color: rgba(255,255,255,0.3); border-top-color: #fff; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Modal ── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 300; align-items: center; justify-content: center; }
.modal-overlay.visible { display: flex; }
.modal { background: #fff; border-radius: 10px; padding: 24px; width: min(380px, calc(100vw - 32px)); box-shadow: 0 8px 32px rgba(0,0,0,0.2); }
.modal h3 { font-size: 16px; font-weight: 700; margin-bottom: 16px; }
.modal-footer { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* ── Page Organizer ── */
/* Status bar must float above the organizer overlay */
.status-bar { z-index: 600; }

.org-overlay {
  display: none; position: fixed; top: 52px; left: 0; right: 0; bottom: 0;
  background: #1e1e2e; z-index: 150; flex-direction: column;
}
.org-overlay.visible { display: flex; }

.org-header {
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px; height: 52px; flex-shrink: 0;
  background: #1a1a2e; border-bottom: 1px solid #333;
}
.org-title { font-size: 16px; font-weight: 700; color: #fff; white-space: nowrap; }
.org-count {
  font-size: 11px; color: #a0c8ff; background: rgba(110,198,255,.15);
  border: 1px solid rgba(110,198,255,.3); border-radius: 10px;
  padding: 2px 9px; white-space: nowrap;
}
.org-status-msg { font-size: 12px; color: #a0c8ff; flex-shrink: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.org-body { flex: 1; overflow-y: auto; padding: 20px; background: #1e1e2e; }

.org-empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100%; min-height: 200px; color: #ccc; text-align: center;
}
.org-empty-icon { font-size: 48px; margin-bottom: 12px; }
.org-empty p { font-size: 15px; }
.org-empty strong { color: #6ec6ff; }

.org-grid {
  display: flex; flex-wrap: wrap; gap: 12px; align-content: flex-start;
}

.org-card {
  width: 350px; background: #fff; border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.5);
  cursor: grab; user-select: none;
  border: 2px solid transparent; position: relative;
  flex-shrink: 0; transition: box-shadow .15s, opacity .15s, border-color .12s;
}
.org-card:active { cursor: grabbing; }
.org-card.dragging { opacity: 0.35; }
.org-card.selected {
  border-color: #0078d4;
  box-shadow: 0 0 0 3px rgba(0,120,212,0.3), 0 2px 10px rgba(0,0,0,0.5);
}

/* Checkmark badge — top-left, visible on hover (faint) and when selected (solid) */
.org-card-check {
  position: absolute; top: 8px; left: 8px;
  width: 26px; height: 26px; border-radius: 50%;
  background: rgba(0,0,0,0.35); border: 2px solid rgba(255,255,255,0.7);
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s, background .12s;
  pointer-events: none; z-index: 3;
}
.org-card:hover .org-card-check { opacity: 0.6; }
.org-card.selected .org-card-check {
  opacity: 1;
  background: #0078d4;
  border-color: #fff;
}

/* Drop-position indicators — blue bar before/after a card */
.org-card.drop-before::before,
.org-card.drop-after::after {
  content: ''; position: absolute; top: -2px; bottom: -2px; width: 4px;
  background: #0078d4; border-radius: 2px; z-index: 1;
}
.org-card.drop-before::before { left: -8px; }
.org-card.drop-after::after  { right: -8px; }

.org-card-thumb {
  width: 100%; aspect-ratio: 1 / 1; background: #e8eaf0;
  overflow: hidden; border-radius: 4px 4px 0 0;
  display: flex; align-items: center; justify-content: center;
}
.org-card-thumb img {
  width: 100%; height: 100%; object-fit: contain; display: block;
  transition: transform 0.2s; transform-origin: center center;
}

.org-card-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 10px; border-top: 1px solid #eee;
  font-size: 13px; color: #555; background: #f4f4f6;
  border-radius: 0 0 6px 6px; gap: 8px;
}
.org-card-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.org-card-rot { display: flex; gap: 6px; flex-shrink: 0; }
.org-card-rot button {
  padding: 8px 14px; font-size: 20px; line-height: 1;
  background: #e0e0e8; border: none; border-radius: 6px;
  cursor: pointer; color: #333;
  transition: background .12s, transform .1s;
}
.org-card-rot button:hover { background: #c8c8d8; transform: scale(1.08); }

.org-card-del {
  position: absolute; top: -8px; right: -8px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #d13438; color: #fff;
  border: 2px solid #1e1e2e; cursor: pointer;
  font-size: 13px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s; z-index: 2;
}
.org-card:hover .org-card-del { opacity: 1; }

.org-footer {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 20px; flex-shrink: 0;
  background: #1a1a2e; border-top: 1px solid #333;
}
.org-hint { font-size: 12px; color: #888; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── AI Fill modal ── */
.ai-fill-modal-inner {
  width: min(640px, calc(100vw - 32px));
}
.ai-fill-section {
  margin-bottom: 20px;
}
.ai-fill-section-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: #0078d4;
  margin-bottom: 6px;
}
.ai-fill-company-scroll {
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
}
.ai-fill-company-table {
  width: 100%;
  border-collapse: collapse;
}
.ai-fill-company-table tr + tr { border-top: 1px solid #f0f0f0; }
.ai-fill-company-table td:first-child {
  padding: 5px 10px;
  font-size: 12px;
  color: #555;
  white-space: nowrap;
  width: 42%;
  vertical-align: middle;
}
.ai-fill-company-table td:last-child { padding: 3px 8px; }
.ai-fill-company-table input[type="text"] {
  width: 100%;
  font-size: 13px;
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  transition: border-color .15s, background .15s;
}
.ai-fill-company-table input[type="text"]:focus {
  border-color: #0078d4;
  background: #fff;
  outline: none;
}
.ai-fill-custom-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.ai-fill-extract-col {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex-shrink: 0;
  min-width: 140px;
}

/* Extract-from-file loading overlay */
.extract-loading-wrap { position: relative; }
.extract-overlay {
  position: absolute;
  inset: -6px;
  background: rgba(243,246,255,0.96);
  backdrop-filter: blur(2px);
  border: 1px solid #c8d4ef;
  border-radius: 8px;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  z-index: 5;
}
.extract-overlay.show { display: flex; }
.extract-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid #d0ddf5;
  border-top-color: #0078d4;
  border-radius: 50%;
  animation: spin .75s linear infinite;
}
.extract-overlay-msg {
  font-size: 13px;
  font-weight: 600;
  color: #1a3a6e;
  text-align: center;
}
.extract-overlay-hint {
  font-size: 11px;
  color: #94a3b8;
}

/* ═══════════════════════════════════════════════════════════
   RESPONSIVE / MOBILE  —  all breakpoint rules live here
   ═══════════════════════════════════════════════════════════ */

/* Touch-action: let the browser scroll when the user swipes on an
   empty PDF area, but intercept pointer events on items so dragging works. */
.pdf-item { touch-action: none; }

/* ── Toolbar always scrollable so every button stays reachable ── */
.toolbar {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
#main-controls, #text-controls { flex-shrink: 0; }

/* ── Toolbar pagination ── */
.tb-page {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
/* Nav arrows — hidden on desktop */
.tb-nav-btn {
  display: none;
}

/* ── Tablet (≤ 900 px) ── */
@media (max-width: 900px) {
  .toolbar { gap: 6px; padding: 6px 10px; }
  .org-card { width: calc(50% - 6px); }
  .org-hint { display: none; }
}

/* ── iPad and below (≤ 1024 px): shorten model badge + icon-only toolbar ── */
@media (max-width: 1024px) {
  .model-full  { display: none; }
  .model-short { display: inline; }
  .btn-label { display: none; }
  .toolbar .btn { padding: 8px 10px; }
  .toolbar .btn.btn-primary { padding: 8px 12px; }
}

/* ── Guide modal: wider on tablet / desktop ── */
@media (min-width: 768px) {
  .guide-modal-box { width: min(760px, calc(100vw - 48px)); max-height: 90vh; }
}
@media (min-width: 1024px) {
  .guide-modal-box { width: min(880px, calc(100vw - 80px)); max-height: 92vh; }
}

/* ── Phone (≤ 768 px): icon-only toolbar + paginated pages ── */
@media (max-width: 768px) {
  .btn-label { display: none; }
  .toolbar .btn { padding: 8px 10px; }
  .toolbar .btn.btn-primary { padding: 8px 12px; }

  /* Toolbar: positioning context for pinned nav buttons; no horizontal scroll.
     padding-right: 88px = 44px (▶ nav) + 44px (download anchor) */
  .toolbar {
    position: relative;
    overflow-x: hidden;
    padding-left: 44px;
    padding-right: 88px;
  }

  /* Download button — always visible, pinned just left of the ▶ nav button */
  .tb-download-anchor {
    position: absolute;
    right: 44px;
    top: 0;
    bottom: 0;
    width: 44px;
    padding: 0 !important;
    justify-content: center;
    border-radius: 0;
    border-left: 1px solid rgba(0, 120, 212, 0.35);
    z-index: 2;
  }

  /* Both control spans: fill remaining space and center content.
     No display override — JS controls show/hide via inline style. */
  #main-controls,
  #text-controls {
    flex: 1;
    justify-content: center;
    min-width: 0;
  }

  /* Nav buttons: full toolbar height, pinned to left/right edges */
  .tb-nav-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    bottom: 0;
    width: 44px;
    background: #f5f5f5;
    border: none;
    cursor: pointer;
    color: #555;
    z-index: 2;
    transition: background .15s;
  }
  .tb-nav-btn:hover { background: #e8e8e8; }
  #toolbar-prev, #text-prev { left: 0;  border-right: 1px solid #d8d8d8; }
  #toolbar-next, #text-next { right: 0; border-left:  1px solid #d8d8d8; }

  /* Hide the between-page separator on mobile */
  .tb-page-sep { display: none; }

  /* Pages hidden by default; JS adds .tb-active to show one */
  .tb-page { display: none; }
  .tb-page.tb-active { display: inline-flex; }

  /* Reduce page-area padding so pages fit without a horizontal scrollbar */
  .pages-scroll { padding: 16px 12px; }
}

/* ── Mobile (≤ 640 px) ── */
@media (max-width: 640px) {
  /* Header — slim, drop badges */
  .app-header { padding: 0 12px; gap: 8px; height: 48px; }
  .app-header .logo { font-size: 14px; }
  .header-user  { font-size: 11px; max-width: 110px; }
  .header-link  { font-size: 12px; padding: 4px 8px; }
  .btn-logout   { font-size: 12px; padding: 4px 8px; }

  /* Editor area: account for shorter header and iOS notch */
  .editor-layout { height: calc(100dvh - 48px); }

  /* Pages scroll */
  .pages-scroll { padding: 12px 8px; }

  /* Upload zone */
  .upload-zone { padding: 40px 20px; }
  .upload-zone .upload-icon { font-size: 36px; }
  .upload-zone p { font-size: 13px; }

  /* Admin layout */
  .admin-layout { padding: 0 12px; margin: 16px auto; }
  .chop-section { flex-direction: column; }
  .add-user-form { flex-direction: column; }
  .chop-preview { min-width: unset; width: 100%; }

  /* AI suggestion panel — slide up from bottom as a sheet */
  .ai-panel {
    position: fixed !important;
    bottom: 0; left: 0; right: 0;
    width: 100% !important;
    max-height: 60vh;
    border-left: none;
    border-top: 2px solid #e0e0e0;
    border-radius: 16px 16px 0 0;
    z-index: 200;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  }

  /* Modals — centred on mobile (same as desktop, just constrained width) */
  .modal {
    max-height: 88vh;
    overflow-y: auto;
  }
  .modal h3 { font-size: 15px; }
  .modal-footer { flex-direction: row; gap: 8px; }
  .modal-footer .btn { flex: 1; justify-content: center; }

  /* AI Fill modal */
  .ai-fill-custom-row { flex-direction: column; }
  .ai-fill-extract-col { flex-direction: row; align-items: center; gap: 8px; width: 100%; }
  .ai-fill-company-scroll { max-height: 180px; }

  /* Organizer — 2-column grid */
  .org-card { width: calc(50% - 6px); }
  .org-card-rot button { padding: 6px 10px; font-size: 17px; }
  .org-footer { padding: 10px 12px; flex-wrap: wrap; }
  .org-header {
    padding: 8px 12px; gap: 8px;
    flex-wrap: wrap; height: auto; min-height: 52px;
  }
  .org-title { font-size: 14px; }
}

/* ── Very small phones (≤ 380 px): single-column organizer ── */
@media (max-width: 380px) {
  .org-card { width: 100%; }
}

/* ── Coarse-pointer (touch) devices: WCAG 2.5.5 — 44 × 44 px minimum targets ── */
@media (pointer: coarse) {
  /* All buttons: at least 44 px tall */
  .btn    { min-height: 44px; }
  .btn-sm { min-height: 44px; padding-top: 8px; padding-bottom: 8px; }

  /* Number input in text toolbar */
  #font-size-input { min-height: 44px; padding-top: 8px; padding-bottom: 8px; }

  /* Colour swatches: grow to 28 px so tap targets are reachable */
  .color-swatch { width: 28px; height: 28px; }

  /* Signature slot cards in the sig modal */
  .sig-slot-card { min-height: 88px; }

  /* Item frame handles: keep desktop sizes — user wants frame to tightly wrap content */

  /* Page rotation buttons */
  .page-rot-btn { padding: 4px 10px; font-size: 16px; }
}

/* ── Feedback button ─────────────────────────────────────────────────────── */
.btn-feedback {
  background: transparent;
  border: 1.5px solid rgba(255,255,255,0.35);
  color: rgba(255,255,255,0.75);
  width: 28px; height: 28px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition: border-color .15s, color .15s;
}
.btn-feedback:hover    { border-color: #fff; color: #fff; }
.btn-feedback:disabled { opacity: .5; cursor: wait; pointer-events: none; }

/* ── Feedback modal ──────────────────────────────────────────────────────── */
.fb-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 1100;
  align-items: center; justify-content: center;
  padding: 1rem;
}
.fb-overlay.visible { display: flex; }
.fb-box {
  background: #fff;
  border-radius: 10px;
  width: clamp(320px, calc(100vw - 32px), 560px);
  max-height: 90vh;
  display: flex; flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
}
.fb-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
}
.fb-title { font-size: 1rem; font-weight: 600; }
.fb-body  { padding: 18px 20px 20px; overflow-y: auto; flex: 1; }
.fb-type-row { display: flex; gap: .5rem; }
.fb-type-btn {
  flex: 1; padding: .55rem .75rem;
  border: 2px solid #e0e0e0; border-radius: 8px;
  background: transparent; font-size: .88rem; font-weight: 500;
  cursor: pointer; font-family: inherit; color: #888;
  display: flex; align-items: center; justify-content: center; gap: .35rem;
  transition: border-color .15s, background .15s, color .15s;
}
.fb-type-btn.selected             { border-color: #0078d4; background: #eff6ff; color: #0078d4; }
.fb-type-btn:hover:not(.selected) { border-color: #9ca3af; color: #1a1a1a; }
.fb-ss-wrap {
  border: 1px solid #e0e0e0; border-radius: 6px; overflow: hidden;
  min-height: 56px; max-height: 180px;
  display: flex; align-items: center; justify-content: center;
  background: #f0f2f5; cursor: zoom-in;
}
.fb-ss-wrap img { max-width: 100%; max-height: 180px; display: block; }
.fb-ss-ph { color: #888; font-size: .85rem; padding: 1.2rem; text-align: center; }

/* Shared form utility classes used by the feedback modal */
.fb-body .field        { margin-bottom: 1rem; }
.fb-body .field-label  { font-size: .82rem; font-weight: 600; color: #444; margin-bottom: .4rem; }
.fb-body .req          { color: #dc2626; }
.fb-body .input        { width: 100%; border: 1px solid #d1d5db; border-radius: 6px; padding: .5rem .7rem; font-size: .88rem; font-family: inherit; resize: vertical; }
.fb-body .input:focus  { outline: none; border-color: #0078d4; box-shadow: 0 0 0 3px rgba(0,120,212,.12); }
.fb-body .form-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: .75rem; }
.fb-body .error-box    { background: #fef2f2; border: 1px solid #fca5a5; color: #dc2626; padding: .55rem .75rem; border-radius: 6px; font-size: .85rem; margin-top: .5rem; }
.fb-body .btn          { padding: .45rem 1rem; border-radius: 6px; font-size: .88rem; font-weight: 500; cursor: pointer; border: 1px solid transparent; font-family: inherit; }
.fb-body .btn.ghost    { background: transparent; border-color: #d1d5db; color: #555; }
.fb-body .btn.ghost:hover { background: #f3f4f6; }
.fb-body .btn.primary  { background: #0078d4; color: #fff; border-color: #0078d4; }
.fb-body .btn.primary:hover { background: #006cbe; }
.fb-body .btn:disabled { opacity: .5; cursor: not-allowed; }
