/* ============================================
   File Management System - Mobile Responsive Fixes
   Fixes: sidebar on mobile, inconsistent page sizes,
   overflowing tables, inline-style grids, touch targets
   ============================================ */

/* -----------------------------------------------
   1. GLOBAL: Prevent any element from blowing out
      the viewport width on mobile
----------------------------------------------- */
html, body {
    overflow-x: hidden;
    width: 100%;
}

*, *::before, *::after {
    box-sizing: border-box;
    max-width: 100%;
}

img, video, iframe, embed, object {
    max-width: 100%;
    height: auto;
}

/* -----------------------------------------------
   2. LAYOUT: Ensure .content never causes overflow
----------------------------------------------- */
.content {
    min-width: 0;
    overflow-x: hidden;
}

.main-content {
    min-width: 0;
    overflow-x: hidden;
}

/* -----------------------------------------------
   3. TABLES: All tables must scroll, not overflow
----------------------------------------------- */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: block;
}

/* Reduce minimum table width on mobile so it doesn't
   force horizontal scroll of the entire page */
@media (max-width: 768px) {
    .table {
        min-width: 480px;
    }
}

@media (max-width: 480px) {
    .table {
        min-width: 420px;
    }

    /* Truncate very long text in table cells */
    .table td {
        max-width: 200px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* But allow action button cells to wrap freely */
    .table td:last-child {
        white-space: normal;
        max-width: none;
    }
}

/* -----------------------------------------------
   4. SIDEBAR: Ensure it is fully off-screen by
      default on mobile (some browsers ignore transform
      unless hardware acceleration is forced)
----------------------------------------------- */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        -webkit-transform: translateX(-100%);
        will-change: transform;
        /* Ensure it covers full height on iOS Safari */
        height: 100%;
        height: -webkit-fill-available;
    }

    .sidebar.active {
        transform: translateX(0);
        -webkit-transform: translateX(0);
    }

    /* Overlay must cover the ENTIRE screen (keep display:none from base CSS) */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        z-index: 998;
    }

    .sidebar-overlay.active {
        display: block;
    }

    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
}

/* -----------------------------------------------
   5. HEADER: Keep header items from overflowing
      on very small screens
----------------------------------------------- */
@media (max-width: 480px) {
    .header {
        padding: 8px 10px;
        gap: 8px;
    }

    .header-left {
        min-width: 0;
        flex: 1;
        display: flex;
        align-items: center;
        gap: 8px;
        overflow: hidden;
    }

    .header-title {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 14px;
        max-width: 160px;
    }

    .header-right {
        flex-shrink: 0;
    }
}

/* -----------------------------------------------
   6. INLINE GRID OVERRIDES: Pages with hardcoded
      inline display:grid styles that bypass CSS classes.
      These selectors target the actual inline style
      patterns found in dashboard.php, profile.php, etc.
----------------------------------------------- */

/* Catch any element using display:grid with inline style */
@media (max-width: 768px) {
    /* dashboard quick-actions flex wrap */
    .content [style*="display:flex"][style*="flex-wrap:wrap"],
    .content [style*="display: flex"][style*="flex-wrap: wrap"] {
        flex-direction: column;
    }

    /* Any grid with two equal columns */
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns:repeat(2"] {
        grid-template-columns: 1fr !important;
    }
}

@media (max-width: 480px) {
    /* Catch any remaining inline grids */
    [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }

    /* Quick action buttons in dashboard should stack */
    .content [style*="display:flex"][style*="flex-wrap:wrap"] a,
    .content [style*="display: flex"][style*="flex-wrap: wrap"] a {
        width: 100%;
        justify-content: center;
    }
}

/* -----------------------------------------------
   7. CARDS: Consistent sizing across all pages
----------------------------------------------- */
.card {
    width: 100%;
    min-width: 0;
    overflow: hidden;
}

/* Prevent card header from overflowing on narrow screens */
@media (max-width: 480px) {
    .card-header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .card-header h3,
    .card-header .card-title {
        font-size: 14px;
        min-width: 0;
        flex: 1;
    }

    /* Buttons inside card headers should never overflow */
    .card-header .btn {
        white-space: nowrap;
        flex-shrink: 0;
    }
}

