/* Stage 1: phones. Loaded only by the WB and Ozon autoresponder pages.
   Keep desktop rules and all API/event handlers untouched. */
@media (max-width: 767px) {
    .ozon-products-content {
        width: 100%;
        max-width: 100%;
    }

    .ar-board-wrap { padding-top: 0; }
    .ar-board-title {
        position: static;
        transform: none;
        font-size: clamp(26px, 7.5vw, 38px);
        line-height: 1.15;
        white-space: normal;
        margin: 0 0 12px;
    }
    .ar-shell { padding: 16px 12px 12px; border-radius: 20px; }
    .ar-page-head { flex-wrap: wrap !important; gap: 12px !important; }
    .ar-page-subtitle { flex-basis: 100% !important; order: 3; font-size: 14px; line-height: 1.5; }
    .ar-segmented { width: 100%; min-width: 0; flex: 1 1 100%; }
    .ar-segmented-btn {
        min-height: 44px;
        min-width: 0;
        padding: 8px;
        justify-content: center;
        flex-wrap: wrap;
        flex: 1 1 auto;
        white-space: normal;
    }
    .ar-segmented-btn .ar-badge { flex-basis: 100%; margin-left: 0 !important; }
    .ar-stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 8px; }
    .ar-stat-card { min-width: 0; padding: 12px; }
    .ar-stat-status-text { font-size: 16px; white-space: normal; overflow-wrap: anywhere; }
    .ar-section { padding: 16px 12px; margin-bottom: 16px; }
    .ar-section-title { font-size: 17px; line-height: 1.35; }
    .ar-setting-info, .ar-form-group, .ar-modal-col { min-width: 0; }
    .ar-setting-desc { line-height: 1.5; }
    .ar-setting-label { flex-wrap: wrap; }
    .ar-setting-row { flex-direction: column; align-items: stretch; gap: 10px; }
    .ar-setting-control, .ar-threshold-control { width: 100%; max-width: 100%; margin-left: 0; }
    .ar-setting-row:has(.ar-toggle) {
        flex-direction: row;
        align-items: flex-start;
        gap: 12px;
    }
    .ar-setting-row:has(.ar-toggle) .ar-setting-control { width: auto; flex: 0 0 52px; }
    .ar-toggle { height: 44px; }
    .ar-toggle-slider { top: 8px; bottom: 8px; }
    .ar-toggle input:focus-visible + .ar-toggle-slider { outline: 2px solid var(--ar-orange); outline-offset: 3px; }
    .ar-threshold-slider { margin: 14px 0; touch-action: pan-y; }
    .ar-threshold-slider::-webkit-slider-thumb { width: 28px; height: 28px; }
    .ar-threshold-slider::-moz-range-thumb { width: 28px; height: 28px; }
    .ar-nav-tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
    .ar-nav-tab { min-height: 48px; padding: 8px; white-space: normal; justify-content: center; text-align: center; }
    #ar-questions-section .ar-filter-tabs {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 4px;
        padding: 4px;
        margin: 0 12px 16px;
        max-width: calc(100% - 24px);
        overflow: visible;
    }
    #ar-questions-section .ar-filter-tab {
        min-width: 0;
        min-height: 48px;
        padding: 8px 6px;
        justify-content: center;
        white-space: normal;
        text-align: center;
        line-height: 1.35;
    }
    #ar-questions-section .ar-filter-tab > span:first-child { min-width: 0; overflow-wrap: anywhere; }
    #ar-questions-section .ar-tab-counter { flex-shrink: 0; }
    #ar-questions-section > div:first-child { padding: 0 12px; }
    .ar-btn, .ar-btn-sm, .ar-btn-xs, .ar-chip-btn, .ar-kb-mode-btn, .aq-page-btn {
        min-height: 44px;
        max-width: 100%;
        white-space: normal !important;
        justify-content: center;
        text-align: center;
        box-sizing: border-box;
    }
    .ar-wrap .ar-btn { height: auto !important; }
    .ar-token-banner { flex-direction: column; align-items: stretch; padding: 16px !important; gap: 14px; }
    .ar-token-banner-content { min-width: 0; }
    .ar-token-banner-action, .ar-token-banner-action .ar-btn { width: 100%; }
    .ar-stop-topics-grid, .ar-features-grid { grid-template-columns: minmax(0, 1fr); }
    .ar-upload-card { padding: 16px 12px; }
    .ar-upload-actions { flex-direction: column; }
    .ar-upload-actions .ar-btn { width: 100%; }
    .ar-file-status { max-width: 100%; overflow-wrap: anywhere; }
    .ar-tpl-header { padding: 14px 12px; align-items: flex-start; }
    .ar-tpl-header-left { flex-wrap: wrap; }
    .ar-tpl-badges, .ar-tpl-actions { flex-wrap: wrap; }
    .ar-tpl-body { padding: 0 12px 16px; }
    .ar-tpl-inline-form { flex-wrap: wrap; }
    .ar-tpl-inline-form input { flex-basis: 100%; min-width: 0; }
    .ar-tpl-chip { max-width: 100%; overflow-wrap: anywhere; }
    .ar-tpl-chip button { min-width: 32px; min-height: 32px; justify-content: center; }
    .ar-wrap [style*="min-width: 260px"] { min-width: 0 !important; }

    /* The original rows stay intact, including dynamically loaded rows.
       Only their visual layout changes; details and action handlers still work. */
    .ar-wrap .ar-table, .ar-wrap .ar-table tbody, .ar-wrap .ar-table tr,
    .ar-wrap .ar-table td { display: block; width: 100%; }
    .ar-wrap .ar-table { table-layout: auto; min-width: 0 !important; border: 0; overflow: visible; }
    .ar-wrap .ar-table thead { display: none; }
    .ar-wrap .ar-table tbody tr {
        border: 1px solid var(--ar-line-soft);
        border-radius: 14px;
        margin-bottom: 12px;
        padding: 6px 0;
        background: #fff;
    }
    .ar-wrap .ar-table td {
        padding: 10px 12px !important;
        text-align: left !important;
        font-size: 14px;
        border: 0;
    }
    .ar-wrap .ar-table td::before {
        display: block;
        margin-bottom: 6px;
        font-size: 12px;
        font-weight: 600;
        color: #64748b;
    }
    #ar-products-table-container td:nth-child(1)::before { content: "Артикул"; }
    #ar-products-table-container td:nth-child(2)::before { content: "Название и характеристики"; }
    #ar-products-table-container td:nth-child(3)::before { content: "Категория"; }
    #ar-products-table-container td:nth-child(4)::before { content: "Действия"; }
    #tab-knowledge td:nth-child(1)::before { content: "Артикул товара"; }
    #tab-knowledge td:nth-child(2)::before { content: "Информация о товаре"; }
    #tab-knowledge td:nth-child(3)::before { content: "Примеры вопросов"; }
    [data-marketplace="ozon"] #ar-products-table-container td:nth-child(1)::before { content: "SKU"; }
    [data-marketplace="ozon"] #tab-knowledge td:nth-child(1)::before { content: "SKU товара"; }
    #ar-questions-table-container td:nth-child(1)::before { content: "№"; }
    #ar-questions-table-container td:nth-child(2)::before { content: "Товар"; }
    #ar-questions-table-container td:nth-child(3)::before { content: "Вопрос покупателя"; }
    #ar-questions-table-container td:nth-child(4)::before { content: "Статус"; }
    #ar-questions-table-container td:nth-child(5)::before { content: "Ответ / решение"; }
    #ar-questions-table-container td:nth-child(6)::before { content: "Действия"; }
    #ar-questions-table-container { padding: 0 12px; }
    .ar-wrap .btn-delete-product { width: 44px !important; height: 44px !important; }
    .ar-expanded-content, .ar-answer-box, .ar-result-box { overflow-wrap: anywhere; }

    /* Question cards: override the inline desktop column widths as well as
       the nested 84px action toolbar. Keep the original nodes and handlers. */
    #ar-questions-table-container .ar-table tbody tr {
        display: flex;
        flex-direction: column;
        gap: 10px;
        padding: 14px 12px;
    }
    #ar-questions-table-container .ar-table td {
        width: 100% !important;
        min-width: 0;
        padding: 0 !important;
    }
    #ar-questions-table-container .ar-table td::before { display: none; }
    #ar-questions-table-container td:nth-child(2) { order: 1; }
    #ar-questions-table-container td:nth-child(4) { order: 2; }
    #ar-questions-table-container td:nth-child(3) { order: 3; }
    #ar-questions-table-container td:nth-child(5) { order: 4; }
    #ar-questions-table-container td:nth-child(6) { order: 5; }
    #ar-questions-table-container td:nth-child(1) {
        order: 6;
        border-top: 1px solid var(--ar-line-soft);
        padding-top: 8px !important;
    }
    #ar-questions-table-container .ar-id-chip {
        max-width: 100%;
        font-size: 11px;
        font-weight: 400;
        background: transparent;
        border: 0;
        padding: 4px 0;
        color: #64748b;
        overflow-wrap: anywhere;
    }
    #ar-questions-table-container td:nth-child(2) > div:nth-child(2) {
        font-size: 14px !important;
        line-height: 1.45 !important;
    }
    #ar-questions-table-container td:nth-child(4) > div {
        flex-direction: row !important;
        align-items: center !important;
        flex-wrap: wrap;
        gap: 8px !important;
    }
    #ar-questions-table-container td:nth-child(4) > div > span {
        text-align: left !important;
    }
    #ar-questions-table-container td:nth-child(3) > div,
    #ar-questions-table-container td:nth-child(5) > div {
        width: 100%;
        min-width: 0;
        padding: 12px !important;
        font-size: 14px !important;
        line-height: 1.5 !important;
    }
    #ar-questions-table-container td:nth-child(3) > div > div:first-child,
    #ar-questions-table-container td:nth-child(5) > div > div:first-child:has(span) {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 3px !important;
        margin-bottom: 8px !important;
    }
    #ar-questions-table-container td:nth-child(3) > div > div:first-child > span,
    #ar-questions-table-container td:nth-child(5) > div > div:first-child > span {
        white-space: normal !important;
        overflow-wrap: anywhere;
    }
    #ar-questions-table-container td:nth-child(6) > div {
        width: 100% !important;
        flex-direction: row !important;
        flex-wrap: wrap;
        gap: 8px !important;
        margin: 0 !important;
    }
    #ar-questions-table-container td:nth-child(6) .ar-btn-sm {
        flex: 1 1 100px;
        width: auto !important;
        height: auto !important;
        min-height: 44px;
        padding: 10px 12px !important;
        font-size: 13px !important;
    }

    .ar-modal-backdrop { padding: 10px; box-sizing: border-box; }
    .ar-modal-dialog, .ar-modal-dialog--confirm {
        min-width: 0;
        max-height: calc(100vh - 20px);
        max-height: calc(100dvh - 20px);
        box-sizing: border-box;
    }
    .ar-modal-dialog { border-radius: 18px; }
    .ar-modal-dialog--confirm { padding: 52px 18px 20px; overflow-y: auto; }
    .ar-modal-header { padding: 12px; gap: 8px; flex-shrink: 0; }
    .ar-modal-title { font-size: 16px; }
    .ar-modal-title-group { min-width: 0; gap: 6px; }
    .ar-modal-close, .ar-confirm-close-btn { min-width: 44px; min-height: 44px; flex-shrink: 0; }
    .ar-modal-body { min-height: 0; padding: 12px; overscroll-behavior-y: contain; }
    .ar-modal-grid { grid-template-columns: minmax(0, 1fr) !important; }
    .ar-modal-footer { padding: 12px; gap: 8px; flex-shrink: 0; }
    .ar-modal-footer > div { flex-wrap: wrap; gap: 8px; }
    .ar-modal-footer .ar-btn { flex: 1 1 auto; min-height: 44px; white-space: normal !important; }
    .ar-modal-dialog input:not([type="checkbox"]):not([type="range"]),
    .ar-modal-dialog textarea, .ar-modal-dialog select,
    .ar-wrap .ar-form-input, .ar-wrap .ar-form-textarea, .ar-wrap .ar-form-select,
    .ar-product-fcn-select .ar-search-input { font-size: 16px; max-width: 100%; min-width: 0; }
    .ar-product-fcn-select .fcn-select-option { min-height: 44px; }
    .ar-product-fcn-select .ar-opt-name { min-width: 0; white-space: normal; overflow-wrap: anywhere; }
    .ar-save-notification { max-width: calc(100% - 24px); bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }
}

