/* =============================================================================
   Responsive — viewport-specific rules only.
   Grid reflow, card sizing, and form layout are handled by container queries
   in layout.css and components.css. This file is for things that genuinely
   depend on the viewport: off-canvas sidebar, mobile header, touch targets.
   ============================================================================= */

/* ----- Phones / small tablets -------------------------------------------- */
@media (max-width: 768px) {
  /* App shell collapses to a single column; sidebar becomes off-canvas. */
  .app-layout,
  .app-layout:has(.sidebar.collapsed) {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: auto;
    max-height: 80svh;
    transform: translateY(100%);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    overflow-y: auto;
    transition: transform var(--duration-slow) var(--ease-emphasized);
  }

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

  /* Mobile header */
  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    background: var(--brand-700);
    color: var(--text-inverse);
    margin: calc(-1 * var(--gutter)) calc(-1 * var(--gutter)) var(--space-5);
    box-shadow: var(--elev-1);
  }

  .mobile-header h1 {
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
  }

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

  .menu-toggle:hover { background: oklch(1 0 0 / 0.1); }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: var(--surface-overlay);
    z-index: 99;
    display: none;
  }
  .sidebar-overlay.open { display: block; }

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

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

  .modal { padding: var(--space-5); max-height: 88svh; }
  .modal h3 { font-size: var(--text-lg); }

  /* Bigger touch targets on mobile (Apple/Material guidance: ≥44px). */
  .btn    { min-height: 44px; }
  .btn-sm { min-height: 38px; }
  .btn-xs { min-height: 32px; }
  .page-header .btn { width: 100%; }

  /* Toast goes full-width and above any home-indicator. */
  .toast-container {
    bottom: max(var(--space-4), env(safe-area-inset-bottom, 0));
    right: var(--space-4);
    left: var(--space-4);
  }
  .toast { max-width: 100%; }

  /* Tighter table padding when wrapping is enabled. */
  table th, table td {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-xs);
  }

  /* Mobile "card table" — opt-in via .table-cards (existing convention). */
  .table-cards { background: transparent; border: none; }
  .table-cards thead { display: none; }
  .table-cards tbody tr {
    display: block;
    margin-bottom: var(--space-3);
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: var(--space-3);
  }
  .table-cards tbody tr:hover td { background: transparent; color: inherit; }
  .table-cards tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-1) 0;
    border-bottom: 1px solid var(--border-subtle);
  }
  .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(--text-muted);
    letter-spacing: var(--tracking-wide);
    flex-shrink: 0;
    margin-right: var(--space-3);
  }

  /* Page-specific mobile tweaks (chat, groups) — kept from the prior file. */
  .chat-container {
    height: calc(100svh - 160px);
    max-height: calc(100svh - 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%; }

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

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

  /* Inline-styled progress bars: clamp width so they don't blow out rows. */
  .progress-bar[style*="width"] {
    width: 100% !important;
    max-width: 120px;
  }

  /* Rive canvases */
  #owlHappyCanvas,
  #chatOwlCanvas { width: 250px !important; height: 250px !important; }
  #owlCanvas { width: 240px !important; height: 240px !important; }
}

/* ----- Desktop --------------------------------------------------------- */
@media (min-width: 769px) {
  .mobile-header,
  .menu-toggle { display: none; }
  .sidebar-overlay { display: none !important; }
}

/* ----- Very small phones ----------------------------------------------- */
@media (max-width: 480px) {
  html { font-size: 15px; }

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

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

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

  .question-nav button {
    width: 32px;
    height: 32px;
    font-size: var(--text-xs);
  }

  #owlHappyCanvas,
  #chatOwlCanvas { width: 200px !important; height: 200px !important; }
  #owlCanvas     { width: 180px !important; height: 180px !important; }
}
