/* ============================================================
   DengueOS — Responsive/PWA helpers
   Applies on top of Bootstrap + app-dark theme to polish
   mobile/tablet experience and PWA standalone display.
   ============================================================ */

/* ---------- PWA standalone tweaks ---------- */
@media all and (display-mode: standalone) {
    body {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    #page-topbar {
        padding-top: env(safe-area-inset-top);
    }
}

/* ---------- Global mobile hygiene ---------- */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

img, svg, video { max-width: 100%; }

/* Container padding on very small screens */
@media (max-width: 575.98px) {
    .container-fluid,
    .page-content .container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
    .page-title-box {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.5rem;
    }
    .page-title-box .page-title-right {
        width: 100%;
    }
    .page-title-box h4 {
        font-size: 1.1rem !important;
    }
}

/* ---------- Cards & spacing ---------- */
@media (max-width: 767.98px) {
    .card-body { padding: 1rem; }
    .card-title { font-size: 1rem; }
    h1, .h1 { font-size: 1.5rem; }
    h2, .h2 { font-size: 1.3rem; }
    h3, .h3 { font-size: 1.15rem; }
    h4, .h4 { font-size: 1.05rem; }
}

/* ---------- Tables: horizontal scroll on mobile ---------- */
@media (max-width: 991.98px) {
    .table-responsive-auto,
    .card .table:not(.table-nowrap-exempt) {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .table th, .table td {
        white-space: nowrap;
    }
}

/* ---------- Forms: stack and size inputs for touch ---------- */
@media (max-width: 767.98px) {
    .form-control,
    .form-select,
    .select2-container .select2-selection--single,
    .select2-container .select2-selection--multiple {
        font-size: 16px; /* prevents iOS zoom on focus */
        min-height: 42px;
    }
    .form-group,
    .mb-3 { margin-bottom: 1rem !important; }

    .btn {
        min-height: 42px;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }

    .form-actions,
    .card-footer {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .form-actions .btn,
    .card-footer .btn {
        flex: 1 1 auto;
    }
}

/* ---------- Sidebar: off-canvas behavior on small screens ---------- */
/* Theme already uses display:none <992px and .sidebar-enable to show.
   We add overlay + smooth slide, keeping theme behavior intact. */
@media (max-width: 991.98px) {
    .vertical-menu {
        z-index: 1050;
        box-shadow: 2px 0 16px rgba(0, 0, 0, 0.2);
        max-width: 85vw;
    }
    .main-content {
        margin-left: 0 !important;
    }
    body.sidebar-enable::before {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.45);
        z-index: 1040;
    }
    /* prevent horizontal scroll when sidebar open */
    body.sidebar-enable { overflow: hidden; }
}

/* ---------- Topbar ---------- */
@media (max-width: 575.98px) {
    #page-topbar .navbar-header {
        padding: 0 0.5rem;
    }
    .navbar-brand-box { width: auto !important; min-width: 60px; }
    .navbar-brand-box .logo-lg img { max-height: 40px; }
}

/* ---------- Dashboards & widgets ---------- */
@media (max-width: 767.98px) {
    .mini-stats-wid .card-body { padding: 0.85rem; }
    .apexcharts-canvas { max-width: 100% !important; }
}

/* ---------- Modals ---------- */
@media (max-width: 575.98px) {
    .modal-dialog {
        margin: 0.5rem;
    }
    .modal-body { padding: 1rem; }
    .modal-footer { flex-wrap: wrap; gap: 0.5rem; }
    .modal-footer .btn { flex: 1 1 auto; }
}

/* ---------- Landing / welcome page polish ---------- */
@media (max-width: 767.98px) {
    .navbar-links { display: none; }
    .navbar-cta .btn-secondary-glow,
    .navbar-cta .ml-2 { display: none; }
    .hero h1 { font-size: 1.9rem !important; line-height: 1.2; }
    .hero-subtitle { font-size: 1rem !important; }
    .hero-ctas { flex-wrap: wrap; gap: 0.5rem; }
    .hero-ctas a { flex: 1 1 100%; text-align: center; }
    .section-title { font-size: 1.6rem !important; line-height: 1.25; }
    .solution-grid,
    .modules-grid,
    .steps-grid,
    .kpi-grid,
    .security-items,
    .footer-grid {
        grid-template-columns: 1fr !important;
    }
    .footer-bottom {
        flex-direction: column;
        text-align: center;
        gap: 0.25rem;
    }
}

