:root {
  --bg: #f1f5f9;
  --bg-gradient: linear-gradient(180deg, #eff6ff 0%, #f1f5f9 100%);
  --panel: #ffffff;
  --panel-soft: #f8fafc;
  --ink: #0f172a;
  --ink-soft: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --primary-soft-hover: #bfdbfe;
  --primary-ink: #1e3a8a;
  --accent: #0ea5e9;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warn: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 44px rgba(30, 58, 138, 0.14);
  --radius: 10px;
  --radius-sm: 8px;
  --radius-lg: 14px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg-gradient);
  color: var(--ink);
  font-family: "Inter", "Sarabun", -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

.ico { width: 18px; height: 18px; flex-shrink: 0; }
.ico.chev { width: 14px; height: 14px; opacity: 0.6; }

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 78px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark-link {
  display: grid; place-items: center;
  width: 50px; height: 50px; flex: 0 0 auto;
  border-radius: var(--radius);
  text-decoration: none;
  filter: drop-shadow(0 8px 18px rgba(37, 99, 235, 0.32));
  transition: transform 0.15s ease;
}
.brand-mark-link:hover { transform: translateY(-1px) scale(1.03); }
.brand-logo { display: block; width: 48px; height: 48px; }
.brand-text { min-width: 0; }
.brand-title-row {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
h1 {
  margin: 0;
  display: inline-flex;
  align-items: baseline;
  gap: 0;
  font-family: "Inter", "Sarabun", system-ui, sans-serif;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.045em;
  line-height: 1;
  font-feature-settings: "ss01", "cv11";
}
.brand-doc {
  color: var(--ink);
  font-weight: 600;
  opacity: 0.85;
}
.brand-modi {
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 800;
}
.brand-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-left: 5px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  text-indent: -9999px;
  transform: translateY(-2px);
  box-shadow: 0 3px 8px rgba(37, 99, 235, 0.45);
}
.brand-mini-logo {
  display: grid;
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  color: #fff;
  overflow: hidden;
  text-decoration: none;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.24);
  transform-origin: center;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
  will-change: transform;
}
.brand-mini-logo svg {
  display: block;
  width: 31px;
  height: 31px;
}
.brand-mini-logo:hover {
  animation: brandLogoPulse 0.72s ease-in-out infinite alternate;
  box-shadow: 0 10px 22px rgba(14, 165, 233, 0.34);
}
.brand-mini-logo:active { transform: scale(0.96); }
@keyframes brandLogoPulse {
  from { transform: translateY(0) scale(1); }
  to { transform: translateY(-1px) scale(1.08); }
}
@media (prefers-reduced-motion: reduce) {
  .brand-mini-logo:hover {
    animation: none;
    transform: scale(1.04);
  }
}
@media (max-width: 700px) {
  .brand { gap: 8px; }
  .brand-mark-link { display: none; }
}
.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 500;
  letter-spacing: 0.01em;
}

.seo-intro {
  display: grid;
  gap: 6px;
  max-width: 620px;
  margin: 2px auto 8px;
  text-align: center;
}

.seo-intro h2 {
  margin: 0;
  color: var(--ink);
  font-size: 24px;
  line-height: 1.15;
  font-weight: 800;
}

.seo-intro p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  min-height: 36px; padding: 7px 12px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff; color: var(--ink-soft); font-weight: 500;
  cursor: pointer; transition: all 0.15s ease; box-shadow: var(--shadow-sm);
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); transform: translateY(-1px); }
.btn:active:not(:disabled) { transform: translateY(0); }
.btn:disabled { cursor: not-allowed; opacity: 0.45; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; box-shadow: 0 6px 16px rgba(37, 99, 235, 0.25); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); color: #fff; }
.btn-soft { width: 100%; justify-content: center; background: var(--primary-soft); border-color: var(--primary-soft); color: var(--primary-ink); }
.btn-soft:hover:not(:disabled) { background: var(--primary-soft-hover); border-color: var(--primary-soft-hover); }

