/* Mobile-first navigation and workspace refinement */
@media (max-width: 720px) {
  html, body { min-width: 320px; min-height: 100%; }
  body { overflow-x: hidden; -webkit-text-size-adjust: 100%; }
  .workspace { min-height: 100dvh; }

  /* Slide-in menu: labels remain visible even when desktop collapse state persists. */
  #sidebar, #sidebar.collapsed {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 25;
    width: min(330px, 86vw);
    height: 100dvh;
    flex: 0 0 min(330px, 86vw);
    padding: max(16px, env(safe-area-inset-top)) 14px max(12px, env(safe-area-inset-bottom));
    overflow: hidden;
    transform: translateX(-105%);
    border: 0;
    border-right: 1px solid var(--line);
    border-radius: 0 18px 18px 0;
    background: var(--surface);
    box-shadow: 18px 0 55px color-mix(in srgb, var(--text) 18%, transparent);
    transition: transform .22s ease;
  }
  #sidebar.mobile-open, #sidebar.collapsed.mobile-open { transform: translateX(0); }
  #sidebar .sidebar-brand { justify-content: space-between; padding: 2px 5px 18px; }
  #sidebar .brand-lockup > span:last-child,
  #sidebar .demo-banner > span:last-child,
  #sidebar .profile-mini-copy,
  #sidebar .more-dots,
  #sidebar .collapse-button { display: initial !important; }
  #sidebar .demo-banner { justify-content: flex-start; margin: 0 2px 12px; padding: 10px 11px; }
  #sidebar .side-nav { display: grid; flex: 1; min-height: 0; gap: 3px; overflow-x: hidden; overflow-y: auto; padding: 0 2px 12px; overscroll-behavior: contain; }
  #sidebar .nav-group-label { padding: 16px 11px 6px; font-size: 10px; }
  #sidebar .nav-link,
  #sidebar.collapsed .nav-link {
    display: flex;
    width: 100%;
    min-height: 48px;
    justify-content: flex-start;
    gap: 13px;
    padding: 10px 12px;
    border-radius: 11px;
    color: var(--muted);
    text-align: left;
    font-size: 14px;
    line-height: 1.3;
  }
  #sidebar #sideNav .nav-link > span:not(.nav-icon),
  #sidebar.collapsed #sideNav .nav-link > span:not(.nav-icon) {
    display: block !important;
    visibility: visible !important;
    min-width: 0;
    overflow: visible;
    color: inherit;
    opacity: 1 !important;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  #sidebar .nav-icon { width: 23px; flex: 0 0 23px; color: var(--primary); font-size: 18px; text-align: center; }
  #sidebar .nav-link.active { background: color-mix(in srgb, var(--primary) 10%, var(--surface)); color: var(--primary); }
  #sidebar .sidebar-bottom { padding-top: 10px; }
  #sidebar .profile-mini { justify-content: flex-start; min-height: 52px; }

  .app-column { min-width: 0; width: 100%; }
  .topbar { position: sticky; z-index: 8; height: 58px; gap: 6px; padding: 0 12px; }
  .mobile-menu { display: inline-grid; flex: 0 0 40px; width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 11px; background: var(--surface-soft); color: var(--text); }
  .breadcrumbs { min-width: 0; flex: 1; gap: 5px; overflow: hidden; white-space: nowrap; font-size: 11px; }
  .breadcrumbs > span:first-child, .crumb-separator { display: none; }
  .breadcrumbs strong { overflow: hidden; text-overflow: ellipsis; }
  .top-actions { flex: 0 0 auto; gap: 3px; }
  .top-actions .connection-indicator, .top-actions .global-search-trigger,
  .top-actions .top-language, .top-actions .user-menu > .chevron { display: none; }
  .top-actions .user-menu { padding: 0 2px; }
  .top-actions .theme-button { width: 38px; }
  .main-content { width: 100%; max-width: none; padding: 20px 15px calc(88px + env(safe-area-inset-bottom)); }

  /* Keep the high-value destinations clear; the More action opens the full labelled menu. */
  #mobileNav {
    position: fixed;
    inset: auto 0 0;
    z-index: 12;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
    padding: 7px max(10px, env(safe-area-inset-right)) calc(7px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    border-top: 1px solid var(--line);
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    box-shadow: 0 -8px 28px color-mix(in srgb, var(--text) 7%, transparent);
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
  }
  #mobileNav button { display: grid; min-width: 0; min-height: 58px; align-content: center; justify-items: center; gap: 4px; padding: 5px 3px; border: 0; border-radius: 12px; background: transparent; color: var(--muted); font-size: 10px; font-weight: 550; line-height: 1.15; text-align: center; }
  #mobileNav button:nth-child(4), #mobileNav button:nth-child(5) { display: none; }
  #mobileNav button span:first-child { display: block; font-size: 19px; line-height: 1; }
  #mobileNav button span:last-child { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #mobileNav button.active { background: color-mix(in srgb, var(--primary) 9%, var(--surface)); color: var(--primary); font-weight: 750; }
  #mobileNav button:focus-visible { outline-offset: -2px; }

  .page-heading { display: grid; gap: 12px; margin-bottom: 18px; }
  .page-heading h1 { font-size: clamp(23px, 6vw, 29px); }
  .page-heading p { font-size: 13px; line-height: 1.55; }
  .heading-actions { display: grid; grid-template-columns: 1fr; }
  .heading-actions .button { width: 100%; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
  .stat-card { min-width: 0; padding: 13px; }
  .stat-value { font-size: 23px; }
  .dashboard-grid, .assistant-layout, .data-grid, .settings-grid { grid-template-columns: minmax(0, 1fr); }
  .quick-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel, .data-card, .case-card, .matter-summary, .case-documents, .case-assistant-card, .analysis-card { border-radius: 14px; }
  .filter-bar { align-items: stretch; padding: 10px; }
  .filter-bar input, .filter-bar select { width: 100%; max-width: none; min-height: 46px; }
  .filter-bar .button { width: 100%; }
  .case-home-head, .case-welcome { align-items: stretch; flex-direction: column; gap: 14px; margin-bottom: 20px; }
  .case-home-head h1, .case-welcome h1 { font-size: clamp(29px, 8vw, 36px); }
  .case-home-head .button, .case-welcome .button { width: 100%; min-height: 48px; }
  .case-dashboard-grid, .case-analysis-grid, .matter-side-column, .case-grid { grid-template-columns: minmax(0, 1fr); }
  .case-toolbar { flex-wrap: wrap; gap: 8px; }
  .case-search { width: 100%; flex: 1 1 100%; min-height: 48px; }
  .case-total { padding-top: 0; }
  .matter-title-row { flex-direction: column; gap: 10px; }
  .matter-title-row h1 { font-size: clamp(27px, 7vw, 33px); overflow-wrap: anywhere; }
  .matter-summary-head { flex-direction: column; gap: 12px; }
  .matter-summary-head .button { width: 100%; }
  .case-stepper { grid-template-columns: 1fr; gap: 13px; }
  .case-documents li em { display: none; }
  .case-inline-upload { width: 42px; padding: 0; font-size: 0; }
  .case-inline-upload::before { content: '+'; font-size: 20px; }
  .chat-composer { grid-template-columns: 1fr; }
  .chat-composer .button { width: 100%; }
  .settings-tabs { display: flex; overflow-x: auto; }
  .settings-tabs button { min-height: 44px; white-space: nowrap; }
  .settings-form { grid-template-columns: minmax(0, 1fr); }
  .field.full, .modal-field.full { grid-column: auto; }
  .search-dialog { width: 100%; max-height: 88dvh; border-radius: 16px 16px 0 0; }
  .modal-backdrop { align-items: end; padding: 0; }
  .modal { width: 100%; max-height: 90dvh; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .modal-fields { grid-template-columns: minmax(0, 1fr); }
  .modal-field.full { grid-column: auto; }
  .toast-region { right: 12px; bottom: calc(82px + env(safe-area-inset-bottom)); left: 12px; }
  .toast { max-width: none; }
}

@media (max-width: 380px) {
  #sidebar, #sidebar.collapsed { width: min(310px, 88vw); flex-basis: min(310px, 88vw); }
  #mobileNav { gap: 2px; padding-inline: 6px; }
  #mobileNav button { font-size: 9px; }
  .main-content { padding-right: 12px; padding-left: 12px; }
}

@media (min-width: 721px) and (max-width: 900px) {
  #sidebar.mobile-open .nav-link > span:not(.nav-icon),
  #sidebar.mobile-open .brand-lockup > span:last-child,
  #sidebar.mobile-open .demo-banner > span:last-child,
  #sidebar.mobile-open .profile-mini-copy { display: initial !important; }
  #sidebar.mobile-open { width: min(310px, 78vw); flex-basis: min(310px, 78vw); }
}

@media (prefers-reduced-motion: reduce) {
  #sidebar, #mobileNav, .nav-link { scroll-behavior: auto !important; transition: none !important; }
}

/* Stage 4: reliable touch targets and bounded mobile layouts. */
@media (max-width: 720px) {
  html, body, #workspace, .app-column, .main-content { max-width: 100%; }
  body, #workspace, .app-column { overflow-x: clip; }
  .main-content > *, .page-heading > *, .dashboard-grid > *, .data-grid > *,
  .case-grid > *, .case-dashboard-grid > *, .matter-layout > *, .panel,
  .data-card, .case-card, .matter-summary, .case-documents { min-width: 0; }
  .case-card h2, .case-client, .case-card-meta b, .matter-title-row h1,
  .data-card h3, .row-main strong { overflow-wrap: anywhere; }
  button, input, select, textarea { max-width: 100%; }
  .filter-bar select, .filter-bar input, .case-search input,
  .settings-form input, .settings-form select, .settings-form textarea,
  .modal-fields input, .modal-fields select, .modal-fields textarea { min-height: 46px; }
  .case-card footer button, .case-review-switch, .small-button,
  .heading-actions .button, .quick-action { min-height: 44px; }
  .case-card footer { row-gap: 8px; }
  .case-empty { padding: 34px 16px; }
  .modal-backdrop, .search-overlay { overflow-y: auto; overscroll-behavior: contain; }
  .modal, .search-dialog { max-width: 100%; }
  .modal form, .modal-fields, .search-dialog, .search-results { min-width: 0; }
  .toast-region { max-width: 100%; }
}