/* Stage 2: MacBook-sized laptop viewports (also applies to Windows laptops).
   Larger desktop viewports, phones and tablets are excluded. */
@media (min-width: 1025px) and (max-width: 1728px) and (hover: hover) and (pointer: fine) {
    .ar-page-head { flex-wrap: wrap !important; gap: 12px !important; }
    .ar-page-subtitle { flex: 1 1 100% !important; order: 3; line-height: 1.5; }
    .ar-segmented { flex-shrink: 0; }
    .ar-nav-tab { padding: 10px 12px; flex-shrink: 0; }
    #tab-knowledge .ar-table th:first-child { width: 110px !important; }
    #tab-knowledge .ar-table th:last-child { width: 30% !important; }
    #ar-questions-table-container .ar-table { min-width: 900px; }
    #ar-products-table-container .ar-table { min-width: 640px; }
    .ar-token-banner { flex-wrap: wrap; }
    .ar-token-banner-content { flex-basis: 280px !important; }
    .ar-token-banner-action { margin-left: auto; }
}

@media (min-width: 1025px) and (max-width: 1512px) and (hover: hover) and (pointer: fine) {
    .ozon-right-rail {
        flex-basis: 280px !important;
        width: 280px !important;
        max-width: 280px !important;
    }
    .ar-stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .ar-setting-row:has(.ar-threshold-control) { flex-direction: column; align-items: stretch; gap: 16px; }
    .ar-setting-row:has(.ar-threshold-control) .ar-setting-control { margin-left: 0; }
    .ar-threshold-control { width: 100%; }
    .ar-stop-topics-grid { grid-template-columns: minmax(0, 1fr); }
    .ar-setting-label { flex-wrap: wrap; }
}

