/* TB CRM — final mobile adaptive layer.
   Loaded after page/theme styles so mobile rules stay authoritative. */

:root {
  --mobile-safe-top: env(safe-area-inset-top, 0px);
  --mobile-safe-right: env(safe-area-inset-right, 0px);
  --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
  --mobile-safe-left: env(safe-area-inset-left, 0px);
  --mobile-gutter: clamp(12px, 3.8vw, 20px);
  --mobile-control-height: 48px;
  --mobile-nav-clearance: calc(78px + var(--mobile-safe-bottom));
}

html {
  min-width: 0;
  max-width: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body,
main,
.page-wrap,
.app-main {
  min-width: 0;
  max-width: 100%;
}

img,
svg,
video,
canvas {
  max-width: 100%;
}

@media (max-width: 991.98px) {
  body[data-tbos-module="crm"] {
    overscroll-behavior-x: none;
  }

  body[data-tbos-module="crm"] .page-wrap {
    width: 100%;
    padding-right: max(var(--mobile-gutter), var(--mobile-safe-right)) !important;
    padding-bottom: var(--mobile-nav-clearance) !important;
    padding-left: max(var(--mobile-gutter), var(--mobile-safe-left)) !important;
  }

  body[data-tbos-module="crm"] .app-main,
  body[data-tbos-module="crm"] .app-shell,
  body[data-tbos-module="crm"] .container,
  body[data-tbos-module="crm"] .container-fluid {
    min-width: 0;
    max-width: 100%;
  }

  body[data-tbos-module="crm"] :where(
    .page-head,
    .toolbar,
    .section-head,
    .card-header,
    .modal-header,
    .modal-footer,
    .avito-card-head,
    .avito-hero,
    .ed-hero,
    .ef-hero,
    .scx-hero,
    .order-hero
  ) {
    min-width: 0;
    flex-wrap: wrap;
  }

  body[data-tbos-module="crm"] :where(
    .grid,
    .row,
    [class$="-grid"],
    [class*="-grid "],
    [class$="-layout"],
    [class*="-layout "]
  ) > * {
    min-width: 0;
  }

  body[data-tbos-module="crm"] :where(
    button,
    .btn,
    [role="button"],
    summary,
    .avito-btn,
    .cc-primary-btn,
    .cc-secondary-btn,
    .ed-btn,
    .ef-jump
  ) {
    touch-action: manipulation;
  }

  body[data-tbos-module="crm"] :where(
    input:not([type="checkbox"]):not([type="radio"]),
    select,
    textarea
  ) {
    max-width: 100%;
  }

  body[data-tbos-module="crm"] :where(
    .table-responsive,
    .table-wrap,
    [class$="-table-wrap"],
    [class*="-table-wrap "]
  ) {
    max-width: 100%;
    overflow-x: auto !important;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  body[data-tbos-module="crm"] :where(.tabs, [class$="-tabs"], [class*="-tabs "]) {
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  body[data-tbos-module="crm"] :where(.tabs, [class$="-tabs"], [class*="-tabs "])::-webkit-scrollbar {
    display: none;
  }

  body[data-tbos-module="crm"] :where(.tabs, [class$="-tabs"], [class*="-tabs "]) > * {
    flex: 0 0 auto;
  }

  body[data-tbos-module="crm"] :where(.stars-galaxy-bg, .neon-circuit-bg, .watermark-bg) {
    display: none !important;
  }

  body[data-tbos-module="crm"] .mobile-bottom-nav {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    padding-bottom: max(.45rem, var(--mobile-safe-bottom));
  }

  body[data-tbos-module="crm"] .mobile-bottom-link {
    min-width: 0;
    min-height: 52px;
    padding: .35rem .2rem;
    touch-action: manipulation;
  }

  body[data-tbos-module="crm"] .mobile-bottom-link-primary {
    color: #fff;
    background: var(--brand);
    box-shadow: 0 8px 20px color-mix(in srgb, var(--brand) 28%, transparent);
  }

  body[data-tbos-module="crm"] .mobile-bottom-link-primary.active {
    color: #fff;
    background: var(--brand);
  }

  body[data-tbos-module="crm"] .mobile-bottom-icon,
  body[data-tbos-module="crm"] .mobile-bottom-icon .crm-nav-icon {
    display: block;
    width: 22px;
    height: 22px;
  }
}

@media (max-width: 767.98px) {
  body[data-tbos-module="crm"] {
    font-size: 16px;
  }

  body[data-tbos-module="crm"] .app-nav {
    min-height: calc(58px + var(--mobile-safe-top));
    padding-top: var(--mobile-safe-top);
  }

  body[data-tbos-module="crm"] .page-wrap {
    min-height: 100dvh;
    padding-top: calc(58px + var(--mobile-safe-top)) !important;
  }

  body[data-tbos-module="crm"] :where(
    input:not([type="checkbox"]):not([type="radio"]),
    select,
    textarea,
    .form-control,
    .form-select
  ) {
    min-height: var(--mobile-control-height);
    border-radius: 12px;
    font-size: 16px !important;
  }

  body[data-tbos-module="crm"] textarea {
    min-height: 112px;
  }

  body[data-tbos-module="crm"] :where(
    button,
    .btn,
    [role="button"],
    summary,
    .avito-btn,
    .cc-primary-btn,
    .cc-secondary-btn,
    .ed-btn
  ) {
    min-height: 44px;
  }

  body[data-tbos-module="crm"] :where(a, button, .btn, [role="button"]):active {
    opacity: .78;
  }

  body[data-tbos-module="crm"] :where(
    .section-block,
    .page-card,
    .form-card,
    .table-card,
    .metric-card,
    .avito-card,
    .avito-panel,
    .ed-panel,
    .ef-pane,
    .scx-card,
    .block-card
  ) {
    max-width: 100%;
    border-radius: 16px !important;
  }

  body[data-tbos-module="crm"] :where(
    .page-head,
    .toolbar,
    .section-head,
    .avito-card-head,
    .avito-hero-actions,
    .ed-hero-actions,
    .ef-jumps,
    .form-actions-row,
    .modal-footer
  ) {
    align-items: stretch !important;
    gap: 8px !important;
  }

  body[data-tbos-module="crm"] :where(
    .page-head,
    .toolbar,
    .avito-card-head,
    .avito-hero-actions,
    .ed-hero-actions,
    .form-actions-row
  ) > :where(.btn, button, form) {
    max-width: 100%;
  }

  body[data-tbos-module="crm"] :where(
    .modal-dialog,
    .modal-dialog.modal-sm,
    .modal-dialog.modal-lg,
    .modal-dialog.modal-xl
  ) {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 100dvh;
    margin: 0 !important;
  }

  body[data-tbos-module="crm"] .modal-content {
    min-height: 100dvh;
    border: 0;
    border-radius: 0 !important;
  }

  body[data-tbos-module="crm"] .modal-body {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: var(--mobile-gutter);
  }

  body[data-tbos-module="crm"] .modal-footer {
    position: sticky;
    bottom: 0;
    z-index: 2;
    padding-right: max(var(--mobile-gutter), var(--mobile-safe-right));
    padding-bottom: max(var(--mobile-gutter), var(--mobile-safe-bottom));
    padding-left: max(var(--mobile-gutter), var(--mobile-safe-left));
    background: var(--surface-strong);
  }

  body[data-tbos-module="crm"] .modal-footer > :where(.btn, button) {
    flex: 1 1 140px;
  }

  body[data-tbos-module="crm"] :where(
    .avito-grid,
    .avito-kpis,
    .avito-form-grid,
    .avito-settings-layout,
    .avito-service-grid,
    .avito-review-grid,
    .an-grid,
    .an-2col,
    .an-filters-grid,
    .fn-grid,
    .ed-layout,
    .ed-stats,
    .ef-pane,
    .ef-master-pane,
    .ef-finance-pane,
    .service-center-grid,
    .client-layout
  ) {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  body[data-tbos-module="crm"] :where(.an-kpi-grid, .avito-bid-metrics, .avito-service-kpis) {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  body[data-tbos-module="crm"] :where(.an-table, .avito-performance-table) {
    min-width: 720px;
  }

  body[data-tbos-module="crm"] :where(.flash-stack, .toast-stack, .cc-toast-stack) {
    right: max(10px, var(--mobile-safe-right)) !important;
    bottom: calc(var(--mobile-nav-clearance) + 8px) !important;
    left: max(10px, var(--mobile-safe-left)) !important;
    width: auto !important;
    max-width: none !important;
  }

  body[data-tbos-module="crm"] :where(.flash-banner, .toast, [role="status"]) {
    overflow-wrap: anywhere;
  }

  body[data-tbos-module="crm"] :where(h1, h2, h3, p, dd, td, .text-muted, .avito-muted) {
    overflow-wrap: anywhere;
  }

  /* Orders: keep the operational list readable instead of squeezing table cells. */
  body[data-tbos-module="crm"] .orders-page {
    width: 100%;
    padding: 8px !important;
    gap: 10px !important;
  }

  body[data-tbos-module="crm"] .orders-page :where(.orders-filters, .orders-filters-v2) {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px !important;
    padding: 10px !important;
  }

  body[data-tbos-module="crm"] .orders-page .orders-search-box {
    grid-column: 1 / -1;
    width: 100%;
  }

  body[data-tbos-module="crm"] .orders-page .order-row-card {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto !important;
    gap: 8px 12px !important;
    margin-bottom: 8px;
    padding: 12px !important;
    border: 1px solid var(--op-border, var(--border));
    border-left-width: 4px;
    border-radius: 14px;
  }

  body[data-tbos-module="crm"] .orders-page .orders-check-cell {
    display: none !important;
  }

  body[data-tbos-module="crm"] .orders-page .orders-col {
    min-width: 0;
    padding: 0 !important;
  }

  body[data-tbos-module="crm"] .orders-page .orders-col[data-label]::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 2px;
    color: var(--op-muted, var(--muted));
    font-size: 10px;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
  }

  body[data-tbos-module="crm"] .orders-page .orders-col[data-label="Клиент"],
  body[data-tbos-module="crm"] .orders-page .orders-col[data-label="Адрес"],
  body[data-tbos-module="crm"] .orders-page .orders-col[data-label="Работы"] {
    grid-column: 1 / -1;
  }

  body[data-tbos-module="crm"] .orders-page .orders-col[data-label="Тип"],
  body[data-tbos-module="crm"] .orders-page .orders-col[data-label="Город"] {
    display: none !important;
  }

  /* Service center: one compact command header and swipeable tabs. */
  body[data-tbos-module="crm"] .scm-hdr {
    height: auto !important;
    min-height: 56px;
    padding: 8px 10px !important;
    gap: 8px !important;
  }

  body[data-tbos-module="crm"] .scm-hdr-sep,
  body[data-tbos-module="crm"] .scm-hdr-pills,
  body[data-tbos-module="crm"] .scm-brand-sub {
    display: none !important;
  }

  body[data-tbos-module="crm"] .scm-hdr-brand {
    min-width: 0;
    font-size: 14px !important;
  }

  body[data-tbos-module="crm"] .scm-hdr-acts {
    min-width: 0;
    margin-left: auto;
    overflow-x: auto;
    scrollbar-width: none;
  }

  body[data-tbos-module="crm"] .scm-hdr-acts::-webkit-scrollbar { display: none; }

  body[data-tbos-module="crm"] .scm-tabs {
    position: sticky;
    top: calc(58px + var(--mobile-safe-top));
    z-index: 25;
    padding: 4px 8px !important;
    gap: 4px !important;
    background: var(--surface-strong, var(--surface));
  }

  body[data-tbos-module="crm"] .scm-tabs > * {
    flex: 0 0 auto;
    min-height: 42px;
  }

  /* Settings: categories stay reachable without a tall wall of buttons. */
  body[data-tbos-module="crm"] .cc-sidebar-nav {
    display: flex !important;
    grid-template-columns: none !important;
    gap: 6px;
    padding-bottom: 4px;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
  }

  body[data-tbos-module="crm"] .cc-sidebar-nav::-webkit-scrollbar { display: none; }
  body[data-tbos-module="crm"] .cc-sidebar-nav > * {
    flex: 0 0 auto;
    min-width: 132px;
    scroll-snap-align: start;
  }

  body[data-tbos-module="crm"] :where(.cc-stage, .settings-page-stage) {
    padding: 12px !important;
  }

  /* Marketing: metrics and attribution remain legible on a narrow phone. */
  body[data-tbos-module="crm"] .rp-tabs {
    overflow-x: auto;
    scrollbar-width: none;
  }

  body[data-tbos-module="crm"] .rp-tabs::-webkit-scrollbar { display: none; }
  body[data-tbos-module="crm"] .rp-tab { flex: 0 0 auto; min-height: 44px; }
  body[data-tbos-module="crm"] .rp-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  body[data-tbos-module="crm"] .rp-stat {
    min-width: 0;
    padding: 12px;
  }

  body[data-tbos-module="crm"] .rp-panel { padding: 12px; }
  body[data-tbos-module="crm"] .rp-panel:has(.rp-table) {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
  }
  body[data-tbos-module="crm"] .rp-table { min-width: 680px; }
  body[data-tbos-module="crm"] .rp-acc { align-items: flex-start; }
}

@media (max-width: 479.98px) {
  body[data-tbos-module="crm"] .mobile-bottom-text {
    font-size: 10px;
  }

  body[data-tbos-module="crm"] :where(.an-kpi-grid, .avito-bid-metrics, .avito-service-kpis) {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  body[data-tbos-module="crm"] :where(.section-block, .toolbar, .avito-card, .avito-panel, .ed-panel, .ef-pane) {
    padding: 12px !important;
  }

  body[data-tbos-module="crm"] .rp-stats { grid-template-columns: minmax(0, 1fr); }
  body[data-tbos-module="crm"] .rp-acc { display: grid; }
  body[data-tbos-module="crm"] .rp-acc > :last-child { text-align: left !important; }
}

/* Mobile field workflow: order card becomes a focused one-column screen. */
@media (max-width: 767.98px) {
  body[data-tbos-module="crm"]:has(.order-detail-redesign) .page-wrap {
    padding-right: max(10px, var(--mobile-safe-right)) !important;
    padding-left: max(10px, var(--mobile-safe-left)) !important;
  }

  body[data-tbos-module="crm"] .order-detail-redesign :where(.page-shell, .order-main-grid, .order-main-column, .order-side-column) {
    width: 100%;
    min-width: 0;
  }

  body[data-tbos-module="crm"] .order-detail-redesign .order-hero { gap: 12px; padding: 8px 0 12px; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-topbar-left {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr);
    align-items: start;
    width: 100%;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .order-back-btn {
    width: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0 !important;
    overflow: hidden;
    color: transparent;
    font-size: 0;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .order-back-btn::before { content: "←"; color: var(--c-tx); font-size: 20px; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-title-line { align-items: flex-start; gap: 6px; }
  body[data-tbos-module="crm"] .order-detail-redesign .order-hero h1 { flex: 1 0 100%; font-size: 1.35rem; }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.status-chip-lg, .review-type-chip) { min-height: 32px; padding: 6px 10px; }

  body[data-tbos-module="crm"] .order-detail-redesign .od-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 8px;
    overflow: visible;
    border-radius: 14px;
    background: var(--c-s1);
  }
  body[data-tbos-module="crm"] .order-detail-redesign .od-toolbar-primary {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 6px;
    min-width: 0;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .od-toolbar-secondary { min-width: 44px; }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.od-next-action, .od-tbtn) {
    min-height: 44px !important;
    justify-content: center;
    border-radius: 10px;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .od-next-action {
    min-width: 0;
    padding-inline: 10px;
    white-space: normal;
    text-align: center;
  }

  body[data-tbos-module="crm"] .order-detail-redesign .od-command-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-command-cell { min-height: 72px; padding: 11px 12px; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-command-cell.is-action { grid-column: auto; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-command-value {
    overflow: visible;
    text-overflow: clip;
    white-space: normal;
    overflow-wrap: anywhere;
    font-size: 13px;
  }

  body[data-tbos-module="crm"] .order-detail-redesign .od-progress {
    grid-template-columns: repeat(5, minmax(72px, 1fr));
    margin-inline: -2px;
    padding: 10px 4px 8px;
    overflow-x: auto;
    scroll-snap-type: x proximity;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .od-progress-step { min-width: 72px; scroll-snap-align: center; }
  body[data-tbos-module="crm"] .order-detail-redesign .od-progress-context { align-items: flex-start; font-size: 13px; }

  body[data-tbos-module="crm"] .order-detail-redesign .od-tabs {
    position: sticky;
    top: 0;
    z-index: 30;
    margin-inline: -2px;
    padding: 5px;
    overflow-x: auto;
    background: var(--c-s1);
    box-shadow: 0 6px 18px color-mix(in srgb, var(--c-bg) 70%, transparent);
  }
  body[data-tbos-module="crm"] .order-detail-redesign .od-tab { min-width: max-content; min-height: 44px; padding-inline: 14px; }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.order-panel, .block-card, .sidebar-card) {
    border-radius: 14px;
    background: var(--c-s1);
  }
  body[data-tbos-module="crm"] .order-detail-redesign .sidebar-card { position: static; max-height: none; overflow: visible; }

  body[data-tbos-module="crm"] .order-detail-redesign :where(.report-modal-shell, .sd-modal-shell) { align-items: flex-end; padding: 0; }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.report-modal-dialog, .sd-modal-dialog) {
    width: 100%;
    max-height: calc(94dvh - var(--mobile-safe-top));
    border-radius: 22px 22px 0 0;
    padding-bottom: var(--mobile-safe-bottom);
  }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.form-control, .form-select, input, textarea, select) {
    min-height: 48px;
    font-size: 16px !important;
  }
  body[data-tbos-module="crm"] .order-detail-redesign .history-comment-input-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 48px 48px;
    gap: 8px;
    width: 100%;
  }
  body[data-tbos-module="crm"] .order-detail-redesign :where(.history-comment-file-btn, .history-comment-input-row button) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    min-height: 48px;
  }
}

/* Marketplace pages use their own shell but share touch and safe-area rules. */
@media (max-width: 767.98px) {
  body[data-tbos-module="marketplace"] {
    min-width: 0;
    max-width: 100%;
    padding-bottom: calc(64px + var(--mobile-safe-bottom));
    overflow-x: clip;
    font-size: 16px;
  }

  body[data-tbos-module="marketplace"] :where(button, .market-btn, [role="button"], summary) {
    min-height: 44px;
    touch-action: manipulation;
  }

  body[data-tbos-module="marketplace"] :where(input, select, textarea) {
    min-height: 48px;
    max-width: 100%;
    font-size: 16px !important;
  }

  body[data-tbos-module="marketplace"] :where(
    .market-shell,
    .market-section,
    .market-header,
    .market-header-compact
  ) {
    max-width: 100%;
    padding-right: max(var(--mobile-gutter), var(--mobile-safe-right)) !important;
    padding-left: max(var(--mobile-gutter), var(--mobile-safe-left)) !important;
  }

  body[data-tbos-module="marketplace"] :where(
    [class$="-grid"],
    [class*="-grid "],
    [class$="-layout"],
    [class*="-layout "]
  ) > * {
    min-width: 0;
  }

  body[data-tbos-module="marketplace"] .tbm-home-hero-clean {
    width: 100%;
    min-height: 0 !important;
    aspect-ratio: 1672 / 941;
    margin-top: 12px;
    padding: 0 !important;
    border-radius: 22px;
    background-position: center !important;
    background-size: cover !important;
  }

  body[data-tbos-module="marketplace"] .tbm-home-hero-clean__copy {
    display: none !important;
  }

  body[data-tbos-module="marketplace"] :where(.market-bg, .market-ai-concierge) {
    animation: none !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .mob-nav-drawer-panel,
  .mobile-bottom-nav,
  .mob-market-bottom-nav {
    animation: none !important;
    transition: none !important;
  }
}

@media print {
  .mobile-bottom-nav,
  .mob-bottom-nav,
  .mob-nav-drawer,
  .mob-market-bottom-nav {
    display: none !important;
  }
}
