/* Mobile adaptations */
@media (max-width: 768px) {
  :root {
    --header-h: 56px;
    --sidebar-w: min(280px, 88vw);
  }

  body {
    font-size: 14px;
  }

  .topbar {
    padding: 0 0.85rem;
  }

  .topbar-title {
    font-size: 0.9rem;
    max-width: 52vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .content {
    padding: 0.85rem;
  }

  .page-header h1 {
    font-size: 1.35rem;
  }

  .page-header p {
    font-size: 0.88rem;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.65rem;
  }

  .stat-card {
    padding: 0.85rem;
  }

  .stat-card .value {
    font-size: 1.35rem;
  }

  .stat-card .icon {
    width: 32px;
    height: 32px;
    font-size: 1rem;
    margin-bottom: 0.35rem;
  }

  .card {
    padding: 1rem;
    border-radius: 10px;
  }

  .card-header-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .btn {
    min-height: 44px;
    padding: 0.65rem 1rem;
  }

  .btn-block-mobile {
    width: 100%;
  }

  .spam-actions {
    flex-direction: column;
  }

  .spam-actions .btn {
    width: 100%;
  }

  .spam-stop-wrap {
    margin-top: 1rem;
  }

  .spam-progress-percent {
    font-size: 1.25rem;
  }

  .users-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .users-toolbar .search-wrap,
  .per-page-select {
    width: 100%;
    min-width: 0;
  }

  .form-input,
  .form-textarea,
  .form-select {
    font-size: 16px;
  }

  .users-table {
    font-size: 0.88rem;
  }

  .users-table th {
    font-size: 0.7rem;
  }

  .user-hero {
    flex-direction: column;
    align-items: stretch;
  }

  .user-hero-actions {
    width: 100%;
  }

  .user-hero-actions .btn {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
  }

  .user-stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .catalog-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .catalog-toolbar .btn,
  .catalog-toolbar .form-input {
    width: 100%;
  }

  .modal {
    width: calc(100vw - 1.5rem);
    max-width: none;
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    margin: 0.75rem;
    padding: 1.1rem;
  }

  .modal-actions {
    flex-direction: column-reverse;
    gap: 0.5rem;
  }

  .modal-actions .btn {
    width: 100%;
  }

  .toast-container {
    left: 0.75rem;
    right: 0.75rem;
    top: auto;
    bottom: 1rem;
  }

  .toast {
    max-width: none;
  }

  .pagination {
    gap: 0.25rem;
  }

  .page-btn {
    min-width: 2rem;
    height: 2rem;
    font-size: 0.82rem;
  }

  .live-feed {
    max-height: 280px;
  }

  .chart-card {
    padding: 1rem;
  }

  .auth-card {
    margin: 1rem;
    padding: 1.25rem;
    width: calc(100% - 2rem);
    max-width: 400px;
  }

  .tree-node-header {
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  .tree-actions {
    width: 100%;
    justify-content: flex-start;
  }

  /* Mobile list cards — Priority+ / compact KV (2026 admin pattern) */
  .users-table-wrap,
  .ref-table-scroll {
    overflow: visible;
  }

  .users-table,
  .purchases-table,
  .top-buyers-table,
  .swaps-table,
  .promo-table,
  .ref-table {
    min-width: 0 !important;
    width: 100%;
    table-layout: auto !important;
  }

  .users-table thead {
    display: none;
  }

  .users-table tbody {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    padding: 0.55rem;
  }

  .users-table tr {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    width: 100%;
    box-sizing: border-box;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.65rem 0.75rem;
  }

  /* Empty state — never stack letters */
  .users-table tr.table-empty,
  .users-table tr:has(> td.users-empty) {
    display: flex !important;
    flex-direction: row !important;
    align-items: center;
    justify-content: center;
    min-height: 6rem;
    background: transparent;
    border: 1px dashed var(--border);
    padding: 1.25rem;
  }

  .users-table td.users-empty {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 auto;
    text-align: center !important;
    color: var(--text-muted);
    font-size: 0.95rem;
    line-height: 1.45;
    word-break: normal !important;
    overflow-wrap: normal !important;
    white-space: normal !important;
    writing-mode: horizontal-tb !important;
  }

  .users-table td.users-empty::before {
    display: none !important;
    content: none !important;
  }

  /* Dense list-item header — full width (override desktop col widths like 3rem) */
  .users-table td.mlc-primary,
  .top-buyers-table td.mlc-primary,
  .swaps-table td.mlc-primary,
  .purchases-table td.mlc-primary,
  .promo-table td.mlc-primary,
  .ref-table td.mlc-primary {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    padding: 0;
    margin: 0;
    border-bottom: none;
    text-align: left !important;
    box-sizing: border-box;
  }

  .users-table td.mlc-primary::before {
    display: none !important;
    content: none !important;
  }

  .mlc-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.55rem;
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--text);
    line-height: 1.25;
    min-width: 0;
    width: 100%;
  }

  .mlc-title > span:first-child,
  .mlc-title > a:first-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mlc-title .text-success,
  .mlc-title .badge,
  .mlc-title .swap-badge,
  .mlc-title .promo-status,
  .mlc-title .top-rank {
    flex-shrink: 0;
  }

  .mlc-sub {
    margin-top: 0.15rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: normal;
    overflow-wrap: normal;
    max-width: 100%;
  }

  .mlc-sub code,
  .mlc-primary code {
    word-break: normal !important;
    overflow-wrap: normal !important;
    white-space: nowrap;
    background: transparent;
    padding: 0;
    font-size: inherit;
  }

  .mlc-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem 0.65rem;
    margin-top: 0.35rem;
    font-size: 0.74rem;
    color: var(--text-muted);
    line-height: 1.4;
    width: 100%;
  }

  .mlc-meta > span {
    display: inline;
    white-space: nowrap;
    flex: 0 0 auto;
  }

  .mlc-meta strong {
    color: var(--text);
    font-weight: 600;
  }

  .mlc-foot {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.4rem;
    margin-top: 0.45rem;
    padding-top: 0.45rem;
    border-top: 1px solid color-mix(in srgb, var(--border) 75%, transparent);
  }

  .mlc-foot .btn {
    min-height: 34px;
    padding: 0.35rem 0.7rem;
    font-size: 0.78rem;
    flex: 0 0 auto;
  }

  .mlc-foot .btn-block-mobile {
    flex: 1 1 auto;
  }

  /* Hide almost all KV rows — content lives in primary */
  .users-table td[data-label]:not(.mlc-primary):not([data-label=""]) {
    display: none !important;
  }

  .users-table td.mlc-fold {
    display: none !important;
  }

  .users-table td:not([data-label]),
  .users-table td[data-label=""] {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: 0;
    margin: 0;
    border: none;
  }

  .users-table td:not([data-label])::before,
  .users-table td[data-label=""]::before {
    display: none !important;
    content: none !important;
  }

  /* If action cell follows primary, tuck into foot spacing */
  .users-table td.mlc-primary + td[data-label=""],
  .users-table td.mlc-primary ~ td[data-label=""] {
    margin-top: 0.45rem;
    padding-top: 0.45rem;
    border-top: 1px solid color-mix(in srgb, var(--border) 75%, transparent);
  }

  .users-table .user-name,
  .users-table .user-date,
  .users-table .table-link,
  .users-table .text-muted {
    text-align: left;
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
  }

  .users-table .swap-actions,
  .users-table .promo-actions {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.35rem;
    width: 100%;
    min-width: 0;
  }

  .users-table .swap-actions .btn,
  .users-table .promo-actions .btn {
    flex: 1 1 calc(50% - 0.25rem);
    min-height: 34px;
    padding: 0.35rem 0.55rem;
    font-size: 0.78rem;
    justify-content: center;
  }

  /* Single action (Профиль / Смотреть) — compact, right-aligned */
  .users-table td[data-label=""]:not(.swap-actions) {
    justify-content: flex-end;
  }

  .users-table td[data-label=""]:not(.swap-actions) > .btn {
    flex: 0 0 auto;
    min-height: 32px;
    min-width: 5.5rem;
    padding: 0.3rem 0.75rem;
    font-size: 0.78rem;
    justify-content: center;
  }

  .swaps-table td,
  .swaps-table th {
    overflow: visible;
  }

  .swaps-table td code {
    word-break: normal;
  }

  .swaps-tabs {
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 0.2rem;
  }

  .swaps-tabs::-webkit-scrollbar {
    display: none;
  }

  .swaps-tabs .swaps-tab {
    flex: 0 0 auto;
  }

  .swaps-tab.active {
    outline: none;
    box-shadow: none;
  }

  .top-buyers-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
  }

  .top-buyers-toolbar .swaps-tabs {
    width: 100%;
  }

  .top-buyers-table td:nth-child(1),
  .swaps-table td:nth-child(1) {
    width: auto !important;
    text-align: left !important;
  }

  .users-filters,
  .swaps-toolbar,
  .purchases-toolbar {
    gap: 0.45rem;
  }

  .ref-page .ref-table {
    min-width: 0;
  }

  .ref-table-scroll-tall {
    max-height: none;
  }

  .tree-header {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .tree-title {
    flex: 1 1 100%;
    min-width: 0;
    order: 2;
  }

  .tree-header .chevron,
  .tree-badge {
    order: 1;
  }

  .tree-actions {
    order: 3;
    width: 100%;
    justify-content: flex-start;
    margin-top: 0.15rem;
  }

  .tree-children {
    padding-left: 0.65rem;
    padding-right: 0.35rem;
  }

  .tree-children .tree-children {
    padding-left: 0.5rem;
  }

  .promo-header {
    flex-direction: column;
    align-items: stretch;
  }

  .promo-header .btn {
    width: 100%;
  }

  .card-header-row {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 420px) {
  .stats-grid,
  .user-stats-grid {
    grid-template-columns: 1fr;
  }

  .user-hero-actions .btn {
    flex: 1 1 100%;
  }

  .spam-stats-row {
    grid-template-columns: 1fr 1fr;
  }
}

/* Safe area for notched phones */
@supports (padding: max(0px)) {
  .topbar {
    padding-left: max(0.85rem, env(safe-area-inset-left));
    padding-right: max(0.85rem, env(safe-area-inset-right));
  }

  .content {
    padding-bottom: max(0.85rem, env(safe-area-inset-bottom));
  }

  .toast-container {
    bottom: max(1rem, env(safe-area-inset-bottom));
  }
}
