/**
 * GreenWise Mobile Responsive Stylesheet
 * Auto-injected via global-header.js, also available for manual inclusion.
 * Breakpoint: 768px (tablet/phone), 480px (small phone)
 *
 * Provides:
 *   - Touch-friendly tap targets (44px min)
 *   - Responsive nav (hamburger menu via global-header.js)
 *   - Table horizontal scroll containers
 *   - Full-width buttons on small screens
 *   - Readable font sizes without zooming
 *   - Modal/dialog full-screen on mobile
 *   - Card layouts for data grids
 *   - Flexbox/grid responsive utilities
 */

/* ============================================================
   BASE MOBILE RESETS
   ============================================================ */
@media (max-width: 768px) {

    /* Prevent horizontal overflow globally */
    html {
        overflow-x: hidden;
    }

    body {
        padding: 0 !important;
        overflow-x: hidden;
        -webkit-text-size-adjust: 100%;
    }

    /* Reduce oversized max-width containers */
    .gh-header > div {
        padding-left: 0 !important;
        padding-right: 0 !important;
    }

    /* ============================================================
       GLOBAL HEADER - MOBILE HAMBURGER NAV
       ============================================================ */

    .gh-header {
        padding: 0.5rem 0.75rem !important;
        margin-bottom: 0 !important;
        border-radius: 0 !important;
    }

    /* Hide desktop nav on mobile - hamburger menu replaces it */
    .gh-header nav {
        display: none !important;
    }

    /* Show hamburger button on mobile */
    #mobileHamburgerBtn {
        display: flex !important;
        align-items: center;
        justify-content: center;
    }

    /* Hide search on mobile header (available in hamburger menu instead) */
    #headerSearchContainer {
        display: none !important;
    }

    /* Keep icon buttons smaller */
    .gh-icon-btn {
        width: 36px !important;
        height: 36px !important;
        padding: 0.3rem !important;
        font-size: 1rem !important;
    }

    /* Ensure logo is smaller */
    .gh-header img,
    .gh-logo img {
        height: 32px !important;
    }

    /* User info in header */
    .gh-user-name {
        font-size: 0.8rem !important;
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .gh-user-role {
        display: none !important;
    }

    /* User avatar smaller */
    #headerUserAvatar {
        width: 32px !important;
        height: 32px !important;
        font-size: 0.85rem !important;
    }

    /* Logout button hide on mobile (available in user dropdown) */
    .gh-logout-btn {
        display: none !important;
    }

    /* ============================================================
       HAMBURGER MENU STYLES
       ============================================================ */

    #mobileMenuOverlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 99998;
        display: none;
        opacity: 0;
        transition: opacity 0.25s ease;
    }

    #mobileMenuOverlay.active {
        display: block;
        opacity: 1;
    }

    #mobileMenuPanel {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 280px;
        max-width: 85vw;
        background: var(--bg-card-solid, #1f2937);
        z-index: 99999;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
        display: flex;
        flex-direction: column;
    }

    #mobileMenuPanel.active {
        transform: translateX(0);
    }

    .mobile-menu-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 1rem;
        border-bottom: 1px solid var(--border-primary, rgba(75, 85, 99, 0.3));
        flex-shrink: 0;
    }

    .mobile-menu-header img {
        height: 28px;
    }

    .mobile-menu-close {
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: none;
        border: 1px solid var(--border-primary, rgba(75, 85, 99, 0.3));
        border-radius: 8px;
        color: var(--text-muted, #9ca3af);
        font-size: 1.2rem;
        cursor: pointer;
    }

    .mobile-menu-search {
        padding: 0.75rem 1rem;
        border-bottom: 1px solid var(--border-primary, rgba(75, 85, 99, 0.3));
        flex-shrink: 0;
    }

    .mobile-menu-search input {
        width: 100%;
        padding: 0.6rem 0.75rem;
        border-radius: 8px;
        border: 1px solid var(--border-primary, rgba(75, 85, 99, 0.3));
        background: var(--bg-input, rgba(17, 24, 39, 0.5));
        color: var(--text-primary, #ffffff);
        font-size: 16px;
        min-height: 44px;
    }

    .mobile-menu-nav {
        flex: 1;
        overflow-y: auto;
        padding: 0.5rem 0;
    }

    .mobile-nav-item {
        display: flex;
        align-items: center;
        padding: 0.75rem 1.25rem;
        color: var(--text-secondary, #e5e7eb);
        text-decoration: none;
        font-size: 0.9rem;
        border-left: 3px solid transparent;
        transition: all 0.15s ease;
        min-height: 44px;
    }

    .mobile-nav-item:active,
    .mobile-nav-item:hover {
        background: var(--bg-hover, rgba(55, 65, 81, 0.5));
        border-left-color: var(--accent-green, #10b981);
        color: var(--accent-green-light, #4ade80);
    }

    .mobile-nav-item.active {
        background: rgba(74, 222, 128, 0.1);
        border-left-color: var(--accent-green-light, #4ade80);
        color: var(--accent-green-light, #4ade80);
        font-weight: 600;
    }

    .mobile-nav-group-label {
        padding: 0.6rem 1.25rem 0.25rem;
        font-size: 0.7rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-muted, #6b7280);
    }

    .mobile-nav-divider {
        height: 1px;
        background: var(--border-primary, rgba(75, 85, 99, 0.3));
        margin: 0.5rem 1rem;
    }

    .mobile-menu-footer {
        padding: 0.75rem 1rem;
        border-top: 1px solid var(--border-primary, rgba(75, 85, 99, 0.3));
        flex-shrink: 0;
    }

    .mobile-menu-footer a {
        display: block;
        padding: 0.6rem 0.75rem;
        color: var(--btn-danger-color, #ef4444);
        text-decoration: none;
        font-size: 0.9rem;
        text-align: center;
        border-radius: 8px;
        border: 1px solid var(--btn-danger-border, rgba(248, 113, 113, 0.3));
        min-height: 44px;
        line-height: 1.8;
    }

    /* Hamburger button */
    #mobileHamburgerBtn {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        background: rgba(255,255,255,0.18);
        border: 1px solid rgba(255,255,255,0.3);
        border-radius: 8px;
        color: white;
        cursor: pointer;
        flex-shrink: 0;
        padding: 0;
    }

    #mobileHamburgerBtn svg {
        width: 20px;
        height: 20px;
        stroke: white;
    }

    /* ============================================================
       TYPOGRAPHY - READABLE SIZES
       ============================================================ */

    h1, .page-title {
        font-size: 1.25rem !important;
    }

    h2 {
        font-size: 1.1rem !important;
    }

    h3 {
        font-size: 1rem !important;
    }

    /* ============================================================
       PAGE HEADERS & TOOLBARS
       ============================================================ */

    .page-header,
    .header:not(.gh-header),
    [class*="page-header"],
    [class*="toolbar"] {
        padding: 0.75rem !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
        border-radius: 0 !important;
    }

    .header-content,
    [class*="header-content"] {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.5rem !important;
    }

    .header-left,
    .header-right,
    [class*="header-left"],
    [class*="header-right"],
    [class*="header-actions"] {
        justify-content: center !important;
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
    }

    /* ============================================================
       TABLES - HORIZONTAL SCROLL
       ============================================================ */

    table {
        display: block;
        width: 100% !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        font-size: 0.8rem !important;
    }

    thead, tbody, tfoot {
        display: table;
        width: 100%;
    }

    table th,
    table td {
        padding: 0.5rem 0.6rem !important;
        white-space: nowrap;
    }

    /* Table container wrapper */
    .table-responsive,
    .table-container,
    [class*="table-wrap"] {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    /* ============================================================
       FORMS - TOUCH-FRIENDLY INPUTS
       ============================================================ */

    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    input[type="password"],
    input[type="search"],
    input[type="url"],
    textarea,
    select {
        min-height: 44px !important;
        font-size: 16px !important; /* Prevents iOS auto-zoom on focus */
        padding: 0.6rem 0.75rem !important;
    }

    label {
        font-size: 0.85rem !important;
        margin-bottom: 0.25rem;
    }

    /* Form groups stack vertically */
    .form-group,
    .form-row,
    [class*="form-group"],
    [class*="form-row"] {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }

    /* ============================================================
       BUTTONS - TOUCH-FRIENDLY
       ============================================================ */

    button:not(.gh-icon-btn):not(.mobile-menu-close):not(#mobileHamburgerBtn):not(.gw-ql-item):not(#gw-ql-btn):not(#calNavPrev):not(#calNavNext):not([style*="font-size: 1"]),
    .btn,
    input[type="submit"],
    input[type="button"],
    a.btn,
    a[class*="btn-"] {
        min-height: 44px;
        font-size: 0.85rem !important;
        padding: 0.6rem 1rem !important;
    }

    /* ============================================================
       MODALS / DIALOGS - FULL SCREEN ON MOBILE
       ============================================================ */

    /* My Calendar modal */
    #myCalendarModal > div {
        max-width: 100% !important;
        width: 100% !important;
        height: 100vh !important;
        border-radius: 0 !important;
    }

    /* Calendar week view: stack days vertically instead of 7-column grid */
    #calGrid > div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
        gap: 4px !important;
        min-height: auto !important;
    }

    /* Calendar month view: keep 7 columns but smaller */
    #calGrid > div > div[style*="grid-template-columns: repeat(7"] {
        font-size: 0.6rem !important;
    }

    /* Masquerade modal */
    #masqueradeModal > div {
        max-width: 100% !important;
        width: 100% !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
    }

    /* ============================================================
       CARDS & GRID LAYOUTS
       ============================================================ */

    /* Portal cards grid — 3 columns on wider mobile, 2 on narrow */
    .portal-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 0.6rem !important;
        padding: 0 0.75rem !important;
    }
    .portal-grid .portal-card {
        padding: 0.85rem 0.5rem !important;
        border-radius: 10px !important;
    }
    .portal-grid .portal-icon {
        width: 48px !important;
        height: 48px !important;
        font-size: 1.5rem !important;
        margin-bottom: 0.6rem !important;
    }
    .portal-grid .portal-card h3 {
        font-size: 0.72rem !important;
        margin: 0 0 0.25rem 0 !important;
    }
    .portal-grid .portal-card p {
        font-size: 0.65rem !important;
        display: -webkit-box !important;
        -webkit-line-clamp: 2 !important;
        -webkit-box-orient: vertical !important;
        overflow: hidden !important;
    }

    /* Other card grids */
    .cards-grid {
        grid-template-columns: 1fr !important;
        gap: 0.75rem !important;
        padding: 0 0.5rem !important;
    }

    /* Force single-column for grids with large minmax */
    [style*="grid-template-columns: repeat(auto-fit, minmax(3"],
    [style*="grid-template-columns: repeat(auto-fit, minmax(4"],
    [style*="grid-template-columns:repeat(auto-fit, minmax(3"],
    [style*="grid-template-columns:repeat(auto-fit, minmax(4"] {
        grid-template-columns: 1fr !important;
    }

    /* Two-column grids go single */
    [style*="grid-template-columns: repeat(2"],
    [style*="grid-template-columns:repeat(2"],
    [style*="grid-template-columns: repeat(3"],
    [style*="grid-template-columns:repeat(3"],
    [style*="grid-template-columns: repeat(4"],
    [style*="grid-template-columns:repeat(4"] {
        grid-template-columns: 1fr !important;
    }

    /* Card padding reduction */
    .card,
    [class*="card"]:not(#mobileMenuPanel):not(.gh-header) {
        border-radius: 10px !important;
    }

    /* ============================================================
       SIDEBAR / FILTER PANELS
       ============================================================ */

    .filter-bar,
    [class*="filter-bar"],
    [class*="filters-row"] {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }

    /* ============================================================
       KANBAN / BOARD VIEWS
       ============================================================ */

    /* Kanban columns: horizontal scroll instead of overflow hidden */
    .kanban-board,
    [class*="kanban-board"],
    .board-columns {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        padding-bottom: 0.5rem !important;
    }

    .kanban-column,
    [class*="kanban-col"] {
        min-width: 260px !important;
        flex-shrink: 0 !important;
    }

    /* ============================================================
       FLEX CONTAINERS - WRAP ON MOBILE
       ============================================================ */

    /* Stat rows / metric cards */
    .stats-row,
    .metrics-row,
    [class*="stats-row"],
    [class*="metrics-row"],
    [class*="stat-cards"] {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }

    /* ============================================================
       DROPDOWNS ON MOBILE
       ============================================================ */

    .gh-dropdown-menu {
        position: fixed !important;
        left: 0.75rem !important;
        right: 0.75rem !important;
        width: auto !important;
        max-height: 60vh !important;
        overflow-y: auto !important;
        border-radius: 12px !important;
    }

    .gh-dropdown-item {
        min-height: 44px !important;
        display: flex !important;
        align-items: center !important;
        padding: 0.75rem 1rem !important;
    }

    /* ============================================================
       SEARCH RESULTS PANEL
       ============================================================ */

    #headerSearchResults {
        left: 0.5rem !important;
        right: 0.5rem !important;
        top: auto !important;
        bottom: 0 !important;
        width: auto !important;
        max-height: 70vh !important;
        border-radius: 12px 12px 0 0 !important;
    }

    /* ============================================================
       SCROLLBAR WIDTHS
       ============================================================ */

    ::-webkit-scrollbar {
        width: 4px !important;
        height: 4px !important;
    }

    /* ============================================================
       QUICK LAUNCH FAB - adjust position for mobile
       ============================================================ */

    #gw-ql-btn {
        width: 48px !important;
        height: 48px !important;
        bottom: 16px !important;
        right: 16px !important;
        font-size: 1.2rem !important;
    }

    .gw-ql-item {
        width: 40px !important;
        height: 40px !important;
    }

    /* Assistant panel - full width on mobile */
    #assistantPanel {
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-height: 70vh !important;
        border-radius: 16px 16px 0 0 !important;
    }

    /* Email toast - full width on mobile */
    #emailToast {
        left: 12px !important;
        right: 12px !important;
        bottom: 72px !important;
        width: auto !important;
    }

    /* ============================================================
       TAB BARS / PILL NAVIGATION
       ============================================================ */

    .tab-bar,
    .tabs,
    [class*="tab-bar"],
    [class*="tabs-container"],
    [class*="division-tabs"] {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        gap: 0.25rem !important;
        padding-bottom: 0.25rem !important;
        scrollbar-width: none !important;
    }

    .tab-bar::-webkit-scrollbar,
    .tabs::-webkit-scrollbar,
    [class*="tab-bar"]::-webkit-scrollbar,
    [class*="tabs-container"]::-webkit-scrollbar {
        display: none;
    }

    /* ============================================================
       MASQUERADE BANNER
       ============================================================ */

    #masqueradeBanner {
        font-size: 0.8rem !important;
        padding: 0.5rem 0.75rem !important;
    }

    /* ============================================================
       INLINE STYLE OVERRIDES FOR COMMON PATTERNS
       ============================================================ */

    /* Fixed margins that cause overflow */
    [style*="margin: 1rem 2rem"],
    [style*="margin:1rem 2rem"] {
        margin-left: 0 !important;
        margin-right: 0 !important;
    }

    [style*="padding: 1rem 2rem"],
    [style*="padding:1rem 2rem"],
    [style*="padding: 1.5rem 2rem"],
    [style*="padding:1.5rem 2rem"],
    [style*="padding: 2rem"],
    [style*="padding:2rem"] {
        padding-left: 0.75rem !important;
        padding-right: 0.75rem !important;
    }

    /* Large fixed min-widths */
    [style*="min-width: 300px"],
    [style*="min-width:300px"],
    [style*="min-width: 350px"],
    [style*="min-width:350px"],
    [style*="min-width: 400px"],
    [style*="min-width:400px"],
    [style*="min-width: 450px"],
    [style*="min-width:450px"] {
        min-width: unset !important;
        width: 100% !important;
    }

    /* Fixed widths on search/filter inputs */
    [style*="width: 300px"],
    [style*="width:300px"],
    [style*="width: 400px"],
    [style*="width:400px"],
    [style*="width: 500px"],
    [style*="width:500px"] {
        width: 100% !important;
    }
}

