/* ============================================================
   GUARUGEO - MOBILE UI (MODULAR & OPTIMIZED)
   ============================================================ */

@media (max-width: 768px) {
    :root {
        --sidebar-width: 100vw;
    }

    #app-container {
        flex-direction: column;
        height: 100vh;
        height: 100dvh;
        width: 100vw;
        overflow: hidden;
        margin: 0;
        padding: 0;
    }

    /* --- SIDEBAR AS BOTTOM SHEET --- */
    #sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100vw;
        height: 75vh;
        /* More space for content */
        z-index: 2000;
        border-radius: 20px 20px 0 0;
        transform: translateY(calc(100% - 65px));
        /* Peek height */
        transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        background: #fff;
        box-shadow: 0 -8px 25px rgba(0, 0, 0, 0.15);
        display: flex;
        flex-direction: column;
    }

    #sidebar.active {
        transform: translateY(0);
    }

    /* Drag Handle - Larger Touch Area */
    #sidebar::after {
        content: '';
        position: absolute;
        top: 10px;
        left: 50%;
        transform: translateX(-50%);
        width: 48px;
        height: 5px;
        background: #cbd5e1;
        border-radius: 4px;
        z-index: 2001;
    }

    /* Header Compacted for Mobile */
    .sidebar-logo-container {
        padding: 20px 16px 5px;
        /* Adjusted for handle */
        display: flex;
        justify-content: flex-start;
        height: 65px;
        align-items: center;
    }

    .sidebar-logo-container img {
        height: 32px;
        width: auto;
    }

    .sidebar-header {
        padding: 5px 16px 16px;
        border-bottom: 1px solid #f1f5f9;
    }

    /* Actions Row (Icons) */
    .header-actions {
        position: absolute;
        right: 16px;
        top: 18px;
        gap: 12px;
    }

    .header-actions button {
        font-size: 1.25rem !important;
        padding: 8px !important;
    }

    /* Search Box Mobile */
    .search-box {
        margin-top: 10px;
    }
    
    .search-box input {
        padding: 14px 16px;
        font-size: 16px; /* Prevents iOS auto-zoom */
        border-radius: 12px;
        height: 50px;
    }

    /* --- FORM & INPUT OPTIMIZATION --- */
    input[type="text"], input[type="email"], input[type="password"], select, textarea {
        font-size: 16px !important; /* Absolute requirement for iOS mobile */
        padding: 12px !important;
        border-radius: 8px !important;
        min-height: 48px;
    }

    button {
        min-height: 48px; /* Standard Apple/Google touch target */
        font-size: 16px;
        font-weight: 700;
        letter-spacing: 0.5px;
    }

    /* --- LOGIN BOX OPTIMIZATION --- */
    #loginOverlay {
        padding: 0;
        background: #0f172a; /* Solid for performance */
    }

    .login-box {
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        border-radius: 0 !important;
        padding: 30px 20px !important;
        display: flex;
        flex-direction: column;
        justify-content: center;
        box-shadow: none !important;
        animation: fadeIn 0.4s ease-out;
    }

    .login-box img {
        max-width: 180px !important;
        margin-bottom: 20px !important;
    }

    .login-box h2 {
        font-size: 22px !important;
        margin-bottom: 8px !important;
    }

    .login-box p {
        font-size: 13px !important;
        margin-bottom: 30px !important;
    }

    .login-box form {
        width: 100%;
        max-width: 320px;
        margin: 0 auto;
    }

    .login-box input {
        height: 52px !important;
        margin-bottom: 12px !important;
        background: #f8fafc !important;
        border: 1px solid #e2e8f0 !important;
    }

    .login-box button#btnLogin {
        height: 52px !important;
        margin-top: 10px !important;
        background: #2563eb !important;
        box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
    }

    /* Results List Mobile */
    .result-item {
        padding: 16px !important;
        margin-bottom: 12px !important;
        border-radius: 12px !important;
        background: #f8fafc;
    }

    .result-item h4 {
        font-size: 16px !important;
        margin-bottom: 6px !important;
    }

    /* --- MAP CONTROLS REPOSITIONING --- */
    /* Push controls up to avoid bottom sheet obstruction */
    .leaflet-bottom {
        bottom: 80px !important;
    }

    .gps-location-btn {
        bottom: 140px !important;
        right: 16px !important;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    }

    /* --- TOOLTIP MOBILE OPTIMIZATION --- */
    /* Full width card at bottom, or constrained modal */
    .leaflet-popup-content-wrapper {
        border-radius: 16px;
        padding: 0;
        overflow: hidden;
    }

    .leaflet-popup-content {
        margin: 0 !important;
        width: 300px !important; /* Fixed width for better layout */
        max-width: 90vw !important;
    }
    
    /* If using custom non-leaflet tooltip div */
    .lot-tooltip {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        height: auto !important;
        max-height: 85vh !important;
        border-radius: 20px 20px 0 0 !important;
        transform: none !important; /* Override translate centering */
        margin: 0 !important;
        box-shadow: 0 -5px 20px rgba(0,0,0,0.2) !important;
        overflow-y: auto !important;
        /* Smooth Entrance */
        animation: slideUp 0.3s ease-out;
        z-index: 10000 !important;
    }

    @keyframes slideUp {
        from { transform: translateY(100%); opacity: 0; }
        to { transform: translateY(0); opacity: 1; }
    }

    /* Tooltip Header Mobile */
    .lot-tooltip-header {
        position: sticky;
        top: 0;
        background: #fff;
        z-index: 10;
        padding: 16px !important;
        border-bottom: 1px solid #f1f5f9;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .lot-tooltip-close {
        font-size: 28px !important;
        padding: 10px !important; /* Larger touch target */
        color: #64748b !important;
        background: #f1f5f9 !important;
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Tooltip Actions - Horizontal Scroll */
    .header-buttons-wrapper {
        display: flex;
        overflow-x: auto;
        padding: 4px 16px 12px;
        gap: 8px;
        -webkit-overflow-scrolling: touch; /* Smooth scroll iOS */
        scrollbar-width: none; /* Hide scrollbar */
    }
    
    .header-buttons-wrapper::-webkit-scrollbar {
        display: none; 
    }

    .tooltip-action-btn {
        flex: 0 0 auto; /* Don't shrink */
        padding: 8px 16px !important;
        border-radius: 20px !important;
        font-size: 13px !important;
    }
    
    /* Show text on mobile for better clarity in horiz scroll */
    .tooltip-action-btn .btn-text {
        display: inline-block !important; 
    }

    /* TOOLTIP HEADER REFACTOR (PREMIUM DARK) */
    .lot-tooltip-header {
        background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%) !important;
        padding: 24px 20px 0 20px !important;
        min-width: 0 !important;
        border-radius: 16px 16px 0 0 !important;
    }

    #lot-tooltip-header .lot-tooltip-tabs {
        margin: 0 -20px !important; /* Adjust for mobile padding */
        background: rgba(255, 255, 255, 0.05) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        backdrop-filter: blur(10px) !important;
    }

    .lot-tab-btn {
        padding: 12px 15px !important;
        font-size: 9px !important;
    }

    .header-content-flex {
        flex-direction: column !important;
        gap: 16px !important;
        position: relative !important;
    }

    .header-text-info {
        min-width: 100% !important;
        padding-right: 30px !important; /* Space for close button */
    }

    #lot-tooltip-actions {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 6px !important;
        width: 100% !important;
        justify-content: stretch !important;
        margin-top: 10px !important;
        padding-bottom: 5px !important;
    }

    #lot-tooltip-actions > button, 
    #lot-tooltip-actions > div > button {
        width: 100% !important;
        height: 40px !important; /* Compact but touch friendly */
        justify-content: center !important;
        padding: 4px !important;
        font-size: 10px !important;
    }

    .header-buttons-wrapper button .btn-text {
        display: none !important; /* Icon only on 4-col grid to save space */
    }

    /* Smallest buttons keep text if it fits, but usually hide it */
    .header-buttons-wrapper button:first-child .btn-text {
        display: none !important; 
    }

    #lot-tooltip-close-btn {
        position: absolute !important;
        top: 12px !important;
        right: 12px !important;
        font-size: 22px !important;
        color: white !important;
        background: rgba(255, 255, 255, 0.15) !important;
        width: 34px !important;
        height: 34px !important;
        border-radius: 50% !important;
        border: 2px solid rgba(255, 255, 255, 0.2) !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        padding: 0 !important;
        z-index: 1000 !important;
        cursor: pointer !important;
        box-shadow: 0 2px 8px rgba(0,0,0,0.3) !important;
    }
    
    #lot-tooltip-close-btn:hover {
        background: rgba(255, 255, 255, 0.3) !important;
        transform: scale(1.1);
    }
    
    /* Primary button (Navigate) gets full width, others share 4-col grid */
    #lot-tooltip-actions > button:first-child,
    #lot-tooltip-actions > div:first-child > button {
        grid-column: span 4 !important;
        height: 44px !important;
        font-size: 11px !important;
    }

    #lot-tooltip-actions > button:first-child .btn-text,
    #lot-tooltip-actions > div:first-child > button .btn-text {
        display: inline !important;
    }

    /* Hide specific long texts on mobile buttons if needed */
    #lot-tooltip-actions .btn-text {
        display: inline !important; /* Ensure they are visible but might wrap */
    }
    #lot-tooltip-actions button:first-child:nth-last-child(odd) {
        grid-column: span 2;
    }

    /* Tooltip Tab Bar Mobile */
    .lot-tooltip-tabs {
        display: flex;
        padding: 0 10px;
        background: rgba(255,255,255,0.05);
        border-bottom: 1px solid #f1f5f9;
        overflow-x: auto;
        white-space: nowrap;
        scrollbar-width: none;
    }

    .lot-tab-btn {
        flex: 1;
        min-width: 120px;
        padding: 14px 10px !important;
        font-size: 11px !important;
        font-weight: 800 !important;
        text-transform: uppercase;
        border-bottom: 3px solid transparent !important;
        color: #94a3b8 !important;
        background: none !important;
    }

    .lot-tab-btn.active {
        color: #2563eb !important;
        border-bottom-color: #2563eb !important;
    }

    /* --- HISTORY & SOLAR WIDGETS --- */
    /* Reposition History Panel to not block bottom sheet */
    #history-panel {
        bottom: 80px !important;
        right: 10px !important;
        width: auto !important;
        max-width: 200px;
        border-radius: 12px;
    }
    
    #history-panel.collapsed {
        transform: translateX(calc(100% - 40px)); /* Slide to side instead of down */
    }

    /* --- MODALS FULLSCREEN ON MOBILE --- */
    .custom-modal {
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        max-height: 100% !important;
        border-radius: 0 !important;
        display: flex;
        flex-direction: column;
    }

    .custom-modal-body {
        flex: 1;
        overflow-y: auto;
        padding: 20px !important;
    }
    
    .custom-modal-close {
        font-size: 32px;
        padding: 10px;
    }

    /* --- FILTER CHIPS --- */
    .filter-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .filter-chip {
        flex: 1 1 calc(50% - 8px);
        min-height: 48px; /* Slightly larger for touch */
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 8px 12px !important;
        font-size: 14px !important;
        border-radius: 10px !important;
    }

    /* Responsive Grid for Dashboard Cards */
    .dashboard-grid, .stats-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .stat-card {
        padding: 15px !important;
    }

    /* SAFE AREA PADDING (iPhone X+) */
    body {
        padding-bottom: env(safe-area-inset-bottom);
        padding-top: 0 !important; /* Force no top padding for 100% usage */
    }
    
    #sidebar {
        padding-bottom: calc(env(safe-area-inset-bottom) + 10px);
    }

    /* Weather Widget Mobile Repositioning */
    #weather-widget {
        top: 10px !important;
        right: 10px !important;
        transform: scale(0.9);
        transform-origin: top right;
        z-index: 500 !important;
    }

    /* Hide complex desktop-only elements */
    .desktop-only {
        display: none !important;
    }
}

