/* =====================================================
   SISTEMA DE MONITOREO IoT - ESTILOS GLOBALES (CORREGIDO)
   ===================================================== */

/* Reset y configuración base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Paleta de colores */
    --brand-blue: #3b82c4;
    --brand-blue-light: #60a5fa;
    --brand-blue-dark: #2563eb;
    --brand-blue-soft: #dbeafe;
    --brand-blue-ultra-soft: #eff6ff;

    /* Sistema */
    --primary: #1f2937;
    --secondary: #f3f4f6;
    --accent: var(--brand-blue);
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --bg: #f9fafb;
    --card-bg: #ffffff;
    --text: #111827;
    --text-light: #6b7280;
    --border: #e5e7eb;
    --shadow: rgba(59, 130, 196, 0.08);
    --shadow-hover: rgba(59, 130, 196, 0.15);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}

/* Fondo decorativo */
body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 300px;
    background: linear-gradient(135deg, var(--brand-blue-soft) 0%, var(--brand-blue-ultra-soft) 100%);
    z-index: -1;
    opacity: 0.3;
}

/* Contenedores */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

.container-wide {
    max-width: 1600px; /* Un poco más ancho para tablas históricas */
    margin: 0 auto;
    padding: 2rem;
}

/* =====================================================
   HEADER
   ===================================================== */
.header {
    margin-bottom: 3rem;
    display: flex;
    align-items: center;
    gap: 1.5rem;
    flex-wrap: wrap;
}

.logo {
    width: 56px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 2px 4px rgba(59, 130, 196, 0.2));
    transition: transform 0.3s ease;
}

.logo:hover img {
    transform: scale(1.05);
}

.header-text {
    flex: 1;
}

.header-text h1 {
    font-size: 1.875rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 0.25rem;
}

.header-text p {
    color: var(--text-light);
    font-size: 0.95rem;
}

.header-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 10px;
}

/* =====================================================
   MÉTRICAS Y ESTADÍSTICAS
   ===================================================== */
.metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 3rem;
}

.metric-card {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.metric-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px var(--shadow-hover);
    border-color: var(--brand-blue-soft);
}

.metric-icon {
    width: 40px;
    height: 40px;
    background: var(--brand-blue-soft);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--brand-blue);
    font-size: 1.25rem;
    margin-bottom: 1rem;
}

.metric-value {
    font-size: 2.25rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 0.25rem;
}

.metric-label {
    font-size: 0.875rem;
    color: var(--text-light);
    font-weight: 500;
}

/* Stats (usado en histórico) */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: var(--card-bg);
    padding: 1rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-light);
}

/* =====================================================
   CONTROLES Y BOTONES
   ===================================================== */
.controls-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: 0 1px 3px var(--shadow);
}

.controls {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end; /* Alinea los inputs y botones abajo */
    gap: 1.5rem;
    width: 100%;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    flex: 1;
    min-width: 200px; /* Evita que se encojan demasiado */
}

.control-group label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-light);
}

/* Inputs y Selects */
.form-control, 
.control-group input, 
.control-group select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.875rem;
    background: var(--bg);
    color: var(--text);
    transition: all 0.2s ease;
}

.control-actions {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

/* Botones Generales */
.btn {
    padding: 0.75rem 1.5rem;
    border: 1px solid var(--border);
    background: var(--card-bg);
    color: var(--text);
    border-radius: 10px;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap; /* Evita que el texto se rompa */
}

.btn:hover {
    border-color: var(--brand-blue);
    color: var(--brand-blue-dark);
    transform: translateY(-1px);
}

.btn-primary {
    background: var(--brand-blue);
    color: white;
    border-color: var(--brand-blue);
}

.btn-primary:hover {
    background: var(--brand-blue-dark);
    color: white;
}

/* Botón Mini (Para el Header) */
.btn-mini {
    background: white;
    border: 1px solid var(--border);
    padding: 0.4rem 0.8rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-light);
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.btn-mini:hover {
    border-color: var(--brand-blue);
    color: var(--brand-blue);
}

.btn-mini.active {
    background: var(--brand-blue-ultra-soft);
    border-color: var(--brand-blue);
    color: var(--brand-blue-dark);
}

/* =====================================================
   DISPOSITIVOS (GRID)
   ===================================================== */
.devices-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.device-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.5rem;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.device-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 12px 32px var(--shadow-hover);
    border-color: var(--brand-blue-soft);
}

