/* ========================================
   SGT FLOW - RESPONSIVE STYLES
   Mobile-First Fluid Design
   ======================================== */

/* ========================================
   CORE MOBILE SIDEBAR TOGGLE
   ======================================== */

/* Default state: sidebar hidden on mobile */
@media (max-width: 767.98px) {
    #sidebar-wrapper {
        width: 260px;
        margin-left: -260px;
        position: fixed !important;
        top: 0;
        left: 0;
        height: 100vh;
        z-index: 1050;
        transition: margin 0.35s ease-in-out, transform 0.35s ease-in-out;
    }
    
    #sidebar-wrapper .list-group {
        width: 260px;
    }
    
    /* When toggled open on mobile */
    body.sb-sidenav-toggled #sidebar-wrapper {
        margin-left: 0;
    }
    
    /* Page content takes full width on mobile */
    #page-content-wrapper {
        min-width: 100vw;
        width: 100%;
        margin-left: 0 !important;
    }
    
    /* Overlay when sidebar is open - sits below sidebar but above content */
    body.sb-sidenav-toggled::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1045;
        opacity: 1;
        transition: opacity 0.35s ease;
        cursor: pointer;
    }
}

/* Desktop: sidebar visible by default */
@media (min-width: 768px) {
    #sidebar-wrapper {
        margin-left: 0;
        position: relative;
    }
    
    #page-content-wrapper {
        min-width: 0;
        width: 100%;
    }
    
    /* When toggled closed on desktop */
    body.sb-sidenav-toggled #sidebar-wrapper {
        margin-left: -260px;
    }
}

/* ========================================
   MOBILE NAVIGATION (< 768px)
   ======================================== */
@media (max-width: 767.98px) {
    .navbar {
        padding: 0.5rem 1rem;
        position: sticky;
        top: 0;
        z-index: 1030;
    }
    
    .navbar-brand {
        font-size: 1.2rem;
    }
    
    /* Sidebar toggle button */
    #sidebarToggle {
        padding: 0.5rem;
        margin-right: 0.5rem;
    }
    
    .card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    .card-body {
        padding: 1rem;
    }
    
    .content-container {
        padding: 15px;
    }
    
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .table th, .table td {
        padding: 0.5rem;
        font-size: 0.875rem;
    }
    
    .status-badge {
        display: inline-block;
        margin-bottom: 0.25rem;
        white-space: nowrap;
        font-size: 0.7rem;
        padding: 0.25rem 0.5rem;
    }
    
    .form-control, .form-select {
        font-size: 16px; /* Prevents zoom on iOS */
    }
    
    .btn {
        padding: 0.5rem 0.75rem;
        min-height: 44px;
        font-size: 0.9rem;
    }
    
    .btn-sm {
        min-height: 36px;
        padding: 0.375rem 0.625rem;
    }
    
    .dashboard-card {
        height: auto !important;
        margin-bottom: 1rem;
    }
    
    .dashboard-card:hover {
        transform: translateY(-4px);
    }
    
    .icon-box {
        width: 56px;
        height: 56px;
        padding: 12px;
    }
    
    .request-list .request-item {
        padding: 0.75rem;
    }
    
    .request-list .request-item:hover {
        transform: translateX(2px) translateY(-1px);
    }
    
    .message-list {
        height: 300px;
    }
    
    .message-item {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
    }
    
    .message-sent {
        margin-left: 1rem;
    }
    
    .message-received {
        margin-right: 1rem;
    }
    
    .site-title {
        font-size: 1.5rem;
    }
    
    /* User dropdown on mobile */
    #userDropdown {
        padding: 0.5rem;
        font-size: 0.85rem;
    }
    
    #userDropdown .badge {
        display: none;
    }
    
    /* Hide username text on very small screens, show only icon */
    #userDropdown span:not(.badge) {
        display: none;
    }
    
    /* Dropdown menus on mobile - bottom sheet style */
    .dropdown-menu {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: 16px 16px 0 0 !important;
        max-height: 70vh;
        overflow-y: auto;
        transform: none !important;
        animation: mobileSlideUp 0.25s ease-out !important;
        z-index: 1060 !important;
    }
    
    .notification-dropdown {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 16px 16px 0 0 !important;
        max-height: 70vh;
        overflow-y: auto;
        transform: none !important;
        animation: mobileSlideUp 0.25s ease-out !important;
        z-index: 1060 !important;
    }

    @keyframes mobileSlideUp {
        from {
            opacity: 0;
            transform: translateY(100%) !important;
        }
        to {
            opacity: 1;
            transform: translateY(0) !important;
        }
    }

    /* Mobile dropdown backdrop overlay */
    .dropdown.show::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1055;
        animation: fadeIn 0.2s ease-out;
    }
    .notification-menu.show::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1055;
        animation: fadeIn 0.2s ease-out;
    }
    @keyframes fadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }
}

