/**
 * Mobile / tablet layout utilities (max-width: 1023px unless noted).
 * Desktop (lg / 1024px+) is unchanged.
 *
 * Usage:
 * - Logged-in list/form pages: wrap content in <div class="ms-page">...</div>
 * - Dashboard: also use class "dash-page" on the same wrapper
 * - Clinic show: also use class "ms-clinic"
 * - Login: set @section('body_class', 'login-page') on welcome
 */

/* =============================================================================
   App shell — sidebar (all breakpoints + mobile drawer scroll)
   ============================================================================= */

#app-sidebar {
    transition: width 200ms ease;
}

#app-sidebar.collapsed {
    width: 64px;
}

#app-sidebar .menu {
    width: 100%;
    -webkit-overflow-scrolling: touch;
}

#app-sidebar .menu a {
    transition: padding 200ms ease;
}

#app-sidebar .menu span {
    display: inline-block;
    white-space: nowrap;
    overflow: hidden;
    width: auto;
    opacity: 1;
    transition: opacity 200ms ease, width 200ms ease, margin 200ms ease;
}

#app-sidebar.collapsed .menu span {
    opacity: 0;
    width: 0;
    margin-left: 0;
}

#app-sidebar.collapsed .menu li {
    width: 64px;
}

#app-sidebar.collapsed .menu a {
    justify-content: center;
    padding-left: 0;
    padding-right: 0;
}

#app-sidebar .menu img:not(.sidebar-logo) {
    width: 20px;
    height: 20px;
}

#app-sidebar .sidebar-logo {
    display: block;
    width: 150px;
    height: auto;
    max-height: 60px;
    object-fit: contain;
    transition: width 200ms ease, transform 200ms ease;
}

#app-sidebar.collapsed .sidebar-logo {
    width: 60px;
    max-height: 60px;
    transform: translateX(0);
}

#app-sidebar.collapsed .quota-card,
#app-sidebar.collapsed .user-info,
#app-sidebar.collapsed .logout-text {
    display: none;
}

#app-sidebar.collapsed .logout-button {
    justify-content: center;
}

#app-sidebar .sidebar-panel {
    height: 100%;
    max-height: 100dvh;
    min-height: 0;
}

@media (max-width: 1023px) {
    #app-sidebar .sidebar-panel {
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    #app-sidebar .sidebar-panel > .menu {
        flex: none;
        min-height: auto;
        overflow: visible;
    }
}

/* =============================================================================
   Login page (body.login-page)
   ============================================================================= */

body.login-page {
    overflow: hidden;
}

body.login-page nav {
    display: none !important;
}

body.login-page main {
    padding: 0 !important;
    margin: 0 !important;
    min-height: 100vh;
    overflow: hidden;
}

body.login-page .login-container {
    min-height: 100vh;
    display: flex;
    width: 100%;
}

body.login-page .login-left,
body.login-page .login-right {
    flex: 1 1 50%;
    min-height: 100vh;
}

body.login-page .login-left {
    background: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: clamp(30px, 6vh, 80px) clamp(24px, 6vw, 110px);
}

