/* Estilos del dashboard Malinalco. Heredado de tenebrios-node con cambios:
   - 6 zone-cards en lugar de termometros / hbars / devices
   - sin history-panel ni time-slider (no hay historico en MVP)
   - tabs Temp / Hum
   - logo CONDOR y paleta dorada preservados */

:root {
    --c-bg:         #1a2630;
    --c-panel:      #243B4A;
    --c-panel-lt:   #344955;
    --c-border:     #3a5a6a;
    --c-gold:       #E8B830;
    --c-gold-dim:   rgba(232,184,48,0.25);
    --c-text:       #e8e0d8;
    --c-text-dim:   rgba(255,255,255,0.68);
    --c-cyan:       #6bdcff;
    --c-red:        #ff5b4f;
    --c-blue:       #4a9fd4;
    --font-ui:      'Barlow Condensed', 'Segoe UI', sans-serif;
    --font-data:    'Orbitron', sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { height: 100%; font-size: 18px; }
body {
    font-family: var(--font-ui);
    background-color: var(--c-bg);
    background-image: radial-gradient(rgba(58,90,106,0.38) 1px, transparent 1px);
    background-size: 28px 28px;
    color: var(--c-text);
    display: flex; flex-direction: column;
    height: 100vh; overflow: hidden;
}

/* Header — bandeja superior con jerarquia clara y mejor profundidad */
.header {
    background: linear-gradient(180deg, #3a5161 0%, #344955 100%);
    padding: 5px 12px; text-align: center;
    border-bottom: 2px solid #E8B830;
    box-shadow: 0 2px 6px rgba(0,0,0,0.35);
    display: flex; align-items: center; justify-content: space-between;
    flex-shrink: 0;
    gap: 14px;
}
.header-left { display: flex; align-items: center; gap: 0; }
.brand-logo {
    display: flex; flex-direction: column;
    gap: 2px;
    line-height: 1;
}
.brand-main {
    display: flex; align-items: center; gap: 0;
}
.brand-icon {
    width: 30px; height: 30px;
    color: var(--c-gold);
    filter: drop-shadow(0 0 7px rgba(232,184,48,0.38));
    flex-shrink: 0;
    margin-right: -4px;
}
.brand-name {
    font-family: var(--font-ui);
    font-size: 1rem;
    color: var(--c-gold);
    font-weight: 700;
    letter-spacing: 1.8px;
    text-transform: lowercase;
    text-shadow: 0 0 8px rgba(232,184,48,0.4);
    line-height: 1;
}
.brand-tag {
    font-family: var(--font-ui);
    font-size: 0.36rem;
    color: rgba(255,255,255,0.7);
    letter-spacing: 0.8px;
    text-transform: uppercase;
    line-height: 1.1;
    font-weight: 600;
}
.header h1 {
    font-family: var(--font-ui);
    font-size: 1.4rem; color: var(--c-gold); font-weight: 700;
    letter-spacing: 6px; text-transform: uppercase; margin: 0;
    line-height: 1.05;
    text-shadow: 0 0 12px rgba(232,184,48,0.4);
}
.header .subtitle {
    font-family: var(--font-ui);
    font-size: 0.6rem; color: rgba(255,255,255,0.75);
    letter-spacing: 4px; text-transform: uppercase;
    margin-top: 2px; font-weight: 400;
}
.header .conn-line { font-family: var(--font-ui); font-size: 0.75rem; color: rgba(255,255,255,0.7); }
.header-center {
    display: flex; flex-direction: column; align-items: center;
    gap: 3px;
    font-size: 0.78rem; color: #E8B830;
    font-family: 'Orbitron', sans-serif;
    letter-spacing: 1.6px;
    font-weight: 500;
    text-shadow: 0 0 9px rgba(232,184,48,0.45);
    padding: 3px 10px 4px;
    border: 1px solid rgba(232,184,48,0.25);
    border-radius: 5px;
    background: rgba(0,0,0,0.18);
}
.header-center .conn-line {
    display: inline-flex; align-items: center; gap: 6px;
    font-family: var(--font-ui);
    font-size: 0.46rem;
    letter-spacing: 1px;
    text-transform: uppercase;
    color: rgba(255,255,255,0.72);
    font-weight: 600;
    padding: 1px 8px;
    border-radius: 999px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    text-shadow: none;
}
.header-right { display: flex; align-items: center; gap: 5px; }
.condor-logo {
    display: flex; flex-direction: column; align-items: flex-end;
    line-height: 1;
}
.condor-logo .condor-ing {
    font-family: var(--font-ui);
    font-size: 0.5rem; color: #aabac8; font-weight: 600;
    letter-spacing: 1.8px; text-transform: uppercase;
}
.condor-logo .condor-name {
    font-family: var(--font-ui);
    font-size: 1rem; color: #fff; font-weight: 700;
    letter-spacing: 2.5px; text-transform: uppercase;
    margin-top: 2px;
}
.condor-swoosh {
    display: flex; flex-direction: column; align-items: flex-end; gap: 2px; margin-left: 3px;
}
.condor-swoosh span { display: block; height: 2.5px; border-radius: 2px; }
.condor-swoosh .sw1 { width: 22px; background: #E8B830; position: relative; }
.condor-swoosh .sw1::before {
    content: ''; position: absolute; left: -5px; top: -2px;
    width: 7px; height: 7px; background: #E8B830; border-radius: 50%;
    box-shadow: 0 0 6px rgba(232,184,48,0.6);
}
.condor-swoosh .sw2 { width: 18px; background: #8aa0b0; }
.condor-swoosh .sw3 { width: 18px; background: #8aa0b0; }

/* Dashboard layout */
.dashboard {
    display: flex; flex-direction: column;
    flex: 1; min-height: 0; padding: 4px 8px;
    overflow: hidden;
}
.main-row { display: flex; gap: 3px; flex: 1; min-height: 0; }
.render-column { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#scene-container {
    flex: 1; background: #243B4A; border-radius: 8px; padding: 4px;
    border: 1px solid #3a5a6a; min-width: 0; min-height: 0;
    touch-action: none; position: relative;
    box-shadow: 0 0 0 1px var(--c-border), inset 0 0 50px rgba(0,0,0,0.45);
}
#scene3d { width: 100%; height: 100%; display: block; }
#scene3d-canvas { width: 100%; height: 100%; display: block; outline: none; touch-action: none; }

/* Side panel — cards con profundidad, compacto para fit-to-screen */
.side-panel {
    width: 400px; flex-shrink: 0; display: flex; flex-direction: column;
    gap: 6px; overflow-y: auto; min-height: 0; padding-right: 2px;
    border-left: 1px solid rgba(232,184,48,0.22);
    padding-left: 6px;
}
.side-panel::-webkit-scrollbar { width: 6px; }
.side-panel::-webkit-scrollbar-track { background: transparent; }
.side-panel::-webkit-scrollbar-thumb {
    background: #3a5a6a; border-radius: 3px;
}
.side-panel::-webkit-scrollbar-thumb:hover { background: #4a6a7a; }

.indicators-section {
    padding: 7px 10px 8px;
    background: linear-gradient(180deg, #2a4456 0%, #243B4A 100%);
    border-radius: 10px;
    border: 1px solid #3a5a6a;
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    flex: 1 1 0; min-height: 0;
    display: flex; flex-direction: column;
    overflow: hidden;
}
.indicators-section h2 {
    font-family: var(--font-ui);
    font-size: 0.82rem; color: var(--c-gold);
    margin: 0 0 5px 0;
    padding-bottom: 4px;
    border-bottom: 1px solid rgba(232,184,48,0.28);
    letter-spacing: 3px; text-transform: uppercase;
    font-weight: 700;
    display: flex; align-items: center; gap: 8px;
    flex-shrink: 0;
}
.indicators-section h2::before {
    content: ''; display: inline-block;
    width: 4px; height: 14px;
    background: #E8B830; border-radius: 2px;
    box-shadow: 0 0 6px rgba(232,184,48,0.5);
}

/* Barras horizontales por sensor, agrupadas por zona (estilo tenebrios) */
.hbars-row {
    display: flex; flex-direction: column; gap: 3px;
    flex: 1; min-height: 0; overflow-y: auto;
}
.hbars-row::-webkit-scrollbar { width: 4px; }
.hbars-row::-webkit-scrollbar-thumb { background: #3a5a6a; border-radius: 2px; }

/* Cabecera de cada grupo de sensores dentro de la seccion — chip lateral */
.zone-group-header {
    font-family: var(--font-ui);
    font-size: 0.65rem; color: #aabac8;
    text-transform: uppercase; letter-spacing: 2px;
    font-weight: 700;
    padding: 3px 4px 3px 8px; margin-top: 4px;
    border-left: 2px solid var(--c-blue);
    background: rgba(74,159,212,0.08);
    border-radius: 0 4px 4px 0;
    box-shadow: -2px 0 8px rgba(74,159,212,0.22);
}
.zone-group-header:first-child { margin-top: 0; }
.zone-empty {
    font-size: 0.55rem; color: #607888; font-style: italic;
    padding: 2px 8px 3px; opacity: 0.75;
}
.hbar-item {
    display: flex; align-items: center; gap: 6px;
    padding: 1px 3px; border-radius: 4px;
    transition: background 0.15s;
    animation: slide-in-bar 0.32s ease both;
    animation-delay: calc(var(--i, 0) * 42ms);
}
.hbar-item:hover { background: rgba(255,255,255,0.04); }
.hbar-label {
    font-family: var(--font-ui);
    font-size: 0.68rem; color: #d4dde2; text-transform: uppercase;
    letter-spacing: 1px; min-width: 86px; text-align: right; font-weight: 600;
}
.hbar-badge {
    font-size: 0.5rem; color: #8aa0b0; font-weight: 400;
    letter-spacing: 0; text-transform: none;
}
.hbar-track-wrap { flex: 1; display: flex; flex-direction: column; gap: 1px; }
.hbar-track {
    width: 100%; height: 18px; background: #141d26;
    border-radius: 9px; position: relative;
    border: 1px solid #3a5060;
    box-shadow: inset 0 1px 2px rgba(0,0,0,0.5);
}
.hbar-fill {
    position: absolute; top: 0; left: 0; bottom: 0;
    border-radius: 9px 0 0 9px; transition: width 0.5s, background 0.5s;
    z-index: 1; overflow: hidden;
}
.hbar-fill::after {
    content: ''; position: absolute;
    top: 0; bottom: 0; left: -50%; width: 40%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.22), transparent);
    animation: shimmer-bar 2.8s ease-in-out infinite;
    pointer-events: none;
}
/* Marcas verticales: limites del rango ideal (low/high) */
.hbar-mark {
    position: absolute; top: -3px; bottom: -3px; width: 0;
    border-left: 2px solid rgba(255,255,255,0.8);
    z-index: 2; pointer-events: none;
}
/* Marca central: punto ideal a mantener */
.hbar-mark-mid {
    position: absolute; top: -4px; bottom: -4px; width: 0;
    border-left: 2px dashed #E8B830;
    z-index: 2; pointer-events: none;
}
.hbar-scale {
    display: flex; justify-content: space-between;
    font-family: var(--font-ui);
    font-size: 0.55rem; color: #7088a0; padding: 0 2px;
}
.hbar-value-wrap {
    display: flex; flex-direction: column; align-items: flex-end;
    min-width: 80px; gap: 2px;
}
.hbar-value {
    font-size: 0.9rem; font-weight: bold; color: var(--c-gold);
    text-align: right;
    font-family: var(--font-data);
    transition: color 0.3s;
}
.hbar-value small { font-size: 0.65em; opacity: 0.75; margin-left: 2px; font-weight: 500; }
.hbar-age {
    font-family: var(--font-ui);
    font-size: 0.5rem; font-weight: 600;
    color: #4a6070; letter-spacing: 0.3px;
    text-align: right; line-height: 1;
    transition: color 0.4s;
}
.hbar-age.age-warn  { color: var(--c-gold); }
.hbar-age.age-stale { color: var(--c-red); animation: pulse-age 1.4s ease-in-out infinite; }

@keyframes pulse-age {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.35; }
}

/* Alertas por valor fuera de rango ideal:
   - alert-low: temperatura baja / humedad baja → cyan
   - alert-high: temperatura alta / humedad alta → rojo */
.hbar-item.alert-low .hbar-value {
    color: #6bdcff !important;
    border-left: 4px solid #6bdcff;
    padding-left: 6px;
}
.hbar-item.alert-high .hbar-value {
    color: #ff5b4f !important;
    border-left: 4px solid #ff5b4f;
    padding-left: 6px;
}
/* Alerta por antiguedad de datos sobrescribe a las de rango */
.hbar-item.alert-stale .hbar-value {
    border-left: 4px solid #d44 !important;
}

.hbar-item.muted .hbar-value { color: #607888; opacity: 0.6; }
.hbar-item.muted .hbar-fill { display: none; }

/* Etiqueta textual de alerta junto al label (Frío / Caliente / Bajo / Alto) */
.hbar-alert-tag {
    display: inline-block;
    font-size: 0.55rem; font-weight: 700; letter-spacing: 1px;
    padding: 2px 6px; margin-left: 5px; border-radius: 3px;
    vertical-align: middle;
}
.hbar-alert-tag.low  { background: rgba(107,220,255,0.18); color: #6bdcff; border: 1px solid rgba(107,220,255,0.35); }
.hbar-alert-tag.high { background: rgba(255,91,79,0.18);   color: #ff5b4f; border: 1px solid rgba(255,91,79,0.35); }

/* Colorbar horizontal — paleta del modo activo con sus margenes (min..max) */
.colorbar-h {
    position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
    z-index: 9; display: flex; flex-direction: column; align-items: stretch;
    background: rgba(26,38,48,0.80); padding: 8px 14px; gap: 5px;
    border-radius: 6px; border: 1px solid #3a5a6a; pointer-events: none;
    width: min(540px, 60%);
}
.colorbar-h-title {
    font-family: var(--font-data); font-size: 0.72rem;
    color: var(--c-gold); letter-spacing: 2px; text-align: center;
    font-weight: 500; text-transform: uppercase;
    transition: opacity 0.22s ease;
}
.colorbar-h-bar {
    height: 22px; border-radius: 4px;
    border: 1px solid #3a5a6a;
    transition: opacity 0.22s ease;
}
.colorbar-h-scale {
    display: flex; justify-content: space-between;
    font-size: 0.78rem; color: #ffffff;
    font-family: var(--font-data); font-weight: 500;
    transition: opacity 0.22s ease;
}
.colorbar-h-scale span {
    padding-left: 4px; border-left: 1px solid rgba(255,255,255,0.35);
}
.colorbar-h-scale span:last-child {
    border-left: none; padding-left: 0;
    padding-right: 4px; border-right: 1px solid rgba(255,255,255,0.35);
}

/* Mode toggle (Temp / Hum) — overlay sobre el render */
.mode-toggle {
    position: absolute; top: 8px; left: 8px; z-index: 100;
    display: flex; flex-wrap: wrap; max-width: calc(100% - 16px);
    border-radius: 6px; overflow: hidden;
    border: 1px solid #3a5a6a;
}
.mode-toggle button {
    background: #243B4A; color: #8aa0b0; border: none;
    padding: 8px 18px; font-size: 0.85rem; cursor: pointer;
    font-weight: 600; letter-spacing: 1.5px; text-transform: uppercase;
    transition: background 0.15s, color 0.15s;
}
.mode-toggle button.active { background: #E8B830; color: #1a2630; }

/* ---- Animaciones globales ---- */
@keyframes shimmer-bar {
    0%   { left: -50%; }
    100% { left: 110%; }
}
@keyframes slide-in-bar {
    from { opacity: 0; transform: translateX(-10px); }
    to   { opacity: 1; transform: translateX(0); }
}
@keyframes pulse-dot {
    0%, 100% { box-shadow: 0 0 0 0 rgba(109,191,90,0.55); }
    50%       { box-shadow: 0 0 0 5px rgba(109,191,90,0); }
}
@keyframes pulse-dot-err {
    0%, 100% { box-shadow: 0 0 0 0 rgba(221,68,68,0.55); }
    50%       { box-shadow: 0 0 0 5px rgba(221,68,68,0); }
}

/* Connection dot */
.connection-dot {
    display: inline-block; width: 10px; height: 10px;
    border-radius: 50%; margin-right: 6px; vertical-align: middle;
}
.connection-dot.ok  {
    background: #6dbf5a;
    animation: pulse-dot 2.2s ease-in-out infinite;
}
.connection-dot.err {
    background: #d44;
    animation: pulse-dot-err 1.6s ease-in-out infinite;
}
.connection-dot.warn { background: var(--c-gold); }

/* Boton inmersion */
.btn-expand-render {
    display: block;
    position: absolute; bottom: 12px; right: 12px; z-index: 20;
    background: rgba(36,59,74,0.92); color: #E8B830; border: 1px solid #3a5a6a;
    border-radius: 4px; padding: 7px 14px; font-size: 0.8rem; cursor: pointer;
    font-weight: 700; text-transform: uppercase; letter-spacing: 1.2px;
}
.btn-expand-render:hover { background: rgba(232,184,48,0.2); }

/* Modo INMERSION */
@keyframes pulse-hint {
    0%, 100% { opacity: 0.55; }
    50% { opacity: 0.85; }
}
body.immersive { height: 100vh !important; overflow: hidden !important; }
body.immersive .header,
body.immersive .footer { display: none !important; }
body.immersive .side-panel {
    position: fixed !important;
    top: 0 !important; right: 0 !important;
    width: 400px !important; height: 100vh !important;
    z-index: 210 !important;
    background: rgba(18, 28, 38, 0.97) !important;
    border-left: 1px solid #3a5a6a !important;
    overflow-y: auto !important; overflow-x: hidden !important;
    padding: 8px 4px !important;
    gap: 6px !important;
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
    flex-direction: column !important;
    display: flex !important;
}
body.immersive .side-panel.panel-visible {
    transform: translateX(0) !important;
    pointer-events: auto !important;
}
.panel-edge-hint { display: none; }
body.immersive .panel-edge-hint {
    display: block;
    position: fixed; top: 0; right: 0; width: 8px; height: 100vh;
    background: linear-gradient(to left, rgba(232,184,48,0.55), transparent);
    z-index: 209; pointer-events: none;
    transition: opacity 0.25s;
    animation: pulse-hint 2s ease-in-out 5;
}
body.immersive.panel-open .panel-edge-hint { opacity: 0; }
body.immersive .dashboard { padding: 0 !important; height: 100vh !important; flex: 1 1 auto !important; }
body.immersive .main-row { gap: 0 !important; }
body.immersive .render-column { width: 100% !important; height: 100vh !important; flex: 1 1 auto !important; }
body.immersive #scene-container {
    border-radius: 0 !important; border: none !important; padding: 0 !important;
    height: 100vh !important; width: 100vw !important;
}

.footer { display: none; }

/* ---- Responsive: movil vertical (<768px) ---- */
@media (max-width: 768px) {
    /* Header: compacto — ocultar timestamp y logo para liberar espacio */
    .header {
        justify-content: space-between; gap: 4px; padding: 6px 10px;
        flex-wrap: nowrap;
    }
    .header-left { gap: 8px; flex: 1; min-width: 0; }
    .brand-icon { width: 38px; height: 38px; }
    .brand-name { font-size: 1rem; letter-spacing: 2px; }
    .brand-tag { display: none; }
    .header h1 { font-size: 0.95rem; letter-spacing: 2px; }
    .header .subtitle { font-size: 0.44rem; letter-spacing: 2px; }
    /* En movil mostramos solo el badge "Conectado" (sin timestamp) para no robar
       espacio del logo, manteniendo visible el estado de conexion. */
    .header-center {
        display: flex; flex-direction: row;
        padding: 0; border: none; background: transparent;
        text-shadow: none; gap: 0;
    }
    #mqtt-timestamp { display: none; }
    .header-center .conn-line {
        font-size: 0.42rem; letter-spacing: 1.1px;
        padding: 2px 8px; gap: 5px;
    }
    .header-right { display: none; }

    /* Layout: render arriba, panel lateral debajo — todo scrollable */
    body { overflow-y: auto !important; height: auto !important; min-height: 100dvh; }
    .dashboard { flex: none !important; height: auto !important; overflow: visible !important; padding: 4px; }
    .main-row { flex-direction: column !important; flex: none !important; gap: 6px; }
    .render-column {
        height: 50vh !important; min-height: 300px !important; max-height: 520px !important;
        flex: none !important; overflow: hidden !important;
    }
    #scene-container {
        border-radius: 8px; position: relative !important;
        flex: 1; overflow: hidden !important;
    }

    /* Botones modo: touch-friendly (44px min iOS HIG) */
    .mode-toggle button {
        padding: 10px 18px; font-size: 0.78rem;
        min-height: 44px; letter-spacing: 1px;
    }

    /* Colorbar: ancho completo, fija al fondo del render */
    .colorbar-h {
        left: 8px !important; right: 8px !important;
        width: auto !important; bottom: 10px !important;
        transform: none !important;
    }
    .colorbar-h-title { font-size: 0.62rem; }
    .colorbar-h-scale { font-size: 0.62rem; }
    .colorbar-h-bar { height: 18px; }

    /* Boton inmersion oculto en movil */
    .btn-expand-render { display: none !important; }

    /* Panel lateral: ancho completo, sin divisor dorado en movil */
    .side-panel {
        width: 100% !important; flex-shrink: 0;
        overflow-y: visible !important; gap: 8px;
        border-left: none !important; padding-left: 0 !important;
    }
    /* Sections: no flex-split, altura natural para mostrar todo */
    .indicators-section {
        flex: none !important; height: auto !important;
        overflow: visible !important; padding: 10px 12px;
    }
    .indicators-section h2 { font-size: 0.78rem; margin-bottom: 6px; }
    /* hbars-row: eliminar overflow interno — el body se encarga */
    .hbars-row { flex: none !important; overflow-y: visible !important; gap: 5px; }

    /* Filas de barras: touch-friendly */
    .hbar-item { padding: 3px 3px; gap: 6px; }
    .hbar-label { min-width: 82px; font-size: 0.68rem; }
    .hbar-track { height: 22px; border-radius: 11px; }
    .hbar-fill { border-radius: 11px 0 0 11px; }
    .hbar-mark { top: -3px; bottom: -3px; }
    .hbar-mark-mid { top: -4px; bottom: -4px; }
    .hbar-scale { font-size: 0.55rem; }
    .hbar-value { font-size: 0.92rem; min-width: 76px; }
    .zone-group-header { font-size: 0.65rem; padding: 4px 4px 4px 8px; margin-top: 5px; }
    .hbar-alert-tag { font-size: 0.5rem; padding: 1px 5px; }
}

/* ---- Responsive: tablet horizontal ---- */
@media (max-width: 1024px) and (orientation: landscape) and (max-height: 600px) {
    body { overflow: auto !important; height: auto !important; min-height: 100vh; }
    .dashboard { flex: none !important; height: auto !important; overflow: visible !important; min-height: calc(100vh - 50px); }
    .main-row { flex-direction: column !important; flex: none !important; }
    .render-column { height: 55vh !important; min-height: 250px !important; flex: none !important; }
    .side-panel { width: 100% !important; overflow-y: visible !important; }
    .indicators-section { flex: none !important; height: auto !important; overflow: visible !important; }
    .hbars-row { flex: none !important; overflow-y: visible !important; }
    .header-right { display: none !important; }
    .header-center { font-size: 0.72rem !important; }
    .mode-toggle button { padding: 6px 14px; font-size: 0.65rem; }
    .btn-expand-render { display: none !important; }
}