/* -----------------------------------------------
   8. MODALS: Full-width on mobile, proper scroll
----------------------------------------------- */
@media (max-width: 768px) {
    .modal {
        padding: 0;
        align-items: flex-end; /* Sheet-style on mobile */
    }

    .modal-content {
        width: 100%;
        max-width: 100%;
        max-height: 90vh;
        border-radius: 16px 16px 0 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

@media (max-width: 480px) {
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 12px 15px;
    }

    .modal-header h3 {
        font-size: 15px;
    }
}

/* -----------------------------------------------
   9. FORMS: Full-width inputs, touch-friendly targets
----------------------------------------------- */
@media (max-width: 768px) {
    .form-control {
        width: 100%;
        font-size: 16px; /* Prevents iOS auto-zoom on focus */
        min-height: 44px; /* Touch target minimum */
    }

    select.form-control {
        min-height: 44px;
    }

    .form-group {
        margin-bottom: 12px;
    }

    /* Filter bar: stack vertically */
    .filter-bar,
    .filter-bar form,
    .search-filters {
        flex-direction: column !important;
        width: 100%;
    }

    .filter-bar .form-control,
    .filter-bar select,
    .filter-bar .btn {
        width: 100% !important;
        min-width: unset !important;
    }
}

/* -----------------------------------------------
   10. BUTTONS: Minimum touch target size
----------------------------------------------- */
@media (max-width: 768px) {
    .btn {
        min-height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .btn-sm {
        min-height: 34px;
        padding: 6px 10px;
    }

    /* Action buttons in table rows: always wrap */
    .action-btns {
        flex-wrap: wrap;
        gap: 4px;
    }

}

/* -----------------------------------------------
   11. PAGE-SIZE CONSISTENCY: Every page must have
       the same baseline padding and full-width content
----------------------------------------------- */

/* Ensure all pages using .content have uniform sizing */
.content {
    width: 100%;
    flex: 1;
}

/* Page title bar consistent on all pages */
.page-title {
    margin-bottom: 20px;
}

@media (max-width: 768px) {
    .page-title {
        margin-bottom: 15px;
    }
}

@media (max-width: 480px) {
    .page-title {
        margin-bottom: 10px;
    }

    .page-title h2 {
        font-size: 18px;
    }

    .page-title p {
        font-size: 12px;
    }

    /* Breadcrumbs */
    .breadcrumb {
        font-size: 11px;
        flex-wrap: wrap;
    }
}

/* -----------------------------------------------
   12. MESSAGES / CHAT: Fully responsive on mobile
----------------------------------------------- */
@media (max-width: 768px) {
    .chat-container {
        flex-direction: column;
        height: auto;
        min-height: 0;
    }

    .chat-sidebar {
        width: 100%;
        max-height: 180px;
        border-right: none;
        border-bottom: 1px solid #dee2e6;
        flex-shrink: 0;
        overflow-y: auto;
    }

    .chat-main {
        flex: 1;
        min-height: 350px;
        max-height: 60vh;
    }

    .chat-input-area {
        position: sticky;
        bottom: 0;
        background: white;
        padding: 8px;
        border-top: 1px solid #dee2e6;
    }

    .chat-input-area input[type="text"],
    .chat-input-area textarea {
        font-size: 16px; /* Prevent iOS zoom */
    }
}

/* -----------------------------------------------
   13. STATS GRID: Consistent across all pages
----------------------------------------------- */
@media (max-width: 576px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px;
    }
}

@media (max-width: 360px) {
    .stats-grid {
        grid-template-columns: 1fr !important;
    }
}

/* -----------------------------------------------
   14. PROFILE PAGE: Avatar and grid fixes
----------------------------------------------- */
@media (max-width: 768px) {
    .profile-avatar-section {
        text-align: center;
    }

    .grid-profile {
        grid-template-columns: 1fr !important;
    }
}

/* -----------------------------------------------
   15. LOGIN PAGE: Proper mobile padding
----------------------------------------------- */
@media (max-width: 480px) {
    .login-card {
        padding: 24px 16px;
        border-radius: 8px;
    }

    .login-page {
        padding: 16px;
        align-items: flex-start;
        padding-top: 40px;
    }
}

/* -----------------------------------------------
   16. FOOTER: No overflow at bottom of pages
----------------------------------------------- */
@media (max-width: 480px) {
    .footer {
        padding: 10px 15px;
        font-size: 11px;
    }

    .footer-content {
        flex-direction: column;
        gap: 4px;
        text-align: center;
    }
}

/* -----------------------------------------------
   17. SCROLLBAR: Custom thin scrollbar for tables
       on mobile to show scroll affordance
----------------------------------------------- */
.table-responsive::-webkit-scrollbar {
    height: 4px;
}

.table-responsive::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 2px;
}

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

/* -----------------------------------------------
   18. FIX: white-space:nowrap inside flex items
       that causes overflow (seen in dashboard activity)
----------------------------------------------- */
@media (max-width: 768px) {
    [style*="white-space:nowrap"],
    [style*="white-space: nowrap"] {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: unset !important;
    }
}

/* -----------------------------------------------
   19. FILE/ARCHIVE NAME: Truncate long names in
       tables with full name shown on hover (title)
----------------------------------------------- */
.file-name-cell {
    display: inline-block;
    max-width: 220px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: middle;
    cursor: default;
}

@media (max-width: 1200px) {
    .file-name-cell { max-width: 180px; }
}

@media (max-width: 992px) {
    .file-name-cell { max-width: 150px; }
}

@media (max-width: 768px) {
    .file-name-cell { max-width: 130px; }
}

/* -----------------------------------------------
   20. DESCRIPTION FIELDS: Long descriptions must
       wrap to multiple lines, never force horizontal
       scroll. Applies to all <td> and display cells.
----------------------------------------------- */
td, .table td {
    white-space: normal !important;
    word-break: break-word;
    overflow-wrap: break-word;
}

/* Description column specifically */
.desc-cell {
    white-space: normal !important;
    word-break: break-word;
    max-width: 300px;
    line-height: 1.4;
}

/* Any element with inline white-space:nowrap that is NOT
   inside an action button area should be overridden */
.card-body [style*="white-space:nowrap"],
.card-body [style*="white-space: nowrap"],
.content [style*="white-space:nowrap"],
.content [style*="white-space: nowrap"] {
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: unset !important;
}

/* Textarea description fields */
textarea.form-control {
    white-space: pre-wrap;
    word-wrap: break-word;
    resize: vertical;
    min-height: 80px;
}

/* -----------------------------------------------
   21. GLOBAL TABLE CELL: Prevent any cell content
       from forcing horizontal overflow on the page
----------------------------------------------- */
.table td, .table th {
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* But never clip action buttons */
.table td:last-child,
.table th:last-child {
    max-width: none;
    overflow: visible;
    white-space: nowrap;
}