body.login-page .login-right {
    background: #f8f9fa;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

body.login-page .brand-logo {
    display: flex;
    justify-content: center;
    margin-bottom: 12px;
}

body.login-page .brand-logo img {
    width: 140px;
    height: auto;
    display: block;
}

body.login-page .login-heading {
    font-size: 24px;
    font-weight: bold;
    color: #1f2937;
    margin-bottom: 8px;
}

body.login-page .welcome-text {
    font-size: 16px;
    color: #6b7280;
    margin-bottom: 40px;
}

body.login-page .password-field {
    position: relative;
}

body.login-page .password-field input {
    padding-right: 2.75rem;
}

body.login-page .password-toggle {
    position: absolute;
    right: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: #6b7280;
    background: none;
    border: none;
    padding: 0.25rem;
    cursor: pointer;
    line-height: 1;
}

body.login-page .password-toggle:hover {
    color: #374151;
}

body.login-page .promo-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

@media (max-width: 1024px) {
    body.login-page {
        overflow: auto;
    }

    body.login-page .login-container {
        flex-direction: column;
    }

    body.login-page .login-left {
        flex: 1 1 100%;
        max-width: 100%;
        min-height: 100vh;
        padding: 48px 32px;
    }

    body.login-page .login-right {
        display: none;
    }
}

/* =============================================================================
   Shared list / tab pages (.ms-page)
   ============================================================================= */

@media (max-width: 1023px) {
    .ms-page .text-sm.breadcrumbs ul,
    .ms-page .breadcrumbs ul {
        flex-wrap: wrap;
        row-gap: 0.25rem;
    }

    .ms-page .tabs {
        flex-wrap: wrap;
        align-content: flex-start;
    }

    .ms-page .tabs label.tab {
        flex-shrink: 0;
        font-size: 0.8125rem;
        padding: 0 0.625rem;
        height: 2.25rem;
        white-space: nowrap;
    }

    .ms-page .tabs .tab-content {
        flex: 0 0 100%;
        width: 100%;
        max-width: 100%;
        min-width: 0;
    }

    /* Datatable toolbar */
    .ms-page .py-4.border-b > .flex.items-center.justify-between {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .ms-page .py-4.border-b .flex.items-center.space-x-4 {
        flex-direction: column;
        align-items: stretch;
        width: 100%;
        gap: 0.75rem;
    }

    .ms-page .py-4.border-b .flex.items-center.space-x-4 > * {
        margin-left: 0 !important;
    }

    .ms-page .py-4.border-b .relative {
        width: 100%;
    }

    .ms-page [id^="search-input-"] {
        width: 100% !important;
        max-width: 100%;
    }

    .ms-page [id$="-filters-inline"] {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .ms-page .flex.items-center.space-x-2:has([id^="entries-select-"]) {
        flex-wrap: wrap;
        gap: 0.375rem;
    }

    .ms-page .py-4.border-b > .flex.items-center.justify-between > .flex.items-center.space-x-3 {
        flex-wrap: wrap;
        width: 100%;
        gap: 0.5rem;
    }

    .ms-page .py-4.border-b > .flex.items-center.justify-between > .flex.items-center.space-x-3 > * {
        margin-left: 0 !important;
    }

    .ms-page .py-4.border-b a.inline-flex,
    .ms-page .py-4.border-b button.inline-flex {
        justify-content: center;
    }

    /* Datatable pagination */
    .ms-page .px-4.py-4.border-t > .flex.items-center.justify-between {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .ms-page .px-4.py-4.border-t .flex.items-center.space-x-2 {
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.375rem;
    }

    .ms-page .px-4.py-4.border-t .flex.items-center.space-x-2 > * {
        margin-left: 0 !important;
    }

    /* Advanced filters panel */
    .ms-page [id^="advanced-filters-panel-"] .flex.items-center.justify-between {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .ms-page [id^="advanced-filters-panel-"] .flex.items-center.space-x-2 {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .ms-page [id^="advanced-filters-panel-"] .flex.items-center.space-x-2 > * {
        margin-left: 0 !important;
    }

    /*
     * Datatable: horizontal scroll + wrapped text per column.
     * (Avoid table-layout:fixed at 100% — too many columns overlap on phone.)
     */
    .ms-page .overflow-x-auto {
        margin-left: 0;
        margin-right: 0;
        width: 100%;
        max-width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .ms-page .overflow-x-auto > .inline-block {
        min-width: min-content;
        width: max-content;
        max-width: none;
    }

    .ms-page table.table-fixed {
        table-layout: auto;
        width: max-content;
        min-width: 100%;
    }

    /* Override nested data-table @media max-width caps (120–200px) */
    .ms-page .table-responsive th,
    .ms-page .table-responsive td {
        max-width: none !important;
        line-height: 1.4;
        vertical-align: top;
    }

    /* Headers: allow titles to wrap (Blade adds whitespace-nowrap on th) */
    .ms-page .table-responsive thead th:not(.action-column) {
        white-space: normal !important;
        min-width: 6.5rem !important;
        max-width: 9.5rem !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .ms-page .table-responsive thead th:not(.action-column) .flex span {
        white-space: normal !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .ms-page .table-responsive tbody td.text-wrap {
        white-space: normal !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        min-width: 6.5rem !important;
        max-width: 9.5rem !important;
    }

    .ms-page .table-responsive tbody td.whitespace-nowrap {
        white-space: nowrap !important;
        min-width: 4.75rem !important;
        max-width: none !important;
    }

    .ms-page .table-responsive th.action-column,
    .ms-page .table-responsive td.action-column {
        min-width: 5.5rem !important;
        max-width: 6.5rem !important;
        white-space: nowrap !important;
    }

    .ms-page .table-responsive .action-buttons {
        flex-direction: row;
        flex-wrap: nowrap;
    }

    .ms-page .table-responsive thead tr#search-row td {
        min-width: 6.5rem !important;
        max-width: 9.5rem !important;
    }

    .ms-page .table-responsive thead tr#search-row td:last-child {
        min-width: 5.5rem !important;
        max-width: 6.5rem !important;
    }

    /* Account settings — package table + form columns */
    .ms-page.ms-settings .tab-content .overflow-x-auto .table th,
    .ms-page.ms-settings .tab-content .overflow-x-auto .table td {
        white-space: normal;
        word-wrap: break-word;
        overflow-wrap: break-word;
        min-width: 5.5rem;
        max-width: 9rem;
        vertical-align: top;
        font-size: 0.8125rem;
        padding: 0.5rem 0.625rem;
    }

    .ms-page.ms-settings .tab-content .flex.flex-wrap.-mx-2 > [class*="sm:w-1/2"],
    .ms-page.ms-settings .tab-content [class*="md:w-1/2"],
    .ms-page.ms-settings .tab-content [class*="sm:w-1/2"] {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .ms-page.ms-settings .tab-content .card.m-2 {
        margin-left: 0.25rem;
        margin-right: 0.25rem;
    }

    /* Examination bulk action bars (rendered on body) */
    #bulk-action-bar-exam-table,
    #bulk-action-bar-assigned-exam-table {
        min-width: 0 !important;
        width: calc(100vw - 2rem);
        max-width: calc(100vw - 2rem);
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        padding: 0.75rem 1rem;
    }

    #bulk-action-bar-exam-table .flex.items-center.gap-3,
    #bulk-action-bar-assigned-exam-table .flex.items-center.gap-3 {
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.5rem;
    }

    #bulk-action-bar-exam-table > span,
    #bulk-action-bar-assigned-exam-table > span {
        white-space: normal;
        text-align: center;
    }

    /* Clinic show — details tab */
    .ms-page.ms-clinic #tab-details .flex.flex-wrap.items-center.justify-between.mb-4 {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .ms-page.ms-clinic #tab-details #readonly-buttons-top {
        width: 100%;
    }

    .ms-page.ms-clinic #tab-details #readonly-buttons-top .btn {
        width: 100%;
        justify-content: center;
    }

    .ms-page.ms-clinic #tab-details #logo-details {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }

    .ms-page.ms-clinic #tab-details .flex.flex-wrap.-mx-2 > [class*="sm:w-1/2"] {
        width: 100% !important;
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    /* Dashboard */
    .dash-page #dashboard-tabs {
        flex-wrap: wrap;
        align-content: flex-start;
        gap: 0.25rem;
        margin-left: 0.25rem;
        margin-right: 0.25rem;
    }

    .dash-page #dashboard-tabs label.tab {
        flex-shrink: 0;
        font-size: 0.8125rem;
        padding: 0 0.625rem;
        height: 2.25rem;
        white-space: nowrap;
    }

    .dash-page #dashboard-tabs #dash-filter-bar {
        flex: 0 0 100%;
        width: 100%;
        max-width: 100%;
    }

    .dash-page #dashboard-tabs .tab-content {
        flex: 0 0 100%;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        order: 2;
    }

    .dash-page #dash-filter-bar .dash-filter-row {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .dash-page #dash-filter-bar .dash-filter-fields {
        flex-direction: column;
        gap: 0.75rem;
    }

    .dash-page #dash-filter-bar .dash-filter-clear {
        align-self: flex-end;
    }

    .dash-page #dash-filter-bar .dropdown-content[style*="width:24rem"] {
        width: min(24rem, calc(100vw - 2rem)) !important;
        max-width: calc(100vw - 2rem);
    }

    .dash-page #dashboard-tabs .tab-content > .flex.gap-4.mt-4.mx-2 {
        flex-direction: column;
    }

    .dash-page #dashboard-tabs .tab-content > .flex.gap-4.mt-4.mx-2 > div[style*="flex:0 0 58%"] {
        flex: 1 1 auto !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    .dash-page #dashboard-tabs .tab-content .flex.gap-4.flex-1 {
        flex-direction: column;
    }

    .dash-page #dashboard-tabs .tab-content .flex.items-center.justify-center.gap-10 {
        flex-direction: column;
        gap: 1.25rem;
    }

    .dash-page #dashboard-tabs .tab-content .flex.items-start.justify-between.mb-4 {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }

    .dash-page #dashboard-tabs .tab-content .flex.items-start.justify-between.mb-4 .flex.gap-1 {
        flex-wrap: wrap;
    }

    .dash-page #dashboard-tabs .dash-table-pagination {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.5rem;
    }

    .dash-page #dashboard-tabs .dash-table-pagination > div:last-child {
        justify-content: center;
        flex-wrap: wrap;
    }

    .dash-page #dashboard-tabs .tab-content .bg-white.rounded-xl.border.border-gray-200.shadow-sm.p-5 {
        padding: 1rem;
    }

    .dash-page #dashboard-tabs .tab-content .bg-white.rounded-xl.border.border-gray-200.shadow-sm.p-5:has([style*="grid-template-columns:3fr"]) {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .dash-page #dashboard-tabs .tab-content [style*="grid-template-columns:3fr repeat(4,1fr)"] {
        min-width: 28rem;
    }

    .dash-page #dashboard-tabs #tab-main .module-grid {
        grid-template-columns: 1fr;
    }
}

@media (min-width: 640px) and (max-width: 1023px) {
    .dash-page #dashboard-tabs #tab-main .module-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}
