/* Responsive safety layer for both the shell and embedded core pages.
   Appearance only: no hidden business fields, role changes or API overrides.
   Wide ledgers scroll inside their own region instead of squeezing words. */
@media screen {
  .service-orders-ledger > .park-panel-body.service-orders-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: auto !important;
    overscroll-behavior-x: contain;
  }
  .service-orders-ledger:has(.service-orders-table)::before {
    content: "Проведите по таблице влево, чтобы увидеть остальные столбцы";
    display: block;
    padding: 10px 14px;
    color: var(--fg-3, #667182);
    background: var(--bg-elev, #fff);
    border-bottom: 1px solid var(--line, #e8ebef);
    font-size: 12px;
    line-height: 1.5;
  }
  .stock-page .service-orders-table {
    min-width: 1440px;
    table-layout: auto;
  }
  .stock-page .service-orders-table :is(th, td) {
    overflow-wrap: normal;
    word-break: normal;
  }
  .stock-page .service-orders-table :is(th, td):nth-child(5) {
    min-width: 240px;
    max-width: 320px;
    white-space: normal;
  }
  .stock-page .service-orders-table .moysklad-sum {
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }
}

@media screen and (max-width: 900px) {
  .stock-page, .dt-record-page, .dt-rental, .rp-page,
  .repair-order-fullscreen .drawer-body > * {
    min-width: 0;
    max-width: 100%;
  }

  /* The classic order is still a dialog. The new order is a page; do not
     give the page a fixed viewport height or a second vertical scrollbar. */
  .repair-order-fullscreen:not(.repair-order-create-dialog) .drawer-body {
    grid-template-columns: minmax(0, 1fr) !important;
    min-width: 0;
  }
  .repair-order-fullscreen:not(.repair-order-create-dialog) .drawer-body > * {
    grid-column: 1 / -1 !important;
    grid-row: auto !important;
  }
  .repair-order-fullscreen .drawer-body div:has(> .moysklad-table) {
    min-width: 0;
    max-width: 100%;
    overflow-x: auto !important;
    overscroll-behavior-x: contain;
  }
  .repair-order-fullscreen .drawer-body .moysklad-table {
    min-width: 660px;
    table-layout: auto;
  }
  .repair-order-fullscreen .moysklad-item-row td:nth-child(3) { min-width: 220px; }
  .repair-order-fullscreen .moysklad-item-row td:nth-child(n+4) { white-space: nowrap; }
  .repair-order-fullscreen .drawer-body h3:has(+ div > .moysklad-table) {
    flex-wrap: wrap;
    gap: 8px 16px;
    align-items: flex-start !important;
  }
  .repair-order-fullscreen .drawer-body h3:has(+ div > .moysklad-table) > .mono {
    white-space: nowrap;
  }
  .repair-order-fullscreen .drawer-body > .moysklad-totals-row + div {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  .repair-order-fullscreen .drawer-body > .moysklad-totals-row + div table {
    table-layout: fixed;
    width: 100%;
  }
  .repair-order-fullscreen .drawer-body > .moysklad-totals-row + div td {
    overflow-wrap: anywhere;
  }
  .repair-order-fullscreen .drawer-head { flex-wrap: wrap; gap: 12px; }
  .repair-order-fullscreen .drawer-head > div { min-width: 0; max-width: 100%; }
  .repair-order-fullscreen .drawer-head h2 { overflow-wrap: anywhere; line-height: 1.35; }
  .repair-order-fullscreen .repair-document-header-actions { flex-wrap: wrap; }
  .repair-order-fullscreen .status-stepper-container { min-width: 0; overflow-x: auto; }
  .repair-order-fullscreen .stepper-step-wrapper { min-width: 72px; }
  .repair-order-fullscreen .drawer-body .order-action-bar {
    width: 100% !important;
    min-width: 0;
    flex-wrap: wrap;
  }
  .stock-page .stock-filters { flex-wrap: wrap; }
  .stock-page .stock-filters > * { min-width: 0; max-width: 100%; }
  .stock-page .stock-filters > .type-selector { flex-wrap: wrap; height: auto; }
  .stock-page .stock-search-field { flex: 1 1 220px; }

  /* Reuse status actions as a bottom panel outside the scrolling ledger. */
  .stock-page.page-enter { animation: none; }
  .service-orders-table .interactive-status-badge-container > .stock-select-menu {
    position: fixed !important;
    inset: auto 12px max(12px, env(safe-area-inset-bottom)) !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: 460px;
    margin-inline: auto;
    max-height: calc(100dvh - 100px);
    overflow-y: auto;
    transform: none !important;
    z-index: 350 !important;
    text-align: left;
  }
  .service-orders-table .interactive-status-badge-container > .stock-select-menu button { min-height: 44px; }

  /* Core lists live in an iframe: its document must not grow wider than
     the shell. Keep the overflow on the actual table container. */
  [data-slot="table-container"] { min-width: 0; max-width: 100%; overflow-x: auto; }
  .dt-record-content, .dt-aside, .dt-work, .dt-client-identity { min-width: 0; }
}

@media screen and (max-width: 640px) {
  .page:not(.is-core) {
    padding: 16px max(12px, env(safe-area-inset-right)) max(64px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .topbar { gap: 5px; padding-inline: 8px; }
  .topbar .crumbs { min-width: 0; overflow: hidden; }
  .topbar .crumbs b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar :is(.icon-btn, .sidebar-toggle, .user-menu-trigger, .status-trigger, .search) {
    flex-shrink: 0;
    min-height: 44px;
    min-width: 44px;
  }
  .stock-head, .stock-head-actions { flex-wrap: wrap; gap: 10px; }
  .stock-page > .park-kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stock-page > .park-kpi-grid .park-kpi { min-height: 0; padding: 12px; }
  .stock-page > .park-kpi-grid .park-kpi-value { font-size: 23px; }
  .stock-page .stock-filters > .type-selector { width: 100%; gap: 6px !important; }
  .stock-page .stock-filters .type-selector button { flex: 1 1 auto; white-space: normal; }
  .stock-page .stock-filters > .stock-search-field { flex-basis: 100%; }
  .stock-page .service-wait-summary { grid-template-columns: minmax(0, 1fr); }
  .stock-page .service-wait-summary small { grid-column: auto; }
  .stock-page .service-wait-time { white-space: normal; }

  .drawer.repair-order-fullscreen:not(.repair-order-create-dialog):not(.dt-order-page) {
    inset: 8px !important;
    transform: none !important;
    animation: none !important;
    overflow: clip;
    width: auto !important;
    height: calc(100dvh - 16px) !important;
    max-height: calc(100dvh - 16px);
  }
  .repair-order-fullscreen .drawer-head { padding: 14px 12px; flex-shrink: 0; }
  .repair-order-fullscreen .drawer-head h2 { font-size: 18px; }
  .repair-order-fullscreen .drawer-head > .repair-document-header-actions { margin-left: auto; justify-content: flex-end; }
  .repair-order-fullscreen .drawer-body { padding: 12px; min-height: 0; }
  /* Keep all six item fields visible on a phone, reusing the existing cells,
     headings and action buttons. Tablet/desktop retain the familiar table. */
  html .repair-order-fullscreen .drawer-body table.moysklad-table {
    display: block;
    width: 100%;
    min-width: 0 !important;
    border-collapse: separate;
    background: transparent;
  }
  .repair-order-fullscreen .drawer-body table.moysklad-table > thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }
  .repair-order-fullscreen .drawer-body table.moysklad-table > tbody { display: grid; gap: 10px; padding: 8px; }
  .repair-order-fullscreen .moysklad-item-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px 10px;
    padding: 10px;
    border: 1px solid var(--line, #e8ebef);
    border-radius: 8px;
    align-items: start;
  }
  .repair-order-fullscreen .moysklad-item-row > td {
    display: block;
    width: auto !important;
    min-width: 0 !important;
    padding: 3px 0 !important;
    border: 0 !important;
    text-align: left !important;
    font-size: 13px;
  }
  .repair-order-fullscreen .moysklad-item-row > td:first-child { grid-column: 1; grid-row: 1; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(2) { grid-column: 2; grid-row: 1; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(3) {
    grid-column: 1 / -1;
    grid-row: 2;
    font-size: 14px;
    line-height: 1.5;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(n+4):nth-child(-n+6) { grid-row: 3; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(4)::before { content: "Кол-во"; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(5)::before { content: "Цена"; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(6)::before { content: "Сумма"; }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(n+4):nth-child(-n+6)::before {
    display: block;
    margin-bottom: 5px;
    color: var(--fg-3, #667182);
    font-size: 11px;
    font-weight: 400;
  }
  .repair-order-fullscreen .moysklad-item-row > td:nth-child(7) { grid-column: 3; grid-row: 1; justify-self: end; }
  .repair-order-fullscreen table.moysklad-table .moysklad-totals-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    padding: 8px;
  }
  .repair-order-fullscreen table.moysklad-table .moysklad-totals-row > td { padding: 4px !important; border: 0; }
  .repair-order-fullscreen table.moysklad-table .moysklad-totals-row > td:first-child { flex: 1; }
  .repair-order-fullscreen .drawer-body .order-action-bar > .stock-select-field {
    width: 100% !important;
    flex: 1 1 100% !important;
  }
  .repair-order-fullscreen .drawer-body .order-action-bar > div {
    grid-template-columns: minmax(0, 1fr) !important;
    max-width: 100%;
  }
  .repair-order-fullscreen .drawer-body .order-action-bar .btn {
    height: auto;
    min-height: 44px;
    white-space: normal;
  }
  .repair-order-fullscreen .repair-quote-inputs {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
  .repair-order-fullscreen .repair-quote-inputs > :first-child,
  .repair-order-fullscreen .repair-quote-inputs > button { grid-column: 1 / -1; }
  .repair-order-fullscreen .repair-quote-inputs > div { min-width: 0; }
  .repair-order-fullscreen form:has(> .repair-quote-inputs) > div:first-child { max-width: 100%; flex-wrap: wrap; }
  .repair-order-fullscreen form:has(> .repair-quote-inputs) > div:first-child button { min-height: 44px; }
  .repair-order-create-dialog .drawer-form-row { grid-template-columns: minmax(0, 1fr); }
  .repair-documents-menu { min-width: min(270px, calc(100vw - 48px)); max-width: calc(100vw - 48px); }
  .repair-document-equipment { grid-template-columns: minmax(0, 1fr); }
  .floating-action-bar { max-width: calc(100vw - 24px); width: max-content; flex-wrap: wrap; justify-content: center; border-radius: 14px; gap: 8px; bottom: max(12px, env(safe-area-inset-bottom)); }

  /* The client editor is inside a padded page, not a full-width portal. */
  html[data-design-trial] .dt-record-page .gb-client-intake {
    width: 100%;
    min-height: 0;
    max-height: calc(100dvh - 140px);
  }
  .gb-client-intake__header, .gb-client-intake__footer { padding: 12px; gap: 10px; }
  .gb-client-intake__body { padding: 14px 12px; }
  .gb-client-intake__footer { flex-wrap: wrap; }
  .gb-client-intake__button { min-height: 44px; }
  html[data-design-trial] .dt-client { align-items: flex-start; }
  html[data-design-trial] .dt-client-payment { margin-left: 0; width: 100%; }
  html[data-design-trial] .dt-client-payment .rd-actions-main { justify-content: flex-start; }
  html[data-design-trial] .rd-actions button { min-height: 44px !important; }
  html[data-design-trial] :is([data-slot="button"], [data-slot="tabs-trigger"]) { min-height: 44px; }

  /* 16px native inputs prevent focus-zoom on iOS without disabling pinch zoom. */
  :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea) { font-size: 16px !important; }
  .repair-order-fullscreen :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea) { min-height: 44px; }
  :is(.stock-page, .repair-order-fullscreen, .dt-record-page, .dt-rental, .rp-page) :is(.btn, .icon-btn, .stock-select-trigger) {
    min-height: 44px !important;
    touch-action: manipulation;
  }
  .stock-page.dt-services-host .type-selector button { min-height: 44px !important; }
  :is(.drawer-body, .gb-client-intake__body, .repair-document-content) { overscroll-behavior-y: contain; }
}

@media screen and (max-width: 380px) {
  .topbar { gap: 3px; padding-inline: 5px; }
  .stock-page > .park-kpi-grid { grid-template-columns: minmax(0, 1fr); }
  .repair-order-fullscreen .service-wait-stopwatch { font-size: 11px; padding-inline: 6px; }
}
