/* Телефон: шапка, вкладки, выбор магазина, вход, настройки, общие таблицы и окна.
   Только правила внутри @media (max-width: 640px): компьютерный вид не меняется. */
@media (max-width: 640px) {
  /* ---------- Каркас страницы ----------
     Поля страницы 16px, рамки разделов тоньше: на 360 вложенные .panel и
     .panelblock съедали ~140px ширины. Значения общие для всех вкладок. */
  main { padding: 0 max(16px, env(safe-area-inset-left)) calc(32px + env(safe-area-inset-bottom))
                 max(16px, env(safe-area-inset-right)); }
  .glow { padding: 14px 0; }
  .panel { padding: 8px; border-radius: var(--r-block); }
  .panelblock { padding: 16px 12px 18px; border-radius: 14px; }
  .pagehead { margin-bottom: 18px; }

  /* ---------- Шапка: логотип и магазин в одну строку, вкладки — второй ---------- */
  header { padding: calc(14px + env(safe-area-inset-top)) 0 6px; }
  .headinner { position: relative; flex-wrap: wrap; gap: 12px 8px;
               padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left)); }
  .brand { flex: none; margin-right: 0; gap: 8px; }
  .brand-mark { width: 22px; height: 22px; border-width: 3px; }
  .brand-name { font-size: 15px; letter-spacing: .08em; }
  /* Правая группа берёт остаток первой строки по нулевой основе: иначе
     длинное имя магазина переносило бы её на отдельную строку. */
  .headspacer { display: none; }
  .headright { flex: 1 1 0; min-width: 0; gap: 8px; justify-content: flex-end; }
  /* Выход есть в настройках: в шапке на телефоне ему нет места рядом с магазином. */
  .headright #logout { display: none; }

  /* Магазин: длинное название режется многоточием, капсула не выталкивает
     шестерёнку за край. */
  .shoppicker { position: static; flex: 0 1 auto; min-width: 0; }
  .shopbtn { max-width: 100%; min-width: 0; padding: 0 14px; gap: 8px; }
  #shop-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .shopbtn svg { flex: none; }
  /* Список магазинов — во всю ширину под первой строкой шапки: у правого края
     обычный выпадающий список вылезал бы за экран. */
  .shopmenu { left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right));
              top: 46px; min-width: 0; max-height: min(64vh, 460px); overflow-y: auto;
              overscroll-behavior: contain; }
  .shopitem { min-height: 44px; padding: 11px 14px; white-space: normal; overflow-wrap: anywhere;
              font-size: 14px; line-height: 1.3; }
  .shopitem.add { min-height: 46px; }

  .headright .gearbtn { width: 40px; height: 40px; }

  /* Вкладки: одна строка, листается пальцем; выбранная прокручивается в
     видимую часть (01-core.js). Полоса прокрутки не нужна — край обрезанной
     вкладки сам говорит, что дальше есть ещё. */
  .headinner .tabs { order: 3; flex: 1 1 100%; max-width: 100%; margin: 0;
                     overflow-x: auto; overscroll-behavior-x: contain;
                     scrollbar-width: none; -webkit-overflow-scrolling: touch;
                     scroll-padding: 0 5px; }
  .headinner .tabs::-webkit-scrollbar { display: none; }
  .headinner .tabs a { flex: none; white-space: nowrap; padding: 11px 16px; font-size: 14px;
                       display: inline-flex; align-items: center; min-height: 40px; }

  /* ---------- Вход ---------- */
  .login-overlay { padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
  .login-card { padding: 24px 20px; }
  .login-card input, .passfield input { font-size: 16px; }
  .login-card .passfield button { min-height: 40px; margin: 0; padding: 0 2px; }
  .login-card button[type="submit"] { min-height: 48px; }

  /* ---------- Тосты, полоска загрузки, крестик поля ---------- */
  #toasts { bottom: calc(14px + env(safe-area-inset-bottom)); width: calc(100vw - 24px); }
  .toast { padding: 12px 46px 12px 14px; overflow-wrap: anywhere; }
  .toast .x { top: 2px; right: 2px; width: 40px; height: 40px; padding: 0; font-size: 20px; }
  #busy-bar { top: env(safe-area-inset-top); }
  /* Крестик «стереть» виден прежним, а ловится пальцем по зоне 40px. */
  .field-clear::before { content: ""; position: absolute; inset: -11px; }

  /* Подсказки наведением: крупное фото не нужно — касание фото и так ведёт на
     карточку Ozon; подсказка «Отключить магазин» дублирует текст окна. */
  .photo-peek { display: none !important; }
  .setfoot .tip { display: none; }
  .spark-tip { white-space: normal; max-width: calc(100vw - 24px); }

  /* ---------- Пустые состояния ---------- */
  .emptystate { padding: 28px 18px; gap: 12px; }
  .emptystate-title { font-size: 20px; }
  .emptystate-sub { font-size: 14px; }
  .empty { padding: 16px 8px; }

  /* ---------- Общие таблицы: база для всех разделов ----------
     Поля ячеек уже, чтобы на экран помещались числа; разделы задают своё
     поверх (их файлы грузятся позже). */
  th, td { padding: 9px 10px; }
  th { padding: 0 10px 6px; }
  .hist th { padding: 0 10px 6px; }
  .hist td { padding: 9px 10px; }
  .sortable, .sqtable th { -webkit-tap-highlight-color: transparent; }

  /* ---------- Окна: почти на весь экран, содержимое прокручивается ---------- */
  .modal, .sheet { padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
                   overscroll-behavior: contain; }
  .modal-card { width: 100%; max-width: 100%; max-height: calc(100dvh - 24px);
                overflow-y: auto; overscroll-behavior: contain;
                padding: 20px 16px 16px; border-radius: 20px; }
  .modal-title { font-size: 18px; margin-bottom: 16px; overflow-wrap: anywhere; }
  /* Кнопки окна прилипают к нижнему краю: длинное окно не прячет «Закрыть». */
  .modal-card > .modal-actions:last-child,
  .modal-card > .modal-actions:nth-last-child(2):has(+ .login-err:empty) {
    position: sticky; bottom: -16px; z-index: 2; margin: 0 -16px -16px;
    padding: 12px 16px 16px; background: var(--surface); flex-wrap: wrap; }
  .modal-actions { flex-wrap: wrap; }
  .modal-actions > button, .modal-actions > a { flex: 1 1 0; min-height: 44px; }
  .fcontrol, .modal-card input, .modal-card select, .modal-card textarea { font-size: 16px; }

  .sheet-inner { width: 100%; max-height: calc(100dvh - 24px); padding: 16px; border-radius: 18px;
                 overscroll-behavior: contain; }
  .sheet.wide .sheet-inner { width: 100%; max-height: calc(100dvh - 24px); }
  /* Заголовок с крестиком держится наверху, пока содержимое листается. */
  .sheet-head { position: sticky; top: -16px; z-index: 3; margin: -16px -16px 0;
                padding: 10px 6px 6px 16px; background: var(--surface); gap: 8px; }
  .sheet-head h3 { margin: 6px 0; font-size: 17px; overflow-wrap: anywhere; }
  .sheet-head button { width: 44px; height: 44px; padding: 0; flex: none; }
  .sheet textarea { font-size: 16px; }
  .sheet-actions > button { flex: 1 1 0; min-height: 44px; }
  .noteitem { padding: 8px 4px 8px 10px; }
  .noteitem .drop { min-width: 40px; min-height: 40px; }

  /* Подтверждение ask() и окна на его основе */
  .confirmwrap { padding: max(16px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom)); }
  .confirmbox { padding: 22px 18px 18px; border-radius: 22px; max-height: calc(100dvh - 32px);
                overflow-y: auto; overscroll-behavior: contain; }
  .confirmbox .ctitle { font-size: 19px; overflow-wrap: anywhere; }
  .confirmacts { gap: 8px; }
  .confirmacts button { flex: 1 1 0; height: auto; min-height: 46px; padding-left: 14px; padding-right: 14px; }
  .confirmbox input, .cityfind { font-size: 16px; min-height: 44px; }

  /* Окно правил сбора позиций */
  .rules-lead, .rules-note { font-size: 13.5px; }

  /* ---------- Настройки ---------- */
  #settings { padding-top: 10px; }
  #settings > .cardbar { flex-wrap: wrap; gap: 8px 10px; margin-bottom: 14px; }
  #settings-back { white-space: nowrap; min-height: 44px; padding: 0 18px; }
  #settings > .cardbar .spacer { flex: 1 1 0; min-width: 0; }
  #account-name { height: 28px; }
  #logout-settings { min-height: 44px; padding: 0 12px; margin-left: auto; }

  .setshop { flex-direction: column; align-items: stretch; gap: 14px; padding: 16px 14px; border-radius: 16px; }
  .setshop .sep { display: none; }
  .nameedit { width: 100%; font-size: 17px; padding: 4px 0; border-bottom-color: var(--line); }

  .setgroup { padding: 16px 14px 6px; border-radius: 16px; }
  .setgroup:last-of-type { padding-bottom: 14px; }
  .setrow { flex-wrap: wrap; gap: 10px 12px; padding: 14px 0; }
  .setrow-label { width: auto; flex: 1 1 100%; min-width: 0; }
  /* Строка с одним тумблером: подпись слева, тумблер справа в той же строке. */
  .setrow:has(> .toggle) .setrow-label,
  .setrow:has(> #acc-add-toggle) .setrow-label,
  .setrow:has(> .actthr) .setrow-label { flex: 1 1 0; }
  .setrow .spacer { display: none; }
  .setrow .mask { flex: 1 1 100%; min-width: 0; overflow-wrap: anywhere; line-height: 1.5; }
  .setrow .mask:empty { display: none; }
  .setrow > .keychip { margin-right: auto; }
  .setrow > button:not(.toggle) { min-height: 44px; }
  .setrow form { flex: 1 1 100%; }
  .setrow input { height: 44px; }
  .setsub { overflow-wrap: anywhere; }

  .keyform { flex-wrap: wrap; gap: 8px; }
  .keyform input { flex: 1 1 160px; font-size: 16px; min-height: 44px; }
  .keyform button { min-height: 44px; }
  .keyform.wrap input { flex: 1 1 100%; }
  .keyform.wrap button { flex: 1 1 0; }

  .loginlist { margin-top: 0; }
  .loginrow { flex-wrap: wrap; gap: 6px 10px; padding: 8px 8px 8px 12px; overflow-wrap: anywhere; }
  .loginrow button { min-height: 40px; margin-left: auto; }

  /* Аккаунты парсера: таблица в пять колонок — карточкой на аккаунт. */
  #add-account { flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
  #add-account input { flex: 1 1 100% !important; font-size: 16px; min-height: 44px; }
  #add-account button { flex: 1 1 100%; min-height: 44px; }
  #accounts, #accounts tbody { display: block; width: 100%; }
  #accounts thead { display: none; }
  #accounts tbody tr { display: grid; grid-template-columns: minmax(0, 1fr) auto;
                       grid-template-areas: "name name" "state on" "phone on" "acts acts";
                       gap: 6px 12px; align-items: center; margin-bottom: 6px;
                       padding: 12px 12px 6px 14px; border-radius: var(--r-row); background: var(--card); }
  #accounts tbody tr td, #accounts tbody tr td:first-child, #accounts tbody tr td:last-child {
    display: block; width: auto; padding: 0; background: none; border-radius: 0; font-size: 14px; }
  #accounts td:nth-child(1) { grid-area: name; overflow-wrap: anywhere; font-size: 15px; }
  #accounts td:nth-child(2) { grid-area: state; }
  #accounts td:nth-child(3) { grid-area: phone; color: var(--muted); font-size: 13px; }
  #accounts td:nth-child(4) { grid-area: on; }
  #accounts tbody tr td:nth-child(5) { grid-area: acts; display: flex; align-items: center; gap: 4px;
                              margin: 2px -6px 0; border-top: 1px solid var(--line); padding-top: 4px; }
  #accounts tbody tr td.empty { padding: 4px 0 10px; }
  #accounts td.actions .link { min-height: 40px; font-size: 13px; padding: 0 8px; }
  #accounts td.actions .del { min-width: 40px; min-height: 40px; margin-left: auto; }

  .setfoot { justify-content: center; padding: 6px 4px 0; }
  .setfoot .link { min-height: 40px; }
  /* Магазин внутри вкладки: список раскрывается под кнопкой, а не под шапкой. */
  .shopslot .shoppicker { position: relative; }
  .shopslot .shopmenu { left: 0; right: auto; top: calc(100% + 6px); min-width: 220px; max-width: calc(100vw - 32px); }

  .setoff { flex-direction: column; align-items: flex-start; gap: 10px; }
  .setoff button { height: auto; min-height: 40px; white-space: normal; text-align: left; }
}