.icon-btn {
  display: grid; width: 34px; height: 34px; min-height: 34px;
  place-items: center; padding: 0;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: #fff; color: var(--ink-soft); cursor: pointer;
  transition: all 0.15s ease;
}
.icon-btn:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
.icon-btn:disabled { cursor: not-allowed; opacity: 0.45; }

.file-button input { display: none; }

.zoom-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow-sm);
}
.zoom-group .icon-btn {
  width: 30px; height: 30px; min-height: 30px;
  border-color: transparent;
  background: transparent;
}
.zoom-group .icon-btn:hover:not(:disabled) { background: var(--primary-soft); border-color: transparent; }
.zoom-level {
  min-width: 50px;
  text-align: center;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  user-select: none;
}

/* ===== Toolbar row (centered) ===== */
.toolbar-row {
  position: sticky;
  top: 78px;
  z-index: 25;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  padding: 10px 16px;
}

.toolbar-center {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px;
  max-width: 1280px;
  margin: 0 auto;
}

.tool {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 38px; padding: 6px 11px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--ink-soft);
  font-weight: 500; cursor: pointer; transition: all 0.15s ease;
}
.tool:hover { background: var(--panel-soft); }
.tool.is-active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary-ink); }
.tool .ico { color: var(--muted); }
.tool.is-active .ico { color: var(--primary); }
.tool.danger { color: var(--danger); }
.tool.danger:hover { background: var(--danger-soft); }
.tool:disabled { opacity: 0.45; cursor: not-allowed; }

.tool-divider { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

/* ===== Tool popup ===== */
.tool-pop { position: relative; }
.popup {
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  min-width: 260px;
  max-width: 320px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow-lg);
  display: grid;
  gap: 10px;
}
.popup[hidden] { display: none; }
.popup-row { display: grid; gap: 6px; }
.popup-row label { font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.popup-row select, .popup-row input[type="text"], .popup-row input[type="number"], .popup-row input[type="url"] {
  width: 100%; height: 34px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0 10px; background: #fff;
}
.popup-row input[type="color"] { width: 100%; height: 34px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 3px; cursor: pointer; }
.popup-row input[type="range"] { width: 100%; -webkit-appearance: none; height: 4px; background: var(--line); border-radius: 999px; outline: none; }
.popup-row input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--primary); cursor: pointer; border: 2px solid #fff; box-shadow: 0 2px 6px rgba(37,99,235,0.4); }
.popup-row input[type="range"]::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: var(--primary); border: 2px solid #fff; cursor: pointer; }

