@media (max-width: 980px) {
  .auth-screen, .split, .purchase-layout, .dashboard-grid { grid-template-columns: 1fr; }
  body.mobile-menu-open { overflow: hidden; }
  .shell { min-height: 100dvh; display: block; padding: 8px; }
  .sidebar {
    position: fixed;
    inset-block: 8px;
    right: 8px;
    z-index: 70;
    width: min(320px, calc(100vw - 48px));
    max-width: none;
    padding: 12px;
    gap: 10px;
    border-radius: 20px;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateX(calc(100% + 24px));
    transition: transform 0.28s ease, opacity 0.2s ease, visibility 0.28s;
    animation: none;
  }
  .shell.mobile-menu-open .sidebar { opacity: 1; visibility: visible; transform: translateX(0); }
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 60;
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: rgba(15, 23, 42, 0.38);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s ease, visibility 0.2s;
    backdrop-filter: blur(3px);
  }
  .shell.mobile-menu-open .sidebar-backdrop { display: block; opacity: 1; visibility: visible; pointer-events: auto; }
  .sidebar-header { padding: 5px; }
  .sidebar-mobile-close {
    width: 40px;
    height: 40px;
    min-height: 40px;
    margin-inline-start: auto;
    padding: 0;
    border-radius: 50%;
    display: inline-grid;
    place-items: center;
    color: var(--ink);
    background: rgba(118, 118, 128, 0.12);
    border: 1px solid var(--line-soft);
    box-shadow: none;
    font-size: 24px;
    line-height: 1;
  }
  .sidebar-footer { display: none; }
  .nav-list { min-height: 0; display: grid; gap: 4px; overflow-x: hidden; overflow-y: auto; scrollbar-width: thin; }
  .nav-list-primary { flex: 1 1 auto; min-height: 110px; }
  .nav-list-utility { flex: 0 0 auto; margin-top: 0; padding-top: 10px; overflow: visible; border-top: 1px solid var(--line-soft); }
  .nav-button { width: 100%; min-height: 44px; padding: 7px 11px; justify-content: flex-start; }
  .nav-button:hover, .nav-button.active { transform: none; }
  .sidebar-mobile-actions {
    flex: 0 0 auto;
    display: grid;
    gap: 8px;
    padding-top: 10px;
    border-top: 1px solid var(--line-soft);
  }
  .sidebar-mobile-actions .profile-menu-button,
  .sidebar-mobile-action {
    width: 100%;
    min-height: 44px;
    justify-content: flex-start;
    box-shadow: none;
  }
  .sidebar-workspace-picker {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr;
    gap: 7px;
  }
  .sidebar-workspace-picker input,
  .sidebar-workspace-picker select {
    width: 100%;
    min-width: 0;
  }
  .sidebar-mobile-action-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 7px;
  }
  .topbar-actions { display: none; }
  .mobile-menu-toggle {
    width: 44px;
    height: 44px;
    min-height: 44px;
    flex: 0 0 44px;
    display: inline-grid;
    place-items: center;
    padding: 0;
    border-radius: 13px;
    color: var(--ink);
    background: rgba(118, 118, 128, 0.12);
    border: 1px solid var(--line-soft);
    box-shadow: none;
    font-size: 20px;
  }
  .main { min-width: 0; min-height: 0; }
  .topbar { min-height: 0; flex-wrap: wrap; border-radius: 18px; padding: 10px 14px; }
  .topbar > .actions { flex: 1 1 520px; justify-content: flex-end; }
  .workspace-picker { flex: 1 1 360px; min-width: 0; }
  .workspace-picker input { width: min(220px, 35vw); }
  .workspace-picker select { flex: 1; min-width: 150px; }
  .content { overflow-x: hidden; }
  .cart-panel { position: static; }
  .brand-pane { min-height: 220px; padding: 28px; }
  .stats-grid { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
  .chat-layout, .ai-workspace { height: calc(100dvh - 235px); min-height: 560px; grid-template-columns: 1fr; grid-template-rows: 170px minmax(0, 1fr); }
  .ai-sidebar { grid-template-rows: auto minmax(0, 1fr); }
  .messaging-shell {
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
  }
  .messaging-shell .main {
    height: 100%;
    min-height: 0;
  }
  .messaging-shell .content {
    min-height: 0;
    overflow: hidden;
  }
  .messaging-shell .chat-layout,
  .messaging-shell .ai-workspace,
  .messaging-shell .chat-messenger {
    height: 100%;
    min-height: 0;
  }
}