/* ============================================================
   PANEL (admin CRUD) — responsive polish
   Targets resources/views/panel/**
   ============================================================ */

/* --- Card header toolbar: wrap buttons on small screens --- */
.card-header.d-flex,
.card .card-header {
    flex-wrap: wrap;
    gap: 0.5rem;
    row-gap: 0.5rem;
}
.card-header .d-flex {
    flex-wrap: wrap;
    gap: 0.5rem;
}
@media (max-width: 575.98px) {
    .card-header {
        align-items: flex-start !important;
    }
    .card-header h4,
    .card-header h5 {
        font-size: 1rem;
        width: 100%;
    }
    .card-header .btn-sm {
        font-size: 0.75rem;
        padding: 0.35rem 0.55rem;
    }
    .card-header .btn i {
        margin-right: 0.2rem;
    }
}

/* --- Tablet gap: make col-md-3 / col-md-4 become 2-col on small tablets --- */
@media (min-width: 576px) and (max-width: 767.98px) {
    [class*="col-md-3"],
    [class*="col-md-4"] {
        flex: 0 0 auto;
        width: 50%;
    }
    [class*="col-md-2"] {
        flex: 0 0 auto;
        width: 50%;
    }
}

/* --- Form rows: add gutter so stacked fields breathe --- */
@media (max-width: 767.98px) {
    .row > [class*="col-"] > .form-group,
    .row > [class*="col-"] > .mb-3 {
        margin-bottom: 0.85rem;
    }
    textarea.form-control { min-height: 90px; }
}

/* --- Action buttons inside table rows: keep them inline + scrollable --- */
.table td .btn + .btn,
.table td .btn + a.btn,
.table td form + .btn,
.table td form + form {
    margin-left: 0.25rem;
}
@media (max-width: 575.98px) {
    .table td,
    .table th {
        padding: 0.5rem 0.5rem;
        font-size: 0.85rem;
    }
    .table td .btn {
        padding: 0.3rem 0.5rem;
        font-size: 0.75rem;
    }
}

/* --- Filter row partials (panel/**/form/filters.blade.php) --- */
.filters-row,
.row.filters,
.card-body > .row:first-child {
    row-gap: 0.5rem;
}

/* --- Ensure tables without .table-responsive still scroll on mobile --- */
@media (max-width: 767.98px) {
    .card-body > .table:not(.table-responsive .table),
    .card > .table:not(.table-responsive .table) {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* --- Page title box buttons wrap --- */
.page-title-box .page-title-right,
.page-title-box .breadcrumb {
    flex-wrap: wrap;
}

/* --- Mindmap (panel/territories/mindmap.blade.php) --- */
#mindmap {
    width: 100%;
    min-height: 360px;
}
@media (max-width: 767.98px) {
    #mindmap {
        height: calc(100vh - 220px) !important;
        min-height: 320px;
    }
}
@media (max-width: 575.98px) {
    #mindmap {
        height: calc(100vh - 240px) !important;
    }
}

/* --- Select2 dropdowns inside panel forms --- */
.select2-container--default .select2-selection--single {
    height: auto !important;
    min-height: 38px;
    padding: 4px 8px;
    display: flex;
    align-items: center;
}
.select2-container { width: 100% !important; max-width: 100%; }

/* --- Dropify (file upload) on mobile --- */
@media (max-width: 575.98px) {
    .dropify-wrapper {
        height: 160px !important;
    }
    .dropify-wrapper .dropify-message p {
        font-size: 0.85rem;
    }
}

/* --- Repeater rows (panel forms with jquery.repeater) --- */
@media (max-width: 767.98px) {
    [data-repeater-item] {
        border-bottom: 1px dashed #dee2e6;
        padding-bottom: 0.75rem;
        margin-bottom: 0.75rem;
    }
    [data-repeater-item] .row > [class*="col-"] {
        margin-bottom: 0.5rem;
    }
}

/* --- Pagination --- */
@media (max-width: 575.98px) {
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
    }
    .pagination .page-link {
        padding: 0.35rem 0.65rem;
        font-size: 0.85rem;
    }
}

