/**
 * Responsive Design & Mobile Optimization
 * Makes the system work perfectly on smartphones and tablets
 */

/* ============================================
   MOBILE-FIRST BASE STYLES
   ============================================ */

/* Ensure proper viewport scaling */
html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
}

/* Touch-friendly tap targets */
button, a, input, select, textarea {
    min-height: 44px;
    min-width: 44px;
}

/* Prevent text overflow */
* {
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* ============================================
   MOBILE NAVIGATION (< 768px)
   ============================================ */

@media (max-width: 767px) {
    /* Sidebar becomes full-screen overlay */
    .sidebar {
        position: fixed;
        left: -280px;
        top: 0;
        width: 280px;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s ease;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    }
    
    .sidebar.active {
        left: 0;
    }
    
    /* Mobile header with hamburger */
    .mobile-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 1rem;
        background: white;
        border-bottom: 1px solid #e2e8f0;
        position: sticky;
        top: 0;
        z-index: 999;
    }
    
    .hamburger {
        display: flex;
        flex-direction: column;
        gap: 4px;
        cursor: pointer;
        padding: 8px;
    }
    
    .hamburger span {
        width: 24px;
        height: 3px;
        background: #1e293b;
        border-radius: 2px;
        transition: all 0.3s;
    }
    
    .hamburger.active span:nth-child(1) {
        transform: rotate(45deg) translate(5px, 5px);
    }
    
    .hamburger.active span:nth-child(2) {
        opacity: 0;
    }
    
    .hamburger.active span:nth-child(3) {
        transform: rotate(-45deg) translate(7px, -6px);
    }
    
    /* Overlay when sidebar is open */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }
    
    .sidebar-overlay.active {
        display: block;
    }
    
    /* Main content adjustments */
    .main-content {
        margin-left: 0;
        padding: 1rem;
    }
    
    /* Container adjustments */
    .container {
        padding: 0.5rem;
    }
    
    /* Page header */
    .page-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 1rem;
    }
    
    .page-header > div:last-child {
        width: 100%;
    }
    
    .page-header .btn {
        width: 100%;
        justify-content: center;
    }
}

/* ============================================
   TABLET (768px - 1024px)
   ============================================ */

@media (min-width: 768px) and (max-width: 1024px) {
    .sidebar {
        width: 220px;
    }
    
    .main-content {
        margin-left: 220px;
    }
    
    .container {
        padding: 1.5rem;
    }
}

/* ============================================
   RESPONSIVE TABLES
   ============================================ */

@media (max-width: 767px) {
    /* Stack table on mobile */
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Alternative: Card-style tables */
    table.mobile-cards {
        display: block;
    }
    
    table.mobile-cards thead {
        display: none;
    }
    
    table.mobile-cards tbody {
        display: block;
    }
    
    table.mobile-cards tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        padding: 1rem;
        background: white;
    }
    
    table.mobile-cards td {
        display: flex;
        justify-content: space-between;
        padding: 0.5rem 0;
        border: none;
    }
    
    table.mobile-cards td:before {
        content: attr(data-label);
        font-weight: 600;
        color: #64748b;
        margin-right: 1rem;
    }
}

/* ============================================
   RESPONSIVE FORMS
   ============================================ */

@media (max-width: 767px) {
    .form-row {
        flex-direction: column;
    }
    
    .form-group {
        width: 100% !important;
    }
    
    /* Larger touch targets for inputs */
    input, select, textarea {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.875rem;
    }
    
    /* Stack form buttons */
    .form-actions {
        flex-direction: column;
    }
    
    .form-actions .btn {
        width: 100%;
    }
}

/* ============================================
   RESPONSIVE CARDS & GRIDS
   ============================================ */

