/* ── Reset & Base ────────────────────────────────────────── */

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: #1a1a2e;
  color: #e0e0e0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Toolbar ─────────────────────────────────────────────── */

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #16213e;
  border-bottom: 1px solid #0f3460;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.toolbar h1 {
  font-size: 16px;
  font-weight: 600;
  color: #e94560;
  margin-right: 8px;
  white-space: nowrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  border-left: 1px solid #0f3460;
}

button, select {
  background: #0f3460;
  color: #e0e0e0;
  border: 1px solid #533483;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
}

button:hover { background: #533483; }
button.active { background: #e94560; border-color: #e94560; }

select { padding: 4px 6px; }

.page-info {
  font-size: 13px;
  color: #aaa;
  min-width: 80px;
  text-align: center;
}

/* ── Upload Overlay ──────────────────────────────────────── */

.upload-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.upload-overlay.hidden { display: none; }

.upload-box {
  border: 3px dashed #533483;
  border-radius: 16px;
  padding: 60px 80px;
  text-align: center;
  transition: border-color 0.2s, background 0.2s;
  background: #16213e;
  max-width: 500px;
}

.upload-box.dragover {
  border-color: #e94560;
  background: #1a1a3e;
}

.upload-box h2 {
  font-size: 22px;
  margin-bottom: 12px;
  color: #e94560;
}

.upload-box p {
  color: #aaa;
  margin-bottom: 20px;
}

.upload-box input[type="file"] { display: none; }

.upload-box label {
  display: inline-block;
  background: #e94560;
  color: white;
  padding: 10px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
}

.upload-box label:hover { background: #c73a52; }

.file-list {
  margin-top: 20px;
  max-height: 150px;
  overflow-y: auto;
}

.file-list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #0f3460;
  border-radius: 4px;
  margin-top: 6px;
  font-size: 13px;
  cursor: pointer;
}

.file-list-item:hover { background: #533483; }

/* ── Main Layout (side-by-side panels) ───────────────────── */

.main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.panel + .panel { border-left: 2px solid #0f3460; }

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #16213e;
  font-size: 12px;
  color: #aaa;
  flex-shrink: 0;
  border-bottom: 1px solid #0f3460;
}

.panel-header span { font-weight: 600; color: #e94560; }

.panel-content {
  flex: 1;
  overflow: auto;
  background: #111;
}

.panel-content iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: white;
}

/* ── PDF.js Canvas Container ─────────────────────────────── */

#pdfjs-container {
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 0 auto;
  width: fit-content;
  min-width: 100%;
  will-change: transform;
}

#pdfjs-container .page-placeholder {
  position: relative;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
  background: #f5f5f5;
}

#pdfjs-container .page-placeholder canvas {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none; /* let touches pass through to text layer */
}

/* ── Text Layer (PDF.js) ─────────────────────────────────── */

