/* Mobile-only presentation layer. Desktop computed styles are intentionally untouched. */
.mobile-only { display: none !important; }

@media (max-width: 767px) {
  :root {
    --mobile-page-x: 16px;
    --mobile-gap: 12px;
    --mobile-control-h: 44px;
    --mobile-header-h: 60px;
    --mobile-radius: 10px;
    --mobile-shadow: 0 8px 28px rgba(15, 23, 42, .14);
  }

  html { overflow-x: hidden; scroll-padding-top: calc(var(--mobile-header-h) + 12px); }
  body { min-width: 0; overflow-x: hidden; font-size: 16px; line-height: 1.4; }
  body.mobile-layer-open { position: fixed; right: 0; left: 0; width: 100%; overflow: hidden; }
  .mobile-only { display: flex !important; }
  [hidden] { display: none !important; }

  .topbar, .tabs { display: none !important; }
  .mobile-header {
    position: sticky; top: 0; z-index: 1500; align-items: center; gap: 8px;
    width: 100%; min-height: var(--mobile-header-h); padding: 8px max(var(--mobile-page-x), env(safe-area-inset-right)) 8px max(var(--mobile-page-x), env(safe-area-inset-left));
    border-bottom: 1px solid var(--line); background: rgba(255,255,255,.96); box-shadow: 0 2px 12px rgba(15,23,42,.06); backdrop-filter: blur(10px);
  }
  .mobile-brand { display: grid; min-width: 0; line-height: 1.1; }
  .mobile-brand > span { color: var(--ink); font-size: 18px; font-weight: 850; }
  .mobile-brand small { display: block; max-width: 115px; overflow: hidden; color: var(--muted); font-size: 10px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
  .mobile-current-section { min-width: 0; margin-left: auto; overflow: hidden; color: var(--muted); font-size: 13px; font-weight: 750; text-overflow: ellipsis; white-space: nowrap; }
  .mobile-menu-button, .mobile-nav-close {
    flex: 0 0 44px; align-items: center; justify-content: center; width: 44px; min-width: 44px; height: 44px; min-height: 44px; padding: 0; border: 1px solid var(--line); border-radius: 9px; background: #fff; color: var(--ink);
  }
  .mobile-menu-button { display: grid; align-content: center; gap: 4px; }
  .mobile-menu-button span { display: block; width: 20px; height: 2px; border-radius: 2px; background: currentColor; }
  .mobile-nav-backdrop { position: fixed; inset: 0; z-index: 1990; background: rgba(15,23,42,.4); }
  .mobile-nav {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 2000; flex-direction: column; width: min(360px, 100vw); padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); background: #fff; box-shadow: -12px 0 40px rgba(15,23,42,.2);
  }
  .mobile-nav > header { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 64px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
  .mobile-nav > header div { display: grid; min-width: 0; }
  .mobile-nav > header strong { font-size: 20px; }
  .mobile-nav > header small { overflow-wrap: anywhere; color: var(--muted); font-size: 12px; }
  .mobile-nav-close { display: inline-flex; font-size: 28px; font-weight: 400; }
  .mobile-nav-freshness { padding: 8px 16px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: 12px; font-weight: 650; }
  .mobile-nav > nav { display: grid; flex: 1 1 auto; align-content: start; overflow-y: auto; overscroll-behavior: contain; padding: 8px; }
  .mobile-nav > nav > a, .mobile-nav > nav button, .mobile-logout {
    display: flex; align-items: center; justify-content: space-between; width: 100%; min-height: 48px; padding: 10px 12px; border: 0; border-radius: 8px; background: transparent; color: var(--ink); font: inherit; font-size: 15px; font-weight: 720; text-align: left; text-decoration: none;
  }
  .mobile-nav > nav > a.active { position: relative; background: #eaf2ff; color: var(--accent); }
  .mobile-nav > nav > a.active::before { position: absolute; top: 8px; bottom: 8px; left: 0; width: 3px; border-radius: 3px; background: var(--accent); content: ""; }
  .mobile-upmarket-links { display: grid; margin: 0 0 4px 18px; padding-left: 8px; border-left: 1px solid var(--line); }
  .mobile-upmarket-links button { min-height: 44px !important; color: var(--muted) !important; font-size: 14px !important; }
  .mobile-logout { flex: 0 0 auto; margin: 8px 16px; width: calc(100% - 32px); border: 1px solid #fecaca; color: var(--red); }

  main { min-height: calc(100dvh - var(--mobile-header-h)); }
  .workspace { width: 100%; min-width: 0; padding: 12px max(var(--mobile-page-x), env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(var(--mobile-page-x), env(safe-area-inset-left)); }
  .tab-panel, .tab-panel.active { min-width: 0; max-width: 100%; }
  .status { margin: 0 0 12px; padding: 10px 12px; border-radius: var(--mobile-radius); font-size: 13px; overflow-wrap: anywhere; }
  h1 { font-size: 22px; line-height: 1.2; }
  h2, .section-heading h2 { font-size: 18px; line-height: 1.25; }
  h3 { font-size: 16px; line-height: 1.3; }
  p, label, input, select, textarea, button { max-width: 100%; }
  p, span, strong, td, th { overflow-wrap: anywhere; }

  button, a.button, input[type="button"], input[type="submit"] { min-height: var(--mobile-control-h); border-radius: 8px; font-size: 15px; touch-action: manipulation; }
  button:active { transform: scale(.98); }
  button:disabled { opacity: .55; transform: none; }
  input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]), select, textarea {
    width: 100%; min-width: 0; min-height: var(--mobile-control-h); padding: 10px 12px; border-radius: 8px; font-size: 16px;
  }
  input[type="checkbox"], input[type="radio"] { width: 20px; min-width: 20px; height: 20px; }
  .checkbox-field, .alerts-compact-toggle, .task-checkline, .prices-check { align-items: center; min-height: 44px; gap: 10px; }
  :focus-visible { outline: 3px solid rgba(45,108,223,.3); outline-offset: 2px; }

  .panel, .metrics, .db-strip, .filters, .table-wrap, .dashboard-block, .alerts-panel, .warehouse-overview, .warehouse-table-panel, .status, .chart-panel, .price-compare-panel {
    max-width: 100%; border-radius: var(--mobile-radius); box-shadow: none;
  }
  .panel, .dashboard-block, .alerts-panel, .warehouse-table-panel, .chart-panel, .price-compare-panel { padding: 12px; }
  .dashboard-block, .panel, .chart-panel, .alerts-panel { margin-bottom: 12px; }
  .section-heading, .panel-head, .dashboard-title-row, .dashboard-indicator-heading, .alerts-heading-actions, .alerts-list-toolbar {
    align-items: stretch !important; flex-direction: column !important; gap: 10px !important;
  }
  .section-heading > button, .dashboard-indicator-heading > button, .dashboard-problem-show-all, [data-open-alerts], [data-open-price-compare] { width: 100%; }

  .period-form, .period-form-inline, .graphs-form {
    display: grid; grid-template-columns: minmax(0,1fr); gap: 10px; align-items: stretch; width: 100%; margin: 0 0 12px; padding: 12px;
  }
  .period-form-inline { margin-top: 10px; padding: 0; }
  .period-form label, .period-form button, .date-range-field, .date-range-button, .graphs-form > label, .graphs-form button { width: 100%; min-width: 0; margin: 0 !important; }
  .period-form > [data-download-dashboard], .period-form > [data-download-sales], .period-form > [data-download-table] { margin-left: 0; }

  .mobile-filter-trigger {
    align-items: center; justify-content: space-between; gap: 8px; width: 100%; margin: 0 0 10px; padding: 0 12px; border: 1px solid #cfe0f6; background: #eef6ff; color: var(--accent); font-weight: 800;
  }
  .mobile-filter-trigger b { display: grid; place-items: center; min-width: 24px; height: 24px; margin-left: auto; padding: 0 6px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 12px; }
  .mobile-filter-trigger i { font-style: normal; }
  .mobile-filter-panel { display: none !important; }
  .mobile-filter-panel.mobile-filter-open { display: grid !important; grid-template-columns: minmax(0,1fr) !important; gap: 10px !important; width: 100%; margin-bottom: 12px; padding: 12px; border: 1px solid var(--line); border-radius: var(--mobile-radius); background: #fff; }
  .mobile-filter-panel > label, .mobile-filter-panel > button { width: 100%; min-width: 0; }

  .dashboard-kpi-grid, .dashboard-dynamics-grid, .dashboard-attention-grid, .dashboard-recommendations-grid, .dashboard-events-grid, .dashboard-upmarket-grid, .dashboard-price-compare-grid, .price-kpi-grid, .metrics, .warehouse-cards-grid, .supply-review-cards, .supply-excel-grid {
    grid-template-columns: repeat(2, minmax(0,1fr)) !important; gap: 10px !important;
  }
  .dashboard-indicator-card, .dashboard-kpi-card, .metric, .price-kpi-card { min-width: 0; padding: 12px; }
  .dashboard-kpi-value, .metric strong { font-size: clamp(20px, 6vw, 26px); line-height: 1.15; white-space: normal; }
  .dashboard-attention-grid, .dashboard-recommendations-grid, .dashboard-events-grid, .dashboard-price-compare-grid, .dashboard-product-grid { grid-template-columns: minmax(0,1fr) !important; }
  .dashboard-problem-layout, .dashboard-stock-columns, .dashboard-warehouse-layout { grid-template-columns: minmax(0,1fr) !important; }
  .dashboard-top-control { width: 100%; }

  .dashboard-chart-metrics { display: grid !important; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; }
  .dashboard-chart-metrics label { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; }
  .dashboard-sales-chart, .brand-dynamics-chart, .warehouse-dynamics-chart, .chart-canvas-wrap, .graph-chart-wrap { position: relative; width: 100%; min-width: 0; min-height: 280px; overflow: hidden; }
  canvas, svg { max-width: 100%; }
  .dashboard-sales-svg { min-width: 0 !important; width: 100% !important; }

  .table-wrap, .warehouse-dynamics-table-wrap, .prices-table-wrap, .upmarket-generic-wrap, .purchase-contract-table, .supply-products-table, .supply-plan-table {
    position: relative; display: block; width: 100%; max-width: 100%; overflow-x: auto !important; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch;
  }
  .mobile-table-hint { position: sticky; left: 0; align-items: center; width: max-content; max-width: calc(100vw - 56px); margin: 0 0 8px; padding: 5px 8px; border-radius: 6px; background: #eef6ff; color: var(--accent); font-size: 11px; font-weight: 750; }
  table { font-size: 13px; }
  th, td { min-height: 44px; }

  /* Product-heavy reports become readable cards; detailed operational tables keep local scrolling. */
  #productsTab .products-table-wrap table, #profitabilityTab .profitability-table-wrap table, #graphsTab .graph-products-wrap table { min-width: 0 !important; width: 100%; }
  #productsTab .products-table-wrap thead, #profitabilityTab .profitability-table-wrap thead, #graphsTab .graph-products-wrap thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  #productsTab .products-table-wrap tbody, #profitabilityTab .profitability-table-wrap tbody, #graphsTab .graph-products-wrap tbody { display: grid; gap: 10px; }
  #productsTab .products-table-wrap tr, #profitabilityTab .profitability-table-wrap tr, #graphsTab .graph-products-wrap tr {
    display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 0 12px; width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
  }
  #productsTab .products-table-wrap td, #profitabilityTab .profitability-table-wrap td, #graphsTab .graph-products-wrap td {
    display: grid; align-content: start; min-width: 0; padding: 8px 0; border: 0; border-bottom: 1px solid #edf1f6; white-space: normal;
  }
  #productsTab .products-table-wrap td::before, #profitabilityTab .profitability-table-wrap td::before, #graphsTab .graph-products-wrap td::before {
    display: block; margin-bottom: 3px; color: var(--muted); font-size: 11px; font-weight: 750; content: attr(data-label);
  }
  #productsTab .products-table-wrap td:first-child, #profitabilityTab .profitability-table-wrap td:first-child, #graphsTab .graph-products-wrap td:first-child { grid-column: 1 / -1; }
  .product-table-cell { grid-template-columns: 56px minmax(0,1fr) !important; min-width: 0; }
  .product-table-cell img { width: 56px !important; height: 56px !important; object-fit: cover; }

  .brands-grid, .price-sheet-grid, .task-list, .alerts-list, .supply-warehouse-grid { grid-template-columns: minmax(0,1fr) !important; }
  .brand-card, .price-product-card, .alert-card, .task-card, .supply-warehouse-card { min-width: 0; max-width: 100%; padding: 12px; }
  .price-product-head { grid-template-columns: 64px minmax(0,1fr) !important; }
  .price-product-image { width: 64px !important; height: 64px !important; }
  .alert-card, .alert-card.grouped { grid-template-columns: minmax(0,1fr) !important; grid-template-areas: "head" "product" "values" "problems" "actions" !important; }
  .alert-values { grid-template-columns: minmax(0,1fr) !important; }
  .alert-actions, .task-table-actions { flex-wrap: wrap; min-width: 0; }

  .brand-view-toggle, .price-view-toggle, .prices-mode-toggle, .prices-goal-toggle, .task-view-toggle { display: grid !important; grid-auto-flow: column; grid-auto-columns: minmax(0,1fr); width: 100%; overflow-x: auto; }
  .brand-view-toggle button, .price-view-toggle button, .prices-mode-toggle button, .prices-goal-toggle button, .task-view-toggle button { min-width: 44px; }
  .task-quick-filters, .upmarket-nav-submenu, .supply-warehouse-filter-pills { display: flex !important; flex-wrap: nowrap !important; width: 100%; overflow-x: auto; scroll-snap-type: x proximity; }
  .task-quick-filters button, .upmarket-nav-submenu button, .supply-warehouse-filter-pills button { flex: 0 0 auto; scroll-snap-align: start; }

  .modal-backdrop { z-index: 2100; align-items: stretch; padding: 0; }
  .modal-card, .task-modal-card, .warehouse-dynamics-modal .modal-card, .upmarket-document-card, .purchase-product-detail-card, .dashboard-products-modal-card {
    display: flex; flex-direction: column; width: 100vw !important; max-width: none !important; height: 100dvh; max-height: none !important; margin: 0; padding: 0 0 env(safe-area-inset-bottom); border: 0; border-radius: 0; overflow: hidden;
  }
  .modal-card > header, .task-modal-header, .supply-modal-header {
    position: sticky; top: 0; z-index: 3; flex: 0 0 auto; min-height: 60px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: #fff;
  }
  .modal-card > header button, .task-modal-header > button, .supply-modal-header button { flex: 0 0 44px; width: 44px; min-width: 44px; height: 44px; padding: 0; }
  .modal-card > :not(header), .task-modal-body { min-width: 0; }
  .task-modal-body, .dashboard-products-modal-body { flex: 1 1 auto; overflow-y: auto; padding: 12px; }
  .task-modal-layout, .task-form-grid, .task-create-fields { grid-template-columns: minmax(0,1fr) !important; }
  .task-modal-footer, .alert-postpone-actions { position: sticky; bottom: 0; z-index: 3; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: #fff; }

  .supply-modal[aria-hidden="false"], .purchase-modal[aria-hidden="false"] { padding: 0; }
  .supply-modal-shell, .purchase-modal-shell { width: 100vw !important; max-width: none !important; height: 100dvh !important; max-height: none !important; border-radius: 0 !important; }
  .supply-modal-body, .purchase-modal-body { min-width: 0; overflow: auto; }
  .supply-modal-actions { flex-wrap: wrap; }
  .purchase-header-add::after { content: " Добавить товар"; }
  .purchase-header-add { width: auto !important; padding: 0 10px !important; }

  #supplyPlanningTab .supply-planning-grid, .supply-check-filters, .purchase-planning-filters, .purchase-planning-toolbar, .settings-grid, .settings-form-grid { grid-template-columns: minmax(0,1fr) !important; }
  #openPurchasePlanning, #openSupplyPlanning, #recalculateAlerts, #openTaskCreate { width: 100%; }

  .loading-overlay { padding: 16px; }
  .loading-card { width: 100%; max-width: 360px; padding: 24px 16px; border-radius: 12px; }
  .login-screen { min-height: 100dvh; padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); overflow-y: auto; }
  .login-card { width: 100%; max-width: 400px; padding: 20px; }
  .login-card button { width: 100%; }

  @media (orientation: landscape) {
    .dashboard-sales-chart, .brand-dynamics-chart, .warehouse-dynamics-chart, .chart-canvas-wrap, .graph-chart-wrap { min-height: 240px; }
  }
}

@media (max-width: 374px) {
  :root { --mobile-page-x: 12px; }
  .mobile-current-section { display: none; }
  .dashboard-kpi-grid, .dashboard-dynamics-grid, .dashboard-upmarket-grid, .price-kpi-grid, .metrics, .warehouse-cards-grid, .supply-review-cards, .supply-excel-grid { grid-template-columns: minmax(0,1fr) !important; }
  #productsTab .products-table-wrap tr, #profitabilityTab .profitability-table-wrap tr, #graphsTab .graph-products-wrap tr { grid-template-columns: minmax(0,1fr); }
  #productsTab .products-table-wrap td:first-child, #profitabilityTab .profitability-table-wrap td:first-child, #graphsTab .graph-products-wrap td:first-child { grid-column: auto; }
  .task-modal-footer, .alert-postpone-actions { grid-template-columns: minmax(0,1fr); }
}

@media (max-width: 767px) and (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
