/* Copyright 2014 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@import "pdf_viewer";

.pdf {
  &__toolbar {
    position: relative;
    left: 0;
    right: 0;
    cursor: default;
  }

  &__outer {
    user-select: text;
    overflow: hidden;
    width: 100%;
    height: 100%;
    position: relative;

    .textLayer,
    .pdf__resize {
      /* dark 蓝色、紫色背景显示不清楚，文档位于 https://github.com/siyuan-note/siyuan/issues/5351 */
      mix-blend-mode: multiply;
    }
  }

  &__util {
    z-index: 4;
    padding: 8px;

    &--hide .pdf__util__hide {
      display: none;
    }

    .b3-menu__item:hover {
      background-color: var(--b3-list-hover);
    }
  }

  &__rect {
    cursor: pointer;

    &--hl > div {
      transition: opacity 1500ms linear;
      animation-duration: 1s;
      animation-fill-mode: both;
      animation-name: fadeIn;
    }

    & > div {
      box-sizing: border-box;
      position: absolute;
      z-index: 1;

      &:first-child {
        border-bottom-left-radius: var(--b3-border-radius);
        border-top-left-radius: var(--b3-border-radius);
      }

      &:last-child {
        border-top-right-radius: var(--b3-border-radius);
        border-bottom-right-radius: var(--b3-border-radius);
      }
    }
  }

  &__resize {
    position: fixed;
    border: 2px solid var(--b3-pdf-background1);
    box-sizing: border-box;
    border-radius: var(--b3-border-radius);
  }
}

.pdf__outer--dark {
  .pdfViewer .page canvas,
  .thumbnailImage {
    filter: grayscale(0%) invert(100%) contrast(100%) brightness(100%) hue-rotate(180deg);
  }

  .textLayer {
    background-color: var(--b3-pdf-dark);
  }

  .textLayer,
  .pdf__resize {
    z-index: 2; // pdf 缩放后 canvas 会出现在末尾，导致无法选中和查看标注
    mix-blend-mode: screen;
  }
}

.annotationLayer section:not(.linkAnnotation) {
  display: none;
}

.pdfViewer.enablePermissions .textLayer span {
  user-select: none !important;
  cursor: not-allowed;
}

#viewerContainer.pdfPresentationMode:fullscreen {
  top: 0;
  border-top: 2px solid rgba(0, 0, 0, 0);
  background-color: var(--b3-theme-background);
  width: 100%;
  height: 100%;
  overflow: hidden;
  cursor: none;
  user-select: none;
}

.pdfPresentationMode:fullscreen a:not(.internalLink) {
  display: none;
}

.pdfPresentationMode:fullscreen .textLayer span {
  cursor: none;
}

.pdfPresentationMode.pdfPresentationModeControls > *,
.pdfPresentationMode.pdfPresentationModeControls .textLayer span {
  cursor: default;
}

#sidebarContainer {
  position: absolute;
  top: 30px;
  bottom: 0;
  width: var(--sidebar-width);
  visibility: hidden;
  z-index: 3;
  transition-duration: 200ms;
  transition-timing-function: ease;
  transition-property: left;
  left: calc(0px - var(--sidebar-width));
  background-color: var(--b3-theme-surface);
}

#outerContainer.sidebarResizing #sidebarContainer {
  /* Improve responsiveness and avoid visual glitches when the sidebar is resized. */
  transition-duration: 0s;
  /* Prevent e.g. the thumbnails being selected when the sidebar is resized. */
  user-select: none;
}

#outerContainer.sidebarMoving #sidebarContainer,
#outerContainer.sidebarOpen #sidebarContainer {
  visibility: visible;
}

#outerContainer.sidebarOpen #sidebarContainer {
  left: 0;
}

#mainContainer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  min-width: 320px;
}

#sidebarContent {
  top: 30px;
  bottom: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  width: 100%;
  left: 0;
}

#viewerContainer {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  top: 30px;
  right: 0;
  bottom: 0;
  left: 0;
  outline: none;
}

#viewerContainer:not(.pdfPresentationMode) {
  transition-duration: 200ms;
  transition-timing-function: ease;
}

#outerContainer.sidebarResizing #viewerContainer {
  /* Improve responsiveness and avoid visual glitches when the sidebar is resized. */
  transition-duration: 0s;
}