/* Indicadores de estado */
.device-card::before {
    content: "";
    position: absolute;
    top: -20px;
    right: -20px;
    width: 60px;
    height: 60px;
    background: var(--success);
    clip-path: polygon(30% 0%, 70% 0%, 100% 50%, 70% 100%, 30% 100%, 0% 50%);
    opacity: 0.1;
}

.device-card.offline::before { background: var(--danger); }
.device-card.alert::before { background: var(--warning); }

.device-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
}

.device-id {
    font-weight: 700;
    font-size: 1.125rem;
    color: var(--primary);
}

.device-metrics {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1rem;
}

.metric {
    display: flex;
    flex-direction: column;
    padding: 0.75rem;
    background: var(--brand-blue-ultra-soft);
    border-radius: 8px;
    text-align: center;
}

.metric-name {
    font-size: 0.75rem;
    color: var(--text-light);
    text-transform: uppercase;
    font-weight: 600;
}

.metric-value {
    font-size: 1.375rem;
    font-weight: 700;
    color: var(--brand-blue-dark);
}

.device-info {
    border-top: 1px solid var(--border);
    padding-top: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    color: var(--text-light);
}

/* Badges */
.alert-badge {
    background: var(--warning);
    color: white;
    padding: 0.25rem 0.75rem;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 600;
    display: inline-block;
    margin-bottom: 1rem;
}

/* =====================================================
   TABLAS (HISTÓRICO)
   ===================================================== */
.table-card {
    background: var(--card-bg);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 1px 3px var(--shadow);
    border: 1px solid var(--border);
    margin-top: 2rem;
}

.table-container {
    max-height: 600px;
    overflow-y: auto;
    overflow-x: auto;
}

table {
    width: 100%;
    min-width: 900px;
    border-collapse: collapse;
}

thead {
    background: var(--brand-blue-ultra-soft);
    position: sticky;
    top: 0;
    z-index: 10;
}

th {
    padding: 1rem 1.5rem;
    text-align: left;
    font-weight: 600;
    color: var(--text);
    font-size: 0.875rem;
    text-transform: uppercase;
    border-bottom: 2px solid var(--brand-blue-soft);
}

td {
    padding: 1rem 1.5rem;
    text-align: left;
    font-size: 0.875rem;
    color: var(--text);
    border-bottom: 1px solid var(--border);
}

tbody tr:hover {
    background: var(--brand-blue-ultra-soft);
}

.alert-cell { color: var(--warning); font-weight: 700; }
.temp-cell { color: var(--brand-blue); font-weight: 600; }
.humidity-cell { color: var(--brand-blue-dark); font-weight: 600; }

/* =====================================================
   MENSAJES Y FOOTER
   ===================================================== */
.status-message {
    text-align: center;
    padding: 3rem;
    color: var(--brand-blue);
}

.error-message {
    background: #fee2e2;
    color: #dc2626;
    padding: 1rem;
    border-radius: 10px;
    margin: 1rem 0;
    border: 1px solid #fecaca;
    text-align: center;
}

.loading-indicator {
    display: inline-block;
    width: 40px;
    height: 40px;
    margin: 0 auto;
    border: 3px solid var(--brand-blue-soft);
    border-top-color: var(--brand-blue);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.footer {
    text-align: center;
    padding: 2rem 0;
    color: var(--text-light);
    font-size: 0.875rem;
    border-top: 1px solid var(--border);
    margin-top: 3rem;
}

/* =====================================================
   RESPONSIVE
   ===================================================== */
@media (max-width: 768px) {
    .header {
        flex-direction: column;
        text-align: center;
    }
    
    .header-actions {
        margin-left: 0;
        margin-top: 1rem;
        flex-direction: column;
    }
    
    .controls {
        flex-direction: column;
        align-items: stretch;
    }
    
    .control-actions {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
        justify-content: center;
    }
    
    .devices-grid {
        grid-template-columns: 1fr;
    }
    
    .stats {
        grid-template-columns: 1fr 1fr;
    }
}
.grafico-container {
    /* Esto evita que el gráfico se parta a la mitad */
    page-break-inside: avoid; 
    break-inside: avoid;
    
    /* Opcional: asegura margen al imprimir */
    display: block;
    margin-bottom: 20px;
}