/* Stage 3: tablets in portrait and landscape. Large touch-only screens
   are deliberately separated from the fine-pointer laptop rules. */
@media (min-width: 768px) and (max-width: 1024px),
       (min-width: 1025px) and (max-width: 1366px) and (hover: none) and (pointer: coarse) {
    .ozon-page-layout { flex-direction: column; gap: 20px; }
    .ozon-products-content { width: 100%; max-width: 100%; }
    .ozon-right-rail {
        position: static;
        flex: 1 1 auto !important;
        width: 100% !important;
        max-width: 100% !important;
        padding-top: 0;
    }
    .ozon-right-rail .notification-shell { width: 100% !important; max-width: 100% !important; }
    .ar-board-wrap { padding-top: 0; }
    .ar-board-title { position: static; transform: none; font-size: 42px; margin: 0 0 14px; }
    .ar-shell { padding: 20px 16px 16px; border-radius: 24px; }
    .ar-section { padding: 20px 16px; }
    .ar-page-head { flex-wrap: wrap !important; gap: 12px !important; }
    .ar-page-subtitle { flex: 1 1 100% !important; order: 3; line-height: 1.5; }
    .ar-segmented { min-width: 0; flex-shrink: 0; }
    .ar-stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
    .ar-stat-card, .ar-setting-info, .ar-form-group, .ar-modal-col { min-width: 0; }
    .ar-stat-status-text { white-space: normal; }
    .ar-nav-tabs { flex-wrap: wrap; }
    .ar-nav-tab { flex: 1 1 40%; justify-content: center; white-space: normal; text-align: center; }
    .ar-setting-label { flex-wrap: wrap; }
    .ar-setting-row:has(.ar-threshold-control) { flex-direction: column; align-items: stretch; gap: 14px; }
    .ar-setting-row:has(.ar-threshold-control) .ar-setting-control { margin-left: 0; }
    .ar-threshold-control { width: 100%; }
    .ar-stop-topics-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)); }
    .ar-token-banner { flex-wrap: wrap; }
    .ar-token-banner-content { min-width: 0; flex-basis: 280px !important; }
    .ar-token-banner-action { margin-left: auto; }
    .ar-tpl-header-left, .ar-tpl-badges, .ar-tpl-actions, .ar-tpl-inline-form { flex-wrap: wrap; }
    .ar-tpl-inline-form input { min-width: 0; }
    #tab-knowledge .ar-table th:first-child { width: 110px !important; }
    #tab-knowledge .ar-table th:last-child { width: 30% !important; }
    #ar-products-table-container .ar-table { min-width: 640px; }
    #ar-questions-table-container .ar-table { min-width: 900px; }
    .ar-btn, .ar-btn-sm, .ar-btn-xs, .ar-nav-tab, .ar-filter-tab,
    .ar-segmented-btn, .ar-kb-mode-btn, .ar-chip-btn, .aq-page-btn { min-height: 44px; }
    .ar-wrap .ar-btn { height: auto !important; white-space: normal !important; }
    .ar-wrap .btn-delete-product { width: 44px !important; height: 44px !important; }
    .ar-modal-close, .ar-confirm-close-btn { min-width: 44px; min-height: 44px; }
    .ar-modal-dialog { max-height: calc(100vh - 40px); max-height: calc(100dvh - 40px); }
    .ar-modal-body { min-height: 0; overscroll-behavior-y: contain; }
    .ar-modal-footer, .ar-modal-header { flex-shrink: 0; }
    .ar-modal-grid { grid-template-columns: minmax(0, 1fr); }
    .ar-wrap .ar-form-input, .ar-wrap .ar-form-select, .ar-wrap .ar-form-textarea,
    .ar-modal-dialog input:not([type="checkbox"]):not([type="range"]),
    .ar-modal-dialog textarea, .ar-product-fcn-select .ar-search-input { font-size: 16px; min-width: 0; max-width: 100%; }
    .ar-product-fcn-select .fcn-select-option { min-height: 44px; }
}
