:root {
  --primary-blue: #153c5e;
  --accent-gold: #b38b36;
  --bg-gray: #f0f4f8;
  --text-dark: #2b3035;
  --white: #ffffff;
  --muted: #5f6872;
  --border: #d9e2ec;
  --soft-border: #e7edf3;
  --control-bg: #fbfdff;
  --control-hover: #fff9ed;
  --shadow-soft: 0 8px 22px rgba(21, 60, 94, 0.09);
  --sidebar-width: 278px;
  --mobile-sidebar-width: min(340px, calc(100vw - 28px));
  --header-height: 78px;
  --zoom: 1;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
  scroll-behavior: auto;
  overflow-anchor: none;
  overflow: hidden;
}

body {
  margin: 0;
  height: 100vh;
  min-height: 100vh;
  overflow: hidden;
  overflow-anchor: none;
  background: var(--bg-gray);
  color: var(--text-dark);
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

button,
input {
  font: inherit;
}

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

/* Header allineato pixel-per-pixel a quello della pagina principale
   (stesso logo, tipografia, padding e bordo) per uno switch senza scatti */
.viewer-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  gap: 18px;
  align-items: center;
  padding: 10px 50px;
  background: var(--white);
  border-bottom: 3px solid var(--accent-gold);
}

.document-heading {
  display: flex;
  flex: 1 1 auto;
  gap: 18px;
  align-items: center;
  min-width: 0;
}

.brand {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  color: var(--primary-blue);
  text-decoration: none;
  transition: color 120ms ease;
}

.brand #header-logo {
  max-height: 70px;
  object-fit: contain;
  margin-right: 5px;
  margin-bottom: 5px;
}

.brand .nome-logo {
  margin: 10px 0 0;
  color: var(--primary-blue);
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  transition: color 120ms ease;
}

.brand:hover .nome-logo {
  color: var(--accent-gold);
}

#document-title {
  margin: 0;
  overflow: hidden;
  padding-left: 18px;
  border-left: 1px solid var(--border);
  color: var(--primary-blue);
  font-size: 1.08rem;
  letter-spacing: -0.01em;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  margin-left: auto;
}

.toolbar button,
.toolbar a {
  min-height: 40px;
  border: 1px solid var(--soft-border);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--primary-blue);
  cursor: pointer;
  font-weight: 700;
  line-height: 1;
  text-decoration: none;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    color 120ms ease,
    box-shadow 120ms ease;
}

.toolbar button {
  padding: 0 12px;
}

.toolbar a {
  display: inline-flex;
  align-items: center;
  padding: 0 12px;
}

.toolbar .icon-button {
  justify-content: center;
  width: 40px;
  padding: 0;
}

.icon-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.toolbar a svg {
  width: 17px;
  height: 17px;
  margin-right: 7px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.toolbar .icon-button svg {
  margin-right: 0;
}

.zoom-controls button svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.2;
}

.toolbar button:hover:not(:disabled),
.toolbar a:hover {
  border-color: var(--accent-gold);
  background: var(--control-hover);
  color: var(--accent-gold);
}

.toolbar button:active:not(:disabled),
.toolbar a:active {
  background: rgba(179, 139, 54, 0.12);
  transform: translateY(0.5px);
}

.toolbar button:disabled {
  opacity: 0.38;
  cursor: default;
}

.toolbar button:focus-visible,
.toolbar a:focus-visible,
.sidebar-tab:focus-visible,
.search-nav-button:focus-visible,
.pdf-annotation-link:focus-visible,
.thumbnail-button:focus-visible,
.search-result-button:focus-visible,
.page-link:focus-visible {
  outline: 3px solid rgba(179, 139, 54, 0.5);
  outline-offset: 2px;
}

.zoom-controls,
.page-indicator {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  border: 1px solid var(--soft-border);
  border-radius: 6px;
  overflow: hidden;
  background: var(--control-bg);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.55);
}

.page-indicator {
  gap: 4px;
  padding: 0 12px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.page-indicator:focus-within {
  border-color: rgba(179, 139, 54, 0.58);
  box-shadow:
    0 0 0 3px rgba(179, 139, 54, 0.14),
    inset 0 0 0 1px rgba(255, 255, 255, 0.75);
}

.page-input-label {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
}

#page-input {
  width: 36px;
  border: 0;
  appearance: textfield;
  -moz-appearance: textfield;
  background: transparent;
  color: var(--primary-blue);
  font-weight: 800;
  text-align: center;
  outline: 0;
}

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

