/* =============================================================
   Responsive mobile — bottom nav, touch targets, layouts colapsados.
   Breakpoint principal: max-width 768px. Safe-area y anti-zoom iOS.
   ============================================================= */

:root {
    --mobile-nav-height: 108px;
    --mobile-nav-sections-height: 52px;
    --mobile-nav-actions-height: 48px;
    --mobile-nav-z: 200;
    --mobile-touch-min: 44px;
    --mobile-input-font: 16px;
}

/* ============================================================
   BOTTOM NAVIGATION BAR
   ============================================================ */
.mobile-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    flex-direction: column;
    background: var(--app-header-bg);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-top: var(--border-width) solid var(--border-sutil);
    box-shadow: 0 -2px 16px rgba(15, 23, 42, 0.06);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: var(--mobile-nav-z);
    transform: translateY(0);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
}

.mobile-nav.is-hidden {
    transform: translateY(100%);
    pointer-events: none;
}

body.dark-mode .mobile-nav {
    box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.4);
}

/* Fila 1: secciones del plan */
.mobile-nav-sections {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: var(--space-1);
    padding: var(--space-2) var(--space-2) 0;
    min-height: var(--mobile-nav-sections-height);
}

.mobile-nav-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: var(--space-1);
    min-height: var(--mobile-touch-min);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    text-decoration: none;
    background: none;
    border: none;
    cursor: pointer;
    transition: color var(--transition-fast), background var(--transition-fast);
    font-family: var(--font-ui);
}

.mobile-nav-item .icon {
    width: 18px;
    height: 18px;
}

.mobile-nav-item > span:not(.tab-badge):not(.mobile-tab-badge) {
    font-size: 9px;
    font-weight: 600;
    letter-spacing: 0.2px;
    line-height: 1.1;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.mobile-nav-tab.active {
    color: var(--accent);
    background: rgba(var(--success-rgb), 0.1);
}

.mobile-nav-tab:active {
    background: var(--surface-2);
}

.mobile-tab-badge {
    position: absolute;
    top: 2px;
    right: 6px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    font-size: 9px;
    line-height: 16px;
}

/* Fila 2: acciones rápidas */
.mobile-nav-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    min-height: var(--mobile-nav-actions-height);
}

.mobile-nav-action {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex: 1;
    min-height: 40px;
    max-height: 44px;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-md);
    border: var(--border-width) solid var(--border-sutil);
    background: var(--surface-2);
    color: var(--text-secondary);
    cursor: pointer;
    font-family: var(--font-ui);
    transition: border-color var(--transition-fast), background var(--transition-fast), transform var(--transition-fast);
}

.mobile-nav-action .icon {
    width: 18px;
    height: 18px;
}

.mobile-nav-action:active:not(:disabled) {
    transform: scale(0.97);
}

.mobile-nav-action:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.mobile-nav-action--primary {
    flex: 1.35;
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-deep) 100%);
    border-color: transparent;
    color: var(--accent-fg);
    box-shadow: var(--shadow-glow);
}

.mobile-nav-action--primary .icon {
    color: var(--accent-fg);
}

.mobile-nav-action--primary span {
    font-size: 11px;
    font-weight: var(--font-weight-semibold);
}

.mobile-nav-action--primary.is-loading .icon {
    animation: spin 0.8s linear infinite;
}

/* Acciones secundarias: solo icono visible */
.mobile-nav-action:not(.mobile-nav-action--primary) .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ============================================================
   TOUCH TARGETS + INPUTS
   Solo se aplican en <=768px para no afectar al desktop.
   ============================================================ */
@media (max-width: 768px) {

    .mobile-nav {
        display: flex;
    }

    /* CTAs del sidebar: en móvil viven en la bottom bar */
    .sim-actions-desktop {
        display: none !important;
    }

    /* Pestañas del sidebar: en móvil viven en la bottom bar */
    .config-panel .section-tabs.sidebar-tabs {
        display: none !important;
    }

    /* Padding inferior para que el contenido no quede tapado por la nav */
    .calculator-wrapper.simulator-layout {
        padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom) + var(--space-4));
    }

    body.landing-page main {
        padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    }

    /* Inputs touch-friendly y sin zoom auto en iOS */
    .input,
    .input-number,
    .calculator-wrapper input[type="text"],
    .calculator-wrapper input[type="number"],
    .calculator-wrapper input[type="email"],
    .calculator-wrapper input[type="password"],
    .calculator-wrapper select,
    .calculator-wrapper textarea {
        font-size: var(--mobile-input-font);
        min-height: var(--mobile-touch-min);
    }

    select.input,
    .calculator-wrapper select {
        height: var(--mobile-touch-min);
    }

    /* Botones primarios y secundarios con altura tactil */
    .btn-primary,
    .btn-secondary,
    .btn-ghost,
    .btn-run,
    .btn-small {
        min-height: var(--mobile-touch-min);
    }

    /* CTA principal de simulacion mas grande */
    .btn-run.btn-simulate {
        min-height: 56px;
        font-size: var(--text-base);
    }

    /* Slider con thumb mas grande */
    input[type="range"].input-range {
        height: 6px;
    }
    input[type="range"].input-range::-webkit-slider-thumb {
        width: 24px;
        height: 24px;
    }
    input[type="range"].input-range::-moz-range-thumb {
        width: 24px;
        height: 24px;
    }

    /* Secciones colapsables: trigger mas comodo */
    .collapsible-trigger {
        min-height: var(--mobile-touch-min);
        padding: var(--space-4);
    }

    /* Tabs de seccion: dejar tambien texto pero compactar */
    .section-tab.tab-btn {
        padding: var(--space-2);
        gap: var(--space-1);
    }
    .section-tab.tab-btn > span:not(.tab-badge):not(.tab-status) {
        font-size: 11px;
    }

    /* Fields grid: 1 columna */
    .field-group,
    .grid-2-col {
        grid-template-columns: 1fr !important;
    }
}

