/* ============================================================
   Responsive Design — Hosting CRM Theme
   ============================================================
   Mobile-first approach with min-width media queries.
   Breakpoints: 640px (sm), 1024px (md), 1280px (lg)

   Depends on: tokens.css, reset.css, utilities.css
   Import AFTER all component CSS files.
   ============================================================ */

/* ============================================================
   BASE — Mobile (default, < 640px)
   ============================================================
   All mobile styles are the default (no media query needed).
   Larger screens override via min-width breakpoints below.
   ============================================================ */

/* ── Mobile-First Base Adjustments ────────────────────────── */

/* 44px minimum touch target for interactive elements.
   Checkboxes/radios are intentionally excluded — their wrapping label is the
   tap target; forcing 44px made the custom control oversized and misaligned. */
button,
a,
input[type="submit"],
input[type="button"],
[role="button"],
.nav-link,
.action-btn,
.header-toggle,
.sidebar-pin-btn {
    min-height: 44px;
    min-width: 44px;
}

/* Keep checkboxes/radios at their natural size (label provides the tap area). */
input[type="checkbox"],
input[type="radio"] {
    min-width: 0;
    min-height: 0;
}

/* Enforce minimum font size for readability on mobile */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-size: var(--text-sm, 0.875rem);
    line-height: var(--leading-normal, 1.5);
    overflow-x: hidden;
}

/* Ensure minimum readable font sizes */
p, li, td, th, span, a, label, input, select, textarea, button {
    font-size: max(var(--text-sm, 0.875rem), 14px);
}

/* ── Sidebar — Mobile Overlay Behavior ────────────────────── */

/* Hide sidebar off-screen on mobile by default */
#sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 260px !important;
    z-index: var(--z-modal-backdrop, 1050);
    box-shadow: none;
}

/* Sidebar open state (toggled via JS adding .open class) */
#sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.25);
}

/* Sidebar hover should not expand on mobile */
#sidebar:hover {
    width: 260px !important;
}

/* Sidebar nav links: full-width touch targets on mobile */
#sidebar .sidebar-nav .nav-link {
    height: 44px;
    padding: 0 1rem;
    font-size: 0.875rem;
}

#sidebar .sidebar-nav .nav-link span:not(.nav-badge) {
    opacity: 1;
}

#sidebar .sidebar-brand .brand-text {
    opacity: 1;
}

#sidebar .sidebar-section-label {
    display: block;
    opacity: 1;
}

#sidebar .sidebar-footer .nav-link span {
    opacity: 1;
}

/* Sidebar pin button: full touch target */
.sidebar-pin-btn {
    height: 44px;
    padding: 0 1rem;
}

.sidebar-pin-btn span {
    opacity: 1;
}

/* ── Content Wrapper — No Sidebar Offset on Mobile ────────── */
#page-content-wrapper {
    margin-left: 0 !important;
    width: 100% !important;
}

/* ── Sidebar Backdrop Overlay ─────────────────────────────── */
body.sidebar-open::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: calc(var(--z-sidebar, 1040) - 1);
    animation: fadeIn 0.2s ease;
}

body.sidebar-open {
    overflow: hidden;
}

/* ── Header — Mobile Adjustments ──────────────────────────── */
.top-header {
    padding: 0 0.75rem;
    height: var(--header-height, 60px);
}

/* Show mobile sidebar toggle */
.top-header .header-toggle {
    display: flex !important;
}

/* Hide header brand text on small screens to save space */
.header-brand {
    display: none;
}

/* Search bar: full width on mobile */
.top-header .header-search {
    flex: 1;
    max-width: none;
    margin: 0 0.5rem;
}

.top-header .header-search::after {
    display: none;
}

/* Header search input: touch-friendly height */
.top-header .header-search input {
    height: 38px;
    font-size: 16px; /* Prevents iOS zoom on focus */
}

/* Header actions: reduce gap */
.top-header .header-actions {
    gap: 0.125rem;
}

/* Header action buttons: touch-friendly size */
.top-header .header-actions .action-btn {
    width: 38px;
    height: 38px;
}

/* Hide secondary header actions on mobile to reduce clutter */
.top-header .header-actions .action-btn[title="Cloud Shell"],
.top-header .header-actions .action-btn[title="Directory"],
.top-header .header-actions .action-btn[title="Help"] {
    display: none;
}

