/*
 * PLATFORM-DOCUMENT-PREVIEW-01 — Visionneuse de documents, feuille UNIQUE.
 *
 * ══ UNE SEULE FEUILLE POUR CINQ SURFACES ══
 * Le back-office, le portail client et le site public n'ont pas la même
 * charte. La visionneuse, elle, doit se comporter partout pareil : c'est une
 * fenêtre modale posée AU-DESSUS de la page, pas un composant de la page.
 * Elle ne réutilise donc aucune variable de thème et n'en impose aucune.
 *
 * ══ POURQUOI TOUT EST PRÉFIXÉ .csdv- ══
 * Elle atterrit dans des pages qui ont déjà leurs propres classes. Un préfixe
 * de cinq caractères coûte moins cher qu'une collision découverte en
 * production sur une seule des cinq surfaces.
 *
 * ══ minmax(0, …) PARTOUT ══
 * `1fr` vaut `minmax(auto, 1fr)` : une grille qui contient un nom de fichier
 * long déborde au lieu de le tronquer. Cette leçon a déjà coûté trois
 * correctifs sur ce dépôt ; elle est appliquée d'emblée ici.
 */

.csdv-root {
    --csdv-ink: #0f172a;
    --csdv-paper: #ffffff;
    --csdv-muted: #64748b;
    --csdv-line: #e2e8f0;
    --csdv-accent: #0b3d2e;
    --csdv-accent-soft: #14624a;
    --csdv-gold: #e9b949;
    --csdv-shadow: 0 24px 64px rgba(15, 23, 42, .34);

    position: fixed;
    inset: 0;
    z-index: 20000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: clamp(0px, 2vw, 32px);
    background: rgba(15, 23, 42, .72);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--csdv-ink);
}

.csdv-root[data-open="1"] { display: flex; }

.csdv-shell {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    width: min(1180px, 100%);
    height: min(92vh, 100%);
    max-height: 100%;
    background: var(--csdv-paper);
    border-radius: 14px;
    box-shadow: var(--csdv-shadow);
    overflow: hidden;
}

/* ── En-tête ─────────────────────────────────────────────────────────── */

.csdv-head {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    padding: 14px 16px;
    border-bottom: 1px solid var(--csdv-line);
    background: linear-gradient(180deg, #fbfdfc, #f4f7f6);
}

.csdv-titles { min-width: 0; }

.csdv-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1.3;
    /* Un nom de fichier de 180 caractères ne doit pas pousser le bouton
       de fermeture hors de l'écran. */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.csdv-close {
    flex: none;
    width: 40px;
    height: 40px;
    border: 1px solid var(--csdv-line);
    border-radius: 10px;
    background: #fff;
    color: var(--csdv-ink);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
}

.csdv-close:hover { background: #f1f5f9; }
.csdv-close:focus-visible,
.csdv-btn:focus-visible,
.cs-doc-open:focus-visible {
    outline: 3px solid var(--csdv-gold);
    outline-offset: 2px;
}

/* ── Corps ───────────────────────────────────────────────────────────── */

.csdv-body {
    position: relative;
    min-height: 0;
    overflow: auto;
    background: #eef2f5;
    display: grid;
    place-items: center;
    padding: 0;
}

.csdv-body[data-mode="image"] { padding: 16px; }

/* Un document encadré gère son PROPRE défilement. Laisser en plus le corps
   défiler produisait deux barres imbriquées, dont l'extérieure ne servait à
   rien. Le corps devient donc un simple bloc, sans débordement. */
.csdv-body[data-mode="pdf"],
.csdv-body[data-mode="svg"] {
    display: block;
    padding: 0;
    overflow: hidden;
}

/* ── PDF rendu par PDF.js, dans nos propres canevas ──────────────────── */

.csdv-pdf {
    width: 100%;
    height: 100%;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    background: #52565c;   /* le gris des liseuses : le papier ressort */
    scroll-behavior: smooth;
}

.csdv-pdf:focus-visible {
    outline: 3px solid var(--csdv-gold);
    outline-offset: -3px;
}

.csdv-pdf__pages {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 12px;
    min-height: 100%;
}

/* Chaque page est une feuille : fond blanc, ombre portée, jamais étirée. */
.csdv-pdf__page {
    display: block;
    max-width: 100%;
    background: #fff;
    box-shadow: 0 2px 12px rgba(0, 0, 0, .35);
    border-radius: 2px;
}

.csdv-pdf__status {
    margin: 0;
    padding: 24px 16px;
    color: #e2e8f0;
    font-size: .84rem;
    text-align: center;
}

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

.csdv-frame {
    width: 100%;
    /* La hauteur vient du parent, qui la tient de la grille de la fenêtre.
       `min-height: 60vh` forçait auparavant le cadre à dépasser la zone
       disponible : c'est ce qui créait la seconde barre de défilement. */
    height: 100%;
    border: 0;
    background: #fff;
    display: block;
}

.csdv-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 6px 20px rgba(15, 23, 42, .16);
}

.csdv-media {
    width: min(560px, 100%);
    margin: 32px 16px;
}

.csdv-text {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 20px;
    background: #fff;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: .82rem;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    overflow: auto;
}

/* ── États : chargement, repli, erreur ───────────────────────────────── */

.csdv-state {
    display: grid;
    gap: 10px;
    justify-items: center;
    text-align: center;
    max-width: 460px;
    padding: 40px 24px;
    color: var(--csdv-muted);
}

.csdv-state i { font-size: 2rem; color: var(--csdv-accent-soft); }
.csdv-state strong { color: var(--csdv-ink); font-size: 1rem; }
.csdv-state p { margin: 0; font-size: .85rem; line-height: 1.55; }

/* Le motif technique : présent pour le support, discret pour l'utilisateur. */
.csdv-why { font-size: .76rem; opacity: .72; }

/* Les suites proposées quand le rendu a échoué (§8). */
.csdv-state-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 6px;
}