#outerContainer.sidebarOpen
#viewerContainer:not(.pdfPresentationMode) {
  transition-property: left;
  left: var(--sidebar-width);
}

#toolbarSidebar,
#toolbarViewer {
  width: 100%;
  height: 30px;
  display: flex;
  padding-left: 8px;
  box-sizing: border-box;
}

#toolbarViewer #numPages {
  line-height: 30px;
  padding-left: 4px;
}

#sidebarResizer {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 6px;
  z-index: 200;
  cursor: ew-resize;
  right: -6px;

  &::after {
    content: "";
    width: 1px;
    height: 100%;
    background-color: var(--b3-border-color);
    position: absolute;
    top: 0;
    left: 3px;
  }

  &:hover::after {
    background-color: var(--b3-scroll-color);
  }
}

#loadingBar {
  position: absolute;
  height: 4px;
  background-color: var(--b3-theme-primary-lightest);
  transition-duration: 200ms;
  transition-timing-function: ease;
  z-index: 3;
}

#loadingBar {
  transition-property: left;
  left: 0;
  right: var(--b3-pdf-offset);
}

#outerContainer.sidebarOpen #loadingBar {
  left: var(--sidebar-width);
}

#outerContainer.sidebarResizing #loadingBar {
  /* Improve responsiveness and avoid visual glitches when the sidebar is resized. */
  transition-duration: 0s;
}

#loadingBar .progress {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--progressBar-percent);
  height: 100%;
  background-color: var(--b3-theme-primary-light);
  overflow: hidden;
  transition: width 200ms;
}

@keyframes progressIndeterminate {
  0% {
    left: -142px;
  }
  100% {
    left: 0;
  }
}

#loadingBar .progress.indeterminate {
  background-color: var(--b3-theme-on-surface);
  transition: none;
}

#loadingBar .progress.indeterminate .glimmer {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: calc(100% + 150px);
  background: repeating-linear-gradient(
                  135deg,
                  var(--b3-theme-primary-lighter) 0,
                  var(--b3-theme-on-surface) 5px,
                  var(--b3-theme-on-surface) 45px,
                  var(--b3-theme-primary-light) 55px,
                  var(--b3-theme-primary-light) 95px,
                  var(--b3-theme-primary-lighter) 100px
  );
  animation: progressIndeterminate 1s linear infinite;
}

.findbar {
  left: 40px;
  position: absolute;
  top: 30px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  overflow: initial;
  z-index: 99; // https://github.com/siyuan-note/siyuan/issues/13614

  #findInput {
    width: 120px;
  }
}

.secondaryToolbar {
  right: 4px;
  position: absolute;
  top: 30px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  // 需高于 pdf 中的链接
  z-index: 38;

  .b3-menu__items {
    max-height: 60vh;
  }
}

#secondaryToolbarButtonContainer.hiddenScrollModeButtons > .scrollModeButtons,
#secondaryToolbarButtonContainer.hiddenSpreadModeButtons > .spreadModeButtons {
  display: none !important;
}

#findMsg {
  color: var(--b3-theme-error);
}

#findMsg:empty {
  display: none;
}

.toolbarButton {
  border: none;
  background: none;
  cursor: pointer;
  padding: 5px;
  user-select: none;
  width: 24px;
  box-sizing: border-box;
  height: 24px;
  margin-right: 8px;
  color: var(--b3-theme-on-surface);
  overflow: initial;
  align-self: center;

  &:hover,
  &:focus {
    color: var(--b3-theme-on-background);
  }

  & > svg {
    height: 14px;
    width: 14px;
  }

  &[disabled] {
    color: var(--b3-theme-surface-lighter);
  }
}

.toolbarButton.toggled,
.secondaryToolbarButton.toggled {
  color: var(--b3-theme-primary);
}

.secondaryToolbarButton {
  &:hover {
    background-color: var(--b3-list-hover);
  }

  &[disabled] {
    opacity: .38;
    cursor: not-allowed;
  }
}

.dropdownToolbarButton {
  --scale-select-width: 140px;
  width: var(--scale-select-width);
  margin: 2px 8px 2px 0;

  select {
    width: inherit;
    min-width: auto;
    padding: 0 8px;
    height: 26px;
  }
}

#customScaleOption {
  display: none;
}