/* ============================================================
   GUARUGEO - MOBILE TOUCH TARGET ENHANCEMENTS (FINE-TUNING)
   Applies only on phones. No function is removed.
   ============================================================ */
@media (max-width: 768px) {

    /* --- SIDEBAR TABS: larger tap zone, readable text --- */
    .tab-btn {
        padding: 16px 14px !important;
        font-size: 11px !important;
        letter-spacing: 0.3px !important;
        min-height: 52px;
    }

    /* Sidebar tab content padding – more breathing room */
    .sidebar-tab-content {
        padding: 16px !important;
    }

    /* --- FILTER CHIPS: bigger tap zone & readable text --- */
    .filter-chip {
        font-size: 12px !important;
        padding: 14px 10px !important;
        min-height: 52px;
        border-radius: 14px !important;
        letter-spacing: 0.3px !important;
    }

    /* --- SEARCH RESULTS: bigger rows for fat fingers --- */
    .search-item {
        padding: 18px 16px !important;
        min-height: 60px;
    }

    .search-item h4 {
        font-size: 15px !important;
    }

    .search-item p {
        font-size: 12px !important;
        margin-top: 3px;
    }

    /* Suggestion dropdown rows */
    .suggestion-item {
        padding: 14px 16px !important;
        min-height: 52px;
    }

    .suggestion-main {
        font-size: 14px !important;
    }

    /* --- CONTEXT MENU: large tap targets --- */
    .context-menu-item {
        padding: 16px 18px !important;
        font-size: 15px !important;
        min-height: 52px;
        gap: 12px !important;
    }

    /* --- MAP BACK BUTTON: easier to hit --- */
    .map-back-btn {
        top: 70px !important;
        left: 10px !important;
        padding: 14px 20px !important;
        font-size: 15px !important;
        border-radius: 14px !important;
        min-height: 52px;
    }

    /* --- LOT-TOOLTIP TABS: still fits in a row, but tappable --- */
    .lot-tab-btn {
        padding: 16px 12px !important;
        font-size: 10px !important;
        gap: 5px !important;
        min-height: 52px;
    }

    /* Tooltip body – more padding so data rows are easier to read */
    .lot-tooltip-body {
        padding: 20px 16px !important;
    }

    /* Info Blocks Mobile Grid */
    .lot-tooltip .info-block {
        padding: 8px !important;
    }

    .lot-tooltip .info-label {
        font-size: 9px !important;
    }

    .lot-tooltip .info-value {
        font-size: 13px !important;
    }

    /* --- TOOLTIP ACTION BUTTONS (inside enrichment card) --- */
    .tooltip-action-btn,
    .btn-enrich,
    .btn-certidao,
    .btn-streetview,
    .btn-report,
    .btn-portfolio {
        min-height: 48px !important;
        font-size: 13px !important;
        padding: 10px 16px !important;
        border-radius: 22px !important;
    }

    /* --- INLINE DATA BUTTONS (copy, expand) inside tooltip rows --- */
    .data-field-copy-btn,
    .inline-btn {
        min-height: 44px !important;
        min-width: 44px !important;
        padding: 8px !important;
        font-size: 14px !important;
    }

    /* --- HEADER ACTION BUTTONS (notification bell, logout, etc) --- */
    .header-actions button,
    #btnNotifications,
    #btnLogout,
    #btnAdmin,
    #btnLeads {
        min-height: 44px !important;
        min-width: 44px !important;
        font-size: 1.3rem !important;
        padding: 10px !important;
        border-radius: 50%;
    }

    /* --- STRATEGY BAR ICONS: easier to tap --- */
    .strategy-bar i {
        font-size: 20px !important;
        padding: 8px !important;
    }

    /* --- GPS BUTTON: consistent size --- */
    .gps-location-btn {
        width: 52px !important;
        height: 52px !important;
        font-size: 22px !important;
    }

    /* Prevent data-tooltip hover labels from showing on touch
       (tooltips opened by hover make no sense on mobile) */
    [data-tooltip]::before,
    [data-tooltip]::after {
        display: none !important;
    }
}