.page-total {
  white-space: nowrap;
}

.zoom-controls:focus-within {
  border-color: rgba(179, 139, 54, 0.58);
  box-shadow:
    0 0 0 3px rgba(179, 139, 54, 0.14),
    inset 0 0 0 1px rgba(255, 255, 255, 0.75);
}

.zoom-controls button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  border: 0;
  border-radius: 0;
  background: transparent;
  font-size: 1.04rem;
}

.zoom-controls button + .zoom-input-label {
  border-left: 1px solid var(--soft-border);
}

.zoom-input-label {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 44px;
  min-width: 78px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.zoom-input-label::after {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 1px;
  content: "";
  background: var(--soft-border);
  pointer-events: none;
}

#zoom-input {
  width: 42px;
  border: 0;
  appearance: textfield;
  -moz-appearance: textfield;
  background: transparent;
  color: var(--primary-blue);
  font-weight: 800;
  text-align: center;
  outline: 0;
}

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

.sidebar-tab {
  position: fixed;
  top: calc(var(--header-height) + 14px);
  left: 12px;
  z-index: 40;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid rgba(21, 60, 94, 0.16);
  border-radius: 999px;
  padding: 0;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 6px 18px rgba(21, 60, 94, 0.14);
  color: var(--primary-blue);
  cursor: pointer;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.sidebar-tab svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.sidebar-tab:hover {
  border-color: var(--accent-gold);
  color: var(--accent-gold);
}

body:not(.sidebar-collapsed) .sidebar-tab {
  left: calc(var(--sidebar-width) + 12px);
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  height: calc(100vh - var(--header-height));
  min-height: 0;
  overflow: hidden;
}

body.sidebar-collapsed .app-shell {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar {
  position: relative;
  align-self: start;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: contain;
  background: var(--white);
  border-right: 1px solid var(--soft-border);
  transition: transform 160ms ease;
}

body.sidebar-collapsed .sidebar {
  pointer-events: none;
  transform: translateX(-100%);
}

.search-panel,
.thumbnail-panel {
  padding: 14px;
  border-bottom: 1px solid var(--soft-border);
}

.search-panel {
  position: relative;
  flex: 0 0 auto;
  z-index: 5;
  background: var(--white);
  box-shadow: 0 10px 18px rgba(21, 60, 94, 0.06);
}

.thumbnail-panel {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.panel-heading h2 {
  display: block;
  margin: 0 0 8px;
  color: var(--primary-blue);
  font-size: 0.94rem;
  font-weight: 800;
}

.search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px 44px;
  gap: 8px;
  align-items: center;
}

.search-box {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--soft-border);
  border-radius: 6px;
  padding: 0 10px 0 0;
  background: var(--control-bg);
  color: var(--text-dark);
  cursor: text;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    box-shadow 120ms ease;
}

.search-box svg,
.search-nav-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.search-box:focus-within {
  border-color: rgba(179, 139, 54, 0.58);
  box-shadow: 0 0 0 3px rgba(179, 139, 54, 0.14);
}

.search-icon {
  grid-column: 1;
  grid-row: 1;
  color: var(--muted);
  justify-self: center;
  pointer-events: none;
}

.search-clear-button {
  grid-column: 1;
  grid-row: 1;
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 5px;
  padding: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.search-clear-button:hover,
.search-clear-button:focus-visible {
  background: rgba(21, 60, 94, 0.08);
  color: var(--primary-blue);
  outline: 0;
}

body.search-active .search-icon {
  display: none;
}

body.search-active .search-clear-button {
  display: inline-flex;
}

.search-box input {
  grid-column: 2;
  grid-row: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text-dark);
  font-weight: 600;
}

.search-box input::placeholder {
  color: #7a8591;
}

.search-box input::-webkit-search-cancel-button,
.search-box input::-webkit-search-decoration {
  -webkit-appearance: none;
}

.search-nav-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--soft-border);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--primary-blue);
  cursor: pointer;
  font-weight: 700;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    color 120ms ease;
}

.search-nav-button:hover {
  border-color: var(--accent-gold);
  background: var(--control-hover);
  color: var(--accent-gold);
}

.search-status {
  min-height: 20px;
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 600;
}

.search-status:empty {
  display: none;
}

.panel-heading {
  display: flex;
  gap: 10px;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
}

.panel-heading h2 {
  margin: 0;
}

#page-count {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.thumbnail-list {
  display: grid;
  gap: 10px;
}

body.search-active .thumbnail-button {
  display: none;
}