#pageWidthOption {
  border-bottom: 1px rgba(255, 255, 255, .5) solid;
}

.bookmark[href="#"] {
  opacity: .5;
  pointer-events: none;
}

.toolbarField[type="checkbox"] {
  opacity: 0;
  position: absolute !important;
  margin: 10px 0 3px 7px;
}

.toolbarField.pageNumber {
  min-width: 16px;
  text-align: right;
  width: 48px;
  padding: 1px 8px;
  margin: 2px 0;
}

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

.toolbarField.pageNumber.visiblePageIsLoading {
  background-image: var(--loading-icon);
  background-repeat: no-repeat;
  background-position: 3px;
}

#thumbnailView,
#outlineView,
#attachmentsView,
#layersView {
  width: calc(100% - 60px);
  top: 0;
  bottom: 0;
  padding: 10px 30px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  position: absolute;
}

#thumbnailView > a:active,
#thumbnailView > a:focus {
  outline: 0;
}

#thumbnailView .thumbnail {
  float: left;
  width: var(--thumbnail-width);
  height: var(--thumbnail-height);
  margin: 0 10px 5px;
  padding: 1px;
  border: 7px solid transparent;
  border-radius: 2px;

  &:hover {
    border-color: var(--b3-list-hover);
  }

  &.selected {
    border-color: var(--b3-border-color);
  }
}

.thumbnailImage {
  width: var(--thumbnail-width);
  height: var(--thumbnail-height);
  opacity: 0.9;
}

.thumbnailSelectionRing {
  border-radius: var(--b3-border-radius);
  padding: 7px;
  margin: 0 auto;
  box-shadow: 0 0 1px 1px var(--b3-theme-surface-lighter);
}

a:focus > .thumbnail > .thumbnailSelectionRing > .thumbnailImage,
.thumbnail:hover > .thumbnailSelectionRing > .thumbnailImage {
  opacity: .9;
}

a:focus > .thumbnail > .thumbnailSelectionRing,
.thumbnail:hover > .thumbnailSelectionRing {
  background-color: var(--b3-list-hover);
  background-clip: padding-box;
  color: rgba(255, 255, 255, .9);
}

.thumbnail.selected > .thumbnailSelectionRing > .thumbnailImage {
  opacity: 1;
}

.thumbnail.selected > .thumbnailSelectionRing {
  background-color: var(--b3-list-hover);
  background-clip: padding-box;
  color: rgba(255, 255, 255, 1);
}

.treeWithDeepNesting > .treeItem,
.treeItem > .treeItems {
  margin-left: 20px;
}

.treeItem > a {
  text-decoration: none;
  display: inline-block;
  min-width: 95%;
  height: auto;
  margin-bottom: 1px;
  border-radius: var(--b3-border-radius);
  line-height: 15px;
  user-select: none;
  white-space: normal;
  cursor: pointer;
  padding: 4px 0 4px 4px;
  color: var(--b3-theme-on-background);
}

#layersView .treeItem > a > * {
  cursor: pointer;
}

#layersView .treeItem > a > label {
  padding-left: 4px;
}

.treeItem {
  position: relative;

  .treeItemToggler {
    transition: var(--b3-transition);
    height: 24px;
    width: 24px;
    cursor: pointer;
    text-align: center;
    color: var(--b3-theme-on-surface);
    position: absolute;
    left: -19px;
    top: 2px;

    svg {
      height: 20px;
      width: 8px;
    }

    &.treeItemsHidden {
      & ~ .treeItems {
        display: none;
      }

      transform: rotate(-90deg);
      left: -17px;
      top: 0;
    }

    &:hover {
      color: var(--b3-theme-on-background);
    }
  }
}

.treeItem.selected > a {
  background-color: var(--b3-theme-surface-lighter);
}

.treeItemToggler:hover + a,
.treeItemToggler:hover ~ .treeItems,
.treeItem:not(.selected) > a:hover {
  background-color: var(--b3-list-hover);
}

#errorWrapper {
  background: none repeat scroll 0 0 var(--b3-theme-on-background);
  color: var(--main-color);
  left: 0;
  position: absolute;
  right: 0;
  z-index: 1000;
  padding: 3px 6px;
}

#errorMessageLeft {
  float: left;
}

#errorMessageRight {
  float: right;
}