.textLayer {
  position: absolute;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  z-index: 2;
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

.textLayer :is(span, br) {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
  user-select: text;
  -webkit-user-select: text;
}

.textLayer span::selection {
  background: rgba(0, 100, 255, 0.3);
}

.textLayer .endOfContent {
  display: block;
  position: absolute;
  inset: 100% 0 0;
  z-index: 0;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
}

.textLayer.selecting .endOfContent {
  top: 0;
}

/* On touch devices the OS hijacks long-press text selection with its own
   selection UI + edit menu ("Markieren/Kopieren"), which buries our
   Highlight/Note bar. Disable native selection here; viewer.js drives the
   Selection object programmatically instead (long-press + drag). */
@media (pointer: coarse) {
  .textLayer,
  .textLayer :is(span, br) {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}

/* Custom selection highlight, drawn by viewer.js during touch selection. */
.selection-rect {
  position: absolute;
  background: rgba(0, 100, 255, 0.3);
  pointer-events: none;
  z-index: 2;
  border-radius: 1px;
}

/* ── PDF Link Annotations ────────────────────────────────── */

.link-annotation-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.link-annotation-layer .pdf-link {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  text-decoration: none;
  border-radius: 2px;
  transition: background 0.15s;
}

.link-annotation-layer .pdf-link:hover {
  background: rgba(0, 100, 255, 0.12);
}

/* ── Annotation Overlay ──────────────────────────────────── */

.annotation-overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
}

.annotation-overlay .highlight-rect {
  position: absolute;
  background: rgba(255, 230, 0, 0.35);
  mix-blend-mode: multiply;
  pointer-events: auto;
  cursor: pointer;
  border-radius: 2px;
  transition: background 0.15s;
}

.annotation-overlay .highlight-rect:hover {
  background: rgba(255, 180, 0, 0.5);
}

.annotation-overlay .note-marker {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  width: 24px;
  height: 24px;
  background: #e94560;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: white;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  transform: translate(-12px, -12px);
  transition: transform 0.15s;
  user-select: none;
}

.annotation-overlay .note-marker:hover {
  transform: translate(-12px, -12px) scale(1.2);
}

/* Active tool state */
button.tool-active {
  background: #e94560 !important;
  border-color: #e94560 !important;
}

/* Floating selection action bar */
.selection-bar {
  position: fixed;
  z-index: 200;
  background: #16213e;
  border: 1px solid #533483;
  border-radius: 8px;
  padding: 4px;
  display: flex;
  gap: 4px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}

.selection-bar button {
  font-size: 12px;
  padding: 6px 12px;
}

/* Note popup */
.note-popup {
  position: fixed;
  z-index: 200;
  background: #16213e;
  border: 1px solid #533483;
  border-radius: 8px;
  padding: 12px;
  width: 250px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.note-popup textarea {
  width: 100%;
  height: 80px;
  background: #0f3460;
  color: #e0e0e0;
  border: 1px solid #533483;
  border-radius: 4px;
  padding: 8px;
  font-size: 13px;
  resize: vertical;
}

.note-popup .note-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  justify-content: flex-end;
}

.note-popup .note-actions button {
  font-size: 12px;
  padding: 4px 10px;
}

.note-popup .btn-delete { background: #c73a52; border-color: #c73a52; }

/* ── Debug / Settings Panel ──────────────────────────────── */

.debug-panel {
  width: 290px;
  background: #16213e;
  border-left: 1px solid #0f3460;
  padding: 12px;
  overflow-y: auto;
  flex-shrink: 0;
  font-size: 12px;
}

.debug-panel h3 {
  color: #e94560;
  font-size: 13px;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid #0f3460;
}

.debug-section { margin-bottom: 16px; }

.debug-row {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  border-bottom: 1px solid #0f346033;
}

.debug-row .label { color: #888; }
.debug-row .value { color: #e0e0e0; font-family: monospace; }

.debug-hint {
  color: #666;
  font-size: 10px;
  margin-bottom: 10px;
  font-style: italic;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  margin: 2px 0;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.15s;
}

.toggle-row:hover { background: #0f3460; }

.toggle-row input[type="checkbox"] {
  accent-color: #e94560;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.toggle-label {
  font-size: 11px;
  color: #ccc;
  user-select: none;
}

.toggle-desc {
  font-size: 9px;
  color: #555;
  padding: 0 6px 2px 26px;
  line-height: 1.3;
}

.toggle-row-spaced { margin-top: 6px; }
.toggle-select { margin-left: auto; width: 70px; }

.sync-scroll-label {
  font-size: 13px;
  cursor: pointer;
}

.btn-apply {
  margin-top: 10px;
  width: 100%;
  background: #e94560;
}

.debug-panel .warn-badge {
  display: inline-block;
  background: #e94560;
  color: white;
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 6px;
  vertical-align: middle;
}

/* ── Page Input ──────────────────────────────────────────── */

.page-input {
  display: none;
  width: 50px;
  background: #0f3460;
  color: #e0e0e0;
  border: 1px solid #533483;
  border-radius: 4px;
  padding: 3px 6px;
  font-size: 13px;
  text-align: center;
  -moz-appearance: textfield;
}

.page-input::-webkit-outer-spin-button,
.page-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.page-info { cursor: pointer; }
.page-info.editing { display: none; }
.page-input.visible { display: inline-block; }

/* ── TOC Panel ───────────────────────────────────────────── */

.toc-panel {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  background: #16213e;
  border-right: 1px solid #0f3460;
  z-index: 95;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
}

.toc-panel.open { transform: translateX(0); }

.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid #0f3460;
  flex-shrink: 0;
}

.toc-header span {
  font-size: 14px;
  font-weight: 600;
  color: #e94560;
}

.toc-header button {
  background: none;
  border: none;
  color: #aaa;
  font-size: 18px;
  cursor: pointer;
  padding: 2px 6px;
}

.toc-header button:hover { color: #e94560; }

.toc-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.toc-empty {
  color: #666;
  font-size: 13px;
  padding: 16px;
  text-align: center;
  font-style: italic;
}

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 0;
  color: #ccc;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.15s, color 0.15s;
}

.toc-item:hover { background: #0f3460; color: #e94560; }
.toc-item.active { color: #e94560; background: #0f346088; }

.toc-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 94;
}

.toc-backdrop.open { display: block; }

/* ── Scrollbar ───────────────────────────────────────────── */

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: #111; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* Debug toggle button (hidden on desktop, shown on mobile) */
.btn-debug-toggle { display: none; }

/* ── Mobile (≤768px) ─────────────────────────────────────── */

@media (max-width: 768px) {
  .toolbar {
    padding: 6px 8px;
    gap: 6px;
  }

  .toolbar h1 {
    font-size: 14px;
    margin-right: 4px;
  }

  .toolbar-group {
    padding: 0 4px;
    gap: 4px;
  }

  button, select {
    padding: 6px 8px;
    font-size: 12px;
    min-height: 32px;
  }

  .page-info {
    min-width: 50px;
    font-size: 12px;
  }

  /* Hide native browser panel — only show PDF.js viewer */
  .panel.native-panel { display: none; }
  .panel.pdfjs-panel { border-left: none; }

  /* Hide panel header — only one panel visible */
  .panel-header { display: none; }

  #pdfjs-container {
    padding: 6px;
    gap: 6px;
  }

  /* Debug panel: slide-in drawer from right */
  .debug-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 280px;
    z-index: 90;
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }

  .debug-panel.open { transform: translateX(0); }

  .debug-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 89;
  }

  .debug-backdrop.open { display: block; }

  .btn-debug-toggle {
    display: inline-flex;
    align-items: center;
    font-size: 12px;
  }

  /* Upload box: less padding on small screens */
  .upload-box {
    padding: 30px 20px;
    margin: 16px;
    max-width: calc(100vw - 32px);
  }

  .upload-box h2 { font-size: 18px; }

  .toc-panel { width: 260px; }
}