/* ========================================
   SMALL PHONES (< 576px)
   ======================================== */
@media (max-width: 575.98px) {
    .card-body {
        padding: 0.75rem;
    }
    
    .content-container {
        padding: 10px;
    }
    
    .btn-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .btn-group .btn {
        border-radius: 10px !important;
        margin: 0;
    }
    
    .modal-dialog {
        margin: 0.5rem;
    }
    
    .modal-content {
        border-radius: 16px;
    }
    
    .table {
        font-size: 0.8rem;
    }
    
    .table td {
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    .display-5 {
        font-size: 1.75rem;
    }
    
    .display-6 {
        font-size: 1.25rem;
    }
    
    .status-count {
        font-size: 1.5rem;
    }
    
    h1, .h1 {
        font-size: 1.5rem;
    }
    
    h2, .h2 {
        font-size: 1.25rem;
    }
    
    h3, .h3 {
        font-size: 1.1rem;
    }
    
    /* Don't stack all flex containers */
    .d-flex.justify-content-between.align-items-center.mb-4 {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .d-flex.justify-content-between.align-items-center.mb-4 h1 {
        flex: 1;
        min-width: 150px;
    }
    
    .d-flex.justify-content-between.align-items-center.mb-4 .btn {
        width: auto;
    }
}

/* ========================================
   TABLETS (768px - 991px)
   ======================================== */
@media (min-width: 768px) and (max-width: 991.98px) {
    .col-md-4 {
        width: 50%;
    }
    
    .dashboard-card {
        height: 100%;
    }
    
    .table th, .table td {
        padding: 0.75rem;
    }
    
    .icon-box {
        width: 64px;
        height: 64px;
    }
    
    #userDropdown span:not(.badge) {
        display: inline;
    }
}

/* ========================================
   FORM RESPONSIVENESS
   ======================================== */
@media (max-width: 767.98px) {
    .row.form-row > [class*="col-"] {
        margin-bottom: 1rem;
    }
    
    input[type="date"].form-control {
        min-height: 44px;
    }
    
    .custom-file-label {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .filter-form {
        padding: 15px;
    }
    
    .filter-form .row > div {
        margin-bottom: 0.75rem;
    }
    
    /* Form rows on mobile */
    .row {
        margin-left: -8px;
        margin-right: -8px;
    }
    
    .row > [class*="col-"] {
        padding-left: 8px;
        padding-right: 8px;
    }
}

/* ========================================
   PRINT STYLES
   ======================================== */
@media print {
    #sidebar-wrapper,
    .navbar,
    .no-print,
    .btn,
    .notification-popup {
        display: none !important;
    }
    
    #page-content-wrapper {
        margin-left: 0 !important;
        width: 100% !important;
    }
    
    .card {
        break-inside: avoid;
        border: 1px solid #ddd;
        box-shadow: none !important;
        border-radius: 0;
    }
    
    body {
        background-color: white !important;
        color: black !important;
    }
    
    .table {
        border-collapse: collapse;
    }
    
    .table th,
    .table td {
        border: 1px solid #ddd;
        padding: 8px;
    }
}

/* ========================================
   ACCESSIBILITY - FOCUS STATES
   ======================================== */
:focus {
    outline: 3px solid rgba(46, 196, 182, 0.5) !important;
    outline-offset: 2px !important;
}

/* Remove ALL focus effects from navbar */
.navbar :focus,
.navbar :focus-visible,
.navbar :focus-within,
.navbar .btn:focus,
.navbar .btn-link:focus,
.navbar .dropdown:focus,
.navbar .dropdown:focus-within,
.navbar .dropdown-toggle:focus,
.navbar .dropdown-menu:focus,
.navbar .dropdown-menu:focus-within,
#notificationDropdown:focus,
#notificationDropdown:focus-visible,
#theme-toggle:focus,
#theme-toggle:focus-visible,
#userDropdown:focus,
#userDropdown:focus-visible,
#sidebarToggle:focus,
#sidebarToggle:focus-visible {
    outline: none !important;
    outline-offset: 0 !important;
    box-shadow: none !important;
}

