/* ================================
   GRAPH COMPONENT · Cytoscape
   Scope: .hi-graph-*
   ================================
   Reutiliza del design system: --hi-paper, --hi-ink, --hi-ink-mute, --hi-rule,
   --hi-mono, --hi-serif, --hi-sans, --hi-accent, --hi-paper-warm.
   Variables nuevas (este archivo): --hi-graph-bg, --hi-graph-toolbar-bg,
   --hi-graph-toolbar-fg, --hi-graph-sidebar-w, --hi-graph-accent,
   --hi-graph-shadow, --hi-graph-shadow-lg, --hi-graph-radius.

   Decisiones:
   - Toolbar navy (#1a202c) en vez de --hi-ink (marrón): el grafo es una
     "zona foco" distinta, y el navy contrasta mejor con el acento naranja.
   - --hi-graph-accent (#ed8936) es la versión saturada de la familia gold
     (#d9bd86). --hi-gold no leía sobre el toolbar oscuro.
   - Sombras con tinte navy para casar con la temperatura del toolbar.
   - El body usa flex:1 en vez de height:calc(100% - 44px) para evitar el
     número mágico y adaptarse si el padding del toolbar cambia. */

/* Variables nuevas del scope */
:root {
    /* Fondo del canvas: levemente más claro y neutro que --hi-paper. */
    --hi-graph-bg: #F6F2EC;
    /* Toolbar: navy frío, no marrón — funciona como marco "modo foco". */
    --hi-graph-toolbar-bg: #1a202c;
    /* Alias local de --hi-paper para que la intención sea legible acá. */
    --hi-graph-toolbar-fg: var(--hi-paper);
    --hi-graph-sidebar-w: 280px;
    /* Acento del grafo: naranja saturado, familia del --hi-gold. */
    --hi-graph-accent: #D4A017;
    /* Sombras navy para combinar con el toolbar frío. */
    --hi-graph-shadow: 0 2px 8px rgba(44, 33, 24, 0.08);
    --hi-graph-shadow-lg: 0 8px 24px rgba(44, 33, 24, 0.16);
    --hi-graph-radius: 4px;
}

/* Container principal. Hereda su alto del inline style (70vh) que emite el JS.
   Column flex para que toolbar y body se repartan el alto. */
.hi-graph {
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--hi-rule);
    border-radius: var(--hi-graph-radius);
    overflow: hidden;
    background: var(--hi-graph-bg);
    box-shadow: var(--hi-graph-shadow);
}

/* Root: flex column para que el body llene el espacio restante
   después del toolbar (o todo el espacio si toolbar está oculto). */
.hi-graph-root {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* Toolbar superior: banda oscura densa. flex-wrap para que en pantallas
   medianas los botones bajen sin desbordar. */
.hi-graph-toolbar {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--hi-graph-toolbar-bg);
    color: var(--hi-graph-toolbar-fg);
    flex-wrap: wrap;
    flex-shrink: 0;
}

/* Botones del toolbar: ghost sobre el navy. Mono como el resto de controles. */
.hi-graph-layout-btn,
.hi-graph-action-btn {
    background: transparent;
    color: inherit;
    border: 1px solid transparent;
    padding: 0.25rem 0.75rem;
    border-radius: 3px;
    font-family: var(--hi-mono);
    font-size: 0.75rem;
    letter-spacing: 0.04em;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.hi-graph-layout-btn:hover,
.hi-graph-action-btn:hover {
    background: rgba(255, 255, 255, 0.08);
}

/* Estado activo del layout (lo togglea el JS al cambiar el algoritmo). */
.hi-graph-layout-btn.is-active {
    background: var(--hi-graph-accent);
    color: #fff;
    border-color: var(--hi-graph-accent);
}

/* Toggle de sidebar: margin-left auto lo empuja a la derecha del toolbar. */
.hi-graph-toggle-sidebar {
    margin-left: auto;
    background: transparent;
    color: var(--hi-graph-toolbar-fg);
    border: 1px solid transparent;
    padding: 0.25rem 0.6rem;
    border-radius: 3px;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    transition: background 0.15s, border-color 0.15s;
}

.hi-graph-toggle-sidebar:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.2);
}

/* Focus visible global para todos los controles interactivos del componente. */
.hi-graph-layout-btn:focus-visible,
.hi-graph-action-btn:focus-visible,
.hi-graph-toggle-sidebar:focus-visible,
.hi-graph-input:focus-visible,
.hi-graph-select:focus-visible,
.hi-graph-btn-clear:focus-visible,
.hi-graph-popup-close:focus-visible,
.hi-graph-popup-link:focus-visible {
    outline: 2px solid var(--hi-graph-accent);
    outline-offset: 2px;
}

