/* Iter 43.67 — Visor de Disponibilidad frontend (Fase C).
   Scope: clases .p3dv-av-* dentro del wrapper .p3dv-wrapper en modos
   `data-mode="availability"` o `data-mode="both"`. No tocan el 3D.    */

.p3dv-wrapper[data-mode="availability"] .p3dv-canvas-wrapper,
.p3dv-wrapper[data-mode="availability"] > .p3dv-sidebar,
.p3dv-wrapper[data-mode="availability"] > .p3dv-detail-panel {
    display: none;
}

/* Iter 43.70/71 — En data-mode="both" el wrapper deja de ser grid 260px+1fr.
   Pasa a flex column para apilar mode-tabs + view-3d + view-av. La rejilla
   sidebar/canvas/detail se recrea DENTRO de .p3dv-view-3d para mantener el
   layout original del visor 3D sin ningún cambio en viewer.js. Usamos
   !important porque la regla base `.p3dv-wrapper { display: grid }` vive
   en viewer.css y el caché del navegador puede mantener una versión
   anterior de availability-viewer.css en la que la sobrescritura faltaba:
   sin !important veríamos los selectores apilados como grid items y el
   layout aparece roto (mode-tabs en columna lateral). */
.p3dv-wrapper[data-mode="both"] {
    display: flex !important;
    flex-direction: column !important;
}
.p3dv-wrapper[data-mode="both"] > .p3dv-view-3d {
    display: grid !important;
    grid-template-columns: 260px 1fr !important;
    grid-template-rows: 1fr !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    position: relative !important;
}
.p3dv-wrapper[data-mode="both"] > .p3dv-view-av {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    position: relative !important;
}
.p3dv-wrapper[data-mode="both"] > .p3dv-view-av > .p3dv-av-view {
    /* En both, el alto lo decide el flex del view-av; no var(--p3dv-height). */
    height: 100% !important;
    min-height: 0 !important;
}
/* Mode-tabs siempre en fila (row), nunca herede column del wrapper. */
.p3dv-wrapper[data-mode="both"] > .p3dv-mode-tabs {
    flex-direction: row !important;
    flex: 0 0 auto !important;
}

/* Mode tabs (visibles solo en data-mode="both"). */
.p3dv-mode-tabs {
    display: none;
    gap: 4px;
    padding: 8px 12px;
    background: #f6f7f7;
    border-bottom: 1px solid #dcdcde;
}
.p3dv-wrapper[data-mode="both"] .p3dv-mode-tabs { display: flex; }
.p3dv-mode-tabs button {
    appearance: none;
    background: #fff;
    border: 1px solid #dcdcde;
    border-radius: 4px;
    padding: 6px 14px;
    font-size: 13px;
    color: #2c3338;
    cursor: pointer;
    line-height: 1;
}
.p3dv-mode-tabs button.is-active {
    background: #2271b1;
    color: #fff;
    border-color: #2271b1;
}
.p3dv-mode-tabs button:hover:not(.is-active) {
    background: #f0f0f1;
}

/* Container principal del visor de disponibilidad.
   Iter 43.117 — La sidebar (tabla lateral) se dimensiona según su contenido
   (`fit-content(60%)`): si la tabla cabe holgadamente en 360-400px, ocupa lo
   suyo y el plano usa el resto; si necesita más ancho, empuja al plano
   HACIA LA IZQUIERDA (reduciendo su tamaño) en vez de cortarse por la
   derecha. Antes usábamos `minmax(280px, 360px)`: cualquier columna extra
   provocaba scroll horizontal dentro de la sidebar. */
.p3dv-av-view {
    display: grid;
    grid-template-columns: minmax(0, 1fr) fit-content(60%);
    grid-template-rows: auto 1fr;
    gap: 8px;
    width: 100%;
    height: var(--p3dv-height, 600px);
    min-height: 380px;
    background: #fff;
    overflow: hidden;
}
.p3dv-av-filters {
    grid-column: 1 / -1;
    grid-row: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 10px 14px;
    padding: 10px 14px;
    background: #f6f7f7;
    border-bottom: 1px solid #dcdcde;
}
.p3dv-av-filter {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
}
.p3dv-av-filter-label {
    font-weight: 600;
    color: #2c3338;
}
.p3dv-av-filter select {
    min-width: 90px;
    padding: 4px 8px;
    border: 1px solid #c3c4c7;
    border-radius: 3px;
    background: #fff;
    font-size: 13px;
}
.p3dv-av-filter-empty {
    font-style: italic;
    color: #777;
    font-size: 13px;
}