@media (max-width: 640px) {
  html, body, #app { width: 100%; max-width: 100%; overflow-x: hidden; }
  .shell { padding: 0; gap: 0; }
  .sidebar { inset-block: 0; right: 0; width: min(310px, calc(100vw - 42px)); border-radius: 18px 0 0 18px; border-right: 0; padding: 10px; box-shadow: 0 6px 28px var(--shadow); }
  .sidebar-header { min-width: 0; }
  .sidebar-logo { width: 34px; height: 34px; flex: 0 0 34px; }
  .sidebar-title { min-width: 0; }
  .sidebar-title strong, .sidebar-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .nav-button { min-height: 44px; padding: 6px 10px; border-radius: 11px; font-size: 12px; }
  .nav-icon { width: 22px; height: 22px; flex-basis: 22px; }
  .topbar { border-radius: 0; border-inline: 0; padding: 10px; align-items: flex-start; }
  .topbar h2 { font-size: 17px; }
  .topbar-user { font-size: 11px; }
  .topbar > .actions { width: 100%; flex-basis: 100%; justify-content: stretch; }
  .subscription-renewal-banner {
    margin: 8px 10px 0;
    align-items: stretch;
    flex-direction: column;
  }
  .subscription-renewal-banner button { width: 100%; }
  .workspace-picker { width: 100%; flex: 1 1 100%; display: grid; grid-template-columns: minmax(0,1fr) minmax(120px,1fr); }
  .workspace-picker input, .workspace-picker select { width: 100%; min-width: 0; }
  .theme-toggle, #logout { min-width: 44px; min-height: 44px; }
  .main, .content, .content > *, .surface, .toolbar, .field-grid { min-width: 0; max-width: 100%; }
  .content { width: 100%; padding: 10px; overflow-x: hidden; }
  .surface { padding: 12px; margin-bottom: 10px; border-radius: 14px; }
  .auth-card { padding: 20px; }
  .two-col, .three-col, .stats-grid, .activity-row, .filter-row, .plan-limits { grid-template-columns: 1fr; }
  .toolbar { align-items: stretch; }
  .toolbar > * { max-width: 100%; }
  .toolbar .grow { flex-basis: 100%; }
  .actions { width: 100%; }
  .actions button, .actions .btn-link { min-width: 0; flex: 1 1 auto; min-height: 44px; white-space: normal; }
  .inline-form { width: 100%; flex-basis: 100%; flex-direction: column; }
  .inline-form input, .inline-form select { max-width: none; }
  input, textarea, select, button { min-height: 44px; }
  .table-wrap { max-width: 100%; border-radius: 14px; -webkit-overflow-scrolling: touch; }
  table, .compact-table table { min-width: 620px; }
  th, td { padding: 9px 10px; font-size: 12px; }
  .modal-backdrop { padding: 0; place-items: stretch; }
  .modal { width: 100%; max-height: 100dvh; height: 100dvh; border-radius: 0; border: 0; }
  .modal-header, .modal-footer { padding-inline: 12px; }
  .admin-tabs { flex-wrap: nowrap; overflow-x: auto; border-radius: 14px; }
  .admin-tabs button { flex: 0 0 auto; }
  .status-box { align-items: flex-start; flex-direction: column; }
  .grid-list, .plan-store, .module-launcher-grid { grid-template-columns: minmax(0, 1fr); }
  .module-launcher { min-height: 88px; grid-template-columns: 40px minmax(0, 1fr); align-items: start; padding: 12px; gap: 10px; }
  .module-launcher-icon { width: 40px; height: 40px; }
  .module-launcher > b { display: none; }
  .module-launcher strong { font-size: 14px; line-height: 1.7; }
  .module-launcher small { margin-top: 2px; font-size: 11px; line-height: 1.7; }
  .invitation-card { align-items: stretch; flex-direction: column; }
  .invitation-actions { width: 100%; }
  .invitation-actions button { flex: 1 1 0; }
  .chat-layout, .ai-workspace { height: calc(100dvh - 235px); min-height: 480px; grid-template-rows: 132px minmax(0,1fr); gap: 8px; }
  .messaging-shell .chat-layout,
  .messaging-shell .ai-workspace,
  .messaging-shell .chat-messenger {
    height: 100%;
    min-height: 0;
  }
  .messaging-shell .ai-workspace {
    position: relative;
    display: block;
  }
  .messaging-shell .ai-main {
    height: 100%;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .messaging-shell .ai-main-toolbar {
    min-height: 48px;
    padding-bottom: 8px;
  }
  .messaging-shell .ai-mobile-chats-toggle {
    min-width: 0;
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    text-align: right;
  }
  .messaging-shell .ai-mobile-chats-toggle strong {
    overflow: hidden;
    color: var(--muted);
    font-size: 12px;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .messaging-shell .ai-menu-chevron {
    font-size: 18px;
    transition: transform 0.2s ease;
  }
  .messaging-shell .ai-chats-open .ai-menu-chevron {
    transform: rotate(180deg);
  }
  .messaging-shell .ai-sidebar {
    position: absolute;
    inset: 52px 0 auto;
    z-index: 11;
    max-height: min(55dvh, 420px);
    display: none;
    grid-template-rows: auto minmax(110px, 1fr);
    padding: 10px;
    background: var(--panel-strong);
    border: 1px solid var(--line-soft);
    border-radius: 14px;
    box-shadow: 0 18px 44px var(--shadow-strong);
    backdrop-filter: blur(22px);
  }
  .messaging-shell .ai-workspace.ai-chats-open .ai-sidebar {
    display: grid;
  }
  .messaging-shell .ai-chat-list {
    max-height: calc(min(55dvh, 420px) - 82px);
    overflow-y: auto;
  }
  .messaging-shell .ai-actions-menu {
    width: min(210px, calc(100vw - 40px));
  }
  .chat-compose { grid-template-columns: minmax(0,1fr) 48px; padding: 8px; }
  .chat-compose button { width: 48px; padding: 0; }
  .messages { padding: 10px; }
  .bubble { max-width: 90%; }
  #toast-root { inset-inline: 10px; bottom: 76px; }
  .toast { min-width: 0; max-width: none; width: 100%; }
}

@media (max-width: 390px) {
  .workspace-picker { grid-template-columns: 1fr; }
  .topbar > .actions { gap: 6px; }
  .nav-button { gap: 6px; }
}