#errorMoreInfo {
  background-color: var(--b3-theme-surface);
  color: var(--b3-theme-on-surface);
  border: 1px solid var(--b3-theme-surface-lighter);
  padding: 3px;
  margin: 3px;
  width: 98%;
}

.overlayButton {
  width: auto;
  margin: 3px 4px 2px !important;
  padding: 2px 11px;
  color: var(--b3-theme-on-background);
  background-color: var(--b3-theme-background);
  border: 0 none !important;
}

#dialogContainer > .dialog {
  padding: 8px;
  box-shadow: var(--b3-dialog-shadow);
  border: 1px solid var(--b3-theme-surface-lighter);
  background-color: var(--b3-theme-surface);
  border-radius: var(--b3-border-radius-b);
  width: 520px;
  position: absolute;
  inset-inline-start: 0;
  inset-inline-end: 0;
  height: fit-content;
  margin: auto;
  display: none;


  &--open {
    position: fixed;
    inset-block-start: 0;
    inset-block-end: 0;
    max-width: calc((100% - 6px) - 2em);
    max-height: calc((100% - 6px) - 2em);
    user-select: text;
    display: block;
    z-index: 100;
    overflow: auto;
  }

  & > .row {
    display: table-row;

    & > span {
      white-space: nowrap;
      color: var(--b3-theme-on-surface);
      padding-right: 8px;
    }
  }

  & > .row > * {
    display: table-cell;
    color: var(--b3-theme-on-background);
  }

  .toolbarField {
    margin: 5px 0;
  }

  .separator {
    display: block;
    margin: 4px 0;
    height: 1px;
    width: 100%;
    background-color: var(--b3-theme-surface-lighter);
  }

  .buttonRow {
    text-align: center;
    vertical-align: middle;
  }

  :link {
    color: rgba(255, 255, 255, 1);
  }
}

#passwordOverlay > dialog {
  text-align: center;
}

#passwordOverlay .toolbarField {
  width: 200px;
}

#documentPropertiesOverlay > dialog {
  text-align: left;
}

#documentPropertiesOverlay .row > * {
  min-width: 100px;
}

#documentPropertiesOverlay .row > * {
  text-align: left;
}

#documentPropertiesOverlay .row > span {
  width: 125px;
  word-wrap: break-word;
}

#documentPropertiesOverlay .row > p {
  max-width: 225px;
  word-wrap: break-word;
}

#documentPropertiesOverlay .buttonRow {
  margin-top: 10px;
}

.clearBoth {
  clear: both;
}

.fileInput {
  background: rgba(255, 255, 255, 1);
  color: rgba(0, 0, 0, 1);
  margin-top: 5px;
  visibility: hidden;
  position: fixed;
  right: 0;
  top: 0;
}

#PDFBug {
  background: none repeat scroll 0 0 rgba(255, 255, 255, 1);
  border: 1px solid rgba(102, 102, 102, 1);
  position: fixed;
  top: 30px;
  right: 0;
  bottom: 0;
  font-size: 10px;
  padding: 0;
  width: 300px;
}

#PDFBug .controls {
  background: rgba(238, 238, 238, 1);
  border-bottom: 1px solid rgba(102, 102, 102, 1);
  padding: 3px;
}

#PDFBug .panels {
  bottom: 0;
  left: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  right: 0;
  top: 27px;
}

#PDFBug .panels > div {
  padding: 5px;
}

#PDFBug button.active {
  font-weight: bold;
}

.debuggerShowText {
  background: none repeat scroll 0 0 rgba(255, 255, 0, 1);
  color: rgba(0, 0, 255, 1);
}

.debuggerHideText:hover {
  background: none repeat scroll 0 0 rgba(255, 255, 0, 1);
}

#PDFBug .stats {
  font-family: courier;
  font-size: 10px;
  white-space: pre;
}

#PDFBug .stats .title {
  font-weight: bold;
}

#PDFBug table {
  font-size: 10px;
}

#PDFBug table.showText {
  border-collapse: collapse;
  text-align: center;
}

#PDFBug table.showText,
#PDFBug table.showText tr,
#PDFBug table.showText td {
  border: 1px solid black;
  padding: 1px;
}

#PDFBug table.showText td.advance {
  color: grey;
}