/* ============================================================
   GUARUGEO - MOBILE BUG FIXES
   1. Logo centralizado na tela de login
   2. Botões do tooltip não "fogem" horizontalmente
   ============================================================ */
@media (max-width: 768px) {

    /* --- FIX 1: Login logo — centralizar no eixo cruzado do flex column --- */
    .login-box img {
        align-self: center !important;   /* Centra na flex column */
        display: block !important;
        margin-left: auto !important;
        margin-right: auto !important;
        max-width: 160px !important;     /* Menor para caber sem esticar */
        height: auto !important;
    }

    /* LOGIN BOX: garante que o flex column centraliza tudo */
    .login-box {
        align-items: center !important;
    }

    /* Login form: largura total dentro da box */
    .login-box form {
        width: 100% !important;
        max-width: 340px !important;
        margin: 0 auto !important;
    }

    /* --- FIX 2: Better Tooltip Buttons — Grid based --- */
    .header-buttons-wrapper {
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        flex-wrap: nowrap !important;
        overflow-x: visible !important;
        gap: 8px !important;
        padding: 12px 16px !important;
    }

    .header-buttons-wrapper .tooltip-action-btn,
    .header-buttons-wrapper button {
        flex: none !important;
        max-width: 100% !important;
        min-width: 0 !important;
        width: 100% !important;
        height: 48px !important; /* Better touch target */
        border-radius: 12px !important; /* Smoother */
        font-size: 13px !important;
    }

    /* Tooltip header adjustment for better mobile feel */
    .lot-tooltip-header {
        padding: 20px 16px 12px !important;
    }
}

