/* Mobile-first product layer. Loaded last and limited to phone/tablet widths. */

@media (max-width: 760px) {
  :root {
    --mobile-header-height: 64px;
    --mobile-nav-height: 72px;
    --mobile-page-gutter: 14px;
  }

  html {
    min-width: 320px;
    scroll-padding-top: calc(var(--mobile-header-height) + 12px);
    background: #f6f7f9;
  }

  body {
    min-width: 320px;
    min-height: 100svh;
    padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    background: #f6f7f9;
    overscroll-behavior-y: none;
  }

  .app-shell {
    display: block;
    min-height: 100svh;
  }

  /* Compact app header and native-style bottom navigation. */
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 90;
    width: 100%;
    height: var(--mobile-header-height);
    min-height: var(--mobile-header-height);
    padding: 10px var(--mobile-page-gutter);
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 12px;
    overflow: visible;
    border: 0;
    border-bottom: 1px solid rgba(255,255,255,.08);
    background: rgba(17,24,39,.96);
    box-shadow: 0 8px 24px rgba(15,23,42,.12);
    backdrop-filter: blur(18px);
  }

  .sidebar::after,
  .workspace-card,
  .sidebar-footer {
    display: none !important;
  }

  .brand {
    min-height: 44px;
    padding: 0;
    flex: 1;
    gap: 10px;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    font-size: 15px;
  }

  .brand strong { font-size: 15px; }
  .brand small {
    margin-top: 1px;
    color: #aeb5c2;
    font-size: 10px;
    letter-spacing: 0;
    text-transform: none;
  }

  .nav-list {
    position: fixed;
    z-index: 220;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    padding: 7px max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 3px;
    overflow-x: auto;
    overflow-y: hidden;
    border-top: 1px solid #e5e7eb;
    background: rgba(255,255,255,.97);
    box-shadow: 0 -10px 30px rgba(15,23,42,.09);
    backdrop-filter: blur(20px);
    scrollbar-width: none;
  }

  .nav-list::-webkit-scrollbar { display: none; }

  .nav-item {
    position: relative;
    flex: 1 0 62px;
    width: auto;
    min-width: 62px;
    max-width: 94px;
    min-height: 56px;
    padding: 6px 4px 5px;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 22px 14px;
    place-items: center;
    align-content: center;
    gap: 3px;
    border-radius: 12px;
    color: #667085;
    background: transparent;
    box-shadow: none;
    text-align: center;
    transform: none !important;
  }

  .nav-item:not(:disabled):hover {
    color: #4f46e5;
    background: #f5f7ff;
    transform: none;
  }

  .nav-item.active {
    color: #4338ca;
    background: #eef2ff;
    box-shadow: none;
  }

  .nav-item > span:first-child {
    display: block;
    font-size: 19px;
    line-height: 22px;
  }

  .nav-item > span:nth-child(2) { display: none; }
  .nav-item::after {
    content: attr(data-mobile-label);
    display: block;
    max-width: 100%;
    overflow: hidden;
    font-size: 10px;
    font-weight: 700;
    line-height: 14px;
    letter-spacing: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav-item em {
    position: absolute;
    top: 3px;
    right: 5px;
    min-width: 18px;
    max-width: 34px;
    padding: 2px 4px;
    overflow: hidden;
    border: 1px solid #e0e4ff;
    color: #4f46e5;
    background: #f5f7ff;
    font-size: 9px;
    line-height: 12px;
    letter-spacing: 0;
    text-overflow: ellipsis;
    text-transform: none;
    white-space: nowrap;
  }

  .nav-item.mobile-coming-soon em { display: none; }
  .nav-item:disabled { opacity: .5; }
  .admin-nav-item { margin-top: 0; border-top: 0; }

  /* Page frame */
  .app-main {
    width: 100%;
    max-width: none;
    padding: 18px var(--mobile-page-gutter) calc(30px + var(--mobile-nav-height) + env(safe-area-inset-bottom));
  }

  .topbar {
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0,1fr) auto;
    align-items: start;
    gap: 8px 10px;
  }

  .topbar-copy { min-width: 0; }
  .topbar .eyebrow { margin-bottom: 4px; font-size: 10px; }
  .topbar h1 {
    margin-bottom: 5px;
    font-size: clamp(23px, 7vw, 28px);
    line-height: 1.12;
  }
  .page-subtitle {
    max-width: 270px;
    font-size: 12px;
    line-height: 1.45;
  }
  .connection-badge {
    min-height: 34px;
    max-width: 132px;
    padding: 7px 9px;
    overflow: hidden;
    font-size: 10px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .dashboard,
  .orders-view,
  .products-view,
  .business-profile-view { margin-top: 18px; }

  /* Onboarding: action first, benefits second. */
  .connect-layout {
    margin-top: 18px;
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
  .connect-form-card {
    order: 1;
    min-height: auto;
    padding: 20px 16px;
    border-radius: 16px;
  }
  .connect-hero {
    order: 2;
    min-height: auto;
    padding: 24px 20px;
    border-radius: 18px;
    box-shadow: none;
  }
  .connect-hero h2 {
    margin: 16px 0 12px;
    font-size: 30px;
    line-height: 1.02;
  }
  .connect-hero-content > p {
    margin-bottom: 20px;
    font-size: 13px;
    line-height: 1.55;
  }
  .feature-list { gap: 10px; }
  .feature-list > div {
    grid-template-columns: 30px minmax(0,1fr);
    gap: 10px;
  }
  .feature-list strong { font-size: 12px; }
  .feature-list small { font-size: 10px; line-height: 1.4; }
  .date-grid { gap: 9px; }
  .primary-button { min-height: 50px; }
  .loading-card {
    min-height: calc(100svh - var(--mobile-header-height) - var(--mobile-nav-height) - 70px);
    margin-top: 18px;
    padding: 40px 18px;
    border-radius: 18px;
  }

  /* Dashboard */
  .dashboard-head {
    display: grid;
    grid-template-columns: minmax(0,1fr) auto;
    align-items: start;
    gap: 12px;
    margin-bottom: 16px;
  }
  .store-row { gap: 10px; }
  .store-logo { width: 36px; height: 36px; }
  .store-row h2 { font-size: 19px; }
  .dashboard-subline {
    margin: 7px 0 0;
    flex-wrap: wrap;
    gap: 5px 7px;
    font-size: 10px;
  }
  #disconnect {
    min-width: 82px;
    height: 38px;
    padding: 0 10px;
    font-size: 10px;
    text-align: center;
  }

  .metrics-grid {
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 10px;
  }
  .metric-card {
    min-width: 0;
    min-height: 110px;
    padding: 14px;
    border-radius: 14px;
  }
  .metric-card > span,
  .metric-label { font-size: 10px; line-height: 1.25; }
  .metric-card strong {
    margin: 12px 0 6px;
    overflow: hidden;
    font-size: clamp(19px, 5.4vw, 23px);
    line-height: 1.05;
    letter-spacing: -.055em;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .metric-card small { font-size: 10px; line-height: 1.3; }

  .performance-strip {
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 1px;
    margin-top: 10px;
    border-radius: 14px;
  }
  .performance-strip > div { min-height: 76px; padding: 12px; }
  .performance-strip span,
  .performance-strip small { font-size: 10px; }
  .performance-strip strong { font-size: 16px; }

  .analytics-grid {
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 10px;
  }
  .chart-panel,
  .status-panel,
  .orders-panel { padding: 14px; border-radius: 14px; }
  .chart-header {
    align-items: stretch;
    flex-direction: column;
  }
  .chart-actions {
    width: 100%;
    align-items: stretch;
    flex-direction: column;
    gap: 9px;
  }
  .segmented-control {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3,minmax(0,1fr));
    overflow: hidden;
  }
  .segmented-control button {
    min-width: 0;
    height: 34px;
    padding: 0 5px;
    font-size: 10px;
  }
  .chart-summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    text-align: left;
  }
  .chart-summary span { font-size: 10px; }
  .sales-chart { min-height: 220px; margin-top: 10px; }
  .sales-chart svg { height: 220px; }
  .status-overview { grid-template-columns: 86px 1fr; gap: 14px; }
  .status-donut { width: 84px; height: 84px; }

  /* Orders become native cards instead of wide desktop tables. */
  .orders-panel { margin-top: 10px; }
  .orders-view-head {
    align-items: flex-start;
    margin-bottom: 12px;
  }
  .orders-view-head h2 { font-size: 22px; }
  #exportOrders { min-width: 72px; }
  .orders-toolbar {
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 8px;
    padding: 10px;
  }
  .orders-toolbar .search-field { grid-column: 1 / -1; }
  .orders-toolbar input,
  .orders-toolbar select { height: 44px; font-size: 12px; }
  #ordersPageSize { display: none; }
  .orders-register { overflow: visible; background: transparent; border: 0; box-shadow: none; }
  .orders-panel .table-wrap,
  .orders-register .table-wrap,
  .products-register .table-wrap {
    margin-top: 12px;
    overflow: visible;
  }
  .orders-panel table,
  .orders-register table,
  .products-register table {
    min-width: 0;
    border-collapse: separate;
  }
  .orders-panel thead,
  .orders-register thead,
  .products-register thead { display: none; }
  #ordersTable,
  #allOrdersTable,
  #productsTable {
    display: grid;
    gap: 10px;
  }
  #ordersTable tr,
  #allOrdersTable tr,
  #productsTable tr {
    min-width: 0;
    padding: 13px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: #fff;
    box-shadow: var(--shadow-sm);
  }
  #ordersTable td,
  #allOrdersTable td,
  #productsTable td {
    min-width: 0;
    padding: 0;
    border: 0;
    background: transparent !important;
    font-size: 11px;
  }
  #ordersTable td[colspan],
  #allOrdersTable td[colspan],
  #productsTable td[colspan] { grid-column: 1 / -1; padding: 12px; text-align: center; }

  #ordersTable tr {
    display: grid;
    grid-template-columns: minmax(0,1fr) auto;
    grid-template-areas:
      "code amount"
      "date amount"
      "status delivery";
    gap: 6px 12px;
    align-items: center;
  }
  #ordersTable td:nth-child(1) { grid-area: code; }
  #ordersTable td:nth-child(2) { grid-area: date; color: var(--ink-muted); }
  #ordersTable td:nth-child(3) { grid-area: status; }
  #ordersTable td:nth-child(4) { grid-area: delivery; color: var(--ink-muted); text-align: right; }
  #ordersTable td:nth-child(5) { grid-area: amount; align-self: start; font-weight: 750; }

  #allOrdersTable tr {
    display: grid;
    grid-template-columns: minmax(0,1fr) auto;
    grid-template-areas:
      "code amount"
      "date amount"
      "status action"
      "city action";
    gap: 6px 12px;
    align-items: center;
  }
  #allOrdersTable td:nth-child(1) { grid-area: code; }
  #allOrdersTable td:nth-child(2) { grid-area: date; color: var(--ink-muted); }
  #allOrdersTable td:nth-child(3) { grid-area: status; }
  #allOrdersTable td:nth-child(4) { grid-area: city; color: var(--ink-muted); }
  #allOrdersTable td:nth-child(5),
  #allOrdersTable td:nth-child(6) { display: none; }
  #allOrdersTable td:nth-child(7) { grid-area: amount; align-self: start; font-weight: 750; }
  #allOrdersTable td:nth-child(8) { grid-area: action; align-self: end; }
  .row-action { width: 40px; height: 40px; }
  .pagination {
    margin-top: 10px;
    padding: 10px 0 0;
    justify-content: space-between;
    border-top: 0;
  }
  .pagination button { min-width: 88px; height: 40px; font-size: 10px; }
  .pagination span { min-width: 0; font-size: 10px; }

  /* Order details use a bottom sheet. */
  .drawer-backdrop { z-index: 300; }
  .order-drawer {
    z-index: 301;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: min(92svh, 780px);
    padding: 18px 16px calc(20px + env(safe-area-inset-bottom));
    border-radius: 22px 22px 0 0;
    box-shadow: 0 -24px 70px rgba(10,11,18,.22);
    transform: translateY(105%);
  }
  .order-drawer.open { transform: translateY(0); }
  .drawer-header { position: sticky; top: -18px; z-index: 2; padding-top: 4px; background: #fff; }
  .drawer-header button { width: 42px; height: 42px; }
  .drawer-meta > div { min-height: 62px; }
  .drawer-meta > div > span,
  .drawer-meta > div > div,
  .drawer-section-head p,
  .entry-main p,
  .entry-money span { font-size: 10px; }
  .entry-main h4 { font-size: 12px; white-space: normal; }
  .entry-card { padding: 12px; flex-direction: column; }
  .entry-money { width: 100%; display: flex; justify-content: space-between; text-align: left; }

  /* Products */
  .products-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
  }
  .products-head h2 { font-size: 22px; }
  .products-head-actions { width: 100%; display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
  .products-head-actions .primary-button { margin-top: 0; }
  .product-metrics { grid-template-columns: repeat(2,minmax(0,1fr)); gap: 10px; }
  .product-metric { min-height: 104px; padding: 13px; }
  .product-metric strong { font-size: 21px; }
  .products-grid { grid-template-columns: 1fr; gap: 10px; }
  .products-chart-panel,
  .abc-panel { padding: 14px; }
  .product-bar-row { grid-template-columns: 20px minmax(0,1fr) auto; gap: 8px; }
  .product-bar-track { display: none; }
  .products-toolbar {
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 8px;
    padding: 10px;
  }
  .products-toolbar .search-field { grid-column: 1 / -1; }
  .products-toolbar input,
  .products-toolbar select { height: 44px; font-size: 12px; }
  .products-toolbar select:last-child { grid-column: 1 / -1; }
  .products-register { overflow: visible; border: 0; background: transparent; box-shadow: none; }
  #productsTable tr {
    display: grid;
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 11px 12px;
  }
  #productsTable td:first-child { grid-column: 1 / -1; }
  #productsTable td:nth-child(n+2) { display: grid; gap: 3px; }
  #productsTable td:nth-child(2)::before { content: "ABC"; }
  #productsTable td:nth-child(3)::before { content: "Заказы"; }
  #productsTable td:nth-child(4)::before { content: "Продано"; }
  #productsTable td:nth-child(5)::before { content: "Остаток"; }
  #productsTable td:nth-child(6)::before { content: "GMV"; }
  #productsTable td:nth-child(7)::before { content: "Потери"; }
  #productsTable td:nth-child(8)::before { content: "Доля"; }
  #productsTable td:nth-child(n+2)::before {
    color: var(--ink-muted);
    font-size: 10px;
    font-weight: 650;
  }
  .product-cell { max-width: none; }
  .product-cell > span { font-size: 13px; white-space: normal; }
  .product-cell small { font-size: 10px; }

  /* Settings and forms */
  .marketplace-settings,
  .business-profile-form { padding: 18px 14px; border-radius: 16px; }
  .settings-section-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }
  .settings-section-head h2 { font-size: 22px; }
  .settings-account-email { max-width: 100%; overflow-wrap: anywhere; }
  .marketplace-settings-card { padding: 16px; }
  .settings-card-actions { align-items: stretch; }
  .settings-card-actions .primary-button { margin-top: 0; }
  .profile-grid,
  .profile-grid.two { grid-template-columns: 1fr; }
  .profile-grid input,
  .profile-grid select,
  .marketplace-settings-card input { min-height: 50px; font-size: 16px; }
  .settings-secret-field button { min-width: 76px; }
  .profile-actions { position: static; margin: 22px 0 0; padding: 18px 0 0; }

  /* Dialogs are bottom sheets, never tiny centered desktop windows. */
  .catalog-scan-dialog {
    align-items: end;
    padding: 0;
  }
  .catalog-scan-form {
    width: 100%;
    max-width: none;
    max-height: calc(94svh - env(safe-area-inset-bottom));
    padding: 20px 16px calc(18px + env(safe-area-inset-bottom));
    overflow-y: auto;
    border-radius: 22px 22px 0 0;
  }
  .catalog-scan-grid { grid-template-columns: 1fr; }
  .catalog-scan-actions { position: sticky; bottom: 0; padding-top: 12px; background: #fff; }
  .catalog-scan-actions .primary-button,
  .catalog-scan-actions .secondary-button { min-height: 48px; }

  /* Product import */
  .kaspi-import-view { gap: 14px; }
  .kaspi-import-head { gap: 12px; }
  .kaspi-import-head h2 { font-size: 25px; }
  .kaspi-import-guide { gap: 9px; }
  .kaspi-import-guide article { min-height: 0; padding: 15px; }
  .kaspi-import-layout { gap: 12px; }
  .kaspi-import-section,
  .kaspi-import-preview,
  .kaspi-import-result { padding: 16px; }
  .kaspi-import-grid,
  .kaspi-import-side { grid-template-columns: 1fr; }
  .kaspi-import-submit {
    position: sticky;
    bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom));
    z-index: 20;
    padding: 13px;
    border-radius: 14px;
    background: rgba(255,255,255,.96);
    box-shadow: 0 -8px 28px rgba(15,23,42,.1);
    backdrop-filter: blur(14px);
  }
  .kaspi-import-submit p { display: none; }

  /* Admin remains usable without pretending to be a desktop table. */
  .admin-view { padding: 0; }
  .admin-hero { padding: 18px; border-radius: 16px; }
  .admin-owner-badge { white-space: normal; }
  .admin-tabs { overflow-x: auto; scrollbar-width: none; }
  .admin-tabs::-webkit-scrollbar { display: none; }
  .admin-tabs button { min-width: max-content; }
  .admin-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .admin-kpi { min-height: 108px; padding: 14px; }
  .admin-kpi strong { font-size: 24px; }
  .admin-section { padding: 14px; }

  /* Authentication has a compact premium hero instead of a blank form page. */
  body.auth-locked { background: #fff; }
  .auth-gate {
    display: block;
    min-height: 100svh;
    overflow-y: auto;
    background: #fff;
  }
  .auth-visual {
    display: block;
    min-height: 220px;
    padding: 22px 20px 27px;
    border-radius: 0 0 26px 26px;
  }
  .auth-brand-mark { width: 40px; height: 40px; border-radius: 12px; }
  .auth-hero { max-width: none; margin-top: 30px; }
  .auth-hero span { padding: 6px 9px; font-size: 10px; }
  .auth-hero h1 {
    max-width: 340px;
    margin: 12px 0 0;
    font-size: clamp(32px, 10vw, 40px);
    line-height: .98;
  }
  .auth-hero p,
  .auth-points { display: none; }
  .auth-panel {
    min-height: auto;
    padding: 18px 14px calc(30px + env(safe-area-inset-bottom));
    align-items: flex-start;
  }
  .auth-card {
    max-width: none;
    min-height: 0;
    padding: 20px 8px 10px;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    backdrop-filter: none;
  }
  .auth-card-head { margin-bottom: 20px; }
  .auth-card-head h2 { font-size: 27px; }
  .auth-field input { min-height: 56px; font-size: 16px; }
  .auth-submit { min-height: 56px; }

  @media (max-width: 360px) {
    :root { --mobile-page-gutter: 11px; }
    .brand small { display: none; }
    .nav-item { flex-basis: 58px; min-width: 58px; }
    .nav-item::after { font-size: 9px; }
    .metrics-grid { gap: 8px; }
    .metric-card { padding: 12px; }
    .metric-card strong { font-size: 18px; }
    .date-grid { grid-template-columns: 1fr; }
  }
}

@media (min-width: 761px) and (max-width: 960px) {
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 80;
    height: auto;
    min-height: 72px;
    padding: 12px 18px;
    flex-direction: row;
    align-items: center;
    overflow: visible;
  }
  .brand { min-width: 190px; }
  .workspace-card,
  .sidebar-footer { display: none; }
  .nav-list {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 5px;
  }
  .nav-item {
    width: auto;
    min-width: 52px;
    padding: 0 10px;
    grid-template-columns: auto auto;
  }
  .nav-item em { display: none; }
  .app-main { padding-bottom: 46px; }
}
