/**
 * LTD.DO PWA - Mobile-First Responsive Styles
 * Optimizado para todos los dispositivos
 */

/* ─────────────────────────────────────────────
   VARIABLES Y RESET
   ───────────────────────────────────────────── */
:root {
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
    --sidebar-width: 256px;
    --sidebar-collapsed: 80px;
    --header-height: 64px;
    --bottom-nav-height: 64px;
    --indigo-600: #4F46E5;
    --indigo-700: #4338CA;
}

/* Safe areas para PWA standalone */
.pwa-standalone body {
    padding-top: var(--safe-area-top);
    padding-bottom: var(--safe-area-bottom);
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

/* Touch optimizations */
* {
    -webkit-tap-highlight-color: transparent;
}

input,
textarea,
select,
button,
a {
    touch-action: manipulation;
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

/* ─────────────────────────────────────────────
   OFFLINE BANNER
   ───────────────────────────────────────────── */
#pwa-offline-banner {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9998;
    background: #F59E0B;
    color: #78350F;
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 500;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

#pwa-offline-banner i {
    animation: pulse 2s infinite;
}

/* ─────────────────────────────────────────────
   PULL TO REFRESH INDICATOR
   ───────────────────────────────────────────── */
#pwa-pull-refresh {
    position: fixed;
    top: -50px;
    left: 50%;
    transform: translateX(-50%) translateY(0);
    z-index: 9997;
    background: var(--indigo-600);
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

/* ─────────────────────────────────────────────
   INSTALL BANNER (MOBILE)
   ───────────────────────────────────────────── */
#pwa-install-banner-mobile {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9996;
    background: white;
    padding: 16px 20px;
    padding-bottom: calc(16px + var(--safe-area-bottom));
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
    align-items: center;
    gap: 12px;
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

#pwa-install-banner-mobile .pwa-install-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: var(--indigo-600);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#pwa-install-banner-mobile .pwa-install-icon span {
    color: white;
    font-weight: bold;
    font-size: 14px;
}

/* ─────────────────────────────────────────────
   BOTTOM NAVIGATION (MOBILE)
   ───────────────────────────────────────────── */
#pwa-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9990;
    background: white;
    border-top: 1px solid #E5E7EB;
    padding-bottom: var(--safe-area-bottom);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.08);
    height: auto;
    transition: transform 0.2s ease;
}

body.keyboard-active #pwa-bottom-nav {
    transform: translateY(100%);
    pointer-events: none;
}

#pwa-bottom-nav nav {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: var(--bottom-nav-height);
    padding: 0 4px;
}

#pwa-bottom-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    padding: 6px 2px;
    color: #9CA3AF;
    text-decoration: none;
    font-size: 10px;
    font-weight: 500;
    transition: color 0.2s;
    position: relative;
    min-width: 0;
}

#pwa-bottom-nav a i {
    font-size: 18px;
    margin-bottom: 3px;
}

#pwa-bottom-nav a.text-indigo-600 {
    color: var(--indigo-600);
}

#pwa-bottom-nav a .nav-badge {
    position: absolute;
    top: 2px;
    right: 50%;
    transform: translateX(12px);
    background: #EF4444;
    color: white;
    font-size: 9px;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

/* ─────────────────────────────────────────────
   MOBILE SIDEBAR OVERLAY
   ───────────────────────────────────────────── */
#mobile-sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9991;
    opacity: 0;
    transition: opacity 0.3s;
}

#mobile-sidebar-overlay.active {
    opacity: 1;
}

/* ─────────────────────────────────────────────
   RESPONSIVE CARDS
   ───────────────────────────────────────────── */
.pwa-card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: box-shadow 0.2s;
}

.pwa-card:active {
    box-shadow: 0 0 0 2px var(--indigo-600);
}

/* ─────────────────────────────────────────────
   RESPONSIVE TABLE → CARDS
   ───────────────────────────────────────────── */
.pwa-table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.pwa-table-responsive::-webkit-scrollbar {
    height: 4px;
}

