/* Visualizzatore condiviso PDF / fogli di calcolo (dalila-file-viewer.js).
   Usa le variabili del tema di laboratorio.css: chiaro e scuro. */

.dfv {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  overflow: hidden;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.dfv:not([open]) {
  display: none;
}

.dfv::backdrop {
  background: transparent;
}

.dfv-open,
.dfv-open body {
  overflow: hidden;
}

.dfv-backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay, rgba(2, 6, 13, 0.62));
  backdrop-filter: blur(3px);
}

.dfv-panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(1180px, 100%);
  height: min(900px, 100%);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
}

.dfv-header {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 12px 14px 10px 16px;
  border-bottom: 1px solid var(--border);
}

.dfv-badge {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  min-width: 42px;
  height: 30px;
  padding: 0 6px;
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.dfv-badge[data-kind="pdf"] {
  background: rgba(225, 72, 72, 0.16);
  color: #e26060;
}

.dfv-badge[data-kind="spreadsheet"] {
  background: rgba(32, 160, 96, 0.16);
  color: #2fa86b;
}

.dfv-heading {
  flex: 1 1 auto;
  min-width: 0;
}

.dfv-title {
  margin: 0;
  overflow: hidden;
  font-size: 1rem;
  font-weight: 650;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dfv-meta {
  margin: 2px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.8rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dfv-actions {
  display: flex;
  flex: 0 1 auto;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.dfv-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 34px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease;
}

.dfv-button:hover {
  background: var(--surface-soft);
}

.dfv-button:focus-visible,
.dfv-inline-action:focus-visible,
.dfv-link:focus-visible,
.dfv-thumb:focus-visible,
.dfv-sheet-tab:focus-visible,
.dfv-name-box:focus-visible,
.dfv-page-input:focus-visible,
.dfv-compare-select:focus-visible,
.dfv-grid-body:focus-visible,
.dfv-pdf-scroll:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.dfv-button:disabled {
  opacity: 0.5;
  cursor: default;
}

.dfv-download {
  border-color: transparent;
  background: var(--primary);
  color: #fff;
}

.dfv-download:hover {
  background: var(--primary-hover);
}

.dfv-close {
  padding: 0;
}

.dfv-compare-select {
  height: 34px;
  max-width: 260px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
}

.dfv-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.dfv-toolbar[hidden],
.dfv-notes[hidden],
.dfv-status[hidden] {
  display: none;
}

.dfv-toolbar-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.dfv-page-input {
  width: 64px;
  height: 34px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-elevated);
  color: var(--text);
  font: inherit;
  text-align: center;
}

.dfv-page-total {
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}

.dfv-zoom-label {
  min-width: 64px;
  font-variant-numeric: tabular-nums;
}

.dfv-thumbs-toggle[aria-pressed="true"] {
  border-color: var(--primary);
  color: var(--primary);
}

.dfv-notes {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.8rem;
}

.dfv-notes p {
  margin: 0;
}

.dfv-notes p + p {
  margin-top: 4px;
}

.dfv-panel > .dfv-header,
.dfv-panel > .dfv-toolbar,
.dfv-panel > .dfv-notes,
.dfv-panel > .dfv-status {
  flex: 0 0 auto;
}

.dfv-body {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: var(--surface);
}

.dfv-status {
  margin: 0;
  padding: 6px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.78rem;
}

.dfv-status[data-tone="warning"] {
  color: var(--danger);
}

.dfv-loading,
.dfv-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 100%;
  padding: 32px;
  color: var(--muted);
  text-align: center;
}

.dfv-error-title {
  max-width: 520px;
  margin: 0;
  color: var(--text);
  font-weight: 600;
}

.dfv-error p {
  margin: 0;
}

.dfv-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: dfv-spin 0.8s linear infinite;
}

@keyframes dfv-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .dfv-spinner {
    animation-duration: 2.4s;
  }
}

/* ---------------- PDF ---------------- */

.dfv-pdf {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  height: 100%;
}