body.search-active .thumbnail-list {
  gap: 6px;
}

.thumbnail-button {
  position: relative;
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 104px;
  border: 1px solid var(--soft-border);
  border-radius: 6px;
  padding: 8px;
  background: var(--control-bg);
  color: var(--primary-blue);
  cursor: pointer;
  text-align: left;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    box-shadow 120ms ease;
}

.thumbnail-button:hover,
.thumbnail-button.is-active {
  border-color: var(--accent-gold);
  background: var(--control-hover);
  box-shadow: 0 6px 16px rgba(21, 60, 94, 0.07);
}

.thumbnail-button canvas {
  justify-self: center;
  max-width: 86px;
  max-height: 86px;
  border: 1px solid var(--soft-border);
  background: var(--white);
}

.thumbnail-meta {
  min-width: 0;
}

.thumbnail-title {
  display: block;
  font-size: 0.9rem;
  font-weight: 800;
  line-height: 1.2;
}

.thumbnail-hits {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.thumbnail-button.has-matches .thumbnail-hits {
  color: var(--accent-gold);
}

.search-result-button {
  display: grid;
  gap: 4px;
  width: 100%;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 8px 9px;
  background: transparent;
  color: var(--text-dark);
  cursor: pointer;
  text-align: left;
  transition:
    background-color 120ms ease,
    border-color 120ms ease;
}

.search-result-button:hover {
  border-color: var(--soft-border);
  background: var(--control-hover);
}

.search-result-button.is-active {
  border-color: var(--accent-gold);
  background: var(--control-hover);
}

.search-result-page {
  color: var(--primary-blue);
  font-size: 0.78rem;
  font-weight: 800;
}

.search-result-snippet {
  overflow: hidden;
  color: var(--muted);
  display: -webkit-box;
  font-size: 0.82rem;
  line-height: 1.35;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.search-result-snippet mark {
  border-radius: 2px;
  padding: 0 1px;
  background: rgba(255, 220, 82, 0.42);
  color: inherit;
  font-weight: 800;
}

.document-area {
  position: relative;
  min-width: 0;
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px 18px 42px;
}

.status {
  color: var(--muted);
}

.status.error {
  max-width: 920px;
  margin: 0 auto 16px;
  color: #9c2f2f;
  font-weight: 700;
}

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

.status:empty {
  display: none;
}

.viewer {
  width: 100%;
  overflow-x: auto;
  overflow-anchor: none;
  padding: 0 0 32px;
}

.viewer:focus-visible {
  outline: 3px solid rgba(179, 139, 54, 0.5);
  outline-offset: 2px;
}

.viewer-loader {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  padding: 28px;
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.82), rgba(240, 244, 248, 0.76) 54%, rgba(240, 244, 248, 0.52));
  pointer-events: none;
  transition: opacity 220ms ease, visibility 220ms ease;
}

.viewer-loader.is-hidden {
  visibility: hidden;
  opacity: 0;
}

.loader-card {
  display: grid;
  gap: 13px;
  width: min(360px, 100%);
  padding: 18px 20px;
  color: var(--primary-blue);
  font-size: 0.92rem;
  font-weight: 800;
  text-align: center;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--soft-border);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(21, 60, 94, 0.12);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.loader-track {
  position: relative;
  height: 5px;
  overflow: hidden;
  background: #e8eef4;
  border-radius: 999px;
}

.loader-track span {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -42%;
  width: 42%;
  border-radius: inherit;
  background: linear-gradient(90deg, rgba(179, 139, 54, 0), var(--accent-gold), rgba(179, 139, 54, 0));
  animation: loader-progress 1.1s ease-in-out infinite;
}

.page-shell {
  position: relative;
  margin: 0 auto 22px;
  overflow-anchor: none;
}

.pdf-page {
  position: absolute;
  top: 0;
  left: 50%;
  overflow: hidden;
  background: var(--white);
  box-shadow: 0 8px 24px rgba(21, 60, 94, 0.14);
  transform: translateX(-50%) scale(var(--page-scale, var(--zoom)));
  transform-origin: top center;
}

.pdf-page::before {
  position: absolute;
  inset: 0;
  z-index: 1;
  content: "";
  background:
    linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.7), transparent),
    linear-gradient(#eef3f7, #eef3f7);
  background-size: 45% 100%, 100% 100%;
  background-position: -80% 0, 0 0;
  animation: page-shimmer 1.2s linear infinite;
}

.pdf-page.is-rendered::before {
  display: none;
}