/* --- Alerts / flash messages --- */
@media (max-width: 575.98px) {
    .alert {
        padding: 0.75rem;
        font-size: 0.9rem;
    }
}

/* --- Show/detail pages: dt/dd or definition lists --- */
@media (max-width: 575.98px) {
    dl.row > dt,
    dl.row > dd {
        width: 100%;
        flex: 0 0 100%;
        max-width: 100%;
        text-align: left !important;
    }
    dl.row > dt { margin-bottom: 0.15rem; }
    dl.row > dd { margin-bottom: 0.75rem; }
}

/* ============================================================
   MOBILE TABLES — CSS-only stacked "cards" layout
   NO horizontal scroll on mobile. Every <table class="table">
   automatically becomes a list of cards below 768px, independent
   of JS. pwa.js enhances the output by mirroring <th> text into
   data-label — but the layout works even if JS never runs.
   ============================================================ */
@media (max-width: 767.98px) {
    /* Kill horizontal scroll on the wrapper — the table is now vertical */
    .table-responsive {
        overflow-x: visible !important;
        overflow-y: visible !important;
        border: 0 !important;
        display: block;
        width: 100%;
    }

    /* Disarm Bootstrap's table flow so <tr>/<td> become blocks */
    .card .table,
    .table-responsive > .table,
    table.table {
        width: 100% !important;
        min-width: 0 !important;
        margin-bottom: 0;
        background: transparent;
        border: 0;
        table-layout: auto;
    }

    .card .table > thead,
    .table-responsive > .table > thead,
    table.table > thead {
        display: none !important;
    }

    .card .table > tbody,
    .table-responsive > .table > tbody,
    table.table > tbody {
        display: block;
        width: 100%;
    }

    .card .table > tbody > tr,
    .table-responsive > .table > tbody > tr,
    table.table > tbody > tr {
        display: block;
        width: 100%;
        border: 1px solid #e0e4ea;
        border-radius: 0.6rem;
        margin: 0 0 0.75rem;
        padding: 0.6rem 0.85rem;
        background: #ffffff;
        box-shadow: 0 1px 3px rgba(16, 24, 40, 0.06);
    }

    .card .table > tbody > tr > td,
    .card .table > tbody > tr > th,
    .table-responsive > .table > tbody > tr > td,
    .table-responsive > .table > tbody > tr > th,
    table.table > tbody > tr > td,
    table.table > tbody > tr > th {
        display: flex !important;
        align-items: flex-start;
        justify-content: space-between;
        gap: 0.75rem;
        width: 100% !important;
        min-height: 0;
        padding: 0.5rem 0 !important;
        margin: 0 !important;
        border: 0 !important;
        border-bottom: 1px dashed #eef1f5 !important;
        text-align: right !important;
        font-weight: 400;
        font-size: 0.9rem;
        color: #212529;
        white-space: normal !important;
        word-break: break-word;
        overflow-wrap: anywhere;
    }

    .card .table > tbody > tr > td:last-child,
    .card .table > tbody > tr > th:last-child,
    .table-responsive > .table > tbody > tr > td:last-child,
    .table-responsive > .table > tbody > tr > th:last-child,
    table.table > tbody > tr > td:last-child,
    table.table > tbody > tr > th:last-child {
        border-bottom: 0 !important;
    }

    /* Label (from data-label if pwa.js ran) */
    .card .table > tbody > tr > td[data-label]::before,
    .card .table > tbody > tr > th[data-label]::before,
    .table-responsive > .table > tbody > tr > td[data-label]::before,
    .table-responsive > .table > tbody > tr > th[data-label]::before,
    table.table > tbody > tr > td[data-label]::before,
    table.table > tbody > tr > th[data-label]::before {
        content: attr(data-label);
        display: inline-block;
        flex: 0 0 42%;
        max-width: 42%;
        text-align: left;
        font-weight: 600;
        color: #6b7280;
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.02em;
        line-height: 1.3;
        padding-top: 0.1rem;
        word-break: break-word;
    }

    /* First column (usually ID) — becomes the card header */
    .card .table > tbody > tr > th:first-child,
    .table-responsive > .table > tbody > tr > th:first-child,
    table.table > tbody > tr > th:first-child {
        background: #f3f6fb;
        border-radius: 0.4rem !important;
        padding: 0.5rem 0.75rem !important;
        margin: -0.25rem -0.25rem 0.5rem !important;
        font-weight: 600;
        color: #1f2937;
        font-size: 0.95rem;
    }

    /* Last column (actions) — full-width button row */
    .card .table > tbody > tr > td:last-child,
    .table-responsive > .table > tbody > tr > td:last-child,
    table.table > tbody > tr > td:last-child {
        flex-wrap: wrap;
        gap: 0.4rem;
        padding-top: 0.75rem !important;
        margin-top: 0.25rem !important;
        border-top: 1px solid #eef1f5 !important;
        justify-content: flex-start;
    }
    .card .table > tbody > tr > td:last-child[data-label]::before,
    .table-responsive > .table > tbody > tr > td:last-child[data-label]::before,
    table.table > tbody > tr > td:last-child[data-label]::before {
        flex: 0 0 100%;
        max-width: 100%;
        margin-bottom: 0.25rem;
    }
    .card .table > tbody > tr > td:last-child .btn,
    .table-responsive > .table > tbody > tr > td:last-child .btn,
    table.table > tbody > tr > td:last-child .btn {
        flex: 0 0 auto;
        min-height: 42px;
        min-width: 42px;
        padding: 0.45rem 0.65rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    .card .table > tbody > tr > td:last-child .btn i,
    .table-responsive > .table > tbody > tr > td:last-child .btn i,
    table.table > tbody > tr > td:last-child .btn i {
        font-size: 1.05rem !important;
    }

    /* Badges & pills inside cards */
    .card .table .badge,
    .table-responsive > .table .badge,
    table.table .badge {
        font-size: 0.78rem;
        padding: 0.3em 0.55em;
    }

    /* Dark theme variant */
    body[data-layout-mode="dark"] .card .table > tbody > tr,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr,
    body[data-layout-mode="dark"] table.table > tbody > tr {
        background: #2a3042;
        border-color: #32394e;
    }
    body[data-layout-mode="dark"] .card .table > tbody > tr > td,
    body[data-layout-mode="dark"] .card .table > tbody > tr > th,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr > td,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr > th,
    body[data-layout-mode="dark"] table.table > tbody > tr > td,
    body[data-layout-mode="dark"] table.table > tbody > tr > th {
        color: #c3cbe4;
        border-bottom-color: #32394e !important;
    }
    body[data-layout-mode="dark"] .card .table > tbody > tr > td[data-label]::before,
    body[data-layout-mode="dark"] .card .table > tbody > tr > th[data-label]::before,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr > td[data-label]::before,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr > th[data-label]::before,
    body[data-layout-mode="dark"] table.table > tbody > tr > td[data-label]::before,
    body[data-layout-mode="dark"] table.table > tbody > tr > th[data-label]::before {
        color: #9aa1b9;
    }
    body[data-layout-mode="dark"] .card .table > tbody > tr > th:first-child,
    body[data-layout-mode="dark"] .table-responsive > .table > tbody > tr > th:first-child,
    body[data-layout-mode="dark"] table.table > tbody > tr > th:first-child {
        background: #32394e;
        color: #e4e8f1;
    }
}

/* Small phones: label goes ABOVE the value (more room for both) */
@media (max-width: 479.98px) {
    .card .table > tbody > tr > td,
    .card .table > tbody > tr > th:not(:first-child),
    .table-responsive > .table > tbody > tr > td,
    .table-responsive > .table > tbody > tr > th:not(:first-child),
    table.table > tbody > tr > td,
    table.table > tbody > tr > th:not(:first-child) {
        flex-direction: column;
        align-items: stretch;
        text-align: left !important;
        gap: 0.15rem;
    }
    .card .table > tbody > tr > td[data-label]::before,
    .card .table > tbody > tr > th[data-label]::before,
    .table-responsive > .table > tbody > tr > td[data-label]::before,
    .table-responsive > .table > tbody > tr > th[data-label]::before,
    table.table > tbody > tr > td[data-label]::before,
    table.table > tbody > tr > th[data-label]::before {
        flex: 0 0 auto;
        max-width: 100%;
    }
}

/* Opt-out: keep a specific table as a real table on mobile
   Usage: <table class="table table-keep-grid"> */
@media (max-width: 767.98px) {
    .table.table-keep-grid,
    .table.table-keep-grid > thead,
    .table.table-keep-grid > tbody,
    .table.table-keep-grid > tbody > tr,
    .table.table-keep-grid > tbody > tr > td,
    .table.table-keep-grid > tbody > tr > th {
        display: revert !important;
    }
    .table-responsive:has(.table-keep-grid) {
        overflow-x: auto !important;
    }
}

/* ============================================================
   LOGIN / AUTH pages — full responsive rework
   Target: resources/views/auth/login.blade.php
   ============================================================ */
.auth-full-bg {
    position: relative;
    min-height: 100vh;
}
.auth-full-page-content {
    min-height: 100vh;
    display: flex;
    align-items: center;
}
.auth-full-page-content .w-100 {
    max-width: 420px;
    margin: 0 auto;
}

/* Hide the marketing / carousel panel below lg so the form is the hero */
@media (max-width: 1199.98px) {
    .auth-full-bg {
        display: none !important;
    }
    .row.no-gutters > .col-xl-3 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

@media (max-width: 575.98px) {
    .auth-full-page-content {
        padding: 1.25rem !important;
        min-height: 100vh;
    }
    .auth-full-page-content img.img-fluid {
        width: 200px !important;
        max-width: 70vw;
    }
    .auth-full-page-content .form-group label {
        font-size: 0.9rem;
    }
    .auth-full-page-content .form-control {
        font-size: 1rem;
        padding: 0.6rem 0.75rem;
        height: auto;
    }
    .auth-full-page-content .btn {
        padding: 0.7rem 1rem;
        font-size: 1rem;
        min-height: 46px;
    }
    .auth-full-page-content .float-right {
        float: none !important;
        display: block;
        margin-top: 0.25rem;
        margin-bottom: 0.25rem;
        font-size: 0.85rem;
    }
}

/* Safe-area padding on notched devices */
@supports (padding: env(safe-area-inset-top)) {
    .auth-full-page-content {
        padding-top: calc(1.5rem + env(safe-area-inset-top)) !important;
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom)) !important;
    }
}