/* ============================================================
   SMALL PHONE BREAKPOINT (below 480px)
   ============================================================ */
@media (max-width: 480px) {

    /* Portal drops to 2 columns on narrow phones */
    .portal-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    h1, .page-title {
        font-size: 1.1rem !important;
    }

    h2 {
        font-size: 1rem !important;
    }

    .page-header,
    .header:not(.gh-header) {
        padding: 0.5rem !important;
    }

    button:not(.gh-icon-btn):not(.mobile-menu-close):not(#mobileHamburgerBtn),
    .btn {
        font-size: 0.8rem !important;
    }

    table {
        font-size: 0.75rem !important;
    }

    table th,
    table td {
        padding: 0.35rem 0.4rem !important;
    }

    #mobileMenuPanel {
        width: 260px;
    }
}

/* ============================================================
   DESKTOP: HIDE MOBILE-ONLY ELEMENTS
   ============================================================ */
@media (min-width: 769px) {
    #mobileHamburgerBtn {
        display: none !important;
    }

    #mobileMenuOverlay,
    #mobileMenuPanel {
        display: none !important;
    }
}

/* ============================================================
   PRINT STYLES - Hide mobile nav on print
   ============================================================ */
@media print {
    #mobileMenuOverlay,
    #mobileMenuPanel,
    #mobileHamburgerBtn,
    #gw-ql-btn,
    .gw-ql-item,
    .gw-ql-tip,
    #emailToast,
    #assistantPanel {
        display: none !important;
    }
}