/* ============================================================
   MOBILE RESULTS DASHBOARD (Fase 3 components)
   ============================================================ */
@media (max-width: 768px) {

    /* Indicador de exito: vertical, gauge mas pequeno */
    .success-indicator {
        flex-direction: column;
        text-align: center;
        gap: var(--space-4);
        padding: var(--space-4);
    }
    .success-gauge {
        width: 96px;
        height: 96px;
    }
    .gauge-svg {
        width: 96px;
        height: 96px;
    }
    .gauge-value {
        font-size: var(--text-xl);
    }
    .success-title {
        font-size: var(--text-xl);
    }
    .success-subtitle {
        font-size: var(--text-sm);
    }
    .success-info .ruin-risk {
        margin-left: auto;
        margin-right: auto;
    }

    /* Tarjetas de escenario: una sola columna */
    .scenario-cards {
        grid-template-columns: 1fr;
    }
    .scenario-featured::before {
        display: none;
    }
    .scenario-value .font-display {
        font-size: var(--text-2xl);
    }

    /* Totales: 2x2 */
    .totals-grid {
        grid-template-columns: 1fr 1fr;
        gap: var(--space-2);
    }
    .total-card {
        padding: var(--space-3);
    }
    .total-card .metric-value {
        font-size: var(--text-base);
    }

    /* Diagnosis card mas compacto */
    .diagnosis-card {
        padding: var(--space-4);
    }

    /* Plan summary card: 1 columna */
    .plan-summary-grid {
        grid-template-columns: 1fr;
        gap: var(--space-3);
    }
    .portfolio-legend {
        gap: var(--space-3);
    }
}

@media (max-width: 480px) {
    /* En pantallas muy pequenas, totales en 1 columna */
    .totals-grid {
        grid-template-columns: 1fr;
    }
}

/* ============================================================
   TABLAS Y CONTENIDO ANCHO: scroll horizontal en movil
   ============================================================ */
@media (max-width: 768px) {
    .table-wrapper,
    .table-scroll-area,
    #yearlyTableBody,
    #heatmapTable {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    #heatmapTable {
        min-width: 480px;
    }
}

/* ============================================================
   HEADER en movil (simulador)
   ============================================================ */
@media (max-width: 768px) {
    /* Anula style.css: .header-content { flex-direction: column } que rompe la barra */
    .app-header.site-header .header-inner.header-content {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        flex-wrap: nowrap;
        gap: var(--space-2);
        padding: var(--space-2) var(--space-3);
        height: auto;
        min-height: var(--app-header-height);
        text-align: left;
    }

    .app-header .logo {
        flex-shrink: 0;
        min-width: 0;
    }

    .app-header .header-controls,
    .app-header .controls-top {
        flex: 1 1 auto;
        min-width: 0;
        flex-wrap: nowrap;
        justify-content: flex-end;
        gap: var(--space-2);
        margin-left: 0;
    }

    .app-header .select-compact,
    .app-header .select-top {
        flex: 0 1 auto;
        min-width: 0;
        max-width: 5.5rem;
        padding: 0 var(--space-2);
        font-size: var(--text-xs);
        height: 36px;
    }

    .app-header #currSelector {
        max-width: 6.25rem;
    }

    .app-header .btn-icon,
    .app-header #btnNotifications,
    .app-header #themeToggle {
        width: 36px;
        height: 36px;
        flex-shrink: 0;
    }

    .app-header .btn-secondary.nav-link-btn {
        padding: 0 var(--space-2);
        flex-shrink: 0;
    }

    .app-header .btn-secondary.nav-link-btn span,
    .app-header .nav-link-btn span {
        display: none;
    }

    .sidebar-footer.config-cta,
    .config-cta {
        padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom) + var(--space-4));
    }

    #toast-container {
        bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom) + 72px);
    }
}

/* Modo oscuro — cajas legacy de resultados en movil */
@media (max-width: 768px) {
    body.dark-mode .results-panel .stat-box {
        background: var(--surface-2);
        border-color: var(--border-sutil);
        color: var(--text-primary);
    }

    body.dark-mode .results-panel .stat-box h3 {
        color: var(--text-secondary);
    }
}

/* ============================================================
   LANDING: ajustes de hero mockup para movil
   ============================================================ */
@media (max-width: 480px) {
    .hero-mockup .mockup-overlay {
        bottom: var(--space-2);
        right: var(--space-2);
    }
    .mockup-metric {
        min-width: 160px;
        padding: var(--space-2) var(--space-3);
    }
}

/* ============================================================
   ANIMACION del spinner de simulate en bottom nav
   ============================================================ */
@keyframes spin {
    to { transform: rotate(360deg); }
}
