/**
 * Social Ops — mobile-only polish (≤800px).
 * Loaded last; desktop styles unchanged above this breakpoint.
 */

@media (max-width: 800px) {
  /* VA sidebar — workspace first, compact sections */
  body.scoped-employee .sidebar-scroll {
    display: flex;
    flex-direction: column;
  }
  body.scoped-employee #navSectionWorkspace,
  body.scoped-employee #navWorkspaceLinks {
    order: -20;
  }
  body.scoped-employee #navSectionMarketing,
  body.scoped-employee #navMarketingWrap {
    order: -10;
  }
  body.scoped-employee #navWorkspaceLinks .side-link {
    padding-top: 0.55rem;
    padding-bottom: 0.55rem;
  }

  /* --- Safe area + overflow --- */
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }
  body {
    overflow-x: hidden;
  }
  #loginScreen {
    padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
      max(1.5rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  }
  .main {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  }
  .sidebar {
    padding-top: 0;
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 200;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100dvh;
    max-height: 100dvh;
  }
  .sidebar-backdrop {
    z-index: 190;
  }
  .sidebar-head {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    flex-shrink: 0;
    position: relative;
    top: auto;
    z-index: 3;
    background: var(--sidebar-bg);
    padding-top: max(0.65rem, env(safe-area-inset-top));
    padding-bottom: 0.75rem;
    margin-bottom: 0.35rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }
  .sidebar .brand strong {
    font-size: 0.95rem;
    line-height: 1.2;
  }
  .sidebar .brand .muted {
    font-size: 0.72rem;
    line-height: 1.2;
  }
  .sidebar .brand {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
    flex: 1;
    min-width: 0;
  }
  .sidebar-close {
    display: inline-flex !important;
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border-color: rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.06);
    color: var(--sidebar-text);
  }
  .sidebar-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* Close control: fixed top-right on the dimmed area (always reachable while scrolling) */
  body.sb-open .sidebar-toggle {
    position: fixed;
    top: max(0.65rem, env(safe-area-inset-top));
    right: max(0.75rem, env(safe-area-inset-right));
    left: auto;
    z-index: 220;
    width: 44px;
    height: 44px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.5);
  }
  .sidebar-close:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  body.sb-open .app-topbar .top,
  body.sb-open .app-back-btn:not(:disabled),
  body.sb-open .notif-bell-wrap,
  body.sb-open .theme-toggle {
    visibility: hidden;
    pointer-events: none;
  }

  /* --- Topbar: flex wrap (more reliable than grid on mobile Safari) --- */
  .app-topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 0.45rem;
    row-gap: 0.3rem;
    margin-bottom: 0.65rem;
    background: var(--bg);
    position: sticky;
    top: 0;
    z-index: 60;
    padding-top: max(0.15rem, env(safe-area-inset-top));
  }
  .sidebar-toggle {
    order: 1;
    flex: 0 0 auto;
  }
  .app-back-btn:not(:disabled) {
    order: 2;
    flex: 0 0 auto;
    font-size: 0.78rem;
    padding: 0.4rem 0.55rem;
    max-width: 7.5rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .app-back-btn:disabled {
    display: none;
  }
  .notif-bell-wrap {
    order: 3;
    margin-left: auto !important;
    flex: 0 0 auto;
  }
  .theme-toggle {
    order: 4;
    flex: 0 0 auto;
  }
  .app-topbar .top {
    order: 10;
    flex: 1 1 100%;
    width: 100%;
    min-width: 0;
  }
  .app-topbar h1 {
    font-size: 1.05rem;
    line-height: 1.25;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .app-topbar .hint {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    margin: 0.12rem 0 0;
    line-height: 1.35;
    max-width: 100%;
  }
  body.ops-hide-pageheader .app-back-btn:disabled {
    display: none;
  }

  /* Long usernames / URLs must wrap, not blow layout */
  #tree,
  .modal-body,
  .ops-panel,
  .av-card,
  .task-card,
  .conn-sidebar {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* Touch targets */
  .btn,
  .side-link,
  .av-tab,
  .av-status-tab,
  .conn-model-tab,
  .tasks-filter-btn,
  .tasks-scope-btn,
  .ops-tab,
  .ops-va-tabs button,
  .wt-week-btn {
    min-height: 40px;
  }
  .btn-sm,
  .btn-xs {
    min-height: 36px;
  }

  /* --- Modals --- */
  .modal-overlay {
    padding: max(0.35rem, env(safe-area-inset-top)) max(0.35rem, env(safe-area-inset-right))
      max(0.35rem, env(safe-area-inset-bottom)) max(0.35rem, env(safe-area-inset-left));
  }
  .modal {
    width: 100%;
    max-width: 100%;
    border-radius: 14px;
    max-height: min(92vh, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 1rem));
  }
  .modal-wide {
    max-width: 100%;
  }
  .modal-header h2 {
    font-size: 1rem;
    padding-right: 2rem;
  }
  .modal-footer {
    flex-direction: column-reverse;
    gap: 0.45rem;
  }
  .modal-footer .btn {
    width: 100%;
  }
  .row-2 {
    grid-template-columns: 1fr !important;
  }

  /* --- Notifications: positioned by app.js positionNotifDropdown() --- */

  /* --- Banners --- */
  .banner {
    font-size: 0.85rem;
    padding: 0.65rem 0.75rem;
  }

  /* --- Operations (fixes + polish) --- */
  .ops-root {
    gap: 0.75rem;
  }
  .ops-panel {
    padding: 0.85rem 0.9rem;
    border-radius: 12px;
  }
  .ops-panel-h {
    font-size: 0.7rem;
    margin-bottom: 0.75rem;
  }
  .ops-panel-head-row {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }
  .ops-va-team-toolbar,
  .ops-va-team-model-filter {
    width: 100%;
    min-width: 0;
  }

  /* Keep real table layout inside scroll wrapper */
  .ops-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    margin: 0 -0.15rem;
    padding: 0 0.15rem;
  }
  .ops-table {
    display: table !important;
    width: max-content;
    min-width: 100%;
    white-space: nowrap;
    font-size: 0.8rem;
  }
  .ops-table thead,
  .ops-table tbody,
  .ops-table tr {
    display: table-row-group !important;
  }
  .ops-table tr {
    display: table-row !important;
  }
  .ops-table th,
  .ops-table td {
    display: table-cell !important;
    padding: 0.45rem 0.5rem;
  }

  .ops-boss-grid,
  .ops-boss-mid {
    grid-template-columns: 1fr !important;
  }
  .ops-bk-row,
  .ops-stat-row--compact {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
  .ops-boss-actions,
  .ops-accounts-toolbar,
  .ops-profile-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }
  .ops-boss-actions .btn,
  .ops-accounts-toolbar .btn {
    width: 100%;
  }
  .ops-analytics-panel {
    padding: 0.75rem;
  }
  .ops-analytics-panel .chart-area {
    min-height: 200px;
  }
  .ops-va-acc-row-inner {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }
  .ops-va-acc-row-actions {
    flex-wrap: wrap;
    gap: 0.35rem;
  }
  .ops-va-acc-row-actions .btn {
    flex: 1 1 calc(50% - 0.35rem);
  }
  .ops-timer-row,
  .ops-worked-row {
    flex-direction: column;
    align-items: stretch;
  }
  .ops-profile-grid {
    grid-template-columns: 1fr !important;
  }

  /* --- Tasks: list OR detail (see tasks.js) --- */
  .tasks-root--mobile .tasks-layout {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }
  .tasks-root--mobile .tasks-list {
    max-height: none;
    overflow: visible;
  }
  .tasks-root--mobile .tasks-detail-wrap {
    display: none;
  }
  .tasks-root--mobile.tasks-root--detail-open .tasks-list-wrap {
    display: none;
  }
  .tasks-root--mobile.tasks-root--detail-open .tasks-detail-wrap {
    display: block;
  }
  .tasks-mobile-back {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-bottom: 0.65rem;
    font: inherit;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--accent);
    background: none;
    border: none;
    padding: 0.25rem 0;
    cursor: pointer;
  }
  .tasks-dashboard-head {
    flex-direction: column;
    align-items: flex-start;
  }
  .tasks-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.45rem;
  }
  .tasks-stat-num {
    font-size: 1.35rem;
  }
  .tasks-date-filters {
    gap: 0.35rem;
  }
  .tasks-filter-btn {
    flex: 1 1 calc(50% - 0.35rem);
    text-align: center;
    padding: 0.45rem 0.5rem;
  }
  .tasks-custom-range {
    flex-direction: column;
    align-items: stretch;
  }
  .tasks-custom-range label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
  }
  .tasks-custom-range input[type="date"] {
    margin-left: 0;
    width: 100%;
  }
  .tasks-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .tasks-toolbar .btn-primary {
    width: 100%;
  }
  .task-detail-status {
    max-width: none;
  }
  .task-detail-status select,
  .task-comment-form textarea {
    width: 100%;
  }

  /* --- Connections --- */
  .conn-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .conn-search {
    max-width: none;
    width: 100%;
    min-width: 0;
  }
  .conn-toolbar-actions {
    width: 100%;
  }
  .conn-toolbar-actions .btn {
    flex: 1 1 calc(50% - 0.25rem);
    text-align: center;
  }
  .conn-model-tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.15rem;
  }
  .conn-model-tab {
    flex-shrink: 0;
  }
  .conn-layout {
    grid-template-columns: 1fr;
  }
  .conn-canvas-host {
    height: min(42vh, 360px);
    min-height: 260px;
  }
  .conn-sidebar {
    border-radius: 12px;
    max-height: none;
  }
  .conn-sidebar-head {
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  /* --- Account vault --- */
  .av-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .av-toolbar-actions {
    width: 100%;
    flex-direction: column;
  }
  .av-toolbar-actions .btn {
    width: 100%;
  }
  .av-tabs,
  .av-status-tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.15rem;
  }
  .av-status-bar {
    flex-direction: column;
    align-items: stretch;
  }
  .av-model-filter {
    margin-left: 0;
    width: 100%;
  }
  .av-model-filter-select {
    flex: 1;
    max-width: none;
    width: 100%;
  }
  .av-tab,
  .av-status-tab {
    flex-shrink: 0;
  }
  .av-card-head {
    gap: 0.65rem;
  }
  .av-actions {
    flex-wrap: wrap;
    gap: 0.35rem;
  }
  .av-actions .btn {
    flex: 1 1 calc(50% - 0.35rem);
    text-align: center;
  }
  .av-card-creds {
    grid-template-columns: 1fr !important;
  }
  .av-field-val,
  .av-copyable-text {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .av-secret {
    flex-wrap: wrap;
    gap: 0.25rem;
  }

  /* --- Weekly tracking --- */
  .wt-toolbar {
    gap: 0.65rem;
  }
  .wt-body {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  .wt-table {
    min-width: 640px;
    font-size: 0.78rem;
  }
  .wt-sheet-kpis,
  .wt-sheet-convs {
    grid-template-columns: 1fr 1fr !important;
  }

  /* --- Soul recreate --- */
  .sr-grid {
    grid-template-columns: 1fr !important;
  }
  .sr-row {
    grid-template-columns: 1fr !important;
  }
  .sr-item {
    grid-template-columns: 64px 64px 1fr !important;
  }
  .sr-item-actions {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
  }
  .sr-item-actions .btn {
    flex: 1 1 calc(50% - 0.25rem);
  }
  .sr-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .sr-actions .btn {
    width: 100%;
  }
  .sr-tg-add {
    flex-direction: column;
    align-items: stretch;
  }

  /* --- Copy / secret helpers --- */
  .av-copy,
  .av-reveal,
  .av-copyable .btn {
    min-width: 44px;
  }
  .ops-acc-copy-btn,
  .ops-acc-copy-hit {
    min-height: 44px;
  }
  .ops-acc-copy-btn {
    min-width: 72px;
  }
  .ops-acc-avatar-wrap {
    width: 72px;
  }
  .ops-acc-avatar-dl {
    min-height: 44px;
    font-size: 0.58rem;
  }
  .ops-acc-card {
    grid-template-columns: 72px 1fr;
    gap: 0.75rem;
  }
  .ops-acc-avatar {
    width: 72px;
    height: 72px;
  }
}

@media (max-width: 480px) {
  .ops-stat-row,
  .ops-stat-row--compact,
  .ops-bk-row {
    grid-template-columns: 1fr 1fr !important;
  }
  .ops-analytics-panel .chart-area {
    min-height: 180px;
  }
  .tasks-stats {
    grid-template-columns: 1fr;
  }
  .tasks-filter-btn {
    flex: 1 1 100%;
  }
  .conn-toolbar-actions .btn,
  .av-actions .btn,
  .sr-item-actions .btn {
    flex: 1 1 100%;
  }
  .app-topbar h1 {
    font-size: 0.98rem;
  }
}