@media (max-width: 767px) {
    /* Single column for card grids */
    [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }
    
    /* KPI cards */
    .kpi-grid {
        grid-template-columns: 1fr !important;
    }
    
    /* Chart containers */
    .chart-container {
        height: 250px !important;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    /* Two columns for tablets */
    [style*="grid-template-columns: repeat(auto-fit"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

/* ============================================
   RESPONSIVE MODALS
   ============================================ */

@media (max-width: 767px) {
    .modal-content {
        width: 95% !important;
        max-width: 95% !important;
        margin: 1rem;
        max-height: 90vh;
    }
    
    .modal-body {
        max-height: calc(90vh - 140px);
        overflow-y: auto;
    }
    
    .modal-header h2 {
        font-size: 1.25rem;
    }
    
    .modal-footer {
        flex-direction: column;
    }
    
    .modal-footer .btn {
        width: 100%;
    }
}

/* ============================================
   RESPONSIVE BUTTONS
   ============================================ */

@media (max-width: 767px) {
    /* Stack button groups */
    .btn-group {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
        margin: 0.25rem 0;
    }
    
    /* Smaller button text on mobile */
    .btn {
        font-size: 0.875rem;
        padding: 0.75rem 1rem;
    }
    
    .btn-sm {
        font-size: 0.75rem;
        padding: 0.5rem 0.75rem;
    }
}

/* ============================================
   RESPONSIVE TYPOGRAPHY
   ============================================ */

@media (max-width: 767px) {
    h1 {
        font-size: 1.75rem;
    }
    
    h2 {
        font-size: 1.5rem;
    }
    
    h3 {
        font-size: 1.25rem;
    }
    
    h4 {
        font-size: 1.1rem;
    }
    
    p, li {
        font-size: 0.9rem;
    }
}

/* ============================================
   RESPONSIVE CHARTS
   ============================================ */

@media (max-width: 767px) {
    canvas {
        max-height: 250px !important;
    }
    
    .chart-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================
   RESPONSIVE FILTERS & SEARCH
   ============================================ */

@media (max-width: 767px) {
    .filter-bar {
        flex-direction: column;
    }
    
    .filter-bar > * {
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    .search-box {
        width: 100%;
    }
}

/* ============================================
   RESPONSIVE DATE RANGE FILTERS
   ============================================ */

@media (max-width: 767px) {
    /* Date range filter cards */
    .card form[style*="grid-template-columns"] {
        display: flex !important;
        flex-direction: column;
        gap: 1rem;
    }
    
    .card form > div {
        width: 100%;
    }
    
    .card form button {
        width: 100%;
    }
}

/* ============================================
   OFFLINE INDICATOR
   ============================================ */

.offline-indicator {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    background: #fbbf24;
    color: #92400e;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 9999;
    display: none;
    animation: slideIn 0.3s ease;
}

.offline-indicator.show {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

@keyframes slideIn {
    from {
        transform: translateY(100px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ============================================
   INSTALL PWA PROMPT
   ============================================ */

.install-prompt {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1rem;
    display: none;
    align-items: center;
    justify-content: space-between;
    z-index: 9998;
    box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.15);
}

.install-prompt.show {
    display: flex;
}

.install-prompt .btn {
    background: white;
    color: #667eea;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-weight: 600;
}

@media (max-width: 767px) {
    .install-prompt {
        flex-direction: column;
        text-align: center;
        gap: 1rem;
    }
    
    .install-prompt .btn {
        width: 100%;
    }
}

/* ============================================
   ACCESSIBILITY IMPROVEMENTS
   ============================================ */

/* Focus visible for keyboard navigation */
*:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* Skip to main content link */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: #3b82f6;
    color: white;
    padding: 0.5rem 1rem;
    text-decoration: none;
    z-index: 10000;
}

.skip-link:focus {
    top: 0;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    * {
        border-color: currentColor !important;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
    .sidebar,
    .mobile-header,
    .btn,
    .offline-indicator,
    .install-prompt {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0;
    }
    
    table {
        page-break-inside: avoid;
    }
    
    tr {
        page-break-inside: avoid;
    }
}
