/* ========================================
   AGRICONNECT - GLOBAL RESPONSIVE STYLES
   Using Bit Platform Design System
   ======================================== */

/* ========== CRITICAL: PREVENT HORIZONTAL SCROLL ========== */
html, body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

/* ========== CSS CUSTOM PROPERTIES ========== */
:root {
    /* Responsive Spacing Scale */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    --spacing-2xl: 48px;
    
    /* Dynamic spacing based on viewport */
    --container-padding: var(--spacing-md);
    --section-gap: var(--spacing-lg);
    --card-gap: var(--spacing-md);
    
    /* Typography scale */
    --text-xs: clamp(0.75rem, 2vw, 0.875rem);
    --text-sm: clamp(0.875rem, 2.5vw, 1rem);
    --text-base: clamp(1rem, 3vw, 1.125rem);
    --text-lg: clamp(1.125rem, 3.5vw, 1.25rem);
    --text-xl: clamp(1.25rem, 4vw, 1.5rem);
    --text-2xl: clamp(1.5rem, 5vw, 2rem);
    --text-3xl: clamp(2rem, 6vw, 3rem);
    
    /* Border radius */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
    
    /* Z-index scale */
    --z-dropdown: 1000;
    --z-sticky: 1020;
    --z-fixed: 1030;
    --z-modal-backdrop: 1040;
    --z-modal: 1050;
    --z-popover: 1060;
    --z-tooltip: 1070;
}

/* Tablet breakpoint adjustments */
@media (min-width: 600px) {
    :root {
        --container-padding: var(--spacing-lg);
        --section-gap: var(--spacing-xl);
        --card-gap: var(--spacing-lg);
    }
}

/* Desktop breakpoint adjustments */
@media (min-width: 960px) {
    :root {
        --container-padding: var(--spacing-xl);
        --section-gap: var(--spacing-2xl);
    }
}

/* ========== RESET & BASE STYLES ========== */

/* Box sizing border-box for all elements */
*, *::before, *::after {
    box-sizing: border-box;
}

/* Touch-friendly tap highlights */
* {
    -webkit-tap-highlight-color: rgba(22, 163, 74, 0.1); /* AgriConnect green */
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-size: 16px; /* Prevents iOS zoom on input focus */
}

body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    line-height: 1.6;
}

/* ========== RESPONSIVE TYPOGRAPHY ========== */

h1, .h1 { font-size: var(--text-3xl); font-weight: 700; }
h2, .h2 { font-size: var(--text-2xl); font-weight: 600; }
h3, .h3 { font-size: var(--text-xl); font-weight: 600; }
h4, .h4 { font-size: var(--text-lg); font-weight: 600; }
h5, .h5 { font-size: var(--text-base); font-weight: 500; }
h6, .h6 { font-size: var(--text-sm); font-weight: 500; }

p {
    font-size: var(--text-base);
    margin: 0 0 1rem;
}

.text-small {
    font-size: var(--text-sm);
}

.text-tiny {
    font-size: var(--text-xs);
}

/* ========== CONTAINER SYSTEM ========== */

.container {
    width: 100%;
    max-width: 1440px;
    margin: 0 auto;
    padding: 0 var(--container-padding);
}

.container-fluid {
    width: 100%;
    padding: 0 var(--container-padding);
}

.container-narrow {
    max-width: 960px;
}

/* ========== RESPONSIVE GRID SYSTEM ========== */

.grid-responsive {
    display: grid;
    gap: var(--card-gap);
    grid-template-columns: 1fr;
}