/* Body: sidebar + canvas. flex:1 llena el resto del container. */
.hi-graph-body {
    display: flex;
    flex-direction: row;
    flex: 1;
    min-height: 0;
    position: relative;
}

/* Sidebar de filtros. Columna fija a la izquierda; en mobile pasa a overlay
   (ver @media al final). */
.hi-graph-sidebar {
    width: var(--hi-graph-sidebar-w);
    flex-shrink: 0;
    padding: 1rem;
    background: var(--hi-paper);
    border-right: 1px solid var(--hi-rule);
    overflow-y: auto;
    transition: transform 0.2s, opacity 0.2s;
}

.hi-graph-side-section {
    margin-bottom: 1.5rem;
}

.hi-graph-side-section[hidden],
.hi-graph-type[hidden] {
    display: none;
    margin-bottom: 0;
}

.hi-graph-side-label {
    display: block;
    font-family: var(--hi-sans);
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--hi-ink);
    margin-bottom: 0.5rem;
}

.hi-graph-input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--hi-rule);
    border-radius: 3px;
    font-family: var(--hi-sans);
    font-size: 0.875rem;
    color: var(--hi-ink);
    background: var(--hi-surface);
    box-sizing: border-box;
    transition: border-color 0.15s;
}

.hi-graph-input:focus {
    outline: none;
    border-color: var(--hi-graph-accent);
}

.hi-graph-type {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
    cursor: pointer;
    font-family: var(--hi-sans);
    font-size: 0.875rem;
    color: var(--hi-ink);
}

.hi-graph-type input[type="checkbox"] {
    margin: 0;
    accent-color: var(--hi-graph-accent);
    cursor: pointer;
}

.hi-graph-type-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.hi-graph-select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--hi-rule);
    border-radius: 3px;
    font-family: var(--hi-sans);
    font-size: 0.875rem;
    color: var(--hi-ink);
    background: var(--hi-surface);
    box-sizing: border-box;
    transition: border-color 0.15s;
}

.hi-graph-select:focus {
    outline: none;
    border-color: var(--hi-graph-accent);
}

.hi-graph-btn-clear {
    width: 100%;
    padding: 0.5rem 1rem;
    background: transparent;
    border: 1px solid var(--hi-ink-mute);
    color: var(--hi-ink);
    cursor: pointer;
    font-family: var(--hi-sans);
    font-size: 0.75rem;
    letter-spacing: 0.04em;
    border-radius: 3px;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.hi-graph-btn-clear:hover {
    background: var(--hi-ink);
    color: var(--hi-paper);
    border-color: var(--hi-ink);
}

/* Canvas (Cytoscape monta acá). min-width:0 evita que Cytoscape haga
   crecer el flex. */
.hi-graph-canvas {
    flex: 1;
    position: relative;
    overflow: hidden;
    min-width: 0;
}

.hi-graph-cy {
    width: 100%;
    height: 100%;
}

/* Popup de info de nodo: posicionado por JS cerca del nodo cliqueado. */
.hi-graph-popup {
    position: absolute;
    width: 220px;
    max-width: calc(100% - 2rem);
    max-height: calc(100% - 2rem);
    overflow-y: auto;
    background: var(--hi-surface);
    border: 1px solid var(--hi-rule);
    border-radius: var(--hi-graph-radius);
    box-shadow: var(--hi-graph-shadow-lg);
    padding: 0.75rem;
    z-index: 5;
    font-family: var(--hi-sans);
    font-size: 0.875rem;
    color: var(--hi-ink);
    transition: opacity 0.15s, transform 0.15s;
}

.hi-graph-popup[hidden] {
    display: none;
}

.hi-graph-popup-close {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    color: var(--hi-ink-mute);
    padding: 0.15rem 0.35rem;
    border-radius: 3px;
    transition: color 0.15s, background 0.15s;
}

.hi-graph-popup-close:hover {
    color: var(--hi-ink);
    background: var(--hi-paper-warm);
}

.hi-graph-popup-img {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    margin: 0 auto 0.5rem;
    display: block;
    border: 2px solid var(--hi-rule);
    background: var(--hi-paper-warm);
}

.hi-graph-popup-name {
    font-family: var(--hi-serif);
    font-size: 1rem;
    font-weight: 600;
    text-align: center;
    color: var(--hi-ink);
    margin: 0 0 0.15rem;
    line-height: 1.2;
}

.hi-graph-popup-type {
    font-family: var(--hi-mono);
    font-size: 0.65rem;
    text-transform: uppercase;
    color: var(--hi-ink-mute);
    text-align: center;
    letter-spacing: 0.1em;
    margin: 0 0 0.35rem;
}

.hi-graph-popup-meta {
    font-size: 0.75rem;
    color: var(--hi-ink-mute);
    text-align: center;
    margin: 0 0 0.5rem;
    line-height: 1.4;
}

.hi-graph-popup-link {
    display: block;
    text-align: center;
    padding: 0.4rem 0.75rem;
    background: var(--hi-ink);
    color: var(--hi-paper);
    text-decoration: none;
    font-family: var(--hi-mono);
    font-size: 0.65rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    border-radius: 3px;
    transition: background 0.15s, color 0.15s;
}

.hi-graph-popup-link:hover {
    background: var(--hi-graph-accent);
    color: #fff;
}

/* Estado de error: cubre el canvas con un velo crema y mensaje centrado. */
.hi-graph-error {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(250, 248, 243, 0.95);
    color: var(--hi-ink-mute);
    font-family: var(--hi-sans);
    font-size: 0.9rem;
    padding: 2rem;
    text-align: center;
    z-index: 4;
}

.hi-graph-error[hidden] {
    display: none;
}

/* Loading: el JS agrega .hi-graph-loading durante el fetch + init. El spinner
   es CSS puro (no imagen). */
.hi-graph-loading {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    background: var(--hi-graph-bg);
    z-index: 3;
}

.hi-graph-loading[hidden] {
    display: none;
}

.hi-graph-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--hi-rule);
    border-top-color: var(--hi-graph-accent);
    border-radius: 50%;
    animation: hi-graph-spin 0.8s linear infinite;
}