.inline-label { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.inline-label output { text-transform: none; letter-spacing: 0; color: var(--primary-ink); font-weight: 600; font-size: 12.5px; }

.color-row { display: grid; grid-template-columns: 1fr 38px; gap: 8px; }
.color-row-pop { gap: 8px; }
.color-row-pop input[type="text"] { width: 100%; height: 32px; }

.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.swatches button { min-height: 24px; height: 24px; border: 1px solid var(--line); border-radius: 6px; padding: 0; cursor: pointer; transition: transform 0.1s ease; }
.swatches button:hover { transform: scale(1.1); }

.brush-preview-wrap { display: grid; min-height: 48px; place-items: center; border: 1px dashed var(--line-strong); border-radius: var(--radius-sm); background: var(--panel-soft); }
.brush-preview { display: block; min-width: 1px; min-height: 1px; border-radius: 999px; }

.segmented { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px; background: var(--panel-soft); border-radius: var(--radius-sm); }
.segmented button {
  flex: 1 1 auto;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  min-height: 30px; padding: 4px 8px;
  border: 1px solid transparent; border-radius: 6px;
  background: transparent; color: var(--ink-soft); cursor: pointer;
  font-size: 12.5px; transition: all 0.15s ease;
}
.segmented button:hover { background: #fff; }
.segmented button.is-active { background: #fff; border-color: var(--primary); color: var(--primary-ink); box-shadow: var(--shadow-sm); }
.segmented .ico { width: 14px; height: 14px; }

.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-soft); cursor: pointer; }
.checkbox-row input { accent-color: var(--primary); }

/* ===== Find bar ===== */
.findbar {
  position: sticky;
  top: 148px;
  z-index: 24;
  display: flex; align-items: center; gap: 6px;
  margin: 10px auto;
  padding: 8px 10px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff; box-shadow: var(--shadow);
  max-width: 520px;
}
.findbar[hidden] { display: none; }
.findbar input { flex: 1; height: 34px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0 10px; }
.findbar input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.find-status { color: var(--muted); font-size: 12.5px; min-width: 48px; text-align: center; }

/* ===== Workspace ===== */
.workspace {
  display: grid;
  grid-template-columns: 168px minmax(0, 1fr);
  height: calc(100vh - 140px);
  min-height: 0;
}

.thumbnail-sidebar {
  position: sticky;
  top: 140px;
  align-self: start;
  height: calc(100vh - 140px);
  background: var(--panel);
  overflow: auto;
  padding: 14px;
  border-right: 1px solid var(--line);
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.thumbnail-sidebar::-webkit-scrollbar { width: 8px; }
.thumbnail-sidebar::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }

.sidebar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sidebar-head strong { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.blank-page-select, .sidebar-control select, .sidebar-control input {
  width: 100%; height: 34px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--panel-soft); padding: 0 8px; font-size: 12.5px; color: var(--ink-soft);
}
.sidebar-control { display: grid; gap: 4px; margin-bottom: 10px; }
.sidebar-control span { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.thumbnails { display: grid; gap: 10px; margin-top: 10px; }
.thumb {
  position: relative; display: block; width: 100%;
  min-height: 144px; padding: 8px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--panel-soft); cursor: pointer; transition: all 0.15s ease;
}
.thumb:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }
.thumb.is-active { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.thumb canvas { display: block; width: 100%; height: 110px; object-fit: contain; background: #fff; border-radius: 6px; box-shadow: var(--shadow-sm); }
.thumb-index { position: absolute; left: 10px; bottom: 8px; display: grid; min-width: 22px; height: 22px; padding: 0 7px; place-items: center; border-radius: 999px; background: var(--primary); color: #fff; font-size: 11px; font-weight: 600; line-height: 1; box-shadow: var(--shadow-sm); }
.thumb-actions { position: absolute; right: 6px; bottom: 6px; display: flex; gap: 4px; opacity: 0; transition: opacity 0.15s ease; }
.thumb:hover .thumb-actions, .thumb.is-active .thumb-actions { opacity: 1; }
.thumb-actions button { display: grid; width: 24px; height: 24px; min-height: 24px; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink-soft); cursor: pointer; transition: all 0.15s ease; }
.thumb-actions button:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
.thumb-actions button.is-delete:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-soft); }
.thumb-actions .ico { width: 14px; height: 14px; }

/* ===== Document area ===== */
.document-shell { position: relative; min-width: 0; min-height: 0; height: 100%; padding: 28px 24px 28px 24px; overflow: auto; overscroll-behavior: contain; outline: 2px solid transparent; outline-offset: -10px; transition: outline-color 0.15s ease, background 0.15s ease; }

/* ===== Rulers + Guides ===== */
.ruler {
  position: sticky;
  z-index: 9;
  background: var(--panel-soft);
  border: 1px solid var(--line);
}
.ruler-h {
  top: 0;
  left: 0;
  right: 0;
  height: 12px;
  margin: -28px -24px 16px;
  cursor: ns-resize;
  background-image: repeating-linear-gradient(90deg, var(--line) 0 1px, transparent 1px 10px);
}
.ruler-v {
  position: fixed;
  top: 140px;
  left: 168px;
  bottom: 0;
  width: 12px;
  cursor: ew-resize;
  background-image: repeating-linear-gradient(0deg, var(--line) 0 1px, transparent 1px 10px);
}
@media (max-width: 1100px) { .ruler-v { left: 140px; } }
@media (max-width: 820px) { .ruler-v { display: none; } }

