/* ============================================
   RESPONSIVE OVERRIDES - Mobile & Tablet
   Covers ALL tabs: Dashboard, Vehicles, Children,
   Home, Admin, Nanny, Projects, Menus, Bank Analysis
   ============================================ */

/* ---- Viewport meta safety ---- */
html {
    -webkit-text-size-adjust: 100%;
    overflow-x: hidden;
}

body {
    overflow-x: hidden;
}

/* ==============================================
   TABLET  (769px – 1024px)
   ============================================== */
@media (max-width: 1024px) {

    /* --- Menu calendar: 7 cols → scrollable or smaller --- */
    .menu-week-grid {
        grid-template-columns: repeat(7, minmax(120px, 1fr)) !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* --- Children: growth charts side by side but smaller --- */
    .child-charts-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* --- Admin entity detail: 2 cols → 1 col --- */
    .admin-entity-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* --- Inline grids: prevent overflow --- */
    [style*="grid-template-columns: repeat(auto-fill, minmax(300px"] {
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)) !important;
    }

    /* --- Page header button groups: wrap --- */
    .page-header > div[style*="display: flex"] {
        flex-wrap: wrap !important;
    }

    .page-header > div[style*="display: flex"] .btn {
        font-size: 0.8125rem;
        padding: 0.5rem 0.875rem;
    }

    /* --- Home sub-nav: scrollable --- */
    .sub-nav {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }

    .sub-nav::-webkit-scrollbar {
        display: none;
    }

    .sub-nav-item {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* --- Admin sub-tabs: scrollable --- */
    div[style*="border-bottom: 2px solid var(--border)"][style*="padding-bottom: 0"] {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        scrollbar-width: none;
    }

    div[style*="border-bottom: 2px solid var(--border)"][style*="padding-bottom: 0"]::-webkit-scrollbar {
        display: none;
    }

    .admin-sub-tab {
        white-space: nowrap;
        flex-shrink: 0;
    }
}

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

    /* ---- Global: prevent horizontal scroll ---- */
    .main-content {
        overflow-x: hidden;
        padding: 1rem !important;
        padding-top: 3.5rem !important;
    }

    /* ---- Page header: stack title + buttons ---- */
    .page-header {
        flex-direction: column !important;
        gap: 1rem !important;
        align-items: stretch !important;
        padding: 1rem !important;
    }

    .page-header > div:last-child {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .page-header > div[style*="display: flex"] {
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
    }

    .page-header > div[style*="display: flex"] > .btn {
        flex: 1 1 auto;
        min-width: 0;
        font-size: 0.75rem !important;
        padding: 0.5rem 0.625rem !important;
        white-space: nowrap;
    }

    .page-header .btn svg {
        width: 16px;
        height: 16px;
        flex-shrink: 0;
    }

    /* ---- All inline grids: single column ---- */
    [style*="grid-template-columns: repeat(auto-fill, minmax(300px"] {
        grid-template-columns: 1fr !important;
    }

    [style*="grid-template-columns: repeat(auto-fill, minmax(200px"] {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)) !important;
    }

    [style*="grid-template-columns: repeat(auto-fit, minmax(200px"] {
        grid-template-columns: 1fr 1fr !important;
    }

    /* ---- MENUS TAB ---- */

    /* Calendar: 7 cols → 1 col vertical list */
    .menu-week-grid {
        grid-template-columns: 1fr !important;
        gap: 0.75rem !important;
        overflow-x: visible !important;
    }

    /* Day columns: reduce min-height */
    .menu-day-column {
        min-height: auto !important;
    }

    /* Week navigation: stack */
    .menu-week-nav {
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
        justify-content: center !important;
    }

    /* Meal modal: recipe form grid → 1 col */
    [style*="grid-template-columns: 1fr auto"][style*="gap: 1rem"] {
        grid-template-columns: 1fr !important;
    }

    /* Meal modal tabs */
    .meal-modal-tab {
        font-size: 0.8125rem !important;
        padding: 0.5rem 0.75rem !important;
    }

    /* ---- CHILDREN TAB ---- */

    /* Growth charts: 2 cols → 1 col */
    .child-charts-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* Detail blocks */
    .detail-blocks {
        grid-template-columns: 1fr !important;
    }

    /* ---- ADMIN TAB ---- */

    /* Entity detail: 2 cols → 1 col */
    .admin-entity-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* Admin sub-tabs: horizontal scroll */
    div[style*="border-bottom: 2px solid var(--border)"][style*="padding-bottom: 0"] {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        gap: 0 !important;
        scrollbar-width: none;
    }

    div[style*="border-bottom: 2px solid var(--border)"][style*="padding-bottom: 0"]::-webkit-scrollbar {
        display: none;
    }

    .admin-sub-tab {
        white-space: nowrap !important;
        flex-shrink: 0 !important;
        font-size: 0.8125rem !important;
        padding: 0.625rem 0.875rem !important;
    }

    /* Document/object search: full width */
    div[style*="max-width: 500px"] {
        max-width: 100% !important;
    }

    /* Section headers: stack */
    .home-section > div[style*="display: flex"][style*="justify-content: space-between"] {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem !important;
    }

    /* ---- HOME TAB ---- */

    /* Sub-nav: horizontal scroll */
    .sub-nav {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        scrollbar-width: none;
        gap: 0.25rem !important;
    }

    .sub-nav::-webkit-scrollbar {
        display: none;
    }

    .sub-nav-item {
        white-space: nowrap;
        flex-shrink: 0;
        font-size: 0.8125rem;
        padding: 0.5rem 0.75rem;
    }

    /* Home section headers */
    #home-tab .home-section > div:first-child[style*="display: flex"] {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem !important;
    }

    /* Home card details */
    .home-card-details {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.75rem !important;
    }

    .home-card-content {
        padding: 1rem;
    }

    /* Item cards grid */
    #equipments-list,
    #maintenances-list,
    #works-list {
        display: flex !important;
        flex-direction: column !important;
        gap: 0.75rem !important;
    }

    /* ---- NANNY TAB ---- */

    /* Layout already stacks at 968px via CSS, ensure calendar fits */
    .nanny-layout {
        grid-template-columns: 1fr !important;
    }

    .nanny-right-column {
        position: static !important;
    }

    /* Nanny calendar: reduce cell height and font size for tablet */
    .calendar-day {
        min-height: 100px !important;
        font-size: 0.7rem !important;
    }

    /* Smaller event text on tablet */
    .calendar-day > div > div {
        font-size: 0.65rem !important;
    }

    /* Nanny calendar: make table cells smaller */
    #nanny-calendar table {
        font-size: 0.75rem;
    }

    #nanny-calendar th,
    #nanny-calendar td {
        padding: 0.25rem !important;
    }

    /* ---- BANK ANALYSIS TAB ---- */

    /* Sub tabs: scrollable */
    .sub-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }

    .sub-tabs::-webkit-scrollbar {
        display: none;
    }

    .sub-tab {
        white-space: nowrap;
        flex-shrink: 0;
        font-size: 0.8125rem;
        padding: 0.625rem 1rem;
    }

    /* Upload zone: smaller padding */
    .upload-zone {
        padding: 1.5rem;
    }

    /* ---- PROJECTS TAB ---- */
    #projects-grid {
        grid-template-columns: 1fr !important;
    }

    /* ---- VEHICLES TAB ---- */
    #vehicles-grid {
        grid-template-columns: 1fr !important;
    }

    /* ---- MODAL ---- */
    .modal {
        width: 95% !important;
        max-width: none !important;
        margin: 0.5rem !important;
        max-height: 90vh !important;
    }

    .modal-body {
        padding: 1rem !important;
    }

    /* Modal form rows */
    .modal-body .form-row {
        grid-template-columns: 1fr !important;
    }

    /* Recipe form: stack grids on mobile */
    .recipe-form-row {
        grid-template-columns: 1fr !important;
    }

    .recipe-form-row-3 {
        grid-template-columns: 1fr 1fr 1fr !important;
    }

    /* Modal inline grids fallback */
    .modal-body [style*="grid-template-columns: repeat(3"] {
        grid-template-columns: 1fr !important;
    }

    .modal-body [style*="grid-template-columns: repeat(2"] {
        grid-template-columns: 1fr !important;
    }

    /* Import recipe modal: site badges */
    .modal-body [style*="flex-wrap: wrap"][style*="justify-content: center"] {
        gap: 0.375rem !important;
    }

    /* ---- DASHBOARD TAB ---- */
    .dashboard-grid {
        grid-template-columns: 1fr !important;
    }

    /* ---- Tables: horizontal scroll ---- */
    .table-container,
    div:has(> .table) {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ---- Inline flex section headers (used across Home, Admin, Menus) ---- */
    div[style*="display: flex"][style*="justify-content: space-between"][style*="align-items: center"][style*="margin-bottom"] {
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
    }
}

