@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 ease;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    overflow-y: auto;
  }

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

  .main-content {
    margin-left: 0;
    padding: var(--space-4);
  }

  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4);
    background: var(--color-dark-blue);
    color: var(--color-white);
    margin: calc(-1 * var(--space-4));
    margin-bottom: var(--space-6);
  }

  [data-theme="dark"] .mobile-header {
    background: #4e51a0;
  }

  .mobile-header h1 {
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    letter-spacing: 0.05em;
  }

  .menu-toggle {
    background: none;
    border: none;
    color: var(--color-white);
    font-size: var(--text-xl);
    cursor: pointer;
    padding: var(--space-2);
  }

  .sidebar-toggle { display: none; }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    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-header-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }

  .chat-header-bar > div {
    width: 100%;
    flex-wrap: wrap;
  }

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

  .msg-avatar {
    width: 28px;
    height: 28px;
    font-size: var(--text-xs);
  }

  .chat-send-btn {
    padding: var(--space-3);
    min-width: 60px;
  }

  .model-select {
    width: 100%;
  }

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

  .add-row {
    flex-direction: column;
    align-items: stretch;
  }

  .add-row select,
  .add-row input {
    min-width: 0 !important;
    width: 100%;
  }

  .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 */
  .toast-container {
    bottom: var(--space-4);
    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-gray-200);
    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);
  }

  /* Settings page */
  .setting-item {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }

  /* 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-header {
    display: none;
  }

  .menu-toggle {
    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);
  }

  .mobile-header {
    margin: calc(-1 * var(--space-3));
    margin-bottom: var(--space-4);
  }

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

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

  .msg-bubble {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-xs);
  }

  .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);
  }
}