/* User info: show only avatar on mobile */
.header-user .user-info {
    display: none !important;
}

.header-user {
    padding: 0.25rem;
}

.header-user .user-avatar {
    width: 32px;
    height: 32px;
}

.header-chevron {
    display: none;
}

/* ── Main Content — Reduced Padding ───────────────────────── */
main {
    padding: 1rem !important;
}

/* ── Page Container — Mobile Adjustments ──────────────────── */
.page-container {
    padding: 1rem;
}

/* ── Page Header — Stack Vertically ───────────────────────── */
.page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.page-title {
    font-size: 1.25rem;
}

/* ── Tables — Horizontal Scroll ───────────────────────────── */
.table-responsive-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -1rem;
    padding: 0 1rem;
    border-bottom: 1px solid var(--border-default, #e2e8f0);
}

.table-responsive-wrapper .table {
    min-width: 600px;
    width: max-content;
}

/* Add scroll indicator shadow on right edge */
.table-responsive-wrapper {
    position: relative;
}

.table-responsive-wrapper::after {
    content: '';
    position: sticky;
    right: 0;
    top: 0;
    bottom: 0;
    width: 24px;
    background: linear-gradient(to right, transparent, var(--bg-base, #ffffff));
    pointer-events: none;
    flex-shrink: 0;
}

/* ── Forms — Stack Vertically ─────────────────────────────── */
/* Form groups: full width on mobile */
.sd-form-group,
.form-group {
    margin-bottom: 1rem;
}

/* Form row: stack on mobile */
.form-row,
.u-grid.cols-2,
.u-grid.cols-3,
.u-grid.cols-4 {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Form inputs: full width + touch-friendly */
.sd-input,
.sd-select,
.sd-textarea,
.form-control,
.form-select {
    width: 100%;
    font-size: 16px; /* Prevents iOS zoom on focus */
    min-height: 44px;
    padding: 0.625rem 0.75rem;
}

/* Textarea: slightly shorter min height on mobile */
.sd-textarea,
textarea {
    min-height: 6rem;
}

/* Select: ensure touch-friendly */
.sd-select,
select.form-select {
    min-height: 44px;
}

/* ── Buttons — Full Width on Mobile ───────────────────────── */
/* Block buttons: full width on mobile */
.sd-button--block,
.btn-block {
    width: 100%;
    justify-content: center;
}

/* Default buttons: full width in form contexts */
.form-actions,
button[type="submit"],
.btn-group-mobile {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-actions .sd-button,
.form-actions .btn,
button[type="submit"] {
    width: 100%;
    min-height: 44px;
}

/* Button groups: stack on mobile */
.sd-button-group {
    display: flex;
    flex-direction: column;
    width: 100%;
}

.sd-button-group .sd-button {
    width: 100%;
    border-radius: var(--radius-sm, 6px);
}

.sd-button-group .sd-button + .sd-button {
    margin-left: 0;
    margin-top: -1px;
}

.sd-button-group .sd-button:first-child {
    border-radius: var(--radius-sm, 6px) var(--radius-sm, 6px) 0 0;
}

.sd-button-group .sd-button:last-child {
    border-radius: 0 0 var(--radius-sm, 6px) var(--radius-sm, 6px);
}

/* ── Cards — Mobile Adjustments ───────────────────────────── */
.card {
    border-radius: var(--radius-md, 8px);
}

.card-header {
    padding: 0.875rem 1rem;
    font-size: 0.875rem;
}

.card-body {
    padding: 1rem;
}

/* ── Stat Cards — Adjust for Mobile ───────────────────────── */
.stat-card {
    padding: 1rem;
}

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

.stat-card .stat-label {
    font-size: 0.7rem;
}

/* ── Section Adjustments ──────────────────────────────────── */
.section {
    padding: 1.25rem 0;
}

.section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

/* ── Module Navigation — Scrollable on Mobile ─────────────── */
.module-nav {
    padding: 0 0.75rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.module-nav-inner {
    flex-wrap: nowrap;
    gap: 0.5rem;
}

.module-nav-left {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.module-nav-left::-webkit-scrollbar {
    display: none;
}

.module-nav-title {
    display: none;
}

.module-nav-tabs {
    flex-wrap: nowrap;
}

.module-nav-tab {
    padding: 0.5rem 0.625rem;
    font-size: 0.78rem;
    white-space: nowrap;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}

.module-nav-right {
    display: none;
}

/* ── Search Dropdown — Mobile ─────────────────────────────── */
.search-results-dropdown {
    position: fixed;
    top: var(--header-height, 60px);
    left: 0;
    right: 0;
    width: 100% !important;
    max-height: calc(100vh - var(--header-height, 60px));
    border-radius: 0;
    transform: none !important;
}

@keyframes searchDropdownIn {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ── Breadcrumbs — Truncate on Mobile ─────────────────────── */
.breadcrumb-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    margin-bottom: 1rem;
}

.breadcrumb-wrapper .breadcrumb {
    font-size: 0.75rem;
}

/* ── Dropdowns — Mobile-Full-Width ────────────────────────── */
.dropdown-menu {
    min-width: 100%;
}

.dropdown-item {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0.625rem 0.75rem;
    font-size: 0.875rem;
}

/* ── Modals — Mobile-Full-Screen ──────────────────────────── */
.modal-content {
    border-radius: 0;
    min-height: 100vh;
    border: none;
}

.modal-header {
    padding: 0.875rem 1rem;
}

.modal-body {
    padding: 1rem;
}

/* ── Pagination — Compact on Mobile ───────────────────────── */
.pagination {
    flex-wrap: wrap;
    gap: 0.25rem;
    justify-content: center;
}

.pagination .page-link {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.82rem;
}

/* ── Alerts — Adjusted Padding ────────────────────────────── */
.alert {
    padding: 0.75rem 1rem;
    font-size: 0.8125rem;
}

/* ── Blade Info Grid — Single Column ──────────────────────── */
.blade-info-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
}

.blade-info-grid.cols-2,
.blade-info-grid.cols-3 {
    grid-template-columns: 1fr;
}

/* ── Login Page — Mobile Adjustments ──────────────────────── */
.login-card .card-body {
    padding: 1.5rem;
}

.login-card h4 {
    font-size: 1.05rem;
}

/* ── Touch-Friendly Link Styles ───────────────────────────── */
a {
    padding: 0.25rem 0;
}

/* ── Scrollable Containers ────────────────────────────────── */
.u-overflow-x-auto,
.u-overflow-scroll {
    -webkit-overflow-scrolling: touch;
}

/* ── Utility Responsive Overrides ─────────────────────────── */
/* Hide desktop-only elements on mobile */
.u-hidden-sm-down {
    display: none;
}

/* Show mobile-only elements */
.u-visible-sm-down {
    display: block;
}

/* Grid adjustments for mobile: single column */
.u-grid {
    gap: 1rem;
}


/* ============================================================
   SM — Small Tablet (min-width: 640px)
   ============================================================ */
@media (min-width: 640px) {

    /* ── Layout ─────────────────────────────────────────────── */
    main {
        padding: 1.25rem !important;
    }

    .page-container {
        padding: 1.25rem 1.5rem;
    }

    /* ── Header ─────────────────────────────────────────────── */
    .top-header {
        padding: 0 1rem;
    }

    .header-brand {
        display: block;
    }

    .top-header .header-search {
        max-width: 360px;
        margin: 0 auto;
    }

    /* ── Page Header ────────────────────────────────────────── */
    .page-header {
        margin-bottom: 2rem;
    }

    .page-title {
        font-size: 1.5rem;
    }

    /* ── Forms ──────────────────────────────────────────────── */
    /* Two-column form layout on small tablets */
    .form-row.two-col,
    .form-row-cols-2 {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 1rem;
    }

    .form-row.two-col > *,
    .form-row-cols-2 > * {
        flex: 1 1 calc(50% - 0.5rem);
        min-width: 200px;
    }

    /* ── Buttons ────────────────────────────────────────────── */
    /* Inline button actions on small tablets */
    .form-actions,
    button[type="submit"] {
        flex-direction: row;
        justify-content: flex-end;
    }

    .form-actions .sd-button,
    .form-actions .btn,
    button[type="submit"] {
        width: auto;
    }

    /* Button groups: inline on small tablets */
    .sd-button-group {
        display: inline-flex;
        flex-direction: row;
        width: auto;
    }

    .sd-button-group .sd-button {
        width: auto;
    }

    .sd-button-group .sd-button + .sd-button {
        margin-top: 0;
        margin-left: -1px;
    }

    .sd-button-group .sd-button:first-child {
        border-radius: var(--radius-sm, 6px) 0 0 var(--radius-sm, 6px);
    }

    .sd-button-group .sd-button:last-child {
        border-radius: 0 var(--radius-sm, 6px) var(--radius-sm, 6px) 0;
    }

    /* ── Stat Cards ─────────────────────────────────────────── */
    .stat-card .stat-value {
        font-size: 1.75rem;
    }

    /* ── Cards ──────────────────────────────────────────────── */
    .card-body {
        padding: 1.25rem;
    }

    /* ── Module Navigation ──────────────────────────────────── */
    .module-nav-title {
        display: block;
    }

    .module-nav-right {
        display: flex;
    }

    /* ── Dropdowns ──────────────────────────────────────────── */
    .dropdown-menu {
        min-width: 180px;
    }

    /* ── Breadcrumbs ────────────────────────────────────────── */
    .breadcrumb-wrapper {
        white-space: normal;
    }

    /* ── Blade Info Grid ────────────────────────────────────── */
    .blade-info-grid {
        grid-template-columns: 1fr 1fr;
    }

    .blade-info-grid.cols-3 {
        grid-template-columns: 1fr 1fr;
    }

    /* ── Utility Responsive Overrides ───────────────────────── */
    .u-hidden-sm-down {
        display: block;
    }

    .u-visible-sm-down {
        display: none;
    }
}


/* ============================================================
   MD — Tablet / Small Desktop (min-width: 1024px)
   ============================================================ */
@media (min-width: 1024px) {

    /* ── Sidebar — Return to Desktop Behavior ───────────────── */
    #sidebar {
        transform: none;
        width: var(--sidebar-width, 48px) !important;
        z-index: var(--z-sidebar, 1040);
        box-shadow: none;
    }

    /* Sidebar hover expand */
    #sidebar:hover {
        width: var(--sidebar-expanded-width, 220px) !important;
    }

    /* Sidebar nav: hidden labels by default (expand on hover) */
    #sidebar .sidebar-nav .nav-link {
        height: 36px;
        padding: 0 0.55rem;
    }

    #sidebar .sidebar-nav .nav-link span:not(.nav-badge) {
        opacity: 0;
    }

    #sidebar:hover .sidebar-nav .nav-link span:not(.nav-badge) {
        opacity: 1;
    }

    #sidebar .sidebar-brand .brand-text {
        opacity: 0;
    }

    #sidebar:hover .sidebar-brand .brand-text {
        opacity: 1;
    }

    #sidebar .sidebar-section-label {
        display: none;
        opacity: 0;
    }

    #sidebar:hover .sidebar-section-label {
        display: block;
        opacity: 1;
    }

    #sidebar .sidebar-footer .nav-link span {
        opacity: 0;
    }

    #sidebar:hover .sidebar-footer .nav-link span {
        opacity: 1;
    }

    /* Sidebar pin button: hidden label by default */
    .sidebar-pin-btn {
        height: 36px;
        padding: 0 0.75rem;
    }

    .sidebar-pin-btn span {
        opacity: 0;
    }

    #sidebar:hover .sidebar-pin-btn span {
        opacity: 1;
    }

    /* ── Content Wrapper — Sidebar Offset ───────────────────── */
    #page-content-wrapper {
        margin-left: var(--sidebar-width, 48px) !important;
        width: calc(100% - var(--sidebar-width, 48px)) !important;
    }

    /* ── Sidebar Backdrop — Not Needed on Tablet+ ───────────── */
    body.sidebar-open::before {
        display: none;
    }

    body.sidebar-open {
        overflow: auto;
    }

    /* ── Header ─────────────────────────────────────────────── */
    .top-header {
        padding: 0 1.25rem;
    }

    .top-header .header-search {
        max-width: 420px;
    }

    .top-header .header-search input {
        height: 34px;
        font-size: 0.875rem;
    }

    /* Show all header actions */
    .top-header .header-actions .action-btn[title="Cloud Shell"],
    .top-header .header-actions .action-btn[title="Directory"],
    .top-header .header-actions .action-btn[title="Help"] {
        display: flex;
    }

    .header-user .user-info {
        display: flex !important;
    }

    .header-chevron {
        display: block;
    }

    /* Hide mobile sidebar toggle */
    .top-header .header-toggle {
        display: none !important;
    }

    /* ── Main Content ───────────────────────────────────────── */
    main {
        padding: 1.5rem !important;
    }

    .page-container {
        padding: 1.5rem 2rem;
    }

    /* ── Page Header ────────────────────────────────────────── */
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .page-title {
        font-size: 1.75rem;
    }

    /* ── Tables — No Scroll Needed ──────────────────────────── */
    .table-responsive-wrapper {
        overflow-x: visible;
        margin: 0;
        padding: 0;
    }

    .table-responsive-wrapper .table {
        min-width: auto;
        width: 100%;
    }

    .table-responsive-wrapper::after {
        display: none;
    }

    /* ── Forms — Two Column Layout ──────────────────────────── */
    .form-row.two-col,
    .form-row-cols-2 {
        flex-direction: row;
    }

    /* ── Module Navigation ──────────────────────────────────── */
    .module-nav {
        padding: 0 1.5rem;
    }

    .module-nav-title {
        display: block;
    }

    /* ── Sections ───────────────────────────────────────────── */
    .section {
        padding: 1.5rem 0;
    }

    .section-header {
        flex-direction: row;
        margin-bottom: 1.25rem;
    }

    /* ── Stat Cards ─────────────────────────────────────────── */
    .stat-card {
        padding: 1.25rem 1.5rem;
    }

    .stat-card .stat-value {
        font-size: 1.85rem;
    }

    /* ── Modals ─────────────────────────────────────────────── */
    .modal-content {
        border-radius: var(--radius-lg, 12px);
        min-height: auto;
    }

    .modal-header {
        padding: 1rem 1.25rem;
    }

    .modal-body {
        padding: 1.25rem;
    }

    /* ── Blade Info Grid ────────────────────────────────────── */
    .blade-info-grid.cols-3 {
        grid-template-columns: 1fr 1fr 1fr;
    }

    /* ── Login Page ─────────────────────────────────────────── */
    .login-card .card-body {
        padding: 2.5rem;
    }
}