.pdf-page canvas {
  display: block;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 180ms ease;
}

.pdf-page.is-rendered canvas {
  opacity: 1;
}

.text-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  line-height: 1;
  text-size-adjust: none;
  forced-color-adjust: none;
}

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

.text-layer ::selection {
  color: transparent;
  background: rgba(21, 60, 94, 0.32);
}

.annotation-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.highlight-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.search-highlight {
  position: absolute;
  border-radius: 1px;
  background: rgba(255, 220, 82, 0.34);
  box-shadow: none;
  mix-blend-mode: multiply;
}

.search-highlight.is-active {
  background: rgba(255, 196, 46, 0.48);
  box-shadow: none;
}

.pdf-annotation-link,
.page-link {
  position: absolute;
  display: block;
  border-radius: 2px;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
}

/* Durante la selezione del testo i link non devono intercettare il drag */
body.is-text-selecting .pdf-annotation-link,
body.is-text-selecting .page-link {
  pointer-events: none;
}

.pdf-annotation-link:hover,
.page-link:hover,
.pdf-annotation-link.is-group-hover,
.page-link.is-group-hover {
  background: rgba(179, 139, 54, 0.16);
  box-shadow: inset 0 0 0 1px rgba(179, 139, 54, 0.45);
}

.page-shell.search-focus .pdf-page {
  box-shadow:
    0 0 0 3px rgba(179, 139, 54, 0.55),
    0 8px 24px rgba(21, 60, 94, 0.14);
}

@keyframes page-shimmer {
  to {
    background-position: 180% 0, 0 0;
  }
}

@keyframes loader-progress {
  to {
    left: 100%;
  }
}

@media screen and (max-width: 860px) {
  :root {
    --header-height: 142px;
  }

  .sidebar-tab {
    top: calc(var(--header-height) + 12px);
  }

  body:not(.sidebar-collapsed) .sidebar-tab {
    left: 16px;
  }

  .toolbar button,
  .toolbar a,
  .zoom-controls {
    min-height: 44px;
  }

  .toolbar .icon-button,
  .zoom-controls button {
    width: 44px;
    min-width: 44px;
  }

  .app-shell {
    display: block;
    height: calc(100dvh - var(--header-height));
  }

  .sidebar {
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    left: 0;
    z-index: 35;
    width: var(--mobile-sidebar-width);
    height: auto;
    max-height: none;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    box-shadow: 14px 0 32px rgba(21, 60, 94, 0.16);
  }

  body:not(.sidebar-collapsed) .search-panel {
    padding-left: 64px;
  }

  .document-area {
    height: 100%;
    min-height: 0;
    padding: 14px 10px 34px;
  }

  .viewer {
    padding-bottom: 22px;
  }

  body.sidebar-collapsed .sidebar {
    display: flex;
    pointer-events: none;
    transform: translateX(-105%);
  }
}

@media screen and (min-width: 681px) and (max-width: 860px) {
  :root {
    --header-height: 92px;
    --mobile-sidebar-width: 360px;
  }
}

/* Header compatto: stesso breakpoint e stesse proporzioni della home */
@media screen and (max-width: 768px) {
  .viewer-header {
    flex-direction: column;
    gap: 8px;
    align-items: center;
    padding: 14px 16px;
  }

  .document-heading {
    flex-direction: column;
    gap: 4px;
    text-align: center;
  }

  .brand {
    flex-direction: column;
  }

  .brand #header-logo {
    max-height: 58px;
    margin-right: 0;
    margin-bottom: 4px;
  }

  .brand .nome-logo {
    margin: 0;
    font-size: 1.55rem;
  }

  #document-title {
    padding-left: 0;
    border-left: 0;
    font-size: 1rem;
    white-space: normal;
  }

  .toolbar {
    margin-left: 0;
    justify-content: center;
  }
}

@media screen and (max-width: 430px) {
  .zoom-input-label {
    min-width: 72px;
    padding-inline: 8px;
  }

  .toolbar a {
    padding-inline: 11px;
  }

  .search-panel,
  .thumbnail-panel {
    padding: 12px;
  }

  .thumbnail-button {
    grid-template-columns: 86px minmax(0, 1fr);
    min-height: 98px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  .sidebar,
  .page-shell,
    .pdf-page canvas {
    transition: none;
  }

  .pdf-page::before {
    animation: none;
  }

  .loader-track span {
    animation: none;
    left: 0;
    width: 100%;
    background: var(--accent-gold);
  }
}