/* ==============================================
   SMALL MOBILE  (max-width: 480px)
   ============================================== */
@media (max-width: 480px) {

    .main-content {
        padding: 0.75rem !important;
        padding-top: 3.5rem !important;
    }

    /* ---- Page header: smaller text ---- */
    .page-title {
        font-size: 1.125rem !important;
    }

    .page-subtitle {
        font-size: 0.75rem !important;
    }

    /* ---- Buttons: compact ---- */
    .page-header > div[style*="display: flex"] > .btn {
        font-size: 0.6875rem !important;
        padding: 0.375rem 0.5rem !important;
        gap: 0.25rem !important;
    }

    .page-header > div[style*="display: flex"] > .btn svg {
        width: 14px !important;
        height: 14px !important;
    }

    /* Button text: hide on very small screens, show icon only */
    .page-header .btn {
        width: auto !important;
    }

    /* ---- Menu calendar days ---- */
    .menu-day-column {
        min-height: auto !important;
    }

    /* ---- Inline grids: ensure single col ---- */
    [style*="grid-template-columns: repeat(auto-fill, minmax(200px"] {
        grid-template-columns: 1fr 1fr !important;
        gap: 0.5rem !important;
    }

    [style*="grid-template-columns: repeat(auto-fit, minmax(200px"] {
        grid-template-columns: 1fr !important;
    }

    [style*="grid-template-columns: repeat(auto-fit, minmax(240px"] {
        grid-template-columns: 1fr !important;
    }

    [style*="grid-template-columns: repeat(auto-fit, minmax(140px"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* ---- Modal: full screen on small devices ---- */
    .modal {
        width: 100% !important;
        height: 100% !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
        margin: 0 !important;
    }

    .modal-body {
        max-height: calc(100vh - 60px) !important;
        overflow-y: auto !important;
    }

    /* ---- Info blocks: smaller padding ---- */
    .info-block {
        padding: 1rem !important;
    }

    .info-block-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem !important;
    }

    /* ---- Stat cards: smaller ---- */
    .stat-card {
        padding: 1rem !important;
    }

    .stat-card-value {
        font-size: 1.5rem !important;
    }

    /* ---- Home cards ---- */
    .home-card-image {
        height: 100px !important;
    }

    .home-card-details {
        grid-template-columns: 1fr 1fr !important;
    }

    /* ---- Nanny calendar cells ---- */
    .calendar-day {
        min-height: 80px !important;
        font-size: 0.65rem !important;
        padding: 0.25rem !important;
    }

    /* Even smaller event text on mobile */
    .calendar-day > div > div {
        font-size: 0.6rem !important;
        padding: 0.125rem 0.25rem !important;
    }

    #nanny-calendar th,
    #nanny-calendar td {
        padding: 0.125rem !important;
        font-size: 0.6875rem !important;
    }

    /* ---- Recipe detail in modal ---- */
    .modal-body [style*="gap: 0.75rem"] img[style*="border-radius"] {
        width: 36px !important;
        height: 36px !important;
    }

    /* ---- Week navigation ---- */
    .menu-week-nav .btn {
        font-size: 0.75rem !important;
        padding: 0.375rem 0.625rem !important;
    }

    .menu-week-nav h3 {
        font-size: 0.875rem !important;
    }

    /* ---- Recurring payments ---- */
    .recurring-payment-card {
        padding: 1rem !important;
    }

    .amount-main {
        font-size: 1.25rem !important;
    }

    .amount-range {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }
}

/* ==============================================
   LANDSCAPE MOBILE FIX
   ============================================== */
@media (max-width: 768px) and (orientation: landscape) {
    .main-content {
        padding-top: 3rem !important;
    }

    /* Menu calendar: 2 cols in landscape */
    .menu-week-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .modal {
        max-height: 95vh !important;
    }
}

/* ==============================================
   TOUCH / HOVER ADJUSTMENTS
   ============================================== */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets */
    .nav-item {
        padding: 0.875rem 1rem;
    }

    .btn {
        min-height: 44px;
    }

    .btn-sm {
        min-height: 36px;
    }

    /* Remove hover transforms on touch devices */
    .vehicle-card:hover,
    .stat-card:hover,
    .info-block:hover,
    .document-card:hover,
    .item-card:hover,
    .home-card:hover,
    .quick-action-btn:hover {
        transform: none;
    }

    /* Ensure delete buttons are always visible on touch */
    .entity-doc-download-btn {
        opacity: 1 !important;
    }
}