.dfv-pdf.has-thumbs {
  grid-template-columns: 152px minmax(0, 1fr);
}

.dfv-thumbs {
  display: none;
  overflow-y: auto;
  padding: 12px 10px;
  border-right: 1px solid var(--border);
  background: var(--surface-elevated);
}

.dfv-pdf.has-thumbs .dfv-thumbs {
  display: block;
}

.dfv-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 100%;
  margin: 0 0 10px;
  padding: 6px 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.dfv-thumb-frame {
  display: block;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
}

.dfv-thumb-frame canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.dfv-thumb.is-current {
  background: var(--surface-soft);
  color: var(--text);
}

.dfv-thumb.is-current .dfv-thumb-frame {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary);
}

.dfv-pdf-main {
  position: relative;
  min-width: 0;
  height: 100%;
}

.dfv-pdf-scroll {
  position: absolute;
  inset: 0;
  overflow: auto;
  overscroll-behavior: contain;
  background: var(--surface-soft);
}

.dfv-pdf-pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: max-content;
  min-width: 100%;
  padding: 16px;
}

.dfv-pdf-page {
  position: relative;
  flex: 0 0 auto;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
}

.dfv-pdf-page-number {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #9aa6b8;
  font-size: 0.9rem;
}

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

.dfv-pdf-page.has-error::after {
  content: attr(data-error);
  position: absolute;
  inset: auto 0 12px;
  color: #a91e2c;
  font-size: 0.82rem;
  text-align: center;
}

.dfv-pdf-canvas {
  position: absolute;
  inset: 0;
  display: block;
}

/* Livello di testo selezionabile, con le variabili richieste da TextLayer di
   pdf.js (--total-scale-factor, --min-font-size, --font-height, --scale-x). */
.dfv-text-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: clip;
  line-height: 1;
  letter-spacing: normal;
  word-spacing: normal;
  text-align: initial;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  caret-color: CanvasText;
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}

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

.dfv-text-layer > :not(.markedContent),
.dfv-text-layer .markedContent span:not(.markedContent) {
  z-index: 1;
  --font-height: 0;
  font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1;
  --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}

.dfv-text-layer .markedContent {
  display: contents;
}

.dfv-text-layer span[role="img"] {
  cursor: default;
  user-select: none;
}

.dfv-text-layer ::selection {
  background: rgba(18, 136, 255, 0.28);
}

.dfv-text-layer br::selection {
  background: transparent;
}

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

/* ---------------- Foglio ---------------- */

.dfv-sheet {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.dfv-sheet > .dfv-formula-bar,
.dfv-sheet > .dfv-segment,
.dfv-sheet > .dfv-sheet-tabs {
  flex: 0 0 auto;
}

.dfv-sheet > .dfv-grid {
  flex: 1 1 auto;
}

.dfv-formula-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-elevated);
}

.dfv-name-box {
  flex: 0 0 120px;
  width: 120px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: 600 0.82rem ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.dfv-name-box.is-invalid {
  border-color: var(--danger);
}

.dfv-fx {
  color: var(--muted);
  font-style: italic;
  font-weight: 600;
}

.dfv-formula {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 12px;
  min-width: 0;
  max-height: 64px;
  overflow: auto;
  font-size: 0.85rem;
}

.dfv-formula-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  word-break: break-all;
}

.dfv-formula-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.dfv-formula-value {
  color: var(--muted);
}

.dfv-formula-muted {
  color: var(--muted);
  font-size: 0.78rem;
}

.dfv-formula-warning {
  color: var(--danger);
  font-size: 0.8rem;
}

.dfv-segment {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
}

.dfv-segment[hidden] {
  display: none;
}

