/**
 * LetisFlow Import Editor — Layout
 *
 * Stage = grid 3 colonne: toolbar | canvas | (panel AI in Giorno 4-5).
 * Per Giorno 2 panel AI è placeholder. PDF multi-pagina renderizzato
 * verticalmente (Acrobat-style) con scroll interno.
 */

/* ── FRAME 3-col flat: toolbar | PDF stage | panel AI ─────────────────
 * Nessun grid annidato. Lo stage è solo il box del PDF. Il panel AI
 * è fratello dello stage, non figlio.
 *
 * Breakpoints:
 *   ≥ 1200px : 3 colonne (toolbar 64 | PDF 1fr | panel 320)
 *   980-1199 : 2 colonne (toolbar 64 | PDF 1fr); panel sotto a tutta larghezza
 *   < 980    : stack verticale (toolbar orizzontale; panel sotto)
 */
.lf-pdf-frame {
  display: grid;
  grid-template-columns: 96px 1fr;
  grid-template-areas:
    "toolbar stage"
    "panel   panel";
  gap: 16px;
  align-items: start;
}
.lf-pdf-frame > .lf-pdf-stage__toolbar { grid-area: toolbar; }
.lf-pdf-frame > .lf-pdf-stage         { grid-area: stage;   min-width: 0; }
.lf-pdf-frame > .lf-pdf-panel         { grid-area: panel;   width: 100%;  }

@media (min-width: 1200px) {
  .lf-pdf-frame {
    grid-template-columns: 96px minmax(0, 1fr) 320px;
    grid-template-areas: "toolbar stage panel";
  }
  .lf-pdf-frame > .lf-pdf-panel { width: auto; }
}
@media (max-width: 767px) {
  .lf-pdf-frame {
    grid-template-columns: 1fr;
    grid-template-areas:
      "toolbar"
      "stage"
      "panel";
    gap: 10px;
  }
}

/* Stage = SOLO il box che contiene il PDF viewport. Niente grid. */
.lf-pdf-stage {
  background: #1a1f2e;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  padding: 10px;
  min-height: 600px;
  position: relative;
  overflow: hidden;
  min-width: 0; /* permette al PDF di restringersi se necessario senza overflow del grid */
}

/* ── TOOLBAR esterna allo stage ──────────────────────────────────────
 * !important strategico per superare CSS dei temi WP che spesso
 * sovrascrivono button/font con priorità alta.
 */
.lf-pdf-stage__toolbar {
  display: flex !important;
  flex-direction: column;
  gap: 8px !important;
  padding: 12px 10px !important;
  background: #0f1720 !important;
  border: 1px solid rgba(255, 255, 255, 0.12) !important;
  border-radius: 14px !important;
  align-self: start;
  position: sticky;
  top: 12px;
  width: 96px !important;
  z-index: 20;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  box-sizing: border-box;
}
@media (max-width: 767px) {
  .lf-pdf-stage__toolbar {
    flex-direction: row !important;
    flex-wrap: wrap;
    width: auto !important;
    position: static;
    padding: 10px 12px !important;
    justify-content: center;
    gap: 8px !important;
  }
}

.lf-pdf-stage__tool {
  background: rgba(255, 255, 255, 0.04) !important;
  border: 1px solid rgba(255, 255, 255, 0.10) !important;
  color: #f9fafb !important;
  border-radius: 10px !important;
  padding: 12px 6px !important;
  cursor: pointer !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 6px !important;
  font-family: inherit !important;
  transition: all 0.12s;
  min-height: 68px !important;
  width: 100%;
  box-sizing: border-box;
  text-align: center !important;
  text-transform: none !important;
  letter-spacing: 0 !important;
  line-height: 1 !important;
  font-size: 1rem !important;
  box-shadow: none !important;
}
@media (max-width: 767px) {
  .lf-pdf-stage__tool {
    min-height: 60px !important;
    min-width: 72px !important;
    width: auto;
    flex: 0 0 auto;
  }
}
.lf-pdf-stage__tool:hover {
  background: rgba(249, 115, 22, 0.16) !important;
  border-color: rgba(249, 115, 22, 0.45) !important;
  color: #fff !important;
  transform: translateY(-1px);
}
.lf-pdf-stage__tool.is-active {
  background: linear-gradient(180deg, #fb923c, #f97316) !important;
  color: #0b0b0f !important;
  border-color: #f97316 !important;
  box-shadow: 0 4px 12px rgba(249, 115, 22, 0.45) !important;
}
.lf-pdf-stage__tool[disabled] {
  opacity: 0.35;
  cursor: not-allowed !important;
}

/* Icona (primo span) */
.lf-pdf-stage__tool > span:first-child {
  font-size: 1.5rem !important;
  line-height: 1 !important;
  letter-spacing: 0 !important;
  display: block !important;
  pointer-events: none;
}
/* Label */
.lf-pdf-stage__tool-label {
  font-size: 0.72rem !important;
  letter-spacing: 0.2px !important;
  font-weight: 700 !important;
  line-height: 1.1 !important;
  white-space: nowrap !important;
  display: block !important;
  pointer-events: none;
}

/* Divisore tra modify-tools e undo/redo */
.lf-pdf-stage__toolbar > [data-tool="undo"] {
  margin-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.10) !important;
  padding-top: 16px !important;
}
@media (max-width: 767px) {
  .lf-pdf-stage__toolbar > [data-tool="undo"] {
    margin-top: 0;
    border-top: none !important;
    padding-top: 12px !important;
  }
}