.pwa-table-responsive::-webkit-scrollbar-track {
    background: #F3F4F6;
}

.pwa-table-responsive::-webkit-scrollbar-thumb {
    background: #D1D5DB;
    border-radius: 2px;
}

/* ─────────────────────────────────────────────
   RESPONSIVE FORMS
   ───────────────────────────────────────────── */
.pwa-form-group {
    margin-bottom: 16px;
}

.pwa-form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: #374151;
    margin-bottom: 4px;
}

.pwa-form-group input,
.pwa-form-group select,
.pwa-form-group textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #D1D5DB;
    border-radius: 8px;
    font-size: 16px;
    /* Prevent iOS zoom */
    transition: border-color 0.2s, box-shadow 0.2s;
    -webkit-appearance: none;
}

.pwa-form-group input:focus,
.pwa-form-group select:focus,
.pwa-form-group textarea:focus {
    outline: none;
    border-color: var(--indigo-600);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

/* ─────────────────────────────────────────────
   RESPONSIVE MODALS
   ───────────────────────────────────────────── */
.pwa-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9995;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.pwa-modal {
    background: white;
    border-radius: 16px;
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.25);
}

/* ─────────────────────────────────────────────
   FLOATING ACTION BUTTON
   ───────────────────────────────────────────── */
.pwa-fab {
    position: fixed;
    bottom: 80px;
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 28px;
    background: var(--indigo-600);
    color: white;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
    z-index: 9989;
    border: none;
    cursor: pointer;
    font-size: 20px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.pwa-fab:active {
    transform: scale(0.95);
}

.pwa-standalone .pwa-fab {
    bottom: calc(80px + var(--safe-area-bottom));
}

/* ─────────────────────────────────────────────
   SWIPE ACTIONS (for lists)
   ───────────────────────────────────────────── */
.pwa-swipeable {
    overflow: hidden;
    position: relative;
}

.pwa-swipeable-content {
    transition: transform 0.2s;
}

.pwa-swipeable-actions {
    position: absolute;
    top: 0;
    right: 0;
    height: 100%;
    display: flex;
    align-items: center;
    transform: translateX(100%);
}

/* ─────────────────────────────────────────────
   LOADING / SKELETON
   ───────────────────────────────────────────── */
.pwa-skeleton {
    background: linear-gradient(90deg, #F3F4F6 25%, #E5E7EB 50%, #F3F4F6 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
    border-radius: 6px;
}

@keyframes shimmer {
    0% {
        background-position: -200% 0;
    }

    100% {
        background-position: 200% 0;
    }
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* ─────────────────────────────────────────────
   RESPONSIVE BREAKPOINTS - MOBILE FIRST
   ───────────────────────────────────────────── */

/* ===== EXTRA SMALL (<480px) ===== */
@media (max-width: 479px) {

    /* Sidebar completely hidden */
    aside.bg-white.shadow-lg {
        position: fixed !important;
        z-index: 9992 !important;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        width: var(--sidebar-width) !important;
        height: 100vh;
    }

    aside.bg-white.shadow-lg.mobile-open {
        transform: translateX(0);
    }

    /* Main content full width */
    .flex.h-screen>.flex-1 {
        width: 100vw !important;
        margin-left: 0 !important;
    }

    /* Header adjustments */
    header.bg-white.shadow-sm {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    header h1 {
        font-size: 14px !important;
    }

    /* Content padding */
    main.flex-1 {
        padding: 12px !important;
        padding-bottom: calc(var(--bottom-nav-height) + 16px + var(--safe-area-bottom)) !important;
    }

    /* Show bottom navigation */
    #pwa-bottom-nav {
        display: block !important;
    }

    /* Stats grid */
    .grid.grid-cols-2,
    .grid.grid-cols-3,
    .grid.grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    .grid.grid-cols-2>div,
    .grid.grid-cols-3>div,
    .grid.grid-cols-4>div {
        padding: 12px !important;
    }

    /* Tables → cards on mobile */
    table.pwa-responsive-table thead {
        display: none !important;
    }

    table.pwa-responsive-table tbody tr {
        display: block !important;
        margin-bottom: 12px !important;
        border: 1px solid #E5E7EB !important;
        border-radius: 12px !important;
        padding: 12px !important;
        background: white !important;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05) !important;
    }

    table.pwa-responsive-table tbody td {
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        padding: 6px 0 !important;
        border: none !important;
        font-size: 13px !important;
    }

    table.pwa-responsive-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6B7280;
        font-size: 12px;
        text-transform: uppercase;
        margin-right: 8px;
    }

    /* Show FAB */
    .pwa-fab {
        display: flex !important;
    }

    /* Chat panel full screen */
    .fixed.right-0.top-0.h-full.w-96 {
        width: 100vw !important;
    }

    /* Filter/search bar stacking */
    .pwa-search-filters {
        flex-direction: column !important;
        gap: 8px !important;
    }

    .pwa-search-filters>* {
        width: 100% !important;
    }

    /* Buttons full width on mobile */
    .pwa-mobile-full-btn {
        width: 100% !important;
        justify-content: center !important;
    }

    /* Hide desktop-only elements */
    .pwa-desktop-only {
        display: none !important;
    }
}

/* ===== SMALL (480px - 767px) ===== */
@media (min-width: 480px) and (max-width: 767px) {
    aside.bg-white.shadow-lg {
        position: fixed !important;
        z-index: 9992 !important;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        width: var(--sidebar-width) !important;
        height: 100vh;
    }

    aside.bg-white.shadow-lg.mobile-open {
        transform: translateX(0);
    }

    .flex.h-screen>.flex-1 {
        width: 100vw !important;
        margin-left: 0 !important;
    }

    main.flex-1 {
        padding: 16px !important;
        padding-bottom: calc(var(--bottom-nav-height) + 20px + var(--safe-area-bottom)) !important;
    }

    #pwa-bottom-nav {
        display: block !important;
    }

    .grid.grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .pwa-fab {
        display: flex !important;
    }

    .fixed.right-0.top-0.h-full.w-96 {
        width: 100vw !important;
    }

    .pwa-desktop-only {
        display: none !important;
    }
}