/* Stage: plano + svg overlay. */
.p3dv-av-stage {
    grid-column: 1;
    grid-row: 2;
    position: relative;
    overflow: auto;
    /* Iter 43.83 — Fondo blanco sólido (antes había un patrón de ajedrez
       que se veía como transparencia en la sección del plano que quedaba
       fuera de la imagen). */
    background: #ffffff;
    border-right: 1px solid #e5e7eb;
    min-height: 0;
    /* Iter 43.78 — Centra el holder inline-block horizontalmente. */
    text-align: center;
}
/* Iter 43.77 — Titular con la combinación activa. */
.p3dv-av-title {
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    margin: 0;
    padding: 8px 14px;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(4px);
    border-bottom: 1px solid #e5e7eb;
    font-size: 15px;
    font-weight: 600;
    color: #1e293b;
    z-index: 5;
    letter-spacing: 0.01em;
}
.p3dv-av-title:empty { display: none; }
.p3dv-av-title .p3dv-av-title-key {
    color: #6b7280;
    font-weight: 500;
    margin-right: 4px;
}
.p3dv-av-title .p3dv-av-title-sep {
    color: #cbd5e1;
    margin: 0 6px;
}
/* Iter 43.78 — Wrapper interno que agrupa img + svg. Es inline-block
   con position:relative, así el SVG con inset:0 cubre exactamente el
   área de la imagen — sin cálculos manuales que puedan desplazarse
   1-2 px por scrollbars, baseline sub-pixel o zoom del navegador. */
.p3dv-av-plan-holder {
    position: relative;
    display: inline-block;
    max-width: 100%;
    line-height: 0; /* elimina el baseline gap del inline */
    margin: 0 auto;
}
.p3dv-av-plan-img {
    display: block;
    max-width: 100%;
    height: auto;
    user-select: none;
    -webkit-user-drag: none;
    vertical-align: top;
}
.p3dv-av-plan-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    overflow: visible;
}
.p3dv-av-plan-svg .p3dv-av-poly-group { pointer-events: auto; cursor: pointer; }
.p3dv-av-plan-svg .p3dv-av-poly {
    /* Iter 43.89 — `fill-opacity` en la transición para que el cambio de
       opacidad del relleno en hover (aplicado inline) sea suave. */
    transition: fill-opacity 0.15s ease, stroke-width 0.15s ease;
}
.p3dv-av-plan-svg .p3dv-av-poly-group:hover .p3dv-av-poly,
.p3dv-av-plan-svg .p3dv-av-poly-group.is-hover .p3dv-av-poly {
    /* La opacidad del relleno en hover se controla desde Ajustes → viewer JS
       inyecta `style="fill-opacity:…"` inline al pasar por encima. El
       stroke-width crece un poco para reforzar el borde. */
    stroke-width: 2.5;
}
.p3dv-av-plan-svg .p3dv-av-poly-group.is-selected .p3dv-av-poly {
    stroke-width: 3;
    stroke: #1d4ed8;
}
.p3dv-av-poly-label {
    font-size: 11px;
    font-weight: 600;
    fill: #0f172a;
    paint-order: stroke;
    stroke: #ffffffcc;
    stroke-width: 3px;
    stroke-linejoin: round;
    pointer-events: none;
    user-select: none;
}
/* Iter 43.79 — Titular a tamaño normal, subtitular más pequeño. */
.p3dv-av-poly-label .p3dv-av-poly-title { font-size: 12px; font-weight: 700; }
.p3dv-av-poly-label .p3dv-av-poly-sub   { font-size: 10px; font-weight: 500; fill: #334155; }

.p3dv-av-empty {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    text-align: center;
    color: #6b7280;
    background: rgba(255, 255, 255, 0.85);
    font-size: 14px;
}
.p3dv-av-stage.is-empty .p3dv-av-empty { display: flex; }

/* Sidebar: leyenda + tabla. */
.p3dv-av-sidebar {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: #fafafa;
}
.p3dv-av-legend {
    padding: 12px 14px;
    border-bottom: 1px solid #e5e7eb;
}
.p3dv-av-legend-title {
    margin: 0 0 8px;
    font-size: 13px;
    font-weight: 600;
    color: #2c3338;
}
.p3dv-av-legend-list {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.p3dv-av-legend-list li {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: #374151;
}
.p3dv-av-legend-empty {
    font-style: italic;
    color: #888;
}
.p3dv-av-swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    flex-shrink: 0;
}

.p3dv-av-table-wrap {
    flex: 1 1 auto;
    overflow: auto;
    padding: 0;
}
.p3dv-av-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.p3dv-av-table thead th {
    position: sticky;
    top: 0;
    background: #f6f7f7;
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid #dcdcde;
    font-weight: 600;
    color: #2c3338;
}
.p3dv-av-table tbody td {
    padding: 7px 10px;
    border-bottom: 1px solid #f0f0f1;
    color: #374151;
}
.p3dv-av-table tbody tr {
    cursor: pointer;
}
.p3dv-av-table tbody tr:hover,
.p3dv-av-table tbody tr.is-hover {
    background: #eef6ff;
}
.p3dv-av-table tbody tr.is-selected {
    background: #dbeafe;
}
.p3dv-av-table tbody tr.is-selected td:first-child {
    font-weight: 600;
}
.p3dv-av-table-empty {
    text-align: center;
    font-style: italic;
    color: #888;
    padding: 16px !important;
}
.p3dv-av-row-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    margin-right: 8px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    vertical-align: middle;
}