/* 2 columns on tablet */
@media (min-width: 600px) {
    .grid-responsive {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .grid-responsive-2 {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* 3-4 columns on desktop */
@media (min-width: 960px) {
    .grid-responsive {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .grid-responsive-4 {
        grid-template-columns: repeat(4, 1fr);
    }
}

@media (min-width: 1280px) {
    .grid-responsive {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* ========== TOUCH-FRIENDLY ELEMENTS ========== */

/* Minimum touch targets (44x44px for iOS, 48x48px for Android) */
/* Exclude BitToggle buttons which have their own sizing */
button:not(.bit-tgl-btn), a[role="button"], input[type="button"], input[type="submit"] {
    min-height: 44px;
    min-width: 44px;
    padding: 12px 20px;
    cursor: pointer;
}

@media (min-width: 600px) {
    button:not(.bit-tgl-btn), a[role="button"] {
        min-height: 40px;
    }
}

/* Form inputs - exclude checkbox/radio which have their own sizing */
input:not([type="checkbox"]):not([type="radio"]):not(.bit-tgl-inp), textarea, select {
    font-size: 16px; /* Prevents iOS zoom */
    min-height: 44px;
    padding: 12px 16px;
    border-radius: var(--radius-md);
}

/* ========== BIT TOGGLE FIX ========== */
/* Reset BitToggle to use its default styling */
.bit-tgl {
    min-height: unset !important;
    min-width: unset !important;
}

.bit-tgl-btn {
    min-height: unset !important;
    min-width: unset !important;
    padding: unset !important;
    border-radius: 9999px !important;
}

/* ========== RESPONSIVE IMAGES ========== */

img {
    max-width: 100%;
    height: auto;
    display: block;
}

.responsive-image {
    width: 100%;
    height: auto;
    object-fit: cover;
}

.responsive-image-contain {
    width: 100%;
    height: auto;
    object-fit: contain;
}

/* ========== VISIBILITY UTILITIES ========== */

/* Hide on specific breakpoints */
@media (max-width: 599px) {
    .hide-mobile { display: none !important; }
}

@media (min-width: 600px) and (max-width: 959px) {
    .hide-tablet { display: none !important; }
}

@media (min-width: 960px) {
    .hide-desktop { display: none !important; }
}

/* Show only on specific breakpoints */
.show-mobile { display: none !important; }
@media (max-width: 599px) {
    .show-mobile { display: block !important; }
}

.show-tablet { display: none !important; }
@media (min-width: 600px) and (max-width: 959px) {
    .show-tablet { display: block !important; }
}

.show-desktop { display: none !important; }
@media (min-width: 960px) {
    .show-desktop { display: block !important; }
}

/* ========== FLEXBOX UTILITIES ========== */

.flex-stack {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.flex-row {
    display: flex;
    flex-direction: row;
    gap: var(--spacing-md);
}

/* Stack horizontally on mobile, row on desktop */
.flex-stack-mobile {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

@media (min-width: 600px) {
    .flex-stack-mobile {
        flex-direction: row;
    }
}

/* ========== WIDTH UTILITIES ========== */

.full-width { width: 100% !important; }

@media (max-width: 599px) {
    .full-width-mobile { width: 100% !important; }
}

/* ========== SPACING UTILITIES ========== */

.spacing-xs { gap: var(--spacing-xs) !important; }
.spacing-sm { gap: var(--spacing-sm) !important; }
.spacing-md { gap: var(--spacing-md) !important; }
.spacing-lg { gap: var(--spacing-lg) !important; }
.spacing-xl { gap: var(--spacing-xl) !important; }

/* Padding utilities */
.p-0 { padding: 0 !important; }
.p-xs { padding: var(--spacing-xs) !important; }
.p-sm { padding: var(--spacing-sm) !important; }
.p-md { padding: var(--spacing-md) !important; }
.p-lg { padding: var(--spacing-lg) !important; }
.p-xl { padding: var(--spacing-xl) !important; }

/* Margin utilities */
.m-0 { margin: 0 !important; }
.m-xs { margin: var(--spacing-xs) !important; }
.m-sm { margin: var(--spacing-sm) !important; }
.m-md { margin: var(--spacing-md) !important; }
.m-lg { margin: var(--spacing-lg) !important; }
.m-xl { margin: var(--spacing-xl) !important; }

/* Margin bottom only */
.mb-0 { margin-bottom: 0 !important; }
.mb-sm { margin-bottom: var(--spacing-sm) !important; }
.mb-md { margin-bottom: var(--spacing-md) !important; }
.mb-lg { margin-bottom: var(--spacing-lg) !important; }

/* ========== CARD COMPONENTS ========== */

.card {
    background: var(--bit-color-background-primary, #ffffff);
    border: 1px solid var(--bit-color-border-primary, #e5e7eb);
    border-radius: var(--radius-lg);
    padding: var(--spacing-lg);
    box-shadow: var(--shadow-sm);
    transition: box-shadow 0.2s ease;
}

.card:hover {
    box-shadow: var(--shadow-md);
}

@media (max-width: 599px) {
    .card {
        padding: var(--spacing-md);
        border-radius: var(--radius-md);
    }
}

/* ========== MOBILE BOTTOM NAVIGATION ========== */

.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bit-color-background-primary);
    border-top: 1px solid var(--bit-color-border-primary);
    padding: 8px 0;
    z-index: var(--z-sticky);
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
}

@media (min-width: 960px) {
    .bottom-nav {
        display: none;
    }
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    min-width: 60px;
    color: var(--bit-color-foreground-secondary);
    text-decoration: none;
    font-size: 11px;
    transition: color 0.2s ease;
}

.bottom-nav-item:hover,
.bottom-nav-item.active {
    color: var(--bit-color-primary);
}

.bottom-nav-item i {
    font-size: 24px;
}

/* ========== SAFE AREA SUPPORT (iOS Notch) ========== */

@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    .bottom-nav {
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }
    
    .fixed-top {
        padding-top: max(0px, env(safe-area-inset-top));
    }
}

/* ========== LOADING STATES ========== */

.skeleton {
    background: linear-gradient(
        90deg,
        #f0f0f0 25%,
        #e0e0e0 50%,
        #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: var(--radius-md);
}

@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.spinner {
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--bit-color-primary);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* ========== SCROLLING OPTIMIZATION ========== */

.smooth-scroll {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

.hide-scrollbar::-webkit-scrollbar {
    display: none;
}

.hide-scrollbar {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

/* ========== PERFORMANCE OPTIMIZATIONS ========== */

/* Use hardware acceleration for animations */
.will-change-transform {
    will-change: transform;
}

.will-change-opacity {
    will-change: opacity;
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ========== PRINT STYLES ========== */

@media print {
    .no-print {
        display: none !important;
    }
    
    body {
        font-size: 12pt;
    }
    
    .container {
        max-width: 100%;
    }
}

/* ========== ACCESSIBILITY ========== */

/* Focus visible styles */
:focus-visible {
    outline: 2px solid var(--bit-color-primary);
    outline-offset: 2px;
}

/* Skip to main content link */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--bit-color-primary);
    color: white;
    padding: 8px;
    text-decoration: none;
    z-index: 9999;
}

.skip-link:focus {
    top: 0;
}

/* Screen reader only */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* ========== BIT PLATFORM OVERRIDES ========== */

/* Ensure Bit components are mobile-friendly */
bit-button {
    min-height: 44px !important;
}

bit-text-field input,
bit-number-field input,
bit-search-box input {
    font-size: 16px !important; /* Prevents iOS zoom */
}

@media (max-width: 599px) {
    /* Full width forms on mobile */
    bit-text-field,
    bit-number-field,
    bit-dropdown,
    bit-date-picker,
    bit-search-box {
        width: 100% !important;
    }
}

/* ========== UTILITY CLASSES ========== */

.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

.font-bold { font-weight: 700; }
.font-semibold { font-weight: 600; }
.font-medium { font-weight: 500; }
.font-normal { font-weight: 400; }

.truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* ========== MOBILE RESPONSIVE DATA GRID ========== */
/* Card-based mobile layout for data tables with expand/collapse */

.responsive-grid {
    width: 100%;
}

/* Desktop: Standard table layout */
@media (min-width: 769px) {
    .responsive-grid table {
        width: 100%;
        border-collapse: collapse;
    }

    .responsive-grid .mobile-card-view {
        display: none !important;
    }

    .responsive-grid .desktop-table-view {
        display: table !important;
    }

    .responsive-grid .expand-btn {
        display: none !important;
    }
}

/* Mobile: Card layout */
@media (max-width: 768px) {
    .responsive-grid .desktop-table-view {
        display: none !important;
    }

    .responsive-grid .mobile-card-view {
        display: block !important;
    }

    /* Mobile Card Container */
    .mobile-card-list {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    /* Individual Mobile Card */
    .mobile-data-card {
        background: white;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        overflow: hidden;
        border: 1px solid #e5e7eb;
    }

    .mobile-data-card.expanded {
        border-color: #22c55e;
        box-shadow: 0 4px 12px rgba(34, 197, 94, 0.15);
    }

    /* Card Header with Image and Primary Info */
    .mobile-card-header {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.875rem;
        cursor: pointer;
        transition: background-color 0.2s ease;
    }

    .mobile-card-header:hover {
        background: #f9fafb;
    }

    .mobile-card-header:active {
        background: #f3f4f6;
    }

    /* Card Image/Avatar */
    .mobile-card-image {
        width: 56px;
        height: 56px;
        border-radius: 8px;
        object-fit: cover;
        flex-shrink: 0;
        background: #f3f4f6;
    }

    .mobile-card-avatar {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        object-fit: cover;
        flex-shrink: 0;
        background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: 1rem;
    }

    /* Primary Info Section */
    .mobile-card-info {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
    }

    .mobile-card-title {
        font-weight: 600;
        font-size: 0.9375rem;
        color: #111827;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mobile-card-subtitle {
        font-size: 0.8125rem;
        color: #6b7280;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mobile-card-meta {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    /* Expand Button */
    .expand-btn {
        width: 36px;
        height: 36px;
        border-radius: 8px;
        background: #f3f4f6;
        border: none;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #6b7280;
        transition: all 0.2s ease;
        flex-shrink: 0;
        cursor: pointer;
        min-height: unset !important;
        min-width: unset !important;
        padding: 0 !important;
    }

    .expand-btn:hover {
        background: #e5e7eb;
        color: #374151;
    }

    .expand-btn .expand-icon {
        transition: transform 0.2s ease;
        font-size: 1rem;
    }

    .mobile-data-card.expanded .expand-btn {
        background: #dcfce7;
        color: #16a34a;
    }

    .mobile-data-card.expanded .expand-icon {
        transform: rotate(180deg);
    }

    /* Expandable Details Section */
    .mobile-card-details {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease, padding 0.3s ease;
        background: #f9fafb;
    }

    .mobile-data-card.expanded .mobile-card-details {
        max-height: 500px;
        padding: 0.875rem;
        border-top: 1px solid #e5e7eb;
    }

    /* Detail Rows */
    .detail-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem 0;
        border-bottom: 1px solid #e5e7eb;
    }

    .detail-row:last-child {
        border-bottom: none;
    }

    .detail-label {
        font-size: 0.8125rem;
        color: #6b7280;
        font-weight: 500;
    }

    .detail-value {
        font-size: 0.875rem;
        color: #111827;
        font-weight: 500;
        text-align: right;
    }

    /* Actions Row */
    .mobile-card-actions {
        display: flex;
        gap: 0.5rem;
        padding-top: 0.75rem;
        margin-top: 0.5rem;
        border-top: 1px solid #e5e7eb;
        flex-wrap: wrap;
    }

    .mobile-card-actions .bit-btn,
    .mobile-card-actions button {
        flex: 1;
        min-width: 80px;
        justify-content: center;
    }

    /* Status Badges in Mobile Cards */
    .mobile-status-badge {
        display: inline-flex;
        align-items: center;
        gap: 0.25rem;
        padding: 0.25rem 0.625rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 600;
    }

    .mobile-status-badge.success {
        background: #dcfce7;
        color: #16a34a;
    }

    .mobile-status-badge.warning {
        background: #fef3c7;
        color: #d97706;
    }

    .mobile-status-badge.error {
        background: #fee2e2;
        color: #dc2626;
    }

    .mobile-status-badge.info {
        background: #dbeafe;
        color: #2563eb;
    }

    .mobile-status-badge.neutral {
        background: #f3f4f6;
        color: #6b7280;
    }
}

/* ========== MOBILE FILTER BAR ========== */
@media (max-width: 768px) {
    .filter-bar,
    .filters-section {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .filter-bar > *,
    .filters-section .row > * {
        width: 100% !important;
    }

    .filter-bar .bit-txt-fld,
    .filter-bar .bit-drp,
    .filter-bar .bit-tgl {
        width: 100% !important;
    }
}
