:root {
  color-scheme: light;
  --bg: #f4f7f8;
  --surface: #ffffff;
  --surface-2: #eef3f1;
  --ink: #17202a;
  --muted: #667781;
  --line: #d7e0df;
  --accent: #0f8b8d;
  --accent-dark: #0a6e70;
  --green: #26805f;
  --yellow: #f2b84b;
  --red: #c74b43;
  --focus: #1d9bf0;
  --shadow: 0 12px 34px rgba(23, 32, 42, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

body.editor-locked {
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
}

button,
label {
  user-select: none;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 800;
}

.brand h1,
.panel-header h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 19px;
  line-height: 1.15;
}

.brand p,
.panel-header p,
.status-line {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.topbar-actions,
.toc-tools,
.page-controls,
.page-edit-tools,
.pdf-edit-tools,
.viewer-toolbar {
  display: flex;
  align-items: center;
}

.topbar-actions {
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.button,
.tool-button,
.icon-button,
.drop-action {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  cursor: pointer;
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    color 0.15s ease,
    transform 0.15s ease;
}

.button,
.drop-action {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  font-weight: 700;
  text-decoration: none;
}

.button-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.button-accent {
  background: #173c45;
  color: white;
  border-color: #173c45;
}

.button:hover,
.tool-button:hover,
.icon-button:hover,
.drop-action:hover {
  border-color: var(--accent);
}

.button-primary:hover {
  background: var(--accent-dark);
}

.button-accent:hover {
  background: #0f3037;
}

button:disabled,
.button[aria-disabled="true"],
.tool-button[aria-disabled="true"],
.tool-button:disabled,
.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
label:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 32%, transparent);
  outline-offset: 2px;
}

.workspace {
  height: calc(100vh - 72px);
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 1fr) 8px minmax(360px, var(--toc-width, 43vw));
  overflow: hidden;
}

.viewer-panel {
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: grid;
  border-right: 1px solid var(--line);
  background:
    linear-gradient(90deg, rgba(15, 139, 141, 0.04), transparent 30%),
    var(--surface-2);
}

.drop-zone {
  display: grid;
  place-items: center;
  padding: 32px;
}

.drop-zone::before {
  content: "";
  width: min(620px, 84%);
  max-width: 100%;
  aspect-ratio: 1.42;
  border: 2px dashed #a8b8b6;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(15, 139, 141, 0.12), transparent 55%),
    repeating-linear-gradient(0deg, rgba(23, 32, 42, 0.055), rgba(23, 32, 42, 0.055) 1px, transparent 1px, transparent 24px),
    #ffffff;
  box-shadow: var(--shadow);
  grid-area: 1 / 1;
}

.drop-zone.drag-over::before {
  border-color: var(--accent);
  background:
    linear-gradient(135deg, rgba(15, 139, 141, 0.18), transparent 55%),
    repeating-linear-gradient(0deg, rgba(23, 32, 42, 0.055), rgba(23, 32, 42, 0.055) 1px, transparent 1px, transparent 24px),
    #ffffff;
}

.drop-action {
  grid-area: 1 / 1;
  min-width: 150px;
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  box-shadow: 0 10px 24px rgba(15, 139, 141, 0.24);
}

.viewer {
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

.viewer-toolbar {
  min-height: 58px;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.76);
  flex-wrap: wrap;
}

.page-controls {
  gap: 8px;
}

.icon-button {
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  font-size: 20px;
  line-height: 1;
}

.icon-button.strong {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.page-input {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.page-input input {
  width: 56px;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--ink);
  text-align: right;
  font-weight: 700;
}

.page-input input:focus {
  outline: none;
}

.zoom-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

.zoom-control select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 8px;
}

.page-edit-tools,
.pdf-edit-tools {
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.compact-select {
  min-height: 34px;
  max-width: 148px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 8px;
  font-size: 13px;
}

.compact-select.small {
  max-width: 74px;
}

.font-select {
  max-width: 170px;
}

.number-input {
  width: 76px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 8px;
  font-size: 13px;
}

.color-input {
  width: 36px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 3px;
  cursor: pointer;
}

.tool-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.viewer-body {
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(104px, var(--thumb-width, 154px)) 8px minmax(0, 1fr);
}

.thumbnail-rail {
  min-height: 0;
  height: 100%;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  padding: 12px 10px;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.58);
}

