/* Below 1100px the layout tightens. Below 768px it becomes a phone layout:
   the sidebar turns into a drawer and every wide table becomes stacked cards. */

.drawer-backdrop,
.menu-btn {
  display: none;
}

@media (max-width: 1200px) {
  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stat:nth-child(3) {
    border-right: 0;
  }

  .stat:nth-child(n + 4) {
    border-top: 1px solid var(--border);
  }

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

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

@media (max-width: 900px) {
  .cols-3 {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}

@media (max-width: 768px) {
  .app,
  .app[data-rail="on"] {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    z-index: 70;
    transform: translateX(-100%);
    transition: transform var(--t-base) var(--ease-out);
    box-shadow: var(--shadow-drawer);
  }

  .app[data-drawer="open"] .sidebar {
    transform: translateX(0);
  }

  .app[data-rail="on"] .brand-name,
  .app[data-rail="on"] .nav-group,
  .app[data-rail="on"] .nav-link span,
  .app[data-rail="on"] .nav-count {
    display: revert;
  }

  .app[data-rail="on"] .nav-link {
    justify-content: flex-start;
    padding: var(--s2) var(--s3);
  }

  .app[data-drawer="open"] .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(20, 22, 26, 0.4);
    z-index: 65;
  }

  .menu-btn {
    display: inline-flex;
  }

  .rail-btn {
    display: none;
  }

  /* The search needs a full line of its own once the buttons crowd it. */
  .topbar {
    height: auto;
    padding: var(--s2) var(--s4);
    flex-wrap: wrap;
  }

  .search {
    order: 3;
    flex: 1 0 100%;
    max-width: none;
  }

  .topbar-spacer {
    order: 1;
  }

  .user-name {
    display: none;
  }

  .view {
    padding: var(--s4);
  }

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

  .stat {
    border-right: 1px solid var(--border);
  }

  .stat:nth-child(2n) {
    border-right: 0;
  }

  .stat:nth-child(n + 3) {
    border-top: 1px solid var(--border);
  }

  .stat-value {
    font-size: 22px;
  }

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

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

  .page-actions {
    margin-left: 0;
    width: 100%;
  }

  .page-actions .btn {
    flex: 1;
  }

  /* Tables become cards. Each cell carries its own label. */
  table.data,
  table.data tbody,
  table.data tr,
  table.data td {
    display: block;
    width: 100%;
  }

  table.data thead {
    display: none;
  }

  table.data tr {
    padding: var(--s3) var(--s4);
    border-bottom: 1px solid var(--border);
  }

  table.data tr:last-child {
    border-bottom: 0;
  }

  table.data td {
    height: auto;
    padding: var(--s1) 0;
    border-bottom: 0;
    display: flex;
    align-items: center;
    gap: var(--s3);
  }

  table.data td::before {
    content: attr(data-label);
    font-size: var(--fs-meta);
    color: var(--text-muted);
    min-width: 96px;
    flex-shrink: 0;
  }

  table.data td[data-label=""]::before {
    display: none;
  }

  table.data td.right {
    text-align: left;
  }

  table.data td.cell-primary {
    font-size: var(--fs-body);
    font-weight: 600;
    padding-bottom: var(--s2);
  }

  table.data td.cell-primary::before {
    display: none;
  }

  .kv dt {
    min-width: 108px;
  }

  .toast-stack {
    right: var(--s4);
    left: var(--s4);
    bottom: var(--s4);
  }
}

@media (max-width: 420px) {
  .view {
    padding: var(--s3);
  }

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

  .stat {
    border-right: 0;
  }

  .stat:nth-child(n + 2) {
    border-top: 1px solid var(--border);
  }
}
