/* Advanced PDF Viewer – Frontend Styles v1.2 */

.apv-viewer-wrapper {
  --apv-bg: #1e1e1e;
  --apv-toolbar: #2d2d2d;
  --apv-accent: #3b82f6;
  --apv-text: #f3f4f6;
  --apv-border: #404040;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--apv-bg);
  color: var(--apv-text);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25);
  position: relative;
  max-width: 100%;
  width: 100%;
  box-sizing: border-box;
}

.apv-viewer-title {
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  background: var(--apv-toolbar);
  border-bottom: 1px solid var(--apv-border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Toolbar */
.apv-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  background: var(--apv-toolbar);
  border-bottom: 1px solid var(--apv-border);
  user-select: none;
}

.apv-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--apv-text);
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.apv-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

.apv-btn:active {
  background: rgba(255, 255, 255, 0.2);
}

.apv-sep {
  width: 1px;
  height: 20px;
  background: var(--apv-border);
  margin: 0 4px;
}

.apv-page-info {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
}

.apv-page-input {
  width: 48px;
  height: 28px;
  text-align: center;
  border: 1px solid var(--apv-border);
  border-radius: 4px;
  background: #1a1a1a;
  color: var(--apv-text);
  font-size: 13px;
}

.apv-zoom-level {
  font-size: 12px;
  min-width: 40px;
  text-align: center;
}

/* Search bar */
.apv-search-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: #252525;
  border-bottom: 1px solid var(--apv-border);
}

.apv-search-input {
  flex: 1;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--apv-border);
  border-radius: 6px;
  background: #1a1a1a;
  color: var(--apv-text);
  font-size: 14px;
}

.apv-search-count {
  font-size: 12px;
  opacity: 0.8;
  min-width: 60px;
}

/* Main area */
.apv-main {
  display: flex;
  height: 70vh;
  min-height: 320px;
  max-height: 900px;
  position: relative;
}

.apv-thumbs {
  width: 110px;
  overflow-y: auto;
  background: #181818;
  border-right: 1px solid var(--apv-border);
  padding: 8px;
  flex-shrink: 0;
}

.apv-thumb {
  margin-bottom: 8px;
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  opacity: 0.75;
  transition: opacity 0.15s, border-color 0.15s;
}

.apv-thumb:hover,
.apv-thumb.active {
  opacity: 1;
  border-color: var(--apv-accent);
}

.apv-thumb canvas {
  display: block;
  width: 100%;
  height: auto;
}

.apv-canvas-wrap {
  flex: 1;
  overflow: auto;
  position: relative;
  background: #121212;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

.apv-canvas {
  display: block;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.5);
  background: #fff;
  max-width: 100%;
  height: auto !important; /* prevent forced compression look */
}

.apv-text-layer {
  position: absolute;
  left: 0;
  top: 0;
  overflow: hidden;
  opacity: 0.25;
  line-height: 1;
  pointer-events: none;
}

.apv-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  font-size: 15px;
  z-index: 5;
}

/* Crop overlay – size/position set in JS to match the canvas */
.apv-crop-overlay {
  position: absolute;
  cursor: crosshair;
  z-index: 10;
  background: rgba(0, 0, 0, 0.12);
  box-sizing: border-box;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.apv-crop-box {
  position: absolute;
  border: 2px dashed #3b82f6;
  background: rgba(59, 130, 246, 0.18);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  box-sizing: border-box;
}

.apv-crop-panel,
.apv-share-panel {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #2d2d2d;
  border: 1px solid var(--apv-border);
  border-radius: 10px;
  padding: 14px 18px;
  z-index: 20;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  max-width: 90%;
  text-align: center;
}

.apv-share-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin: 10px 0;
}

.apv-share-buttons a,
.apv-share-buttons button {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 6px;
  background: var(--apv-accent);
  color: #fff;
  text-decoration: none;
  font-size: 13px;
  border: none;
  cursor: pointer;
}

.apv-share-buttons a:hover,
.apv-share-buttons button:hover {
  filter: brightness(1.1);
}

.apv-share-close,
.apv-crop-panel .apv-btn {
  margin-top: 6px;
}

/* ========== GALLERY ========== */
.apv-gallery {
  margin: 1.5em 0;
  width: 100%;
  box-sizing: border-box;
}

.apv-gallery-items {
  display: grid;
  gap: 20px;
  width: 100%;
  grid-template-columns: repeat(var(--apv-cols-d, 3), 1fr);
}