.thumbnails-all {
  width: 100%;
}

.thumbnail-actions {
  display: grid;
  gap: 7px;
}

.thumbnail-actions .tool-button {
  width: 100%;
}

.thumbnail-list {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding-right: 2px;
}

.thumbnail-button {
  position: relative;
  display: grid;
  gap: 6px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 7px;
  cursor: pointer;
  text-align: left;
}

.thumbnail-card-actions {
  position: absolute;
  top: 6px;
  left: 6px;
  right: 6px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  z-index: 2;
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.thumbnail-button:hover .thumbnail-card-actions,
.thumbnail-button.active .thumbnail-card-actions,
.thumbnail-button.selected .thumbnail-card-actions,
.thumbnail-button:focus-within .thumbnail-card-actions {
  opacity: 1;
  transform: translateY(0);
}

.thumb-action {
  min-width: 0;
  height: 24px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.94);
  color: var(--ink);
  cursor: pointer;
  font-size: 12px;
  font-weight: 900;
  line-height: 1;
  padding: 0;
}

.thumb-action:hover {
  border-color: var(--accent);
}

.thumb-action.danger {
  color: var(--red);
}

.thumbnail-button.dragging {
  opacity: 0.46;
}

.thumbnail-button.drop-target {
  border-color: var(--yellow);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--yellow) 32%, transparent);
}

.thumbnail-button:hover,
.thumbnail-button.active {
  border-color: var(--accent);
  background: #f5fbfa;
}

.thumbnail-button.selected {
  border-color: var(--focus);
  background: #f4f9ff;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 22%, transparent);
}

.thumbnail-button.active.selected {
  border-color: var(--accent);
  box-shadow:
    0 0 0 2px color-mix(in srgb, var(--accent) 28%, transparent),
    inset 0 0 0 2px color-mix(in srgb, var(--focus) 36%, transparent);
}

.thumbnail-button canvas {
  display: block;
  width: 100%;
  height: auto;
  background: white;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(23, 32, 42, 0.1);
}

.thumbnail-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.canvas-wrap {
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: auto;
  padding: 26px;
  display: grid;
  align-content: start;
  justify-content: center;
}

.thumbnail-splitter {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  background: #edf3f2;
  color: var(--muted);
  cursor: col-resize;
  z-index: 3;
}

.thumbnail-splitter span {
  width: 22px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-weight: 900;
}

.page-stage {
  position: relative;
  width: max-content;
  height: max-content;
}

#pdfCanvas {
  display: block;
  background: white;
  border-radius: 3px;
  box-shadow: 0 18px 52px rgba(23, 32, 42, 0.18);
}

.annotation-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: auto;
}

.annotation-layer.tool-text,
.annotation-layer.tool-stamp {
  cursor: crosshair;
}

.annotation-layer.tool-draw {
  cursor: crosshair;
}

.annotation-item {
  position: absolute;
  min-width: 6px;
  min-height: 6px;
  border: 1px solid transparent;
  outline: 0;
  touch-action: none;
  cursor: pointer;
}

.annotation-item.selected {
  border-color: var(--focus);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 24%, transparent);
}

.annotation-text {
  display: block;
  width: 100%;
  height: 100%;
  padding: 2px 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.72);
  cursor: text;
}

.annotation-stamp {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border: 2px solid currentColor;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.2);
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.annotation-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: auto;
}

.annotation-draw {
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.resize-handle {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 12px;
  height: 12px;
  border: 2px solid white;
  border-radius: 4px;
  background: var(--focus);
  cursor: nwse-resize;
  display: none;
}

.selection-box {
  position: absolute;
  border: 1px solid var(--focus);
  background: color-mix(in srgb, var(--focus) 14%, transparent);
  pointer-events: none;
}

.move-handle {
  position: absolute;
  left: -8px;
  top: -8px;
  width: 18px;
  height: 18px;
  display: none;
  place-items: center;
  border: 1px solid var(--focus);
  border-radius: 6px;
  background: white;
  color: var(--focus);
  cursor: move;
  font-size: 12px;
  line-height: 1;
  padding: 0;
  z-index: 2;
}

.annotation-item.selected .resize-handle,
.annotation-item.selected .move-handle {
  display: block;
}

.annotation-item.selected .move-handle {
  display: grid;
}

.draw-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}

.workspace-splitter {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  background: #edf3f2;
  color: var(--muted);
  cursor: col-resize;
  z-index: 4;
}

