/* Global responsive utilities for layout + feature pages
   Phase M26: add shared classes first, then migrate pages incrementally. */

/* Medium desktop: sidebar occupies less width to reduce content squeeze. */
@media (max-width: 1280px) {
  .wrap {
    padding: 16px;
  }

  .app-shell {
    --sidebar-width: 240px;
  }
}

/* Tablet landscape: tighten top-level spacing further. */
@media (max-width: 1100px) {
  .wrap {
    padding: 12px;
  }

  .app-shell {
    gap: 12px;
    --sidebar-width: 220px;
  }

  .sidebar {
    padding: 12px;
  }
}

body{
  overflow-x: hidden;
}

/* Shared utility classes for page-level responsive cleanup. */
.u-grid-2,
.u-grid-3,
.u-grid-4 {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.u-grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.u-grid-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.u-grid-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.u-scroll-x {
  overflow-x: auto;
  width: 100%;
}

.u-actions-wrap {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.u-min-0 {
  min-width: 0;
}

.u-input-full {
  width: 100%;
  min-width: 0;
}

/* Unified breakpoints for form/grid stacking on narrow screens. */
@media (max-width: 1080px) {
  .u-grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 980px) {
  .u-grid-2,
  .u-grid-3,
  .u-grid-4 {
    grid-template-columns: 1fr;
  }

  .auc-header,
  .auc-section-head {
    flex-direction: column;
  }

  .auc-searchbar,
  .auc-selected,
  .auc-result,
  .auc-form-grid,
  .auc-advanced-grid {
    grid-template-columns: 1fr;
  }

  .auc-search-field {
    max-width: none;
  }

  .auc-actions .btn,
  .auc-searchbar .btn {
    width: 100%;
  }

  .auc-permissions ul {
    columns: 1;
  }
}

/* Desktop/Mobile Utility Visibility Helpers */
@media (min-width: 769px) {
  .mobile-nav-toggle,
  .mobile-filter-toolbar,
  .bottom-sheet-overlay {
    display: none !important;
  }
}

/* Tablet portrait & Mobile Breakpoints (768px and 576px) */
@media (max-width: 768px) {
  .wrap {
    padding: 10px 6px;
  }

  .card {
    padding: 12px;
    border-radius: var(--radius-md, 8px);
  }

  /* Hide floating FAB button to eliminate content overlap */
  .drawer-fab {
    display: none !important;
  }

  /* Keep the off-canvas backdrop clickable so tapping outside closes the sidebar. */
  .drawer-overlay {
    display: block;
  }

  /* Display mobile nav toggle button */
  .mobile-nav-toggle,
  button.mobile-nav-toggle,
  .brand .mobile-nav-toggle {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    min-height: 38px !important;
    min-width: 38px !important;
  }

  /* Hide desktop filter row on mobile */
  .desktop-filter-row,
  div.desktop-filter-row,
  .quo-filter-grid.desktop-filter-row,
  #moreFiltersPanel {
    display: none !important;
  }

  /* Mobile Filter Toolbar Layout */
  .mobile-filter-toolbar {
    display: flex !important;
    flex-direction: column;
    gap: 8px;
  }

  .mobile-controls-row {
    display: flex;
    gap: 6px;
    align-items: center;
  }

  .mobile-controls-row .mobile-sort-select {
    flex: 1 1 auto;
    min-width: 0;
    height: 40px;
    font-size: 12px;
    padding: 4px 8px;
  }

  .mobile-controls-row .mobile-filter-btn {
    height: 40px;
    padding: 0 12px;
    font-size: 12px;
    white-space: nowrap;
  }

  .mobile-controls-row .mobile-reset-btn {
    height: 40px;
    width: 40px;
    min-width: 40px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Touch Target Size Enforcement (Minimum 44px x 44px) */
  .btn,
  .pagination a,
  .chip-remove,
  .filter-chip {
    /* min-height: 40px; */
    touch-action: manipulation;
  }

  /* App Topbar Mobile Clean Compact Layout */
  .app-topbar {
    display: block !important;
    padding: 8px 12px !important;
    border-radius: 10px !important;
  }

  .app-topbar .brand {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
  }

  .app-topbar .mobile-nav-toggle {
    margin-right: 0 !important;
  }

  .app-topbar .brand .logo {
    width: 56px;
    height: 32px;
  }

  .app-topbar .brand-meta {
    flex: 1 1 auto;
    min-width: 0;
  }

  .app-topbar .brand-meta .title {
    display: none !important;
  }

  .app-topbar .app-subtitle {
    color: var(--color-text-primary);
    font-family: var(--font-heading);
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .app-topbar .desktop-account-nav {
    display: none !important;
  }

  .mobile-account-menu {
    position: relative;
    display: block;
    flex: 0 0 auto;
  }

  .mobile-account-trigger {
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    list-style: none;
    cursor: pointer;
    color: var(--color-brand-blue);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-md);
  }

  .mobile-account-trigger::-webkit-details-marker {
    display: none;
  }

  .mobile-account-trigger:focus-visible {
    outline: 2px solid var(--color-brand-blue);
    outline-offset: 2px;
  }

  .mobile-account-menu[open] .mobile-account-trigger {
    background: rgba(0, 141, 210, 0.08);
    border-color: var(--color-brand-blue);
  }

  .mobile-account-panel {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    z-index: 1300;
    width: min(250px, calc(100vw - 32px));
    padding: 12px;
    box-sizing: border-box;
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-soft);
  }

  .mobile-account-identity {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    padding-bottom: 10px;
  }

  .mobile-account-identity strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-role-switch {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    padding: 10px 0;
    border-top: 1px solid var(--color-border-subtle);
  }

  .mobile-role-switch label {
    color: var(--color-text-secondary);
    font-size: 12px;
  }

  .mobile-role-switch select {
    width: 100%;
    min-width: 0;
    min-height: 38px;
  }

  .mobile-account-logout {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 8px 0 0;
    box-sizing: border-box;
    border-top: 1px solid var(--color-border-subtle);
    color: var(--color-status-danger);
    font-weight: 600;
  }

  .mobile-account-logout:hover,
  .mobile-account-logout:focus-visible {
    color: var(--color-status-danger);
  }

  /* KPI Grid Mobile 2x2 with Selection Highlight */
  .dashboard-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }

  .dashboard-kpi-card {
    padding: 10px 12px !important;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1.5px solid transparent;
  }

  .dashboard-kpi-card.is-selected {
    border-color: var(--color-brand-blue, #0284c7) !important;
    background: #f0f9ff !important;
    box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.15);
  }

  .dashboard-kpi-number {
    font-size: 20px !important;
    font-weight: 700;
  }

  /* Mobile Bottom Sheet Drawer */
  .bottom-sheet-overlay {
    display: block !important;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(2px);
    z-index: 1050;
    opacity: 0;
    visibility: hidden;
    transition: all 0.25s ease;
  }

  .bottom-sheet-overlay.is-open {
    opacity: 1;
    visibility: visible;
  }

  .bottom-sheet-drawer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: #ffffff;
    border-radius: 16px 16px 0 0;
    z-index: 1051;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 0 -10px 25px rgba(0,0,0,0.15);
  }

  .bottom-sheet-overlay.is-open .bottom-sheet-drawer {
    transform: translateY(0);
  }

  .bottom-sheet-header {
    padding: 16px 20px;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .bottom-sheet-title {
    font-size: 16px;
    font-weight: 700;
    color: #0f172a;
  }

  .bottom-sheet-body {
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .bottom-sheet-footer {
    padding: 14px 20px;
    border-top: 1px solid #e2e8f0;
    display: flex;
    gap: 10px;
    background: #f8fafc;
  }

  .bottom-sheet-footer .btn {
    flex: 1;
    height: 46px;
    font-weight: 600;
  }

  .dev-role-switch {
    font-size: 11px;
  }

  .dev-role-switch select {
    padding: 2px 6px;
    font-size: 11px;
  }

  /* Page Topbar Mobile Header */
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .topbar-actions {
    width: 100%;
  }

  .topbar-actions .btn {
    width: 100%;
    justify-content: center;
    display: none;
  }

  /* KPI Grid Mobile 2x2 */
  .dashboard-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }

  .dashboard-kpi-card {
    padding: 10px 12px !important;
  }

  .dashboard-kpi-number {
    font-size: 20px !important;
  }

  /* Filter Grid Mobile Stack */
  .quo-filter-grid,
  .projects-filter-grid,
  .clients-filter-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }

  .quo-filter-grid-more:not(#moreFiltersPanel) {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }

  .quo-filter-grid .row,
  .projects-filter-grid .row {
    display: flex !important;
    flex-direction: row !important;
    width: 100% !important;
    gap: 6px !important;
  }

  .quo-filter-grid .row .btn,
  .projects-filter-grid .row .btn {
    flex: 1 1 0 !important;
    justify-content: center !important;
    padding: 8px 4px !important;
    font-size: 12px !important;
    white-space: nowrap;
  }

  /* Table-to-Cards Dedicated Mobile SaaS Layout */
  .table-cards-view table,
  .table-cards-view tbody,
  .table-cards-view tr {
    display: block;
    width: 100%;
  }

  .table-cards-view thead {
    display: none !important;
  }

  .table-cards-view tr {
    background: var(--color-bg-surface, #ffffff);
    border: 1px solid var(--color-border-subtle, #e2e8f0);
    border-radius: var(--radius-md, 10px);
    padding: 14px 16px;
    margin-bottom: 12px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.04);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .table-cards-view td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 0;
    border: none !important;
    width: 100% !important;
    box-sizing: border-box;
  }

  .table-cards-view td::before {
    display: none !important;
  }

  /* Ref Number (Header of Card) */
  .table-cards-view td:nth-child(1) {
    font-size: 13px;
    font-weight: 700;
    color: var(--color-brand-blue, #0284c7);
    word-break: break-all;
    order: 1;
  }

  /* Date (Placed Top Right) */
  .table-cards-view td:nth-child(4) {
    font-size: 11px;
    color: var(--color-text-secondary, #64748b);
    order: 1;
  }

  /* Client Name (Subheader) */
  .table-cards-view td:nth-child(2) {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-primary, #1e293b);
    order: 2;
    margin-top: 2px;
  }

  /* Service Name */
  .table-cards-view td:nth-child(3) {
    font-size: 12px;
    color: var(--color-text-secondary, #475569);
    order: 3;
  }

  /* Badges Row */
  .table-cards-view td:nth-child(5) {
    order: 4;
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .table-cards-view td:nth-child(5) > div {
    flex-direction: row !important;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center !important;
  }

  /* Card Footer Divider & Row (Fee + Actions) */
  .table-cards-view td:nth-child(6) {
    order: 5;
    font-size: 15px;
    font-weight: 700;
    color: var(--color-text-primary, #0f172a);
    padding-top: 8px;
    border-top: 1px dashed var(--color-border-subtle, #e2e8f0) !important;
  }

  .table-cards-view td:nth-child(7) {
    order: 6;
    justify-content: flex-end;
    gap: 8px;
    padding-top: 4px;
  }

  /* Quotation cards keep their header and footer items on shared horizontal rows. */
  .quo-cards-view tr {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 8px;
    padding: 12px 14px;
    margin-bottom: 10px;
  }

  .quo-cards-view td[data-col="quotation"] {
    width: auto !important;
  }

  .quo-cards-view td[data-col="amount"] {
    width: auto !important;
    margin-left: auto;
    justify-content: flex-end;
  }

  .quo-cards-view td[data-col="client"],
  .quo-cards-view td[data-col="service"],
  .quo-cards-view td[data-col="updated"] {
    width: 100% !important;
  }

  .quo-cards-view td[data-col="workflow"],
  .quo-cards-view td[data-col="delivery"],
  .quo-cards-view td[data-col="outcome"] {
    display: flex !important;
    width: auto !important;
    min-height: 24px;
    margin-top: 10px !important;
    padding-top: 0 !important;
    border-top: 0 !important;
    align-self: center;
    align-items: center;
  }

  .quo-cards-view td[data-col="next"],
  .quo-cards-view td[data-col="actions"] {
    display: flex !important;
    width: auto !important;
    min-height: 40px;
    margin-top: 8px !important;
    padding-top: 8px !important;
    border-top: 1px solid var(--color-border-subtle) !important;
    align-self: center;
    align-items: center;
  }

  .quo-cards-view td[data-col="next"] {
    flex: 1 1 calc(100% - 48px);
    margin-left: 0;
    justify-content: flex-start;
  }

  .quo-cards-view td[data-col="next"] .next-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100% !important;
    min-height: 40px;
  }

  .quo-cards-view td[data-col="actions"] {
    flex: 0 0 40px;
    justify-content: center;
  }

  .quo-cards-view td[data-col="actions"] .row-menu-toggle {
    width: 40px;
    height: 40px;
    border-color: var(--color-border-subtle);
    background: var(--color-bg-surface);
  }

  /* Table responsive fallback wrapper */
  .table-responsive,
  .u-scroll-x {
    display: block;
    width: 100%;
  }

  /* Action buttons auto-wrap */
  .u-actions-wrap,
  .header-actions,
  .form-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .u-actions-wrap .btn,
  .header-actions .btn,
  .form-actions .btn {
    width: 100%;
    text-align: center;
  }

  /* Drawers and modals responsive sizing */
  .drawer,
  .modal-dialog,
  .drawer-content {
    width: 100% !important;
    max-width: 100% !important;
  }
}

@media (max-width: 576px) {
  body {
    font-size: 13px;
  }

  h1, .h1 { font-size: 20px; }
  h2, .h2 { font-size: 18px; }
  h3, .h3 { font-size: 16px; }

  th, td {
    padding: 8px 6px;
    font-size: 12px;
  }

  .btn {
    padding: 8px 10px;
    font-size: 13px;
  }

  .mobile-search-row .search-field-wrap{
        display: flex;
    align-content: center;
    align-items: center;
    gap: 6px;
  }
  .mobile-search-input{
    width: 100%;
  }
}