.guides-layer { position: absolute; inset: 0; pointer-events: none; z-index: 7; }
.guide {
  position: absolute;
  pointer-events: auto;
  background: var(--accent);
  opacity: 0.7;
}
.guide-h { left: 0; right: 0; height: 1px; cursor: ns-resize; }
.guide-h::after { content: ""; position: absolute; left: 0; right: 0; top: -3px; bottom: -3px; }
.guide-v { top: 0; bottom: 0; width: 1px; cursor: ew-resize; }
.guide-v::after { content: ""; position: absolute; top: 0; bottom: 0; left: -3px; right: -3px; }
.guide.is-dragging { opacity: 1; box-shadow: 0 0 0 1px rgba(14,165,233,0.4); }

/* ===== Context menu ===== */
.ctx-menu {
  position: fixed;
  z-index: 90;
  min-width: 180px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  box-shadow: var(--shadow-lg);
  display: grid;
  gap: 2px;
}
.ctx-menu[hidden] { display: none; }
.ctx-menu button {
  display: block;
  width: 100%;
  padding: 7px 12px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}
.ctx-menu button:hover:not(:disabled) { background: var(--primary-soft); color: var(--primary-ink); }
.ctx-menu button:disabled { opacity: 0.4; cursor: not-allowed; }
.ctx-menu button.danger { color: var(--danger); }
.ctx-menu button.danger:hover { background: var(--danger-soft); }
.ctx-sep { height: 1px; background: var(--line); margin: 4px 6px; }

/* ===== Form field drag handle ===== */
.form-handle {
  position: absolute;
  top: -8px;
  left: -8px;
  width: 14px;
  height: 14px;
  background: var(--primary);
  border: 2px solid #fff;
  border-radius: 3px;
  cursor: move;
  box-shadow: var(--shadow-sm);
  display: none;
  z-index: 2;
}
.document-shell[data-tool="select"] .form-handle { display: block; }
.form-field:hover .form-handle,
.form-field:focus-within .form-handle,
.form-field:hover .form-resize-handle,
.form-field:focus-within .form-resize-handle,
.form-field:hover .form-delete-btn,
.form-field:focus-within .form-delete-btn { display: grid; }
.document-shell[data-tool="select"] .form-resize-handle,
.document-shell[data-tool="select"] .form-delete-btn { display: grid; }
.text-handle { display: none; }
.document-shell[data-tool="select"] .text-handle,
.text-annotation-wrap:hover .text-handle,
.text-annotation-wrap:focus-within .text-handle { display: block; }

.chev {
  padding: 2px;
  border-radius: 4px;
  margin-left: 1px;
}
.chev:hover { background: rgba(37,99,235,0.18); }
.document-shell.is-dragover { outline-color: var(--primary); background: var(--primary-soft); }
.document-shell[data-tool="hand"] { cursor: grab; }
.document-shell[data-tool="hand"].is-panning { cursor: grabbing; }
.document-shell[data-can-pan="true"][data-tool="select"] { cursor: grab; }
.document-shell[data-can-pan="true"][data-tool="select"].is-panning { cursor: grabbing; }

.empty-art {
  display: block;
  width: 200px;
  height: auto;
  margin-bottom: 14px;
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.15);
  border: 2px solid #fff;
}

.empty-state {
  display: grid; justify-items: center; gap: 10px;
  max-width: 560px; margin: 12vh auto 0; padding: 36px 28px;
  border: 1.5px dashed var(--line-strong); border-radius: var(--radius-lg);
  background: rgba(255, 255, 255, 0.7); text-align: center;
}
.empty-icon { display: grid; place-items: center; width: 72px; height: 72px; margin-bottom: 4px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); }
.empty-state strong { font-size: 18px; font-weight: 600; color: var(--ink); }
.empty-state span { color: var(--muted); line-height: 1.55; font-size: 13px; }

.pages { display: grid; gap: 36px; justify-items: center; }
.page-wrap {
  position: relative; transform-origin: center center;
  background: #fff; border-radius: 6px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12), 0 2px 6px rgba(15, 23, 42, 0.06);
  line-height: 0; overflow: hidden;
}