.workspace-splitter span {
  width: 24px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-weight: 900;
}

body.resizing-workspace {
  cursor: col-resize;
  user-select: none;
}

body.resizing-thumbnails {
  cursor: col-resize;
  user-select: none;
}

.toc-panel {
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  background: var(--surface);
}

.panel-header {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--line);
}

.panel-header h2 {
  font-size: 18px;
}

.toc-tools {
  gap: 8px;
  flex-wrap: wrap;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
}

.tool-button {
  min-height: 34px;
  padding: 0 11px;
  font-size: 13px;
  font-weight: 700;
}

.tool-button.danger {
  color: var(--red);
}

.file-tool {
  display: inline-flex;
  align-items: center;
}

.toc-table {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.toc-head,
.toc-row {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 70px 66px 208px;
  gap: 8px;
  align-items: center;
}

.toc-head {
  padding: 9px 18px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.toc-body {
  overflow: auto;
  padding: 10px 10px 12px;
}

.toc-row {
  min-height: 48px;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
}

.toc-row + .toc-row {
  margin-top: 4px;
}

.toc-row:hover,
.toc-row.selected {
  background: #f5fbfa;
  border-color: #cfe1df;
}

.toc-row.dragging {
  opacity: 0.52;
}

.title-cell {
  min-width: 0;
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: 6px;
  align-items: center;
  padding-left: calc((var(--level) - 1) * 18px);
}

.drag-handle {
  color: var(--muted);
  cursor: grab;
  text-align: center;
  font-size: 18px;
}

.toc-row input,
.toc-row select {
  width: 100%;
  min-width: 0;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 0 8px;
}

.toc-row input[type="number"] {
  text-align: right;
}

.row-actions {
  display: grid;
  grid-template-columns: repeat(6, 30px);
  gap: 5px;
  justify-content: end;
}

.row-actions .icon-button {
  width: 30px;
  height: 30px;
  font-size: 16px;
}

.row-actions .delete {
  color: var(--red);
}

.row-actions .child {
  color: var(--green);
}

.empty-toc {
  display: none;
  padding: 18px;
  border-top: 1px solid var(--line);
}

.empty-toc.visible {
  display: block;
}

.status-line {
  min-height: 38px;
  padding: 10px 18px;
  border-top: 1px solid var(--line);
  background: #fbfcfc;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 28px;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(23, 32, 42, 0.45);
}

.modal-card {
  position: relative;
  width: min(1060px, 94vw);
  max-height: min(840px, 90vh);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 26px 80px rgba(23, 32, 42, 0.26);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.modal-header h2,
.modal-header p {
  margin: 0;
}

.modal-header h2 {
  font-size: 18px;
}

.modal-header p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
}

.all-thumbnail-grid {
  min-height: 0;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 14px;
  padding: 18px;
  background: var(--surface-2);
}

.hidden {
  display: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 980px) {
  body.editor-locked {
    overflow: auto;
  }

  .workspace {
    grid-template-columns: 1fr;
    height: auto;
    overflow: visible;
  }

  .workspace-splitter {
    display: none;
  }

  .viewer-panel,
  .toc-panel {
    min-height: auto;
    height: auto;
  }

  .viewer-panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .canvas-wrap {
    max-height: 68vh;
  }

  .toc-panel {
    min-height: 520px;
  }
}

@media (max-width: 640px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    width: 100%;
    justify-content: stretch;
  }

  .topbar-actions .button {
    flex: 1 1 110px;
  }

  .viewer-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .page-edit-tools {
    justify-content: flex-start;
  }

  .pdf-edit-tools {
    justify-content: flex-start;
  }

  .viewer-body {
    grid-template-columns: minmax(96px, var(--thumb-width, 112px)) 8px minmax(0, 1fr);
  }

  .thumbnail-rail {
    padding: 10px 8px;
  }

  .canvas-wrap {
    padding: 16px;
  }

  .toc-head {
    display: none;
  }

  .toc-row {
    grid-template-columns: 1fr 66px 60px;
    align-items: start;
  }

  .title-cell {
    grid-column: 1 / -1;
  }

  .row-actions {
    grid-column: 1 / -1;
    grid-template-columns: repeat(6, 34px);
    justify-content: start;
  }

  .row-actions .icon-button {
    width: 34px;
    height: 34px;
  }
}