/* ============================================================
   LG — Desktop (min-width: 1280px)
   ============================================================ */
@media (min-width: 1280px) {

    /* ── Content — Max Width Constraint ─────────────────────── */
    .page-container {
        max-width: var(--content-max-width, 1400px);
        padding: 2rem 2.5rem;
    }

    /* ── Main Content ───────────────────────────────────────── */
    main {
        padding: 2rem !important;
    }

    /* ── Header ─────────────────────────────────────────────── */
    .top-header {
        padding: 0 1.5rem;
    }

    .top-header .header-search {
        max-width: 460px;
    }

    /* ── Page Header ────────────────────────────────────────── */
    .page-title {
        font-size: 1.875rem;
    }

    /* ── Tables ─────────────────────────────────────────────── */
    .table thead th {
        padding: 0.875rem 1.25rem;
    }

    .table tbody td {
        padding: 0.875rem 1.25rem;
    }

    /* ── Stat Cards ─────────────────────────────────────────── */
    .stat-card {
        padding: 1.5rem 2rem;
    }

    .stat-card .stat-value {
        font-size: 2rem;
    }

    /* ── Sections ───────────────────────────────────────────── */
    .section {
        padding: 2rem 0;
    }

    /* ── Utility Responsive Overrides ───────────────────────── */
    .u-hidden-lg-down {
        display: block;
    }
}


/* ============================================================
   DARK MODE — Responsive Overrides
   ============================================================ */
[data-theme="dark"] body.sidebar-open::before {
    background: rgba(0, 0, 0, 0.7);
}

[data-theme="dark"] .table-responsive-wrapper::after {
    background: linear-gradient(to right, transparent, var(--bg-base, #0f1117));
}


/* ============================================================
   REDUCED MOTION — Respect User Preferences
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
    #sidebar {
        transition: none;
    }

    body.sidebar-open::before {
        animation: none;
    }

    .table-responsive-wrapper::after {
        display: none;
    }
}


/* ============================================================
   PRINT — Hide Responsive Elements
   ============================================================ */
@media print {
    #sidebar,
    .top-header,
    .header-toggle,
    body.sidebar-open::before {
        display: none !important;
    }

    #page-content-wrapper {
        margin-left: 0 !important;
        width: 100% !important;
    }

    .table-responsive-wrapper {
        overflow: visible;
    }

    .table-responsive-wrapper::after {
        display: none;
    }
}