/* Responsive: sidebar debajo del stage en pantallas estrechas.
   Iter 43.90 — En móvil el plano ocupa TODO el ancho y coge una altura
   generosa (~65vh) para que se lea con comodidad; la leyenda + tabla
   fluyen debajo. Retiramos la altura FIJA del container y desactivamos
   el `overflow:hidden` para que la página pueda hacer scroll natural
   cuando la tabla es larga (el <table> mantiene su propio scroll para
   listas muy grandes gracias a max-height + overflow del wrap). */
@media (max-width: 768px) {
    .p3dv-av-view {
        /* Grid single-column: filtros arriba, stage a media pantalla,
           sidebar (leyenda + tabla) debajo. */
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto;
        height: auto;
        min-height: 0;
        overflow: visible;
    }
    .p3dv-av-stage {
        grid-column: 1;
        grid-row: 2;
        border-right: none;
        border-bottom: 1px solid #e5e7eb;
        /* Reservamos alto real para el plano: 65% del viewport es lo que
           mejor equilibra "se ve completo" y "hay espacio para el resto".
           Usamos min-height para permitir que el img crezca si aporta
           más aspect-ratio que este mínimo. */
        min-height: 65vh;
    }
    .p3dv-av-sidebar {
        grid-column: 1;
        grid-row: 3;
        /* Sin tope de altura: la tabla fluye debajo del plano y la
           página scrollea. Si el listado es muy largo, `.p3dv-av-table-wrap`
           limita internamente. */
        max-height: none;
        border-top: 1px solid #e5e7eb;
    }
    .p3dv-av-table-wrap {
        /* Scroll interno cuando la lista es muy larga. Balance entre
           ver bastantes filas y no obligar a scrollear página entera
           sólo para volver al plano. */
        max-height: 55vh;
    }
    /* Iter 43.90 — En modo `both` el padre fijaba `height:100% !important`
       al `.p3dv-av-view` y lo aplastaba en móvil. Anulamos SÓLO la altura
       (respetando el `display` que el JS de tabs alterna, y el flex que
       usa el 3D). Necesitamos !important porque la regla base también
       lo lleva. */
    .p3dv-wrapper[data-mode="both"] > .p3dv-view-av {
        height: auto !important;
        flex: 0 0 auto !important;
    }
    .p3dv-wrapper[data-mode="both"] > .p3dv-view-av > .p3dv-av-view {
        height: auto !important;
    }
}