.btn:focus,
.form-control:focus,
.form-select:focus {
    box-shadow: 0 0 0 4px rgba(46, 196, 182, 0.25) !important;
}

/* Override: NO focus for navbar buttons */
.navbar .btn:focus {
    box-shadow: none !important;
}

/* Skip to content link */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--accent-color);
    color: white;
    padding: 8px 16px;
    z-index: 10000;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

/* ========================================
   HIGH CONTRAST MODE
   ======================================== */
@media (prefers-contrast: more) {
    :root {
        --accent-color: #00a896 !important;
        --secondary-accent: #b8c900 !important;
    }
    
    body.light-theme {
        --background-color: #ffffff !important;
        --text-color: #000000 !important;
        --border-color: #000000 !important;
    }
    
    body.dark-theme {
        --background-color: #000000 !important;
        --text-color: #ffffff !important;
        --border-color: #ffffff !important;
    }
    
    .btn,
    .card,
    .form-control,
    .status-badge {
        border-width: 2px !important;
    }
}

/* ========================================
   REDUCED MOTION PREFERENCE
   ======================================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .card:hover,
    .btn:hover,
    .dashboard-card:hover,
    .request-list .request-item:hover {
        transform: none !important;
    }
}

/* ========================================
   LANDSCAPE PHONE ORIENTATION
   ======================================== */
@media (max-height: 500px) and (orientation: landscape) {
    .content-container {
        padding: 10px;
    }
    
    .card-body {
        padding: 0.75rem;
    }
    
    .modal-body {
        max-height: 60vh;
        overflow-y: auto;
    }
    
    #sidebar-wrapper {
        overflow-y: auto;
    }
}

/* ========================================
   LARGE SCREENS (> 1400px)
   ======================================== */
@media (min-width: 1400px) {
    .container-fluid {
        max-width: 1600px;
        margin: 0 auto;
    }
    
    .content-container {
        padding: 30px 40px;
    }
    
    .dashboard-card {
        padding: 1.5rem;
    }
}

/* ========================================
   TOUCH DEVICE OPTIMIZATIONS
   ======================================== */
@media (hover: none) and (pointer: coarse) {
    .btn {
        min-height: 48px;
    }
    
    .dropdown-item {
        padding: 12px 16px;
    }
    
    #sidebar-wrapper .list-group-item {
        padding: 16px 20px;
    }
    
    /* Simpler hover effects on touch */
    .card:hover {
        transform: none;
    }
    
    .dashboard-card:hover {
        transform: none;
    }
    
    .request-list .request-item:hover {
        transform: none;
    }
    
    #sidebar-wrapper .list-group-item:hover {
        padding-left: 1.25rem;
    }
    
    /* Active state for touch */
    .btn:active {
        transform: scale(0.98);
    }
    
    .card:active {
        transform: scale(0.99);
    }
    
    #sidebar-wrapper .list-group-item:active {
        background-color: rgba(46, 196, 182, 0.2);
    }
}

/* ========================================
   SAFE AREA INSETS (NOTCH DEVICES)
   ======================================== */
@supports (padding: max(0px)) {
    .navbar {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
    
    .content-container {
        padding-left: max(15px, env(safe-area-inset-left));
        padding-right: max(15px, env(safe-area-inset-right));
        padding-bottom: max(15px, env(safe-area-inset-bottom));
    }
    
    #sidebar-wrapper {
        padding-bottom: env(safe-area-inset-bottom);
        padding-left: env(safe-area-inset-left);
    }
    
    @media (max-width: 767.98px) {
        .dropdown-menu {
            padding-bottom: max(16px, env(safe-area-inset-bottom));
        }
    }
}

/* ========================================
   WRAPPER FLEX LAYOUT FIX
   ======================================== */
#wrapper {
    display: flex;
    width: 100%;
    overflow-x: hidden;
}

@media (max-width: 767.98px) {
    #wrapper {
        flex-direction: column;
    }
}
/* Additional Mobile Fixes */
@media (max-width: 767.98px) {
    .table-responsive {
        -webkit-overflow-scrolling: touch;
    }
    
    .d-flex.justify-content-between.align-items-center.mb-4 {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 1rem;
    }
    
    .d-flex.justify-content-between.align-items-center.mb-4 > div {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .form-control, .form-select {
        font-size: 16px !important;
    }
}