/* ============================================================
   TOPBAR — user dropdown (profile + logout)
   The button used to be empty on <1200px (both label + chevron
   were hidden via d-xl-inline-block). Now it always shows a
   visible avatar icon so mobile users can log out.
   ============================================================ */
.user-dropdown #page-header-user-dropdown {
    padding: 0.5rem 0.9rem;
    line-height: 1;
    min-height: 44px;
}
.user-dropdown .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(107, 188, 135, 0.15);
    color: #6BBC87;
    font-size: 20px;
    line-height: 1;
    flex: 0 0 32px;
}
.user-dropdown .user-avatar i {
    font-size: 22px;
    line-height: 1;
}
.user-dropdown .mdi-chevron-down {
    font-size: 18px;
    line-height: 1;
    opacity: 0.7;
}

/* Dark theme variant */
body[data-layout-mode="dark"] .user-dropdown .user-avatar {
    background: rgba(107, 188, 135, 0.2);
    color: #a9e4c1;
}

/* Mobile: hide the chevron to save space but keep the avatar */
@media (max-width: 575.98px) {
    .user-dropdown #page-header-user-dropdown {
        padding: 0.4rem 0.6rem;
    }
    .user-dropdown .mdi-chevron-down {
        display: none !important;
    }
}

/* The dropdown menu itself — make sure it doesn't clip off the right edge */
.user-dropdown .dropdown-menu {
    min-width: 200px;
    right: 0;
    left: auto;
}
@media (max-width: 575.98px) {
    .user-dropdown .dropdown-menu {
        position: absolute !important;
        right: 0;
        left: auto !important;
        transform: none !important;
        margin-top: 0.25rem;
    }
}

/* ---------- Touch targets ---------- */
@media (hover: none) and (pointer: coarse) {
    a, button, .btn, .dropdown-item, .nav-link {
        min-height: 40px;
    }
}

/* ---------- Print ---------- */
@media print {
    .vertical-menu,
    #page-topbar,
    .footer,
    .no-print { display: none !important; }
    .main-content { margin-left: 0 !important; }
}