/* ===== MEDIUM / TABLET (768px - 1023px) ===== */
@media (min-width: 768px) and (max-width: 1023px) {
    aside.bg-white.shadow-lg {
        width: var(--sidebar-collapsed) !important;
    }

    aside.bg-white.shadow-lg [x-show="sidebarOpen"] {
        display: none !important;
    }

    main.flex-1 {
        padding: 20px !important;
    }

    .grid.grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .grid.grid-cols-3 {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .pwa-mobile-only {
        display: none !important;
    }

    /* AI Chat panel smaller */
    .fixed.right-0.top-0.h-full.w-96 {
        width: 350px !important;
    }
}

/* ===== LARGE / DESKTOP (1024px - 1279px) ===== */
@media (min-width: 1024px) and (max-width: 1279px) {
    .pwa-mobile-only {
        display: none !important;
    }
}

/* ===== EXTRA LARGE (1280px+) ===== */
@media (min-width: 1280px) {
    .pwa-mobile-only {
        display: none !important;
    }
}

/* ─────────────────────────────────────────────
   LANDSCAPE MOBILE
   ───────────────────────────────────────────── */
@media (max-width: 767px) and (orientation: landscape) {
    #pwa-bottom-nav nav {
        height: 48px;
    }

    #pwa-bottom-nav a {
        flex-direction: row;
        gap: 4px;
    }

    #pwa-bottom-nav a i {
        margin-bottom: 0;
        font-size: 16px;
    }

    main.flex-1 {
        padding-bottom: calc(56px + var(--safe-area-bottom)) !important;
    }
}

/* ─────────────────────────────────────────────
   PRINT STYLES
   ───────────────────────────────────────────── */