.pdf-canvas, .draw-canvas, .text-layer { display: block; width: 100%; height: 100%; }
.draw-canvas, .text-layer { position: absolute; inset: 0; }
.draw-canvas { z-index: 2; touch-action: none; }
.text-layer { z-index: 3; pointer-events: none; }
.text-layer[data-role="annotations"] { z-index: 4; }
.text-layer[data-role="find"] { z-index: 5; pointer-events: none; }

.text-annotation-wrap { pointer-events: auto; }
.text-annotation {
  min-width: 40px; min-height: 25px;
  padding: 3px 5px; border: 1px dashed transparent;
  background: transparent; color: var(--ink); line-height: 1.3;
  outline: none; white-space: pre-wrap; border-radius: 4px;
}
.text-annotation:focus,
.text-annotation-wrap.is-dragging .text-annotation {
  border-color: var(--primary); background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.page-label { position: absolute; right: 10px; bottom: -24px; color: var(--muted); font-size: 12px; font-weight: 500; line-height: 1; }

/* ===== Floating toolbar (text) ===== */
.floating-toolbar { position: fixed; z-index: 30; display: flex; align-items: center; gap: 4px; max-width: calc(100vw - 24px); padding: 6px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; box-shadow: var(--shadow-lg); }
.floating-toolbar[hidden] { display: none; }
.floating-toolbar select { height: 32px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel-soft); padding: 0 8px; font-size: 12.5px; max-width: 140px; }
.floating-toolbar .icon-btn { width: 32px; height: 32px; min-height: 32px; border-color: transparent; }
.floating-toolbar .icon-btn:hover { background: var(--primary-soft); }
.text-toolbar-sep { display: inline-block; width: 1px; height: 22px; background: var(--line); margin: 0 2px; }
.floating-toolbar .text-delete { color: var(--danger); }
.floating-toolbar .text-delete:hover { background: var(--danger-soft); }