/* Status indicator (Salvato / Salvataggio... / Errore) */
.lf-pdf-stage__status {
  margin-top: 8px;
  padding: 6px 4px;
  font-size: 0.6rem;
  text-align: center;
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.55);
  background: rgba(255, 255, 255, 0.04);
  line-height: 1.3;
  word-break: break-word;
  min-height: 18px;
}
.lf-pdf-stage__status[data-kind="ok"]      { color: #86efac; background: rgba(34,197,94,0.12); }
.lf-pdf-stage__status[data-kind="pending"] { color: #fdba74; background: rgba(249,115,22,0.12); }
.lf-pdf-stage__status[data-kind="err"]     { color: #fca5a5; background: rgba(239,68,68,0.12); }
@media (max-width: 767px) {
  .lf-pdf-stage__status {
    flex-basis: 100%;
    margin-top: 4px;
    text-align: center;
  }
}

/* ── VIEWPORT scrollabile delle pagine ───────────────────────────────── */
.lf-pdf-stage__viewport {
  position: relative;
  background: #525659;
  border-radius: 10px;
  padding: 18px 12px;
  max-height: 80vh;
  overflow-y: auto;
  overflow-x: auto;
  width: 100%;
  box-sizing: border-box;
}

.lf-pdf-stage__pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  min-height: 100%;
}

/* Una pagina = wrapper relativo che ospita canvas PDF.js + overlay Fabric */
.lf-pdf-page {
  position: relative;
  background: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  border-radius: 2px;
  flex-shrink: 0;
}
.lf-pdf-page__canvas {
  display: block;
}

/* Fabric.js v6 wrappa il canvas in <div class="canvas-container">.
 * Forzato absolute SOPRA il canvas PDF e SOPRA il textLayer.
 * pointer-events:auto con !important per garantire ricezione eventi
 * (necessario per Sposta/Testo/Cover/AreaAI). */
.lf-pdf-page .canvas-container {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  pointer-events: auto !important;
  z-index: 10 !important;
}
.lf-pdf-page .canvas-container > canvas,
.lf-pdf-page__fabric {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  pointer-events: auto !important;
}

.lf-pdf-page__num {
  position: absolute;
  top: -22px;
  left: 0;
  font-size: 0.7rem;
  color: rgba(255, 255, 255, 0.55);
  font-weight: 600;
  letter-spacing: 0.5px;
  font-family: 'DejaVu Sans', sans-serif;
}

/* ── STATI: loading / error ──────────────────────────────────────────── */
.lf-pdf-stage__msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 320px;
  color: rgba(255, 255, 255, 0.7);
  text-align: center;
  padding: 40px 24px;
}
.lf-pdf-stage__msg .lf-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: #f97316;
  border-radius: 50%;
  animation: lf-spin 0.8s linear infinite;
}
.lf-pdf-stage__msg-title {
  font-weight: 700;
  font-size: 0.95rem;
}
.lf-pdf-stage__msg-detail {
  font-size: 0.82rem;
  opacity: 0.75;
  max-width: 480px;
  line-height: 1.5;
}

/* ── PANEL AI (fratello dello stage, non figlio) ─────────────────────── */
.lf-pdf-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: #0f1720;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  align-self: start;
  box-sizing: border-box;
  max-height: calc(100vh - 24px);
  overflow-y: auto;
}
@media (min-width: 1200px) {
  /* Sticky solo sul desktop large dove è una colonna laterale */
  .lf-pdf-panel {
    position: sticky;
    top: 12px;
    z-index: 20;
  }
}
.lf-pdf-panel__title {
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: rgba(255, 255, 255, 0.55);
  margin-bottom: 2px;
}

