@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 80vh;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    border-right: none;
    border-top: 1px solid var(--color-border);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    box-shadow: 0 -8px 40px rgba(10, 12, 24, 0.18);
    overflow-y: auto;
  }

  /* Bottom-sheet grab handle */
  .sidebar::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    border-radius: var(--radius-full);
    background: var(--color-gray-300);
    margin: 0 auto var(--space-3);
    flex-shrink: 0;
  }

  .sidebar.open {
    transform: translateY(0);
  }

  /* The sheet covers the bar (z-index 100 > 90), so it owns the safe area
     while open. Rows get a 48px target — the old 9px padding was a desktop
     pointer measurement applied to thumbs. */
  .sidebar {
    padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
  }

  .sidebar-nav a,
  .sidebar-action-btn {
    min-height: 48px;
    padding: var(--space-3) var(--space-4);
  }

  .sidebar-user { padding: var(--space-4) var(--space-5); }

  .sidebar-lang-btn { padding: 11px 0; font-size: 13px; }

  :root { --mobile-nav-h: 56px; }

  .main-content {
    margin-left: 0;
    padding: var(--space-4);
    /* Reserve the bar's strip so the last row of content stays reachable. */
    padding-bottom: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom, 0px) + var(--space-4));
  }

  /* The sheet is open: the page behind it must not scroll with it. */
  body.sheet-open { overflow: hidden; }

  /* ---- Bottom navigation ------------------------------------------------
     The primary way around on a phone: thumb-reachable, always on screen, and
     showing where you are without being opened first. Four destinations plus
     the sheet, which still holds the full nav. */
  .mobile-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 90;
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    background: color-mix(in srgb, var(--color-surface) 92%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-top: 1px solid var(--color-border);
    box-shadow: 0 -2px 18px rgba(10, 12, 24, 0.07);
    /* Clears the iPhone home indicator without padding the bar on Android. */
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-height: var(--mobile-nav-h);
    padding: 6px 4px;
    border: none;
    background: none;
    color: var(--color-gray-500);
    font-family: inherit;
    font-size: 11px;
    font-weight: var(--weight-medium);
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: color var(--transition);
  }

  /* Pill behind the glyph — carries the active state at a glance, and gives
     the tap a target that reads as pressable. */
  .mobile-nav-ico {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 26px;
    border-radius: var(--radius-full);
    background: transparent;
    transition: background var(--transition), transform var(--transition);
  }

  .mobile-nav-ico svg { width: 21px; height: 21px; stroke-width: 1.9; }

  .mobile-nav-lab {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-nav-item:active .mobile-nav-ico { transform: scale(0.9); }

  .mobile-nav-item.active { color: var(--color-primary); font-weight: var(--weight-semibold); }
  .mobile-nav-item.active .mobile-nav-ico {
    background: color-mix(in srgb, var(--color-primary) 14%, transparent);
  }

  .mobile-nav-item:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
    border-radius: var(--radius-md);
  }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 12, 24, 0.5);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 99;
    display: none;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .grid-2, .grid-3, .grid-4 {
    grid-template-columns: 1fr;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-4);
  }

  .filter-bar {
    flex-direction: column;
  }

  .filter-bar .form-control,
  .filter-bar input[type="search"] {
    width: 100%;
    min-width: 0;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .modal {
    width: 95%;
    padding: var(--space-4);
    max-height: 85vh;
  }

  .modal h3 {
    font-size: var(--text-lg);
  }

  /* Stat cards: 2 columns on tablet */
  .grid.grid-4 {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Buttons: full width on mobile, larger touch targets */
  .btn { min-height: 44px; }
  .btn-sm { min-height: 40px; }
  .btn-xs { min-height: 32px; }
  .page-header .btn {
    width: 100%;
  }

  /* Chat page mobile */
  .chat-container {
    height: calc(100vh - 160px);
    max-height: calc(100vh - 160px);
  }

  .chat-msg {
    max-width: 95%;
  }

  /* Groups page mobile */
  .group-stats {
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  .tab-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }

  .tab-btn {
    flex-shrink: 0;
  }

  .item-row {
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  /* Toast mobile positioning — lifted clear of the bottom bar */
  .toast-container {
    bottom: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom, 0px) + var(--space-3));
    right: var(--space-4);
    left: var(--space-4);
  }

  .toast { max-width: 100%; }

  /* Table improvements */
  .table-wrapper {
    margin: 0 calc(-1 * var(--space-4));
    padding: 0 var(--space-4);
  }

  table th, table td {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-xs);
  }

  /* Mobile card tables */
  .table-cards thead { display: none; }
  .table-cards tbody tr {
    display: block;
    margin-bottom: var(--space-3);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-3);
  }
  .table-cards tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-1) 0;
    border-bottom: 1px solid var(--color-gray-100);
  }
  .table-cards tbody td:last-child { border-bottom: none; }
  .table-cards tbody td::before {
    content: attr(data-label);
    font-weight: var(--weight-semibold);
    font-size: var(--text-xs);
    text-transform: uppercase;
    color: var(--color-gray-600);
    flex-shrink: 0;
    margin-right: var(--space-3);
  }

  /* Progress bar fixed width override */
  .progress-bar[style*="width"] {
    width: 100% !important;
    max-width: 120px;
  }

  /* Pagination mobile */
  .pagination {
    flex-wrap: wrap;
  }

  /* Alert spacing */
  .alert {
    font-size: var(--text-xs);
  }

}

@media (min-width: 769px) {
  .mobile-nav {
    display: none;
  }

  .sidebar-overlay {
    display: none !important;
  }
}

@media (max-width: 480px) {
  html {
    font-size: 14px;
  }

  .stat-card .stat-value {
    font-size: var(--text-2xl);
  }

  /* Single column for stat cards on small phones */
  .grid.grid-4 {
    grid-template-columns: 1fr;
  }

  .main-content {
    padding: var(--space-3);
    padding-bottom: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom, 0px) + var(--space-3));
  }

  /* Five labels across a 320px screen: shrink the type, keep the target. */
  .mobile-nav-item { font-size: 10px; }
  .mobile-nav-ico { width: 42px; }

  /* Chat: tighter spacing */
  .chat-container {
    height: calc(100vh - 140px);
    max-height: calc(100vh - 140px);
  }

  .chat-messages {
    padding: var(--space-2);
  }

  .card {
    padding: var(--space-4);
  }

  .page-header h2 {
    font-size: var(--text-xl);
  }

  /* Test-take page mobile */
  .question-nav button {
    width: 32px;
    height: 32px;
    font-size: var(--text-xs);
  }

  .timer {
    font-size: var(--text-base);
    padding: var(--space-1) var(--space-3);
  }
}