.apv-gallery-list .apv-gallery-items {
  grid-template-columns: 1fr !important;
}

.apv-gallery-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 24px;
  flex-wrap: wrap;
}

.apv-page-btn {
  padding: 10px 18px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  color: #1e293b;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.apv-page-btn:hover:not(:disabled) {
  background: #3b82f6;
  border-color: #3b82f6;
  color: #fff;
}

.apv-page-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.apv-page-status {
  font-size: 14px;
  color: #475569;
  font-weight: 500;
}

.apv-gallery-item.apv-page-hidden {
  display: none !important;
}

.apv-gallery-list .apv-gallery-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 16px;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.apv-gallery-list .apv-gallery-thumb {
  width: 90px;
  height: 120px;
  flex-shrink: 0;
  aspect-ratio: auto;
}

.apv-gallery-item {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  overflow: hidden;
  transition: box-shadow 0.2s, transform 0.2s;
  display: flex;
  flex-direction: column;
  min-width: 0; /* prevent grid blowout */
}

.apv-gallery-item:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.apv-gallery-thumb {
  position: relative;
  width: 100%;
  height: 220px;          /* fixed comfortable height on desktop */
  background: #e8ecf1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.apv-gallery-thumb canvas,
.apv-gallery-thumb img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}

.apv-gallery-thumb .apv-pdf-icon {
  font-size: 40px;
  opacity: 0.45;
}

.apv-gallery-thumb .apv-thumb-loading {
  font-size: 12px;
  color: #64748b;
}

.apv-preview-btn {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}

.apv-gallery-item:hover .apv-preview-btn {
  opacity: 1;
}

.apv-gallery-title {
  padding: 12px 14px 4px;
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.apv-gallery-actions {
  display: flex;
  gap: 8px;
  padding: 10px 14px 14px;
  margin-top: auto;
}

.apv-gallery-actions .apv-btn,
.apv-gallery-actions a.apv-btn {
  flex: 1;
  text-align: center;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 6px;
  background: #3b82f6;
  color: #fff;
  text-decoration: none;
  border: none;
  cursor: pointer;
}

.apv-gallery-actions a.apv-download-link {
  background: #64748b;
}

/* Modal */
.apv-modal {
  position: fixed;
  inset: 0;
  z-index: 99999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

.apv-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}

.apv-modal-content {
  position: relative;
  width: min(960px, 98vw);
  max-height: 94vh;
  background: #1e1e1e;
  border-radius: 12px;
  overflow: hidden;
  z-index: 1;
  display: flex;
  flex-direction: column;
}

.apv-modal-close {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 30;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}

.apv-modal-body {
  flex: 1;
  overflow: auto;
}

.apv-modal-viewer .apv-main {
  height: 72vh;
  max-height: none;
}

.apv-error {
  color: #dc2626;
  padding: 12px;
}

.apv-block-placeholder {
  padding: 20px;
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: 6px;
  color: #92400e;
  font-size: 14px;
}

/* ========== RESPONSIVE ========== */
/* Tablet */
@media (max-width: 900px) {
  .apv-gallery-items {
    grid-template-columns: repeat(var(--apv-cols-t, 2), 1fr);
  }
}

@media (max-width: 700px) {
  .apv-gallery-thumb {
    height: 160px;
  }

  .apv-main {
    height: 60vh;
    min-height: 280px;
  }

  .apv-toolbar {
    gap: 2px;
    padding: 6px 8px;
  }

  .apv-btn {
    min-width: 28px;
    height: 28px;
    font-size: 14px;
  }

  .apv-thumbs {
    width: 72px;
  }

  .apv-canvas-wrap {
    padding: 8px;
  }
}

/* Mobile */
@media (max-width: 480px) {
  .apv-gallery-items {
    grid-template-columns: repeat(var(--apv-cols-m, 1), 1fr);
  }

  .apv-gallery-thumb {
    height: 200px;
  }

  .apv-main {
    height: 55vh;
  }

  .apv-modal {
    padding: 0;
  }

  .apv-modal-content {
    width: 100vw;
    max-height: 100vh;
    border-radius: 0;
  }

  .apv-modal-viewer .apv-main {
    height: calc(100vh - 100px);
  }
}

/* Fullscreen */
.apv-viewer-wrapper:fullscreen {
  border-radius: 0;
  height: 100%;
  width: 100%;
}

.apv-viewer-wrapper:fullscreen .apv-main {
  height: calc(100% - 90px);
  max-height: none;
}
