:root {
  --ink: #11162b;
  --ink-soft: #5b6280;
  --ink-faint: #8d93ab;
  --bg: #0e1430;
  --rail: #161d3f;
  --panel: #ffffff;
  --panel-muted: #f3f5fa;
  --stage-bg: #1b2348;
  --accent: #d93030;
  --accent-soft: #f8dcdc;
  --accent-2: #2f54ad;
  --accent-2-soft: #dde6fb;
  --line: #e1e4ee;
  --line-dark: #2a3262;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--stage-bg);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- Topbar ---------- */
#topbar {
  height: 56px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--line-dark);
  color: #fff;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  background: none;
  border: none;
  padding: 4px 6px;
  margin: -4px -6px;
  border-radius: 8px;
  cursor: pointer;
  color: inherit;
  font-family: inherit;
}
.brand:hover { background: rgba(255, 255, 255, 0.08); }
.brand span { font-weight: 700; font-size: 15px; letter-spacing: 0.01em; }

.file-controls { display: flex; align-items: center; gap: 10px; min-width: 0; }
.filename {
  font-size: 13px;
  color: #aab1d6;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-nav { display: flex; align-items: center; gap: 6px; color: #cfd4ee; font-size: 13px; }
.page-nav input[type="number"] {
  width: 44px;
  text-align: center;
  background: #232b54;
  color: #fff;
  border: 1px solid var(--line-dark);
  border-radius: 6px;
  padding: 4px 2px;
}
.page-nav .of { color: #8d93ab; }
.page-nav .sep { width: 1px; height: 20px; background: var(--line-dark); margin: 0 4px; }
#zoom-level { width: 42px; text-align: center; font-size: 12.5px; color: #cfd4ee; }

.icon-btn {
  background: #232b54;
  border: 1px solid var(--line-dark);
  color: #fff;
  border-radius: 6px;
  width: 26px;
  height: 26px;
  line-height: 1;
  font-size: 14px;
}
.icon-btn:hover { background: #2c3566; }
.icon-btn:disabled { opacity: 0.35; cursor: default; }

.spacer { flex: 1 1 auto; }

.primary-actions { display: flex; gap: 6px; flex: 0 0 auto; }

button.icon-action {
  border: none;
  border-radius: 8px;
  padding: 7px 10px;
  background: transparent;
  border: 1px solid #353e6e;
  color: #e7e9f7;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
button.icon-action:hover:not(:disabled) { background: #1a2150; }
button.icon-action.accent {
  background: linear-gradient(135deg, var(--accent), #b32424);
  border-color: transparent;
}
button.icon-action.accent:hover:not(:disabled) { filter: brightness(1.12); }
button.icon-action:disabled { opacity: 0.35; cursor: default; }

button.primary, button.ghost {
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
}
button.primary {
  background: linear-gradient(135deg, var(--accent), #b32424);
  color: #fff;
}
button.primary:disabled { opacity: 0.35; cursor: default; }
button.ghost {
  background: transparent;
  border: 1px solid #353e6e;
  color: #e7e9f7;
}
button.ghost:disabled { opacity: 0.35; cursor: default; }
button.ghost:not(:disabled):hover { background: #1a2150; }
button.block { width: 100%; margin-top: 10px; }

/* ---------- Body layout ---------- */
#body { flex: 1 1 auto; display: flex; min-height: 0; }

/* ---------- Tool rail ---------- */
#toolrail {
  flex: 0 0 92px;
  background: var(--rail);
  border-right: 1px solid var(--line-dark);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 14px 8px;
  gap: 6px;
}
.rail-divider { height: 1px; background: var(--line-dark); margin: 6px 4px; }

.toolrail-about {
  margin-top: auto;
  padding: 10px 6px 2px;
  border-top: 1px solid var(--line-dark);
}
.toolrail-about .about-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #6c74a0;
  margin: 0 0 6px;
}
.toolrail-about p {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  font-size: 10.5px;
  color: #aab1d6;
  margin: 0 0 3px;
  white-space: nowrap;
}
.toolrail-about p span:first-child { color: #6c74a0; }
.toolrail-about p span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool,
.about-btn {
  background: transparent;
  border: none;
  border-radius: 9px;
  color: #aab1d6;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.tool .glyph,
.about-btn .glyph {
  font-size: 16px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: #1f2654;
}
.tool:hover,
.about-btn:hover { color: #fff; }
.tool.active { color: #fff; }
.tool.active .glyph {
  background: linear-gradient(135deg, var(--accent-2), #1c3173);
  clip-path: polygon(50% 0%, 100% 38%, 80% 100%, 20% 100%, 0% 38%);
  border-radius: 4px;
}

/* ---------- Stage ---------- */
#stage {
  flex: 1 1 auto;
  overflow: auto;
  position: relative;
  background: var(--stage-bg);
  padding: 28px 0 60px;
}

#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.drop-card {
  background: var(--panel);
  border: 2px dashed #3a437a;
  border-radius: 16px;
  padding: 48px 56px;
  text-align: center;
  max-width: 360px;
}
.drop-card .badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.04em;
  margin-bottom: 14px;
}
.drop-card h1 { font-size: 17px; margin: 0 0 8px; }
.drop-card p { font-size: 13px; color: var(--ink-soft); margin: 0 0 18px; line-height: 1.5; }

#stage.dragover .drop-card { border-color: var(--accent-2); background: var(--accent-2-soft); }

#pages-container { display: flex; flex-direction: column; align-items: center; gap: 22px; }

.page-wrapper {
  position: relative;
  background: #fff;
  box-shadow: 0 6px 24px rgba(0,0,0,0.35);
}
.page-wrapper canvas { display: block; }
.page-number-tag {
  position: absolute;
  bottom: -22px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: #8d93ab;
}

.annotationOverlay { position: absolute; inset: 0; }
#pages-container.mode-addtext .page-wrapper { cursor: crosshair; }

.draw-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
#pages-container.mode-draw .draw-layer {
  pointer-events: auto;
  cursor: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyOCIgaGVpZ2h0PSIyOCIgdmlld0JveD0iMCAwIDI4IDI4Ij4KICA8ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgxNCwxNCkgcm90YXRlKDQ1KSB0cmFuc2xhdGUoLTIsLTExKSI+CiAgICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iNCIgaGVpZ2h0PSIxNiIgcng9IjEiIGZpbGw9IiMyZjU0YWQiIHN0cm9rZT0iIzExMTYyYiIgc3Ryb2tlLXdpZHRoPSIwLjgiLz4KICAgIDxwb2x5Z29uIHBvaW50cz0iMCwxNiA0LDE2IDIsMjIiIGZpbGw9IiMxYTFhMmUiIHN0cm9rZT0iIzExMTYyYiIgc3Ryb2tlLXdpZHRoPSIwLjgiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4KICAgIDxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSI0IiBoZWlnaHQ9IjMiIGZpbGw9IiNhYWIxZDYiLz4KICA8L2c+Cjwvc3ZnPgo=") 6 22, crosshair;
}
#pages-container.mode-erase .draw-layer {
  pointer-events: auto;
  cursor: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyOCIgaGVpZ2h0PSIyOCIgdmlld0JveD0iMCAwIDI4IDI4Ij4KICA8ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgxNCwxNSkgcm90YXRlKDQ1KSB0cmFuc2xhdGUoLTcsLTUpIj4KICAgIDxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxNCIgaGVpZ2h0PSIxMCIgcng9IjIiIGZpbGw9IiNmZjhmYjMiIHN0cm9rZT0iIzExMTYyYiIgc3Ryb2tlLXdpZHRoPSIwLjgiLz4KICAgIDxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxNCIgaGVpZ2h0PSI0IiByeD0iMiIgZmlsbD0iI2ZmZDFlMSIgc3Ryb2tlPSIjMTExNjJiIiBzdHJva2Utd2lkdGg9IjAuOCIvPgogIDwvZz4KPC9zdmc+Cg==") 14 15, cell;
}

.textbox {
  position: absolute;
  min-width: 40px;
  min-height: 1.3em;
  border: 1px dashed var(--accent-2);
  background: rgba(47, 84, 173, 0.06);
}
.textbox:focus-within { border-style: solid; background: rgba(47, 84, 173, 0.1); }
.textbox-content {
  width: 100%;
  padding: 2px 4px 2px 16px;
  outline: none;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  line-height: 1.25;
  box-sizing: border-box;
  min-height: 1.3em;
}
.textbox .handle {
  position: absolute;
  left: -16px;
  top: -1px;
  width: 14px;
  height: 14px;
  background: var(--accent-2);
  color: #fff;
  border-radius: 3px;
  font-size: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
}
.textbox .remove {
  position: absolute;
  right: -8px;
  top: -8px;
  width: 16px;
  height: 16px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  font-size: 10px;
  line-height: 16px;
  text-align: center;
  cursor: pointer;
  display: none;
}
.textbox:hover .remove { display: block; }
.textbox .resize-handle {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 13px;
  height: 13px;
  background: var(--accent-2);
  border: 2px solid #fff;
  border-radius: 3px;
  cursor: nwse-resize;
  z-index: 2;
}

/* ---------- Side panel ---------- */
#side-panel {
  flex: 0 0 300px;
  background: var(--panel);
  border-left: 1px solid var(--line);
  padding: 18px;
  overflow-y: auto;
  display: none;
}
#side-panel.open { display: block; }
.panel-section { display: none; }
.panel-section.active { display: block; }
.panel-section h2 { font-size: 14px; margin: 0 0 10px; }
.hint { font-size: 12.5px; color: var(--ink-soft); line-height: 1.5; margin: 0 0 12px; }
.hint.small { font-size: 11.5px; margin-top: 10px; }

.field { display: block; font-size: 12.5px; color: var(--ink-soft); margin-bottom: 10px; }
.field[hidden] { display: none; }
.field span { display: block; margin-bottom: 4px; font-weight: 600; }
.field input[type="text"],
.field input[type="number"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 9px;
  font-size: 13px;
  color: var(--ink);
}
.field input[type="color"] {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 2px;
}
.field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 9px;
  font-size: 13px;
  color: var(--ink);
  background: #fff;
}
.field .slider-row { display: flex; align-items: center; gap: 8px; margin-bottom: 0; }
.field .slider-row input[type="range"] { flex: 1; accent-color: var(--accent-2); }
.field .slider-row span {
  display: inline;
  font-weight: 400;
  margin-bottom: 0;
  min-width: 34px;
  text-align: right;
  color: var(--ink);
}

#merge-list { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 6px; }
#merge-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  font-size: 12.5px;
}
#merge-list li .mname { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#merge-list li .mbtns { display: flex; gap: 2px; flex: 0 0 auto; }
#merge-list li button {
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 13px;
  padding: 0 4px;
}
#merge-list li button:hover { color: var(--accent); }

.split-mode { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; font-size: 12.5px; }
.split-mode label { display: flex; align-items: center; gap: 6px; }

/* ---------- About button + modal ---------- */
.about-btn { margin-top: auto; }

.about-overlay {
  position: fixed;
  inset: 0;
  background: rgba(14, 20, 48, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 24px;
}
.about-overlay[hidden] { display: none; }
.about-dialog {
  position: relative;
  background: var(--panel);
  color: var(--ink);
  border-radius: 14px;
  max-width: 460px;
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  padding: 28px 28px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.about-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 28px;
  height: 28px;
  border: none;
  background: var(--panel-muted);
  color: var(--ink-soft);
  border-radius: 50%;
  cursor: pointer;
  font-size: 13px;
}
.about-close:hover { background: var(--accent-soft); color: var(--accent); }
.about-header { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.about-header h2 { margin: 0; font-size: 17px; }
.about-version { margin: 2px 0 0; font-size: 12.5px; color: var(--ink-soft); }
.about-section { margin-bottom: 16px; }
.about-section h3 {
  margin: 0 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent-2);
}
.about-section p { margin: 0; font-size: 13px; line-height: 1.55; color: var(--ink-soft); }
.about-section a { color: var(--accent-2); }

/* ---------- Form field overlays ---------- */
#pages-container.mode-form .page-wrapper { cursor: cell; }

.form-field-overlay {
  position: absolute;
  border: 2px solid #2563eb;
  background: rgba(37, 99, 235, 0.08);
  border-radius: 3px;
  box-sizing: border-box;
  user-select: none;
  min-width: 14px;
  min-height: 14px;
  z-index: 20;
}
.form-field-overlay.form-field-suggested {
  border-color: #d97706;
  background: rgba(217, 119, 6, 0.1);
}
.form-field-move-handle {
  position: absolute;
  top: -18px;
  left: -1px;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #2563eb;
  color: #fff;
  font-size: 12px;
  border-radius: 3px 0 0 0;
  cursor: grab;
  touch-action: none;
  z-index: 2;
}
.form-field-move-handle:active { cursor: grabbing; }
.form-field-suggested .form-field-move-handle { background: #d97706; }
.form-field-label {
  position: absolute;
  top: -18px;
  left: 19px;
  right: 38px;
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: #2563eb;
  color: #fff;
  padding: 2px 4px;
  border-radius: 0 3px 0 0;
  pointer-events: none;
}
.form-field-suggested .form-field-label { background: #d97706; }
.form-field-btn {
  position: absolute;
  top: -18px;
  border: none;
  border-radius: 0;
  font-size: 11px;
  line-height: 1;
  padding: 2px 5px;
  height: 18px;
  cursor: pointer;
  font-weight: 700;
  z-index: 2;
}
.form-field-remove { right: -1px; background: #ef4444; color: #fff; border-radius: 0 3px 0 0; }
.form-field-accept { right: 18px; background: #16a34a; color: #fff; }
.form-field-resize {
  position: absolute;
  bottom: 0; right: 0;
  width: 10px; height: 10px;
  background: #2563eb;
  border-radius: 2px 0 0 0;
  cursor: se-resize;
  touch-action: none;
}
.form-field-suggested .form-field-resize { background: #d97706; }

/* Form field overlay inputs (for live preview typing) */
.form-field-input {
  position: absolute;
  left: 2px; top: 2px; right: 2px; bottom: 2px;
  border: none;
  background: rgba(255,255,255,0.85);
  font-size: 11px;
  padding: 2px 4px;
  resize: none;
  outline: none;
  width: calc(100% - 8px);
  box-sizing: border-box;
  color: #111;
}
.form-field-cb-wrap {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.form-field-cb-wrap input[type="checkbox"] {
  width: 12px; height: 12px;
  pointer-events: auto;
  cursor: pointer;
}
.form-field-cb-wrap input[type="radio"] {
  width: 12px; height: 12px;
  pointer-events: auto;
  cursor: pointer;
}
.form-field-overlay.form-field-round {
  border-radius: 50%;
}
.form-field-overlay.form-field-round .form-field-resize { border-radius: 50% 0 0 0; }