@media print {

    aside,
    header,
    #pwa-bottom-nav,
    #pwa-install-banner-mobile,
    #pwa-offline-banner,
    #pwa-pull-refresh,
    .pwa-fab,
    #pwa-toast-container,
    #pwa-update-banner {
        display: none !important;
    }

    main.flex-1 {
        padding: 0 !important;
    }

    body {
        background: white !important;
    }
}

/* ─────────────────────────────────────────────
   DARK MODE SUPPORT (FUTURE)
   ───────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
    /* Reserved for dark mode */
}

/* ─────────────────────────────────────────────
   REDUCED MOTION
   ───────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ─────────────────────────────────────────────
   HIGH CONTRAST
   ───────────────────────────────────────────── */
@media (prefers-contrast: high) {
    .sidebar-link.active {
        outline: 2px solid white;
    }

    button,
    a {
        outline-offset: 2px;
    }
}

/* ─────────────────────────────────────────────
   MISC RESPONSIVE UTILITIES
   ───────────────────────────────────────────── */

/* Truncate text */
.pwa-truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pwa-truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Touch-friendly spacing */
.pwa-touch-target {
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Status bar colored */
.pwa-standalone header.bg-white.shadow-sm {
    padding-top: var(--safe-area-top);
}

/* Horizontal scroll container */
.pwa-horizontal-scroll {
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    gap: 12px;
    padding-bottom: 8px;
}

.pwa-horizontal-scroll>* {
    scroll-snap-align: start;
    flex-shrink: 0;
}

.pwa-horizontal-scroll::-webkit-scrollbar {
    display: none;
}

/* Sticky header for tables */
.pwa-sticky-header thead th {
    position: sticky;
    top: 0;
    background: white;
    z-index: 10;
    box-shadow: 0 1px 0 #E5E7EB;
}

/* Image responsive */
.pwa-img-responsive {
    width: 100%;
    height: auto;
    object-fit: cover;
    border-radius: 8px;
}

/* ─────────────────────────────────────────────
   WHATSAPP CHAT SPECIFIC MOBILE FIXES
   ───────────────────────────────────────────── */
@media (max-width: 768px) {

    /* Absolute full screen fit for Pixel 4 and similar */
    #whatsappChat,
    #whatsappChat * {
        box-sizing: border-box !important;
    }

    #whatsappChat {
        position: fixed !important;
        top: 64px !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        z-index: 40 !important;
        /* Calculate height precisely: 100dvh - TopBar(64) - BottomNav(64) - SafeAreas */
        height: calc(100dvh - 128px) !important;
        height: calc(100dvh - 128px - env(safe-area-inset-bottom, 0px)) !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
        border: none !important;
        background: white !important;
        transition: height 0.1s ease-out;
    }

    body.keyboard-active #whatsappChat {
        height: calc(100dvh - 64px) !important;
        height: calc(100dvh - 64px - env(safe-area-inset-bottom, 0px)) !important;
    }

    /* Force the conversation area to be the ONLY thing taking width when active */
    .conversation-area {
        width: 100vw !important;
        min-width: 100vw !important;
        max-width: 100vw !important;
        flex: 1 1 100% !important;
        height: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
    }

    .conversation-area>div {
        width: 100% !important;
        height: 100% !important;
    }

    .msg-bubble {
        max-width: 82% !important;
        word-wrap: break-word !important;
        font-size: 13px !important;
    }

    .conversation-area .px-6 {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }

    .conversation-area .py-4 {
        padding-top: 6px !important;
        padding-bottom: 6px !important;
    }

    div[x-ref="chatMessages"] {
        padding-left: 8px !important;
        padding-right: 8px !important;
        padding-top: 10px !important;
        overflow-x: hidden !important;
    }

    .conversation-area .bg-gray-50.border.rounded-2xl {
        margin: 0 !important;
        padding: 4px !important;
    }

    .conversation-area textarea {
        font-size: 14px !important;
        min-height: 40px !important;
    }

    .conversation-area .tracking-widest {
        display: none !important;
    }
}