/* ===== Modals ===== */
.modal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; background: rgba(15,23,42,0.5); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); padding: 20px; animation: fadeIn 0.15s ease; }
.modal[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-content { width: min(560px, 100%); max-height: 90vh; overflow: auto; border-radius: var(--radius-lg); background: #fff; box-shadow: 0 32px 80px rgba(15,23,42,0.4); animation: slideUp 0.2s ease; }
@keyframes slideUp { from { transform: translateY(10px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-wide { width: min(820px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head strong { font-size: 16px; font-weight: 600; }
.modal-close { border-color: transparent; }
.modal-body { display: grid; gap: 14px; padding: 18px 20px 22px; }
.muted { margin: 0; color: var(--muted); font-size: 13px; }

.field { display: grid; gap: 6px; }
.field span { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.field input { width: 100%; height: 38px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0 12px; background: #fff; }
.field input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }

#signaturePad { display: block; width: 100%; height: 200px; border: 1.5px dashed var(--line-strong); border-radius: var(--radius); background: var(--panel-soft); touch-action: none; cursor: crosshair; }
.signature-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.inline-control { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.inline-control input[type="color"] { width: 40px; height: 34px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 2px; cursor: pointer; }

.saved-signatures { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.saved-signatures .sig-card { position: relative; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--panel-soft); padding: 6px; cursor: pointer; transition: all 0.15s ease; }
.saved-signatures .sig-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }
.saved-signatures .sig-card img { display: block; width: 100%; height: 72px; object-fit: contain; background: #fff; border-radius: 6px; }
.saved-signatures .sig-card button { position: absolute; top: 4px; right: 4px; display: grid; width: 22px; height: 22px; min-height: 22px; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--danger); cursor: pointer; }
.saved-signatures .sig-card button:hover { background: var(--danger-soft); border-color: var(--danger); }

.ocr-toolbar { display: flex; gap: 8px; }
.ocr-toolbar input { flex: 1; height: 38px; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0 12px; background: #fff; }
.ocr-toolbar input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.ocr-result { max-height: 420px; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 14px; background: var(--panel-soft); white-space: pre-wrap; font-size: 13.5px; line-height: 1.65; font-family: "Sarabun", "Inter", monospace; }
.ocr-result mark { background: #fef3c7; color: var(--ink); padding: 0 2px; border-radius: 3px; }

.version-list { display: grid; gap: 8px; }
.version-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px; background: var(--panel-soft); }
.version-row:hover { border-color: var(--primary); }
.version-row span { font-size: 13px; color: var(--ink); }
.version-row strong { font-weight: 600; }
.version-row small { color: var(--muted); font-size: 11.5px; }
.version-row button { padding: 6px 12px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink-soft); font-size: 12.5px; cursor: pointer; transition: all 0.15s ease; }
.version-row button:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
.version-row button[data-action="delete"]:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-soft); }

/* ===== Annotation overlays ===== */
.redact-rect { position: absolute; pointer-events: auto; cursor: move; border-radius: 1px; }
.redact-rect.is-white { background: #fff; border: 1px solid var(--line); }
.redact-rect.is-black { background: #000; }
.redact-rect.is-selected { outline: 2px dashed var(--danger); outline-offset: 1px; }

.markup-rect { position: absolute; pointer-events: auto; cursor: move; border-radius: 2px; }
.markup-rect.is-highlight { /* alpha is handled inline by JS */ }
.markup-rect.is-strike { background: transparent; }
.markup-rect.is-strike::after { content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 2px; background: var(--ink); transform: translateY(-50%); }
.markup-rect.is-underline { background: transparent; }
.markup-rect.is-underline::after { content: ""; position: absolute; left: 0; right: 0; bottom: 2px; height: 2px; background: var(--ink); }
.markup-rect.is-selected { outline: 2px dashed var(--primary); }

.shape-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2; }
.shape-svg .shape { pointer-events: auto; cursor: move; }
.shape-svg .shape.is-selected { filter: drop-shadow(0 0 0 var(--primary)); }

.shape-bbox {
  position: absolute;
  border: 1.5px dashed var(--primary);
  pointer-events: none;
  z-index: 5;
  box-sizing: border-box;
}
.bbox-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 2px solid var(--primary);
  border-radius: 2px;
  box-shadow: var(--shadow-sm);
  pointer-events: auto;
  z-index: 6;
}
.bbox-nw { left: -7px; top: -7px; cursor: nwse-resize; }
.bbox-n  { left: 50%; top: -7px; transform: translateX(-50%); cursor: ns-resize; }
.bbox-ne { right: -7px; top: -7px; cursor: nesw-resize; }
.bbox-w  { left: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.bbox-e  { right: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.bbox-sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.bbox-s  { left: 50%; bottom: -7px; transform: translateX(-50%); cursor: ns-resize; }
.bbox-se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.bbox-p1, .bbox-p2 { border-radius: 50%; cursor: grab; }

.form-field { position: absolute; border: 1px dashed rgba(37, 99, 235, 0.5); background: rgba(219, 234, 254, 0.35); pointer-events: auto; border-radius: 3px; }
.form-field:focus-within { background: rgba(219, 234, 254, 0.6); border-color: var(--primary); }
.form-field input, .form-field textarea, .form-field select {
  width: 100%;
  height: 100%;
  border: 0;
  background: rgba(255, 255, 255, 0.92);
  padding: 2px 4px;
  font: inherit;
  color: var(--ink);
  outline: none;
  resize: none;
}
.form-field.is-text,
.form-field.is-dropdown {
  border-style: solid;
  background: rgba(219, 234, 254, 0.45);
}
.form-field.is-dropdown select {
  padding-right: 26px;
  appearance: none;
  -webkit-appearance: none;
}
.form-dropdown-arrow {
  position: absolute;
  right: 7px;
  top: 50%;
  z-index: 1;
  transform: translateY(-52%);
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  pointer-events: none;
}
.form-options-btn {
  position: absolute;
  top: -9px;
  right: -9px;
  z-index: 3;
  display: grid;
  width: 18px;
  height: 18px;
  min-height: 18px;
  place-items: center;
  padding: 0;
  border: 1px solid var(--primary);
  border-radius: 999px;
  background: #fff;
  color: var(--primary);
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  box-shadow: var(--shadow-sm);
}
.form-resize-handle {
  position: absolute;
  right: -8px;
  bottom: -8px;
  z-index: 3;
  display: none;
  width: 14px;
  height: 14px;
  border: 2px solid var(--primary);
  border-radius: 4px;
  background: #fff;
  cursor: nwse-resize;
  box-shadow: var(--shadow-sm);
}
.form-delete-btn {
  position: absolute;
  top: -9px;
  right: 12px;
  z-index: 3;
  display: none;
  width: 18px;
  height: 18px;
  min-height: 18px;
  place-items: center;
  padding: 0;
  border: 1px solid var(--danger);
  border-radius: 999px;
  background: #fff;
  color: var(--danger);
  box-shadow: var(--shadow-sm);
}
.form-delete-btn .ico {
  width: 12px;
  height: 12px;
}
.dropdown-options-panel {
  width: min(520px, calc(100vw - 32px));
}
.dropdown-options-panel textarea {
  width: 100%;
  min-height: 220px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  padding: 10px 12px;
  color: var(--ink);
  font: inherit;
  line-height: 1.45;
  resize: vertical;
}
.dropdown-options-panel textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}
.form-field.is-checkbox, .form-field.is-radio { display: grid; place-items: center; }
.form-field.is-checkbox input, .form-field.is-radio input { width: 70%; height: 70%; cursor: pointer; accent-color: var(--primary); }

.signature-stamp { position: absolute; pointer-events: auto; cursor: move; user-select: none; }
.signature-stamp img { display: block; width: 100%; height: 100%; pointer-events: none; }
.signature-stamp.is-image img { object-fit: cover; }
.signature-stamp.is-selected { outline: 2px dashed var(--primary); outline-offset: 2px; }
.signature-stamp .resize-handle { position: absolute; right: -8px; bottom: -8px; width: 14px; height: 14px; border: 2px solid var(--primary); background: #fff; border-radius: 4px; cursor: nwse-resize; box-shadow: var(--shadow-sm); }
.image-stamp-tools {
  position: absolute;
  top: -34px;
  left: 0;
  z-index: 4;
  display: none;
  gap: 6px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  box-shadow: var(--shadow-sm);
}
.signature-stamp.is-image:hover .image-stamp-tools,
.signature-stamp.is-image.is-selected .image-stamp-tools {
  display: inline-flex;
}
.image-stamp-tools button {
  display: grid;
  width: 24px;
  height: 24px;
  min-height: 24px;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.image-stamp-tools button:hover { background: var(--primary-soft); color: var(--primary); }
.image-stamp-tools .ico { width: 14px; height: 14px; }

.link-rect { position: absolute; pointer-events: auto; cursor: pointer; border: 1.5px solid rgba(37, 99, 235, 0.7); background: rgba(37, 99, 235, 0.08); border-radius: 3px; }
.link-rect:hover { background: rgba(37, 99, 235, 0.18); }

.find-hit { position: absolute; background: rgba(254, 240, 138, 0.65); border-radius: 2px; }
.find-hit.is-current { background: rgba(251, 191, 36, 0.85); box-shadow: 0 0 0 2px rgba(251, 146, 60, 0.5); }

.crop-overlay { position: absolute; inset: 0; pointer-events: auto; z-index: 6; cursor: crosshair; }
.crop-mask { position: absolute; background: rgba(0, 0, 0, 0.45); pointer-events: none; }
.crop-rect-frame { position: absolute; border: 2px dashed #fff; pointer-events: none; box-shadow: 0 0 0 9999px rgba(0,0,0,0.45); }

/* ===== Status toast ===== */
.status { position: fixed; right: 20px; bottom: 20px; z-index: 80; max-width: min(360px, calc(100vw - 40px)); padding: 12px 16px; border-radius: var(--radius); background: var(--ink); color: #fff; font-size: 13.5px; line-height: 1.4; box-shadow: 0 16px 32px rgba(15,23,42,0.3); opacity: 0; transform: translateY(8px); transition: opacity 0.2s ease, transform 0.2s ease; pointer-events: none; }
.status.is-visible { opacity: 1; transform: translateY(0); }

.quality-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.quality-card {
  display: grid;
  gap: 4px;
  padding: 14px 12px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-soft);
  text-align: left;
  cursor: pointer;
  transition: all 0.15s ease;
}
.quality-card:hover { border-color: var(--primary); background: var(--primary-soft); transform: translateY(-1px); }
.quality-card.is-active { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.quality-card strong { font-size: 14px; color: var(--ink); font-weight: 600; }
.quality-card span { font-size: 12px; color: var(--muted); }
.export-type-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.export-type-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-soft);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}
.export-type-card .ico { width: 20px; height: 20px; color: var(--primary); flex: 0 0 auto; }
.export-type-card span { display: grid; gap: 2px; min-width: 0; }
.export-type-card strong { font-size: 14px; }
.export-type-card small { color: var(--muted); font-size: 12px; line-height: 1.25; }
.export-type-card:hover,
.export-type-card.is-active {
  border-color: var(--primary);
  background: var(--primary-soft);
}

/* ===== Feedback ===== */
.feedback-fab {
  position: fixed;
  right: 20px;
  bottom: 36px;
  z-index: 55;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  color: #fff;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  box-shadow: 0 14px 30px rgba(37, 99, 235, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.feedback-fab:hover { transform: translateY(-2px); box-shadow: 0 18px 36px rgba(37, 99, 235, 0.45); }
.feedback-fab .ico { color: #fff; }
@media (max-width: 640px) { .feedback-fab span { display: none; } }

.field textarea {
  width: 100%;
  min-height: 110px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  font: inherit;
  color: var(--ink);
  resize: vertical;
}
.field textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.field input[type="file"] { padding: 8px; font-size: 12.5px; background: var(--panel-soft); }

.attach-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 8px;
}
.attach-list:empty { display: none; }
.attach-item {
  position: relative;
  display: grid;
  gap: 4px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel-soft);
  font-size: 11px;
  text-align: center;
}
.attach-item img {
  width: 100%;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  background: #fff;
}
.attach-item .filename { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-item .remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  min-height: 20px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--danger);
  font-size: 11px;
  cursor: pointer;
}
.attach-item .remove:hover { background: var(--danger-soft); border-color: var(--danger); }

.version-badge {
  position: fixed;
  right: 10px;
  bottom: 6px;
  z-index: 10;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.02em;
  opacity: 0.55;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
@media print { .version-badge { display: none; } }

/* ===== Responsive ===== */
@media (max-width: 1100px) {
  .workspace { grid-template-columns: 140px minmax(0, 1fr); }
  .btn span { display: none; }
  .btn { padding: 7px 10px; }
}
@media (max-width: 820px) {
  .topbar { align-items: flex-start; flex-direction: column; min-height: auto; }
  .actions { width: 100%; justify-content: flex-start; }
  .btn span { display: inline; }
  .workspace { grid-template-columns: 1fr; height: auto; min-height: calc(100vh - 140px); }
  .thumbnail-sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .thumbnails { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .tool span { display: none; }
  .document-shell { padding: 18px 12px 32px; }
  .floating-toolbar { flex-wrap: wrap; }
  .popup { left: 0; transform: none; }
}

/* ===== Print ===== */
@media print {
  body { background: #fff; }
  .topbar, .toolbar-row, .findbar, .thumbnail-sidebar, .empty-state, .status, .floating-toolbar, .page-label,
  .form-dropdown-arrow, .form-options-btn, .form-handle, .form-resize-handle, .form-delete-btn,
  .image-stamp-tools, .signature-stamp .resize-handle { display: none !important; }
  .form-field.is-text,
  .form-field.is-dropdown {
    border-color: transparent !important;
    background: transparent !important;
  }
  .form-field.is-text input,
  .form-field.is-text textarea,
  .form-field.is-dropdown select {
    background: transparent !important;
    box-shadow: none !important;
  }
  .workspace, .document-shell, .pages { display: block; padding: 0; min-height: auto; overflow: visible; }
  .page-wrap { width: 100% !important; height: auto !important; margin: 0 auto; box-shadow: none; page-break-after: always; }
  .page-wrap:last-child { page-break-after: auto; }
}