.csdv-spin {
    width: 28px;
    height: 28px;
    border: 3px solid var(--csdv-line);
    border-top-color: var(--csdv-accent);
    border-radius: 50%;
    animation: csdv-turn .8s linear infinite;
}

@keyframes csdv-turn { to { transform: rotate(360deg); } }

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

/* ── Pied : les capacités, et rien d'autre ───────────────────────────── */

.csdv-foot {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    padding: 12px 16px;
    border-top: 1px solid var(--csdv-line);
    background: #fbfdfc;
}

.csdv-meta {
    min-width: 0;
    font-size: .76rem;
    color: var(--csdv-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.csdv-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.csdv-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    /* 40 px de haut au repos, 44 sur téléphone (plus bas) : la cible tactile
       recommandée. Mesuré à 35 px lors de la recette — le remplissage seul ne
       suffit pas, il faut une hauteur minimale explicite. */
    min-height: 40px;
    padding: 9px 14px;
    border: 1px solid var(--csdv-line);
    border-radius: 9px;
    background: #fff;
    color: var(--csdv-ink);
    font-size: .82rem;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}

.csdv-btn:hover { background: #f1f5f9; }

.csdv-btn--primary {
    background: var(--csdv-accent);
    border-color: var(--csdv-accent);
    color: #fff;
}

.csdv-btn--primary:hover { background: var(--csdv-accent-soft); }

/* ── Le déclencheur, posé par DocumentLink::trigger() ────────────────── */

.cs-doc-open {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    max-width: 100%;
    padding: 6px 11px;
    border: 1px solid var(--csdv-line, #e2e8f0);
    border-radius: 8px;
    background: #fff;
    color: #0b3d2e;
    font: inherit;
    font-size: .82rem;
    font-weight: 600;
    cursor: pointer;
}

.cs-doc-open:hover { background: #f0fdf4; border-color: #a7d7c5; }

.cs-doc-open > span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ── Téléphone : la fenêtre prend tout l'écran ───────────────────────── */

@media (max-width: 640px) {
    .csdv-root {
        padding: 0;
        /* iOS : l'encoche en haut, la barre d'accueil en bas. Sans ces marges,
           le bouton de fermeture passe sous l'encoche et le pied sous la barre
           — deux commandes rendues inatteignables au doigt. */
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    .csdv-shell {
        width: 100%;
        height: 100%;
        max-height: none;
        border-radius: 0;
    }

    .csdv-head { padding: 12px; }
    .csdv-title { font-size: .93rem; }

    /* En dessous de 640 px, le pied passe sur deux lignes : sinon le nom du
       document et deux boutons se disputent 375 px et l'un des deux sort. */
    .csdv-foot {
        grid-template-columns: minmax(0, 1fr);
        gap: 8px;
    }

    .csdv-actions { justify-content: stretch; }
    .csdv-btn { flex: 1 1 auto; min-height: 44px; }
    .csdv-close { width: 44px; height: 44px; }
}

/* ── Impression : seul le document, jamais l'interface ───────────────── */

@media print {
    body.csdv-printing > *:not(.csdv-root) { display: none !important; }
    /* Les canevas sont le document : ils sortent, le décor de lecture non. */
    .csdv-pdf { overflow: visible !important; background: none !important; height: auto !important; }
    .csdv-pdf__pages { display: block !important; padding: 0 !important; }
    .csdv-pdf__page { box-shadow: none !important; page-break-after: always; max-width: 100% !important; }
    .csdv-pdf__status { display: none !important; }
    .csdv-root { position: static; background: none; padding: 0; }
    .csdv-head, .csdv-foot, .csdv-close { display: none !important; }
    .csdv-shell { box-shadow: none; height: auto; width: 100%; }
    .csdv-body { overflow: visible; background: none; }
}