.lf-aip-mode {
  background: rgba(56, 189, 248, 0.06);
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.lf-aip-mode__label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: rgba(255, 255, 255, 0.5);
}
.lf-aip-mode__options {
  display: flex;
  gap: 4px;
}
.lf-aip-mode__btn {
  flex: 1;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.10);
  color: rgba(255, 255, 255, 0.85);
  border-radius: 6px;
  padding: 6px 4px;
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.12s;
  white-space: nowrap;
}
.lf-aip-mode__btn:hover {
  background: rgba(56, 189, 248, 0.15);
  border-color: rgba(56, 189, 248, 0.4);
}
.lf-aip-mode__btn.is-active {
  background: linear-gradient(180deg, #38bdf8, #0ea5e9);
  border-color: #0ea5e9;
  color: #0b0b0f;
  box-shadow: 0 2px 8px rgba(56, 189, 248, 0.4);
}

.lf-aip-context {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  padding: 8px 10px;
}
.lf-aip-context__label {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: rgba(255, 255, 255, 0.45);
  margin-bottom: 4px;
}
.lf-aip-context__body {
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.78);
  line-height: 1.45;
  word-break: break-word;
}
.lf-aip-context.is-active {
  background: rgba(249, 115, 22, 0.1);
  border-color: rgba(249, 115, 22, 0.3);
}
.lf-aip-context.is-active .lf-aip-context__label { color: #fb923c; }

.lf-aip-templates {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.lf-aip-tpl {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #e2e8f0;
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.lf-aip-tpl:hover  { background: rgba(249, 115, 22, 0.18); border-color: rgba(249, 115, 22, 0.4); }
.lf-aip-tpl.active { background: #f97316; border-color: #f97316; color: #0b0b0f; }

.lf-aip-input {
  width: 100%;
  box-sizing: border-box;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  color: #fff;
  padding: 8px 10px;
  font-size: 0.82rem;
  line-height: 1.5;
  resize: vertical;
  font-family: inherit;
}
.lf-aip-input:focus {
  outline: none;
  border-color: #f97316;
  box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.18);
}

.lf-aip-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.lf-aip-counter {
  font-size: 0.7rem;
  color: rgba(255, 255, 255, 0.55);
  font-weight: 600;
}
.lf-aip-counter.exhausted { color: #fca5a5; }
.lf-aip-send {
  background: linear-gradient(90deg, #f97316, #fb923c);
  color: #0b0b0f;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
.lf-aip-send:disabled { opacity: 0.45; cursor: not-allowed; }

.lf-aip-msg {
  font-size: 0.78rem;
  line-height: 1.5;
  padding: 8px 10px;
  border-radius: 6px;
}
.lf-aip-msg[data-kind="info"]  { background: rgba(56, 189, 248, 0.1); color: #7dd3fc; border: 1px solid rgba(56,189,248,0.25); }
.lf-aip-msg[data-kind="warn"]  { background: rgba(245, 158, 11, 0.1); color: #fcd34d; border: 1px solid rgba(245,158,11,0.3); }
.lf-aip-msg[data-kind="err"]   { background: rgba(239, 68, 68, 0.1);  color: #fca5a5; border: 1px solid rgba(239,68,68,0.25); }

.lf-aip-pending {
  background: rgba(34, 197, 94, 0.08);
  border: 1px solid rgba(34, 197, 94, 0.3);
  border-radius: 8px;
  padding: 10px;
}
.lf-aip-pending__title {
  font-size: 0.85rem;
  font-weight: 700;
  color: #86efac;
  margin-bottom: 4px;
}
.lf-aip-pending__hint {
  font-size: 0.74rem;
  color: rgba(255, 255, 255, 0.6);
  margin-bottom: 10px;
  line-height: 1.4;
}
.lf-aip-pending__actions {
  display: flex;
  gap: 6px;
}
.lf-aip-pending__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.lf-aip-btn {
  flex: 1 1 calc(50% - 3px);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  border: 1px solid transparent;
  transition: all 0.12s;
}
.lf-aip-btn--ghost   { background: transparent;          color: rgba(255,255,255,0.7);  border-color: rgba(255,255,255,0.18); }
.lf-aip-btn--ghost:hover { background: rgba(255,255,255,0.06); color: #fff; }
.lf-aip-btn--preview { background: rgba(56, 189, 248, 0.15); color: #7dd3fc; border-color: rgba(56, 189, 248, 0.35); }
.lf-aip-btn--preview:hover { background: rgba(56, 189, 248, 0.25); }
.lf-aip-btn--primary { background: #22c55e; color: #0b0b0f; }
.lf-aip-btn--primary:hover { background: #16a34a; }
.lf-aip-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.lf-aip-pending__diag {
  font-size: 0.72rem;
  color: rgba(255, 255, 255, 0.55);
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  padding: 6px 8px;
  margin-bottom: 8px;
  line-height: 1.5;
  font-family: 'SF Mono', Menlo, monospace;
  max-height: 100px;
  overflow-y: auto;
}

/* ── FLOATING BUTTON "Modifica con AI" (su selezione testo) ─────────── */
.lf-pdf-floating-ai {
  position: absolute;
  z-index: 50;
  background: #f97316;
  color: #0b0b0f;
  border: none;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 6px 20px rgba(249,115,22,0.4);
  white-space: nowrap;
}
.lf-pdf-floating-ai:hover { transform: translateY(-1px); }

/* ── TEXT LAYER PDF.js — fuori dal DOM tranne in text-select ──────────
 *
 * Approccio radicale: display:none di default. Il textLayer non esiste
 * proprio nel layout, quindi NON PUÒ intercettare eventi.
 * Solo in mode text-select il textLayer riappare e prende il controllo.
 *
 * pointer-events:none + !important sono ridondanti ma li teniamo come
 * defensive layer in caso display:none venga sovrascritto.
 */
.lf-pdf-page__textlayer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  opacity: 0;
  line-height: 1.0;
  display: none !important;        /* ← rimosso da layout di default */
  pointer-events: none !important;
}
.lf-pdf-page__textlayer * {
  pointer-events: none !important;
}
.lf-pdf-page__textlayer > span {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: inherit;
  transform-origin: 0% 0%;
}

/* MODE text-select: textLayer attivo per selezione testo */
.lf-pdf-stage[data-tool="text-select"] .lf-pdf-page__textlayer {
  display: block !important;
  pointer-events: auto !important;
  cursor: text;
  user-select: text;
}
.lf-pdf-stage[data-tool="text-select"] .lf-pdf-page__textlayer * {
  pointer-events: auto !important;
}
.lf-pdf-stage[data-tool="text-select"] .lf-pdf-page__fabric,
.lf-pdf-stage[data-tool="text-select"] .lf-pdf-page .canvas-container {
  pointer-events: none !important;
}
.lf-pdf-page__textlayer ::selection { background: rgba(249, 115, 22, 0.45); }

/* ── DEBUG HUD ────────────────────────────────────────────────────────
 * Box flottante in alto a destra dello stage, sempre visibile, mostra
 * lo stato real-time di tool/eventi/fabric. Permette di diagnosticare
 * senza aprire DevTools.
 */
.lf-debug-hud {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 100;
  background: rgba(0, 0, 0, 0.85);
  color: #86efac;
  font-family: 'SF Mono', Menlo, monospace;
  font-size: 0.65rem;
  line-height: 1.4;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(34, 197, 94, 0.3);
  min-width: 240px;
  max-width: 320px;
  pointer-events: none;
  white-space: pre-wrap;
}
.lf-debug-hud.is-warn { color: #fcd34d; border-color: rgba(245, 158, 11, 0.5); }
.lf-debug-hud.is-err  { color: #fca5a5; border-color: rgba(239, 68, 68, 0.6); }
.lf-debug-hud-line { display: block; }
.lf-debug-hud-key  { color: #94a3b8; }
.lf-debug-hud-val  { color: inherit; font-weight: bold; }

/* ── PREVIEW AI overlay (verde tratteggiato) — applicato via dataset ── */
.lf-pdf-page__fabric.has-pending::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  border: 2px dashed rgba(34, 197, 94, 0.45);
  border-radius: 4px;
}

/* ── BANNER NOTA "PDF originale" sopra lo stage ──────────────────────── */
.lf-pdf-stage__hint {
  font-size: 0.78rem;
  line-height: 1.5;
  color: rgba(255, 255, 255, 0.65);
  background: rgba(249, 115, 22, 0.08);
  border: 1px solid rgba(249, 115, 22, 0.25);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
}
.lf-pdf-stage__hint strong { color: #fb923c; }

/* ── TESTO ESTRATTO (declassato, tab secondaria) ─────────────────────── */
.lf-text-fallback-toggle {
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.5);
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: underline;
  padding: 4px 0;
  font-family: inherit;
}
.lf-text-fallback-toggle:hover {
  color: #fb923c;
}

/* AI disabled for initial launch — collapse 3rd column when panel is not rendered */
@media (min-width: 1200px) {
  .lf-pdf-frame--ai-off {
    grid-template-columns: 96px 1fr !important;
    grid-template-areas: "toolbar stage" !important;
  }
}