#viewer.textLayer-visible .textLayer {
  opacity: 1;
}

#viewer.textLayer-visible .canvasWrapper {
  background-color: rgba(128, 255, 128, 1);
}

#viewer.textLayer-visible .canvasWrapper canvas {
  mix-blend-mode: screen;
}

#viewer.textLayer-visible .textLayer span {
  background-color: rgba(255, 255, 0, .1);
  color: rgba(0, 0, 0, 1);
  border: solid 1px rgba(255, 0, 0, .5);
  box-sizing: border-box;
}

#viewer.textLayer-hover .textLayer span:hover {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(0, 0, 0, 1);
}

#viewer.textLayer-shadow .textLayer span {
  background-color: rgba(255, 255, 255, .6);
  color: rgba(0, 0, 0, 1);
}

.editToolbar {
  display: none;
}

.rect-to-annotation {
  cursor: crosshair !important;
  user-select: none;

  *:not(input):not(textarea):not(button):not(select):not(:link) {
    cursor: inherit !important;
    user-select: auto;
  }
}

.grab-to-pan-grab {
  //cursor: url("images/grab.cur"), move !important;
  cursor: grab !important;
}

.grab-to-pan-grab *:not(input):not(textarea):not(button):not(select):not(:link) {
  cursor: inherit !important;
}

.grab-to-pan-grab:active,
.grab-to-pan-grabbing {
  //cursor: url("images/grabbing.cur"), move !important;
  cursor: grabbing !important;
  position: fixed;
  background: rgba(0, 0, 0, 0);
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 50000; /* should be higher than anything else in PDF.js! */
}

@page {
  margin: 0;
}

#printContainer {
  display: none;
}

@media print {
  /* Rules for browsers that don't support mozPrintCallback. */
  #sidebarContainer,
  #secondaryToolbar,
  .toolbar,
  #loadingBox,
  #errorWrapper,
  .textLayer,
  .canvasWrapper {
    display: none;
  }
  #viewerContainer {
    overflow: visible;
  }

  #mainContainer,
  #viewerContainer,
  .page,
  .page canvas {
    position: static;
    padding: 0;
    margin: 0;
  }

  .page {
    float: left;
    display: none;
    border: none;
    box-shadow: none;
    background-clip: content-box;
    background-color: rgba(255, 255, 255, 1);
  }

  .page[data-loaded] {
    display: block;
  }

  .fileInput {
    display: none;
  }

  #printContainer {
    height: 100%;
  }
  /* wrapper around (scaled) print canvas elements */
  #printContainer > .printedPage {
    page-break-after: always;
    page-break-inside: avoid;

    /* The wrapper always cover the whole page. */
    height: 100%;
    width: 100%;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  #printContainer > .xfaPrintedPage .xfaPage {
    position: absolute;
  }

  #printContainer > .xfaPrintedPage {
    page-break-after: always;
    page-break-inside: avoid;
    width: 100%;
    height: 100%;
    position: relative;
  }

  #printContainer > .printedPage canvas,
  #printContainer > .printedPage img {
    /* The intrinsic canvas / image size will make sure that we fit the page. */
    max-width: 100%;
    max-height: 100%;
    display: block;
  }
}

.visibleLargeView,
.visibleMediumView,
.visibleSmallView {
  display: none;
}

@media all and (max-width: 840px) {
  #outerContainer.sidebarOpen #viewerContainer {
    left: 0 !important;
  }

  #outerContainer .hiddenLargeView,
  #outerContainer .hiddenMediumView {
    display: inherit;
  }
  #outerContainer .visibleLargeView,
  #outerContainer .visibleMediumView {
    display: none;
  }
}

@media all and (max-width: 770px) {
  #outerContainer .hiddenLargeView {
    display: none;
  }
  #outerContainer .visibleLargeView {
    display: inherit;
  }
}

@media all and (max-width: 700px) {
  #outerContainer .hiddenMediumView {
    display: none;
  }
  #outerContainer .visibleMediumView {
    display: inherit;
  }
}

@media all and (max-width: 640px) {
  .hiddenSmallView,
  .hiddenSmallView * {
    display: none;
  }
  .visibleSmallView {
    display: inherit;
  }
}

@media all and (max-width: 535px) {
  #scaleSelectContainer {
    display: none;
  }
}