.hi-graph-loading-label {
    font-family: var(--hi-mono);
    font-size: 0.7rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--hi-ink-mute);
}

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

/* Backdrop móvil: lo emite el JS cuando el sidebar se abre. Tap cierra. */
.hi-graph-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(44, 33, 24, 0.4);
    z-index: 9;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.hi-graph-backdrop.is-open {
    opacity: 1;
    pointer-events: auto;
}

/* ================================
   Responsive · mobile (≤ 768px)
   ================================ */
@media (max-width: 768px) {
    .hi-graph-body {
        flex-direction: column;
    }

    /* Sidebar pasa a overlay deslizante. */
    .hi-graph-sidebar {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        width: var(--hi-graph-sidebar-w);
        max-width: 85vw;
        z-index: 10;
        transform: translateX(-100%);
        box-shadow: var(--hi-graph-shadow-lg);
        border-right: 1px solid var(--hi-rule);
    }

    .hi-graph-sidebar.is-open {
        transform: translateX(0);
    }

    /* Iconos en mobile: cada botón lleva data-icon="◉" / "⤢" / etc. Acá
       ocultamos el textContent (font-size: 0) y mostramos el glyph vía
       ::before. En desktop el data-icon se ignora (regla sólo en media). */
    .hi-graph-layout-btn,
    .hi-graph-action-btn {
        font-size: 0;
        padding: 0.4rem 0.6rem;
    }

    .hi-graph-layout-btn::before,
    .hi-graph-action-btn::before {
        content: attr(data-icon);
        font-size: 1rem;
        line-height: 1;
    }

    .hi-graph-toggle-sidebar {
        font-size: 1.1rem;
    }
}

/* Pantallas muy chicas: popup full-width y toolbar más compacto. */
@media (max-width: 480px) {
    .hi-graph-popup {
        top: 0.5rem;
        right: 0.5rem;
        left: 0.5rem;
        width: auto;
        max-width: none;
    }

    .hi-graph-toolbar {
        padding: 0.4rem 0.6rem;
    }
}

/* Accesibilidad · reduced motion: deshabilita transiciones y spinner. */
@media (prefers-reduced-motion: reduce) {
    .hi-graph-layout-btn,
    .hi-graph-action-btn,
    .hi-graph-toggle-sidebar,
    .hi-graph-input,
    .hi-graph-select,
    .hi-graph-btn-clear,
    .hi-graph-popup,
    .hi-graph-popup-close,
    .hi-graph-popup-link,
    .hi-graph-sidebar,
    .hi-graph-backdrop {
        transition: none;
    }

    .hi-graph-spinner {
        animation: none;
    }
}