.dfv-segment button {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.dfv-grid {
  --dfv-rowhead: 48px;
  position: relative;
  display: grid;
  grid-template-columns: var(--dfv-rowhead) minmax(0, 1fr);
  grid-template-rows: 26px minmax(0, 1fr);
  min-height: 0;
  background: var(--surface);
  font-size: 0.82rem;
}

.dfv-grid-corner,
.dfv-grid-colhead,
.dfv-grid-rowhead {
  position: relative;
  overflow: hidden;
  background: var(--surface-soft);
  color: var(--muted);
}

.dfv-grid-corner {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.dfv-grid-colhead {
  border-bottom: 1px solid var(--border);
}

.dfv-grid-rowhead {
  border-right: 1px solid var(--border);
}

.dfv-grid-colhead-inner,
.dfv-grid-rowhead-inner {
  position: absolute;
  top: 0;
  left: 0;
  will-change: transform;
}

.dfv-grid-colhead-inner {
  height: 100%;
}

.dfv-grid-rowhead-inner {
  width: 100%;
}

.dfv-colhead-cell,
.dfv-rowhead-cell {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.74rem;
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.dfv-colhead-cell {
  top: 0;
  height: 100%;
  border-right: 1px solid var(--border);
}

.dfv-rowhead-cell {
  left: 0;
  width: 100%;
  height: 24px;
  border-bottom: 1px solid var(--border);
}

.dfv-colhead-cell.is-selected,
.dfv-rowhead-cell.is-selected {
  background: rgba(18, 136, 255, 0.16);
  color: var(--text);
  font-weight: 600;
}

.dfv-colhead-cell.is-hidden,
.dfv-rowhead-cell.is-hidden {
  opacity: 0.45;
}

.dfv-grid-body {
  position: relative;
  overflow: auto;
  overscroll-behavior: contain;
}

.dfv-grid-canvas {
  position: relative;
}

.dfv-cell {
  position: absolute;
  overflow: hidden;
  padding: 0 6px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  line-height: 23px;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: cell;
}

.dfv-cell.has-fill {
  color: #10213f;
}

.dfv-cell.is-number {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.dfv-cell.is-boolean {
  text-align: center;
}

.dfv-cell.is-error {
  color: var(--danger);
}

.dfv-cell.is-wrapped {
  line-height: 1.25;
  white-space: normal;
}

.dfv-cell.is-merged {
  z-index: 1;
}

.dfv-cell.has-formula::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  border-top: 6px solid var(--primary);
  border-right: 6px solid transparent;
  opacity: 0.6;
}

.dfv-cell.is-missing {
  color: var(--danger);
  font-style: italic;
  text-align: center;
}

.dfv-cell.is-loading {
  background: linear-gradient(90deg, var(--surface) 0%, var(--surface-soft) 50%, var(--surface) 100%);
  background-size: 200% 100%;
  animation: dfv-shimmer 1.4s ease-in-out infinite;
}

.dfv-cell.is-error-block {
  background: repeating-linear-gradient(135deg, var(--surface), var(--surface) 6px, var(--surface-soft) 6px, var(--surface-soft) 12px);
}

@keyframes dfv-shimmer {
  from {
    background-position: 100% 0;
  }

  to {
    background-position: -100% 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .dfv-cell.is-loading {
    animation: none;
  }
}

.dfv-selection {
  position: absolute;
  z-index: 3;
  border: 2px solid var(--primary);
  background: rgba(18, 136, 255, 0.08);
  pointer-events: none;
}

.dfv-sheet-tabs {
  display: flex;
  align-items: stretch;
  gap: 2px;
  overflow-x: auto;
  padding: 0 8px;
  border-top: 1px solid var(--border);
  background: var(--surface-elevated);
  scrollbar-width: thin;
}

.dfv-sheet-tab {
  flex: 0 0 auto;
  max-width: 220px;
  padding: 8px 14px;
  overflow: hidden;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.82rem;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.dfv-sheet-tab.is-active {
  border-bottom-color: var(--primary);
  color: var(--text);
  font-weight: 600;
}

.dfv-sheet-tab.is-hidden-sheet {
  font-style: italic;
  opacity: 0.75;
}

.dfv-sheet-tab:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.dfv-sheet-tabs-note {
  align-self: center;
  padding: 0 10px;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

/* ---------------- Confronto ---------------- */

.dfv-compare {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: auto;
}

.dfv-compare-summary {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.dfv-compare-headline {
  margin: 0 0 6px;
  font-weight: 650;
}

.dfv-compare-summary details {
  margin: 4px 0;
}

.dfv-compare-summary summary {
  cursor: pointer;
}

.dfv-compare-summary ul {
  margin: 4px 0 0;
  padding-left: 20px;
  color: var(--muted);
}

.dfv-verified summary {
  color: var(--success);
}

.dfv-not-verified summary {
  color: var(--danger);
}

.dfv-compare-note,
.dfv-compare-method {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}

.dfv-compare-sheets {
  padding: 8px 16px;
  font-size: 0.85rem;
}

.dfv-compare-sheets p {
  margin: 0;
}

.dfv-compare-table-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
}

.dfv-compare-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.dfv-compare-table th,
.dfv-compare-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.dfv-compare-table th {
  position: sticky;
  top: 0;
  background: var(--surface-soft);
  color: var(--muted);
  font-weight: 600;
}

.dfv-compare-table .dfv-before {
  color: var(--danger);
  word-break: break-word;
}

.dfv-compare-table .dfv-after {
  color: var(--success);
  word-break: break-word;
}

.dfv-link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.dfv-compare-pdf {
  display: grid;
  grid-template-columns: minmax(240px, 0.8fr) minmax(0, 2fr);
  flex: 1 1 auto;
  min-height: 420px;
}

.dfv-compare-diff {
  overflow: auto;
  padding: 10px 14px;
  border-right: 1px solid var(--border);
  font-size: 0.8rem;
}

.dfv-diff-page {
  margin-bottom: 14px;
}

.dfv-diff-title {
  margin-bottom: 4px;
  font-weight: 600;
}

.dfv-diff-line {
  padding: 1px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.76rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.dfv-diff-line.is-removed {
  background: rgba(225, 72, 72, 0.12);
  color: var(--danger);
}

.dfv-diff-line.is-added {
  background: var(--success-bg);
  color: var(--success);
}

.dfv-compare-panes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-width: 0;
}

.dfv-compare-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  border-right: 1px solid var(--border);
}

.dfv-compare-pane h3 {
  margin: 0;
  padding: 8px 12px;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dfv-compare-pane-body {
  position: relative;
  min-height: 0;
}

/* ---------------- Pulsanti in linea (chat, Storage, Cowork) ---------------- */

.dfv-inline-actions {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.dfv-inline-action {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
}

.dfv-inline-action:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.dfv-inline-actions.is-compact .dfv-inline-action {
  height: 24px;
  padding: 0 8px;
  font-size: 0.72rem;
}

/* ---------------- Mobile ---------------- */

@media (max-width: 760px) {
  .dfv {
    padding: 0;
  }

  .dfv-panel {
    width: 100%;
    height: 100%;
    height: 100dvh;
    border: 0;
    border-radius: 0;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .dfv-header {
    flex-wrap: wrap;
    padding: 10px 12px;
  }

  .dfv-heading {
    flex-basis: calc(100% - 100px);
  }

  .dfv-actions {
    flex-basis: 100%;
    justify-content: flex-start;
  }

  .dfv-close {
    position: absolute;
    top: calc(10px + env(safe-area-inset-top));
    right: 12px;
  }

  .dfv-button span {
    display: none;
  }

  .dfv-download span,
  .dfv-revise span,
  .dfv-ask span {
    display: inline;
  }

  .dfv-pdf.has-thumbs {
    grid-template-columns: 136px minmax(0, 1fr);
  }

  .dfv-compare-pdf {
    grid-template-columns: 1fr;
  }

  .dfv-compare-diff {
    max-height: 40vh;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .dfv-compare-panes {
    min-height: 60vh;
  }

  .dfv-name-box {
    flex-basis: 92px;
    width: 92px;
  }
}
